@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
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+
import { orderKeysBetween, toError } from '@drawback/core';
|
|
2
|
+
import type { AnyEditor, EditorEventName, ElementId, LayerId } from '@drawback/core';
|
|
3
|
+
import { useCanvasView, useEditor, useEditorValue, useSelection } from '@drawback/react';
|
|
4
|
+
import {
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
useSyncExternalStore,
|
|
11
|
+
type DragEvent,
|
|
12
|
+
type KeyboardEvent,
|
|
13
|
+
type MouseEvent,
|
|
14
|
+
type ReactNode,
|
|
15
|
+
} from 'react';
|
|
16
|
+
import { useUi } from '../../stores/ui-context.js';
|
|
17
|
+
import { Icon } from '../../components/icons.js';
|
|
18
|
+
import {
|
|
19
|
+
activeGuide,
|
|
20
|
+
buildLayerRows,
|
|
21
|
+
editorTreeSource,
|
|
22
|
+
stickyRows,
|
|
23
|
+
visibleRange,
|
|
24
|
+
type TreeRow,
|
|
25
|
+
} from './layers-tree.js';
|
|
26
|
+
import { IconButton, Island, cx } from '../../components/primitives.js';
|
|
27
|
+
|
|
28
|
+
const ROW_HEIGHT = 30;
|
|
29
|
+
const ROW_GAP = 1;
|
|
30
|
+
const ROW_STRIDE = ROW_HEIGHT + ROW_GAP;
|
|
31
|
+
const ROW_PADDING = 4;
|
|
32
|
+
const INDENT = 14;
|
|
33
|
+
const TOGGLE_SIZE = 16;
|
|
34
|
+
const TREE_PADDING = 6;
|
|
35
|
+
const END_KEY = 'end';
|
|
36
|
+
const TREE_EVENTS = [
|
|
37
|
+
'document.change',
|
|
38
|
+
'document.load',
|
|
39
|
+
'layer.change',
|
|
40
|
+
'layer.active',
|
|
41
|
+
'group.change',
|
|
42
|
+
'permissions.change',
|
|
43
|
+
'mode.change',
|
|
44
|
+
] as const;
|
|
45
|
+
|
|
46
|
+
type DropZone = 'above' | 'below' | 'inside';
|
|
47
|
+
|
|
48
|
+
interface DropTarget {
|
|
49
|
+
readonly key: string;
|
|
50
|
+
readonly row: TreeRow;
|
|
51
|
+
readonly zone: DropZone;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface DragState {
|
|
55
|
+
readonly kind: 'layer' | 'elements';
|
|
56
|
+
readonly ids: readonly string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function rowIds(row: TreeRow): readonly ElementId[] {
|
|
60
|
+
return row.kind === 'element' ? [row.record.id] : row.kind === 'group' ? row.members : [];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isLocked(row: TreeRow): boolean {
|
|
64
|
+
return row.record.locked;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isHiddenRow(row: TreeRow): boolean {
|
|
68
|
+
return row.kind === 'group' ? false : row.record.hidden;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function rowName(row: TreeRow, t: ReturnType<typeof useUi>['t']): string {
|
|
72
|
+
if (row.kind === 'layer') {
|
|
73
|
+
return row.record.name;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (row.kind === 'group') {
|
|
77
|
+
return row.record.name ?? t('layers.group');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return row.label;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function rename(editor: AnyEditor, row: TreeRow, name: string): void {
|
|
84
|
+
const trimmed = name.trim();
|
|
85
|
+
|
|
86
|
+
if (row.kind === 'layer') {
|
|
87
|
+
if (trimmed !== '') {
|
|
88
|
+
editor.layers.rename(row.record.id, trimmed);
|
|
89
|
+
}
|
|
90
|
+
} else if (row.kind === 'group') {
|
|
91
|
+
editor.groups.rename(row.record.id, trimmed);
|
|
92
|
+
} else {
|
|
93
|
+
editor.elements.update(row.record.id, { name: trimmed === '' ? undefined : trimmed } as never);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Counts an editor event; components that read it re-render each time the event fires. */
|
|
98
|
+
function useEventCount(editor: AnyEditor, name: EditorEventName): number {
|
|
99
|
+
const count = useRef(0);
|
|
100
|
+
const subscribe = useCallback(
|
|
101
|
+
(notify: () => void) =>
|
|
102
|
+
editor.events.on(name, () => {
|
|
103
|
+
count.current += 1;
|
|
104
|
+
notify();
|
|
105
|
+
}),
|
|
106
|
+
[editor, name],
|
|
107
|
+
);
|
|
108
|
+
const read = useCallback(() => count.current, []);
|
|
109
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Hiding and locking need `core.lock` on the row's layer, group or element. */
|
|
113
|
+
function canLock(editor: AnyEditor, row: TreeRow): boolean {
|
|
114
|
+
return editor.permissions.can(
|
|
115
|
+
'core.lock',
|
|
116
|
+
row.kind === 'layer'
|
|
117
|
+
? { layer: row.record.id }
|
|
118
|
+
: row.kind === 'group'
|
|
119
|
+
? { group: row.record.id }
|
|
120
|
+
: row.record.id,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function toggleVisible(editor: AnyEditor, row: TreeRow): void {
|
|
125
|
+
if (row.kind === 'layer') {
|
|
126
|
+
editor.layers.setVisible(row.record.id, row.record.hidden);
|
|
127
|
+
} else if (row.kind === 'element') {
|
|
128
|
+
editor.elements.update(row.record.id, { hidden: !row.record.hidden });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function toggleLocked(editor: AnyEditor, row: TreeRow): void {
|
|
133
|
+
if (row.kind === 'layer') {
|
|
134
|
+
editor.layers.setLocked(row.record.id, !row.record.locked);
|
|
135
|
+
} else if (row.kind === 'group') {
|
|
136
|
+
editor.groups.setLocked(row.record.id, !row.record.locked);
|
|
137
|
+
} else {
|
|
138
|
+
editor.elements.update(row.record.id, { locked: !row.record.locked });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function dropElements(
|
|
143
|
+
editor: AnyEditor,
|
|
144
|
+
ids: readonly ElementId[],
|
|
145
|
+
target: TreeRow,
|
|
146
|
+
zone: DropZone,
|
|
147
|
+
): void {
|
|
148
|
+
if (target.kind === 'layer') {
|
|
149
|
+
editor.layers.moveElements(ids, target.record.id, { position: 'top' });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const anchor = target.kind === 'element' ? target.record.id : target.members[0];
|
|
154
|
+
const anchorRecord = anchor === undefined ? undefined : editor.elements.get(anchor);
|
|
155
|
+
|
|
156
|
+
if (anchor === undefined || anchorRecord === undefined || ids.includes(anchor)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Editor reads inside a transaction see the committed document, so the target layer and order
|
|
161
|
+
// keys are computed up front and written in one transaction.
|
|
162
|
+
const wanted = new Set<ElementId>(ids);
|
|
163
|
+
const all = { includeHidden: true, includeRestricted: true } as const;
|
|
164
|
+
const moving = editor.elements.inRenderOrder(all).filter((element) => wanted.has(element.id));
|
|
165
|
+
const siblings = editor.elements
|
|
166
|
+
.inRenderOrder({ ...all, layerId: anchorRecord.layerId })
|
|
167
|
+
.filter((element) => !wanted.has(element.id));
|
|
168
|
+
const index = siblings.findIndex((element) => element.id === anchor);
|
|
169
|
+
const lower = zone === 'below' ? siblings[index - 1] : siblings[index];
|
|
170
|
+
const upper = zone === 'below' ? siblings[index] : siblings[index + 1];
|
|
171
|
+
const keys = orderKeysBetween(lower?.order ?? null, upper?.order ?? null, moving.length);
|
|
172
|
+
|
|
173
|
+
editor.transact(
|
|
174
|
+
(transaction) => {
|
|
175
|
+
moving.forEach((element, position) => {
|
|
176
|
+
const order = keys[position];
|
|
177
|
+
|
|
178
|
+
if (order !== undefined) {
|
|
179
|
+
transaction.elements.update(element.id, { layerId: anchorRecord.layerId, order });
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
{ label: 'Reorder' },
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function dropLayer(editor: AnyEditor, id: LayerId, target: TreeRow, zone: DropZone): void {
|
|
188
|
+
if (target.kind !== 'layer' || target.record.id === id) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (zone === 'inside') {
|
|
193
|
+
editor.layers.move(id, { parentId: target.record.id });
|
|
194
|
+
} else if (zone === 'above') {
|
|
195
|
+
editor.layers.move(id, { parentId: target.record.parentId ?? null, after: target.record.id });
|
|
196
|
+
} else {
|
|
197
|
+
editor.layers.move(id, { parentId: target.record.parentId ?? null, before: target.record.id });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** A small label next to the cursor instead of the browser's snapshot of the whole row. */
|
|
202
|
+
function setDragBadge(event: DragEvent<HTMLElement>, label: string): void {
|
|
203
|
+
const host = event.currentTarget.closest('.nx-layers') ?? event.currentTarget.ownerDocument.body;
|
|
204
|
+
const badge = event.currentTarget.ownerDocument.createElement('div');
|
|
205
|
+
badge.className = 'nx-drag-badge';
|
|
206
|
+
badge.textContent = label;
|
|
207
|
+
host.appendChild(badge);
|
|
208
|
+
event.dataTransfer.setDragImage(badge, -12, -8);
|
|
209
|
+
setTimeout(() => badge.remove(), 0);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function Thumbnail({ id }: { readonly id: ElementId }): ReactNode {
|
|
213
|
+
const view = useCanvasView();
|
|
214
|
+
const revision = useEditorValue((editor) => editor.elements.get(id), {
|
|
215
|
+
events: ['document.change'],
|
|
216
|
+
equals: Object.is,
|
|
217
|
+
});
|
|
218
|
+
const [url, setUrl] = useState<string | undefined>(undefined);
|
|
219
|
+
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
if (view === undefined) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
let objectUrl: string | undefined;
|
|
226
|
+
let cancelled = false;
|
|
227
|
+
void view
|
|
228
|
+
.exportImage({ ids: [id], padding: 2, scale: 0.25, background: 'transparent' })
|
|
229
|
+
.then((result) => {
|
|
230
|
+
if (!cancelled && result.blob !== undefined) {
|
|
231
|
+
objectUrl = URL.createObjectURL(result.blob);
|
|
232
|
+
setUrl(objectUrl);
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
.catch(() => undefined);
|
|
236
|
+
return () => {
|
|
237
|
+
cancelled = true;
|
|
238
|
+
|
|
239
|
+
if (objectUrl !== undefined) {
|
|
240
|
+
URL.revokeObjectURL(objectUrl);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}, [view, id, revision]);
|
|
244
|
+
|
|
245
|
+
return url === undefined ? (
|
|
246
|
+
<span className="nx-thumb" />
|
|
247
|
+
) : (
|
|
248
|
+
<img
|
|
249
|
+
className="nx-thumb"
|
|
250
|
+
src={url}
|
|
251
|
+
alt=""
|
|
252
|
+
/>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface LayersPanelProps {
|
|
257
|
+
/** Draw small element previews (default false). */
|
|
258
|
+
readonly thumbnails?: boolean;
|
|
259
|
+
readonly onClose?: () => void;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Layers, groups and elements as a virtualized tree: selection sync, rename, visibility and
|
|
264
|
+
* lock toggles, search, drag-and-drop reordering and moving between layers.
|
|
265
|
+
*/
|
|
266
|
+
export function LayersPanel({ thumbnails = false, onClose }: LayersPanelProps): ReactNode {
|
|
267
|
+
const editor = useEditor();
|
|
268
|
+
const { t, openContextMenu, isHidden } = useUi();
|
|
269
|
+
const selection = useSelection();
|
|
270
|
+
const selected = useMemo(() => new Set<string>(selection), [selection]);
|
|
271
|
+
// Permission changes (a new resolver or actor) re-render rows: hide and lock depend on them.
|
|
272
|
+
const permissionGeneration = useEventCount(editor, 'permissions.change');
|
|
273
|
+
const revision = `${useEditorValue(
|
|
274
|
+
(current) =>
|
|
275
|
+
`${current.document.revision}:${current.modes.current()}:${current.layers.active()}`,
|
|
276
|
+
{ events: TREE_EVENTS },
|
|
277
|
+
)}:${permissionGeneration}`;
|
|
278
|
+
const [expanded, setExpanded] = useState<ReadonlySet<string>>(new Set());
|
|
279
|
+
const [collapsed, setCollapsed] = useState<ReadonlySet<string>>(new Set());
|
|
280
|
+
const [query, setQuery] = useState('');
|
|
281
|
+
const [editing, setEditing] = useState<string | undefined>(undefined);
|
|
282
|
+
const [scroll, setScroll] = useState({ top: 0, height: 400 });
|
|
283
|
+
const [drop, setDrop] = useState<{ readonly key: string; readonly zone: DropZone } | undefined>(
|
|
284
|
+
undefined,
|
|
285
|
+
);
|
|
286
|
+
const drag = useRef<DragState | undefined>(undefined);
|
|
287
|
+
const anchor = useRef<ElementId | undefined>(undefined);
|
|
288
|
+
const listRef = useRef<HTMLDivElement>(null);
|
|
289
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
290
|
+
const rows = useMemo(
|
|
291
|
+
() => buildLayerRows(editorTreeSource(editor), { expanded, collapsed, query }),
|
|
292
|
+
[editor, revision, expanded, collapsed, query],
|
|
293
|
+
);
|
|
294
|
+
const range = visibleRange(scroll.top, scroll.height, ROW_STRIDE, rows.length);
|
|
295
|
+
const sticky = useMemo(
|
|
296
|
+
() => stickyRows(rows, scroll.top, ROW_STRIDE, TREE_PADDING),
|
|
297
|
+
[rows, scroll.top],
|
|
298
|
+
);
|
|
299
|
+
const guide = useMemo(() => {
|
|
300
|
+
const anchorIndex = rows.findIndex((row) => {
|
|
301
|
+
const ids = rowIds(row);
|
|
302
|
+
return row.kind !== 'layer' && ids.length > 0 && ids.every((id) => selected.has(id));
|
|
303
|
+
});
|
|
304
|
+
return activeGuide(
|
|
305
|
+
rows,
|
|
306
|
+
anchorIndex >= 0 ? anchorIndex : rows.findIndex((row) => row.kind === 'layer' && row.active),
|
|
307
|
+
);
|
|
308
|
+
}, [rows, selected]);
|
|
309
|
+
|
|
310
|
+
useEffect(() => {
|
|
311
|
+
const list = listRef.current;
|
|
312
|
+
|
|
313
|
+
if (list === null) {
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const measure = (): void =>
|
|
318
|
+
setScroll({ top: list.scrollTop, height: list.clientHeight || 400 });
|
|
319
|
+
measure();
|
|
320
|
+
const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(measure) : undefined;
|
|
321
|
+
observer?.observe(list);
|
|
322
|
+
return () => observer?.disconnect();
|
|
323
|
+
}, []);
|
|
324
|
+
|
|
325
|
+
const toggleExpanded = (row: TreeRow): void => {
|
|
326
|
+
if (row.kind === 'layer') {
|
|
327
|
+
setCollapsed((current) => {
|
|
328
|
+
const next = new Set(current);
|
|
329
|
+
|
|
330
|
+
if (next.has(row.key)) {
|
|
331
|
+
next.delete(row.key);
|
|
332
|
+
} else {
|
|
333
|
+
next.add(row.key);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return next;
|
|
337
|
+
});
|
|
338
|
+
} else {
|
|
339
|
+
setExpanded((current) => {
|
|
340
|
+
const next = new Set(current);
|
|
341
|
+
|
|
342
|
+
if (next.has(row.key)) {
|
|
343
|
+
next.delete(row.key);
|
|
344
|
+
} else {
|
|
345
|
+
next.add(row.key);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return next;
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
const select = (row: TreeRow, event: MouseEvent | KeyboardEvent): void => {
|
|
354
|
+
if (row.kind === 'layer') {
|
|
355
|
+
editor.layers.setActive(row.record.id);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const ids = rowIds(row);
|
|
360
|
+
|
|
361
|
+
if (event.shiftKey && anchor.current !== undefined && row.kind === 'element') {
|
|
362
|
+
const elementRows = rows.filter((entry) => entry.kind === 'element');
|
|
363
|
+
const from = elementRows.findIndex((entry) => entry.record.id === anchor.current);
|
|
364
|
+
const to = elementRows.findIndex((entry) => entry.record.id === row.record.id);
|
|
365
|
+
|
|
366
|
+
if (from >= 0 && to >= 0) {
|
|
367
|
+
const [low, high] = from < to ? [from, to] : [to, from];
|
|
368
|
+
editor.selection.set(
|
|
369
|
+
elementRows.slice(low, high + 1).map((entry) => entry.record.id as ElementId),
|
|
370
|
+
);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (event.metaKey || event.ctrlKey) {
|
|
376
|
+
const current = new Set(editor.selection.get());
|
|
377
|
+
const all = ids.every((id) => current.has(id));
|
|
378
|
+
|
|
379
|
+
for (const id of ids) {
|
|
380
|
+
if (all) {
|
|
381
|
+
current.delete(id);
|
|
382
|
+
} else {
|
|
383
|
+
current.add(id);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
editor.selection.set([...current]);
|
|
388
|
+
} else {
|
|
389
|
+
editor.selection.set(ids);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
anchor.current = ids[0];
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const onRowKeyDown = (row: TreeRow, event: KeyboardEvent<HTMLDivElement>): void => {
|
|
396
|
+
if (editing !== undefined) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (event.key === 'F2') {
|
|
401
|
+
event.preventDefault();
|
|
402
|
+
setEditing(row.key);
|
|
403
|
+
} else if (event.key === 'Enter' || event.key === ' ') {
|
|
404
|
+
event.preventDefault();
|
|
405
|
+
select(row, event);
|
|
406
|
+
} else if (event.altKey && (event.key === 'ArrowUp' || event.key === 'ArrowDown')) {
|
|
407
|
+
event.preventDefault();
|
|
408
|
+
const up = event.key === 'ArrowUp';
|
|
409
|
+
|
|
410
|
+
if (row.kind === 'layer') {
|
|
411
|
+
const index = rows.indexOf(row);
|
|
412
|
+
const sibling = rows
|
|
413
|
+
.slice(0, index)
|
|
414
|
+
.reverse()
|
|
415
|
+
.concat(rows.slice(index + 1))
|
|
416
|
+
.find(
|
|
417
|
+
(entry) =>
|
|
418
|
+
entry.kind === 'layer' &&
|
|
419
|
+
entry.depth === row.depth &&
|
|
420
|
+
(up ? rows.indexOf(entry) < index : rows.indexOf(entry) > index),
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
if (sibling !== undefined) {
|
|
424
|
+
dropLayer(editor, row.record.id, sibling, up ? 'above' : 'below');
|
|
425
|
+
}
|
|
426
|
+
} else if (row.kind === 'element') {
|
|
427
|
+
if (up) {
|
|
428
|
+
editor.order.bringForward([row.record.id]);
|
|
429
|
+
} else {
|
|
430
|
+
editor.order.sendBackward([row.record.id]);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
} else if (event.key === 'ArrowRight' && row.expandable && !row.expanded) {
|
|
434
|
+
toggleExpanded(row);
|
|
435
|
+
} else if (event.key === 'ArrowLeft' && row.expanded) {
|
|
436
|
+
toggleExpanded(row);
|
|
437
|
+
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
438
|
+
event.preventDefault();
|
|
439
|
+
const next = (event.currentTarget.parentElement?.querySelector(
|
|
440
|
+
`[data-row-index="${rows.indexOf(row) + (event.key === 'ArrowDown' ? 1 : -1)}"]`,
|
|
441
|
+
) ?? null) as HTMLElement | null;
|
|
442
|
+
next?.focus();
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
const zoneOf = (row: TreeRow, ratio: number): DropZone => {
|
|
447
|
+
if (row.kind === 'layer' && drag.current?.kind === 'layer') {
|
|
448
|
+
return ratio < 0.25 ? 'above' : ratio > 0.75 ? 'below' : 'inside';
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (row.kind === 'layer') {
|
|
452
|
+
return 'inside';
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return ratio < 0.5 ? 'above' : 'below';
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const canDrop = (row: TreeRow): boolean =>
|
|
459
|
+
drag.current !== undefined && (drag.current.kind === 'elements' || row.kind === 'layer');
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The drop target under the pointer, from its offset in the list: the gap between two rows
|
|
463
|
+
* counts as the lower part of the row above, so moving across rows or their children never
|
|
464
|
+
* clears the indicator. Pinned layer rows take drops themselves, and the empty space below
|
|
465
|
+
* the rows moves a layer to the bottom of the root.
|
|
466
|
+
*/
|
|
467
|
+
const dropTargetAt = (event: DragEvent<HTMLDivElement>): DropTarget | undefined => {
|
|
468
|
+
const list = listRef.current;
|
|
469
|
+
const content = contentRef.current;
|
|
470
|
+
|
|
471
|
+
if (drag.current === undefined || list === null || content === null || rows.length === 0) {
|
|
472
|
+
return undefined;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const viewportY = event.clientY - list.getBoundingClientRect().top;
|
|
476
|
+
const pinned = sticky.find(
|
|
477
|
+
(entry) => viewportY >= entry.top && viewportY < entry.top + ROW_STRIDE,
|
|
478
|
+
);
|
|
479
|
+
const pinnedRow = pinned === undefined ? undefined : rows[pinned.index];
|
|
480
|
+
|
|
481
|
+
if (pinned !== undefined && pinnedRow !== undefined) {
|
|
482
|
+
return canDrop(pinnedRow)
|
|
483
|
+
? {
|
|
484
|
+
key: pinnedRow.key,
|
|
485
|
+
row: pinnedRow,
|
|
486
|
+
zone: zoneOf(pinnedRow, (viewportY - pinned.top) / ROW_HEIGHT),
|
|
487
|
+
}
|
|
488
|
+
: undefined;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const offset = event.clientY - content.getBoundingClientRect().top - TREE_PADDING;
|
|
492
|
+
const index = Math.max(0, Math.floor(offset / ROW_STRIDE));
|
|
493
|
+
const row = rows[index];
|
|
494
|
+
|
|
495
|
+
if (row !== undefined) {
|
|
496
|
+
const ratio = offset < 0 ? 0 : (offset % ROW_STRIDE) / ROW_HEIGHT;
|
|
497
|
+
return canDrop(row) ? { key: row.key, row, zone: zoneOf(row, ratio) } : undefined;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (drag.current.kind === 'layer') {
|
|
501
|
+
const root = [...rows].reverse().find((entry) => entry.kind === 'layer' && entry.depth === 0);
|
|
502
|
+
return root === undefined ? undefined : { key: END_KEY, row: root, zone: 'below' };
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const last = rows[rows.length - 1];
|
|
506
|
+
return last === undefined || !canDrop(last)
|
|
507
|
+
? undefined
|
|
508
|
+
: { key: last.key, row: last, zone: zoneOf(last, 1) };
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
const showDrop = (target: DropTarget | undefined) =>
|
|
512
|
+
setDrop((current) =>
|
|
513
|
+
target === undefined
|
|
514
|
+
? undefined
|
|
515
|
+
: current?.key === target.key && current.zone === target.zone
|
|
516
|
+
? current
|
|
517
|
+
: { key: target.key, zone: target.zone },
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
const renderRow = (row: TreeRow, index: number, top: number, pinned = false): ReactNode => {
|
|
521
|
+
const ids = rowIds(row);
|
|
522
|
+
const lockable = canLock(editor, row);
|
|
523
|
+
const isSelected =
|
|
524
|
+
row.kind === 'layer' ? row.active : ids.length > 0 && ids.every((id) => selected.has(id));
|
|
525
|
+
const reorderable =
|
|
526
|
+
row.kind === 'layer'
|
|
527
|
+
? editor.permissions.can('core.manage-layers', { layer: row.record.id })
|
|
528
|
+
: ids.every((id) => editor.permissions.can('core.reorder', id));
|
|
529
|
+
const name = rowName(row, t);
|
|
530
|
+
return (
|
|
531
|
+
<div
|
|
532
|
+
key={row.key}
|
|
533
|
+
{...(pinned
|
|
534
|
+
? { 'aria-hidden': true, tabIndex: -1, 'data-testid': `layer-pinned-${row.record.id}` }
|
|
535
|
+
: {
|
|
536
|
+
role: 'treeitem',
|
|
537
|
+
'aria-level': row.depth + 1,
|
|
538
|
+
'aria-selected': isSelected,
|
|
539
|
+
'aria-expanded': row.expandable ? row.expanded : undefined,
|
|
540
|
+
tabIndex: index === 0 ? 0 : -1,
|
|
541
|
+
'data-row-index': index,
|
|
542
|
+
'data-testid': `layer-row-${row.kind}-${row.record.id}`,
|
|
543
|
+
})}
|
|
544
|
+
className={cx(
|
|
545
|
+
'nx-row-item',
|
|
546
|
+
`nx-row-${row.kind}`,
|
|
547
|
+
isSelected && 'nx-selected',
|
|
548
|
+
isHiddenRow(row) && 'nx-dimmed',
|
|
549
|
+
drop?.key === row.key && `nx-drop-${drop.zone}`,
|
|
550
|
+
)}
|
|
551
|
+
style={{
|
|
552
|
+
position: 'absolute',
|
|
553
|
+
top,
|
|
554
|
+
height: ROW_HEIGHT,
|
|
555
|
+
left: 0,
|
|
556
|
+
right: 0,
|
|
557
|
+
paddingInlineStart: ROW_PADDING + row.depth * INDENT,
|
|
558
|
+
['--nx-row-indent' as string]: `${ROW_PADDING + row.depth * INDENT}px`,
|
|
559
|
+
}}
|
|
560
|
+
title={
|
|
561
|
+
reorderable
|
|
562
|
+
? undefined
|
|
563
|
+
: editor.permissions.explain(
|
|
564
|
+
row.kind === 'layer' ? 'core.manage-layers' : 'core.reorder',
|
|
565
|
+
row.kind === 'layer' ? { layer: row.record.id } : ids[0],
|
|
566
|
+
).reason
|
|
567
|
+
}
|
|
568
|
+
draggable={reorderable && editing !== row.key}
|
|
569
|
+
onClick={(event) => {
|
|
570
|
+
select(row, event);
|
|
571
|
+
|
|
572
|
+
if (pinned && listRef.current !== null) {
|
|
573
|
+
listRef.current.scrollTop = TREE_PADDING + (index - row.depth) * ROW_STRIDE;
|
|
574
|
+
}
|
|
575
|
+
}}
|
|
576
|
+
onDoubleClick={() => setEditing(row.key)}
|
|
577
|
+
onKeyDown={(event) => onRowKeyDown(row, event)}
|
|
578
|
+
onContextMenu={(event) => {
|
|
579
|
+
event.preventDefault();
|
|
580
|
+
|
|
581
|
+
if (row.kind !== 'layer' && !isSelected) {
|
|
582
|
+
editor.selection.set(ids);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
openContextMenu({
|
|
586
|
+
clientX: event.clientX,
|
|
587
|
+
clientY: event.clientY,
|
|
588
|
+
...(row.kind === 'element' ? { elementId: row.record.id } : {}),
|
|
589
|
+
});
|
|
590
|
+
}}
|
|
591
|
+
onDragStart={(event) => {
|
|
592
|
+
const dragging =
|
|
593
|
+
row.kind === 'layer'
|
|
594
|
+
? { kind: 'layer' as const, ids: [row.record.id] }
|
|
595
|
+
: {
|
|
596
|
+
kind: 'elements' as const,
|
|
597
|
+
ids: isSelected ? editor.selection.get() : ids,
|
|
598
|
+
};
|
|
599
|
+
drag.current = dragging;
|
|
600
|
+
event.dataTransfer.effectAllowed = 'move';
|
|
601
|
+
const own: readonly string[] = row.kind === 'layer' ? [row.record.id] : ids;
|
|
602
|
+
setDragBadge(
|
|
603
|
+
event,
|
|
604
|
+
dragging.ids.length === own.length && dragging.ids.every((id) => own.includes(id))
|
|
605
|
+
? name
|
|
606
|
+
: t('layers.dragCount', { count: dragging.ids.length }),
|
|
607
|
+
);
|
|
608
|
+
event.dataTransfer.setData('application/x-drawback-layers', dragging.ids.join(','));
|
|
609
|
+
}}
|
|
610
|
+
onDragEnd={() => {
|
|
611
|
+
drag.current = undefined;
|
|
612
|
+
setDrop(undefined);
|
|
613
|
+
}}
|
|
614
|
+
>
|
|
615
|
+
{Array.from({ length: row.depth }, (_, level) => (
|
|
616
|
+
<span
|
|
617
|
+
key={level}
|
|
618
|
+
aria-hidden="true"
|
|
619
|
+
className={cx(
|
|
620
|
+
'nx-tree-guide',
|
|
621
|
+
guide !== undefined &&
|
|
622
|
+
guide.level === level &&
|
|
623
|
+
index >= guide.start &&
|
|
624
|
+
index < guide.end &&
|
|
625
|
+
'nx-active',
|
|
626
|
+
)}
|
|
627
|
+
style={{ insetInlineStart: ROW_PADDING + level * INDENT + TOGGLE_SIZE / 2 }}
|
|
628
|
+
/>
|
|
629
|
+
))}
|
|
630
|
+
{row.expandable ? (
|
|
631
|
+
<button
|
|
632
|
+
type="button"
|
|
633
|
+
className="nx-tree-toggle"
|
|
634
|
+
tabIndex={-1}
|
|
635
|
+
aria-label={row.expanded ? t('layers.collapse') : t('layers.expand')}
|
|
636
|
+
onClick={(event) => {
|
|
637
|
+
event.stopPropagation();
|
|
638
|
+
toggleExpanded(row);
|
|
639
|
+
}}
|
|
640
|
+
>
|
|
641
|
+
<Icon name={row.expanded ? 'collapse' : 'expand'} />
|
|
642
|
+
</button>
|
|
643
|
+
) : (
|
|
644
|
+
<span className="nx-tree-toggle" />
|
|
645
|
+
)}
|
|
646
|
+
{row.kind === 'element' && thumbnails ? (
|
|
647
|
+
<Thumbnail id={row.record.id} />
|
|
648
|
+
) : (
|
|
649
|
+
<Icon
|
|
650
|
+
name={row.kind === 'layer' ? 'layers' : row.kind === 'group' ? 'group' : row.icon}
|
|
651
|
+
/>
|
|
652
|
+
)}
|
|
653
|
+
{editing === row.key ? (
|
|
654
|
+
<input
|
|
655
|
+
className="nx-rename"
|
|
656
|
+
defaultValue={name}
|
|
657
|
+
aria-label={t('layers.rename')}
|
|
658
|
+
autoFocus
|
|
659
|
+
data-testid="layer-rename"
|
|
660
|
+
onClick={(event) => event.stopPropagation()}
|
|
661
|
+
onKeyDown={(event) => {
|
|
662
|
+
if (event.key === 'Enter') {
|
|
663
|
+
rename(editor, row, event.currentTarget.value);
|
|
664
|
+
setEditing(undefined);
|
|
665
|
+
} else if (event.key === 'Escape') {
|
|
666
|
+
event.stopPropagation();
|
|
667
|
+
setEditing(undefined);
|
|
668
|
+
}
|
|
669
|
+
}}
|
|
670
|
+
onBlur={(event) => {
|
|
671
|
+
rename(editor, row, event.currentTarget.value);
|
|
672
|
+
setEditing(undefined);
|
|
673
|
+
}}
|
|
674
|
+
/>
|
|
675
|
+
) : (
|
|
676
|
+
<span className="nx-row-name">{name}</span>
|
|
677
|
+
)}
|
|
678
|
+
<span
|
|
679
|
+
className="nx-row-actions"
|
|
680
|
+
onClick={(event) => event.stopPropagation()}
|
|
681
|
+
onDoubleClick={(event) => event.stopPropagation()}
|
|
682
|
+
>
|
|
683
|
+
{row.kind !== 'group' && (
|
|
684
|
+
<IconButton
|
|
685
|
+
icon={isHiddenRow(row) ? 'eyeOff' : 'eye'}
|
|
686
|
+
label={isHiddenRow(row) ? t('layers.show') : t('layers.hide')}
|
|
687
|
+
disabled={!lockable}
|
|
688
|
+
onClick={() => toggleVisible(editor, row)}
|
|
689
|
+
className="nx-row-action"
|
|
690
|
+
tabIndex={-1}
|
|
691
|
+
testId={`layer-visibility-${row.record.id}`}
|
|
692
|
+
/>
|
|
693
|
+
)}
|
|
694
|
+
<IconButton
|
|
695
|
+
icon={isLocked(row) ? 'lock' : 'unlock'}
|
|
696
|
+
label={isLocked(row) ? t('layers.unlock') : t('layers.lock')}
|
|
697
|
+
disabled={!lockable}
|
|
698
|
+
onClick={() => toggleLocked(editor, row)}
|
|
699
|
+
className={cx('nx-row-action', isLocked(row) && 'nx-on')}
|
|
700
|
+
tabIndex={-1}
|
|
701
|
+
testId={`layer-lock-${row.record.id}`}
|
|
702
|
+
/>
|
|
703
|
+
</span>
|
|
704
|
+
</div>
|
|
705
|
+
);
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
const addLayer = (): void => {
|
|
709
|
+
const id = editor.layers.create({
|
|
710
|
+
name: t('layers.newName', { number: editor.layers.list().length + 1 }),
|
|
711
|
+
});
|
|
712
|
+
editor.layers.setActive(id);
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
return (
|
|
716
|
+
<Island
|
|
717
|
+
className="nx-layers"
|
|
718
|
+
label={t('layers.label')}
|
|
719
|
+
testId="layers-panel"
|
|
720
|
+
>
|
|
721
|
+
<header className="nx-panel-header">
|
|
722
|
+
<h2>{t('layers.label')}</h2>
|
|
723
|
+
{!isHidden('layers:add') && (
|
|
724
|
+
<IconButton
|
|
725
|
+
icon="add"
|
|
726
|
+
label={t('layers.add')}
|
|
727
|
+
onClick={addLayer}
|
|
728
|
+
testId="layers-add"
|
|
729
|
+
disabled={!editor.permissions.can('core.manage-layers')}
|
|
730
|
+
/>
|
|
731
|
+
)}
|
|
732
|
+
{onClose !== undefined && (
|
|
733
|
+
<IconButton
|
|
734
|
+
icon="close"
|
|
735
|
+
label={t('dialog.close')}
|
|
736
|
+
onClick={onClose}
|
|
737
|
+
testId="layers-close"
|
|
738
|
+
/>
|
|
739
|
+
)}
|
|
740
|
+
</header>
|
|
741
|
+
<label className="nx-search">
|
|
742
|
+
<Icon name="search" />
|
|
743
|
+
<input
|
|
744
|
+
type="search"
|
|
745
|
+
placeholder={t('layers.search')}
|
|
746
|
+
aria-label={t('layers.search')}
|
|
747
|
+
value={query}
|
|
748
|
+
data-testid="layers-search"
|
|
749
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
750
|
+
/>
|
|
751
|
+
</label>
|
|
752
|
+
<div
|
|
753
|
+
ref={listRef}
|
|
754
|
+
className="nx-tree"
|
|
755
|
+
role="tree"
|
|
756
|
+
aria-label={t('layers.label')}
|
|
757
|
+
aria-multiselectable="true"
|
|
758
|
+
data-testid="layers-tree"
|
|
759
|
+
onScroll={(event) =>
|
|
760
|
+
setScroll({
|
|
761
|
+
top: event.currentTarget.scrollTop,
|
|
762
|
+
height: event.currentTarget.clientHeight,
|
|
763
|
+
})
|
|
764
|
+
}
|
|
765
|
+
onDragOver={(event) => {
|
|
766
|
+
const target = dropTargetAt(event);
|
|
767
|
+
showDrop(target);
|
|
768
|
+
|
|
769
|
+
if (target !== undefined) {
|
|
770
|
+
event.preventDefault();
|
|
771
|
+
}
|
|
772
|
+
}}
|
|
773
|
+
onDragLeave={(event) => {
|
|
774
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
775
|
+
const inside =
|
|
776
|
+
event.clientX > rect.left &&
|
|
777
|
+
event.clientX < rect.right &&
|
|
778
|
+
event.clientY > rect.top &&
|
|
779
|
+
event.clientY < rect.bottom;
|
|
780
|
+
|
|
781
|
+
if (!inside) {
|
|
782
|
+
showDrop(undefined);
|
|
783
|
+
}
|
|
784
|
+
}}
|
|
785
|
+
onDrop={(event) => {
|
|
786
|
+
event.preventDefault();
|
|
787
|
+
const dragging = drag.current;
|
|
788
|
+
const target = dropTargetAt(event);
|
|
789
|
+
drag.current = undefined;
|
|
790
|
+
showDrop(undefined);
|
|
791
|
+
|
|
792
|
+
if (dragging === undefined || target === undefined) {
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
try {
|
|
797
|
+
if (dragging.kind === 'layer') {
|
|
798
|
+
dropLayer(editor, dragging.ids[0] as LayerId, target.row, target.zone);
|
|
799
|
+
} else {
|
|
800
|
+
dropElements(editor, dragging.ids as ElementId[], target.row, target.zone);
|
|
801
|
+
}
|
|
802
|
+
} catch (error) {
|
|
803
|
+
editor.events.reportError(toError(error), 'layers panel drop');
|
|
804
|
+
}
|
|
805
|
+
}}
|
|
806
|
+
>
|
|
807
|
+
{rows.length === 0 && (
|
|
808
|
+
<p className="nx-caption">{query === '' ? t('layers.empty') : t('layers.noMatches')}</p>
|
|
809
|
+
)}
|
|
810
|
+
{sticky.length > 0 && (
|
|
811
|
+
<div className="nx-tree-pinned">
|
|
812
|
+
{sticky.map((entry, position) => {
|
|
813
|
+
const row = rows[entry.index];
|
|
814
|
+
return row === undefined ? null : (
|
|
815
|
+
<div
|
|
816
|
+
key={row.key}
|
|
817
|
+
className={cx(
|
|
818
|
+
'nx-pinned-row',
|
|
819
|
+
position === sticky.length - 1 && 'nx-pinned-last',
|
|
820
|
+
)}
|
|
821
|
+
style={{ top: entry.top, height: ROW_STRIDE, zIndex: sticky.length - position }}
|
|
822
|
+
>
|
|
823
|
+
{renderRow(row, entry.index, 0, true)}
|
|
824
|
+
</div>
|
|
825
|
+
);
|
|
826
|
+
})}
|
|
827
|
+
</div>
|
|
828
|
+
)}
|
|
829
|
+
<div
|
|
830
|
+
ref={contentRef}
|
|
831
|
+
style={{
|
|
832
|
+
height: rows.length === 0 ? 0 : rows.length * ROW_STRIDE - ROW_GAP + 2 * TREE_PADDING,
|
|
833
|
+
position: 'relative',
|
|
834
|
+
}}
|
|
835
|
+
>
|
|
836
|
+
{rows
|
|
837
|
+
.slice(range.start, range.end)
|
|
838
|
+
.map((row, offset) =>
|
|
839
|
+
renderRow(
|
|
840
|
+
row,
|
|
841
|
+
range.start + offset,
|
|
842
|
+
TREE_PADDING + (range.start + offset) * ROW_STRIDE,
|
|
843
|
+
),
|
|
844
|
+
)}
|
|
845
|
+
{drop?.key === END_KEY && (
|
|
846
|
+
<div
|
|
847
|
+
className="nx-drop-end nx-drop-below"
|
|
848
|
+
style={{
|
|
849
|
+
top: TREE_PADDING + rows.length * ROW_STRIDE - ROW_GAP,
|
|
850
|
+
['--nx-row-indent' as string]: `${ROW_PADDING}px`,
|
|
851
|
+
}}
|
|
852
|
+
/>
|
|
853
|
+
)}
|
|
854
|
+
</div>
|
|
855
|
+
</div>
|
|
856
|
+
</Island>
|
|
857
|
+
);
|
|
858
|
+
}
|