@drawback/ui 0.0.0-stage → 0.1.0
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 +22 -2
- package/dist/i18n-BZnCrD-C.d.mts +265 -0
- package/dist/index.d.mts +588 -0
- package/dist/index.mjs +4821 -0
- package/dist/locales/de.d.mts +3 -0
- package/dist/locales/de.mjs +231 -0
- package/package.json +69 -4
- package/src/app/default-editor.tsx +371 -0
- package/src/components/color-picker/color-picker.tsx +341 -0
- package/src/components/color-picker/colors.ts +135 -0
- package/src/components/font-picker/font-picker.tsx +298 -0
- package/src/components/icons.tsx +222 -0
- package/src/components/primitives.tsx +565 -0
- package/src/components/style-icons.tsx +172 -0
- package/src/features/canvas-commands/canvas-commands.tsx +173 -0
- package/src/features/context-menu/context-menu.tsx +354 -0
- package/src/features/controls/controls.tsx +281 -0
- package/src/features/export/export-dialog.tsx +164 -0
- package/src/features/layers/layers-tree.ts +335 -0
- package/src/features/layers/layers.tsx +858 -0
- package/src/features/links/links.tsx +268 -0
- package/src/features/main-menu/canvas-properties.tsx +121 -0
- package/src/features/main-menu/main-menu.tsx +215 -0
- package/src/features/minimap/minimap.tsx +69 -0
- package/src/features/properties/linear-properties.tsx +169 -0
- package/src/features/properties/properties.tsx +573 -0
- package/src/features/shortcuts/shortcuts-dialog.tsx +120 -0
- package/src/features/toolbar/toolbar.tsx +216 -0
- package/src/hooks/use-latest.ts +16 -0
- package/src/index.ts +138 -0
- package/src/lib/commands.ts +14 -0
- package/src/lib/download.ts +8 -0
- package/src/lib/i18n.ts +350 -0
- package/src/lib/locales/de.ts +231 -0
- package/src/lib/plugins.tsx +92 -0
- package/src/lib/registry.ts +140 -0
- package/src/stores/ui-context.tsx +253 -0
- package/src/styles.css +1366 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,4821 @@
|
|
|
1
|
+
import { EditorProvider, EditorView, deepEqual, useActiveTool, useBackground, useCanvasView, useCommand, useEditor, useEditorValue, useElement, useGrid, useMode, usePermission, usePresentation, useSelectedElements, useSelection, useStyleSummary, useToolLocked, useTools } from "@drawback/react";
|
|
2
|
+
import { Fragment, createContext, useCallback, useContext, useEffect, useEffectEvent, useId, useInsertionEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { createPortal } from "react-dom";
|
|
5
|
+
import { ALargeSmall, AlignCenterHorizontal, AlignCenterVertical, AlignEndHorizontal, AlignEndVertical, AlignHorizontalDistributeCenter, AlignStartHorizontal, AlignStartVertical, AlignVerticalDistributeCenter, ArrowDown, ArrowDownToLine, ArrowLeftRight, ArrowUp, ArrowUpDown, ArrowUpToLine, Ban, Check, ChevronDown, ChevronRight, Circle, CircleQuestionMark, ClipboardPaste, CodeXml, Copy, Crop, Diamond, Download, Ellipsis, Eraser, Eye, EyeOff, Focus, FolderOpen, Frame, Grid3x3, Grip, Group, Hand, Heading, Hexagon, Image, Keyboard, Laser, Layers, Link, Lock, LockOpen, Magnet, Map as Map$1, MapPin, Maximize, Menu, Minus, Monitor, Moon, MousePointer2, MoveRight, Pencil, Pipette, Plus, Redo2, RotateCcw, Save, ScanSearch, Scissors, Search, SearchX, Shapes, SlidersHorizontal, Sparkle, Square, Sun, TextAlignCenter, TextAlignEnd, TextAlignStart, Trash, Type, Undo2, Ungroup, X, ZoomIn, ZoomOut } from "lucide-react";
|
|
6
|
+
import { ARROW_TYPE, LINE_TYPE, isArrowElbowed, isLinearClosed, isLinearCurved, linearWorldPoints, orderKeysBetween, toError } from "@drawback/core";
|
|
7
|
+
import { mountMinimap } from "@drawback/canvas";
|
|
8
|
+
// src/lib/i18n.ts
|
|
9
|
+
const en = {
|
|
10
|
+
"toolbar.label": "Drawing tools",
|
|
11
|
+
"toolbar.lock": "Keep selected tool active after drawing",
|
|
12
|
+
"toolbar.more": "More tools",
|
|
13
|
+
"tool.core.select": "Selection",
|
|
14
|
+
"tool.core.hand": "Hand (panning)",
|
|
15
|
+
"tool.core.rectangle": "Rectangle",
|
|
16
|
+
"tool.core.diamond": "Diamond",
|
|
17
|
+
"tool.core.ellipse": "Ellipse",
|
|
18
|
+
"tool.core.arrow": "Arrow",
|
|
19
|
+
"tool.core.line": "Line",
|
|
20
|
+
"tool.core.freedraw": "Draw",
|
|
21
|
+
"tool.core.text": "Text",
|
|
22
|
+
"tool.core.image": "Insert image",
|
|
23
|
+
"tool.core.eraser": "Eraser",
|
|
24
|
+
"tool.core.laser": "Laser pointer",
|
|
25
|
+
"tool.core.frame": "Frame tool",
|
|
26
|
+
"shortcut.hint": "{label} — {shortcut}",
|
|
27
|
+
/** Joins alternative shortcuts in tooltips: "R or 2". */
|
|
28
|
+
"shortcut.or": " or ",
|
|
29
|
+
"history.undo": "Undo",
|
|
30
|
+
"history.redo": "Redo",
|
|
31
|
+
"zoom.in": "Zoom in",
|
|
32
|
+
"zoom.out": "Zoom out",
|
|
33
|
+
"zoom.reset": "Reset zoom",
|
|
34
|
+
"zoom.fit": "Zoom to fit",
|
|
35
|
+
"zoom.toSelection": "Zoom to selection",
|
|
36
|
+
"zoom.menu": "Zoom",
|
|
37
|
+
"zoom.level": "{percent}%",
|
|
38
|
+
"scroll.toContent": "Scroll back to content",
|
|
39
|
+
"zen.enter": "Zen mode",
|
|
40
|
+
"zen.exit": "Exit zen mode",
|
|
41
|
+
"view.exit": "Exit view mode",
|
|
42
|
+
"laser.label": "Laser pointer",
|
|
43
|
+
"properties.label": "Element properties",
|
|
44
|
+
"properties.stroke": "Stroke",
|
|
45
|
+
"properties.background": "Background",
|
|
46
|
+
"properties.fill": "Fill",
|
|
47
|
+
"properties.strokeWidth": "Stroke width",
|
|
48
|
+
"properties.strokeStyle": "Stroke style",
|
|
49
|
+
"properties.sloppiness": "Sloppiness",
|
|
50
|
+
"properties.arrowType": "Arrow type",
|
|
51
|
+
"properties.polygon": "Polygon",
|
|
52
|
+
"properties.cornerRadius": "Edges",
|
|
53
|
+
"properties.opacity": "Opacity",
|
|
54
|
+
"properties.arrowheads": "Arrowheads",
|
|
55
|
+
"properties.startArrowhead": "Start",
|
|
56
|
+
"properties.endArrowhead": "End",
|
|
57
|
+
"properties.fontFamily": "Font family",
|
|
58
|
+
"properties.fontSize": "Font size",
|
|
59
|
+
"properties.textAlign": "Text align",
|
|
60
|
+
"properties.textColor": "Text color",
|
|
61
|
+
"properties.layers": "Layers",
|
|
62
|
+
"properties.align": "Align",
|
|
63
|
+
"properties.actions": "Actions",
|
|
64
|
+
"properties.mixed": "Mixed",
|
|
65
|
+
"properties.custom": "Color",
|
|
66
|
+
"properties.selected": "{count, plural, one {# element} other {# elements}} selected",
|
|
67
|
+
"value.none": "None",
|
|
68
|
+
"value.solid": "Solid",
|
|
69
|
+
"value.hachure": "Hatch",
|
|
70
|
+
"value.cross-hatch": "Cross-hatch",
|
|
71
|
+
"value.dashed": "Dashed",
|
|
72
|
+
"value.dotted": "Dotted",
|
|
73
|
+
"value.thin": "Thin",
|
|
74
|
+
"value.bold": "Bold",
|
|
75
|
+
"value.extraBold": "Extra bold",
|
|
76
|
+
"value.architect": "Architect",
|
|
77
|
+
"value.artist": "Artist",
|
|
78
|
+
"value.cartoonist": "Cartoonist",
|
|
79
|
+
"value.sharp": "Sharp",
|
|
80
|
+
"value.curved": "Curved",
|
|
81
|
+
"value.elbow": "Elbow",
|
|
82
|
+
"value.open": "Open",
|
|
83
|
+
"value.closed": "Closed",
|
|
84
|
+
"value.round": "Round",
|
|
85
|
+
"value.small": "Small",
|
|
86
|
+
"value.medium": "Medium",
|
|
87
|
+
"value.large": "Large",
|
|
88
|
+
"value.extraLarge": "Extra large",
|
|
89
|
+
"value.left": "Left",
|
|
90
|
+
"value.center": "Center",
|
|
91
|
+
"value.right": "Right",
|
|
92
|
+
"value.arrow": "Arrow",
|
|
93
|
+
"value.triangle": "Triangle",
|
|
94
|
+
"value.bar": "Bar",
|
|
95
|
+
"value.circle": "Circle",
|
|
96
|
+
"value.diamond": "Diamond",
|
|
97
|
+
"font.core.sans": "Normal",
|
|
98
|
+
"font.core.hand": "Hand-drawn",
|
|
99
|
+
"font.core.mono": "Code",
|
|
100
|
+
"font.more": "More fonts",
|
|
101
|
+
"font.current": "Font: {name}",
|
|
102
|
+
"font.search": "Quick search",
|
|
103
|
+
"font.inScene": "In this scene",
|
|
104
|
+
"font.available": "Available fonts",
|
|
105
|
+
"font.none": "No fonts found",
|
|
106
|
+
"action.bringToFront": "Bring to front",
|
|
107
|
+
"action.bringForward": "Bring forward",
|
|
108
|
+
"action.sendBackward": "Send backward",
|
|
109
|
+
"action.sendToBack": "Send to back",
|
|
110
|
+
"action.alignLeft": "Align left",
|
|
111
|
+
"action.alignCenter": "Center horizontally",
|
|
112
|
+
"action.alignRight": "Align right",
|
|
113
|
+
"action.alignTop": "Align top",
|
|
114
|
+
"action.alignMiddle": "Center vertically",
|
|
115
|
+
"action.alignBottom": "Align bottom",
|
|
116
|
+
"action.distributeHorizontal": "Distribute horizontally",
|
|
117
|
+
"action.distributeVertical": "Distribute vertically",
|
|
118
|
+
"action.group": "Group selection",
|
|
119
|
+
"action.ungroup": "Ungroup",
|
|
120
|
+
"action.flipHorizontal": "Flip horizontal",
|
|
121
|
+
"action.flipVertical": "Flip vertical",
|
|
122
|
+
"action.lock": "Lock or unlock",
|
|
123
|
+
"action.duplicate": "Duplicate",
|
|
124
|
+
"action.delete": "Delete",
|
|
125
|
+
"action.crop": "Crop image",
|
|
126
|
+
"action.copy": "Copy",
|
|
127
|
+
"action.cut": "Cut",
|
|
128
|
+
"action.paste": "Paste",
|
|
129
|
+
"action.selectAll": "Select all",
|
|
130
|
+
"action.copyStyles": "Copy styles",
|
|
131
|
+
"action.pasteStyles": "Paste styles",
|
|
132
|
+
"action.copyAsPng": "Copy to clipboard as PNG",
|
|
133
|
+
"action.copyAsSvg": "Copy to clipboard as SVG",
|
|
134
|
+
"action.copyElementLink": "Copy link to object",
|
|
135
|
+
"toast.linkCopied": "Link copied",
|
|
136
|
+
"color.open": "{label}: more colors",
|
|
137
|
+
"color.colors": "Colors",
|
|
138
|
+
"color.shades": "Shades",
|
|
139
|
+
"color.shade": "Shade {index}",
|
|
140
|
+
"color.noShades": "No shades available for this color",
|
|
141
|
+
"color.hex": "Hex code",
|
|
142
|
+
"color.picker": "Pick a color from the screen",
|
|
143
|
+
"color.name.transparent": "Transparent",
|
|
144
|
+
"color.name.white": "White",
|
|
145
|
+
"color.name.black": "Black",
|
|
146
|
+
"color.name.gray": "Gray",
|
|
147
|
+
"color.name.bronze": "Bronze",
|
|
148
|
+
"color.name.cyan": "Cyan",
|
|
149
|
+
"color.name.blue": "Blue",
|
|
150
|
+
"color.name.violet": "Violet",
|
|
151
|
+
"color.name.grape": "Grape",
|
|
152
|
+
"color.name.pink": "Pink",
|
|
153
|
+
"color.name.green": "Green",
|
|
154
|
+
"color.name.teal": "Teal",
|
|
155
|
+
"color.name.yellow": "Yellow",
|
|
156
|
+
"color.name.orange": "Orange",
|
|
157
|
+
"color.name.red": "Red",
|
|
158
|
+
"action.canvasProperties": "Canvas properties",
|
|
159
|
+
"view.mode": "View mode",
|
|
160
|
+
"bindings.toggle": "Arrow binding",
|
|
161
|
+
"snapping.midpoints": "Snap to midpoints",
|
|
162
|
+
"canvas.label": "Canvas",
|
|
163
|
+
"canvas.background": "Canvas background",
|
|
164
|
+
"canvas.backgroundReset": "Use theme background",
|
|
165
|
+
"grid.label": "Grid",
|
|
166
|
+
"grid.type": "Grid type",
|
|
167
|
+
"grid.type.none": "None",
|
|
168
|
+
"grid.type.square": "Square",
|
|
169
|
+
"grid.type.dot": "Dots",
|
|
170
|
+
"grid.type.hex-pointy": "Hex (pointy)",
|
|
171
|
+
"grid.type.hex-flat": "Hex (flat)",
|
|
172
|
+
"grid.size": "Cell size",
|
|
173
|
+
"grid.color": "Grid color",
|
|
174
|
+
"grid.majorEvery": "Major lines every",
|
|
175
|
+
"grid.visible": "Show grid",
|
|
176
|
+
"grid.toggle": "Toggle grid",
|
|
177
|
+
"snapping.label": "Snapping",
|
|
178
|
+
"snapping.enabled": "Snap to objects",
|
|
179
|
+
"snapping.grid": "Snap to grid",
|
|
180
|
+
"theme.label": "Theme",
|
|
181
|
+
"theme.light": "Light",
|
|
182
|
+
"theme.dark": "Dark",
|
|
183
|
+
"theme.system": "System",
|
|
184
|
+
"language.label": "Language",
|
|
185
|
+
"layers.label": "Layers and objects",
|
|
186
|
+
"layers.toggle": "Layers",
|
|
187
|
+
"layers.search": "Search",
|
|
188
|
+
"layers.add": "Add layer",
|
|
189
|
+
"layers.newName": "Layer {number}",
|
|
190
|
+
"layers.show": "Show",
|
|
191
|
+
"layers.hide": "Hide",
|
|
192
|
+
"layers.lock": "Lock",
|
|
193
|
+
"layers.unlock": "Unlock",
|
|
194
|
+
"layers.expand": "Expand",
|
|
195
|
+
"layers.collapse": "Collapse",
|
|
196
|
+
"layers.rename": "Rename",
|
|
197
|
+
"layers.delete": "Delete layer",
|
|
198
|
+
"layers.group": "Group",
|
|
199
|
+
"layers.empty": "Nothing here yet",
|
|
200
|
+
"layers.noMatches": "No matches",
|
|
201
|
+
"layers.dragCount": "{count, plural, one {# element} other {# elements}}",
|
|
202
|
+
"layers.setActive": "Draw on this layer",
|
|
203
|
+
"menu.label": "Main menu",
|
|
204
|
+
"menu.open": "Open…",
|
|
205
|
+
"menu.save": "Save to file",
|
|
206
|
+
"menu.exportImage": "Export image…",
|
|
207
|
+
"menu.exportBundle": "Save with images",
|
|
208
|
+
"menu.reset": "Reset the canvas",
|
|
209
|
+
"menu.resetConfirm": "This will clear the whole canvas. Continue?",
|
|
210
|
+
"menu.shortcuts": "Keyboard shortcuts",
|
|
211
|
+
"menu.canvas": "Canvas settings",
|
|
212
|
+
"link.edit": "Edit link…",
|
|
213
|
+
"link.label": "Link",
|
|
214
|
+
"link.placeholder": "Type or paste your link here",
|
|
215
|
+
"link.change": "Edit link",
|
|
216
|
+
"link.save": "Save",
|
|
217
|
+
"link.remove": "Remove link",
|
|
218
|
+
"link.invalid": "This link cannot be used here.",
|
|
219
|
+
"contextMenu.label": "Context menu",
|
|
220
|
+
"export.title": "Export image",
|
|
221
|
+
"export.format": "Format",
|
|
222
|
+
"export.selectionOnly": "Only selected",
|
|
223
|
+
"export.background": "Background",
|
|
224
|
+
"export.grid": "Include grid",
|
|
225
|
+
"export.scale": "Scale",
|
|
226
|
+
"export.padding": "Padding",
|
|
227
|
+
"export.download": "Download",
|
|
228
|
+
"export.warnings": "{count, plural, one {# warning} other {# warnings}}",
|
|
229
|
+
"shortcuts.title": "Keyboard shortcuts",
|
|
230
|
+
"shortcuts.search": "Search shortcuts",
|
|
231
|
+
"shortcuts.tools": "Tools",
|
|
232
|
+
"shortcuts.commands": "Editor",
|
|
233
|
+
"dialog.close": "Close",
|
|
234
|
+
"minimap.label": "Minimap",
|
|
235
|
+
"minimap.toggle": "Toggle minimap",
|
|
236
|
+
"error.import": "Could not open the file: {message}"
|
|
237
|
+
};
|
|
238
|
+
/**
|
|
239
|
+
* Built-in languages, offered in the main menu's language picker under their own names.
|
|
240
|
+
* Catalogs other than English are separate chunks, loaded when a locale first needs them.
|
|
241
|
+
*/
|
|
242
|
+
const LANGUAGES = [{
|
|
243
|
+
locale: "en",
|
|
244
|
+
name: "English"
|
|
245
|
+
}, {
|
|
246
|
+
locale: "de",
|
|
247
|
+
name: "Deutsch",
|
|
248
|
+
load: () => import("./locales/de.mjs").then((module) => module.de)
|
|
249
|
+
}];
|
|
250
|
+
const loadedCatalogs = /* @__PURE__ */ new Map();
|
|
251
|
+
const pendingCatalogs = /* @__PURE__ */ new Map();
|
|
252
|
+
function languageOf(locale) {
|
|
253
|
+
const base = locale.toLowerCase().split("-")[0];
|
|
254
|
+
return LANGUAGES.find((entry) => entry.locale === base);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Built-in catalog for a locale (`de-AT` falls back to `de`) once it has loaded, else
|
|
258
|
+
* `undefined` (always for English). See {@link loadMessages}.
|
|
259
|
+
*/
|
|
260
|
+
function builtinMessages(locale) {
|
|
261
|
+
const language = languageOf(locale);
|
|
262
|
+
return language === void 0 ? void 0 : loadedCatalogs.get(language.locale);
|
|
263
|
+
}
|
|
264
|
+
/** Whether `locale` has a built-in catalog that is not loaded yet. */
|
|
265
|
+
function needsMessages(locale) {
|
|
266
|
+
const language = languageOf(locale);
|
|
267
|
+
return language?.load !== void 0 && !loadedCatalogs.has(language.locale);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Loads the built-in catalog for `locale` (once; later calls share the result). Await it
|
|
271
|
+
* before mounting to render a non-English editor without first showing English.
|
|
272
|
+
*/
|
|
273
|
+
function loadMessages(locale) {
|
|
274
|
+
const language = languageOf(locale);
|
|
275
|
+
if (language?.load === void 0) return Promise.resolve(void 0);
|
|
276
|
+
const cached = loadedCatalogs.get(language.locale);
|
|
277
|
+
if (cached !== void 0) return Promise.resolve(cached);
|
|
278
|
+
let pending = pendingCatalogs.get(language.locale);
|
|
279
|
+
if (pending === void 0) {
|
|
280
|
+
pending = language.load().then((messages) => {
|
|
281
|
+
loadedCatalogs.set(language.locale, messages);
|
|
282
|
+
pendingCatalogs.delete(language.locale);
|
|
283
|
+
return messages;
|
|
284
|
+
}, (error) => {
|
|
285
|
+
pendingCatalogs.delete(language.locale);
|
|
286
|
+
throw error;
|
|
287
|
+
});
|
|
288
|
+
pendingCatalogs.set(language.locale, pending);
|
|
289
|
+
}
|
|
290
|
+
return pending;
|
|
291
|
+
}
|
|
292
|
+
const PLURAL = /\{(\w+),\s*plural,\s*((?:[^{}]*\{[^{}]*\})+)\s*\}/g;
|
|
293
|
+
const BRANCH = /(=\d+|zero|one|two|few|many|other)\s*\{([^{}]*)\}/g;
|
|
294
|
+
/**
|
|
295
|
+
* Translator over a message catalog: `{name}` interpolation and
|
|
296
|
+
* `{count, plural, one {…} other {…}}` with `#` for the number (`Intl.PluralRules`).
|
|
297
|
+
*/
|
|
298
|
+
function createTranslator(messages = {}, locale = "en") {
|
|
299
|
+
const rules = new Intl.PluralRules(locale);
|
|
300
|
+
const numbers = new Intl.NumberFormat(locale);
|
|
301
|
+
return (key, values = {}) => {
|
|
302
|
+
return (messages[key] ?? en[key] ?? key).replace(PLURAL, (_match, name, body) => {
|
|
303
|
+
const count = Number(values[name] ?? 0);
|
|
304
|
+
const branches = /* @__PURE__ */ new Map();
|
|
305
|
+
for (const branch of body.matchAll(BRANCH)) branches.set(branch[1], branch[2]);
|
|
306
|
+
return (branches.get(`=${count}`) ?? branches.get(rules.select(count)) ?? branches.get("other") ?? "").replaceAll("#", numbers.format(count));
|
|
307
|
+
}).replace(/\{(\w+)\}/g, (match, name) => values[name] === void 0 ? match : String(values[name]));
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
// src/lib/registry.ts
|
|
311
|
+
/** Per-editor registrations for toolbar items, context menu items, property editors and icons. */
|
|
312
|
+
function createUiRegistry() {
|
|
313
|
+
let version = 0;
|
|
314
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
315
|
+
const changed = () => {
|
|
316
|
+
version += 1;
|
|
317
|
+
for (const listener of [...listeners]) listener();
|
|
318
|
+
};
|
|
319
|
+
const collection = () => {
|
|
320
|
+
const values = /* @__PURE__ */ new Map();
|
|
321
|
+
return {
|
|
322
|
+
register: (value) => {
|
|
323
|
+
values.set(value.id, value);
|
|
324
|
+
changed();
|
|
325
|
+
return () => {
|
|
326
|
+
if (values.get(value.id) === value) {
|
|
327
|
+
values.delete(value.id);
|
|
328
|
+
changed();
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
},
|
|
332
|
+
list: () => [...values.values()]
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
const map = () => {
|
|
336
|
+
const values = /* @__PURE__ */ new Map();
|
|
337
|
+
return {
|
|
338
|
+
register: (key, value) => {
|
|
339
|
+
values.set(key, value);
|
|
340
|
+
changed();
|
|
341
|
+
return () => {
|
|
342
|
+
if (values.get(key) === value) {
|
|
343
|
+
values.delete(key);
|
|
344
|
+
changed();
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
},
|
|
348
|
+
get: (key) => values.get(key),
|
|
349
|
+
keys: () => [...values.keys()]
|
|
350
|
+
};
|
|
351
|
+
};
|
|
352
|
+
const editors = map();
|
|
353
|
+
const icons = map();
|
|
354
|
+
return {
|
|
355
|
+
toolbar: collection(),
|
|
356
|
+
contextMenu: collection(),
|
|
357
|
+
propertyEditors: {
|
|
358
|
+
register: editors.register,
|
|
359
|
+
get: editors.get,
|
|
360
|
+
types: editors.keys
|
|
361
|
+
},
|
|
362
|
+
icons: {
|
|
363
|
+
register: icons.register,
|
|
364
|
+
get: icons.get
|
|
365
|
+
},
|
|
366
|
+
version: () => version,
|
|
367
|
+
subscribe: (listener) => {
|
|
368
|
+
listeners.add(listener);
|
|
369
|
+
return () => listeners.delete(listener);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/** Sorts by `order`, then id, so equal orders are deterministic. */
|
|
374
|
+
function byOrder(values) {
|
|
375
|
+
return [...values].sort((left, right) => (left.order ?? 500) - (right.order ?? 500) || (left.id < right.id ? -1 : left.id > right.id ? 1 : 0));
|
|
376
|
+
}
|
|
377
|
+
// src/stores/ui-context.tsx
|
|
378
|
+
const UiContext = createContext(void 0);
|
|
379
|
+
const fallbackRegistry = createUiRegistry();
|
|
380
|
+
function subscribeColorScheme(listener) {
|
|
381
|
+
if (typeof matchMedia !== "function") return () => void 0;
|
|
382
|
+
const query = matchMedia("(prefers-color-scheme: dark)");
|
|
383
|
+
query.addEventListener("change", listener);
|
|
384
|
+
return () => query.removeEventListener("change", listener);
|
|
385
|
+
}
|
|
386
|
+
function prefersDark() {
|
|
387
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches;
|
|
388
|
+
}
|
|
389
|
+
/** Resolves `'system'` through `prefers-color-scheme`, following changes. */
|
|
390
|
+
function useResolvedTheme(setting) {
|
|
391
|
+
const dark = useSyncExternalStore(subscribeColorScheme, prefersDark, () => false);
|
|
392
|
+
return setting === "system" ? dark ? "dark" : "light" : setting;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Strings, registrations, hidden controls, theme and panel state for UI components.
|
|
396
|
+
* `<DefaultEditor>` renders one; hosts placing components themselves can render their own.
|
|
397
|
+
*/
|
|
398
|
+
function UiProvider({ messages, locale, onLocaleChange, dir = "ltr", registry = fallbackRegistry, hidden = [], theme, onThemeChange, portalContainer = null, initialOpen = [], children }) {
|
|
399
|
+
const [ownTheme, setOwnTheme] = useState(theme ?? "system");
|
|
400
|
+
const themeSetting = onThemeChange === void 0 ? theme ?? ownTheme : theme ?? "system";
|
|
401
|
+
const resolved = useResolvedTheme(themeSetting);
|
|
402
|
+
const [open, setOpenState] = useState(() => new Set(initialOpen));
|
|
403
|
+
const [contextMenu, openContextMenu] = useState(void 0);
|
|
404
|
+
useSyncExternalStore(registry.subscribe, registry.version, registry.version);
|
|
405
|
+
const registryVersion = registry.version();
|
|
406
|
+
const [ownLocale, setOwnLocale] = useState(locale ?? "en");
|
|
407
|
+
const currentLocale = onLocaleChange === void 0 ? locale ?? ownLocale : locale ?? "en";
|
|
408
|
+
const [shown, setShown] = useState({ locale: "en" });
|
|
409
|
+
const loading = needsMessages(currentLocale);
|
|
410
|
+
const catalog = loading ? shown : {
|
|
411
|
+
locale: currentLocale,
|
|
412
|
+
messages: builtinMessages(currentLocale)
|
|
413
|
+
};
|
|
414
|
+
useEffect(() => {
|
|
415
|
+
if (!loading) return;
|
|
416
|
+
let cancelled = false;
|
|
417
|
+
loadMessages(currentLocale).then((messages) => {
|
|
418
|
+
if (!cancelled) setShown({
|
|
419
|
+
locale: currentLocale,
|
|
420
|
+
...messages === void 0 ? {} : { messages }
|
|
421
|
+
});
|
|
422
|
+
}, (error) => {
|
|
423
|
+
console.error(`Could not load the "${currentLocale}" catalog.`, error);
|
|
424
|
+
if (!cancelled) setShown({ locale: currentLocale });
|
|
425
|
+
});
|
|
426
|
+
return () => {
|
|
427
|
+
cancelled = true;
|
|
428
|
+
};
|
|
429
|
+
}, [currentLocale, loading]);
|
|
430
|
+
const t = useMemo(() => createTranslator({
|
|
431
|
+
...catalog.messages,
|
|
432
|
+
...messages
|
|
433
|
+
}, catalog.locale), [
|
|
434
|
+
messages,
|
|
435
|
+
catalog.messages,
|
|
436
|
+
catalog.locale
|
|
437
|
+
]);
|
|
438
|
+
const setLocale = useCallback((next) => {
|
|
439
|
+
if (onLocaleChange === void 0) setOwnLocale(next);
|
|
440
|
+
else onLocaleChange(next);
|
|
441
|
+
}, [onLocaleChange]);
|
|
442
|
+
const hiddenKey = hidden.join("|");
|
|
443
|
+
const hiddenSet = useMemo(() => new Set(hiddenKey === "" ? [] : hiddenKey.split("|")), [hiddenKey]);
|
|
444
|
+
const setTheme = useCallback((next) => {
|
|
445
|
+
if (onThemeChange === void 0) setOwnTheme(next);
|
|
446
|
+
else onThemeChange(next);
|
|
447
|
+
}, [onThemeChange]);
|
|
448
|
+
const setOpen = useCallback((panel, value) => {
|
|
449
|
+
setOpenState((current) => {
|
|
450
|
+
if (current.has(panel) === value) return current;
|
|
451
|
+
const next = new Set(current);
|
|
452
|
+
if (value) next.add(panel);
|
|
453
|
+
else next.delete(panel);
|
|
454
|
+
return next;
|
|
455
|
+
});
|
|
456
|
+
}, []);
|
|
457
|
+
const value = useMemo(() => ({
|
|
458
|
+
t,
|
|
459
|
+
registry,
|
|
460
|
+
registryVersion,
|
|
461
|
+
isHidden: (id) => hiddenSet.has(id),
|
|
462
|
+
theme: resolved,
|
|
463
|
+
themeSetting,
|
|
464
|
+
setTheme,
|
|
465
|
+
locale: currentLocale,
|
|
466
|
+
setLocale,
|
|
467
|
+
portalContainer,
|
|
468
|
+
dir,
|
|
469
|
+
isOpen: (panel) => open.has(panel),
|
|
470
|
+
setOpen,
|
|
471
|
+
contextMenu,
|
|
472
|
+
openContextMenu
|
|
473
|
+
}), [
|
|
474
|
+
t,
|
|
475
|
+
registry,
|
|
476
|
+
registryVersion,
|
|
477
|
+
hiddenSet,
|
|
478
|
+
resolved,
|
|
479
|
+
themeSetting,
|
|
480
|
+
setTheme,
|
|
481
|
+
currentLocale,
|
|
482
|
+
setLocale,
|
|
483
|
+
portalContainer,
|
|
484
|
+
dir,
|
|
485
|
+
open,
|
|
486
|
+
setOpen,
|
|
487
|
+
contextMenu
|
|
488
|
+
]);
|
|
489
|
+
return /* @__PURE__ */ jsx(UiContext, {
|
|
490
|
+
value,
|
|
491
|
+
children
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
function useUi() {
|
|
495
|
+
const context = useContext(UiContext);
|
|
496
|
+
if (context === void 0) throw new Error("UI components must be rendered inside <DefaultEditor> or <UiProvider>.");
|
|
497
|
+
return context;
|
|
498
|
+
}
|
|
499
|
+
function useT() {
|
|
500
|
+
return useUi().t;
|
|
501
|
+
}
|
|
502
|
+
// src/lib/commands.ts
|
|
503
|
+
/** Edits the link of the single selected element (Mod+K). */
|
|
504
|
+
const LINK_COMMAND = "ui.link.edit";
|
|
505
|
+
/** Copies the selection (or the whole drawing) to the system clipboard as a PNG image. */
|
|
506
|
+
const COPY_AS_PNG_COMMAND = "ui.copyAsPng";
|
|
507
|
+
/** Copies the selection (or the whole drawing) to the system clipboard as SVG markup. */
|
|
508
|
+
const COPY_AS_SVG_COMMAND = "ui.copyAsSvg";
|
|
509
|
+
/** Copies a URL that points at the single selected element (`links.elementUrl`). */
|
|
510
|
+
const COPY_ELEMENT_LINK_COMMAND = "ui.copyElementLink";
|
|
511
|
+
/** Shows or hides the minimap. */
|
|
512
|
+
const MINIMAP_COMMAND = "ui.minimap.toggle";
|
|
513
|
+
/** Opens or closes the main menu, which holds the canvas settings. */
|
|
514
|
+
const CANVAS_PROPERTIES_COMMAND = "ui.canvasProperties.toggle";
|
|
515
|
+
// src/components/style-icons.tsx
|
|
516
|
+
function svg(children, fill = false) {
|
|
517
|
+
return function StyleIcon({ size = 18, "aria-hidden": hidden }) {
|
|
518
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
519
|
+
width: size,
|
|
520
|
+
height: size,
|
|
521
|
+
viewBox: "0 0 20 20",
|
|
522
|
+
fill: fill ? "currentColor" : "none",
|
|
523
|
+
stroke: "currentColor",
|
|
524
|
+
strokeLinecap: "round",
|
|
525
|
+
strokeLinejoin: "round",
|
|
526
|
+
"aria-hidden": hidden,
|
|
527
|
+
children
|
|
528
|
+
});
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
const styleIcons = {
|
|
532
|
+
strokeThin: svg(/* @__PURE__ */ jsx("path", {
|
|
533
|
+
d: "M4 10h12",
|
|
534
|
+
strokeWidth: 1.25
|
|
535
|
+
})),
|
|
536
|
+
strokeMedium: svg(/* @__PURE__ */ jsx("path", {
|
|
537
|
+
d: "M4 10h12",
|
|
538
|
+
strokeWidth: 2.5
|
|
539
|
+
})),
|
|
540
|
+
strokeBold: svg(/* @__PURE__ */ jsx("path", {
|
|
541
|
+
d: "M4 10h12",
|
|
542
|
+
strokeWidth: 4
|
|
543
|
+
})),
|
|
544
|
+
strokeSolid: svg(/* @__PURE__ */ jsx("path", {
|
|
545
|
+
d: "M4 10h12",
|
|
546
|
+
strokeWidth: 2
|
|
547
|
+
})),
|
|
548
|
+
strokeDashed: svg(/* @__PURE__ */ jsx("path", {
|
|
549
|
+
d: "M4 10h12",
|
|
550
|
+
strokeWidth: 2,
|
|
551
|
+
strokeDasharray: "4 3",
|
|
552
|
+
strokeLinecap: "butt"
|
|
553
|
+
})),
|
|
554
|
+
strokeDotted: svg(/* @__PURE__ */ jsx("path", {
|
|
555
|
+
d: "M4 10h12",
|
|
556
|
+
strokeWidth: 2,
|
|
557
|
+
strokeDasharray: "0 4"
|
|
558
|
+
})),
|
|
559
|
+
sloppinessArchitect: svg(/* @__PURE__ */ jsx("path", {
|
|
560
|
+
d: "M2.5 13.5c2.5-1 4.5-5 7-5s2.5 3 4 3 2.5-1.5 4-3",
|
|
561
|
+
strokeWidth: 1.25
|
|
562
|
+
})),
|
|
563
|
+
sloppinessArtist: svg(/* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("path", {
|
|
564
|
+
d: "M2.5 13.5c2.5-1 4.5-5 7-5s2.5 3 4 3 2.5-1.5 4-3",
|
|
565
|
+
strokeWidth: 1.25
|
|
566
|
+
}), /* @__PURE__ */ jsx("path", {
|
|
567
|
+
d: "M3 15c2.5-1.5 4.5-5 6.5-5.5s3 2.5 4.5 2.5 2.5-1.5 3.5-2.5",
|
|
568
|
+
strokeWidth: 1
|
|
569
|
+
})] })),
|
|
570
|
+
sloppinessCartoonist: svg(/* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("path", {
|
|
571
|
+
d: "M2.5 13c1-.5 1.5-2 2.5-3s1.5.5 2.5-1 1.5-2 2.5-1.5 1 2.5 2 2.5 1.5-1 2.5-1.5 1.5-1 3-2",
|
|
572
|
+
strokeWidth: 1.25
|
|
573
|
+
}), /* @__PURE__ */ jsx("path", {
|
|
574
|
+
d: "M3 15.5c1.5-1 2-3 3.5-3.5s1 1 2.5-.5 2-2.5 3-1.5.5 2 2 1.5 1.5-1.5 3.5-2",
|
|
575
|
+
strokeWidth: 1
|
|
576
|
+
})] })),
|
|
577
|
+
edgeSharp: svg(/* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("path", {
|
|
578
|
+
d: "M12.5 4H16v12H4v-3.5",
|
|
579
|
+
strokeWidth: 1.25,
|
|
580
|
+
strokeDasharray: "1.5 2.5"
|
|
581
|
+
}), /* @__PURE__ */ jsx("path", {
|
|
582
|
+
d: "M4 11V4h7",
|
|
583
|
+
strokeWidth: 1.5,
|
|
584
|
+
strokeLinejoin: "miter",
|
|
585
|
+
strokeLinecap: "butt"
|
|
586
|
+
})] })),
|
|
587
|
+
edgeRound: svg(/* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("path", {
|
|
588
|
+
d: "M12.5 4H16v12H4v-3.5",
|
|
589
|
+
strokeWidth: 1.25,
|
|
590
|
+
strokeDasharray: "1.5 2.5"
|
|
591
|
+
}), /* @__PURE__ */ jsx("path", {
|
|
592
|
+
d: "M4 11V8.5A4.5 4.5 0 0 1 8.5 4H11",
|
|
593
|
+
strokeWidth: 1.5
|
|
594
|
+
})] })),
|
|
595
|
+
arrowSharp: svg(/* @__PURE__ */ jsx("path", {
|
|
596
|
+
d: "M4 16 16 4m-5 0h5v5",
|
|
597
|
+
strokeWidth: 1.5
|
|
598
|
+
})),
|
|
599
|
+
arrowCurved: svg(/* @__PURE__ */ jsx("path", {
|
|
600
|
+
d: "M4 16C4 9 9 4 16 4m-4-3 4 3-3 4",
|
|
601
|
+
strokeWidth: 1.5
|
|
602
|
+
})),
|
|
603
|
+
arrowElbow: svg(/* @__PURE__ */ jsx("path", {
|
|
604
|
+
d: "M4 16v-6h12V4m-3 3 3-3 3 3",
|
|
605
|
+
strokeWidth: 1.5
|
|
606
|
+
})),
|
|
607
|
+
polygon: svg(/* @__PURE__ */ jsx("path", {
|
|
608
|
+
d: "M10 3 17 8l-2.5 8h-9L3 8z",
|
|
609
|
+
strokeWidth: 1.5
|
|
610
|
+
})),
|
|
611
|
+
polyline: svg(/* @__PURE__ */ jsx("path", {
|
|
612
|
+
d: "M3 15 8 5l4 8 5-9",
|
|
613
|
+
strokeWidth: 1.5
|
|
614
|
+
})),
|
|
615
|
+
/** A sans-serif capital A (Excalidraw's "normal" font icon). */
|
|
616
|
+
fontSans: svg(/* @__PURE__ */ jsx("path", {
|
|
617
|
+
d: "M5 16.5 10 3.5l5 13M6.9 11.5h6.2",
|
|
618
|
+
strokeWidth: 1.5
|
|
619
|
+
})),
|
|
620
|
+
fontSerif: svg(/* @__PURE__ */ jsx("path", {
|
|
621
|
+
d: "M5.5 16.5 10 3.5l4.5 13M7.2 11.5h5.6M3.5 16.5h4M12.5 16.5h4",
|
|
622
|
+
strokeWidth: 1.25
|
|
623
|
+
}))
|
|
624
|
+
};
|
|
625
|
+
// src/components/icons.tsx
|
|
626
|
+
/** Built-in icon ids mapped to lucide components; hosts override per id through the registry. */
|
|
627
|
+
/** Lucide's hexagon has a vertex on top; turned a quarter it has a flat top. */
|
|
628
|
+
const HexagonFlat = (props) => /* @__PURE__ */ jsx(Hexagon, {
|
|
629
|
+
...props,
|
|
630
|
+
style: { transform: "rotate(90deg)" }
|
|
631
|
+
});
|
|
632
|
+
const builtinIcons = {
|
|
633
|
+
"core.select": MousePointer2,
|
|
634
|
+
"core.hand": Hand,
|
|
635
|
+
"core.rectangle": Square,
|
|
636
|
+
"core.diamond": Diamond,
|
|
637
|
+
"core.ellipse": Circle,
|
|
638
|
+
"core.arrow": MoveRight,
|
|
639
|
+
"core.line": Minus,
|
|
640
|
+
"core.freedraw": Pencil,
|
|
641
|
+
"core.text": Type,
|
|
642
|
+
"core.image": Image,
|
|
643
|
+
"core.eraser": Eraser,
|
|
644
|
+
"core.laser": Laser,
|
|
645
|
+
rectangle: Square,
|
|
646
|
+
diamond: Diamond,
|
|
647
|
+
ellipse: Circle,
|
|
648
|
+
arrow: MoveRight,
|
|
649
|
+
line: Minus,
|
|
650
|
+
freedraw: Pencil,
|
|
651
|
+
text: Type,
|
|
652
|
+
image: Image,
|
|
653
|
+
element: Shapes,
|
|
654
|
+
unknown: Sparkle,
|
|
655
|
+
pin: MapPin,
|
|
656
|
+
lock: Lock,
|
|
657
|
+
link: Link,
|
|
658
|
+
edit: Pencil,
|
|
659
|
+
pipette: Pipette,
|
|
660
|
+
unlock: LockOpen,
|
|
661
|
+
undo: Undo2,
|
|
662
|
+
redo: Redo2,
|
|
663
|
+
zoomIn: ZoomIn,
|
|
664
|
+
zoomOut: ZoomOut,
|
|
665
|
+
fit: Maximize,
|
|
666
|
+
focus: Focus,
|
|
667
|
+
menu: Menu,
|
|
668
|
+
eye: Eye,
|
|
669
|
+
eyeOff: EyeOff,
|
|
670
|
+
expand: ChevronRight,
|
|
671
|
+
collapse: ChevronDown,
|
|
672
|
+
layers: Layers,
|
|
673
|
+
add: Plus,
|
|
674
|
+
delete: Trash,
|
|
675
|
+
copy: Copy,
|
|
676
|
+
cut: Scissors,
|
|
677
|
+
paste: ClipboardPaste,
|
|
678
|
+
group: Group,
|
|
679
|
+
ungroup: Ungroup,
|
|
680
|
+
flipHorizontal: ArrowLeftRight,
|
|
681
|
+
flipVertical: ArrowUpDown,
|
|
682
|
+
bringToFront: ArrowUpToLine,
|
|
683
|
+
bringForward: ArrowUp,
|
|
684
|
+
sendBackward: ArrowDown,
|
|
685
|
+
sendToBack: ArrowDownToLine,
|
|
686
|
+
alignLeft: AlignStartVertical,
|
|
687
|
+
alignCenter: AlignCenterVertical,
|
|
688
|
+
alignRight: AlignEndVertical,
|
|
689
|
+
alignTop: AlignStartHorizontal,
|
|
690
|
+
alignMiddle: AlignCenterHorizontal,
|
|
691
|
+
alignBottom: AlignEndHorizontal,
|
|
692
|
+
distributeHorizontal: AlignHorizontalDistributeCenter,
|
|
693
|
+
distributeVertical: AlignVerticalDistributeCenter,
|
|
694
|
+
crop: Crop,
|
|
695
|
+
download: Download,
|
|
696
|
+
open: FolderOpen,
|
|
697
|
+
save: Save,
|
|
698
|
+
keyboard: Keyboard,
|
|
699
|
+
help: CircleQuestionMark,
|
|
700
|
+
light: Sun,
|
|
701
|
+
dark: Moon,
|
|
702
|
+
system: Monitor,
|
|
703
|
+
grid: Grid3x3,
|
|
704
|
+
gridNone: Ban,
|
|
705
|
+
gridSquare: Grid3x3,
|
|
706
|
+
gridDot: Grip,
|
|
707
|
+
gridHexPointy: Hexagon,
|
|
708
|
+
gridHexFlat: HexagonFlat,
|
|
709
|
+
magnet: Magnet,
|
|
710
|
+
close: X,
|
|
711
|
+
search: Search,
|
|
712
|
+
reset: RotateCcw,
|
|
713
|
+
settings: SlidersHorizontal,
|
|
714
|
+
more: Ellipsis,
|
|
715
|
+
check: Check,
|
|
716
|
+
textLeft: TextAlignStart,
|
|
717
|
+
textCenter: TextAlignCenter,
|
|
718
|
+
textRight: TextAlignEnd,
|
|
719
|
+
"core.frame": Frame,
|
|
720
|
+
frame: Frame,
|
|
721
|
+
fontHand: Pencil,
|
|
722
|
+
fontMono: CodeXml,
|
|
723
|
+
fontDisplay: Heading,
|
|
724
|
+
fontPicker: ALargeSmall,
|
|
725
|
+
laser: Laser,
|
|
726
|
+
map: Map$1,
|
|
727
|
+
minus: Minus,
|
|
728
|
+
zoomFit: ScanSearch,
|
|
729
|
+
zoomReset: SearchX,
|
|
730
|
+
...styleIcons
|
|
731
|
+
};
|
|
732
|
+
/** Renders an icon by id: registry override, then built-in, then a generic shape. */
|
|
733
|
+
function Icon({ name, size = 16, strokeWidth = 1 }) {
|
|
734
|
+
const { registry } = useUi();
|
|
735
|
+
const Component = registry.icons.get(name) ?? builtinIcons[name] ?? Shapes;
|
|
736
|
+
return /* @__PURE__ */ jsx(Component, {
|
|
737
|
+
size,
|
|
738
|
+
strokeWidth: strokeWidth * 24 / size,
|
|
739
|
+
"aria-hidden": true
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
// src/components/primitives.tsx
|
|
743
|
+
function cx(...names) {
|
|
744
|
+
return names.filter(Boolean).join(" ");
|
|
745
|
+
}
|
|
746
|
+
/** Square icon button with an accessible label and a tooltip that shows the shortcut. */
|
|
747
|
+
function IconButton({ icon, label, shortcut, onClick, pressed, disabled, danger, testId, className, tabIndex, children }) {
|
|
748
|
+
const { t } = useUi();
|
|
749
|
+
const keys = (typeof shortcut === "string" ? [shortcut] : shortcut ?? []).map((key) => formatShortcut(key)).filter((key) => key !== void 0 && key !== "");
|
|
750
|
+
const title = keys.length === 0 ? label : t("shortcut.hint", {
|
|
751
|
+
label,
|
|
752
|
+
shortcut: keys.join(t("shortcut.or"))
|
|
753
|
+
});
|
|
754
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
755
|
+
type: "button",
|
|
756
|
+
className: cx("nx-icon-button", danger && "nx-danger", className),
|
|
757
|
+
"aria-label": label,
|
|
758
|
+
title,
|
|
759
|
+
"aria-pressed": pressed,
|
|
760
|
+
disabled,
|
|
761
|
+
onClick,
|
|
762
|
+
"data-testid": testId,
|
|
763
|
+
tabIndex,
|
|
764
|
+
children: [/* @__PURE__ */ jsx(Icon, { name: icon }), children]
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
function Island({ children, className, label, testId, role }) {
|
|
768
|
+
return /* @__PURE__ */ jsx("div", {
|
|
769
|
+
className: cx("nx-island", className),
|
|
770
|
+
"aria-label": label,
|
|
771
|
+
role,
|
|
772
|
+
"data-testid": testId,
|
|
773
|
+
children
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
/** Formats `Mod+Shift+Z` for the platform (⌘⇧Z on macOS). */
|
|
777
|
+
function formatShortcut(shortcut) {
|
|
778
|
+
if (shortcut === void 0) return;
|
|
779
|
+
const mac = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform);
|
|
780
|
+
return shortcut.split("+").map((part) => part === "Mod" ? mac ? "⌘" : "Ctrl" : part === "Shift" && mac ? "⇧" : part === "Alt" && mac ? "⌥" : part).join(mac ? "" : "+");
|
|
781
|
+
}
|
|
782
|
+
function focusables(root) {
|
|
783
|
+
return [...root.querySelectorAll("button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])")];
|
|
784
|
+
}
|
|
785
|
+
/** Moves focus among items with the arrow keys (roving focus for toolbars and menus). */
|
|
786
|
+
function handleRovingKeys(event, orientation) {
|
|
787
|
+
const root = event.currentTarget;
|
|
788
|
+
const field = event.target;
|
|
789
|
+
if (field.tagName === "SELECT" || field.tagName === "TEXTAREA" || field instanceof HTMLInputElement && ![
|
|
790
|
+
"checkbox",
|
|
791
|
+
"radio",
|
|
792
|
+
"button"
|
|
793
|
+
].includes(field.type)) return;
|
|
794
|
+
const items = focusables(root);
|
|
795
|
+
const index = items.indexOf(document.activeElement);
|
|
796
|
+
const next = orientation === "horizontal" ? "ArrowRight" : "ArrowDown";
|
|
797
|
+
const previous = orientation === "horizontal" ? "ArrowLeft" : "ArrowUp";
|
|
798
|
+
let target;
|
|
799
|
+
if (event.key === next) target = (index + 1) % items.length;
|
|
800
|
+
else if (event.key === previous) target = index < 0 ? items.length - 1 : (index - 1 + items.length) % items.length;
|
|
801
|
+
else if (event.key === "Home") target = 0;
|
|
802
|
+
else if (event.key === "End") target = items.length - 1;
|
|
803
|
+
if (target !== void 0 && items[target] !== void 0) {
|
|
804
|
+
event.preventDefault();
|
|
805
|
+
items[target]?.focus();
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
function Portal({ children }) {
|
|
809
|
+
const { portalContainer } = useUi();
|
|
810
|
+
return portalContainer === null ? null : createPortal(children, portalContainer);
|
|
811
|
+
}
|
|
812
|
+
function useDismiss(open, refs, onClose) {
|
|
813
|
+
const onPointer = useEffectEvent((event) => {
|
|
814
|
+
const target = event.target;
|
|
815
|
+
const nested = (target instanceof Element ? target : target.parentElement)?.closest(".nx-popover");
|
|
816
|
+
const own = refs[0]?.current;
|
|
817
|
+
if (!(nested != null && own != null && nested !== own && (own.compareDocumentPosition(nested) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0) && !refs.some((ref) => ref.current?.contains(target))) onClose();
|
|
818
|
+
});
|
|
819
|
+
const onKey = useEffectEvent((event) => {
|
|
820
|
+
if (event.key === "Escape") {
|
|
821
|
+
event.stopPropagation();
|
|
822
|
+
onClose();
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
useEffect(() => {
|
|
826
|
+
if (!open) return;
|
|
827
|
+
const pointer = (event) => onPointer(event);
|
|
828
|
+
const key = (event) => onKey(event);
|
|
829
|
+
document.addEventListener("pointerdown", pointer, true);
|
|
830
|
+
document.addEventListener("keydown", key, true);
|
|
831
|
+
return () => {
|
|
832
|
+
document.removeEventListener("pointerdown", pointer, true);
|
|
833
|
+
document.removeEventListener("keydown", key, true);
|
|
834
|
+
};
|
|
835
|
+
}, [open]);
|
|
836
|
+
}
|
|
837
|
+
/** Floating panel next to an anchor, portaled into the editor root. */
|
|
838
|
+
function Popover({ open, onClose, anchor, placement = "bottom-start", label, role = "dialog", testId, children }) {
|
|
839
|
+
const { portalContainer } = useUi();
|
|
840
|
+
const ref = useRef(null);
|
|
841
|
+
const [style, setStyle] = useState({ visibility: "hidden" });
|
|
842
|
+
useDismiss(open, [ref, anchor], onClose);
|
|
843
|
+
useLayoutEffect(() => {
|
|
844
|
+
if (!open || anchor.current === null || portalContainer === null || ref.current === null) return;
|
|
845
|
+
const container = portalContainer.getBoundingClientRect();
|
|
846
|
+
const box = anchor.current.getBoundingClientRect();
|
|
847
|
+
const panel = ref.current.getBoundingClientRect();
|
|
848
|
+
let left = placement === "right-start" ? box.right + 8 : placement === "bottom-end" ? box.right - panel.width : box.left;
|
|
849
|
+
let top = placement === "right-start" ? box.top : placement === "top-start" ? box.top - panel.height - 8 : box.bottom + 8;
|
|
850
|
+
left = Math.max(container.left + 8, Math.min(left, container.right - panel.width - 8));
|
|
851
|
+
top = Math.max(container.top + 8, Math.min(top, container.bottom - panel.height - 8));
|
|
852
|
+
setStyle({
|
|
853
|
+
left: left - container.left,
|
|
854
|
+
top: top - container.top
|
|
855
|
+
});
|
|
856
|
+
}, [
|
|
857
|
+
open,
|
|
858
|
+
anchor,
|
|
859
|
+
portalContainer,
|
|
860
|
+
placement
|
|
861
|
+
]);
|
|
862
|
+
const positioned = style.visibility !== "hidden";
|
|
863
|
+
useEffect(() => {
|
|
864
|
+
if (open && positioned) (ref.current?.querySelector("[data-autofocus]:not(:disabled)") ?? focusables(ref.current ?? document.body)[0])?.focus();
|
|
865
|
+
}, [open, positioned]);
|
|
866
|
+
if (!open) return null;
|
|
867
|
+
return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx("div", {
|
|
868
|
+
ref,
|
|
869
|
+
className: "nx-popover nx-island",
|
|
870
|
+
role,
|
|
871
|
+
"aria-label": label,
|
|
872
|
+
style,
|
|
873
|
+
"data-testid": testId,
|
|
874
|
+
onKeyDown: role === "menu" ? (event) => handleRovingKeys(event, "vertical") : void 0,
|
|
875
|
+
children
|
|
876
|
+
}) });
|
|
877
|
+
}
|
|
878
|
+
function MenuItems({ entries, onDone }) {
|
|
879
|
+
return entries.map((entry) => /* @__PURE__ */ jsxs("div", {
|
|
880
|
+
className: "nx-menu-group",
|
|
881
|
+
children: [entry.separatorBefore === true && /* @__PURE__ */ jsx("div", {
|
|
882
|
+
className: "nx-separator",
|
|
883
|
+
role: "separator"
|
|
884
|
+
}), /* @__PURE__ */ jsxs("button", {
|
|
885
|
+
type: "button",
|
|
886
|
+
role: entry.checked === void 0 ? "menuitem" : "menuitemcheckbox",
|
|
887
|
+
...entry.checked === void 0 ? {} : { "aria-checked": entry.checked },
|
|
888
|
+
className: cx("nx-menu-item", entry.danger && "nx-danger"),
|
|
889
|
+
disabled: entry.disabled,
|
|
890
|
+
"data-testid": `menu-item-${entry.id}`,
|
|
891
|
+
onClick: () => {
|
|
892
|
+
onDone();
|
|
893
|
+
entry.onSelect();
|
|
894
|
+
},
|
|
895
|
+
children: [
|
|
896
|
+
entry.checked === true ? /* @__PURE__ */ jsx(Icon, { name: "check" }) : entry.icon === void 0 ? /* @__PURE__ */ jsx("span", { className: "nx-menu-icon" }) : /* @__PURE__ */ jsx(Icon, { name: entry.icon }),
|
|
897
|
+
/* @__PURE__ */ jsx("span", {
|
|
898
|
+
className: "nx-menu-label",
|
|
899
|
+
children: entry.label
|
|
900
|
+
}),
|
|
901
|
+
entry.shortcut !== void 0 && /* @__PURE__ */ jsx("kbd", { children: formatShortcut(entry.shortcut) })
|
|
902
|
+
]
|
|
903
|
+
})]
|
|
904
|
+
}, entry.id));
|
|
905
|
+
}
|
|
906
|
+
/** Modal dialog inside the editor: traps focus, closes on Escape, restores focus. */
|
|
907
|
+
function Dialog({ open, onClose, title, testId, wide, children }) {
|
|
908
|
+
const { t } = useUi();
|
|
909
|
+
const ref = useRef(null);
|
|
910
|
+
const titleId = useId();
|
|
911
|
+
const close = useEffectEvent(() => onClose());
|
|
912
|
+
useEffect(() => {
|
|
913
|
+
if (!open) return;
|
|
914
|
+
const previous = document.activeElement;
|
|
915
|
+
focusables(ref.current ?? document.body)[0]?.focus();
|
|
916
|
+
const onDocumentKey = (event) => {
|
|
917
|
+
if (event.key === "Escape" && !event.defaultPrevented && !(event.target instanceof Node && ref.current?.contains(event.target) === true)) close();
|
|
918
|
+
};
|
|
919
|
+
document.addEventListener("keydown", onDocumentKey);
|
|
920
|
+
return () => {
|
|
921
|
+
document.removeEventListener("keydown", onDocumentKey);
|
|
922
|
+
previous?.focus?.();
|
|
923
|
+
};
|
|
924
|
+
}, [open]);
|
|
925
|
+
if (!open) return null;
|
|
926
|
+
const onKeyDown = (event) => {
|
|
927
|
+
if (event.key === "Escape") {
|
|
928
|
+
event.stopPropagation();
|
|
929
|
+
onClose();
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
if (event.key !== "Tab" || ref.current === null) return;
|
|
933
|
+
const items = focusables(ref.current);
|
|
934
|
+
const first = items[0];
|
|
935
|
+
const last = items.at(-1);
|
|
936
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
937
|
+
event.preventDefault();
|
|
938
|
+
last?.focus();
|
|
939
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
940
|
+
event.preventDefault();
|
|
941
|
+
first?.focus();
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx("div", {
|
|
945
|
+
className: "nx-dialog-backdrop",
|
|
946
|
+
onPointerDown: (event) => {
|
|
947
|
+
if (event.target === event.currentTarget) onClose();
|
|
948
|
+
},
|
|
949
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
950
|
+
ref,
|
|
951
|
+
className: cx("nx-dialog nx-island", wide && "nx-dialog-wide"),
|
|
952
|
+
role: "dialog",
|
|
953
|
+
"aria-modal": "true",
|
|
954
|
+
"aria-labelledby": titleId,
|
|
955
|
+
"data-testid": testId,
|
|
956
|
+
onKeyDown,
|
|
957
|
+
children: [/* @__PURE__ */ jsxs("header", {
|
|
958
|
+
className: "nx-dialog-header",
|
|
959
|
+
children: [/* @__PURE__ */ jsx("h2", {
|
|
960
|
+
id: titleId,
|
|
961
|
+
children: title
|
|
962
|
+
}), /* @__PURE__ */ jsx(IconButton, {
|
|
963
|
+
icon: "close",
|
|
964
|
+
label: t("dialog.close"),
|
|
965
|
+
onClick: onClose,
|
|
966
|
+
testId: "dialog-close"
|
|
967
|
+
})]
|
|
968
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
969
|
+
className: "nx-dialog-body",
|
|
970
|
+
children
|
|
971
|
+
})]
|
|
972
|
+
})
|
|
973
|
+
}) });
|
|
974
|
+
}
|
|
975
|
+
/** Row of toggle buttons for one value; `undefined` value shows no selection (mixed). */
|
|
976
|
+
function Choice({ label, value, options, onChange, testId, disabled }) {
|
|
977
|
+
return /* @__PURE__ */ jsx("div", {
|
|
978
|
+
className: "nx-choice",
|
|
979
|
+
role: "radiogroup",
|
|
980
|
+
"aria-label": label,
|
|
981
|
+
"data-testid": testId,
|
|
982
|
+
onKeyDown: (event) => handleRovingKeys(event, "horizontal"),
|
|
983
|
+
children: options.map((option) => /* @__PURE__ */ jsx("button", {
|
|
984
|
+
type: "button",
|
|
985
|
+
role: "radio",
|
|
986
|
+
"aria-checked": value === option.value,
|
|
987
|
+
"aria-label": option.label,
|
|
988
|
+
title: option.label,
|
|
989
|
+
disabled,
|
|
990
|
+
className: cx("nx-choice-option", option.swatch !== void 0 && "nx-swatch-option", (option.icon !== void 0 || option.glyph !== void 0) && "nx-choice-square"),
|
|
991
|
+
"data-value": String(option.value),
|
|
992
|
+
onClick: () => onChange(option.value),
|
|
993
|
+
children: option.swatch !== void 0 ? /* @__PURE__ */ jsx("span", {
|
|
994
|
+
className: cx("nx-swatch", option.swatch === "transparent" && "nx-swatch-none"),
|
|
995
|
+
style: option.swatch === "transparent" ? void 0 : { background: option.swatch }
|
|
996
|
+
}) : option.icon !== void 0 ? /* @__PURE__ */ jsx(Icon, { name: option.icon }) : option.glyph !== void 0 ? /* @__PURE__ */ jsx("span", {
|
|
997
|
+
className: "nx-choice-glyph",
|
|
998
|
+
"aria-hidden": true,
|
|
999
|
+
children: option.glyph
|
|
1000
|
+
}) : /* @__PURE__ */ jsx("span", {
|
|
1001
|
+
className: "nx-choice-text",
|
|
1002
|
+
children: option.label
|
|
1003
|
+
})
|
|
1004
|
+
}, String(option.value)))
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
function Section({ title, children }) {
|
|
1008
|
+
const id = useId();
|
|
1009
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
1010
|
+
className: "nx-section",
|
|
1011
|
+
"aria-labelledby": id,
|
|
1012
|
+
children: [/* @__PURE__ */ jsx("h3", {
|
|
1013
|
+
id,
|
|
1014
|
+
className: "nx-section-title",
|
|
1015
|
+
children: title
|
|
1016
|
+
}), children]
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
// src/features/links/links.tsx
|
|
1020
|
+
/** Opens the link editor for the single selected element (Mod+K, context menu, properties). */
|
|
1021
|
+
/** Screen gap between the element and the popover; clears the rotation handle. */
|
|
1022
|
+
const POPOVER_GAP = 44;
|
|
1023
|
+
/** Approximate popover height, used to decide whether it fits above the element. */
|
|
1024
|
+
const POPOVER_HEIGHT = 44;
|
|
1025
|
+
const VIEW_MARGIN = 8;
|
|
1026
|
+
function LinkInput({ id, onDone }) {
|
|
1027
|
+
const editor = useEditor();
|
|
1028
|
+
const { t } = useUi();
|
|
1029
|
+
const current = editor.links.get(id);
|
|
1030
|
+
const [draft, setDraft] = useState(current ?? "");
|
|
1031
|
+
const [error, setError] = useState(false);
|
|
1032
|
+
const finished = useRef(false);
|
|
1033
|
+
/** Saves the draft (empty removes the link); false when the link is rejected. */
|
|
1034
|
+
const commit = () => {
|
|
1035
|
+
const trimmed = draft.trim();
|
|
1036
|
+
const next = trimmed === "" ? void 0 : trimmed;
|
|
1037
|
+
if (next === current) return true;
|
|
1038
|
+
try {
|
|
1039
|
+
editor.links.set(id, next);
|
|
1040
|
+
return true;
|
|
1041
|
+
} catch {
|
|
1042
|
+
setError(true);
|
|
1043
|
+
return false;
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
const finish = () => {
|
|
1047
|
+
finished.current = true;
|
|
1048
|
+
onDone();
|
|
1049
|
+
};
|
|
1050
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("input", {
|
|
1051
|
+
type: "text",
|
|
1052
|
+
autoFocus: true,
|
|
1053
|
+
className: cx("nx-link-input", error && "nx-invalid"),
|
|
1054
|
+
value: draft,
|
|
1055
|
+
placeholder: t("link.placeholder"),
|
|
1056
|
+
"aria-label": t("link.label"),
|
|
1057
|
+
"aria-invalid": error,
|
|
1058
|
+
"data-testid": "link-input",
|
|
1059
|
+
onChange: (event) => {
|
|
1060
|
+
setDraft(event.target.value);
|
|
1061
|
+
setError(false);
|
|
1062
|
+
},
|
|
1063
|
+
onKeyDown: (event) => {
|
|
1064
|
+
if (event.key === "Enter") {
|
|
1065
|
+
event.preventDefault();
|
|
1066
|
+
if (commit()) finish();
|
|
1067
|
+
} else if (event.key === "Escape") {
|
|
1068
|
+
event.preventDefault();
|
|
1069
|
+
event.stopPropagation();
|
|
1070
|
+
finish();
|
|
1071
|
+
}
|
|
1072
|
+
},
|
|
1073
|
+
onBlur: () => {
|
|
1074
|
+
if (!finished.current) {
|
|
1075
|
+
commit();
|
|
1076
|
+
finish();
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
}), error && /* @__PURE__ */ jsx("p", {
|
|
1080
|
+
className: "nx-link-error",
|
|
1081
|
+
role: "alert",
|
|
1082
|
+
children: t("link.invalid")
|
|
1083
|
+
})] });
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Excalidraw-style link editor: a popover above the single selected element. It shows the
|
|
1087
|
+
* element's link (click to open) with edit and remove buttons, or an input while editing
|
|
1088
|
+
* (Mod+K, "Edit link…" in the context menu, the properties action). Enter or clicking away
|
|
1089
|
+
* saves, Escape cancels, an empty link removes it.
|
|
1090
|
+
*/
|
|
1091
|
+
function LinkEditor() {
|
|
1092
|
+
const editor = useEditor();
|
|
1093
|
+
const { t, isOpen, setOpen } = useUi();
|
|
1094
|
+
const selection = useSelection();
|
|
1095
|
+
const id = selection.length === 1 ? selection[0] : void 0;
|
|
1096
|
+
const element = useElement(id);
|
|
1097
|
+
const canEdit = usePermission("core.edit.content", id);
|
|
1098
|
+
const busy = useEditorValue((current) => current.interactions.active() !== void 0 || current.text.editing() !== void 0, { events: [
|
|
1099
|
+
"interaction.start",
|
|
1100
|
+
"interaction.commit",
|
|
1101
|
+
"interaction.cancel",
|
|
1102
|
+
"text.editing"
|
|
1103
|
+
] });
|
|
1104
|
+
const ref = useRef(null);
|
|
1105
|
+
const editing = isOpen("link") && id !== void 0 && canEdit;
|
|
1106
|
+
const url = typeof element?.link === "string" ? element.link : void 0;
|
|
1107
|
+
const shown = id !== void 0 && element !== void 0 && !busy && (editing || url !== void 0);
|
|
1108
|
+
const viewport = useEditorValue((current) => shown ? current.viewport.get() : void 0, { events: ["viewport.change"] }) ?? editor.viewport.get();
|
|
1109
|
+
useEffect(() => {
|
|
1110
|
+
const offCommand = editor.commands.register({
|
|
1111
|
+
id: LINK_COMMAND,
|
|
1112
|
+
label: t("link.edit"),
|
|
1113
|
+
canRun: () => {
|
|
1114
|
+
const selected = editor.selection.get();
|
|
1115
|
+
return selected.length === 1 && selected[0] !== void 0 && editor.permissions.can("core.edit.content", selected[0]);
|
|
1116
|
+
},
|
|
1117
|
+
run: () => setOpen("link", true)
|
|
1118
|
+
}, { replace: true });
|
|
1119
|
+
const offKey = editor.input.keymap.bind("Mod+K", LINK_COMMAND);
|
|
1120
|
+
return () => {
|
|
1121
|
+
offKey();
|
|
1122
|
+
offCommand();
|
|
1123
|
+
};
|
|
1124
|
+
}, [
|
|
1125
|
+
editor,
|
|
1126
|
+
setOpen,
|
|
1127
|
+
t
|
|
1128
|
+
]);
|
|
1129
|
+
useEffect(() => {
|
|
1130
|
+
const off = editor.events.on("selection.change", () => setOpen("link", false));
|
|
1131
|
+
return () => {
|
|
1132
|
+
off();
|
|
1133
|
+
setOpen("link", false);
|
|
1134
|
+
};
|
|
1135
|
+
}, [editor, setOpen]);
|
|
1136
|
+
const bounds = element === void 0 ? void 0 : editor.elements.boundsFor(element, "visual");
|
|
1137
|
+
const centerX = bounds === void 0 ? 0 : ((bounds.minX + bounds.maxX) / 2 - viewport.x) * viewport.zoom;
|
|
1138
|
+
const top = bounds === void 0 ? 0 : (bounds.minY - viewport.y) * viewport.zoom;
|
|
1139
|
+
const bottom = bounds === void 0 ? 0 : (bounds.maxY - viewport.y) * viewport.zoom;
|
|
1140
|
+
const above = top - POPOVER_GAP - POPOVER_HEIGHT >= VIEW_MARGIN;
|
|
1141
|
+
useLayoutEffect(() => {
|
|
1142
|
+
const node = ref.current;
|
|
1143
|
+
const parent = node?.offsetParent;
|
|
1144
|
+
if (node === null || !(parent instanceof HTMLElement)) return;
|
|
1145
|
+
const half = node.offsetWidth / 2;
|
|
1146
|
+
const left = Math.max(half + VIEW_MARGIN, Math.min(centerX, parent.clientWidth - half - VIEW_MARGIN));
|
|
1147
|
+
node.style.left = `${left}px`;
|
|
1148
|
+
});
|
|
1149
|
+
if (!shown) return null;
|
|
1150
|
+
const openable = url !== void 0 && editor.links.canOpen(url, { elementId: id });
|
|
1151
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1152
|
+
ref,
|
|
1153
|
+
className: "nx-link-popover nx-island",
|
|
1154
|
+
role: "group",
|
|
1155
|
+
"aria-label": t("link.label"),
|
|
1156
|
+
"data-testid": "link-editor",
|
|
1157
|
+
style: {
|
|
1158
|
+
left: centerX,
|
|
1159
|
+
top: above ? top - POPOVER_GAP : bottom + POPOVER_GAP,
|
|
1160
|
+
transform: above ? "translate(-50%, -100%)" : "translate(-50%, 0)"
|
|
1161
|
+
},
|
|
1162
|
+
children: editing ? /* @__PURE__ */ jsx(LinkInput, {
|
|
1163
|
+
id,
|
|
1164
|
+
onDone: () => setOpen("link", false)
|
|
1165
|
+
}, id) : /* @__PURE__ */ jsxs(Fragment$1, { children: [openable ? /* @__PURE__ */ jsx("a", {
|
|
1166
|
+
className: "nx-link-url",
|
|
1167
|
+
href: url,
|
|
1168
|
+
title: url,
|
|
1169
|
+
"data-testid": "link-url",
|
|
1170
|
+
onClick: (event) => {
|
|
1171
|
+
event.preventDefault();
|
|
1172
|
+
editor.links.open(id, { source: "pointer" });
|
|
1173
|
+
},
|
|
1174
|
+
children: url
|
|
1175
|
+
}) : /* @__PURE__ */ jsx("span", {
|
|
1176
|
+
className: "nx-link-url nx-inert",
|
|
1177
|
+
title: url,
|
|
1178
|
+
"data-testid": "link-url",
|
|
1179
|
+
children: url
|
|
1180
|
+
}), canEdit && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(IconButton, {
|
|
1181
|
+
icon: "edit",
|
|
1182
|
+
label: t("link.change"),
|
|
1183
|
+
onClick: () => setOpen("link", true),
|
|
1184
|
+
testId: "link-edit"
|
|
1185
|
+
}), /* @__PURE__ */ jsx(IconButton, {
|
|
1186
|
+
icon: "delete",
|
|
1187
|
+
label: t("link.remove"),
|
|
1188
|
+
danger: true,
|
|
1189
|
+
onClick: () => editor.links.set(id, void 0),
|
|
1190
|
+
testId: "link-remove"
|
|
1191
|
+
})] })] })
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
/** @deprecated Renamed to {@link LinkEditor}; links are edited in a popover now. */
|
|
1195
|
+
const LinkDialog = LinkEditor;
|
|
1196
|
+
// src/features/controls/controls.tsx
|
|
1197
|
+
function UndoRedo() {
|
|
1198
|
+
const { t } = useUi();
|
|
1199
|
+
const undo = useCommand("core.undo");
|
|
1200
|
+
const redo = useCommand("core.redo");
|
|
1201
|
+
return /* @__PURE__ */ jsxs(Island, {
|
|
1202
|
+
className: "nx-row",
|
|
1203
|
+
testId: "undo-redo",
|
|
1204
|
+
children: [/* @__PURE__ */ jsx(IconButton, {
|
|
1205
|
+
icon: "undo",
|
|
1206
|
+
label: t("history.undo"),
|
|
1207
|
+
shortcut: undo.shortcuts,
|
|
1208
|
+
disabled: !undo.enabled,
|
|
1209
|
+
onClick: () => undo.run(),
|
|
1210
|
+
testId: "undo"
|
|
1211
|
+
}), /* @__PURE__ */ jsx(IconButton, {
|
|
1212
|
+
icon: "redo",
|
|
1213
|
+
label: t("history.redo"),
|
|
1214
|
+
shortcut: redo.shortcuts,
|
|
1215
|
+
disabled: !redo.enabled,
|
|
1216
|
+
onClick: () => redo.run(),
|
|
1217
|
+
testId: "redo"
|
|
1218
|
+
})]
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
function ZoomControls({ minimap } = {}) {
|
|
1222
|
+
const { t, isOpen, setOpen } = useUi();
|
|
1223
|
+
const zoom = useEditorValue((current) => current.viewport.get().zoom, { events: ["viewport.change"] });
|
|
1224
|
+
const zoomIn = useCommand("core.zoomIn");
|
|
1225
|
+
const zoomOut = useCommand("core.zoomOut");
|
|
1226
|
+
const reset = useCommand("core.resetZoom");
|
|
1227
|
+
const fit = useCommand("core.zoomToFit");
|
|
1228
|
+
const toSelection = useCommand("core.zoomToSelection");
|
|
1229
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
1230
|
+
const anchor = useRef(null);
|
|
1231
|
+
const percent = Math.round(zoom * 100);
|
|
1232
|
+
const minimapOpen = minimap !== void 0 && isOpen("minimap");
|
|
1233
|
+
const entries = [
|
|
1234
|
+
{
|
|
1235
|
+
id: "zoom-in",
|
|
1236
|
+
label: t("zoom.in"),
|
|
1237
|
+
icon: "add",
|
|
1238
|
+
shortcut: zoomIn.shortcuts[0],
|
|
1239
|
+
onSelect: () => zoomIn.run()
|
|
1240
|
+
},
|
|
1241
|
+
{
|
|
1242
|
+
id: "zoom-out",
|
|
1243
|
+
label: t("zoom.out"),
|
|
1244
|
+
icon: "minus",
|
|
1245
|
+
shortcut: zoomOut.shortcuts[0],
|
|
1246
|
+
onSelect: () => zoomOut.run()
|
|
1247
|
+
},
|
|
1248
|
+
{
|
|
1249
|
+
id: "zoom-fit",
|
|
1250
|
+
label: t("zoom.fit"),
|
|
1251
|
+
icon: "zoomFit",
|
|
1252
|
+
shortcut: fit.shortcuts[0],
|
|
1253
|
+
separatorBefore: true,
|
|
1254
|
+
onSelect: () => fit.run()
|
|
1255
|
+
},
|
|
1256
|
+
{
|
|
1257
|
+
id: "zoom-selection",
|
|
1258
|
+
label: t("zoom.toSelection"),
|
|
1259
|
+
icon: "zoomFit",
|
|
1260
|
+
shortcut: toSelection.shortcuts[0],
|
|
1261
|
+
disabled: !toSelection.enabled,
|
|
1262
|
+
onSelect: () => toSelection.run()
|
|
1263
|
+
},
|
|
1264
|
+
{
|
|
1265
|
+
id: "zoom-reset",
|
|
1266
|
+
label: t("zoom.reset"),
|
|
1267
|
+
icon: "zoomReset",
|
|
1268
|
+
shortcut: reset.shortcuts[0],
|
|
1269
|
+
onSelect: () => reset.run()
|
|
1270
|
+
}
|
|
1271
|
+
];
|
|
1272
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1273
|
+
className: "nx-zoom",
|
|
1274
|
+
children: [
|
|
1275
|
+
minimapOpen && /* @__PURE__ */ jsx("div", {
|
|
1276
|
+
className: "nx-minimap-card",
|
|
1277
|
+
children: minimap
|
|
1278
|
+
}),
|
|
1279
|
+
/* @__PURE__ */ jsxs(Island, {
|
|
1280
|
+
className: "nx-row",
|
|
1281
|
+
testId: "zoom-controls",
|
|
1282
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
1283
|
+
ref: anchor,
|
|
1284
|
+
type: "button",
|
|
1285
|
+
className: "nx-zoom-level",
|
|
1286
|
+
"aria-haspopup": "menu",
|
|
1287
|
+
"aria-expanded": menuOpen,
|
|
1288
|
+
"aria-label": `${t("zoom.menu")} (${t("zoom.level", { percent })})`,
|
|
1289
|
+
title: t("zoom.menu"),
|
|
1290
|
+
onClick: () => setMenuOpen(!menuOpen),
|
|
1291
|
+
"data-testid": "zoom-menu",
|
|
1292
|
+
children: t("zoom.level", { percent })
|
|
1293
|
+
}), minimap !== void 0 && /* @__PURE__ */ jsx(IconButton, {
|
|
1294
|
+
icon: "map",
|
|
1295
|
+
label: t("minimap.toggle"),
|
|
1296
|
+
pressed: minimapOpen,
|
|
1297
|
+
onClick: () => setOpen("minimap", !minimapOpen),
|
|
1298
|
+
className: "nx-minimap-toggle",
|
|
1299
|
+
testId: "minimap-toggle"
|
|
1300
|
+
})]
|
|
1301
|
+
}),
|
|
1302
|
+
/* @__PURE__ */ jsx(Popover, {
|
|
1303
|
+
open: menuOpen,
|
|
1304
|
+
onClose: () => setMenuOpen(false),
|
|
1305
|
+
anchor,
|
|
1306
|
+
placement: "top-start",
|
|
1307
|
+
role: "menu",
|
|
1308
|
+
label: t("zoom.menu"),
|
|
1309
|
+
testId: "zoom-menu-popover",
|
|
1310
|
+
children: /* @__PURE__ */ jsx(MenuItems, {
|
|
1311
|
+
entries,
|
|
1312
|
+
onDone: () => setMenuOpen(false)
|
|
1313
|
+
})
|
|
1314
|
+
})
|
|
1315
|
+
]
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
/** Shown when content exists but none of it is in view. */
|
|
1319
|
+
/** Centers the content at 100% zoom, like returning to the drawing where it was made. */
|
|
1320
|
+
function scrollToContent(editor) {
|
|
1321
|
+
const content = editor.elements.contentBounds();
|
|
1322
|
+
if (content === void 0) return Promise.resolve();
|
|
1323
|
+
const { zoom } = editor.viewport.get();
|
|
1324
|
+
const visible = editor.viewport.visibleBounds();
|
|
1325
|
+
const width = (visible.maxX - visible.minX) * zoom;
|
|
1326
|
+
const height = (visible.maxY - visible.minY) * zoom;
|
|
1327
|
+
return editor.viewport.set({
|
|
1328
|
+
zoom: 1,
|
|
1329
|
+
x: (content.minX + content.maxX) / 2 - width / 2,
|
|
1330
|
+
y: (content.minY + content.maxY) / 2 - height / 2
|
|
1331
|
+
}, { animate: { duration: 300 } });
|
|
1332
|
+
}
|
|
1333
|
+
function ScrollToContent() {
|
|
1334
|
+
const editor = useEditor();
|
|
1335
|
+
const { t } = useUi();
|
|
1336
|
+
const revision = useEditorValue((current) => current.document.revision, { events: [
|
|
1337
|
+
"document.change",
|
|
1338
|
+
"document.load",
|
|
1339
|
+
"permissions.change",
|
|
1340
|
+
"layer.change"
|
|
1341
|
+
] });
|
|
1342
|
+
const content = useMemo(() => revision >= 0 ? editor.elements.contentBounds() : void 0, [editor, revision]);
|
|
1343
|
+
if (!useEditorValue((current) => {
|
|
1344
|
+
if (content === void 0) return false;
|
|
1345
|
+
const visible = current.viewport.visibleBounds();
|
|
1346
|
+
return content.maxX < visible.minX || content.minX > visible.maxX || content.maxY < visible.minY || content.minY > visible.maxY;
|
|
1347
|
+
}, { events: ["viewport.change"] })) return null;
|
|
1348
|
+
return /* @__PURE__ */ jsx("button", {
|
|
1349
|
+
type: "button",
|
|
1350
|
+
className: "nx-pill",
|
|
1351
|
+
onClick: () => void scrollToContent(editor),
|
|
1352
|
+
"data-testid": "scroll-to-content",
|
|
1353
|
+
children: t("scroll.toContent")
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
function LaserButton() {
|
|
1357
|
+
const editor = useEditor();
|
|
1358
|
+
const active = useActiveTool();
|
|
1359
|
+
const { t } = useUi();
|
|
1360
|
+
if (!useEditorValue((current) => current.tools.has("core.laser") && current.tools.isAvailable("core.laser"), { events: [
|
|
1361
|
+
"mode.change",
|
|
1362
|
+
"permissions.change",
|
|
1363
|
+
"tool.change"
|
|
1364
|
+
] })) return null;
|
|
1365
|
+
return /* @__PURE__ */ jsx(IconButton, {
|
|
1366
|
+
icon: "laser",
|
|
1367
|
+
label: t("laser.label"),
|
|
1368
|
+
shortcut: "K",
|
|
1369
|
+
pressed: active === "core.laser",
|
|
1370
|
+
onClick: () => editor.tools.activate(active === "core.laser" ? "core.select" : "core.laser"),
|
|
1371
|
+
testId: "laser-button"
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
function ZenToggle() {
|
|
1375
|
+
const { t } = useUi();
|
|
1376
|
+
const presentation = usePresentation();
|
|
1377
|
+
const zen = useCommand("core.zen.toggle");
|
|
1378
|
+
return /* @__PURE__ */ jsx(IconButton, {
|
|
1379
|
+
icon: "focus",
|
|
1380
|
+
label: presentation.zen ? t("zen.exit") : t("zen.enter"),
|
|
1381
|
+
shortcut: zen.shortcuts,
|
|
1382
|
+
pressed: presentation.zen,
|
|
1383
|
+
onClick: () => zen.run(),
|
|
1384
|
+
testId: "zen-toggle"
|
|
1385
|
+
});
|
|
1386
|
+
}
|
|
1387
|
+
function GridControls() {
|
|
1388
|
+
const { t } = useUi();
|
|
1389
|
+
const grid = useGrid();
|
|
1390
|
+
const toggle = useCommand("core.grid.toggle");
|
|
1391
|
+
return /* @__PURE__ */ jsx(IconButton, {
|
|
1392
|
+
icon: "grid",
|
|
1393
|
+
label: t("grid.toggle"),
|
|
1394
|
+
shortcut: toggle.shortcuts,
|
|
1395
|
+
pressed: grid.visible,
|
|
1396
|
+
disabled: !toggle.enabled,
|
|
1397
|
+
onClick: () => toggle.run(),
|
|
1398
|
+
testId: "grid-toggle"
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
// src/features/layers/layers-tree.ts
|
|
1402
|
+
const layerKey = (id) => `layer:${id}`;
|
|
1403
|
+
const groupKey = (layerId, id) => `group:${layerId}:${id}`;
|
|
1404
|
+
const elementKey = (id) => `element:${id}`;
|
|
1405
|
+
/**
|
|
1406
|
+
* Flattens layers (top layer first), groups and elements (topmost first) into visible rows.
|
|
1407
|
+
* A group appears in each layer that holds some of its members, at its topmost member.
|
|
1408
|
+
*/
|
|
1409
|
+
function buildLayerRows(source, options) {
|
|
1410
|
+
const query = options.query?.trim().toLowerCase() ?? "";
|
|
1411
|
+
const filtering = query !== "" || options.type !== void 0;
|
|
1412
|
+
const matchesText = (text) => query === "" || (text ?? "").toLowerCase().includes(query);
|
|
1413
|
+
const buildLayer = (node) => {
|
|
1414
|
+
const layer = node.layer;
|
|
1415
|
+
const children = [...node.children].reverse().map(buildLayer);
|
|
1416
|
+
const groupNodes = /* @__PURE__ */ new Map();
|
|
1417
|
+
const skipElements = !filtering && options.collapsed.has(layerKey(layer.id));
|
|
1418
|
+
const elements = skipElements ? [] : source.elementsOf(layer.id);
|
|
1419
|
+
for (const element of elements) {
|
|
1420
|
+
const description = source.describe(element);
|
|
1421
|
+
const matches = matchesText(description.label) && (options.type === void 0 || element.type === options.type);
|
|
1422
|
+
let parent = children;
|
|
1423
|
+
const chain = [...source.groupsOf(element.id)].reverse();
|
|
1424
|
+
for (const groupId of chain) {
|
|
1425
|
+
let groupNode = groupNodes.get(groupId);
|
|
1426
|
+
if (groupNode === void 0) {
|
|
1427
|
+
const record = source.group(groupId);
|
|
1428
|
+
if (record === void 0) continue;
|
|
1429
|
+
const members = [];
|
|
1430
|
+
const created = {
|
|
1431
|
+
row: {
|
|
1432
|
+
kind: "group",
|
|
1433
|
+
key: groupKey(layer.id, groupId),
|
|
1434
|
+
record,
|
|
1435
|
+
layerId: layer.id,
|
|
1436
|
+
members
|
|
1437
|
+
},
|
|
1438
|
+
children: [],
|
|
1439
|
+
matches: query !== "" && matchesText(record.name),
|
|
1440
|
+
members
|
|
1441
|
+
};
|
|
1442
|
+
groupNodes.set(groupId, created);
|
|
1443
|
+
parent.push(created);
|
|
1444
|
+
groupNode = created;
|
|
1445
|
+
}
|
|
1446
|
+
groupNode.members.push(element.id);
|
|
1447
|
+
parent = groupNode.children;
|
|
1448
|
+
}
|
|
1449
|
+
parent.push({
|
|
1450
|
+
row: {
|
|
1451
|
+
kind: "element",
|
|
1452
|
+
key: elementKey(element.id),
|
|
1453
|
+
record: element,
|
|
1454
|
+
label: description.label,
|
|
1455
|
+
icon: description.icon
|
|
1456
|
+
},
|
|
1457
|
+
children: [],
|
|
1458
|
+
matches
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
return {
|
|
1462
|
+
row: {
|
|
1463
|
+
kind: "layer",
|
|
1464
|
+
key: layerKey(layer.id),
|
|
1465
|
+
record: layer,
|
|
1466
|
+
active: layer.id === source.activeLayer
|
|
1467
|
+
},
|
|
1468
|
+
children,
|
|
1469
|
+
matches: query !== "" && options.type === void 0 && matchesText(layer.name),
|
|
1470
|
+
...skipElements && source.elementsOf(layer.id).length > 0 ? { hasMore: true } : {}
|
|
1471
|
+
};
|
|
1472
|
+
};
|
|
1473
|
+
const visible = (node) => !filtering || node.matches || node.children.some(visible);
|
|
1474
|
+
const rows = [];
|
|
1475
|
+
const flatten = (node, depth) => {
|
|
1476
|
+
if (!visible(node)) return;
|
|
1477
|
+
const expandable = node.children.length > 0 || node.hasMore === true;
|
|
1478
|
+
const key = node.row.key;
|
|
1479
|
+
const expanded = expandable && (filtering || (node.row.kind === "layer" ? !options.collapsed.has(key) : options.expanded.has(key)));
|
|
1480
|
+
rows.push({
|
|
1481
|
+
...node.row,
|
|
1482
|
+
depth,
|
|
1483
|
+
expandable,
|
|
1484
|
+
expanded
|
|
1485
|
+
});
|
|
1486
|
+
if (expanded) for (const child of node.children) flatten(child, depth + 1);
|
|
1487
|
+
};
|
|
1488
|
+
for (const node of [...source.layers].reverse().map(buildLayer)) flatten(node, 0);
|
|
1489
|
+
return rows;
|
|
1490
|
+
}
|
|
1491
|
+
/**
|
|
1492
|
+
* The indent guide to emphasize for the row at `anchor`: its own children when it is expanded,
|
|
1493
|
+
* otherwise its siblings under the parent row. `end` is exclusive.
|
|
1494
|
+
*/
|
|
1495
|
+
function activeGuide(rows, anchor) {
|
|
1496
|
+
const row = rows[anchor];
|
|
1497
|
+
if (row === void 0) return;
|
|
1498
|
+
let parent = anchor;
|
|
1499
|
+
if (!row.expandable || !row.expanded) do
|
|
1500
|
+
parent -= 1;
|
|
1501
|
+
while (parent >= 0 && (rows[parent]?.depth ?? 0) >= row.depth);
|
|
1502
|
+
const level = rows[parent]?.depth;
|
|
1503
|
+
if (level === void 0) return;
|
|
1504
|
+
let end = parent + 1;
|
|
1505
|
+
while (end < rows.length && (rows[end]?.depth ?? 0) > level) end += 1;
|
|
1506
|
+
return end > parent + 1 ? {
|
|
1507
|
+
level,
|
|
1508
|
+
start: parent + 1,
|
|
1509
|
+
end
|
|
1510
|
+
} : void 0;
|
|
1511
|
+
}
|
|
1512
|
+
/**
|
|
1513
|
+
* Expanded layer rows pinned to the top of the viewport while their contents scroll past,
|
|
1514
|
+
* outermost first. `padding` is the space above the first row.
|
|
1515
|
+
*/
|
|
1516
|
+
function stickyRows(rows, scrollTop, rowStride, padding = 0) {
|
|
1517
|
+
const content = scrollTop - padding;
|
|
1518
|
+
const result = [];
|
|
1519
|
+
while (content > 0) {
|
|
1520
|
+
const depth = result.length;
|
|
1521
|
+
const probe = Math.floor((content + depth * rowStride) / rowStride);
|
|
1522
|
+
let index = Math.min(probe, rows.length - 1);
|
|
1523
|
+
while (index >= 0 && (rows[index]?.depth ?? 0) > depth) index -= 1;
|
|
1524
|
+
const row = rows[index];
|
|
1525
|
+
if (row === void 0 || row.kind !== "layer" || row.depth !== depth || !row.expanded || index * rowStride >= content + depth * rowStride) break;
|
|
1526
|
+
let end = index + 1;
|
|
1527
|
+
while (end < rows.length && (rows[end]?.depth ?? 0) > depth) end += 1;
|
|
1528
|
+
const top = Math.min(depth * rowStride, (end - 2) * rowStride - content);
|
|
1529
|
+
if (end === index + 1 || probe >= end || top + rowStride <= depth * rowStride) break;
|
|
1530
|
+
result.push({
|
|
1531
|
+
index,
|
|
1532
|
+
top
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
return result;
|
|
1536
|
+
}
|
|
1537
|
+
/** Index range of rows to render for a scroll position (inclusive start, exclusive end). */
|
|
1538
|
+
function visibleRange(scrollTop, viewportHeight, rowHeight, count, overscan = 6) {
|
|
1539
|
+
const start = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
|
|
1540
|
+
const end = Math.min(count, Math.ceil((scrollTop + viewportHeight) / rowHeight) + overscan);
|
|
1541
|
+
return {
|
|
1542
|
+
start,
|
|
1543
|
+
end: Math.max(start, end)
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
/** Tree source backed by the public editor API. */
|
|
1547
|
+
function editorTreeSource(editor) {
|
|
1548
|
+
const byLayer = /* @__PURE__ */ new Map();
|
|
1549
|
+
return {
|
|
1550
|
+
layers: editor.layers.tree(),
|
|
1551
|
+
activeLayer: editor.layers.active(),
|
|
1552
|
+
elementsOf: (layerId) => {
|
|
1553
|
+
let elements = byLayer.get(layerId);
|
|
1554
|
+
if (elements === void 0) {
|
|
1555
|
+
elements = [...editor.elements.inRenderOrder({
|
|
1556
|
+
layerId,
|
|
1557
|
+
includeHidden: true
|
|
1558
|
+
})].reverse();
|
|
1559
|
+
byLayer.set(layerId, elements);
|
|
1560
|
+
}
|
|
1561
|
+
return elements;
|
|
1562
|
+
},
|
|
1563
|
+
groupsOf: (elementId) => editor.groups.ancestors(elementId),
|
|
1564
|
+
group: (id) => editor.groups.get(id),
|
|
1565
|
+
describe: (element) => editor.elements.describe(element.id) ?? {
|
|
1566
|
+
label: element.type,
|
|
1567
|
+
icon: "element"
|
|
1568
|
+
}
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
// src/features/layers/layers.tsx
|
|
1572
|
+
const ROW_HEIGHT = 30;
|
|
1573
|
+
const ROW_GAP = 1;
|
|
1574
|
+
const ROW_STRIDE = 31;
|
|
1575
|
+
const ROW_PADDING = 4;
|
|
1576
|
+
const INDENT = 14;
|
|
1577
|
+
const TOGGLE_SIZE = 16;
|
|
1578
|
+
const TREE_PADDING = 6;
|
|
1579
|
+
const END_KEY = "end";
|
|
1580
|
+
const TREE_EVENTS = [
|
|
1581
|
+
"document.change",
|
|
1582
|
+
"document.load",
|
|
1583
|
+
"layer.change",
|
|
1584
|
+
"layer.active",
|
|
1585
|
+
"group.change",
|
|
1586
|
+
"permissions.change",
|
|
1587
|
+
"mode.change"
|
|
1588
|
+
];
|
|
1589
|
+
function rowIds(row) {
|
|
1590
|
+
return row.kind === "element" ? [row.record.id] : row.kind === "group" ? row.members : [];
|
|
1591
|
+
}
|
|
1592
|
+
function isLocked(row) {
|
|
1593
|
+
return row.record.locked;
|
|
1594
|
+
}
|
|
1595
|
+
function isHiddenRow(row) {
|
|
1596
|
+
return row.kind === "group" ? false : row.record.hidden;
|
|
1597
|
+
}
|
|
1598
|
+
function rowName(row, t) {
|
|
1599
|
+
if (row.kind === "layer") return row.record.name;
|
|
1600
|
+
if (row.kind === "group") return row.record.name ?? t("layers.group");
|
|
1601
|
+
return row.label;
|
|
1602
|
+
}
|
|
1603
|
+
function rename(editor, row, name) {
|
|
1604
|
+
const trimmed = name.trim();
|
|
1605
|
+
if (row.kind === "layer") {
|
|
1606
|
+
if (trimmed !== "") editor.layers.rename(row.record.id, trimmed);
|
|
1607
|
+
} else if (row.kind === "group") editor.groups.rename(row.record.id, trimmed);
|
|
1608
|
+
else editor.elements.update(row.record.id, { name: trimmed === "" ? void 0 : trimmed });
|
|
1609
|
+
}
|
|
1610
|
+
/** Counts an editor event; components that read it re-render each time the event fires. */
|
|
1611
|
+
function useEventCount(editor, name) {
|
|
1612
|
+
const count = useRef(0);
|
|
1613
|
+
const subscribe = useCallback((notify) => editor.events.on(name, () => {
|
|
1614
|
+
count.current += 1;
|
|
1615
|
+
notify();
|
|
1616
|
+
}), [editor, name]);
|
|
1617
|
+
const read = useCallback(() => count.current, []);
|
|
1618
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
1619
|
+
}
|
|
1620
|
+
/** Hiding and locking need `core.lock` on the row's layer, group or element. */
|
|
1621
|
+
function canLock(editor, row) {
|
|
1622
|
+
return editor.permissions.can("core.lock", row.kind === "layer" ? { layer: row.record.id } : row.kind === "group" ? { group: row.record.id } : row.record.id);
|
|
1623
|
+
}
|
|
1624
|
+
function toggleVisible(editor, row) {
|
|
1625
|
+
if (row.kind === "layer") editor.layers.setVisible(row.record.id, row.record.hidden);
|
|
1626
|
+
else if (row.kind === "element") editor.elements.update(row.record.id, { hidden: !row.record.hidden });
|
|
1627
|
+
}
|
|
1628
|
+
function toggleLocked(editor, row) {
|
|
1629
|
+
if (row.kind === "layer") editor.layers.setLocked(row.record.id, !row.record.locked);
|
|
1630
|
+
else if (row.kind === "group") editor.groups.setLocked(row.record.id, !row.record.locked);
|
|
1631
|
+
else editor.elements.update(row.record.id, { locked: !row.record.locked });
|
|
1632
|
+
}
|
|
1633
|
+
function dropElements(editor, ids, target, zone) {
|
|
1634
|
+
if (target.kind === "layer") {
|
|
1635
|
+
editor.layers.moveElements(ids, target.record.id, { position: "top" });
|
|
1636
|
+
return;
|
|
1637
|
+
}
|
|
1638
|
+
const anchor = target.kind === "element" ? target.record.id : target.members[0];
|
|
1639
|
+
const anchorRecord = anchor === void 0 ? void 0 : editor.elements.get(anchor);
|
|
1640
|
+
if (anchor === void 0 || anchorRecord === void 0 || ids.includes(anchor)) return;
|
|
1641
|
+
const wanted = new Set(ids);
|
|
1642
|
+
const all = {
|
|
1643
|
+
includeHidden: true,
|
|
1644
|
+
includeRestricted: true
|
|
1645
|
+
};
|
|
1646
|
+
const moving = editor.elements.inRenderOrder(all).filter((element) => wanted.has(element.id));
|
|
1647
|
+
const siblings = editor.elements.inRenderOrder({
|
|
1648
|
+
...all,
|
|
1649
|
+
layerId: anchorRecord.layerId
|
|
1650
|
+
}).filter((element) => !wanted.has(element.id));
|
|
1651
|
+
const index = siblings.findIndex((element) => element.id === anchor);
|
|
1652
|
+
const lower = zone === "below" ? siblings[index - 1] : siblings[index];
|
|
1653
|
+
const upper = zone === "below" ? siblings[index] : siblings[index + 1];
|
|
1654
|
+
const keys = orderKeysBetween(lower?.order ?? null, upper?.order ?? null, moving.length);
|
|
1655
|
+
editor.transact((transaction) => {
|
|
1656
|
+
moving.forEach((element, position) => {
|
|
1657
|
+
const order = keys[position];
|
|
1658
|
+
if (order !== void 0) transaction.elements.update(element.id, {
|
|
1659
|
+
layerId: anchorRecord.layerId,
|
|
1660
|
+
order
|
|
1661
|
+
});
|
|
1662
|
+
});
|
|
1663
|
+
}, { label: "Reorder" });
|
|
1664
|
+
}
|
|
1665
|
+
function dropLayer(editor, id, target, zone) {
|
|
1666
|
+
if (target.kind !== "layer" || target.record.id === id) return;
|
|
1667
|
+
if (zone === "inside") editor.layers.move(id, { parentId: target.record.id });
|
|
1668
|
+
else if (zone === "above") editor.layers.move(id, {
|
|
1669
|
+
parentId: target.record.parentId ?? null,
|
|
1670
|
+
after: target.record.id
|
|
1671
|
+
});
|
|
1672
|
+
else editor.layers.move(id, {
|
|
1673
|
+
parentId: target.record.parentId ?? null,
|
|
1674
|
+
before: target.record.id
|
|
1675
|
+
});
|
|
1676
|
+
}
|
|
1677
|
+
/** A small label next to the cursor instead of the browser's snapshot of the whole row. */
|
|
1678
|
+
function setDragBadge(event, label) {
|
|
1679
|
+
const host = event.currentTarget.closest(".nx-layers") ?? event.currentTarget.ownerDocument.body;
|
|
1680
|
+
const badge = event.currentTarget.ownerDocument.createElement("div");
|
|
1681
|
+
badge.className = "nx-drag-badge";
|
|
1682
|
+
badge.textContent = label;
|
|
1683
|
+
host.appendChild(badge);
|
|
1684
|
+
event.dataTransfer.setDragImage(badge, -12, -8);
|
|
1685
|
+
setTimeout(() => badge.remove(), 0);
|
|
1686
|
+
}
|
|
1687
|
+
function Thumbnail({ id }) {
|
|
1688
|
+
const view = useCanvasView();
|
|
1689
|
+
const revision = useEditorValue((editor) => editor.elements.get(id), {
|
|
1690
|
+
events: ["document.change"],
|
|
1691
|
+
equals: Object.is
|
|
1692
|
+
});
|
|
1693
|
+
const [url, setUrl] = useState(void 0);
|
|
1694
|
+
useEffect(() => {
|
|
1695
|
+
if (view === void 0) return;
|
|
1696
|
+
let objectUrl;
|
|
1697
|
+
let cancelled = false;
|
|
1698
|
+
view.exportImage({
|
|
1699
|
+
ids: [id],
|
|
1700
|
+
padding: 2,
|
|
1701
|
+
scale: .25,
|
|
1702
|
+
background: "transparent"
|
|
1703
|
+
}).then((result) => {
|
|
1704
|
+
if (!cancelled && result.blob !== void 0) {
|
|
1705
|
+
objectUrl = URL.createObjectURL(result.blob);
|
|
1706
|
+
setUrl(objectUrl);
|
|
1707
|
+
}
|
|
1708
|
+
}).catch(() => void 0);
|
|
1709
|
+
return () => {
|
|
1710
|
+
cancelled = true;
|
|
1711
|
+
if (objectUrl !== void 0) URL.revokeObjectURL(objectUrl);
|
|
1712
|
+
};
|
|
1713
|
+
}, [
|
|
1714
|
+
view,
|
|
1715
|
+
id,
|
|
1716
|
+
revision
|
|
1717
|
+
]);
|
|
1718
|
+
return url === void 0 ? /* @__PURE__ */ jsx("span", { className: "nx-thumb" }) : /* @__PURE__ */ jsx("img", {
|
|
1719
|
+
className: "nx-thumb",
|
|
1720
|
+
src: url,
|
|
1721
|
+
alt: ""
|
|
1722
|
+
});
|
|
1723
|
+
}
|
|
1724
|
+
/**
|
|
1725
|
+
* Layers, groups and elements as a virtualized tree: selection sync, rename, visibility and
|
|
1726
|
+
* lock toggles, search, drag-and-drop reordering and moving between layers.
|
|
1727
|
+
*/
|
|
1728
|
+
function LayersPanel({ thumbnails = false, onClose }) {
|
|
1729
|
+
const editor = useEditor();
|
|
1730
|
+
const { t, openContextMenu, isHidden } = useUi();
|
|
1731
|
+
const selection = useSelection();
|
|
1732
|
+
const selected = useMemo(() => new Set(selection), [selection]);
|
|
1733
|
+
const permissionGeneration = useEventCount(editor, "permissions.change");
|
|
1734
|
+
const revision = `${useEditorValue((current) => `${current.document.revision}:${current.modes.current()}:${current.layers.active()}`, { events: TREE_EVENTS })}:${permissionGeneration}`;
|
|
1735
|
+
const [expanded, setExpanded] = useState(/* @__PURE__ */ new Set());
|
|
1736
|
+
const [collapsed, setCollapsed] = useState(/* @__PURE__ */ new Set());
|
|
1737
|
+
const [query, setQuery] = useState("");
|
|
1738
|
+
const [editing, setEditing] = useState(void 0);
|
|
1739
|
+
const [scroll, setScroll] = useState({
|
|
1740
|
+
top: 0,
|
|
1741
|
+
height: 400
|
|
1742
|
+
});
|
|
1743
|
+
const [drop, setDrop] = useState(void 0);
|
|
1744
|
+
const drag = useRef(void 0);
|
|
1745
|
+
const anchor = useRef(void 0);
|
|
1746
|
+
const listRef = useRef(null);
|
|
1747
|
+
const contentRef = useRef(null);
|
|
1748
|
+
const rows = useMemo(() => buildLayerRows(editorTreeSource(editor), {
|
|
1749
|
+
expanded,
|
|
1750
|
+
collapsed,
|
|
1751
|
+
query
|
|
1752
|
+
}), [
|
|
1753
|
+
editor,
|
|
1754
|
+
revision,
|
|
1755
|
+
expanded,
|
|
1756
|
+
collapsed,
|
|
1757
|
+
query
|
|
1758
|
+
]);
|
|
1759
|
+
const range = visibleRange(scroll.top, scroll.height, ROW_STRIDE, rows.length);
|
|
1760
|
+
const sticky = useMemo(() => stickyRows(rows, scroll.top, ROW_STRIDE, TREE_PADDING), [rows, scroll.top]);
|
|
1761
|
+
const guide = useMemo(() => {
|
|
1762
|
+
const anchorIndex = rows.findIndex((row) => {
|
|
1763
|
+
const ids = rowIds(row);
|
|
1764
|
+
return row.kind !== "layer" && ids.length > 0 && ids.every((id) => selected.has(id));
|
|
1765
|
+
});
|
|
1766
|
+
return activeGuide(rows, anchorIndex >= 0 ? anchorIndex : rows.findIndex((row) => row.kind === "layer" && row.active));
|
|
1767
|
+
}, [rows, selected]);
|
|
1768
|
+
useEffect(() => {
|
|
1769
|
+
const list = listRef.current;
|
|
1770
|
+
if (list === null) return;
|
|
1771
|
+
const measure = () => setScroll({
|
|
1772
|
+
top: list.scrollTop,
|
|
1773
|
+
height: list.clientHeight || 400
|
|
1774
|
+
});
|
|
1775
|
+
measure();
|
|
1776
|
+
const observer = typeof ResizeObserver === "function" ? new ResizeObserver(measure) : void 0;
|
|
1777
|
+
observer?.observe(list);
|
|
1778
|
+
return () => observer?.disconnect();
|
|
1779
|
+
}, []);
|
|
1780
|
+
const toggleExpanded = (row) => {
|
|
1781
|
+
if (row.kind === "layer") setCollapsed((current) => {
|
|
1782
|
+
const next = new Set(current);
|
|
1783
|
+
if (next.has(row.key)) next.delete(row.key);
|
|
1784
|
+
else next.add(row.key);
|
|
1785
|
+
return next;
|
|
1786
|
+
});
|
|
1787
|
+
else setExpanded((current) => {
|
|
1788
|
+
const next = new Set(current);
|
|
1789
|
+
if (next.has(row.key)) next.delete(row.key);
|
|
1790
|
+
else next.add(row.key);
|
|
1791
|
+
return next;
|
|
1792
|
+
});
|
|
1793
|
+
};
|
|
1794
|
+
const select = (row, event) => {
|
|
1795
|
+
if (row.kind === "layer") {
|
|
1796
|
+
editor.layers.setActive(row.record.id);
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
const ids = rowIds(row);
|
|
1800
|
+
if (event.shiftKey && anchor.current !== void 0 && row.kind === "element") {
|
|
1801
|
+
const elementRows = rows.filter((entry) => entry.kind === "element");
|
|
1802
|
+
const from = elementRows.findIndex((entry) => entry.record.id === anchor.current);
|
|
1803
|
+
const to = elementRows.findIndex((entry) => entry.record.id === row.record.id);
|
|
1804
|
+
if (from >= 0 && to >= 0) {
|
|
1805
|
+
const [low, high] = from < to ? [from, to] : [to, from];
|
|
1806
|
+
editor.selection.set(elementRows.slice(low, high + 1).map((entry) => entry.record.id));
|
|
1807
|
+
return;
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
if (event.metaKey || event.ctrlKey) {
|
|
1811
|
+
const current = new Set(editor.selection.get());
|
|
1812
|
+
const all = ids.every((id) => current.has(id));
|
|
1813
|
+
for (const id of ids) if (all) current.delete(id);
|
|
1814
|
+
else current.add(id);
|
|
1815
|
+
editor.selection.set([...current]);
|
|
1816
|
+
} else editor.selection.set(ids);
|
|
1817
|
+
anchor.current = ids[0];
|
|
1818
|
+
};
|
|
1819
|
+
const onRowKeyDown = (row, event) => {
|
|
1820
|
+
if (editing !== void 0) return;
|
|
1821
|
+
if (event.key === "F2") {
|
|
1822
|
+
event.preventDefault();
|
|
1823
|
+
setEditing(row.key);
|
|
1824
|
+
} else if (event.key === "Enter" || event.key === " ") {
|
|
1825
|
+
event.preventDefault();
|
|
1826
|
+
select(row, event);
|
|
1827
|
+
} else if (event.altKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
|
|
1828
|
+
event.preventDefault();
|
|
1829
|
+
const up = event.key === "ArrowUp";
|
|
1830
|
+
if (row.kind === "layer") {
|
|
1831
|
+
const index = rows.indexOf(row);
|
|
1832
|
+
const sibling = rows.slice(0, index).reverse().concat(rows.slice(index + 1)).find((entry) => entry.kind === "layer" && entry.depth === row.depth && (up ? rows.indexOf(entry) < index : rows.indexOf(entry) > index));
|
|
1833
|
+
if (sibling !== void 0) dropLayer(editor, row.record.id, sibling, up ? "above" : "below");
|
|
1834
|
+
} else if (row.kind === "element") {
|
|
1835
|
+
if (up) editor.order.bringForward([row.record.id]);
|
|
1836
|
+
else editor.order.sendBackward([row.record.id]);
|
|
1837
|
+
}
|
|
1838
|
+
} else if (event.key === "ArrowRight" && row.expandable && !row.expanded) toggleExpanded(row);
|
|
1839
|
+
else if (event.key === "ArrowLeft" && row.expanded) toggleExpanded(row);
|
|
1840
|
+
else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
1841
|
+
event.preventDefault();
|
|
1842
|
+
(event.currentTarget.parentElement?.querySelector(`[data-row-index="${rows.indexOf(row) + (event.key === "ArrowDown" ? 1 : -1)}"]`) ?? null)?.focus();
|
|
1843
|
+
}
|
|
1844
|
+
};
|
|
1845
|
+
const zoneOf = (row, ratio) => {
|
|
1846
|
+
if (row.kind === "layer" && drag.current?.kind === "layer") return ratio < .25 ? "above" : ratio > .75 ? "below" : "inside";
|
|
1847
|
+
if (row.kind === "layer") return "inside";
|
|
1848
|
+
return ratio < .5 ? "above" : "below";
|
|
1849
|
+
};
|
|
1850
|
+
const canDrop = (row) => drag.current !== void 0 && (drag.current.kind === "elements" || row.kind === "layer");
|
|
1851
|
+
/**
|
|
1852
|
+
* The drop target under the pointer, from its offset in the list: the gap between two rows
|
|
1853
|
+
* counts as the lower part of the row above, so moving across rows or their children never
|
|
1854
|
+
* clears the indicator. Pinned layer rows take drops themselves, and the empty space below
|
|
1855
|
+
* the rows moves a layer to the bottom of the root.
|
|
1856
|
+
*/
|
|
1857
|
+
const dropTargetAt = (event) => {
|
|
1858
|
+
const list = listRef.current;
|
|
1859
|
+
const content = contentRef.current;
|
|
1860
|
+
if (drag.current === void 0 || list === null || content === null || rows.length === 0) return;
|
|
1861
|
+
const viewportY = event.clientY - list.getBoundingClientRect().top;
|
|
1862
|
+
const pinned = sticky.find((entry) => viewportY >= entry.top && viewportY < entry.top + ROW_STRIDE);
|
|
1863
|
+
const pinnedRow = pinned === void 0 ? void 0 : rows[pinned.index];
|
|
1864
|
+
if (pinned !== void 0 && pinnedRow !== void 0) return canDrop(pinnedRow) ? {
|
|
1865
|
+
key: pinnedRow.key,
|
|
1866
|
+
row: pinnedRow,
|
|
1867
|
+
zone: zoneOf(pinnedRow, (viewportY - pinned.top) / ROW_HEIGHT)
|
|
1868
|
+
} : void 0;
|
|
1869
|
+
const offset = event.clientY - content.getBoundingClientRect().top - TREE_PADDING;
|
|
1870
|
+
const index = Math.max(0, Math.floor(offset / ROW_STRIDE));
|
|
1871
|
+
const row = rows[index];
|
|
1872
|
+
if (row !== void 0) {
|
|
1873
|
+
const ratio = offset < 0 ? 0 : offset % ROW_STRIDE / ROW_HEIGHT;
|
|
1874
|
+
return canDrop(row) ? {
|
|
1875
|
+
key: row.key,
|
|
1876
|
+
row,
|
|
1877
|
+
zone: zoneOf(row, ratio)
|
|
1878
|
+
} : void 0;
|
|
1879
|
+
}
|
|
1880
|
+
if (drag.current.kind === "layer") {
|
|
1881
|
+
const root = [...rows].reverse().find((entry) => entry.kind === "layer" && entry.depth === 0);
|
|
1882
|
+
return root === void 0 ? void 0 : {
|
|
1883
|
+
key: END_KEY,
|
|
1884
|
+
row: root,
|
|
1885
|
+
zone: "below"
|
|
1886
|
+
};
|
|
1887
|
+
}
|
|
1888
|
+
const last = rows[rows.length - 1];
|
|
1889
|
+
return last === void 0 || !canDrop(last) ? void 0 : {
|
|
1890
|
+
key: last.key,
|
|
1891
|
+
row: last,
|
|
1892
|
+
zone: zoneOf(last, 1)
|
|
1893
|
+
};
|
|
1894
|
+
};
|
|
1895
|
+
const showDrop = (target) => setDrop((current) => target === void 0 ? void 0 : current?.key === target.key && current.zone === target.zone ? current : {
|
|
1896
|
+
key: target.key,
|
|
1897
|
+
zone: target.zone
|
|
1898
|
+
});
|
|
1899
|
+
const renderRow = (row, index, top, pinned = false) => {
|
|
1900
|
+
const ids = rowIds(row);
|
|
1901
|
+
const lockable = canLock(editor, row);
|
|
1902
|
+
const isSelected = row.kind === "layer" ? row.active : ids.length > 0 && ids.every((id) => selected.has(id));
|
|
1903
|
+
const reorderable = row.kind === "layer" ? editor.permissions.can("core.manage-layers", { layer: row.record.id }) : ids.every((id) => editor.permissions.can("core.reorder", id));
|
|
1904
|
+
const name = rowName(row, t);
|
|
1905
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1906
|
+
...pinned ? {
|
|
1907
|
+
"aria-hidden": true,
|
|
1908
|
+
tabIndex: -1,
|
|
1909
|
+
"data-testid": `layer-pinned-${row.record.id}`
|
|
1910
|
+
} : {
|
|
1911
|
+
role: "treeitem",
|
|
1912
|
+
"aria-level": row.depth + 1,
|
|
1913
|
+
"aria-selected": isSelected,
|
|
1914
|
+
"aria-expanded": row.expandable ? row.expanded : void 0,
|
|
1915
|
+
tabIndex: index === 0 ? 0 : -1,
|
|
1916
|
+
"data-row-index": index,
|
|
1917
|
+
"data-testid": `layer-row-${row.kind}-${row.record.id}`
|
|
1918
|
+
},
|
|
1919
|
+
className: cx("nx-row-item", `nx-row-${row.kind}`, isSelected && "nx-selected", isHiddenRow(row) && "nx-dimmed", drop?.key === row.key && `nx-drop-${drop.zone}`),
|
|
1920
|
+
style: {
|
|
1921
|
+
position: "absolute",
|
|
1922
|
+
top,
|
|
1923
|
+
height: ROW_HEIGHT,
|
|
1924
|
+
left: 0,
|
|
1925
|
+
right: 0,
|
|
1926
|
+
paddingInlineStart: ROW_PADDING + row.depth * INDENT,
|
|
1927
|
+
["--nx-row-indent"]: `${ROW_PADDING + row.depth * INDENT}px`
|
|
1928
|
+
},
|
|
1929
|
+
title: reorderable ? void 0 : editor.permissions.explain(row.kind === "layer" ? "core.manage-layers" : "core.reorder", row.kind === "layer" ? { layer: row.record.id } : ids[0]).reason,
|
|
1930
|
+
draggable: reorderable && editing !== row.key,
|
|
1931
|
+
onClick: (event) => {
|
|
1932
|
+
select(row, event);
|
|
1933
|
+
if (pinned && listRef.current !== null) listRef.current.scrollTop = TREE_PADDING + (index - row.depth) * ROW_STRIDE;
|
|
1934
|
+
},
|
|
1935
|
+
onDoubleClick: () => setEditing(row.key),
|
|
1936
|
+
onKeyDown: (event) => onRowKeyDown(row, event),
|
|
1937
|
+
onContextMenu: (event) => {
|
|
1938
|
+
event.preventDefault();
|
|
1939
|
+
if (row.kind !== "layer" && !isSelected) editor.selection.set(ids);
|
|
1940
|
+
openContextMenu({
|
|
1941
|
+
clientX: event.clientX,
|
|
1942
|
+
clientY: event.clientY,
|
|
1943
|
+
...row.kind === "element" ? { elementId: row.record.id } : {}
|
|
1944
|
+
});
|
|
1945
|
+
},
|
|
1946
|
+
onDragStart: (event) => {
|
|
1947
|
+
const dragging = row.kind === "layer" ? {
|
|
1948
|
+
kind: "layer",
|
|
1949
|
+
ids: [row.record.id]
|
|
1950
|
+
} : {
|
|
1951
|
+
kind: "elements",
|
|
1952
|
+
ids: isSelected ? editor.selection.get() : ids
|
|
1953
|
+
};
|
|
1954
|
+
drag.current = dragging;
|
|
1955
|
+
event.dataTransfer.effectAllowed = "move";
|
|
1956
|
+
const own = row.kind === "layer" ? [row.record.id] : ids;
|
|
1957
|
+
setDragBadge(event, dragging.ids.length === own.length && dragging.ids.every((id) => own.includes(id)) ? name : t("layers.dragCount", { count: dragging.ids.length }));
|
|
1958
|
+
event.dataTransfer.setData("application/x-drawback-layers", dragging.ids.join(","));
|
|
1959
|
+
},
|
|
1960
|
+
onDragEnd: () => {
|
|
1961
|
+
drag.current = void 0;
|
|
1962
|
+
setDrop(void 0);
|
|
1963
|
+
},
|
|
1964
|
+
children: [
|
|
1965
|
+
Array.from({ length: row.depth }, (_, level) => /* @__PURE__ */ jsx("span", {
|
|
1966
|
+
"aria-hidden": "true",
|
|
1967
|
+
className: cx("nx-tree-guide", guide !== void 0 && guide.level === level && index >= guide.start && index < guide.end && "nx-active"),
|
|
1968
|
+
style: { insetInlineStart: ROW_PADDING + level * INDENT + TOGGLE_SIZE / 2 }
|
|
1969
|
+
}, level)),
|
|
1970
|
+
row.expandable ? /* @__PURE__ */ jsx("button", {
|
|
1971
|
+
type: "button",
|
|
1972
|
+
className: "nx-tree-toggle",
|
|
1973
|
+
tabIndex: -1,
|
|
1974
|
+
"aria-label": row.expanded ? t("layers.collapse") : t("layers.expand"),
|
|
1975
|
+
onClick: (event) => {
|
|
1976
|
+
event.stopPropagation();
|
|
1977
|
+
toggleExpanded(row);
|
|
1978
|
+
},
|
|
1979
|
+
children: /* @__PURE__ */ jsx(Icon, { name: row.expanded ? "collapse" : "expand" })
|
|
1980
|
+
}) : /* @__PURE__ */ jsx("span", { className: "nx-tree-toggle" }),
|
|
1981
|
+
row.kind === "element" && thumbnails ? /* @__PURE__ */ jsx(Thumbnail, { id: row.record.id }) : /* @__PURE__ */ jsx(Icon, { name: row.kind === "layer" ? "layers" : row.kind === "group" ? "group" : row.icon }),
|
|
1982
|
+
editing === row.key ? /* @__PURE__ */ jsx("input", {
|
|
1983
|
+
className: "nx-rename",
|
|
1984
|
+
defaultValue: name,
|
|
1985
|
+
"aria-label": t("layers.rename"),
|
|
1986
|
+
autoFocus: true,
|
|
1987
|
+
"data-testid": "layer-rename",
|
|
1988
|
+
onClick: (event) => event.stopPropagation(),
|
|
1989
|
+
onKeyDown: (event) => {
|
|
1990
|
+
if (event.key === "Enter") {
|
|
1991
|
+
rename(editor, row, event.currentTarget.value);
|
|
1992
|
+
setEditing(void 0);
|
|
1993
|
+
} else if (event.key === "Escape") {
|
|
1994
|
+
event.stopPropagation();
|
|
1995
|
+
setEditing(void 0);
|
|
1996
|
+
}
|
|
1997
|
+
},
|
|
1998
|
+
onBlur: (event) => {
|
|
1999
|
+
rename(editor, row, event.currentTarget.value);
|
|
2000
|
+
setEditing(void 0);
|
|
2001
|
+
}
|
|
2002
|
+
}) : /* @__PURE__ */ jsx("span", {
|
|
2003
|
+
className: "nx-row-name",
|
|
2004
|
+
children: name
|
|
2005
|
+
}),
|
|
2006
|
+
/* @__PURE__ */ jsxs("span", {
|
|
2007
|
+
className: "nx-row-actions",
|
|
2008
|
+
onClick: (event) => event.stopPropagation(),
|
|
2009
|
+
onDoubleClick: (event) => event.stopPropagation(),
|
|
2010
|
+
children: [row.kind !== "group" && /* @__PURE__ */ jsx(IconButton, {
|
|
2011
|
+
icon: isHiddenRow(row) ? "eyeOff" : "eye",
|
|
2012
|
+
label: isHiddenRow(row) ? t("layers.show") : t("layers.hide"),
|
|
2013
|
+
disabled: !lockable,
|
|
2014
|
+
onClick: () => toggleVisible(editor, row),
|
|
2015
|
+
className: "nx-row-action",
|
|
2016
|
+
tabIndex: -1,
|
|
2017
|
+
testId: `layer-visibility-${row.record.id}`
|
|
2018
|
+
}), /* @__PURE__ */ jsx(IconButton, {
|
|
2019
|
+
icon: isLocked(row) ? "lock" : "unlock",
|
|
2020
|
+
label: isLocked(row) ? t("layers.unlock") : t("layers.lock"),
|
|
2021
|
+
disabled: !lockable,
|
|
2022
|
+
onClick: () => toggleLocked(editor, row),
|
|
2023
|
+
className: cx("nx-row-action", isLocked(row) && "nx-on"),
|
|
2024
|
+
tabIndex: -1,
|
|
2025
|
+
testId: `layer-lock-${row.record.id}`
|
|
2026
|
+
})]
|
|
2027
|
+
})
|
|
2028
|
+
]
|
|
2029
|
+
}, row.key);
|
|
2030
|
+
};
|
|
2031
|
+
const addLayer = () => {
|
|
2032
|
+
const id = editor.layers.create({ name: t("layers.newName", { number: editor.layers.list().length + 1 }) });
|
|
2033
|
+
editor.layers.setActive(id);
|
|
2034
|
+
};
|
|
2035
|
+
return /* @__PURE__ */ jsxs(Island, {
|
|
2036
|
+
className: "nx-layers",
|
|
2037
|
+
label: t("layers.label"),
|
|
2038
|
+
testId: "layers-panel",
|
|
2039
|
+
children: [
|
|
2040
|
+
/* @__PURE__ */ jsxs("header", {
|
|
2041
|
+
className: "nx-panel-header",
|
|
2042
|
+
children: [
|
|
2043
|
+
/* @__PURE__ */ jsx("h2", { children: t("layers.label") }),
|
|
2044
|
+
!isHidden("layers:add") && /* @__PURE__ */ jsx(IconButton, {
|
|
2045
|
+
icon: "add",
|
|
2046
|
+
label: t("layers.add"),
|
|
2047
|
+
onClick: addLayer,
|
|
2048
|
+
testId: "layers-add",
|
|
2049
|
+
disabled: !editor.permissions.can("core.manage-layers")
|
|
2050
|
+
}),
|
|
2051
|
+
onClose !== void 0 && /* @__PURE__ */ jsx(IconButton, {
|
|
2052
|
+
icon: "close",
|
|
2053
|
+
label: t("dialog.close"),
|
|
2054
|
+
onClick: onClose,
|
|
2055
|
+
testId: "layers-close"
|
|
2056
|
+
})
|
|
2057
|
+
]
|
|
2058
|
+
}),
|
|
2059
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2060
|
+
className: "nx-search",
|
|
2061
|
+
children: [/* @__PURE__ */ jsx(Icon, { name: "search" }), /* @__PURE__ */ jsx("input", {
|
|
2062
|
+
type: "search",
|
|
2063
|
+
placeholder: t("layers.search"),
|
|
2064
|
+
"aria-label": t("layers.search"),
|
|
2065
|
+
value: query,
|
|
2066
|
+
"data-testid": "layers-search",
|
|
2067
|
+
onChange: (event) => setQuery(event.target.value)
|
|
2068
|
+
})]
|
|
2069
|
+
}),
|
|
2070
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2071
|
+
ref: listRef,
|
|
2072
|
+
className: "nx-tree",
|
|
2073
|
+
role: "tree",
|
|
2074
|
+
"aria-label": t("layers.label"),
|
|
2075
|
+
"aria-multiselectable": "true",
|
|
2076
|
+
"data-testid": "layers-tree",
|
|
2077
|
+
onScroll: (event) => setScroll({
|
|
2078
|
+
top: event.currentTarget.scrollTop,
|
|
2079
|
+
height: event.currentTarget.clientHeight
|
|
2080
|
+
}),
|
|
2081
|
+
onDragOver: (event) => {
|
|
2082
|
+
const target = dropTargetAt(event);
|
|
2083
|
+
showDrop(target);
|
|
2084
|
+
if (target !== void 0) event.preventDefault();
|
|
2085
|
+
},
|
|
2086
|
+
onDragLeave: (event) => {
|
|
2087
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
2088
|
+
if (!(event.clientX > rect.left && event.clientX < rect.right && event.clientY > rect.top && event.clientY < rect.bottom)) showDrop(void 0);
|
|
2089
|
+
},
|
|
2090
|
+
onDrop: (event) => {
|
|
2091
|
+
event.preventDefault();
|
|
2092
|
+
const dragging = drag.current;
|
|
2093
|
+
const target = dropTargetAt(event);
|
|
2094
|
+
drag.current = void 0;
|
|
2095
|
+
showDrop(void 0);
|
|
2096
|
+
if (dragging === void 0 || target === void 0) return;
|
|
2097
|
+
try {
|
|
2098
|
+
if (dragging.kind === "layer") dropLayer(editor, dragging.ids[0], target.row, target.zone);
|
|
2099
|
+
else dropElements(editor, dragging.ids, target.row, target.zone);
|
|
2100
|
+
} catch (error) {
|
|
2101
|
+
editor.events.reportError(toError(error), "layers panel drop");
|
|
2102
|
+
}
|
|
2103
|
+
},
|
|
2104
|
+
children: [
|
|
2105
|
+
rows.length === 0 && /* @__PURE__ */ jsx("p", {
|
|
2106
|
+
className: "nx-caption",
|
|
2107
|
+
children: query === "" ? t("layers.empty") : t("layers.noMatches")
|
|
2108
|
+
}),
|
|
2109
|
+
sticky.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
2110
|
+
className: "nx-tree-pinned",
|
|
2111
|
+
children: sticky.map((entry, position) => {
|
|
2112
|
+
const row = rows[entry.index];
|
|
2113
|
+
return row === void 0 ? null : /* @__PURE__ */ jsx("div", {
|
|
2114
|
+
className: cx("nx-pinned-row", position === sticky.length - 1 && "nx-pinned-last"),
|
|
2115
|
+
style: {
|
|
2116
|
+
top: entry.top,
|
|
2117
|
+
height: ROW_STRIDE,
|
|
2118
|
+
zIndex: sticky.length - position
|
|
2119
|
+
},
|
|
2120
|
+
children: renderRow(row, entry.index, 0, true)
|
|
2121
|
+
}, row.key);
|
|
2122
|
+
})
|
|
2123
|
+
}),
|
|
2124
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2125
|
+
ref: contentRef,
|
|
2126
|
+
style: {
|
|
2127
|
+
height: rows.length === 0 ? 0 : rows.length * ROW_STRIDE - ROW_GAP + 12,
|
|
2128
|
+
position: "relative"
|
|
2129
|
+
},
|
|
2130
|
+
children: [rows.slice(range.start, range.end).map((row, offset) => renderRow(row, range.start + offset, TREE_PADDING + (range.start + offset) * ROW_STRIDE)), drop?.key === END_KEY && /* @__PURE__ */ jsx("div", {
|
|
2131
|
+
className: "nx-drop-end nx-drop-below",
|
|
2132
|
+
style: {
|
|
2133
|
+
top: TREE_PADDING + rows.length * ROW_STRIDE - ROW_GAP,
|
|
2134
|
+
["--nx-row-indent"]: `${ROW_PADDING}px`
|
|
2135
|
+
}
|
|
2136
|
+
})]
|
|
2137
|
+
})
|
|
2138
|
+
]
|
|
2139
|
+
})
|
|
2140
|
+
]
|
|
2141
|
+
});
|
|
2142
|
+
}
|
|
2143
|
+
// src/hooks/use-latest.ts
|
|
2144
|
+
/**
|
|
2145
|
+
* A ref that always holds the latest `value`, for callbacks that outlive a render and may also
|
|
2146
|
+
* run during one (where `useEffectEvent` would throw). Updated in an insertion effect, so it is
|
|
2147
|
+
* current before layout effects and event handlers run, and never written during render.
|
|
2148
|
+
*/
|
|
2149
|
+
function useLatest(value) {
|
|
2150
|
+
const ref = useRef(value);
|
|
2151
|
+
useInsertionEffect(() => {
|
|
2152
|
+
ref.current = value;
|
|
2153
|
+
});
|
|
2154
|
+
return ref;
|
|
2155
|
+
}
|
|
2156
|
+
// src/features/canvas-commands/canvas-commands.tsx
|
|
2157
|
+
function reportError(editor, error, context) {
|
|
2158
|
+
editor.events.reportError(toError(error), context);
|
|
2159
|
+
}
|
|
2160
|
+
/**
|
|
2161
|
+
* Registers the UI-level commands the context menu and shortcuts use: copy as PNG
|
|
2162
|
+
* (Shift+Alt+C) / SVG, copy link to object (with a short confirmation), the minimap toggle and opening the canvas settings in the main menu (Alt+/).
|
|
2163
|
+
* `<DefaultEditor>` renders it; custom layouts can render it too.
|
|
2164
|
+
*/
|
|
2165
|
+
function CanvasCommands() {
|
|
2166
|
+
const editor = useEditor();
|
|
2167
|
+
const [notice, setNotice] = useState(void 0);
|
|
2168
|
+
const view = useCanvasView();
|
|
2169
|
+
const { t, isOpen, setOpen, isHidden } = useUi();
|
|
2170
|
+
const latest = useLatest({
|
|
2171
|
+
view,
|
|
2172
|
+
isOpen,
|
|
2173
|
+
setOpen,
|
|
2174
|
+
isHidden
|
|
2175
|
+
});
|
|
2176
|
+
const latestView = () => latest.current.view;
|
|
2177
|
+
const panels = () => latest.current;
|
|
2178
|
+
useEffect(() => {
|
|
2179
|
+
const exportable = () => latestView() !== void 0 && editor.elements.query().length > 0;
|
|
2180
|
+
const copy = (format) => {
|
|
2181
|
+
const current = latestView();
|
|
2182
|
+
if (current === void 0) return;
|
|
2183
|
+
const exported = current.exportImage({
|
|
2184
|
+
format,
|
|
2185
|
+
bounds: editor.selection.get().length > 0 ? "selection" : "content",
|
|
2186
|
+
padding: 10
|
|
2187
|
+
});
|
|
2188
|
+
const type = format === "png" ? "image/png" : "text/plain";
|
|
2189
|
+
const blob = exported.then((result) => {
|
|
2190
|
+
if (format === "svg") return new Blob([result.svg ?? ""], { type });
|
|
2191
|
+
if (result.blob === void 0) throw new Error("PNG export produced no image.");
|
|
2192
|
+
return result.blob;
|
|
2193
|
+
});
|
|
2194
|
+
(typeof ClipboardItem === "function" ? navigator.clipboard.write([new ClipboardItem({ [type]: blob })]) : format === "svg" ? blob.then((value) => value.text()).then((text) => navigator.clipboard.writeText(text)) : Promise.reject(/* @__PURE__ */ new Error("This browser cannot copy images to the clipboard."))).catch((error) => reportError(editor, error, `command:ui.copyAs.${format}`));
|
|
2195
|
+
};
|
|
2196
|
+
const clipboardAvailable = () => typeof navigator !== "undefined" && navigator.clipboard !== void 0;
|
|
2197
|
+
const offs = [
|
|
2198
|
+
editor.commands.register({
|
|
2199
|
+
id: COPY_AS_PNG_COMMAND,
|
|
2200
|
+
label: t("action.copyAsPng"),
|
|
2201
|
+
canRun: () => exportable() && clipboardAvailable() && typeof ClipboardItem === "function",
|
|
2202
|
+
run: () => copy("png")
|
|
2203
|
+
}, { replace: true }),
|
|
2204
|
+
editor.commands.register({
|
|
2205
|
+
id: COPY_AS_SVG_COMMAND,
|
|
2206
|
+
label: t("action.copyAsSvg"),
|
|
2207
|
+
canRun: () => exportable() && clipboardAvailable(),
|
|
2208
|
+
run: () => copy("svg")
|
|
2209
|
+
}, { replace: true }),
|
|
2210
|
+
editor.commands.register({
|
|
2211
|
+
id: COPY_ELEMENT_LINK_COMMAND,
|
|
2212
|
+
label: t("action.copyElementLink"),
|
|
2213
|
+
canRun: () => {
|
|
2214
|
+
const [id, ...rest] = editor.selection.get();
|
|
2215
|
+
return id !== void 0 && rest.length === 0 && clipboardAvailable() && editor.links.elementUrl(id) !== void 0;
|
|
2216
|
+
},
|
|
2217
|
+
run: () => {
|
|
2218
|
+
const [id] = editor.selection.get();
|
|
2219
|
+
const url = id === void 0 ? void 0 : editor.links.elementUrl(id);
|
|
2220
|
+
if (url !== void 0) navigator.clipboard.writeText(url).then(() => setNotice(t("toast.linkCopied"))).catch((error) => reportError(editor, error, `command:${COPY_ELEMENT_LINK_COMMAND}`));
|
|
2221
|
+
}
|
|
2222
|
+
}, { replace: true }),
|
|
2223
|
+
editor.commands.register({
|
|
2224
|
+
id: MINIMAP_COMMAND,
|
|
2225
|
+
label: t("minimap.label"),
|
|
2226
|
+
canRun: () => !panels().isHidden("minimap"),
|
|
2227
|
+
run: () => panels().setOpen("minimap", !panels().isOpen("minimap"))
|
|
2228
|
+
}, { replace: true }),
|
|
2229
|
+
editor.commands.register({
|
|
2230
|
+
id: CANVAS_PROPERTIES_COMMAND,
|
|
2231
|
+
label: t("action.canvasProperties"),
|
|
2232
|
+
canRun: () => !panels().isHidden("mainMenu"),
|
|
2233
|
+
run: () => panels().setOpen("mainMenu", !panels().isOpen("mainMenu"))
|
|
2234
|
+
}, { replace: true }),
|
|
2235
|
+
editor.input.keymap.bind("Shift+Alt+C", COPY_AS_PNG_COMMAND),
|
|
2236
|
+
editor.input.keymap.bind("Alt+/", CANVAS_PROPERTIES_COMMAND)
|
|
2237
|
+
];
|
|
2238
|
+
return () => {
|
|
2239
|
+
for (const off of offs.reverse()) off();
|
|
2240
|
+
};
|
|
2241
|
+
}, [editor, t]);
|
|
2242
|
+
useEffect(() => {
|
|
2243
|
+
if (notice === void 0) return;
|
|
2244
|
+
const timer = setTimeout(() => setNotice(void 0), 2e3);
|
|
2245
|
+
return () => clearTimeout(timer);
|
|
2246
|
+
}, [notice]);
|
|
2247
|
+
return notice === void 0 ? null : /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx("div", {
|
|
2248
|
+
className: "nx-toast nx-toast-info",
|
|
2249
|
+
role: "status",
|
|
2250
|
+
"data-testid": "ui-notice",
|
|
2251
|
+
children: notice
|
|
2252
|
+
}) });
|
|
2253
|
+
}
|
|
2254
|
+
// src/features/context-menu/context-menu.tsx
|
|
2255
|
+
function command(id, commandId, label, order, group, options = {}) {
|
|
2256
|
+
return {
|
|
2257
|
+
id,
|
|
2258
|
+
order,
|
|
2259
|
+
group,
|
|
2260
|
+
label,
|
|
2261
|
+
...options.shortcut === void 0 ? {} : { shortcut: options.shortcut },
|
|
2262
|
+
...options.danger === void 0 ? {} : { danger: options.danger },
|
|
2263
|
+
...options.when === void 0 ? {} : { when: options.when },
|
|
2264
|
+
...options.checked === void 0 ? {} : { checked: options.checked },
|
|
2265
|
+
enabled: (context) => context.editor.commands.has(commandId) && context.editor.commands.canExecute(commandId, options.args),
|
|
2266
|
+
run: (context) => {
|
|
2267
|
+
context.editor.commands.execute(commandId, options.args);
|
|
2268
|
+
}
|
|
2269
|
+
};
|
|
2270
|
+
}
|
|
2271
|
+
const onElement = (context) => context.selection.length > 0;
|
|
2272
|
+
const onCanvas = (context) => context.selection.length === 0;
|
|
2273
|
+
/**
|
|
2274
|
+
* Built-in context menu entries, in Excalidraw's order; `t` resolves labels at render time.
|
|
2275
|
+
* On an element: clipboard, copy as image, styles, grouping, stacking, flipping, link /
|
|
2276
|
+
* duplicate / lock, delete. On the canvas: paste, copy as image, select all, view toggles.
|
|
2277
|
+
*/
|
|
2278
|
+
function builtinContextMenuItems(t, ui) {
|
|
2279
|
+
const snapping = (context) => context.editor.snapping.settings();
|
|
2280
|
+
return [
|
|
2281
|
+
command("core.cut", "core.cut", t("action.cut"), 100, "clipboard", {
|
|
2282
|
+
when: onElement,
|
|
2283
|
+
shortcut: "Mod+X"
|
|
2284
|
+
}),
|
|
2285
|
+
command("core.copy", "core.copy", t("action.copy"), 110, "clipboard", {
|
|
2286
|
+
when: onElement,
|
|
2287
|
+
shortcut: "Mod+C"
|
|
2288
|
+
}),
|
|
2289
|
+
command("core.paste", "core.paste", t("action.paste"), 120, "clipboard", { shortcut: "Mod+V" }),
|
|
2290
|
+
command(COPY_AS_PNG_COMMAND, COPY_AS_PNG_COMMAND, t("action.copyAsPng"), 150, "export"),
|
|
2291
|
+
command(COPY_AS_SVG_COMMAND, COPY_AS_SVG_COMMAND, t("action.copyAsSvg"), 160, "export"),
|
|
2292
|
+
command("core.selectAll", "core.selectAll", t("action.selectAll"), 170, "select", { when: onCanvas }),
|
|
2293
|
+
command("core.style.copy", "core.style.copy", t("action.copyStyles"), 200, "styles", { when: onElement }),
|
|
2294
|
+
command("core.style.paste", "core.style.paste", t("action.pasteStyles"), 210, "styles", { when: onElement }),
|
|
2295
|
+
command("core.group", "core.group", t("action.group"), 300, "group", { when: onElement }),
|
|
2296
|
+
command("core.ungroup", "core.ungroup", t("action.ungroup"), 310, "group", { when: onElement }),
|
|
2297
|
+
command("core.sendBackward", "core.sendBackward", t("action.sendBackward"), 400, "order", { when: onElement }),
|
|
2298
|
+
command("core.bringForward", "core.bringForward", t("action.bringForward"), 410, "order", { when: onElement }),
|
|
2299
|
+
command("core.sendToBack", "core.sendToBack", t("action.sendToBack"), 420, "order", { when: onElement }),
|
|
2300
|
+
command("core.bringToFront", "core.bringToFront", t("action.bringToFront"), 430, "order", { when: onElement }),
|
|
2301
|
+
command("core.flipHorizontal", "core.flipHorizontal", t("action.flipHorizontal"), 500, "transform", { when: onElement }),
|
|
2302
|
+
command("core.flipVertical", "core.flipVertical", t("action.flipVertical"), 510, "transform", { when: onElement }),
|
|
2303
|
+
command(LINK_COMMAND, LINK_COMMAND, t("link.edit"), 600, "link", { when: (context) => context.selection.length === 1 }),
|
|
2304
|
+
command(COPY_ELEMENT_LINK_COMMAND, COPY_ELEMENT_LINK_COMMAND, t("action.copyElementLink"), 605, "link", { when: (context) => context.selection.length === 1 }),
|
|
2305
|
+
command("core.duplicate", "core.duplicate", t("action.duplicate"), 610, "edit", { when: onElement }),
|
|
2306
|
+
command("core.lock.toggle", "core.lock.toggle", t("action.lock"), 620, "edit", { when: onElement }),
|
|
2307
|
+
command("core.grid.toggle", "core.grid.toggle", t("grid.toggle"), 700, "view", {
|
|
2308
|
+
when: onCanvas,
|
|
2309
|
+
checked: (context) => context.editor.grid.visible()
|
|
2310
|
+
}),
|
|
2311
|
+
command("core.snapping.objects.toggle", "core.snapping.objects.toggle", t("snapping.enabled"), 710, "view", {
|
|
2312
|
+
when: onCanvas,
|
|
2313
|
+
checked: (context) => snapping(context).enabled && snapping(context).objects
|
|
2314
|
+
}),
|
|
2315
|
+
command("core.bindings.toggle", "core.bindings.toggle", t("bindings.toggle"), 720, "view", {
|
|
2316
|
+
when: onCanvas,
|
|
2317
|
+
checked: (context) => context.editor.bindings.options().enabled
|
|
2318
|
+
}),
|
|
2319
|
+
command("core.snapping.midpoints.toggle", "core.snapping.midpoints.toggle", t("snapping.midpoints"), 730, "view", {
|
|
2320
|
+
when: onCanvas,
|
|
2321
|
+
checked: (context) => snapping(context).enabled && snapping(context).connectionPoints
|
|
2322
|
+
}),
|
|
2323
|
+
command("core.zen.toggle", "core.zen.toggle", t("zen.enter"), 740, "view", {
|
|
2324
|
+
when: onCanvas,
|
|
2325
|
+
checked: (context) => context.editor.modes.presentation().zen
|
|
2326
|
+
}),
|
|
2327
|
+
command("core.view.toggle", "core.view.toggle", t("view.mode"), 750, "view", {
|
|
2328
|
+
when: onCanvas,
|
|
2329
|
+
checked: (context) => context.editor.modes.current() === "view"
|
|
2330
|
+
}),
|
|
2331
|
+
command(MINIMAP_COMMAND, MINIMAP_COMMAND, t("minimap.label"), 760, "view", {
|
|
2332
|
+
when: onCanvas,
|
|
2333
|
+
...ui === void 0 ? {} : { checked: () => ui.isOpen("minimap") }
|
|
2334
|
+
}),
|
|
2335
|
+
command(CANVAS_PROPERTIES_COMMAND, CANVAS_PROPERTIES_COMMAND, t("action.canvasProperties"), 770, "view", { when: (context) => onCanvas(context) && context.editor.commands.canExecute("ui.canvasProperties.toggle") }),
|
|
2336
|
+
command("core.delete", "core.delete", t("action.delete"), 900, "danger", {
|
|
2337
|
+
when: onElement,
|
|
2338
|
+
danger: true
|
|
2339
|
+
})
|
|
2340
|
+
];
|
|
2341
|
+
}
|
|
2342
|
+
function shortcutFor(editor, item) {
|
|
2343
|
+
return item.shortcut ?? formatShortcut(editor.commands.shortcutsFor(item.id)[0]);
|
|
2344
|
+
}
|
|
2345
|
+
/** Menu for the current context-menu request (right click on the canvas or a panel row). */
|
|
2346
|
+
function ContextMenu() {
|
|
2347
|
+
const editor = useEditor();
|
|
2348
|
+
const { t, registry, contextMenu, openContextMenu, portalContainer, isOpen } = useUi();
|
|
2349
|
+
const ref = useRef(null);
|
|
2350
|
+
const [position, setPosition] = useState({
|
|
2351
|
+
left: 0,
|
|
2352
|
+
top: 0
|
|
2353
|
+
});
|
|
2354
|
+
const close = () => openContextMenu(void 0);
|
|
2355
|
+
const entries = useMemo(() => {
|
|
2356
|
+
if (contextMenu === void 0) return [];
|
|
2357
|
+
const element = contextMenu.elementId === void 0 ? void 0 : editor.elements.get(contextMenu.elementId);
|
|
2358
|
+
const context = {
|
|
2359
|
+
editor,
|
|
2360
|
+
...element === void 0 ? {} : { element },
|
|
2361
|
+
selection: editor.selection.get(),
|
|
2362
|
+
world: contextMenu.world ?? editor.viewport.screenToWorld({
|
|
2363
|
+
x: 0,
|
|
2364
|
+
y: 0
|
|
2365
|
+
}),
|
|
2366
|
+
t
|
|
2367
|
+
};
|
|
2368
|
+
const items = byOrder([...builtinContextMenuItems(t, { isOpen }), ...registry.contextMenu.list()]).filter((item) => item.when?.(context) ?? true);
|
|
2369
|
+
let previousGroup;
|
|
2370
|
+
return items.map((item, index) => {
|
|
2371
|
+
const separatorBefore = index > 0 && item.group !== previousGroup;
|
|
2372
|
+
previousGroup = item.group;
|
|
2373
|
+
return {
|
|
2374
|
+
id: item.id,
|
|
2375
|
+
label: typeof item.label === "function" ? item.label(context) : item.label,
|
|
2376
|
+
shortcut: shortcutFor(editor, item),
|
|
2377
|
+
disabled: !(item.enabled?.(context) ?? true),
|
|
2378
|
+
...item.checked === void 0 ? {} : { checked: item.checked(context) },
|
|
2379
|
+
...item.danger === void 0 ? {} : { danger: item.danger },
|
|
2380
|
+
separatorBefore,
|
|
2381
|
+
onSelect: () => item.run(context)
|
|
2382
|
+
};
|
|
2383
|
+
});
|
|
2384
|
+
}, [
|
|
2385
|
+
contextMenu,
|
|
2386
|
+
editor,
|
|
2387
|
+
registry,
|
|
2388
|
+
t,
|
|
2389
|
+
isOpen
|
|
2390
|
+
]);
|
|
2391
|
+
useLayoutEffect(() => {
|
|
2392
|
+
if (contextMenu === void 0 || portalContainer === null || ref.current === null) return;
|
|
2393
|
+
const container = portalContainer.getBoundingClientRect();
|
|
2394
|
+
const menu = ref.current.getBoundingClientRect();
|
|
2395
|
+
setPosition({
|
|
2396
|
+
left: Math.max(4, Math.min(contextMenu.clientX - container.left, container.width - menu.width - 4)),
|
|
2397
|
+
top: Math.max(4, Math.min(contextMenu.clientY - container.top, container.height - menu.height - 4))
|
|
2398
|
+
});
|
|
2399
|
+
ref.current.focus({ preventScroll: true });
|
|
2400
|
+
}, [
|
|
2401
|
+
contextMenu,
|
|
2402
|
+
portalContainer,
|
|
2403
|
+
entries.length
|
|
2404
|
+
]);
|
|
2405
|
+
useEffect(() => {
|
|
2406
|
+
if (contextMenu === void 0) return;
|
|
2407
|
+
const onPointer = (event) => {
|
|
2408
|
+
if (!ref.current?.contains(event.target)) openContextMenu(void 0);
|
|
2409
|
+
};
|
|
2410
|
+
const onKey = (event) => {
|
|
2411
|
+
if (event.key === "Escape") {
|
|
2412
|
+
event.stopPropagation();
|
|
2413
|
+
openContextMenu(void 0);
|
|
2414
|
+
}
|
|
2415
|
+
};
|
|
2416
|
+
document.addEventListener("pointerdown", onPointer, true);
|
|
2417
|
+
document.addEventListener("keydown", onKey, true);
|
|
2418
|
+
return () => {
|
|
2419
|
+
document.removeEventListener("pointerdown", onPointer, true);
|
|
2420
|
+
document.removeEventListener("keydown", onKey, true);
|
|
2421
|
+
};
|
|
2422
|
+
}, [contextMenu, openContextMenu]);
|
|
2423
|
+
if (contextMenu === void 0 || entries.length === 0) return null;
|
|
2424
|
+
return /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsx("div", {
|
|
2425
|
+
ref,
|
|
2426
|
+
className: "nx-popover nx-island nx-context-menu",
|
|
2427
|
+
role: "menu",
|
|
2428
|
+
tabIndex: -1,
|
|
2429
|
+
"aria-label": t("contextMenu.label"),
|
|
2430
|
+
"data-testid": "context-menu",
|
|
2431
|
+
style: position,
|
|
2432
|
+
onKeyDown: (event) => handleRovingKeys(event, "vertical"),
|
|
2433
|
+
children: /* @__PURE__ */ jsx(MenuItems, {
|
|
2434
|
+
entries,
|
|
2435
|
+
onDone: close
|
|
2436
|
+
})
|
|
2437
|
+
}) });
|
|
2438
|
+
}
|
|
2439
|
+
function useContextMenuForCanvas() {
|
|
2440
|
+
const editor = useEditor();
|
|
2441
|
+
const { openContextMenu } = useUi();
|
|
2442
|
+
return useCallback((info) => {
|
|
2443
|
+
if (info.elementId !== void 0 && !editor.selection.has(info.elementId)) editor.selection.set([info.elementId]);
|
|
2444
|
+
else if (info.elementId === void 0) editor.selection.clear();
|
|
2445
|
+
openContextMenu({
|
|
2446
|
+
clientX: info.clientX,
|
|
2447
|
+
clientY: info.clientY,
|
|
2448
|
+
world: info.world,
|
|
2449
|
+
...info.elementId === void 0 ? {} : { elementId: info.elementId }
|
|
2450
|
+
});
|
|
2451
|
+
}, [editor, openContextMenu]);
|
|
2452
|
+
}
|
|
2453
|
+
// src/lib/download.ts
|
|
2454
|
+
/** Saves `data` as a file named `name` through a temporary object URL. */
|
|
2455
|
+
function download(name, data, type = "application/json") {
|
|
2456
|
+
const blob = typeof data === "string" ? new Blob([data], { type }) : data;
|
|
2457
|
+
const url = URL.createObjectURL(blob);
|
|
2458
|
+
Object.assign(document.createElement("a"), {
|
|
2459
|
+
href: url,
|
|
2460
|
+
download: name
|
|
2461
|
+
}).click();
|
|
2462
|
+
setTimeout(() => URL.revokeObjectURL(url), 1e3);
|
|
2463
|
+
}
|
|
2464
|
+
// src/features/export/export-dialog.tsx
|
|
2465
|
+
/** PNG/SVG export with a preview: format, selection only, background, grid, scale, padding. */
|
|
2466
|
+
function ExportDialog({ fileName = "canvas" }) {
|
|
2467
|
+
const view = useCanvasView();
|
|
2468
|
+
const selection = useSelection();
|
|
2469
|
+
const { t, isOpen, setOpen } = useUi();
|
|
2470
|
+
const open = isOpen("export");
|
|
2471
|
+
const [format, setFormat] = useState("png");
|
|
2472
|
+
const [selectionOnly, setSelectionOnly] = useState(false);
|
|
2473
|
+
const [background, setBackground] = useState(true);
|
|
2474
|
+
const [grid, setGrid] = useState(false);
|
|
2475
|
+
const [scale, setScale] = useState(1);
|
|
2476
|
+
const [padding, setPadding] = useState(16);
|
|
2477
|
+
const [preview, setPreview] = useState(void 0);
|
|
2478
|
+
const onlySelected = selectionOnly && selection.length > 0;
|
|
2479
|
+
const options = useMemo(() => ({
|
|
2480
|
+
bounds: onlySelected ? "selection" : "content",
|
|
2481
|
+
padding,
|
|
2482
|
+
grid,
|
|
2483
|
+
...background ? {} : { background: "transparent" }
|
|
2484
|
+
}), [
|
|
2485
|
+
onlySelected,
|
|
2486
|
+
padding,
|
|
2487
|
+
grid,
|
|
2488
|
+
background
|
|
2489
|
+
]);
|
|
2490
|
+
useEffect(() => {
|
|
2491
|
+
if (!open || view === void 0) return;
|
|
2492
|
+
let cancelled = false;
|
|
2493
|
+
let url;
|
|
2494
|
+
view.exportImage({
|
|
2495
|
+
...options,
|
|
2496
|
+
format: "png",
|
|
2497
|
+
scale: .5
|
|
2498
|
+
}).then((result) => {
|
|
2499
|
+
if (!cancelled && result.blob !== void 0) {
|
|
2500
|
+
url = URL.createObjectURL(result.blob);
|
|
2501
|
+
setPreview({
|
|
2502
|
+
url,
|
|
2503
|
+
warnings: result.warnings.length
|
|
2504
|
+
});
|
|
2505
|
+
}
|
|
2506
|
+
}).catch(() => setPreview(void 0));
|
|
2507
|
+
return () => {
|
|
2508
|
+
cancelled = true;
|
|
2509
|
+
if (url !== void 0) URL.revokeObjectURL(url);
|
|
2510
|
+
};
|
|
2511
|
+
}, [
|
|
2512
|
+
open,
|
|
2513
|
+
view,
|
|
2514
|
+
options
|
|
2515
|
+
]);
|
|
2516
|
+
const save = async () => {
|
|
2517
|
+
if (view === void 0) return;
|
|
2518
|
+
const result = await view.exportImage({
|
|
2519
|
+
...options,
|
|
2520
|
+
format,
|
|
2521
|
+
scale
|
|
2522
|
+
});
|
|
2523
|
+
if (format === "svg") download(`${fileName}.svg`, result.svg ?? "", "image/svg+xml");
|
|
2524
|
+
else if (result.blob !== void 0) download(`${fileName}.png`, result.blob);
|
|
2525
|
+
};
|
|
2526
|
+
return /* @__PURE__ */ jsx(Dialog, {
|
|
2527
|
+
open,
|
|
2528
|
+
onClose: () => setOpen("export", false),
|
|
2529
|
+
title: t("export.title"),
|
|
2530
|
+
testId: "export-dialog",
|
|
2531
|
+
wide: true,
|
|
2532
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
2533
|
+
className: "nx-export",
|
|
2534
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
2535
|
+
className: "nx-export-preview",
|
|
2536
|
+
children: preview !== void 0 && /* @__PURE__ */ jsx("img", {
|
|
2537
|
+
src: preview.url,
|
|
2538
|
+
alt: "",
|
|
2539
|
+
"data-testid": "export-preview"
|
|
2540
|
+
})
|
|
2541
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
2542
|
+
className: "nx-fields",
|
|
2543
|
+
children: [
|
|
2544
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2545
|
+
className: "nx-field",
|
|
2546
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("export.format") }), /* @__PURE__ */ jsxs("select", {
|
|
2547
|
+
value: format,
|
|
2548
|
+
"data-testid": "export-format",
|
|
2549
|
+
onChange: (event) => setFormat(event.target.value === "svg" ? "svg" : "png"),
|
|
2550
|
+
children: [/* @__PURE__ */ jsx("option", {
|
|
2551
|
+
value: "png",
|
|
2552
|
+
children: "PNG"
|
|
2553
|
+
}), /* @__PURE__ */ jsx("option", {
|
|
2554
|
+
value: "svg",
|
|
2555
|
+
children: "SVG"
|
|
2556
|
+
})]
|
|
2557
|
+
})]
|
|
2558
|
+
}),
|
|
2559
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2560
|
+
className: "nx-check",
|
|
2561
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
2562
|
+
type: "checkbox",
|
|
2563
|
+
checked: onlySelected,
|
|
2564
|
+
disabled: selection.length === 0,
|
|
2565
|
+
onChange: (event) => setSelectionOnly(event.target.checked)
|
|
2566
|
+
}), t("export.selectionOnly")]
|
|
2567
|
+
}),
|
|
2568
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2569
|
+
className: "nx-check",
|
|
2570
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
2571
|
+
type: "checkbox",
|
|
2572
|
+
checked: background,
|
|
2573
|
+
"data-testid": "export-background",
|
|
2574
|
+
onChange: (event) => setBackground(event.target.checked)
|
|
2575
|
+
}), t("export.background")]
|
|
2576
|
+
}),
|
|
2577
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2578
|
+
className: "nx-check",
|
|
2579
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
2580
|
+
type: "checkbox",
|
|
2581
|
+
checked: grid,
|
|
2582
|
+
onChange: (event) => setGrid(event.target.checked)
|
|
2583
|
+
}), t("export.grid")]
|
|
2584
|
+
}),
|
|
2585
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2586
|
+
className: "nx-field",
|
|
2587
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("export.scale") }), /* @__PURE__ */ jsxs("select", {
|
|
2588
|
+
value: scale,
|
|
2589
|
+
"data-testid": "export-scale",
|
|
2590
|
+
onChange: (event) => setScale(Number(event.target.value)),
|
|
2591
|
+
children: [
|
|
2592
|
+
/* @__PURE__ */ jsx("option", {
|
|
2593
|
+
value: 1,
|
|
2594
|
+
children: "1×"
|
|
2595
|
+
}),
|
|
2596
|
+
/* @__PURE__ */ jsx("option", {
|
|
2597
|
+
value: 2,
|
|
2598
|
+
children: "2×"
|
|
2599
|
+
}),
|
|
2600
|
+
/* @__PURE__ */ jsx("option", {
|
|
2601
|
+
value: 3,
|
|
2602
|
+
children: "3×"
|
|
2603
|
+
})
|
|
2604
|
+
]
|
|
2605
|
+
})]
|
|
2606
|
+
}),
|
|
2607
|
+
/* @__PURE__ */ jsxs("label", {
|
|
2608
|
+
className: "nx-field",
|
|
2609
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("export.padding") }), /* @__PURE__ */ jsx("input", {
|
|
2610
|
+
type: "number",
|
|
2611
|
+
min: 0,
|
|
2612
|
+
max: 200,
|
|
2613
|
+
value: padding,
|
|
2614
|
+
onChange: (event) => setPadding(Math.max(0, Number(event.target.value) || 0))
|
|
2615
|
+
})]
|
|
2616
|
+
}),
|
|
2617
|
+
preview !== void 0 && preview.warnings > 0 && /* @__PURE__ */ jsx("p", {
|
|
2618
|
+
className: "nx-caption",
|
|
2619
|
+
children: t("export.warnings", { count: preview.warnings })
|
|
2620
|
+
}),
|
|
2621
|
+
/* @__PURE__ */ jsx("button", {
|
|
2622
|
+
type: "button",
|
|
2623
|
+
className: "nx-primary",
|
|
2624
|
+
"data-testid": "export-download",
|
|
2625
|
+
onClick: () => void save(),
|
|
2626
|
+
children: t("export.download")
|
|
2627
|
+
})
|
|
2628
|
+
]
|
|
2629
|
+
})]
|
|
2630
|
+
})
|
|
2631
|
+
});
|
|
2632
|
+
}
|
|
2633
|
+
// src/components/color-picker/colors.ts
|
|
2634
|
+
/** Lightest to darkest. */
|
|
2635
|
+
const COLOR_SHADES = {
|
|
2636
|
+
gray: [
|
|
2637
|
+
"#f8f9fa",
|
|
2638
|
+
"#e9ecef",
|
|
2639
|
+
"#ced4da",
|
|
2640
|
+
"#868e96",
|
|
2641
|
+
"#343a40"
|
|
2642
|
+
],
|
|
2643
|
+
bronze: [
|
|
2644
|
+
"#f8f1ee",
|
|
2645
|
+
"#eaddd7",
|
|
2646
|
+
"#d2bab0",
|
|
2647
|
+
"#a18072",
|
|
2648
|
+
"#846358"
|
|
2649
|
+
],
|
|
2650
|
+
cyan: [
|
|
2651
|
+
"#e3fafc",
|
|
2652
|
+
"#99e9f2",
|
|
2653
|
+
"#3bc9db",
|
|
2654
|
+
"#15aabf",
|
|
2655
|
+
"#0c8599"
|
|
2656
|
+
],
|
|
2657
|
+
blue: [
|
|
2658
|
+
"#e7f5ff",
|
|
2659
|
+
"#a5d8ff",
|
|
2660
|
+
"#4dabf7",
|
|
2661
|
+
"#228be6",
|
|
2662
|
+
"#1971c2"
|
|
2663
|
+
],
|
|
2664
|
+
violet: [
|
|
2665
|
+
"#f3f0ff",
|
|
2666
|
+
"#d0bfff",
|
|
2667
|
+
"#9775fa",
|
|
2668
|
+
"#7950f2",
|
|
2669
|
+
"#6741d9"
|
|
2670
|
+
],
|
|
2671
|
+
grape: [
|
|
2672
|
+
"#f8f0fc",
|
|
2673
|
+
"#eebefa",
|
|
2674
|
+
"#da77f2",
|
|
2675
|
+
"#be4bdb",
|
|
2676
|
+
"#9c36b5"
|
|
2677
|
+
],
|
|
2678
|
+
pink: [
|
|
2679
|
+
"#fff0f6",
|
|
2680
|
+
"#fcc2d7",
|
|
2681
|
+
"#f783ac",
|
|
2682
|
+
"#e64980",
|
|
2683
|
+
"#c2255c"
|
|
2684
|
+
],
|
|
2685
|
+
green: [
|
|
2686
|
+
"#ebfbee",
|
|
2687
|
+
"#b2f2bb",
|
|
2688
|
+
"#69db7c",
|
|
2689
|
+
"#40c057",
|
|
2690
|
+
"#2f9e44"
|
|
2691
|
+
],
|
|
2692
|
+
teal: [
|
|
2693
|
+
"#e6fcf5",
|
|
2694
|
+
"#96f2d7",
|
|
2695
|
+
"#38d9a9",
|
|
2696
|
+
"#12b886",
|
|
2697
|
+
"#099268"
|
|
2698
|
+
],
|
|
2699
|
+
yellow: [
|
|
2700
|
+
"#fff9db",
|
|
2701
|
+
"#ffec99",
|
|
2702
|
+
"#ffd43b",
|
|
2703
|
+
"#fab005",
|
|
2704
|
+
"#f08c00"
|
|
2705
|
+
],
|
|
2706
|
+
orange: [
|
|
2707
|
+
"#fff4e6",
|
|
2708
|
+
"#ffd8a8",
|
|
2709
|
+
"#ffa94d",
|
|
2710
|
+
"#fd7e14",
|
|
2711
|
+
"#e8590c"
|
|
2712
|
+
],
|
|
2713
|
+
red: [
|
|
2714
|
+
"#fff5f5",
|
|
2715
|
+
"#ffc9c9",
|
|
2716
|
+
"#ff8787",
|
|
2717
|
+
"#fa5252",
|
|
2718
|
+
"#e03131"
|
|
2719
|
+
]
|
|
2720
|
+
};
|
|
2721
|
+
const SOLID = {
|
|
2722
|
+
transparent: "transparent",
|
|
2723
|
+
white: "#ffffff",
|
|
2724
|
+
black: "#1e1e1e"
|
|
2725
|
+
};
|
|
2726
|
+
/** The picker grid, row by row, with each cell's hotkey. */
|
|
2727
|
+
const COLOR_GRID = [
|
|
2728
|
+
{
|
|
2729
|
+
key: "q",
|
|
2730
|
+
family: "transparent"
|
|
2731
|
+
},
|
|
2732
|
+
{
|
|
2733
|
+
key: "w",
|
|
2734
|
+
family: "white"
|
|
2735
|
+
},
|
|
2736
|
+
{
|
|
2737
|
+
key: "e",
|
|
2738
|
+
family: "gray"
|
|
2739
|
+
},
|
|
2740
|
+
{
|
|
2741
|
+
key: "r",
|
|
2742
|
+
family: "black"
|
|
2743
|
+
},
|
|
2744
|
+
{
|
|
2745
|
+
key: "t",
|
|
2746
|
+
family: "bronze"
|
|
2747
|
+
},
|
|
2748
|
+
{
|
|
2749
|
+
key: "a",
|
|
2750
|
+
family: "cyan"
|
|
2751
|
+
},
|
|
2752
|
+
{
|
|
2753
|
+
key: "s",
|
|
2754
|
+
family: "blue"
|
|
2755
|
+
},
|
|
2756
|
+
{
|
|
2757
|
+
key: "d",
|
|
2758
|
+
family: "violet"
|
|
2759
|
+
},
|
|
2760
|
+
{
|
|
2761
|
+
key: "f",
|
|
2762
|
+
family: "grape"
|
|
2763
|
+
},
|
|
2764
|
+
{
|
|
2765
|
+
key: "g",
|
|
2766
|
+
family: "pink"
|
|
2767
|
+
},
|
|
2768
|
+
{
|
|
2769
|
+
key: "z",
|
|
2770
|
+
family: "green"
|
|
2771
|
+
},
|
|
2772
|
+
{
|
|
2773
|
+
key: "x",
|
|
2774
|
+
family: "teal"
|
|
2775
|
+
},
|
|
2776
|
+
{
|
|
2777
|
+
key: "c",
|
|
2778
|
+
family: "yellow"
|
|
2779
|
+
},
|
|
2780
|
+
{
|
|
2781
|
+
key: "v",
|
|
2782
|
+
family: "orange"
|
|
2783
|
+
},
|
|
2784
|
+
{
|
|
2785
|
+
key: "b",
|
|
2786
|
+
family: "red"
|
|
2787
|
+
}
|
|
2788
|
+
];
|
|
2789
|
+
/** Shade index the grid shows for strokes (darkest) and backgrounds (light). */
|
|
2790
|
+
const DEFAULT_SHADE = {
|
|
2791
|
+
stroke: 4,
|
|
2792
|
+
background: 1
|
|
2793
|
+
};
|
|
2794
|
+
/** Quick picks shown next to the picker button, as in Excalidraw. */
|
|
2795
|
+
const STROKE_COLORS = [
|
|
2796
|
+
"#1e1e1e",
|
|
2797
|
+
"#e03131",
|
|
2798
|
+
"#2f9e44",
|
|
2799
|
+
"#1971c2",
|
|
2800
|
+
"#f08c00"
|
|
2801
|
+
];
|
|
2802
|
+
const FILL_COLORS = [
|
|
2803
|
+
"transparent",
|
|
2804
|
+
"#ffc9c9",
|
|
2805
|
+
"#b2f2bb",
|
|
2806
|
+
"#a5d8ff",
|
|
2807
|
+
"#ffec99"
|
|
2808
|
+
];
|
|
2809
|
+
function hasShades(family) {
|
|
2810
|
+
return family in COLOR_SHADES;
|
|
2811
|
+
}
|
|
2812
|
+
/** The color a grid cell stands for at a shade index (solid families ignore it). */
|
|
2813
|
+
function familyColor(family, shade) {
|
|
2814
|
+
return hasShades(family) ? COLOR_SHADES[family][shade] ?? COLOR_SHADES[family][0] ?? "#000000" : SOLID[family];
|
|
2815
|
+
}
|
|
2816
|
+
/** The family (and shade index, for shaded families) a color belongs to, if it is in the palette. */
|
|
2817
|
+
function familyOf(color) {
|
|
2818
|
+
if (color === void 0) return;
|
|
2819
|
+
const value = color.trim().toLowerCase();
|
|
2820
|
+
for (const [family, solid] of Object.entries(SOLID)) if (value === solid) return {
|
|
2821
|
+
family,
|
|
2822
|
+
shade: void 0
|
|
2823
|
+
};
|
|
2824
|
+
for (const [family, shades] of Object.entries(COLOR_SHADES)) {
|
|
2825
|
+
const shade = shades.indexOf(value);
|
|
2826
|
+
if (shade >= 0) return {
|
|
2827
|
+
family,
|
|
2828
|
+
shade
|
|
2829
|
+
};
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
/** `#rgb`/`#rrggbb` (the `#` optional) as lower-case `#rrggbb`; `undefined` when invalid. */
|
|
2833
|
+
function normalizeHex(input) {
|
|
2834
|
+
const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(input.trim());
|
|
2835
|
+
if (match === null) return;
|
|
2836
|
+
const digits = (match[1] ?? "").toLowerCase();
|
|
2837
|
+
return `#${digits.length === 3 ? [...digits].map((digit) => digit + digit).join("") : digits}`;
|
|
2838
|
+
}
|
|
2839
|
+
/** Whether light text reads better on `color` (hex colors only; others count as light). */
|
|
2840
|
+
function isDarkColor(color) {
|
|
2841
|
+
const hex = normalizeHex(color);
|
|
2842
|
+
if (hex === void 0) return false;
|
|
2843
|
+
const channel = (offset) => {
|
|
2844
|
+
const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
|
|
2845
|
+
return value <= .03928 ? value / 12.92 : ((value + .055) / 1.055) ** 2.4;
|
|
2846
|
+
};
|
|
2847
|
+
return .2126 * channel(1) + .7152 * channel(3) + .0722 * channel(5) < .35;
|
|
2848
|
+
}
|
|
2849
|
+
// src/components/color-picker/color-picker.tsx
|
|
2850
|
+
/**
|
|
2851
|
+
* The browser's EyeDropper (Chromium only): a magnifier that picks a color from anywhere on
|
|
2852
|
+
* screen. `undefined` where unsupported, so the button is hidden there.
|
|
2853
|
+
*/
|
|
2854
|
+
function eyeDropperOpener() {
|
|
2855
|
+
const EyeDropper = typeof window === "undefined" ? void 0 : Reflect.get(window, "EyeDropper");
|
|
2856
|
+
if (typeof EyeDropper !== "function") return;
|
|
2857
|
+
return async () => {
|
|
2858
|
+
const result = await new EyeDropper().open();
|
|
2859
|
+
return typeof result.sRGBHex === "string" ? result.sRGBHex : void 0;
|
|
2860
|
+
};
|
|
2861
|
+
}
|
|
2862
|
+
/** A color chip; transparent shows a checkerboard, `undefined` (mixed values) a hatch. */
|
|
2863
|
+
function Swatch({ color }) {
|
|
2864
|
+
return /* @__PURE__ */ jsx("span", {
|
|
2865
|
+
className: cx("nx-swatch", color === "transparent" && "nx-swatch-transparent", color === void 0 && "nx-swatch-mixed"),
|
|
2866
|
+
style: color === void 0 || color === "transparent" ? void 0 : { background: color }
|
|
2867
|
+
});
|
|
2868
|
+
}
|
|
2869
|
+
/**
|
|
2870
|
+
* Excalidraw-style color picker: the color grid (keys q–t, a–g, z–b), the active color's
|
|
2871
|
+
* shades (Shift+1–5; none for transparent, white and black) and a hex code input with a
|
|
2872
|
+
* pipette that copies a color from the screen (EyeDropper, where the browser has it).
|
|
2873
|
+
*/
|
|
2874
|
+
function ColorPicker({ label, value, kind, onChange }) {
|
|
2875
|
+
const { t } = useUi();
|
|
2876
|
+
const active = familyOf(value);
|
|
2877
|
+
const shades = active !== void 0 && hasShades(active.family) ? COLOR_SHADES[active.family] : [];
|
|
2878
|
+
const shade = active?.shade ?? DEFAULT_SHADE[kind];
|
|
2879
|
+
const hex = value === void 0 ? void 0 : normalizeHex(value);
|
|
2880
|
+
const [draft, setDraft] = useState(void 0);
|
|
2881
|
+
const shown = draft ?? (hex ?? "").slice(1);
|
|
2882
|
+
const eyeDropper = eyeDropperOpener();
|
|
2883
|
+
const onHotkey = useEffectEvent((event) => {
|
|
2884
|
+
const target = event.target;
|
|
2885
|
+
if (target instanceof HTMLInputElement && target.type !== "color" || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable || event.ctrlKey || event.metaKey || event.altKey || event.repeat) return;
|
|
2886
|
+
const digit = /^(?:Digit|Numpad)([1-5])$/.exec(event.code)?.[1];
|
|
2887
|
+
const color = event.shiftKey && digit !== void 0 ? shades[Number(digit) - 1] : event.shiftKey ? void 0 : (() => {
|
|
2888
|
+
const cell = COLOR_GRID.find((entry) => entry.key === event.key.toLowerCase());
|
|
2889
|
+
return cell === void 0 ? void 0 : familyColor(cell.family, shade);
|
|
2890
|
+
})();
|
|
2891
|
+
if (color !== void 0) {
|
|
2892
|
+
event.preventDefault();
|
|
2893
|
+
event.stopPropagation();
|
|
2894
|
+
onChange(color);
|
|
2895
|
+
}
|
|
2896
|
+
});
|
|
2897
|
+
useEffect(() => {
|
|
2898
|
+
const onKey = (event) => onHotkey(event);
|
|
2899
|
+
document.addEventListener("keydown", onKey, true);
|
|
2900
|
+
return () => document.removeEventListener("keydown", onKey, true);
|
|
2901
|
+
}, []);
|
|
2902
|
+
const root = useRef(null);
|
|
2903
|
+
const activeFamily = active?.family;
|
|
2904
|
+
useEffect(() => {
|
|
2905
|
+
const focused = document.activeElement;
|
|
2906
|
+
if (root.current !== null && focused instanceof HTMLElement && root.current.contains(focused) && !(focused instanceof HTMLInputElement)) root.current.querySelector(".nx-color-grid [aria-checked=\"true\"]")?.focus();
|
|
2907
|
+
}, [activeFamily]);
|
|
2908
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2909
|
+
ref: root,
|
|
2910
|
+
className: "nx-color-picker",
|
|
2911
|
+
children: [
|
|
2912
|
+
/* @__PURE__ */ jsx("h4", {
|
|
2913
|
+
className: "nx-color-picker-title",
|
|
2914
|
+
children: label
|
|
2915
|
+
}),
|
|
2916
|
+
/* @__PURE__ */ jsx("span", {
|
|
2917
|
+
className: "nx-caption",
|
|
2918
|
+
children: t("color.colors")
|
|
2919
|
+
}),
|
|
2920
|
+
/* @__PURE__ */ jsx("div", {
|
|
2921
|
+
className: "nx-color-grid",
|
|
2922
|
+
role: "radiogroup",
|
|
2923
|
+
"aria-label": t("color.colors"),
|
|
2924
|
+
children: COLOR_GRID.map(({ key, family }) => {
|
|
2925
|
+
const color = familyColor(family, shade);
|
|
2926
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
2927
|
+
type: "button",
|
|
2928
|
+
role: "radio",
|
|
2929
|
+
"aria-checked": active?.family === family,
|
|
2930
|
+
"aria-label": `${t(`color.name.${family}`)} (${key.toUpperCase()})`,
|
|
2931
|
+
title: `${t(`color.name.${family}`)} — ${key.toUpperCase()}`,
|
|
2932
|
+
className: cx("nx-color-cell", isDarkColor(color) && "nx-on-dark"),
|
|
2933
|
+
"data-testid": `color-${family}`,
|
|
2934
|
+
"data-autofocus": active?.family === family ? "" : void 0,
|
|
2935
|
+
onClick: () => onChange(color),
|
|
2936
|
+
children: [/* @__PURE__ */ jsx(Swatch, { color }), /* @__PURE__ */ jsx("span", {
|
|
2937
|
+
className: "nx-color-key",
|
|
2938
|
+
children: key
|
|
2939
|
+
})]
|
|
2940
|
+
}, key);
|
|
2941
|
+
})
|
|
2942
|
+
}),
|
|
2943
|
+
/* @__PURE__ */ jsx("span", {
|
|
2944
|
+
className: "nx-caption",
|
|
2945
|
+
children: t("color.shades")
|
|
2946
|
+
}),
|
|
2947
|
+
shades.length === 0 ? /* @__PURE__ */ jsx("p", {
|
|
2948
|
+
className: "nx-color-no-shades",
|
|
2949
|
+
"data-testid": "color-no-shades",
|
|
2950
|
+
children: t("color.noShades")
|
|
2951
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
2952
|
+
className: "nx-color-shades",
|
|
2953
|
+
role: "radiogroup",
|
|
2954
|
+
"aria-label": t("color.shades"),
|
|
2955
|
+
children: shades.map((color, index) => /* @__PURE__ */ jsxs("button", {
|
|
2956
|
+
type: "button",
|
|
2957
|
+
role: "radio",
|
|
2958
|
+
"aria-checked": active?.shade === index,
|
|
2959
|
+
"aria-label": `${t("color.shade", { index: index + 1 })} (Shift+${index + 1})`,
|
|
2960
|
+
title: `${t("color.shade", { index: index + 1 })} — Shift+${index + 1}`,
|
|
2961
|
+
className: cx("nx-color-cell", isDarkColor(color) && "nx-on-dark"),
|
|
2962
|
+
"data-testid": `color-shade-${index + 1}`,
|
|
2963
|
+
onClick: () => onChange(color),
|
|
2964
|
+
children: [/* @__PURE__ */ jsx(Swatch, { color }), /* @__PURE__ */ jsxs("span", {
|
|
2965
|
+
className: "nx-color-key",
|
|
2966
|
+
children: ["⇧", index + 1]
|
|
2967
|
+
})]
|
|
2968
|
+
}, color))
|
|
2969
|
+
}),
|
|
2970
|
+
/* @__PURE__ */ jsx("span", {
|
|
2971
|
+
className: "nx-caption",
|
|
2972
|
+
children: t("color.hex")
|
|
2973
|
+
}),
|
|
2974
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2975
|
+
className: "nx-color-hex",
|
|
2976
|
+
children: [
|
|
2977
|
+
/* @__PURE__ */ jsx("span", {
|
|
2978
|
+
"aria-hidden": true,
|
|
2979
|
+
children: "#"
|
|
2980
|
+
}),
|
|
2981
|
+
/* @__PURE__ */ jsx("input", {
|
|
2982
|
+
type: "text",
|
|
2983
|
+
value: shown,
|
|
2984
|
+
spellCheck: false,
|
|
2985
|
+
maxLength: 7,
|
|
2986
|
+
"aria-label": t("color.hex"),
|
|
2987
|
+
"data-testid": "color-hex",
|
|
2988
|
+
placeholder: value === void 0 ? t("properties.mixed") : "",
|
|
2989
|
+
onChange: (event) => {
|
|
2990
|
+
setDraft(event.target.value.replace(/^#/, ""));
|
|
2991
|
+
const next = normalizeHex(event.target.value);
|
|
2992
|
+
if (next !== void 0 && event.target.value.replace(/^#/, "").length === 6) onChange(next);
|
|
2993
|
+
},
|
|
2994
|
+
onKeyDown: (event) => {
|
|
2995
|
+
if (event.key === "Enter") {
|
|
2996
|
+
const next = normalizeHex(shown);
|
|
2997
|
+
if (next !== void 0) onChange(next);
|
|
2998
|
+
setDraft(void 0);
|
|
2999
|
+
}
|
|
3000
|
+
},
|
|
3001
|
+
onBlur: () => {
|
|
3002
|
+
const next = normalizeHex(shown);
|
|
3003
|
+
if (next !== void 0 && next !== hex) onChange(next);
|
|
3004
|
+
setDraft(void 0);
|
|
3005
|
+
}
|
|
3006
|
+
}),
|
|
3007
|
+
eyeDropper !== void 0 && /* @__PURE__ */ jsx(IconButton, {
|
|
3008
|
+
icon: "pipette",
|
|
3009
|
+
label: t("color.picker"),
|
|
3010
|
+
testId: "color-eyedropper",
|
|
3011
|
+
onClick: () => {
|
|
3012
|
+
eyeDropper().then((picked) => {
|
|
3013
|
+
const next = picked === void 0 ? void 0 : normalizeHex(picked);
|
|
3014
|
+
if (next !== void 0) onChange(next);
|
|
3015
|
+
}).catch(() => void 0);
|
|
3016
|
+
}
|
|
3017
|
+
})
|
|
3018
|
+
]
|
|
3019
|
+
})
|
|
3020
|
+
]
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
/**
|
|
3024
|
+
* Quick-pick swatches, then the current color as a button that opens the {@link ColorPicker}
|
|
3025
|
+
* (Excalidraw's stroke and background controls).
|
|
3026
|
+
*/
|
|
3027
|
+
function ColorField({ label, value, palette, onChange, kind = "stroke", testId, disabled }) {
|
|
3028
|
+
const { t } = useUi();
|
|
3029
|
+
const [open, setOpen] = useState(false);
|
|
3030
|
+
const anchor = useRef(null);
|
|
3031
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3032
|
+
className: "nx-color-field",
|
|
3033
|
+
"data-testid": testId,
|
|
3034
|
+
children: [
|
|
3035
|
+
/* @__PURE__ */ jsx(Choice, {
|
|
3036
|
+
label,
|
|
3037
|
+
value,
|
|
3038
|
+
disabled,
|
|
3039
|
+
options: palette.map((color) => ({
|
|
3040
|
+
value: color,
|
|
3041
|
+
label: color === "transparent" ? t("value.none") : color,
|
|
3042
|
+
swatch: color
|
|
3043
|
+
})),
|
|
3044
|
+
onChange
|
|
3045
|
+
}),
|
|
3046
|
+
/* @__PURE__ */ jsx("span", {
|
|
3047
|
+
className: "nx-color-divider",
|
|
3048
|
+
"aria-hidden": true
|
|
3049
|
+
}),
|
|
3050
|
+
/* @__PURE__ */ jsx("button", {
|
|
3051
|
+
ref: anchor,
|
|
3052
|
+
type: "button",
|
|
3053
|
+
className: "nx-choice-option nx-swatch-option nx-color-current",
|
|
3054
|
+
"aria-label": t("color.open", { label }),
|
|
3055
|
+
title: t("color.open", { label }),
|
|
3056
|
+
"aria-haspopup": "dialog",
|
|
3057
|
+
"aria-expanded": open,
|
|
3058
|
+
disabled,
|
|
3059
|
+
"data-testid": "color-picker-toggle",
|
|
3060
|
+
onClick: () => setOpen(!open),
|
|
3061
|
+
children: /* @__PURE__ */ jsx(Swatch, { color: value })
|
|
3062
|
+
}),
|
|
3063
|
+
/* @__PURE__ */ jsx(Popover, {
|
|
3064
|
+
open,
|
|
3065
|
+
onClose: () => setOpen(false),
|
|
3066
|
+
anchor,
|
|
3067
|
+
placement: "right-start",
|
|
3068
|
+
label,
|
|
3069
|
+
testId: "color-picker",
|
|
3070
|
+
children: /* @__PURE__ */ jsx(ColorPicker, {
|
|
3071
|
+
label,
|
|
3072
|
+
value,
|
|
3073
|
+
kind,
|
|
3074
|
+
onChange
|
|
3075
|
+
})
|
|
3076
|
+
})
|
|
3077
|
+
]
|
|
3078
|
+
});
|
|
3079
|
+
}
|
|
3080
|
+
// src/features/main-menu/canvas-properties.tsx
|
|
3081
|
+
const BACKGROUNDS = [
|
|
3082
|
+
"#ffffff",
|
|
3083
|
+
"#f8f9fa",
|
|
3084
|
+
"#f5faff",
|
|
3085
|
+
"#fffce8",
|
|
3086
|
+
"#fdf8f6",
|
|
3087
|
+
"#121212"
|
|
3088
|
+
];
|
|
3089
|
+
const GRID_COLORS = [
|
|
3090
|
+
"#868e96",
|
|
3091
|
+
"#ced4da",
|
|
3092
|
+
"#343a40",
|
|
3093
|
+
"#a5d8ff",
|
|
3094
|
+
"#ffc9c9"
|
|
3095
|
+
];
|
|
3096
|
+
const GRID_TYPES = [
|
|
3097
|
+
{
|
|
3098
|
+
value: "none",
|
|
3099
|
+
icon: "gridNone"
|
|
3100
|
+
},
|
|
3101
|
+
{
|
|
3102
|
+
value: "square",
|
|
3103
|
+
icon: "gridSquare"
|
|
3104
|
+
},
|
|
3105
|
+
{
|
|
3106
|
+
value: "dot",
|
|
3107
|
+
icon: "gridDot"
|
|
3108
|
+
},
|
|
3109
|
+
{
|
|
3110
|
+
value: "hex-pointy",
|
|
3111
|
+
icon: "gridHexPointy"
|
|
3112
|
+
},
|
|
3113
|
+
{
|
|
3114
|
+
value: "hex-flat",
|
|
3115
|
+
icon: "gridHexFlat"
|
|
3116
|
+
}
|
|
3117
|
+
];
|
|
3118
|
+
/** Shared canvas background and grid settings (grid visibility and snapping are view toggles). */
|
|
3119
|
+
function CanvasPropertiesPanel() {
|
|
3120
|
+
const editor = useEditor();
|
|
3121
|
+
const { t } = useUi();
|
|
3122
|
+
const background = useBackground();
|
|
3123
|
+
const grid = useGrid();
|
|
3124
|
+
const canCanvas = usePermission("core.manage-canvas");
|
|
3125
|
+
const canGrid = usePermission("core.manage-grid");
|
|
3126
|
+
const type = grid.config.type;
|
|
3127
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3128
|
+
className: "nx-canvas-properties",
|
|
3129
|
+
"data-testid": "canvas-properties",
|
|
3130
|
+
children: [
|
|
3131
|
+
/* @__PURE__ */ jsxs(Section, {
|
|
3132
|
+
title: t("canvas.background"),
|
|
3133
|
+
children: [/* @__PURE__ */ jsx(ColorField, {
|
|
3134
|
+
label: t("canvas.background"),
|
|
3135
|
+
value: background,
|
|
3136
|
+
palette: BACKGROUNDS,
|
|
3137
|
+
kind: "background",
|
|
3138
|
+
disabled: !canCanvas,
|
|
3139
|
+
onChange: (color) => editor.document.setBackground(color),
|
|
3140
|
+
testId: "canvas-background"
|
|
3141
|
+
}), background !== void 0 && /* @__PURE__ */ jsx("button", {
|
|
3142
|
+
type: "button",
|
|
3143
|
+
className: "nx-link",
|
|
3144
|
+
disabled: !canCanvas,
|
|
3145
|
+
onClick: () => editor.document.setBackground(void 0),
|
|
3146
|
+
children: t("canvas.backgroundReset")
|
|
3147
|
+
})]
|
|
3148
|
+
}),
|
|
3149
|
+
/* @__PURE__ */ jsxs("div", {
|
|
3150
|
+
className: "nx-menu-row",
|
|
3151
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("grid.label") }), /* @__PURE__ */ jsx(Choice, {
|
|
3152
|
+
label: t("grid.type"),
|
|
3153
|
+
value: type,
|
|
3154
|
+
disabled: !canGrid,
|
|
3155
|
+
options: GRID_TYPES.map(({ value, icon }) => ({
|
|
3156
|
+
value,
|
|
3157
|
+
icon,
|
|
3158
|
+
label: t(`grid.type.${value}`)
|
|
3159
|
+
})),
|
|
3160
|
+
onChange: (value) => value === "none" ? editor.grid.clear() : editor.grid.set({ type: value }),
|
|
3161
|
+
testId: "grid-type"
|
|
3162
|
+
})]
|
|
3163
|
+
}),
|
|
3164
|
+
type !== "none" && /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
3165
|
+
/* @__PURE__ */ jsxs("label", {
|
|
3166
|
+
className: "nx-field",
|
|
3167
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("grid.size") }), /* @__PURE__ */ jsx("input", {
|
|
3168
|
+
type: "number",
|
|
3169
|
+
min: 1,
|
|
3170
|
+
max: 1e3,
|
|
3171
|
+
value: grid.config.size,
|
|
3172
|
+
disabled: !canGrid,
|
|
3173
|
+
"data-testid": "grid-size",
|
|
3174
|
+
onChange: (event) => {
|
|
3175
|
+
const size = Number(event.target.value);
|
|
3176
|
+
if (Number.isFinite(size) && size >= 1) editor.grid.set({ size });
|
|
3177
|
+
}
|
|
3178
|
+
})]
|
|
3179
|
+
}),
|
|
3180
|
+
(type === "square" || type === "dot") && /* @__PURE__ */ jsxs("label", {
|
|
3181
|
+
className: "nx-field",
|
|
3182
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("grid.majorEvery") }), /* @__PURE__ */ jsx("input", {
|
|
3183
|
+
type: "number",
|
|
3184
|
+
min: 1,
|
|
3185
|
+
max: 100,
|
|
3186
|
+
value: grid.config.majorEvery ?? 1,
|
|
3187
|
+
disabled: !canGrid,
|
|
3188
|
+
onChange: (event) => {
|
|
3189
|
+
const every = Math.round(Number(event.target.value));
|
|
3190
|
+
if (every >= 1 && every <= 100) editor.grid.set({ majorEvery: every });
|
|
3191
|
+
}
|
|
3192
|
+
})]
|
|
3193
|
+
}),
|
|
3194
|
+
/* @__PURE__ */ jsx(Section, {
|
|
3195
|
+
title: t("grid.color"),
|
|
3196
|
+
children: /* @__PURE__ */ jsx(ColorField, {
|
|
3197
|
+
label: t("grid.color"),
|
|
3198
|
+
value: grid.config.color,
|
|
3199
|
+
palette: GRID_COLORS,
|
|
3200
|
+
disabled: !canGrid,
|
|
3201
|
+
onChange: (color) => editor.grid.set({
|
|
3202
|
+
color,
|
|
3203
|
+
majorColor: void 0
|
|
3204
|
+
}),
|
|
3205
|
+
testId: "grid-color"
|
|
3206
|
+
})
|
|
3207
|
+
})
|
|
3208
|
+
] })
|
|
3209
|
+
]
|
|
3210
|
+
});
|
|
3211
|
+
}
|
|
3212
|
+
// src/features/main-menu/main-menu.tsx
|
|
3213
|
+
/** Opens a document file or bundle chosen by the user (replace import). */
|
|
3214
|
+
async function openFile(editor, file) {
|
|
3215
|
+
const parsed = JSON.parse(await file.text());
|
|
3216
|
+
if (parsed !== null && typeof parsed === "object" && Reflect.get(parsed, "format") === "interactive-canvas-bundle") await editor.io.importBundle(parsed);
|
|
3217
|
+
else editor.io.importDocument(parsed);
|
|
3218
|
+
await editor.viewport.zoomToFit({ padding: 48 });
|
|
3219
|
+
}
|
|
3220
|
+
/** Hamburger menu: open and save files, export, shortcuts, reset, then theme and canvas settings inline. */
|
|
3221
|
+
function MainMenu({ items, fileName = "canvas" }) {
|
|
3222
|
+
const editor = useEditor();
|
|
3223
|
+
const { t, isOpen, setOpen, setTheme, themeSetting, locale, setLocale, isHidden } = useUi();
|
|
3224
|
+
const anchor = useRef(null);
|
|
3225
|
+
const input = useRef(null);
|
|
3226
|
+
const [error, setError] = useState(void 0);
|
|
3227
|
+
const open = isOpen("mainMenu");
|
|
3228
|
+
const fail = (cause) => setError(t("error.import", { message: cause instanceof Error ? cause.message : String(cause) }));
|
|
3229
|
+
const entries = [
|
|
3230
|
+
{
|
|
3231
|
+
id: "open",
|
|
3232
|
+
label: t("menu.open"),
|
|
3233
|
+
icon: "open",
|
|
3234
|
+
onSelect: () => input.current?.click()
|
|
3235
|
+
},
|
|
3236
|
+
{
|
|
3237
|
+
id: "save",
|
|
3238
|
+
label: t("menu.save"),
|
|
3239
|
+
icon: "save",
|
|
3240
|
+
onSelect: () => download(`${fileName}.json`, editor.io.stringify(editor.io.exportDocument({
|
|
3241
|
+
pendingAssets: "keep",
|
|
3242
|
+
meta: { app: "drawback" }
|
|
3243
|
+
})))
|
|
3244
|
+
},
|
|
3245
|
+
{
|
|
3246
|
+
id: "saveBundle",
|
|
3247
|
+
label: t("menu.exportBundle"),
|
|
3248
|
+
icon: "download",
|
|
3249
|
+
onSelect: () => void editor.io.exportBundle().then(({ bundle }) => download(`${fileName}.bundle.json`, editor.io.stringify(bundle))).catch(fail)
|
|
3250
|
+
},
|
|
3251
|
+
{
|
|
3252
|
+
id: "exportImage",
|
|
3253
|
+
label: t("menu.exportImage"),
|
|
3254
|
+
icon: "download",
|
|
3255
|
+
onSelect: () => setOpen("export", true)
|
|
3256
|
+
},
|
|
3257
|
+
{
|
|
3258
|
+
id: "shortcuts",
|
|
3259
|
+
label: t("menu.shortcuts"),
|
|
3260
|
+
icon: "keyboard",
|
|
3261
|
+
separatorBefore: true,
|
|
3262
|
+
shortcut: "?",
|
|
3263
|
+
onSelect: () => setOpen("shortcuts", true)
|
|
3264
|
+
},
|
|
3265
|
+
{
|
|
3266
|
+
id: "reset",
|
|
3267
|
+
label: t("menu.reset"),
|
|
3268
|
+
icon: "reset",
|
|
3269
|
+
danger: true,
|
|
3270
|
+
separatorBefore: true,
|
|
3271
|
+
disabled: !editor.permissions.can("core.delete"),
|
|
3272
|
+
onSelect: () => {
|
|
3273
|
+
if (typeof confirm === "function" && !confirm(t("menu.resetConfirm"))) return;
|
|
3274
|
+
editor.transform.delete(editor.elements.query().map((element) => element.id));
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3277
|
+
].filter((entry) => !isHidden(`menu:${entry.id}`));
|
|
3278
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
3279
|
+
/* @__PURE__ */ jsx("span", {
|
|
3280
|
+
ref: anchor,
|
|
3281
|
+
className: "nx-anchor",
|
|
3282
|
+
children: /* @__PURE__ */ jsx(IconButton, {
|
|
3283
|
+
icon: "menu",
|
|
3284
|
+
label: t("menu.label"),
|
|
3285
|
+
pressed: open,
|
|
3286
|
+
onClick: () => setOpen("mainMenu", !open),
|
|
3287
|
+
testId: "main-menu"
|
|
3288
|
+
})
|
|
3289
|
+
}),
|
|
3290
|
+
/* @__PURE__ */ jsx("input", {
|
|
3291
|
+
ref: input,
|
|
3292
|
+
type: "file",
|
|
3293
|
+
accept: ".json,application/json",
|
|
3294
|
+
hidden: true,
|
|
3295
|
+
"data-testid": "open-file",
|
|
3296
|
+
onChange: (event) => {
|
|
3297
|
+
const file = event.target.files?.[0];
|
|
3298
|
+
event.target.value = "";
|
|
3299
|
+
if (file !== void 0) {
|
|
3300
|
+
setError(void 0);
|
|
3301
|
+
openFile(editor, file).catch(fail);
|
|
3302
|
+
}
|
|
3303
|
+
}
|
|
3304
|
+
}),
|
|
3305
|
+
/* @__PURE__ */ jsxs(Popover, {
|
|
3306
|
+
open,
|
|
3307
|
+
onClose: () => setOpen("mainMenu", false),
|
|
3308
|
+
anchor,
|
|
3309
|
+
label: t("menu.label"),
|
|
3310
|
+
role: "menu",
|
|
3311
|
+
testId: "main-menu-popover",
|
|
3312
|
+
children: [
|
|
3313
|
+
/* @__PURE__ */ jsx(MenuItems, {
|
|
3314
|
+
entries,
|
|
3315
|
+
onDone: () => setOpen("mainMenu", false)
|
|
3316
|
+
}),
|
|
3317
|
+
items,
|
|
3318
|
+
(!isHidden("menu:theme") || !isHidden("menu:language") || !isHidden("menu:canvas")) && /* @__PURE__ */ jsx("div", {
|
|
3319
|
+
className: "nx-separator",
|
|
3320
|
+
role: "separator"
|
|
3321
|
+
}),
|
|
3322
|
+
/* @__PURE__ */ jsxs("div", {
|
|
3323
|
+
className: "nx-menu-settings",
|
|
3324
|
+
children: [
|
|
3325
|
+
!isHidden("menu:theme") && /* @__PURE__ */ jsxs("div", {
|
|
3326
|
+
className: "nx-menu-row",
|
|
3327
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("theme.label") }), /* @__PURE__ */ jsx(Choice, {
|
|
3328
|
+
label: t("theme.label"),
|
|
3329
|
+
value: themeSetting,
|
|
3330
|
+
options: [
|
|
3331
|
+
{
|
|
3332
|
+
value: "light",
|
|
3333
|
+
label: t("theme.light"),
|
|
3334
|
+
icon: "light"
|
|
3335
|
+
},
|
|
3336
|
+
{
|
|
3337
|
+
value: "dark",
|
|
3338
|
+
label: t("theme.dark"),
|
|
3339
|
+
icon: "dark"
|
|
3340
|
+
},
|
|
3341
|
+
{
|
|
3342
|
+
value: "system",
|
|
3343
|
+
label: t("theme.system"),
|
|
3344
|
+
icon: "system"
|
|
3345
|
+
}
|
|
3346
|
+
],
|
|
3347
|
+
onChange: setTheme,
|
|
3348
|
+
testId: "theme"
|
|
3349
|
+
})]
|
|
3350
|
+
}),
|
|
3351
|
+
!isHidden("menu:language") && /* @__PURE__ */ jsxs("label", {
|
|
3352
|
+
className: "nx-field",
|
|
3353
|
+
children: [/* @__PURE__ */ jsx("span", { children: t("language.label") }), /* @__PURE__ */ jsx("select", {
|
|
3354
|
+
value: LANGUAGES.find((entry) => locale.startsWith(entry.locale))?.locale ?? locale,
|
|
3355
|
+
"data-testid": "language",
|
|
3356
|
+
onChange: (event) => setLocale(event.target.value),
|
|
3357
|
+
children: LANGUAGES.map((entry) => /* @__PURE__ */ jsx("option", {
|
|
3358
|
+
value: entry.locale,
|
|
3359
|
+
lang: entry.locale,
|
|
3360
|
+
children: entry.name
|
|
3361
|
+
}, entry.locale))
|
|
3362
|
+
})]
|
|
3363
|
+
}),
|
|
3364
|
+
!isHidden("menu:canvas") && /* @__PURE__ */ jsx(CanvasPropertiesPanel, {})
|
|
3365
|
+
]
|
|
3366
|
+
})
|
|
3367
|
+
]
|
|
3368
|
+
}),
|
|
3369
|
+
error !== void 0 && /* @__PURE__ */ jsx(Portal, { children: /* @__PURE__ */ jsxs("div", {
|
|
3370
|
+
className: "nx-toast",
|
|
3371
|
+
role: "alert",
|
|
3372
|
+
"data-testid": "ui-error",
|
|
3373
|
+
children: [error, /* @__PURE__ */ jsx(IconButton, {
|
|
3374
|
+
icon: "close",
|
|
3375
|
+
label: t("dialog.close"),
|
|
3376
|
+
onClick: () => setError(void 0)
|
|
3377
|
+
})]
|
|
3378
|
+
}) })
|
|
3379
|
+
] });
|
|
3380
|
+
}
|
|
3381
|
+
// src/features/shortcuts/shortcuts-dialog.tsx
|
|
3382
|
+
const ARROWS = "←↑→↓";
|
|
3383
|
+
const ARROW_KEYS = [
|
|
3384
|
+
"ArrowLeft",
|
|
3385
|
+
"ArrowUp",
|
|
3386
|
+
"ArrowRight",
|
|
3387
|
+
"ArrowDown"
|
|
3388
|
+
];
|
|
3389
|
+
/** Replaces the four arrow keys with one `←↑→↓` entry per modifier combination. */
|
|
3390
|
+
function compactArrows(keys) {
|
|
3391
|
+
const result = [];
|
|
3392
|
+
for (const key of keys) {
|
|
3393
|
+
const arrow = ARROW_KEYS.find((name) => key.endsWith(name));
|
|
3394
|
+
const prefix = arrow === void 0 ? "" : key.slice(0, -arrow.length);
|
|
3395
|
+
if (arrow === void 0 || !ARROW_KEYS.every((name) => keys.includes(prefix + name))) result.push(key);
|
|
3396
|
+
else if (!result.includes(prefix + ARROWS)) result.push(prefix + ARROWS);
|
|
3397
|
+
}
|
|
3398
|
+
return result;
|
|
3399
|
+
}
|
|
3400
|
+
/** Searchable list of tool shortcuts and keymap bindings with command labels. */
|
|
3401
|
+
function ShortcutsDialog() {
|
|
3402
|
+
const editor = useEditor();
|
|
3403
|
+
const { t, isOpen, setOpen } = useUi();
|
|
3404
|
+
const [query, setQuery] = useState("");
|
|
3405
|
+
const open = isOpen("shortcuts");
|
|
3406
|
+
const sections = useMemo(() => {
|
|
3407
|
+
if (!open) return [];
|
|
3408
|
+
const tools = editor.tools.list().filter((tool) => tool.shortcuts.length > 0).map((tool) => {
|
|
3409
|
+
const translated = t(`tool.${tool.id}`);
|
|
3410
|
+
return {
|
|
3411
|
+
id: tool.id,
|
|
3412
|
+
label: translated === `tool.${tool.id}` ? tool.label ?? tool.id : translated,
|
|
3413
|
+
keys: tool.shortcuts
|
|
3414
|
+
};
|
|
3415
|
+
});
|
|
3416
|
+
const commands = /* @__PURE__ */ new Map();
|
|
3417
|
+
for (const binding of editor.input.keymap.list()) {
|
|
3418
|
+
const label = editor.commands.label(binding.commandId);
|
|
3419
|
+
if (label === void 0 || binding.commandId === "core.tool.activate") continue;
|
|
3420
|
+
const entry = commands.get(binding.commandId) ?? {
|
|
3421
|
+
id: binding.commandId,
|
|
3422
|
+
label,
|
|
3423
|
+
keys: []
|
|
3424
|
+
};
|
|
3425
|
+
if (!entry.keys.includes(binding.shortcut)) entry.keys.push(binding.shortcut);
|
|
3426
|
+
commands.set(binding.commandId, entry);
|
|
3427
|
+
}
|
|
3428
|
+
const needle = query.trim().toLowerCase();
|
|
3429
|
+
const filter = (entries) => entries.filter((entry) => needle === "" || entry.label.toLowerCase().includes(needle));
|
|
3430
|
+
return [{
|
|
3431
|
+
title: t("shortcuts.tools"),
|
|
3432
|
+
entries: filter(tools)
|
|
3433
|
+
}, {
|
|
3434
|
+
title: t("shortcuts.commands"),
|
|
3435
|
+
entries: filter([...commands.values()])
|
|
3436
|
+
}];
|
|
3437
|
+
}, [
|
|
3438
|
+
editor,
|
|
3439
|
+
open,
|
|
3440
|
+
query,
|
|
3441
|
+
t
|
|
3442
|
+
]);
|
|
3443
|
+
return /* @__PURE__ */ jsxs(Dialog, {
|
|
3444
|
+
open,
|
|
3445
|
+
onClose: () => setOpen("shortcuts", false),
|
|
3446
|
+
title: t("shortcuts.title"),
|
|
3447
|
+
testId: "shortcuts-dialog",
|
|
3448
|
+
wide: true,
|
|
3449
|
+
children: [/* @__PURE__ */ jsx("label", {
|
|
3450
|
+
className: "nx-search",
|
|
3451
|
+
children: /* @__PURE__ */ jsx("input", {
|
|
3452
|
+
type: "search",
|
|
3453
|
+
placeholder: t("shortcuts.search"),
|
|
3454
|
+
"aria-label": t("shortcuts.search"),
|
|
3455
|
+
value: query,
|
|
3456
|
+
"data-testid": "shortcuts-search",
|
|
3457
|
+
onChange: (event) => setQuery(event.target.value)
|
|
3458
|
+
})
|
|
3459
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
3460
|
+
className: "nx-shortcuts",
|
|
3461
|
+
children: sections.map((section) => /* @__PURE__ */ jsxs("section", { children: [/* @__PURE__ */ jsx("h3", {
|
|
3462
|
+
className: "nx-section-title",
|
|
3463
|
+
children: section.title
|
|
3464
|
+
}), /* @__PURE__ */ jsx("dl", { children: section.entries.map((entry) => /* @__PURE__ */ jsxs("div", {
|
|
3465
|
+
className: "nx-shortcut-row",
|
|
3466
|
+
children: [/* @__PURE__ */ jsx("dt", { children: entry.label }), /* @__PURE__ */ jsx("dd", { children: compactArrows(entry.keys).map((key) => /* @__PURE__ */ jsx("kbd", { children: formatShortcut(key) }, key)) })]
|
|
3467
|
+
}, entry.id)) })] }, section.title))
|
|
3468
|
+
})]
|
|
3469
|
+
});
|
|
3470
|
+
}
|
|
3471
|
+
// src/features/minimap/minimap.tsx
|
|
3472
|
+
const PALETTES = {
|
|
3473
|
+
light: {
|
|
3474
|
+
background: "rgba(255, 255, 255, 0.9)",
|
|
3475
|
+
element: "#868e96",
|
|
3476
|
+
viewport: "#4263eb",
|
|
3477
|
+
viewportFill: "rgba(66, 99, 235, 0.1)"
|
|
3478
|
+
},
|
|
3479
|
+
dark: {
|
|
3480
|
+
background: "rgba(35, 35, 41, 0.9)",
|
|
3481
|
+
element: "#adb5bd",
|
|
3482
|
+
viewport: "#91a7ff",
|
|
3483
|
+
viewportFill: "rgba(145, 167, 255, 0.15)"
|
|
3484
|
+
}
|
|
3485
|
+
};
|
|
3486
|
+
/** Overview of the document; click or drag to navigate. */
|
|
3487
|
+
function Minimap({ width = 200, height = 140, renderers }) {
|
|
3488
|
+
const editor = useEditor();
|
|
3489
|
+
const { t, theme } = useUi();
|
|
3490
|
+
const host = useRef(null);
|
|
3491
|
+
const view = useRef(void 0);
|
|
3492
|
+
useEffect(() => {
|
|
3493
|
+
if (host.current === null) return;
|
|
3494
|
+
const minimap = mountMinimap(editor, host.current, {
|
|
3495
|
+
width,
|
|
3496
|
+
height
|
|
3497
|
+
});
|
|
3498
|
+
view.current = minimap;
|
|
3499
|
+
return () => {
|
|
3500
|
+
minimap.dispose();
|
|
3501
|
+
view.current = void 0;
|
|
3502
|
+
};
|
|
3503
|
+
}, [
|
|
3504
|
+
editor,
|
|
3505
|
+
width,
|
|
3506
|
+
height
|
|
3507
|
+
]);
|
|
3508
|
+
useEffect(() => {
|
|
3509
|
+
view.current?.setOptions({
|
|
3510
|
+
colors: PALETTES[theme],
|
|
3511
|
+
...renderers === void 0 ? {} : { renderers }
|
|
3512
|
+
});
|
|
3513
|
+
}, [theme, renderers]);
|
|
3514
|
+
return /* @__PURE__ */ jsx(Island, {
|
|
3515
|
+
className: "nx-minimap",
|
|
3516
|
+
label: t("minimap.label"),
|
|
3517
|
+
testId: "minimap-island",
|
|
3518
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
3519
|
+
ref: host,
|
|
3520
|
+
role: "img",
|
|
3521
|
+
"aria-label": t("minimap.label")
|
|
3522
|
+
})
|
|
3523
|
+
});
|
|
3524
|
+
}
|
|
3525
|
+
// src/features/properties/linear-properties.tsx
|
|
3526
|
+
function uniform(values) {
|
|
3527
|
+
const [first] = values;
|
|
3528
|
+
return values.every((value) => value === first) ? first : void 0;
|
|
3529
|
+
}
|
|
3530
|
+
function toolProps(editor, toolId) {
|
|
3531
|
+
const props = editor.tools.getDefaults(toolId).props;
|
|
3532
|
+
return props !== null && typeof props === "object" && !Array.isArray(props) ? props : {};
|
|
3533
|
+
}
|
|
3534
|
+
function setToolProps(editor, toolId, patch) {
|
|
3535
|
+
if (!editor.tools.has(toolId)) return;
|
|
3536
|
+
const defaults = editor.tools.getDefaults(toolId);
|
|
3537
|
+
editor.tools.setDefaults(toolId, {
|
|
3538
|
+
...defaults,
|
|
3539
|
+
props: {
|
|
3540
|
+
...toolProps(editor, toolId),
|
|
3541
|
+
...patch
|
|
3542
|
+
}
|
|
3543
|
+
});
|
|
3544
|
+
}
|
|
3545
|
+
function arrowTypeOf(element) {
|
|
3546
|
+
return isArrowElbowed(element) ? "elbow" : isLinearCurved(element) ? "curved" : "sharp";
|
|
3547
|
+
}
|
|
3548
|
+
function toolArrowType(props) {
|
|
3549
|
+
return props.elbowed === true ? "elbow" : props.curved === true ? "curved" : "sharp";
|
|
3550
|
+
}
|
|
3551
|
+
/**
|
|
3552
|
+
* Edges (sharp or round) and polygon closing for lines, and the arrow type for arrows. Without a
|
|
3553
|
+
* selection it edits the active line or arrow tool's defaults; choices also become new defaults.
|
|
3554
|
+
*/
|
|
3555
|
+
function LinearProperties() {
|
|
3556
|
+
const editor = useEditor();
|
|
3557
|
+
const { t } = useUi();
|
|
3558
|
+
const selected = useSelectedElements();
|
|
3559
|
+
const tool = useActiveTool();
|
|
3560
|
+
const [, refresh] = useState(0);
|
|
3561
|
+
const editable = selected.filter((element) => !editor.elements.isLocked(element.id) && editor.permissions.can("core.resize", element.id));
|
|
3562
|
+
const lines = editable.filter((element) => element.type === LINE_TYPE);
|
|
3563
|
+
const arrows = editable.filter((element) => element.type === ARROW_TYPE);
|
|
3564
|
+
const toolMode = selected.length === 0;
|
|
3565
|
+
const showLines = toolMode ? tool === LINE_TYPE : lines.length > 0;
|
|
3566
|
+
const showArrows = toolMode ? tool === ARROW_TYPE : arrows.length > 0;
|
|
3567
|
+
const polygonCandidates = lines.filter((element) => linearWorldPoints(element).length >= 3);
|
|
3568
|
+
const showPolygon = !toolMode && polygonCandidates.length > 0 && polygonCandidates.length === lines.length;
|
|
3569
|
+
if (!showLines && !showArrows) return null;
|
|
3570
|
+
const edge = toolMode ? toolProps(editor, LINE_TYPE).curved === false ? "sharp" : "round" : uniform(lines.map((element) => isLinearCurved(element) ? "round" : "sharp"));
|
|
3571
|
+
const arrowType = toolMode ? toolArrowType(toolProps(editor, ARROW_TYPE)) : uniform(arrows.map(arrowTypeOf));
|
|
3572
|
+
const outline = uniform(lines.map((element) => isLinearClosed(element) ? "closed" : "open"));
|
|
3573
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
3574
|
+
showLines && /* @__PURE__ */ jsx(Section, {
|
|
3575
|
+
title: t("properties.cornerRadius"),
|
|
3576
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
3577
|
+
label: t("properties.cornerRadius"),
|
|
3578
|
+
value: edge,
|
|
3579
|
+
options: [{
|
|
3580
|
+
value: "sharp",
|
|
3581
|
+
label: t("value.sharp"),
|
|
3582
|
+
icon: "edgeSharp"
|
|
3583
|
+
}, {
|
|
3584
|
+
value: "round",
|
|
3585
|
+
label: t("value.round"),
|
|
3586
|
+
icon: "edgeRound"
|
|
3587
|
+
}],
|
|
3588
|
+
onChange: (next) => {
|
|
3589
|
+
const curved = next === "round";
|
|
3590
|
+
if (lines.length > 0) editor.linear.setCurved(lines.map((element) => element.id), curved);
|
|
3591
|
+
setToolProps(editor, LINE_TYPE, { curved });
|
|
3592
|
+
refresh((count) => count + 1);
|
|
3593
|
+
},
|
|
3594
|
+
testId: "line-edges"
|
|
3595
|
+
})
|
|
3596
|
+
}),
|
|
3597
|
+
showPolygon && /* @__PURE__ */ jsx(Section, {
|
|
3598
|
+
title: t("properties.polygon"),
|
|
3599
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
3600
|
+
label: t("properties.polygon"),
|
|
3601
|
+
value: outline,
|
|
3602
|
+
options: [{
|
|
3603
|
+
value: "open",
|
|
3604
|
+
label: t("value.open"),
|
|
3605
|
+
icon: "polyline"
|
|
3606
|
+
}, {
|
|
3607
|
+
value: "closed",
|
|
3608
|
+
label: t("value.closed"),
|
|
3609
|
+
icon: "polygon"
|
|
3610
|
+
}],
|
|
3611
|
+
onChange: (next) => editor.linear.setClosed(lines.map((element) => element.id), next === "closed"),
|
|
3612
|
+
testId: "line-polygon"
|
|
3613
|
+
})
|
|
3614
|
+
}),
|
|
3615
|
+
showArrows && /* @__PURE__ */ jsx(Section, {
|
|
3616
|
+
title: t("properties.arrowType"),
|
|
3617
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
3618
|
+
label: t("properties.arrowType"),
|
|
3619
|
+
value: arrowType,
|
|
3620
|
+
options: [
|
|
3621
|
+
{
|
|
3622
|
+
value: "sharp",
|
|
3623
|
+
label: t("value.sharp"),
|
|
3624
|
+
icon: "arrowSharp"
|
|
3625
|
+
},
|
|
3626
|
+
{
|
|
3627
|
+
value: "curved",
|
|
3628
|
+
label: t("value.curved"),
|
|
3629
|
+
icon: "arrowCurved"
|
|
3630
|
+
},
|
|
3631
|
+
{
|
|
3632
|
+
value: "elbow",
|
|
3633
|
+
label: t("value.elbow"),
|
|
3634
|
+
icon: "arrowElbow"
|
|
3635
|
+
}
|
|
3636
|
+
],
|
|
3637
|
+
onChange: (next) => {
|
|
3638
|
+
if (arrows.length > 0) editor.linear.setArrowType(arrows.map((element) => element.id), next);
|
|
3639
|
+
setToolProps(editor, ARROW_TYPE, {
|
|
3640
|
+
curved: next === "curved",
|
|
3641
|
+
elbowed: next === "elbow"
|
|
3642
|
+
});
|
|
3643
|
+
refresh((count) => count + 1);
|
|
3644
|
+
},
|
|
3645
|
+
testId: "arrow-type"
|
|
3646
|
+
})
|
|
3647
|
+
})
|
|
3648
|
+
] });
|
|
3649
|
+
}
|
|
3650
|
+
// src/components/font-picker/font-picker.tsx
|
|
3651
|
+
/** The three quick picks next to the font picker, like Excalidraw's. */
|
|
3652
|
+
const QUICK_FONTS = [
|
|
3653
|
+
{
|
|
3654
|
+
id: "core.hand",
|
|
3655
|
+
icon: "fontHand"
|
|
3656
|
+
},
|
|
3657
|
+
{
|
|
3658
|
+
id: "core.sans",
|
|
3659
|
+
icon: "fontSans"
|
|
3660
|
+
},
|
|
3661
|
+
{
|
|
3662
|
+
id: "core.mono",
|
|
3663
|
+
icon: "fontMono"
|
|
3664
|
+
}
|
|
3665
|
+
];
|
|
3666
|
+
const CATEGORY_ICONS = {
|
|
3667
|
+
sans: "fontSans",
|
|
3668
|
+
serif: "fontSerif",
|
|
3669
|
+
mono: "fontMono",
|
|
3670
|
+
hand: "fontHand",
|
|
3671
|
+
display: "fontDisplay"
|
|
3672
|
+
};
|
|
3673
|
+
/** Role name of a built-in id ("Hand-drawn"), else the font's own name. */
|
|
3674
|
+
function fontRoleLabel(editor, t, id) {
|
|
3675
|
+
const translated = t(`font.${id}`);
|
|
3676
|
+
return translated === `font.${id}` ? fontName(editor.fonts.get(id), t, id) : translated;
|
|
3677
|
+
}
|
|
3678
|
+
/** The font's own name ("Inter"), falling back to the role name for system stacks. */
|
|
3679
|
+
function fontName(font, t, id) {
|
|
3680
|
+
if (font?.label !== void 0) return font.label;
|
|
3681
|
+
const translated = t(`font.${id}`);
|
|
3682
|
+
return translated === `font.${id}` ? font?.family ?? id : translated;
|
|
3683
|
+
}
|
|
3684
|
+
function previewFont(editor, id) {
|
|
3685
|
+
return editor.fonts.css({
|
|
3686
|
+
family: id,
|
|
3687
|
+
size: 15,
|
|
3688
|
+
weight: 400,
|
|
3689
|
+
style: "normal"
|
|
3690
|
+
});
|
|
3691
|
+
}
|
|
3692
|
+
/**
|
|
3693
|
+
* Searchable list of every registered font, with the fonts the document uses first. Each name
|
|
3694
|
+
* is drawn in its own font. Arrow keys move, Enter picks.
|
|
3695
|
+
*/
|
|
3696
|
+
function FontPicker({ value, onChange }) {
|
|
3697
|
+
const editor = useEditor();
|
|
3698
|
+
const { t } = useUi();
|
|
3699
|
+
const listId = useId();
|
|
3700
|
+
const [query, setQuery] = useState("");
|
|
3701
|
+
const [activeId, setActiveId] = useState(void 0);
|
|
3702
|
+
const list = useRef(null);
|
|
3703
|
+
const fonts = useEditorValue((current) => current.fonts.list(), { events: ["font.status"] });
|
|
3704
|
+
const used = useEditorValue((current) => current.fonts.used(), {
|
|
3705
|
+
events: ["document.change", "document.load"],
|
|
3706
|
+
equals: (left, right) => left.length === right.length && left.every((id, index) => id === right[index])
|
|
3707
|
+
});
|
|
3708
|
+
useEffect(() => {
|
|
3709
|
+
editor.fonts.load(fonts.map((font) => font.id));
|
|
3710
|
+
}, [editor, fonts]);
|
|
3711
|
+
const entries = useMemo(() => {
|
|
3712
|
+
const needle = query.trim().toLocaleLowerCase();
|
|
3713
|
+
const inScene = new Set(used);
|
|
3714
|
+
const matches = fonts.map((font) => ({
|
|
3715
|
+
id: font.id,
|
|
3716
|
+
label: fontName(font, t, font.id),
|
|
3717
|
+
icon: CATEGORY_ICONS[font.category ?? "sans"],
|
|
3718
|
+
section: inScene.has(font.id) ? "scene" : "available"
|
|
3719
|
+
})).filter((entry) => needle === "" || entry.label.toLocaleLowerCase().includes(needle));
|
|
3720
|
+
const byLabel = (left, right) => left.label.localeCompare(right.label);
|
|
3721
|
+
return [...matches.filter((entry) => entry.section === "scene").sort(byLabel), ...matches.filter((entry) => entry.section === "available").sort(byLabel)];
|
|
3722
|
+
}, [
|
|
3723
|
+
fonts,
|
|
3724
|
+
used,
|
|
3725
|
+
query,
|
|
3726
|
+
t
|
|
3727
|
+
]);
|
|
3728
|
+
const moved = entries.findIndex((entry) => entry.id === activeId);
|
|
3729
|
+
const current = entries.findIndex((entry) => entry.id === value);
|
|
3730
|
+
const active = moved >= 0 ? moved : query === "" && current >= 0 ? current : 0;
|
|
3731
|
+
const activate = (index) => {
|
|
3732
|
+
setActiveId(entries[index]?.id);
|
|
3733
|
+
list.current?.querySelector(`[data-index="${index}"]`)?.scrollIntoView({ block: "nearest" });
|
|
3734
|
+
};
|
|
3735
|
+
const revealCurrent = useCallback((node) => {
|
|
3736
|
+
node?.scrollIntoView({ block: "nearest" });
|
|
3737
|
+
}, []);
|
|
3738
|
+
const optionId = (index) => `${listId}-${index}`;
|
|
3739
|
+
const sections = [{
|
|
3740
|
+
id: "scene",
|
|
3741
|
+
title: t("font.inScene")
|
|
3742
|
+
}, {
|
|
3743
|
+
id: "available",
|
|
3744
|
+
title: t("font.available")
|
|
3745
|
+
}];
|
|
3746
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3747
|
+
className: "nx-font-picker",
|
|
3748
|
+
"data-testid": "font-picker-panel",
|
|
3749
|
+
children: [/* @__PURE__ */ jsxs("label", {
|
|
3750
|
+
className: "nx-font-search",
|
|
3751
|
+
children: [/* @__PURE__ */ jsx(Icon, { name: "search" }), /* @__PURE__ */ jsx("input", {
|
|
3752
|
+
type: "search",
|
|
3753
|
+
value: query,
|
|
3754
|
+
placeholder: t("font.search"),
|
|
3755
|
+
"aria-label": t("font.search"),
|
|
3756
|
+
role: "combobox",
|
|
3757
|
+
"aria-expanded": true,
|
|
3758
|
+
"aria-controls": listId,
|
|
3759
|
+
"aria-activedescendant": entries.length === 0 ? void 0 : optionId(active),
|
|
3760
|
+
"data-autofocus": "",
|
|
3761
|
+
"data-testid": "font-search",
|
|
3762
|
+
onChange: (event) => {
|
|
3763
|
+
setQuery(event.target.value);
|
|
3764
|
+
setActiveId(void 0);
|
|
3765
|
+
},
|
|
3766
|
+
onKeyDown: (event) => {
|
|
3767
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
3768
|
+
event.preventDefault();
|
|
3769
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
3770
|
+
if (entries.length > 0) activate((active + step + entries.length) % entries.length);
|
|
3771
|
+
} else if (event.key === "Enter") {
|
|
3772
|
+
const entry = entries[active];
|
|
3773
|
+
if (entry !== void 0) {
|
|
3774
|
+
event.preventDefault();
|
|
3775
|
+
onChange(entry.id);
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
})]
|
|
3780
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
3781
|
+
ref: list,
|
|
3782
|
+
id: listId,
|
|
3783
|
+
className: "nx-font-list",
|
|
3784
|
+
role: "listbox",
|
|
3785
|
+
"aria-label": t("properties.fontFamily"),
|
|
3786
|
+
children: [entries.length === 0 && /* @__PURE__ */ jsx("p", {
|
|
3787
|
+
className: "nx-font-empty",
|
|
3788
|
+
children: t("font.none")
|
|
3789
|
+
}), sections.map((section) => {
|
|
3790
|
+
const items = entries.map((entry, index) => ({
|
|
3791
|
+
entry,
|
|
3792
|
+
index
|
|
3793
|
+
})).filter(({ entry }) => entry.section === section.id);
|
|
3794
|
+
if (items.length === 0) return null;
|
|
3795
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3796
|
+
role: "group",
|
|
3797
|
+
"aria-label": section.title,
|
|
3798
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
3799
|
+
className: "nx-font-section",
|
|
3800
|
+
"aria-hidden": true,
|
|
3801
|
+
children: section.title
|
|
3802
|
+
}), items.map(({ entry, index }) => /* @__PURE__ */ jsxs("div", {
|
|
3803
|
+
id: optionId(index),
|
|
3804
|
+
role: "option",
|
|
3805
|
+
"aria-selected": entry.id === value,
|
|
3806
|
+
className: cx("nx-font-option", index === active && "nx-active", entry.id === value && "nx-selected"),
|
|
3807
|
+
"data-index": index,
|
|
3808
|
+
"data-testid": `font-option-${entry.id}`,
|
|
3809
|
+
ref: entry.id === value ? revealCurrent : void 0,
|
|
3810
|
+
onPointerMove: () => {
|
|
3811
|
+
if (index !== active) setActiveId(entry.id);
|
|
3812
|
+
},
|
|
3813
|
+
onPointerDown: (event) => event.preventDefault(),
|
|
3814
|
+
onClick: () => onChange(entry.id),
|
|
3815
|
+
children: [/* @__PURE__ */ jsx(Icon, { name: entry.icon }), /* @__PURE__ */ jsx("span", {
|
|
3816
|
+
style: { font: previewFont(editor, entry.id) },
|
|
3817
|
+
children: entry.label
|
|
3818
|
+
})]
|
|
3819
|
+
}, entry.id))]
|
|
3820
|
+
}, section.id);
|
|
3821
|
+
})]
|
|
3822
|
+
})]
|
|
3823
|
+
});
|
|
3824
|
+
}
|
|
3825
|
+
/**
|
|
3826
|
+
* Quick picks for the hand-drawn, normal and code fonts, then a button that opens the
|
|
3827
|
+
* {@link FontPicker}. The button is highlighted while any other font is set.
|
|
3828
|
+
*/
|
|
3829
|
+
function FontFamilyField({ value, onChange, testId }) {
|
|
3830
|
+
const editor = useEditor();
|
|
3831
|
+
const { t } = useUi();
|
|
3832
|
+
const [open, setOpen] = useState(false);
|
|
3833
|
+
const anchor = useRef(null);
|
|
3834
|
+
const quick = QUICK_FONTS.some((font) => font.id === value);
|
|
3835
|
+
const other = value !== void 0 && !quick;
|
|
3836
|
+
const title = value === void 0 || quick ? t("font.more") : t("font.current", { name: fontName(editor.fonts.get(value), t, value) });
|
|
3837
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3838
|
+
className: "nx-color-field",
|
|
3839
|
+
"data-testid": testId,
|
|
3840
|
+
children: [
|
|
3841
|
+
/* @__PURE__ */ jsx(Choice, {
|
|
3842
|
+
label: t("properties.fontFamily"),
|
|
3843
|
+
value,
|
|
3844
|
+
options: QUICK_FONTS.map((font) => ({
|
|
3845
|
+
value: font.id,
|
|
3846
|
+
label: fontRoleLabel(editor, t, font.id),
|
|
3847
|
+
icon: font.icon
|
|
3848
|
+
})),
|
|
3849
|
+
onChange
|
|
3850
|
+
}),
|
|
3851
|
+
/* @__PURE__ */ jsx("span", {
|
|
3852
|
+
className: "nx-color-divider",
|
|
3853
|
+
"aria-hidden": true
|
|
3854
|
+
}),
|
|
3855
|
+
/* @__PURE__ */ jsx("button", {
|
|
3856
|
+
ref: anchor,
|
|
3857
|
+
type: "button",
|
|
3858
|
+
className: "nx-choice-option nx-choice-square",
|
|
3859
|
+
"aria-label": title,
|
|
3860
|
+
title,
|
|
3861
|
+
"aria-haspopup": "dialog",
|
|
3862
|
+
"aria-expanded": open,
|
|
3863
|
+
"aria-pressed": other,
|
|
3864
|
+
"data-testid": "font-picker-toggle",
|
|
3865
|
+
onClick: () => setOpen(!open),
|
|
3866
|
+
children: /* @__PURE__ */ jsx(Icon, { name: "fontPicker" })
|
|
3867
|
+
}),
|
|
3868
|
+
/* @__PURE__ */ jsx(Popover, {
|
|
3869
|
+
open,
|
|
3870
|
+
onClose: () => setOpen(false),
|
|
3871
|
+
anchor,
|
|
3872
|
+
placement: "right-start",
|
|
3873
|
+
label: t("properties.fontFamily"),
|
|
3874
|
+
testId: "font-picker",
|
|
3875
|
+
children: /* @__PURE__ */ jsx(FontPicker, {
|
|
3876
|
+
value,
|
|
3877
|
+
onChange: (id) => {
|
|
3878
|
+
onChange(id);
|
|
3879
|
+
setOpen(false);
|
|
3880
|
+
}
|
|
3881
|
+
})
|
|
3882
|
+
})
|
|
3883
|
+
]
|
|
3884
|
+
});
|
|
3885
|
+
}
|
|
3886
|
+
// src/features/properties/properties.tsx
|
|
3887
|
+
function useStyleTarget() {
|
|
3888
|
+
const editor = useEditor();
|
|
3889
|
+
const selected = useSelectedElements();
|
|
3890
|
+
const summary = useStyleSummary();
|
|
3891
|
+
const tool = useActiveTool();
|
|
3892
|
+
const toolDefaults = useEditorValue((current) => current.elements.definitionOf(tool) === void 0 ? void 0 : {
|
|
3893
|
+
keys: current.styles.supportedKeys(tool),
|
|
3894
|
+
values: current.styles.defaults({ tool })
|
|
3895
|
+
}, {
|
|
3896
|
+
events: ["tool.change", "style.defaults.change"],
|
|
3897
|
+
equals: deepEqual
|
|
3898
|
+
});
|
|
3899
|
+
if (selected.length > 0) {
|
|
3900
|
+
const keys = /* @__PURE__ */ new Set();
|
|
3901
|
+
const values = {};
|
|
3902
|
+
for (const [key, entry] of Object.entries(summary.keys)) {
|
|
3903
|
+
if (entry.state === "unsupported") continue;
|
|
3904
|
+
keys.add(key);
|
|
3905
|
+
values[key] = entry.state === "uniform" ? entry.value : void 0;
|
|
3906
|
+
}
|
|
3907
|
+
return {
|
|
3908
|
+
keys,
|
|
3909
|
+
values,
|
|
3910
|
+
apply: (key, value) => {
|
|
3911
|
+
editor.styles.apply(editor.selection.get(), { [key]: value });
|
|
3912
|
+
stickToolDefaults(editor, key, value);
|
|
3913
|
+
}
|
|
3914
|
+
};
|
|
3915
|
+
}
|
|
3916
|
+
if (toolDefaults === void 0) return;
|
|
3917
|
+
return {
|
|
3918
|
+
keys: new Set(toolDefaults.keys),
|
|
3919
|
+
values: toolDefaults.values,
|
|
3920
|
+
apply: (key, value) => stickToolDefaults(editor, key, value)
|
|
3921
|
+
};
|
|
3922
|
+
}
|
|
3923
|
+
function stickToolDefaults(editor, key, value) {
|
|
3924
|
+
for (const { id } of editor.tools.list()) if (editor.elements.definitionOf(id) !== void 0 && editor.styles.supportedKeys(id).includes(key)) editor.styles.setDefaults({ [key]: value }, { tool: id });
|
|
3925
|
+
}
|
|
3926
|
+
const TEXT_TOOL = "core.text";
|
|
3927
|
+
const TEXT_KEYS = /* @__PURE__ */ new Set([
|
|
3928
|
+
"fontFamily",
|
|
3929
|
+
"fontSize",
|
|
3930
|
+
"fontWeight",
|
|
3931
|
+
"fontStyle",
|
|
3932
|
+
"textAlign",
|
|
3933
|
+
"verticalAlign",
|
|
3934
|
+
"textColor",
|
|
3935
|
+
"lineHeight"
|
|
3936
|
+
]);
|
|
3937
|
+
function asString(value) {
|
|
3938
|
+
return typeof value === "string" ? value : void 0;
|
|
3939
|
+
}
|
|
3940
|
+
function sloppinessOf(value) {
|
|
3941
|
+
return typeof value === "number" ? Math.min(2, Math.max(0, Math.round(value))) : void 0;
|
|
3942
|
+
}
|
|
3943
|
+
function asNumber(value) {
|
|
3944
|
+
return typeof value === "number" ? value : void 0;
|
|
3945
|
+
}
|
|
3946
|
+
function OpacityField({ elements }) {
|
|
3947
|
+
const editor = useEditor();
|
|
3948
|
+
const { t } = useUi();
|
|
3949
|
+
const value = new Set(elements.map((element) => element.opacity)).size === 1 ? Math.round((elements[0]?.opacity ?? 1) * 100) : void 0;
|
|
3950
|
+
const commit = (next) => {
|
|
3951
|
+
const opacity = Math.max(0, Math.min(1, next / 100));
|
|
3952
|
+
editor.transact((transaction) => {
|
|
3953
|
+
for (const element of elements) transaction.elements.update(element.id, { opacity });
|
|
3954
|
+
}, {
|
|
3955
|
+
label: "Change opacity",
|
|
3956
|
+
mergeKey: "ui.opacity"
|
|
3957
|
+
});
|
|
3958
|
+
};
|
|
3959
|
+
return /* @__PURE__ */ jsxs("label", {
|
|
3960
|
+
className: "nx-range",
|
|
3961
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
3962
|
+
type: "range",
|
|
3963
|
+
min: 0,
|
|
3964
|
+
max: 100,
|
|
3965
|
+
step: 10,
|
|
3966
|
+
value: value ?? 100,
|
|
3967
|
+
"aria-label": t("properties.opacity"),
|
|
3968
|
+
"data-testid": "opacity",
|
|
3969
|
+
onChange: (event) => commit(Number(event.target.value))
|
|
3970
|
+
}), /* @__PURE__ */ jsx("span", { children: value === void 0 ? t("properties.mixed") : `${value}%` })]
|
|
3971
|
+
});
|
|
3972
|
+
}
|
|
3973
|
+
function ActionButton({ command, icon, label, args }) {
|
|
3974
|
+
const state = useCommand(command, args);
|
|
3975
|
+
return /* @__PURE__ */ jsx(IconButton, {
|
|
3976
|
+
icon,
|
|
3977
|
+
label,
|
|
3978
|
+
shortcut: state.shortcuts,
|
|
3979
|
+
disabled: !state.enabled,
|
|
3980
|
+
onClick: () => state.run(args),
|
|
3981
|
+
testId: `action-${command}${args === void 0 ? "" : `-${JSON.stringify(args).replace(/\W+/g, "")}`}`
|
|
3982
|
+
});
|
|
3983
|
+
}
|
|
3984
|
+
function ElementActions({ count }) {
|
|
3985
|
+
const { t } = useUi();
|
|
3986
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
3987
|
+
/* @__PURE__ */ jsx(Section, {
|
|
3988
|
+
title: t("properties.layers"),
|
|
3989
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
3990
|
+
className: "nx-row",
|
|
3991
|
+
children: [
|
|
3992
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
3993
|
+
command: "core.sendToBack",
|
|
3994
|
+
icon: "sendToBack",
|
|
3995
|
+
label: t("action.sendToBack")
|
|
3996
|
+
}),
|
|
3997
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
3998
|
+
command: "core.sendBackward",
|
|
3999
|
+
icon: "sendBackward",
|
|
4000
|
+
label: t("action.sendBackward")
|
|
4001
|
+
}),
|
|
4002
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4003
|
+
command: "core.bringForward",
|
|
4004
|
+
icon: "bringForward",
|
|
4005
|
+
label: t("action.bringForward")
|
|
4006
|
+
}),
|
|
4007
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4008
|
+
command: "core.bringToFront",
|
|
4009
|
+
icon: "bringToFront",
|
|
4010
|
+
label: t("action.bringToFront")
|
|
4011
|
+
})
|
|
4012
|
+
]
|
|
4013
|
+
})
|
|
4014
|
+
}),
|
|
4015
|
+
count > 1 && /* @__PURE__ */ jsx(Section, {
|
|
4016
|
+
title: t("properties.align"),
|
|
4017
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
4018
|
+
className: "nx-row nx-wrap",
|
|
4019
|
+
children: [
|
|
4020
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4021
|
+
command: "core.align",
|
|
4022
|
+
args: { edge: "left" },
|
|
4023
|
+
icon: "alignLeft",
|
|
4024
|
+
label: t("action.alignLeft")
|
|
4025
|
+
}),
|
|
4026
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4027
|
+
command: "core.align",
|
|
4028
|
+
args: { edge: "center" },
|
|
4029
|
+
icon: "alignCenter",
|
|
4030
|
+
label: t("action.alignCenter")
|
|
4031
|
+
}),
|
|
4032
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4033
|
+
command: "core.align",
|
|
4034
|
+
args: { edge: "right" },
|
|
4035
|
+
icon: "alignRight",
|
|
4036
|
+
label: t("action.alignRight")
|
|
4037
|
+
}),
|
|
4038
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4039
|
+
command: "core.align",
|
|
4040
|
+
args: { edge: "top" },
|
|
4041
|
+
icon: "alignTop",
|
|
4042
|
+
label: t("action.alignTop")
|
|
4043
|
+
}),
|
|
4044
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4045
|
+
command: "core.align",
|
|
4046
|
+
args: { edge: "middle" },
|
|
4047
|
+
icon: "alignMiddle",
|
|
4048
|
+
label: t("action.alignMiddle")
|
|
4049
|
+
}),
|
|
4050
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4051
|
+
command: "core.align",
|
|
4052
|
+
args: { edge: "bottom" },
|
|
4053
|
+
icon: "alignBottom",
|
|
4054
|
+
label: t("action.alignBottom")
|
|
4055
|
+
}),
|
|
4056
|
+
count > 2 && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(ActionButton, {
|
|
4057
|
+
command: "core.distribute",
|
|
4058
|
+
args: { axis: "horizontal" },
|
|
4059
|
+
icon: "distributeHorizontal",
|
|
4060
|
+
label: t("action.distributeHorizontal")
|
|
4061
|
+
}), /* @__PURE__ */ jsx(ActionButton, {
|
|
4062
|
+
command: "core.distribute",
|
|
4063
|
+
args: { axis: "vertical" },
|
|
4064
|
+
icon: "distributeVertical",
|
|
4065
|
+
label: t("action.distributeVertical")
|
|
4066
|
+
})] })
|
|
4067
|
+
]
|
|
4068
|
+
})
|
|
4069
|
+
}),
|
|
4070
|
+
/* @__PURE__ */ jsx(Section, {
|
|
4071
|
+
title: t("properties.actions"),
|
|
4072
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
4073
|
+
className: "nx-row nx-wrap",
|
|
4074
|
+
children: [
|
|
4075
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4076
|
+
command: "core.duplicate",
|
|
4077
|
+
icon: "copy",
|
|
4078
|
+
label: t("action.duplicate")
|
|
4079
|
+
}),
|
|
4080
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4081
|
+
command: "core.delete",
|
|
4082
|
+
icon: "delete",
|
|
4083
|
+
label: t("action.delete")
|
|
4084
|
+
}),
|
|
4085
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4086
|
+
command: "core.group",
|
|
4087
|
+
icon: "group",
|
|
4088
|
+
label: t("action.group")
|
|
4089
|
+
}),
|
|
4090
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4091
|
+
command: "core.ungroup",
|
|
4092
|
+
icon: "ungroup",
|
|
4093
|
+
label: t("action.ungroup")
|
|
4094
|
+
}),
|
|
4095
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4096
|
+
command: "core.flipHorizontal",
|
|
4097
|
+
icon: "flipHorizontal",
|
|
4098
|
+
label: t("action.flipHorizontal")
|
|
4099
|
+
}),
|
|
4100
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4101
|
+
command: "core.flipVertical",
|
|
4102
|
+
icon: "flipVertical",
|
|
4103
|
+
label: t("action.flipVertical")
|
|
4104
|
+
}),
|
|
4105
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4106
|
+
command: "core.lock.toggle",
|
|
4107
|
+
icon: "lock",
|
|
4108
|
+
label: t("action.lock")
|
|
4109
|
+
}),
|
|
4110
|
+
count === 1 && /* @__PURE__ */ jsx(ActionButton, {
|
|
4111
|
+
command: "ui.link.edit",
|
|
4112
|
+
icon: "link",
|
|
4113
|
+
label: t("link.edit")
|
|
4114
|
+
}),
|
|
4115
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
4116
|
+
command: "core.image.crop",
|
|
4117
|
+
icon: "crop",
|
|
4118
|
+
label: t("action.crop")
|
|
4119
|
+
})
|
|
4120
|
+
]
|
|
4121
|
+
})
|
|
4122
|
+
})
|
|
4123
|
+
] });
|
|
4124
|
+
}
|
|
4125
|
+
function CustomEditors({ elements }) {
|
|
4126
|
+
const editor = useEditor();
|
|
4127
|
+
const { registry } = useUi();
|
|
4128
|
+
return [...new Set(elements.map((element) => element.type))].map((type) => {
|
|
4129
|
+
const Component = registry.propertyEditors.get(type);
|
|
4130
|
+
return Component === void 0 ? null : /* @__PURE__ */ jsx("div", {
|
|
4131
|
+
className: "nx-custom-editor",
|
|
4132
|
+
"data-testid": `property-editor-${type}`,
|
|
4133
|
+
children: /* @__PURE__ */ jsx(Component, {
|
|
4134
|
+
editor,
|
|
4135
|
+
elements: elements.filter((element) => element.type === type)
|
|
4136
|
+
})
|
|
4137
|
+
}, type);
|
|
4138
|
+
});
|
|
4139
|
+
}
|
|
4140
|
+
/**
|
|
4141
|
+
* Styles of the selection (or of the active drawing tool's defaults), element actions and
|
|
4142
|
+
* registered custom property editors. Mixed values show no selected option.
|
|
4143
|
+
*/
|
|
4144
|
+
function PropertiesPanel() {
|
|
4145
|
+
const editor = useEditor();
|
|
4146
|
+
const { t } = useUi();
|
|
4147
|
+
const selected = useSelectedElements();
|
|
4148
|
+
const target = useStyleTarget();
|
|
4149
|
+
const tool = useActiveTool();
|
|
4150
|
+
if (target === void 0 && selected.length === 0) return null;
|
|
4151
|
+
const hasText = selected.length === 0 || selected.some((element) => element.type === "core.text" || (editor.text.layoutOf(element)?.text ?? "") !== "");
|
|
4152
|
+
const has = (key) => target?.keys.has(key) === true && (!TEXT_KEYS.has(key) || hasText);
|
|
4153
|
+
const value = (key) => target?.values[key];
|
|
4154
|
+
const set = (key) => (next) => target?.apply(key, next);
|
|
4155
|
+
const fillVisible = has("fillColor") && value("fillStyle") !== "none";
|
|
4156
|
+
return /* @__PURE__ */ jsx(Island, {
|
|
4157
|
+
className: "nx-properties",
|
|
4158
|
+
label: t("properties.label"),
|
|
4159
|
+
testId: "properties-panel",
|
|
4160
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
4161
|
+
className: "nx-scroll",
|
|
4162
|
+
children: [
|
|
4163
|
+
selected.length > 1 && /* @__PURE__ */ jsx("p", {
|
|
4164
|
+
className: "nx-caption",
|
|
4165
|
+
children: t("properties.selected", { count: selected.length })
|
|
4166
|
+
}),
|
|
4167
|
+
has("textColor") && (selected.length > 0 || tool === TEXT_TOOL) && /* @__PURE__ */ jsx(Section, {
|
|
4168
|
+
title: t("properties.textColor"),
|
|
4169
|
+
children: /* @__PURE__ */ jsx(ColorField, {
|
|
4170
|
+
label: t("properties.textColor"),
|
|
4171
|
+
value: asString(value("textColor")),
|
|
4172
|
+
palette: STROKE_COLORS,
|
|
4173
|
+
onChange: set("textColor"),
|
|
4174
|
+
testId: "text-color"
|
|
4175
|
+
})
|
|
4176
|
+
}),
|
|
4177
|
+
has("strokeColor") && /* @__PURE__ */ jsx(Section, {
|
|
4178
|
+
title: t("properties.stroke"),
|
|
4179
|
+
children: /* @__PURE__ */ jsx(ColorField, {
|
|
4180
|
+
label: t("properties.stroke"),
|
|
4181
|
+
value: asString(value("strokeColor")),
|
|
4182
|
+
palette: STROKE_COLORS,
|
|
4183
|
+
onChange: set("strokeColor"),
|
|
4184
|
+
testId: "stroke-color"
|
|
4185
|
+
})
|
|
4186
|
+
}),
|
|
4187
|
+
has("fillColor") && /* @__PURE__ */ jsx(Section, {
|
|
4188
|
+
title: t("properties.background"),
|
|
4189
|
+
children: /* @__PURE__ */ jsx(ColorField, {
|
|
4190
|
+
label: t("properties.background"),
|
|
4191
|
+
value: value("fillStyle") === "none" ? "transparent" : asString(value("fillColor")),
|
|
4192
|
+
palette: FILL_COLORS,
|
|
4193
|
+
kind: "background",
|
|
4194
|
+
onChange: (color) => {
|
|
4195
|
+
if (color === "transparent") {
|
|
4196
|
+
target?.apply("fillStyle", "none");
|
|
4197
|
+
return;
|
|
4198
|
+
}
|
|
4199
|
+
target?.apply("fillColor", color);
|
|
4200
|
+
if (value("fillStyle") === "none") target?.apply("fillStyle", "solid");
|
|
4201
|
+
},
|
|
4202
|
+
testId: "fill-color"
|
|
4203
|
+
})
|
|
4204
|
+
}),
|
|
4205
|
+
fillVisible && has("fillStyle") && /* @__PURE__ */ jsx(Section, {
|
|
4206
|
+
title: t("properties.fill"),
|
|
4207
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4208
|
+
label: t("properties.fill"),
|
|
4209
|
+
value: asString(value("fillStyle")),
|
|
4210
|
+
options: [
|
|
4211
|
+
{
|
|
4212
|
+
value: "hachure",
|
|
4213
|
+
label: t("value.hachure")
|
|
4214
|
+
},
|
|
4215
|
+
{
|
|
4216
|
+
value: "cross-hatch",
|
|
4217
|
+
label: t("value.cross-hatch")
|
|
4218
|
+
},
|
|
4219
|
+
{
|
|
4220
|
+
value: "solid",
|
|
4221
|
+
label: t("value.solid")
|
|
4222
|
+
}
|
|
4223
|
+
],
|
|
4224
|
+
onChange: set("fillStyle"),
|
|
4225
|
+
testId: "fill-style"
|
|
4226
|
+
})
|
|
4227
|
+
}),
|
|
4228
|
+
has("strokeWidth") && /* @__PURE__ */ jsx(Section, {
|
|
4229
|
+
title: t("properties.strokeWidth"),
|
|
4230
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4231
|
+
label: t("properties.strokeWidth"),
|
|
4232
|
+
value: asNumber(value("strokeWidth")),
|
|
4233
|
+
options: [
|
|
4234
|
+
{
|
|
4235
|
+
value: 1,
|
|
4236
|
+
label: t("value.thin"),
|
|
4237
|
+
icon: "strokeThin"
|
|
4238
|
+
},
|
|
4239
|
+
{
|
|
4240
|
+
value: 2,
|
|
4241
|
+
label: t("value.medium"),
|
|
4242
|
+
icon: "strokeMedium"
|
|
4243
|
+
},
|
|
4244
|
+
{
|
|
4245
|
+
value: 4,
|
|
4246
|
+
label: t("value.bold"),
|
|
4247
|
+
icon: "strokeBold"
|
|
4248
|
+
}
|
|
4249
|
+
],
|
|
4250
|
+
onChange: set("strokeWidth"),
|
|
4251
|
+
testId: "stroke-width"
|
|
4252
|
+
})
|
|
4253
|
+
}),
|
|
4254
|
+
has("strokeStyle") && /* @__PURE__ */ jsx(Section, {
|
|
4255
|
+
title: t("properties.strokeStyle"),
|
|
4256
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4257
|
+
label: t("properties.strokeStyle"),
|
|
4258
|
+
value: asString(value("strokeStyle")),
|
|
4259
|
+
options: [
|
|
4260
|
+
{
|
|
4261
|
+
value: "solid",
|
|
4262
|
+
label: t("value.solid"),
|
|
4263
|
+
icon: "strokeSolid"
|
|
4264
|
+
},
|
|
4265
|
+
{
|
|
4266
|
+
value: "dashed",
|
|
4267
|
+
label: t("value.dashed"),
|
|
4268
|
+
icon: "strokeDashed"
|
|
4269
|
+
},
|
|
4270
|
+
{
|
|
4271
|
+
value: "dotted",
|
|
4272
|
+
label: t("value.dotted"),
|
|
4273
|
+
icon: "strokeDotted"
|
|
4274
|
+
}
|
|
4275
|
+
],
|
|
4276
|
+
onChange: set("strokeStyle"),
|
|
4277
|
+
testId: "stroke-style"
|
|
4278
|
+
})
|
|
4279
|
+
}),
|
|
4280
|
+
has("roughness") && /* @__PURE__ */ jsx(Section, {
|
|
4281
|
+
title: t("properties.sloppiness"),
|
|
4282
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4283
|
+
label: t("properties.sloppiness"),
|
|
4284
|
+
value: sloppinessOf(value("roughness")),
|
|
4285
|
+
options: [
|
|
4286
|
+
{
|
|
4287
|
+
value: 0,
|
|
4288
|
+
label: t("value.architect"),
|
|
4289
|
+
icon: "sloppinessArchitect"
|
|
4290
|
+
},
|
|
4291
|
+
{
|
|
4292
|
+
value: 1,
|
|
4293
|
+
label: t("value.artist"),
|
|
4294
|
+
icon: "sloppinessArtist"
|
|
4295
|
+
},
|
|
4296
|
+
{
|
|
4297
|
+
value: 2,
|
|
4298
|
+
label: t("value.cartoonist"),
|
|
4299
|
+
icon: "sloppinessCartoonist"
|
|
4300
|
+
}
|
|
4301
|
+
],
|
|
4302
|
+
onChange: set("roughness"),
|
|
4303
|
+
testId: "sloppiness"
|
|
4304
|
+
})
|
|
4305
|
+
}),
|
|
4306
|
+
has("cornerRadius") && /* @__PURE__ */ jsx(Section, {
|
|
4307
|
+
title: t("properties.cornerRadius"),
|
|
4308
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4309
|
+
label: t("properties.cornerRadius"),
|
|
4310
|
+
value: asNumber(value("cornerRadius")) === void 0 ? void 0 : value("cornerRadius") > 0 ? 12 : 0,
|
|
4311
|
+
options: [{
|
|
4312
|
+
value: 0,
|
|
4313
|
+
label: t("value.sharp"),
|
|
4314
|
+
icon: "edgeSharp"
|
|
4315
|
+
}, {
|
|
4316
|
+
value: 12,
|
|
4317
|
+
label: t("value.round"),
|
|
4318
|
+
icon: "edgeRound"
|
|
4319
|
+
}],
|
|
4320
|
+
onChange: set("cornerRadius"),
|
|
4321
|
+
testId: "corner-radius"
|
|
4322
|
+
})
|
|
4323
|
+
}),
|
|
4324
|
+
/* @__PURE__ */ jsx(LinearProperties, {}),
|
|
4325
|
+
has("startArrowhead") && /* @__PURE__ */ jsx(Section, {
|
|
4326
|
+
title: t("properties.arrowheads"),
|
|
4327
|
+
children: ["startArrowhead", "endArrowhead"].map((key) => /* @__PURE__ */ jsx(Choice, {
|
|
4328
|
+
label: t(`properties.${key}`),
|
|
4329
|
+
value: asString(value(key)),
|
|
4330
|
+
options: [
|
|
4331
|
+
{
|
|
4332
|
+
value: "none",
|
|
4333
|
+
label: t("value.none")
|
|
4334
|
+
},
|
|
4335
|
+
{
|
|
4336
|
+
value: "arrow",
|
|
4337
|
+
label: t("value.arrow")
|
|
4338
|
+
},
|
|
4339
|
+
{
|
|
4340
|
+
value: "triangle",
|
|
4341
|
+
label: t("value.triangle")
|
|
4342
|
+
},
|
|
4343
|
+
{
|
|
4344
|
+
value: "bar",
|
|
4345
|
+
label: t("value.bar")
|
|
4346
|
+
},
|
|
4347
|
+
{
|
|
4348
|
+
value: "circle",
|
|
4349
|
+
label: t("value.circle")
|
|
4350
|
+
}
|
|
4351
|
+
],
|
|
4352
|
+
onChange: set(key),
|
|
4353
|
+
testId: key
|
|
4354
|
+
}, key))
|
|
4355
|
+
}),
|
|
4356
|
+
has("fontFamily") && /* @__PURE__ */ jsx(Section, {
|
|
4357
|
+
title: t("properties.fontFamily"),
|
|
4358
|
+
children: /* @__PURE__ */ jsx(FontFamilyField, {
|
|
4359
|
+
value: asString(value("fontFamily")),
|
|
4360
|
+
onChange: set("fontFamily"),
|
|
4361
|
+
testId: "font-family"
|
|
4362
|
+
})
|
|
4363
|
+
}),
|
|
4364
|
+
has("fontSize") && /* @__PURE__ */ jsx(Section, {
|
|
4365
|
+
title: t("properties.fontSize"),
|
|
4366
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4367
|
+
label: t("properties.fontSize"),
|
|
4368
|
+
value: asNumber(value("fontSize")),
|
|
4369
|
+
options: [
|
|
4370
|
+
{
|
|
4371
|
+
value: 16,
|
|
4372
|
+
label: t("value.small"),
|
|
4373
|
+
glyph: "S"
|
|
4374
|
+
},
|
|
4375
|
+
{
|
|
4376
|
+
value: 20,
|
|
4377
|
+
label: t("value.medium"),
|
|
4378
|
+
glyph: "M"
|
|
4379
|
+
},
|
|
4380
|
+
{
|
|
4381
|
+
value: 28,
|
|
4382
|
+
label: t("value.large"),
|
|
4383
|
+
glyph: "L"
|
|
4384
|
+
},
|
|
4385
|
+
{
|
|
4386
|
+
value: 36,
|
|
4387
|
+
label: t("value.extraLarge"),
|
|
4388
|
+
glyph: "XL"
|
|
4389
|
+
}
|
|
4390
|
+
],
|
|
4391
|
+
onChange: set("fontSize"),
|
|
4392
|
+
testId: "font-size"
|
|
4393
|
+
})
|
|
4394
|
+
}),
|
|
4395
|
+
has("textAlign") && /* @__PURE__ */ jsx(Section, {
|
|
4396
|
+
title: t("properties.textAlign"),
|
|
4397
|
+
children: /* @__PURE__ */ jsx(Choice, {
|
|
4398
|
+
label: t("properties.textAlign"),
|
|
4399
|
+
value: asString(value("textAlign")),
|
|
4400
|
+
options: [
|
|
4401
|
+
{
|
|
4402
|
+
value: "left",
|
|
4403
|
+
label: t("value.left"),
|
|
4404
|
+
icon: "textLeft"
|
|
4405
|
+
},
|
|
4406
|
+
{
|
|
4407
|
+
value: "center",
|
|
4408
|
+
label: t("value.center"),
|
|
4409
|
+
icon: "textCenter"
|
|
4410
|
+
},
|
|
4411
|
+
{
|
|
4412
|
+
value: "right",
|
|
4413
|
+
label: t("value.right"),
|
|
4414
|
+
icon: "textRight"
|
|
4415
|
+
}
|
|
4416
|
+
],
|
|
4417
|
+
onChange: set("textAlign"),
|
|
4418
|
+
testId: "text-align"
|
|
4419
|
+
})
|
|
4420
|
+
}),
|
|
4421
|
+
selected.length > 0 && /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
4422
|
+
/* @__PURE__ */ jsx(Section, {
|
|
4423
|
+
title: t("properties.opacity"),
|
|
4424
|
+
children: /* @__PURE__ */ jsx(OpacityField, { elements: selected })
|
|
4425
|
+
}),
|
|
4426
|
+
/* @__PURE__ */ jsx(ElementActions, { count: selected.length }),
|
|
4427
|
+
/* @__PURE__ */ jsx(CustomEditors, { elements: selected })
|
|
4428
|
+
] })
|
|
4429
|
+
]
|
|
4430
|
+
})
|
|
4431
|
+
});
|
|
4432
|
+
}
|
|
4433
|
+
// src/lib/plugins.tsx
|
|
4434
|
+
function install(registry, plugin) {
|
|
4435
|
+
const facet = plugin.ui;
|
|
4436
|
+
const undo = [];
|
|
4437
|
+
for (const item of facet?.toolbar ?? []) undo.push(registry.toolbar.register(item));
|
|
4438
|
+
for (const item of facet?.contextMenu ?? []) undo.push(registry.contextMenu.register(item));
|
|
4439
|
+
for (const [type, component] of Object.entries(facet?.propertyEditors ?? {})) undo.push(registry.propertyEditors.register(type, component));
|
|
4440
|
+
for (const [id, component] of Object.entries(facet?.icons ?? {})) undo.push(registry.icons.register(id, component));
|
|
4441
|
+
return () => undo.forEach((step) => step());
|
|
4442
|
+
}
|
|
4443
|
+
/**
|
|
4444
|
+
* Installs the `ui` facets of the editor's plugins into the UI registry and follows later
|
|
4445
|
+
* installs and uninstalls. `<DefaultEditor>` renders it; custom layouts can render it too.
|
|
4446
|
+
*/
|
|
4447
|
+
function UiPlugins() {
|
|
4448
|
+
const editor = useEditor();
|
|
4449
|
+
const { registry } = useUi();
|
|
4450
|
+
useEffect(() => {
|
|
4451
|
+
const installed = /* @__PURE__ */ new Map();
|
|
4452
|
+
for (const plugin of editor.plugins.list()) installed.set(plugin.id, install(registry, plugin));
|
|
4453
|
+
const off = editor.events.on("plugin.change", (event) => {
|
|
4454
|
+
installed.get(event.id)?.();
|
|
4455
|
+
installed.delete(event.id);
|
|
4456
|
+
const plugin = event.change === "install" ? editor.plugins.list().find((entry) => entry.id === event.id) : void 0;
|
|
4457
|
+
if (plugin !== void 0) installed.set(plugin.id, install(registry, plugin));
|
|
4458
|
+
});
|
|
4459
|
+
return () => {
|
|
4460
|
+
off();
|
|
4461
|
+
for (const undo of installed.values()) undo();
|
|
4462
|
+
};
|
|
4463
|
+
}, [editor, registry]);
|
|
4464
|
+
return null;
|
|
4465
|
+
}
|
|
4466
|
+
// src/features/toolbar/toolbar.tsx
|
|
4467
|
+
const TOOL_ORDER = {
|
|
4468
|
+
"core.hand": 100,
|
|
4469
|
+
"core.select": 200,
|
|
4470
|
+
"core.rectangle": 300,
|
|
4471
|
+
"core.diamond": 310,
|
|
4472
|
+
"core.ellipse": 320,
|
|
4473
|
+
"core.arrow": 400,
|
|
4474
|
+
"core.line": 410,
|
|
4475
|
+
"core.freedraw": 500,
|
|
4476
|
+
"core.text": 510,
|
|
4477
|
+
"core.image": 520,
|
|
4478
|
+
"core.eraser": 600,
|
|
4479
|
+
"core.frame": 690,
|
|
4480
|
+
"core.laser": 700
|
|
4481
|
+
};
|
|
4482
|
+
const GROUP_BREAKS = /* @__PURE__ */ new Set([
|
|
4483
|
+
300,
|
|
4484
|
+
400,
|
|
4485
|
+
500,
|
|
4486
|
+
600
|
|
4487
|
+
]);
|
|
4488
|
+
/** Tools listed in the toolbar's "more tools" menu instead of as buttons (like Excalidraw). */
|
|
4489
|
+
const OVERFLOW_TOOLS = /* @__PURE__ */ new Set([
|
|
4490
|
+
"core.image",
|
|
4491
|
+
"core.frame",
|
|
4492
|
+
"core.laser"
|
|
4493
|
+
]);
|
|
4494
|
+
/** Toolbar button with a menu of the less used tools; pressed while one of them is active. */
|
|
4495
|
+
function ToolOverflow({ tools }) {
|
|
4496
|
+
const editor = useEditor();
|
|
4497
|
+
const active = useActiveTool();
|
|
4498
|
+
const { t } = useUi();
|
|
4499
|
+
const [open, setOpen] = useState(false);
|
|
4500
|
+
const anchor = useRef(null);
|
|
4501
|
+
const entries = tools.map((tool) => {
|
|
4502
|
+
const translated = t(`tool.${tool.id}`);
|
|
4503
|
+
return {
|
|
4504
|
+
id: tool.id,
|
|
4505
|
+
label: translated === `tool.${tool.id}` ? tool.label ?? tool.id : translated,
|
|
4506
|
+
icon: tool.id,
|
|
4507
|
+
shortcut: tool.shortcuts[0],
|
|
4508
|
+
onSelect: () => editor.tools.activate(tool.id)
|
|
4509
|
+
};
|
|
4510
|
+
});
|
|
4511
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("span", {
|
|
4512
|
+
ref: anchor,
|
|
4513
|
+
className: "nx-anchor",
|
|
4514
|
+
children: /* @__PURE__ */ jsx(IconButton, {
|
|
4515
|
+
icon: "more",
|
|
4516
|
+
label: t("toolbar.more"),
|
|
4517
|
+
pressed: open || tools.some((tool) => tool.id === active),
|
|
4518
|
+
onClick: () => setOpen(!open),
|
|
4519
|
+
testId: "tool-overflow",
|
|
4520
|
+
className: "nx-tool"
|
|
4521
|
+
})
|
|
4522
|
+
}), /* @__PURE__ */ jsx(Popover, {
|
|
4523
|
+
open,
|
|
4524
|
+
onClose: () => setOpen(false),
|
|
4525
|
+
anchor,
|
|
4526
|
+
placement: "bottom-end",
|
|
4527
|
+
label: t("toolbar.more"),
|
|
4528
|
+
role: "menu",
|
|
4529
|
+
testId: "tool-overflow-menu",
|
|
4530
|
+
children: /* @__PURE__ */ jsx(MenuItems, {
|
|
4531
|
+
entries,
|
|
4532
|
+
onDone: () => setOpen(false)
|
|
4533
|
+
})
|
|
4534
|
+
})] });
|
|
4535
|
+
}
|
|
4536
|
+
function ToolButton({ id, label, fallbackLabel, icon, shortcut, shortcuts, showShortcut = true }) {
|
|
4537
|
+
const editor = useEditor();
|
|
4538
|
+
const active = useActiveTool();
|
|
4539
|
+
const { t } = useUi();
|
|
4540
|
+
const translated = t(`tool.${id}`);
|
|
4541
|
+
const name = label ?? (translated === `tool.${id}` ? fallbackLabel ?? id : translated);
|
|
4542
|
+
return /* @__PURE__ */ jsx(IconButton, {
|
|
4543
|
+
icon: icon ?? id,
|
|
4544
|
+
label: name,
|
|
4545
|
+
shortcut: shortcuts ?? shortcut,
|
|
4546
|
+
pressed: active === id,
|
|
4547
|
+
onClick: () => editor.tools.activate(id),
|
|
4548
|
+
testId: `tool-${id}`,
|
|
4549
|
+
className: "nx-tool",
|
|
4550
|
+
children: showShortcut && shortcut !== void 0 && /* @__PURE__ */ jsx("span", {
|
|
4551
|
+
className: "nx-tool-key",
|
|
4552
|
+
children: shortcut
|
|
4553
|
+
})
|
|
4554
|
+
});
|
|
4555
|
+
}
|
|
4556
|
+
function ToolLockButton() {
|
|
4557
|
+
const locked = useToolLocked();
|
|
4558
|
+
const lock = useCommand("core.tool.toggleLock");
|
|
4559
|
+
const { t } = useUi();
|
|
4560
|
+
return /* @__PURE__ */ jsx(IconButton, {
|
|
4561
|
+
icon: locked ? "lock" : "unlock",
|
|
4562
|
+
label: t("toolbar.lock"),
|
|
4563
|
+
shortcut: lock.shortcuts,
|
|
4564
|
+
pressed: locked,
|
|
4565
|
+
onClick: () => lock.run(),
|
|
4566
|
+
testId: "tool-lock"
|
|
4567
|
+
});
|
|
4568
|
+
}
|
|
4569
|
+
/**
|
|
4570
|
+
* Available tools in a fixed group order (unknown custom tools after the built-ins), plus
|
|
4571
|
+
* registered toolbar items placed by `order`. Arrow keys move between buttons.
|
|
4572
|
+
*/
|
|
4573
|
+
function Toolbar({ start, end }) {
|
|
4574
|
+
const tools = useTools();
|
|
4575
|
+
const { t, registry, isHidden } = useUi();
|
|
4576
|
+
const available = tools.filter((tool) => tool.available && !isHidden(`tool:${tool.id}`));
|
|
4577
|
+
const visible = available.filter((tool) => !OVERFLOW_TOOLS.has(tool.id));
|
|
4578
|
+
const overflow = available.filter((tool) => OVERFLOW_TOOLS.has(tool.id));
|
|
4579
|
+
const entries = byOrder([...visible.map((tool, index) => ({
|
|
4580
|
+
id: tool.id,
|
|
4581
|
+
order: TOOL_ORDER[tool.id] ?? 800 + index,
|
|
4582
|
+
render: () => /* @__PURE__ */ jsx(ToolButton, {
|
|
4583
|
+
id: tool.id,
|
|
4584
|
+
...tool.label === void 0 ? {} : { fallbackLabel: tool.label },
|
|
4585
|
+
...tool.shortcuts[0] === void 0 ? {} : {
|
|
4586
|
+
shortcut: tool.shortcuts[0],
|
|
4587
|
+
shortcuts: tool.shortcuts
|
|
4588
|
+
}
|
|
4589
|
+
})
|
|
4590
|
+
})), ...registry.toolbar.list().filter((item) => !isHidden(`toolbar:${item.id}`))]);
|
|
4591
|
+
let previousOrder = 0;
|
|
4592
|
+
return /* @__PURE__ */ jsx(Island, {
|
|
4593
|
+
className: "nx-toolbar",
|
|
4594
|
+
label: t("toolbar.label"),
|
|
4595
|
+
role: "toolbar",
|
|
4596
|
+
testId: "toolbar",
|
|
4597
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
4598
|
+
className: "nx-toolbar-items",
|
|
4599
|
+
onKeyDown: (event) => handleRovingKeys(event, "horizontal"),
|
|
4600
|
+
children: [
|
|
4601
|
+
start,
|
|
4602
|
+
!isHidden("toolLock") && /* @__PURE__ */ jsx(ToolLockButton, {}),
|
|
4603
|
+
/* @__PURE__ */ jsx("div", { className: "nx-separator nx-vertical" }),
|
|
4604
|
+
entries.map((entry) => {
|
|
4605
|
+
const breakBefore = [...GROUP_BREAKS].some((limit) => previousOrder < limit && entry.order >= limit);
|
|
4606
|
+
previousOrder = entry.order;
|
|
4607
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [breakBefore && /* @__PURE__ */ jsx("div", { className: cx("nx-separator", "nx-vertical") }), entry.render()] }, entry.id);
|
|
4608
|
+
}),
|
|
4609
|
+
end,
|
|
4610
|
+
overflow.length > 0 && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("div", { className: "nx-separator nx-vertical" }), /* @__PURE__ */ jsx(ToolOverflow, { tools: overflow })] })
|
|
4611
|
+
]
|
|
4612
|
+
})
|
|
4613
|
+
});
|
|
4614
|
+
}
|
|
4615
|
+
// src/app/default-editor.tsx
|
|
4616
|
+
const THEME_BACKGROUND = {
|
|
4617
|
+
light: "#ffffff",
|
|
4618
|
+
dark: "#121212"
|
|
4619
|
+
};
|
|
4620
|
+
function Layout({ components = {}, slots = {}, canvas, minimapRenderers, fileName, onMount }) {
|
|
4621
|
+
const editor = useEditor();
|
|
4622
|
+
const { t, isHidden, isOpen, setOpen, theme } = useUi();
|
|
4623
|
+
const presentation = usePresentation();
|
|
4624
|
+
const mode = useMode();
|
|
4625
|
+
const selection = useSelection();
|
|
4626
|
+
const tool = useActiveTool();
|
|
4627
|
+
const drawingTool = useEditorValue((editor) => editor.elements.definitionOf(editor.tools.active()) !== void 0, { events: ["tool.change"] });
|
|
4628
|
+
const onContextMenu = useContextMenuForCanvas();
|
|
4629
|
+
const zen = presentation.zen;
|
|
4630
|
+
const viewing = mode === "view";
|
|
4631
|
+
const show = (id) => !isHidden(id);
|
|
4632
|
+
const ToolbarComponent = components.Toolbar ?? Toolbar;
|
|
4633
|
+
const Properties = components.PropertiesPanel ?? PropertiesPanel;
|
|
4634
|
+
const Layers = components.LayersPanel ?? LayersPanel;
|
|
4635
|
+
const Menu = components.MainMenu ?? MainMenu;
|
|
4636
|
+
const Zoom = components.ZoomControls ?? ZoomControls;
|
|
4637
|
+
const History = components.UndoRedo ?? UndoRedo;
|
|
4638
|
+
const MinimapComponent = components.Minimap ?? Minimap;
|
|
4639
|
+
const ContextMenuComponent = components.ContextMenu ?? ContextMenu;
|
|
4640
|
+
const showProperties = show("properties") && !zen && !viewing && (selection.length > 0 || drawingTool) && tool !== "core.eraser";
|
|
4641
|
+
const layersOpen = isOpen("layers") && show("layers") && !zen;
|
|
4642
|
+
const contextMenuEnabled = show("contextMenu");
|
|
4643
|
+
const linkEditorShown = show("linkEditor");
|
|
4644
|
+
const canvasOptions = useMemo(() => ({
|
|
4645
|
+
...canvas,
|
|
4646
|
+
linkBadges: canvas?.linkBadges ?? (linkEditorShown ? "unselected" : true),
|
|
4647
|
+
defaultBackground: canvas?.defaultBackground ?? THEME_BACKGROUND[theme],
|
|
4648
|
+
...contextMenuEnabled ? { onContextMenu } : {}
|
|
4649
|
+
}), [
|
|
4650
|
+
canvas,
|
|
4651
|
+
theme,
|
|
4652
|
+
onContextMenu,
|
|
4653
|
+
contextMenuEnabled,
|
|
4654
|
+
linkEditorShown
|
|
4655
|
+
]);
|
|
4656
|
+
return /* @__PURE__ */ jsxs(EditorView, {
|
|
4657
|
+
options: canvasOptions,
|
|
4658
|
+
className: "nx-canvas",
|
|
4659
|
+
...onMount === void 0 ? {} : { onMount },
|
|
4660
|
+
children: [
|
|
4661
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4662
|
+
className: "nx-overlay",
|
|
4663
|
+
children: [
|
|
4664
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4665
|
+
className: "nx-top",
|
|
4666
|
+
children: [
|
|
4667
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4668
|
+
className: "nx-top-left",
|
|
4669
|
+
children: [show("mainMenu") && !zen && /* @__PURE__ */ jsx(Menu, { ...slots.mainMenuItems === void 0 ? {} : { items: slots.mainMenuItems } }), slots.topLeft]
|
|
4670
|
+
}),
|
|
4671
|
+
show("toolbar") && /* @__PURE__ */ jsx(ToolbarComponent, {
|
|
4672
|
+
start: slots.toolbarStart,
|
|
4673
|
+
end: slots.toolbarEnd
|
|
4674
|
+
}),
|
|
4675
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4676
|
+
className: "nx-top-right",
|
|
4677
|
+
children: [slots.topRight, show("layers") && !zen && /* @__PURE__ */ jsx(IconButton, {
|
|
4678
|
+
icon: "layers",
|
|
4679
|
+
label: t("layers.toggle"),
|
|
4680
|
+
pressed: layersOpen,
|
|
4681
|
+
onClick: () => setOpen("layers", !layersOpen),
|
|
4682
|
+
testId: "layers-toggle"
|
|
4683
|
+
})]
|
|
4684
|
+
})
|
|
4685
|
+
]
|
|
4686
|
+
}),
|
|
4687
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4688
|
+
className: "nx-middle",
|
|
4689
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
4690
|
+
className: "nx-side nx-side-start",
|
|
4691
|
+
children: showProperties && /* @__PURE__ */ jsx(Properties, {})
|
|
4692
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
4693
|
+
className: "nx-side nx-side-end",
|
|
4694
|
+
children: layersOpen && /* @__PURE__ */ jsx(Layers, { onClose: () => setOpen("layers", false) })
|
|
4695
|
+
})]
|
|
4696
|
+
}),
|
|
4697
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4698
|
+
className: "nx-bottom",
|
|
4699
|
+
children: [
|
|
4700
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4701
|
+
className: "nx-bottom-left",
|
|
4702
|
+
children: [
|
|
4703
|
+
!zen && show("zoom") && /* @__PURE__ */ jsx(Zoom, { ...show("minimap") ? { minimap: /* @__PURE__ */ jsx(MinimapComponent, { ...minimapRenderers === void 0 && canvas?.renderers === void 0 ? {} : { renderers: minimapRenderers ?? canvas?.renderers } }) } : {} }),
|
|
4704
|
+
!zen && !viewing && show("undoRedo") && /* @__PURE__ */ jsx(History, {}),
|
|
4705
|
+
!zen && show("grid") && /* @__PURE__ */ jsx(Island, {
|
|
4706
|
+
className: "nx-row",
|
|
4707
|
+
children: /* @__PURE__ */ jsx(GridControls, {})
|
|
4708
|
+
}),
|
|
4709
|
+
slots.bottomLeft
|
|
4710
|
+
]
|
|
4711
|
+
}),
|
|
4712
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4713
|
+
className: "nx-bottom-center",
|
|
4714
|
+
children: [show("scrollToContent") && /* @__PURE__ */ jsx(ScrollToContent, {}), zen && /* @__PURE__ */ jsx("button", {
|
|
4715
|
+
type: "button",
|
|
4716
|
+
className: "nx-pill",
|
|
4717
|
+
"data-testid": "exit-zen",
|
|
4718
|
+
onClick: () => editor.modes.setPresentation({ zen: false }),
|
|
4719
|
+
children: t("zen.exit")
|
|
4720
|
+
})]
|
|
4721
|
+
}),
|
|
4722
|
+
/* @__PURE__ */ jsxs("div", {
|
|
4723
|
+
className: "nx-bottom-right",
|
|
4724
|
+
children: [slots.bottomRight, !zen && show("help") && /* @__PURE__ */ jsx(Island, {
|
|
4725
|
+
className: "nx-row",
|
|
4726
|
+
children: /* @__PURE__ */ jsx(IconButton, {
|
|
4727
|
+
icon: "help",
|
|
4728
|
+
label: t("menu.shortcuts"),
|
|
4729
|
+
shortcut: "?",
|
|
4730
|
+
pressed: isOpen("shortcuts"),
|
|
4731
|
+
onClick: () => setOpen("shortcuts", true),
|
|
4732
|
+
testId: "help"
|
|
4733
|
+
})
|
|
4734
|
+
})]
|
|
4735
|
+
})
|
|
4736
|
+
]
|
|
4737
|
+
})
|
|
4738
|
+
]
|
|
4739
|
+
}),
|
|
4740
|
+
/* @__PURE__ */ jsx(ContextMenuComponent, {}),
|
|
4741
|
+
/* @__PURE__ */ jsx(CanvasCommands, {}),
|
|
4742
|
+
/* @__PURE__ */ jsx(ExportDialog, { ...fileName === void 0 ? {} : { fileName } }),
|
|
4743
|
+
/* @__PURE__ */ jsx(ShortcutsDialog, {}),
|
|
4744
|
+
linkEditorShown && /* @__PURE__ */ jsx(LinkEditor, {})
|
|
4745
|
+
]
|
|
4746
|
+
});
|
|
4747
|
+
}
|
|
4748
|
+
/**
|
|
4749
|
+
* Complete editor: canvas plus the default controls inside the given container. Renders its own
|
|
4750
|
+
* `EditorProvider` and `UiProvider`; every control can be hidden, replaced or extended.
|
|
4751
|
+
*/
|
|
4752
|
+
function DefaultEditor({ editor, theme, onThemeChange, messages, locale, onLocaleChange, dir = "ltr", hidden, components, registry, slots, canvas, minimapRenderers, initialOpen, fileName, className, onMount }) {
|
|
4753
|
+
const [root, setRoot] = useState(null);
|
|
4754
|
+
const [ownTheme, setOwnTheme] = useState(theme ?? "system");
|
|
4755
|
+
const themeSetting = onThemeChange === void 0 ? ownTheme : theme ?? "system";
|
|
4756
|
+
const changeTheme = useCallback((next) => onThemeChange === void 0 ? setOwnTheme(next) : onThemeChange(next), [onThemeChange]);
|
|
4757
|
+
return /* @__PURE__ */ jsx(EditorProvider, {
|
|
4758
|
+
editor,
|
|
4759
|
+
children: /* @__PURE__ */ jsxs(UiProvider, {
|
|
4760
|
+
theme: themeSetting,
|
|
4761
|
+
onThemeChange: changeTheme,
|
|
4762
|
+
portalContainer: root,
|
|
4763
|
+
dir,
|
|
4764
|
+
...messages === void 0 ? {} : { messages },
|
|
4765
|
+
...locale === void 0 ? {} : { locale },
|
|
4766
|
+
...onLocaleChange === void 0 ? {} : { onLocaleChange },
|
|
4767
|
+
...registry === void 0 ? {} : { registry },
|
|
4768
|
+
...hidden === void 0 ? {} : { hidden },
|
|
4769
|
+
...initialOpen === void 0 ? {} : { initialOpen },
|
|
4770
|
+
children: [/* @__PURE__ */ jsx(UiPlugins, {}), /* @__PURE__ */ jsx(EditorRoot, {
|
|
4771
|
+
ref: setRoot,
|
|
4772
|
+
className,
|
|
4773
|
+
dir,
|
|
4774
|
+
children: /* @__PURE__ */ jsx(Layout, {
|
|
4775
|
+
...components === void 0 ? {} : { components },
|
|
4776
|
+
...slots === void 0 ? {} : { slots },
|
|
4777
|
+
...canvas === void 0 ? {} : { canvas },
|
|
4778
|
+
...minimapRenderers === void 0 ? {} : { minimapRenderers },
|
|
4779
|
+
...fileName === void 0 ? {} : { fileName },
|
|
4780
|
+
...onMount === void 0 ? {} : { onMount }
|
|
4781
|
+
})
|
|
4782
|
+
})]
|
|
4783
|
+
})
|
|
4784
|
+
});
|
|
4785
|
+
}
|
|
4786
|
+
/**
|
|
4787
|
+
* Scoped root element for UI components: carries the theme, direction and the `.nx-editor`
|
|
4788
|
+
* class all styles are scoped to. Opens the shortcut reference on `?`.
|
|
4789
|
+
*/
|
|
4790
|
+
function EditorRoot({ ref, className, dir, children }) {
|
|
4791
|
+
const { theme, setOpen } = useUi();
|
|
4792
|
+
const [element, setElement] = useState(null);
|
|
4793
|
+
useEffect(() => {
|
|
4794
|
+
if (element === null) return;
|
|
4795
|
+
const onKey = (event) => {
|
|
4796
|
+
const target = event.target;
|
|
4797
|
+
if (event.key === "?" && target !== null && !target.isContentEditable && ![
|
|
4798
|
+
"INPUT",
|
|
4799
|
+
"TEXTAREA",
|
|
4800
|
+
"SELECT"
|
|
4801
|
+
].includes(target.tagName)) {
|
|
4802
|
+
event.preventDefault();
|
|
4803
|
+
setOpen("shortcuts", true);
|
|
4804
|
+
}
|
|
4805
|
+
};
|
|
4806
|
+
element.addEventListener("keydown", onKey);
|
|
4807
|
+
return () => element.removeEventListener("keydown", onKey);
|
|
4808
|
+
}, [element, setOpen]);
|
|
4809
|
+
return /* @__PURE__ */ jsx("div", {
|
|
4810
|
+
ref: (node) => {
|
|
4811
|
+
setElement(node);
|
|
4812
|
+
ref?.(node);
|
|
4813
|
+
},
|
|
4814
|
+
className: cx("nx-editor", className),
|
|
4815
|
+
"data-theme": theme,
|
|
4816
|
+
dir,
|
|
4817
|
+
"data-testid": "nx-editor",
|
|
4818
|
+
children
|
|
4819
|
+
});
|
|
4820
|
+
}
|
|
4821
|
+
export { CANVAS_PROPERTIES_COMMAND, COLOR_GRID, COLOR_SHADES, COPY_AS_PNG_COMMAND, COPY_AS_SVG_COMMAND, COPY_ELEMENT_LINK_COMMAND, CanvasCommands, CanvasPropertiesPanel, Choice, ColorField, ColorPicker, ContextMenu, DEFAULT_SHADE, DefaultEditor, Dialog, EditorRoot, ExportDialog, FILL_COLORS, FontFamilyField, FontPicker, GridControls, Icon, IconButton, Island, LANGUAGES, LINK_COMMAND, LaserButton, LayersPanel, LinkDialog, LinkEditor, MINIMAP_COMMAND, MainMenu, MenuItems, Minimap, Popover, PropertiesPanel, QUICK_FONTS, STROKE_COLORS, ScrollToContent, Section, ShortcutsDialog, Swatch, ToolButton, ToolLockButton, Toolbar, UiPlugins, UiProvider, UndoRedo, ZenToggle, ZoomControls, buildLayerRows, builtinContextMenuItems, builtinIcons, builtinMessages, byOrder, createTranslator, createUiRegistry, editorTreeSource, en, familyColor, familyOf, fontRoleLabel, formatShortcut, loadMessages, needsMessages, normalizeHex, openFile, useContextMenuForCanvas, useResolvedTheme, useT, useUi, visibleRange };
|