|
1 | | -import { Excalidraw, exportToSvg, getSceneVersion } from "@excalidraw/excalidraw"; |
2 | | -import { TypeWidgetProps } from "./type_widget"; |
3 | | -import "@excalidraw/excalidraw/index.css"; |
4 | | -import { useEditorSpacedUpdate, useNoteLabelBoolean } from "../react/hooks"; |
5 | | -import { useCallback, useMemo, useRef } from "preact/hooks"; |
6 | | -import { type ExcalidrawImperativeAPI, type AppState, type BinaryFileData, LibraryItem, ExcalidrawProps } from "@excalidraw/excalidraw/types"; |
7 | | -import options from "../../services/options"; |
8 | | -import "./Canvas.css"; |
9 | | -import FNote from "../../entities/fnote"; |
10 | 1 | import { RefObject } from "preact"; |
11 | | -import server from "../../services/server"; |
| 2 | +import NoteContext from "../../../components/note_context"; |
| 3 | +import FNote from "../../../entities/fnote"; |
| 4 | +import { AppState, BinaryFileData, ExcalidrawImperativeAPI, ExcalidrawProps, LibraryItem } from "@excalidraw/excalidraw/types"; |
| 5 | +import { useRef } from "preact/hooks"; |
| 6 | +import { useEditorSpacedUpdate } from "../../react/hooks"; |
12 | 7 | import { ExcalidrawElement, NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types"; |
13 | | -import { goToLinkExt } from "../../services/link"; |
14 | | -import NoteContext from "../../components/note_context"; |
15 | | - |
16 | | -// currently required by excalidraw, in order to allows self-hosting fonts locally. |
17 | | -// this avoids making excalidraw load the fonts from an external CDN. |
18 | | -window.EXCALIDRAW_ASSET_PATH = `${window.location.pathname}/node_modules/@excalidraw/excalidraw/dist/prod`; |
| 8 | +import { exportToSvg, getSceneVersion } from "@excalidraw/excalidraw"; |
| 9 | +import server from "../../../services/server"; |
19 | 10 |
|
20 | 11 | interface AttachmentMetadata { |
21 | 12 | title: string; |
22 | 13 | attachmentId: string; |
23 | 14 | } |
24 | 15 |
|
25 | | -interface CanvasContent { |
| 16 | +export interface CanvasContent { |
26 | 17 | elements: ExcalidrawElement[]; |
27 | 18 | files: BinaryFileData[]; |
28 | 19 | appState: Partial<AppState>; |
29 | 20 | } |
30 | 21 |
|
31 | | -export default function Canvas({ note, noteContext }: TypeWidgetProps) { |
32 | | - const apiRef = useRef<ExcalidrawImperativeAPI>(null); |
33 | | - const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly"); |
34 | | - const themeStyle = useMemo(() => { |
35 | | - const documentStyle = window.getComputedStyle(document.documentElement); |
36 | | - return documentStyle.getPropertyValue("--theme-style")?.trim() as AppState["theme"]; |
37 | | - }, []); |
38 | | - const persistence = usePersistence(note, noteContext, apiRef, themeStyle, isReadOnly); |
39 | | - |
40 | | - /** Use excalidraw's native zoom instead of the global zoom. */ |
41 | | - const onWheel = useCallback((e: MouseEvent) => { |
42 | | - if (e.ctrlKey) { |
43 | | - e.preventDefault(); |
44 | | - e.stopPropagation(); |
45 | | - } |
46 | | - }, []); |
47 | | - |
48 | | - const onLinkOpen = useCallback((element: NonDeletedExcalidrawElement, event: CustomEvent) => { |
49 | | - let link = element.link; |
50 | | - if (!link) { |
51 | | - return false; |
52 | | - } |
53 | | - |
54 | | - if (link.startsWith("root/")) { |
55 | | - link = "#" + link; |
56 | | - } |
57 | | - |
58 | | - const { nativeEvent } = event.detail; |
59 | | - event.preventDefault(); |
60 | | - return goToLinkExt(nativeEvent, link, null); |
61 | | - }, []); |
62 | | - |
63 | | - return ( |
64 | | - <div className="canvas-render" onWheel={onWheel}> |
65 | | - <div className="excalidraw-wrapper"> |
66 | | - <Excalidraw |
67 | | - excalidrawAPI={api => apiRef.current = api} |
68 | | - theme={themeStyle} |
69 | | - viewModeEnabled={isReadOnly || options.is("databaseReadonly")} |
70 | | - zenModeEnabled={false} |
71 | | - isCollaborating={false} |
72 | | - detectScroll={false} |
73 | | - handleKeyboardGlobally={false} |
74 | | - autoFocus={false} |
75 | | - UIOptions={{ |
76 | | - canvasActions: { |
77 | | - saveToActiveFile: false, |
78 | | - export: false |
79 | | - } |
80 | | - }} |
81 | | - onLinkOpen={onLinkOpen} |
82 | | - {...persistence} |
83 | | - /> |
84 | | - </div> |
85 | | - </div> |
86 | | - ) |
87 | | -} |
88 | | - |
89 | | -function usePersistence(note: FNote, noteContext: NoteContext | null | undefined, apiRef: RefObject<ExcalidrawImperativeAPI>, theme: AppState["theme"], isReadOnly: boolean): Partial<ExcalidrawProps> { |
| 22 | +export default function useCanvasPersistence(note: FNote, noteContext: NoteContext | null | undefined, apiRef: RefObject<ExcalidrawImperativeAPI>, theme: AppState["theme"], isReadOnly: boolean): Partial<ExcalidrawProps> { |
90 | 23 | const libraryChanged = useRef(false); |
91 | 24 |
|
92 | 25 | /** |
|
0 commit comments