@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.
Files changed (38) hide show
  1. package/README.md +22 -2
  2. package/dist/i18n-BZnCrD-C.d.mts +265 -0
  3. package/dist/index.d.mts +588 -0
  4. package/dist/index.mjs +4821 -0
  5. package/dist/locales/de.d.mts +3 -0
  6. package/dist/locales/de.mjs +231 -0
  7. package/package.json +69 -4
  8. package/src/app/default-editor.tsx +371 -0
  9. package/src/components/color-picker/color-picker.tsx +341 -0
  10. package/src/components/color-picker/colors.ts +135 -0
  11. package/src/components/font-picker/font-picker.tsx +298 -0
  12. package/src/components/icons.tsx +222 -0
  13. package/src/components/primitives.tsx +565 -0
  14. package/src/components/style-icons.tsx +172 -0
  15. package/src/features/canvas-commands/canvas-commands.tsx +173 -0
  16. package/src/features/context-menu/context-menu.tsx +354 -0
  17. package/src/features/controls/controls.tsx +281 -0
  18. package/src/features/export/export-dialog.tsx +164 -0
  19. package/src/features/layers/layers-tree.ts +335 -0
  20. package/src/features/layers/layers.tsx +858 -0
  21. package/src/features/links/links.tsx +268 -0
  22. package/src/features/main-menu/canvas-properties.tsx +121 -0
  23. package/src/features/main-menu/main-menu.tsx +215 -0
  24. package/src/features/minimap/minimap.tsx +69 -0
  25. package/src/features/properties/linear-properties.tsx +169 -0
  26. package/src/features/properties/properties.tsx +573 -0
  27. package/src/features/shortcuts/shortcuts-dialog.tsx +120 -0
  28. package/src/features/toolbar/toolbar.tsx +216 -0
  29. package/src/hooks/use-latest.ts +16 -0
  30. package/src/index.ts +138 -0
  31. package/src/lib/commands.ts +14 -0
  32. package/src/lib/download.ts +8 -0
  33. package/src/lib/i18n.ts +350 -0
  34. package/src/lib/locales/de.ts +231 -0
  35. package/src/lib/plugins.tsx +92 -0
  36. package/src/lib/registry.ts +140 -0
  37. package/src/stores/ui-context.tsx +253 -0
  38. package/src/styles.css +1366 -0
