@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,281 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useActiveTool,
|
|
3
|
+
useCommand,
|
|
4
|
+
useEditor,
|
|
5
|
+
useEditorValue,
|
|
6
|
+
useGrid,
|
|
7
|
+
usePresentation,
|
|
8
|
+
} from '@drawback/react';
|
|
9
|
+
import type { AnyEditor } from '@drawback/core';
|
|
10
|
+
import { useMemo, useRef, useState, type ReactNode } from 'react';
|
|
11
|
+
import { useUi } from '../../stores/ui-context.js';
|
|
12
|
+
import {
|
|
13
|
+
IconButton,
|
|
14
|
+
Island,
|
|
15
|
+
MenuItems,
|
|
16
|
+
Popover,
|
|
17
|
+
type MenuEntry,
|
|
18
|
+
} from '../../components/primitives.js';
|
|
19
|
+
|
|
20
|
+
export function UndoRedo(): ReactNode {
|
|
21
|
+
const { t } = useUi();
|
|
22
|
+
const undo = useCommand('core.undo');
|
|
23
|
+
const redo = useCommand('core.redo');
|
|
24
|
+
return (
|
|
25
|
+
<Island
|
|
26
|
+
className="nx-row"
|
|
27
|
+
testId="undo-redo"
|
|
28
|
+
>
|
|
29
|
+
<IconButton
|
|
30
|
+
icon="undo"
|
|
31
|
+
label={t('history.undo')}
|
|
32
|
+
shortcut={undo.shortcuts}
|
|
33
|
+
disabled={!undo.enabled}
|
|
34
|
+
onClick={() => undo.run()}
|
|
35
|
+
testId="undo"
|
|
36
|
+
/>
|
|
37
|
+
<IconButton
|
|
38
|
+
icon="redo"
|
|
39
|
+
label={t('history.redo')}
|
|
40
|
+
shortcut={redo.shortcuts}
|
|
41
|
+
disabled={!redo.enabled}
|
|
42
|
+
onClick={() => redo.run()}
|
|
43
|
+
testId="redo"
|
|
44
|
+
/>
|
|
45
|
+
</Island>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ZoomControlsProps {
|
|
50
|
+
/** Shown above the controls while the minimap toggle is pressed; without it there is no toggle. */
|
|
51
|
+
readonly minimap?: ReactNode;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function ZoomControls({ minimap }: ZoomControlsProps = {}): ReactNode {
|
|
55
|
+
const { t, isOpen, setOpen } = useUi();
|
|
56
|
+
// Zoom only: panning must not re-render the controls.
|
|
57
|
+
const zoom = useEditorValue((current) => current.viewport.get().zoom, {
|
|
58
|
+
events: ['viewport.change'],
|
|
59
|
+
});
|
|
60
|
+
const zoomIn = useCommand('core.zoomIn');
|
|
61
|
+
const zoomOut = useCommand('core.zoomOut');
|
|
62
|
+
const reset = useCommand('core.resetZoom');
|
|
63
|
+
const fit = useCommand('core.zoomToFit');
|
|
64
|
+
const toSelection = useCommand('core.zoomToSelection');
|
|
65
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
66
|
+
const anchor = useRef<HTMLButtonElement>(null);
|
|
67
|
+
const percent = Math.round(zoom * 100);
|
|
68
|
+
const minimapOpen = minimap !== undefined && isOpen('minimap');
|
|
69
|
+
const entries: readonly MenuEntry[] = [
|
|
70
|
+
{
|
|
71
|
+
id: 'zoom-in',
|
|
72
|
+
label: t('zoom.in'),
|
|
73
|
+
icon: 'add',
|
|
74
|
+
shortcut: zoomIn.shortcuts[0],
|
|
75
|
+
onSelect: () => zoomIn.run(),
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
id: 'zoom-out',
|
|
79
|
+
label: t('zoom.out'),
|
|
80
|
+
icon: 'minus',
|
|
81
|
+
shortcut: zoomOut.shortcuts[0],
|
|
82
|
+
onSelect: () => zoomOut.run(),
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 'zoom-fit',
|
|
86
|
+
label: t('zoom.fit'),
|
|
87
|
+
icon: 'zoomFit',
|
|
88
|
+
shortcut: fit.shortcuts[0],
|
|
89
|
+
separatorBefore: true,
|
|
90
|
+
onSelect: () => fit.run(),
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
id: 'zoom-selection',
|
|
94
|
+
label: t('zoom.toSelection'),
|
|
95
|
+
icon: 'zoomFit',
|
|
96
|
+
shortcut: toSelection.shortcuts[0],
|
|
97
|
+
disabled: !toSelection.enabled,
|
|
98
|
+
onSelect: () => toSelection.run(),
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: 'zoom-reset',
|
|
102
|
+
label: t('zoom.reset'),
|
|
103
|
+
icon: 'zoomReset',
|
|
104
|
+
shortcut: reset.shortcuts[0],
|
|
105
|
+
onSelect: () => reset.run(),
|
|
106
|
+
},
|
|
107
|
+
];
|
|
108
|
+
return (
|
|
109
|
+
<div className="nx-zoom">
|
|
110
|
+
{minimapOpen && <div className="nx-minimap-card">{minimap}</div>}
|
|
111
|
+
<Island
|
|
112
|
+
className="nx-row"
|
|
113
|
+
testId="zoom-controls"
|
|
114
|
+
>
|
|
115
|
+
<button
|
|
116
|
+
ref={anchor}
|
|
117
|
+
type="button"
|
|
118
|
+
className="nx-zoom-level"
|
|
119
|
+
aria-haspopup="menu"
|
|
120
|
+
aria-expanded={menuOpen}
|
|
121
|
+
aria-label={`${t('zoom.menu')} (${t('zoom.level', { percent })})`}
|
|
122
|
+
title={t('zoom.menu')}
|
|
123
|
+
onClick={() => setMenuOpen(!menuOpen)}
|
|
124
|
+
data-testid="zoom-menu"
|
|
125
|
+
>
|
|
126
|
+
{t('zoom.level', { percent })}
|
|
127
|
+
</button>
|
|
128
|
+
{minimap !== undefined && (
|
|
129
|
+
<IconButton
|
|
130
|
+
icon="map"
|
|
131
|
+
label={t('minimap.toggle')}
|
|
132
|
+
pressed={minimapOpen}
|
|
133
|
+
onClick={() => setOpen('minimap', !minimapOpen)}
|
|
134
|
+
className="nx-minimap-toggle"
|
|
135
|
+
testId="minimap-toggle"
|
|
136
|
+
/>
|
|
137
|
+
)}
|
|
138
|
+
</Island>
|
|
139
|
+
<Popover
|
|
140
|
+
open={menuOpen}
|
|
141
|
+
onClose={() => setMenuOpen(false)}
|
|
142
|
+
anchor={anchor}
|
|
143
|
+
placement="top-start"
|
|
144
|
+
role="menu"
|
|
145
|
+
label={t('zoom.menu')}
|
|
146
|
+
testId="zoom-menu-popover"
|
|
147
|
+
>
|
|
148
|
+
<MenuItems
|
|
149
|
+
entries={entries}
|
|
150
|
+
onDone={() => setMenuOpen(false)}
|
|
151
|
+
/>
|
|
152
|
+
</Popover>
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Shown when content exists but none of it is in view. */
|
|
158
|
+
/** Centers the content at 100% zoom, like returning to the drawing where it was made. */
|
|
159
|
+
export function scrollToContent(editor: AnyEditor): Promise<void> {
|
|
160
|
+
const content = editor.elements.contentBounds();
|
|
161
|
+
|
|
162
|
+
if (content === undefined) {
|
|
163
|
+
return Promise.resolve();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const { zoom } = editor.viewport.get();
|
|
167
|
+
const visible = editor.viewport.visibleBounds();
|
|
168
|
+
const width = (visible.maxX - visible.minX) * zoom;
|
|
169
|
+
const height = (visible.maxY - visible.minY) * zoom;
|
|
170
|
+
return editor.viewport.set(
|
|
171
|
+
{
|
|
172
|
+
zoom: 1,
|
|
173
|
+
x: (content.minX + content.maxX) / 2 - width / 2,
|
|
174
|
+
y: (content.minY + content.maxY) / 2 - height / 2,
|
|
175
|
+
},
|
|
176
|
+
{ animate: { duration: 300 } },
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function ScrollToContent(): ReactNode {
|
|
181
|
+
const editor = useEditor();
|
|
182
|
+
const { t } = useUi();
|
|
183
|
+
const revision = useEditorValue((current) => current.document.revision, {
|
|
184
|
+
events: ['document.change', 'document.load', 'permissions.change', 'layer.change'],
|
|
185
|
+
});
|
|
186
|
+
const content = useMemo(
|
|
187
|
+
() => (revision >= 0 ? editor.elements.contentBounds() : undefined),
|
|
188
|
+
[editor, revision],
|
|
189
|
+
);
|
|
190
|
+
const lost = useEditorValue(
|
|
191
|
+
(current) => {
|
|
192
|
+
if (content === undefined) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const visible = current.viewport.visibleBounds();
|
|
197
|
+
return (
|
|
198
|
+
content.maxX < visible.minX ||
|
|
199
|
+
content.minX > visible.maxX ||
|
|
200
|
+
content.maxY < visible.minY ||
|
|
201
|
+
content.minY > visible.maxY
|
|
202
|
+
);
|
|
203
|
+
},
|
|
204
|
+
{ events: ['viewport.change'] },
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
if (!lost) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<button
|
|
213
|
+
type="button"
|
|
214
|
+
className="nx-pill"
|
|
215
|
+
onClick={() => void scrollToContent(editor)}
|
|
216
|
+
data-testid="scroll-to-content"
|
|
217
|
+
>
|
|
218
|
+
{t('scroll.toContent')}
|
|
219
|
+
</button>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function LaserButton(): ReactNode {
|
|
224
|
+
const editor = useEditor();
|
|
225
|
+
const active = useActiveTool();
|
|
226
|
+
const { t } = useUi();
|
|
227
|
+
const available = useEditorValue(
|
|
228
|
+
(current) => current.tools.has('core.laser') && current.tools.isAvailable('core.laser'),
|
|
229
|
+
{
|
|
230
|
+
events: ['mode.change', 'permissions.change', 'tool.change'],
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
if (!available) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return (
|
|
239
|
+
<IconButton
|
|
240
|
+
icon="laser"
|
|
241
|
+
label={t('laser.label')}
|
|
242
|
+
shortcut="K"
|
|
243
|
+
pressed={active === 'core.laser'}
|
|
244
|
+
onClick={() => editor.tools.activate(active === 'core.laser' ? 'core.select' : 'core.laser')}
|
|
245
|
+
testId="laser-button"
|
|
246
|
+
/>
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function ZenToggle(): ReactNode {
|
|
251
|
+
const { t } = useUi();
|
|
252
|
+
const presentation = usePresentation();
|
|
253
|
+
const zen = useCommand('core.zen.toggle');
|
|
254
|
+
return (
|
|
255
|
+
<IconButton
|
|
256
|
+
icon="focus"
|
|
257
|
+
label={presentation.zen ? t('zen.exit') : t('zen.enter')}
|
|
258
|
+
shortcut={zen.shortcuts}
|
|
259
|
+
pressed={presentation.zen}
|
|
260
|
+
onClick={() => zen.run()}
|
|
261
|
+
testId="zen-toggle"
|
|
262
|
+
/>
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function GridControls(): ReactNode {
|
|
267
|
+
const { t } = useUi();
|
|
268
|
+
const grid = useGrid();
|
|
269
|
+
const toggle = useCommand('core.grid.toggle');
|
|
270
|
+
return (
|
|
271
|
+
<IconButton
|
|
272
|
+
icon="grid"
|
|
273
|
+
label={t('grid.toggle')}
|
|
274
|
+
shortcut={toggle.shortcuts}
|
|
275
|
+
pressed={grid.visible}
|
|
276
|
+
disabled={!toggle.enabled}
|
|
277
|
+
onClick={() => toggle.run()}
|
|
278
|
+
testId="grid-toggle"
|
|
279
|
+
/>
|
|
280
|
+
);
|
|
281
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { useCanvasView, useSelection } from '@drawback/react';
|
|
2
|
+
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
|
3
|
+
import { useUi } from '../../stores/ui-context.js';
|
|
4
|
+
|
|
5
|
+
import { Dialog } from '../../components/primitives.js';
|
|
6
|
+
import { download } from '../../lib/download.js';
|
|
7
|
+
|
|
8
|
+
/** PNG/SVG export with a preview: format, selection only, background, grid, scale, padding. */
|
|
9
|
+
export function ExportDialog({ fileName = 'canvas' }: { readonly fileName?: string }): ReactNode {
|
|
10
|
+
const view = useCanvasView();
|
|
11
|
+
const selection = useSelection();
|
|
12
|
+
const { t, isOpen, setOpen } = useUi();
|
|
13
|
+
const open = isOpen('export');
|
|
14
|
+
const [format, setFormat] = useState<'png' | 'svg'>('png');
|
|
15
|
+
const [selectionOnly, setSelectionOnly] = useState(false);
|
|
16
|
+
const [background, setBackground] = useState(true);
|
|
17
|
+
const [grid, setGrid] = useState(false);
|
|
18
|
+
const [scale, setScale] = useState(1);
|
|
19
|
+
const [padding, setPadding] = useState(16);
|
|
20
|
+
const [preview, setPreview] = useState<{ url: string; warnings: number } | undefined>(undefined);
|
|
21
|
+
const onlySelected = selectionOnly && selection.length > 0;
|
|
22
|
+
const options = useMemo(
|
|
23
|
+
() => ({
|
|
24
|
+
bounds: onlySelected ? ('selection' as const) : ('content' as const),
|
|
25
|
+
padding,
|
|
26
|
+
grid,
|
|
27
|
+
...(background ? {} : { background: 'transparent' as const }),
|
|
28
|
+
}),
|
|
29
|
+
[onlySelected, padding, grid, background],
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!open || view === undefined) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let cancelled = false;
|
|
38
|
+
let url: string | undefined;
|
|
39
|
+
void view
|
|
40
|
+
.exportImage({ ...options, format: 'png', scale: 0.5 })
|
|
41
|
+
.then((result) => {
|
|
42
|
+
if (!cancelled && result.blob !== undefined) {
|
|
43
|
+
url = URL.createObjectURL(result.blob);
|
|
44
|
+
setPreview({ url, warnings: result.warnings.length });
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
.catch(() => setPreview(undefined));
|
|
48
|
+
return () => {
|
|
49
|
+
cancelled = true;
|
|
50
|
+
|
|
51
|
+
if (url !== undefined) {
|
|
52
|
+
URL.revokeObjectURL(url);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}, [open, view, options]);
|
|
56
|
+
|
|
57
|
+
const save = async (): Promise<void> => {
|
|
58
|
+
if (view === undefined) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const result = await view.exportImage({ ...options, format, scale });
|
|
63
|
+
|
|
64
|
+
if (format === 'svg') {
|
|
65
|
+
download(`${fileName}.svg`, result.svg ?? '', 'image/svg+xml');
|
|
66
|
+
} else if (result.blob !== undefined) {
|
|
67
|
+
download(`${fileName}.png`, result.blob);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<Dialog
|
|
73
|
+
open={open}
|
|
74
|
+
onClose={() => setOpen('export', false)}
|
|
75
|
+
title={t('export.title')}
|
|
76
|
+
testId="export-dialog"
|
|
77
|
+
wide
|
|
78
|
+
>
|
|
79
|
+
<div className="nx-export">
|
|
80
|
+
<div className="nx-export-preview">
|
|
81
|
+
{preview !== undefined && (
|
|
82
|
+
<img
|
|
83
|
+
src={preview.url}
|
|
84
|
+
alt=""
|
|
85
|
+
data-testid="export-preview"
|
|
86
|
+
/>
|
|
87
|
+
)}
|
|
88
|
+
</div>
|
|
89
|
+
<div className="nx-fields">
|
|
90
|
+
<label className="nx-field">
|
|
91
|
+
<span>{t('export.format')}</span>
|
|
92
|
+
<select
|
|
93
|
+
value={format}
|
|
94
|
+
data-testid="export-format"
|
|
95
|
+
onChange={(event) => setFormat(event.target.value === 'svg' ? 'svg' : 'png')}
|
|
96
|
+
>
|
|
97
|
+
<option value="png">PNG</option>
|
|
98
|
+
<option value="svg">SVG</option>
|
|
99
|
+
</select>
|
|
100
|
+
</label>
|
|
101
|
+
<label className="nx-check">
|
|
102
|
+
<input
|
|
103
|
+
type="checkbox"
|
|
104
|
+
checked={onlySelected}
|
|
105
|
+
disabled={selection.length === 0}
|
|
106
|
+
onChange={(event) => setSelectionOnly(event.target.checked)}
|
|
107
|
+
/>
|
|
108
|
+
{t('export.selectionOnly')}
|
|
109
|
+
</label>
|
|
110
|
+
<label className="nx-check">
|
|
111
|
+
<input
|
|
112
|
+
type="checkbox"
|
|
113
|
+
checked={background}
|
|
114
|
+
data-testid="export-background"
|
|
115
|
+
onChange={(event) => setBackground(event.target.checked)}
|
|
116
|
+
/>
|
|
117
|
+
{t('export.background')}
|
|
118
|
+
</label>
|
|
119
|
+
<label className="nx-check">
|
|
120
|
+
<input
|
|
121
|
+
type="checkbox"
|
|
122
|
+
checked={grid}
|
|
123
|
+
onChange={(event) => setGrid(event.target.checked)}
|
|
124
|
+
/>
|
|
125
|
+
{t('export.grid')}
|
|
126
|
+
</label>
|
|
127
|
+
<label className="nx-field">
|
|
128
|
+
<span>{t('export.scale')}</span>
|
|
129
|
+
<select
|
|
130
|
+
value={scale}
|
|
131
|
+
data-testid="export-scale"
|
|
132
|
+
onChange={(event) => setScale(Number(event.target.value))}
|
|
133
|
+
>
|
|
134
|
+
<option value={1}>1×</option>
|
|
135
|
+
<option value={2}>2×</option>
|
|
136
|
+
<option value={3}>3×</option>
|
|
137
|
+
</select>
|
|
138
|
+
</label>
|
|
139
|
+
<label className="nx-field">
|
|
140
|
+
<span>{t('export.padding')}</span>
|
|
141
|
+
<input
|
|
142
|
+
type="number"
|
|
143
|
+
min={0}
|
|
144
|
+
max={200}
|
|
145
|
+
value={padding}
|
|
146
|
+
onChange={(event) => setPadding(Math.max(0, Number(event.target.value) || 0))}
|
|
147
|
+
/>
|
|
148
|
+
</label>
|
|
149
|
+
{preview !== undefined && preview.warnings > 0 && (
|
|
150
|
+
<p className="nx-caption">{t('export.warnings', { count: preview.warnings })}</p>
|
|
151
|
+
)}
|
|
152
|
+
<button
|
|
153
|
+
type="button"
|
|
154
|
+
className="nx-primary"
|
|
155
|
+
data-testid="export-download"
|
|
156
|
+
onClick={() => void save()}
|
|
157
|
+
>
|
|
158
|
+
{t('export.download')}
|
|
159
|
+
</button>
|
|
160
|
+
</div>
|
|
161
|
+
</div>
|
|
162
|
+
</Dialog>
|
|
163
|
+
);
|
|
164
|
+
}
|