返回 presentation-ai
useEditorFonts.ts
根目录 / src / components / notebook / presentation / editor / hooks / useEditorFonts.ts
1 "use client";
2
3 import { type PlateEditor } from "platejs/react";
4 import { useCallback, useState } from "react";
5
6 import { extractFontsFromEditor } from "@/components/plate/utils/extractFontsFromEditor";
7
8 export function useEditorFonts() {
9 const [fontsToLoad, setFontsToLoad] = useState<string[]>([]);
10
11 const onEditorReady = useCallback(
12 (editor: PlateEditor, overrides: Array<string | undefined>) => {
13 const fonts = extractFontsFromEditor(editor);
14 const validOverrides = (overrides.filter(Boolean) as string[]) ?? [];
15 setFontsToLoad(Array.from(new Set([...fonts, ...validOverrides])));
16 },
17 [],
18 );
19
20 const updateFontsFromEditor = useCallback((editor: PlateEditor) => {
21 const fontsArray = extractFontsFromEditor(editor);
22 setFontsToLoad(Array.from(new Set([...fontsArray])));
23 }, []);
24
25 return { fontsToLoad, onEditorReady, updateFontsFromEditor };
26 }
27
27 lines TYPESCRIPT