Skip to content

Commit 41938f6

Browse files
matthewlipskinperez0111
authored andcommitted
Implemented PR feedback
1 parent 302c620 commit 41938f6

3 files changed

Lines changed: 72 additions & 19 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Editor, Extension, getSchema, Mark, Node } from "@tiptap/core";
2+
import { describe, expect, it } from "vite-plus/test";
3+
import { CommentMark } from "../comments/mark.js";
4+
import { defaultStyleSpecs } from "./defaultBlocks.js";
5+
6+
const extensions = [
7+
Node.create({ name: "doc", topNode: true, content: "text*" }),
8+
Node.create({ name: "text", group: "inline" }),
9+
...Object.values(defaultStyleSpecs).map((style) => style.implementation.mark),
10+
];
11+
12+
describe("inline code mark exclusions", () => {
13+
it.each([false, true])(
14+
"builds exclusions from enabled marks (comments enabled: %s)",
15+
(withComments) => {
16+
const editor = new Editor({
17+
element: null,
18+
extensions: [
19+
...extensions,
20+
Extension.create({
21+
name: "customMarks",
22+
addExtensions() {
23+
return [
24+
Mark.create({ name: "customStyle" }),
25+
...(withComments ? [CommentMark] : []),
26+
];
27+
},
28+
}),
29+
],
30+
});
31+
32+
try {
33+
const { marks } = editor.schema;
34+
const code = marks.code;
35+
expect(code.spec.excludes).not.toBe("_");
36+
for (const mark of Object.values(marks)) {
37+
expect(code.excludes(mark)).toBe(mark.name !== "comment");
38+
}
39+
40+
if (withComments) {
41+
const codeMark = code.create();
42+
const commentMark = marks.comment.create({ threadId: "thread" });
43+
expect(codeMark.addToSet([commentMark])).toHaveLength(2);
44+
expect(commentMark.addToSet([codeMark])).toHaveLength(2);
45+
}
46+
} finally {
47+
editor.destroy();
48+
}
49+
},
50+
);
51+
52+
it("supports schema creation without an editor", () => {
53+
const { marks } = getSchema(extensions);
54+
expect(marks.code.excludes(marks.bold)).toBe(true);
55+
});
56+
});

‎packages/core/src/blocks/defaultBlocks.ts‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { InputRule, markInputRule } from "@tiptap/core";
2-
import type { MarkType } from "@tiptap/pm/model";
1+
import { flattenExtensions, InputRule, markInputRule } from "@tiptap/core";
32
import Bold from "@tiptap/extension-bold";
43
import Code from "@tiptap/extension-code";
54
import Italic from "@tiptap/extension-italic";
@@ -140,22 +139,20 @@ export const defaultStyleSpecs = {
140139
strike: createStyleSpecFromTipTapMark(Strike, "boolean"),
141140
code: createStyleSpecFromTipTapMark(
142141
Code.extend({
143-
onBeforeCreate() {
144-
// By default, code marks are configured to not overlap with any other
145-
// marks with `exclude: "_"`. However, comment marks should still be
146-
// allowed to overlap code marks. There is unfortunately no way to make
147-
// the `exclude` option contain all possible marks except comments, so
148-
// we instead remove the comment mark from it in `onBeforeCreate`.
149-
const commentType = this.editor.schema.marks.comment;
150-
const codeType = this.type as MarkType & {
151-
excluded?: readonly MarkType[];
152-
};
153-
154-
if (commentType && codeType.excluded?.includes(commentType)) {
155-
codeType.excluded = codeType.excluded.filter(
156-
(markType) => markType !== commentType,
157-
);
142+
excludes() {
143+
// Exclude all enabled marks except comments when building the schema.
144+
// The extension manager is not available yet during schema creation.
145+
if (!this.editor) {
146+
return "_";
158147
}
148+
149+
return flattenExtensions(this.editor.options.extensions)
150+
.filter(
151+
(extension) =>
152+
extension.type === "mark" && extension.name !== "comment",
153+
)
154+
.map((extension) => extension.name)
155+
.join(" ");
159156
},
160157
addInputRules() {
161158
return [

‎tests/src/end-to-end/comments/comments.test.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import App from "@examples/07-collaboration/09-comments-testing/src/App";
22
import { beforeEach, describe, expect, test, vi } from "vite-plus/test";
33
import { render } from "vitest-browser-react";
4-
import { browserName, page, userEvent } from "../../utils/context.js";
4+
import { browserName, MOD, page, userEvent } from "../../utils/context.js";
55
import { EDITOR_SELECTOR, LINK_BUTTON_SELECTOR } from "../../utils/const.js";
66
import {
77
expectElement,
@@ -206,7 +206,7 @@ describe("Check Comments functionality", () => {
206206
await doubleClickElement(
207207
document.querySelectorAll("span.bn-thread-mark")[0] as HTMLElement,
208208
);
209-
await userEvent.keyboard("{ControlOrMeta>}e{/ControlOrMeta}");
209+
await userEvent.keyboard(`{${MOD}>}e{/${MOD}}`);
210210

211211
// The comment must be preserved, and the text must now also be inline code,
212212
// i.e. the comment and code marks coexist on the same text.

0 commit comments

Comments
 (0)