@@ -0,0 +1,298 @@
1
+ import type { AnyEditor, FontCategory, FontDescriptor, FontId } from '@drawback/core';
2
+ import { useEditor, useEditorValue } from '@drawback/react';
3
+ import { useCallback, useEffect, useId, useMemo, useRef, useState, type ReactNode } from 'react';
4
+ import { useUi } from '../../stores/ui-context.js';
5
+ import { Icon } from '../icons.js';
6
+ import { Choice, Popover, cx } from '../primitives.js';
7
+
8
+ /** The three quick picks next to the font picker, like Excalidraw's. */
9
+ export const QUICK_FONTS: readonly { readonly id: FontId; readonly icon: string }[] = [
10
+ { id: 'core.hand', icon: 'fontHand' },
11
+ { id: 'core.sans', icon: 'fontSans' },
12
+ { id: 'core.mono', icon: 'fontMono' },
13
+ ];
14
+
15
+ const CATEGORY_ICONS: Readonly<Record<FontCategory, string>> = {
16
+ sans: 'fontSans',
17
+ serif: 'fontSerif',
18
+ mono: 'fontMono',
19
+ hand: 'fontHand',
20
+ display: 'fontDisplay',
21
+ };
22
+
23
+ type Translate = ReturnType<typeof useUi>['t'];
24
+
25
+ /** Role name of a built-in id ("Hand-drawn"), else the font's own name. */
26
+ export function fontRoleLabel(editor: AnyEditor, t: Translate, id: FontId): string {
27
+ const translated = t(`font.${id}`);
28
+ return translated === `font.${id}` ? fontName(editor.fonts.get(id), t, id) : translated;
29
+ }
30
+
31
+ /** The font's own name ("Inter"), falling back to the role name for system stacks. */
32
+ function fontName(font: FontDescriptor | undefined, t: Translate, id: FontId): string {
33
+ if (font?.label !== undefined) {
34
+ return font.label;
35
+ }
36
+
37
+ const translated = t(`font.${id}`);
38
+ return translated === `font.${id}` ? (font?.family ?? id) : translated;
39
+ }
40
+
41
+ function previewFont(editor: AnyEditor, id: FontId): string {
42
+ return editor.fonts.css({ family: id, size: 15, weight: 400, style: 'normal' });
43
+ }
44
+
45
+ interface PickerEntry {
46
+ readonly id: FontId;
47
+ readonly label: string;
48
+ readonly icon: string;
49
+ readonly section: 'scene' | 'available';
50
+ }
51
+
52
+ export interface FontPickerProps {
53
+ /** Current font; `undefined` when mixed. */
54
+ readonly value: FontId | undefined;
55
+ readonly onChange: (id: FontId) => void;
56
+ }
57
+
58
+ /**
59
+ * Searchable list of every registered font, with the fonts the document uses first. Each name
60
+ * is drawn in its own font. Arrow keys move, Enter picks.
61
+ */
62
+ export function FontPicker({ value, onChange }: FontPickerProps): ReactNode {
63
+ const editor = useEditor();
64
+ const { t } = useUi();
65
+ const listId = useId();
66
+ const [query, setQuery] = useState('');
67
+ // The row the arrow keys moved to; `undefined` starts on the current font (or the first match).
68
+ const [activeId, setActiveId] = useState<FontId | undefined>(undefined);
69
+ const list = useRef<HTMLDivElement>(null);
70
+ const fonts = useEditorValue((current) => current.fonts.list(), { events: ['font.status'] });
71
+ const used = useEditorValue((current) => current.fonts.used(), {
72
+ events: ['document.change', 'document.load'],
73
+ equals: (left, right) =>
74
+ left.length === right.length && left.every((id, index) => id === right[index]),
75
+ });
76
+
77
+ // Load every listed font so the names render in their own faces.
78
+ useEffect(() => {
79
+ void editor.fonts.load(fonts.map((font) => font.id));
80
+ }, [editor, fonts]);
81
+
82
+ const entries = useMemo((): readonly PickerEntry[] => {
83
+ const needle = query.trim().toLocaleLowerCase();
84
+ const inScene = new Set(used);
85
+ const all = fonts.map((font): PickerEntry => ({
86
+ id: font.id,
87
+ label: fontName(font, t, font.id),
88
+ icon: CATEGORY_ICONS[font.category ?? 'sans'],
89
+ section: inScene.has(font.id) ? 'scene' : 'available',
90
+ }));
91
+ const matches = all.filter(
92
+ (entry) => needle === '' || entry.label.toLocaleLowerCase().includes(needle),
93
+ );
94
+ const byLabel = (left: PickerEntry, right: PickerEntry) =>
95
+ left.label.localeCompare(right.label);
96
+ return [
97
+ ...matches.filter((entry) => entry.section === 'scene').sort(byLabel),
98
+ ...matches.filter((entry) => entry.section === 'available').sort(byLabel),
99
+ ];
100
+ }, [fonts, used, query, t]);
101
+
102
+ const moved = entries.findIndex((entry) => entry.id === activeId);
103
+ const current = entries.findIndex((entry) => entry.id === value);
104
+ const active = moved >= 0 ? moved : query === '' && current >= 0 ? current : 0;
105
+ const activate = (index: number): void => {
106
+ setActiveId(entries[index]?.id);
107
+ list.current
108
+ ?.querySelector<HTMLElement>(`[data-index="${index}"]`)
109
+ ?.scrollIntoView({ block: 'nearest' });
110
+ };
111
+ // Brings the current font into view when the list mounts.
112
+ const revealCurrent = useCallback((node: HTMLDivElement | null) => {
113
+ node?.scrollIntoView({ block: 'nearest' });
114
+ }, []);
115
+
116
+ const optionId = (index: number) => `${listId}-${index}`;
117
+ const sections = [
118
+ { id: 'scene', title: t('font.inScene') },
119
+ { id: 'available', title: t('font.available') },
120
+ ] as const;
121
+
122
+ return (
123
+ <div
124
+ className="nx-font-picker"
125
+ data-testid="font-picker-panel"
126
+ >
127
+ <label className="nx-font-search">
128
+ <Icon name="search" />
129
+ <input
130
+ type="search"
131
+ value={query}
132
+ placeholder={t('font.search')}
133
+ aria-label={t('font.search')}
134
+ role="combobox"
135
+ aria-expanded
136
+ aria-controls={listId}
137
+ aria-activedescendant={entries.length === 0 ? undefined : optionId(active)}
138
+ data-autofocus=""
139
+ data-testid="font-search"
140
+ onChange={(event) => {
141
+ setQuery(event.target.value);
142
+ setActiveId(undefined);
143
+ }}
144
+ onKeyDown={(event) => {
145
+ if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
146
+ event.preventDefault();
147
+ const step = event.key === 'ArrowDown' ? 1 : -1;
148
+
149
+ if (entries.length > 0) {
150
+ activate((active + step + entries.length) % entries.length);
151
+ }
152
+ } else if (event.key === 'Enter') {
153
+ const entry = entries[active];
154
+
155
+ if (entry !== undefined) {
156
+ event.preventDefault();
157
+ onChange(entry.id);
158
+ }
159
+ }
160
+ }}
161
+ />
162
+ </label>
163
+ <div
164
+ ref={list}
165
+ id={listId}
166
+ className="nx-font-list"
167
+ role="listbox"
168
+ aria-label={t('properties.fontFamily')}
169
+ >
170
+ {entries.length === 0 && <p className="nx-font-empty">{t('font.none')}</p>}
171
+ {sections.map((section) => {
172
+ const items = entries
173
+ .map((entry, index) => ({ entry, index }))
174
+ .filter(({ entry }) => entry.section === section.id);
175
+
176
+ if (items.length === 0) {
177
+ return null;
178
+ }
179
+
180
+ return (
181
+ <div
182
+ key={section.id}
183
+ role="group"
184
+ aria-label={section.title}
185
+ >
186
+ <div
187
+ className="nx-font-section"
188
+ aria-hidden
189
+ >
190
+ {section.title}
191
+ </div>
192
+ {items.map(({ entry, index }) => (
193
+ <div
194
+ key={entry.id}
195
+ id={optionId(index)}
196
+ role="option"
197
+ aria-selected={entry.id === value}
198
+ className={cx(
199
+ 'nx-font-option',
200
+ index === active && 'nx-active',
201
+ entry.id === value && 'nx-selected',
202
+ )}
203
+ data-index={index}
204
+ data-testid={`font-option-${entry.id}`}
205
+ ref={entry.id === value ? revealCurrent : undefined}
206
+ onPointerMove={() => {
207
+ if (index !== active) {
208
+ setActiveId(entry.id);
209
+ }
210
+ }}
211
+ onPointerDown={(event) => event.preventDefault()}
212
+ onClick={() => onChange(entry.id)}
213
+ >
214
+ <Icon name={entry.icon} />
215
+ <span style={{ font: previewFont(editor, entry.id) }}>{entry.label}</span>
216
+ </div>
217
+ ))}
218
+ </div>
219
+ );
220
+ })}
221
+ </div>
222
+ </div>
223
+ );
224
+ }
225
+
226
+ export interface FontFamilyFieldProps {
227
+ readonly value: FontId | undefined;
228
+ readonly onChange: (id: FontId) => void;
229
+ readonly testId?: string;
230
+ }
231
+
232
+ /**
233
+ * Quick picks for the hand-drawn, normal and code fonts, then a button that opens the
234
+ * {@link FontPicker}. The button is highlighted while any other font is set.
235
+ */
236
+ export function FontFamilyField({ value, onChange, testId }: FontFamilyFieldProps): ReactNode {
237
+ const editor = useEditor();
238
+ const { t } = useUi();
239
+ const [open, setOpen] = useState(false);
240
+ const anchor = useRef<HTMLButtonElement>(null);
241
+ const quick = QUICK_FONTS.some((font) => font.id === value);
242
+ const other = value !== undefined && !quick;
243
+ const title =
244
+ value === undefined || quick
245
+ ? t('font.more')
246
+ : t('font.current', { name: fontName(editor.fonts.get(value), t, value) });
247
+ return (
248
+ <div
249
+ className="nx-color-field"
250
+ data-testid={testId}
251
+ >
252
+ <Choice
253
+ label={t('properties.fontFamily')}
254
+ value={value}
255
+ options={QUICK_FONTS.map((font) => ({
256
+ value: font.id,
257
+ label: fontRoleLabel(editor, t, font.id),
258
+ icon: font.icon,
259
+ }))}
260
+ onChange={onChange}
261
+ />
262
+ <span
263
+ className="nx-color-divider"
264
+ aria-hidden
265
+ />
266
+ <button
267
+ ref={anchor}
268
+ type="button"
269
+ className="nx-choice-option nx-choice-square"
270
+ aria-label={title}
271
+ title={title}
272
+ aria-haspopup="dialog"
273
+ aria-expanded={open}
274
+ aria-pressed={other}
275
+ data-testid="font-picker-toggle"
276
+ onClick={() => setOpen(!open)}
277
+ >
278
+ <Icon name="fontPicker" />
279
+ </button>
280
+ <Popover
281
+ open={open}
282
+ onClose={() => setOpen(false)}
283
+ anchor={anchor}
284
+ placement="right-start"
285
+ label={t('properties.fontFamily')}
286
+ testId="font-picker"
287
+ >
288
+ <FontPicker
289
+ value={value}
290
+ onChange={(id) => {
291
+ onChange(id);
292
+ setOpen(false);
293
+ }}
294
+ />
295
+ </Popover>
296
+ </div>
297
+ );
298
+ }
@@ -0,0 +1,222 @@
1
+ import {
2
+ ALargeSmall,
3
+ AlignCenterHorizontal,
4
+ AlignCenterVertical,
5
+ AlignEndHorizontal,
6
+ AlignEndVertical,
7
+ AlignHorizontalDistributeCenter,
8
+ AlignStartHorizontal,
9
+ AlignStartVertical,
10
+ AlignVerticalDistributeCenter,
11
+ ArrowDown,
12
+ ArrowDownToLine,
13
+ ArrowLeftRight,
14
+ ArrowUp,
15
+ ArrowUpToLine,
16
+ ArrowUpDown,
17
+ Check,
18
+ ChevronDown,
19
+ ChevronRight,
20
+ Circle,
21
+ ClipboardPaste,
22
+ Copy,
23
+ Crop,
24
+ Diamond,
25
+ Download,
26
+ Ellipsis,
27
+ Eraser,
28
+ Eye,
29
+ EyeOff,
30
+ Focus,
31
+ FolderOpen,
32
+ Ban,
33
+ CircleQuestionMark,
34
+ CodeXml,
35
+ Frame,
36
+ Heading,
37
+ Grid3x3,
38
+ Grip,
39
+ Hexagon,
40
+ Group,
41
+ Hand,
42
+ Image,
43
+ Keyboard,
44
+ Laser,
45
+ Layers,
46
+ Link,
47
+ Lock,
48
+ LockOpen,
49
+ Magnet,
50
+ Map as MapIcon,
51
+ MapPin,
52
+ Maximize,
53
+ Menu,
54
+ Minus,
55
+ Monitor,
56
+ Moon,
57
+ MousePointer2,
58
+ MoveRight,
59
+ Pencil,
60
+ Pipette,
61
+ Plus,
62
+ Redo2,
63
+ RotateCcw,
64
+ Save,
65
+ Scissors,
66
+ Search,
67
+ ScanSearch,
68
+ SearchX,
69
+ Shapes,
70
+ SlidersHorizontal,
71
+ Sparkle,
72
+ Square,
73
+ Sun,
74
+ TextAlignCenter,
75
+ TextAlignEnd,
76
+ TextAlignStart,
77
+ Trash,
78
+ Type,
79
+ Undo2,
80
+ Ungroup,
81
+ X,
82
+ ZoomIn,
83
+ ZoomOut,
84
+ } from 'lucide-react';
85
+ import type { ReactNode } from 'react';
86
+ import { useUi } from '../stores/ui-context.js';
87
+ import type { IconComponent } from '../lib/registry.js';
88
+ import { styleIcons } from './style-icons.js';
89
+
90
+ /** Built-in icon ids mapped to lucide components; hosts override per id through the registry. */
91
+ /** Lucide's hexagon has a vertex on top; turned a quarter it has a flat top. */
92
+ const HexagonFlat: IconComponent = (props) => (
93
+ <Hexagon
94
+ {...props}
95
+ style={{ transform: 'rotate(90deg)' }}
96
+ />
97
+ );
98
+
99
+ export const builtinIcons: Readonly<Record<string, IconComponent>> = {
100
+ 'core.select': MousePointer2,
101
+ 'core.hand': Hand,
102
+ 'core.rectangle': Square,
103
+ 'core.diamond': Diamond,
104
+ 'core.ellipse': Circle,
105
+ 'core.arrow': MoveRight,
106
+ 'core.line': Minus,
107
+ 'core.freedraw': Pencil,
108
+ 'core.text': Type,
109
+ 'core.image': Image,
110
+ 'core.eraser': Eraser,
111
+ 'core.laser': Laser,
112
+ rectangle: Square,
113
+ diamond: Diamond,
114
+ ellipse: Circle,
115
+ arrow: MoveRight,
116
+ line: Minus,
117
+ freedraw: Pencil,
118
+ text: Type,
119
+ image: Image,
120
+ element: Shapes,
121
+ unknown: Sparkle,
122
+ pin: MapPin,
123
+ lock: Lock,
124
+ link: Link,
125
+ edit: Pencil,
126
+ pipette: Pipette,
127
+ unlock: LockOpen,
128
+ undo: Undo2,
129
+ redo: Redo2,
130
+ zoomIn: ZoomIn,
131
+ zoomOut: ZoomOut,
132
+ fit: Maximize,
133
+ focus: Focus,
134
+ menu: Menu,
135
+ eye: Eye,
136
+ eyeOff: EyeOff,
137
+ expand: ChevronRight,
138
+ collapse: ChevronDown,
139
+ layers: Layers,
140
+ add: Plus,
141
+ delete: Trash,
142
+ copy: Copy,
143
+ cut: Scissors,
144
+ paste: ClipboardPaste,
145
+ group: Group,
146
+ ungroup: Ungroup,
147
+ flipHorizontal: ArrowLeftRight,
148
+ flipVertical: ArrowUpDown,
149
+ bringToFront: ArrowUpToLine,
150
+ bringForward: ArrowUp,
151
+ sendBackward: ArrowDown,
152
+ sendToBack: ArrowDownToLine,
153
+ alignLeft: AlignStartVertical,
154
+ alignCenter: AlignCenterVertical,
155
+ alignRight: AlignEndVertical,
156
+ alignTop: AlignStartHorizontal,
157
+ alignMiddle: AlignCenterHorizontal,
158
+ alignBottom: AlignEndHorizontal,
159
+ distributeHorizontal: AlignHorizontalDistributeCenter,
160
+ distributeVertical: AlignVerticalDistributeCenter,
161
+ crop: Crop,
162
+ download: Download,
163
+ open: FolderOpen,
164
+ save: Save,
165
+ keyboard: Keyboard,
166
+ help: CircleQuestionMark,
167
+ light: Sun,
168
+ dark: Moon,
169
+ system: Monitor,
170
+ grid: Grid3x3,
171
+ gridNone: Ban,
172
+ gridSquare: Grid3x3,
173
+ gridDot: Grip,
174
+ gridHexPointy: Hexagon,
175
+ gridHexFlat: HexagonFlat,
176
+ magnet: Magnet,
177
+ close: X,
178
+ search: Search,
179
+ reset: RotateCcw,
180
+ settings: SlidersHorizontal,
181
+ more: Ellipsis,
182
+ check: Check,
183
+ textLeft: TextAlignStart,
184
+ textCenter: TextAlignCenter,
185
+ textRight: TextAlignEnd,
186
+ 'core.frame': Frame,
187
+ frame: Frame,
188
+ fontHand: Pencil,
189
+ fontMono: CodeXml,
190
+ fontDisplay: Heading,
191
+ fontPicker: ALargeSmall,
192
+ laser: Laser,
193
+ map: MapIcon,
194
+ minus: Minus,
195
+ zoomFit: ScanSearch,
196
+ zoomReset: SearchX,
197
+ ...styleIcons,
198
+ };
199
+
200
+ export interface IconProps {
201
+ readonly name: string;
202
+ /** Pixel size (default 16). */
203
+ readonly size?: number;
204
+ /**
205
+ * Stroke width in screen pixels (default 1), whatever the size. Lucide measures strokes on
206
+ * its 24-unit grid, where 1 at 16px would draw 0.67px lines that blur into grey.
207
+ */
208
+ readonly strokeWidth?: number;
209
+ }
210
+
211
+ /** Renders an icon by id: registry override, then built-in, then a generic shape. */
212
+ export function Icon({ name, size = 16, strokeWidth = 1 }: IconProps): ReactNode {
213
+ const { registry } = useUi();
214
+ const Component = registry.icons.get(name) ?? builtinIcons[name] ?? Shapes;
215
+ return (
216
+ <Component
217
+ size={size}
218
+ strokeWidth={(strokeWidth * 24) / size}
219
+ aria-hidden
220
+ />
221
+ );
222
+ }