Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(md): keep draft comment marks out of the shared document (#6880)

gbirmanf7548ddc799f
diff --git a/apps/web/src/features/block-md/comments/draftCommentMarkSync.test.ts b/apps/web/src/features/block-md/comments/draftCommentMarkSync.test.ts
new file mode 100644
index 0000000000..57edf3f4e8
--- /dev/null
+++ b/apps/web/src/features/block-md/comments/draftCommentMarkSync.test.ts
@@ -0,0 +1,350 @@
+import { $reconcileLexicalState } from '@core/component/LexicalMarkdown/collaboration/reconcile';
+import {
+ CREATE_DRAFT_COMMENT_COMMAND,
+ commentPlugin,
+} from '@core/component/LexicalMarkdown/plugins/comments/commentPlugin';
+import {
+ getSaveState,
+ initializeEditorWithState,
+ loroSyncState,
+} from '@core/component/LexicalMarkdown/utils';
+import {
+ $createCommentNode,
+ $isCommentNode,
+ $updateAllNodeIds,
+ CommentNode,
+ type NodeIdMappings,
+ stripDraftCommentMarks,
+} from '@macro-inc/lexical-core';
+import {
+ $createParagraphNode,
+ $createRangeSelection,
+ $createTextNode,
+ $getNodeByKey,
+ $getRoot,
+ $isElementNode,
+ $setSelection,
+ createEditor,
+ type LexicalEditor,
+ type LexicalNode,
+ type SerializedEditorState,
+ type SerializedLexicalNode,
+} from 'lexical';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+// The editor utilities pull in the plugin barrel, whose leaves open the
+// storage and connection-gateway sockets on import.
+vi.mock('@service-storage/websocket', () => ({
+ storageWS: { reconnectIfDisconnected: vi.fn() },
+ createWebSocketJob: vi.fn(),
+}));
+vi.mock('@service-connection/websocket', () => ({
+ ws: { addEventListener: vi.fn(), send: vi.fn() },
+ state: () => 'closed',
+ createConnectionBlockWebsocketEffect: vi.fn(),
+ createConnectionWebsocketEffect: vi.fn(),
+}));
+
+const disposers: (() => void)[] = [];
+afterEach(() => {
+ for (const dispose of disposers.splice(0)) dispose();
+});
+
+function setup() {
+ const editor = createEditor({
+ namespace: 'draft-comment-mark-sync',
+ nodes: [CommentNode],
+ onError: (error) => {
+ throw error;
+ },
+ });
+ const el = document.createElement('div');
+ document.body.append(el);
+ editor.setRootElement(el);
+ const mappings: NodeIdMappings = {
+ idToNodeKeyMap: new Map(),
+ nodeKeyToIdMap: new Map(),
+ };
+ disposers.push(
+ commentPlugin({
+ peerId: () => '1',
+ ops: {
+ add: () => {},
+ init: () => {},
+ setActiveIds: () => {},
+ remove: () => {},
+ },
+ })(editor)
+ );
+ disposers.push(() => {
+ editor.setRootElement(null);
+ el.remove();
+ });
+ return { editor, mappings };
+}
+
+/** Two paragraphs with a draft mark over "world" in the first. */
+function seedWithDraft(editor: LexicalEditor, mappings: NodeIdMappings) {
+ editor.update(
+ () => {
+ const text = $createTextNode('hello world');
+ $getRoot().append(
+ $createParagraphNode().append(text),
+ $createParagraphNode().append($createTextNode('second'))
+ );
+ const selection = $createRangeSelection();
+ selection.anchor.set(text.getKey(), 6, 'text');
+ selection.focus.set(text.getKey(), 11, 'text');
+ $setSelection(selection);
+ },
+ { discrete: true }
+ );
+ editor.dispatchCommand(CREATE_DRAFT_COMMENT_COMMAND, undefined);
+ editor.update(() => $updateAllNodeIds(mappings), { discrete: true });
+ return draftKeys(editor)[0];
+}
+
+function draftKeys(editor: LexicalEditor) {
+ return editor.read(() =>
+ allNodes()
+ .filter((node) => $isCommentNode(node) && node.getIsDraft())
+ .map((node) => node.getKey())
+ );
+}
+
+function draftText(editor: LexicalEditor, key: string) {
+ return editor.read(() => $getNodeByKey(key)?.getTextContent());
+}
+
+function allNodes(node: LexicalNode = $getRoot()): LexicalNode[] {
+ return $isElementNode(node)
+ ? [node, ...node.getChildren().flatMap((child) => allNodes(child))]
+ : [node];
+}
+
+function markTypes(node: SerializedLexicalNode): string[] {
+ const children =
+ 'children' in node && Array.isArray(node.children)
+ ? (node.children as SerializedLexicalNode[])
+ : [];
+ return [node.type, ...children.flatMap(markTypes)].filter(
+ (type) => type === 'comment-mark'
+ );
+}
+
+function paragraphText(editor: LexicalEditor) {
+ return editor.read(() =>
+ $getRoot()
+ .getChildren()
+ .map((block) => block.getTextContent())
+ );
+}
+
+/** Applies a remote state the way the collab provider does. */
+function applyRemote(
+ editor: LexicalEditor,
+ mappings: NodeIdMappings,
+ remote: SerializedEditorState
+) {
+ editor.update(
+ () => {
+ $reconcileLexicalState(
+ editor.getEditorState().toJSON(),
+ remote,
+ mappings,
+ () => '1'
+ );
+ },
+ { discrete: true }
+ );
+}
+
+/** The shared state as a peer would see it, with `edit` applied to its first paragraph. */
+function remoteState(
+ editor: LexicalEditor,
+ edit: (children: SerializedLexicalNode[]) => SerializedLexicalNode[]
+): SerializedEditorState {
+ const state = structuredClone(loroSyncState(editor.getEditorState()));
+ const first = state.root.children[0] as SerializedLexicalNode & {
+ children: SerializedLexicalNode[];
+ };
+ first.children = edit(first.children);
+ return state;
+}
+
+describe('draft comment marks stay out of the shared document', () => {
+ it('leaves the draft out of the synced and saved state but keeps its text', () => {
+ const { editor, mappings } = setup();
+ seedWithDraft(editor, mappings);
+ expect(draftKeys(editor)).toHaveLength(1);
+
+ for (const state of [
+ loroSyncState(editor.getEditorState()),
+ getSaveState(editor.getEditorState()),
+ ]) {
+ expect(markTypes(state.root)).toEqual([]);
+ expect(JSON.stringify(state)).toContain('world');
+ }
+ });
+
+ it('keeps committed comment marks in the synced state', () => {
+ const { editor } = setup();
+ editor.update(
+ () => {
+ $getRoot().append(
+ $createParagraphNode().append(
+ $createCommentNode({ ids: ['posted'] }).append(
+ $createTextNode('posted')
+ )
+ )
+ );
+ },
+ { discrete: true }
+ );
+ expect(markTypes(loroSyncState(editor.getEditorState()).root)).toEqual([
+ 'comment-mark',
+ ]);
+ });
+
+ it('returns the same state object when there is no draft to strip', () => {
+ const { editor } = setup();
+ editor.update(
+ () => {
+ $getRoot().append(
+ $createParagraphNode().append($createTextNode('plain'))
+ );
+ },
+ { discrete: true }
+ );
+ const state = editor.getEditorState().toJSON();
+ expect(stripDraftCommentMarks(state)).toBe(state);
+ });
+
+ it('keeps the local draft mark node through a remote edit elsewhere', () => {
+ const { editor, mappings } = setup();
+ const key = seedWithDraft(editor, mappings);
+
+ const remote = structuredClone(loroSyncState(editor.getEditorState()));
+ const second = remote.root.children[1] as SerializedLexicalNode & {
+ children: (SerializedLexicalNode & { text: string })[];
+ };
+ second.children[0].text = 'second, edited remotely';
+ applyRemote(editor, mappings, remote);
+
+ expect(draftKeys(editor)).toEqual([key]);
+ expect(draftText(editor, key)).toBe('world');
+ expect(paragraphText(editor)).toEqual([
+ 'hello world',
+ 'second, edited remotely',
+ ]);
+ });
+
+ it('keeps the draft over its text when a peer edits that text', () => {
+ const { editor, mappings } = setup();
+ const key = seedWithDraft(editor, mappings);
+
+ const remote = remoteState(editor, (children) =>
+ children.map((child, index) =>
+ index === 1 ? { ...child, text: 'world!' } : child
+ )
+ );
+ applyRemote(editor, mappings, remote);
+
+ expect(draftKeys(editor)).toEqual([key]);
+ expect(draftText(editor, key)).toBe('world!');
+ expect(paragraphText(editor)[0]).toBe('hello world!');
+ });
+
+ it('drops the draft when a peer deletes all of its text', () => {
+ const { editor, mappings } = setup();
+ seedWithDraft(editor, mappings);
+
+ const remote = remoteState(editor, (children) => children.slice(0, 1));
+ applyRemote(editor, mappings, remote);
+
+ expect(draftKeys(editor)).toEqual([]);
+ expect(paragraphText(editor)[0]).toBe('hello ');
+ });
+
+ it('strips drafts that a previous session left in the shared state', () => {
+ const { editor, mappings } = setup();
+ editor.update(
+ () => {
+ $getRoot().append(
+ $createParagraphNode().append($createTextNode('left behind'))
+ );
+ $updateAllNodeIds(mappings);
+ },
+ { discrete: true }
+ );
+
+ const remote = remoteState(editor, (children) => [
+ {
+ type: 'comment-mark',
+ version: 1,
+ format: '',
+ indent: 0,
+ direction: null,
+ ids: ['stale'],
+ threadId: -1,
+ isDraft: true,
+ children,
+ } as SerializedLexicalNode,
+ ]);
+ applyRemote(editor, mappings, remote);
+
+ expect(draftKeys(editor)).toEqual([]);
+ expect(paragraphText(editor)).toEqual(['left behind']);
+ });
+
+ it('does not load drafts saved in a document', () => {
+ const { editor } = setup();
+ const saved: SerializedEditorState = {
+ root: {
+ type: 'root',
+ version: 1,
+ format: '',
+ indent: 0,
+ direction: null,
+ children: [
+ {
+ type: 'paragraph',
+ version: 1,
+ format: '',
+ indent: 0,
+ direction: null,
+ textFormat: 0,
+ textStyle: '',
+ children: [
+ {
+ type: 'comment-mark',
+ version: 1,
+ format: '',
+ indent: 0,
+ direction: null,
+ ids: ['stale'],
+ threadId: -1,
+ isDraft: true,
+ children: [
+ {
+ type: 'text',
+ version: 1,
+ text: 'saved draft',
+ format: 0,
+ detail: 0,
+ mode: 'normal',
+ style: '',
+ } as SerializedLexicalNode,
+ ],
+ } as SerializedLexicalNode,
+ ],
+ } as SerializedLexicalNode,
+ ],
+ },
+ } as SerializedEditorState;
+ initializeEditorWithState(editor, saved);
+
+ expect(draftKeys(editor)).toEqual([]);
+ expect(paragraphText(editor)).toEqual(['saved draft']);
+ });
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx
index cafd47ff3f..7efab85eee 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx
@@ -43,6 +43,7 @@ import {
type NodeIdMappings,
SKIP_DOM_SELECTION_TAG,
SKIP_SCROLL_INTO_VIEW_TAG,
+ stripDraftCommentMarks,
} from '@macro-inc/lexical-core';
import type { Span } from '@macro-inc/observability';
import type { NodeKey, UpdateListenerPayload } from 'lexical';
@@ -507,6 +508,8 @@ export function CollabProvider(props: CollabProviderProps) {
// Indicate that we have completed the first sync
setDidFirstSync(true);
+ let hasAbandonedDrafts = false;
+
// Initialize the editor with the initial state from the sync service
if (empty) {
logSyncService({
@@ -523,9 +526,13 @@ export function CollabProvider(props: CollabProviderProps) {
context: {},
message: 'editor init: versioned state',
});
+ const initialState =
+ state.state as unknown as SerializedEditorState;
+ const stateWithoutDrafts = stripDraftCommentMarks(initialState);
+ hasAbandonedDrafts = stateWithoutDrafts !== initialState;
const initError = initializeEditorWithVersionedState(
props.editor,
- state.state as unknown as SerializedEditorState,
+ stateWithoutDrafts,
() => loroManager.peerIdStr
);
if (initError !== null) {
@@ -544,6 +551,10 @@ export function CollabProvider(props: CollabProviderProps) {
// Start the sync engine
startSync();
+ // Remove drafts that earlier sessions left in the shared document.
+ if (hasAbandonedDrafts && !readOnly()) {
+ props.editor.dispatchCommand(FORCE_SYNC_COMMAND, undefined);
+ }
props.setEditorReady(true);
const documentId = syncSource()!.documentId;
resumeSpan(documentId)?.event('editor.ready');
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts
index 719a8c8a4f..fceb41e528 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts
@@ -1,10 +1,13 @@
import {
$getNodeById,
$getPeerId,
+ $liftDraftCommentMarks,
+ $restoreDraftCommentMarks,
$setLocal,
isSerializedImageNode,
isSerializedVideoNode,
type NodeIdMappings,
+ stripDraftCommentMarks,
} from '@macro-inc/lexical-core';
import deepEqual from 'fast-deep-equal';
import {
@@ -90,20 +93,29 @@ export function $reconcileLexicalState(
): void {
const editor = $getEditor();
+ // The local draft comment mark is never synced, so it is lifted out while
+ // the tree is compared with the shared state. Drafts that arrive in the
+ // shared state were left behind by sessions that ended mid-draft.
+ const drafts = $liftDraftCommentMarks();
+ const oldRoot = stripDraftCommentMarks(oldState).root;
+ const newRoot = stripDraftCommentMarks(newState).root;
+
const newParentMap = new Map<string, string | undefined>();
const oldParentMap = new Map<string, string | undefined>();
- buildParentMap(newState.root, undefined, newParentMap);
- buildParentMap(oldState.root, undefined, oldParentMap);
+ buildParentMap(newRoot, undefined, newParentMap);
+ buildParentMap(oldRoot, undefined, oldParentMap);
$applyChildren(
editor,
mapping,
- oldState.root,
- newState.root,
+ oldRoot,
+ newRoot,
oldParentMap,
newParentMap,
peerId
);
+
+ $restoreDraftCommentMarks(drafts);
}
// Mark incoming tracked peerId nodes with correct local status.
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/comments/commentPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/comments/commentPlugin.ts
index c2620ce5ae..7c7e4d243b 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/comments/commentPlugin.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/comments/commentPlugin.ts
@@ -115,10 +115,6 @@ export const COMMIT_COMMENT_MARK_COMMAND = createCommand<{
markId: string;
}>('COMMIT_COMMENT_MARK_COMMAND');
-const CLEANUP_COMMENTS_COMMAND = createCommand<string[]>(
- 'CLEANUP_COMMENTS_COMMAND'
-);
-
const generateMarkId = () => v7();
export const markNodeKeysToIDs: Map<NodeKey, Array<string>> = new Map();
@@ -398,14 +394,6 @@ function registerPlugin(editor: LexicalEditor, props: CommentPluginProps) {
},
COMMAND_PRIORITY_EDITOR
),
- editor.registerCommand(
- CLEANUP_COMMENTS_COMMAND,
- (payload) => {
- $disposeExternalDraftComments(payload);
- return true;
- },
- COMMAND_PRIORITY_EDITOR
- ),
// Publish mounted marks after their commands and lookup entries are ready.
editor.registerMutationListener(
@@ -470,31 +458,6 @@ export function commentPlugin(props: CommentPluginProps) {
};
}
-function _$disposeLocalDraftComments() {
- $traverseNodes($getRoot(), (node) => {
- if ($isCommentNode(node)) {
- if (node.getIsDraft() && node.getIsLocal()) {
- $unwrapMarkNode(node);
- }
- }
- });
-}
-
-function $disposeExternalDraftComments(validPeerIds: string[]) {
- $traverseNodes($getRoot(), (node) => {
- if ($isCommentNode(node)) {
- const nodePeerId = $getPeerId(node);
- if (!nodePeerId) {
- $unwrapMarkNode(node);
- return;
- }
- if (!validPeerIds.includes(nodePeerId)) {
- $unwrapMarkNode(node);
- }
- }
- });
-}
-
function $removeOrphanedCommentMarks(validMarkIds: ReadonlySet<string>) {
$traverseNodes($getRoot(), (node) => {
if (!$isCommentNode(node) || node.getIsDraft()) return;
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/utils.ts b/apps/web/src/lib/core/component/LexicalMarkdown/utils.ts
index 266012f111..d6929ce654 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/utils.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/utils.ts
@@ -27,6 +27,7 @@ import {
EXTERNAL_TRANSFORMERS,
INITIALIZE_LOCAL_STATUS,
INTERNAL_TRANSFORMERS,
+ stripDraftCommentMarks,
} from '@macro-inc/lexical-core';
import { SKIP_SCROLL_INTO_VIEW_TAG } from '@macro-inc/lexical-core/constants';
import {
@@ -171,7 +172,7 @@ export function initializeEditorWithState(
) {
if (!state || state.root.children?.length === 0) return;
try {
- const parsed = editor.parseEditorState(state);
+ const parsed = editor.parseEditorState(stripDraftCommentMarks(state));
editor.setEditorState(parsed);
editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
editor.dispatchCommand(INITIALIZE_DOCUMENT_IDS, undefined);
@@ -837,7 +838,7 @@ function transformSerializedEditorState(
* NOTE: this is no longer true for loro. but is being used for legacy DSS save.
*/
function cleanState(state: SerializedEditorState): SerializedEditorState {
- return transformSerializedEditorState(state, [
+ return transformSerializedEditorState(stripDraftCommentMarks(state), [
// custom code nodes must have no children.
(node) => {
if (node.type === 'custom-code') {
@@ -871,13 +872,16 @@ export function loroSyncState(state: EditorState): SerializedEditorState {
} else {
serializedState = state.toJSON();
}
- return transformSerializedEditorState(serializedState, [
- // completion nodes should not be saved.
- (node) => {
- if (node.type === 'completion') return null;
- return node;
- },
- ]);
+ return transformSerializedEditorState(
+ stripDraftCommentMarks(serializedState),
+ [
+ // completion nodes should not be saved.
+ (node) => {
+ if (node.type === 'completion') return null;
+ return node;
+ },
+ ]
+ );
}
function serializedSateWithSearchText(
diff --git a/packages/lexical-core/nodes/CommentNode.ts b/packages/lexical-core/nodes/CommentNode.ts
index be3c5732f6..a2640715b6 100644
--- a/packages/lexical-core/nodes/CommentNode.ts
+++ b/packages/lexical-core/nodes/CommentNode.ts
@@ -2,6 +2,7 @@ import { MarkNode, type SerializedMarkNode } from '@lexical/mark';
import { $dfs } from '@lexical/utils';
import {
$applyNodeReplacement,
+ $getNodeByKey,
$getRoot,
$isRootOrShadowRoot,
type EditorConfig,
@@ -10,6 +11,8 @@ import {
type LexicalUpdateJSON,
type NodeKey,
type RangeSelection,
+ type SerializedEditorState,
+ type SerializedLexicalNode,
type Spread,
} from 'lexical';
import { $applyIdFromSerialized } from '../plugins/nodeIdPlugin';
@@ -274,3 +277,105 @@ export class CommentNode extends MarkNode {
return node;
}
}
+
+function isSerializedDraftCommentNode(
+ node: SerializedLexicalNode
+): node is SerializedCommentNode {
+ return (
+ node.type === CommentNode.getType() &&
+ (node as SerializedCommentNode).isDraft === true
+ );
+}
+
+/**
+ * A copy of `node` with every draft comment mark replaced by its content, or
+ * `node` itself when it holds none.
+ */
+function stripDraftCommentMarksFromNode<T extends SerializedLexicalNode>(
+ node: T
+): T {
+ if (!('children' in node) || !Array.isArray(node.children)) return node;
+ let changed = false;
+ const children: SerializedLexicalNode[] = [];
+ for (const child of node.children as SerializedLexicalNode[]) {
+ const stripped = stripDraftCommentMarksFromNode(child);
+ if (isSerializedDraftCommentNode(stripped)) {
+ changed = true;
+ children.push(...stripped.children);
+ continue;
+ }
+ if (stripped !== child) changed = true;
+ children.push(stripped);
+ }
+ return changed ? { ...node, children } : node;
+}
+
+/**
+ * A draft comment mark only anchors the open composer of the editor that
+ * created it, so it must never reach the shared document: a session that
+ * ends before the comment is posted or cancelled would leave it there for
+ * good. Returns `state` itself when it holds no draft marks.
+ */
+export function stripDraftCommentMarks(
+ state: SerializedEditorState
+): SerializedEditorState {
+ const root = stripDraftCommentMarksFromNode(state.root);
+ return root === state.root ? state : { ...state, root };
+}
+
+export type LiftedDraftCommentMark = {
+ mark: CommentNode;
+ childKeys: NodeKey[];
+};
+
+/**
+ * Unwraps every draft comment mark so the live tree matches its
+ * `stripDraftCommentMarks` serialization. Pass the result to
+ * `$restoreDraftCommentMarks` in the same update.
+ */
+export function $liftDraftCommentMarks(): LiftedDraftCommentMark[] {
+ const drafts: CommentNode[] = [];
+ for (const { node } of $dfs($getRoot())) {
+ if ($isCommentNode(node) && node.getIsDraft()) drafts.push(node);
+ }
+ return drafts.map((mark) => {
+ const children = mark.getChildren();
+ for (const child of children) mark.insertBefore(child);
+ mark.remove(true);
+ return { mark, childKeys: children.map((child) => child.getKey()) };
+ });
+}
+
+/**
+ * Wraps the lifted drafts' content again, keeping each original mark node so
+ * the composer anchored to it survives. Content that is gone is left out, and
+ * content that is no longer contiguous is wrapped in one mark per run.
+ */
+export function $restoreDraftCommentMarks(
+ lifted: readonly LiftedDraftCommentMark[]
+): void {
+ // Reverse lift order, so an enclosing draft wraps an inner one already restored.
+ for (const { mark, childKeys } of [...lifted].reverse()) {
+ let run: CommentNode | null = null;
+ let reusedMark = false;
+ for (const key of childKeys) {
+ const child = $getNodeByKey(key);
+ if (!child?.isAttached()) {
+ run = null;
+ continue;
+ }
+ if (run === null || !run.getNextSibling()?.is(child)) {
+ run = reusedMark
+ ? $createCommentNode({
+ ids: mark.getIDs(),
+ threadId: mark.getThreadId(),
+ isDraft: true,
+ })
+ : mark;
+ reusedMark = true;
+ child.insertBefore(run);
+ }
+ run.append(child);
+ }
+ }
+}