@open-slide/core 2.0.0-beta.3 → 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 +4 -4
- 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/routes/slide.tsx +34 -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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AlignCenter, AlignLeft, AlignRight, Bold, Italic, Minus, Plus } from 'lucide-react';
|
|
2
2
|
import { type RefObject, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
import { useHistory } from '@/components/history-provider';
|
|
3
4
|
import { findSlideSource } from '@/lib/inspector/fiber';
|
|
4
5
|
import {
|
|
5
6
|
isEditableTextContainer,
|
|
@@ -7,20 +8,21 @@ import {
|
|
|
7
8
|
pickElement,
|
|
8
9
|
pickInspectorTarget,
|
|
9
10
|
} from '@/lib/inspector/pick-target';
|
|
11
|
+
import {
|
|
12
|
+
restoreTextSelection,
|
|
13
|
+
selectionTextOffsets,
|
|
14
|
+
styleContext,
|
|
15
|
+
} from '@/lib/inspector/text-selection';
|
|
16
|
+
import type { EditOp } from '@/lib/inspector/use-editor';
|
|
17
|
+
import { isTypingTarget } from '@/lib/keys';
|
|
10
18
|
import { useLocale } from '@/lib/use-locale';
|
|
11
19
|
import { cn } from '@/lib/utils';
|
|
12
|
-
import {
|
|
13
|
-
collectDomTextParts,
|
|
14
|
-
type DomTextPart,
|
|
15
|
-
type InlineEditTarget,
|
|
16
|
-
readEditableText,
|
|
17
|
-
useInspector,
|
|
18
|
-
} from './inspector-provider';
|
|
20
|
+
import { type InlineEditTarget, readEditableText, useInspector } from './inspector-provider';
|
|
19
21
|
|
|
20
22
|
type RelRect = { left: number; top: number; width: number; height: number };
|
|
21
23
|
type TextRange = { start: number; end: number };
|
|
22
24
|
|
|
23
|
-
const RANGE_STYLE_KEYS = new Set(['fontSize', 'fontWeight', 'fontStyle', 'color']);
|
|
25
|
+
const RANGE_STYLE_KEYS = new Set(['fontSize', 'fontWeight', 'fontStyle', 'fontFamily', 'color']);
|
|
24
26
|
const TOOLBAR_GAP = 8;
|
|
25
27
|
const TOOLBAR_HEIGHT = 36;
|
|
26
28
|
|
|
@@ -39,87 +41,78 @@ function pickEditableAnchor(
|
|
|
39
41
|
return hit;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
// Click-to-edit lives outside the inspector: it works in the plain slide
|
|
43
|
-
// view, without activating inspect mode or opening the panel.
|
|
44
44
|
export function InlineEditLayer() {
|
|
45
|
-
const { slideId, active, inlineEdit, startInlineEdit, stopInlineEdit } =
|
|
45
|
+
const { slideId, active, inlineEdit, selected, selection, startInlineEdit, stopInlineEdit } =
|
|
46
|
+
useInspector();
|
|
46
47
|
const layerRef = useRef<HTMLDivElement>(null);
|
|
47
48
|
|
|
48
|
-
// Plain view: a single click on a text run starts editing with the caret
|
|
49
|
-
// at the click point. A double-click's second click lands on the
|
|
50
|
-
// now-contenteditable element, so native word selection still happens.
|
|
51
49
|
useEffect(() => {
|
|
52
|
-
if (
|
|
53
|
-
const
|
|
54
|
-
if (inlineEdit?.anchor.contains(
|
|
55
|
-
if (!isInspectableEventTarget(
|
|
56
|
-
const hit = pickEditableAnchor(
|
|
50
|
+
if (!active) return;
|
|
51
|
+
const onDblClick = (event: MouseEvent) => {
|
|
52
|
+
if (inlineEdit?.anchor.contains(event.target as Node)) return;
|
|
53
|
+
if (!isInspectableEventTarget(event.target)) return;
|
|
54
|
+
const hit = pickEditableAnchor(event.clientX, event.clientY, slideId);
|
|
57
55
|
if (!hit) return;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
startInlineEdit({
|
|
61
|
-
line: hit.line,
|
|
62
|
-
column: hit.column,
|
|
63
|
-
anchor: hit.anchor,
|
|
64
|
-
point: { x: e.clientX, y: e.clientY },
|
|
65
|
-
selectWord: false,
|
|
66
|
-
});
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
event.stopImmediatePropagation();
|
|
58
|
+
startInlineEdit({ ...hit, point: { x: event.clientX, y: event.clientY }, selectWord: true });
|
|
67
59
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
60
|
+
const onKey = (event: KeyboardEvent) => {
|
|
61
|
+
if (
|
|
62
|
+
event.key !== 'Enter' ||
|
|
63
|
+
event.isComposing ||
|
|
64
|
+
event.keyCode === 229 ||
|
|
65
|
+
inlineEdit ||
|
|
66
|
+
!selected ||
|
|
67
|
+
selection.length !== 1
|
|
68
|
+
)
|
|
69
|
+
return;
|
|
70
|
+
if (
|
|
71
|
+
event.metaKey ||
|
|
72
|
+
event.ctrlKey ||
|
|
73
|
+
event.altKey ||
|
|
74
|
+
event.shiftKey ||
|
|
75
|
+
isTypingTarget(event.target)
|
|
76
|
+
)
|
|
77
|
+
return;
|
|
78
|
+
if (
|
|
79
|
+
event.target instanceof Element &&
|
|
80
|
+
event.target.closest(
|
|
81
|
+
'[data-inspector-ui], [role="dialog"], [role="menu"], [role="listbox"], button, a',
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
return;
|
|
85
|
+
if (!isEditableTextContainer(selected.anchor)) return;
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
event.stopImmediatePropagation();
|
|
88
|
+
startInlineEdit(selected);
|
|
90
89
|
};
|
|
91
90
|
window.addEventListener('dblclick', onDblClick, true);
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
window.addEventListener('keydown', onKey, true);
|
|
92
|
+
return () => {
|
|
93
|
+
window.removeEventListener('dblclick', onDblClick, true);
|
|
94
|
+
window.removeEventListener('keydown', onKey, true);
|
|
95
|
+
};
|
|
96
|
+
}, [active, slideId, inlineEdit, selected, selection.length, startInlineEdit]);
|
|
94
97
|
|
|
95
98
|
useEffect(() => {
|
|
96
99
|
if (!inlineEdit) return;
|
|
97
100
|
const { anchor } = inlineEdit;
|
|
98
|
-
const onPointerDown = (
|
|
99
|
-
if (anchor.contains(
|
|
100
|
-
if (e.target instanceof Element && e.target.closest('[data-inspector-ui]')) return;
|
|
101
|
-
// In the plain view a single click on another text run switches the
|
|
102
|
-
// session to it; in inspect mode a click means "select", so just exit.
|
|
103
|
-
if (!active && isInspectableEventTarget(e.target)) {
|
|
104
|
-
const hit = pickEditableAnchor(e.clientX, e.clientY, slideId);
|
|
105
|
-
if (hit) {
|
|
106
|
-
e.preventDefault();
|
|
107
|
-
startInlineEdit({
|
|
108
|
-
line: hit.line,
|
|
109
|
-
column: hit.column,
|
|
110
|
-
anchor: hit.anchor,
|
|
111
|
-
point: { x: e.clientX, y: e.clientY },
|
|
112
|
-
selectWord: false,
|
|
113
|
-
});
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
101
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
102
|
+
if (anchor.contains(event.target as Node) || !isInspectableEventTarget(event.target)) return;
|
|
117
103
|
stopInlineEdit();
|
|
118
104
|
};
|
|
119
|
-
const onKey = (
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
105
|
+
const onKey = (event: KeyboardEvent) => {
|
|
106
|
+
if (event.key !== 'Escape' || event.isComposing || event.keyCode === 229) return;
|
|
107
|
+
if (
|
|
108
|
+
event.target instanceof Element &&
|
|
109
|
+
event.target.closest(
|
|
110
|
+
'[data-inspector-ui], [role="dialog"], [role="menu"], [role="listbox"]',
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
return;
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
event.stopImmediatePropagation();
|
|
123
116
|
stopInlineEdit();
|
|
124
117
|
};
|
|
125
118
|
window.addEventListener('pointerdown', onPointerDown, true);
|
|
@@ -128,69 +121,7 @@ export function InlineEditLayer() {
|
|
|
128
121
|
window.removeEventListener('pointerdown', onPointerDown, true);
|
|
129
122
|
window.removeEventListener('keydown', onKey, true);
|
|
130
123
|
};
|
|
131
|
-
}, [inlineEdit,
|
|
132
|
-
|
|
133
|
-
// Hovering an editable text run in the plain view shows a text cursor and
|
|
134
|
-
// a light outline — the discoverability cue for click-to-edit.
|
|
135
|
-
useEffect(() => {
|
|
136
|
-
if (import.meta.env.PROD || active) return;
|
|
137
|
-
const styleEl = document.createElement('style');
|
|
138
|
-
styleEl.textContent = HOVER_HINT_CSS;
|
|
139
|
-
document.head.appendChild(styleEl);
|
|
140
|
-
let hovered: HTMLElement | null = null;
|
|
141
|
-
let raf = 0;
|
|
142
|
-
const clear = () => {
|
|
143
|
-
const el = hovered;
|
|
144
|
-
hovered = null;
|
|
145
|
-
if (!el) return;
|
|
146
|
-
if (el.getAttribute(TEXT_HOVER_ATTR) !== 'true') {
|
|
147
|
-
el.removeAttribute(TEXT_HOVER_ATTR);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
el.setAttribute(TEXT_HOVER_ATTR, 'out');
|
|
151
|
-
window.setTimeout(() => {
|
|
152
|
-
// Re-entering the element mid-fade flips the value back to 'true'.
|
|
153
|
-
if (el.getAttribute(TEXT_HOVER_ATTR) === 'out') el.removeAttribute(TEXT_HOVER_ATTR);
|
|
154
|
-
}, HOVER_FADE_MS + 40);
|
|
155
|
-
};
|
|
156
|
-
const onMove = (e: PointerEvent) => {
|
|
157
|
-
const { clientX, clientY, target } = e;
|
|
158
|
-
cancelAnimationFrame(raf);
|
|
159
|
-
raf = requestAnimationFrame(() => {
|
|
160
|
-
const hit = isInspectableEventTarget(target)
|
|
161
|
-
? pickEditableAnchor(clientX, clientY, slideId)
|
|
162
|
-
: null;
|
|
163
|
-
const anchor =
|
|
164
|
-
hit && hit.anchor.getAttribute('contenteditable') !== 'true' ? hit.anchor : null;
|
|
165
|
-
if (anchor === hovered) return;
|
|
166
|
-
clear();
|
|
167
|
-
if (anchor) {
|
|
168
|
-
hovered = anchor;
|
|
169
|
-
if (anchor.hasAttribute(TEXT_HOVER_ATTR)) {
|
|
170
|
-
// Mid-fade-out re-entry: transition straight back to visible.
|
|
171
|
-
anchor.setAttribute(TEXT_HOVER_ATTR, 'true');
|
|
172
|
-
} else {
|
|
173
|
-
anchor.setAttribute(TEXT_HOVER_ATTR, 'in');
|
|
174
|
-
requestAnimationFrame(() => {
|
|
175
|
-
if (hovered === anchor && anchor.getAttribute(TEXT_HOVER_ATTR) === 'in') {
|
|
176
|
-
anchor.setAttribute(TEXT_HOVER_ATTR, 'true');
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
};
|
|
183
|
-
window.addEventListener('pointermove', onMove, true);
|
|
184
|
-
return () => {
|
|
185
|
-
window.removeEventListener('pointermove', onMove, true);
|
|
186
|
-
cancelAnimationFrame(raf);
|
|
187
|
-
hovered = null;
|
|
188
|
-
for (const el of document.querySelectorAll(`[${TEXT_HOVER_ATTR}]`)) {
|
|
189
|
-
el.removeAttribute(TEXT_HOVER_ATTR);
|
|
190
|
-
}
|
|
191
|
-
styleEl.remove();
|
|
192
|
-
};
|
|
193
|
-
}, [active, slideId]);
|
|
124
|
+
}, [inlineEdit, stopInlineEdit]);
|
|
194
125
|
|
|
195
126
|
if (import.meta.env.PROD) return null;
|
|
196
127
|
return (
|
|
@@ -200,52 +131,12 @@ export function InlineEditLayer() {
|
|
|
200
131
|
key={inlineEdit.session ?? `${inlineEdit.line}:${inlineEdit.column}`}
|
|
201
132
|
target={inlineEdit}
|
|
202
133
|
layerRef={layerRef}
|
|
203
|
-
showOutline={!active}
|
|
204
134
|
/>
|
|
205
135
|
)}
|
|
206
136
|
</div>
|
|
207
137
|
);
|
|
208
138
|
}
|
|
209
139
|
|
|
210
|
-
const TEXT_HOVER_ATTR = 'data-slide-text-hover';
|
|
211
|
-
const HOVER_FADE_MS = 160;
|
|
212
|
-
|
|
213
|
-
// The outline fades by transitioning outline-color through transient
|
|
214
|
-
// attribute values: 'in' (transparent) → 'true' (visible) on enter, and
|
|
215
|
-
// 'true' → 'out' (transparent) before removal on leave.
|
|
216
|
-
const HOVER_HINT_CSS = `
|
|
217
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}],
|
|
218
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}] * {
|
|
219
|
-
cursor: text !important;
|
|
220
|
-
}
|
|
221
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}] {
|
|
222
|
-
outline: 2px solid rgba(59, 130, 246, 0.5) !important;
|
|
223
|
-
outline-offset: 2px !important;
|
|
224
|
-
}
|
|
225
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}='in'],
|
|
226
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}='out'] {
|
|
227
|
-
outline-color: rgba(59, 130, 246, 0) !important;
|
|
228
|
-
}
|
|
229
|
-
@media (prefers-reduced-motion: no-preference) {
|
|
230
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}] {
|
|
231
|
-
transition: outline-color ${HOVER_FADE_MS}ms ease !important;
|
|
232
|
-
}
|
|
233
|
-
/* The 'in' frame must land on transparent instantly; with the transition
|
|
234
|
-
active it would itself animate away from the pre-hover outline-color. */
|
|
235
|
-
[data-inspector-root] [${TEXT_HOVER_ATTR}='in'] {
|
|
236
|
-
transition: none !important;
|
|
237
|
-
}
|
|
238
|
-
@keyframes osd-inline-outline-in {
|
|
239
|
-
from {
|
|
240
|
-
opacity: 0;
|
|
241
|
-
}
|
|
242
|
-
to {
|
|
243
|
-
opacity: 1;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
`;
|
|
248
|
-
|
|
249
140
|
const INLINE_EDITING_CSS = `
|
|
250
141
|
[data-inspector-root] [data-slide-editing][data-slide-editing],
|
|
251
142
|
[data-inspector-root] [data-slide-editing][data-slide-editing] * {
|
|
@@ -261,55 +152,108 @@ const INLINE_EDITING_CSS = `
|
|
|
261
152
|
function ActiveInlineEditor({
|
|
262
153
|
target,
|
|
263
154
|
layerRef,
|
|
264
|
-
showOutline,
|
|
265
155
|
}: {
|
|
266
156
|
target: InlineEditTarget;
|
|
267
157
|
layerRef: RefObject<HTMLDivElement | null>;
|
|
268
|
-
showOutline: boolean;
|
|
269
158
|
}) {
|
|
270
|
-
const {
|
|
159
|
+
const {
|
|
160
|
+
bufferOps,
|
|
161
|
+
stopInlineEdit,
|
|
162
|
+
setInlineSelection,
|
|
163
|
+
registerInlineStyle,
|
|
164
|
+
panelOpen,
|
|
165
|
+
panelHidden,
|
|
166
|
+
opsVersion,
|
|
167
|
+
committing,
|
|
168
|
+
} = useInspector();
|
|
169
|
+
const history = useHistory();
|
|
271
170
|
const [sel, setSel] = useState<TextRange | null>(null);
|
|
171
|
+
const selectionRef = useRef<TextRange | null>(null);
|
|
272
172
|
const { anchor } = target;
|
|
273
173
|
const rect = useAnchorRect(anchor, layerRef);
|
|
174
|
+
const previousTextRef = useRef(readEditableText(anchor));
|
|
274
175
|
|
|
275
176
|
const commit = useCallback(() => {
|
|
276
177
|
if (!anchor.isConnected) return;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
178
|
+
const value = readEditableText(anchor);
|
|
179
|
+
const prevText = previousTextRef.current;
|
|
180
|
+
if (value === prevText) return;
|
|
181
|
+
previousTextRef.current = value;
|
|
182
|
+
bufferOps(target.line, target.column, anchor, [{ kind: 'set-text', value, prevText }]);
|
|
280
183
|
}, [anchor, target.line, target.column, bufferOps]);
|
|
281
184
|
|
|
185
|
+
const applyStyleOps = useCallback(
|
|
186
|
+
(ops: EditOp[]) => {
|
|
187
|
+
if (!anchor.isConnected || ops.some((op) => op.kind !== 'set-style')) return false;
|
|
188
|
+
commit();
|
|
189
|
+
const range = selectionTextOffsets(anchor) ?? selectionRef.current;
|
|
190
|
+
const focused = document.activeElement;
|
|
191
|
+
const prevText = readEditableText(anchor);
|
|
192
|
+
bufferOps(
|
|
193
|
+
target.line,
|
|
194
|
+
target.column,
|
|
195
|
+
anchor,
|
|
196
|
+
ops.map((op): EditOp => {
|
|
197
|
+
if (op.kind !== 'set-style') return op;
|
|
198
|
+
return range && range.end > range.start && RANGE_STYLE_KEYS.has(op.key)
|
|
199
|
+
? { ...op, kind: 'set-text-range-style', ...range, prevText }
|
|
200
|
+
: { ...op, prevText };
|
|
201
|
+
}),
|
|
202
|
+
);
|
|
203
|
+
if (range) restoreTextSelection(anchor, range);
|
|
204
|
+
if (
|
|
205
|
+
focused instanceof HTMLElement &&
|
|
206
|
+
focused !== document.body &&
|
|
207
|
+
!anchor.contains(focused)
|
|
208
|
+
) {
|
|
209
|
+
focused.focus({ preventScroll: true });
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
},
|
|
213
|
+
[anchor, target.line, target.column, bufferOps, commit],
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
useEffect(() => registerInlineStyle(applyStyleOps), [registerInlineStyle, applyStyleOps]);
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
void opsVersion;
|
|
219
|
+
previousTextRef.current = readEditableText(anchor);
|
|
220
|
+
}, [anchor, opsVersion]);
|
|
221
|
+
|
|
282
222
|
const applyTextStyle = useCallback(
|
|
283
223
|
(key: string, value: string | null) => {
|
|
284
|
-
|
|
285
|
-
if (sel && sel.end > sel.start && RANGE_STYLE_KEYS.has(key)) {
|
|
286
|
-
bufferOps(target.line, target.column, anchor, [
|
|
287
|
-
{ kind: 'set-text-range-style', start: sel.start, end: sel.end, key, value },
|
|
288
|
-
]);
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
bufferOps(target.line, target.column, anchor, [
|
|
292
|
-
{ kind: 'set-style', key, value, prevText: readEditableText(anchor) },
|
|
293
|
-
]);
|
|
224
|
+
applyStyleOps([{ kind: 'set-style', key, value }]);
|
|
294
225
|
},
|
|
295
|
-
[
|
|
226
|
+
[applyStyleOps],
|
|
296
227
|
);
|
|
297
228
|
|
|
298
229
|
const toggleBold = useCallback(() => {
|
|
299
230
|
const bold = parseInt(getComputedStyle(styleContext(anchor, sel)).fontWeight, 10) >= 600;
|
|
300
|
-
applyTextStyle('fontWeight', bold ?
|
|
231
|
+
applyTextStyle('fontWeight', bold ? '400' : '700');
|
|
301
232
|
}, [anchor, sel, applyTextStyle]);
|
|
302
233
|
|
|
303
234
|
const toggleItalic = useCallback(() => {
|
|
304
235
|
const italic = getComputedStyle(styleContext(anchor, sel)).fontStyle === 'italic';
|
|
305
|
-
applyTextStyle('fontStyle', italic ?
|
|
236
|
+
applyTextStyle('fontStyle', italic ? 'normal' : 'italic');
|
|
306
237
|
}, [anchor, sel, applyTextStyle]);
|
|
307
238
|
|
|
239
|
+
const applyHistory = useCallback(
|
|
240
|
+
(redo: boolean) => {
|
|
241
|
+
if (committing) return;
|
|
242
|
+
commit();
|
|
243
|
+
const range = selectionTextOffsets(anchor) ?? selectionRef.current;
|
|
244
|
+
if (redo) history.redo();
|
|
245
|
+
else history.undo();
|
|
246
|
+
previousTextRef.current = readEditableText(anchor);
|
|
247
|
+
if (range) restoreTextSelection(anchor, range);
|
|
248
|
+
},
|
|
249
|
+
[anchor, commit, committing, history.undo, history.redo],
|
|
250
|
+
);
|
|
251
|
+
|
|
308
252
|
// The setup effect must run exactly once per anchor — re-running it would
|
|
309
253
|
// re-place the caret and clobber the user's live selection — so everything
|
|
310
254
|
// with an unstable identity is reached through latest-refs.
|
|
311
|
-
const latestRef = useRef({ commit, toggleBold, toggleItalic });
|
|
312
|
-
latestRef.current = { commit, toggleBold, toggleItalic };
|
|
255
|
+
const latestRef = useRef({ commit, toggleBold, toggleItalic, applyHistory });
|
|
256
|
+
latestRef.current = { commit, toggleBold, toggleItalic, applyHistory };
|
|
313
257
|
const initialCaretRef = useRef({ point: target.point, selectWord: target.selectWord ?? false });
|
|
314
258
|
|
|
315
259
|
useEffect(() => {
|
|
@@ -323,8 +267,12 @@ function ActiveInlineEditor({
|
|
|
323
267
|
|
|
324
268
|
const onBeforeInput = (e: Event) => {
|
|
325
269
|
const ev = e as InputEvent;
|
|
270
|
+
if (ev.isComposing) return;
|
|
326
271
|
const type = ev.inputType;
|
|
327
|
-
if (type === '
|
|
272
|
+
if (type === 'historyUndo' || type === 'historyRedo') {
|
|
273
|
+
ev.preventDefault();
|
|
274
|
+
latestRef.current.applyHistory(type === 'historyRedo');
|
|
275
|
+
} else if (type === 'insertParagraph' || type === 'insertLineBreak') {
|
|
328
276
|
ev.preventDefault();
|
|
329
277
|
document.execCommand('insertLineBreak');
|
|
330
278
|
} else if (type === 'insertFromPaste' || type === 'insertFromDrop') {
|
|
@@ -347,6 +295,7 @@ function ActiveInlineEditor({
|
|
|
347
295
|
};
|
|
348
296
|
const onCompositionEnd = () => latestRef.current.commit();
|
|
349
297
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
298
|
+
if (e.isComposing || e.keyCode === 229) return;
|
|
350
299
|
if ((e.metaKey || e.ctrlKey) && !e.altKey) {
|
|
351
300
|
const key = e.key.toLowerCase();
|
|
352
301
|
if (key === 'b') {
|
|
@@ -355,13 +304,21 @@ function ActiveInlineEditor({
|
|
|
355
304
|
} else if (key === 'i') {
|
|
356
305
|
e.preventDefault();
|
|
357
306
|
latestRef.current.toggleItalic();
|
|
307
|
+
} else if (key === 'z' || (key === 'y' && e.ctrlKey)) {
|
|
308
|
+
e.preventDefault();
|
|
309
|
+
latestRef.current.applyHistory(e.shiftKey || key === 'y');
|
|
358
310
|
}
|
|
359
311
|
}
|
|
360
312
|
};
|
|
361
313
|
const onSelectionChange = () => {
|
|
362
314
|
const offsets = selectionTextOffsets(anchor);
|
|
363
315
|
if (offsets === null) return;
|
|
364
|
-
|
|
316
|
+
if (selectionRef.current?.start === offsets.start && selectionRef.current.end === offsets.end)
|
|
317
|
+
return;
|
|
318
|
+
selectionRef.current = offsets;
|
|
319
|
+
const range = offsets.end > offsets.start ? offsets : null;
|
|
320
|
+
setSel(range);
|
|
321
|
+
setInlineSelection(range);
|
|
365
322
|
};
|
|
366
323
|
|
|
367
324
|
anchor.addEventListener('beforeinput', onBeforeInput);
|
|
@@ -369,6 +326,7 @@ function ActiveInlineEditor({
|
|
|
369
326
|
anchor.addEventListener('compositionend', onCompositionEnd);
|
|
370
327
|
anchor.addEventListener('keydown', onKeyDown);
|
|
371
328
|
document.addEventListener('selectionchange', onSelectionChange);
|
|
329
|
+
onSelectionChange();
|
|
372
330
|
return () => {
|
|
373
331
|
anchor.removeEventListener('beforeinput', onBeforeInput);
|
|
374
332
|
anchor.removeEventListener('input', onInput);
|
|
@@ -377,12 +335,13 @@ function ActiveInlineEditor({
|
|
|
377
335
|
document.removeEventListener('selectionchange', onSelectionChange);
|
|
378
336
|
styleEl.remove();
|
|
379
337
|
if (anchor.isConnected) {
|
|
338
|
+
if (document.activeElement === anchor) anchor.blur();
|
|
380
339
|
anchor.removeAttribute('contenteditable');
|
|
381
340
|
anchor.removeAttribute('spellcheck');
|
|
382
341
|
anchor.removeAttribute('data-slide-editing');
|
|
383
342
|
}
|
|
384
343
|
};
|
|
385
|
-
}, [anchor]);
|
|
344
|
+
}, [anchor, setInlineSelection]);
|
|
386
345
|
|
|
387
346
|
// An HMR remount replaces the anchor node (taking its contenteditable
|
|
388
347
|
// attribute with it), so a disconnected anchor ends the session.
|
|
@@ -400,26 +359,15 @@ function ActiveInlineEditor({
|
|
|
400
359
|
if (!rect) return null;
|
|
401
360
|
return (
|
|
402
361
|
<>
|
|
403
|
-
{
|
|
404
|
-
<
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
height: rect.height,
|
|
411
|
-
outline: '2px solid #3b82f6',
|
|
412
|
-
animation: 'osd-inline-outline-in 160ms ease-out',
|
|
413
|
-
}}
|
|
362
|
+
{(!panelOpen || panelHidden) && (
|
|
363
|
+
<TextToolbar
|
|
364
|
+
anchor={anchor}
|
|
365
|
+
layerRef={layerRef}
|
|
366
|
+
rect={rect}
|
|
367
|
+
sel={sel}
|
|
368
|
+
applyStyle={applyTextStyle}
|
|
414
369
|
/>
|
|
415
370
|
)}
|
|
416
|
-
<TextToolbar
|
|
417
|
-
anchor={anchor}
|
|
418
|
-
layerRef={layerRef}
|
|
419
|
-
rect={rect}
|
|
420
|
-
sel={sel}
|
|
421
|
-
applyStyle={applyTextStyle}
|
|
422
|
-
/>
|
|
423
371
|
</>
|
|
424
372
|
);
|
|
425
373
|
}
|
|
@@ -550,14 +498,14 @@ function TextToolbar({
|
|
|
550
498
|
<ToolbarIconButton
|
|
551
499
|
label={t.inspector.boldAria}
|
|
552
500
|
pressed={bold}
|
|
553
|
-
onClick={() => applyStyle('fontWeight', bold ?
|
|
501
|
+
onClick={() => applyStyle('fontWeight', bold ? '400' : '700')}
|
|
554
502
|
>
|
|
555
503
|
<Bold className="size-3.5" />
|
|
556
504
|
</ToolbarIconButton>
|
|
557
505
|
<ToolbarIconButton
|
|
558
506
|
label={t.inspector.italicAria}
|
|
559
507
|
pressed={italic}
|
|
560
|
-
onClick={() => applyStyle('fontStyle', italic ?
|
|
508
|
+
onClick={() => applyStyle('fontStyle', italic ? 'normal' : 'italic')}
|
|
561
509
|
>
|
|
562
510
|
<Italic className="size-3.5" />
|
|
563
511
|
</ToolbarIconButton>
|
|
@@ -671,13 +619,6 @@ function FontSizeInput({
|
|
|
671
619
|
);
|
|
672
620
|
}
|
|
673
621
|
|
|
674
|
-
function styleContext(anchor: HTMLElement, sel: TextRange | null): HTMLElement {
|
|
675
|
-
if (!sel) return anchor;
|
|
676
|
-
const node = window.getSelection()?.anchorNode;
|
|
677
|
-
const el = node instanceof HTMLElement ? node : (node?.parentElement ?? null);
|
|
678
|
-
return el && anchor.contains(el) ? el : anchor;
|
|
679
|
-
}
|
|
680
|
-
|
|
681
622
|
function focusAndPlaceCaret(
|
|
682
623
|
anchor: HTMLElement,
|
|
683
624
|
point: { x: number; y: number } | undefined,
|
|
@@ -730,61 +671,6 @@ function caretRangeAtPoint(x: number, y: number): Range | null {
|
|
|
730
671
|
return range;
|
|
731
672
|
}
|
|
732
673
|
|
|
733
|
-
// Map the live DOM selection to offsets in the normalized editable text —
|
|
734
|
-
// the same coordinate space `set-text-range-style` ops use.
|
|
735
|
-
export function selectionTextOffsets(root: HTMLElement): TextRange | null {
|
|
736
|
-
const selection = window.getSelection();
|
|
737
|
-
if (!selection || selection.rangeCount === 0) return null;
|
|
738
|
-
const range = selection.getRangeAt(0);
|
|
739
|
-
if (!root.contains(range.startContainer) || !root.contains(range.endContainer)) return null;
|
|
740
|
-
const parts: DomTextPart[] = [];
|
|
741
|
-
collectDomTextParts(root, parts);
|
|
742
|
-
const start = pointToTextOffset(parts, range.startContainer, range.startOffset);
|
|
743
|
-
const end = pointToTextOffset(parts, range.endContainer, range.endOffset);
|
|
744
|
-
if (start === null || end === null) return null;
|
|
745
|
-
return start <= end ? { start, end } : { start: end, end: start };
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
function pointToTextOffset(parts: DomTextPart[], container: Node, offset: number): number | null {
|
|
749
|
-
const probe = document.createRange();
|
|
750
|
-
try {
|
|
751
|
-
probe.setStart(container, offset);
|
|
752
|
-
} catch {
|
|
753
|
-
return null;
|
|
754
|
-
}
|
|
755
|
-
probe.collapse(true);
|
|
756
|
-
let total = 0;
|
|
757
|
-
for (const part of parts) {
|
|
758
|
-
if (part.node === container && part.node instanceof Text) {
|
|
759
|
-
const full = collapsedTextSlice(part.node, part.node.data);
|
|
760
|
-
const prefix = collapsedTextSlice(part.node, part.node.data.slice(0, offset));
|
|
761
|
-
const leading = Math.max(0, full.indexOf(part.current));
|
|
762
|
-
return total + Math.min(Math.max(prefix.length - leading, 0), part.current.length);
|
|
763
|
-
}
|
|
764
|
-
let cmp: number;
|
|
765
|
-
try {
|
|
766
|
-
cmp = probe.comparePoint(part.node, 0);
|
|
767
|
-
} catch {
|
|
768
|
-
return null;
|
|
769
|
-
}
|
|
770
|
-
if (cmp > 0) break;
|
|
771
|
-
if (cmp < 0) {
|
|
772
|
-
total += part.current.length;
|
|
773
|
-
continue;
|
|
774
|
-
}
|
|
775
|
-
break;
|
|
776
|
-
}
|
|
777
|
-
return total;
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
function collapsedTextSlice(node: Text, value: string): string {
|
|
781
|
-
const whiteSpace = node.parentElement ? getComputedStyle(node.parentElement).whiteSpace : '';
|
|
782
|
-
if (whiteSpace === 'pre' || whiteSpace === 'pre-wrap' || whiteSpace === 'break-spaces') {
|
|
783
|
-
return value;
|
|
784
|
-
}
|
|
785
|
-
return value.replace(/\s+/g, ' ');
|
|
786
|
-
}
|
|
787
|
-
|
|
788
674
|
function rgbToHex(value: string): string | null {
|
|
789
675
|
const m = value.match(/^rgba?\(([^)]+)\)$/);
|
|
790
676
|
if (!m) return null;
|