@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,216 @@
|
|
|
1
|
+
import { useActiveTool, useCommand, useEditor, useToolLocked, useTools } from '@drawback/react';
|
|
2
|
+
import { Fragment, useRef, useState, type ReactNode } from 'react';
|
|
3
|
+
import { useUi } from '../../stores/ui-context.js';
|
|
4
|
+
import { byOrder } from '../../lib/registry.js';
|
|
5
|
+
import {
|
|
6
|
+
IconButton,
|
|
7
|
+
Island,
|
|
8
|
+
MenuItems,
|
|
9
|
+
Popover,
|
|
10
|
+
cx,
|
|
11
|
+
handleRovingKeys,
|
|
12
|
+
type MenuEntry,
|
|
13
|
+
} from '../../components/primitives.js';
|
|
14
|
+
|
|
15
|
+
const TOOL_ORDER: Readonly<Record<string, number>> = {
|
|
16
|
+
'core.hand': 100,
|
|
17
|
+
'core.select': 200,
|
|
18
|
+
'core.rectangle': 300,
|
|
19
|
+
'core.diamond': 310,
|
|
20
|
+
'core.ellipse': 320,
|
|
21
|
+
'core.arrow': 400,
|
|
22
|
+
'core.line': 410,
|
|
23
|
+
'core.freedraw': 500,
|
|
24
|
+
'core.text': 510,
|
|
25
|
+
'core.image': 520,
|
|
26
|
+
'core.eraser': 600,
|
|
27
|
+
'core.frame': 690,
|
|
28
|
+
'core.laser': 700,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const GROUP_BREAKS = new Set([300, 400, 500, 600]);
|
|
32
|
+
|
|
33
|
+
/** Tools listed in the toolbar's "more tools" menu instead of as buttons (like Excalidraw). */
|
|
34
|
+
const OVERFLOW_TOOLS: ReadonlySet<string> = new Set(['core.image', 'core.frame', 'core.laser']);
|
|
35
|
+
|
|
36
|
+
type ToolInfo = ReturnType<typeof useTools>[number];
|
|
37
|
+
|
|
38
|
+
/** Toolbar button with a menu of the less used tools; pressed while one of them is active. */
|
|
39
|
+
function ToolOverflow({ tools }: { readonly tools: readonly ToolInfo[] }): ReactNode {
|
|
40
|
+
const editor = useEditor();
|
|
41
|
+
const active = useActiveTool();
|
|
42
|
+
const { t } = useUi();
|
|
43
|
+
const [open, setOpen] = useState(false);
|
|
44
|
+
const anchor = useRef<HTMLSpanElement>(null);
|
|
45
|
+
const entries: MenuEntry[] = tools.map((tool) => {
|
|
46
|
+
const translated = t(`tool.${tool.id}`);
|
|
47
|
+
return {
|
|
48
|
+
id: tool.id,
|
|
49
|
+
label: translated === `tool.${tool.id}` ? (tool.label ?? tool.id) : translated,
|
|
50
|
+
icon: tool.id,
|
|
51
|
+
shortcut: tool.shortcuts[0],
|
|
52
|
+
onSelect: () => editor.tools.activate(tool.id),
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
return (
|
|
56
|
+
<>
|
|
57
|
+
<span
|
|
58
|
+
ref={anchor}
|
|
59
|
+
className="nx-anchor"
|
|
60
|
+
>
|
|
61
|
+
<IconButton
|
|
62
|
+
icon="more"
|
|
63
|
+
label={t('toolbar.more')}
|
|
64
|
+
pressed={open || tools.some((tool) => tool.id === active)}
|
|
65
|
+
onClick={() => setOpen(!open)}
|
|
66
|
+
testId="tool-overflow"
|
|
67
|
+
className="nx-tool"
|
|
68
|
+
/>
|
|
69
|
+
</span>
|
|
70
|
+
<Popover
|
|
71
|
+
open={open}
|
|
72
|
+
onClose={() => setOpen(false)}
|
|
73
|
+
anchor={anchor}
|
|
74
|
+
placement="bottom-end"
|
|
75
|
+
label={t('toolbar.more')}
|
|
76
|
+
role="menu"
|
|
77
|
+
testId="tool-overflow-menu"
|
|
78
|
+
>
|
|
79
|
+
<MenuItems
|
|
80
|
+
entries={entries}
|
|
81
|
+
onDone={() => setOpen(false)}
|
|
82
|
+
/>
|
|
83
|
+
</Popover>
|
|
84
|
+
</>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ToolButtonProps {
|
|
89
|
+
readonly id: string;
|
|
90
|
+
/** Overrides the catalog label `tool.<id>`. */
|
|
91
|
+
readonly label?: string;
|
|
92
|
+
/** Used when the catalog has no `tool.<id>` entry (e.g. the tool definition's label). */
|
|
93
|
+
readonly fallbackLabel?: string;
|
|
94
|
+
readonly icon?: string;
|
|
95
|
+
/** Primary shortcut, shown as the hint on the button. */
|
|
96
|
+
readonly shortcut?: string;
|
|
97
|
+
/** All shortcuts for the tooltip ("R or 2"); defaults to `shortcut`. */
|
|
98
|
+
readonly shortcuts?: readonly string[];
|
|
99
|
+
readonly showShortcut?: boolean;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function ToolButton({
|
|
103
|
+
id,
|
|
104
|
+
label,
|
|
105
|
+
fallbackLabel,
|
|
106
|
+
icon,
|
|
107
|
+
shortcut,
|
|
108
|
+
shortcuts,
|
|
109
|
+
showShortcut = true,
|
|
110
|
+
}: ToolButtonProps): ReactNode {
|
|
111
|
+
const editor = useEditor();
|
|
112
|
+
const active = useActiveTool();
|
|
113
|
+
const { t } = useUi();
|
|
114
|
+
const translated = t(`tool.${id}`);
|
|
115
|
+
const name = label ?? (translated === `tool.${id}` ? (fallbackLabel ?? id) : translated);
|
|
116
|
+
return (
|
|
117
|
+
<IconButton
|
|
118
|
+
icon={icon ?? id}
|
|
119
|
+
label={name}
|
|
120
|
+
shortcut={shortcuts ?? shortcut}
|
|
121
|
+
pressed={active === id}
|
|
122
|
+
onClick={() => editor.tools.activate(id)}
|
|
123
|
+
testId={`tool-${id}`}
|
|
124
|
+
className="nx-tool"
|
|
125
|
+
>
|
|
126
|
+
{/* Shortcut hints are a deliberate choice (unlike Excalidraw); keep them. See CLAUDE.md. */}
|
|
127
|
+
{showShortcut && shortcut !== undefined && <span className="nx-tool-key">{shortcut}</span>}
|
|
128
|
+
</IconButton>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function ToolLockButton(): ReactNode {
|
|
133
|
+
const locked = useToolLocked();
|
|
134
|
+
const lock = useCommand('core.tool.toggleLock');
|
|
135
|
+
const { t } = useUi();
|
|
136
|
+
return (
|
|
137
|
+
<IconButton
|
|
138
|
+
icon={locked ? 'lock' : 'unlock'}
|
|
139
|
+
label={t('toolbar.lock')}
|
|
140
|
+
shortcut={lock.shortcuts}
|
|
141
|
+
pressed={locked}
|
|
142
|
+
onClick={() => lock.run()}
|
|
143
|
+
testId="tool-lock"
|
|
144
|
+
/>
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ToolbarProps {
|
|
149
|
+
readonly start?: ReactNode;
|
|
150
|
+
readonly end?: ReactNode;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Available tools in a fixed group order (unknown custom tools after the built-ins), plus
|
|
155
|
+
* registered toolbar items placed by `order`. Arrow keys move between buttons.
|
|
156
|
+
*/
|
|
157
|
+
export function Toolbar({ start, end }: ToolbarProps): ReactNode {
|
|
158
|
+
const tools = useTools();
|
|
159
|
+
const { t, registry, isHidden } = useUi();
|
|
160
|
+
const available = tools.filter((tool) => tool.available && !isHidden(`tool:${tool.id}`));
|
|
161
|
+
const visible = available.filter((tool) => !OVERFLOW_TOOLS.has(tool.id));
|
|
162
|
+
const overflow = available.filter((tool) => OVERFLOW_TOOLS.has(tool.id));
|
|
163
|
+
const entries = byOrder([
|
|
164
|
+
...visible.map((tool, index) => ({
|
|
165
|
+
id: tool.id,
|
|
166
|
+
order: TOOL_ORDER[tool.id] ?? 800 + index,
|
|
167
|
+
render: () => (
|
|
168
|
+
<ToolButton
|
|
169
|
+
id={tool.id}
|
|
170
|
+
{...(tool.label === undefined ? {} : { fallbackLabel: tool.label })}
|
|
171
|
+
{...(tool.shortcuts[0] === undefined
|
|
172
|
+
? {}
|
|
173
|
+
: { shortcut: tool.shortcuts[0], shortcuts: tool.shortcuts })}
|
|
174
|
+
/>
|
|
175
|
+
),
|
|
176
|
+
})),
|
|
177
|
+
...registry.toolbar.list().filter((item) => !isHidden(`toolbar:${item.id}`)),
|
|
178
|
+
]);
|
|
179
|
+
let previousOrder = 0;
|
|
180
|
+
return (
|
|
181
|
+
<Island
|
|
182
|
+
className="nx-toolbar"
|
|
183
|
+
label={t('toolbar.label')}
|
|
184
|
+
role="toolbar"
|
|
185
|
+
testId="toolbar"
|
|
186
|
+
>
|
|
187
|
+
<div
|
|
188
|
+
className="nx-toolbar-items"
|
|
189
|
+
onKeyDown={(event) => handleRovingKeys(event, 'horizontal')}
|
|
190
|
+
>
|
|
191
|
+
{start}
|
|
192
|
+
{!isHidden('toolLock') && <ToolLockButton />}
|
|
193
|
+
<div className="nx-separator nx-vertical" />
|
|
194
|
+
{entries.map((entry) => {
|
|
195
|
+
const breakBefore = [...GROUP_BREAKS].some(
|
|
196
|
+
(limit) => previousOrder < limit && entry.order >= limit,
|
|
197
|
+
);
|
|
198
|
+
previousOrder = entry.order;
|
|
199
|
+
return (
|
|
200
|
+
<Fragment key={entry.id}>
|
|
201
|
+
{breakBefore && <div className={cx('nx-separator', 'nx-vertical')} />}
|
|
202
|
+
{entry.render()}
|
|
203
|
+
</Fragment>
|
|
204
|
+
);
|
|
205
|
+
})}
|
|
206
|
+
{end}
|
|
207
|
+
{overflow.length > 0 && (
|
|
208
|
+
<>
|
|
209
|
+
<div className="nx-separator nx-vertical" />
|
|
210
|
+
<ToolOverflow tools={overflow} />
|
|
211
|
+
</>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
</Island>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useInsertionEffect, useRef, type RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A ref that always holds the latest `value`, for callbacks that outlive a render and may also
|
|
5
|
+
* run during one (where `useEffectEvent` would throw). Updated in an insertion effect, so it is
|
|
6
|
+
* current before layout effects and event handlers run, and never written during render.
|
|
7
|
+
*/
|
|
8
|
+
export function useLatest<Value>(value: Value): RefObject<Value> {
|
|
9
|
+
const ref = useRef(value);
|
|
10
|
+
|
|
11
|
+
useInsertionEffect(() => {
|
|
12
|
+
ref.current = value;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return ref;
|
|
16
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DefaultEditor,
|
|
3
|
+
EditorRoot,
|
|
4
|
+
type DefaultEditorComponents,
|
|
5
|
+
type DefaultEditorProps,
|
|
6
|
+
type DefaultEditorSlots,
|
|
7
|
+
} from './app/default-editor.js';
|
|
8
|
+
export {
|
|
9
|
+
UiProvider,
|
|
10
|
+
useResolvedTheme,
|
|
11
|
+
useT,
|
|
12
|
+
useUi,
|
|
13
|
+
type ContextMenuRequest,
|
|
14
|
+
type PanelId,
|
|
15
|
+
type ResolvedTheme,
|
|
16
|
+
type ThemeSetting,
|
|
17
|
+
type UiContextValue,
|
|
18
|
+
type UiProviderProps,
|
|
19
|
+
} from './stores/ui-context.js';
|
|
20
|
+
export {
|
|
21
|
+
LANGUAGES,
|
|
22
|
+
builtinMessages,
|
|
23
|
+
createTranslator,
|
|
24
|
+
en,
|
|
25
|
+
loadMessages,
|
|
26
|
+
needsMessages,
|
|
27
|
+
type LanguageInfo,
|
|
28
|
+
type MessageKey,
|
|
29
|
+
type Messages,
|
|
30
|
+
type Translate,
|
|
31
|
+
type TranslateValues,
|
|
32
|
+
} from './lib/i18n.js';
|
|
33
|
+
export {
|
|
34
|
+
byOrder,
|
|
35
|
+
createUiRegistry,
|
|
36
|
+
type ContextMenuContext,
|
|
37
|
+
type ContextMenuItem,
|
|
38
|
+
type IconComponent,
|
|
39
|
+
type PropertyEditorProps,
|
|
40
|
+
type ToolbarItem,
|
|
41
|
+
type UiRegistry,
|
|
42
|
+
} from './lib/registry.js';
|
|
43
|
+
export { Icon, builtinIcons, type IconProps } from './components/icons.js';
|
|
44
|
+
export {
|
|
45
|
+
Toolbar,
|
|
46
|
+
ToolButton,
|
|
47
|
+
ToolLockButton,
|
|
48
|
+
type ToolButtonProps,
|
|
49
|
+
type ToolbarProps,
|
|
50
|
+
} from './features/toolbar/toolbar.js';
|
|
51
|
+
export {
|
|
52
|
+
GridControls,
|
|
53
|
+
LaserButton,
|
|
54
|
+
ScrollToContent,
|
|
55
|
+
UndoRedo,
|
|
56
|
+
ZenToggle,
|
|
57
|
+
ZoomControls,
|
|
58
|
+
type ZoomControlsProps,
|
|
59
|
+
} from './features/controls/controls.js';
|
|
60
|
+
export { PropertiesPanel } from './features/properties/properties.js';
|
|
61
|
+
export { CanvasPropertiesPanel } from './features/main-menu/canvas-properties.js';
|
|
62
|
+
export { LinkDialog, LinkEditor } from './features/links/links.js';
|
|
63
|
+
export {
|
|
64
|
+
CANVAS_PROPERTIES_COMMAND,
|
|
65
|
+
COPY_AS_PNG_COMMAND,
|
|
66
|
+
COPY_AS_SVG_COMMAND,
|
|
67
|
+
COPY_ELEMENT_LINK_COMMAND,
|
|
68
|
+
LINK_COMMAND,
|
|
69
|
+
MINIMAP_COMMAND,
|
|
70
|
+
} from './lib/commands.js';
|
|
71
|
+
export { LayersPanel, type LayersPanelProps } from './features/layers/layers.js';
|
|
72
|
+
export {
|
|
73
|
+
buildLayerRows,
|
|
74
|
+
editorTreeSource,
|
|
75
|
+
visibleRange,
|
|
76
|
+
type BuildOptions,
|
|
77
|
+
type ElementRow,
|
|
78
|
+
type GroupRow,
|
|
79
|
+
type LayerRow,
|
|
80
|
+
type LayerTreeSource,
|
|
81
|
+
type LayerTreeSourceNode,
|
|
82
|
+
type TreeRow,
|
|
83
|
+
} from './features/layers/layers-tree.js';
|
|
84
|
+
export { CanvasCommands } from './features/canvas-commands/canvas-commands.js';
|
|
85
|
+
export {
|
|
86
|
+
ContextMenu,
|
|
87
|
+
builtinContextMenuItems,
|
|
88
|
+
useContextMenuForCanvas,
|
|
89
|
+
type BuiltinContextMenuState,
|
|
90
|
+
} from './features/context-menu/context-menu.js';
|
|
91
|
+
export { ExportDialog } from './features/export/export-dialog.js';
|
|
92
|
+
export { MainMenu, openFile, type MainMenuProps } from './features/main-menu/main-menu.js';
|
|
93
|
+
export { ShortcutsDialog } from './features/shortcuts/shortcuts-dialog.js';
|
|
94
|
+
export { Minimap, type MinimapProps } from './features/minimap/minimap.js';
|
|
95
|
+
export {
|
|
96
|
+
ColorField,
|
|
97
|
+
ColorPicker,
|
|
98
|
+
Swatch,
|
|
99
|
+
type ColorFieldProps,
|
|
100
|
+
type ColorPickerProps,
|
|
101
|
+
} from './components/color-picker/color-picker.js';
|
|
102
|
+
export {
|
|
103
|
+
FontFamilyField,
|
|
104
|
+
FontPicker,
|
|
105
|
+
QUICK_FONTS,
|
|
106
|
+
fontRoleLabel,
|
|
107
|
+
type FontFamilyFieldProps,
|
|
108
|
+
type FontPickerProps,
|
|
109
|
+
} from './components/font-picker/font-picker.js';
|
|
110
|
+
export {
|
|
111
|
+
COLOR_GRID,
|
|
112
|
+
COLOR_SHADES,
|
|
113
|
+
DEFAULT_SHADE,
|
|
114
|
+
FILL_COLORS,
|
|
115
|
+
STROKE_COLORS,
|
|
116
|
+
familyColor,
|
|
117
|
+
familyOf,
|
|
118
|
+
normalizeHex,
|
|
119
|
+
type ColorFamily,
|
|
120
|
+
type ColorKind,
|
|
121
|
+
type ShadedFamily,
|
|
122
|
+
} from './components/color-picker/colors.js';
|
|
123
|
+
export {
|
|
124
|
+
Choice,
|
|
125
|
+
Dialog,
|
|
126
|
+
IconButton,
|
|
127
|
+
Island,
|
|
128
|
+
MenuItems,
|
|
129
|
+
Popover,
|
|
130
|
+
Section,
|
|
131
|
+
formatShortcut,
|
|
132
|
+
type ChoiceOption,
|
|
133
|
+
type DialogProps,
|
|
134
|
+
type IconButtonProps,
|
|
135
|
+
type MenuEntry,
|
|
136
|
+
type PopoverProps,
|
|
137
|
+
} from './components/primitives.js';
|
|
138
|
+
export { UiPlugins, type UiPluginFacet } from './lib/plugins.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Ids of the commands the default UI registers; keymaps and context menus refer to them.
|
|
2
|
+
|
|
3
|
+
/** Edits the link of the single selected element (Mod+K). */
|
|
4
|
+
export const LINK_COMMAND = 'ui.link.edit';
|
|
5
|
+
/** Copies the selection (or the whole drawing) to the system clipboard as a PNG image. */
|
|
6
|
+
export const COPY_AS_PNG_COMMAND = 'ui.copyAsPng';
|
|
7
|
+
/** Copies the selection (or the whole drawing) to the system clipboard as SVG markup. */
|
|
8
|
+
export const COPY_AS_SVG_COMMAND = 'ui.copyAsSvg';
|
|
9
|
+
/** Copies a URL that points at the single selected element (`links.elementUrl`). */
|
|
10
|
+
export const COPY_ELEMENT_LINK_COMMAND = 'ui.copyElementLink';
|
|
11
|
+
/** Shows or hides the minimap. */
|
|
12
|
+
export const MINIMAP_COMMAND = 'ui.minimap.toggle';
|
|
13
|
+
/** Opens or closes the main menu, which holds the canvas settings. */
|
|
14
|
+
export const CANVAS_PROPERTIES_COMMAND = 'ui.canvasProperties.toggle';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Saves `data` as a file named `name` through a temporary object URL. */
|
|
2
|
+
export function download(name: string, data: Blob | string, type = 'application/json'): void {
|
|
3
|
+
const blob = typeof data === 'string' ? new Blob([data], { type }) : data;
|
|
4
|
+
const url = URL.createObjectURL(blob);
|
|
5
|
+
const link = Object.assign(document.createElement('a'), { href: url, download: name });
|
|
6
|
+
link.click();
|
|
7
|
+
setTimeout(() => URL.revokeObjectURL(url), 1_000);
|
|
8
|
+
}
|