Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/core/src/blocks/defaultBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InputRule, markInputRule } from "@tiptap/core";
import type { MarkType } from "@tiptap/pm/model";
import Bold from "@tiptap/extension-bold";
import Code from "@tiptap/extension-code";
import Italic from "@tiptap/extension-italic";
Expand Down Expand Up @@ -139,6 +140,23 @@ export const defaultStyleSpecs = {
strike: createStyleSpecFromTipTapMark(Strike, "boolean"),
code: createStyleSpecFromTipTapMark(
Code.extend({
onBeforeCreate() {
// By default, code marks are configured to not overlap with any other
// marks with `exclude: "_"`. However, comment marks should still be
// allowed to overlap code marks. There is unfortunately no way to make
// the `exclude` option contain all possible marks except comments, so
// we instead remove the comment mark from it in `onBeforeCreate`.
const commentType = this.editor.schema.marks.comment;
const codeType = this.type as MarkType & {
excluded?: readonly MarkType[];
};

if (commentType && codeType.excluded?.includes(commentType)) {
codeType.excluded = codeType.excluded.filter(
(markType) => markType !== commentType,
);
}
},
Comment on lines +143 to +159

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this solution, can the excluded be a function in tiptap? And we can read the editor to see the other marks that exist in the schema. It being a runtime hack isn't great, especially when we can know all of the marks that the editor has included in it

addInputRules() {
return [
// Matches any string that starts with a backtick, ends with a
Expand Down
32 changes: 31 additions & 1 deletion tests/src/end-to-end/comments/comments.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import App from "@examples/07-collaboration/09-comments-testing/src/App";
import { beforeEach, describe, expect, test, vi } from "vite-plus/test";
import { render } from "vitest-browser-react";
import { browserName, page, userEvent } from "../../utils/context.js";
import { browserName, MOD, page, userEvent } from "../../utils/context.js";
import { EDITOR_SELECTOR, LINK_BUTTON_SELECTOR } from "../../utils/const.js";
import {
expectElement,
Expand Down Expand Up @@ -133,6 +133,36 @@ describe("Check Comments functionality", () => {
).toBeVisible();
});

test("Should preserve existing comments when adding a code mark", async () => {
await focusOnEditor();

await userEvent.keyboard("hello");
await doubleClickElement(page.getByText("hello").element());

await userEvent.click(await waitForSelector('[data-test="addcomment"]'));
await waitForSelector(".bn-thread");

await userEvent.keyboard("test comment");
await userEvent.click(await waitForSelector('button[data-test="save"]'));

// Re-select the commented text and toggle inline code on it (Cmd/Ctrl+E).
await doubleClickElement(
document.querySelectorAll("span.bn-thread-mark")[0] as HTMLElement,
);
await userEvent.keyboard(`{${MOD}>}e{/${MOD}}`);

// The comment must be preserved, and the text must now also be inline code,
// i.e. the comment and code marks coexist on the same text.
await expectElement(
await waitForSelector("span.bn-thread-mark"),
).toBeVisible();
await expectElement(
await waitForSelector(
"span.bn-thread-mark code, code span.bn-thread-mark",
),
).toBeVisible();
});

test.skipIf(browserName === "webkit")(
"Should select thread on first click and open link on second click",
async () => {
Expand Down
Loading