@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,140 @@
|
|
|
1
|
+
import type { AnyEditor, ElementId, ElementRecord, Point } from '@drawback/core';
|
|
2
|
+
import type { ComponentType, ReactNode } from 'react';
|
|
3
|
+
import type { Translate } from './i18n.js';
|
|
4
|
+
|
|
5
|
+
export interface ToolbarItem {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
/** Position among tools; built-ins use 100–1000 in steps of 100. */
|
|
8
|
+
readonly order: number;
|
|
9
|
+
render(): ReactNode;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface PropertyEditorProps {
|
|
13
|
+
readonly editor: AnyEditor;
|
|
14
|
+
/** Selected elements of the editor's type. */
|
|
15
|
+
readonly elements: readonly ElementRecord[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ContextMenuContext {
|
|
19
|
+
readonly editor: AnyEditor;
|
|
20
|
+
/** Element under the pointer, if any (already selected by the menu). */
|
|
21
|
+
readonly element?: ElementRecord;
|
|
22
|
+
readonly selection: readonly ElementId[];
|
|
23
|
+
readonly world: Point;
|
|
24
|
+
readonly t: Translate;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ContextMenuItem {
|
|
28
|
+
readonly id: string;
|
|
29
|
+
/** Lower numbers come first; built-ins use 100–1000. Items with equal `group` are separated from others. */
|
|
30
|
+
readonly order?: number;
|
|
31
|
+
readonly group?: string;
|
|
32
|
+
readonly label: string | ((context: ContextMenuContext) => string);
|
|
33
|
+
readonly shortcut?: string;
|
|
34
|
+
readonly danger?: boolean;
|
|
35
|
+
when?(context: ContextMenuContext): boolean;
|
|
36
|
+
enabled?(context: ContextMenuContext): boolean;
|
|
37
|
+
/** Makes the item a toggle: rendered as a checkbox item with a check mark while true. */
|
|
38
|
+
checked?(context: ContextMenuContext): boolean;
|
|
39
|
+
run(context: ContextMenuContext): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type IconComponent = ComponentType<{
|
|
43
|
+
readonly size?: number;
|
|
44
|
+
/** Lucide-style stroke width on a 24-unit grid (1px on screen); custom icons may ignore it. */
|
|
45
|
+
readonly strokeWidth?: number;
|
|
46
|
+
readonly 'aria-hidden'?: boolean;
|
|
47
|
+
}>;
|
|
48
|
+
|
|
49
|
+
interface Collection<Value> {
|
|
50
|
+
register(value: Value): () => void;
|
|
51
|
+
list(): readonly Value[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface UiRegistry {
|
|
55
|
+
readonly toolbar: Collection<ToolbarItem>;
|
|
56
|
+
readonly contextMenu: Collection<ContextMenuItem>;
|
|
57
|
+
readonly propertyEditors: {
|
|
58
|
+
register(type: string, component: ComponentType<PropertyEditorProps>): () => void;
|
|
59
|
+
get(type: string): ComponentType<PropertyEditorProps> | undefined;
|
|
60
|
+
types(): readonly string[];
|
|
61
|
+
};
|
|
62
|
+
readonly icons: {
|
|
63
|
+
register(id: string, component: IconComponent): () => void;
|
|
64
|
+
get(id: string): IconComponent | undefined;
|
|
65
|
+
};
|
|
66
|
+
/** Bumps on every registration change. */
|
|
67
|
+
version(): number;
|
|
68
|
+
subscribe(listener: () => void): () => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Per-editor registrations for toolbar items, context menu items, property editors and icons. */
|
|
72
|
+
export function createUiRegistry(): UiRegistry {
|
|
73
|
+
let version = 0;
|
|
74
|
+
const listeners = new Set<() => void>();
|
|
75
|
+
const changed = (): void => {
|
|
76
|
+
version += 1;
|
|
77
|
+
|
|
78
|
+
for (const listener of [...listeners]) {
|
|
79
|
+
listener();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const collection = <Value extends { readonly id: string }>(): Collection<Value> => {
|
|
83
|
+
const values = new Map<string, Value>();
|
|
84
|
+
return {
|
|
85
|
+
register: (value) => {
|
|
86
|
+
values.set(value.id, value);
|
|
87
|
+
changed();
|
|
88
|
+
return () => {
|
|
89
|
+
if (values.get(value.id) === value) {
|
|
90
|
+
values.delete(value.id);
|
|
91
|
+
changed();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
list: () => [...values.values()],
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
const map = <Value>() => {
|
|
99
|
+
const values = new Map<string, Value>();
|
|
100
|
+
return {
|
|
101
|
+
register: (key: string, value: Value) => {
|
|
102
|
+
values.set(key, value);
|
|
103
|
+
changed();
|
|
104
|
+
return () => {
|
|
105
|
+
if (values.get(key) === value) {
|
|
106
|
+
values.delete(key);
|
|
107
|
+
changed();
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
get: (key: string) => values.get(key),
|
|
112
|
+
keys: () => [...values.keys()],
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const editors = map<ComponentType<PropertyEditorProps>>();
|
|
116
|
+
const icons = map<IconComponent>();
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
toolbar: collection<ToolbarItem>(),
|
|
120
|
+
contextMenu: collection<ContextMenuItem>(),
|
|
121
|
+
propertyEditors: { register: editors.register, get: editors.get, types: editors.keys },
|
|
122
|
+
icons: { register: icons.register, get: icons.get },
|
|
123
|
+
version: () => version,
|
|
124
|
+
subscribe: (listener) => {
|
|
125
|
+
listeners.add(listener);
|
|
126
|
+
return () => listeners.delete(listener);
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Sorts by `order`, then id, so equal orders are deterministic. */
|
|
132
|
+
export function byOrder<Value extends { readonly id: string; readonly order?: number }>(
|
|
133
|
+
values: readonly Value[],
|
|
134
|
+
): Value[] {
|
|
135
|
+
return [...values].sort(
|
|
136
|
+
(left, right) =>
|
|
137
|
+
(left.order ?? 500) - (right.order ?? 500) ||
|
|
138
|
+
(left.id < right.id ? -1 : left.id > right.id ? 1 : 0),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useState,
|
|
8
|
+
useSyncExternalStore,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
} from 'react';
|
|
11
|
+
import type { ElementId, Point } from '@drawback/core';
|
|
12
|
+
import {
|
|
13
|
+
builtinMessages,
|
|
14
|
+
createTranslator,
|
|
15
|
+
loadMessages,
|
|
16
|
+
needsMessages,
|
|
17
|
+
type Messages,
|
|
18
|
+
type Translate,
|
|
19
|
+
} from '../lib/i18n.js';
|
|
20
|
+
import { createUiRegistry, type UiRegistry } from '../lib/registry.js';
|
|
21
|
+
|
|
22
|
+
export type ThemeSetting = 'light' | 'dark' | 'system';
|
|
23
|
+
export type ResolvedTheme = 'light' | 'dark';
|
|
24
|
+
export type PanelId =
|
|
25
|
+
'layers' | 'canvas' | 'shortcuts' | 'export' | 'mainMenu' | 'link' | 'minimap';
|
|
26
|
+
|
|
27
|
+
export interface ContextMenuRequest {
|
|
28
|
+
readonly clientX: number;
|
|
29
|
+
readonly clientY: number;
|
|
30
|
+
/** Element the menu is about; it is selected when the menu opens. */
|
|
31
|
+
readonly elementId?: ElementId;
|
|
32
|
+
readonly world?: Point;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface UiContextValue {
|
|
36
|
+
readonly t: Translate;
|
|
37
|
+
readonly registry: UiRegistry;
|
|
38
|
+
/** Changes whenever the registry changes, so consumers re-render. */
|
|
39
|
+
readonly registryVersion: number;
|
|
40
|
+
/** Whether a control id was hidden by the host. */
|
|
41
|
+
isHidden(id: string): boolean;
|
|
42
|
+
readonly theme: ResolvedTheme;
|
|
43
|
+
readonly themeSetting: ThemeSetting;
|
|
44
|
+
setTheme(theme: ThemeSetting): void;
|
|
45
|
+
readonly locale: string;
|
|
46
|
+
setLocale(locale: string): void;
|
|
47
|
+
/** Element menus, tooltips and dialogs portal into. */
|
|
48
|
+
readonly portalContainer: HTMLElement | null;
|
|
49
|
+
readonly dir: 'ltr' | 'rtl';
|
|
50
|
+
isOpen(panel: PanelId): boolean;
|
|
51
|
+
setOpen(panel: PanelId, open: boolean): void;
|
|
52
|
+
readonly contextMenu: ContextMenuRequest | undefined;
|
|
53
|
+
openContextMenu(request: ContextMenuRequest | undefined): void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const UiContext = createContext<UiContextValue | undefined>(undefined);
|
|
57
|
+
const fallbackRegistry = createUiRegistry();
|
|
58
|
+
|
|
59
|
+
export interface UiProviderProps {
|
|
60
|
+
/** Overrides on top of the built-in catalog for `locale`. */
|
|
61
|
+
readonly messages?: Messages;
|
|
62
|
+
/** Controlled locale; without `onLocaleChange` the provider keeps its own state. */
|
|
63
|
+
readonly locale?: string;
|
|
64
|
+
readonly onLocaleChange?: (locale: string) => void;
|
|
65
|
+
readonly dir?: 'ltr' | 'rtl';
|
|
66
|
+
readonly registry?: UiRegistry;
|
|
67
|
+
readonly hidden?: readonly string[];
|
|
68
|
+
/** Controlled theme; without `onThemeChange` the provider keeps its own state. */
|
|
69
|
+
readonly theme?: ThemeSetting;
|
|
70
|
+
readonly onThemeChange?: (theme: ThemeSetting) => void;
|
|
71
|
+
readonly portalContainer?: HTMLElement | null;
|
|
72
|
+
readonly initialOpen?: readonly PanelId[];
|
|
73
|
+
readonly children?: ReactNode;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function subscribeColorScheme(listener: () => void): () => void {
|
|
77
|
+
if (typeof matchMedia !== 'function') {
|
|
78
|
+
return () => undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const query = matchMedia('(prefers-color-scheme: dark)');
|
|
82
|
+
query.addEventListener('change', listener);
|
|
83
|
+
return () => query.removeEventListener('change', listener);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function prefersDark(): boolean {
|
|
87
|
+
return typeof matchMedia === 'function' && matchMedia('(prefers-color-scheme: dark)').matches;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Resolves `'system'` through `prefers-color-scheme`, following changes. */
|
|
91
|
+
export function useResolvedTheme(setting: ThemeSetting): ResolvedTheme {
|
|
92
|
+
const dark = useSyncExternalStore(subscribeColorScheme, prefersDark, () => false);
|
|
93
|
+
return setting === 'system' ? (dark ? 'dark' : 'light') : setting;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Strings, registrations, hidden controls, theme and panel state for UI components.
|
|
98
|
+
* `<DefaultEditor>` renders one; hosts placing components themselves can render their own.
|
|
99
|
+
*/
|
|
100
|
+
export function UiProvider({
|
|
101
|
+
messages,
|
|
102
|
+
locale,
|
|
103
|
+
onLocaleChange,
|
|
104
|
+
dir = 'ltr',
|
|
105
|
+
registry = fallbackRegistry,
|
|
106
|
+
hidden = [],
|
|
107
|
+
theme,
|
|
108
|
+
onThemeChange,
|
|
109
|
+
portalContainer = null,
|
|
110
|
+
initialOpen = [],
|
|
111
|
+
children,
|
|
112
|
+
}: UiProviderProps): ReactNode {
|
|
113
|
+
const [ownTheme, setOwnTheme] = useState<ThemeSetting>(theme ?? 'system');
|
|
114
|
+
const themeSetting = onThemeChange === undefined ? (theme ?? ownTheme) : (theme ?? 'system');
|
|
115
|
+
const resolved = useResolvedTheme(themeSetting);
|
|
116
|
+
const [open, setOpenState] = useState<ReadonlySet<PanelId>>(() => new Set(initialOpen));
|
|
117
|
+
const [contextMenu, openContextMenu] = useState<ContextMenuRequest | undefined>(undefined);
|
|
118
|
+
useSyncExternalStore(registry.subscribe, registry.version, registry.version);
|
|
119
|
+
const registryVersion = registry.version();
|
|
120
|
+
const [ownLocale, setOwnLocale] = useState(locale ?? 'en');
|
|
121
|
+
const currentLocale = onLocaleChange === undefined ? (locale ?? ownLocale) : (locale ?? 'en');
|
|
122
|
+
// The last catalog that was ready. While a newly picked language loads, it stays on screen,
|
|
123
|
+
// so the UI never flashes English; loaded catalogs are cached and apply immediately.
|
|
124
|
+
const [shown, setShown] = useState<{ readonly locale: string; readonly messages?: Messages }>({
|
|
125
|
+
locale: 'en',
|
|
126
|
+
});
|
|
127
|
+
const loading = needsMessages(currentLocale);
|
|
128
|
+
const catalog = loading
|
|
129
|
+
? shown
|
|
130
|
+
: { locale: currentLocale, messages: builtinMessages(currentLocale) };
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (!loading) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let cancelled = false;
|
|
138
|
+
loadMessages(currentLocale).then(
|
|
139
|
+
(messages) => {
|
|
140
|
+
if (!cancelled) {
|
|
141
|
+
setShown({ locale: currentLocale, ...(messages === undefined ? {} : { messages }) });
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
(error: unknown) => {
|
|
145
|
+
console.error(`Could not load the "${currentLocale}" catalog.`, error);
|
|
146
|
+
|
|
147
|
+
if (!cancelled) {
|
|
148
|
+
setShown({ locale: currentLocale });
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
return () => {
|
|
153
|
+
cancelled = true;
|
|
154
|
+
};
|
|
155
|
+
}, [currentLocale, loading]);
|
|
156
|
+
|
|
157
|
+
const t = useMemo(
|
|
158
|
+
() => createTranslator({ ...catalog.messages, ...messages }, catalog.locale),
|
|
159
|
+
[messages, catalog.messages, catalog.locale],
|
|
160
|
+
);
|
|
161
|
+
const setLocale = useCallback(
|
|
162
|
+
(next: string) => {
|
|
163
|
+
if (onLocaleChange === undefined) {
|
|
164
|
+
setOwnLocale(next);
|
|
165
|
+
} else {
|
|
166
|
+
onLocaleChange(next);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
[onLocaleChange],
|
|
170
|
+
);
|
|
171
|
+
const hiddenKey = hidden.join('|');
|
|
172
|
+
const hiddenSet = useMemo(
|
|
173
|
+
() => new Set(hiddenKey === '' ? [] : hiddenKey.split('|')),
|
|
174
|
+
[hiddenKey],
|
|
175
|
+
);
|
|
176
|
+
const setTheme = useCallback(
|
|
177
|
+
(next: ThemeSetting) => {
|
|
178
|
+
if (onThemeChange === undefined) {
|
|
179
|
+
setOwnTheme(next);
|
|
180
|
+
} else {
|
|
181
|
+
onThemeChange(next);
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
[onThemeChange],
|
|
185
|
+
);
|
|
186
|
+
const setOpen = useCallback((panel: PanelId, value: boolean) => {
|
|
187
|
+
setOpenState((current) => {
|
|
188
|
+
if (current.has(panel) === value) {
|
|
189
|
+
return current;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const next = new Set(current);
|
|
193
|
+
|
|
194
|
+
if (value) {
|
|
195
|
+
next.add(panel);
|
|
196
|
+
} else {
|
|
197
|
+
next.delete(panel);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return next;
|
|
201
|
+
});
|
|
202
|
+
}, []);
|
|
203
|
+
const value = useMemo<UiContextValue>(
|
|
204
|
+
() => ({
|
|
205
|
+
t,
|
|
206
|
+
registry,
|
|
207
|
+
registryVersion,
|
|
208
|
+
isHidden: (id) => hiddenSet.has(id),
|
|
209
|
+
theme: resolved,
|
|
210
|
+
themeSetting,
|
|
211
|
+
setTheme,
|
|
212
|
+
locale: currentLocale,
|
|
213
|
+
setLocale,
|
|
214
|
+
portalContainer,
|
|
215
|
+
dir,
|
|
216
|
+
isOpen: (panel) => open.has(panel),
|
|
217
|
+
setOpen,
|
|
218
|
+
contextMenu,
|
|
219
|
+
openContextMenu,
|
|
220
|
+
}),
|
|
221
|
+
[
|
|
222
|
+
t,
|
|
223
|
+
registry,
|
|
224
|
+
registryVersion,
|
|
225
|
+
hiddenSet,
|
|
226
|
+
resolved,
|
|
227
|
+
themeSetting,
|
|
228
|
+
setTheme,
|
|
229
|
+
currentLocale,
|
|
230
|
+
setLocale,
|
|
231
|
+
portalContainer,
|
|
232
|
+
dir,
|
|
233
|
+
open,
|
|
234
|
+
setOpen,
|
|
235
|
+
contextMenu,
|
|
236
|
+
],
|
|
237
|
+
);
|
|
238
|
+
return <UiContext value={value}>{children}</UiContext>;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function useUi(): UiContextValue {
|
|
242
|
+
const context = useContext(UiContext);
|
|
243
|
+
|
|
244
|
+
if (context === undefined) {
|
|
245
|
+
throw new Error('UI components must be rendered inside <DefaultEditor> or <UiProvider>.');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return context;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function useT(): Translate {
|
|
252
|
+
return useUi().t;
|
|
253
|
+
}
|