@weasel-js/text-tools 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.md +37 -2
- package/dist/chunk-VLXWRY3D.js +1179 -0
- package/dist/chunk-VLXWRY3D.js.map +1 -0
- package/dist/index.d.ts +213 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/presets-oTMEI2UF.d.ts +378 -0
- package/dist/presets.d.ts +9 -0
- package/dist/presets.js +3 -0
- package/dist/presets.js.map +1 -0
- package/package.json +74 -4
|
@@ -0,0 +1,1179 @@
|
|
|
1
|
+
import { defineAction, ActionDisabledReason, isBody, shareToolOptions, withToolOptionDefault, layoutBoxOf } from '@weasel-js/routing';
|
|
2
|
+
import { shapePropertiesSchema, effectSourceFor, arrivalBounds, strokeOutsetPerAxis, inkReach, nodeMemo, PAINT_SLOT, paintsPose, defineTool, noteFontRead, withInheritedPaint, hasStroke, NodeOutlineError, registerPaintStandIn, findMountedCanvas, asNodeId, shownLookup, scenePaintedPoseOf, defaultDrawOne, inheritedPaintIn, nodesAsShown, sceneDerivedPathOf, worldToLocal, targetTransformOf, indexedCandidates, AUTO_POSE_DESCRIPTOR, derivedPartsOf } from '@weasel-js/core';
|
|
3
|
+
import { pathThroughPoseFrame, unionRects, transformPath, PATH_M, PATH_L, PATH_Z, poseFrameOf, boundsOfPath, meanScale, pathContainsPoint, rectContainsPoint } from '@weasel-js/geom';
|
|
4
|
+
import { DEFAULT_TEXT_FILL, measureTextBounds, runsToPlainText, isTextData, textLineBoxes, textClusters, TextOutlinesError, loadTextOutlines, textEngineGeneration, textFitScale, DEFAULT_TEXT_STYLE, textPoseContainsPoint, caretIndexAt, textToPathsByPaint, textToPath } from '@weasel-js/text';
|
|
5
|
+
import { isDev } from '@weasel-js/registry';
|
|
6
|
+
import { pathIntersect } from '@weasel-js/geom/booleans';
|
|
7
|
+
import { resolveFill, resolveStroke, INHERIT, rgbaToHex, isSolidPaint, mixOklab, parseColor, resolveScreenLength } from '@weasel-js/paint';
|
|
8
|
+
import { textCommandFromPose, commandFill, dropShadowLayers } from '@weasel-js/renderer';
|
|
9
|
+
import { faceGeneration } from '@weasel-js/font';
|
|
10
|
+
import { useState, useLayoutEffect, useMemo, useRef, useEffect, useCallback } from 'react';
|
|
11
|
+
import { useLatest, isDev as isDev$1 } from '@weasel-js/react';
|
|
12
|
+
import { replaceRunRange, runStyleAt, useTextEdit } from '@weasel-js/text-dom';
|
|
13
|
+
import { styleOptionLeaves, shapeToolOptions, INSERT_DRAG_MODS, INSERT_REPOSITION_BINDING, insertBindingBehaviors, toolOptionPaint } from '@weasel-js/editing';
|
|
14
|
+
import { decimal } from '@weasel-js/quantity';
|
|
15
|
+
import { cursorFor } from '@weasel-js/cursor';
|
|
16
|
+
import { useDepSource } from '@weasel-js/routing/react';
|
|
17
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
18
|
+
import * as presets from '@weasel-js/editing/presets';
|
|
19
|
+
|
|
20
|
+
// src/actions/editEffectText.ts
|
|
21
|
+
function textUnder(deps, world) {
|
|
22
|
+
const scene = deps?.scene;
|
|
23
|
+
const open = deps?.editAnchors;
|
|
24
|
+
if (!world || !scene || !deps?.textEdit || !open?.editingId) return null;
|
|
25
|
+
const under = deps.nodeAtPoint?.(world);
|
|
26
|
+
if (!under) return null;
|
|
27
|
+
const text = effectSourceFor(scene, under, "items");
|
|
28
|
+
if (text === null || scene.isLocked(text) || open.anchorUnder(world) !== -1) return null;
|
|
29
|
+
return String(text);
|
|
30
|
+
}
|
|
31
|
+
var editEffectTextAction = defineAction({
|
|
32
|
+
id: "editEffectText",
|
|
33
|
+
label: "Edit text along the path",
|
|
34
|
+
// On the effect's body, so it outranks the untargeted anchor click; an anchor under the pointer gives the click back.
|
|
35
|
+
defaultBinding: [{ spec: { kind: "click", target: { kindOf: isBody }, mods: {} }, opts: { id: "editEffectText.click" } }],
|
|
36
|
+
eligible: { capability: "edits-anchors" },
|
|
37
|
+
requires: ["textEdit", "nodeAtPoint", "scene", "editAnchors"],
|
|
38
|
+
invoker: {
|
|
39
|
+
timing: "immediate",
|
|
40
|
+
run(deps, params) {
|
|
41
|
+
const hit = textUnder(deps, params?.world);
|
|
42
|
+
if (hit !== null) deps.textEdit?.startEdit(hit, { caret: "end" });
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
enabled: (deps, at) => textUnder(deps, at) !== null ? true : ActionDisabledReason.NotApplicable
|
|
46
|
+
});
|
|
47
|
+
var enterTextEditAtAction = defineAction({
|
|
48
|
+
id: "enterTextEditAt",
|
|
49
|
+
label: "Edit text here",
|
|
50
|
+
defaultBinding: [{
|
|
51
|
+
spec: { kind: "doubleClick", target: { kindOf: isBody } },
|
|
52
|
+
opts: { id: "enterTextEditAt.doubleClick" }
|
|
53
|
+
}],
|
|
54
|
+
eligible: { mode: "normal" },
|
|
55
|
+
requires: ["textEdit", "hitAt"],
|
|
56
|
+
invoker: {
|
|
57
|
+
timing: "immediate",
|
|
58
|
+
run: (deps, params) => {
|
|
59
|
+
const world = params?.world;
|
|
60
|
+
const hit = world ? deps.textEdit?.textAt(world) : null;
|
|
61
|
+
if (hit) deps.textEdit.startEdit(hit.id, { caret: hit.caret });
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
enabled: (deps, at) => {
|
|
65
|
+
if (!at || deps?.hitAt?.(at)?.doubleClick) return ActionDisabledReason.NotApplicable;
|
|
66
|
+
return deps?.textEdit?.textAt(at) ? true : ActionDisabledReason.NotApplicable;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// src/actions/enterTextEdit.ts
|
|
71
|
+
var enterTextEditAction = {
|
|
72
|
+
id: "enterTextEdit",
|
|
73
|
+
label: "Enter text edit",
|
|
74
|
+
eligible: { mode: "normal" },
|
|
75
|
+
requires: ["textEdit", "operands"],
|
|
76
|
+
invoker: {
|
|
77
|
+
timing: "immediate",
|
|
78
|
+
run: (deps) => {
|
|
79
|
+
const sel = deps.operands?.get() ?? [];
|
|
80
|
+
if (sel.length !== 1) return "pass";
|
|
81
|
+
const id = sel[0];
|
|
82
|
+
const textEdit = deps.textEdit;
|
|
83
|
+
if (!textEdit) return "pass";
|
|
84
|
+
if (textEdit.isTextNode && !textEdit.isTextNode(id)) return "pass";
|
|
85
|
+
textEdit.startEdit(id);
|
|
86
|
+
return void 0;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
function drawOneWithoutEditedText(editingId, base = defaultDrawOne) {
|
|
91
|
+
return (node, pose, view2, ctx) => {
|
|
92
|
+
const out = base(node, pose, view2, ctx);
|
|
93
|
+
return node.id === editingId ? withoutText(out) : out;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function withoutText(cmds) {
|
|
97
|
+
const out = [];
|
|
98
|
+
for (const c of cmds) {
|
|
99
|
+
if (c.kind === "text") continue;
|
|
100
|
+
if (c.kind !== "group") {
|
|
101
|
+
out.push(c);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const children = withoutText(c.children);
|
|
105
|
+
if (children.length > 0) out.push({ ...c, children });
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
var warnedFamilies = /* @__PURE__ */ new Set();
|
|
110
|
+
function warnFamily(family, message, cause) {
|
|
111
|
+
if (!isDev() || warnedFamilies.has(family)) return;
|
|
112
|
+
warnedFamilies.add(family);
|
|
113
|
+
console.warn(`weasel textEffectItems: ${message}`, ...cause === void 0 ? [] : [cause]);
|
|
114
|
+
}
|
|
115
|
+
function textEffectItems(data, pose) {
|
|
116
|
+
noteFontRead();
|
|
117
|
+
let clusters;
|
|
118
|
+
try {
|
|
119
|
+
clusters = textClusters(data, { width: pose.width, height: pose.height });
|
|
120
|
+
} catch (err) {
|
|
121
|
+
if (!(err instanceof TextOutlinesError)) throw err;
|
|
122
|
+
if (err.reason === "outlines-loading") {
|
|
123
|
+
loadTextOutlines(data).catch((cause) => {
|
|
124
|
+
warnFamily(err.family, `loading the outlines of "${err.family}" failed, so its text lays no items.`, cause);
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
warnFamily(err.family, `text in "${err.family}" lays no items along the path: ${err.message}`);
|
|
128
|
+
}
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const k = scaleOf(pose);
|
|
132
|
+
const grow = k === 1 ? null : [k, 0, 0, k, 0, 0];
|
|
133
|
+
return clusters.map((c) => ({
|
|
134
|
+
length: c.advance * k,
|
|
135
|
+
middle: c.middle * k,
|
|
136
|
+
parts: c.paths.map((p) => ({
|
|
137
|
+
path: grow === null ? p.path : transformPath(p.path, grow),
|
|
138
|
+
fill: p.fill,
|
|
139
|
+
stroke: p.stroke ?? null
|
|
140
|
+
})),
|
|
141
|
+
bands: c.rules.map((r) => ({
|
|
142
|
+
x0: r.x0 * k,
|
|
143
|
+
x1: r.x1 * k,
|
|
144
|
+
y0: r.y0 * k,
|
|
145
|
+
y1: r.y1 * k,
|
|
146
|
+
fill: r.fill,
|
|
147
|
+
stroke: r.stroke ?? null
|
|
148
|
+
}))
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
function scaleOf(pose) {
|
|
152
|
+
const m = poseFrameOf(pose);
|
|
153
|
+
return m === null ? 1 : Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
|
|
154
|
+
}
|
|
155
|
+
function textOutlineOf(d, box, inherited) {
|
|
156
|
+
const { text, runs, style, verticalAlign, fit, fill, stroke } = withInheritedPaint(d, inherited);
|
|
157
|
+
const data = { text, runs, style, verticalAlign, fit, fill, stroke };
|
|
158
|
+
return {
|
|
159
|
+
stroked: hasStroke(stroke) || !!runs?.some((r) => hasStroke(r.stroke)),
|
|
160
|
+
path: () => outlined(() => textToPath(data, box)),
|
|
161
|
+
parts: () => outlined(() => textToPathsByPaint(data, box)),
|
|
162
|
+
load: () => loadTextOutlines(data)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function clippedOutline(outline, clip) {
|
|
166
|
+
const box = clip.kind === "rect" ? clip : null;
|
|
167
|
+
const cut = (path) => box && inside(boundsOfPath(path), box) ? path : pathIntersect(path, clip);
|
|
168
|
+
return {
|
|
169
|
+
stroked: outline.stroked,
|
|
170
|
+
path: () => cut(outline.path()),
|
|
171
|
+
parts: () => outline.parts().map((p) => ({ ...p, path: cut(p.path) })).filter((p) => !emptyPath(p.path)),
|
|
172
|
+
load: () => outline.load()
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
var inside = (b, box) => b.x >= box.x && b.y >= box.y && b.x + b.width <= box.x + box.width && b.y + b.height <= box.y + box.height;
|
|
176
|
+
var emptyPath = (p) => p.kind !== "rect" && "commands" in p && p.commands.length === 0;
|
|
177
|
+
function outlined(build) {
|
|
178
|
+
try {
|
|
179
|
+
return build();
|
|
180
|
+
} catch (err) {
|
|
181
|
+
if (!(err instanceof TextOutlinesError)) throw err;
|
|
182
|
+
throw new NodeOutlineError(err.message, {
|
|
183
|
+
summary: `Text in "${err.family}" can't be converted to outlines`,
|
|
184
|
+
pending: err.reason === "outlines-loading",
|
|
185
|
+
cause: err
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
var BLACK = [0, 0, 0, 1];
|
|
190
|
+
function edgeColor(background, shade) {
|
|
191
|
+
const paint = background.paint;
|
|
192
|
+
if (paint === void 0 || !isSolidPaint(paint)) return null;
|
|
193
|
+
return rgbaToHex(mixOklab(BLACK, parseColor(paint.color), Math.min(1, Math.max(0, shade))));
|
|
194
|
+
}
|
|
195
|
+
function textFrameOf(d, box) {
|
|
196
|
+
const bg = resolveFill(d.background);
|
|
197
|
+
if (!bg) return { shadow: null, edge: null };
|
|
198
|
+
return { shadow: shadowOf(d.shadow, box), edge: edgeOf(d.edge, bg, box) };
|
|
199
|
+
}
|
|
200
|
+
function shadowOf(s, box) {
|
|
201
|
+
if (!s || !(s.alpha > 0)) return null;
|
|
202
|
+
const { paths, alphas, alpha } = dropShadowLayers(box, 0, s);
|
|
203
|
+
return { paths, alphas, color: rgbaToHex([0, 0, 0, Math.min(1, s.alpha)]), opacity: alpha };
|
|
204
|
+
}
|
|
205
|
+
function edgeOf(edge, bg, box) {
|
|
206
|
+
if (!edge || !(edge.width > 0)) return null;
|
|
207
|
+
const color = edgeColor(bg, edge.shade);
|
|
208
|
+
if (color === null) return null;
|
|
209
|
+
const w = Math.min(edge.width, box.width / 2, box.height / 2);
|
|
210
|
+
return {
|
|
211
|
+
path: { kind: "rect", x: box.x + w / 2, y: box.y + w / 2, width: box.width - w, height: box.height - w },
|
|
212
|
+
color,
|
|
213
|
+
width: w
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function textShadowBox(d, box) {
|
|
217
|
+
const s = d.shadow;
|
|
218
|
+
if (!s || !(s.alpha > 0) || !resolveFill(d.background)) return null;
|
|
219
|
+
const e = Math.max(0, s.blur) / 2;
|
|
220
|
+
return { x: box.x + s.x - e, y: box.y + s.y - e, width: box.width + 2 * e, height: box.height + 2 * e };
|
|
221
|
+
}
|
|
222
|
+
function clippedToBox(cmd, box) {
|
|
223
|
+
return {
|
|
224
|
+
kind: "group",
|
|
225
|
+
clip: { kind: "rect", x: box.x, y: box.y, width: box.width, height: box.height },
|
|
226
|
+
children: [cmd]
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function textBoxOf(d, p) {
|
|
230
|
+
const pad = Math.max(0, d.padding ?? 0);
|
|
231
|
+
const px = Math.min(pad, p.width / 2);
|
|
232
|
+
const py = Math.min(pad, p.height / 2);
|
|
233
|
+
return { x: p.x + px, y: p.y + py, width: p.width - 2 * px, height: p.height - 2 * py };
|
|
234
|
+
}
|
|
235
|
+
function textPoseOf(d, p, inherited) {
|
|
236
|
+
return {
|
|
237
|
+
...textBoxOf(d, p),
|
|
238
|
+
text: d.text,
|
|
239
|
+
runs: d.runs,
|
|
240
|
+
style: d.style,
|
|
241
|
+
fill: d.fill === INHERIT ? resolveFill(d.fill, inherited?.fill) : d.fill,
|
|
242
|
+
stroke: resolveStroke(d.stroke, inherited?.stroke) ?? void 0,
|
|
243
|
+
verticalAlign: d.verticalAlign,
|
|
244
|
+
fit: d.fit
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
var TEXT_PAINTER = {
|
|
248
|
+
id: "kit:text",
|
|
249
|
+
snapsToBox: true,
|
|
250
|
+
matches: (node) => {
|
|
251
|
+
if (!paintsPose(node.pose)) return false;
|
|
252
|
+
const d = node.data;
|
|
253
|
+
return d?.text != null;
|
|
254
|
+
},
|
|
255
|
+
// Memoized because the renderer's layout cache keys on the `ResolvedRun[]` array
|
|
256
|
+
// identity: a fresh runs array per frame would leave that cache missing on every
|
|
257
|
+
// draw. `fit: 'shrink'` measures, so the faces and layout engines are in the key
|
|
258
|
+
// beside `(data, pose)` and the view scale for `{ px }` text. See PAINT_SLOT.
|
|
259
|
+
paint: (node, pose, ctx) => {
|
|
260
|
+
const d = node.data;
|
|
261
|
+
const inherited = ctx?.inherited;
|
|
262
|
+
const scale = textLayoutScale(d, ctx?.scale);
|
|
263
|
+
const layout = layoutGeneration();
|
|
264
|
+
const held = nodeMemo(
|
|
265
|
+
node,
|
|
266
|
+
scale === 1 ? PAINT_SLOT : SCREEN_PAINT_SLOT,
|
|
267
|
+
pose,
|
|
268
|
+
() => ({ scale, inherited, layout, cmds: paintText(d, pose, inherited, scale) })
|
|
269
|
+
);
|
|
270
|
+
if (held.scale !== scale || held.inherited !== inherited || held.layout !== layout) {
|
|
271
|
+
held.scale = scale;
|
|
272
|
+
held.inherited = inherited;
|
|
273
|
+
held.layout = layout;
|
|
274
|
+
held.cmds = paintText(d, pose, inherited, scale);
|
|
275
|
+
}
|
|
276
|
+
return held.cmds;
|
|
277
|
+
},
|
|
278
|
+
// The pose is a layout box, not a bounding box: the union of the line boxes is what
|
|
279
|
+
// the node actually covers, so picking, lasso, and clipping stop grabbing blank space.
|
|
280
|
+
//
|
|
281
|
+
// `null` rather than an empty path when there are no non-blank lines: a caller
|
|
282
|
+
// reading "no silhouette" falls back to the pose rect, which keeps an empty text
|
|
283
|
+
// node pickable.
|
|
284
|
+
//
|
|
285
|
+
// A background is ink across the whole box, so a node with one is its box.
|
|
286
|
+
// A clipped node's lines are cut to the box, as they are drawn.
|
|
287
|
+
silhouette: (node, pose, ctx) => {
|
|
288
|
+
const d = node.data;
|
|
289
|
+
if (resolveFill(d.background)) {
|
|
290
|
+
return { kind: "rect", x: pose.x, y: pose.y, width: pose.width, height: pose.height };
|
|
291
|
+
}
|
|
292
|
+
const lines = textLineBoxes(textPoseOf(d, pose), { scale: textLayoutScale(d, ctx?.scale) });
|
|
293
|
+
const boxes = d.overflow === "clip" ? cutRects(lines, pose) : lines;
|
|
294
|
+
if (boxes.length === 0) return null;
|
|
295
|
+
return rectsToPath(boxes);
|
|
296
|
+
},
|
|
297
|
+
// `filled` is unconditional, `data.fill: null` included: the silhouette above is line
|
|
298
|
+
// boxes, not glyph ink, so reporting outline-only text as unfilled would leave it
|
|
299
|
+
// grabbable within a stroke width of a box edge and nowhere near the letters.
|
|
300
|
+
ink: (node, _pose, ctx) => {
|
|
301
|
+
const reach = { outset: 0, inset: 0, outsetPx: 0, insetPx: 0 };
|
|
302
|
+
for (const stroke of strokesOf(node.data, ctx?.inherited)) {
|
|
303
|
+
const r = inkReach(stroke, ctx?.scale);
|
|
304
|
+
reach.outset = Math.max(reach.outset, r.outset);
|
|
305
|
+
reach.inset = Math.max(reach.inset, r.inset);
|
|
306
|
+
reach.outsetPx = Math.max(reach.outsetPx, r.outsetPx ?? 0);
|
|
307
|
+
reach.insetPx = Math.max(reach.insetPx, r.insetPx ?? 0);
|
|
308
|
+
}
|
|
309
|
+
const { outsetPx, insetPx, ...world } = reach;
|
|
310
|
+
return { filled: true, ...world, ...outsetPx || insetPx ? { outsetPx, insetPx } : {} };
|
|
311
|
+
},
|
|
312
|
+
drawnBounds: (node, pose, ctx) => {
|
|
313
|
+
const d = node.data;
|
|
314
|
+
const strokes = strokesOf(d, ctx?.inherited);
|
|
315
|
+
const reach = { x: 0, y: 0 };
|
|
316
|
+
for (const stroke of strokes) {
|
|
317
|
+
const r = strokeOutsetPerAxis(stroke, ctx);
|
|
318
|
+
reach.x = Math.max(reach.x, r.x);
|
|
319
|
+
reach.y = Math.max(reach.y, r.y);
|
|
320
|
+
}
|
|
321
|
+
const lines = strokes.length > 0 ? strokedLinesBox(pose, textLineBoxes(textPoseOf(d, pose, ctx?.inherited), { scale: textLayoutScale(d, ctx?.scale) }), reach) : null;
|
|
322
|
+
const shadow = textShadowBox(d, pose);
|
|
323
|
+
return shadow ? unionRects([lines ?? pose, shadow]) : lines;
|
|
324
|
+
},
|
|
325
|
+
outline: (node, pose, ctx) => {
|
|
326
|
+
const d = node.data;
|
|
327
|
+
const outline = textOutlineOf(d, { ...pose, ...textBoxOf(d, pose) }, ctx?.inherited);
|
|
328
|
+
if (d.overflow !== "clip") return outline;
|
|
329
|
+
const clip = pathThroughPoseFrame({ kind: "rect", x: pose.x, y: pose.y, width: pose.width, height: pose.height }, pose);
|
|
330
|
+
return clippedOutline(outline, clip);
|
|
331
|
+
},
|
|
332
|
+
// Line boxes and glyphs follow the faces and the registered layout engines.
|
|
333
|
+
geometryGeneration: () => layoutGeneration(),
|
|
334
|
+
silhouetteKey: (node, ctx) => textLayoutScale(node.data, ctx?.scale),
|
|
335
|
+
effectItems: (node, pose) => {
|
|
336
|
+
const d = node.data;
|
|
337
|
+
return textEffectItems(d, { ...pose, ...textBoxOf(d, pose) });
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
function textLayoutScale(d, scale) {
|
|
341
|
+
if (scale === void 0 || scale === 1) return 1;
|
|
342
|
+
const sized = (s) => typeof s?.fontSize === "object" || typeof s?.letterSpacing === "object";
|
|
343
|
+
return sized(d.style) || (d.runs?.some(sized) ?? false) ? scale : 1;
|
|
344
|
+
}
|
|
345
|
+
var layoutGeneration = () => faceGeneration() + textEngineGeneration();
|
|
346
|
+
var SCREEN_PAINT_SLOT = "shape:paint:text-screen";
|
|
347
|
+
function paintText(d, pose, inherited, scale) {
|
|
348
|
+
const laid = textCommandFromPose(textPoseOf(d, pose, inherited), scale);
|
|
349
|
+
const text = d.overflow === "clip" ? clippedToBox(laid, pose) : laid;
|
|
350
|
+
const bg = resolveFill(d.background);
|
|
351
|
+
if (!bg) return [text];
|
|
352
|
+
const fill = commandFill(bg, pose);
|
|
353
|
+
const { shadow, edge } = textFrameOf(d, pose);
|
|
354
|
+
const out = [];
|
|
355
|
+
if (shadow) {
|
|
356
|
+
const paint = { paint: { kind: "solid", color: shadow.color } };
|
|
357
|
+
out.push({
|
|
358
|
+
kind: "group",
|
|
359
|
+
children: shadow.paths.map((path, i) => ({ kind: "group", alpha: shadow.alphas[i], children: [{ kind: "path", path, fill: paint }] }))
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
out.push({ kind: "path", path: { kind: "rect", x: pose.x, y: pose.y, width: pose.width, height: pose.height }, ...fill ? { fill } : {} });
|
|
363
|
+
if (edge) out.push({ kind: "path", path: edge.path, stroke: { paint: { kind: "solid", color: edge.color }, width: edge.width } });
|
|
364
|
+
out.push(text);
|
|
365
|
+
return out;
|
|
366
|
+
}
|
|
367
|
+
function cutRects(rects, box) {
|
|
368
|
+
const out = [];
|
|
369
|
+
for (const r of rects) {
|
|
370
|
+
const x0 = Math.max(r.x, box.x);
|
|
371
|
+
const y0 = Math.max(r.y, box.y);
|
|
372
|
+
const x1 = Math.min(r.x + r.width, box.x + box.width);
|
|
373
|
+
const y1 = Math.min(r.y + r.height, box.y + box.height);
|
|
374
|
+
if (x1 > x0 && y1 > y0) out.push({ x: x0, y: y0, width: x1 - x0, height: y1 - y0 });
|
|
375
|
+
}
|
|
376
|
+
return out;
|
|
377
|
+
}
|
|
378
|
+
function strokesOf(d, inherited) {
|
|
379
|
+
const out = [];
|
|
380
|
+
const own = resolveStroke(d.stroke, inherited?.stroke);
|
|
381
|
+
if (own) out.push(own);
|
|
382
|
+
for (const r of d.runs ?? []) {
|
|
383
|
+
const s = r.stroke === void 0 ? null : resolveStroke(r.stroke, inherited?.stroke);
|
|
384
|
+
if (s) out.push(s);
|
|
385
|
+
}
|
|
386
|
+
return out;
|
|
387
|
+
}
|
|
388
|
+
function strokedLinesBox(pose, lines, reach) {
|
|
389
|
+
if (!(reach.x > 0 || reach.y > 0)) return null;
|
|
390
|
+
let x0 = pose.x, y0 = pose.y, x1 = pose.x + pose.width, y1 = pose.y + pose.height;
|
|
391
|
+
for (const l of lines) {
|
|
392
|
+
const lx0 = Math.max(l.x, pose.x), ly0 = Math.max(l.y, pose.y);
|
|
393
|
+
const lx1 = Math.min(l.x + l.width, pose.x + pose.width), ly1 = Math.min(l.y + l.height, pose.y + pose.height);
|
|
394
|
+
if (lx1 <= lx0 || ly1 <= ly0) continue;
|
|
395
|
+
x0 = Math.min(x0, lx0 - reach.x);
|
|
396
|
+
y0 = Math.min(y0, ly0 - reach.y);
|
|
397
|
+
x1 = Math.max(x1, lx1 + reach.x);
|
|
398
|
+
y1 = Math.max(y1, ly1 + reach.y);
|
|
399
|
+
}
|
|
400
|
+
return { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
|
|
401
|
+
}
|
|
402
|
+
function rectsToPath(rects) {
|
|
403
|
+
const commands = new Uint8Array(rects.length * 6);
|
|
404
|
+
const coords = new Float32Array(rects.length * 10);
|
|
405
|
+
let ci = 0;
|
|
406
|
+
let cmd = 0;
|
|
407
|
+
for (const r of rects) {
|
|
408
|
+
const { x, y, width: w, height: h } = r;
|
|
409
|
+
commands[cmd++] = PATH_M;
|
|
410
|
+
coords[ci++] = x;
|
|
411
|
+
coords[ci++] = y;
|
|
412
|
+
commands[cmd++] = PATH_L;
|
|
413
|
+
coords[ci++] = x + w;
|
|
414
|
+
coords[ci++] = y;
|
|
415
|
+
commands[cmd++] = PATH_L;
|
|
416
|
+
coords[ci++] = x + w;
|
|
417
|
+
coords[ci++] = y + h;
|
|
418
|
+
commands[cmd++] = PATH_L;
|
|
419
|
+
coords[ci++] = x;
|
|
420
|
+
coords[ci++] = y + h;
|
|
421
|
+
commands[cmd++] = PATH_L;
|
|
422
|
+
coords[ci++] = x;
|
|
423
|
+
coords[ci++] = y;
|
|
424
|
+
commands[cmd++] = PATH_Z;
|
|
425
|
+
}
|
|
426
|
+
return { kind: "polygon", commands, coords, fillRule: "nonzero" };
|
|
427
|
+
}
|
|
428
|
+
function textEditJoin(id) {
|
|
429
|
+
return { coalesceKey: `textEdit:${id}` };
|
|
430
|
+
}
|
|
431
|
+
function editedData(data, edit2, { setText, setRuns, setStyle }) {
|
|
432
|
+
let next = setText ? setText(data, edit2.text) : { ...data, text: edit2.text };
|
|
433
|
+
if (edit2.runs) next = setRuns ? setRuns(next, [...edit2.runs]) : { ...next, runs: edit2.runs };
|
|
434
|
+
if (edit2.style) next = setStyle ? setStyle(next, edit2.style) : { ...next, style: edit2.style };
|
|
435
|
+
return next;
|
|
436
|
+
}
|
|
437
|
+
function sceneTextDrafts(sceneRef, optsRef) {
|
|
438
|
+
let held = null;
|
|
439
|
+
const release = (id) => {
|
|
440
|
+
if (held?.id !== id) return;
|
|
441
|
+
held.hold.drop();
|
|
442
|
+
held = null;
|
|
443
|
+
};
|
|
444
|
+
return {
|
|
445
|
+
draft: (id, draft) => {
|
|
446
|
+
const scene = sceneRef.current;
|
|
447
|
+
if (scene.get(asNodeId(id)) === void 0) return;
|
|
448
|
+
if (held !== null && (held.id !== id || held.scene !== scene)) release(held.id);
|
|
449
|
+
held ??= { id, scene, hold: scene.overrides.hold() };
|
|
450
|
+
const opts = optsRef.current;
|
|
451
|
+
held.hold.show([[asNodeId(id), { data: (stored) => editedData(stored, draft, opts) }]]);
|
|
452
|
+
},
|
|
453
|
+
commit: (id, data) => {
|
|
454
|
+
const scene = sceneRef.current;
|
|
455
|
+
scene.batch("Edit text", () => scene.update(asNodeId(id), { data }), textEditJoin(id));
|
|
456
|
+
release(id);
|
|
457
|
+
},
|
|
458
|
+
end: release,
|
|
459
|
+
close: () => {
|
|
460
|
+
if (held !== null) release(held.id);
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function textHitAt(scene, read, cx, cy, opts = {}) {
|
|
465
|
+
const order = indexedOrderAt(scene, cx, cy) ?? nodesAsShown(scene.overrides, scene.renderOrderNodes());
|
|
466
|
+
const shown = shownLookup(scene);
|
|
467
|
+
const paintedPoseOf = scenePaintedPoseOf(scene);
|
|
468
|
+
const derivedPathOf = sceneDerivedPathOf(scene);
|
|
469
|
+
for (let i = order.length - 1; i >= 0; i--) {
|
|
470
|
+
const node = order[i];
|
|
471
|
+
if (scene.isLocked(node.id) || scene.isHidden(node.id)) continue;
|
|
472
|
+
if (!read.isText(node.data)) {
|
|
473
|
+
const source = effectSourceFor(scene, node.id, "items");
|
|
474
|
+
const text = source === null || scene.isLocked(source) ? void 0 : shown.get(source);
|
|
475
|
+
if (text === void 0 || !read.isText(text.data)) continue;
|
|
476
|
+
const path = derivedPathOf(node);
|
|
477
|
+
if (path !== null && onAGlyph(path, cx, cy)) return { id: String(source), caret: read.text(text.data).length };
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const at = paintedPoseOf(node);
|
|
481
|
+
const p = worldToLocal(targetTransformOf(at), cx, cy);
|
|
482
|
+
if (!p) continue;
|
|
483
|
+
const pose = {
|
|
484
|
+
...textBoxOf({ padding: read.padding(node.data) }, at),
|
|
485
|
+
text: read.text(node.data),
|
|
486
|
+
runs: read.runs(node.data),
|
|
487
|
+
style: read.style(node.data),
|
|
488
|
+
verticalAlign: read.verticalAlign(node.data),
|
|
489
|
+
fit: read.fit?.(node.data)
|
|
490
|
+
};
|
|
491
|
+
const box = { x: at.x, y: at.y, width: at.width, height: at.height };
|
|
492
|
+
if (!textPoseContainsPoint(p.x, p.y, { ...pose, ...box })) continue;
|
|
493
|
+
const scale = textLayoutScale({ style: pose.style, runs: pose.runs }, opts.scale);
|
|
494
|
+
return { id: String(node.id), caret: caretIndexAt(p.x, p.y, pose, { scale }) };
|
|
495
|
+
}
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
function indexedOrderAt(scene, cx, cy) {
|
|
499
|
+
const broad = { box: { x: cx, y: cy, width: 0, height: 0 } };
|
|
500
|
+
const found = indexedCandidates(scene, AUTO_POSE_DESCRIPTOR, () => broad);
|
|
501
|
+
return found;
|
|
502
|
+
}
|
|
503
|
+
function onAGlyph(path, x, y) {
|
|
504
|
+
const parts = derivedPartsOf(path);
|
|
505
|
+
if (parts === void 0) return pathContainsPoint(path, x, y);
|
|
506
|
+
return parts.some(({ path: part }) => {
|
|
507
|
+
const b = boundsOfPath(part);
|
|
508
|
+
return rectContainsPoint(b, x, y);
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
var editors = /* @__PURE__ */ new Map();
|
|
512
|
+
function registerHostTextEditor(container, scene, dep) {
|
|
513
|
+
const key = /* @__PURE__ */ Symbol("textEditor");
|
|
514
|
+
editors.set(key, { container, scene, dep });
|
|
515
|
+
const standIn = registerPaintStandIn(scene, container, withoutText);
|
|
516
|
+
return {
|
|
517
|
+
setEditing(id) {
|
|
518
|
+
if (editors.has(key)) standIn.show(id);
|
|
519
|
+
},
|
|
520
|
+
dispose() {
|
|
521
|
+
editors.delete(key);
|
|
522
|
+
standIn.dispose();
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
function hostTextEditDep(el, scene) {
|
|
527
|
+
for (const e of editors.values()) if (e.scene === scene && e.container.contains(el)) return e.dep;
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
function screenPoseOf(d, painted, read, view2, origin) {
|
|
531
|
+
const inset = textBoxOf({ padding: read.padding(d) }, painted);
|
|
532
|
+
const box = screenBoxOf({ ...painted, ...inset }, view2, origin);
|
|
533
|
+
const style = read.style(d);
|
|
534
|
+
const runs = read.runs(d);
|
|
535
|
+
const scale = textLayoutScale({ style, runs }, view2 ? meanScale(view2.scale) : void 0);
|
|
536
|
+
const k = textFitScale({
|
|
537
|
+
...inset,
|
|
538
|
+
text: read.text(d),
|
|
539
|
+
runs,
|
|
540
|
+
style,
|
|
541
|
+
verticalAlign: read.verticalAlign(d),
|
|
542
|
+
fit: read.fit?.(d)
|
|
543
|
+
}, scale);
|
|
544
|
+
const clip = read.overflow(d) === "clip" ? clipToBox(inset, painted) : null;
|
|
545
|
+
return {
|
|
546
|
+
...box,
|
|
547
|
+
width: box.width / k,
|
|
548
|
+
height: box.height / k,
|
|
549
|
+
zoom: box.zoom * k,
|
|
550
|
+
fontSize: resolveScreenLength(style?.fontSize ?? DEFAULT_TEXT_STYLE.fontSize, scale),
|
|
551
|
+
pxScale: scale,
|
|
552
|
+
verticalAlign: read.verticalAlign(d),
|
|
553
|
+
...clip ? { clip: { top: clip.top / k, left: clip.left / k, bottom: clip.bottom / k, right: clip.right / k } } : {}
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
function clipToBox(inner, box) {
|
|
557
|
+
return {
|
|
558
|
+
top: inner.y - box.y,
|
|
559
|
+
left: inner.x - box.x,
|
|
560
|
+
bottom: box.y + box.height - (inner.y + inner.height),
|
|
561
|
+
right: box.x + box.width - (inner.x + inner.width)
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function screenBoxOf(pose, view2, origin) {
|
|
565
|
+
const zoom = view2 ? view2.scale.x : 1;
|
|
566
|
+
const zoomY = view2 ? view2.scale.y : 1;
|
|
567
|
+
const cx = (origin?.x ?? 0) + (pose.x + pose.width / 2 - (view2?.x ?? 0)) * zoom;
|
|
568
|
+
const cy = (origin?.y ?? 0) + (pose.y + pose.height / 2 - (view2?.y ?? 0)) * zoomY;
|
|
569
|
+
return {
|
|
570
|
+
x: cx - pose.width * zoom / 2,
|
|
571
|
+
y: cy - pose.height * zoom / 2,
|
|
572
|
+
width: pose.width,
|
|
573
|
+
height: pose.height,
|
|
574
|
+
zoom,
|
|
575
|
+
frame: frameLinearOf(pose, zoomY / zoom)
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
function frameLinearOf(pose, stretch) {
|
|
579
|
+
const m = poseFrameOf(pose);
|
|
580
|
+
if (!m && stretch === 1) return void 0;
|
|
581
|
+
const [a, b, c, d] = m ?? [1, 0, 0, 1];
|
|
582
|
+
return [a, b * stretch, c, d * stretch];
|
|
583
|
+
}
|
|
584
|
+
function resolveView(view2) {
|
|
585
|
+
return typeof view2 === "function" ? view2() : view2;
|
|
586
|
+
}
|
|
587
|
+
function projectionsOf(opts) {
|
|
588
|
+
return {
|
|
589
|
+
isText: (data) => opts.current.isText ? opts.current.isText(data) : isTextData(data),
|
|
590
|
+
text: (data) => opts.current.getText ? opts.current.getText(data) : data.text ?? "",
|
|
591
|
+
style: (data) => opts.current.getStyle ? opts.current.getStyle(data) : data.style,
|
|
592
|
+
runs: (data) => opts.current.getRuns ? opts.current.getRuns(data) : data.runs,
|
|
593
|
+
verticalAlign: (data) => opts.current.getVerticalAlign ? opts.current.getVerticalAlign(data) : data.verticalAlign,
|
|
594
|
+
padding: (data) => opts.current.getPadding ? opts.current.getPadding(data) : data.padding,
|
|
595
|
+
overflow: (data) => opts.current.getOverflow ? opts.current.getOverflow(data) : data.overflow,
|
|
596
|
+
fit: (data) => opts.current.getFit ? opts.current.getFit(data) : data.fit
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
function useSceneTextEdit(scene, container, options = {}) {
|
|
600
|
+
const host = useRef(null);
|
|
601
|
+
const edit2 = useSceneTextEditor(scene, container, options);
|
|
602
|
+
const editRef = useLatest(edit2);
|
|
603
|
+
useEffect(() => {
|
|
604
|
+
if (!container) return void 0;
|
|
605
|
+
const handle = registerHostTextEditor(container, scene, {
|
|
606
|
+
startEdit: (id, opts) => editRef.current.startEdit(id, opts),
|
|
607
|
+
textAt: (at) => editRef.current.textAt(at),
|
|
608
|
+
isTextNode: (id) => editRef.current.isTextNode(id)
|
|
609
|
+
});
|
|
610
|
+
host.current = handle;
|
|
611
|
+
return () => {
|
|
612
|
+
handle.dispose();
|
|
613
|
+
if (host.current === handle) host.current = null;
|
|
614
|
+
};
|
|
615
|
+
}, [container, scene, editRef]);
|
|
616
|
+
useEffect(() => {
|
|
617
|
+
host.current?.setEditing(edit2.editingId);
|
|
618
|
+
}, [edit2.editingId, container, scene]);
|
|
619
|
+
return edit2;
|
|
620
|
+
}
|
|
621
|
+
function useSceneTextEditor(scene, container, options = {}, canvas = () => null) {
|
|
622
|
+
const sceneRef = useLatest(scene);
|
|
623
|
+
const optsRef = useLatest(options);
|
|
624
|
+
const containerRef = useLatest(container);
|
|
625
|
+
const canvasRef = useLatest(canvas);
|
|
626
|
+
const drafts = useMemo(() => sceneTextDrafts(sceneRef, optsRef), [sceneRef, optsRef]);
|
|
627
|
+
useEffect(() => () => drafts.close(), [drafts]);
|
|
628
|
+
const surface = useCallback(() => {
|
|
629
|
+
const root = containerRef.current;
|
|
630
|
+
const canvas2 = root ? findMountedCanvas(root, canvasRef.current()) : null;
|
|
631
|
+
if (!root || !canvas2) return null;
|
|
632
|
+
const a = layoutBoxOf(canvas2.element);
|
|
633
|
+
const b = layoutBoxOf(root);
|
|
634
|
+
return {
|
|
635
|
+
canvas: canvas2,
|
|
636
|
+
box: {
|
|
637
|
+
x: (a.left - b.left) / b.scale.x - root.clientLeft + root.scrollLeft,
|
|
638
|
+
y: (a.top - b.top) / b.scale.y - root.clientTop + root.scrollTop,
|
|
639
|
+
width: a.width,
|
|
640
|
+
height: a.height
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
}, [containerRef, canvasRef]);
|
|
644
|
+
const read = useMemo(() => projectionsOf(optsRef), [optsRef]);
|
|
645
|
+
const edit2 = useTextEdit({
|
|
646
|
+
container,
|
|
647
|
+
isEditorChrome: (el) => optsRef.current.isEditorChrome?.(el) ?? false,
|
|
648
|
+
escape: options.escape,
|
|
649
|
+
fontHold: options.fontHold,
|
|
650
|
+
onStart: (id) => optsRef.current.onStart?.(id),
|
|
651
|
+
onEnd: (id) => {
|
|
652
|
+
drafts.end(id);
|
|
653
|
+
optsRef.current.onEnd?.(id);
|
|
654
|
+
sceneRef.current.history.seal();
|
|
655
|
+
},
|
|
656
|
+
onInput: drafts.draft,
|
|
657
|
+
getText: (id) => {
|
|
658
|
+
const node = sceneRef.current.get(asNodeId(id));
|
|
659
|
+
return node ? read.text(node.data) : "";
|
|
660
|
+
},
|
|
661
|
+
getStyle: (id) => {
|
|
662
|
+
const node = sceneRef.current.get(asNodeId(id));
|
|
663
|
+
return node ? read.style(node.data) : void 0;
|
|
664
|
+
},
|
|
665
|
+
getPaint: (id) => {
|
|
666
|
+
const node = sceneRef.current.get(asNodeId(id));
|
|
667
|
+
if (!node) return void 0;
|
|
668
|
+
const get = optsRef.current.getPaint;
|
|
669
|
+
return get ? get(node.data) : shownTextPaint(sceneRef.current, node);
|
|
670
|
+
},
|
|
671
|
+
getScreenPose: (id) => {
|
|
672
|
+
const scene2 = sceneRef.current;
|
|
673
|
+
const node = shownLookup(scene2).get(asNodeId(id));
|
|
674
|
+
if (!node) return null;
|
|
675
|
+
const at = surface();
|
|
676
|
+
const view2 = resolveView(optsRef.current.view) ?? at?.canvas.getView();
|
|
677
|
+
return screenPoseOf(node.data, scenePaintedPoseOf(scene2)(node), read, view2, at?.box);
|
|
678
|
+
},
|
|
679
|
+
getClipRect: () => surface()?.box ?? null,
|
|
680
|
+
getRuns: (id) => {
|
|
681
|
+
const node = sceneRef.current.get(asNodeId(id));
|
|
682
|
+
return node ? read.runs(node.data) : void 0;
|
|
683
|
+
},
|
|
684
|
+
onChange: (id, { text, runs, style }) => {
|
|
685
|
+
const nid = asNodeId(id);
|
|
686
|
+
const node = sceneRef.current.get(nid);
|
|
687
|
+
if (!node || sceneRef.current.isLocked(nid)) return;
|
|
688
|
+
drafts.commit(id, editedData(node.data, { text, runs, style }, optsRef.current));
|
|
689
|
+
}
|
|
690
|
+
});
|
|
691
|
+
const textAt = useCallback(
|
|
692
|
+
(at) => {
|
|
693
|
+
const view2 = resolveView(optsRef.current.view) ?? surface()?.canvas.getView();
|
|
694
|
+
return textHitAt(sceneRef.current, read, at.x, at.y, { scale: view2 ? meanScale(view2.scale) : void 0 });
|
|
695
|
+
},
|
|
696
|
+
[read, sceneRef, optsRef, surface]
|
|
697
|
+
);
|
|
698
|
+
const isTextNode = useCallback((id) => {
|
|
699
|
+
const node = sceneRef.current.get(asNodeId(id));
|
|
700
|
+
return node !== void 0 && read.isText(node.data);
|
|
701
|
+
}, [read, sceneRef]);
|
|
702
|
+
const drawOne = useMemo(() => drawOneWithoutEditedText(edit2.editingId), [edit2.editingId]);
|
|
703
|
+
return { ...edit2, textAt, isTextNode, drawOne };
|
|
704
|
+
}
|
|
705
|
+
function shownTextPaint(scene, node) {
|
|
706
|
+
const { fill, stroke } = withInheritedPaint(node.data, inheritedPaintIn(scene, node));
|
|
707
|
+
return { fill, stroke };
|
|
708
|
+
}
|
|
709
|
+
function createTextHandler(opts = {}) {
|
|
710
|
+
const measure = opts.measure ?? ((text) => measureTextBounds(text));
|
|
711
|
+
return {
|
|
712
|
+
id: "text-tools:text",
|
|
713
|
+
match: (item) => item.kind === "string" && item.mime === "text/plain",
|
|
714
|
+
types: ["text/plain"],
|
|
715
|
+
priority: -110,
|
|
716
|
+
handle(items, ctx) {
|
|
717
|
+
if ((ctx.items ?? items).some((it) => it.kind === "file")) return;
|
|
718
|
+
let index = 0;
|
|
719
|
+
for (const item of items) {
|
|
720
|
+
if (item.kind !== "string") continue;
|
|
721
|
+
const text = item.text.replace(/\r\n?/g, "\n").replace(/\n+$/, "");
|
|
722
|
+
if (text.trim() === "") continue;
|
|
723
|
+
ctx.insert.commit(arrivalBounds(ctx, measure(text), index), { kind: "text", text });
|
|
724
|
+
index++;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
var textContentHandler = createTextHandler();
|
|
730
|
+
function typeIn(data) {
|
|
731
|
+
if (data.text !== "") return { data };
|
|
732
|
+
return {
|
|
733
|
+
data,
|
|
734
|
+
batch: textEditJoin,
|
|
735
|
+
inserted: (id, deps) => deps.get("textEdit")?.startEdit(id, { caret: 0 })
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
var textNodeFactory = (_bounds, extras) => {
|
|
739
|
+
const e = extras;
|
|
740
|
+
const fill = e.style?.fill;
|
|
741
|
+
return typeIn({ text: e.text ?? "", fill: fill === void 0 ? DEFAULT_TEXT_FILL : fill && { paint: fill } });
|
|
742
|
+
};
|
|
743
|
+
var TWO_PLACES = decimal({ maxPlaces: 2, grouping: false });
|
|
744
|
+
var italicEncoding = {
|
|
745
|
+
read: (stored) => stored === "italic",
|
|
746
|
+
write: (on) => on ? "italic" : void 0
|
|
747
|
+
};
|
|
748
|
+
var overflowEncoding = {
|
|
749
|
+
read: (stored) => stored === "clip",
|
|
750
|
+
write: (on) => on ? "clip" : void 0
|
|
751
|
+
};
|
|
752
|
+
var fitEncoding = {
|
|
753
|
+
read: (stored) => stored === "shrink",
|
|
754
|
+
write: (on) => on ? "shrink" : void 0
|
|
755
|
+
};
|
|
756
|
+
var BACKGROUND_LEAF = {
|
|
757
|
+
kind: "object",
|
|
758
|
+
block: true,
|
|
759
|
+
name: "Background",
|
|
760
|
+
description: "Paint behind the text, over the whole box.",
|
|
761
|
+
default: { paint: { kind: "solid", color: "#ffffffff" } },
|
|
762
|
+
children: {
|
|
763
|
+
paint: { kind: "paint", name: "Background", description: "Paint behind the text.", default: { kind: "solid", color: "#ffffffff" }, alpha: true, block: true }
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
var CONTENT = {
|
|
767
|
+
name: "Content",
|
|
768
|
+
members: {
|
|
769
|
+
// `block`: the content is the section, so a 64px label column
|
|
770
|
+
// reading "Text" inside a section reading "Text" is one word
|
|
771
|
+
// twice and a narrower field for the sake of it.
|
|
772
|
+
"data.text": { kind: "string", name: "Text", description: "Text content.", absentIsDefault: true, default: "", block: true }
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
var TYPOGRAPHY = {
|
|
776
|
+
// Headless: `Character` and `Paragraph` carry the labels, and a
|
|
777
|
+
// `Typography` heading over them names nothing a reader can't
|
|
778
|
+
// already see.
|
|
779
|
+
name: "",
|
|
780
|
+
members: {
|
|
781
|
+
// One object leaf, not a row of siblings addressing into it:
|
|
782
|
+
// `data.style` is a `TextStyle`, and its character and
|
|
783
|
+
// paragraph fields are one value that reads as two lists.
|
|
784
|
+
// The groups head those lists and contribute nothing to the
|
|
785
|
+
// path — a field inside one is still a field of the style.
|
|
786
|
+
"data.style": {
|
|
787
|
+
kind: "object",
|
|
788
|
+
name: "Style",
|
|
789
|
+
description: "Typography for the whole node. Its color is the node's own `data.fill`, in Appearance.",
|
|
790
|
+
default: {},
|
|
791
|
+
absentIsDefault: true,
|
|
792
|
+
block: true,
|
|
793
|
+
children: {
|
|
794
|
+
character: {
|
|
795
|
+
name: "Character",
|
|
796
|
+
members: {
|
|
797
|
+
fontFamily: { kind: "font-family", name: "Font", description: "Registered font family.", absentIsDefault: true, default: "sans-serif" },
|
|
798
|
+
fontSize: { kind: "number", display: TWO_PLACES, name: "Size", description: "Font size, world units.", absentIsDefault: true, default: 16, min: 1, step: 1, pair: { with: "data.style.fontWeight", label: "Size / weight" } },
|
|
799
|
+
fontWeight: { kind: "font-weight", name: "Weight", description: "Font weight, from the faces the family has.", absentIsDefault: true, default: 400 },
|
|
800
|
+
// One row of segments, the strip every text editor
|
|
801
|
+
// draws: the slant, the three rules, then the two
|
|
802
|
+
// scripts. Italic is a flag here although the field
|
|
803
|
+
// is `fontStyle`, whose only other value is upright.
|
|
804
|
+
fontStyle: { kind: "boolean", name: "Italic", description: "Set the text in its italic face.", default: false, control: "toggle", icon: (
|
|
805
|
+
/* @icon */
|
|
806
|
+
"core:italic"
|
|
807
|
+
), short: ["I"], pair: { with: ["data.style.underline", "data.style.strikethrough", "data.style.overline", "data.style.script"], label: "Style" }, encoding: italicEncoding },
|
|
808
|
+
underline: { kind: "boolean", name: "Underline", description: "Underline the text.", absentIsDefault: true, default: false, control: "toggle", icon: (
|
|
809
|
+
/* @icon */
|
|
810
|
+
"core:underline"
|
|
811
|
+
), short: ["U"] },
|
|
812
|
+
strikethrough: { kind: "boolean", name: "Strikethrough", description: "Strike through the text.", absentIsDefault: true, default: false, control: "toggle", icon: (
|
|
813
|
+
/* @icon */
|
|
814
|
+
"core:strikethrough"
|
|
815
|
+
), short: ["S"] },
|
|
816
|
+
overline: { kind: "boolean", name: "Overline", description: "Rule the text above its ascent.", absentIsDefault: true, default: false, control: "toggle", icon: (
|
|
817
|
+
/* @icon */
|
|
818
|
+
"core:overline"
|
|
819
|
+
), short: ["O"] },
|
|
820
|
+
script: { kind: "enum", name: "Script", description: "Set the text smaller and raised or lowered, on the line it would otherwise hold.", default: void 0, control: "toggle", clearable: true, options: [{ value: "super", label: "Superscript", icon: (
|
|
821
|
+
/* @icon */
|
|
822
|
+
"core:superscript"
|
|
823
|
+
) }, { value: "sub", label: "Subscript", icon: (
|
|
824
|
+
/* @icon */
|
|
825
|
+
"core:subscript"
|
|
826
|
+
) }] },
|
|
827
|
+
letterSpacing: { kind: "number", display: TWO_PLACES, name: "Tracking", description: "Extra advance per glyph, world units.", absentIsDefault: true, default: 0, step: 0.1 },
|
|
828
|
+
textTransform: { kind: "enum", name: "Case", description: "Draw the text in capitals, lowercase or title case. The text itself is unchanged.", absentIsDefault: true, default: "none", control: "toggle", options: [{ value: "none", label: "None", short: ["\u2013"] }, { value: "uppercase", label: "Uppercase", short: ["AA"] }, { value: "lowercase", label: "Lowercase", short: ["aa"] }, { value: "capitalize", label: "Capitalize", short: ["Aa"] }] },
|
|
829
|
+
fontVariantCaps: { kind: "enum", name: "Small caps", description: "Draw lowercase letters as smaller capitals. The text itself is unchanged.", absentIsDefault: true, default: "normal", control: "toggle", options: [{ value: "normal", label: "Normal", short: ["\u2013"] }, { value: "small-caps", label: "Small caps", short: ["Sc"] }] },
|
|
830
|
+
lang: { kind: "string", name: "Language", description: "A BCP 47 tag, such as sr or ur, that picks the forms a font draws for that language. Empty means none.", default: "" }
|
|
831
|
+
}
|
|
832
|
+
},
|
|
833
|
+
paragraph: {
|
|
834
|
+
name: "Paragraph",
|
|
835
|
+
members: {
|
|
836
|
+
lineHeight: { kind: "number", display: TWO_PLACES, name: "Leading", description: "Line height as a multiple of font size.", absentIsDefault: true, default: 1.2, min: 0.5, step: 0.1 },
|
|
837
|
+
wrap: { kind: "boolean", name: "Wrap", description: "Break lines between words at the box width.", absentIsDefault: true, default: false, control: "switch" },
|
|
838
|
+
tabSize: { kind: "number", display: TWO_PLACES, name: "Tab size", description: "Distance between tab stops, in spaces.", absentIsDefault: true, default: 8, min: 1, step: 1 },
|
|
839
|
+
direction: { kind: "enum", name: "Direction", description: "Reading direction. Auto takes each paragraph's from its first strong character.", absentIsDefault: true, default: "auto", control: "toggle", options: [{ value: "auto", label: "Auto", short: ["Auto"] }, { value: "ltr", label: "Left to right", short: ["LTR"] }, { value: "rtl", label: "Right to left", short: ["RTL"] }] },
|
|
840
|
+
// Last, so the box's vertical alignment — a field of
|
|
841
|
+
// the node rather than of its style — follows it.
|
|
842
|
+
align: { kind: "enum", name: "Align", description: "Horizontal alignment.", absentIsDefault: true, default: "left", control: "toggle", options: [{ value: "left", label: "Left", icon: (
|
|
843
|
+
/* @icon */
|
|
844
|
+
"core:textAlignLeft"
|
|
845
|
+
) }, { value: "center", label: "Center", icon: (
|
|
846
|
+
/* @icon */
|
|
847
|
+
"core:textAlignCenter"
|
|
848
|
+
) }, { value: "right", label: "Right", icon: (
|
|
849
|
+
/* @icon */
|
|
850
|
+
"core:textAlignRight"
|
|
851
|
+
) }, { value: "justify", label: "Justify", icon: (
|
|
852
|
+
/* @icon */
|
|
853
|
+
"core:textAlignJustify"
|
|
854
|
+
) }, { value: "start", label: "Start", icon: (
|
|
855
|
+
/* @icon */
|
|
856
|
+
"core:textAlignStart"
|
|
857
|
+
) }, { value: "end", label: "End", icon: (
|
|
858
|
+
/* @icon */
|
|
859
|
+
"core:textAlignEnd"
|
|
860
|
+
) }] }
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
},
|
|
865
|
+
"data.padding": { kind: "number", display: TWO_PLACES, measure: "length", name: "Padding", description: "Inset from the box to the text on every side, world units.", absentIsDefault: true, default: 0, min: 0, step: 1 },
|
|
866
|
+
"data.verticalAlign": { kind: "enum", name: "Vertical", description: "Where the lines sit in the box's height.", absentIsDefault: true, default: "top", control: "toggle", options: [{ value: "top", label: "Top", icon: (
|
|
867
|
+
/* @icon */
|
|
868
|
+
"core:textAlignTop"
|
|
869
|
+
) }, { value: "center", label: "Middle", icon: (
|
|
870
|
+
/* @icon */
|
|
871
|
+
"core:textAlignMiddle"
|
|
872
|
+
) }, { value: "bottom", label: "Bottom", icon: (
|
|
873
|
+
/* @icon */
|
|
874
|
+
"core:textAlignBottom"
|
|
875
|
+
) }] },
|
|
876
|
+
"data.overflow": { kind: "boolean", name: "Clip", description: "Cut the text at the box's edge instead of drawing it past.", default: false, control: "switch", encoding: overflowEncoding },
|
|
877
|
+
"data.fit": { kind: "boolean", name: "Shrink to fit", description: "Set the text smaller, never larger, until its lines fit the box.", default: false, control: "switch", encoding: fitEncoding }
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
var isHighSurrogate = (s, i) => i >= 0 && i < s.length && (s.charCodeAt(i) & 64512) === 55296;
|
|
881
|
+
function respellRuns(runs, text) {
|
|
882
|
+
const old = runsToPlainText(runs);
|
|
883
|
+
let head = 0;
|
|
884
|
+
while (head < old.length && head < text.length && old[head] === text[head]) head++;
|
|
885
|
+
if (isHighSurrogate(old, head - 1)) head--;
|
|
886
|
+
let tail = 0;
|
|
887
|
+
while (tail < old.length - head && tail < text.length - head && old[old.length - 1 - tail] === text[text.length - 1 - tail]) tail++;
|
|
888
|
+
if (tail > 0 && isHighSurrogate(old, old.length - tail - 1)) tail--;
|
|
889
|
+
const typed = text.slice(head, text.length - tail);
|
|
890
|
+
return replaceRunRange(runs, head, old.length - tail, typed ? [{ ...runStyleAt(runs, head), text: typed }] : []);
|
|
891
|
+
}
|
|
892
|
+
function settleText(next) {
|
|
893
|
+
const data = next.data;
|
|
894
|
+
if (!data || typeof data.text !== "string" || !data.runs?.length || runsToPlainText(data.runs) === data.text) return next;
|
|
895
|
+
return { ...next, data: { ...data, runs: respellRuns(data.runs, data.text) } };
|
|
896
|
+
}
|
|
897
|
+
var TEXT_PROPERTIES = {
|
|
898
|
+
name: "text",
|
|
899
|
+
settle: settleText,
|
|
900
|
+
schema: shapePropertiesSchema({
|
|
901
|
+
// What the node *is* outranks how it is painted: a text node's copy is the first thing an editor reaches for.
|
|
902
|
+
lead: { content: CONTENT },
|
|
903
|
+
appearance: { "data.background": BACKGROUND_LEAF },
|
|
904
|
+
trail: { text: TYPOGRAPHY }
|
|
905
|
+
})
|
|
906
|
+
};
|
|
907
|
+
var optionFill = (option, fallback) => option === void 0 ? fallback : option && { paint: option };
|
|
908
|
+
var STICKY_DEFAULTS = {
|
|
909
|
+
background: { paint: { kind: "solid", color: "#fff1a6ff" } },
|
|
910
|
+
fill: DEFAULT_TEXT_FILL,
|
|
911
|
+
padding: 12,
|
|
912
|
+
style: { fontSize: 18, align: "center", wrap: true },
|
|
913
|
+
verticalAlign: "center",
|
|
914
|
+
overflow: "clip",
|
|
915
|
+
edge: { width: 1, shade: 0.18 },
|
|
916
|
+
shadow: { x: 0, y: 2, blur: 6, alpha: 0.18 },
|
|
917
|
+
// One line of the default type inside the default padding.
|
|
918
|
+
minSize: { width: 48, height: 48 },
|
|
919
|
+
size: { width: 160, height: 160 }
|
|
920
|
+
};
|
|
921
|
+
function stickyData(look) {
|
|
922
|
+
return {
|
|
923
|
+
text: look.text ?? "",
|
|
924
|
+
background: optionFill(look.paper, look.background ?? STICKY_DEFAULTS.background),
|
|
925
|
+
padding: look.padding ?? STICKY_DEFAULTS.padding,
|
|
926
|
+
style: look.style ?? STICKY_DEFAULTS.style,
|
|
927
|
+
verticalAlign: look.verticalAlign ?? STICKY_DEFAULTS.verticalAlign,
|
|
928
|
+
overflow: look.overflow ?? STICKY_DEFAULTS.overflow,
|
|
929
|
+
edge: look.edge ?? STICKY_DEFAULTS.edge,
|
|
930
|
+
shadow: look.shadow ?? STICKY_DEFAULTS.shadow,
|
|
931
|
+
minSize: look.minSize ?? STICKY_DEFAULTS.minSize,
|
|
932
|
+
fill: optionFill(look.ink, look.fill ?? STICKY_DEFAULTS.fill)
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
var stickyNodeFactory = (_bounds, extras) => typeIn(stickyData(extras));
|
|
936
|
+
var STICKY_TOOL_OPTIONS = {
|
|
937
|
+
actionId: ["insert", "place"],
|
|
938
|
+
prefs: {
|
|
939
|
+
name: "Sticky note",
|
|
940
|
+
as: "bar",
|
|
941
|
+
children: {
|
|
942
|
+
paper: { ...styleOptionLeaves({ fill: STICKY_DEFAULTS.background.paint }, ["fill"]).fill, name: "Paper", description: "What a new note is painted on." },
|
|
943
|
+
ink: { ...styleOptionLeaves({ fill: STICKY_DEFAULTS.fill.paint }, ["fill"]).fill, name: "Ink", description: "What a new note\u2019s text is set in." }
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
function createStickyTool(options = {}) {
|
|
948
|
+
const {
|
|
949
|
+
behaviors,
|
|
950
|
+
size = STICKY_DEFAULTS.size,
|
|
951
|
+
minSize = STICKY_DEFAULTS.minSize,
|
|
952
|
+
background,
|
|
953
|
+
fill,
|
|
954
|
+
padding,
|
|
955
|
+
style,
|
|
956
|
+
verticalAlign,
|
|
957
|
+
overflow,
|
|
958
|
+
edge,
|
|
959
|
+
shadow,
|
|
960
|
+
share
|
|
961
|
+
} = options;
|
|
962
|
+
{
|
|
963
|
+
const color = (f) => f === void 0 ? void 0 : toolOptionPaint(f.paint ?? null);
|
|
964
|
+
const toolOptions = withToolOptionDefault(
|
|
965
|
+
withToolOptionDefault(shareToolOptions(STICKY_TOOL_OPTIONS, share), "paper", color(background)),
|
|
966
|
+
"ink",
|
|
967
|
+
color(fill)
|
|
968
|
+
);
|
|
969
|
+
const look = {
|
|
970
|
+
kind: "sticky",
|
|
971
|
+
minSize,
|
|
972
|
+
...padding !== void 0 ? { padding } : {},
|
|
973
|
+
...style !== void 0 ? { style } : {},
|
|
974
|
+
...verticalAlign !== void 0 ? { verticalAlign } : {},
|
|
975
|
+
...overflow !== void 0 ? { overflow } : {},
|
|
976
|
+
...edge !== void 0 ? { edge } : {},
|
|
977
|
+
...shadow !== void 0 ? { shadow } : {}
|
|
978
|
+
};
|
|
979
|
+
return defineTool({
|
|
980
|
+
id: "sticky",
|
|
981
|
+
keybinding: { key: "S" },
|
|
982
|
+
capabilities: ["creates-text"],
|
|
983
|
+
hookName: "useStickyTool",
|
|
984
|
+
options: toolOptions,
|
|
985
|
+
cursor: cursorFor("crosshair", { mark: "rect", fallback: "crosshair" }),
|
|
986
|
+
presentation: {
|
|
987
|
+
label: "Sticky note",
|
|
988
|
+
icon: (
|
|
989
|
+
/* @icon */
|
|
990
|
+
"core:sticky"
|
|
991
|
+
),
|
|
992
|
+
group: "text"
|
|
993
|
+
},
|
|
994
|
+
bindings: [
|
|
995
|
+
{ spec: { kind: "drag", mods: INSERT_DRAG_MODS }, actionId: "insert", opts: { id: "sticky.drag", params: look, ...insertBindingBehaviors(behaviors) } },
|
|
996
|
+
{ spec: { kind: "click" }, actionId: "place", opts: { id: "sticky.click", params: { ...look, size } } },
|
|
997
|
+
{ spec: { kind: "click", target: "selected-body" }, actionId: "enterTextEdit", opts: { id: "sticky.editText" } },
|
|
998
|
+
INSERT_REPOSITION_BINDING
|
|
999
|
+
]
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
function useStickyTool(options = {}) {
|
|
1004
|
+
const {
|
|
1005
|
+
behaviors,
|
|
1006
|
+
size,
|
|
1007
|
+
minSize,
|
|
1008
|
+
background,
|
|
1009
|
+
fill,
|
|
1010
|
+
padding,
|
|
1011
|
+
style,
|
|
1012
|
+
verticalAlign,
|
|
1013
|
+
overflow,
|
|
1014
|
+
edge,
|
|
1015
|
+
shadow,
|
|
1016
|
+
share
|
|
1017
|
+
} = options;
|
|
1018
|
+
return useMemo(
|
|
1019
|
+
() => createStickyTool({ behaviors, size, minSize, background, fill, padding, style, verticalAlign, overflow, edge, shadow, share }),
|
|
1020
|
+
[behaviors, size, minSize, background, fill, padding, style, verticalAlign, overflow, edge, shadow, share]
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
var PRESENTATION = {
|
|
1024
|
+
label: "Text",
|
|
1025
|
+
icon: (
|
|
1026
|
+
/* @icon */
|
|
1027
|
+
"core:text"
|
|
1028
|
+
),
|
|
1029
|
+
group: "text"
|
|
1030
|
+
};
|
|
1031
|
+
var TEXT_TOOL_OPTIONS = shareToolOptions(shapeToolOptions({
|
|
1032
|
+
actionId: "insert",
|
|
1033
|
+
name: "Text",
|
|
1034
|
+
keys: ["fill"],
|
|
1035
|
+
style: { fill: DEFAULT_TEXT_FILL.paint }
|
|
1036
|
+
}), { fill: "textFill" });
|
|
1037
|
+
function createTextTool(options = {}) {
|
|
1038
|
+
const { behaviors, share } = options;
|
|
1039
|
+
return defineTool({
|
|
1040
|
+
id: "text",
|
|
1041
|
+
keybinding: { key: "T" },
|
|
1042
|
+
capabilities: ["creates-text"],
|
|
1043
|
+
hookName: "useTextTool",
|
|
1044
|
+
options: shareToolOptions(TEXT_TOOL_OPTIONS, share),
|
|
1045
|
+
cursor: "text",
|
|
1046
|
+
presentation: PRESENTATION,
|
|
1047
|
+
bindings: [
|
|
1048
|
+
{ spec: { kind: "drag", mods: INSERT_DRAG_MODS }, actionId: "insert", opts: { id: "text.drag", params: { kind: "text" }, ...insertBindingBehaviors(behaviors) } },
|
|
1049
|
+
{ spec: { kind: "click", target: "selected-body" }, actionId: "enterTextEdit", opts: { id: "text.editText" } },
|
|
1050
|
+
INSERT_REPOSITION_BINDING
|
|
1051
|
+
]
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
function useTextTool(options = {}) {
|
|
1055
|
+
const { behaviors, share } = options;
|
|
1056
|
+
return useMemo(() => createTextTool({ behaviors, share }), [behaviors, share]);
|
|
1057
|
+
}
|
|
1058
|
+
function useTextEditDepSource(scene) {
|
|
1059
|
+
const sceneRef = useLatest(scene);
|
|
1060
|
+
useDepSource("textEdit", () => {
|
|
1061
|
+
const sc = sceneRef.current;
|
|
1062
|
+
return {
|
|
1063
|
+
startEdit(_id, _opts) {
|
|
1064
|
+
if (isDev$1()) {
|
|
1065
|
+
console.warn(
|
|
1066
|
+
'weasel textEditDep: startEdit called but this canvas has no text editor: it was given createTextPreset({ editor: false }), or a host useSceneTextEdit holds it without publishing the "textEdit" dep. Publish it with useDepSource("textEdit", ...) from inside <SceneCanvas>.'
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
},
|
|
1070
|
+
textAt: () => null,
|
|
1071
|
+
isTextNode(id) {
|
|
1072
|
+
return isTextData(sc.get(id)?.data);
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
function useDefaultTextEditor(scene, elementRef, config, on) {
|
|
1078
|
+
const [element, setElement] = useState(null);
|
|
1079
|
+
useLayoutEffect(() => {
|
|
1080
|
+
if (elementRef.current !== element) setElement(elementRef.current);
|
|
1081
|
+
});
|
|
1082
|
+
const live = on && config !== false;
|
|
1083
|
+
const container = live && element ? element.offsetParent ?? element.parentElement : null;
|
|
1084
|
+
const elRef = useLatest(element);
|
|
1085
|
+
const edit2 = useSceneTextEditor(
|
|
1086
|
+
scene,
|
|
1087
|
+
container,
|
|
1088
|
+
config || {},
|
|
1089
|
+
() => elRef.current
|
|
1090
|
+
);
|
|
1091
|
+
const editRef = useLatest(edit2);
|
|
1092
|
+
const dep = useMemo(() => ({
|
|
1093
|
+
startEdit: (id, opts) => editRef.current.startEdit(id, opts),
|
|
1094
|
+
textAt: (at) => editRef.current.textAt(at),
|
|
1095
|
+
isTextNode: (id) => editRef.current.isTextNode(id)
|
|
1096
|
+
}), [editRef]);
|
|
1097
|
+
const sceneRef = useLatest(scene);
|
|
1098
|
+
const host = useMemo(() => () => {
|
|
1099
|
+
const el = elRef.current;
|
|
1100
|
+
return el === null ? null : hostTextEditDep(el, sceneRef.current);
|
|
1101
|
+
}, [elRef, sceneRef]);
|
|
1102
|
+
return { edit: edit2, container, dep, element: container ? element : null, host };
|
|
1103
|
+
}
|
|
1104
|
+
function DefaultTextEditDep({ editor }) {
|
|
1105
|
+
const { dep, element, host } = editor;
|
|
1106
|
+
useDepSource("textEdit", (below) => host() ?? (element ? dep : below()));
|
|
1107
|
+
return null;
|
|
1108
|
+
}
|
|
1109
|
+
function useEditingStandIn(scene, editor) {
|
|
1110
|
+
const within = editor.element;
|
|
1111
|
+
const editing = editor.edit.editingId;
|
|
1112
|
+
const editingRef = useLatest(editing);
|
|
1113
|
+
const standIn = useRef(null);
|
|
1114
|
+
useEffect(() => {
|
|
1115
|
+
if (!within) return void 0;
|
|
1116
|
+
const held = registerPaintStandIn(scene, within, withoutText);
|
|
1117
|
+
held.show(editingRef.current);
|
|
1118
|
+
standIn.current = held;
|
|
1119
|
+
return () => {
|
|
1120
|
+
held.dispose();
|
|
1121
|
+
standIn.current = null;
|
|
1122
|
+
};
|
|
1123
|
+
}, [scene, within, editingRef]);
|
|
1124
|
+
useEffect(() => {
|
|
1125
|
+
standIn.current?.show(editing);
|
|
1126
|
+
}, [editing]);
|
|
1127
|
+
}
|
|
1128
|
+
function TextEditStub({ scene }) {
|
|
1129
|
+
useTextEditDepSource(scene);
|
|
1130
|
+
return null;
|
|
1131
|
+
}
|
|
1132
|
+
function canvasTextEditor(config) {
|
|
1133
|
+
function CanvasTextEditor({ scene, canvasRef, paintsInto }) {
|
|
1134
|
+
const editor = useDefaultTextEditor(scene, canvasRef, config, !paintsInto);
|
|
1135
|
+
useEditingStandIn(scene, editor);
|
|
1136
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1137
|
+
/* @__PURE__ */ jsx(TextEditStub, { scene }),
|
|
1138
|
+
/* @__PURE__ */ jsx(DefaultTextEditDep, { editor })
|
|
1139
|
+
] });
|
|
1140
|
+
}
|
|
1141
|
+
return CanvasTextEditor;
|
|
1142
|
+
}
|
|
1143
|
+
var textNodeRouting = {
|
|
1144
|
+
name: "text",
|
|
1145
|
+
matches: (data, node) => node?.kind !== "group" && isTextData(data)
|
|
1146
|
+
};
|
|
1147
|
+
function createTextPreset(options = {}) {
|
|
1148
|
+
return {
|
|
1149
|
+
id: "text",
|
|
1150
|
+
label: "Text",
|
|
1151
|
+
shapes: [TEXT_PAINTER],
|
|
1152
|
+
routing: [textNodeRouting],
|
|
1153
|
+
properties: [TEXT_PROPERTIES],
|
|
1154
|
+
insertNodeFactories: { text: textNodeFactory, sticky: stickyNodeFactory },
|
|
1155
|
+
actions: [enterTextEditAction, enterTextEditAtAction, editEffectTextAction],
|
|
1156
|
+
components: [canvasTextEditor(options.editor)],
|
|
1157
|
+
contentHandlers: [textContentHandler]
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
var textPreset = createTextPreset();
|
|
1161
|
+
function createTextToolsPreset(options = {}) {
|
|
1162
|
+
const { behaviors, share, sticky, text = textPreset } = options;
|
|
1163
|
+
return {
|
|
1164
|
+
id: "tools:text",
|
|
1165
|
+
label: "Text tools",
|
|
1166
|
+
tools: [createTextTool({ behaviors, share }), createStickyTool({ ...sticky, behaviors, share })],
|
|
1167
|
+
includes: [text, presets.insert]
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
var textToolsPreset = createTextToolsPreset();
|
|
1171
|
+
var whiteboardPreset = {
|
|
1172
|
+
id: "whiteboard",
|
|
1173
|
+
label: "Whiteboard",
|
|
1174
|
+
includes: [presets.view, presets.pick, presets.move, presets.resize, presets.edit, textToolsPreset]
|
|
1175
|
+
};
|
|
1176
|
+
|
|
1177
|
+
export { STICKY_DEFAULTS, STICKY_TOOL_OPTIONS, TEXT_PAINTER, TEXT_PROPERTIES, TEXT_TOOL_OPTIONS, clippedToBox, createStickyTool, createTextHandler, createTextPreset, createTextTool, createTextToolsPreset, drawOneWithoutEditedText, edgeColor, editEffectTextAction, enterTextEditAction, enterTextEditAtAction, stickyData, stickyNodeFactory, textBoxOf, textContentHandler, textEditJoin, textEffectItems, textFrameOf, textLayoutScale, textNodeFactory, textNodeRouting, textOutlineOf, textPoseOf, textPreset, textToolsPreset, useSceneTextEdit, useStickyTool, useTextTool, whiteboardPreset, withoutText };
|
|
1178
|
+
//# sourceMappingURL=chunk-VLXWRY3D.js.map
|
|
1179
|
+
//# sourceMappingURL=chunk-VLXWRY3D.js.map
|