Skip to content

Instantly share code, notes, and snippets.

@fantactuka
Created September 19, 2022 14:51
Show Gist options
  • Select an option

  • Save fantactuka/9d459b48bd689e851ad08e5aab899671 to your computer and use it in GitHub Desktop.

Select an option

Save fantactuka/9d459b48bd689e851ad08e5aab899671 to your computer and use it in GitHub Desktop.

Revisions

  1. fantactuka revised this gist Sep 19, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion export-ydoc.js
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ export default function exportYDoc(

    applyUpdate(doc, yDocState, null);

    // Force sync update so that ydoc updates applied above are applied as well
    // Force sync update so that ydoc applied above is processed before we try exporting
    editor.update(() => {
    if (editor._pendingEditorState != null) {
    editor._pendingEditorState._flushSync = true;
  2. fantactuka created this gist Sep 19, 2022.
    75 changes: 75 additions & 0 deletions export-ydoc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    import {createEditor} from 'Lexical';
    import {createBinding, syncYjsChangesToLexical} from 'LexicalYjs';

    import {Doc, applyUpdate} from 'yjs';

    export default function exportYDoc(
    yDocState: Uint8Array,
    nodes: Array<Class<LexicalNode>>,
    ): SerializedEditorState {
    const emptyFunction = () => {};
    const provider: Provider = {
    awareness: {
    getLocalState: () => null,
    getStates: () => new Map(),
    off: emptyFunction,
    on: emptyFunction,
    setLocalState: emptyFunction,
    },
    connect: emptyFunction,
    disconnect: emptyFunction,
    off: emptyFunction,
    on: emptyFunction,
    };

    const editor = createEditor({
    namespace: 'headless',
    nodes,
    onError: error => {
    throw error;
    },
    });

    editor._headless = true;

    const id = 'main';
    const docMap = new Map();
    const doc = new Doc();
    const binding = createBinding(editor, provider, id, doc, docMap);
    const {root} = binding;
    root
    .getSharedType()
    .observeDeep((events: Array<YEvent>, transaction?: Transaction) => {
    if (transaction?.origin !== binding) {
    syncYjsChangesToLexical(binding, provider, events);
    }
    });

    applyUpdate(doc, yDocState, null);

    // Force sync update so that ydoc updates applied above are applied as well
    editor.update(() => {
    if (editor._pendingEditorState != null) {
    editor._pendingEditorState._flushSync = true;
    }
    });

    // At this point editor is populated with data from YDoc
    // and can either return lexical json, or run export to html or markdown

    const json = editor.getEditorState().toJSON();
    const html = $generateHtmlFromNodes(editor, null);
    let markdown;

    editor.update(() => {
    markdown = $convertToMarkdownString(TRANSFORMERS);
    ...
    });


    return {
    markdown,
    json,
    html,
    }
    }