diff --git a/package-lock.json b/package-lock.json index 0bd5014dee..7e73bcc8eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28925,4 +28925,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index e7a4205f27..a3f0111f29 100644 --- a/package.json +++ b/package.json @@ -34,4 +34,4 @@ "postpublish": "rm -rf packages/core/README.md && rm -rf packages/react/README.md", "clean": "lerna run --stream clean" } -} +} \ No newline at end of file diff --git a/packages/core/src/extensions/FilePanel/FilePanelPlugin.ts b/packages/core/src/extensions/FilePanel/FilePanelPlugin.ts index 1bdbea01ea..6ffac4a519 100644 --- a/packages/core/src/extensions/FilePanel/FilePanelPlugin.ts +++ b/packages/core/src/extensions/FilePanel/FilePanelPlugin.ts @@ -71,8 +71,10 @@ export class FilePanelView if (this.state?.show) { const blockElement = this.pmView.root.querySelector( `[data-node-type="blockContainer"][data-id="${this.state.block.id}"]` - )!; - + ); + if (!blockElement) { + return; + } this.state.referencePos = blockElement.getBoundingClientRect(); this.emitUpdate(); } @@ -86,8 +88,10 @@ export class FilePanelView if (!this.state?.show && pluginState.block && this.editor.isEditable) { const blockElement = this.pmView.root.querySelector( `[data-node-type="blockContainer"][data-id="${pluginState.block.id}"]` - )!; - + ); + if (!blockElement) { + return; + } this.state = { show: true, referencePos: blockElement.getBoundingClientRect(), @@ -157,7 +161,7 @@ export class FilePanelProsemirrorPlugin< props: { handleKeyDown: (_view, event: KeyboardEvent) => { if (event.key === "Escape" && this.shown) { - this.view!.closeMenu(); + this.view?.closeMenu(); return true; } return false; @@ -189,5 +193,5 @@ export class FilePanelProsemirrorPlugin< return this.on("update", callback); } - public closeMenu = () => this.view!.closeMenu(); + public closeMenu = () => this.view?.closeMenu(); } diff --git a/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts b/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts index 1a6d59515e..ad5fe52776 100644 --- a/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts +++ b/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts @@ -205,7 +205,6 @@ export class FormattingToolbarView implements PluginView { if (isNodeSelection(selection)) { const node = this.pmView.nodeDOM(from) as HTMLElement; - if (node) { return node.getBoundingClientRect(); } diff --git a/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts b/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts index 337c2fea46..6bc5cc7cde 100644 --- a/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts +++ b/packages/core/src/extensions/SideMenu/SideMenuPlugin.ts @@ -267,7 +267,7 @@ export class SideMenuView< // When false, the drag handle with be just to the left of the element // TODO: Is there any case where we want this to be false? private horizontalPosAnchoredAtRoot: boolean; - private horizontalPosAnchor: number; + private horizontalPosAnchor: number | undefined; private hoveredBlock: HTMLElement | undefined; @@ -290,9 +290,12 @@ export class SideMenuView< }; this.horizontalPosAnchoredAtRoot = true; - this.horizontalPosAnchor = ( - this.pmView.dom.firstChild! as HTMLElement - ).getBoundingClientRect().x; + + if (this.pmView.dom.firstChild) { + this.horizontalPosAnchor = ( + this.pmView.dom.firstChild as HTMLElement + ).getBoundingClientRect().x; + } this.pmView.root.addEventListener( "drop", @@ -337,8 +340,12 @@ export class SideMenuView< // size/position, so we get the boundingRect of the first child (i.e. the // blockGroup that wraps all blocks in the editor) for more accurate side // menu placement. + if (!this.pmView.dom.firstChild) { + return; + } + const editorBoundingBox = ( - this.pmView.dom.firstChild! as HTMLElement + this.pmView.dom.firstChild as HTMLElement ).getBoundingClientRect(); this.horizontalPosAnchor = editorBoundingBox.x; @@ -441,7 +448,7 @@ export class SideMenuView< if (!pos || pos.inside === -1) { const evt = new Event("drop", event) as any; const editorBoundingBox = ( - this.pmView.dom.firstChild! as HTMLElement + this.pmView.dom.firstChild as HTMLElement ).getBoundingClientRect(); evt.clientX = event.clientX < editorBoundingBox.left || @@ -474,10 +481,10 @@ export class SideMenuView< top: event.clientY, }); - if (!pos || pos.inside === -1) { + if (!pos || (pos.inside === -1 && this.pmView.dom.firstChild)) { const evt = new Event("dragover", event) as any; const editorBoundingBox = ( - this.pmView.dom.firstChild! as HTMLElement + this.pmView.dom.firstChild as HTMLElement ).getBoundingClientRect(); evt.clientX = editorBoundingBox.left + editorBoundingBox.width / 2; evt.clientY = event.clientY; @@ -555,8 +562,8 @@ export class SideMenuView< }; onScroll = () => { - if (this.state?.show) { - const blockContent = this.hoveredBlock!.firstChild as HTMLElement; + if (this.state?.show && this.hoveredBlock?.firstChild) { + const blockContent = this.hoveredBlock.firstChild as HTMLElement; const blockContentBoundingBox = blockContent.getBoundingClientRect(); this.state.referencePos = new DOMRect( @@ -624,7 +631,11 @@ export class SideMenuView< this.emitUpdate(this.state); } - const blockContent = this.hoveredBlock!.firstChild! as HTMLElement; + if (!this.hoveredBlock?.firstChild) { + return; + } + + const blockContent = this.hoveredBlock.firstChild as HTMLElement; const blockContentBoundingBox = blockContent.getBoundingClientRect(); const pos = this.pmView.posAtCoords({ diff --git a/packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts b/packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts index 0d547a11db..1cf45b71fd 100644 --- a/packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts +++ b/packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts @@ -54,7 +54,10 @@ class SuggestionMenuView< const decorationNode = this.rootEl?.querySelector( `[data-decoration-id="${this.pluginState!.decorationId}"]` ); - this.state.referencePos = decorationNode!.getBoundingClientRect(); + if (!decorationNode) { + return; + } + this.state.referencePos = decorationNode.getBoundingClientRect(); this.emitUpdate(this.pluginState!.triggerCharacter!); } }; @@ -89,10 +92,10 @@ class SuggestionMenuView< `[data-decoration-id="${this.pluginState!.decorationId}"]` ); - if (this.editor.isEditable) { + if (this.editor.isEditable && decorationNode) { this.state = { show: true, - referencePos: decorationNode!.getBoundingClientRect(), + referencePos: decorationNode.getBoundingClientRect(), query: this.pluginState!.query, }; diff --git a/packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts b/packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts index a94b0b0e9a..bf40dade8c 100644 --- a/packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts +++ b/packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts @@ -159,7 +159,11 @@ export class TableHandlesView< const rowIndex = getChildIndex(target.parentElement!); const cellRect = target.getBoundingClientRect(); const tableRect = - target.parentElement!.parentElement!.getBoundingClientRect(); + target.parentElement?.parentElement?.getBoundingClientRect(); + + if (!tableRect) { + return; + } const blockEl = getDraggableBlockFromElement(target, this.pmView); if (!blockEl) { diff --git a/packages/dev-scripts/package.json b/packages/dev-scripts/package.json index 501a92fb8a..6babc253bc 100644 --- a/packages/dev-scripts/package.json +++ b/packages/dev-scripts/package.json @@ -30,4 +30,4 @@ "../../.eslintrc.js" ] } -} +} \ No newline at end of file