@haklex/rich-ext-whiteboard 0.0.0-stage → 0.65.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -2
- package/dist/WhiteboardConfigContext.d.ts +14 -0
- package/dist/WhiteboardConfigContext.d.ts.map +1 -0
- package/dist/WhiteboardDisplayRenderer.d.ts +14 -0
- package/dist/WhiteboardDisplayRenderer.d.ts.map +1 -0
- package/dist/WhiteboardEditNode.d.ts +14 -0
- package/dist/WhiteboardEditNode.d.ts.map +1 -0
- package/dist/WhiteboardEditRenderer.d.ts +8 -0
- package/dist/WhiteboardEditRenderer.d.ts.map +1 -0
- package/dist/WhiteboardEditorDialog.d.ts +23 -0
- package/dist/WhiteboardEditorDialog.d.ts.map +1 -0
- package/dist/WhiteboardExpandShell-D1s7iDKh.js +2550 -0
- package/dist/WhiteboardExpandShell.d.ts +9 -0
- package/dist/WhiteboardExpandShell.d.ts.map +1 -0
- package/dist/WhiteboardFrameStates.d.ts +11 -0
- package/dist/WhiteboardFrameStates.d.ts.map +1 -0
- package/dist/WhiteboardNode.d.ts +22 -0
- package/dist/WhiteboardNode.d.ts.map +1 -0
- package/dist/WhiteboardOptionsMenu.d.ts +16 -0
- package/dist/WhiteboardOptionsMenu.d.ts.map +1 -0
- package/dist/WhiteboardPlugin.d.ts +3 -0
- package/dist/WhiteboardPlugin.d.ts.map +1 -0
- package/dist/WhiteboardSSRRenderer.d.ts +8 -0
- package/dist/WhiteboardSSRRenderer.d.ts.map +1 -0
- package/dist/acceptance/brainstorm-board.d.ts +3 -0
- package/dist/acceptance/brainstorm-board.d.ts.map +1 -0
- package/dist/acceptance/brainstorm.test.d.ts +2 -0
- package/dist/acceptance/brainstorm.test.d.ts.map +1 -0
- package/dist/acceptance/fixtures.d.ts +5 -0
- package/dist/acceptance/fixtures.d.ts.map +1 -0
- package/dist/acceptance/review-fixes.test.d.ts +2 -0
- package/dist/acceptance/review-fixes.test.d.ts.map +1 -0
- package/dist/acceptance/roundtrip.test.d.ts +2 -0
- package/dist/acceptance/roundtrip.test.d.ts.map +1 -0
- package/dist/canvas-state.d.ts +5 -0
- package/dist/canvas-state.d.ts.map +1 -0
- package/dist/display.test.d.ts +2 -0
- package/dist/display.test.d.ts.map +1 -0
- package/dist/editor/FrameNameEditor.d.ts +6 -0
- package/dist/editor/FrameNameEditor.d.ts.map +1 -0
- package/dist/editor/MobileDock.d.ts +6 -0
- package/dist/editor/MobileDock.d.ts.map +1 -0
- package/dist/editor/RichTextEditor.d.ts +7 -0
- package/dist/editor/RichTextEditor.d.ts.map +1 -0
- package/dist/editor/RichTextFormatBar.d.ts +8 -0
- package/dist/editor/RichTextFormatBar.d.ts.map +1 -0
- package/dist/editor/StylePanel.d.ts +6 -0
- package/dist/editor/StylePanel.d.ts.map +1 -0
- package/dist/editor/StyleSheet.d.ts +7 -0
- package/dist/editor/StyleSheet.d.ts.map +1 -0
- package/dist/editor/TextEditor.d.ts +7 -0
- package/dist/editor/TextEditor.d.ts.map +1 -0
- package/dist/editor/Toolbar.d.ts +20 -0
- package/dist/editor/Toolbar.d.ts.map +1 -0
- package/dist/editor/WhiteboardCanvas.d.ts +14 -0
- package/dist/editor/WhiteboardCanvas.d.ts.map +1 -0
- package/dist/editor/WhiteboardEditor.d.ts +10 -0
- package/dist/editor/WhiteboardEditor.d.ts.map +1 -0
- package/dist/editor/ZoomControls.d.ts +7 -0
- package/dist/editor/ZoomControls.d.ts.map +1 -0
- package/dist/editor/actions.d.ts +13 -0
- package/dist/editor/actions.d.ts.map +1 -0
- package/dist/editor/actions.test.d.ts +2 -0
- package/dist/editor/actions.test.d.ts.map +1 -0
- package/dist/editor/binding-actions.d.ts +8 -0
- package/dist/editor/binding-actions.d.ts.map +1 -0
- package/dist/editor/binding.test.d.ts +2 -0
- package/dist/editor/binding.test.d.ts.map +1 -0
- package/dist/editor/card-tool.test.d.ts +2 -0
- package/dist/editor/card-tool.test.d.ts.map +1 -0
- package/dist/editor/chrome.css.d.ts +24 -0
- package/dist/editor/chrome.css.d.ts.map +1 -0
- package/dist/editor/clipboard-actions.d.ts +6 -0
- package/dist/editor/clipboard-actions.d.ts.map +1 -0
- package/dist/editor/clipboard-events.test.d.ts +2 -0
- package/dist/editor/clipboard-events.test.d.ts.map +1 -0
- package/dist/editor/clipboard.test.d.ts +2 -0
- package/dist/editor/clipboard.test.d.ts.map +1 -0
- package/dist/editor/editor.css.d.ts +5 -0
- package/dist/editor/editor.css.d.ts.map +1 -0
- package/dist/editor/elbow.test.d.ts +2 -0
- package/dist/editor/elbow.test.d.ts.map +1 -0
- package/dist/editor/focus.test.d.ts +2 -0
- package/dist/editor/focus.test.d.ts.map +1 -0
- package/dist/editor/frames.test.d.ts +2 -0
- package/dist/editor/frames.test.d.ts.map +1 -0
- package/dist/editor/gestures.d.ts +11 -0
- package/dist/editor/gestures.d.ts.map +1 -0
- package/dist/editor/grid.d.ts +9 -0
- package/dist/editor/grid.d.ts.map +1 -0
- package/dist/editor/grid.test.d.ts +2 -0
- package/dist/editor/grid.test.d.ts.map +1 -0
- package/dist/editor/history.d.ts +13 -0
- package/dist/editor/history.d.ts.map +1 -0
- package/dist/editor/image-actions.d.ts +17 -0
- package/dist/editor/image-actions.d.ts.map +1 -0
- package/dist/editor/image.test.d.ts +2 -0
- package/dist/editor/image.test.d.ts.map +1 -0
- package/dist/editor/layout.d.ts +4 -0
- package/dist/editor/layout.d.ts.map +1 -0
- package/dist/editor/linear-editor.d.ts +28 -0
- package/dist/editor/linear-editor.d.ts.map +1 -0
- package/dist/editor/linear-editor.test.d.ts +2 -0
- package/dist/editor/linear-editor.test.d.ts.map +1 -0
- package/dist/editor/overlay/LinearHandles.d.ts +6 -0
- package/dist/editor/overlay/LinearHandles.d.ts.map +1 -0
- package/dist/editor/overlay/SelectionOverlay.d.ts +7 -0
- package/dist/editor/overlay/SelectionOverlay.d.ts.map +1 -0
- package/dist/editor/overlay/TransformHandles.d.ts +6 -0
- package/dist/editor/overlay/TransformHandles.d.ts.map +1 -0
- package/dist/editor/render-fidelity.test.d.ts +2 -0
- package/dist/editor/render-fidelity.test.d.ts.map +1 -0
- package/dist/editor/richtext-actions.d.ts +6 -0
- package/dist/editor/richtext-actions.d.ts.map +1 -0
- package/dist/editor/richtext-edit.test.d.ts +2 -0
- package/dist/editor/richtext-edit.test.d.ts.map +1 -0
- package/dist/editor/richtext-editor.css.d.ts +17 -0
- package/dist/editor/richtext-editor.css.d.ts.map +1 -0
- package/dist/editor/richtext-format.test.d.ts +2 -0
- package/dist/editor/richtext-format.test.d.ts.map +1 -0
- package/dist/editor/richtext-load.test.d.ts +2 -0
- package/dist/editor/richtext-load.test.d.ts.map +1 -0
- package/dist/editor/selection-utils.d.ts +17 -0
- package/dist/editor/selection-utils.d.ts.map +1 -0
- package/dist/editor/selection.test.d.ts +2 -0
- package/dist/editor/selection.test.d.ts.map +1 -0
- package/dist/editor/shadow.test.d.ts +2 -0
- package/dist/editor/shadow.test.d.ts.map +1 -0
- package/dist/editor/shortcuts.d.ts +16 -0
- package/dist/editor/shortcuts.d.ts.map +1 -0
- package/dist/editor/store.d.ts +21 -0
- package/dist/editor/store.d.ts.map +1 -0
- package/dist/editor/store.test.d.ts +2 -0
- package/dist/editor/store.test.d.ts.map +1 -0
- package/dist/editor/style-options.d.ts +21 -0
- package/dist/editor/style-options.d.ts.map +1 -0
- package/dist/editor/style-props.d.ts +10 -0
- package/dist/editor/style-props.d.ts.map +1 -0
- package/dist/editor/style-rows.d.ts +4 -0
- package/dist/editor/style-rows.d.ts.map +1 -0
- package/dist/editor/svg-paste.test.d.ts +2 -0
- package/dist/editor/svg-paste.test.d.ts.map +1 -0
- package/dist/editor/svg.test.d.ts +2 -0
- package/dist/editor/svg.test.d.ts.map +1 -0
- package/dist/editor/text-actions.d.ts +15 -0
- package/dist/editor/text-actions.d.ts.map +1 -0
- package/dist/editor/text-editor.css.d.ts +3 -0
- package/dist/editor/text-editor.css.d.ts.map +1 -0
- package/dist/editor/text.test.d.ts +2 -0
- package/dist/editor/text.test.d.ts.map +1 -0
- package/dist/editor/tools/card.d.ts +5 -0
- package/dist/editor/tools/card.d.ts.map +1 -0
- package/dist/editor/tools/creation.d.ts +8 -0
- package/dist/editor/tools/creation.d.ts.map +1 -0
- package/dist/editor/tools/eraser.d.ts +3 -0
- package/dist/editor/tools/eraser.d.ts.map +1 -0
- package/dist/editor/tools/freedraw.d.ts +3 -0
- package/dist/editor/tools/freedraw.d.ts.map +1 -0
- package/dist/editor/tools/hand.d.ts +3 -0
- package/dist/editor/tools/hand.d.ts.map +1 -0
- package/dist/editor/tools/image.d.ts +3 -0
- package/dist/editor/tools/image.d.ts.map +1 -0
- package/dist/editor/tools/index.d.ts +9 -0
- package/dist/editor/tools/index.d.ts.map +1 -0
- package/dist/editor/tools/linear.d.ts +10 -0
- package/dist/editor/tools/linear.d.ts.map +1 -0
- package/dist/editor/tools/point-gesture.d.ts +10 -0
- package/dist/editor/tools/point-gesture.d.ts.map +1 -0
- package/dist/editor/tools/selection.d.ts +9 -0
- package/dist/editor/tools/selection.d.ts.map +1 -0
- package/dist/editor/tools/shape.d.ts +15 -0
- package/dist/editor/tools/shape.d.ts.map +1 -0
- package/dist/editor/tools/text.d.ts +3 -0
- package/dist/editor/tools/text.d.ts.map +1 -0
- package/dist/editor/tools/transform-gesture.d.ts +13 -0
- package/dist/editor/tools/transform-gesture.d.ts.map +1 -0
- package/dist/editor/tools/types.d.ts +28 -0
- package/dist/editor/tools/types.d.ts.map +1 -0
- package/dist/editor/tools.test.d.ts +2 -0
- package/dist/editor/tools.test.d.ts.map +1 -0
- package/dist/editor/touch.test.d.ts +2 -0
- package/dist/editor/touch.test.d.ts.map +1 -0
- package/dist/editor/transform.d.ts +25 -0
- package/dist/editor/transform.d.ts.map +1 -0
- package/dist/editor/transform.test.d.ts +2 -0
- package/dist/editor/transform.test.d.ts.map +1 -0
- package/dist/editor/types.d.ts +41 -0
- package/dist/editor/types.d.ts.map +1 -0
- package/dist/editor/viewport.d.ts +14 -0
- package/dist/editor/viewport.d.ts.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4941 -0
- package/dist/model/bindable.d.ts +3 -0
- package/dist/model/bindable.d.ts.map +1 -0
- package/dist/model/binding.d.ts +15 -0
- package/dist/model/binding.d.ts.map +1 -0
- package/dist/model/bounds.d.ts +17 -0
- package/dist/model/bounds.d.ts.map +1 -0
- package/dist/model/bounds.test.d.ts +2 -0
- package/dist/model/bounds.test.d.ts.map +1 -0
- package/dist/model/clipboard.d.ts +11 -0
- package/dist/model/clipboard.d.ts.map +1 -0
- package/dist/model/clone.d.ts +6 -0
- package/dist/model/clone.d.ts.map +1 -0
- package/dist/model/create.d.ts +17 -0
- package/dist/model/create.d.ts.map +1 -0
- package/dist/model/create.test.d.ts +2 -0
- package/dist/model/create.test.d.ts.map +1 -0
- package/dist/model/defaults.d.ts +22 -0
- package/dist/model/defaults.d.ts.map +1 -0
- package/dist/model/elbow.d.ts +16 -0
- package/dist/model/elbow.d.ts.map +1 -0
- package/dist/model/elbow.test.d.ts +2 -0
- package/dist/model/elbow.test.d.ts.map +1 -0
- package/dist/model/fractional-index.d.ts +9 -0
- package/dist/model/fractional-index.d.ts.map +1 -0
- package/dist/model/fractional-index.test.d.ts +2 -0
- package/dist/model/fractional-index.test.d.ts.map +1 -0
- package/dist/model/frames.d.ts +8 -0
- package/dist/model/frames.d.ts.map +1 -0
- package/dist/model/frames.test.d.ts +2 -0
- package/dist/model/frames.test.d.ts.map +1 -0
- package/dist/model/geometry.d.ts +13 -0
- package/dist/model/geometry.d.ts.map +1 -0
- package/dist/model/linear-points.d.ts +5 -0
- package/dist/model/linear-points.d.ts.map +1 -0
- package/dist/model/measure.d.ts +16 -0
- package/dist/model/measure.d.ts.map +1 -0
- package/dist/model/mutate.d.ts +8 -0
- package/dist/model/mutate.d.ts.map +1 -0
- package/dist/model/random.d.ts +3 -0
- package/dist/model/random.d.ts.map +1 -0
- package/dist/model/restore.d.ts +6 -0
- package/dist/model/restore.d.ts.map +1 -0
- package/dist/model/restore.test.d.ts +2 -0
- package/dist/model/restore.test.d.ts.map +1 -0
- package/dist/model/richtext-editable.d.ts +3 -0
- package/dist/model/richtext-editable.d.ts.map +1 -0
- package/dist/model/richtext-layout.d.ts +6 -0
- package/dist/model/richtext-layout.d.ts.map +1 -0
- package/dist/model/richtext-layout.test.d.ts +2 -0
- package/dist/model/richtext-layout.test.d.ts.map +1 -0
- package/dist/model/richtext.d.ts +42 -0
- package/dist/model/richtext.d.ts.map +1 -0
- package/dist/model/shadow.d.ts +5 -0
- package/dist/model/shadow.d.ts.map +1 -0
- package/dist/model/svg.d.ts +8 -0
- package/dist/model/svg.d.ts.map +1 -0
- package/dist/model/svg.test.d.ts +2 -0
- package/dist/model/svg.test.d.ts.map +1 -0
- package/dist/model/text-layout.d.ts +13 -0
- package/dist/model/text-layout.d.ts.map +1 -0
- package/dist/model/text.test.d.ts +2 -0
- package/dist/model/text.test.d.ts.map +1 -0
- package/dist/rename.test.d.ts +2 -0
- package/dist/rename.test.d.ts.map +1 -0
- package/dist/rich-ext-whiteboard.css +2 -0
- package/dist/scene/InteractiveScene.d.ts +11 -0
- package/dist/scene/InteractiveScene.d.ts.map +1 -0
- package/dist/scene/Scene.d.ts +27 -0
- package/dist/scene/Scene.d.ts.map +1 -0
- package/dist/scene/context.d.ts +4 -0
- package/dist/scene/context.d.ts.map +1 -0
- package/dist/scene/elements.d.ts +7 -0
- package/dist/scene/elements.d.ts.map +1 -0
- package/dist/scene/fidelity.test.d.ts +2 -0
- package/dist/scene/fidelity.test.d.ts.map +1 -0
- package/dist/scene/fill.test.d.ts +2 -0
- package/dist/scene/fill.test.d.ts.map +1 -0
- package/dist/scene/fonts.d.ts +11 -0
- package/dist/scene/fonts.d.ts.map +1 -0
- package/dist/scene/freedraw.d.ts +3 -0
- package/dist/scene/freedraw.d.ts.map +1 -0
- package/dist/scene/normalize.d.ts +4 -0
- package/dist/scene/normalize.d.ts.map +1 -0
- package/dist/scene/richtext/RichTextBody.d.ts +6 -0
- package/dist/scene/richtext/RichTextBody.d.ts.map +1 -0
- package/dist/scene/richtext/RichTextContent.d.ts +6 -0
- package/dist/scene/richtext/RichTextContent.d.ts.map +1 -0
- package/dist/scene/richtext.test.d.ts +2 -0
- package/dist/scene/richtext.test.d.ts.map +1 -0
- package/dist/scene/rough.d.ts +19 -0
- package/dist/scene/rough.d.ts.map +1 -0
- package/dist/scene/scene.test.d.ts +2 -0
- package/dist/scene/scene.test.d.ts.map +1 -0
- package/dist/scene/shadow.d.ts +6 -0
- package/dist/scene/shadow.d.ts.map +1 -0
- package/dist/scene/shadow.test.d.ts +2 -0
- package/dist/scene/shadow.test.d.ts.map +1 -0
- package/dist/scene/styles.css.d.ts +7 -0
- package/dist/scene/styles.css.d.ts.map +1 -0
- package/dist/scene/svg-element.d.ts +6 -0
- package/dist/scene/svg-element.d.ts.map +1 -0
- package/dist/scene/svg.test.d.ts +2 -0
- package/dist/scene/svg.test.d.ts.map +1 -0
- package/dist/scene/types.d.ts +149 -0
- package/dist/scene/types.d.ts.map +1 -0
- package/dist/slot.d.ts +11 -0
- package/dist/slot.d.ts.map +1 -0
- package/dist/static.d.ts +18 -0
- package/dist/static.d.ts.map +1 -0
- package/dist/static.mjs +1 -0
- package/dist/styles.css.d.ts +29 -0
- package/dist/styles.css.d.ts.map +1 -0
- package/dist/transformer.d.ts +2 -0
- package/dist/transformer.d.ts.map +1 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.test.d.ts +2 -0
- package/dist/types.test.d.ts.map +1 -0
- package/dist/useWhiteboardData.d.ts +16 -0
- package/dist/useWhiteboardData.d.ts.map +1 -0
- package/dist/useWhiteboardFont.d.ts +3 -0
- package/dist/useWhiteboardFont.d.ts.map +1 -0
- package/package.json +70 -3
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,4941 @@
|
|
|
1
|
+
import { $ as angleOf$1, A as newElement, B as randomId, C as layoutBoundText, D as bumpVersion, E as parseSvgSize, Et as __exportAll, F as newRichTextElement, G as getCommonBounds, H as DEFAULT_STYLE, I as newSvgElement, J as getElementCenter, K as getElementAbsoluteCorners, L as newTextElement, M as newFreedrawElement, N as newImageElement, O as syncIndices, P as newLinearElement, Q as toLocalPoint, R as contentRoot, S as restoreElements, T as isSvgSource, Tt as WHITEBOARD_NODE_KEY, U as fontFamilyToCss, V as randomInteger, W as elementsInBox, X as hitTestElement, Y as getLinearAbsolutePoints, Z as isLinear, _ as Scene, _t as whiteboardFullscreenPopup, a as WhiteboardSSRRenderer, at as rotatePoint, b as resolveRichTextHeights, c as WhiteboardEmptyState, ct as isLocalScenePath, d as DEFAULT_FONT_CSS_HREF, dt as EXCALIDRAW_BLOCK_TRANSFORMER, et as boundsContain, f as useWhiteboardFont, ft as whiteboardDialogBody, g as DARK_SCENE_FILTER, gt as whiteboardEditorContainer, h as toScene, ht as whiteboardEditOverlay, i as WhiteboardNode, it as normalizeBox, j as newFrameElement, k as getLineHeight, l as WhiteboardErrorState, lt as parseSnapshot, m as countElements, mt as whiteboardEditLabel, n as $createWhiteboardNode, nt as boundsOfPoints, o as WhiteboardDisplayRenderer, ot as WhiteboardConfigProvider, p as useWhiteboardData, pt as whiteboardDialogCanvas, q as getElementBounds, r as $isWhiteboardNode, rt as distanceToSegment, s as _defineProperty, st as useWhiteboardConfig, t as WhiteboardExpandShell, tt as boundsIntersect, u as WhiteboardLoadingState, ut as serializeSnapshot, v as SceneContent, vt as whiteboardOptionsActions, w as refreshTextDimensions, wt as whiteboardShell, x as normalizeElement, y as RICHTEXT_BASE_STYLE, z as richTextPlainText } from "./WhiteboardExpandShell-D1s7iDKh.js";
|
|
2
|
+
import { Children, Suspense, createElement, lazy, useCallback, useEffect, useId, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { useColorScheme } from "@haklex/rich-editor/static";
|
|
5
|
+
import { Activity, AlignCenter, AlignLeft, AlignRight, ArrowDown, ArrowDownToLine, ArrowUp, ArrowUpToLine, Bold, Circle, Clipboard, Code, Copy, Diamond, Dot, Download, Ellipsis, Equal, Eraser, Frame, GripHorizontal, Group, Hand, Hash, Heading, Highlighter, Image as Image$1, Italic, Link, List, ListOrdered, Lock, LockOpen, Menu, Minus, MousePointer2, MoveRight, PenLine, PenTool, Pencil, Plus, Redo2, Ruler, Slash, Spline, Square, Squircle, StickyNote, Strikethrough, Trash2, Type, Undo2, Ungroup, Waypoints } from "lucide-react";
|
|
6
|
+
import { ActionButton, EditableGate, Field, ModalShell, ModalShellBody, ModalShellFooter, ModalShellHeader, Popover, PopoverBody, PopoverPanel, PopoverTrigger, SegmentedControl, TextInput, ViewportGate, confirmDiscardChanges, dismissTopDialog, presentDialog, toast } from "@haklex/rich-editor-ui";
|
|
7
|
+
import { $createParagraphNode, $getNodeByKey, $getSelection, $insertNodes, $isRangeSelection, COMMAND_PRIORITY_EDITOR, FORMAT_TEXT_COMMAND, createCommand } from "lexical";
|
|
8
|
+
import { usePortalTheme } from "@haklex/rich-style-token";
|
|
9
|
+
import { $isLinkNode, LinkNode, TOGGLE_LINK_COMMAND } from "@lexical/link";
|
|
10
|
+
import { $isListNode, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND, ListItemNode, ListNode, REMOVE_LIST_COMMAND } from "@lexical/list";
|
|
11
|
+
import { LexicalComposer } from "@lexical/react/LexicalComposer";
|
|
12
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
13
|
+
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
|
14
|
+
import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
|
|
15
|
+
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
|
|
16
|
+
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
|
|
17
|
+
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
|
|
18
|
+
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
|
19
|
+
import { $createHeadingNode, $isHeadingNode, HeadingNode, QuoteNode } from "@lexical/rich-text";
|
|
20
|
+
import { $patchStyleText, $setBlocksType } from "@lexical/selection";
|
|
21
|
+
import { createPortal } from "react-dom";
|
|
22
|
+
//#region src/WhiteboardEditNode.ts
|
|
23
|
+
var LazyEditRenderer = lazy(() => Promise.resolve().then(() => WhiteboardEditRenderer_exports).then((m) => ({ default: m.WhiteboardEditRenderer })));
|
|
24
|
+
var WhiteboardEditNode = class WhiteboardEditNode extends WhiteboardNode {
|
|
25
|
+
static clone(node) {
|
|
26
|
+
return new WhiteboardEditNode(node.__snapshot, node.__key);
|
|
27
|
+
}
|
|
28
|
+
constructor(snapshot, key) {
|
|
29
|
+
super(snapshot, key);
|
|
30
|
+
}
|
|
31
|
+
static importJSON(_serializedNode) {
|
|
32
|
+
return new WhiteboardEditNode(_serializedNode.snapshot).updateFromJSON(_serializedNode);
|
|
33
|
+
}
|
|
34
|
+
decorate(editor, config) {
|
|
35
|
+
const nodeKey = this.__key;
|
|
36
|
+
const fallback = createElement("div", {
|
|
37
|
+
className: "rich-excalidraw-loading",
|
|
38
|
+
style: {
|
|
39
|
+
position: "relative",
|
|
40
|
+
width: "100%",
|
|
41
|
+
aspectRatio: "16 / 10",
|
|
42
|
+
margin: "1rem 0"
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const editable = createElement(ViewportGate, {
|
|
46
|
+
fallback,
|
|
47
|
+
children: createElement(Suspense, { fallback }, createElement(LazyEditRenderer, {
|
|
48
|
+
snapshot: parseSnapshot(this.__snapshot),
|
|
49
|
+
onSnapshotChange: (snapshot) => {
|
|
50
|
+
editor.update(() => {
|
|
51
|
+
const node = $getNodeByKey(nodeKey);
|
|
52
|
+
if (node) node.setSnapshot(serializeSnapshot(snapshot));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}))
|
|
56
|
+
});
|
|
57
|
+
return createElement(EditableGate, {
|
|
58
|
+
editor,
|
|
59
|
+
editable,
|
|
60
|
+
readOnly: super.decorate(editor, config)
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
_defineProperty(WhiteboardEditNode, "commandItems", [{
|
|
65
|
+
title: "Whiteboard",
|
|
66
|
+
icon: createElement(PenTool, { size: 20 }),
|
|
67
|
+
description: "Whiteboard canvas",
|
|
68
|
+
keywords: [
|
|
69
|
+
"excalidraw",
|
|
70
|
+
"whiteboard",
|
|
71
|
+
"draw",
|
|
72
|
+
"canvas"
|
|
73
|
+
],
|
|
74
|
+
nested: false,
|
|
75
|
+
section: "MEDIA",
|
|
76
|
+
placement: ["slash", "toolbar"],
|
|
77
|
+
group: "insert",
|
|
78
|
+
onSelect: (editor) => {
|
|
79
|
+
editor.update(() => {
|
|
80
|
+
$insertNodes([$createWhiteboardEditNode("{}")]);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}]);
|
|
84
|
+
function $createWhiteboardEditNode(snapshot) {
|
|
85
|
+
return new WhiteboardEditNode(snapshot);
|
|
86
|
+
}
|
|
87
|
+
function $isWhiteboardEditNode(node) {
|
|
88
|
+
return node instanceof WhiteboardEditNode;
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/editor/text-actions.ts
|
|
92
|
+
var TEXT_CONTAINERS = /* @__PURE__ */ new Set([
|
|
93
|
+
"rectangle",
|
|
94
|
+
"ellipse",
|
|
95
|
+
"diamond",
|
|
96
|
+
"arrow"
|
|
97
|
+
]);
|
|
98
|
+
var created = /* @__PURE__ */ new WeakMap();
|
|
99
|
+
function canHoldText(el) {
|
|
100
|
+
return TEXT_CONTAINERS.has(el.type);
|
|
101
|
+
}
|
|
102
|
+
function getBoundTextId(el) {
|
|
103
|
+
return el.boundElements?.find((b) => b.type === "text")?.id ?? null;
|
|
104
|
+
}
|
|
105
|
+
function find(store, id) {
|
|
106
|
+
return store.getState().elements.find((el) => el.id === id);
|
|
107
|
+
}
|
|
108
|
+
function replace(store, next) {
|
|
109
|
+
const byId = new Map(next.map((el) => [el.id, el]));
|
|
110
|
+
store.updateTransient(({ elements }) => ({ elements: elements.map((el) => byId.get(el.id) ?? el) }));
|
|
111
|
+
}
|
|
112
|
+
function startTextEditing(store, opts) {
|
|
113
|
+
const state = store.getState();
|
|
114
|
+
store.beginTransient();
|
|
115
|
+
created.delete(store);
|
|
116
|
+
if (opts.containerId) {
|
|
117
|
+
const container = find(store, opts.containerId);
|
|
118
|
+
if (!container) return;
|
|
119
|
+
const existing = getBoundTextId(container);
|
|
120
|
+
if (existing && find(store, existing)) {
|
|
121
|
+
store.setState({
|
|
122
|
+
editingTextId: existing,
|
|
123
|
+
selectedIds: /* @__PURE__ */ new Set([container.id])
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const text = newTextElement({
|
|
128
|
+
text: "",
|
|
129
|
+
containerId: container.id,
|
|
130
|
+
textAlign: "center",
|
|
131
|
+
verticalAlign: "middle"
|
|
132
|
+
}, state.currentStyle);
|
|
133
|
+
const laid = layoutBoundText(container, text);
|
|
134
|
+
const nextContainer = bumpVersion(laid.container, { boundElements: [...container.boundElements ?? [], {
|
|
135
|
+
id: text.id,
|
|
136
|
+
type: "text"
|
|
137
|
+
}] });
|
|
138
|
+
store.updateTransient(({ elements }) => ({ elements: [...elements.map((el) => el.id === container.id ? nextContainer : el), laid.text] }));
|
|
139
|
+
created.set(store, text.id);
|
|
140
|
+
store.setState({
|
|
141
|
+
editingTextId: text.id,
|
|
142
|
+
selectedIds: /* @__PURE__ */ new Set([container.id])
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (opts.elementId) {
|
|
147
|
+
store.setState({
|
|
148
|
+
editingTextId: opts.elementId,
|
|
149
|
+
selectedIds: /* @__PURE__ */ new Set([opts.elementId])
|
|
150
|
+
});
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const at = opts.at ?? [0, 0];
|
|
154
|
+
const style = state.currentStyle;
|
|
155
|
+
const lineHeightPx = style.fontSize * getLineHeight(style.fontFamily);
|
|
156
|
+
const text = newTextElement({
|
|
157
|
+
text: "",
|
|
158
|
+
x: at[0],
|
|
159
|
+
y: at[1] - lineHeightPx / 2,
|
|
160
|
+
height: lineHeightPx
|
|
161
|
+
}, style);
|
|
162
|
+
store.updateTransient(({ elements }) => ({ elements: [...elements, text] }));
|
|
163
|
+
created.set(store, text.id);
|
|
164
|
+
store.setState({
|
|
165
|
+
editingTextId: text.id,
|
|
166
|
+
selectedIds: /* @__PURE__ */ new Set([text.id])
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function updateEditingText(store, value) {
|
|
170
|
+
const id = store.getState().editingTextId;
|
|
171
|
+
const el = id ? find(store, id) : void 0;
|
|
172
|
+
if (!el) return;
|
|
173
|
+
const draft = {
|
|
174
|
+
...el,
|
|
175
|
+
text: value,
|
|
176
|
+
originalText: value
|
|
177
|
+
};
|
|
178
|
+
const container = el.containerId ? find(store, el.containerId) : void 0;
|
|
179
|
+
if (container) {
|
|
180
|
+
const laid = layoutBoundText(container, draft);
|
|
181
|
+
replace(store, [laid.text, laid.container]);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
replace(store, [refreshTextDimensions(draft)]);
|
|
185
|
+
}
|
|
186
|
+
function finishTextEditing(store) {
|
|
187
|
+
const id = store.getState().editingTextId;
|
|
188
|
+
if (!id) return;
|
|
189
|
+
const el = find(store, id);
|
|
190
|
+
const isNew = created.get(store) === id;
|
|
191
|
+
created.delete(store);
|
|
192
|
+
store.setState({ editingTextId: null });
|
|
193
|
+
if (!el) {
|
|
194
|
+
store.endTransient(false);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const empty = !(el.originalText ?? el.text).trim();
|
|
198
|
+
if (empty && isNew) {
|
|
199
|
+
store.endTransient(false);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (empty) {
|
|
203
|
+
const container = el.containerId ? find(store, el.containerId) : void 0;
|
|
204
|
+
const updates = [bumpVersion(el, { isDeleted: true })];
|
|
205
|
+
if (container) updates.push(bumpVersion(container, { boundElements: (container.boundElements ?? []).filter((b) => b.id !== el.id) }));
|
|
206
|
+
replace(store, updates);
|
|
207
|
+
store.setState({ selectedIds: /* @__PURE__ */ new Set() });
|
|
208
|
+
} else if (!isNew) replace(store, [bumpVersion(el)]);
|
|
209
|
+
store.endTransient(true);
|
|
210
|
+
}
|
|
211
|
+
function relayoutBoundTexts(elements, containerIds) {
|
|
212
|
+
const byId = new Map(elements.map((el) => [el.id, el]));
|
|
213
|
+
const updates = /* @__PURE__ */ new Map();
|
|
214
|
+
for (const id of containerIds) {
|
|
215
|
+
const container = byId.get(id);
|
|
216
|
+
const textId = container ? getBoundTextId(container) : null;
|
|
217
|
+
const text = textId ? byId.get(textId) : void 0;
|
|
218
|
+
if (!container || !text || text.isDeleted) continue;
|
|
219
|
+
const laid = layoutBoundText(container, text);
|
|
220
|
+
updates.set(text.id, bumpVersion(text, laid.text));
|
|
221
|
+
if (laid.container !== container) updates.set(container.id, laid.container);
|
|
222
|
+
}
|
|
223
|
+
if (updates.size === 0) return [...elements];
|
|
224
|
+
return elements.map((el) => updates.get(el.id) ?? el);
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/canvas-state.ts
|
|
228
|
+
var EDITOR_SELECTOR = "[data-whiteboard-editor]";
|
|
229
|
+
function isEditorTarget(target) {
|
|
230
|
+
return target instanceof Element && !!target.closest(EDITOR_SELECTOR);
|
|
231
|
+
}
|
|
232
|
+
function editorOwnsEscape(store) {
|
|
233
|
+
const state = store.getState();
|
|
234
|
+
return !!(state.editingTextId || state.editingRichTextId || state.editingFrameId || store.isTransient() || state.tool !== "selection" && !state.toolLocked);
|
|
235
|
+
}
|
|
236
|
+
function releaseEscape(store, canvas) {
|
|
237
|
+
const state = store.getState();
|
|
238
|
+
if (state.editingTextId) {
|
|
239
|
+
finishTextEditing(store);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (state.editingRichTextId) {
|
|
243
|
+
store.setState({ editingRichTextId: null });
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (state.editingFrameId) {
|
|
247
|
+
store.setState({ editingFrameId: null });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
canvas?.dispatchEvent(new KeyboardEvent("keydown", {
|
|
251
|
+
key: "Escape",
|
|
252
|
+
bubbles: true,
|
|
253
|
+
cancelable: true
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
function createHistory(limit = 100) {
|
|
257
|
+
let undoStack = [];
|
|
258
|
+
let redoStack = [];
|
|
259
|
+
return {
|
|
260
|
+
push(entry) {
|
|
261
|
+
undoStack.push(entry);
|
|
262
|
+
if (undoStack.length > limit) undoStack = undoStack.slice(-limit);
|
|
263
|
+
redoStack = [];
|
|
264
|
+
},
|
|
265
|
+
undo(current) {
|
|
266
|
+
const entry = undoStack.pop();
|
|
267
|
+
if (!entry) return null;
|
|
268
|
+
redoStack.push(current);
|
|
269
|
+
return entry;
|
|
270
|
+
},
|
|
271
|
+
redo(current) {
|
|
272
|
+
const entry = redoStack.pop();
|
|
273
|
+
if (!entry) return null;
|
|
274
|
+
undoStack.push(current);
|
|
275
|
+
return entry;
|
|
276
|
+
},
|
|
277
|
+
rewrite(fn) {
|
|
278
|
+
undoStack = undoStack.map(fn);
|
|
279
|
+
redoStack = redoStack.map(fn);
|
|
280
|
+
},
|
|
281
|
+
canUndo: () => undoStack.length > 0,
|
|
282
|
+
canRedo: () => redoStack.length > 0,
|
|
283
|
+
clear() {
|
|
284
|
+
undoStack = [];
|
|
285
|
+
redoStack = [];
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/editor/store.ts
|
|
291
|
+
var DEFAULT_BACKGROUND = "#ffffff";
|
|
292
|
+
function normalizeChanged(prev, next) {
|
|
293
|
+
const before = new Map(prev.map((el) => [el.id, el]));
|
|
294
|
+
const normalized = next.map((el) => before.get(el.id) === el ? el : normalizeElement(el));
|
|
295
|
+
return syncIndices(normalized);
|
|
296
|
+
}
|
|
297
|
+
function referencedFileIds(elements) {
|
|
298
|
+
const ids = /* @__PURE__ */ new Set();
|
|
299
|
+
for (const el of elements) if (!el.isDeleted && el.type === "image" && el.fileId) ids.add(el.fileId);
|
|
300
|
+
return ids;
|
|
301
|
+
}
|
|
302
|
+
function createWhiteboardStore(input) {
|
|
303
|
+
const original = input ?? {};
|
|
304
|
+
const originalAppState = original.appState;
|
|
305
|
+
const originalFiles = original.files ?? {};
|
|
306
|
+
const initialElements = resolveRichTextHeights(Array.isArray(original.elements) ? original.elements : []);
|
|
307
|
+
const initialBackground = originalAppState?.viewBackgroundColor ?? DEFAULT_BACKGROUND;
|
|
308
|
+
const initialGrid = originalAppState?.gridSize ?? null;
|
|
309
|
+
let state = {
|
|
310
|
+
elements: initialElements,
|
|
311
|
+
files: originalFiles,
|
|
312
|
+
canvasSize: {
|
|
313
|
+
width: 0,
|
|
314
|
+
height: 0
|
|
315
|
+
},
|
|
316
|
+
selectedIds: /* @__PURE__ */ new Set(),
|
|
317
|
+
selectedPointIndex: null,
|
|
318
|
+
selectionBox: null,
|
|
319
|
+
suggestedBindingId: null,
|
|
320
|
+
editingGroupId: null,
|
|
321
|
+
editingFrameId: null,
|
|
322
|
+
editingLinearId: null,
|
|
323
|
+
editingRichTextId: null,
|
|
324
|
+
editingTextId: null,
|
|
325
|
+
erasingIds: /* @__PURE__ */ new Set(),
|
|
326
|
+
tool: "selection",
|
|
327
|
+
toolLocked: false,
|
|
328
|
+
currentStyle: DEFAULT_STYLE,
|
|
329
|
+
viewport: {
|
|
330
|
+
scrollX: 0,
|
|
331
|
+
scrollY: 0,
|
|
332
|
+
zoom: 1
|
|
333
|
+
},
|
|
334
|
+
gridSize: initialGrid,
|
|
335
|
+
viewBackgroundColor: initialBackground
|
|
336
|
+
};
|
|
337
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
338
|
+
const history = createHistory();
|
|
339
|
+
let transientSnapshot = null;
|
|
340
|
+
const emit = () => {
|
|
341
|
+
for (const listener of listeners) listener();
|
|
342
|
+
};
|
|
343
|
+
const set = (patch) => {
|
|
344
|
+
state = {
|
|
345
|
+
...state,
|
|
346
|
+
...patch
|
|
347
|
+
};
|
|
348
|
+
emit();
|
|
349
|
+
};
|
|
350
|
+
const docOf = () => ({
|
|
351
|
+
elements: state.elements,
|
|
352
|
+
files: state.files
|
|
353
|
+
});
|
|
354
|
+
const apply = (updater) => {
|
|
355
|
+
const current = docOf();
|
|
356
|
+
const result = updater(current);
|
|
357
|
+
if (!result) return null;
|
|
358
|
+
const elements = result.elements ? resolveRichTextHeights(normalizeChanged(current.elements, result.elements)) : current.elements;
|
|
359
|
+
const files = result.files ?? current.files;
|
|
360
|
+
if (elements === current.elements && files === current.files) return null;
|
|
361
|
+
return {
|
|
362
|
+
elements,
|
|
363
|
+
files
|
|
364
|
+
};
|
|
365
|
+
};
|
|
366
|
+
const restore = (doc) => {
|
|
367
|
+
const alive = new Set(doc.elements.filter((el) => !el.isDeleted).map((el) => el.id));
|
|
368
|
+
set({
|
|
369
|
+
elements: doc.elements,
|
|
370
|
+
files: doc.files,
|
|
371
|
+
selectedIds: new Set([...state.selectedIds].filter((id) => alive.has(id))),
|
|
372
|
+
editingTextId: null,
|
|
373
|
+
editingFrameId: null,
|
|
374
|
+
editingLinearId: null,
|
|
375
|
+
selectedPointIndex: null,
|
|
376
|
+
suggestedBindingId: null
|
|
377
|
+
});
|
|
378
|
+
};
|
|
379
|
+
return {
|
|
380
|
+
getState: () => state,
|
|
381
|
+
subscribe(listener) {
|
|
382
|
+
listeners.add(listener);
|
|
383
|
+
return () => listeners.delete(listener);
|
|
384
|
+
},
|
|
385
|
+
setState: set,
|
|
386
|
+
commit(updater) {
|
|
387
|
+
if (transientSnapshot) {
|
|
388
|
+
const next = apply(updater);
|
|
389
|
+
if (next) set(next);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const before = docOf();
|
|
393
|
+
const next = apply(updater);
|
|
394
|
+
if (!next) return;
|
|
395
|
+
history.push(before);
|
|
396
|
+
set(next);
|
|
397
|
+
},
|
|
398
|
+
amend(fn) {
|
|
399
|
+
history.rewrite(fn);
|
|
400
|
+
if (transientSnapshot) transientSnapshot = fn(transientSnapshot);
|
|
401
|
+
const next = apply((doc) => fn(doc));
|
|
402
|
+
if (next) set(next);
|
|
403
|
+
},
|
|
404
|
+
beginTransient() {
|
|
405
|
+
transientSnapshot ?? (transientSnapshot = docOf());
|
|
406
|
+
},
|
|
407
|
+
updateTransient(updater) {
|
|
408
|
+
const next = apply(updater);
|
|
409
|
+
if (next) set(next);
|
|
410
|
+
},
|
|
411
|
+
endTransient(keep) {
|
|
412
|
+
const snapshot = transientSnapshot;
|
|
413
|
+
transientSnapshot = null;
|
|
414
|
+
if (!snapshot) return;
|
|
415
|
+
const unchanged = snapshot.elements === state.elements && snapshot.files === state.files;
|
|
416
|
+
if (!keep) {
|
|
417
|
+
if (!unchanged) restore(snapshot);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (!unchanged) history.push(snapshot);
|
|
421
|
+
},
|
|
422
|
+
isTransient: () => transientSnapshot !== null,
|
|
423
|
+
undo() {
|
|
424
|
+
const entry = history.undo(docOf());
|
|
425
|
+
if (entry) restore(entry);
|
|
426
|
+
},
|
|
427
|
+
redo() {
|
|
428
|
+
const entry = history.redo(docOf());
|
|
429
|
+
if (entry) restore(entry);
|
|
430
|
+
},
|
|
431
|
+
canUndo: history.canUndo,
|
|
432
|
+
canRedo: history.canRedo,
|
|
433
|
+
isDirty: () => state.elements !== initialElements || state.files !== originalFiles || state.viewBackgroundColor !== initialBackground || state.gridSize !== initialGrid,
|
|
434
|
+
getScene() {
|
|
435
|
+
const scene = {
|
|
436
|
+
...original,
|
|
437
|
+
elements: state.elements.filter((el) => !el.isDeleted)
|
|
438
|
+
};
|
|
439
|
+
const appStatePatch = {};
|
|
440
|
+
if (state.viewBackgroundColor !== initialBackground) appStatePatch.viewBackgroundColor = state.viewBackgroundColor;
|
|
441
|
+
if (state.gridSize !== initialGrid) appStatePatch.gridSize = state.gridSize;
|
|
442
|
+
if (originalAppState || Object.keys(appStatePatch).length > 0) scene.appState = {
|
|
443
|
+
...originalAppState,
|
|
444
|
+
...appStatePatch
|
|
445
|
+
};
|
|
446
|
+
const referenced = referencedFileIds(state.elements);
|
|
447
|
+
const files = { ...originalFiles };
|
|
448
|
+
for (const [id, file] of Object.entries(state.files)) if (!(id in originalFiles) && referenced.has(id)) files[id] = file;
|
|
449
|
+
if ("files" in original || Object.keys(files).length > 0) scene.files = files;
|
|
450
|
+
return scene;
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
function useStore(store, selector) {
|
|
455
|
+
return useSyncExternalStore(store.subscribe, () => selector(store.getState()), () => selector(store.getState()));
|
|
456
|
+
}
|
|
457
|
+
function useNarrowLayout(store) {
|
|
458
|
+
return useStore(store, (s) => s.canvasSize.width > 0 && s.canvasSize.width < 640);
|
|
459
|
+
}
|
|
460
|
+
//#endregion
|
|
461
|
+
//#region src/editor/chrome.css.ts
|
|
462
|
+
var floating = "_14kbw0v0";
|
|
463
|
+
var toolbar = "_14kbw0v1";
|
|
464
|
+
var toolButton = "_14kbw0v3";
|
|
465
|
+
var separator = "_14kbw0v5";
|
|
466
|
+
var panel = "_14kbw0v6";
|
|
467
|
+
var row = "_14kbw0v7";
|
|
468
|
+
var rowLabel = "_14kbw0v8";
|
|
469
|
+
var options = "_14kbw0v9";
|
|
470
|
+
var swatch = "_14kbw0va";
|
|
471
|
+
var transparentSwatch = "_14kbw0vb";
|
|
472
|
+
var range = "_14kbw0vc";
|
|
473
|
+
var zoom = "_14kbw0vd";
|
|
474
|
+
var zoomTop = "_14kbw0vf";
|
|
475
|
+
var dock = "_14kbw0vg";
|
|
476
|
+
var sheet = "_14kbw0vh";
|
|
477
|
+
var grabber = "_14kbw0vi";
|
|
478
|
+
var formatBar = "_14kbw0vk";
|
|
479
|
+
var formatSwatch = "_14kbw0vl";
|
|
480
|
+
var formatBarInput = "_14kbw0vm";
|
|
481
|
+
//#endregion
|
|
482
|
+
//#region src/editor/Toolbar.tsx
|
|
483
|
+
var TOOLS = [
|
|
484
|
+
{
|
|
485
|
+
tool: "hand",
|
|
486
|
+
icon: Hand,
|
|
487
|
+
key: "H",
|
|
488
|
+
label: "Hand"
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
tool: "selection",
|
|
492
|
+
icon: MousePointer2,
|
|
493
|
+
key: "V",
|
|
494
|
+
label: "Select"
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
tool: "rectangle",
|
|
498
|
+
icon: Square,
|
|
499
|
+
key: "R",
|
|
500
|
+
label: "Rectangle"
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
tool: "diamond",
|
|
504
|
+
icon: Diamond,
|
|
505
|
+
key: "D",
|
|
506
|
+
label: "Diamond"
|
|
507
|
+
},
|
|
508
|
+
{
|
|
509
|
+
tool: "ellipse",
|
|
510
|
+
icon: Circle,
|
|
511
|
+
key: "O",
|
|
512
|
+
label: "Ellipse"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
tool: "arrow",
|
|
516
|
+
icon: MoveRight,
|
|
517
|
+
key: "A",
|
|
518
|
+
label: "Arrow"
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
tool: "line",
|
|
522
|
+
icon: Minus,
|
|
523
|
+
key: "L",
|
|
524
|
+
label: "Line"
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
tool: "freedraw",
|
|
528
|
+
icon: Pencil,
|
|
529
|
+
key: "P",
|
|
530
|
+
label: "Draw"
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
tool: "text",
|
|
534
|
+
icon: Type,
|
|
535
|
+
key: "T",
|
|
536
|
+
label: "Text"
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
tool: "card",
|
|
540
|
+
icon: StickyNote,
|
|
541
|
+
key: "N",
|
|
542
|
+
label: "Card"
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
tool: "image",
|
|
546
|
+
icon: Image$1,
|
|
547
|
+
key: "I",
|
|
548
|
+
label: "Image"
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
tool: "frame",
|
|
552
|
+
icon: Frame,
|
|
553
|
+
key: "F",
|
|
554
|
+
label: "Frame"
|
|
555
|
+
},
|
|
556
|
+
{
|
|
557
|
+
tool: "eraser",
|
|
558
|
+
icon: Eraser,
|
|
559
|
+
key: "E",
|
|
560
|
+
label: "Eraser"
|
|
561
|
+
}
|
|
562
|
+
];
|
|
563
|
+
var WhiteboardToolbar = ({ className, onToolSelect, showKeys = true, store }) => {
|
|
564
|
+
const tool = useStore(store, (s) => s.tool);
|
|
565
|
+
const locked = useStore(store, (s) => s.toolLocked);
|
|
566
|
+
const LockIcon = locked ? Lock : LockOpen;
|
|
567
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
568
|
+
className: `${toolbar}${className ? ` ${className}` : ""}`,
|
|
569
|
+
role: "toolbar",
|
|
570
|
+
children: [
|
|
571
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
572
|
+
icon: true,
|
|
573
|
+
"aria-label": locked ? "Unlock tool" : "Keep tool active",
|
|
574
|
+
"aria-pressed": locked,
|
|
575
|
+
className: toolButton,
|
|
576
|
+
size: "lg",
|
|
577
|
+
variant: "ghost",
|
|
578
|
+
onClick: () => store.setState({ toolLocked: !locked }),
|
|
579
|
+
children: /* @__PURE__ */ jsx(LockIcon, { size: 16 })
|
|
580
|
+
}),
|
|
581
|
+
/* @__PURE__ */ jsx("span", { className: separator }),
|
|
582
|
+
TOOLS.map(({ tool: value, icon: Icon, key, label }) => /* @__PURE__ */ jsxs(ActionButton, {
|
|
583
|
+
icon: true,
|
|
584
|
+
"aria-label": `${label} (${key})`,
|
|
585
|
+
"aria-pressed": tool === value,
|
|
586
|
+
className: toolButton,
|
|
587
|
+
"data-tool": value,
|
|
588
|
+
size: "lg",
|
|
589
|
+
title: `${label} — ${key}`,
|
|
590
|
+
variant: "ghost",
|
|
591
|
+
onClick: () => {
|
|
592
|
+
if (onToolSelect?.(value) === false) return;
|
|
593
|
+
store.setState({
|
|
594
|
+
tool: value,
|
|
595
|
+
selectedIds: value === "selection" ? store.getState().selectedIds : /* @__PURE__ */ new Set()
|
|
596
|
+
});
|
|
597
|
+
},
|
|
598
|
+
children: [/* @__PURE__ */ jsx(Icon, { size: 16 }), showKeys && /* @__PURE__ */ jsx("span", {
|
|
599
|
+
className: "_14kbw0v4",
|
|
600
|
+
children: key
|
|
601
|
+
})]
|
|
602
|
+
}, value))
|
|
603
|
+
]
|
|
604
|
+
});
|
|
605
|
+
};
|
|
606
|
+
//#endregion
|
|
607
|
+
//#region src/editor/editor.css.ts
|
|
608
|
+
var root = "_17gtqeu0";
|
|
609
|
+
var canvas = "_17gtqeu1";
|
|
610
|
+
var layer = "_17gtqeu2";
|
|
611
|
+
var accentStroke = "var(--rc-accent)";
|
|
612
|
+
//#endregion
|
|
613
|
+
//#region src/editor/text-editor.css.ts
|
|
614
|
+
var textEditor = "gb4lm20";
|
|
615
|
+
var frameNameEditor = "gb4lm21";
|
|
616
|
+
//#endregion
|
|
617
|
+
//#region src/editor/viewport.ts
|
|
618
|
+
var MIN_ZOOM = .1;
|
|
619
|
+
function clampZoom(zoom) {
|
|
620
|
+
return Math.min(30, Math.max(MIN_ZOOM, zoom));
|
|
621
|
+
}
|
|
622
|
+
function screenToScene([sx, sy], vp) {
|
|
623
|
+
return [sx / vp.zoom - vp.scrollX, sy / vp.zoom - vp.scrollY];
|
|
624
|
+
}
|
|
625
|
+
function sceneToScreen([x, y], vp) {
|
|
626
|
+
return [(x + vp.scrollX) * vp.zoom, (y + vp.scrollY) * vp.zoom];
|
|
627
|
+
}
|
|
628
|
+
function zoomAt(vp, screen, nextZoom) {
|
|
629
|
+
const zoom = clampZoom(nextZoom);
|
|
630
|
+
const [x, y] = screenToScene(screen, vp);
|
|
631
|
+
return {
|
|
632
|
+
zoom,
|
|
633
|
+
scrollX: screen[0] / zoom - x,
|
|
634
|
+
scrollY: screen[1] / zoom - y
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
function fitToContent(bounds, size, padding = 48) {
|
|
638
|
+
const [minX, minY, maxX, maxY] = bounds;
|
|
639
|
+
const w = Math.max(1, maxX - minX);
|
|
640
|
+
const h = Math.max(1, maxY - minY);
|
|
641
|
+
const zoom = clampZoom(Math.min(1, (size.width - padding * 2) / w, (size.height - padding * 2) / h));
|
|
642
|
+
return {
|
|
643
|
+
zoom,
|
|
644
|
+
scrollX: size.width / 2 / zoom - (minX + w / 2),
|
|
645
|
+
scrollY: size.height / 2 / zoom - (minY + h / 2)
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
//#endregion
|
|
649
|
+
//#region src/model/clipboard.ts
|
|
650
|
+
var CLIPBOARD_TYPE = "excalidraw/clipboard";
|
|
651
|
+
function serializeClipboard(elements, files) {
|
|
652
|
+
const used = {};
|
|
653
|
+
for (const el of elements) if (el.type === "image" && el.fileId && files[el.fileId]) used[el.fileId] = files[el.fileId];
|
|
654
|
+
return JSON.stringify({
|
|
655
|
+
type: CLIPBOARD_TYPE,
|
|
656
|
+
elements,
|
|
657
|
+
files: used
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
function parseClipboard(raw) {
|
|
661
|
+
const text = raw.trim();
|
|
662
|
+
if (!text) return null;
|
|
663
|
+
if (text.startsWith("{")) try {
|
|
664
|
+
const json = JSON.parse(text);
|
|
665
|
+
if ((json.type === "excalidraw/clipboard" || json.type === "excalidraw") && Array.isArray(json.elements)) return {
|
|
666
|
+
elements: json.elements.filter((el) => !el.isDeleted),
|
|
667
|
+
files: json.files ?? {}
|
|
668
|
+
};
|
|
669
|
+
return null;
|
|
670
|
+
} catch {
|
|
671
|
+
return { text: raw };
|
|
672
|
+
}
|
|
673
|
+
return { text: raw };
|
|
674
|
+
}
|
|
675
|
+
//#endregion
|
|
676
|
+
//#region src/model/clone.ts
|
|
677
|
+
function remapBinding(binding, idMap) {
|
|
678
|
+
if (!binding) return null;
|
|
679
|
+
const mapped = idMap.get(binding.elementId);
|
|
680
|
+
return mapped ? {
|
|
681
|
+
...binding,
|
|
682
|
+
elementId: mapped
|
|
683
|
+
} : null;
|
|
684
|
+
}
|
|
685
|
+
function cloneElements(source, offset) {
|
|
686
|
+
const idMap = new Map(source.map((el) => [el.id, randomId()]));
|
|
687
|
+
const groupMap = /* @__PURE__ */ new Map();
|
|
688
|
+
const mapGroup = (g) => {
|
|
689
|
+
if (!groupMap.has(g)) groupMap.set(g, randomId());
|
|
690
|
+
return groupMap.get(g);
|
|
691
|
+
};
|
|
692
|
+
return {
|
|
693
|
+
clones: source.map((el) => {
|
|
694
|
+
const clone = {
|
|
695
|
+
...el,
|
|
696
|
+
id: idMap.get(el.id),
|
|
697
|
+
x: el.x + offset[0],
|
|
698
|
+
y: el.y + offset[1],
|
|
699
|
+
seed: randomInteger(),
|
|
700
|
+
version: 1,
|
|
701
|
+
versionNonce: randomInteger(),
|
|
702
|
+
updated: Date.now(),
|
|
703
|
+
index: null,
|
|
704
|
+
groupIds: (el.groupIds ?? []).map(mapGroup),
|
|
705
|
+
boundElements: el.boundElements?.filter((b) => idMap.has(b.id)).map((b) => ({
|
|
706
|
+
...b,
|
|
707
|
+
id: idMap.get(b.id)
|
|
708
|
+
})) ?? null
|
|
709
|
+
};
|
|
710
|
+
if (el.type === "text") clone.containerId = el.containerId ? idMap.get(el.containerId) ?? null : null;
|
|
711
|
+
if (el.type === "arrow" || el.type === "line") {
|
|
712
|
+
const linear = el;
|
|
713
|
+
clone.startBinding = remapBinding(linear.startBinding, idMap);
|
|
714
|
+
clone.endBinding = remapBinding(linear.endBinding, idMap);
|
|
715
|
+
}
|
|
716
|
+
clone.frameId = el.frameId ? idMap.get(el.frameId) ?? null : null;
|
|
717
|
+
return clone;
|
|
718
|
+
}),
|
|
719
|
+
idMap
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
//#endregion
|
|
723
|
+
//#region src/model/frames.ts
|
|
724
|
+
function isFrame(el) {
|
|
725
|
+
return el.type === "frame" || el.type === "magicframe";
|
|
726
|
+
}
|
|
727
|
+
var containerOf = (el) => el.type === "text" ? el.containerId ?? null : null;
|
|
728
|
+
var encloses = (frame, el) => boundsContain(getElementBounds(frame), getElementBounds(el));
|
|
729
|
+
var overlaps = (frame, el) => boundsIntersect(getElementBounds(frame), getElementBounds(el));
|
|
730
|
+
function withFrame(el, frameId) {
|
|
731
|
+
return (el.frameId ?? null) === frameId ? el : bumpVersion(el, { frameId });
|
|
732
|
+
}
|
|
733
|
+
function syncBoundTexts(elements) {
|
|
734
|
+
const byId = new Map(elements.map((el) => [el.id, el]));
|
|
735
|
+
return elements.map((el) => {
|
|
736
|
+
const container = containerOf(el);
|
|
737
|
+
const owner = container ? byId.get(container) : void 0;
|
|
738
|
+
return owner ? withFrame(el, owner.frameId ?? null) : el;
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
function assignFrameMembership(elements, frameId) {
|
|
742
|
+
const frame = elements.find((el) => el.id === frameId);
|
|
743
|
+
if (!frame || frame.isDeleted) return [...elements];
|
|
744
|
+
return syncBoundTexts(elements.map((el) => {
|
|
745
|
+
if (el.id === frameId || el.isDeleted || isFrame(el) || containerOf(el)) return el;
|
|
746
|
+
if (el.frameId && el.frameId !== frameId) return el;
|
|
747
|
+
if (el.frameId === frameId) return withFrame(el, overlaps(frame, el) ? frameId : null);
|
|
748
|
+
return withFrame(el, encloses(frame, el) ? frameId : null);
|
|
749
|
+
}));
|
|
750
|
+
}
|
|
751
|
+
function updateFrameMembershipAfterMove(elements, movedIds) {
|
|
752
|
+
const frames = elements.filter((el) => isFrame(el) && !el.isDeleted);
|
|
753
|
+
return syncBoundTexts(elements.map((el) => {
|
|
754
|
+
if (!movedIds.has(el.id) || el.isDeleted || isFrame(el) || containerOf(el)) return el;
|
|
755
|
+
if (el.frameId && movedIds.has(el.frameId)) return el;
|
|
756
|
+
const current = el.frameId ? frames.find((frame) => frame.id === el.frameId) : void 0;
|
|
757
|
+
if (current && overlaps(current, el)) return el;
|
|
758
|
+
return withFrame(el, frames.findLast((frame) => encloses(frame, el))?.id ?? null);
|
|
759
|
+
}));
|
|
760
|
+
}
|
|
761
|
+
function refreshFrames(elements, changedIds) {
|
|
762
|
+
let current = updateFrameMembershipAfterMove(elements, changedIds);
|
|
763
|
+
for (const el of elements) if (changedIds.has(el.id) && isFrame(el)) current = assignFrameMembership(current, el.id);
|
|
764
|
+
return current;
|
|
765
|
+
}
|
|
766
|
+
function withFrameChildren(elements, ids) {
|
|
767
|
+
const out = new Set(ids);
|
|
768
|
+
const frames = new Set(elements.filter((el) => ids.has(el.id) && isFrame(el)).map((el) => el.id));
|
|
769
|
+
if (frames.size === 0) return out;
|
|
770
|
+
for (const el of elements) if (!el.isDeleted && el.frameId && frames.has(el.frameId)) out.add(el.id);
|
|
771
|
+
return out;
|
|
772
|
+
}
|
|
773
|
+
//#endregion
|
|
774
|
+
//#region src/editor/selection-utils.ts
|
|
775
|
+
var isAlive = (el) => !el.isDeleted;
|
|
776
|
+
function isSelectable(el) {
|
|
777
|
+
return !el.isDeleted && !el.locked;
|
|
778
|
+
}
|
|
779
|
+
function getContainerId(el) {
|
|
780
|
+
return el.type === "text" ? el.containerId ?? null : null;
|
|
781
|
+
}
|
|
782
|
+
function groupMembers(elements, groupId) {
|
|
783
|
+
return elements.filter((el) => isAlive(el) && el.groupIds?.includes(groupId)).map((el) => el.id);
|
|
784
|
+
}
|
|
785
|
+
function selectionForElement(state, el) {
|
|
786
|
+
const groups = el.groupIds ?? [];
|
|
787
|
+
if (groups.length === 0) return [el.id];
|
|
788
|
+
const editing = state.editingGroupId;
|
|
789
|
+
const depth = editing ? groups.indexOf(editing) : -1;
|
|
790
|
+
if (depth === 0) return [el.id];
|
|
791
|
+
const target = depth > 0 ? groups[depth - 1] : groups.at(-1);
|
|
792
|
+
return groupMembers(state.elements, target);
|
|
793
|
+
}
|
|
794
|
+
function getHitElement(elements, p, tolerance) {
|
|
795
|
+
const byId = new Map(elements.map((el) => [el.id, el]));
|
|
796
|
+
for (let i = elements.length - 1; i >= 0; i--) {
|
|
797
|
+
const el = elements[i];
|
|
798
|
+
if (!isSelectable(el)) continue;
|
|
799
|
+
if (!hitTestElement(el, p, tolerance)) continue;
|
|
800
|
+
const containerId = getContainerId(el);
|
|
801
|
+
const container = containerId ? byId.get(containerId) : void 0;
|
|
802
|
+
if (container && isSelectable(container)) return container;
|
|
803
|
+
return el;
|
|
804
|
+
}
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
function getSelectionTarget(state, p, tolerance) {
|
|
808
|
+
const hit = getHitElement(state.elements, p, tolerance);
|
|
809
|
+
if (!hit) return null;
|
|
810
|
+
return {
|
|
811
|
+
hitId: hit.id,
|
|
812
|
+
ids: selectionForElement(state, hit)
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
function withBoundText(elements, ids) {
|
|
816
|
+
const out = new Set(ids);
|
|
817
|
+
for (const el of elements) {
|
|
818
|
+
const containerId = getContainerId(el);
|
|
819
|
+
if (containerId && ids.has(containerId) && isAlive(el)) out.add(el.id);
|
|
820
|
+
}
|
|
821
|
+
return out;
|
|
822
|
+
}
|
|
823
|
+
function selectInBox(state, box) {
|
|
824
|
+
const candidates = state.elements.filter((el) => isSelectable(el) && !getContainerId(el));
|
|
825
|
+
const inside = new Set(elementsInBox(candidates, box).map((el) => el.id));
|
|
826
|
+
const result = /* @__PURE__ */ new Set();
|
|
827
|
+
for (const id of inside) {
|
|
828
|
+
const ids = selectionForElement(state, state.elements.find((e) => e.id === id));
|
|
829
|
+
if (ids.every((member) => inside.has(member))) for (const member of ids) result.add(member);
|
|
830
|
+
}
|
|
831
|
+
return result;
|
|
832
|
+
}
|
|
833
|
+
function getSelectedElements(state) {
|
|
834
|
+
return state.elements.filter((el) => isAlive(el) && state.selectedIds.has(el.id));
|
|
835
|
+
}
|
|
836
|
+
//#endregion
|
|
837
|
+
//#region src/editor/actions.ts
|
|
838
|
+
var DUPLICATE_OFFSET = 10;
|
|
839
|
+
function selectedWithText(store) {
|
|
840
|
+
const state = store.getState();
|
|
841
|
+
return withBoundText(state.elements, state.selectedIds);
|
|
842
|
+
}
|
|
843
|
+
function reorder(store, arrange) {
|
|
844
|
+
const ids = selectedWithText(store);
|
|
845
|
+
if (ids.size === 0) return;
|
|
846
|
+
store.commit(({ elements }) => {
|
|
847
|
+
const arranged = arrange([...elements], ids);
|
|
848
|
+
if (arranged.every((el, i) => el === elements[i])) return;
|
|
849
|
+
return { elements: arranged.map((el) => ids.has(el.id) ? bumpVersion(el, { index: null }) : el) };
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
var bringToFront = (store) => reorder(store, (els, ids) => [...els.filter((el) => !ids.has(el.id)), ...els.filter((el) => ids.has(el.id))]);
|
|
853
|
+
var sendToBack = (store) => reorder(store, (els, ids) => [...els.filter((el) => ids.has(el.id)), ...els.filter((el) => !ids.has(el.id))]);
|
|
854
|
+
var bringForward = (store) => reorder(store, (els, ids) => {
|
|
855
|
+
for (let i = els.length - 2; i >= 0; i--) if (ids.has(els[i].id) && !ids.has(els[i + 1].id)) [els[i], els[i + 1]] = [els[i + 1], els[i]];
|
|
856
|
+
return els;
|
|
857
|
+
});
|
|
858
|
+
var sendBackward = (store) => reorder(store, (els, ids) => {
|
|
859
|
+
for (let i = 1; i < els.length; i++) if (ids.has(els[i].id) && !ids.has(els[i - 1].id)) [els[i], els[i - 1]] = [els[i - 1], els[i]];
|
|
860
|
+
return els;
|
|
861
|
+
});
|
|
862
|
+
function groupSelection(store) {
|
|
863
|
+
const ids = selectedWithText(store);
|
|
864
|
+
if (getSelectedElements(store.getState()).length < 2) return;
|
|
865
|
+
const groupId = randomId();
|
|
866
|
+
store.commit(({ elements }) => ({ elements: elements.map((el) => ids.has(el.id) ? bumpVersion(el, { groupIds: [...el.groupIds ?? [], groupId] }) : el) }));
|
|
867
|
+
}
|
|
868
|
+
function ungroupSelection(store) {
|
|
869
|
+
const ids = selectedWithText(store);
|
|
870
|
+
const outer = new Set(getSelectedElements(store.getState()).map((el) => el.groupIds?.at(-1)).filter((g) => !!g));
|
|
871
|
+
if (outer.size === 0) return;
|
|
872
|
+
store.commit(({ elements }) => ({ elements: elements.map((el) => ids.has(el.id) && el.groupIds?.some((g) => outer.has(g)) ? bumpVersion(el, { groupIds: el.groupIds.filter((g) => !outer.has(g)) }) : el) }));
|
|
873
|
+
store.setState({ editingGroupId: null });
|
|
874
|
+
}
|
|
875
|
+
function duplicateSelection(store) {
|
|
876
|
+
const state = store.getState();
|
|
877
|
+
const ids = selectedWithText(store);
|
|
878
|
+
if (ids.size === 0) return;
|
|
879
|
+
const { clones, idMap } = cloneElements(state.elements.filter((el) => ids.has(el.id) && !el.isDeleted), [DUPLICATE_OFFSET, DUPLICATE_OFFSET]);
|
|
880
|
+
store.commit(({ elements }) => ({ elements: refreshFrames([...elements, ...clones], new Set(clones.map((el) => el.id))) }));
|
|
881
|
+
store.setState({ selectedIds: new Set([...state.selectedIds].map((id) => idMap.get(id)).filter((id) => !!id)) });
|
|
882
|
+
}
|
|
883
|
+
function deleteElements(elements, ids) {
|
|
884
|
+
return elements.map((el) => {
|
|
885
|
+
if (ids.has(el.id)) return bumpVersion(el, { isDeleted: true });
|
|
886
|
+
const patch = {};
|
|
887
|
+
if (el.boundElements?.some((b) => ids.has(b.id))) patch.boundElements = el.boundElements.filter((b) => !ids.has(b.id));
|
|
888
|
+
if (el.type === "arrow" || el.type === "line") {
|
|
889
|
+
const linear = el;
|
|
890
|
+
if (linear.startBinding && ids.has(linear.startBinding.elementId)) patch.startBinding = null;
|
|
891
|
+
if (linear.endBinding && ids.has(linear.endBinding.elementId)) patch.endBinding = null;
|
|
892
|
+
}
|
|
893
|
+
if (el.frameId && ids.has(el.frameId)) patch.frameId = null;
|
|
894
|
+
return Object.keys(patch).length > 0 ? bumpVersion(el, patch) : el;
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
function deleteSelection(store) {
|
|
898
|
+
const ids = selectedWithText(store);
|
|
899
|
+
if (ids.size === 0) return;
|
|
900
|
+
store.commit(({ elements }) => ({ elements: deleteElements(elements, ids) }));
|
|
901
|
+
store.setState({
|
|
902
|
+
selectedIds: /* @__PURE__ */ new Set(),
|
|
903
|
+
selectedPointIndex: null
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
function selectAll(store) {
|
|
907
|
+
const state = store.getState();
|
|
908
|
+
store.setState({
|
|
909
|
+
selectedIds: new Set(state.elements.filter((el) => !el.isDeleted && !el.locked && !(el.type === "text" && el.containerId)).map((el) => el.id)),
|
|
910
|
+
editingGroupId: null
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
var FALLBACK_SIDE = 200;
|
|
914
|
+
var domSizeReader = (url) => new Promise((resolve, reject) => {
|
|
915
|
+
const img = new Image();
|
|
916
|
+
img.addEventListener("load", () => resolve({
|
|
917
|
+
width: img.naturalWidth,
|
|
918
|
+
height: img.naturalHeight
|
|
919
|
+
}));
|
|
920
|
+
img.addEventListener("error", reject);
|
|
921
|
+
img.src = url;
|
|
922
|
+
});
|
|
923
|
+
var readSize = domSizeReader;
|
|
924
|
+
function fitImageSize(width, height, max = 600) {
|
|
925
|
+
const scale = Math.min(1, max / Math.max(width, height));
|
|
926
|
+
return {
|
|
927
|
+
width: Math.round(width * scale),
|
|
928
|
+
height: Math.round(height * scale)
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
function readAsDataURL(file) {
|
|
932
|
+
return new Promise((resolve, reject) => {
|
|
933
|
+
const reader = new FileReader();
|
|
934
|
+
reader.addEventListener("load", () => resolve(String(reader.result)));
|
|
935
|
+
reader.addEventListener("error", () => reject(reader.error));
|
|
936
|
+
reader.readAsDataURL(file);
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
async function insertImageFile(store, file, at, options = {}) {
|
|
940
|
+
const dataURL = await readAsDataURL(file);
|
|
941
|
+
const natural = await readSize(dataURL).catch(() => null);
|
|
942
|
+
const { width, height } = fitImageSize(natural?.width || FALLBACK_SIDE, natural?.height || FALLBACK_SIDE);
|
|
943
|
+
const fileId = randomId();
|
|
944
|
+
const el = newImageElement({
|
|
945
|
+
x: at[0] - width / 2,
|
|
946
|
+
y: at[1] - height / 2,
|
|
947
|
+
width,
|
|
948
|
+
height,
|
|
949
|
+
fileId,
|
|
950
|
+
status: "pending"
|
|
951
|
+
}, store.getState().currentStyle);
|
|
952
|
+
const record = {
|
|
953
|
+
id: fileId,
|
|
954
|
+
mimeType: file.type || "image/png",
|
|
955
|
+
created: Date.now(),
|
|
956
|
+
dataURL
|
|
957
|
+
};
|
|
958
|
+
store.commit(({ elements, files }) => ({
|
|
959
|
+
elements: [...elements, el],
|
|
960
|
+
files: {
|
|
961
|
+
...files,
|
|
962
|
+
[fileId]: record
|
|
963
|
+
}
|
|
964
|
+
}));
|
|
965
|
+
const { toolLocked, tool } = store.getState();
|
|
966
|
+
store.setState({
|
|
967
|
+
selectedIds: /* @__PURE__ */ new Set([el.id]),
|
|
968
|
+
tool: toolLocked ? tool : "selection"
|
|
969
|
+
});
|
|
970
|
+
let url = dataURL;
|
|
971
|
+
if (options.uploadFile) try {
|
|
972
|
+
url = await options.uploadFile(file);
|
|
973
|
+
} catch (error) {
|
|
974
|
+
options.onUploadError?.(error);
|
|
975
|
+
}
|
|
976
|
+
store.amend((doc) => {
|
|
977
|
+
if (!doc.elements.some((item) => item.id === el.id)) return doc;
|
|
978
|
+
const saved = doc.files[fileId];
|
|
979
|
+
return {
|
|
980
|
+
elements: doc.elements.map((item) => item.id === el.id ? bumpVersion(item, { status: "saved" }) : item),
|
|
981
|
+
files: saved ? {
|
|
982
|
+
...doc.files,
|
|
983
|
+
[fileId]: {
|
|
984
|
+
...saved,
|
|
985
|
+
dataURL: url
|
|
986
|
+
}
|
|
987
|
+
} : doc.files
|
|
988
|
+
};
|
|
989
|
+
});
|
|
990
|
+
return el.id;
|
|
991
|
+
}
|
|
992
|
+
//#endregion
|
|
993
|
+
//#region src/editor/clipboard-actions.ts
|
|
994
|
+
function copySelection(store) {
|
|
995
|
+
const state = store.getState();
|
|
996
|
+
const ids = withBoundText(state.elements, state.selectedIds);
|
|
997
|
+
return serializeClipboard(state.elements.filter((el) => ids.has(el.id) && !el.isDeleted), state.files);
|
|
998
|
+
}
|
|
999
|
+
function cutSelection(store) {
|
|
1000
|
+
const data = copySelection(store);
|
|
1001
|
+
deleteSelection(store);
|
|
1002
|
+
return data;
|
|
1003
|
+
}
|
|
1004
|
+
var SVG_FALLBACK_SIDE = 200;
|
|
1005
|
+
function pasteSvg(store, raw, at) {
|
|
1006
|
+
const svg = raw.trim();
|
|
1007
|
+
const size = parseSvgSize(svg) ?? {
|
|
1008
|
+
width: SVG_FALLBACK_SIDE,
|
|
1009
|
+
height: SVG_FALLBACK_SIDE
|
|
1010
|
+
};
|
|
1011
|
+
const scale = Math.min(1, 600 / Math.max(size.width, size.height));
|
|
1012
|
+
const width = size.width * scale;
|
|
1013
|
+
const height = size.height * scale;
|
|
1014
|
+
const el = newSvgElement({
|
|
1015
|
+
svg,
|
|
1016
|
+
width,
|
|
1017
|
+
height,
|
|
1018
|
+
x: at[0] - width / 2,
|
|
1019
|
+
y: at[1] - height / 2
|
|
1020
|
+
}, store.getState().currentStyle);
|
|
1021
|
+
store.commit(({ elements }) => ({ elements: refreshFrames([...elements, el], /* @__PURE__ */ new Set([el.id])) }));
|
|
1022
|
+
store.setState({
|
|
1023
|
+
selectedIds: /* @__PURE__ */ new Set([el.id]),
|
|
1024
|
+
editingGroupId: null
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
function pasteFromClipboard(store, raw, at) {
|
|
1028
|
+
if (isSvgSource(raw)) {
|
|
1029
|
+
pasteSvg(store, raw, at);
|
|
1030
|
+
return true;
|
|
1031
|
+
}
|
|
1032
|
+
const content = parseClipboard(raw);
|
|
1033
|
+
if (!content) return false;
|
|
1034
|
+
const style = store.getState().currentStyle;
|
|
1035
|
+
if ("text" in content) {
|
|
1036
|
+
const lineHeight = style.fontSize * getLineHeight(style.fontFamily);
|
|
1037
|
+
const text = refreshTextDimensions(newTextElement({
|
|
1038
|
+
text: content.text,
|
|
1039
|
+
x: at[0],
|
|
1040
|
+
y: at[1] - lineHeight / 2
|
|
1041
|
+
}, style));
|
|
1042
|
+
store.commit(({ elements }) => ({ elements: [...elements, text] }));
|
|
1043
|
+
store.setState({ selectedIds: /* @__PURE__ */ new Set([text.id]) });
|
|
1044
|
+
return true;
|
|
1045
|
+
}
|
|
1046
|
+
if (content.elements.length === 0) return false;
|
|
1047
|
+
const [minX, minY, maxX, maxY] = getCommonBounds(content.elements);
|
|
1048
|
+
const offset = [at[0] - (minX + maxX) / 2, at[1] - (minY + maxY) / 2];
|
|
1049
|
+
const { clones } = cloneElements(content.elements, offset);
|
|
1050
|
+
store.commit(({ elements, files }) => ({
|
|
1051
|
+
elements: refreshFrames([...elements, ...clones], new Set(clones.map((el) => el.id))),
|
|
1052
|
+
files: {
|
|
1053
|
+
...files,
|
|
1054
|
+
...content.files
|
|
1055
|
+
}
|
|
1056
|
+
}));
|
|
1057
|
+
store.setState({
|
|
1058
|
+
selectedIds: new Set(clones.filter((el) => !(el.type === "text" && el.containerId)).map((el) => el.id)),
|
|
1059
|
+
editingGroupId: null
|
|
1060
|
+
});
|
|
1061
|
+
return true;
|
|
1062
|
+
}
|
|
1063
|
+
//#endregion
|
|
1064
|
+
//#region src/editor/gestures.ts
|
|
1065
|
+
var midpoint = (a, b) => [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
1066
|
+
function createGestureTracker() {
|
|
1067
|
+
const points = /* @__PURE__ */ new Map();
|
|
1068
|
+
let pinch = null;
|
|
1069
|
+
let suppressed = false;
|
|
1070
|
+
return {
|
|
1071
|
+
down(id, screen, viewport) {
|
|
1072
|
+
points.set(id, screen);
|
|
1073
|
+
if (pinch || suppressed) return "ignored";
|
|
1074
|
+
if (points.size < 2) return "single";
|
|
1075
|
+
const [a, b] = [...points.keys()].slice(-2);
|
|
1076
|
+
const pa = points.get(a);
|
|
1077
|
+
const pb = points.get(b);
|
|
1078
|
+
pinch = {
|
|
1079
|
+
ids: [a, b],
|
|
1080
|
+
distance: Math.max(Math.hypot(pb[0] - pa[0], pb[1] - pa[1]), 1),
|
|
1081
|
+
zoom: viewport.zoom,
|
|
1082
|
+
anchor: screenToScene(midpoint(pa, pb), viewport)
|
|
1083
|
+
};
|
|
1084
|
+
return "pinch";
|
|
1085
|
+
},
|
|
1086
|
+
move(id, screen) {
|
|
1087
|
+
if (!points.has(id)) return null;
|
|
1088
|
+
points.set(id, screen);
|
|
1089
|
+
if (!pinch) return null;
|
|
1090
|
+
const pa = points.get(pinch.ids[0]);
|
|
1091
|
+
const pb = points.get(pinch.ids[1]);
|
|
1092
|
+
const zoom = clampZoom(pinch.zoom * Math.hypot(pb[0] - pa[0], pb[1] - pa[1]) / pinch.distance);
|
|
1093
|
+
const [mx, my] = midpoint(pa, pb);
|
|
1094
|
+
return {
|
|
1095
|
+
zoom,
|
|
1096
|
+
scrollX: mx / zoom - pinch.anchor[0],
|
|
1097
|
+
scrollY: my / zoom - pinch.anchor[1]
|
|
1098
|
+
};
|
|
1099
|
+
},
|
|
1100
|
+
up(id) {
|
|
1101
|
+
points.delete(id);
|
|
1102
|
+
if (pinch) {
|
|
1103
|
+
pinch = null;
|
|
1104
|
+
suppressed = true;
|
|
1105
|
+
}
|
|
1106
|
+
if (points.size === 0) suppressed = false;
|
|
1107
|
+
},
|
|
1108
|
+
isActive: () => pinch !== null || suppressed
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
//#endregion
|
|
1112
|
+
//#region src/editor/richtext-editor.css.ts
|
|
1113
|
+
var surface = "_1yp4ot60";
|
|
1114
|
+
var editable = "_1yp4ot61";
|
|
1115
|
+
var paragraph$1 = "_1yp4ot62";
|
|
1116
|
+
var h1 = "_1yp4ot63";
|
|
1117
|
+
var h2 = "_1yp4ot64";
|
|
1118
|
+
var h3 = "_1yp4ot65";
|
|
1119
|
+
var hOther = "_1yp4ot66";
|
|
1120
|
+
var quote = "_1yp4ot67";
|
|
1121
|
+
var list$1 = "_1yp4ot68";
|
|
1122
|
+
var listItem$1 = "_1yp4ot69";
|
|
1123
|
+
var nestedListItem = "_1yp4ot6a";
|
|
1124
|
+
var underline = "_1yp4ot6b";
|
|
1125
|
+
var strikethrough = "_1yp4ot6c";
|
|
1126
|
+
var underlineStrikethrough = "_1yp4ot6d";
|
|
1127
|
+
var link = "_1yp4ot6e";
|
|
1128
|
+
var editingCard = "_1yp4ot6f";
|
|
1129
|
+
//#endregion
|
|
1130
|
+
//#region src/editor/grid.ts
|
|
1131
|
+
function activeGrid(store, e) {
|
|
1132
|
+
const { gridSize } = store.getState();
|
|
1133
|
+
return gridSize && gridSize > 0 && !e.ctrlKey ? gridSize : null;
|
|
1134
|
+
}
|
|
1135
|
+
var snapValue = (value, grid) => grid ? Math.round(value / grid) * grid : value;
|
|
1136
|
+
var snapPoint = ([x, y], grid) => grid ? [snapValue(x, grid), snapValue(y, grid)] : [x, y];
|
|
1137
|
+
//#endregion
|
|
1138
|
+
//#region src/model/bindable.ts
|
|
1139
|
+
var BINDABLE = /* @__PURE__ */ new Set([
|
|
1140
|
+
"rectangle",
|
|
1141
|
+
"diamond",
|
|
1142
|
+
"ellipse",
|
|
1143
|
+
"image",
|
|
1144
|
+
"svg",
|
|
1145
|
+
"richtext",
|
|
1146
|
+
"text",
|
|
1147
|
+
"embeddable",
|
|
1148
|
+
"iframe"
|
|
1149
|
+
]);
|
|
1150
|
+
function isBindable(el) {
|
|
1151
|
+
if (el.isDeleted || !BINDABLE.has(el.type)) return false;
|
|
1152
|
+
return !(el.type === "text" && el.containerId);
|
|
1153
|
+
}
|
|
1154
|
+
var BEND_PENALTY = 60;
|
|
1155
|
+
var DIRS = [
|
|
1156
|
+
[1, 0],
|
|
1157
|
+
[0, 1],
|
|
1158
|
+
[-1, 0],
|
|
1159
|
+
[0, -1]
|
|
1160
|
+
];
|
|
1161
|
+
var dirIndex = ([x, y]) => DIRS.findIndex((d) => d[0] === x && d[1] === y);
|
|
1162
|
+
var same = (a, b) => a[0] === b[0] && a[1] === b[1];
|
|
1163
|
+
function headingForPoint(target, p) {
|
|
1164
|
+
const [minX, minY, maxX, maxY] = getElementBounds(target);
|
|
1165
|
+
const dx = (p[0] - (minX + maxX) / 2) / Math.max((maxX - minX) / 2, 1e-9);
|
|
1166
|
+
const dy = (p[1] - (minY + maxY) / 2) / Math.max((maxY - minY) / 2, 1e-9);
|
|
1167
|
+
if (Math.abs(dx) >= Math.abs(dy)) return dx < 0 ? [-1, 0] : [1, 0];
|
|
1168
|
+
return dy < 0 ? [0, -1] : [0, 1];
|
|
1169
|
+
}
|
|
1170
|
+
function towards(from, to) {
|
|
1171
|
+
const dx = to[0] - from[0];
|
|
1172
|
+
const dy = to[1] - from[1];
|
|
1173
|
+
if (Math.abs(dx) >= Math.abs(dy)) return dx < 0 ? [-1, 0] : [1, 0];
|
|
1174
|
+
return dy < 0 ? [0, -1] : [0, 1];
|
|
1175
|
+
}
|
|
1176
|
+
var expand = ([minX, minY, maxX, maxY], pad) => [
|
|
1177
|
+
minX - pad,
|
|
1178
|
+
minY - pad,
|
|
1179
|
+
maxX + pad,
|
|
1180
|
+
maxY + pad
|
|
1181
|
+
];
|
|
1182
|
+
var strictlyInside = ([x, y], [minX, minY, maxX, maxY]) => x > minX && x < maxX && y > minY && y < maxY;
|
|
1183
|
+
function crosses(a, b, [minX, minY, maxX, maxY]) {
|
|
1184
|
+
const x0 = Math.min(a[0], b[0]);
|
|
1185
|
+
const x1 = Math.max(a[0], b[0]);
|
|
1186
|
+
const y0 = Math.min(a[1], b[1]);
|
|
1187
|
+
const y1 = Math.max(a[1], b[1]);
|
|
1188
|
+
if (x0 === x1) return x0 > minX && x0 < maxX && y1 > minY && y0 < maxY;
|
|
1189
|
+
if (y0 === y1) return y0 > minY && y0 < maxY && x1 > minX && x0 < maxX;
|
|
1190
|
+
return x1 > minX && x0 < maxX && y1 > minY && y0 < maxY;
|
|
1191
|
+
}
|
|
1192
|
+
function exitPoint(p, heading, target) {
|
|
1193
|
+
if (!target) return p;
|
|
1194
|
+
const [minX, minY, maxX, maxY] = expand(getElementBounds(target), 20);
|
|
1195
|
+
if (heading[0] > 0) return [Math.max(maxX, p[0]), p[1]];
|
|
1196
|
+
if (heading[0] < 0) return [Math.min(minX, p[0]), p[1]];
|
|
1197
|
+
if (heading[1] > 0) return [p[0], Math.max(maxY, p[1])];
|
|
1198
|
+
return [p[0], Math.min(minY, p[1])];
|
|
1199
|
+
}
|
|
1200
|
+
var MinHeap = class {
|
|
1201
|
+
constructor() {
|
|
1202
|
+
_defineProperty(this, "items", []);
|
|
1203
|
+
_defineProperty(this, "seq", 0);
|
|
1204
|
+
}
|
|
1205
|
+
get size() {
|
|
1206
|
+
return this.items.length;
|
|
1207
|
+
}
|
|
1208
|
+
push(key, f) {
|
|
1209
|
+
const item = {
|
|
1210
|
+
f,
|
|
1211
|
+
key,
|
|
1212
|
+
seq: this.seq++
|
|
1213
|
+
};
|
|
1214
|
+
const { items } = this;
|
|
1215
|
+
items.push(item);
|
|
1216
|
+
let i = items.length - 1;
|
|
1217
|
+
while (i > 0) {
|
|
1218
|
+
const parent = i - 1 >> 1;
|
|
1219
|
+
if (!this.less(items[i], items[parent])) break;
|
|
1220
|
+
[items[i], items[parent]] = [items[parent], items[i]];
|
|
1221
|
+
i = parent;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
pop() {
|
|
1225
|
+
const { items } = this;
|
|
1226
|
+
const top = items[0];
|
|
1227
|
+
const last = items.pop();
|
|
1228
|
+
if (items.length > 0) {
|
|
1229
|
+
items[0] = last;
|
|
1230
|
+
let i = 0;
|
|
1231
|
+
for (;;) {
|
|
1232
|
+
const l = i * 2 + 1;
|
|
1233
|
+
const r = l + 1;
|
|
1234
|
+
let m = i;
|
|
1235
|
+
if (l < items.length && this.less(items[l], items[m])) m = l;
|
|
1236
|
+
if (r < items.length && this.less(items[r], items[m])) m = r;
|
|
1237
|
+
if (m === i) break;
|
|
1238
|
+
[items[i], items[m]] = [items[m], items[i]];
|
|
1239
|
+
i = m;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
return top.key;
|
|
1243
|
+
}
|
|
1244
|
+
less(a, b) {
|
|
1245
|
+
return a.f < b.f || a.f === b.f && a.seq < b.seq;
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
var sortedUnique = (values) => [...new Set(values)].sort((a, b) => a - b);
|
|
1249
|
+
function searchGrid(from, to, startDir, endDir, boxes) {
|
|
1250
|
+
const xs = sortedUnique([
|
|
1251
|
+
from[0],
|
|
1252
|
+
to[0],
|
|
1253
|
+
(from[0] + to[0]) / 2,
|
|
1254
|
+
...boxes.flatMap((b) => [b[0], b[2]])
|
|
1255
|
+
]);
|
|
1256
|
+
const ys = sortedUnique([
|
|
1257
|
+
from[1],
|
|
1258
|
+
to[1],
|
|
1259
|
+
(from[1] + to[1]) / 2,
|
|
1260
|
+
...boxes.flatMap((b) => [b[1], b[3]])
|
|
1261
|
+
]);
|
|
1262
|
+
const cols = xs.length;
|
|
1263
|
+
const nodeAt = (i) => [xs[i % cols], ys[Math.floor(i / cols)]];
|
|
1264
|
+
const indexOf = ([x, y]) => ys.indexOf(y) * cols + xs.indexOf(x);
|
|
1265
|
+
const blocked = (p) => p !== from && p !== to && boxes.some((b) => strictlyInside(p, b));
|
|
1266
|
+
const edgeBlocked = (a, b) => boxes.some((box) => !strictlyInside(a, box) && !strictlyInside(b, box) && crosses(a, b, box));
|
|
1267
|
+
const startNode = indexOf(from);
|
|
1268
|
+
const goalNode = indexOf(to);
|
|
1269
|
+
const g = /* @__PURE__ */ new Map();
|
|
1270
|
+
const parent = /* @__PURE__ */ new Map();
|
|
1271
|
+
const heap = new MinHeap();
|
|
1272
|
+
const h = (p) => Math.abs(p[0] - to[0]) + Math.abs(p[1] - to[1]);
|
|
1273
|
+
const startKey = startNode * 4 + startDir;
|
|
1274
|
+
g.set(startKey, 0);
|
|
1275
|
+
heap.push(startKey, h(from));
|
|
1276
|
+
let best = null;
|
|
1277
|
+
while (heap.size > 0) {
|
|
1278
|
+
const key = heap.pop();
|
|
1279
|
+
const cost = g.get(key);
|
|
1280
|
+
if (best && cost >= best.cost) break;
|
|
1281
|
+
const node = Math.floor(key / 4);
|
|
1282
|
+
const dir = key % 4;
|
|
1283
|
+
const p = node === startNode ? from : node === goalNode ? to : nodeAt(node);
|
|
1284
|
+
if (node === goalNode) {
|
|
1285
|
+
const total = cost + (dir === endDir ? 0 : BEND_PENALTY * 1.5);
|
|
1286
|
+
if (!best || total < best.cost) best = {
|
|
1287
|
+
cost: total,
|
|
1288
|
+
key
|
|
1289
|
+
};
|
|
1290
|
+
continue;
|
|
1291
|
+
}
|
|
1292
|
+
const xi = node % cols;
|
|
1293
|
+
const yi = Math.floor(node / cols);
|
|
1294
|
+
for (let d = 0; d < 4; d++) {
|
|
1295
|
+
if ((d + 2) % 4 === dir) continue;
|
|
1296
|
+
const nx = xi + DIRS[d][0];
|
|
1297
|
+
const ny = yi + DIRS[d][1];
|
|
1298
|
+
if (nx < 0 || ny < 0 || nx >= cols || ny >= ys.length) continue;
|
|
1299
|
+
const next = ny * cols + nx;
|
|
1300
|
+
const q = next === goalNode ? to : nodeAt(next);
|
|
1301
|
+
if (blocked(q) || edgeBlocked(p, q)) continue;
|
|
1302
|
+
const nextKey = next * 4 + d;
|
|
1303
|
+
const ng = cost + Math.abs(q[0] - p[0]) + Math.abs(q[1] - p[1]) + (d === dir ? 0 : BEND_PENALTY);
|
|
1304
|
+
if (ng >= (g.get(nextKey) ?? Infinity)) continue;
|
|
1305
|
+
g.set(nextKey, ng);
|
|
1306
|
+
parent.set(nextKey, key);
|
|
1307
|
+
heap.push(nextKey, ng + h(q));
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
if (!best) return null;
|
|
1311
|
+
const path = [];
|
|
1312
|
+
let key = best.key;
|
|
1313
|
+
while (key !== void 0) {
|
|
1314
|
+
const node = Math.floor(key / 4);
|
|
1315
|
+
path.push(node === startNode ? from : node === goalNode ? to : nodeAt(node));
|
|
1316
|
+
key = parent.get(key);
|
|
1317
|
+
}
|
|
1318
|
+
return path.reverse();
|
|
1319
|
+
}
|
|
1320
|
+
function simplify(points, keep = /* @__PURE__ */ new Set()) {
|
|
1321
|
+
const out = [];
|
|
1322
|
+
for (const p of points) {
|
|
1323
|
+
if (out.length > 0 && same(out.at(-1), p)) continue;
|
|
1324
|
+
out.push(p);
|
|
1325
|
+
}
|
|
1326
|
+
for (let i = out.length - 2; i > 0; i--) {
|
|
1327
|
+
const [a, b, c] = [
|
|
1328
|
+
out[i - 1],
|
|
1329
|
+
out[i],
|
|
1330
|
+
out[i + 1]
|
|
1331
|
+
];
|
|
1332
|
+
if ((a[0] === b[0] && b[0] === c[0] || a[1] === b[1] && b[1] === c[1]) && !keep.has(`${b[0]},${b[1]}`)) out.splice(i, 1);
|
|
1333
|
+
}
|
|
1334
|
+
return out;
|
|
1335
|
+
}
|
|
1336
|
+
function routeElbowArrow(start, end, opts) {
|
|
1337
|
+
const startHeading = opts.startHeading ?? (opts.startTarget ? headingForPoint(opts.startTarget, end) : towards(start, end));
|
|
1338
|
+
const endHeading = opts.endHeading ?? (opts.endTarget ? headingForPoint(opts.endTarget, start) : towards(end, start));
|
|
1339
|
+
const from = exitPoint(start, startHeading, opts.startTarget);
|
|
1340
|
+
const to = exitPoint(end, endHeading, opts.endTarget);
|
|
1341
|
+
const targets = [opts.startTarget, opts.endTarget].filter((el) => !!el).map((el) => getElementBounds(el));
|
|
1342
|
+
const boxes = [...opts.obstacles, ...targets].map((b) => expand(b, 20));
|
|
1343
|
+
const middle = searchGrid(from, to, dirIndex(startHeading), (dirIndex(endHeading) + 2) % 4, boxes);
|
|
1344
|
+
if (!middle) {
|
|
1345
|
+
const mx = (from[0] + to[0]) / 2;
|
|
1346
|
+
return simplify([
|
|
1347
|
+
start,
|
|
1348
|
+
from,
|
|
1349
|
+
[mx, from[1]],
|
|
1350
|
+
[mx, to[1]],
|
|
1351
|
+
to,
|
|
1352
|
+
end
|
|
1353
|
+
]);
|
|
1354
|
+
}
|
|
1355
|
+
return simplify([
|
|
1356
|
+
start,
|
|
1357
|
+
...middle,
|
|
1358
|
+
end
|
|
1359
|
+
]);
|
|
1360
|
+
}
|
|
1361
|
+
function sidePoint(target, heading, gap) {
|
|
1362
|
+
const [minX, minY, maxX, maxY] = getElementBounds(target);
|
|
1363
|
+
const cx = (minX + maxX) / 2;
|
|
1364
|
+
const cy = (minY + maxY) / 2;
|
|
1365
|
+
if (heading[0] > 0) return [maxX + gap, cy];
|
|
1366
|
+
if (heading[0] < 0) return [minX - gap, cy];
|
|
1367
|
+
if (heading[1] > 0) return [cx, maxY + gap];
|
|
1368
|
+
return [cx, minY - gap];
|
|
1369
|
+
}
|
|
1370
|
+
var centerOf = (el) => {
|
|
1371
|
+
const [minX, minY, maxX, maxY] = getElementBounds(el);
|
|
1372
|
+
return [(minX + maxX) / 2, (minY + maxY) / 2];
|
|
1373
|
+
};
|
|
1374
|
+
function elbowObstacles(elements, arrowId) {
|
|
1375
|
+
return elements.filter((el) => el.id !== arrowId && isBindable(el)).map((el) => getElementBounds(el));
|
|
1376
|
+
}
|
|
1377
|
+
function lookupTarget(byId, id) {
|
|
1378
|
+
const el = id ? byId.get(id) : void 0;
|
|
1379
|
+
return el && !el.isDeleted ? el : null;
|
|
1380
|
+
}
|
|
1381
|
+
function updateElbowArrow(arrow, elements) {
|
|
1382
|
+
const byId = new Map(elements.map((el) => [el.id, el]));
|
|
1383
|
+
const abs = getLinearAbsolutePoints(arrow);
|
|
1384
|
+
if (abs.length < 2) return arrow;
|
|
1385
|
+
const startTarget = lookupTarget(byId, arrow.startBinding?.elementId);
|
|
1386
|
+
const endTarget = lookupTarget(byId, arrow.endBinding?.elementId);
|
|
1387
|
+
const fixed = [...arrow.fixedSegments ?? []].sort((a, b) => a.index - b.index).map((seg) => ({
|
|
1388
|
+
start: [arrow.x + seg.start[0], arrow.y + seg.start[1]],
|
|
1389
|
+
end: [arrow.x + seg.end[0], arrow.y + seg.end[1]]
|
|
1390
|
+
})).filter((seg) => !same(seg.start, seg.end));
|
|
1391
|
+
const firstAnchor = fixed[0]?.start ?? (endTarget ? centerOf(endTarget) : abs.at(-1));
|
|
1392
|
+
const lastAnchor = fixed.at(-1)?.end ?? (startTarget ? centerOf(startTarget) : abs[0]);
|
|
1393
|
+
let start = abs[0];
|
|
1394
|
+
let startHeading;
|
|
1395
|
+
if (startTarget) {
|
|
1396
|
+
startHeading = headingForPoint(startTarget, firstAnchor);
|
|
1397
|
+
start = sidePoint(startTarget, startHeading, arrow.startBinding?.gap ?? 4);
|
|
1398
|
+
}
|
|
1399
|
+
let end = abs.at(-1);
|
|
1400
|
+
let endHeading;
|
|
1401
|
+
if (endTarget) {
|
|
1402
|
+
endHeading = headingForPoint(endTarget, lastAnchor);
|
|
1403
|
+
end = sidePoint(endTarget, endHeading, arrow.endBinding?.gap ?? 4);
|
|
1404
|
+
}
|
|
1405
|
+
const obstacles = elbowObstacles(elements, arrow.id);
|
|
1406
|
+
const route = [];
|
|
1407
|
+
let from = start;
|
|
1408
|
+
let fromHeading = startHeading;
|
|
1409
|
+
let fromTarget = startTarget;
|
|
1410
|
+
for (const seg of fixed) {
|
|
1411
|
+
const d = towards(seg.start, seg.end);
|
|
1412
|
+
route.push(...routeElbowArrow(from, seg.start, {
|
|
1413
|
+
obstacles,
|
|
1414
|
+
startTarget: fromTarget,
|
|
1415
|
+
startHeading: fromHeading,
|
|
1416
|
+
endHeading: [-d[0], -d[1]]
|
|
1417
|
+
}));
|
|
1418
|
+
from = seg.end;
|
|
1419
|
+
fromHeading = d;
|
|
1420
|
+
fromTarget = null;
|
|
1421
|
+
}
|
|
1422
|
+
route.push(...routeElbowArrow(from, end, {
|
|
1423
|
+
obstacles,
|
|
1424
|
+
startTarget: fromTarget,
|
|
1425
|
+
startHeading: fromHeading,
|
|
1426
|
+
endTarget,
|
|
1427
|
+
endHeading
|
|
1428
|
+
}));
|
|
1429
|
+
const points = simplify(route, new Set(fixed.flatMap((seg) => [seg.start, seg.end]).map((p) => `${p[0]},${p[1]}`)));
|
|
1430
|
+
const [ox, oy] = points[0];
|
|
1431
|
+
const rel = points.map(([x, y]) => [x - ox, y - oy]);
|
|
1432
|
+
if (ox === arrow.x && oy === arrow.y && rel.length === arrow.points.length && rel.every((p, i) => same(p, arrow.points[i])) && angleOf$1(arrow) === 0) return arrow;
|
|
1433
|
+
const fixedSegments = [];
|
|
1434
|
+
for (const seg of fixed) {
|
|
1435
|
+
const k = points.findIndex((p, i) => i > 0 && same(points[i - 1], seg.start) && same(p, seg.end));
|
|
1436
|
+
if (k > 0) fixedSegments.push({
|
|
1437
|
+
index: k,
|
|
1438
|
+
start: [seg.start[0] - ox, seg.start[1] - oy],
|
|
1439
|
+
end: [seg.end[0] - ox, seg.end[1] - oy]
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
const [minX, minY, maxX, maxY] = boundsOfPoints(rel);
|
|
1443
|
+
return bumpVersion(arrow, {
|
|
1444
|
+
x: ox,
|
|
1445
|
+
y: oy,
|
|
1446
|
+
angle: 0,
|
|
1447
|
+
points: rel,
|
|
1448
|
+
width: maxX - minX,
|
|
1449
|
+
height: maxY - minY,
|
|
1450
|
+
fixedSegments: arrow.fixedSegments ? fixedSegments : arrow.fixedSegments
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
//#endregion
|
|
1454
|
+
//#region src/model/linear-points.ts
|
|
1455
|
+
function fromAbsolutePoints(el, abs) {
|
|
1456
|
+
const angle = angleOf$1(el);
|
|
1457
|
+
const c0 = getElementCenter(el);
|
|
1458
|
+
const locals = abs.map((p) => rotatePoint(p, c0, -angle));
|
|
1459
|
+
const [minX, minY, maxX, maxY] = boundsOfPoints(locals);
|
|
1460
|
+
const c1 = [(minX + maxX) / 2, (minY + maxY) / 2];
|
|
1461
|
+
const d = [c1[0] - c0[0], c1[1] - c0[1]];
|
|
1462
|
+
const rd = rotatePoint(d, [0, 0], angle);
|
|
1463
|
+
const t = [rd[0] - d[0], rd[1] - d[1]];
|
|
1464
|
+
const q = locals.map(([x, y]) => [x + t[0], y + t[1]]);
|
|
1465
|
+
const [ox, oy] = q[0];
|
|
1466
|
+
const points = q.map(([x, y]) => [x - ox, y - oy]);
|
|
1467
|
+
return {
|
|
1468
|
+
...el,
|
|
1469
|
+
x: ox,
|
|
1470
|
+
y: oy,
|
|
1471
|
+
points,
|
|
1472
|
+
width: maxX - minX,
|
|
1473
|
+
height: maxY - minY
|
|
1474
|
+
};
|
|
1475
|
+
}
|
|
1476
|
+
function movePoint(el, index, p) {
|
|
1477
|
+
const abs = getLinearAbsolutePoints(el);
|
|
1478
|
+
abs[index] = p;
|
|
1479
|
+
return bumpVersion(el, fromAbsolutePoints(el, abs));
|
|
1480
|
+
}
|
|
1481
|
+
var bindingKey = (end) => end === "start" ? "startBinding" : "endBinding";
|
|
1482
|
+
function fromLocal(el, [lx, ly]) {
|
|
1483
|
+
return rotatePoint([el.x + lx, el.y + ly], getElementCenter(el), angleOf$1(el));
|
|
1484
|
+
}
|
|
1485
|
+
function outlineSegments(el) {
|
|
1486
|
+
const { width: w, height: h } = el;
|
|
1487
|
+
const corners = el.type === "diamond" ? [
|
|
1488
|
+
[w / 2, 0],
|
|
1489
|
+
[w, h / 2],
|
|
1490
|
+
[w / 2, h],
|
|
1491
|
+
[0, h / 2]
|
|
1492
|
+
] : [
|
|
1493
|
+
[0, 0],
|
|
1494
|
+
[w, 0],
|
|
1495
|
+
[w, h],
|
|
1496
|
+
[0, h]
|
|
1497
|
+
];
|
|
1498
|
+
return corners.map((p, i) => [p, corners[(i + 1) % corners.length]]);
|
|
1499
|
+
}
|
|
1500
|
+
function distanceToOutline(el, p) {
|
|
1501
|
+
const [lx, ly] = toLocalPoint(el, p);
|
|
1502
|
+
const { width: w, height: h } = el;
|
|
1503
|
+
if (el.type === "ellipse") {
|
|
1504
|
+
const rx = w / 2;
|
|
1505
|
+
const ry = h / 2;
|
|
1506
|
+
if (rx <= 0 || ry <= 0) return Infinity;
|
|
1507
|
+
const norm = Math.hypot((lx - rx) / rx, (ly - ry) / ry);
|
|
1508
|
+
return norm <= 1 ? 0 : (norm - 1) * Math.min(rx, ry);
|
|
1509
|
+
}
|
|
1510
|
+
if (el.type === "diamond") {
|
|
1511
|
+
if (w > 0 && h > 0 && Math.abs(lx - w / 2) / (w / 2) + Math.abs(ly - h / 2) / (h / 2) <= 1) return 0;
|
|
1512
|
+
return Math.min(...outlineSegments(el).map(([a, b]) => distanceToSegment([lx, ly], a, b)));
|
|
1513
|
+
}
|
|
1514
|
+
return Math.hypot(Math.max(-lx, 0, lx - w), Math.max(-ly, 0, ly - h));
|
|
1515
|
+
}
|
|
1516
|
+
function getHoveredBindable(elements, p, zoom, excludeIds = /* @__PURE__ */ new Set()) {
|
|
1517
|
+
const threshold = 15 / zoom;
|
|
1518
|
+
for (let i = elements.length - 1; i >= 0; i--) {
|
|
1519
|
+
const el = elements[i];
|
|
1520
|
+
if (excludeIds.has(el.id) || !isBindable(el)) continue;
|
|
1521
|
+
if (distanceToOutline(el, p) <= threshold) return el;
|
|
1522
|
+
}
|
|
1523
|
+
return null;
|
|
1524
|
+
}
|
|
1525
|
+
function raySegment(o, d, a, b) {
|
|
1526
|
+
const e = [b[0] - a[0], b[1] - a[1]];
|
|
1527
|
+
const denom = d[0] * e[1] - d[1] * e[0];
|
|
1528
|
+
if (Math.abs(denom) < 1e-12) return null;
|
|
1529
|
+
const ax = a[0] - o[0];
|
|
1530
|
+
const ay = a[1] - o[1];
|
|
1531
|
+
const t = (ax * e[1] - ay * e[0]) / denom;
|
|
1532
|
+
const s = (ax * d[1] - ay * d[0]) / denom;
|
|
1533
|
+
return s >= 0 && s <= 1 ? t : null;
|
|
1534
|
+
}
|
|
1535
|
+
function rayHits(el, o, d) {
|
|
1536
|
+
if (el.type !== "ellipse") return outlineSegments(el).map(([a, b]) => raySegment(o, d, a, b)).filter((t) => t !== null);
|
|
1537
|
+
const rx = el.width / 2;
|
|
1538
|
+
const ry = el.height / 2;
|
|
1539
|
+
if (rx <= 0 || ry <= 0) return [];
|
|
1540
|
+
const ox = (o[0] - rx) / rx;
|
|
1541
|
+
const oy = (o[1] - ry) / ry;
|
|
1542
|
+
const dx = d[0] / rx;
|
|
1543
|
+
const dy = d[1] / ry;
|
|
1544
|
+
const a = dx * dx + dy * dy;
|
|
1545
|
+
const b = 2 * (ox * dx + oy * dy);
|
|
1546
|
+
const c = ox * ox + oy * oy - 1;
|
|
1547
|
+
const disc = b * b - 4 * a * c;
|
|
1548
|
+
if (disc < 0 || a === 0) return [];
|
|
1549
|
+
const root = Math.sqrt(disc);
|
|
1550
|
+
return [(-b - root) / (2 * a), (-b + root) / (2 * a)];
|
|
1551
|
+
}
|
|
1552
|
+
function extentAlong(el, [nx, ny]) {
|
|
1553
|
+
const rx = el.width / 2;
|
|
1554
|
+
const ry = el.height / 2;
|
|
1555
|
+
if (el.type === "ellipse") return Math.hypot(rx * nx, ry * ny);
|
|
1556
|
+
if (el.type === "diamond") return Math.max(Math.abs(rx * nx), Math.abs(ry * ny));
|
|
1557
|
+
return Math.abs(rx * nx) + Math.abs(ry * ny);
|
|
1558
|
+
}
|
|
1559
|
+
function frameFor(target, from) {
|
|
1560
|
+
const c = [target.width / 2, target.height / 2];
|
|
1561
|
+
const o = toLocalPoint(target, from);
|
|
1562
|
+
const len = Math.hypot(c[0] - o[0], c[1] - o[1]);
|
|
1563
|
+
if (len < 1e-9) return null;
|
|
1564
|
+
const u = [(c[0] - o[0]) / len, (c[1] - o[1]) / len];
|
|
1565
|
+
const n = [-u[1], u[0]];
|
|
1566
|
+
return {
|
|
1567
|
+
c,
|
|
1568
|
+
o,
|
|
1569
|
+
u,
|
|
1570
|
+
n,
|
|
1571
|
+
len,
|
|
1572
|
+
half: extentAlong(target, n)
|
|
1573
|
+
};
|
|
1574
|
+
}
|
|
1575
|
+
var clamp$1 = (v, min, max) => Math.min(max, Math.max(min, v));
|
|
1576
|
+
function computeFocus(target, from, to) {
|
|
1577
|
+
const f = frameFor(target, from);
|
|
1578
|
+
if (!f || f.half === 0) return 0;
|
|
1579
|
+
const p = toLocalPoint(target, to);
|
|
1580
|
+
const v = [p[0] - f.o[0], p[1] - f.o[1]];
|
|
1581
|
+
const along = v[0] * f.u[0] + v[1] * f.u[1];
|
|
1582
|
+
if (along < 1e-9) return 0;
|
|
1583
|
+
return clamp$1(f.len / along * (v[0] * f.n[0] + v[1] * f.n[1]) / f.half, -1, 1);
|
|
1584
|
+
}
|
|
1585
|
+
function getBindingPoint(target, focus, gap, otherPoint) {
|
|
1586
|
+
const f = frameFor(target, otherPoint);
|
|
1587
|
+
if (!f) return getElementCenter(target);
|
|
1588
|
+
const offset = clamp$1(focus, -1, 1) * f.half;
|
|
1589
|
+
const aim = [f.c[0] + f.n[0] * offset, f.c[1] + f.n[1] * offset];
|
|
1590
|
+
const dist = Math.hypot(aim[0] - f.o[0], aim[1] - f.o[1]);
|
|
1591
|
+
if (dist < 1e-9) return fromLocal(target, aim);
|
|
1592
|
+
const d = [(aim[0] - f.o[0]) / dist, (aim[1] - f.o[1]) / dist];
|
|
1593
|
+
const hits = rayHits(target, f.o, d).filter((t) => t > 1e-9);
|
|
1594
|
+
if (hits.length === 0) return fromLocal(target, aim);
|
|
1595
|
+
const t = Math.max(0, Math.min(...hits) - gap);
|
|
1596
|
+
return fromLocal(target, [f.o[0] + d[0] * t, f.o[1] + d[1] * t]);
|
|
1597
|
+
}
|
|
1598
|
+
function withBoundElement(target, id, type, present) {
|
|
1599
|
+
const list = target.boundElements ?? [];
|
|
1600
|
+
if (list.some((b) => b.id === id) === present) return target;
|
|
1601
|
+
return bumpVersion(target, { boundElements: present ? [...list, {
|
|
1602
|
+
id,
|
|
1603
|
+
type
|
|
1604
|
+
}] : list.filter((b) => b.id !== id) });
|
|
1605
|
+
}
|
|
1606
|
+
function endIndices(linear, end) {
|
|
1607
|
+
const last = linear.points.length - 1;
|
|
1608
|
+
return end === "start" ? [0, Math.min(1, last)] : [last, Math.max(0, last - 1)];
|
|
1609
|
+
}
|
|
1610
|
+
function unbindLinearEnd(elements, linearId, end) {
|
|
1611
|
+
const linear = elements.find((el) => el.id === linearId);
|
|
1612
|
+
const binding = linear?.[bindingKey(end)];
|
|
1613
|
+
if (!linear || !binding) return [...elements];
|
|
1614
|
+
const keepRegistration = linear[bindingKey(end === "start" ? "end" : "start")]?.elementId === binding.elementId;
|
|
1615
|
+
const next = bumpVersion(linear, { [bindingKey(end)]: null });
|
|
1616
|
+
return elements.map((el) => {
|
|
1617
|
+
if (el.id === linearId) return next;
|
|
1618
|
+
if (el.id === binding.elementId && !keepRegistration) return withBoundElement(el, linearId, linear.type, false);
|
|
1619
|
+
return el;
|
|
1620
|
+
});
|
|
1621
|
+
}
|
|
1622
|
+
function bindLinearEnd(elements, linearId, end, targetId) {
|
|
1623
|
+
let current = unbindLinearEnd(elements, linearId, end);
|
|
1624
|
+
const linear = current.find((el) => el.id === linearId);
|
|
1625
|
+
const target = current.find((el) => el.id === targetId);
|
|
1626
|
+
if (!linear || !target || linear.points.length < 2) return current;
|
|
1627
|
+
const abs = getLinearAbsolutePoints(linear);
|
|
1628
|
+
const [index, adjacentIndex] = endIndices(linear, end);
|
|
1629
|
+
const focus = computeFocus(target, abs[adjacentIndex], abs[index]);
|
|
1630
|
+
const binding = {
|
|
1631
|
+
elementId: targetId,
|
|
1632
|
+
focus,
|
|
1633
|
+
gap: 4
|
|
1634
|
+
};
|
|
1635
|
+
const next = {
|
|
1636
|
+
...movePoint(linear, index, getBindingPoint(target, focus, 4, abs[adjacentIndex])),
|
|
1637
|
+
[bindingKey(end)]: binding
|
|
1638
|
+
};
|
|
1639
|
+
current = current.map((el) => {
|
|
1640
|
+
if (el.id === linearId) return next;
|
|
1641
|
+
if (el.id === targetId) return withBoundElement(el, linearId, linear.type, true);
|
|
1642
|
+
return el;
|
|
1643
|
+
});
|
|
1644
|
+
return current;
|
|
1645
|
+
}
|
|
1646
|
+
var finiteOr = (value, fallback) => typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
1647
|
+
function attachmentPoint(target, binding, other) {
|
|
1648
|
+
const fixed = binding.fixedPoint;
|
|
1649
|
+
if (Array.isArray(fixed) && fixed.length === 2 && fixed.every(Number.isFinite)) return fromLocal(target, [fixed[0] * target.width, fixed[1] * target.height]);
|
|
1650
|
+
return getBindingPoint(target, finiteOr(binding.focus, 0), finiteOr(binding.gap, 4), other);
|
|
1651
|
+
}
|
|
1652
|
+
function recomputeEnds(linear, byId, ends) {
|
|
1653
|
+
const abs = getLinearAbsolutePoints(linear);
|
|
1654
|
+
let changed = false;
|
|
1655
|
+
for (const end of ends) {
|
|
1656
|
+
const binding = linear[bindingKey(end)];
|
|
1657
|
+
const target = binding && byId.get(binding.elementId);
|
|
1658
|
+
if (!binding || !target || target.isDeleted) continue;
|
|
1659
|
+
const [index, adjacentIndex] = endIndices(linear, end);
|
|
1660
|
+
const next = attachmentPoint(target, binding, abs[adjacentIndex]);
|
|
1661
|
+
if (!next.every(Number.isFinite)) continue;
|
|
1662
|
+
abs[index] = next;
|
|
1663
|
+
changed = true;
|
|
1664
|
+
}
|
|
1665
|
+
return changed ? bumpVersion(linear, fromAbsolutePoints(linear, abs)) : linear;
|
|
1666
|
+
}
|
|
1667
|
+
function updateBoundArrows(elements, changedIds) {
|
|
1668
|
+
const byId = new Map(elements.map((el) => [el.id, el]));
|
|
1669
|
+
return elements.map((el) => {
|
|
1670
|
+
if (changedIds.has(el.id) || el.type !== "arrow" && el.type !== "line") return el;
|
|
1671
|
+
const linear = el;
|
|
1672
|
+
const ends = ["start", "end"].filter((end) => {
|
|
1673
|
+
const binding = linear[bindingKey(end)];
|
|
1674
|
+
return !!binding && changedIds.has(binding.elementId);
|
|
1675
|
+
});
|
|
1676
|
+
if (ends.length === 0) return el;
|
|
1677
|
+
return linear.elbowed ? updateElbowArrow(linear, elements) : recomputeEnds(linear, byId, ends);
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
function unbindDetached(elements, movedIds) {
|
|
1681
|
+
let current = [...elements];
|
|
1682
|
+
for (const el of elements) {
|
|
1683
|
+
if (!movedIds.has(el.id) || el.type !== "arrow" && el.type !== "line") continue;
|
|
1684
|
+
for (const end of ["start", "end"]) {
|
|
1685
|
+
const binding = el[bindingKey(end)];
|
|
1686
|
+
if (binding && !movedIds.has(binding.elementId)) current = unbindLinearEnd(current, el.id, end);
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
return current;
|
|
1690
|
+
}
|
|
1691
|
+
//#endregion
|
|
1692
|
+
//#region src/editor/binding-actions.ts
|
|
1693
|
+
function refreshBindings(elements, changedIds) {
|
|
1694
|
+
const detached = unbindDetached(elements, changedIds);
|
|
1695
|
+
const updated = updateBoundArrows(detached, changedIds);
|
|
1696
|
+
const arrows = new Set(updated.filter((el, i) => el !== detached[i]).map((el) => el.id));
|
|
1697
|
+
return arrows.size > 0 ? relayoutBoundTexts(updated, arrows) : updated;
|
|
1698
|
+
}
|
|
1699
|
+
function excludedFor(elements, linearId) {
|
|
1700
|
+
const ids = /* @__PURE__ */ new Set([linearId]);
|
|
1701
|
+
for (const el of elements) if (el.type === "text" && el.containerId === linearId) ids.add(el.id);
|
|
1702
|
+
return ids;
|
|
1703
|
+
}
|
|
1704
|
+
function findBindingTarget(elements, linearId, p, zoom) {
|
|
1705
|
+
return getHoveredBindable(elements, p, zoom, excludedFor(elements, linearId));
|
|
1706
|
+
}
|
|
1707
|
+
function bindArrowEnds(elements, linearId, ends, zoom) {
|
|
1708
|
+
let current = [...elements];
|
|
1709
|
+
for (const end of ends) {
|
|
1710
|
+
const linear = current.find((el) => el.id === linearId);
|
|
1711
|
+
if (!linear || linear.type !== "arrow") return current;
|
|
1712
|
+
const abs = getLinearAbsolutePoints(linear);
|
|
1713
|
+
const p = end === "start" ? abs[0] : abs.at(-1);
|
|
1714
|
+
const target = findBindingTarget(current, linearId, p, zoom);
|
|
1715
|
+
current = target ? bindLinearEnd(current, linearId, end, target.id) : unbindLinearEnd(current, linearId, end);
|
|
1716
|
+
}
|
|
1717
|
+
return relayoutBoundTexts(rerouteElbow(current, linearId), /* @__PURE__ */ new Set([linearId]));
|
|
1718
|
+
}
|
|
1719
|
+
function rerouteElbow(elements, linearId) {
|
|
1720
|
+
return elements.map((el) => el.id === linearId && el.elbowed ? updateElbowArrow(el, elements) : el);
|
|
1721
|
+
}
|
|
1722
|
+
//#endregion
|
|
1723
|
+
//#region src/editor/richtext-actions.ts
|
|
1724
|
+
function startRichTextEditing(store, id) {
|
|
1725
|
+
const el = store.getState().elements.find((item) => item.id === id);
|
|
1726
|
+
if (!el || el.type !== "richtext" || el.isDeleted || el.locked) return;
|
|
1727
|
+
store.setState({
|
|
1728
|
+
editingRichTextId: id,
|
|
1729
|
+
selectedIds: /* @__PURE__ */ new Set([id]),
|
|
1730
|
+
editingGroupId: null
|
|
1731
|
+
});
|
|
1732
|
+
}
|
|
1733
|
+
function stopRichTextEditing(store) {
|
|
1734
|
+
if (store.getState().editingRichTextId) store.setState({ editingRichTextId: null });
|
|
1735
|
+
}
|
|
1736
|
+
function commitRichTextContent(store, id, content) {
|
|
1737
|
+
store.commit(({ elements }) => {
|
|
1738
|
+
const next = elements.map((el) => el.id === id && el.type === "richtext" ? bumpVersion(el, { content }) : el);
|
|
1739
|
+
return { elements: refreshBindings(resolveRichTextHeights(next), /* @__PURE__ */ new Set([id])) };
|
|
1740
|
+
});
|
|
1741
|
+
}
|
|
1742
|
+
//#endregion
|
|
1743
|
+
//#region src/editor/tools/creation.ts
|
|
1744
|
+
function replaceElement(store, next) {
|
|
1745
|
+
store.updateTransient(({ elements }) => ({ elements: elements.map((el) => el.id === next.id ? next : el) }));
|
|
1746
|
+
}
|
|
1747
|
+
function appendElement(store, el) {
|
|
1748
|
+
store.updateTransient(({ elements }) => ({ elements: [...elements, el] }));
|
|
1749
|
+
}
|
|
1750
|
+
function finishCreation(store, id, select = true) {
|
|
1751
|
+
store.updateTransient(({ elements }) => ({ elements: refreshFrames(elements, /* @__PURE__ */ new Set([id])) }));
|
|
1752
|
+
store.endTransient(true);
|
|
1753
|
+
const state = store.getState();
|
|
1754
|
+
store.setState({
|
|
1755
|
+
selectedIds: select ? /* @__PURE__ */ new Set([id]) : state.selectedIds,
|
|
1756
|
+
tool: state.toolLocked ? state.tool : "selection"
|
|
1757
|
+
});
|
|
1758
|
+
}
|
|
1759
|
+
function cancelCreation(store) {
|
|
1760
|
+
store.endTransient(false);
|
|
1761
|
+
}
|
|
1762
|
+
function findElement(store, id) {
|
|
1763
|
+
return store.getState().elements.find((el) => el.id === id);
|
|
1764
|
+
}
|
|
1765
|
+
function createCardTool() {
|
|
1766
|
+
let id = null;
|
|
1767
|
+
let start = [0, 0];
|
|
1768
|
+
let origin = [0, 0];
|
|
1769
|
+
return {
|
|
1770
|
+
onPointerDown(ctx, e) {
|
|
1771
|
+
const { store } = ctx;
|
|
1772
|
+
origin = e.scene;
|
|
1773
|
+
start = snapPoint(e.scene, activeGrid(store, e));
|
|
1774
|
+
const el = newRichTextElement({
|
|
1775
|
+
x: start[0],
|
|
1776
|
+
y: start[1],
|
|
1777
|
+
width: 0,
|
|
1778
|
+
height: 0
|
|
1779
|
+
});
|
|
1780
|
+
id = el.id;
|
|
1781
|
+
store.beginTransient();
|
|
1782
|
+
appendElement(store, el);
|
|
1783
|
+
store.setState({ selectedIds: /* @__PURE__ */ new Set() });
|
|
1784
|
+
},
|
|
1785
|
+
onPointerMove(ctx, e) {
|
|
1786
|
+
if (!id) return;
|
|
1787
|
+
const current = findElement(ctx.store, id);
|
|
1788
|
+
if (!current) return;
|
|
1789
|
+
const at = snapPoint(e.scene, activeGrid(ctx.store, e));
|
|
1790
|
+
replaceElement(ctx.store, {
|
|
1791
|
+
...current,
|
|
1792
|
+
x: Math.min(start[0], at[0]),
|
|
1793
|
+
y: Math.min(start[1], at[1]),
|
|
1794
|
+
width: Math.abs(at[0] - start[0]),
|
|
1795
|
+
height: Math.abs(at[1] - start[1])
|
|
1796
|
+
});
|
|
1797
|
+
},
|
|
1798
|
+
onPointerUp(ctx, e) {
|
|
1799
|
+
if (!id) return;
|
|
1800
|
+
const { store } = ctx;
|
|
1801
|
+
const createdId = id;
|
|
1802
|
+
id = null;
|
|
1803
|
+
const current = findElement(store, createdId);
|
|
1804
|
+
if (!current) {
|
|
1805
|
+
cancelCreation(store);
|
|
1806
|
+
return;
|
|
1807
|
+
}
|
|
1808
|
+
const zoom = store.getState().viewport.zoom;
|
|
1809
|
+
const sized = Math.hypot(e.scene[0] - origin[0], e.scene[1] - origin[1]) * zoom < 2 ? {
|
|
1810
|
+
...current,
|
|
1811
|
+
x: origin[0] - 100,
|
|
1812
|
+
y: origin[1] - 60,
|
|
1813
|
+
width: 200,
|
|
1814
|
+
height: 120
|
|
1815
|
+
} : current;
|
|
1816
|
+
replaceElement(store, resolveRichTextHeights([sized])[0]);
|
|
1817
|
+
finishCreation(store, createdId);
|
|
1818
|
+
startRichTextEditing(store, createdId);
|
|
1819
|
+
},
|
|
1820
|
+
cancel(ctx) {
|
|
1821
|
+
if (!id) return;
|
|
1822
|
+
id = null;
|
|
1823
|
+
cancelCreation(ctx.store);
|
|
1824
|
+
}
|
|
1825
|
+
};
|
|
1826
|
+
}
|
|
1827
|
+
//#endregion
|
|
1828
|
+
//#region src/editor/tools/eraser.ts
|
|
1829
|
+
var SAMPLE_STEP = 4;
|
|
1830
|
+
function collect(ctx, from, to) {
|
|
1831
|
+
const state = ctx.store.getState();
|
|
1832
|
+
const tol = 6 / state.viewport.zoom;
|
|
1833
|
+
const erasing = new Set(state.erasingIds);
|
|
1834
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(to[0] - from[0], to[1] - from[1]) / SAMPLE_STEP));
|
|
1835
|
+
const alive = state.elements.filter((el) => !erasing.has(el.id));
|
|
1836
|
+
for (let i = 0; i <= steps; i++) {
|
|
1837
|
+
const hit = getHitElement(alive, [from[0] + (to[0] - from[0]) * i / steps, from[1] + (to[1] - from[1]) * i / steps], tol);
|
|
1838
|
+
if (hit) erasing.add(hit.id);
|
|
1839
|
+
}
|
|
1840
|
+
if (erasing.size !== state.erasingIds.size) ctx.store.setState({ erasingIds: erasing });
|
|
1841
|
+
}
|
|
1842
|
+
function createEraserTool() {
|
|
1843
|
+
let last = null;
|
|
1844
|
+
return {
|
|
1845
|
+
onPointerDown(ctx, e) {
|
|
1846
|
+
last = e.scene;
|
|
1847
|
+
ctx.store.setState({
|
|
1848
|
+
erasingIds: /* @__PURE__ */ new Set(),
|
|
1849
|
+
selectedIds: /* @__PURE__ */ new Set()
|
|
1850
|
+
});
|
|
1851
|
+
collect(ctx, e.scene, e.scene);
|
|
1852
|
+
},
|
|
1853
|
+
onPointerMove(ctx, e) {
|
|
1854
|
+
if (!last) return;
|
|
1855
|
+
collect(ctx, last, e.scene);
|
|
1856
|
+
last = e.scene;
|
|
1857
|
+
},
|
|
1858
|
+
onPointerUp(ctx) {
|
|
1859
|
+
if (!last) return;
|
|
1860
|
+
last = null;
|
|
1861
|
+
const { store } = ctx;
|
|
1862
|
+
const ids = withBoundText(store.getState().elements, store.getState().erasingIds);
|
|
1863
|
+
store.setState({ erasingIds: /* @__PURE__ */ new Set() });
|
|
1864
|
+
if (ids.size === 0) return;
|
|
1865
|
+
store.commit(({ elements }) => ({ elements: deleteElements(elements, ids) }));
|
|
1866
|
+
},
|
|
1867
|
+
cancel(ctx) {
|
|
1868
|
+
last = null;
|
|
1869
|
+
ctx.store.setState({ erasingIds: /* @__PURE__ */ new Set() });
|
|
1870
|
+
}
|
|
1871
|
+
};
|
|
1872
|
+
}
|
|
1873
|
+
//#endregion
|
|
1874
|
+
//#region src/editor/tools/freedraw.ts
|
|
1875
|
+
function createFreedrawTool() {
|
|
1876
|
+
let id = null;
|
|
1877
|
+
return {
|
|
1878
|
+
onPointerDown(ctx, e) {
|
|
1879
|
+
const { store } = ctx;
|
|
1880
|
+
const isPen = e.pointerType === "pen";
|
|
1881
|
+
const el = newFreedrawElement({
|
|
1882
|
+
x: e.scene[0],
|
|
1883
|
+
y: e.scene[1],
|
|
1884
|
+
points: [[0, 0]],
|
|
1885
|
+
pressures: isPen ? [e.pressure] : [],
|
|
1886
|
+
simulatePressure: !isPen
|
|
1887
|
+
}, store.getState().currentStyle);
|
|
1888
|
+
id = el.id;
|
|
1889
|
+
store.beginTransient();
|
|
1890
|
+
appendElement(store, el);
|
|
1891
|
+
store.setState({ selectedIds: /* @__PURE__ */ new Set() });
|
|
1892
|
+
},
|
|
1893
|
+
onPointerMove(ctx, e) {
|
|
1894
|
+
if (!id) return;
|
|
1895
|
+
const el = findElement(ctx.store, id);
|
|
1896
|
+
if (!el) return;
|
|
1897
|
+
const points = [...el.points, [e.scene[0] - el.x, e.scene[1] - el.y]];
|
|
1898
|
+
const [minX, minY, maxX, maxY] = boundsOfPoints(points);
|
|
1899
|
+
replaceElement(ctx.store, {
|
|
1900
|
+
...el,
|
|
1901
|
+
points,
|
|
1902
|
+
pressures: el.simulatePressure ? el.pressures : [...el.pressures, e.pressure],
|
|
1903
|
+
width: maxX - minX,
|
|
1904
|
+
height: maxY - minY
|
|
1905
|
+
});
|
|
1906
|
+
},
|
|
1907
|
+
onPointerUp(ctx) {
|
|
1908
|
+
if (!id) return;
|
|
1909
|
+
id = null;
|
|
1910
|
+
ctx.store.endTransient(true);
|
|
1911
|
+
},
|
|
1912
|
+
cancel(ctx) {
|
|
1913
|
+
if (!id) return;
|
|
1914
|
+
id = null;
|
|
1915
|
+
ctx.store.endTransient(false);
|
|
1916
|
+
}
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
//#endregion
|
|
1920
|
+
//#region src/editor/tools/hand.ts
|
|
1921
|
+
function createHandTool() {
|
|
1922
|
+
let start = null;
|
|
1923
|
+
let origin = null;
|
|
1924
|
+
return {
|
|
1925
|
+
onPointerDown(ctx, e) {
|
|
1926
|
+
start = e.screen;
|
|
1927
|
+
origin = ctx.store.getState().viewport;
|
|
1928
|
+
},
|
|
1929
|
+
onPointerMove(ctx, e) {
|
|
1930
|
+
if (!start || !origin) return;
|
|
1931
|
+
ctx.store.setState({ viewport: {
|
|
1932
|
+
...origin,
|
|
1933
|
+
scrollX: origin.scrollX + (e.screen[0] - start[0]) / origin.zoom,
|
|
1934
|
+
scrollY: origin.scrollY + (e.screen[1] - start[1]) / origin.zoom
|
|
1935
|
+
} });
|
|
1936
|
+
},
|
|
1937
|
+
onPointerUp() {
|
|
1938
|
+
start = null;
|
|
1939
|
+
origin = null;
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
//#endregion
|
|
1944
|
+
//#region src/editor/tools/image.ts
|
|
1945
|
+
function createImageTool() {
|
|
1946
|
+
return {
|
|
1947
|
+
onPointerDown(ctx, e) {
|
|
1948
|
+
ctx.requestImage?.(e.scene);
|
|
1949
|
+
},
|
|
1950
|
+
onPointerMove() {},
|
|
1951
|
+
onPointerUp() {}
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
//#endregion
|
|
1955
|
+
//#region src/editor/tools/linear.ts
|
|
1956
|
+
var SNAP = Math.PI / 12;
|
|
1957
|
+
function withPoints(el, absolute) {
|
|
1958
|
+
const [ox, oy] = absolute[0];
|
|
1959
|
+
const points = absolute.map(([x, y]) => [x - ox, y - oy]);
|
|
1960
|
+
const [minX, minY, maxX, maxY] = boundsOfPoints(points);
|
|
1961
|
+
return {
|
|
1962
|
+
...el,
|
|
1963
|
+
x: ox,
|
|
1964
|
+
y: oy,
|
|
1965
|
+
points,
|
|
1966
|
+
width: maxX - minX,
|
|
1967
|
+
height: maxY - minY
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
function snapAngle(from, to) {
|
|
1971
|
+
const dx = to[0] - from[0];
|
|
1972
|
+
const dy = to[1] - from[1];
|
|
1973
|
+
const length = Math.hypot(dx, dy);
|
|
1974
|
+
const angle = Math.round(Math.atan2(dy, dx) / SNAP) * SNAP;
|
|
1975
|
+
return [from[0] + Math.cos(angle) * length, from[1] + Math.sin(angle) * length];
|
|
1976
|
+
}
|
|
1977
|
+
function absolutePoints(el) {
|
|
1978
|
+
return el.points.map(([x, y]) => [el.x + x, el.y + y]);
|
|
1979
|
+
}
|
|
1980
|
+
function createLinearTool(type, options = {}) {
|
|
1981
|
+
let id = null;
|
|
1982
|
+
let phase = "idle";
|
|
1983
|
+
let downAt = [0, 0];
|
|
1984
|
+
let origin = [0, 0];
|
|
1985
|
+
let suppressBinding = false;
|
|
1986
|
+
const suggest = (ctx, el, p) => {
|
|
1987
|
+
const { elements, viewport } = ctx.store.getState();
|
|
1988
|
+
const target = type === "arrow" && !suppressBinding ? findBindingTarget(elements, el.id, p, viewport.zoom) : null;
|
|
1989
|
+
ctx.store.setState({ suggestedBindingId: target?.id ?? null });
|
|
1990
|
+
};
|
|
1991
|
+
const update = (ctx, e) => {
|
|
1992
|
+
const el = id ? findElement(ctx.store, id) : void 0;
|
|
1993
|
+
if (!el) return;
|
|
1994
|
+
const at = snapPoint(e.scene, activeGrid(ctx.store, e));
|
|
1995
|
+
let next;
|
|
1996
|
+
if (el.elbowed) {
|
|
1997
|
+
const { elements, viewport } = ctx.store.getState();
|
|
1998
|
+
const targetAt = (p) => suppressBinding ? null : findBindingTarget(elements, el.id, p, viewport.zoom);
|
|
1999
|
+
next = withPoints(el, routeElbowArrow(origin, at, {
|
|
2000
|
+
obstacles: elbowObstacles(elements, el.id),
|
|
2001
|
+
startTarget: targetAt(origin),
|
|
2002
|
+
endTarget: targetAt(at)
|
|
2003
|
+
}));
|
|
2004
|
+
} else {
|
|
2005
|
+
const pts = absolutePoints(el);
|
|
2006
|
+
const anchor = pts.at(-2) ?? pts[0];
|
|
2007
|
+
pts[pts.length - 1] = e.shiftKey ? snapAngle(anchor, at) : at;
|
|
2008
|
+
next = withPoints(el, pts);
|
|
2009
|
+
}
|
|
2010
|
+
if (options.onPointMoved) next = options.onPointMoved(ctx, next, e);
|
|
2011
|
+
replaceElement(ctx.store, next);
|
|
2012
|
+
suggest(ctx, next, absolutePoints(next).at(-1));
|
|
2013
|
+
};
|
|
2014
|
+
const finish = (store, dropPreview, ctx) => {
|
|
2015
|
+
const el = id ? findElement(store, id) : void 0;
|
|
2016
|
+
const createdId = id;
|
|
2017
|
+
id = null;
|
|
2018
|
+
phase = "idle";
|
|
2019
|
+
if (!el || !createdId) return;
|
|
2020
|
+
const pts = absolutePoints(el);
|
|
2021
|
+
if (dropPreview && el.elbowed) pts.length = 0;
|
|
2022
|
+
if (dropPreview) pts.pop();
|
|
2023
|
+
if (pts.length < 2) {
|
|
2024
|
+
cancelCreation(store);
|
|
2025
|
+
return;
|
|
2026
|
+
}
|
|
2027
|
+
const finalEl = withPoints(el, pts);
|
|
2028
|
+
replaceElement(store, finalEl);
|
|
2029
|
+
if (type === "arrow" && !suppressBinding) {
|
|
2030
|
+
const zoom = store.getState().viewport.zoom;
|
|
2031
|
+
store.updateTransient(({ elements }) => ({ elements: bindArrowEnds(elements, createdId, ["start", "end"], zoom) }));
|
|
2032
|
+
}
|
|
2033
|
+
store.setState({ suggestedBindingId: null });
|
|
2034
|
+
finishCreation(store, createdId);
|
|
2035
|
+
if (ctx) options.onFinished?.(ctx, finalEl);
|
|
2036
|
+
};
|
|
2037
|
+
return {
|
|
2038
|
+
onPointerDown(ctx, e) {
|
|
2039
|
+
const { store } = ctx;
|
|
2040
|
+
downAt = e.screen;
|
|
2041
|
+
suppressBinding = e.ctrlKey;
|
|
2042
|
+
if (phase !== "multi") origin = snapPoint(e.scene, activeGrid(store, e));
|
|
2043
|
+
if (phase === "multi" && id) {
|
|
2044
|
+
const el = findElement(store, id);
|
|
2045
|
+
if (el.elbowed) {
|
|
2046
|
+
update(ctx, e);
|
|
2047
|
+
finish(store, false, ctx);
|
|
2048
|
+
return;
|
|
2049
|
+
}
|
|
2050
|
+
const pts = absolutePoints(el);
|
|
2051
|
+
replaceElement(store, withPoints(el, [...pts, pts.at(-1)]));
|
|
2052
|
+
return;
|
|
2053
|
+
}
|
|
2054
|
+
const el = newLinearElement(type, {
|
|
2055
|
+
x: origin[0],
|
|
2056
|
+
y: origin[1],
|
|
2057
|
+
points: [[0, 0], [0, 0]]
|
|
2058
|
+
}, store.getState().currentStyle);
|
|
2059
|
+
id = el.id;
|
|
2060
|
+
phase = "dragging";
|
|
2061
|
+
store.beginTransient();
|
|
2062
|
+
appendElement(store, el);
|
|
2063
|
+
store.setState({ selectedIds: /* @__PURE__ */ new Set() });
|
|
2064
|
+
},
|
|
2065
|
+
onPointerMove(ctx, e) {
|
|
2066
|
+
if (phase === "idle") return;
|
|
2067
|
+
suppressBinding = e.ctrlKey;
|
|
2068
|
+
update(ctx, e);
|
|
2069
|
+
},
|
|
2070
|
+
onPointerUp(ctx, e) {
|
|
2071
|
+
if (phase !== "dragging") return;
|
|
2072
|
+
suppressBinding = e.ctrlKey;
|
|
2073
|
+
if (Math.hypot(e.screen[0] - downAt[0], e.screen[1] - downAt[1]) < 2) {
|
|
2074
|
+
phase = "multi";
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
finish(ctx.store, false, ctx);
|
|
2078
|
+
},
|
|
2079
|
+
onDoubleClick(ctx) {
|
|
2080
|
+
if (phase === "multi") finish(ctx.store, true, ctx);
|
|
2081
|
+
},
|
|
2082
|
+
onKeyDown(ctx, e) {
|
|
2083
|
+
if (phase === "idle") return false;
|
|
2084
|
+
if (e.key === "Enter" || e.key === "Escape") {
|
|
2085
|
+
finish(ctx.store, true, ctx);
|
|
2086
|
+
return true;
|
|
2087
|
+
}
|
|
2088
|
+
return false;
|
|
2089
|
+
},
|
|
2090
|
+
cancel(ctx) {
|
|
2091
|
+
if (!id) return;
|
|
2092
|
+
if (phase === "multi") {
|
|
2093
|
+
finish(ctx.store, true, ctx);
|
|
2094
|
+
return;
|
|
2095
|
+
}
|
|
2096
|
+
id = null;
|
|
2097
|
+
phase = "idle";
|
|
2098
|
+
ctx.store.setState({ suggestedBindingId: null });
|
|
2099
|
+
cancelCreation(ctx.store);
|
|
2100
|
+
}
|
|
2101
|
+
};
|
|
2102
|
+
}
|
|
2103
|
+
function isEditableLinear(el) {
|
|
2104
|
+
return !!el && (el.type === "line" || el.type === "arrow") && !el.locked;
|
|
2105
|
+
}
|
|
2106
|
+
function insertPoint(el, segmentIndex, p) {
|
|
2107
|
+
const abs = getLinearAbsolutePoints(el);
|
|
2108
|
+
abs.splice(segmentIndex + 1, 0, p);
|
|
2109
|
+
return bumpVersion(el, fromAbsolutePoints(el, abs));
|
|
2110
|
+
}
|
|
2111
|
+
function removePoint(el, index) {
|
|
2112
|
+
if (el.points.length <= 2) return el;
|
|
2113
|
+
const abs = getLinearAbsolutePoints(el);
|
|
2114
|
+
abs.splice(index, 1);
|
|
2115
|
+
return bumpVersion(el, fromAbsolutePoints(el, abs));
|
|
2116
|
+
}
|
|
2117
|
+
function getMidpoints(el) {
|
|
2118
|
+
const abs = getLinearAbsolutePoints(el);
|
|
2119
|
+
const mids = [];
|
|
2120
|
+
for (let i = 0; i < abs.length - 1; i++) mids.push([(abs[i][0] + abs[i + 1][0]) / 2, (abs[i][1] + abs[i + 1][1]) / 2]);
|
|
2121
|
+
return mids;
|
|
2122
|
+
}
|
|
2123
|
+
function getEditedLinear(state) {
|
|
2124
|
+
const selected = getSelectedElements(state);
|
|
2125
|
+
if (selected.length !== 1) return null;
|
|
2126
|
+
return isEditableLinear(selected[0]) ? selected[0] : null;
|
|
2127
|
+
}
|
|
2128
|
+
function getElbowSegmentHandles(el) {
|
|
2129
|
+
const abs = getLinearAbsolutePoints(el);
|
|
2130
|
+
const handles = [];
|
|
2131
|
+
for (let i = 1; i < abs.length - 2; i++) handles.push({
|
|
2132
|
+
index: i,
|
|
2133
|
+
point: [(abs[i][0] + abs[i + 1][0]) / 2, (abs[i][1] + abs[i + 1][1]) / 2]
|
|
2134
|
+
});
|
|
2135
|
+
return handles;
|
|
2136
|
+
}
|
|
2137
|
+
function moveElbowSegment(el, index, p) {
|
|
2138
|
+
const abs = getLinearAbsolutePoints(el);
|
|
2139
|
+
const axis = abs[index][1] === abs[index + 1][1] ? 1 : 0;
|
|
2140
|
+
abs[index][axis] = p[axis];
|
|
2141
|
+
abs[index + 1][axis] = p[axis];
|
|
2142
|
+
const moved = fromAbsolutePoints(el, abs);
|
|
2143
|
+
const rel = (q) => [q[0] - moved.x, q[1] - moved.y];
|
|
2144
|
+
const others = (el.fixedSegments ?? []).filter((seg) => seg.index !== index + 1);
|
|
2145
|
+
return bumpVersion(el, {
|
|
2146
|
+
...moved,
|
|
2147
|
+
fixedSegments: [...others, {
|
|
2148
|
+
index: index + 1,
|
|
2149
|
+
start: rel(abs[index]),
|
|
2150
|
+
end: rel(abs[index + 1])
|
|
2151
|
+
}].sort((a, b) => a.index - b.index)
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
function hitPointHandle(el, p, zoom) {
|
|
2155
|
+
const radius = 8 / zoom;
|
|
2156
|
+
const near = ([x, y]) => Math.hypot(p[0] - x, p[1] - y) <= radius;
|
|
2157
|
+
const abs = getLinearAbsolutePoints(el);
|
|
2158
|
+
for (let i = abs.length - 1; i >= 0; i--) if ((i === 0 || i === abs.length - 1 || !el.elbowed) && near(abs[i])) return {
|
|
2159
|
+
kind: "point",
|
|
2160
|
+
index: i
|
|
2161
|
+
};
|
|
2162
|
+
if (el.elbowed) {
|
|
2163
|
+
for (const handle of getElbowSegmentHandles(el)) if (near(handle.point)) return {
|
|
2164
|
+
kind: "segment",
|
|
2165
|
+
index: handle.index
|
|
2166
|
+
};
|
|
2167
|
+
return null;
|
|
2168
|
+
}
|
|
2169
|
+
const mids = getMidpoints(el);
|
|
2170
|
+
for (let i = 0; i < mids.length; i++) if (near(mids[i])) return {
|
|
2171
|
+
kind: "midpoint",
|
|
2172
|
+
index: i
|
|
2173
|
+
};
|
|
2174
|
+
return null;
|
|
2175
|
+
}
|
|
2176
|
+
//#endregion
|
|
2177
|
+
//#region src/editor/tools/point-gesture.ts
|
|
2178
|
+
function startPointGesture(ctx, e, onMove) {
|
|
2179
|
+
const { store } = ctx;
|
|
2180
|
+
const state = store.getState();
|
|
2181
|
+
const el = getEditedLinear(state);
|
|
2182
|
+
if (!el) return null;
|
|
2183
|
+
const hit = hitPointHandle(el, e.scene, state.viewport.zoom);
|
|
2184
|
+
if (!hit) return null;
|
|
2185
|
+
const original = el;
|
|
2186
|
+
const baseIndex = hit.kind === "point" ? hit.index : hit.kind === "midpoint" ? hit.index + 1 : -1;
|
|
2187
|
+
let index = baseIndex;
|
|
2188
|
+
let moved = false;
|
|
2189
|
+
let suppressBinding = e.ctrlKey;
|
|
2190
|
+
const downAt = e.screen;
|
|
2191
|
+
const endOf = (i, count) => el.type !== "arrow" ? null : i === 0 ? "start" : i === count - 1 ? "end" : null;
|
|
2192
|
+
store.setState({ selectedPointIndex: hit.kind === "point" ? hit.index : null });
|
|
2193
|
+
store.beginTransient();
|
|
2194
|
+
return {
|
|
2195
|
+
cancel() {
|
|
2196
|
+
store.setState({ suggestedBindingId: null });
|
|
2197
|
+
store.endTransient(false);
|
|
2198
|
+
index = -1;
|
|
2199
|
+
},
|
|
2200
|
+
move(ev) {
|
|
2201
|
+
if (!moved && Math.hypot(ev.screen[0] - downAt[0], ev.screen[1] - downAt[1]) < 2) return;
|
|
2202
|
+
moved = true;
|
|
2203
|
+
let next;
|
|
2204
|
+
if (hit.kind === "segment") next = moveElbowSegment(original, hit.index, ev.scene);
|
|
2205
|
+
else if (hit.kind === "midpoint") next = movePoint(insertPoint(original, hit.index, ev.scene), baseIndex, ev.scene);
|
|
2206
|
+
else next = movePoint(original, baseIndex, ev.scene);
|
|
2207
|
+
const dragged = endOf(baseIndex, next.points.length);
|
|
2208
|
+
if (original.elbowed && dragged) {
|
|
2209
|
+
const key = dragged === "start" ? "startBinding" : "endBinding";
|
|
2210
|
+
next = {
|
|
2211
|
+
...updateElbowArrow({
|
|
2212
|
+
...next,
|
|
2213
|
+
[key]: null
|
|
2214
|
+
}, store.getState().elements),
|
|
2215
|
+
[key]: original[key]
|
|
2216
|
+
};
|
|
2217
|
+
index = dragged === "start" ? 0 : next.points.length - 1;
|
|
2218
|
+
}
|
|
2219
|
+
if (onMove) next = onMove(ctx, next, index, ev);
|
|
2220
|
+
store.updateTransient(({ elements }) => ({ elements: relayoutBoundTexts(elements.map((item) => item.id === next.id ? next : item), /* @__PURE__ */ new Set([next.id])) }));
|
|
2221
|
+
suppressBinding = ev.ctrlKey;
|
|
2222
|
+
const end = endOf(index, next.points.length);
|
|
2223
|
+
const { elements, viewport } = store.getState();
|
|
2224
|
+
const target = end && !suppressBinding ? findBindingTarget(elements, next.id, ev.scene, viewport.zoom) : null;
|
|
2225
|
+
store.setState({
|
|
2226
|
+
selectedPointIndex: index >= 0 ? index : null,
|
|
2227
|
+
suggestedBindingId: target?.id ?? null
|
|
2228
|
+
});
|
|
2229
|
+
},
|
|
2230
|
+
end() {
|
|
2231
|
+
const current = store.getState().elements.find((item) => item.id === el.id);
|
|
2232
|
+
const end = current && moved ? endOf(index, current.points.length) : null;
|
|
2233
|
+
if (end) {
|
|
2234
|
+
const zoom = store.getState().viewport.zoom;
|
|
2235
|
+
store.updateTransient(({ elements }) => ({ elements: suppressBinding ? unbindLinearEnd(elements, el.id, end) : bindArrowEnds(elements, el.id, [end], zoom) }));
|
|
2236
|
+
}
|
|
2237
|
+
store.setState({ suggestedBindingId: null });
|
|
2238
|
+
store.endTransient(moved);
|
|
2239
|
+
index = -1;
|
|
2240
|
+
}
|
|
2241
|
+
};
|
|
2242
|
+
}
|
|
2243
|
+
var MIN_SIZE = 1;
|
|
2244
|
+
var SNAP_ANGLE = Math.PI / 12;
|
|
2245
|
+
function unrotatedBox(el) {
|
|
2246
|
+
if (isLinear(el)) {
|
|
2247
|
+
const [minX, minY, maxX, maxY] = boundsOfPoints(el.points.length ? el.points : [[0, 0]]);
|
|
2248
|
+
return [
|
|
2249
|
+
el.x + minX,
|
|
2250
|
+
el.y + minY,
|
|
2251
|
+
el.x + maxX,
|
|
2252
|
+
el.y + maxY
|
|
2253
|
+
];
|
|
2254
|
+
}
|
|
2255
|
+
return [
|
|
2256
|
+
el.x,
|
|
2257
|
+
el.y,
|
|
2258
|
+
el.x + el.width,
|
|
2259
|
+
el.y + el.height
|
|
2260
|
+
];
|
|
2261
|
+
}
|
|
2262
|
+
function getTransformFrame(elements) {
|
|
2263
|
+
if (elements.length === 1) {
|
|
2264
|
+
const el = elements[0];
|
|
2265
|
+
return {
|
|
2266
|
+
box: unrotatedBox(el),
|
|
2267
|
+
angle: angleOf$1(el),
|
|
2268
|
+
center: getElementCenter(el)
|
|
2269
|
+
};
|
|
2270
|
+
}
|
|
2271
|
+
const box = getCommonBounds(elements);
|
|
2272
|
+
return {
|
|
2273
|
+
box,
|
|
2274
|
+
angle: 0,
|
|
2275
|
+
center: [(box[0] + box[2]) / 2, (box[1] + box[3]) / 2]
|
|
2276
|
+
};
|
|
2277
|
+
}
|
|
2278
|
+
function getTransformHandles(box, zoom, angle = 0) {
|
|
2279
|
+
const pad = 8 / zoom;
|
|
2280
|
+
const [x1, y1, x2, y2] = [
|
|
2281
|
+
box[0] - pad,
|
|
2282
|
+
box[1] - pad,
|
|
2283
|
+
box[2] + pad,
|
|
2284
|
+
box[3] + pad
|
|
2285
|
+
];
|
|
2286
|
+
const cx = (box[0] + box[2]) / 2;
|
|
2287
|
+
const cy = (box[1] + box[3]) / 2;
|
|
2288
|
+
const raw = {
|
|
2289
|
+
nw: [x1, y1],
|
|
2290
|
+
n: [cx, y1],
|
|
2291
|
+
ne: [x2, y1],
|
|
2292
|
+
e: [x2, cy],
|
|
2293
|
+
se: [x2, y2],
|
|
2294
|
+
s: [cx, y2],
|
|
2295
|
+
sw: [x1, y2],
|
|
2296
|
+
w: [x1, cy],
|
|
2297
|
+
rotation: [cx, y1 - 24 / zoom]
|
|
2298
|
+
};
|
|
2299
|
+
if (!angle) return raw;
|
|
2300
|
+
const out = {};
|
|
2301
|
+
for (const [k, p] of Object.entries(raw)) out[k] = rotatePoint(p, [cx, cy], angle);
|
|
2302
|
+
return out;
|
|
2303
|
+
}
|
|
2304
|
+
function hitTransformHandle(handles, p, zoom) {
|
|
2305
|
+
const radius = 8 / zoom;
|
|
2306
|
+
for (const [k, [hx, hy]] of Object.entries(handles)) if (Math.hypot(p[0] - hx, p[1] - hy) <= radius) return k;
|
|
2307
|
+
return null;
|
|
2308
|
+
}
|
|
2309
|
+
function resizeBox(box, handle, delta, opts) {
|
|
2310
|
+
let [x1, y1, x2, y2] = box;
|
|
2311
|
+
const [dx, dy] = delta;
|
|
2312
|
+
if (handle.includes("e")) x2 += dx;
|
|
2313
|
+
if (handle.includes("w")) x1 += dx;
|
|
2314
|
+
if (handle.includes("s")) y2 += dy;
|
|
2315
|
+
if (handle.includes("n")) y1 += dy;
|
|
2316
|
+
if (opts.fromCenter) {
|
|
2317
|
+
if (handle.includes("e")) x1 -= dx;
|
|
2318
|
+
if (handle.includes("w")) x2 -= dx;
|
|
2319
|
+
if (handle.includes("s")) y1 -= dy;
|
|
2320
|
+
if (handle.includes("n")) y2 -= dy;
|
|
2321
|
+
}
|
|
2322
|
+
if (opts.keepRatio) {
|
|
2323
|
+
const ow = box[2] - box[0] || 1;
|
|
2324
|
+
const oh = box[3] - box[1] || 1;
|
|
2325
|
+
const horizontal = handle.includes("e") || handle.includes("w");
|
|
2326
|
+
const vertical = handle.includes("n") || handle.includes("s");
|
|
2327
|
+
const sx = (x2 - x1) / ow;
|
|
2328
|
+
const sy = (y2 - y1) / oh;
|
|
2329
|
+
const s = horizontal && vertical ? Math.abs(sx) > Math.abs(sy) ? sx : sy : horizontal ? sx : sy;
|
|
2330
|
+
const w = ow * s;
|
|
2331
|
+
const h = oh * s;
|
|
2332
|
+
const cx = (box[0] + box[2]) / 2;
|
|
2333
|
+
const cy = (box[1] + box[3]) / 2;
|
|
2334
|
+
if (opts.fromCenter) return [
|
|
2335
|
+
cx - w / 2,
|
|
2336
|
+
cy - h / 2,
|
|
2337
|
+
cx + w / 2,
|
|
2338
|
+
cy + h / 2
|
|
2339
|
+
];
|
|
2340
|
+
const ax = handle.includes("w") ? box[2] : handle.includes("e") ? box[0] : cx - w / 2;
|
|
2341
|
+
const ay = handle.includes("n") ? box[3] : handle.includes("s") ? box[1] : cy - h / 2;
|
|
2342
|
+
const nx1 = handle.includes("w") ? ax - w : ax;
|
|
2343
|
+
const ny1 = handle.includes("n") ? ay - h : ay;
|
|
2344
|
+
return [
|
|
2345
|
+
nx1,
|
|
2346
|
+
ny1,
|
|
2347
|
+
nx1 + w,
|
|
2348
|
+
ny1 + h
|
|
2349
|
+
];
|
|
2350
|
+
}
|
|
2351
|
+
return [
|
|
2352
|
+
Math.min(x1, x2),
|
|
2353
|
+
Math.min(y1, y2),
|
|
2354
|
+
Math.max(x1, x2),
|
|
2355
|
+
Math.max(y1, y2)
|
|
2356
|
+
];
|
|
2357
|
+
}
|
|
2358
|
+
function placeElement(el, frame, next, single) {
|
|
2359
|
+
const [ox1, oy1, ox2, oy2] = frame.box;
|
|
2360
|
+
const sx = (next[2] - next[0]) / (ox2 - ox1 || 1);
|
|
2361
|
+
const sy = (next[3] - next[1]) / (oy2 - oy1 || 1);
|
|
2362
|
+
const elBox = unrotatedBox(el);
|
|
2363
|
+
const newElBox = single ? next : [
|
|
2364
|
+
next[0] + (elBox[0] - ox1) * sx,
|
|
2365
|
+
next[1] + (elBox[1] - oy1) * sy,
|
|
2366
|
+
next[0] + (elBox[2] - ox1) * sx,
|
|
2367
|
+
next[1] + (elBox[3] - oy1) * sy
|
|
2368
|
+
];
|
|
2369
|
+
const w = Math.max(MIN_SIZE, newElBox[2] - newElBox[0]);
|
|
2370
|
+
const h = Math.max(MIN_SIZE, newElBox[3] - newElBox[1]);
|
|
2371
|
+
const localCenter = [(newElBox[0] + newElBox[2]) / 2, (newElBox[1] + newElBox[3]) / 2];
|
|
2372
|
+
const center = single ? rotatePoint(localCenter, frame.center, frame.angle) : localCenter;
|
|
2373
|
+
const patch = {
|
|
2374
|
+
width: w,
|
|
2375
|
+
height: h
|
|
2376
|
+
};
|
|
2377
|
+
if (isLinear(el)) {
|
|
2378
|
+
const pts = el.points.length ? el.points : [[0, 0]];
|
|
2379
|
+
const [minX, minY] = boundsOfPoints(pts);
|
|
2380
|
+
patch.points = pts.map(([px, py]) => [minX + (px - minX) * sx, minY + (py - minY) * sy]);
|
|
2381
|
+
patch.x = center[0] - w / 2 - minX;
|
|
2382
|
+
patch.y = center[1] - h / 2 - minY;
|
|
2383
|
+
} else {
|
|
2384
|
+
patch.x = center[0] - w / 2;
|
|
2385
|
+
patch.y = center[1] - h / 2;
|
|
2386
|
+
}
|
|
2387
|
+
if (el.type === "text") patch.fontSize = el.fontSize * Math.min(sx, sy);
|
|
2388
|
+
return bumpVersion(el, patch);
|
|
2389
|
+
}
|
|
2390
|
+
function resizeElements(elements, handle, start, current, opts) {
|
|
2391
|
+
if (elements.length === 0 || handle === "rotation") return [...elements];
|
|
2392
|
+
const frame = getTransformFrame(elements);
|
|
2393
|
+
const delta = rotatePoint([current[0] - start[0], current[1] - start[1]], [0, 0], -frame.angle);
|
|
2394
|
+
const keepRatio = opts.keepRatio || elements.every((el) => el.type === "text" || el.type === "svg");
|
|
2395
|
+
const next = resizeBox(frame.box, handle, delta, {
|
|
2396
|
+
...opts,
|
|
2397
|
+
keepRatio
|
|
2398
|
+
});
|
|
2399
|
+
const single = elements.length === 1;
|
|
2400
|
+
return resolveRichTextHeights(elements.map((el) => placeElement(el, frame, next, single)));
|
|
2401
|
+
}
|
|
2402
|
+
function normalizeAngle(angle) {
|
|
2403
|
+
const full = Math.PI * 2;
|
|
2404
|
+
return (angle % full + full) % full;
|
|
2405
|
+
}
|
|
2406
|
+
function rotateElements(elements, center, delta, opts) {
|
|
2407
|
+
if (elements.length === 1) {
|
|
2408
|
+
const el = elements[0];
|
|
2409
|
+
let angle = normalizeAngle(angleOf$1(el) + delta);
|
|
2410
|
+
if (opts.snap) angle = normalizeAngle(Math.round(angle / SNAP_ANGLE) * SNAP_ANGLE);
|
|
2411
|
+
return [bumpVersion(el, { angle })];
|
|
2412
|
+
}
|
|
2413
|
+
const step = opts.snap ? Math.round(delta / SNAP_ANGLE) * SNAP_ANGLE : delta;
|
|
2414
|
+
return elements.map((el) => {
|
|
2415
|
+
const c = getElementCenter(el);
|
|
2416
|
+
const [nx, ny] = rotatePoint(c, center, step);
|
|
2417
|
+
return bumpVersion(el, {
|
|
2418
|
+
x: el.x + nx - c[0],
|
|
2419
|
+
y: el.y + ny - c[1],
|
|
2420
|
+
angle: normalizeAngle(angleOf$1(el) + step)
|
|
2421
|
+
});
|
|
2422
|
+
});
|
|
2423
|
+
}
|
|
2424
|
+
function nudge(elements, ids, dx, dy) {
|
|
2425
|
+
return elements.map((el) => ids.has(el.id) ? bumpVersion(el, {
|
|
2426
|
+
x: el.x + dx,
|
|
2427
|
+
y: el.y + dy
|
|
2428
|
+
}) : el);
|
|
2429
|
+
}
|
|
2430
|
+
//#endregion
|
|
2431
|
+
//#region src/editor/tools/transform-gesture.ts
|
|
2432
|
+
function getTransformTargets(state) {
|
|
2433
|
+
const selected = getSelectedElements(state).filter((el) => !(el.type === "text" && el.containerId));
|
|
2434
|
+
if (selected.length === 0) return [];
|
|
2435
|
+
if (selected.length === 1) {
|
|
2436
|
+
const [el] = selected;
|
|
2437
|
+
if (el.locked) return [];
|
|
2438
|
+
if (isLinear(el) && el.type !== "freedraw" && el.points.length <= 2) return [];
|
|
2439
|
+
if (el.type === "arrow" && el.elbowed) return [];
|
|
2440
|
+
if (state.editingLinearId === el.id) return [];
|
|
2441
|
+
}
|
|
2442
|
+
return selected;
|
|
2443
|
+
}
|
|
2444
|
+
function getActiveHandles(state) {
|
|
2445
|
+
const targets = getTransformTargets(state);
|
|
2446
|
+
if (targets.length === 0) return null;
|
|
2447
|
+
const frame = getTransformFrame(targets);
|
|
2448
|
+
return getTransformHandles(frame.box, state.viewport.zoom, frame.angle);
|
|
2449
|
+
}
|
|
2450
|
+
var angleOf = (p, c) => Math.atan2(p[1] - c[1], p[0] - c[0]);
|
|
2451
|
+
function startTransformGesture(ctx, e) {
|
|
2452
|
+
const { store } = ctx;
|
|
2453
|
+
const state = store.getState();
|
|
2454
|
+
const handles = getActiveHandles(state);
|
|
2455
|
+
if (!handles) return null;
|
|
2456
|
+
const handle = hitTransformHandle(handles, e.scene, state.viewport.zoom);
|
|
2457
|
+
if (!handle) return null;
|
|
2458
|
+
const originals = getTransformTargets(state);
|
|
2459
|
+
const ids = new Set(originals.map((el) => el.id));
|
|
2460
|
+
const frame = getTransformFrame(originals);
|
|
2461
|
+
const start = e.scene;
|
|
2462
|
+
const startAngle = angleOf(start, frame.center);
|
|
2463
|
+
let changed = false;
|
|
2464
|
+
store.beginTransient();
|
|
2465
|
+
const replace = (next) => {
|
|
2466
|
+
const byId = new Map(next.map((el) => [el.id, el]));
|
|
2467
|
+
store.updateTransient(({ elements }) => ({ elements: refreshFrames(refreshBindings(relayoutBoundTexts(elements.map((el) => ids.has(el.id) ? byId.get(el.id) : el), ids), ids), ids) }));
|
|
2468
|
+
};
|
|
2469
|
+
return {
|
|
2470
|
+
move(ev) {
|
|
2471
|
+
changed = true;
|
|
2472
|
+
if (handle === "rotation") {
|
|
2473
|
+
const delta = angleOf(ev.scene, frame.center) - startAngle;
|
|
2474
|
+
replace(rotateElements(originals, frame.center, delta, { snap: ev.shiftKey }));
|
|
2475
|
+
return;
|
|
2476
|
+
}
|
|
2477
|
+
replace(resizeElements(originals, handle, start, snapPoint(ev.scene, activeGrid(store, ev)), {
|
|
2478
|
+
keepRatio: ev.shiftKey,
|
|
2479
|
+
fromCenter: ev.altKey
|
|
2480
|
+
}));
|
|
2481
|
+
},
|
|
2482
|
+
end(keep) {
|
|
2483
|
+
store.endTransient(keep && changed);
|
|
2484
|
+
}
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
//#endregion
|
|
2488
|
+
//#region src/editor/tools/selection.ts
|
|
2489
|
+
function moveElements(originals, dx, dy) {
|
|
2490
|
+
return (elements) => elements.map((el) => {
|
|
2491
|
+
const original = originals.get(el.id);
|
|
2492
|
+
return original ? bumpVersion(original, {
|
|
2493
|
+
x: original.x + dx,
|
|
2494
|
+
y: original.y + dy
|
|
2495
|
+
}) : el;
|
|
2496
|
+
});
|
|
2497
|
+
}
|
|
2498
|
+
function createSelectionTool(options = {}) {
|
|
2499
|
+
let mode = "idle";
|
|
2500
|
+
let start = [0, 0];
|
|
2501
|
+
let moved = false;
|
|
2502
|
+
let originals = /* @__PURE__ */ new Map();
|
|
2503
|
+
let dragOrigin = [0, 0];
|
|
2504
|
+
let baseSelection = /* @__PURE__ */ new Set();
|
|
2505
|
+
let gesture = null;
|
|
2506
|
+
let pointGesture = null;
|
|
2507
|
+
return {
|
|
2508
|
+
onPointerDown(ctx, e) {
|
|
2509
|
+
const { store } = ctx;
|
|
2510
|
+
pointGesture = startPointGesture(ctx, e, options.onPointMove);
|
|
2511
|
+
if (pointGesture) {
|
|
2512
|
+
mode = "point";
|
|
2513
|
+
return;
|
|
2514
|
+
}
|
|
2515
|
+
gesture = startTransformGesture(ctx, e);
|
|
2516
|
+
if (gesture) {
|
|
2517
|
+
mode = "transform";
|
|
2518
|
+
return;
|
|
2519
|
+
}
|
|
2520
|
+
const state = store.getState();
|
|
2521
|
+
const target = getSelectionTarget(state, e.scene, 6 / state.viewport.zoom);
|
|
2522
|
+
start = e.scene;
|
|
2523
|
+
moved = false;
|
|
2524
|
+
if (!target) {
|
|
2525
|
+
baseSelection = e.shiftKey ? new Set(state.selectedIds) : /* @__PURE__ */ new Set();
|
|
2526
|
+
store.setState({
|
|
2527
|
+
selectedIds: baseSelection,
|
|
2528
|
+
editingGroupId: e.shiftKey ? state.editingGroupId : null,
|
|
2529
|
+
selectedPointIndex: null
|
|
2530
|
+
});
|
|
2531
|
+
mode = "box";
|
|
2532
|
+
return;
|
|
2533
|
+
}
|
|
2534
|
+
let selected = new Set(state.selectedIds);
|
|
2535
|
+
if (e.shiftKey) {
|
|
2536
|
+
const all = target.ids.every((id) => selected.has(id));
|
|
2537
|
+
for (const id of target.ids) if (all) selected.delete(id);
|
|
2538
|
+
else selected.add(id);
|
|
2539
|
+
} else if (!selected.has(target.hitId)) selected = new Set(target.ids);
|
|
2540
|
+
store.setState({
|
|
2541
|
+
selectedIds: selected,
|
|
2542
|
+
selectedPointIndex: null
|
|
2543
|
+
});
|
|
2544
|
+
if (!selected.has(target.hitId)) {
|
|
2545
|
+
mode = "idle";
|
|
2546
|
+
return;
|
|
2547
|
+
}
|
|
2548
|
+
const ids = withBoundText(state.elements, withFrameChildren(state.elements, selected));
|
|
2549
|
+
originals = new Map(state.elements.filter((el) => ids.has(el.id)).map((el) => [el.id, el]));
|
|
2550
|
+
const [minX, minY] = getCommonBounds([...originals.values()]);
|
|
2551
|
+
dragOrigin = [minX, minY];
|
|
2552
|
+
mode = "drag";
|
|
2553
|
+
store.beginTransient();
|
|
2554
|
+
},
|
|
2555
|
+
onPointerMove(ctx, e) {
|
|
2556
|
+
const { store } = ctx;
|
|
2557
|
+
if (mode === "transform") {
|
|
2558
|
+
gesture?.move(e);
|
|
2559
|
+
return;
|
|
2560
|
+
}
|
|
2561
|
+
if (mode === "point") {
|
|
2562
|
+
pointGesture?.move(e);
|
|
2563
|
+
return;
|
|
2564
|
+
}
|
|
2565
|
+
let dx = e.scene[0] - start[0];
|
|
2566
|
+
let dy = e.scene[1] - start[1];
|
|
2567
|
+
if (mode === "drag") {
|
|
2568
|
+
const zoom = store.getState().viewport.zoom;
|
|
2569
|
+
if (!moved && Math.hypot(dx, dy) * zoom < 2) return;
|
|
2570
|
+
moved = true;
|
|
2571
|
+
const grid = activeGrid(store, e);
|
|
2572
|
+
if (grid) {
|
|
2573
|
+
dx = snapValue(dragOrigin[0] + dx, grid) - dragOrigin[0];
|
|
2574
|
+
dy = snapValue(dragOrigin[1] + dy, grid) - dragOrigin[1];
|
|
2575
|
+
}
|
|
2576
|
+
const move = moveElements(originals, dx, dy);
|
|
2577
|
+
const ids = new Set(originals.keys());
|
|
2578
|
+
store.updateTransient(({ elements }) => ({ elements: refreshFrames(refreshBindings(move(elements), ids), ids) }));
|
|
2579
|
+
return;
|
|
2580
|
+
}
|
|
2581
|
+
if (mode === "box") {
|
|
2582
|
+
const box = normalizeBox(start, e.scene);
|
|
2583
|
+
const inside = selectInBox(store.getState(), box);
|
|
2584
|
+
store.setState({
|
|
2585
|
+
selectionBox: box,
|
|
2586
|
+
selectedIds: /* @__PURE__ */ new Set([...baseSelection, ...inside])
|
|
2587
|
+
});
|
|
2588
|
+
}
|
|
2589
|
+
},
|
|
2590
|
+
onPointerUp(ctx) {
|
|
2591
|
+
const { store } = ctx;
|
|
2592
|
+
if (mode === "transform") gesture?.end(true);
|
|
2593
|
+
if (mode === "point") pointGesture?.end();
|
|
2594
|
+
gesture = null;
|
|
2595
|
+
pointGesture = null;
|
|
2596
|
+
if (mode === "drag") store.endTransient(moved);
|
|
2597
|
+
if (mode === "box") store.setState({ selectionBox: null });
|
|
2598
|
+
mode = "idle";
|
|
2599
|
+
},
|
|
2600
|
+
cancel(ctx) {
|
|
2601
|
+
const { store } = ctx;
|
|
2602
|
+
if (mode === "transform") gesture?.end(false);
|
|
2603
|
+
if (mode === "point") pointGesture?.cancel();
|
|
2604
|
+
if (mode === "drag") store.endTransient(false);
|
|
2605
|
+
if (mode === "box") store.setState({ selectionBox: null });
|
|
2606
|
+
gesture = null;
|
|
2607
|
+
pointGesture = null;
|
|
2608
|
+
mode = "idle";
|
|
2609
|
+
},
|
|
2610
|
+
onDoubleClick(ctx, e) {
|
|
2611
|
+
const { store } = ctx;
|
|
2612
|
+
const state = store.getState();
|
|
2613
|
+
const target = getSelectionTarget(state, e.scene, 6 / state.viewport.zoom);
|
|
2614
|
+
if (!target) {
|
|
2615
|
+
store.setState({ editingGroupId: null });
|
|
2616
|
+
startTextEditing(store, { at: e.scene });
|
|
2617
|
+
return;
|
|
2618
|
+
}
|
|
2619
|
+
const el = state.elements.find((item) => item.id === target.hitId);
|
|
2620
|
+
if (!el) return;
|
|
2621
|
+
if (el.type === "frame" || el.type === "magicframe") {
|
|
2622
|
+
store.setState({
|
|
2623
|
+
editingFrameId: el.id,
|
|
2624
|
+
selectedIds: /* @__PURE__ */ new Set([el.id])
|
|
2625
|
+
});
|
|
2626
|
+
return;
|
|
2627
|
+
}
|
|
2628
|
+
const groups = el.groupIds ?? [];
|
|
2629
|
+
const depth = state.editingGroupId ? groups.indexOf(state.editingGroupId) : groups.length;
|
|
2630
|
+
if (groups.length > 0 && depth > 0) {
|
|
2631
|
+
const nextGroup = groups[depth - 1];
|
|
2632
|
+
const inner = depth - 1 > 0 ? groups[depth - 2] : null;
|
|
2633
|
+
const members = inner ? state.elements.filter((item) => item.groupIds?.includes(inner)).map((item) => item.id) : [el.id];
|
|
2634
|
+
store.setState({
|
|
2635
|
+
editingGroupId: nextGroup,
|
|
2636
|
+
selectedIds: new Set(members)
|
|
2637
|
+
});
|
|
2638
|
+
return;
|
|
2639
|
+
}
|
|
2640
|
+
if (el.type === "richtext") startRichTextEditing(store, el.id);
|
|
2641
|
+
else if (el.type === "text") startTextEditing(store, { elementId: el.id });
|
|
2642
|
+
else if (canHoldText(el)) startTextEditing(store, { containerId: el.id });
|
|
2643
|
+
}
|
|
2644
|
+
};
|
|
2645
|
+
}
|
|
2646
|
+
//#endregion
|
|
2647
|
+
//#region src/editor/tools/shape.ts
|
|
2648
|
+
function shapeBox(start, current, square, fromCenter) {
|
|
2649
|
+
let w = current[0] - start[0];
|
|
2650
|
+
let h = current[1] - start[1];
|
|
2651
|
+
if (square) {
|
|
2652
|
+
const size = Math.max(Math.abs(w), Math.abs(h));
|
|
2653
|
+
w = Math.sign(w || 1) * size;
|
|
2654
|
+
h = Math.sign(h || 1) * size;
|
|
2655
|
+
}
|
|
2656
|
+
if (fromCenter) return {
|
|
2657
|
+
x: start[0] - Math.abs(w),
|
|
2658
|
+
y: start[1] - Math.abs(h),
|
|
2659
|
+
width: Math.abs(w) * 2,
|
|
2660
|
+
height: Math.abs(h) * 2
|
|
2661
|
+
};
|
|
2662
|
+
return {
|
|
2663
|
+
x: Math.min(start[0], start[0] + w),
|
|
2664
|
+
y: Math.min(start[1], start[1] + h),
|
|
2665
|
+
width: Math.abs(w),
|
|
2666
|
+
height: Math.abs(h)
|
|
2667
|
+
};
|
|
2668
|
+
}
|
|
2669
|
+
function createShapeTool(type, options = {}) {
|
|
2670
|
+
let id = null;
|
|
2671
|
+
let start = [0, 0];
|
|
2672
|
+
return {
|
|
2673
|
+
onPointerDown(ctx, e) {
|
|
2674
|
+
const { store } = ctx;
|
|
2675
|
+
const style = store.getState().currentStyle;
|
|
2676
|
+
start = snapPoint(e.scene, activeGrid(store, e));
|
|
2677
|
+
const partial = {
|
|
2678
|
+
x: start[0],
|
|
2679
|
+
y: start[1],
|
|
2680
|
+
width: 0,
|
|
2681
|
+
height: 0
|
|
2682
|
+
};
|
|
2683
|
+
const el = type === "frame" ? newFrameElement(partial, style) : newElement(type, partial, style);
|
|
2684
|
+
id = el.id;
|
|
2685
|
+
store.beginTransient();
|
|
2686
|
+
appendElement(store, el);
|
|
2687
|
+
store.setState({ selectedIds: /* @__PURE__ */ new Set() });
|
|
2688
|
+
},
|
|
2689
|
+
onPointerMove(ctx, e) {
|
|
2690
|
+
if (!id) return;
|
|
2691
|
+
const current = findElement(ctx.store, id);
|
|
2692
|
+
if (!current) return;
|
|
2693
|
+
const at = snapPoint(e.scene, activeGrid(ctx.store, e));
|
|
2694
|
+
replaceElement(ctx.store, {
|
|
2695
|
+
...current,
|
|
2696
|
+
...shapeBox(start, at, e.shiftKey, e.altKey)
|
|
2697
|
+
});
|
|
2698
|
+
},
|
|
2699
|
+
onPointerUp(ctx) {
|
|
2700
|
+
if (!id) return;
|
|
2701
|
+
const { store } = ctx;
|
|
2702
|
+
const el = findElement(store, id);
|
|
2703
|
+
const zoom = store.getState().viewport.zoom;
|
|
2704
|
+
const createdId = id;
|
|
2705
|
+
id = null;
|
|
2706
|
+
if (!el || Math.max(el.width, el.height) * zoom < 2) {
|
|
2707
|
+
cancelCreation(store);
|
|
2708
|
+
return;
|
|
2709
|
+
}
|
|
2710
|
+
finishCreation(store, createdId);
|
|
2711
|
+
options.onCreated?.(ctx, el);
|
|
2712
|
+
},
|
|
2713
|
+
cancel(ctx) {
|
|
2714
|
+
if (!id) return;
|
|
2715
|
+
id = null;
|
|
2716
|
+
cancelCreation(ctx.store);
|
|
2717
|
+
}
|
|
2718
|
+
};
|
|
2719
|
+
}
|
|
2720
|
+
//#endregion
|
|
2721
|
+
//#region src/editor/tools/text.ts
|
|
2722
|
+
function createTextTool() {
|
|
2723
|
+
let downAt = null;
|
|
2724
|
+
return {
|
|
2725
|
+
onPointerDown(_ctx, e) {
|
|
2726
|
+
downAt = e.scene;
|
|
2727
|
+
},
|
|
2728
|
+
onPointerMove() {},
|
|
2729
|
+
onPointerUp(ctx) {
|
|
2730
|
+
const at = downAt;
|
|
2731
|
+
downAt = null;
|
|
2732
|
+
if (!at) return;
|
|
2733
|
+
const { store } = ctx;
|
|
2734
|
+
const state = store.getState();
|
|
2735
|
+
const hit = getHitElement(state.elements, at, 6 / state.viewport.zoom);
|
|
2736
|
+
if (!state.toolLocked) store.setState({ tool: "selection" });
|
|
2737
|
+
if (hit?.type === "text") startTextEditing(store, { elementId: hit.id });
|
|
2738
|
+
else if (hit && canHoldText(hit) && hit.type !== "arrow") startTextEditing(store, { containerId: hit.id });
|
|
2739
|
+
else startTextEditing(store, { at });
|
|
2740
|
+
},
|
|
2741
|
+
cancel() {
|
|
2742
|
+
downAt = null;
|
|
2743
|
+
}
|
|
2744
|
+
};
|
|
2745
|
+
}
|
|
2746
|
+
//#endregion
|
|
2747
|
+
//#region src/editor/tools/index.ts
|
|
2748
|
+
function createToolHandlers() {
|
|
2749
|
+
return {
|
|
2750
|
+
selection: createSelectionTool(),
|
|
2751
|
+
hand: createHandTool(),
|
|
2752
|
+
rectangle: createShapeTool("rectangle"),
|
|
2753
|
+
diamond: createShapeTool("diamond"),
|
|
2754
|
+
ellipse: createShapeTool("ellipse"),
|
|
2755
|
+
frame: createShapeTool("frame"),
|
|
2756
|
+
arrow: createLinearTool("arrow"),
|
|
2757
|
+
line: createLinearTool("line"),
|
|
2758
|
+
freedraw: createFreedrawTool(),
|
|
2759
|
+
eraser: createEraserTool(),
|
|
2760
|
+
text: createTextTool(),
|
|
2761
|
+
card: createCardTool(),
|
|
2762
|
+
image: createImageTool()
|
|
2763
|
+
};
|
|
2764
|
+
}
|
|
2765
|
+
function getToolHandler(handlers, tool) {
|
|
2766
|
+
return handlers[tool] ?? handlers.selection;
|
|
2767
|
+
}
|
|
2768
|
+
//#endregion
|
|
2769
|
+
//#region src/editor/WhiteboardCanvas.tsx
|
|
2770
|
+
var DOT_SPACING = 16;
|
|
2771
|
+
var CHROME_ATTR = "data-whiteboard-chrome";
|
|
2772
|
+
function isChromeTarget(target) {
|
|
2773
|
+
return target instanceof Element && !!target.closest(`[data-whiteboard-chrome]`);
|
|
2774
|
+
}
|
|
2775
|
+
var WHEEL_ZOOM_FACTOR = .01;
|
|
2776
|
+
var WhiteboardCanvas = ({ children, context, handlers, store, theme }) => {
|
|
2777
|
+
const ref = useRef(null);
|
|
2778
|
+
const activeRef = useRef(null);
|
|
2779
|
+
const spaceRef = useRef(false);
|
|
2780
|
+
const lastPointerRef = useRef(null);
|
|
2781
|
+
const gesturesRef = useRef(createGestureTracker());
|
|
2782
|
+
const elements = useStore(store, (s) => s.elements);
|
|
2783
|
+
const files = useStore(store, (s) => s.files);
|
|
2784
|
+
const viewport = useStore(store, (s) => s.viewport);
|
|
2785
|
+
const editingTextId = useStore(store, (s) => s.editingTextId);
|
|
2786
|
+
const editingRichTextId = useStore(store, (s) => s.editingRichTextId);
|
|
2787
|
+
const erasingIds = useStore(store, (s) => s.erasingIds);
|
|
2788
|
+
const tool = useStore(store, (s) => s.tool);
|
|
2789
|
+
const gridSize = useStore(store, (s) => s.gridSize);
|
|
2790
|
+
const toolRef = useRef(tool);
|
|
2791
|
+
useEffect(() => {
|
|
2792
|
+
const previous = toolRef.current;
|
|
2793
|
+
toolRef.current = tool;
|
|
2794
|
+
if (previous === tool) return;
|
|
2795
|
+
const handler = getToolHandler(handlers, previous);
|
|
2796
|
+
handler.cancel?.(context);
|
|
2797
|
+
if (activeRef.current === handler) activeRef.current = null;
|
|
2798
|
+
}, [
|
|
2799
|
+
context,
|
|
2800
|
+
handlers,
|
|
2801
|
+
tool
|
|
2802
|
+
]);
|
|
2803
|
+
const scene = useMemo(() => ({
|
|
2804
|
+
type: "excalidraw",
|
|
2805
|
+
appState: {},
|
|
2806
|
+
elements,
|
|
2807
|
+
files
|
|
2808
|
+
}), [elements, files]);
|
|
2809
|
+
const hiddenIds = useMemo(() => editingTextId ? /* @__PURE__ */ new Set([editingTextId]) : void 0, [editingTextId]);
|
|
2810
|
+
const elementProps = useCallback((el) => {
|
|
2811
|
+
if (el.id === editingRichTextId) return { className: editingCard };
|
|
2812
|
+
if (erasingIds.has(el.id)) return { opacity: .25 };
|
|
2813
|
+
if (el.type === "image" && el.status === "pending") return { opacity: .5 };
|
|
2814
|
+
}, [erasingIds, editingRichTextId]);
|
|
2815
|
+
const toPointer = useCallback((e) => {
|
|
2816
|
+
const rect = ref.current.getBoundingClientRect();
|
|
2817
|
+
const screen = [e.clientX - rect.left, e.clientY - rect.top];
|
|
2818
|
+
return {
|
|
2819
|
+
screen,
|
|
2820
|
+
scene: screenToScene(screen, store.getState().viewport),
|
|
2821
|
+
shiftKey: !!e.shiftKey,
|
|
2822
|
+
altKey: !!e.altKey,
|
|
2823
|
+
ctrlKey: !!(e.ctrlKey || e.metaKey),
|
|
2824
|
+
pressure: e.pressure ?? .5,
|
|
2825
|
+
pointerType: e.pointerType ?? "mouse",
|
|
2826
|
+
pointerId: e.pointerId ?? 1
|
|
2827
|
+
};
|
|
2828
|
+
}, [store]);
|
|
2829
|
+
const onPointerDown = (e) => {
|
|
2830
|
+
if (e.button === 2 || isChromeTarget(e.target)) return;
|
|
2831
|
+
ref.current?.focus({ preventScroll: true });
|
|
2832
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
2833
|
+
if (e.pointerType === "touch") {
|
|
2834
|
+
const start = gesturesRef.current.down(e.pointerId, toPointer(e).screen, store.getState().viewport);
|
|
2835
|
+
if (start === "pinch") {
|
|
2836
|
+
activeRef.current?.cancel?.(context);
|
|
2837
|
+
activeRef.current = null;
|
|
2838
|
+
}
|
|
2839
|
+
if (start !== "single") return;
|
|
2840
|
+
}
|
|
2841
|
+
const handler = e.button === 1 || spaceRef.current ? handlers.hand : getToolHandler(handlers, store.getState().tool);
|
|
2842
|
+
activeRef.current = handler;
|
|
2843
|
+
handler.onPointerDown(context, toPointer(e));
|
|
2844
|
+
};
|
|
2845
|
+
const onPointerMove = (e) => {
|
|
2846
|
+
if (e.pointerType === "touch") {
|
|
2847
|
+
const gestures = gesturesRef.current;
|
|
2848
|
+
const viewport = gestures.move(e.pointerId, toPointer(e).screen);
|
|
2849
|
+
if (viewport) store.setState({ viewport });
|
|
2850
|
+
if (gestures.isActive()) return;
|
|
2851
|
+
}
|
|
2852
|
+
const handler = activeRef.current ?? getToolHandler(handlers, store.getState().tool);
|
|
2853
|
+
const info = toPointer(e);
|
|
2854
|
+
lastPointerRef.current = info.scene;
|
|
2855
|
+
handler.onPointerMove(context, info);
|
|
2856
|
+
};
|
|
2857
|
+
const onPointerUp = (e) => {
|
|
2858
|
+
if (e.pointerType === "touch") {
|
|
2859
|
+
const gestures = gesturesRef.current;
|
|
2860
|
+
const wasActive = gestures.isActive();
|
|
2861
|
+
gestures.up(e.pointerId);
|
|
2862
|
+
if (wasActive) return;
|
|
2863
|
+
}
|
|
2864
|
+
const handler = activeRef.current;
|
|
2865
|
+
activeRef.current = null;
|
|
2866
|
+
handler?.onPointerUp(context, toPointer(e));
|
|
2867
|
+
};
|
|
2868
|
+
useEffect(() => {
|
|
2869
|
+
const node = ref.current;
|
|
2870
|
+
if (!node) return;
|
|
2871
|
+
const onWheel = (e) => {
|
|
2872
|
+
if (isChromeTarget(e.target)) return;
|
|
2873
|
+
e.preventDefault();
|
|
2874
|
+
const vp = store.getState().viewport;
|
|
2875
|
+
const rect = node.getBoundingClientRect();
|
|
2876
|
+
const screen = [e.clientX - rect.left, e.clientY - rect.top];
|
|
2877
|
+
if (e.ctrlKey || e.metaKey) {
|
|
2878
|
+
store.setState({ viewport: zoomAt(vp, screen, vp.zoom * Math.exp(-e.deltaY * WHEEL_ZOOM_FACTOR)) });
|
|
2879
|
+
return;
|
|
2880
|
+
}
|
|
2881
|
+
const dx = e.shiftKey && !e.deltaX ? e.deltaY : e.deltaX;
|
|
2882
|
+
const dy = e.shiftKey && !e.deltaX ? 0 : e.deltaY;
|
|
2883
|
+
store.setState({ viewport: {
|
|
2884
|
+
...vp,
|
|
2885
|
+
scrollX: vp.scrollX - dx / vp.zoom,
|
|
2886
|
+
scrollY: vp.scrollY - dy / vp.zoom
|
|
2887
|
+
} });
|
|
2888
|
+
};
|
|
2889
|
+
const onKey = (e) => {
|
|
2890
|
+
if (e.key === " " && !(e.target instanceof HTMLTextAreaElement)) spaceRef.current = e.type === "keydown";
|
|
2891
|
+
};
|
|
2892
|
+
const measure = () => {
|
|
2893
|
+
const rect = node.getBoundingClientRect();
|
|
2894
|
+
store.setState({ canvasSize: {
|
|
2895
|
+
width: rect.width,
|
|
2896
|
+
height: rect.height
|
|
2897
|
+
} });
|
|
2898
|
+
};
|
|
2899
|
+
measure();
|
|
2900
|
+
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(measure);
|
|
2901
|
+
observer?.observe(node);
|
|
2902
|
+
const pasteTarget = () => {
|
|
2903
|
+
if (lastPointerRef.current) return lastPointerRef.current;
|
|
2904
|
+
const { viewport, canvasSize } = store.getState();
|
|
2905
|
+
return screenToScene([canvasSize.width / 2, canvasSize.height / 2], viewport);
|
|
2906
|
+
};
|
|
2907
|
+
const onCopy = (e) => {
|
|
2908
|
+
if (!e.clipboardData || store.getState().editingTextId || store.getState().editingRichTextId || store.getState().selectedIds.size === 0) return;
|
|
2909
|
+
e.preventDefault();
|
|
2910
|
+
e.clipboardData.setData("text/plain", e.type === "cut" ? cutSelection(store) : copySelection(store));
|
|
2911
|
+
};
|
|
2912
|
+
const onPaste = (e) => {
|
|
2913
|
+
const { editingRichTextId: editingCard, editingTextId: editingText } = store.getState();
|
|
2914
|
+
if (!e.clipboardData || editingText || editingCard) return;
|
|
2915
|
+
const files = [...e.clipboardData.files ?? []].filter((f) => f.type.startsWith("image/"));
|
|
2916
|
+
if (files.length > 0 && context.onFiles) {
|
|
2917
|
+
e.preventDefault();
|
|
2918
|
+
context.onFiles(files, pasteTarget());
|
|
2919
|
+
return;
|
|
2920
|
+
}
|
|
2921
|
+
if (pasteFromClipboard(store, e.clipboardData.getData("text/plain"), pasteTarget())) e.preventDefault();
|
|
2922
|
+
};
|
|
2923
|
+
node.addEventListener("copy", onCopy);
|
|
2924
|
+
node.addEventListener("cut", onCopy);
|
|
2925
|
+
node.addEventListener("paste", onPaste);
|
|
2926
|
+
node.addEventListener("wheel", onWheel, { passive: false });
|
|
2927
|
+
window.addEventListener("keydown", onKey);
|
|
2928
|
+
window.addEventListener("keyup", onKey);
|
|
2929
|
+
return () => {
|
|
2930
|
+
observer?.disconnect();
|
|
2931
|
+
node.removeEventListener("copy", onCopy);
|
|
2932
|
+
node.removeEventListener("cut", onCopy);
|
|
2933
|
+
node.removeEventListener("paste", onPaste);
|
|
2934
|
+
node.removeEventListener("wheel", onWheel);
|
|
2935
|
+
window.removeEventListener("keydown", onKey);
|
|
2936
|
+
window.removeEventListener("keyup", onKey);
|
|
2937
|
+
};
|
|
2938
|
+
}, [context, store]);
|
|
2939
|
+
const dot = (gridSize ?? DOT_SPACING) * viewport.zoom;
|
|
2940
|
+
const isDark = theme === "dark";
|
|
2941
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2942
|
+
"data-whiteboard-canvas": true,
|
|
2943
|
+
className: canvas,
|
|
2944
|
+
ref,
|
|
2945
|
+
tabIndex: 0,
|
|
2946
|
+
style: {
|
|
2947
|
+
backgroundSize: `${dot}px ${dot}px`,
|
|
2948
|
+
backgroundPosition: `${viewport.scrollX * viewport.zoom}px ${viewport.scrollY * viewport.zoom}px`
|
|
2949
|
+
},
|
|
2950
|
+
onPointerCancel: onPointerUp,
|
|
2951
|
+
onPointerDown,
|
|
2952
|
+
onPointerMove,
|
|
2953
|
+
onPointerUp,
|
|
2954
|
+
onDoubleClick: (e) => {
|
|
2955
|
+
if (isChromeTarget(e.target)) return;
|
|
2956
|
+
getToolHandler(handlers, store.getState().tool).onDoubleClick?.(context, toPointer(e));
|
|
2957
|
+
},
|
|
2958
|
+
onDragOver: (e) => {
|
|
2959
|
+
if (e.dataTransfer.types.includes("Files")) e.preventDefault();
|
|
2960
|
+
},
|
|
2961
|
+
onDrop: (e) => {
|
|
2962
|
+
const dropped = [...e.dataTransfer.files ?? []].filter((f) => f.type.startsWith("image/"));
|
|
2963
|
+
if (dropped.length === 0 || !context.onFiles) return;
|
|
2964
|
+
e.preventDefault();
|
|
2965
|
+
context.onFiles(dropped, toPointer(e).scene);
|
|
2966
|
+
},
|
|
2967
|
+
children: [/* @__PURE__ */ jsx("svg", {
|
|
2968
|
+
className: layer,
|
|
2969
|
+
style: isDark ? { filter: DARK_SCENE_FILTER } : void 0,
|
|
2970
|
+
children: /* @__PURE__ */ jsx("g", {
|
|
2971
|
+
transform: `scale(${viewport.zoom}) translate(${viewport.scrollX} ${viewport.scrollY})`,
|
|
2972
|
+
children: /* @__PURE__ */ jsx(SceneContent, {
|
|
2973
|
+
dark: isDark,
|
|
2974
|
+
elementProps,
|
|
2975
|
+
files,
|
|
2976
|
+
hiddenIds,
|
|
2977
|
+
scene
|
|
2978
|
+
})
|
|
2979
|
+
})
|
|
2980
|
+
}), children]
|
|
2981
|
+
});
|
|
2982
|
+
};
|
|
2983
|
+
//#endregion
|
|
2984
|
+
//#region src/editor/FrameNameEditor.tsx
|
|
2985
|
+
var Editor = ({ frame, store }) => {
|
|
2986
|
+
const [value, setValue] = useState(frame.name ?? "");
|
|
2987
|
+
const ref = useRef(null);
|
|
2988
|
+
const viewport = useStore(store, (s) => s.viewport);
|
|
2989
|
+
const [left, top] = sceneToScreen([frame.x, frame.y - 20], viewport);
|
|
2990
|
+
useEffect(() => {
|
|
2991
|
+
ref.current?.focus();
|
|
2992
|
+
ref.current?.select();
|
|
2993
|
+
}, []);
|
|
2994
|
+
const closedRef = useRef(false);
|
|
2995
|
+
const close = (save) => {
|
|
2996
|
+
if (closedRef.current) return;
|
|
2997
|
+
closedRef.current = true;
|
|
2998
|
+
store.setState({ editingFrameId: null });
|
|
2999
|
+
if (!save) return;
|
|
3000
|
+
const name = value.trim() || null;
|
|
3001
|
+
if (name === (frame.name ?? null)) return;
|
|
3002
|
+
store.commit(({ elements }) => ({ elements: elements.map((el) => el.id === frame.id ? bumpVersion(el, { name }) : el) }));
|
|
3003
|
+
};
|
|
3004
|
+
return /* @__PURE__ */ jsx("input", {
|
|
3005
|
+
"data-frame-name-editor": true,
|
|
3006
|
+
className: frameNameEditor,
|
|
3007
|
+
ref,
|
|
3008
|
+
value,
|
|
3009
|
+
[CHROME_ATTR]: "",
|
|
3010
|
+
style: {
|
|
3011
|
+
left,
|
|
3012
|
+
top,
|
|
3013
|
+
height: 20 * viewport.zoom
|
|
3014
|
+
},
|
|
3015
|
+
onBlur: () => close(true),
|
|
3016
|
+
onChange: (e) => setValue(e.currentTarget.value),
|
|
3017
|
+
onKeyDown: (e) => {
|
|
3018
|
+
e.stopPropagation();
|
|
3019
|
+
if (e.key === "Enter") close(true);
|
|
3020
|
+
if (e.key === "Escape") close(false);
|
|
3021
|
+
}
|
|
3022
|
+
});
|
|
3023
|
+
};
|
|
3024
|
+
var FrameNameEditor = ({ store }) => {
|
|
3025
|
+
const frame = useStore(store, (s) => s.editingFrameId ? s.elements.find((el) => el.id === s.editingFrameId) : void 0);
|
|
3026
|
+
if (!frame || frame.isDeleted) return null;
|
|
3027
|
+
return /* @__PURE__ */ jsx(Editor, {
|
|
3028
|
+
frame,
|
|
3029
|
+
store
|
|
3030
|
+
}, frame.id);
|
|
3031
|
+
};
|
|
3032
|
+
//#endregion
|
|
3033
|
+
//#region src/editor/MobileDock.tsx
|
|
3034
|
+
var MobileDock = ({ store }) => /* @__PURE__ */ jsx("div", {
|
|
3035
|
+
"data-mobile-dock": true,
|
|
3036
|
+
"data-whiteboard-chrome": true,
|
|
3037
|
+
className: `${floating} ${dock}`,
|
|
3038
|
+
children: /* @__PURE__ */ jsx(WhiteboardToolbar, {
|
|
3039
|
+
showKeys: false,
|
|
3040
|
+
store
|
|
3041
|
+
})
|
|
3042
|
+
});
|
|
3043
|
+
//#endregion
|
|
3044
|
+
//#region src/editor/overlay/LinearHandles.tsx
|
|
3045
|
+
var LinearHandles = ({ store }) => {
|
|
3046
|
+
const state = useStore(store, (s) => s);
|
|
3047
|
+
if (state.selectionBox || state.editingTextId || state.tool !== "selection") return null;
|
|
3048
|
+
const el = getEditedLinear(state);
|
|
3049
|
+
if (!el) return null;
|
|
3050
|
+
const points = getLinearAbsolutePoints(el).map((p, i, all) => ({
|
|
3051
|
+
i,
|
|
3052
|
+
p: sceneToScreen(p, state.viewport),
|
|
3053
|
+
end: i === 0 || i === all.length - 1
|
|
3054
|
+
})).filter(({ end }) => end || !el.elbowed);
|
|
3055
|
+
const mids = el.elbowed ? [] : getMidpoints(el).map((p) => sceneToScreen(p, state.viewport));
|
|
3056
|
+
const segments = el.elbowed ? getElbowSegmentHandles(el).map((h) => ({
|
|
3057
|
+
...h,
|
|
3058
|
+
point: sceneToScreen(h.point, state.viewport)
|
|
3059
|
+
})) : [];
|
|
3060
|
+
return /* @__PURE__ */ jsxs("g", {
|
|
3061
|
+
"data-linear-handles": true,
|
|
3062
|
+
children: [
|
|
3063
|
+
mids.map(([x, y], i) => /* @__PURE__ */ jsx("circle", {
|
|
3064
|
+
cx: x,
|
|
3065
|
+
cy: y,
|
|
3066
|
+
"data-midpoint": i,
|
|
3067
|
+
fill: accentStroke,
|
|
3068
|
+
fillOpacity: .25,
|
|
3069
|
+
r: 3.5
|
|
3070
|
+
}, `m${i}`)),
|
|
3071
|
+
segments.map(({ index, point: [x, y] }) => /* @__PURE__ */ jsx("rect", {
|
|
3072
|
+
"data-segment": index,
|
|
3073
|
+
fill: "#fff",
|
|
3074
|
+
height: 6,
|
|
3075
|
+
rx: 3,
|
|
3076
|
+
stroke: accentStroke,
|
|
3077
|
+
strokeWidth: 1.5,
|
|
3078
|
+
width: 12,
|
|
3079
|
+
x: x - 6,
|
|
3080
|
+
y: y - 3
|
|
3081
|
+
}, `s${index}`)),
|
|
3082
|
+
points.map(({ i, p: [x, y] }) => /* @__PURE__ */ jsx("circle", {
|
|
3083
|
+
cx: x,
|
|
3084
|
+
cy: y,
|
|
3085
|
+
"data-point": i,
|
|
3086
|
+
fill: state.selectedPointIndex === i ? accentStroke : "#fff",
|
|
3087
|
+
r: 5,
|
|
3088
|
+
stroke: accentStroke,
|
|
3089
|
+
strokeWidth: 1.5
|
|
3090
|
+
}, `p${i}`))
|
|
3091
|
+
]
|
|
3092
|
+
});
|
|
3093
|
+
};
|
|
3094
|
+
//#endregion
|
|
3095
|
+
//#region src/editor/overlay/TransformHandles.tsx
|
|
3096
|
+
var HANDLE_RADIUS = 4.5;
|
|
3097
|
+
var ROTATION_RADIUS = 5;
|
|
3098
|
+
var TransformHandles = ({ store }) => {
|
|
3099
|
+
const state = useStore(store, (s) => s);
|
|
3100
|
+
if (state.selectionBox || state.editingTextId) return null;
|
|
3101
|
+
const handles = getActiveHandles(state);
|
|
3102
|
+
if (!handles) return null;
|
|
3103
|
+
return /* @__PURE__ */ jsx("g", {
|
|
3104
|
+
"data-transform-handles": true,
|
|
3105
|
+
children: Object.entries(handles).map(([name, point]) => {
|
|
3106
|
+
const [x, y] = sceneToScreen(point, state.viewport);
|
|
3107
|
+
return /* @__PURE__ */ jsx("circle", {
|
|
3108
|
+
cx: x,
|
|
3109
|
+
cy: y,
|
|
3110
|
+
"data-handle": name,
|
|
3111
|
+
fill: "#fff",
|
|
3112
|
+
r: name === "rotation" ? ROTATION_RADIUS : HANDLE_RADIUS,
|
|
3113
|
+
stroke: accentStroke,
|
|
3114
|
+
strokeWidth: 1.5
|
|
3115
|
+
}, name);
|
|
3116
|
+
})
|
|
3117
|
+
});
|
|
3118
|
+
};
|
|
3119
|
+
//#endregion
|
|
3120
|
+
//#region src/editor/overlay/SelectionOverlay.tsx
|
|
3121
|
+
function outline(el) {
|
|
3122
|
+
if (isLinear(el)) {
|
|
3123
|
+
const [minX, minY, maxX, maxY] = getCommonBounds([el]);
|
|
3124
|
+
return [
|
|
3125
|
+
[minX, minY],
|
|
3126
|
+
[maxX, minY],
|
|
3127
|
+
[maxX, maxY],
|
|
3128
|
+
[minX, maxY]
|
|
3129
|
+
];
|
|
3130
|
+
}
|
|
3131
|
+
return getElementAbsoluteCorners(el);
|
|
3132
|
+
}
|
|
3133
|
+
var toPoints = (points) => points.map(([x, y]) => `${x},${y}`).join(" ");
|
|
3134
|
+
var SelectionOverlay = ({ store }) => {
|
|
3135
|
+
const state = useStore(store, (s) => s);
|
|
3136
|
+
const { viewport, selectionBox } = state;
|
|
3137
|
+
const selected = getSelectedElements(state).filter((el) => !(el.type === "text" && el.containerId));
|
|
3138
|
+
const suggested = state.suggestedBindingId ? state.elements.find((el) => el.id === state.suggestedBindingId && !el.isDeleted) : void 0;
|
|
3139
|
+
const box = selectionBox ? [sceneToScreen([selectionBox[0], selectionBox[1]], viewport), sceneToScreen([selectionBox[2], selectionBox[3]], viewport)] : null;
|
|
3140
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
3141
|
+
"data-whiteboard-overlay": true,
|
|
3142
|
+
className: layer,
|
|
3143
|
+
children: [
|
|
3144
|
+
suggested && /* @__PURE__ */ jsx("polygon", {
|
|
3145
|
+
"data-binding-highlight": suggested.id,
|
|
3146
|
+
fill: "none",
|
|
3147
|
+
opacity: .6,
|
|
3148
|
+
points: toPoints(outline(suggested).map((p) => sceneToScreen(p, viewport))),
|
|
3149
|
+
stroke: "var(--rc-accent)",
|
|
3150
|
+
strokeWidth: 3
|
|
3151
|
+
}),
|
|
3152
|
+
selected.length > 1 && selected.map((el) => /* @__PURE__ */ jsx("polygon", {
|
|
3153
|
+
fill: "none",
|
|
3154
|
+
opacity: .5,
|
|
3155
|
+
points: toPoints(outline(el).map((p) => sceneToScreen(p, viewport))),
|
|
3156
|
+
stroke: "var(--rc-accent)",
|
|
3157
|
+
strokeWidth: 1
|
|
3158
|
+
}, el.id)),
|
|
3159
|
+
selected.length === 1 && /* @__PURE__ */ jsx("polygon", {
|
|
3160
|
+
"data-selection-frame": true,
|
|
3161
|
+
fill: "none",
|
|
3162
|
+
points: toPoints(outline(selected[0]).map((p) => sceneToScreen(p, viewport))),
|
|
3163
|
+
stroke: "var(--rc-accent)",
|
|
3164
|
+
strokeWidth: 1
|
|
3165
|
+
}),
|
|
3166
|
+
/* @__PURE__ */ jsx(TransformHandles, { store }),
|
|
3167
|
+
/* @__PURE__ */ jsx(LinearHandles, { store }),
|
|
3168
|
+
box && /* @__PURE__ */ jsx("rect", {
|
|
3169
|
+
"data-selection-box": true,
|
|
3170
|
+
fill: "var(--rc-accent)",
|
|
3171
|
+
fillOpacity: .08,
|
|
3172
|
+
height: box[1][1] - box[0][1],
|
|
3173
|
+
stroke: "var(--rc-accent)",
|
|
3174
|
+
strokeWidth: 1,
|
|
3175
|
+
width: box[1][0] - box[0][0],
|
|
3176
|
+
x: box[0][0],
|
|
3177
|
+
y: box[0][1]
|
|
3178
|
+
})
|
|
3179
|
+
]
|
|
3180
|
+
});
|
|
3181
|
+
};
|
|
3182
|
+
//#endregion
|
|
3183
|
+
//#region src/model/richtext-editable.ts
|
|
3184
|
+
var ELEMENT = {
|
|
3185
|
+
direction: null,
|
|
3186
|
+
format: "",
|
|
3187
|
+
indent: 0,
|
|
3188
|
+
version: 1
|
|
3189
|
+
};
|
|
3190
|
+
var text = (value, from) => ({
|
|
3191
|
+
type: "text",
|
|
3192
|
+
text: value,
|
|
3193
|
+
format: typeof from?.format === "number" ? from.format : 0,
|
|
3194
|
+
style: typeof from?.style === "string" ? from.style : "",
|
|
3195
|
+
detail: 0,
|
|
3196
|
+
mode: "normal",
|
|
3197
|
+
version: 1
|
|
3198
|
+
});
|
|
3199
|
+
function inline(node) {
|
|
3200
|
+
switch (node.type) {
|
|
3201
|
+
case "text": return [text(node.text ?? "", node)];
|
|
3202
|
+
case "linebreak":
|
|
3203
|
+
case "tab": return [{
|
|
3204
|
+
type: node.type,
|
|
3205
|
+
version: 1
|
|
3206
|
+
}];
|
|
3207
|
+
case "link":
|
|
3208
|
+
case "autolink": return [{
|
|
3209
|
+
...ELEMENT,
|
|
3210
|
+
type: "link",
|
|
3211
|
+
url: typeof node.url === "string" ? node.url : "",
|
|
3212
|
+
rel: "noopener noreferrer",
|
|
3213
|
+
target: "_blank",
|
|
3214
|
+
title: null,
|
|
3215
|
+
children: (node.children ?? []).flatMap(inline)
|
|
3216
|
+
}];
|
|
3217
|
+
default: {
|
|
3218
|
+
const value = richTextPlainText(node);
|
|
3219
|
+
return value ? [text(value)] : [];
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
var paragraph = (children) => ({
|
|
3224
|
+
...ELEMENT,
|
|
3225
|
+
type: "paragraph",
|
|
3226
|
+
textFormat: 0,
|
|
3227
|
+
textStyle: "",
|
|
3228
|
+
children
|
|
3229
|
+
});
|
|
3230
|
+
function listItem(node, index) {
|
|
3231
|
+
const children = (node.children ?? []).flatMap((child) => child.type === "list" ? [list(child)] : inline(child));
|
|
3232
|
+
return {
|
|
3233
|
+
...ELEMENT,
|
|
3234
|
+
type: "listitem",
|
|
3235
|
+
value: index + 1,
|
|
3236
|
+
checked: void 0,
|
|
3237
|
+
children
|
|
3238
|
+
};
|
|
3239
|
+
}
|
|
3240
|
+
function list(node) {
|
|
3241
|
+
const listType = node.listType === "number" || node.listType === "check" ? node.listType : "bullet";
|
|
3242
|
+
return {
|
|
3243
|
+
...ELEMENT,
|
|
3244
|
+
type: "list",
|
|
3245
|
+
listType,
|
|
3246
|
+
start: 1,
|
|
3247
|
+
tag: listType === "number" ? "ol" : "ul",
|
|
3248
|
+
children: (node.children ?? []).map((item, i) => listItem(item.type === "listitem" ? item : {
|
|
3249
|
+
type: "listitem",
|
|
3250
|
+
children: [item]
|
|
3251
|
+
}, i))
|
|
3252
|
+
};
|
|
3253
|
+
}
|
|
3254
|
+
function block(node) {
|
|
3255
|
+
switch (node.type) {
|
|
3256
|
+
case "paragraph": return paragraph((node.children ?? []).flatMap(inline));
|
|
3257
|
+
case "heading": {
|
|
3258
|
+
const tag = /^h[1-6]$/.test(node.tag ?? "") ? node.tag : "h3";
|
|
3259
|
+
return {
|
|
3260
|
+
...ELEMENT,
|
|
3261
|
+
type: "heading",
|
|
3262
|
+
tag,
|
|
3263
|
+
children: (node.children ?? []).flatMap(inline)
|
|
3264
|
+
};
|
|
3265
|
+
}
|
|
3266
|
+
case "quote": return {
|
|
3267
|
+
...ELEMENT,
|
|
3268
|
+
type: "quote",
|
|
3269
|
+
children: (node.children ?? []).flatMap(inline)
|
|
3270
|
+
};
|
|
3271
|
+
case "list": return list(node);
|
|
3272
|
+
default: return paragraph(inline(node));
|
|
3273
|
+
}
|
|
3274
|
+
}
|
|
3275
|
+
function toEditableContent(content) {
|
|
3276
|
+
const blocks = (contentRoot(content).children ?? []).map(block);
|
|
3277
|
+
return { root: {
|
|
3278
|
+
...ELEMENT,
|
|
3279
|
+
type: "root",
|
|
3280
|
+
children: blocks.length > 0 ? blocks : [paragraph([])]
|
|
3281
|
+
} };
|
|
3282
|
+
}
|
|
3283
|
+
//#endregion
|
|
3284
|
+
//#region src/editor/RichTextFormatBar.tsx
|
|
3285
|
+
var CARD_TEXT_COLORS = [
|
|
3286
|
+
"#1e1e1e",
|
|
3287
|
+
"#868e96",
|
|
3288
|
+
"#e03131",
|
|
3289
|
+
"#2f9e44",
|
|
3290
|
+
"#1971c2",
|
|
3291
|
+
"#f08c00"
|
|
3292
|
+
];
|
|
3293
|
+
var FORMATS = [
|
|
3294
|
+
{
|
|
3295
|
+
format: "bold",
|
|
3296
|
+
icon: Bold,
|
|
3297
|
+
label: "Bold"
|
|
3298
|
+
},
|
|
3299
|
+
{
|
|
3300
|
+
format: "italic",
|
|
3301
|
+
icon: Italic,
|
|
3302
|
+
label: "Italic"
|
|
3303
|
+
},
|
|
3304
|
+
{
|
|
3305
|
+
format: "strikethrough",
|
|
3306
|
+
icon: Strikethrough,
|
|
3307
|
+
label: "Strikethrough"
|
|
3308
|
+
},
|
|
3309
|
+
{
|
|
3310
|
+
format: "code",
|
|
3311
|
+
icon: Code,
|
|
3312
|
+
label: "Inline code"
|
|
3313
|
+
},
|
|
3314
|
+
{
|
|
3315
|
+
format: "highlight",
|
|
3316
|
+
icon: Highlighter,
|
|
3317
|
+
label: "Highlight"
|
|
3318
|
+
}
|
|
3319
|
+
];
|
|
3320
|
+
var DISCRETE = { discrete: true };
|
|
3321
|
+
var keepFocus = (e) => e.preventDefault();
|
|
3322
|
+
function selectionBlock() {
|
|
3323
|
+
const selection = $getSelection();
|
|
3324
|
+
if (!$isRangeSelection(selection)) return null;
|
|
3325
|
+
return selection.anchor.getNode().getTopLevelElement();
|
|
3326
|
+
}
|
|
3327
|
+
function toggleHeading(editor) {
|
|
3328
|
+
editor.update(() => {
|
|
3329
|
+
const selection = $getSelection();
|
|
3330
|
+
if (!$isRangeSelection(selection)) return;
|
|
3331
|
+
const heading = $isHeadingNode(selectionBlock());
|
|
3332
|
+
$setBlocksType(selection, () => heading ? $createParagraphNode() : $createHeadingNode("h3"));
|
|
3333
|
+
}, DISCRETE);
|
|
3334
|
+
}
|
|
3335
|
+
function toggleList(editor, ordered) {
|
|
3336
|
+
if (editor.getEditorState().read(() => $isListNode(selectionBlock()))) editor.dispatchCommand(REMOVE_LIST_COMMAND, void 0);
|
|
3337
|
+
else editor.dispatchCommand(ordered ? INSERT_ORDERED_LIST_COMMAND : INSERT_UNORDERED_LIST_COMMAND, void 0);
|
|
3338
|
+
}
|
|
3339
|
+
function selectionHasLink(editor) {
|
|
3340
|
+
return editor.getEditorState().read(() => {
|
|
3341
|
+
const selection = $getSelection();
|
|
3342
|
+
if (!$isRangeSelection(selection)) return false;
|
|
3343
|
+
return selection.getNodes().some((node) => $isLinkNode(node) || $isLinkNode(node.getParent()));
|
|
3344
|
+
});
|
|
3345
|
+
}
|
|
3346
|
+
function normalizeUrl(raw) {
|
|
3347
|
+
const value = raw.trim();
|
|
3348
|
+
if (!value) return null;
|
|
3349
|
+
if (/^(?:https?:|mailto:)/i.test(value)) return value;
|
|
3350
|
+
if (/^[a-z][\w+.-]*:/i.test(value)) return null;
|
|
3351
|
+
return `https://${value}`;
|
|
3352
|
+
}
|
|
3353
|
+
var RichTextFormatBar = ({ at, host }) => {
|
|
3354
|
+
const [editor] = useLexicalComposerContext();
|
|
3355
|
+
const [visible, setVisible] = useState(false);
|
|
3356
|
+
const [linking, setLinking] = useState(false);
|
|
3357
|
+
useEffect(() => editor.registerUpdateListener(({ editorState }) => {
|
|
3358
|
+
editorState.read(() => {
|
|
3359
|
+
const selection = $getSelection();
|
|
3360
|
+
setVisible($isRangeSelection(selection) && !selection.isCollapsed());
|
|
3361
|
+
});
|
|
3362
|
+
}), [editor]);
|
|
3363
|
+
if (!host || !visible && !linking) return null;
|
|
3364
|
+
const button = (label, Icon, run) => /* @__PURE__ */ jsx(ActionButton, {
|
|
3365
|
+
icon: true,
|
|
3366
|
+
"aria-label": label,
|
|
3367
|
+
size: "sm",
|
|
3368
|
+
title: label,
|
|
3369
|
+
variant: "ghost",
|
|
3370
|
+
onClick: run,
|
|
3371
|
+
onMouseDown: keepFocus,
|
|
3372
|
+
children: /* @__PURE__ */ jsx(Icon, { size: 15 })
|
|
3373
|
+
}, label);
|
|
3374
|
+
return createPortal(/* @__PURE__ */ jsx("div", {
|
|
3375
|
+
"data-richtext-format-bar": true,
|
|
3376
|
+
className: `${floating} ${formatBar}`,
|
|
3377
|
+
style: {
|
|
3378
|
+
left: at[0],
|
|
3379
|
+
top: at[1]
|
|
3380
|
+
},
|
|
3381
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
3382
|
+
children: linking ? /* @__PURE__ */ jsx("input", {
|
|
3383
|
+
autoFocus: true,
|
|
3384
|
+
"aria-label": "Link URL",
|
|
3385
|
+
className: formatBarInput,
|
|
3386
|
+
placeholder: "https://",
|
|
3387
|
+
onBlur: () => setLinking(false),
|
|
3388
|
+
onKeyDown: (e) => {
|
|
3389
|
+
e.stopPropagation();
|
|
3390
|
+
if (e.key === "Escape") setLinking(false);
|
|
3391
|
+
if (e.key !== "Enter") return;
|
|
3392
|
+
const url = normalizeUrl(e.currentTarget.value);
|
|
3393
|
+
setLinking(false);
|
|
3394
|
+
if (url) editor.dispatchCommand(TOGGLE_LINK_COMMAND, url);
|
|
3395
|
+
editor.focus();
|
|
3396
|
+
}
|
|
3397
|
+
}) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
3398
|
+
FORMATS.map(({ format, icon, label }) => button(label, icon, () => editor.dispatchCommand(FORMAT_TEXT_COMMAND, format))),
|
|
3399
|
+
CARD_TEXT_COLORS.map((color) => /* @__PURE__ */ jsx("button", {
|
|
3400
|
+
"aria-label": `Text colour ${color}`,
|
|
3401
|
+
className: formatSwatch,
|
|
3402
|
+
"data-richtext-color": color,
|
|
3403
|
+
style: { background: color },
|
|
3404
|
+
type: "button",
|
|
3405
|
+
onMouseDown: keepFocus,
|
|
3406
|
+
onClick: () => editor.update(() => {
|
|
3407
|
+
const selection = $getSelection();
|
|
3408
|
+
if ($isRangeSelection(selection)) $patchStyleText(selection, { color });
|
|
3409
|
+
}, DISCRETE)
|
|
3410
|
+
}, color)),
|
|
3411
|
+
button("Heading", Heading, () => toggleHeading(editor)),
|
|
3412
|
+
button("Bulleted list", List, () => toggleList(editor, false)),
|
|
3413
|
+
button("Numbered list", ListOrdered, () => toggleList(editor, true)),
|
|
3414
|
+
button("Link", Link, () => {
|
|
3415
|
+
if (selectionHasLink(editor)) editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
|
|
3416
|
+
else setLinking(true);
|
|
3417
|
+
})
|
|
3418
|
+
] })
|
|
3419
|
+
}), host);
|
|
3420
|
+
};
|
|
3421
|
+
//#endregion
|
|
3422
|
+
//#region src/editor/RichTextEditor.tsx
|
|
3423
|
+
var THEME = {
|
|
3424
|
+
paragraph: paragraph$1,
|
|
3425
|
+
heading: {
|
|
3426
|
+
h1,
|
|
3427
|
+
h2,
|
|
3428
|
+
h3,
|
|
3429
|
+
h4: hOther,
|
|
3430
|
+
h5: hOther,
|
|
3431
|
+
h6: hOther
|
|
3432
|
+
},
|
|
3433
|
+
quote,
|
|
3434
|
+
list: {
|
|
3435
|
+
ul: list$1,
|
|
3436
|
+
ol: list$1,
|
|
3437
|
+
listitem: listItem$1,
|
|
3438
|
+
nested: { listitem: nestedListItem }
|
|
3439
|
+
},
|
|
3440
|
+
link,
|
|
3441
|
+
text: {
|
|
3442
|
+
underline,
|
|
3443
|
+
strikethrough,
|
|
3444
|
+
underlineStrikethrough
|
|
3445
|
+
}
|
|
3446
|
+
};
|
|
3447
|
+
var BAR_WIDTH = 440;
|
|
3448
|
+
var BAR_MARGIN = 8;
|
|
3449
|
+
var clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
3450
|
+
function formatBarPosition(left, top, canvas) {
|
|
3451
|
+
const maxLeft = Math.max(BAR_MARGIN, canvas.width - BAR_WIDTH - BAR_MARGIN);
|
|
3452
|
+
const minTop = 44;
|
|
3453
|
+
const maxTop = Math.max(minTop, canvas.height - BAR_MARGIN);
|
|
3454
|
+
return [clamp(left, BAR_MARGIN, maxLeft), clamp(top - BAR_MARGIN, minTop, maxTop)];
|
|
3455
|
+
}
|
|
3456
|
+
var NODES = [
|
|
3457
|
+
HeadingNode,
|
|
3458
|
+
QuoteNode,
|
|
3459
|
+
ListNode,
|
|
3460
|
+
ListItemNode,
|
|
3461
|
+
LinkNode
|
|
3462
|
+
];
|
|
3463
|
+
var EditorBridge = ({ onReady }) => {
|
|
3464
|
+
const [editor] = useLexicalComposerContext();
|
|
3465
|
+
useEffect(() => {
|
|
3466
|
+
onReady(editor);
|
|
3467
|
+
editor.focus();
|
|
3468
|
+
}, [editor, onReady]);
|
|
3469
|
+
return null;
|
|
3470
|
+
};
|
|
3471
|
+
var Surface = ({ dark, el, store }) => {
|
|
3472
|
+
const viewport = useStore(store, (s) => s.viewport);
|
|
3473
|
+
const canvas = useStore(store, (s) => s.canvasSize);
|
|
3474
|
+
const editorRef = useRef(null);
|
|
3475
|
+
const [host, setHost] = useState(null);
|
|
3476
|
+
const initialRef = useRef(null);
|
|
3477
|
+
const id = el.id;
|
|
3478
|
+
const initialConfig = useMemo(() => ({
|
|
3479
|
+
namespace: "whiteboard-card",
|
|
3480
|
+
nodes: NODES,
|
|
3481
|
+
theme: THEME,
|
|
3482
|
+
editorState: JSON.stringify(toEditableContent(el.content)),
|
|
3483
|
+
onError: (error) => {
|
|
3484
|
+
throw error;
|
|
3485
|
+
}
|
|
3486
|
+
}), [id]);
|
|
3487
|
+
useEffect(() => () => {
|
|
3488
|
+
const editor = editorRef.current;
|
|
3489
|
+
if (!editor) return;
|
|
3490
|
+
const json = JSON.stringify(editor.getEditorState().toJSON());
|
|
3491
|
+
if (json === initialRef.current) return;
|
|
3492
|
+
commitRichTextContent(store, id, JSON.parse(json));
|
|
3493
|
+
}, [id, store]);
|
|
3494
|
+
const onReady = useCallback((editor) => {
|
|
3495
|
+
editorRef.current = editor;
|
|
3496
|
+
initialRef.current ?? (initialRef.current = JSON.stringify(editor.getEditorState().toJSON()));
|
|
3497
|
+
}, []);
|
|
3498
|
+
const [left, top] = sceneToScreen([el.x, el.y], viewport);
|
|
3499
|
+
const angle = angleOf$1(el);
|
|
3500
|
+
const rotate = angle ? ` translate(${el.width / 2}px, ${el.height / 2}px) rotate(${angle}rad) translate(${-el.width / 2}px, ${-el.height / 2}px)` : "";
|
|
3501
|
+
return /* @__PURE__ */ jsx("div", {
|
|
3502
|
+
className: surface,
|
|
3503
|
+
"data-whiteboard-richtext-editor": id,
|
|
3504
|
+
ref: (node) => setHost(node?.parentElement ?? null),
|
|
3505
|
+
style: {
|
|
3506
|
+
...RICHTEXT_BASE_STYLE,
|
|
3507
|
+
left,
|
|
3508
|
+
top,
|
|
3509
|
+
width: el.width,
|
|
3510
|
+
minHeight: el.height,
|
|
3511
|
+
padding: 12,
|
|
3512
|
+
opacity: el.opacity / 100,
|
|
3513
|
+
transform: `scale(${viewport.zoom})${rotate}`,
|
|
3514
|
+
filter: dark ? DARK_SCENE_FILTER : void 0
|
|
3515
|
+
},
|
|
3516
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
3517
|
+
onBlur: (e) => {
|
|
3518
|
+
const next = e.relatedTarget;
|
|
3519
|
+
if (e.currentTarget.contains(next) || next?.closest("[data-richtext-format-bar]")) return;
|
|
3520
|
+
stopRichTextEditing(store);
|
|
3521
|
+
},
|
|
3522
|
+
onKeyDown: (e) => {
|
|
3523
|
+
e.stopPropagation();
|
|
3524
|
+
if (e.key === "Escape") {
|
|
3525
|
+
e.preventDefault();
|
|
3526
|
+
stopRichTextEditing(store);
|
|
3527
|
+
}
|
|
3528
|
+
},
|
|
3529
|
+
children: /* @__PURE__ */ jsxs(LexicalComposer, {
|
|
3530
|
+
initialConfig,
|
|
3531
|
+
children: [
|
|
3532
|
+
/* @__PURE__ */ jsx(RichTextPlugin, {
|
|
3533
|
+
ErrorBoundary: LexicalErrorBoundary,
|
|
3534
|
+
contentEditable: /* @__PURE__ */ jsx(ContentEditable, { className: editable })
|
|
3535
|
+
}),
|
|
3536
|
+
/* @__PURE__ */ jsx(HistoryPlugin, {}),
|
|
3537
|
+
/* @__PURE__ */ jsx(ListPlugin, {}),
|
|
3538
|
+
/* @__PURE__ */ jsx(LinkPlugin, {}),
|
|
3539
|
+
/* @__PURE__ */ jsx(EditorBridge, { onReady }),
|
|
3540
|
+
/* @__PURE__ */ jsx(RichTextFormatBar, {
|
|
3541
|
+
at: formatBarPosition(left, top, canvas),
|
|
3542
|
+
host
|
|
3543
|
+
})
|
|
3544
|
+
]
|
|
3545
|
+
})
|
|
3546
|
+
});
|
|
3547
|
+
};
|
|
3548
|
+
var RichTextEditor = ({ dark = false, store }) => {
|
|
3549
|
+
const el = useStore(store, (s) => s.editingRichTextId ? s.elements.find((e) => e.id === s.editingRichTextId && e.type === "richtext") : void 0);
|
|
3550
|
+
if (!el) return null;
|
|
3551
|
+
return /* @__PURE__ */ jsx(Surface, {
|
|
3552
|
+
dark,
|
|
3553
|
+
el,
|
|
3554
|
+
store
|
|
3555
|
+
}, el.id);
|
|
3556
|
+
};
|
|
3557
|
+
//#endregion
|
|
3558
|
+
//#region src/editor/shortcuts.ts
|
|
3559
|
+
var ARROWS = {
|
|
3560
|
+
ArrowLeft: [-1, 0],
|
|
3561
|
+
ArrowRight: [1, 0],
|
|
3562
|
+
ArrowUp: [0, -1],
|
|
3563
|
+
ArrowDown: [0, 1]
|
|
3564
|
+
};
|
|
3565
|
+
var TOOL_KEYS = {
|
|
3566
|
+
v: "selection",
|
|
3567
|
+
h: "hand",
|
|
3568
|
+
r: "rectangle",
|
|
3569
|
+
d: "diamond",
|
|
3570
|
+
o: "ellipse",
|
|
3571
|
+
a: "arrow",
|
|
3572
|
+
l: "line",
|
|
3573
|
+
p: "freedraw",
|
|
3574
|
+
t: "text",
|
|
3575
|
+
i: "image",
|
|
3576
|
+
f: "frame",
|
|
3577
|
+
e: "eraser",
|
|
3578
|
+
n: "card"
|
|
3579
|
+
};
|
|
3580
|
+
var ZOOM_STEP = 1.2;
|
|
3581
|
+
function zoomBy(store, factor) {
|
|
3582
|
+
const { viewport, canvasSize } = store.getState();
|
|
3583
|
+
const center = [canvasSize.width / 2, canvasSize.height / 2];
|
|
3584
|
+
store.setState({ viewport: zoomAt(viewport, center, viewport.zoom * factor) });
|
|
3585
|
+
}
|
|
3586
|
+
function resetZoom(store) {
|
|
3587
|
+
const { viewport, canvasSize } = store.getState();
|
|
3588
|
+
store.setState({ viewport: zoomAt(viewport, [canvasSize.width / 2, canvasSize.height / 2], 1) });
|
|
3589
|
+
}
|
|
3590
|
+
function zoomToFit(store) {
|
|
3591
|
+
const { elements, canvasSize } = store.getState();
|
|
3592
|
+
const alive = elements.filter((el) => !el.isDeleted);
|
|
3593
|
+
if (alive.length === 0 || canvasSize.width === 0) return;
|
|
3594
|
+
store.setState({ viewport: fitToContent(getCommonBounds(alive), canvasSize) });
|
|
3595
|
+
}
|
|
3596
|
+
function handleModified(store, e) {
|
|
3597
|
+
const k = e.key.toLowerCase();
|
|
3598
|
+
if (k === "z") {
|
|
3599
|
+
if (e.shiftKey) store.redo();
|
|
3600
|
+
else store.undo();
|
|
3601
|
+
return true;
|
|
3602
|
+
}
|
|
3603
|
+
if (k === "y") {
|
|
3604
|
+
store.redo();
|
|
3605
|
+
return true;
|
|
3606
|
+
}
|
|
3607
|
+
if (k === "g") {
|
|
3608
|
+
if (e.shiftKey) ungroupSelection(store);
|
|
3609
|
+
else groupSelection(store);
|
|
3610
|
+
return true;
|
|
3611
|
+
}
|
|
3612
|
+
if (k === "d") {
|
|
3613
|
+
duplicateSelection(store);
|
|
3614
|
+
return true;
|
|
3615
|
+
}
|
|
3616
|
+
if (k === "a") {
|
|
3617
|
+
selectAll(store);
|
|
3618
|
+
return true;
|
|
3619
|
+
}
|
|
3620
|
+
if (e.key === "]" || e.key === "}") {
|
|
3621
|
+
if (e.shiftKey) bringToFront(store);
|
|
3622
|
+
else bringForward(store);
|
|
3623
|
+
return true;
|
|
3624
|
+
}
|
|
3625
|
+
if (e.key === "[" || e.key === "{") {
|
|
3626
|
+
if (e.shiftKey) sendToBack(store);
|
|
3627
|
+
else sendBackward(store);
|
|
3628
|
+
return true;
|
|
3629
|
+
}
|
|
3630
|
+
if (e.key === "=" || e.key === "+") {
|
|
3631
|
+
zoomBy(store, ZOOM_STEP);
|
|
3632
|
+
return true;
|
|
3633
|
+
}
|
|
3634
|
+
if (e.key === "-") {
|
|
3635
|
+
zoomBy(store, 1 / ZOOM_STEP);
|
|
3636
|
+
return true;
|
|
3637
|
+
}
|
|
3638
|
+
if (e.key === "0") {
|
|
3639
|
+
resetZoom(store);
|
|
3640
|
+
return true;
|
|
3641
|
+
}
|
|
3642
|
+
if (e.key === "'") {
|
|
3643
|
+
store.setState({ gridSize: store.getState().gridSize ? null : 20 });
|
|
3644
|
+
return true;
|
|
3645
|
+
}
|
|
3646
|
+
return false;
|
|
3647
|
+
}
|
|
3648
|
+
function handleShortcut(store, e) {
|
|
3649
|
+
const state = store.getState();
|
|
3650
|
+
if (state.editingTextId || state.editingRichTextId) return false;
|
|
3651
|
+
if (e.ctrlKey || e.metaKey) return handleModified(store, e);
|
|
3652
|
+
const arrow = ARROWS[e.key];
|
|
3653
|
+
if (arrow && state.selectedIds.size > 0) {
|
|
3654
|
+
const step = e.shiftKey ? 10 : 1;
|
|
3655
|
+
const ids = withBoundText(state.elements, withFrameChildren(state.elements, state.selectedIds));
|
|
3656
|
+
store.commit(({ elements }) => ({ elements: refreshFrames(refreshBindings(nudge(elements, ids, arrow[0] * step, arrow[1] * step), ids), ids) }));
|
|
3657
|
+
return true;
|
|
3658
|
+
}
|
|
3659
|
+
if (e.key === "Delete" || e.key === "Backspace") {
|
|
3660
|
+
const linear = getEditedLinear(state);
|
|
3661
|
+
if (linear && state.selectedPointIndex !== null && linear.points.length > 2) {
|
|
3662
|
+
const index = state.selectedPointIndex;
|
|
3663
|
+
store.commit(({ elements }) => ({ elements: elements.map((el) => el.id === linear.id ? removePoint(linear, index) : el) }));
|
|
3664
|
+
store.setState({ selectedPointIndex: null });
|
|
3665
|
+
return true;
|
|
3666
|
+
}
|
|
3667
|
+
deleteSelection(store);
|
|
3668
|
+
return true;
|
|
3669
|
+
}
|
|
3670
|
+
if (e.key === "Escape") {
|
|
3671
|
+
store.setState({
|
|
3672
|
+
selectedIds: /* @__PURE__ */ new Set(),
|
|
3673
|
+
editingGroupId: null,
|
|
3674
|
+
editingLinearId: null,
|
|
3675
|
+
selectedPointIndex: null,
|
|
3676
|
+
tool: state.toolLocked ? state.tool : "selection"
|
|
3677
|
+
});
|
|
3678
|
+
return true;
|
|
3679
|
+
}
|
|
3680
|
+
if (e.key === "Enter" && state.selectedIds.size === 1) {
|
|
3681
|
+
const [id] = state.selectedIds;
|
|
3682
|
+
if (state.elements.find((item) => item.id === id)?.type === "richtext") {
|
|
3683
|
+
startRichTextEditing(store, id);
|
|
3684
|
+
return true;
|
|
3685
|
+
}
|
|
3686
|
+
}
|
|
3687
|
+
if (e.key === "!" || e.shiftKey && e.key === "1") {
|
|
3688
|
+
zoomToFit(store);
|
|
3689
|
+
return true;
|
|
3690
|
+
}
|
|
3691
|
+
if (e.key.toLowerCase() === "q") {
|
|
3692
|
+
store.setState({ toolLocked: !state.toolLocked });
|
|
3693
|
+
return true;
|
|
3694
|
+
}
|
|
3695
|
+
const tool = !e.altKey && !e.shiftKey ? TOOL_KEYS[e.key.toLowerCase()] : void 0;
|
|
3696
|
+
if (tool) {
|
|
3697
|
+
store.setState({
|
|
3698
|
+
tool,
|
|
3699
|
+
selectedIds: tool === "selection" ? state.selectedIds : /* @__PURE__ */ new Set()
|
|
3700
|
+
});
|
|
3701
|
+
return true;
|
|
3702
|
+
}
|
|
3703
|
+
return false;
|
|
3704
|
+
}
|
|
3705
|
+
//#endregion
|
|
3706
|
+
//#region src/editor/style-options.ts
|
|
3707
|
+
var STROKE_COLORS = [
|
|
3708
|
+
"#1e1e1e",
|
|
3709
|
+
"#e03131",
|
|
3710
|
+
"#2f9e44",
|
|
3711
|
+
"#1971c2",
|
|
3712
|
+
"#f08c00"
|
|
3713
|
+
];
|
|
3714
|
+
var BACKGROUND_COLORS = [
|
|
3715
|
+
"transparent",
|
|
3716
|
+
"#ffc9c9",
|
|
3717
|
+
"#b2f2bb",
|
|
3718
|
+
"#a5d8ff",
|
|
3719
|
+
"#ffec99"
|
|
3720
|
+
];
|
|
3721
|
+
var FILL_STYLES = [
|
|
3722
|
+
{
|
|
3723
|
+
value: "hachure",
|
|
3724
|
+
label: "Hachure",
|
|
3725
|
+
icon: Slash
|
|
3726
|
+
},
|
|
3727
|
+
{
|
|
3728
|
+
value: "cross-hatch",
|
|
3729
|
+
label: "Cross-hatch",
|
|
3730
|
+
icon: Hash
|
|
3731
|
+
},
|
|
3732
|
+
{
|
|
3733
|
+
value: "solid",
|
|
3734
|
+
label: "Solid",
|
|
3735
|
+
icon: Square
|
|
3736
|
+
}
|
|
3737
|
+
];
|
|
3738
|
+
var STROKE_WIDTHS = [
|
|
3739
|
+
{
|
|
3740
|
+
value: 1,
|
|
3741
|
+
label: "Thin",
|
|
3742
|
+
icon: Minus
|
|
3743
|
+
},
|
|
3744
|
+
{
|
|
3745
|
+
value: 2,
|
|
3746
|
+
label: "Bold",
|
|
3747
|
+
icon: Equal
|
|
3748
|
+
},
|
|
3749
|
+
{
|
|
3750
|
+
value: 4,
|
|
3751
|
+
label: "Extra bold",
|
|
3752
|
+
icon: Menu
|
|
3753
|
+
}
|
|
3754
|
+
];
|
|
3755
|
+
var STROKE_STYLES = [
|
|
3756
|
+
{
|
|
3757
|
+
value: "solid",
|
|
3758
|
+
label: "Solid",
|
|
3759
|
+
icon: Minus
|
|
3760
|
+
},
|
|
3761
|
+
{
|
|
3762
|
+
value: "dashed",
|
|
3763
|
+
label: "Dashed",
|
|
3764
|
+
icon: GripHorizontal
|
|
3765
|
+
},
|
|
3766
|
+
{
|
|
3767
|
+
value: "dotted",
|
|
3768
|
+
label: "Dotted",
|
|
3769
|
+
icon: Ellipsis
|
|
3770
|
+
}
|
|
3771
|
+
];
|
|
3772
|
+
var ROUGHNESS = [
|
|
3773
|
+
{
|
|
3774
|
+
value: 0,
|
|
3775
|
+
label: "Architect",
|
|
3776
|
+
icon: Ruler
|
|
3777
|
+
},
|
|
3778
|
+
{
|
|
3779
|
+
value: 1,
|
|
3780
|
+
label: "Artist",
|
|
3781
|
+
icon: Spline
|
|
3782
|
+
},
|
|
3783
|
+
{
|
|
3784
|
+
value: 2,
|
|
3785
|
+
label: "Cartoonist",
|
|
3786
|
+
icon: Activity
|
|
3787
|
+
}
|
|
3788
|
+
];
|
|
3789
|
+
var ROUNDNESS = [{
|
|
3790
|
+
value: "sharp",
|
|
3791
|
+
label: "Sharp",
|
|
3792
|
+
icon: Square
|
|
3793
|
+
}, {
|
|
3794
|
+
value: "round",
|
|
3795
|
+
label: "Round",
|
|
3796
|
+
icon: Squircle
|
|
3797
|
+
}];
|
|
3798
|
+
var SHADOWS = [{
|
|
3799
|
+
value: false,
|
|
3800
|
+
label: "None"
|
|
3801
|
+
}, {
|
|
3802
|
+
value: true,
|
|
3803
|
+
label: "Soft"
|
|
3804
|
+
}];
|
|
3805
|
+
var FONT_SIZES = [
|
|
3806
|
+
{
|
|
3807
|
+
value: 16,
|
|
3808
|
+
label: "S"
|
|
3809
|
+
},
|
|
3810
|
+
{
|
|
3811
|
+
value: 20,
|
|
3812
|
+
label: "M"
|
|
3813
|
+
},
|
|
3814
|
+
{
|
|
3815
|
+
value: 28,
|
|
3816
|
+
label: "L"
|
|
3817
|
+
},
|
|
3818
|
+
{
|
|
3819
|
+
value: 36,
|
|
3820
|
+
label: "XL"
|
|
3821
|
+
}
|
|
3822
|
+
];
|
|
3823
|
+
var FONT_FAMILIES = [
|
|
3824
|
+
{
|
|
3825
|
+
value: 5,
|
|
3826
|
+
label: "Hand"
|
|
3827
|
+
},
|
|
3828
|
+
{
|
|
3829
|
+
value: 2,
|
|
3830
|
+
label: "Sans"
|
|
3831
|
+
},
|
|
3832
|
+
{
|
|
3833
|
+
value: 3,
|
|
3834
|
+
label: "Mono"
|
|
3835
|
+
}
|
|
3836
|
+
];
|
|
3837
|
+
var TEXT_ALIGNS = [
|
|
3838
|
+
{
|
|
3839
|
+
value: "left",
|
|
3840
|
+
label: "Left",
|
|
3841
|
+
icon: AlignLeft
|
|
3842
|
+
},
|
|
3843
|
+
{
|
|
3844
|
+
value: "center",
|
|
3845
|
+
label: "Center",
|
|
3846
|
+
icon: AlignCenter
|
|
3847
|
+
},
|
|
3848
|
+
{
|
|
3849
|
+
value: "right",
|
|
3850
|
+
label: "Right",
|
|
3851
|
+
icon: AlignRight
|
|
3852
|
+
}
|
|
3853
|
+
];
|
|
3854
|
+
var ARROWHEADS = [
|
|
3855
|
+
{
|
|
3856
|
+
value: null,
|
|
3857
|
+
label: "None",
|
|
3858
|
+
icon: Minus
|
|
3859
|
+
},
|
|
3860
|
+
{
|
|
3861
|
+
value: "arrow",
|
|
3862
|
+
label: "Arrow",
|
|
3863
|
+
icon: MoveRight
|
|
3864
|
+
},
|
|
3865
|
+
{
|
|
3866
|
+
value: "triangle",
|
|
3867
|
+
label: "Triangle",
|
|
3868
|
+
icon: Slash
|
|
3869
|
+
},
|
|
3870
|
+
{
|
|
3871
|
+
value: "dot",
|
|
3872
|
+
label: "Dot",
|
|
3873
|
+
icon: Dot
|
|
3874
|
+
},
|
|
3875
|
+
{
|
|
3876
|
+
value: "bar",
|
|
3877
|
+
label: "Bar",
|
|
3878
|
+
icon: Menu
|
|
3879
|
+
}
|
|
3880
|
+
];
|
|
3881
|
+
var ARROW_TYPES = [
|
|
3882
|
+
{
|
|
3883
|
+
value: "sharp",
|
|
3884
|
+
label: "Sharp",
|
|
3885
|
+
icon: Slash
|
|
3886
|
+
},
|
|
3887
|
+
{
|
|
3888
|
+
value: "round",
|
|
3889
|
+
label: "Curved",
|
|
3890
|
+
icon: Spline
|
|
3891
|
+
},
|
|
3892
|
+
{
|
|
3893
|
+
value: "elbow",
|
|
3894
|
+
label: "Elbow",
|
|
3895
|
+
icon: Waypoints
|
|
3896
|
+
}
|
|
3897
|
+
];
|
|
3898
|
+
//#endregion
|
|
3899
|
+
//#region src/editor/style-props.ts
|
|
3900
|
+
var SHAPE = [
|
|
3901
|
+
"stroke",
|
|
3902
|
+
"background",
|
|
3903
|
+
"fillStyle",
|
|
3904
|
+
"strokeWidth",
|
|
3905
|
+
"strokeStyle",
|
|
3906
|
+
"roughness"
|
|
3907
|
+
];
|
|
3908
|
+
var TEXT = [
|
|
3909
|
+
"stroke",
|
|
3910
|
+
"fontSize",
|
|
3911
|
+
"fontFamily",
|
|
3912
|
+
"textAlign"
|
|
3913
|
+
];
|
|
3914
|
+
var ALWAYS = [
|
|
3915
|
+
"opacity",
|
|
3916
|
+
"layers",
|
|
3917
|
+
"actions"
|
|
3918
|
+
];
|
|
3919
|
+
function propsFor(el) {
|
|
3920
|
+
const hasText = el.boundElements?.some((b) => b.type === "text") ? TEXT : [];
|
|
3921
|
+
switch (el.type) {
|
|
3922
|
+
case "rectangle":
|
|
3923
|
+
case "diamond": return [
|
|
3924
|
+
...SHAPE,
|
|
3925
|
+
"roundness",
|
|
3926
|
+
"shadow",
|
|
3927
|
+
...hasText
|
|
3928
|
+
];
|
|
3929
|
+
case "ellipse": return [
|
|
3930
|
+
...SHAPE,
|
|
3931
|
+
"shadow",
|
|
3932
|
+
...hasText
|
|
3933
|
+
];
|
|
3934
|
+
case "arrow": return [
|
|
3935
|
+
"stroke",
|
|
3936
|
+
"strokeWidth",
|
|
3937
|
+
"strokeStyle",
|
|
3938
|
+
"roughness",
|
|
3939
|
+
"arrowheads",
|
|
3940
|
+
"arrowType",
|
|
3941
|
+
...hasText
|
|
3942
|
+
];
|
|
3943
|
+
case "line": return [
|
|
3944
|
+
"stroke",
|
|
3945
|
+
"background",
|
|
3946
|
+
"fillStyle",
|
|
3947
|
+
"strokeWidth",
|
|
3948
|
+
"strokeStyle",
|
|
3949
|
+
"roughness",
|
|
3950
|
+
"roundness"
|
|
3951
|
+
];
|
|
3952
|
+
case "freedraw": return ["stroke", "strokeWidth"];
|
|
3953
|
+
case "text": return TEXT;
|
|
3954
|
+
case "richtext": return [
|
|
3955
|
+
"stroke",
|
|
3956
|
+
"background",
|
|
3957
|
+
"strokeWidth",
|
|
3958
|
+
"roundness",
|
|
3959
|
+
"shadow"
|
|
3960
|
+
];
|
|
3961
|
+
case "image":
|
|
3962
|
+
case "svg": return ["shadow"];
|
|
3963
|
+
default: return [];
|
|
3964
|
+
}
|
|
3965
|
+
}
|
|
3966
|
+
function getApplicableProps(elements) {
|
|
3967
|
+
if (elements.length === 0) return /* @__PURE__ */ new Set();
|
|
3968
|
+
let common = new Set(propsFor(elements[0]));
|
|
3969
|
+
for (const el of elements.slice(1)) {
|
|
3970
|
+
const next = new Set(propsFor(el));
|
|
3971
|
+
common = new Set([...common].filter((p) => next.has(p)));
|
|
3972
|
+
}
|
|
3973
|
+
for (const p of ALWAYS) common.add(p);
|
|
3974
|
+
return common;
|
|
3975
|
+
}
|
|
3976
|
+
function readValue(el, key) {
|
|
3977
|
+
if (key === "roundness") return el.roundness ? "round" : "sharp";
|
|
3978
|
+
if (key === "shadow") return el.shadow === true;
|
|
3979
|
+
if (key === "arrowType") {
|
|
3980
|
+
const arrow = el;
|
|
3981
|
+
return arrow.elbowed ? "elbow" : arrow.roundness ? "round" : "sharp";
|
|
3982
|
+
}
|
|
3983
|
+
return el[key];
|
|
3984
|
+
}
|
|
3985
|
+
function getCommonValue(elements, key) {
|
|
3986
|
+
if (elements.length === 0) return void 0;
|
|
3987
|
+
const first = readValue(elements[0], key);
|
|
3988
|
+
return elements.every((el) => readValue(el, key) === first) ? first : "mixed";
|
|
3989
|
+
}
|
|
3990
|
+
var isText = (el) => el.type === "text";
|
|
3991
|
+
var isArrow = (el) => el.type === "arrow";
|
|
3992
|
+
function patchElement(el, patch) {
|
|
3993
|
+
const out = {};
|
|
3994
|
+
const props = /* @__PURE__ */ new Set([...propsFor(el), ...isText(el) ? TEXT : []]);
|
|
3995
|
+
if (patch.strokeColor !== void 0 && props.has("stroke")) out.strokeColor = patch.strokeColor;
|
|
3996
|
+
if (patch.backgroundColor !== void 0 && props.has("background")) out.backgroundColor = patch.backgroundColor;
|
|
3997
|
+
if (patch.fillStyle !== void 0 && props.has("fillStyle")) out.fillStyle = patch.fillStyle;
|
|
3998
|
+
if (patch.strokeWidth !== void 0 && props.has("strokeWidth")) out.strokeWidth = patch.strokeWidth;
|
|
3999
|
+
if (patch.strokeStyle !== void 0 && props.has("strokeStyle")) out.strokeStyle = patch.strokeStyle;
|
|
4000
|
+
if (patch.roughness !== void 0 && props.has("roughness")) out.roughness = patch.roughness;
|
|
4001
|
+
if (patch.opacity !== void 0) out.opacity = patch.opacity;
|
|
4002
|
+
if (patch.shadow !== void 0 && props.has("shadow")) out.shadow = patch.shadow;
|
|
4003
|
+
if (patch.roundness !== void 0 && props.has("roundness")) {
|
|
4004
|
+
const round = el.type === "line" ? { type: 2 } : { type: 3 };
|
|
4005
|
+
out.roundness = patch.roundness === "round" ? round : null;
|
|
4006
|
+
}
|
|
4007
|
+
if (isText(el)) {
|
|
4008
|
+
if (patch.fontSize !== void 0) out.fontSize = patch.fontSize;
|
|
4009
|
+
if (patch.fontFamily !== void 0) out.fontFamily = patch.fontFamily;
|
|
4010
|
+
if (patch.textAlign !== void 0 && !el.containerId) out.textAlign = patch.textAlign;
|
|
4011
|
+
}
|
|
4012
|
+
if (isArrow(el)) {
|
|
4013
|
+
if (patch.startArrowhead !== void 0) out.startArrowhead = patch.startArrowhead;
|
|
4014
|
+
if (patch.endArrowhead !== void 0) out.endArrowhead = patch.endArrowhead;
|
|
4015
|
+
if (patch.arrowType !== void 0) {
|
|
4016
|
+
out.elbowed = patch.arrowType === "elbow";
|
|
4017
|
+
out.roundness = patch.arrowType === "round" ? { type: 2 } : null;
|
|
4018
|
+
if (!out.elbowed && el.fixedSegments) out.fixedSegments = null;
|
|
4019
|
+
}
|
|
4020
|
+
}
|
|
4021
|
+
if (Object.keys(out).length === 0) return el;
|
|
4022
|
+
const next = bumpVersion(el, out);
|
|
4023
|
+
return isText(next) && !next.containerId ? refreshTextDimensions(next) : next;
|
|
4024
|
+
}
|
|
4025
|
+
function applyStyle(store, patch) {
|
|
4026
|
+
const state = store.getState();
|
|
4027
|
+
store.setState({ currentStyle: {
|
|
4028
|
+
...state.currentStyle,
|
|
4029
|
+
...patch
|
|
4030
|
+
} });
|
|
4031
|
+
if (state.selectedIds.size === 0) return;
|
|
4032
|
+
const ids = withBoundText(state.elements, state.selectedIds);
|
|
4033
|
+
store.commit(({ elements }) => {
|
|
4034
|
+
const patched = elements.map((el) => ids.has(el.id) ? patchElement(el, patch) : el);
|
|
4035
|
+
return { elements: relayoutBoundTexts(patched.map((el, i) => el.type === "arrow" && el.elbowed && !elements[i].elbowed ? updateElbowArrow(el, patched) : el), state.selectedIds) };
|
|
4036
|
+
});
|
|
4037
|
+
}
|
|
4038
|
+
//#endregion
|
|
4039
|
+
//#region src/editor/style-rows.tsx
|
|
4040
|
+
function previewElements(state) {
|
|
4041
|
+
const selected = getSelectedElements(state).filter((el) => !(el.type === "text" && el.containerId));
|
|
4042
|
+
if (selected.length > 0) return selected;
|
|
4043
|
+
switch (state.tool) {
|
|
4044
|
+
case "rectangle":
|
|
4045
|
+
case "diamond":
|
|
4046
|
+
case "ellipse": return [newElement(state.tool, {}, state.currentStyle)];
|
|
4047
|
+
case "arrow":
|
|
4048
|
+
case "line":
|
|
4049
|
+
case "freedraw": return state.tool === "freedraw" ? [{
|
|
4050
|
+
...newElement("rectangle", {}),
|
|
4051
|
+
type: "freedraw"
|
|
4052
|
+
}] : [newLinearElement(state.tool, {}, state.currentStyle)];
|
|
4053
|
+
case "text": return [newTextElement({ text: "" }, state.currentStyle)];
|
|
4054
|
+
default: return [];
|
|
4055
|
+
}
|
|
4056
|
+
}
|
|
4057
|
+
var Row = ({ children, label, name }) => /* @__PURE__ */ jsxs("div", {
|
|
4058
|
+
className: row,
|
|
4059
|
+
"data-style-row": name ?? "",
|
|
4060
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
4061
|
+
className: rowLabel,
|
|
4062
|
+
children: label
|
|
4063
|
+
}), children]
|
|
4064
|
+
});
|
|
4065
|
+
function Segment({ choices, current, onPick }) {
|
|
4066
|
+
const items = choices.map((c) => ({
|
|
4067
|
+
value: String(c.value),
|
|
4068
|
+
label: c.icon ? /* @__PURE__ */ jsx(c.icon, {
|
|
4069
|
+
"aria-label": c.label,
|
|
4070
|
+
size: 15
|
|
4071
|
+
}) : c.label
|
|
4072
|
+
}));
|
|
4073
|
+
return /* @__PURE__ */ jsx(SegmentedControl, {
|
|
4074
|
+
fullWidth: true,
|
|
4075
|
+
items,
|
|
4076
|
+
size: "sm",
|
|
4077
|
+
value: String(current),
|
|
4078
|
+
onChange: (v) => {
|
|
4079
|
+
const choice = choices.find((c) => String(c.value) === v);
|
|
4080
|
+
if (choice) onPick(choice.value);
|
|
4081
|
+
}
|
|
4082
|
+
});
|
|
4083
|
+
}
|
|
4084
|
+
function useStyleRows(store) {
|
|
4085
|
+
const state = useStore(store, (s) => s);
|
|
4086
|
+
if (state.editingTextId || state.selectionBox) return null;
|
|
4087
|
+
const elements = previewElements(state);
|
|
4088
|
+
if (elements.length === 0) return null;
|
|
4089
|
+
const props = getApplicableProps(elements);
|
|
4090
|
+
const hasSelection = state.selectedIds.size > 0;
|
|
4091
|
+
const value = (key) => getCommonValue(elements, key);
|
|
4092
|
+
const set = (patch) => applyStyle(store, patch);
|
|
4093
|
+
const swatches = (key, colors) => /* @__PURE__ */ jsx("div", {
|
|
4094
|
+
className: options,
|
|
4095
|
+
children: colors.map((color) => /* @__PURE__ */ jsx("button", {
|
|
4096
|
+
"aria-label": color,
|
|
4097
|
+
"aria-pressed": value(key) === color,
|
|
4098
|
+
className: `${swatch}${color === "transparent" ? ` ${transparentSwatch}` : ""}`,
|
|
4099
|
+
"data-swatch": `${key}:${color}`,
|
|
4100
|
+
style: color === "transparent" ? void 0 : { background: color },
|
|
4101
|
+
type: "button",
|
|
4102
|
+
onClick: () => set({ [key]: color })
|
|
4103
|
+
}, color))
|
|
4104
|
+
});
|
|
4105
|
+
const action = (label, icon, run) => /* @__PURE__ */ jsx(ActionButton, {
|
|
4106
|
+
icon: true,
|
|
4107
|
+
"aria-label": label,
|
|
4108
|
+
size: "sm",
|
|
4109
|
+
title: label,
|
|
4110
|
+
variant: "ghost",
|
|
4111
|
+
onClick: () => run(store),
|
|
4112
|
+
children: icon
|
|
4113
|
+
}, label);
|
|
4114
|
+
const grouped = elements.some((el) => (el.groupIds ?? []).length > 0);
|
|
4115
|
+
const rows = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
4116
|
+
props.has("stroke") && /* @__PURE__ */ jsx(Row, {
|
|
4117
|
+
label: "Stroke",
|
|
4118
|
+
children: swatches("strokeColor", STROKE_COLORS)
|
|
4119
|
+
}),
|
|
4120
|
+
props.has("background") && /* @__PURE__ */ jsx(Row, {
|
|
4121
|
+
label: "Background",
|
|
4122
|
+
children: swatches("backgroundColor", BACKGROUND_COLORS)
|
|
4123
|
+
}),
|
|
4124
|
+
props.has("fillStyle") && /* @__PURE__ */ jsx(Row, {
|
|
4125
|
+
label: "Fill",
|
|
4126
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4127
|
+
choices: FILL_STYLES,
|
|
4128
|
+
current: value("fillStyle"),
|
|
4129
|
+
onPick: (v) => set({ fillStyle: v })
|
|
4130
|
+
})
|
|
4131
|
+
}),
|
|
4132
|
+
props.has("strokeWidth") && /* @__PURE__ */ jsx(Row, {
|
|
4133
|
+
label: "Stroke width",
|
|
4134
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4135
|
+
choices: STROKE_WIDTHS,
|
|
4136
|
+
current: value("strokeWidth"),
|
|
4137
|
+
onPick: (v) => set({ strokeWidth: v })
|
|
4138
|
+
})
|
|
4139
|
+
}),
|
|
4140
|
+
props.has("strokeStyle") && /* @__PURE__ */ jsx(Row, {
|
|
4141
|
+
label: "Stroke style",
|
|
4142
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4143
|
+
choices: STROKE_STYLES,
|
|
4144
|
+
current: value("strokeStyle"),
|
|
4145
|
+
onPick: (v) => set({ strokeStyle: v })
|
|
4146
|
+
})
|
|
4147
|
+
}),
|
|
4148
|
+
props.has("roughness") && /* @__PURE__ */ jsx(Row, {
|
|
4149
|
+
label: "Sloppiness",
|
|
4150
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4151
|
+
choices: ROUGHNESS,
|
|
4152
|
+
current: value("roughness"),
|
|
4153
|
+
onPick: (v) => set({ roughness: v })
|
|
4154
|
+
})
|
|
4155
|
+
}),
|
|
4156
|
+
props.has("roundness") && /* @__PURE__ */ jsx(Row, {
|
|
4157
|
+
label: "Edges",
|
|
4158
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4159
|
+
choices: ROUNDNESS,
|
|
4160
|
+
current: value("roundness"),
|
|
4161
|
+
onPick: (v) => set({ roundness: v })
|
|
4162
|
+
})
|
|
4163
|
+
}),
|
|
4164
|
+
props.has("shadow") && /* @__PURE__ */ jsx(Row, {
|
|
4165
|
+
label: "Shadow",
|
|
4166
|
+
name: "shadow",
|
|
4167
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4168
|
+
choices: SHADOWS,
|
|
4169
|
+
current: value("shadow"),
|
|
4170
|
+
onPick: (v) => set({ shadow: v })
|
|
4171
|
+
})
|
|
4172
|
+
}),
|
|
4173
|
+
props.has("arrowType") && /* @__PURE__ */ jsx(Row, {
|
|
4174
|
+
label: "Arrow type",
|
|
4175
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4176
|
+
choices: ARROW_TYPES,
|
|
4177
|
+
current: value("arrowType"),
|
|
4178
|
+
onPick: (v) => set({ arrowType: v })
|
|
4179
|
+
})
|
|
4180
|
+
}),
|
|
4181
|
+
props.has("arrowheads") && /* @__PURE__ */ jsxs(Row, {
|
|
4182
|
+
label: "Arrowheads",
|
|
4183
|
+
children: [/* @__PURE__ */ jsx(Segment, {
|
|
4184
|
+
choices: ARROWHEADS,
|
|
4185
|
+
current: value("startArrowhead"),
|
|
4186
|
+
onPick: (v) => set({ startArrowhead: v })
|
|
4187
|
+
}), /* @__PURE__ */ jsx(Segment, {
|
|
4188
|
+
choices: ARROWHEADS,
|
|
4189
|
+
current: value("endArrowhead"),
|
|
4190
|
+
onPick: (v) => set({ endArrowhead: v })
|
|
4191
|
+
})]
|
|
4192
|
+
}),
|
|
4193
|
+
props.has("fontSize") && /* @__PURE__ */ jsx(Row, {
|
|
4194
|
+
label: "Font size",
|
|
4195
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4196
|
+
choices: FONT_SIZES,
|
|
4197
|
+
current: value("fontSize"),
|
|
4198
|
+
onPick: (v) => set({ fontSize: v })
|
|
4199
|
+
})
|
|
4200
|
+
}),
|
|
4201
|
+
props.has("fontFamily") && /* @__PURE__ */ jsx(Row, {
|
|
4202
|
+
label: "Font",
|
|
4203
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4204
|
+
choices: FONT_FAMILIES,
|
|
4205
|
+
current: value("fontFamily"),
|
|
4206
|
+
onPick: (v) => set({ fontFamily: v })
|
|
4207
|
+
})
|
|
4208
|
+
}),
|
|
4209
|
+
props.has("textAlign") && /* @__PURE__ */ jsx(Row, {
|
|
4210
|
+
label: "Align",
|
|
4211
|
+
children: /* @__PURE__ */ jsx(Segment, {
|
|
4212
|
+
choices: TEXT_ALIGNS,
|
|
4213
|
+
current: value("textAlign"),
|
|
4214
|
+
onPick: (v) => set({ textAlign: v })
|
|
4215
|
+
})
|
|
4216
|
+
}),
|
|
4217
|
+
/* @__PURE__ */ jsx(Row, {
|
|
4218
|
+
label: "Opacity",
|
|
4219
|
+
children: /* @__PURE__ */ jsx("input", {
|
|
4220
|
+
"aria-label": "Opacity",
|
|
4221
|
+
className: range,
|
|
4222
|
+
max: 100,
|
|
4223
|
+
min: 0,
|
|
4224
|
+
step: 10,
|
|
4225
|
+
type: "range",
|
|
4226
|
+
value: typeof value("opacity") === "number" ? value("opacity") : 100,
|
|
4227
|
+
onChange: (e) => set({ opacity: Number(e.target.value) })
|
|
4228
|
+
})
|
|
4229
|
+
}),
|
|
4230
|
+
hasSelection && /* @__PURE__ */ jsx(Row, {
|
|
4231
|
+
label: "Layers",
|
|
4232
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
4233
|
+
className: "_14kbw0v9",
|
|
4234
|
+
children: [
|
|
4235
|
+
action("Send to back", /* @__PURE__ */ jsx(ArrowDownToLine, {}), sendToBack),
|
|
4236
|
+
action("Send backward", /* @__PURE__ */ jsx(ArrowDown, {}), sendBackward),
|
|
4237
|
+
action("Bring forward", /* @__PURE__ */ jsx(ArrowUp, {}), bringForward),
|
|
4238
|
+
action("Bring to front", /* @__PURE__ */ jsx(ArrowUpToLine, {}), bringToFront)
|
|
4239
|
+
]
|
|
4240
|
+
})
|
|
4241
|
+
}),
|
|
4242
|
+
hasSelection && /* @__PURE__ */ jsx(Row, {
|
|
4243
|
+
label: "Actions",
|
|
4244
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
4245
|
+
className: "_14kbw0v9",
|
|
4246
|
+
children: [
|
|
4247
|
+
action("Duplicate", /* @__PURE__ */ jsx(Copy, {}), duplicateSelection),
|
|
4248
|
+
elements.length > 1 && action("Group", /* @__PURE__ */ jsx(Group, {}), groupSelection),
|
|
4249
|
+
grouped && action("Ungroup", /* @__PURE__ */ jsx(Ungroup, {}), ungroupSelection),
|
|
4250
|
+
action("Delete", /* @__PURE__ */ jsx(Trash2, {}), deleteSelection)
|
|
4251
|
+
]
|
|
4252
|
+
})
|
|
4253
|
+
})
|
|
4254
|
+
] });
|
|
4255
|
+
return Children.toArray(rows.props.children);
|
|
4256
|
+
}
|
|
4257
|
+
//#endregion
|
|
4258
|
+
//#region src/editor/StylePanel.tsx
|
|
4259
|
+
var StylePanel = ({ store }) => {
|
|
4260
|
+
const rows = useStyleRows(store);
|
|
4261
|
+
if (!rows) return null;
|
|
4262
|
+
return /* @__PURE__ */ jsx("div", {
|
|
4263
|
+
"data-style-panel": true,
|
|
4264
|
+
"data-whiteboard-chrome": true,
|
|
4265
|
+
className: `${floating} ${panel}`,
|
|
4266
|
+
children: rows
|
|
4267
|
+
});
|
|
4268
|
+
};
|
|
4269
|
+
var StyleSheet = ({ store }) => {
|
|
4270
|
+
const rows = useStyleRows(store);
|
|
4271
|
+
const hasSelection = useStore(store, (s) => s.selectedIds.size > 0);
|
|
4272
|
+
const [expanded, setExpanded] = useState(false);
|
|
4273
|
+
if (!rows || !hasSelection) return null;
|
|
4274
|
+
const visible = expanded ? rows : rows.slice(0, 4);
|
|
4275
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4276
|
+
"data-style-sheet": true,
|
|
4277
|
+
"data-whiteboard-chrome": true,
|
|
4278
|
+
className: sheet,
|
|
4279
|
+
children: [
|
|
4280
|
+
/* @__PURE__ */ jsx("div", { className: grabber }),
|
|
4281
|
+
visible,
|
|
4282
|
+
rows.length > 4 && /* @__PURE__ */ jsx("button", {
|
|
4283
|
+
"data-style-more": true,
|
|
4284
|
+
className: "_14kbw0vj",
|
|
4285
|
+
type: "button",
|
|
4286
|
+
onClick: () => setExpanded((v) => !v),
|
|
4287
|
+
children: expanded ? "Fewer options" : "More styles and actions"
|
|
4288
|
+
})
|
|
4289
|
+
]
|
|
4290
|
+
});
|
|
4291
|
+
};
|
|
4292
|
+
//#endregion
|
|
4293
|
+
//#region src/editor/TextEditor.tsx
|
|
4294
|
+
var MIN_WIDTH = 24;
|
|
4295
|
+
var TextEditor = ({ dark = false, store }) => {
|
|
4296
|
+
const editingId = useStore(store, (s) => s.editingTextId);
|
|
4297
|
+
const el = useStore(store, (s) => s.editingTextId ? s.elements.find((e) => e.id === s.editingTextId) : void 0);
|
|
4298
|
+
const viewport = useStore(store, (s) => s.viewport);
|
|
4299
|
+
const ref = useRef(null);
|
|
4300
|
+
useEffect(() => {
|
|
4301
|
+
const node = ref.current;
|
|
4302
|
+
if (!node) return;
|
|
4303
|
+
node.focus({ preventScroll: true });
|
|
4304
|
+
node.setSelectionRange(node.value.length, node.value.length);
|
|
4305
|
+
}, [editingId]);
|
|
4306
|
+
if (!editingId || !el) return null;
|
|
4307
|
+
const [left, top] = sceneToScreen([el.x, el.y], viewport);
|
|
4308
|
+
const zoom = viewport.zoom;
|
|
4309
|
+
const lineHeight = el.lineHeight ?? getLineHeight(el.fontFamily);
|
|
4310
|
+
return /* @__PURE__ */ jsx("textarea", {
|
|
4311
|
+
"data-whiteboard-text-editor": true,
|
|
4312
|
+
className: textEditor,
|
|
4313
|
+
ref,
|
|
4314
|
+
spellCheck: false,
|
|
4315
|
+
value: el.originalText ?? el.text,
|
|
4316
|
+
wrap: el.containerId ? "soft" : "off",
|
|
4317
|
+
style: {
|
|
4318
|
+
left,
|
|
4319
|
+
top,
|
|
4320
|
+
width: Math.max(MIN_WIDTH, el.width) * zoom,
|
|
4321
|
+
height: Math.max(el.height, el.fontSize * lineHeight) * zoom,
|
|
4322
|
+
fontSize: el.fontSize * zoom,
|
|
4323
|
+
lineHeight,
|
|
4324
|
+
fontFamily: fontFamilyToCss(el.fontFamily),
|
|
4325
|
+
color: el.strokeColor,
|
|
4326
|
+
textAlign: el.textAlign,
|
|
4327
|
+
opacity: el.opacity / 100,
|
|
4328
|
+
transform: angleOf$1(el) ? `rotate(${angleOf$1(el)}rad)` : void 0,
|
|
4329
|
+
filter: dark ? DARK_SCENE_FILTER : void 0
|
|
4330
|
+
},
|
|
4331
|
+
onBlur: () => finishTextEditing(store),
|
|
4332
|
+
onChange: (e) => updateEditingText(store, e.target.value),
|
|
4333
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
4334
|
+
onKeyDown: (e) => {
|
|
4335
|
+
e.stopPropagation();
|
|
4336
|
+
if (e.key === "Escape" || e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
4337
|
+
e.preventDefault();
|
|
4338
|
+
finishTextEditing(store);
|
|
4339
|
+
}
|
|
4340
|
+
}
|
|
4341
|
+
});
|
|
4342
|
+
};
|
|
4343
|
+
//#endregion
|
|
4344
|
+
//#region src/editor/ZoomControls.tsx
|
|
4345
|
+
var ZoomControls = ({ compact = false, store }) => {
|
|
4346
|
+
const zoom$1 = useStore(store, (s) => s.viewport.zoom);
|
|
4347
|
+
useStore(store, (s) => s.elements);
|
|
4348
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4349
|
+
"data-whiteboard-chrome": true,
|
|
4350
|
+
className: `${floating} ${zoom}${compact ? ` ${zoomTop}` : ""}`,
|
|
4351
|
+
children: [
|
|
4352
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4353
|
+
icon: true,
|
|
4354
|
+
"aria-label": "Undo",
|
|
4355
|
+
disabled: !store.canUndo(),
|
|
4356
|
+
size: "sm",
|
|
4357
|
+
variant: "ghost",
|
|
4358
|
+
onClick: () => store.undo(),
|
|
4359
|
+
children: /* @__PURE__ */ jsx(Undo2, {})
|
|
4360
|
+
}),
|
|
4361
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4362
|
+
icon: true,
|
|
4363
|
+
"aria-label": "Redo",
|
|
4364
|
+
disabled: !store.canRedo(),
|
|
4365
|
+
size: "sm",
|
|
4366
|
+
variant: "ghost",
|
|
4367
|
+
onClick: () => store.redo(),
|
|
4368
|
+
children: /* @__PURE__ */ jsx(Redo2, {})
|
|
4369
|
+
}),
|
|
4370
|
+
!compact && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
4371
|
+
/* @__PURE__ */ jsx("span", { className: "_14kbw0v5" }),
|
|
4372
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4373
|
+
icon: true,
|
|
4374
|
+
"aria-label": "Zoom out",
|
|
4375
|
+
size: "sm",
|
|
4376
|
+
variant: "ghost",
|
|
4377
|
+
onClick: () => zoomBy(store, 1 / 1.2),
|
|
4378
|
+
children: /* @__PURE__ */ jsx(Minus, {})
|
|
4379
|
+
}),
|
|
4380
|
+
/* @__PURE__ */ jsxs("button", {
|
|
4381
|
+
"aria-label": "Reset zoom",
|
|
4382
|
+
className: "_14kbw0ve",
|
|
4383
|
+
type: "button",
|
|
4384
|
+
onClick: () => resetZoom(store),
|
|
4385
|
+
children: [Math.round(zoom$1 * 100), "%"]
|
|
4386
|
+
}),
|
|
4387
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4388
|
+
icon: true,
|
|
4389
|
+
"aria-label": "Zoom in",
|
|
4390
|
+
size: "sm",
|
|
4391
|
+
variant: "ghost",
|
|
4392
|
+
onClick: () => zoomBy(store, 1.2),
|
|
4393
|
+
children: /* @__PURE__ */ jsx(Plus, {})
|
|
4394
|
+
})
|
|
4395
|
+
] })
|
|
4396
|
+
]
|
|
4397
|
+
});
|
|
4398
|
+
};
|
|
4399
|
+
//#endregion
|
|
4400
|
+
//#region src/editor/WhiteboardEditor.tsx
|
|
4401
|
+
var MULTI_IMAGE_OFFSET = 20;
|
|
4402
|
+
var ARROW_KEYS = /* @__PURE__ */ new Set([
|
|
4403
|
+
"ArrowLeft",
|
|
4404
|
+
"ArrowRight",
|
|
4405
|
+
"ArrowUp",
|
|
4406
|
+
"ArrowDown"
|
|
4407
|
+
]);
|
|
4408
|
+
function isTypingTarget(target) {
|
|
4409
|
+
return target instanceof Element && (target.matches("input, textarea, select") || target.isContentEditable);
|
|
4410
|
+
}
|
|
4411
|
+
var WhiteboardEditor = ({ store, theme, toolbar = "inline", uploadFile, onUploadError }) => {
|
|
4412
|
+
const handlers = useMemo(createToolHandlers, []);
|
|
4413
|
+
const inputRef = useRef(null);
|
|
4414
|
+
const pickAtRef = useRef(null);
|
|
4415
|
+
const optionsRef = useRef({});
|
|
4416
|
+
optionsRef.current = {
|
|
4417
|
+
uploadFile,
|
|
4418
|
+
onUploadError
|
|
4419
|
+
};
|
|
4420
|
+
const context = useMemo(() => {
|
|
4421
|
+
const onFiles = (files, at) => {
|
|
4422
|
+
files.forEach((file, i) => {
|
|
4423
|
+
const offset = i * MULTI_IMAGE_OFFSET;
|
|
4424
|
+
insertImageFile(store, file, [at[0] + offset, at[1] + offset], optionsRef.current);
|
|
4425
|
+
});
|
|
4426
|
+
};
|
|
4427
|
+
const requestImage = (at) => {
|
|
4428
|
+
pickAtRef.current = at;
|
|
4429
|
+
inputRef.current?.click();
|
|
4430
|
+
};
|
|
4431
|
+
return {
|
|
4432
|
+
store,
|
|
4433
|
+
onFiles,
|
|
4434
|
+
requestImage
|
|
4435
|
+
};
|
|
4436
|
+
}, [store]);
|
|
4437
|
+
const tool = useStore(store, (s) => s.tool);
|
|
4438
|
+
const narrow = useNarrowLayout(store);
|
|
4439
|
+
const canvasWidth = useStore(store, (s) => s.canvasSize.width);
|
|
4440
|
+
const positionedRef = useRef(false);
|
|
4441
|
+
useEffect(() => {
|
|
4442
|
+
if (positionedRef.current || canvasWidth <= 0) return;
|
|
4443
|
+
positionedRef.current = true;
|
|
4444
|
+
const { elements, canvasSize } = store.getState();
|
|
4445
|
+
const alive = elements.filter((el) => !el.isDeleted);
|
|
4446
|
+
if (alive.length === 0) return;
|
|
4447
|
+
store.setState({ viewport: fitToContent(getCommonBounds(alive), canvasSize) });
|
|
4448
|
+
}, [canvasWidth, store]);
|
|
4449
|
+
useEffect(() => {
|
|
4450
|
+
if (tool !== "image") return;
|
|
4451
|
+
const { viewport, canvasSize, toolLocked } = store.getState();
|
|
4452
|
+
context.requestImage?.(screenToScene([canvasSize.width / 2, canvasSize.height / 2], viewport));
|
|
4453
|
+
if (!toolLocked) store.setState({ tool: "selection" });
|
|
4454
|
+
}, [
|
|
4455
|
+
context,
|
|
4456
|
+
store,
|
|
4457
|
+
tool
|
|
4458
|
+
]);
|
|
4459
|
+
const onKeyDown = (e) => {
|
|
4460
|
+
if (isTypingTarget(e.target)) return;
|
|
4461
|
+
if (!(e.target instanceof Element && e.target.hasAttribute("data-whiteboard-canvas")) && ARROW_KEYS.has(e.key)) return;
|
|
4462
|
+
if (getToolHandler(handlers, store.getState().tool).onKeyDown?.(context, e.nativeEvent) || handleShortcut(store, e)) e.preventDefault();
|
|
4463
|
+
};
|
|
4464
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4465
|
+
"data-whiteboard-editor": true,
|
|
4466
|
+
className: root,
|
|
4467
|
+
onKeyDown,
|
|
4468
|
+
children: [
|
|
4469
|
+
toolbar === "inline" && !narrow && /* @__PURE__ */ jsx("div", {
|
|
4470
|
+
className: "_14kbw0v2",
|
|
4471
|
+
children: /* @__PURE__ */ jsx(WhiteboardToolbar, { store })
|
|
4472
|
+
}),
|
|
4473
|
+
/* @__PURE__ */ jsxs(WhiteboardCanvas, {
|
|
4474
|
+
context,
|
|
4475
|
+
handlers,
|
|
4476
|
+
store,
|
|
4477
|
+
theme,
|
|
4478
|
+
children: [
|
|
4479
|
+
/* @__PURE__ */ jsx(SelectionOverlay, { store }),
|
|
4480
|
+
/* @__PURE__ */ jsx(TextEditor, {
|
|
4481
|
+
dark: theme === "dark",
|
|
4482
|
+
store
|
|
4483
|
+
}),
|
|
4484
|
+
/* @__PURE__ */ jsx(RichTextEditor, {
|
|
4485
|
+
dark: theme === "dark",
|
|
4486
|
+
store
|
|
4487
|
+
}),
|
|
4488
|
+
/* @__PURE__ */ jsx(FrameNameEditor, { store }),
|
|
4489
|
+
narrow ? /* @__PURE__ */ jsx(StyleSheet, { store }) : /* @__PURE__ */ jsx(StylePanel, { store }),
|
|
4490
|
+
/* @__PURE__ */ jsx(ZoomControls, {
|
|
4491
|
+
compact: narrow,
|
|
4492
|
+
store
|
|
4493
|
+
}),
|
|
4494
|
+
narrow && /* @__PURE__ */ jsx(MobileDock, { store })
|
|
4495
|
+
]
|
|
4496
|
+
}),
|
|
4497
|
+
/* @__PURE__ */ jsx("input", {
|
|
4498
|
+
hidden: true,
|
|
4499
|
+
multiple: true,
|
|
4500
|
+
accept: "image/*",
|
|
4501
|
+
ref: inputRef,
|
|
4502
|
+
type: "file",
|
|
4503
|
+
onChange: (e) => {
|
|
4504
|
+
const files = [...e.currentTarget.files ?? []];
|
|
4505
|
+
e.currentTarget.value = "";
|
|
4506
|
+
if (files.length > 0 && pickAtRef.current) context.onFiles?.(files, pickAtRef.current);
|
|
4507
|
+
}
|
|
4508
|
+
})
|
|
4509
|
+
]
|
|
4510
|
+
});
|
|
4511
|
+
};
|
|
4512
|
+
//#endregion
|
|
4513
|
+
//#region src/WhiteboardOptionsMenu.tsx
|
|
4514
|
+
var STORAGE_ITEMS = [
|
|
4515
|
+
{
|
|
4516
|
+
value: "inline",
|
|
4517
|
+
label: "Inline"
|
|
4518
|
+
},
|
|
4519
|
+
{
|
|
4520
|
+
value: "remote",
|
|
4521
|
+
label: "Remote"
|
|
4522
|
+
},
|
|
4523
|
+
{
|
|
4524
|
+
value: "delta",
|
|
4525
|
+
label: "Delta"
|
|
4526
|
+
}
|
|
4527
|
+
];
|
|
4528
|
+
var WhiteboardOptionsMenu = ({ onCopy, onExport, onOpenChange, open, storage }) => {
|
|
4529
|
+
const id = useId();
|
|
4530
|
+
const storageLabelId = `${id}-storage`;
|
|
4531
|
+
const refId = `${id}-ref`;
|
|
4532
|
+
return /* @__PURE__ */ jsxs(Popover, {
|
|
4533
|
+
open,
|
|
4534
|
+
onOpenChange,
|
|
4535
|
+
children: [/* @__PURE__ */ jsx(PopoverTrigger, { render: /* @__PURE__ */ jsx(ActionButton, {
|
|
4536
|
+
icon: true,
|
|
4537
|
+
"aria-label": "More options",
|
|
4538
|
+
size: "md",
|
|
4539
|
+
title: "More options",
|
|
4540
|
+
children: /* @__PURE__ */ jsx(Ellipsis, {})
|
|
4541
|
+
}) }), /* @__PURE__ */ jsxs(PopoverPanel, {
|
|
4542
|
+
flush: true,
|
|
4543
|
+
align: "end",
|
|
4544
|
+
side: "bottom",
|
|
4545
|
+
size: "md",
|
|
4546
|
+
children: [/* @__PURE__ */ jsx(PopoverBody, { children: /* @__PURE__ */ jsxs("div", {
|
|
4547
|
+
className: whiteboardOptionsActions,
|
|
4548
|
+
children: [/* @__PURE__ */ jsxs(ActionButton, {
|
|
4549
|
+
size: "md",
|
|
4550
|
+
variant: "outline",
|
|
4551
|
+
onClick: onCopy,
|
|
4552
|
+
children: [/* @__PURE__ */ jsx(Clipboard, {}), "Copy JSON"]
|
|
4553
|
+
}), /* @__PURE__ */ jsxs(ActionButton, {
|
|
4554
|
+
size: "md",
|
|
4555
|
+
variant: "outline",
|
|
4556
|
+
onClick: onExport,
|
|
4557
|
+
children: [/* @__PURE__ */ jsx(Download, {}), "Export JSON"]
|
|
4558
|
+
})]
|
|
4559
|
+
}) }), storage && /* @__PURE__ */ jsxs(PopoverBody, {
|
|
4560
|
+
className: "_1xd3mo4m",
|
|
4561
|
+
children: [
|
|
4562
|
+
/* @__PURE__ */ jsx("span", {
|
|
4563
|
+
className: "_1xd3mo4n",
|
|
4564
|
+
children: "Advanced"
|
|
4565
|
+
}),
|
|
4566
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4567
|
+
"aria-labelledby": storageLabelId,
|
|
4568
|
+
className: "_1xd3mo4o",
|
|
4569
|
+
role: "group",
|
|
4570
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
4571
|
+
className: "_1xd3mo4p",
|
|
4572
|
+
id: storageLabelId,
|
|
4573
|
+
children: "Storage"
|
|
4574
|
+
}), /* @__PURE__ */ jsx(SegmentedControl, {
|
|
4575
|
+
fullWidth: true,
|
|
4576
|
+
items: STORAGE_ITEMS,
|
|
4577
|
+
value: storage.mode,
|
|
4578
|
+
onChange: storage.onModeChange
|
|
4579
|
+
})]
|
|
4580
|
+
}),
|
|
4581
|
+
storage.mode !== "inline" && /* @__PURE__ */ jsx(Field, {
|
|
4582
|
+
id: refId,
|
|
4583
|
+
label: "Base URL or ref",
|
|
4584
|
+
children: /* @__PURE__ */ jsx(TextInput, {
|
|
4585
|
+
mono: true,
|
|
4586
|
+
id: refId,
|
|
4587
|
+
placeholder: "https://… or ref:file/…",
|
|
4588
|
+
spellCheck: false,
|
|
4589
|
+
value: storage.refValue,
|
|
4590
|
+
onChange: (event) => storage.onRefChange(event.target.value)
|
|
4591
|
+
})
|
|
4592
|
+
})
|
|
4593
|
+
]
|
|
4594
|
+
})]
|
|
4595
|
+
})]
|
|
4596
|
+
});
|
|
4597
|
+
};
|
|
4598
|
+
//#endregion
|
|
4599
|
+
//#region src/WhiteboardEditorDialog.tsx
|
|
4600
|
+
var defaultUploadError = () => toast({
|
|
4601
|
+
title: "Couldn’t upload the image; it is kept inside the whiteboard",
|
|
4602
|
+
tone: "error"
|
|
4603
|
+
});
|
|
4604
|
+
function initialStorageMode(saveSnapshot, snapshot) {
|
|
4605
|
+
if (!saveSnapshot || !snapshot) return "inline";
|
|
4606
|
+
if (snapshot.type === "delta") return "delta";
|
|
4607
|
+
if (snapshot.type === "remote") return "remote";
|
|
4608
|
+
return "inline";
|
|
4609
|
+
}
|
|
4610
|
+
function initialRef(saveSnapshot, snapshot) {
|
|
4611
|
+
if (!saveSnapshot || !snapshot) return "";
|
|
4612
|
+
if (snapshot.type === "remote") return snapshot.url;
|
|
4613
|
+
if (snapshot.type === "delta") return snapshot.baseUrl;
|
|
4614
|
+
return "";
|
|
4615
|
+
}
|
|
4616
|
+
var WhiteboardEditorDialog = ({ baseData, baseRef, dismiss, holdEscapeDismiss, initialData, initialSnapshot, isConfirming, onClose, onDirtyChange, onSave, onUploadError = defaultUploadError, requestDismiss, saveSnapshot, theme, uploadFile }) => {
|
|
4617
|
+
const [store] = useState(() => createWhiteboardStore(initialData));
|
|
4618
|
+
const bodyRef = useRef(null);
|
|
4619
|
+
const baseRefRef = useRef(baseRef);
|
|
4620
|
+
const baseDataRef = useRef(baseData);
|
|
4621
|
+
const narrow = useNarrowLayout(store);
|
|
4622
|
+
const [sceneDirty, setSceneDirty] = useState(false);
|
|
4623
|
+
const [settingsDirty, setSettingsDirty] = useState(false);
|
|
4624
|
+
const [saved, setSaved] = useState(false);
|
|
4625
|
+
const isDirty = !saved && (sceneDirty || settingsDirty);
|
|
4626
|
+
const [isSaving, setIsSaving] = useState(false);
|
|
4627
|
+
const [optionsOpen, setOptionsOpen] = useState(false);
|
|
4628
|
+
const [storageMode, setStorageMode] = useState(() => initialStorageMode(saveSnapshot, initialSnapshot));
|
|
4629
|
+
const [refValue, setRefValue] = useState(() => initialRef(saveSnapshot, initialSnapshot));
|
|
4630
|
+
useEffect(() => {
|
|
4631
|
+
onDirtyChange(isDirty);
|
|
4632
|
+
}, [isDirty, onDirtyChange]);
|
|
4633
|
+
useEffect(() => store.subscribe(() => setSceneDirty(store.isDirty())), [store]);
|
|
4634
|
+
const focusCanvas = useCallback(() => {
|
|
4635
|
+
queueMicrotask(() => bodyRef.current?.querySelector("[data-whiteboard-canvas]")?.focus({ preventScroll: true }));
|
|
4636
|
+
}, []);
|
|
4637
|
+
const getSnapshot = useCallback(() => store.getScene(), [store]);
|
|
4638
|
+
const uploadBase = useCallback(async (doc) => {
|
|
4639
|
+
if (!saveSnapshot || storageMode === "inline") return;
|
|
4640
|
+
if (storageMode === "delta" && baseRefRef.current && baseDataRef.current) return;
|
|
4641
|
+
if (storageMode === "remote" && baseRefRef.current && baseDataRef.current && JSON.stringify(doc) === JSON.stringify(baseDataRef.current)) return;
|
|
4642
|
+
const ref = await saveSnapshot(doc, baseRefRef.current || void 0);
|
|
4643
|
+
baseRefRef.current = ref;
|
|
4644
|
+
baseDataRef.current = doc;
|
|
4645
|
+
setRefValue(ref);
|
|
4646
|
+
}, [saveSnapshot, storageMode]);
|
|
4647
|
+
const composeSnapshot = useCallback(async (doc) => {
|
|
4648
|
+
if (storageMode === "inline" || !saveSnapshot) return {
|
|
4649
|
+
type: "inline",
|
|
4650
|
+
data: doc
|
|
4651
|
+
};
|
|
4652
|
+
const ref = baseRefRef.current;
|
|
4653
|
+
if (!ref) return;
|
|
4654
|
+
if (storageMode === "delta" && baseDataRef.current) {
|
|
4655
|
+
const { diff } = await import("jsondiffpatch");
|
|
4656
|
+
const delta = diff(baseDataRef.current, structuredClone(doc));
|
|
4657
|
+
return delta ? {
|
|
4658
|
+
type: "delta",
|
|
4659
|
+
baseUrl: ref,
|
|
4660
|
+
delta
|
|
4661
|
+
} : {
|
|
4662
|
+
type: "remote",
|
|
4663
|
+
url: ref
|
|
4664
|
+
};
|
|
4665
|
+
}
|
|
4666
|
+
return {
|
|
4667
|
+
type: "remote",
|
|
4668
|
+
url: ref
|
|
4669
|
+
};
|
|
4670
|
+
}, [saveSnapshot, storageMode]);
|
|
4671
|
+
const uploading = useStore(store, (s) => s.elements.some((el) => el.type === "image" && !el.isDeleted && el.status === "pending"));
|
|
4672
|
+
const handleSave = useCallback(async () => {
|
|
4673
|
+
if (isSaving || uploading) return;
|
|
4674
|
+
const doc = getSnapshot();
|
|
4675
|
+
if (!doc) return;
|
|
4676
|
+
setIsSaving(true);
|
|
4677
|
+
try {
|
|
4678
|
+
await uploadBase(doc);
|
|
4679
|
+
const snapshot = await composeSnapshot(doc);
|
|
4680
|
+
if (!snapshot) return;
|
|
4681
|
+
onSave(snapshot);
|
|
4682
|
+
onClose(doc);
|
|
4683
|
+
setSaved(true);
|
|
4684
|
+
dismiss();
|
|
4685
|
+
} catch {
|
|
4686
|
+
toast({
|
|
4687
|
+
title: "Couldn’t save the whiteboard",
|
|
4688
|
+
tone: "error"
|
|
4689
|
+
});
|
|
4690
|
+
} finally {
|
|
4691
|
+
setIsSaving(false);
|
|
4692
|
+
}
|
|
4693
|
+
}, [
|
|
4694
|
+
isSaving,
|
|
4695
|
+
uploading,
|
|
4696
|
+
getSnapshot,
|
|
4697
|
+
uploadBase,
|
|
4698
|
+
composeSnapshot,
|
|
4699
|
+
onSave,
|
|
4700
|
+
onClose,
|
|
4701
|
+
dismiss
|
|
4702
|
+
]);
|
|
4703
|
+
const optionsOpenRef = useRef(optionsOpen);
|
|
4704
|
+
optionsOpenRef.current = optionsOpen;
|
|
4705
|
+
useEffect(() => {
|
|
4706
|
+
const handleKeyDown = (event) => {
|
|
4707
|
+
if (event.key !== "Escape" || optionsOpenRef.current) return;
|
|
4708
|
+
if (!isConfirming() && editorOwnsEscape(store)) {
|
|
4709
|
+
holdEscapeDismiss?.();
|
|
4710
|
+
if (isEditorTarget(event.target)) return;
|
|
4711
|
+
event.preventDefault();
|
|
4712
|
+
event.stopImmediatePropagation();
|
|
4713
|
+
releaseEscape(store, bodyRef.current?.querySelector("[data-whiteboard-canvas]") ?? null);
|
|
4714
|
+
return;
|
|
4715
|
+
}
|
|
4716
|
+
event.preventDefault();
|
|
4717
|
+
event.stopImmediatePropagation();
|
|
4718
|
+
if (isConfirming()) dismissTopDialog();
|
|
4719
|
+
else requestDismiss();
|
|
4720
|
+
};
|
|
4721
|
+
document.addEventListener("keydown", handleKeyDown, true);
|
|
4722
|
+
return () => document.removeEventListener("keydown", handleKeyDown, true);
|
|
4723
|
+
}, [
|
|
4724
|
+
holdEscapeDismiss,
|
|
4725
|
+
isConfirming,
|
|
4726
|
+
requestDismiss,
|
|
4727
|
+
store
|
|
4728
|
+
]);
|
|
4729
|
+
const handleExportJson = useCallback(() => {
|
|
4730
|
+
const doc = getSnapshot();
|
|
4731
|
+
const blob = new Blob([JSON.stringify(doc, null, 2)], { type: "application/json" });
|
|
4732
|
+
const url = URL.createObjectURL(blob);
|
|
4733
|
+
const anchor = document.createElement("a");
|
|
4734
|
+
anchor.href = url;
|
|
4735
|
+
anchor.download = "excalidraw-snapshot.json";
|
|
4736
|
+
anchor.click();
|
|
4737
|
+
URL.revokeObjectURL(url);
|
|
4738
|
+
}, [getSnapshot]);
|
|
4739
|
+
const handleCopyJson = useCallback(() => {
|
|
4740
|
+
const doc = getSnapshot();
|
|
4741
|
+
navigator.clipboard.writeText(JSON.stringify(doc)).then(() => toast({
|
|
4742
|
+
title: "Copied",
|
|
4743
|
+
tone: "success"
|
|
4744
|
+
}), () => toast({
|
|
4745
|
+
title: "Couldn’t copy",
|
|
4746
|
+
tone: "error"
|
|
4747
|
+
}));
|
|
4748
|
+
}, [getSnapshot]);
|
|
4749
|
+
const handleRefChange = useCallback((value) => {
|
|
4750
|
+
setRefValue(value);
|
|
4751
|
+
baseRefRef.current = value;
|
|
4752
|
+
baseDataRef.current = void 0;
|
|
4753
|
+
setSettingsDirty(true);
|
|
4754
|
+
}, []);
|
|
4755
|
+
const handleStorageModeChange = useCallback((mode) => {
|
|
4756
|
+
setStorageMode(mode);
|
|
4757
|
+
if (mode === "inline") setRefValue("");
|
|
4758
|
+
setSettingsDirty(true);
|
|
4759
|
+
}, []);
|
|
4760
|
+
return /* @__PURE__ */ jsxs(ModalShell, {
|
|
4761
|
+
className: whiteboardShell,
|
|
4762
|
+
onSave: handleSave,
|
|
4763
|
+
children: [
|
|
4764
|
+
/* @__PURE__ */ jsx(ModalShellHeader, {
|
|
4765
|
+
center: narrow ? void 0 : /* @__PURE__ */ jsx(WhiteboardToolbar, {
|
|
4766
|
+
store,
|
|
4767
|
+
onToolSelect: focusCanvas
|
|
4768
|
+
}),
|
|
4769
|
+
icon: /* @__PURE__ */ jsx(PenLine, {}),
|
|
4770
|
+
title: "Whiteboard",
|
|
4771
|
+
actions: /* @__PURE__ */ jsx(WhiteboardOptionsMenu, {
|
|
4772
|
+
open: optionsOpen,
|
|
4773
|
+
storage: saveSnapshot ? {
|
|
4774
|
+
mode: storageMode,
|
|
4775
|
+
onModeChange: handleStorageModeChange,
|
|
4776
|
+
onRefChange: handleRefChange,
|
|
4777
|
+
refValue
|
|
4778
|
+
} : void 0,
|
|
4779
|
+
onCopy: handleCopyJson,
|
|
4780
|
+
onExport: handleExportJson,
|
|
4781
|
+
onOpenChange: setOptionsOpen
|
|
4782
|
+
}),
|
|
4783
|
+
onClose: requestDismiss
|
|
4784
|
+
}),
|
|
4785
|
+
/* @__PURE__ */ jsx(ModalShellBody, {
|
|
4786
|
+
className: whiteboardDialogCanvas,
|
|
4787
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
4788
|
+
className: whiteboardDialogBody,
|
|
4789
|
+
ref: bodyRef,
|
|
4790
|
+
children: /* @__PURE__ */ jsx(WhiteboardEditor, {
|
|
4791
|
+
store,
|
|
4792
|
+
theme,
|
|
4793
|
+
toolbar: "none",
|
|
4794
|
+
uploadFile,
|
|
4795
|
+
onUploadError
|
|
4796
|
+
})
|
|
4797
|
+
})
|
|
4798
|
+
}),
|
|
4799
|
+
/* @__PURE__ */ jsx(ModalShellFooter, {
|
|
4800
|
+
dirty: isDirty,
|
|
4801
|
+
saveDisabled: isSaving || uploading,
|
|
4802
|
+
status: /* @__PURE__ */ jsxs(Fragment, { children: [!isDirty && !isSaving && /* @__PURE__ */ jsx("span", {
|
|
4803
|
+
"aria-hidden": true,
|
|
4804
|
+
className: "_1xd3mo4k"
|
|
4805
|
+
}), isSaving ? "Saving…" : uploading ? "Uploading image…" : isDirty ? "Unsaved changes" : "Saved"] }),
|
|
4806
|
+
onCancel: requestDismiss,
|
|
4807
|
+
onSave: handleSave
|
|
4808
|
+
})
|
|
4809
|
+
]
|
|
4810
|
+
});
|
|
4811
|
+
};
|
|
4812
|
+
//#endregion
|
|
4813
|
+
//#region src/WhiteboardEditRenderer.tsx
|
|
4814
|
+
var WhiteboardEditRenderer_exports = /* @__PURE__ */ __exportAll({ WhiteboardEditRenderer: () => WhiteboardEditRenderer });
|
|
4815
|
+
var WhiteboardEditRenderer = ({ snapshot, onSnapshotChange }) => {
|
|
4816
|
+
const theme = useColorScheme();
|
|
4817
|
+
const { saveSnapshot, uploadFile } = useWhiteboardConfig();
|
|
4818
|
+
const { snapshot: initialData, loading: dataLoading, error: dataError, retry, retryable, baseRef, baseData } = useWhiteboardData(snapshot);
|
|
4819
|
+
const [editedData, setEditedData] = useState();
|
|
4820
|
+
const onSnapshotChangeRef = useRef(onSnapshotChange);
|
|
4821
|
+
onSnapshotChangeRef.current = onSnapshotChange;
|
|
4822
|
+
const saveSnapshotRef = useRef(saveSnapshot);
|
|
4823
|
+
saveSnapshotRef.current = saveSnapshot;
|
|
4824
|
+
const uploadFileRef = useRef(uploadFile);
|
|
4825
|
+
uploadFileRef.current = uploadFile;
|
|
4826
|
+
const snapshotRef = useRef(snapshot);
|
|
4827
|
+
snapshotRef.current = snapshot;
|
|
4828
|
+
const { className: portalClassName } = usePortalTheme();
|
|
4829
|
+
useWhiteboardFont();
|
|
4830
|
+
const previewData = editedData ?? initialData;
|
|
4831
|
+
const previewDataRef = useRef(previewData);
|
|
4832
|
+
previewDataRef.current = previewData;
|
|
4833
|
+
const previewScene = useMemo(() => toScene(previewData), [previewData]);
|
|
4834
|
+
const baseRefRef = useRef(baseRef);
|
|
4835
|
+
const baseDataRefOuter = useRef(baseData);
|
|
4836
|
+
useEffect(() => {
|
|
4837
|
+
if (baseRef) baseRefRef.current = baseRef;
|
|
4838
|
+
}, [baseRef]);
|
|
4839
|
+
useEffect(() => {
|
|
4840
|
+
if (baseData) baseDataRefOuter.current = baseData;
|
|
4841
|
+
}, [baseData]);
|
|
4842
|
+
const handleOpenEditor = useCallback(() => {
|
|
4843
|
+
if (dataLoading) return;
|
|
4844
|
+
const session = {
|
|
4845
|
+
dirty: false,
|
|
4846
|
+
confirming: false,
|
|
4847
|
+
escapeHeld: false
|
|
4848
|
+
};
|
|
4849
|
+
const holdEscapeDismiss = () => {
|
|
4850
|
+
session.escapeHeld = true;
|
|
4851
|
+
setTimeout(() => {
|
|
4852
|
+
session.escapeHeld = false;
|
|
4853
|
+
}, 0);
|
|
4854
|
+
};
|
|
4855
|
+
presentDialog({
|
|
4856
|
+
content: ({ dismiss, requestDismiss }) => /* @__PURE__ */ jsx(WhiteboardEditorDialog, {
|
|
4857
|
+
baseData: baseDataRefOuter.current,
|
|
4858
|
+
baseRef: baseRefRef.current,
|
|
4859
|
+
dismiss,
|
|
4860
|
+
holdEscapeDismiss,
|
|
4861
|
+
initialData: previewDataRef.current,
|
|
4862
|
+
initialSnapshot: snapshotRef.current,
|
|
4863
|
+
isConfirming: () => session.confirming,
|
|
4864
|
+
requestDismiss,
|
|
4865
|
+
saveSnapshot: saveSnapshotRef.current,
|
|
4866
|
+
theme,
|
|
4867
|
+
uploadFile: uploadFileRef.current,
|
|
4868
|
+
onSave: (ref) => onSnapshotChangeRef.current(ref),
|
|
4869
|
+
onClose: (doc) => {
|
|
4870
|
+
if (doc) setEditedData(doc);
|
|
4871
|
+
},
|
|
4872
|
+
onDirtyChange: (dirty) => {
|
|
4873
|
+
session.dirty = dirty;
|
|
4874
|
+
}
|
|
4875
|
+
}),
|
|
4876
|
+
className: whiteboardFullscreenPopup,
|
|
4877
|
+
portalClassName,
|
|
4878
|
+
theme,
|
|
4879
|
+
showCloseButton: false,
|
|
4880
|
+
clickOutsideToDismiss: false,
|
|
4881
|
+
confirmDismiss: async () => {
|
|
4882
|
+
if (session.escapeHeld) return false;
|
|
4883
|
+
if (!session.dirty) return true;
|
|
4884
|
+
if (session.confirming) return false;
|
|
4885
|
+
session.confirming = true;
|
|
4886
|
+
try {
|
|
4887
|
+
return await confirmDiscardChanges();
|
|
4888
|
+
} finally {
|
|
4889
|
+
session.confirming = false;
|
|
4890
|
+
}
|
|
4891
|
+
}
|
|
4892
|
+
});
|
|
4893
|
+
}, [
|
|
4894
|
+
dataLoading,
|
|
4895
|
+
theme,
|
|
4896
|
+
portalClassName
|
|
4897
|
+
]);
|
|
4898
|
+
if (dataLoading) return /* @__PURE__ */ jsx("div", {
|
|
4899
|
+
className: whiteboardEditorContainer,
|
|
4900
|
+
children: /* @__PURE__ */ jsx(WhiteboardLoadingState, {})
|
|
4901
|
+
});
|
|
4902
|
+
if (dataError) return /* @__PURE__ */ jsx("div", {
|
|
4903
|
+
className: whiteboardEditorContainer,
|
|
4904
|
+
children: /* @__PURE__ */ jsx(WhiteboardErrorState, {
|
|
4905
|
+
message: dataError,
|
|
4906
|
+
onRetry: retryable ? retry : void 0
|
|
4907
|
+
})
|
|
4908
|
+
});
|
|
4909
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
4910
|
+
className: whiteboardEditorContainer,
|
|
4911
|
+
children: [previewScene && countElements(previewScene) > 0 ? /* @__PURE__ */ jsx(Scene, {
|
|
4912
|
+
scene: previewScene,
|
|
4913
|
+
theme
|
|
4914
|
+
}) : /* @__PURE__ */ jsx(WhiteboardEmptyState, {}), /* @__PURE__ */ jsx("button", {
|
|
4915
|
+
"aria-label": "Edit whiteboard",
|
|
4916
|
+
className: whiteboardEditOverlay,
|
|
4917
|
+
type: "button",
|
|
4918
|
+
onClick: handleOpenEditor,
|
|
4919
|
+
children: /* @__PURE__ */ jsxs("span", {
|
|
4920
|
+
className: whiteboardEditLabel,
|
|
4921
|
+
children: [/* @__PURE__ */ jsx(Pencil, { size: 14 }), "Edit"]
|
|
4922
|
+
})
|
|
4923
|
+
})]
|
|
4924
|
+
});
|
|
4925
|
+
};
|
|
4926
|
+
//#endregion
|
|
4927
|
+
//#region src/WhiteboardPlugin.tsx
|
|
4928
|
+
var INSERT_WHITEBOARD_COMMAND = createCommand("INSERT_WHITEBOARD");
|
|
4929
|
+
function WhiteboardPlugin() {
|
|
4930
|
+
const [editor] = useLexicalComposerContext();
|
|
4931
|
+
useEffect(() => {
|
|
4932
|
+
return editor.registerCommand(INSERT_WHITEBOARD_COMMAND, (snapshot) => {
|
|
4933
|
+
const node = $createWhiteboardEditNode(snapshot);
|
|
4934
|
+
$insertNodes([node]);
|
|
4935
|
+
return true;
|
|
4936
|
+
}, COMMAND_PRIORITY_EDITOR);
|
|
4937
|
+
}, [editor]);
|
|
4938
|
+
return null;
|
|
4939
|
+
}
|
|
4940
|
+
//#endregion
|
|
4941
|
+
export { $createWhiteboardEditNode, $createWhiteboardNode, $isWhiteboardEditNode, $isWhiteboardNode, DEFAULT_FONT_CSS_HREF, EXCALIDRAW_BLOCK_TRANSFORMER, INSERT_WHITEBOARD_COMMAND, WHITEBOARD_NODE_KEY, WhiteboardConfigProvider, WhiteboardDisplayRenderer, WhiteboardDisplayRenderer as WhiteboardRenderer, WhiteboardDisplayRenderer as WhiteboardStaticRenderer, WhiteboardEditNode, WhiteboardEditRenderer, WhiteboardExpandShell, WhiteboardNode, WhiteboardPlugin, WhiteboardSSRRenderer, isLocalScenePath, parseSnapshot, restoreElements, serializeSnapshot, useWhiteboardConfig, whiteboardFullscreenPopup };
|