Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

967 B
Raw Blame
import type { Editor, JSONContent } from '@tiptap/core';
import { undoDepth, redoDepth } from '@tiptap/pm/history';
import { Selection } from '@tiptap/pm/state';

/** Skip only an untouched standard-empty to standard-empty replacement. */
export function restoreComposerDocument(
  editor: Editor,
  content?: JSONContent | null,
  forceReset = false,
): void {
  const { state } = editor;
  const empty = state.schema.topNodeType.createAndFill();
  if (!forceReset && !editor.view.composing && empty && state.doc.eq(empty) &&
    state.selection.eq(Selection.atStart(state.doc)) && !state.storedMarks?.length &&
    undoDepth(state) === 0 && redoDepth(state) === 0) {
    if (!content) return;
    try {
      if (state.schema.nodeFromJSON(content).eq(empty)) return;
    } catch {
      // Leave malformed/legacy content handling to the existing Tiptap command.
    }
  }
  if (content) editor.commands.setContent(content);
  else editor.commands.clearContent();
}