diff --git a/app/editor/menus/formatting.tsx b/app/editor/menus/formatting.tsx index 283cd9b3ee..3baf9e0044 100644 --- a/app/editor/menus/formatting.tsx +++ b/app/editor/menus/formatting.tsx @@ -241,7 +241,11 @@ export default function formattingMenuItems( shortcut: `${metaDisplay}+⌥+M`, icon: , label: isCodeBlock ? dictionary.comment : undefined, - active: isMarkActive(schema.marks.comment, { resolved: false }), + active: isMarkActive( + schema.marks.comment, + { resolved: false }, + { exact: true } + ), visible: !isMobile || !isEmpty, }, { diff --git a/shared/editor/marks/Comment.ts b/shared/editor/marks/Comment.ts index df0fc4a1d4..8b28778807 100644 --- a/shared/editor/marks/Comment.ts +++ b/shared/editor/marks/Comment.ts @@ -16,6 +16,8 @@ export default class Comment extends Mark { get schema(): MarkSpec { return { + // Allow multiple comments to overlap + excludes: "", attrs: { id: {}, userId: {}, @@ -97,9 +99,13 @@ export default class Comment extends Mark { return this.options.onCreateCommentMark ? (): Command => (state, dispatch) => { if ( - isMarkActive(state.schema.marks.comment, { - resolved: false, - })(state) + isMarkActive( + state.schema.marks.comment, + { + resolved: false, + }, + { exact: true } + )(state) ) { return false; } diff --git a/shared/editor/queries/isMarkActive.ts b/shared/editor/queries/isMarkActive.ts index 9010deb732..2969e67f0b 100644 --- a/shared/editor/queries/isMarkActive.ts +++ b/shared/editor/queries/isMarkActive.ts @@ -3,15 +3,21 @@ import { EditorState } from "prosemirror-state"; import { Primitive } from "utility-types"; import { getMarksBetween } from "./getMarksBetween"; +type Options = { + /** Only return match if the range and attrs is exact */ + exact?: boolean; +}; + /** * Checks if a mark is active in the current selection or not. * * @param type The mark type to check. * @param attrs The attributes to check. + * @param options The options to use. * @returns A function that checks if a mark is active in the current selection or not. */ export const isMarkActive = - (type: MarkType, attrs?: Record) => + (type: MarkType, attrs?: Record, options?: Options) => (state: EditorState): boolean => { if (!type) { return false; @@ -25,12 +31,16 @@ export const isMarkActive = if (!hasMark) { return false; } - if (attrs) { + if (attrs || options) { const results = getMarksBetween(from, to, state); return results.some( - ({ mark }) => + ({ mark, start, end }) => mark.type === type && - Object.keys(attrs).every((key) => mark.attrs[key] === attrs[key]) + (!attrs || + Object.keys(attrs).every( + (key) => mark.attrs[key] === attrs[key] + )) && + (!options?.exact || (start === from && end === to)) ); }