| 1 | "use client"; |
| 2 | |
| 3 | import { type NodeEntry } from "platejs"; |
| 4 | import { type PlateEditor } from "platejs/react"; |
| 5 | |
| 6 | import { ANTV_INFOGRAPHIC } from "@/components/notebook/presentation/editor/lib"; |
| 7 | import { type TAntvInfographicElement } from "@/components/notebook/presentation/editor/plugins/antv-infographic-plugin"; |
| 8 | |
| 9 | function isAntvInfographicElement( |
| 10 | node: unknown, |
| 11 | elementId: string, |
| 12 | ): node is TAntvInfographicElement { |
| 13 | return ( |
| 14 | typeof node === "object" && |
| 15 | node !== null && |
| 16 | "type" in node && |
| 17 | node.type === ANTV_INFOGRAPHIC && |
| 18 | "id" in node && |
| 19 | node.id === elementId |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | export function findInfographicEntryById( |
| 24 | editor: PlateEditor, |
| 25 | elementId: string, |
| 26 | ): NodeEntry<TAntvInfographicElement> | undefined { |
| 27 | if (!elementId) { |
| 28 | return undefined; |
| 29 | } |
| 30 | |
| 31 | const [entry] = Array.from( |
| 32 | editor.api.nodes({ |
| 33 | at: [], |
| 34 | match: (node) => isAntvInfographicElement(node, elementId), |
| 35 | }), |
| 36 | ) as Array<NodeEntry<TAntvInfographicElement>>; |
| 37 | |
| 38 | if (!entry) return undefined; |
| 39 | |
| 40 | const [node, path] = entry; |
| 41 | |
| 42 | if (!isAntvInfographicElement(node, elementId)) { |
| 43 | return undefined; |
| 44 | } |
| 45 | |
| 46 | return [node, path]; |
| 47 | } |
| 48 |