@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,172 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { IconComponent } from '../lib/registry.js';
|
|
3
|
+
|
|
4
|
+
interface SvgProps {
|
|
5
|
+
readonly size?: number;
|
|
6
|
+
readonly 'aria-hidden'?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function svg(children: ReactNode, fill = false): IconComponent {
|
|
10
|
+
return function StyleIcon({ size = 18, 'aria-hidden': hidden }: SvgProps): ReactNode {
|
|
11
|
+
return (
|
|
12
|
+
<svg
|
|
13
|
+
width={size}
|
|
14
|
+
height={size}
|
|
15
|
+
viewBox="0 0 20 20"
|
|
16
|
+
fill={fill ? 'currentColor' : 'none'}
|
|
17
|
+
stroke="currentColor"
|
|
18
|
+
strokeLinecap="round"
|
|
19
|
+
strokeLinejoin="round"
|
|
20
|
+
aria-hidden={hidden}
|
|
21
|
+
>
|
|
22
|
+
{children}
|
|
23
|
+
</svg>
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const styleIcons: Readonly<Record<string, IconComponent>> = {
|
|
29
|
+
strokeThin: svg(
|
|
30
|
+
<path
|
|
31
|
+
d="M4 10h12"
|
|
32
|
+
strokeWidth={1.25}
|
|
33
|
+
/>,
|
|
34
|
+
),
|
|
35
|
+
strokeMedium: svg(
|
|
36
|
+
<path
|
|
37
|
+
d="M4 10h12"
|
|
38
|
+
strokeWidth={2.5}
|
|
39
|
+
/>,
|
|
40
|
+
),
|
|
41
|
+
strokeBold: svg(
|
|
42
|
+
<path
|
|
43
|
+
d="M4 10h12"
|
|
44
|
+
strokeWidth={4}
|
|
45
|
+
/>,
|
|
46
|
+
),
|
|
47
|
+
strokeSolid: svg(
|
|
48
|
+
<path
|
|
49
|
+
d="M4 10h12"
|
|
50
|
+
strokeWidth={2}
|
|
51
|
+
/>,
|
|
52
|
+
),
|
|
53
|
+
strokeDashed: svg(
|
|
54
|
+
<path
|
|
55
|
+
d="M4 10h12"
|
|
56
|
+
strokeWidth={2}
|
|
57
|
+
strokeDasharray="4 3"
|
|
58
|
+
strokeLinecap="butt"
|
|
59
|
+
/>,
|
|
60
|
+
),
|
|
61
|
+
strokeDotted: svg(
|
|
62
|
+
<path
|
|
63
|
+
d="M4 10h12"
|
|
64
|
+
strokeWidth={2}
|
|
65
|
+
strokeDasharray="0 4"
|
|
66
|
+
/>,
|
|
67
|
+
),
|
|
68
|
+
// Sloppiness: one clean stroke, a stroke with a slightly offset second pass, and a wobbly
|
|
69
|
+
// double stroke (after Excalidraw's icons).
|
|
70
|
+
sloppinessArchitect: svg(
|
|
71
|
+
<path
|
|
72
|
+
d="M2.5 13.5c2.5-1 4.5-5 7-5s2.5 3 4 3 2.5-1.5 4-3"
|
|
73
|
+
strokeWidth={1.25}
|
|
74
|
+
/>,
|
|
75
|
+
),
|
|
76
|
+
sloppinessArtist: svg(
|
|
77
|
+
<>
|
|
78
|
+
<path
|
|
79
|
+
d="M2.5 13.5c2.5-1 4.5-5 7-5s2.5 3 4 3 2.5-1.5 4-3"
|
|
80
|
+
strokeWidth={1.25}
|
|
81
|
+
/>
|
|
82
|
+
<path
|
|
83
|
+
d="M3 15c2.5-1.5 4.5-5 6.5-5.5s3 2.5 4.5 2.5 2.5-1.5 3.5-2.5"
|
|
84
|
+
strokeWidth={1}
|
|
85
|
+
/>
|
|
86
|
+
</>,
|
|
87
|
+
),
|
|
88
|
+
sloppinessCartoonist: svg(
|
|
89
|
+
<>
|
|
90
|
+
<path
|
|
91
|
+
d="M2.5 13c1-.5 1.5-2 2.5-3s1.5.5 2.5-1 1.5-2 2.5-1.5 1 2.5 2 2.5 1.5-1 2.5-1.5 1.5-1 3-2"
|
|
92
|
+
strokeWidth={1.25}
|
|
93
|
+
/>
|
|
94
|
+
<path
|
|
95
|
+
d="M3 15.5c1.5-1 2-3 3.5-3.5s1 1 2.5-.5 2-2.5 3-1.5.5 2 2 1.5 1.5-1.5 3.5-2"
|
|
96
|
+
strokeWidth={1}
|
|
97
|
+
/>
|
|
98
|
+
</>,
|
|
99
|
+
),
|
|
100
|
+
// Edges: a dotted square with a solid top-left corner, sharp or rounded.
|
|
101
|
+
edgeSharp: svg(
|
|
102
|
+
<>
|
|
103
|
+
<path
|
|
104
|
+
d="M12.5 4H16v12H4v-3.5"
|
|
105
|
+
strokeWidth={1.25}
|
|
106
|
+
strokeDasharray="1.5 2.5"
|
|
107
|
+
/>
|
|
108
|
+
<path
|
|
109
|
+
d="M4 11V4h7"
|
|
110
|
+
strokeWidth={1.5}
|
|
111
|
+
strokeLinejoin="miter"
|
|
112
|
+
strokeLinecap="butt"
|
|
113
|
+
/>
|
|
114
|
+
</>,
|
|
115
|
+
),
|
|
116
|
+
edgeRound: svg(
|
|
117
|
+
<>
|
|
118
|
+
<path
|
|
119
|
+
d="M12.5 4H16v12H4v-3.5"
|
|
120
|
+
strokeWidth={1.25}
|
|
121
|
+
strokeDasharray="1.5 2.5"
|
|
122
|
+
/>
|
|
123
|
+
<path
|
|
124
|
+
d="M4 11V8.5A4.5 4.5 0 0 1 8.5 4H11"
|
|
125
|
+
strokeWidth={1.5}
|
|
126
|
+
/>
|
|
127
|
+
</>,
|
|
128
|
+
),
|
|
129
|
+
arrowSharp: svg(
|
|
130
|
+
<path
|
|
131
|
+
d="M4 16 16 4m-5 0h5v5"
|
|
132
|
+
strokeWidth={1.5}
|
|
133
|
+
/>,
|
|
134
|
+
),
|
|
135
|
+
arrowCurved: svg(
|
|
136
|
+
<path
|
|
137
|
+
d="M4 16C4 9 9 4 16 4m-4-3 4 3-3 4"
|
|
138
|
+
strokeWidth={1.5}
|
|
139
|
+
/>,
|
|
140
|
+
),
|
|
141
|
+
arrowElbow: svg(
|
|
142
|
+
<path
|
|
143
|
+
d="M4 16v-6h12V4m-3 3 3-3 3 3"
|
|
144
|
+
strokeWidth={1.5}
|
|
145
|
+
/>,
|
|
146
|
+
),
|
|
147
|
+
polygon: svg(
|
|
148
|
+
<path
|
|
149
|
+
d="M10 3 17 8l-2.5 8h-9L3 8z"
|
|
150
|
+
strokeWidth={1.5}
|
|
151
|
+
/>,
|
|
152
|
+
),
|
|
153
|
+
polyline: svg(
|
|
154
|
+
<path
|
|
155
|
+
d="M3 15 8 5l4 8 5-9"
|
|
156
|
+
strokeWidth={1.5}
|
|
157
|
+
/>,
|
|
158
|
+
),
|
|
159
|
+
/** A sans-serif capital A (Excalidraw's "normal" font icon). */
|
|
160
|
+
fontSans: svg(
|
|
161
|
+
<path
|
|
162
|
+
d="M5 16.5 10 3.5l5 13M6.9 11.5h6.2"
|
|
163
|
+
strokeWidth={1.5}
|
|
164
|
+
/>,
|
|
165
|
+
),
|
|
166
|
+
fontSerif: svg(
|
|
167
|
+
<path
|
|
168
|
+
d="M5.5 16.5 10 3.5l4.5 13M7.2 11.5h5.6M3.5 16.5h4M12.5 16.5h4"
|
|
169
|
+
strokeWidth={1.25}
|
|
170
|
+
/>,
|
|
171
|
+
),
|
|
172
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { toError } from '@drawback/core';
|
|
2
|
+
import type { AnyEditor } from '@drawback/core';
|
|
3
|
+
import { useCanvasView, useEditor } from '@drawback/react';
|
|
4
|
+
import { useEffect, useState, type ReactNode } from 'react';
|
|
5
|
+
import { Portal } from '../../components/primitives.js';
|
|
6
|
+
import { useUi } from '../../stores/ui-context.js';
|
|
7
|
+
import { useLatest } from '../../hooks/use-latest.js';
|
|
8
|
+
import {
|
|
9
|
+
CANVAS_PROPERTIES_COMMAND,
|
|
10
|
+
COPY_AS_PNG_COMMAND,
|
|
11
|
+
COPY_AS_SVG_COMMAND,
|
|
12
|
+
COPY_ELEMENT_LINK_COMMAND,
|
|
13
|
+
MINIMAP_COMMAND,
|
|
14
|
+
} from '../../lib/commands.js';
|
|
15
|
+
|
|
16
|
+
function reportError(editor: AnyEditor, error: unknown, context: string): void {
|
|
17
|
+
editor.events.reportError(toError(error), context);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Registers the UI-level commands the context menu and shortcuts use: copy as PNG
|
|
22
|
+
* (Shift+Alt+C) / SVG, copy link to object (with a short confirmation), the minimap toggle and opening the canvas settings in the main menu (Alt+/).
|
|
23
|
+
* `<DefaultEditor>` renders it; custom layouts can render it too.
|
|
24
|
+
*/
|
|
25
|
+
export function CanvasCommands(): ReactNode {
|
|
26
|
+
const editor = useEditor();
|
|
27
|
+
// A short confirmation after copying, e.g. "Link copied".
|
|
28
|
+
const [notice, setNotice] = useState<string | undefined>(undefined);
|
|
29
|
+
const view = useCanvasView();
|
|
30
|
+
const { t, isOpen, setOpen, isHidden } = useUi();
|
|
31
|
+
// Commands read the latest view and panel state without being re-registered. Not effect
|
|
32
|
+
// events: `canRun` is also called while rendering (e.g. by `useCommand`).
|
|
33
|
+
const latest = useLatest({ view, isOpen, setOpen, isHidden });
|
|
34
|
+
const latestView = () => latest.current.view;
|
|
35
|
+
const panels = () => latest.current;
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const exportable = (): boolean =>
|
|
39
|
+
latestView() !== undefined && editor.elements.query().length > 0;
|
|
40
|
+
const copy = (format: 'png' | 'svg'): void => {
|
|
41
|
+
const current = latestView();
|
|
42
|
+
|
|
43
|
+
if (current === undefined) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const exported = current.exportImage({
|
|
48
|
+
format,
|
|
49
|
+
bounds: editor.selection.get().length > 0 ? 'selection' : 'content',
|
|
50
|
+
padding: 10,
|
|
51
|
+
});
|
|
52
|
+
// ClipboardItem takes a promise so Safari still sees the clipboard write as part of the
|
|
53
|
+
// user gesture while the image renders.
|
|
54
|
+
const type = format === 'png' ? 'image/png' : 'text/plain';
|
|
55
|
+
const blob = exported.then((result) => {
|
|
56
|
+
if (format === 'svg') {
|
|
57
|
+
return new Blob([result.svg ?? ''], { type });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (result.blob === undefined) {
|
|
61
|
+
throw new Error('PNG export produced no image.');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return result.blob;
|
|
65
|
+
});
|
|
66
|
+
const written =
|
|
67
|
+
typeof ClipboardItem === 'function'
|
|
68
|
+
? navigator.clipboard.write([new ClipboardItem({ [type]: blob })])
|
|
69
|
+
: format === 'svg'
|
|
70
|
+
? blob.then((value) => value.text()).then((text) => navigator.clipboard.writeText(text))
|
|
71
|
+
: Promise.reject(new Error('This browser cannot copy images to the clipboard.'));
|
|
72
|
+
written.catch((error: unknown) => reportError(editor, error, `command:ui.copyAs.${format}`));
|
|
73
|
+
};
|
|
74
|
+
const clipboardAvailable = (): boolean =>
|
|
75
|
+
typeof navigator !== 'undefined' && navigator.clipboard !== undefined;
|
|
76
|
+
const offs = [
|
|
77
|
+
editor.commands.register(
|
|
78
|
+
{
|
|
79
|
+
id: COPY_AS_PNG_COMMAND,
|
|
80
|
+
label: t('action.copyAsPng'),
|
|
81
|
+
canRun: () => exportable() && clipboardAvailable() && typeof ClipboardItem === 'function',
|
|
82
|
+
run: () => copy('png'),
|
|
83
|
+
},
|
|
84
|
+
{ replace: true },
|
|
85
|
+
),
|
|
86
|
+
editor.commands.register(
|
|
87
|
+
{
|
|
88
|
+
id: COPY_AS_SVG_COMMAND,
|
|
89
|
+
label: t('action.copyAsSvg'),
|
|
90
|
+
canRun: () => exportable() && clipboardAvailable(),
|
|
91
|
+
run: () => copy('svg'),
|
|
92
|
+
},
|
|
93
|
+
{ replace: true },
|
|
94
|
+
),
|
|
95
|
+
editor.commands.register(
|
|
96
|
+
{
|
|
97
|
+
id: COPY_ELEMENT_LINK_COMMAND,
|
|
98
|
+
label: t('action.copyElementLink'),
|
|
99
|
+
canRun: () => {
|
|
100
|
+
const [id, ...rest] = editor.selection.get();
|
|
101
|
+
return (
|
|
102
|
+
id !== undefined &&
|
|
103
|
+
rest.length === 0 &&
|
|
104
|
+
clipboardAvailable() &&
|
|
105
|
+
editor.links.elementUrl(id) !== undefined
|
|
106
|
+
);
|
|
107
|
+
},
|
|
108
|
+
run: () => {
|
|
109
|
+
const [id] = editor.selection.get();
|
|
110
|
+
const url = id === undefined ? undefined : editor.links.elementUrl(id);
|
|
111
|
+
|
|
112
|
+
if (url !== undefined) {
|
|
113
|
+
navigator.clipboard
|
|
114
|
+
.writeText(url)
|
|
115
|
+
.then(() => setNotice(t('toast.linkCopied')))
|
|
116
|
+
.catch((error: unknown) =>
|
|
117
|
+
reportError(editor, error, `command:${COPY_ELEMENT_LINK_COMMAND}`),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
{ replace: true },
|
|
123
|
+
),
|
|
124
|
+
editor.commands.register(
|
|
125
|
+
{
|
|
126
|
+
id: MINIMAP_COMMAND,
|
|
127
|
+
label: t('minimap.label'),
|
|
128
|
+
canRun: () => !panels().isHidden('minimap'),
|
|
129
|
+
run: () => panels().setOpen('minimap', !panels().isOpen('minimap')),
|
|
130
|
+
},
|
|
131
|
+
{ replace: true },
|
|
132
|
+
),
|
|
133
|
+
editor.commands.register(
|
|
134
|
+
{
|
|
135
|
+
id: CANVAS_PROPERTIES_COMMAND,
|
|
136
|
+
label: t('action.canvasProperties'),
|
|
137
|
+
canRun: () => !panels().isHidden('mainMenu'),
|
|
138
|
+
// Canvas settings live in the main menu.
|
|
139
|
+
run: () => panels().setOpen('mainMenu', !panels().isOpen('mainMenu')),
|
|
140
|
+
},
|
|
141
|
+
{ replace: true },
|
|
142
|
+
),
|
|
143
|
+
editor.input.keymap.bind('Shift+Alt+C', COPY_AS_PNG_COMMAND),
|
|
144
|
+
editor.input.keymap.bind('Alt+/', CANVAS_PROPERTIES_COMMAND),
|
|
145
|
+
];
|
|
146
|
+
return () => {
|
|
147
|
+
for (const off of offs.reverse()) {
|
|
148
|
+
off();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}, [editor, t]);
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (notice === undefined) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const timer = setTimeout(() => setNotice(undefined), 2000);
|
|
159
|
+
return () => clearTimeout(timer);
|
|
160
|
+
}, [notice]);
|
|
161
|
+
|
|
162
|
+
return notice === undefined ? null : (
|
|
163
|
+
<Portal>
|
|
164
|
+
<div
|
|
165
|
+
className="nx-toast nx-toast-info"
|
|
166
|
+
role="status"
|
|
167
|
+
data-testid="ui-notice"
|
|
168
|
+
>
|
|
169
|
+
{notice}
|
|
170
|
+
</div>
|
|
171
|
+
</Portal>
|
|
172
|
+
);
|
|
173
|
+
}
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import type { AnyEditor, ElementId } from '@drawback/core';
|
|
2
|
+
import { useEditor } from '@drawback/react';
|
|
3
|
+
import {
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from 'react';
|
|
12
|
+
import { useUi, type PanelId } from '../../stores/ui-context.js';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
CANVAS_PROPERTIES_COMMAND,
|
|
16
|
+
COPY_AS_PNG_COMMAND,
|
|
17
|
+
COPY_AS_SVG_COMMAND,
|
|
18
|
+
COPY_ELEMENT_LINK_COMMAND,
|
|
19
|
+
LINK_COMMAND,
|
|
20
|
+
MINIMAP_COMMAND,
|
|
21
|
+
} from '../../lib/commands.js';
|
|
22
|
+
import { type Translate } from '../../lib/i18n.js';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
MenuItems,
|
|
26
|
+
Portal,
|
|
27
|
+
formatShortcut,
|
|
28
|
+
handleRovingKeys,
|
|
29
|
+
type MenuEntry,
|
|
30
|
+
} from '../../components/primitives.js';
|
|
31
|
+
import { byOrder, type ContextMenuContext, type ContextMenuItem } from '../../lib/registry.js';
|
|
32
|
+
|
|
33
|
+
function command(
|
|
34
|
+
id: string,
|
|
35
|
+
commandId: string,
|
|
36
|
+
label: string,
|
|
37
|
+
order: number,
|
|
38
|
+
group: string,
|
|
39
|
+
options: {
|
|
40
|
+
readonly args?: unknown;
|
|
41
|
+
readonly when?: ContextMenuItem['when'];
|
|
42
|
+
readonly danger?: boolean;
|
|
43
|
+
readonly checked?: ContextMenuItem['checked'];
|
|
44
|
+
/** For commands triggered by native events (clipboard) rather than the keymap. */
|
|
45
|
+
readonly shortcut?: string;
|
|
46
|
+
} = {},
|
|
47
|
+
): ContextMenuItem {
|
|
48
|
+
return {
|
|
49
|
+
id,
|
|
50
|
+
order,
|
|
51
|
+
group,
|
|
52
|
+
label,
|
|
53
|
+
...(options.shortcut === undefined ? {} : { shortcut: options.shortcut }),
|
|
54
|
+
...(options.danger === undefined ? {} : { danger: options.danger }),
|
|
55
|
+
...(options.when === undefined ? {} : { when: options.when }),
|
|
56
|
+
...(options.checked === undefined ? {} : { checked: options.checked }),
|
|
57
|
+
enabled: (context) =>
|
|
58
|
+
context.editor.commands.has(commandId) &&
|
|
59
|
+
context.editor.commands.canExecute(commandId, options.args),
|
|
60
|
+
run: (context) => {
|
|
61
|
+
void context.editor.commands.execute(commandId, options.args);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const onElement = (context: ContextMenuContext): boolean => context.selection.length > 0;
|
|
67
|
+
const onCanvas = (context: ContextMenuContext): boolean => context.selection.length === 0;
|
|
68
|
+
|
|
69
|
+
/** UI state the built-in toggles reflect; optional so hosts can build the list without it. */
|
|
70
|
+
export interface BuiltinContextMenuState {
|
|
71
|
+
isOpen(panel: PanelId): boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Built-in context menu entries, in Excalidraw's order; `t` resolves labels at render time.
|
|
76
|
+
* On an element: clipboard, copy as image, styles, grouping, stacking, flipping, link /
|
|
77
|
+
* duplicate / lock, delete. On the canvas: paste, copy as image, select all, view toggles.
|
|
78
|
+
*/
|
|
79
|
+
export function builtinContextMenuItems(
|
|
80
|
+
t: Translate,
|
|
81
|
+
ui?: BuiltinContextMenuState,
|
|
82
|
+
): readonly ContextMenuItem[] {
|
|
83
|
+
const snapping = (context: ContextMenuContext) => context.editor.snapping.settings();
|
|
84
|
+
return [
|
|
85
|
+
command('core.cut', 'core.cut', t('action.cut'), 100, 'clipboard', {
|
|
86
|
+
when: onElement,
|
|
87
|
+
shortcut: 'Mod+X',
|
|
88
|
+
}),
|
|
89
|
+
command('core.copy', 'core.copy', t('action.copy'), 110, 'clipboard', {
|
|
90
|
+
when: onElement,
|
|
91
|
+
shortcut: 'Mod+C',
|
|
92
|
+
}),
|
|
93
|
+
command('core.paste', 'core.paste', t('action.paste'), 120, 'clipboard', {
|
|
94
|
+
shortcut: 'Mod+V',
|
|
95
|
+
}),
|
|
96
|
+
command(COPY_AS_PNG_COMMAND, COPY_AS_PNG_COMMAND, t('action.copyAsPng'), 150, 'export'),
|
|
97
|
+
command(COPY_AS_SVG_COMMAND, COPY_AS_SVG_COMMAND, t('action.copyAsSvg'), 160, 'export'),
|
|
98
|
+
command('core.selectAll', 'core.selectAll', t('action.selectAll'), 170, 'select', {
|
|
99
|
+
when: onCanvas,
|
|
100
|
+
}),
|
|
101
|
+
command('core.style.copy', 'core.style.copy', t('action.copyStyles'), 200, 'styles', {
|
|
102
|
+
when: onElement,
|
|
103
|
+
}),
|
|
104
|
+
command('core.style.paste', 'core.style.paste', t('action.pasteStyles'), 210, 'styles', {
|
|
105
|
+
when: onElement,
|
|
106
|
+
}),
|
|
107
|
+
command('core.group', 'core.group', t('action.group'), 300, 'group', { when: onElement }),
|
|
108
|
+
command('core.ungroup', 'core.ungroup', t('action.ungroup'), 310, 'group', {
|
|
109
|
+
when: onElement,
|
|
110
|
+
}),
|
|
111
|
+
command('core.sendBackward', 'core.sendBackward', t('action.sendBackward'), 400, 'order', {
|
|
112
|
+
when: onElement,
|
|
113
|
+
}),
|
|
114
|
+
command('core.bringForward', 'core.bringForward', t('action.bringForward'), 410, 'order', {
|
|
115
|
+
when: onElement,
|
|
116
|
+
}),
|
|
117
|
+
command('core.sendToBack', 'core.sendToBack', t('action.sendToBack'), 420, 'order', {
|
|
118
|
+
when: onElement,
|
|
119
|
+
}),
|
|
120
|
+
command('core.bringToFront', 'core.bringToFront', t('action.bringToFront'), 430, 'order', {
|
|
121
|
+
when: onElement,
|
|
122
|
+
}),
|
|
123
|
+
command(
|
|
124
|
+
'core.flipHorizontal',
|
|
125
|
+
'core.flipHorizontal',
|
|
126
|
+
t('action.flipHorizontal'),
|
|
127
|
+
500,
|
|
128
|
+
'transform',
|
|
129
|
+
{ when: onElement },
|
|
130
|
+
),
|
|
131
|
+
command('core.flipVertical', 'core.flipVertical', t('action.flipVertical'), 510, 'transform', {
|
|
132
|
+
when: onElement,
|
|
133
|
+
}),
|
|
134
|
+
command(LINK_COMMAND, LINK_COMMAND, t('link.edit'), 600, 'link', {
|
|
135
|
+
when: (context) => context.selection.length === 1,
|
|
136
|
+
}),
|
|
137
|
+
command(
|
|
138
|
+
COPY_ELEMENT_LINK_COMMAND,
|
|
139
|
+
COPY_ELEMENT_LINK_COMMAND,
|
|
140
|
+
t('action.copyElementLink'),
|
|
141
|
+
605,
|
|
142
|
+
'link',
|
|
143
|
+
{ when: (context) => context.selection.length === 1 },
|
|
144
|
+
),
|
|
145
|
+
command('core.duplicate', 'core.duplicate', t('action.duplicate'), 610, 'edit', {
|
|
146
|
+
when: onElement,
|
|
147
|
+
}),
|
|
148
|
+
command('core.lock.toggle', 'core.lock.toggle', t('action.lock'), 620, 'edit', {
|
|
149
|
+
when: onElement,
|
|
150
|
+
}),
|
|
151
|
+
command('core.grid.toggle', 'core.grid.toggle', t('grid.toggle'), 700, 'view', {
|
|
152
|
+
when: onCanvas,
|
|
153
|
+
checked: (context) => context.editor.grid.visible(),
|
|
154
|
+
}),
|
|
155
|
+
command(
|
|
156
|
+
'core.snapping.objects.toggle',
|
|
157
|
+
'core.snapping.objects.toggle',
|
|
158
|
+
t('snapping.enabled'),
|
|
159
|
+
710,
|
|
160
|
+
'view',
|
|
161
|
+
{
|
|
162
|
+
when: onCanvas,
|
|
163
|
+
checked: (context) => snapping(context).enabled && snapping(context).objects,
|
|
164
|
+
},
|
|
165
|
+
),
|
|
166
|
+
command('core.bindings.toggle', 'core.bindings.toggle', t('bindings.toggle'), 720, 'view', {
|
|
167
|
+
when: onCanvas,
|
|
168
|
+
checked: (context) => context.editor.bindings.options().enabled,
|
|
169
|
+
}),
|
|
170
|
+
command(
|
|
171
|
+
'core.snapping.midpoints.toggle',
|
|
172
|
+
'core.snapping.midpoints.toggle',
|
|
173
|
+
t('snapping.midpoints'),
|
|
174
|
+
730,
|
|
175
|
+
'view',
|
|
176
|
+
{
|
|
177
|
+
when: onCanvas,
|
|
178
|
+
checked: (context) => snapping(context).enabled && snapping(context).connectionPoints,
|
|
179
|
+
},
|
|
180
|
+
),
|
|
181
|
+
command('core.zen.toggle', 'core.zen.toggle', t('zen.enter'), 740, 'view', {
|
|
182
|
+
when: onCanvas,
|
|
183
|
+
checked: (context) => context.editor.modes.presentation().zen,
|
|
184
|
+
}),
|
|
185
|
+
command('core.view.toggle', 'core.view.toggle', t('view.mode'), 750, 'view', {
|
|
186
|
+
when: onCanvas,
|
|
187
|
+
checked: (context) => context.editor.modes.current() === 'view',
|
|
188
|
+
}),
|
|
189
|
+
command(MINIMAP_COMMAND, MINIMAP_COMMAND, t('minimap.label'), 760, 'view', {
|
|
190
|
+
when: onCanvas,
|
|
191
|
+
...(ui === undefined ? {} : { checked: () => ui.isOpen('minimap') }),
|
|
192
|
+
}),
|
|
193
|
+
command(
|
|
194
|
+
CANVAS_PROPERTIES_COMMAND,
|
|
195
|
+
CANVAS_PROPERTIES_COMMAND,
|
|
196
|
+
t('action.canvasProperties'),
|
|
197
|
+
770,
|
|
198
|
+
'view',
|
|
199
|
+
{
|
|
200
|
+
when: (context) =>
|
|
201
|
+
onCanvas(context) && context.editor.commands.canExecute(CANVAS_PROPERTIES_COMMAND),
|
|
202
|
+
},
|
|
203
|
+
),
|
|
204
|
+
command('core.delete', 'core.delete', t('action.delete'), 900, 'danger', {
|
|
205
|
+
when: onElement,
|
|
206
|
+
danger: true,
|
|
207
|
+
}),
|
|
208
|
+
];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function shortcutFor(editor: AnyEditor, item: ContextMenuItem): string | undefined {
|
|
212
|
+
return item.shortcut ?? formatShortcut(editor.commands.shortcutsFor(item.id)[0]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Menu for the current context-menu request (right click on the canvas or a panel row). */
|
|
216
|
+
export function ContextMenu(): ReactNode {
|
|
217
|
+
const editor = useEditor();
|
|
218
|
+
const { t, registry, contextMenu, openContextMenu, portalContainer, isOpen } = useUi();
|
|
219
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
220
|
+
const [position, setPosition] = useState({ left: 0, top: 0 });
|
|
221
|
+
const close = (): void => openContextMenu(undefined);
|
|
222
|
+
|
|
223
|
+
const entries = useMemo((): MenuEntry[] => {
|
|
224
|
+
if (contextMenu === undefined) {
|
|
225
|
+
return [];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const element =
|
|
229
|
+
contextMenu.elementId === undefined ? undefined : editor.elements.get(contextMenu.elementId);
|
|
230
|
+
const context: ContextMenuContext = {
|
|
231
|
+
editor,
|
|
232
|
+
...(element === undefined ? {} : { element }),
|
|
233
|
+
selection: editor.selection.get(),
|
|
234
|
+
world: contextMenu.world ?? editor.viewport.screenToWorld({ x: 0, y: 0 }),
|
|
235
|
+
t,
|
|
236
|
+
};
|
|
237
|
+
const items = byOrder([
|
|
238
|
+
...builtinContextMenuItems(t, { isOpen }),
|
|
239
|
+
...registry.contextMenu.list(),
|
|
240
|
+
]).filter((item) => item.when?.(context) ?? true);
|
|
241
|
+
let previousGroup: string | undefined;
|
|
242
|
+
return items.map((item, index) => {
|
|
243
|
+
const separatorBefore = index > 0 && item.group !== previousGroup;
|
|
244
|
+
previousGroup = item.group;
|
|
245
|
+
return {
|
|
246
|
+
id: item.id,
|
|
247
|
+
label: typeof item.label === 'function' ? item.label(context) : item.label,
|
|
248
|
+
shortcut: shortcutFor(editor, item),
|
|
249
|
+
disabled: !(item.enabled?.(context) ?? true),
|
|
250
|
+
...(item.checked === undefined ? {} : { checked: item.checked(context) }),
|
|
251
|
+
...(item.danger === undefined ? {} : { danger: item.danger }),
|
|
252
|
+
separatorBefore,
|
|
253
|
+
onSelect: () => item.run(context),
|
|
254
|
+
};
|
|
255
|
+
});
|
|
256
|
+
}, [contextMenu, editor, registry, t, isOpen]);
|
|
257
|
+
|
|
258
|
+
useLayoutEffect(() => {
|
|
259
|
+
if (contextMenu === undefined || portalContainer === null || ref.current === null) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const container = portalContainer.getBoundingClientRect();
|
|
264
|
+
const menu = ref.current.getBoundingClientRect();
|
|
265
|
+
setPosition({
|
|
266
|
+
left: Math.max(
|
|
267
|
+
4,
|
|
268
|
+
Math.min(contextMenu.clientX - container.left, container.width - menu.width - 4),
|
|
269
|
+
),
|
|
270
|
+
top: Math.max(
|
|
271
|
+
4,
|
|
272
|
+
Math.min(contextMenu.clientY - container.top, container.height - menu.height - 4),
|
|
273
|
+
),
|
|
274
|
+
});
|
|
275
|
+
// Focus the menu itself, not its first item, so no item looks pre-selected; arrow keys,
|
|
276
|
+
// Home and End move into the items from here.
|
|
277
|
+
ref.current.focus({ preventScroll: true });
|
|
278
|
+
}, [contextMenu, portalContainer, entries.length]);
|
|
279
|
+
|
|
280
|
+
useEffect(() => {
|
|
281
|
+
if (contextMenu === undefined) {
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const onPointer = (event: PointerEvent): void => {
|
|
286
|
+
if (!ref.current?.contains(event.target as Node)) {
|
|
287
|
+
openContextMenu(undefined);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const onKey = (event: KeyboardEvent): void => {
|
|
291
|
+
if (event.key === 'Escape') {
|
|
292
|
+
event.stopPropagation();
|
|
293
|
+
openContextMenu(undefined);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
document.addEventListener('pointerdown', onPointer, true);
|
|
297
|
+
document.addEventListener('keydown', onKey, true);
|
|
298
|
+
return () => {
|
|
299
|
+
document.removeEventListener('pointerdown', onPointer, true);
|
|
300
|
+
document.removeEventListener('keydown', onKey, true);
|
|
301
|
+
};
|
|
302
|
+
}, [contextMenu, openContextMenu]);
|
|
303
|
+
|
|
304
|
+
if (contextMenu === undefined || entries.length === 0) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return (
|
|
309
|
+
<Portal>
|
|
310
|
+
<div
|
|
311
|
+
ref={ref}
|
|
312
|
+
className="nx-popover nx-island nx-context-menu"
|
|
313
|
+
role="menu"
|
|
314
|
+
tabIndex={-1}
|
|
315
|
+
aria-label={t('contextMenu.label')}
|
|
316
|
+
data-testid="context-menu"
|
|
317
|
+
style={position}
|
|
318
|
+
onKeyDown={(event) => handleRovingKeys(event, 'vertical')}
|
|
319
|
+
>
|
|
320
|
+
<MenuItems
|
|
321
|
+
entries={entries}
|
|
322
|
+
onDone={close}
|
|
323
|
+
/>
|
|
324
|
+
</div>
|
|
325
|
+
</Portal>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function useContextMenuForCanvas(): (info: {
|
|
330
|
+
readonly clientX: number;
|
|
331
|
+
readonly clientY: number;
|
|
332
|
+
readonly world: { readonly x: number; readonly y: number };
|
|
333
|
+
readonly elementId?: ElementId;
|
|
334
|
+
}) => void {
|
|
335
|
+
const editor = useEditor();
|
|
336
|
+
const { openContextMenu } = useUi();
|
|
337
|
+
return useCallback(
|
|
338
|
+
(info) => {
|
|
339
|
+
if (info.elementId !== undefined && !editor.selection.has(info.elementId)) {
|
|
340
|
+
editor.selection.set([info.elementId]);
|
|
341
|
+
} else if (info.elementId === undefined) {
|
|
342
|
+
editor.selection.clear();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
openContextMenu({
|
|
346
|
+
clientX: info.clientX,
|
|
347
|
+
clientY: info.clientY,
|
|
348
|
+
world: info.world,
|
|
349
|
+
...(info.elementId === undefined ? {} : { elementId: info.elementId }),
|
|
350
|
+
});
|
|
351
|
+
},
|
|
352
|
+
[editor, openContextMenu],
|
|
353
|
+
);
|
|
354
|
+
}
|