@open-slide/core 2.0.0-beta.2 → 2.0.0-beta.5
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 +1 -1
- package/dist/{build-Bzy-qlGc.js → build-Cu5_qRyE.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-nbSvY7z_.js → config-BQ6rD2iE.js} +226 -27
- package/dist/{config-DjWifhaA.d.ts → config-GMR1QUlM.d.ts} +1 -1
- package/dist/{dev-MXa5Tod4.js → dev-BK641O5e.js} +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/locale/index.d.ts +1 -1
- package/dist/locale/index.js +1 -1
- package/dist/{preview-DEpQcKz3.js → preview-BuCI-RMW.js} +1 -1
- package/dist/{types-BWg-Dtbg.d.ts → types-BcbaKTNY.d.ts} +60 -0
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/{zh-tw-DlKyeNX7.js → zh-tw-B51sMTg3.js} +267 -31
- package/package.json +5 -5
- package/src/app/components/asset-view.tsx +448 -0
- package/src/app/components/history-provider.tsx +68 -44
- package/src/app/components/icon-switcher-indicator.tsx +10 -0
- package/src/app/components/inspector/arrange-panel.tsx +449 -0
- package/src/app/components/inspector/inline-text-editor.tsx +170 -284
- package/src/app/components/inspector/inspect-overlay.tsx +642 -295
- package/src/app/components/inspector/inspector-panel.tsx +434 -427
- package/src/app/components/inspector/inspector-provider.tsx +564 -293
- package/src/app/components/inspector/save-bar.tsx +36 -0
- package/src/app/components/panel/editor-sidebar.tsx +50 -0
- package/src/app/components/panel/panel-fields.tsx +204 -16
- package/src/app/components/panel/panel-shell.tsx +3 -18
- package/src/app/components/style-panel/style-panel.tsx +4 -69
- package/src/app/lib/assets.ts +33 -0
- package/src/app/lib/inspector/geometry.test.ts +324 -0
- package/src/app/lib/inspector/geometry.ts +207 -0
- package/src/app/lib/inspector/text-edit-timeline.test.ts +121 -0
- package/src/app/lib/inspector/text-edit-timeline.ts +12 -0
- package/src/app/lib/inspector/text-selection.ts +117 -0
- package/src/app/lib/inspector/use-editor.ts +1 -1
- package/src/app/lib/inspector/use-visual-editor.ts +429 -0
- package/src/app/lib/inspector/visual-dom.ts +255 -0
- package/src/app/lib/keys.ts +9 -0
- package/src/app/lib/pptx/css.test.ts +33 -0
- package/src/app/lib/pptx/css.ts +12 -0
- package/src/app/lib/pptx/measure.ts +1 -19
- package/src/app/lib/use-document-title.test.ts +14 -0
- package/src/app/lib/use-document-title.ts +16 -0
- package/src/app/routes/presenter.tsx +2 -0
- package/src/app/routes/slide.tsx +36 -9
- package/src/locale/en.ts +70 -8
- package/src/locale/ja.ts +72 -7
- package/src/locale/types.ts +60 -0
- package/src/locale/zh-cn.ts +68 -8
- package/src/locale/zh-tw.ts +68 -8
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import {
|
|
2
|
+
collectDomTextParts,
|
|
3
|
+
type DomTextPart,
|
|
4
|
+
type InlineTextSelection,
|
|
5
|
+
} from '@/components/inspector/inspector-provider';
|
|
6
|
+
|
|
7
|
+
// Map the live DOM selection to offsets in the normalized editable text —
|
|
8
|
+
// the same coordinate space `set-text-range-style` ops use.
|
|
9
|
+
export function selectionTextOffsets(root: HTMLElement): InlineTextSelection | null {
|
|
10
|
+
const selection = window.getSelection();
|
|
11
|
+
if (!selection || selection.rangeCount === 0) return null;
|
|
12
|
+
const range = selection.getRangeAt(0);
|
|
13
|
+
if (!root.contains(range.startContainer) || !root.contains(range.endContainer)) return null;
|
|
14
|
+
const parts: DomTextPart[] = [];
|
|
15
|
+
collectDomTextParts(root, parts);
|
|
16
|
+
const start = pointToTextOffset(parts, range.startContainer, range.startOffset);
|
|
17
|
+
const end = pointToTextOffset(parts, range.endContainer, range.endOffset);
|
|
18
|
+
if (start === null || end === null) return null;
|
|
19
|
+
return start <= end ? { start, end } : { start: end, end: start };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function pointToTextOffset(parts: DomTextPart[], container: Node, offset: number): number | null {
|
|
23
|
+
const probe = document.createRange();
|
|
24
|
+
try {
|
|
25
|
+
probe.setStart(container, offset);
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
probe.collapse(true);
|
|
30
|
+
let total = 0;
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
if (part.node === container && part.node instanceof Text) {
|
|
33
|
+
const full = collapsedTextSlice(part.node, part.node.data);
|
|
34
|
+
const prefix = collapsedTextSlice(part.node, part.node.data.slice(0, offset));
|
|
35
|
+
const leading = Math.max(0, full.indexOf(part.current));
|
|
36
|
+
return total + Math.min(Math.max(prefix.length - leading, 0), part.current.length);
|
|
37
|
+
}
|
|
38
|
+
let cmp: number;
|
|
39
|
+
try {
|
|
40
|
+
cmp = probe.comparePoint(part.node, 0);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if (cmp > 0) break;
|
|
45
|
+
if (cmp < 0) {
|
|
46
|
+
total += part.current.length;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
return total;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function collapsedTextSlice(node: Text, value: string): string {
|
|
55
|
+
const whiteSpace = node.parentElement ? getComputedStyle(node.parentElement).whiteSpace : '';
|
|
56
|
+
if (whiteSpace === 'pre' || whiteSpace === 'pre-wrap' || whiteSpace === 'break-spaces') {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
return value.replace(/\s+/g, ' ');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function styleContext(
|
|
63
|
+
anchor: HTMLElement,
|
|
64
|
+
selection: InlineTextSelection | null,
|
|
65
|
+
): HTMLElement {
|
|
66
|
+
if (!selection) return anchor;
|
|
67
|
+
const parts: DomTextPart[] = [];
|
|
68
|
+
collectDomTextParts(anchor, parts);
|
|
69
|
+
let offset = 0;
|
|
70
|
+
for (const part of parts) {
|
|
71
|
+
if (selection.start < offset + part.current.length) return part.node.parentElement ?? anchor;
|
|
72
|
+
offset += part.current.length;
|
|
73
|
+
}
|
|
74
|
+
return anchor;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function pointAtOffset(root: HTMLElement, offset: number): { node: Node; offset: number } {
|
|
78
|
+
const parts: DomTextPart[] = [];
|
|
79
|
+
collectDomTextParts(root, parts);
|
|
80
|
+
let remaining = Math.max(0, offset);
|
|
81
|
+
for (const part of parts) {
|
|
82
|
+
if (remaining > part.current.length) {
|
|
83
|
+
remaining -= part.current.length;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (part.node instanceof Text) {
|
|
87
|
+
const full = collapsedTextSlice(part.node, part.node.data);
|
|
88
|
+
const normalized = remaining + Math.max(0, full.indexOf(part.current));
|
|
89
|
+
for (let index = 0; index <= part.node.length; index++) {
|
|
90
|
+
if (collapsedTextSlice(part.node, part.node.data.slice(0, index)).length >= normalized) {
|
|
91
|
+
return { node: part.node, offset: index };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { node: part.node, offset: part.node.length };
|
|
95
|
+
}
|
|
96
|
+
const parent = part.node.parentNode;
|
|
97
|
+
if (parent)
|
|
98
|
+
return {
|
|
99
|
+
node: parent,
|
|
100
|
+
offset: Array.from(parent.childNodes).indexOf(part.node) + (remaining > 0 ? 1 : 0),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return { node: root, offset: root.childNodes.length };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function restoreTextSelection(root: HTMLElement, offsets: InlineTextSelection) {
|
|
107
|
+
if (!root.isConnected) return;
|
|
108
|
+
const selection = window.getSelection();
|
|
109
|
+
if (!selection) return;
|
|
110
|
+
const start = pointAtOffset(root, offsets.start);
|
|
111
|
+
const end = pointAtOffset(root, offsets.end);
|
|
112
|
+
const range = document.createRange();
|
|
113
|
+
range.setStart(start.node, start.offset);
|
|
114
|
+
range.setEnd(end.node, end.offset);
|
|
115
|
+
selection.removeAllRanges();
|
|
116
|
+
selection.addRange(range);
|
|
117
|
+
}
|
|
@@ -14,7 +14,7 @@ export type EditOp =
|
|
|
14
14
|
| { kind: 'set-attr-asset'; attr: string; assetPath: string; previewUrl: string }
|
|
15
15
|
| { kind: 'replace-placeholder-with-image'; assetPath: string };
|
|
16
16
|
|
|
17
|
-
export type Edit = { line: number; column: number; ops: EditOp[] };
|
|
17
|
+
export type Edit = { line: number; column: number; ops: EditOp[]; dependsOn?: number };
|
|
18
18
|
|
|
19
19
|
export type EditResult = { ok: boolean; error?: string };
|
|
20
20
|
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { toast } from 'sonner';
|
|
3
|
+
import type { SelectedTarget } from '@/components/inspector/inspector-provider';
|
|
4
|
+
import { isTypingTarget } from '@/lib/keys';
|
|
5
|
+
import { useLocale } from '@/lib/use-locale';
|
|
6
|
+
import { type Alignment, alignRects, distributeRects, unionRects } from './geometry';
|
|
7
|
+
import type { EditOp } from './use-editor';
|
|
8
|
+
import {
|
|
9
|
+
type Canvas,
|
|
10
|
+
canTransform,
|
|
11
|
+
captureTransform,
|
|
12
|
+
editableTargets,
|
|
13
|
+
independentTargets,
|
|
14
|
+
moveOps,
|
|
15
|
+
previewOps,
|
|
16
|
+
readCanvas,
|
|
17
|
+
readFrame,
|
|
18
|
+
restoreTransform,
|
|
19
|
+
sizeOps,
|
|
20
|
+
styleOp,
|
|
21
|
+
} from './visual-dom';
|
|
22
|
+
|
|
23
|
+
export type VisualEdit = SelectedTarget & { ops: EditOp[] };
|
|
24
|
+
export type FramePatch = {
|
|
25
|
+
x?: number;
|
|
26
|
+
y?: number;
|
|
27
|
+
width?: number;
|
|
28
|
+
height?: number;
|
|
29
|
+
rotation?: number;
|
|
30
|
+
};
|
|
31
|
+
export type ArrangeDirection = 'front' | 'back' | 'forward' | 'backward';
|
|
32
|
+
|
|
33
|
+
function isFlexOrGridItem(node: HTMLElement): boolean {
|
|
34
|
+
const display = node.parentElement ? getComputedStyle(node.parentElement).display : '';
|
|
35
|
+
return display.includes('flex') || display.includes('grid');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function stackLevel(node: HTMLElement): number {
|
|
39
|
+
const style = getComputedStyle(node);
|
|
40
|
+
return style.position !== 'static' || isFlexOrGridItem(node)
|
|
41
|
+
? Number.parseInt(style.zIndex, 10) || 0
|
|
42
|
+
: 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function layerOps(node: HTMLElement, level: number): EditOp[] {
|
|
46
|
+
if (stackLevel(node) === level) return [];
|
|
47
|
+
const position = getComputedStyle(node).position;
|
|
48
|
+
return [
|
|
49
|
+
...(position === 'static' && !isFlexOrGridItem(node)
|
|
50
|
+
? [
|
|
51
|
+
styleOp('position', 'relative'),
|
|
52
|
+
...[
|
|
53
|
+
'inset',
|
|
54
|
+
'insetInline',
|
|
55
|
+
'insetBlock',
|
|
56
|
+
'insetInlineStart',
|
|
57
|
+
'insetInlineEnd',
|
|
58
|
+
'insetBlockStart',
|
|
59
|
+
'insetBlockEnd',
|
|
60
|
+
'top',
|
|
61
|
+
'right',
|
|
62
|
+
'bottom',
|
|
63
|
+
'left',
|
|
64
|
+
].map((key) => styleOp(key, 'auto')),
|
|
65
|
+
]
|
|
66
|
+
: []),
|
|
67
|
+
styleOp('zIndex', String(level)),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function preserveLayerLayout(
|
|
72
|
+
edits: VisualEdit[],
|
|
73
|
+
sourceTargets: Map<HTMLElement, SelectedTarget>,
|
|
74
|
+
canvas: Canvas,
|
|
75
|
+
): VisualEdit[] | null {
|
|
76
|
+
const descendants = new Set<HTMLElement>();
|
|
77
|
+
for (const edit of edits) {
|
|
78
|
+
if (!edit.ops.some((op) => op.kind === 'set-style' && op.key === 'position')) continue;
|
|
79
|
+
for (const node of edit.anchor.querySelectorAll<HTMLElement>('*')) {
|
|
80
|
+
if (getComputedStyle(node).position !== 'absolute') continue;
|
|
81
|
+
if (node.offsetParent && edit.anchor.contains(node.offsetParent)) continue;
|
|
82
|
+
if (!sourceTargets.has(node)) return null;
|
|
83
|
+
descendants.add(node);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!descendants.size) return edits;
|
|
87
|
+
const snapshots = [...descendants].map((node) =>
|
|
88
|
+
captureTransform(sourceTargets.get(node) as SelectedTarget, canvas),
|
|
89
|
+
);
|
|
90
|
+
const originals = edits.map((edit) => captureTransform(edit, canvas));
|
|
91
|
+
try {
|
|
92
|
+
for (const edit of edits) previewOps(edit.anchor, edit.ops);
|
|
93
|
+
const compensated = snapshots.flatMap((original) => {
|
|
94
|
+
const current = captureTransform(original.target, canvas);
|
|
95
|
+
const dx = original.frame.x - current.frame.x;
|
|
96
|
+
const dy = original.frame.y - current.frame.y;
|
|
97
|
+
const resized =
|
|
98
|
+
Math.abs(original.frame.width - current.frame.width) > 0.01 ||
|
|
99
|
+
Math.abs(original.frame.height - current.frame.height) > 0.01;
|
|
100
|
+
if (!resized && Math.abs(dx) < 0.01 && Math.abs(dy) < 0.01) return [];
|
|
101
|
+
const dimensions = resized
|
|
102
|
+
? [
|
|
103
|
+
styleOp('minWidth', '0px'),
|
|
104
|
+
styleOp('minHeight', '0px'),
|
|
105
|
+
styleOp('maxWidth', 'none'),
|
|
106
|
+
styleOp('maxHeight', 'none'),
|
|
107
|
+
styleOp('width', `${original.width}px`),
|
|
108
|
+
styleOp('height', `${original.height}px`),
|
|
109
|
+
]
|
|
110
|
+
: [];
|
|
111
|
+
previewOps(original.target.anchor, dimensions);
|
|
112
|
+
const positioned = resized ? captureTransform(original.target, canvas) : current;
|
|
113
|
+
const ops = [
|
|
114
|
+
...dimensions,
|
|
115
|
+
...moveOps(
|
|
116
|
+
positioned,
|
|
117
|
+
{
|
|
118
|
+
x: original.frame.x - positioned.frame.x,
|
|
119
|
+
y: original.frame.y - positioned.frame.y,
|
|
120
|
+
},
|
|
121
|
+
canvas,
|
|
122
|
+
),
|
|
123
|
+
];
|
|
124
|
+
previewOps(original.target.anchor, ops);
|
|
125
|
+
return [{ ...original.target, ops }];
|
|
126
|
+
});
|
|
127
|
+
return [...edits, ...compensated];
|
|
128
|
+
} finally {
|
|
129
|
+
for (const snapshot of [...snapshots, ...originals]) restoreTransform(snapshot);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
type Options = {
|
|
134
|
+
active: boolean;
|
|
135
|
+
inlineEditing: boolean;
|
|
136
|
+
committing: boolean;
|
|
137
|
+
slideId: string;
|
|
138
|
+
selection: SelectedTarget[];
|
|
139
|
+
setSelection: (targets: SelectedTarget[]) => void;
|
|
140
|
+
bufferBatch: (edits: VisualEdit[], coalesceKey?: string) => void;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export function useVisualEditor({
|
|
144
|
+
active,
|
|
145
|
+
inlineEditing,
|
|
146
|
+
committing,
|
|
147
|
+
slideId,
|
|
148
|
+
selection,
|
|
149
|
+
setSelection,
|
|
150
|
+
bufferBatch,
|
|
151
|
+
}: Options) {
|
|
152
|
+
const [snapping, setSnapping] = useState(true);
|
|
153
|
+
const t = useLocale();
|
|
154
|
+
const move = useCallback(
|
|
155
|
+
(deltas: { x: number; y: number }[], coalesceKey?: string) => {
|
|
156
|
+
const canvas = readCanvas();
|
|
157
|
+
if (!canvas || committing || selection.some((target) => !canTransform(target, canvas)))
|
|
158
|
+
return;
|
|
159
|
+
const targets = independentTargets(selection);
|
|
160
|
+
if (
|
|
161
|
+
targets.some(
|
|
162
|
+
(_, index) =>
|
|
163
|
+
!deltas[index] ||
|
|
164
|
+
!Number.isFinite(deltas[index].x) ||
|
|
165
|
+
!Number.isFinite(deltas[index].y),
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
return;
|
|
169
|
+
const edits = targets.flatMap((target, index) => {
|
|
170
|
+
const delta = deltas[index];
|
|
171
|
+
if (!delta.x && !delta.y) return [];
|
|
172
|
+
return [{ ...target, ops: moveOps(captureTransform(target, canvas), delta, canvas) }];
|
|
173
|
+
});
|
|
174
|
+
bufferBatch(edits, coalesceKey);
|
|
175
|
+
},
|
|
176
|
+
[selection, committing, bufferBatch],
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const align = useCallback(
|
|
180
|
+
(alignment: Alignment, toSlide: boolean) => {
|
|
181
|
+
const canvas = readCanvas();
|
|
182
|
+
if (!canvas) return;
|
|
183
|
+
const rects = independentTargets(selection).map((target) => readFrame(target.anchor, canvas));
|
|
184
|
+
move(
|
|
185
|
+
alignRects(
|
|
186
|
+
rects,
|
|
187
|
+
alignment,
|
|
188
|
+
toSlide || rects.length === 1
|
|
189
|
+
? { x: 0, y: 0, width: canvas.width, height: canvas.height }
|
|
190
|
+
: undefined,
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
},
|
|
194
|
+
[selection, move],
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const distribute = useCallback(
|
|
198
|
+
(axis: 'x' | 'y') => {
|
|
199
|
+
const canvas = readCanvas();
|
|
200
|
+
if (!canvas) return;
|
|
201
|
+
move(
|
|
202
|
+
distributeRects(
|
|
203
|
+
independentTargets(selection).map((target) => readFrame(target.anchor, canvas)),
|
|
204
|
+
axis,
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
},
|
|
208
|
+
[selection, move],
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const setFrame = useCallback(
|
|
212
|
+
(patch: FramePatch) => {
|
|
213
|
+
if (
|
|
214
|
+
!Object.keys(patch).length ||
|
|
215
|
+
Object.values(patch).some((value) => !Number.isFinite(value)) ||
|
|
216
|
+
committing
|
|
217
|
+
)
|
|
218
|
+
return;
|
|
219
|
+
const canvas = readCanvas();
|
|
220
|
+
const targets = independentTargets(selection);
|
|
221
|
+
if (!canvas || !targets.length || selection.some((target) => !canTransform(target, canvas)))
|
|
222
|
+
return;
|
|
223
|
+
const snapshots = targets.map((target) => captureTransform(target, canvas));
|
|
224
|
+
const bounds = unionRects(snapshots.map((snapshot) => snapshot.frame));
|
|
225
|
+
if (!bounds) return;
|
|
226
|
+
if (snapshots.length > 1) {
|
|
227
|
+
if (patch.width !== undefined || patch.height !== undefined || patch.rotation !== undefined)
|
|
228
|
+
return;
|
|
229
|
+
move(
|
|
230
|
+
targets.map(() => ({
|
|
231
|
+
x: (patch.x ?? bounds.x) - bounds.x,
|
|
232
|
+
y: (patch.y ?? bounds.y) - bounds.y,
|
|
233
|
+
})),
|
|
234
|
+
);
|
|
235
|
+
} else {
|
|
236
|
+
const snapshot = snapshots[0];
|
|
237
|
+
const ops =
|
|
238
|
+
patch.width !== undefined || patch.height !== undefined
|
|
239
|
+
? sizeOps(
|
|
240
|
+
snapshot,
|
|
241
|
+
{
|
|
242
|
+
...snapshot.frame,
|
|
243
|
+
x: patch.x ?? snapshot.frame.x,
|
|
244
|
+
y: patch.y ?? snapshot.frame.y,
|
|
245
|
+
width: Math.max(8, patch.width ?? snapshot.frame.width),
|
|
246
|
+
height: Math.max(8, patch.height ?? snapshot.frame.height),
|
|
247
|
+
},
|
|
248
|
+
canvas,
|
|
249
|
+
)
|
|
250
|
+
: patch.x !== undefined || patch.y !== undefined
|
|
251
|
+
? moveOps(
|
|
252
|
+
snapshot,
|
|
253
|
+
{ x: (patch.x ?? bounds.x) - bounds.x, y: (patch.y ?? bounds.y) - bounds.y },
|
|
254
|
+
canvas,
|
|
255
|
+
)
|
|
256
|
+
: [];
|
|
257
|
+
if (patch.rotation !== undefined) ops.push(styleOp('rotate', `${patch.rotation}deg`));
|
|
258
|
+
restoreTransform(snapshot);
|
|
259
|
+
bufferBatch([{ ...snapshot.target, ops }]);
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
[selection, move, bufferBatch, committing],
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const arrange = useCallback(
|
|
266
|
+
(direction: ArrangeDirection) => {
|
|
267
|
+
if (committing) return;
|
|
268
|
+
const canvas = readCanvas();
|
|
269
|
+
if (!canvas || selection.some((target) => !canTransform(target, canvas))) return;
|
|
270
|
+
const targets = independentTargets(selection);
|
|
271
|
+
const sourceTargets = new Map(
|
|
272
|
+
editableTargets(canvas, slideId)
|
|
273
|
+
.filter((target) => canTransform(target, canvas))
|
|
274
|
+
.map((target) => [target.anchor, target]),
|
|
275
|
+
);
|
|
276
|
+
const selected = new Set(targets.map((target) => target.anchor));
|
|
277
|
+
const parents = new Set(targets.map((target) => target.anchor.parentElement));
|
|
278
|
+
const edits: VisualEdit[] = [];
|
|
279
|
+
for (const parent of parents) {
|
|
280
|
+
if (!parent) continue;
|
|
281
|
+
const siblings = Array.from(parent.children).filter(
|
|
282
|
+
(node): node is HTMLElement => node instanceof HTMLElement,
|
|
283
|
+
);
|
|
284
|
+
const ordered = siblings
|
|
285
|
+
.map((node, index) => ({ node, index, level: stackLevel(node) }))
|
|
286
|
+
.sort((a, b) => a.level - b.level || a.index - b.index);
|
|
287
|
+
if (siblings.every((node) => sourceTargets.has(node))) {
|
|
288
|
+
const next = ordered.map(({ node }) => node);
|
|
289
|
+
if (direction === 'front' || direction === 'back') {
|
|
290
|
+
next.sort(
|
|
291
|
+
(a, b) =>
|
|
292
|
+
(Number(selected.has(a)) - Number(selected.has(b))) *
|
|
293
|
+
(direction === 'front' ? 1 : -1),
|
|
294
|
+
);
|
|
295
|
+
} else if (direction === 'forward') {
|
|
296
|
+
for (let index = next.length - 2; index >= 0; index--) {
|
|
297
|
+
if (selected.has(next[index]) && !selected.has(next[index + 1]))
|
|
298
|
+
[next[index], next[index + 1]] = [next[index + 1], next[index]];
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
for (let index = 1; index < next.length; index++) {
|
|
302
|
+
if (selected.has(next[index]) && !selected.has(next[index - 1]))
|
|
303
|
+
[next[index - 1], next[index]] = [next[index], next[index - 1]];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (next.every((node, index) => node === ordered[index].node)) continue;
|
|
307
|
+
const base = Math.min(0, ...ordered.map(({ level }) => level));
|
|
308
|
+
for (const [index, node] of next.entries()) {
|
|
309
|
+
const target = sourceTargets.get(node);
|
|
310
|
+
if (target) edits.push({ ...target, ops: layerOps(node, base + index) });
|
|
311
|
+
}
|
|
312
|
+
} else {
|
|
313
|
+
for (const [index, { node, level }] of ordered.entries()) {
|
|
314
|
+
const target = sourceTargets.get(node);
|
|
315
|
+
if (!selected.has(node) || !target) continue;
|
|
316
|
+
const nextLevel =
|
|
317
|
+
direction === 'front'
|
|
318
|
+
? ordered[ordered.length - 1].level + 1
|
|
319
|
+
: direction === 'back'
|
|
320
|
+
? ordered[0].level - 1
|
|
321
|
+
: direction === 'forward'
|
|
322
|
+
? (ordered[index + 1]?.level ?? level) + 1
|
|
323
|
+
: (ordered[index - 1]?.level ?? level) - 1;
|
|
324
|
+
edits.push({ ...target, ops: layerOps(node, nextLevel) });
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const preserved = preserveLayerLayout(
|
|
329
|
+
edits.filter((edit) => edit.ops.length > 0),
|
|
330
|
+
sourceTargets,
|
|
331
|
+
canvas,
|
|
332
|
+
);
|
|
333
|
+
if (preserved) bufferBatch(preserved);
|
|
334
|
+
else toast.error(t.inspector.layerLayoutHint);
|
|
335
|
+
},
|
|
336
|
+
[selection, committing, bufferBatch, slideId, t.inspector.layerLayoutHint],
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
const selectParent = useCallback(() => {
|
|
340
|
+
const canvas = readCanvas();
|
|
341
|
+
const anchor = selection.at(-1)?.anchor.parentElement;
|
|
342
|
+
if (!canvas || !anchor) return;
|
|
343
|
+
const targets = editableTargets(canvas, slideId);
|
|
344
|
+
for (
|
|
345
|
+
let node: HTMLElement | null = anchor;
|
|
346
|
+
node && node !== canvas.root;
|
|
347
|
+
node = node.parentElement
|
|
348
|
+
) {
|
|
349
|
+
const target = targets.find((target) => target.anchor === node);
|
|
350
|
+
if (target) {
|
|
351
|
+
setSelection([target]);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}, [selection, slideId, setSelection]);
|
|
356
|
+
|
|
357
|
+
const selectAll = useCallback(() => {
|
|
358
|
+
const canvas = readCanvas();
|
|
359
|
+
if (!canvas) return;
|
|
360
|
+
const targets = editableTargets(canvas, slideId).filter((target) =>
|
|
361
|
+
canTransform(target, canvas),
|
|
362
|
+
);
|
|
363
|
+
// Full-slide wrappers are layout scaffolding; select the first independent objects inside them.
|
|
364
|
+
const objects = targets.filter((target) => {
|
|
365
|
+
const frame = readFrame(target.anchor, canvas);
|
|
366
|
+
return frame.width < canvas.width - 1 || frame.height < canvas.height - 1;
|
|
367
|
+
});
|
|
368
|
+
setSelection(independentTargets(objects));
|
|
369
|
+
}, [slideId, setSelection]);
|
|
370
|
+
|
|
371
|
+
useEffect(() => {
|
|
372
|
+
if (!active || inlineEditing || committing) return;
|
|
373
|
+
const onKey = (event: KeyboardEvent) => {
|
|
374
|
+
if (
|
|
375
|
+
event.defaultPrevented ||
|
|
376
|
+
event.isComposing ||
|
|
377
|
+
event.keyCode === 229 ||
|
|
378
|
+
isTypingTarget(event.target) ||
|
|
379
|
+
document.querySelector('[data-visual-gesture]')
|
|
380
|
+
)
|
|
381
|
+
return;
|
|
382
|
+
const target = event.target;
|
|
383
|
+
if (
|
|
384
|
+
target instanceof Element &&
|
|
385
|
+
(target.closest('[role="dialog"], [role="menu"], [role="listbox"]') ||
|
|
386
|
+
target.closest('[data-inspector-ui]'))
|
|
387
|
+
)
|
|
388
|
+
return;
|
|
389
|
+
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'a') {
|
|
390
|
+
event.preventDefault();
|
|
391
|
+
event.stopImmediatePropagation();
|
|
392
|
+
selectAll();
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (!selection.length || event.metaKey || event.ctrlKey || event.altKey) return;
|
|
396
|
+
const vectors: Record<string, { x: number; y: number }> = {
|
|
397
|
+
ArrowLeft: { x: -1, y: 0 },
|
|
398
|
+
ArrowRight: { x: 1, y: 0 },
|
|
399
|
+
ArrowUp: { x: 0, y: -1 },
|
|
400
|
+
ArrowDown: { x: 0, y: 1 },
|
|
401
|
+
};
|
|
402
|
+
const vector = vectors[event.key];
|
|
403
|
+
if (!vector) return;
|
|
404
|
+
event.preventDefault();
|
|
405
|
+
event.stopImmediatePropagation();
|
|
406
|
+
const step = event.shiftKey ? 10 : 1;
|
|
407
|
+
move(
|
|
408
|
+
selection.map(() => ({ x: vector.x * step, y: vector.y * step })),
|
|
409
|
+
`nudge:${selection.map((target) => `${target.line}:${target.column}`).join(',')}`,
|
|
410
|
+
);
|
|
411
|
+
};
|
|
412
|
+
window.addEventListener('keydown', onKey, true);
|
|
413
|
+
return () => window.removeEventListener('keydown', onKey, true);
|
|
414
|
+
}, [active, inlineEditing, committing, selection, move, selectAll]);
|
|
415
|
+
|
|
416
|
+
return useMemo(
|
|
417
|
+
() => ({
|
|
418
|
+
snapping,
|
|
419
|
+
setSnapping,
|
|
420
|
+
align,
|
|
421
|
+
distribute,
|
|
422
|
+
setFrame,
|
|
423
|
+
arrange,
|
|
424
|
+
selectParent,
|
|
425
|
+
selectAll,
|
|
426
|
+
}),
|
|
427
|
+
[snapping, align, distribute, setFrame, arrange, selectParent, selectAll],
|
|
428
|
+
);
|
|
429
|
+
}
|