From fd1971fdabec30ea49be473c13b85c9384266962 Mon Sep 17 00:00:00 2001 From: yousefed Date: Tue, 1 Aug 2023 14:00:18 +0200 Subject: [PATCH 1/3] fix initialcontent and uniqueid --- packages/core/src/BlockNoteEditor.ts | 36 ++++++++++---- .../core/src/extensions/UniqueID/UniqueID.ts | 48 +++++++++---------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/packages/core/src/BlockNoteEditor.ts b/packages/core/src/BlockNoteEditor.ts index 25c2323cbb..524967c824 100644 --- a/packages/core/src/BlockNoteEditor.ts +++ b/packages/core/src/BlockNoteEditor.ts @@ -16,7 +16,10 @@ import { blocksToMarkdown, markdownToBlocks, } from "./api/formatConversions/formatConversions"; -import { nodeToBlock } from "./api/nodeConversions/nodeConversions"; +import { + blockToNode, + nodeToBlock, +} from "./api/nodeConversions/nodeConversions"; import { getNodeById } from "./api/util/nodeUtil"; import styles from "./editor.module.css"; import { @@ -44,6 +47,7 @@ import { SideMenuProsemirrorPlugin } from "./extensions/SideMenu/SideMenuPlugin" import { BaseSlashMenuItem } from "./extensions/SlashMenu/BaseSlashMenuItem"; import { SlashMenuProsemirrorPlugin } from "./extensions/SlashMenu/SlashMenuPlugin"; import { getDefaultSlashMenuItems } from "./extensions/SlashMenu/defaultSlashMenuItems"; +import { UniqueID } from "./extensions/UniqueID/UniqueID"; export type BlockNoteEditorOptions = { // TODO: Figure out if enableBlockNoteExtensions/disableHistoryExtension are needed and document them. @@ -201,22 +205,34 @@ export class BlockNoteEditor { this.schema = newOptions.blockSchema; + const initialContent = newOptions.initialContent || [ + { + type: "paragraph", + id: UniqueID.options.generateID(), + }, + ]; + const tiptapOptions: EditorOptions = { - // TODO: This approach to setting initial content is "cleaner" but requires the PM editor schema, which is only - // created after initializing the TipTap editor. Not sure it's feasible. - // content: - // options.initialContent && - // options.initialContent.map((block) => - // blockToNode(block, this._tiptapEditor.schema).toJSON() - // ), ...blockNoteTipTapOptions, ...newOptions._tiptapOptions, onCreate: () => { newOptions.onEditorReady?.(this); - newOptions.initialContent && - this.replaceBlocks(this.topLevelBlocks, newOptions.initialContent); this.ready = true; }, + onBeforeCreate(editor) { + // we have to set the initial content here, because now we can use the editor schema + // which has been created at this point + const schema = editor.editor.schema; + const ic = initialContent.map((block) => blockToNode(block, schema)); + + const root = schema.node( + "doc", + undefined, + schema.node("blockGroup", undefined, ic) + ); + // override the initialcontent + editor.editor.options.content = root.toJSON(); + }, onUpdate: () => { // This seems to be necessary due to a bug in TipTap: // https://github.com/ueberdosis/tiptap/issues/2583 diff --git a/packages/core/src/extensions/UniqueID/UniqueID.ts b/packages/core/src/extensions/UniqueID/UniqueID.ts index 5cb4f2da60..29b7efcc29 100644 --- a/packages/core/src/extensions/UniqueID/UniqueID.ts +++ b/packages/core/src/extensions/UniqueID/UniqueID.ts @@ -1,7 +1,6 @@ import { combineTransactionSteps, Extension, - findChildren, findChildrenInRange, getChangedRanges, } from "@tiptap/core"; @@ -88,33 +87,34 @@ const UniqueID = Extension.create({ }, // check initial content for missing ids onCreate() { + return; // Don’t do this when the collaboration extension is active // because this may update the content, so Y.js tries to merge these changes. // This leads to empty block nodes. // See: https://github.com/ueberdosis/tiptap/issues/2400 - if ( - this.editor.extensionManager.extensions.find( - (extension) => extension.name === "collaboration" - ) - ) { - return; - } - const { view, state } = this.editor; - const { tr, doc } = state; - const { types, attributeName, generateID } = this.options; - const nodesWithoutId = findChildren(doc, (node) => { - return ( - types.includes(node.type.name) && node.attrs[attributeName] === null - ); - }); - nodesWithoutId.forEach(({ node, pos }) => { - tr.setNodeMarkup(pos, undefined, { - ...node.attrs, - [attributeName]: generateID(), - }); - }); - tr.setMeta("addToHistory", false); - view.dispatch(tr); + // if ( + // this.editor.extensionManager.extensions.find( + // (extension) => extension.name === "collaboration" + // ) + // ) { + // return; + // } + // const { view, state } = this.editor; + // const { tr, doc } = state; + // const { types, attributeName, generateID } = this.options; + // const nodesWithoutId = findChildren(doc, (node) => { + // return ( + // types.includes(node.type.name) && node.attrs[attributeName] === null + // ); + // }); + // nodesWithoutId.forEach(({ node, pos }) => { + // tr.setNodeMarkup(pos, undefined, { + // ...node.attrs, + // [attributeName]: generateID(), + // }); + // }); + // tr.setMeta("addToHistory", false); + // view.dispatch(tr); }, addProseMirrorPlugins() { let dragSourceElement: any = null; From d438c9560fe03ae7edd01979d1a217b4f6d8f532 Mon Sep 17 00:00:00 2001 From: yousefed Date: Tue, 1 Aug 2023 14:35:36 +0200 Subject: [PATCH 2/3] fix --- packages/react/src/hooks/useBlockNote.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/react/src/hooks/useBlockNote.ts b/packages/react/src/hooks/useBlockNote.ts index cb5ce28faa..25996000d8 100644 --- a/packages/react/src/hooks/useBlockNote.ts +++ b/packages/react/src/hooks/useBlockNote.ts @@ -25,15 +25,16 @@ export const useBlockNote = ( options: Partial> = {}, deps: DependencyList = [] ): BlockNoteEditor => { - const editorRef = useRef>(initEditor(options)); + const editorRef = useRef>(); - useMemo(() => { + const ret = useMemo(() => { if (editorRef.current) { editorRef.current._tiptapEditor.destroy(); } editorRef.current = initEditor(options); + return editorRef.current; }, [deps, options]); //eslint-disable-line react-hooks/exhaustive-deps - return editorRef.current; + return ret; }; From 27fafc5cbd49e2a3498866afb595bb53104bd2e5 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Tue, 1 Aug 2023 15:50:34 +0200 Subject: [PATCH 3/3] Removed ready check in `BlockNoteView` --- .../core/src/extensions/UniqueID/UniqueID.ts | 59 +++++++++---------- packages/react/src/BlockNoteView.tsx | 39 ++++-------- 2 files changed, 39 insertions(+), 59 deletions(-) diff --git a/packages/core/src/extensions/UniqueID/UniqueID.ts b/packages/core/src/extensions/UniqueID/UniqueID.ts index 29b7efcc29..9250545cb1 100644 --- a/packages/core/src/extensions/UniqueID/UniqueID.ts +++ b/packages/core/src/extensions/UniqueID/UniqueID.ts @@ -86,36 +86,35 @@ const UniqueID = Extension.create({ ]; }, // check initial content for missing ids - onCreate() { - return; - // Don’t do this when the collaboration extension is active - // because this may update the content, so Y.js tries to merge these changes. - // This leads to empty block nodes. - // See: https://github.com/ueberdosis/tiptap/issues/2400 - // if ( - // this.editor.extensionManager.extensions.find( - // (extension) => extension.name === "collaboration" - // ) - // ) { - // return; - // } - // const { view, state } = this.editor; - // const { tr, doc } = state; - // const { types, attributeName, generateID } = this.options; - // const nodesWithoutId = findChildren(doc, (node) => { - // return ( - // types.includes(node.type.name) && node.attrs[attributeName] === null - // ); - // }); - // nodesWithoutId.forEach(({ node, pos }) => { - // tr.setNodeMarkup(pos, undefined, { - // ...node.attrs, - // [attributeName]: generateID(), - // }); - // }); - // tr.setMeta("addToHistory", false); - // view.dispatch(tr); - }, + // onCreate() { + // // Don’t do this when the collaboration extension is active + // // because this may update the content, so Y.js tries to merge these changes. + // // This leads to empty block nodes. + // // See: https://github.com/ueberdosis/tiptap/issues/2400 + // if ( + // this.editor.extensionManager.extensions.find( + // (extension) => extension.name === "collaboration" + // ) + // ) { + // return; + // } + // const { view, state } = this.editor; + // const { tr, doc } = state; + // const { types, attributeName, generateID } = this.options; + // const nodesWithoutId = findChildren(doc, (node) => { + // return ( + // types.includes(node.type.name) && node.attrs[attributeName] === null + // ); + // }); + // nodesWithoutId.forEach(({ node, pos }) => { + // tr.setNodeMarkup(pos, undefined, { + // ...node.attrs, + // [attributeName]: generateID(), + // }); + // }); + // tr.setMeta("addToHistory", false); + // view.dispatch(tr); + // }, addProseMirrorPlugins() { let dragSourceElement: any = null; let transformPasted = false; diff --git a/packages/react/src/BlockNoteView.tsx b/packages/react/src/BlockNoteView.tsx index 90b115f5ec..6e237ad1c1 100644 --- a/packages/react/src/BlockNoteView.tsx +++ b/packages/react/src/BlockNoteView.tsx @@ -1,7 +1,7 @@ import { BlockNoteEditor, BlockSchema } from "@blocknote/core"; import { MantineProvider } from "@mantine/core"; import { EditorContent } from "@tiptap/react"; -import { ReactNode, useEffect, useState } from "react"; +import { HTMLAttributes, ReactNode } from "react"; import { getBlockNoteTheme } from "./BlockNoteTheme"; import { FormattingToolbarPositioner } from "./FormattingToolbar/components/FormattingToolbarPositioner"; import { HyperlinkToolbarPositioner } from "./HyperlinkToolbar/components/HyperlinkToolbarPositioner"; @@ -12,40 +12,21 @@ export function BlockNoteView( props: { editor: BlockNoteEditor; children?: ReactNode; - } & React.HTMLAttributes + } & HTMLAttributes ) { const { editor, children, ...rest } = props; - const [ready, setReady] = useState(false); - - useEffect(() => { - function checkReady() { - if (!props.editor.ready) { - window.setTimeout(checkReady, 100); - } else { - setReady(true); - } - } - checkReady(); - - // TODO: Shouldn't this work with props.editor.ready in deps? It fails in - // tests so it doesn't seem to. - // if (props.editor.ready) { - // setReady(true); - // } - }, [props.editor.ready]); return ( - {ready && - (props.children || ( - <> - - - - - - ))} + {props.children || ( + <> + + + + + + )} );