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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import { findInlineNodes } from './documentHelpers.js';

/**
* Get track changes marks.
* @param {import('prosemirror-state').EditorState} state
* @param {string} id
*
* Tolerates a missing or partially-initialized state and returns an empty array
* instead of throwing. Comment-import bootstrap can call this through a
* setTimeout(0) before the editor's PM state is attached (SD-2641).
*
* @param {import('prosemirror-state').EditorState | null | undefined} state
* @param {string} [id]
* @returns {Array} Array with track changes marks.
*/
export const getTrackChanges = (state, id = null) => {
const trackedChanges = [];
if (!state?.doc) return trackedChanges;
const allInlineNodes = findInlineNodes(state.doc);

if (!allInlineNodes.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, test, expect } from 'vitest';
import { getTrackChanges } from './getTrackChanges.js';

// SD-2641: The helper must not throw when called before the editor's PM state
// is initialized. During DOCX comment-import bootstrap, the orchestrator schedules
// the call via setTimeout(0) which only defers to the next tick — it does not wait
// for editor.state to be attached. The helper is reused from 4 call sites in
// comments-store.js, so we harden it once at the source rather than guarding each
// caller.
describe('getTrackChanges — null-safe input handling', () => {
test('returns [] when state is undefined', () => {
expect(getTrackChanges(undefined)).toEqual([]);
});

test('returns [] when state is null', () => {
expect(getTrackChanges(null)).toEqual([]);
});

test('returns [] when state has no doc property', () => {
expect(getTrackChanges({})).toEqual([]);
});

test('returns [] when state.doc is undefined', () => {
expect(getTrackChanges({ doc: undefined })).toEqual([]);
});
});
Loading