Skip to content

Commit 5023d0e

Browse files
committed
Implemented PR feedback
1 parent 21c0866 commit 5023d0e

2 files changed

Lines changed: 70 additions & 17 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 [

0 commit comments

Comments
 (0)