返回 presentation-ai
extractFontsFromEditor.ts
根目录 / src / components / plate / utils / extractFontsFromEditor.ts
1 import { FontFamilyPlugin } from "@platejs/basic-styles/react";
2 import { type PlateEditor } from "platejs/react";
3
4 export function extractFontsFromEditor(editor: PlateEditor) {
5 const fontFamilies = new Set<string>();
6
7 // Scan all nodes for font family marks
8 try {
9 for (const [node] of editor.api.nodes({
10 at: [],
11 match: (n) => {
12 const nodeWithFont = n as {
13 text?: string;
14 [key: string]: unknown;
15 };
16 return Boolean(nodeWithFont.text && nodeWithFont[FontFamilyPlugin.key]);
17 },
18 })) {
19 const nodeWithFont = node as {
20 text: string;
21 [key: string]: unknown;
22 };
23 if (nodeWithFont[FontFamilyPlugin.key]) {
24 const nodeFontFamily = nodeWithFont[FontFamilyPlugin.key] as string;
25 fontFamilies.add(nodeFontFamily);
26 }
27 }
28 } catch (error) {
29 console.error("Error scanning editor for fonts:", error);
30 return [];
31 }
32
33 // Convert Set to array and update state
34 const fontsArray = Array.from(fontFamilies);
35 return fontsArray;
36 }
37
37 lines TYPESCRIPT