@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,268 @@
1
+ import type { ElementId } from '@drawback/core';
2
+ import {
3
+ useEditor,
4
+ useEditorValue,
5
+ useElement,
6
+ usePermission,
7
+ useSelection,
8
+ } from '@drawback/react';
9
+ import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from 'react';
10
+ import { LINK_COMMAND } from '../../lib/commands.js';
11
+ import { useUi } from '../../stores/ui-context.js';
12
+ import { IconButton, cx } from '../../components/primitives.js';
13
+
14
+ /** Opens the link editor for the single selected element (Mod+K, context menu, properties). */
15
+
16
+ /** Screen gap between the element and the popover; clears the rotation handle. */
17
+ const POPOVER_GAP = 44;
18
+ /** Approximate popover height, used to decide whether it fits above the element. */
19
+ const POPOVER_HEIGHT = 44;
20
+ const VIEW_MARGIN = 8;
21
+
22
+ function LinkInput({
23
+ id,
24
+ onDone,
25
+ }: {
26
+ readonly id: ElementId;
27
+ readonly onDone: () => void;
28
+ }): ReactNode {
29
+ const editor = useEditor();
30
+ const { t } = useUi();
31
+ const current = editor.links.get(id);
32
+ const [draft, setDraft] = useState(current ?? '');
33
+ const [error, setError] = useState(false);
34
+ const finished = useRef(false);
35
+
36
+ /** Saves the draft (empty removes the link); false when the link is rejected. */
37
+ const commit = (): boolean => {
38
+ const trimmed = draft.trim();
39
+ const next = trimmed === '' ? undefined : trimmed;
40
+
41
+ if (next === current) {
42
+ return true;
43
+ }
44
+
45
+ try {
46
+ editor.links.set(id, next);
47
+ return true;
48
+ } catch {
49
+ setError(true);
50
+ return false;
51
+ }
52
+ };
53
+ const finish = (): void => {
54
+ finished.current = true;
55
+ onDone();
56
+ };
57
+
58
+ return (
59
+ <>
60
+ <input
61
+ type="text"
62
+ // oxlint-disable-next-line jsx-a11y/no-autofocus -- the editor opens on an explicit request.
63
+ autoFocus
64
+ className={cx('nx-link-input', error && 'nx-invalid')}
65
+ value={draft}
66
+ placeholder={t('link.placeholder')}
67
+ aria-label={t('link.label')}
68
+ aria-invalid={error}
69
+ data-testid="link-input"
70
+ onChange={(event) => {
71
+ setDraft(event.target.value);
72
+ setError(false);
73
+ }}
74
+ onKeyDown={(event) => {
75
+ if (event.key === 'Enter') {
76
+ event.preventDefault();
77
+
78
+ if (commit()) {
79
+ finish();
80
+ }
81
+ } else if (event.key === 'Escape') {
82
+ event.preventDefault();
83
+ event.stopPropagation();
84
+ finish();
85
+ }
86
+ }}
87
+ onBlur={() => {
88
+ // Clicking elsewhere saves like Excalidraw; a rejected link is dropped, not kept open.
89
+ if (!finished.current) {
90
+ commit();
91
+ finish();
92
+ }
93
+ }}
94
+ />
95
+ {error && (
96
+ <p
97
+ className="nx-link-error"
98
+ role="alert"
99
+ >
100
+ {t('link.invalid')}
101
+ </p>
102
+ )}
103
+ </>
104
+ );
105
+ }
106
+
107
+ /**
108
+ * Excalidraw-style link editor: a popover above the single selected element. It shows the
109
+ * element's link (click to open) with edit and remove buttons, or an input while editing
110
+ * (Mod+K, "Edit link…" in the context menu, the properties action). Enter or clicking away
111
+ * saves, Escape cancels, an empty link removes it.
112
+ */
113
+ export function LinkEditor(): ReactNode {
114
+ const editor = useEditor();
115
+ const { t, isOpen, setOpen } = useUi();
116
+ const selection = useSelection();
117
+ const id = selection.length === 1 ? selection[0] : undefined;
118
+ const element = useElement(id);
119
+ const canEdit = usePermission('core.edit.content', id);
120
+ const busy = useEditorValue(
121
+ (current) =>
122
+ current.interactions.active() !== undefined || current.text.editing() !== undefined,
123
+ {
124
+ events: ['interaction.start', 'interaction.commit', 'interaction.cancel', 'text.editing'],
125
+ },
126
+ );
127
+ const ref = useRef<HTMLDivElement>(null);
128
+ const editing = isOpen('link') && id !== undefined && canEdit;
129
+ const url = typeof element?.link === 'string' ? element.link : undefined;
130
+ const shown =
131
+ id !== undefined && element !== undefined && !busy && (editing || url !== undefined);
132
+ // Follow the viewport only while the popover shows, so panning doesn't re-render it otherwise.
133
+ const viewport =
134
+ useEditorValue((current) => (shown ? current.viewport.get() : undefined), {
135
+ events: ['viewport.change'],
136
+ }) ?? editor.viewport.get();
137
+
138
+ useEffect(() => {
139
+ const offCommand = editor.commands.register(
140
+ {
141
+ id: LINK_COMMAND,
142
+ label: t('link.edit'),
143
+ canRun: () => {
144
+ const selected = editor.selection.get();
145
+ return (
146
+ selected.length === 1 &&
147
+ selected[0] !== undefined &&
148
+ editor.permissions.can('core.edit.content', selected[0])
149
+ );
150
+ },
151
+ run: () => setOpen('link', true),
152
+ },
153
+ { replace: true },
154
+ );
155
+ const offKey = editor.input.keymap.bind('Mod+K', LINK_COMMAND);
156
+ return () => {
157
+ offKey();
158
+ offCommand();
159
+ };
160
+ }, [editor, setOpen, t]);
161
+
162
+ // Selecting something else ends an edit (and so does unmounting the editor).
163
+ useEffect(() => {
164
+ const off = editor.events.on('selection.change', () => setOpen('link', false));
165
+ return () => {
166
+ off();
167
+ setOpen('link', false);
168
+ };
169
+ }, [editor, setOpen]);
170
+
171
+ const bounds = element === undefined ? undefined : editor.elements.boundsFor(element, 'visual');
172
+ const centerX =
173
+ bounds === undefined ? 0 : ((bounds.minX + bounds.maxX) / 2 - viewport.x) * viewport.zoom;
174
+ const top = bounds === undefined ? 0 : (bounds.minY - viewport.y) * viewport.zoom;
175
+ const bottom = bounds === undefined ? 0 : (bounds.maxY - viewport.y) * viewport.zoom;
176
+ const above = top - POPOVER_GAP - POPOVER_HEIGHT >= VIEW_MARGIN;
177
+
178
+ // Keep the popover inside the view horizontally once its width is known.
179
+ useLayoutEffect(() => {
180
+ const node = ref.current;
181
+ const parent = node?.offsetParent;
182
+
183
+ if (node === null || !(parent instanceof HTMLElement)) {
184
+ return;
185
+ }
186
+
187
+ const half = node.offsetWidth / 2;
188
+ const left = Math.max(
189
+ half + VIEW_MARGIN,
190
+ Math.min(centerX, parent.clientWidth - half - VIEW_MARGIN),
191
+ );
192
+ node.style.left = `${left}px`;
193
+ });
194
+
195
+ if (!shown) {
196
+ return null;
197
+ }
198
+
199
+ const openable = url !== undefined && editor.links.canOpen(url, { elementId: id });
200
+
201
+ return (
202
+ <div
203
+ ref={ref}
204
+ className="nx-link-popover nx-island"
205
+ role="group"
206
+ aria-label={t('link.label')}
207
+ data-testid="link-editor"
208
+ style={{
209
+ left: centerX,
210
+ top: above ? top - POPOVER_GAP : bottom + POPOVER_GAP,
211
+ transform: above ? 'translate(-50%, -100%)' : 'translate(-50%, 0)',
212
+ }}
213
+ >
214
+ {editing ? (
215
+ <LinkInput
216
+ key={id}
217
+ id={id}
218
+ onDone={() => setOpen('link', false)}
219
+ />
220
+ ) : (
221
+ <>
222
+ {openable ? (
223
+ <a
224
+ className="nx-link-url"
225
+ href={url}
226
+ title={url}
227
+ data-testid="link-url"
228
+ onClick={(event) => {
229
+ event.preventDefault();
230
+ editor.links.open(id, { source: 'pointer' });
231
+ }}
232
+ >
233
+ {url}
234
+ </a>
235
+ ) : (
236
+ <span
237
+ className="nx-link-url nx-inert"
238
+ title={url}
239
+ data-testid="link-url"
240
+ >
241
+ {url}
242
+ </span>
243
+ )}
244
+ {canEdit && (
245
+ <>
246
+ <IconButton
247
+ icon="edit"
248
+ label={t('link.change')}
249
+ onClick={() => setOpen('link', true)}
250
+ testId="link-edit"
251
+ />
252
+ <IconButton
253
+ icon="delete"
254
+ label={t('link.remove')}
255
+ danger
256
+ onClick={() => editor.links.set(id, undefined)}
257
+ testId="link-remove"
258
+ />
259
+ </>
260
+ )}
261
+ </>
262
+ )}
263
+ </div>
264
+ );
265
+ }
266
+
267
+ /** @deprecated Renamed to {@link LinkEditor}; links are edited in a popover now. */
268
+ export const LinkDialog = LinkEditor;
@@ -0,0 +1,121 @@
1
+ import { useBackground, useEditor, useGrid, usePermission } from '@drawback/react';
2
+ import type { ReactNode } from 'react';
3
+ import { ColorField } from '../../components/color-picker/color-picker.js';
4
+ import { Choice, Section } from '../../components/primitives.js';
5
+ import { useUi } from '../../stores/ui-context.js';
6
+
7
+ const BACKGROUNDS = ['#ffffff', '#f8f9fa', '#f5faff', '#fffce8', '#fdf8f6', '#121212'];
8
+ const GRID_COLORS = ['#868e96', '#ced4da', '#343a40', '#a5d8ff', '#ffc9c9'];
9
+ const GRID_TYPES = [
10
+ { value: 'none', icon: 'gridNone' },
11
+ { value: 'square', icon: 'gridSquare' },
12
+ { value: 'dot', icon: 'gridDot' },
13
+ { value: 'hex-pointy', icon: 'gridHexPointy' },
14
+ { value: 'hex-flat', icon: 'gridHexFlat' },
15
+ ] as const;
16
+
17
+ /** Shared canvas background and grid settings (grid visibility and snapping are view toggles). */
18
+ export function CanvasPropertiesPanel(): ReactNode {
19
+ const editor = useEditor();
20
+ const { t } = useUi();
21
+ const background = useBackground();
22
+ const grid = useGrid();
23
+ const canCanvas = usePermission('core.manage-canvas');
24
+ const canGrid = usePermission('core.manage-grid');
25
+ const type = grid.config.type;
26
+ return (
27
+ <div
28
+ className="nx-canvas-properties"
29
+ data-testid="canvas-properties"
30
+ >
31
+ <Section title={t('canvas.background')}>
32
+ <ColorField
33
+ label={t('canvas.background')}
34
+ value={background}
35
+ palette={BACKGROUNDS}
36
+ kind="background"
37
+ disabled={!canCanvas}
38
+ onChange={(color) => editor.document.setBackground(color)}
39
+ testId="canvas-background"
40
+ />
41
+ {background !== undefined && (
42
+ <button
43
+ type="button"
44
+ className="nx-link"
45
+ disabled={!canCanvas}
46
+ onClick={() => editor.document.setBackground(undefined)}
47
+ >
48
+ {t('canvas.backgroundReset')}
49
+ </button>
50
+ )}
51
+ </Section>
52
+ <div className="nx-menu-row">
53
+ <span>{t('grid.label')}</span>
54
+ <Choice
55
+ label={t('grid.type')}
56
+ value={type}
57
+ disabled={!canGrid}
58
+ options={GRID_TYPES.map(({ value, icon }) => ({
59
+ value,
60
+ icon,
61
+ label: t(`grid.type.${value}`),
62
+ }))}
63
+ onChange={(value) =>
64
+ value === 'none' ? editor.grid.clear() : editor.grid.set({ type: value })
65
+ }
66
+ testId="grid-type"
67
+ />
68
+ </div>
69
+ {type !== 'none' && (
70
+ <>
71
+ <label className="nx-field">
72
+ <span>{t('grid.size')}</span>
73
+ <input
74
+ type="number"
75
+ min={1}
76
+ max={1000}
77
+ value={grid.config.size}
78
+ disabled={!canGrid}
79
+ data-testid="grid-size"
80
+ onChange={(event) => {
81
+ const size = Number(event.target.value);
82
+
83
+ if (Number.isFinite(size) && size >= 1) {
84
+ editor.grid.set({ size });
85
+ }
86
+ }}
87
+ />
88
+ </label>
89
+ {(type === 'square' || type === 'dot') && (
90
+ <label className="nx-field">
91
+ <span>{t('grid.majorEvery')}</span>
92
+ <input
93
+ type="number"
94
+ min={1}
95
+ max={100}
96
+ value={grid.config.majorEvery ?? 1}
97
+ disabled={!canGrid}
98
+ onChange={(event) => {
99
+ const every = Math.round(Number(event.target.value));
100
+ if (every >= 1 && every <= 100) {
101
+ editor.grid.set({ majorEvery: every });
102
+ }
103
+ }}
104
+ />
105
+ </label>
106
+ )}
107
+ <Section title={t('grid.color')}>
108
+ <ColorField
109
+ label={t('grid.color')}
110
+ value={grid.config.color}
111
+ palette={GRID_COLORS}
112
+ disabled={!canGrid}
113
+ onChange={(color) => editor.grid.set({ color, majorColor: undefined })}
114
+ testId="grid-color"
115
+ />
116
+ </Section>
117
+ </>
118
+ )}
119
+ </div>
120
+ );
121
+ }
@@ -0,0 +1,215 @@
1
+ import type { AnyEditor } from '@drawback/core';
2
+ import { useEditor } from '@drawback/react';
3
+ import { useRef, useState, type ReactNode } from 'react';
4
+ import { useUi, type ThemeSetting } from '../../stores/ui-context.js';
5
+
6
+ import { LANGUAGES } from '../../lib/i18n.js';
7
+ import { CanvasPropertiesPanel } from './canvas-properties.js';
8
+ import {
9
+ Choice,
10
+ IconButton,
11
+ MenuItems,
12
+ Popover,
13
+ Portal,
14
+ type MenuEntry,
15
+ } from '../../components/primitives.js';
16
+ import { download } from '../../lib/download.js';
17
+
18
+ /** Opens a document file or bundle chosen by the user (replace import). */
19
+ export async function openFile(editor: AnyEditor, file: Blob): Promise<void> {
20
+ const parsed: unknown = JSON.parse(await file.text());
21
+ const bundle =
22
+ parsed !== null &&
23
+ typeof parsed === 'object' &&
24
+ Reflect.get(parsed, 'format') === 'interactive-canvas-bundle';
25
+
26
+ if (bundle) {
27
+ await editor.io.importBundle(parsed);
28
+ } else {
29
+ editor.io.importDocument(parsed);
30
+ }
31
+
32
+ await editor.viewport.zoomToFit({ padding: 48 });
33
+ }
34
+
35
+ export interface MainMenuProps {
36
+ readonly items?: ReactNode;
37
+ readonly fileName?: string;
38
+ }
39
+
40
+ /** Hamburger menu: open and save files, export, shortcuts, reset, then theme and canvas settings inline. */
41
+ export function MainMenu({ items, fileName = 'canvas' }: MainMenuProps): ReactNode {
42
+ const editor = useEditor();
43
+ const { t, isOpen, setOpen, setTheme, themeSetting, locale, setLocale, isHidden } = useUi();
44
+ const anchor = useRef<HTMLSpanElement>(null);
45
+ const input = useRef<HTMLInputElement>(null);
46
+ const [error, setError] = useState<string | undefined>(undefined);
47
+ const open = isOpen('mainMenu');
48
+ const fail = (cause: unknown): void =>
49
+ setError(
50
+ t('error.import', { message: cause instanceof Error ? cause.message : String(cause) }),
51
+ );
52
+ const allEntries: MenuEntry[] = [
53
+ { id: 'open', label: t('menu.open'), icon: 'open', onSelect: () => input.current?.click() },
54
+ {
55
+ id: 'save',
56
+ label: t('menu.save'),
57
+ icon: 'save',
58
+ onSelect: () =>
59
+ download(
60
+ `${fileName}.json`,
61
+ editor.io.stringify(
62
+ editor.io.exportDocument({ pendingAssets: 'keep', meta: { app: 'drawback' } }),
63
+ ),
64
+ ),
65
+ },
66
+ {
67
+ id: 'saveBundle',
68
+ label: t('menu.exportBundle'),
69
+ icon: 'download',
70
+ onSelect: () =>
71
+ void editor.io
72
+ .exportBundle()
73
+ .then(({ bundle }) => download(`${fileName}.bundle.json`, editor.io.stringify(bundle)))
74
+ .catch(fail),
75
+ },
76
+ {
77
+ id: 'exportImage',
78
+ label: t('menu.exportImage'),
79
+ icon: 'download',
80
+ onSelect: () => setOpen('export', true),
81
+ },
82
+ {
83
+ id: 'shortcuts',
84
+ label: t('menu.shortcuts'),
85
+ icon: 'keyboard',
86
+ separatorBefore: true,
87
+ shortcut: '?',
88
+ onSelect: () => setOpen('shortcuts', true),
89
+ },
90
+ {
91
+ id: 'reset',
92
+ label: t('menu.reset'),
93
+ icon: 'reset',
94
+ danger: true,
95
+ separatorBefore: true,
96
+ disabled: !editor.permissions.can('core.delete'),
97
+ onSelect: () => {
98
+ if (typeof confirm === 'function' && !confirm(t('menu.resetConfirm'))) {
99
+ return;
100
+ }
101
+
102
+ editor.transform.delete(editor.elements.query().map((element) => element.id));
103
+ },
104
+ },
105
+ ];
106
+ const entries = allEntries.filter((entry) => !isHidden(`menu:${entry.id}`));
107
+
108
+ return (
109
+ <>
110
+ <span
111
+ ref={anchor}
112
+ className="nx-anchor"
113
+ >
114
+ <IconButton
115
+ icon="menu"
116
+ label={t('menu.label')}
117
+ pressed={open}
118
+ onClick={() => setOpen('mainMenu', !open)}
119
+ testId="main-menu"
120
+ />
121
+ </span>
122
+ <input
123
+ ref={input}
124
+ type="file"
125
+ accept=".json,application/json"
126
+ hidden
127
+ data-testid="open-file"
128
+ onChange={(event) => {
129
+ const file = event.target.files?.[0];
130
+ event.target.value = '';
131
+
132
+ if (file !== undefined) {
133
+ setError(undefined);
134
+ void openFile(editor, file).catch(fail);
135
+ }
136
+ }}
137
+ />
138
+ <Popover
139
+ open={open}
140
+ onClose={() => setOpen('mainMenu', false)}
141
+ anchor={anchor}
142
+ label={t('menu.label')}
143
+ role="menu"
144
+ testId="main-menu-popover"
145
+ >
146
+ <MenuItems
147
+ entries={entries}
148
+ onDone={() => setOpen('mainMenu', false)}
149
+ />
150
+ {items}
151
+ {(!isHidden('menu:theme') || !isHidden('menu:language') || !isHidden('menu:canvas')) && (
152
+ <div
153
+ className="nx-separator"
154
+ role="separator"
155
+ />
156
+ )}
157
+ <div className="nx-menu-settings">
158
+ {!isHidden('menu:theme') && (
159
+ <div className="nx-menu-row">
160
+ <span>{t('theme.label')}</span>
161
+ <Choice<ThemeSetting>
162
+ label={t('theme.label')}
163
+ value={themeSetting}
164
+ options={[
165
+ { value: 'light', label: t('theme.light'), icon: 'light' },
166
+ { value: 'dark', label: t('theme.dark'), icon: 'dark' },
167
+ { value: 'system', label: t('theme.system'), icon: 'system' },
168
+ ]}
169
+ onChange={setTheme}
170
+ testId="theme"
171
+ />
172
+ </div>
173
+ )}
174
+ {!isHidden('menu:language') && (
175
+ <label className="nx-field">
176
+ <span>{t('language.label')}</span>
177
+ <select
178
+ value={LANGUAGES.find((entry) => locale.startsWith(entry.locale))?.locale ?? locale}
179
+ data-testid="language"
180
+ onChange={(event) => setLocale(event.target.value)}
181
+ >
182
+ {LANGUAGES.map((entry) => (
183
+ <option
184
+ key={entry.locale}
185
+ value={entry.locale}
186
+ lang={entry.locale}
187
+ >
188
+ {entry.name}
189
+ </option>
190
+ ))}
191
+ </select>
192
+ </label>
193
+ )}
194
+ {!isHidden('menu:canvas') && <CanvasPropertiesPanel />}
195
+ </div>
196
+ </Popover>
197
+ {error !== undefined && (
198
+ <Portal>
199
+ <div
200
+ className="nx-toast"
201
+ role="alert"
202
+ data-testid="ui-error"
203
+ >
204
+ {error}
205
+ <IconButton
206
+ icon="close"
207
+ label={t('dialog.close')}
208
+ onClick={() => setError(undefined)}
209
+ />
210
+ </div>
211
+ </Portal>
212
+ )}
213
+ </>
214
+ );
215
+ }
@@ -0,0 +1,69 @@
1
+ import { mountMinimap, type CanvasRenderer, type MinimapView } from '@drawback/canvas';
2
+ import { useEditor } from '@drawback/react';
3
+ import { useEffect, useRef, type ReactNode } from 'react';
4
+ import { useUi } from '../../stores/ui-context.js';
5
+ import { Island } from '../../components/primitives.js';
6
+
7
+ const PALETTES = {
8
+ light: {
9
+ background: 'rgba(255, 255, 255, 0.9)',
10
+ element: '#868e96',
11
+ viewport: '#4263eb',
12
+ viewportFill: 'rgba(66, 99, 235, 0.1)',
13
+ },
14
+ dark: {
15
+ background: 'rgba(35, 35, 41, 0.9)',
16
+ element: '#adb5bd',
17
+ viewport: '#91a7ff',
18
+ viewportFill: 'rgba(145, 167, 255, 0.15)',
19
+ },
20
+ } as const;
21
+
22
+ export interface MinimapProps {
23
+ readonly width?: number;
24
+ readonly height?: number;
25
+ /** Renderers whose `minimap` hook draws custom representations. */
26
+ readonly renderers?: readonly CanvasRenderer[] | undefined;
27
+ }
28
+
29
+ /** Overview of the document; click or drag to navigate. */
30
+ export function Minimap({ width = 200, height = 140, renderers }: MinimapProps): ReactNode {
31
+ const editor = useEditor();
32
+ const { t, theme } = useUi();
33
+ const host = useRef<HTMLDivElement>(null);
34
+ const view = useRef<MinimapView | undefined>(undefined);
35
+
36
+ useEffect(() => {
37
+ if (host.current === null) {
38
+ return undefined;
39
+ }
40
+
41
+ const minimap = mountMinimap(editor, host.current, { width, height });
42
+ view.current = minimap;
43
+ return () => {
44
+ minimap.dispose();
45
+ view.current = undefined;
46
+ };
47
+ }, [editor, width, height]);
48
+
49
+ useEffect(() => {
50
+ view.current?.setOptions({
51
+ colors: PALETTES[theme],
52
+ ...(renderers === undefined ? {} : { renderers }),
53
+ });
54
+ }, [theme, renderers]);
55
+
56
+ return (
57
+ <Island
58
+ className="nx-minimap"
59
+ label={t('minimap.label')}
60
+ testId="minimap-island"
61
+ >
62
+ <div
63
+ ref={host}
64
+ role="img"
65
+ aria-label={t('minimap.label')}
66
+ />
67
+ </Island>
68
+ );
69
+ }