Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

UXW-4342 Fix document crash after removing a chart (#75641)

Denis Berezincb574d0ac182
diff --git a/frontend/src/metabase/documents/hooks/use-block-menus.ts b/frontend/src/metabase/documents/hooks/use-block-menus.ts
index 2c280b64c50..1054d02b74b 100644
--- a/frontend/src/metabase/documents/hooks/use-block-menus.ts
+++ b/frontend/src/metabase/documents/hooks/use-block-menus.ts
@@ -10,7 +10,6 @@ import {
getCurrentDocument,
getHoveredChildTargetId,
} from "metabase/documents/selectors";
-import { isTopLevel } from "metabase/documents/utils/editorNodeUtils";
import { useSelector } from "metabase/redux";
import { documentWithAnchor } from "metabase/urls";
import { isWithinIframe } from "metabase/utils/iframe";
@@ -113,3 +112,22 @@ export function useBlockMenus({
anchorFloatingStyles,
};
}
+
+export function isTopLevel({
+ editor,
+ getPos,
+}: Pick<NodeViewProps, "editor" | "getPos">) {
+ if (!editor || !getPos) {
+ return false;
+ }
+
+ const { doc } = editor.state;
+ const pos = getPos();
+
+ if (pos === null || pos === undefined || pos < 0 || pos > doc.content?.size) {
+ return false;
+ }
+
+ const resolvedPos = doc.resolve(pos);
+ return resolvedPos.depth === 0;
+}
diff --git a/frontend/src/metabase/documents/hooks/use-block-menus.unit.spec.ts b/frontend/src/metabase/documents/hooks/use-block-menus.unit.spec.ts
index 08312d23732..8f5b96c055f 100644
--- a/frontend/src/metabase/documents/hooks/use-block-menus.unit.spec.ts
+++ b/frontend/src/metabase/documents/hooks/use-block-menus.unit.spec.ts
@@ -1,3 +1,5 @@
+import { Schema } from "@tiptap/pm/model";
+import type { Editor, NodeViewProps } from "@tiptap/react";
import fetchMock from "fetch-mock";
import { setupMockIntersectionObserver } from "__support__/intersection-observer";
@@ -13,7 +15,7 @@ import type { Comment } from "metabase-types/api";
import { createMockDocument } from "metabase-types/api/mocks";
import { createMockComment } from "metabase-types/api/mocks/comment";
-import { useBlockMenus } from "./use-block-menus";
+import { isTopLevel, useBlockMenus } from "./use-block-menus";
const NODE_ID = "test-id";
const mockDocument = createMockDocument({ id: 1 });
@@ -90,3 +92,79 @@ describe("useBlockMenus", () => {
expect(result.current.shouldShowMenus).toBe(true);
});
});
+
+const schema = new Schema({
+ nodes: {
+ doc: { content: "block+" },
+ flexContainer: { group: "block", content: "block+" },
+ paragraph: { group: "block", content: "inline*" },
+ text: { group: "inline" },
+ },
+});
+
+const buildEditor = (doc: ReturnType<Schema["node"]>) =>
+ ({ state: { doc } }) as unknown as Editor;
+
+const buildArgs = (
+ editor: Editor | null,
+ getPos: NodeViewProps["getPos"] | null,
+) =>
+ ({ editor, getPos }) as unknown as Pick<NodeViewProps, "editor" | "getPos">;
+
+describe("isTopLevel", () => {
+ it("returns true for a top-level block", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("paragraph", null, [schema.text("hello")]),
+ ]);
+ // Position 0 is before the top-level paragraph (depth 0).
+ expect(isTopLevel(buildArgs(buildEditor(doc), () => 0))).toBe(true);
+ });
+
+ it("returns false for a nested block", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("flexContainer", null, [
+ schema.node("paragraph", null, [schema.text("nested")]),
+ ]),
+ ]);
+ // Position 1 is inside the flexContainer, before the nested paragraph.
+ expect(isTopLevel(buildArgs(buildEditor(doc), () => 1))).toBe(false);
+ });
+
+ it("returns false (without throwing) for a stale out-of-range position", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("paragraph", null, [schema.text("hi")]),
+ ]);
+ const stalePos = doc.content.size + 100;
+ expect(() =>
+ isTopLevel(buildArgs(buildEditor(doc), () => stalePos)),
+ ).not.toThrow();
+ expect(isTopLevel(buildArgs(buildEditor(doc), () => stalePos))).toBe(false);
+ });
+
+ it("returns false (without throwing) for a negative position", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("paragraph", null, [schema.text("hi")]),
+ ]);
+ expect(isTopLevel(buildArgs(buildEditor(doc), () => -1))).toBe(false);
+ });
+
+ it("returns false when editor or getPos is missing", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("paragraph", null, [schema.text("hi")]),
+ ]);
+ expect(isTopLevel(buildArgs(null, () => 0))).toBe(false);
+ expect(isTopLevel(buildArgs(buildEditor(doc), null))).toBe(false);
+ });
+
+ it("returns false when getPos returns undefined", () => {
+ const doc = schema.node("doc", null, [
+ schema.node("paragraph", null, [schema.text("hi")]),
+ ]);
+ expect(isTopLevel(buildArgs(buildEditor(doc), () => undefined))).toBe(
+ false,
+ );
+ expect(
+ isTopLevel(buildArgs(buildEditor(doc), () => undefined as never)),
+ ).toBe(false);
+ });
+});
diff --git a/frontend/src/metabase/documents/utils/editorNodeUtils.ts b/frontend/src/metabase/documents/utils/editorNodeUtils.ts
index 5b87348b557..49b772fb243 100644
--- a/frontend/src/metabase/documents/utils/editorNodeUtils.ts
+++ b/frontend/src/metabase/documents/utils/editorNodeUtils.ts
@@ -1,6 +1,6 @@
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
import type { EditorState } from "@tiptap/pm/state";
-import type { Editor, NodeViewProps } from "@tiptap/react";
+import type { Editor } from "@tiptap/react";
/**
* Updates a card embed node's ID in the ProseMirror editor
@@ -41,23 +41,5 @@ export function updateCardEmbedNodeId(
}
}
-export function isTopLevel({
- editor,
- getPos,
-}: Pick<NodeViewProps, "editor" | "getPos">) {
- if (!editor || !getPos) {
- return true;
- }
-
- const pos = getPos();
-
- if (pos === null || pos === undefined) {
- return true;
- }
-
- const resolvedPos = editor.state.doc.resolve(pos);
- return resolvedPos.depth === 0;
-}
-
export const isMetabotBlock = (state: EditorState): boolean =>
state.selection.$head.parent.type.name === "metabot";