@yuzhouu/canvas-kit 0.1.2
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 +145 -0
- package/_vendor/canvas-core/bindings/BindingUtil.ts +92 -0
- package/_vendor/canvas-core/bindings/BindingUtilRegistry.ts +22 -0
- package/_vendor/canvas-core/bindings/arrow/arrowBindingHelpers.ts +52 -0
- package/_vendor/canvas-core/contextMenu.ts +85 -0
- package/_vendor/canvas-core/editor/Editor.ts +2614 -0
- package/_vendor/canvas-core/editor/HistoryManager.ts +83 -0
- package/_vendor/canvas-core/editor/StateNode.ts +162 -0
- package/_vendor/canvas-core/editor/camera.ts +6 -0
- package/_vendor/canvas-core/editor/clipboard.ts +478 -0
- package/_vendor/canvas-core/editor/commands.ts +833 -0
- package/_vendor/canvas-core/editor/edgeScroll.ts +37 -0
- package/_vendor/canvas-core/editor/editorComputedHelpers.ts +257 -0
- package/_vendor/canvas-core/editor/editorSelectionTransforms.ts +116 -0
- package/_vendor/canvas-core/editor/events.ts +93 -0
- package/_vendor/canvas-core/editor/index.ts +12 -0
- package/_vendor/canvas-core/editor/interactions.ts +101 -0
- package/_vendor/canvas-core/editor/internal.ts +22 -0
- package/_vendor/canvas-core/editor/managers/BindingManager.ts +262 -0
- package/_vendor/canvas-core/editor/managers/CameraManager.ts +147 -0
- package/_vendor/canvas-core/editor/managers/ClipboardManager.ts +32 -0
- package/_vendor/canvas-core/editor/managers/DocumentSnapshotManager.ts +256 -0
- package/_vendor/canvas-core/editor/managers/InputsManager.ts +319 -0
- package/_vendor/canvas-core/editor/managers/InteractionTransientManager.ts +218 -0
- package/_vendor/canvas-core/editor/managers/QueryManager.ts +837 -0
- package/_vendor/canvas-core/editor/managers/RenderWindowManager.ts +347 -0
- package/_vendor/canvas-core/editor/managers/SelectionLayoutManager.ts +196 -0
- package/_vendor/canvas-core/editor/managers/SelectionManager.ts +98 -0
- package/_vendor/canvas-core/editor/managers/SelectionOverlayManager.ts +308 -0
- package/_vendor/canvas-core/editor/managers/ShapeEditingManager.ts +226 -0
- package/_vendor/canvas-core/editor/managers/ShapeGeometryManager.ts +162 -0
- package/_vendor/canvas-core/editor/managers/ShapeHierarchyManager.ts +797 -0
- package/_vendor/canvas-core/editor/managers/SideEffectManager.ts +83 -0
- package/_vendor/canvas-core/editor/managers/SnapManager.ts +433 -0
- package/_vendor/canvas-core/editor/managers/SnapSnapshotBuilder.ts +430 -0
- package/_vendor/canvas-core/editor/managers/SpatialIndexManager.ts +354 -0
- package/_vendor/canvas-core/editor/managers/ToolManager.ts +81 -0
- package/_vendor/canvas-core/editor/managers/queryState.ts +583 -0
- package/_vendor/canvas-core/editor/managers/selectionGeometry.ts +287 -0
- package/_vendor/canvas-core/editor/managers/shapeOrder.ts +350 -0
- package/_vendor/canvas-core/editor/managers/shapeReparenting.ts +160 -0
- package/_vendor/canvas-core/editor/managers/snapCalculations.ts +412 -0
- package/_vendor/canvas-core/editor/managers/snapGeometry.ts +202 -0
- package/_vendor/canvas-core/editor/managers/snapIndicators.ts +403 -0
- package/_vendor/canvas-core/editor/managers/snapTypes.ts +122 -0
- package/_vendor/canvas-core/editor/queries/shapeTransforms.ts +171 -0
- package/_vendor/canvas-core/editor/renderWindow.ts +439 -0
- package/_vendor/canvas-core/editor/resizeGeometry.ts +654 -0
- package/_vendor/canvas-core/editor/selection.ts +489 -0
- package/_vendor/canvas-core/editor/selectionHandleInteractions.ts +43 -0
- package/_vendor/canvas-core/editor/selectionOverlay.ts +567 -0
- package/_vendor/canvas-core/exports/integrity.ts +174 -0
- package/_vendor/canvas-core/exports/svg.ts +494 -0
- package/_vendor/canvas-core/geometry/Box.ts +80 -0
- package/_vendor/canvas-core/geometry/Ellipse2d.ts +67 -0
- package/_vendor/canvas-core/geometry/Geometry2d.ts +52 -0
- package/_vendor/canvas-core/geometry/Mat.ts +92 -0
- package/_vendor/canvas-core/geometry/Polyline2d.ts +63 -0
- package/_vendor/canvas-core/geometry/Rectangle2d.ts +64 -0
- package/_vendor/canvas-core/geometry/Vec.ts +123 -0
- package/_vendor/canvas-core/geometry/index.ts +13 -0
- package/_vendor/canvas-core/geometry/segments.ts +65 -0
- package/_vendor/canvas-core/index.ts +391 -0
- package/_vendor/canvas-core/plugins/index.ts +5 -0
- package/_vendor/canvas-core/plugins/types.ts +53 -0
- package/_vendor/canvas-core/reactivity.ts +16 -0
- package/_vendor/canvas-core/schema/records.ts +221 -0
- package/_vendor/canvas-core/schema/snapshots.ts +527 -0
- package/_vendor/canvas-core/shapes/ShapeUtil.ts +516 -0
- package/_vendor/canvas-core/shapes/ShapeUtilRegistry.ts +26 -0
- package/_vendor/canvas-core/shapes/capabilities.ts +67 -0
- package/_vendor/canvas-core/shapes/types.ts +20 -0
- package/_vendor/canvas-core/toolbar.ts +148 -0
- package/_vendor/canvas-core/tools/hand/HandTool.ts +75 -0
- package/_vendor/canvas-core/tools/index.ts +2 -0
- package/_vendor/canvas-core/tools/select/SelectTool.ts +33 -0
- package/_vendor/canvas-core/tools/select/selectContainment.ts +104 -0
- package/_vendor/canvas-core/tools/select/selectToolHelpers.ts +801 -0
- package/_vendor/canvas-core/tools/select/states/BrushingState.ts +145 -0
- package/_vendor/canvas-core/tools/select/states/DraggingSelectionHandleInteractionState.ts +116 -0
- package/_vendor/canvas-core/tools/select/states/IdleState.ts +159 -0
- package/_vendor/canvas-core/tools/select/states/PointingCanvasState.ts +41 -0
- package/_vendor/canvas-core/tools/select/states/PointingResizeHandleState.ts +78 -0
- package/_vendor/canvas-core/tools/select/states/PointingRotateHandleState.ts +64 -0
- package/_vendor/canvas-core/tools/select/states/PointingSelectionHandleInteractionState.ts +34 -0
- package/_vendor/canvas-core/tools/select/states/PointingShapeState.ts +140 -0
- package/_vendor/canvas-core/tools/select/states/ResizingState.ts +171 -0
- package/_vendor/canvas-core/tools/select/states/RotatingState.ts +124 -0
- package/_vendor/canvas-core/tools/select/states/SelectStateNode.ts +128 -0
- package/_vendor/canvas-core/tools/select/states/TranslatingState.ts +139 -0
- package/_vendor/canvas-core/tools/select/states/index.ts +11 -0
- package/_vendor/canvas-plugin-api/feature.ts +204 -0
- package/_vendor/canvas-plugin-api/index.ts +142 -0
- package/_vendor/canvas-product-react/api/apiRegistry.ts +35 -0
- package/_vendor/canvas-product-react/api/canvasProductApi.ts +422 -0
- package/_vendor/canvas-product-react/api/index.ts +31 -0
- package/_vendor/canvas-product-react/capabilities/clipboard/clipboardService.ts +36 -0
- package/_vendor/canvas-product-react/capabilities/clipboard/systemClipboard.ts +245 -0
- package/_vendor/canvas-product-react/capabilities/context-menu/systemContextMenu.ts +396 -0
- package/_vendor/canvas-product-react/capabilities/context-menu/useContextMenuSurface.ts +59 -0
- package/_vendor/canvas-product-react/capabilities/export/useSvgExport.ts +156 -0
- package/_vendor/canvas-product-react/capabilities/history/useHistoryControls.ts +23 -0
- package/_vendor/canvas-product-react/capabilities/pages/usePageControls.ts +41 -0
- package/_vendor/canvas-product-react/capabilities/toolbar/systemToolbar.ts +490 -0
- package/_vendor/canvas-product-react/capabilities/toolbar/useToolbarSurfaces.ts +92 -0
- package/_vendor/canvas-product-react/capabilities/zoom/useZoomControls.ts +27 -0
- package/_vendor/canvas-product-react/index.ts +84 -0
- package/_vendor/canvas-product-react/persistence/index.ts +18 -0
- package/_vendor/canvas-product-react/persistence/localSnapshotPersistence.ts +294 -0
- package/_vendor/canvas-product-react/runtime/CanvasProductRoot.tsx +250 -0
- package/_vendor/canvas-product-react/runtime/productDefinition.ts +72 -0
- package/_vendor/canvas-product-react/runtime/productServices.ts +96 -0
- package/_vendor/canvas-react/components/CanvasView.tsx +796 -0
- package/_vendor/canvas-react/components/OverlayLayer.tsx +879 -0
- package/_vendor/canvas-react/components/PageSpace.tsx +34 -0
- package/_vendor/canvas-react/components/ShapeLayer.tsx +428 -0
- package/_vendor/canvas-react/components/index.ts +4 -0
- package/_vendor/canvas-react/hooks/useEditor.ts +11 -0
- package/_vendor/canvas-react/hooks/useValue.ts +14 -0
- package/_vendor/canvas-react/host/CanvasRendererRegistryContext.tsx +14 -0
- package/_vendor/canvas-react/host/contextMenuRegistry.ts +61 -0
- package/_vendor/canvas-react/host/domEventGuards.ts +61 -0
- package/_vendor/canvas-react/host/index.ts +38 -0
- package/_vendor/canvas-react/host/pluginHost.ts +635 -0
- package/_vendor/canvas-react/host/rendererRegistry.tsx +58 -0
- package/_vendor/canvas-react/host/toolbarRegistry.ts +100 -0
- package/_vendor/canvas-react/index.ts +3 -0
- package/_vendor/plugin-arrow/ArrowBindingUtil.ts +239 -0
- package/_vendor/plugin-arrow/ArrowShapeUtil.ts +311 -0
- package/_vendor/plugin-arrow/ArrowTool.ts +208 -0
- package/_vendor/plugin-arrow/arrowGeometry.ts +441 -0
- package/_vendor/plugin-arrow/arrowTerminalBindingResolver.ts +253 -0
- package/_vendor/plugin-arrow/arrowTerminalCommands.ts +230 -0
- package/_vendor/plugin-arrow/canvas.tsx +92 -0
- package/_vendor/plugin-arrow/core.ts +409 -0
- package/_vendor/plugin-arrow/feature.ts +20 -0
- package/_vendor/plugin-arrow/index.ts +28 -0
- package/_vendor/plugin-arrow/selectionHandleInteractions.ts +154 -0
- package/_vendor/plugin-arrow/types.ts +63 -0
- package/_vendor/plugin-draw/DrawShapeRenderer.tsx +60 -0
- package/_vendor/plugin-draw/DrawShapeUtil.ts +372 -0
- package/_vendor/plugin-draw/DrawTool.ts +444 -0
- package/_vendor/plugin-draw/canvas.tsx +24 -0
- package/_vendor/plugin-draw/core.ts +515 -0
- package/_vendor/plugin-draw/drawGeometry.ts +491 -0
- package/_vendor/plugin-draw/drawHitTest.ts +271 -0
- package/_vendor/plugin-draw/drawInterpolation.ts +250 -0
- package/_vendor/plugin-draw/drawPath.ts +550 -0
- package/_vendor/plugin-draw/drawSegments.ts +106 -0
- package/_vendor/plugin-draw/drawSelectionTransform.ts +67 -0
- package/_vendor/plugin-draw/drawShapeGeometry.ts +307 -0
- package/_vendor/plugin-draw/drawStrokeGeometry.ts +454 -0
- package/_vendor/plugin-draw/feature.ts +20 -0
- package/_vendor/plugin-draw/index.ts +31 -0
- package/_vendor/plugin-draw/types.ts +36 -0
- package/_vendor/plugin-eraser/EraserTool.ts +297 -0
- package/_vendor/plugin-eraser/canvas.tsx +616 -0
- package/_vendor/plugin-eraser/core.ts +49 -0
- package/_vendor/plugin-eraser/feature.ts +20 -0
- package/_vendor/plugin-eraser/index.ts +4 -0
- package/_vendor/plugin-frame/FrameShapeUtil.ts +173 -0
- package/_vendor/plugin-frame/FrameTool.ts +225 -0
- package/_vendor/plugin-frame/canvas.tsx +75 -0
- package/_vendor/plugin-frame/core.ts +534 -0
- package/_vendor/plugin-frame/feature.ts +20 -0
- package/_vendor/plugin-frame/index.ts +6 -0
- package/_vendor/plugin-geo/GeoShapeUtil.ts +463 -0
- package/_vendor/plugin-geo/GeoTools.ts +247 -0
- package/_vendor/plugin-geo/canvas.tsx +90 -0
- package/_vendor/plugin-geo/core.ts +495 -0
- package/_vendor/plugin-geo/feature.ts +20 -0
- package/_vendor/plugin-geo/index.ts +7 -0
- package/_vendor/plugin-geo/types.ts +27 -0
- package/_vendor/plugin-group/GroupShapeUtil.ts +59 -0
- package/_vendor/plugin-group/canvas.tsx +59 -0
- package/_vendor/plugin-group/core.ts +40 -0
- package/_vendor/plugin-group/feature.ts +20 -0
- package/_vendor/plugin-group/index.ts +5 -0
- package/_vendor/plugin-image/ImageShapeUtil.ts +95 -0
- package/_vendor/plugin-image/canvas.tsx +212 -0
- package/_vendor/plugin-image/core.ts +542 -0
- package/_vendor/plugin-image/crop.ts +770 -0
- package/_vendor/plugin-image/cropInteractions.ts +350 -0
- package/_vendor/plugin-image/cropSession.ts +38 -0
- package/_vendor/plugin-image/feature.ts +20 -0
- package/_vendor/plugin-image/index.ts +29 -0
- package/_vendor/plugin-image/types.ts +55 -0
- package/_vendor/plugin-image-product-react/ImageImportController.tsx +143 -0
- package/_vendor/plugin-image-product-react/imageAssetService.ts +52 -0
- package/_vendor/plugin-image-product-react/imageBlobStore.ts +315 -0
- package/_vendor/plugin-image-product-react/imageHostActions.ts +438 -0
- package/_vendor/plugin-image-product-react/imageProductApi.ts +88 -0
- package/_vendor/plugin-image-product-react/imageToolbarHostBridge.ts +48 -0
- package/_vendor/plugin-image-product-react/imageToolbarRuntime.ts +21 -0
- package/_vendor/plugin-image-product-react/index.ts +60 -0
- package/_vendor/plugin-image-product-react/snapshotGuards.ts +33 -0
- package/_vendor/plugin-image-product-react/svgImageAssetInliner.ts +54 -0
- package/_vendor/plugin-image-product-react/useImageImport.ts +64 -0
- package/_vendor/plugin-laser/LaserTool.ts +154 -0
- package/_vendor/plugin-laser/canvas.tsx +645 -0
- package/_vendor/plugin-laser/core.ts +49 -0
- package/_vendor/plugin-laser/feature.ts +20 -0
- package/_vendor/plugin-laser/index.ts +4 -0
- package/_vendor/plugin-text/ArrowLabelBindingUtil.ts +336 -0
- package/_vendor/plugin-text/GeoLabelBindingUtil.ts +391 -0
- package/_vendor/plugin-text/TextShapeSvgExporter.ts +101 -0
- package/_vendor/plugin-text/TextShapeUtil.ts +284 -0
- package/_vendor/plugin-text/TextTool.ts +228 -0
- package/_vendor/plugin-text/canvas.tsx +115 -0
- package/_vendor/plugin-text/core.ts +422 -0
- package/_vendor/plugin-text/feature.ts +20 -0
- package/_vendor/plugin-text/host/RichTextEditingBox.tsx +144 -0
- package/_vendor/plugin-text/host/RichTextToolbar.tsx +266 -0
- package/_vendor/plugin-text/host/TiptapTextEditor.tsx +191 -0
- package/_vendor/plugin-text/host/richTextRendering.tsx +49 -0
- package/_vendor/plugin-text/host/textDomSelection.ts +93 -0
- package/_vendor/plugin-text/host/textEditingBoxStyle.ts +36 -0
- package/_vendor/plugin-text/host/textEntryFocus.ts +307 -0
- package/_vendor/plugin-text/host/textFontLoader.ts +149 -0
- package/_vendor/plugin-text/host/textMeasurement.ts +258 -0
- package/_vendor/plugin-text/host/textMeasurementSync.ts +77 -0
- package/_vendor/plugin-text/host/textRangeSelection.ts +134 -0
- package/_vendor/plugin-text/host/textShapeRendererRuntime.tsx +89 -0
- package/_vendor/plugin-text/host/textShapeSync.ts +173 -0
- package/_vendor/plugin-text/host/tiptapTextEditorController.ts +148 -0
- package/_vendor/plugin-text/index.ts +18 -0
- package/_vendor/plugin-text/richTextUtils.ts +259 -0
- package/_vendor/plugin-text/textInteractions.ts +366 -0
- package/_vendor/plugin-text/textLabelHelpers.ts +235 -0
- package/_vendor/plugin-text/textShapeHelpers.ts +123 -0
- package/_vendor/plugin-text/textStyle.ts +96 -0
- package/_vendor/plugin-text/textTypes.ts +48 -0
- package/_vendor/signals/atom.ts +74 -0
- package/_vendor/signals/computed.ts +132 -0
- package/_vendor/signals/core.ts +130 -0
- package/_vendor/signals/effect.ts +117 -0
- package/_vendor/signals/index.ts +9 -0
- package/_vendor/signals/types.ts +17 -0
- package/_vendor/store/Store.ts +171 -0
- package/_vendor/store/diff.ts +229 -0
- package/_vendor/store/history.ts +177 -0
- package/_vendor/store/index.ts +32 -0
- package/_vendor/store/types.ts +37 -0
- package/api/canvasKitApi.d.ts +29 -0
- package/index.css +3 -0
- package/index.d.ts +16 -0
- package/index.mjs +1 -0
- package/internal-modules.d.ts +187 -0
- package/package.json +48 -0
- package/product/Shell.d.ts +19 -0
- package/product/chromeContext.d.ts +70 -0
- package/product/createCanvasKitEditor.d.ts +6 -0
- package/product/index.d.ts +7 -0
- package/product/persistence.d.ts +3 -0
- package/product/product.d.ts +22 -0
- package/product/useCanvasClipboardShortcuts.d.ts +6 -0
- package/product-iyBbEPIs.mjs +4 -0
- package/product.mjs +1 -0
- package/standard/extensions.d.ts +1 -0
- package/standard/index.d.ts +3 -0
- package/standard/standardApi.d.ts +91 -0
- package/standard-BmrxwqpJ.mjs +44 -0
- package/standard.mjs +1 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useEffect,
|
|
3
|
+
useRef,
|
|
4
|
+
type MutableRefObject,
|
|
5
|
+
} from "react";
|
|
6
|
+
import type { Editor as TiptapEditor } from "@tiptap/react";
|
|
7
|
+
import type {
|
|
8
|
+
Editor,
|
|
9
|
+
ShapeEditingEntryRequest,
|
|
10
|
+
ShapeEditingSession,
|
|
11
|
+
} from "@land/canvas-core";
|
|
12
|
+
import type { TextShapeRecord } from "../textTypes";
|
|
13
|
+
import type { TiptapTextEditorController } from "./tiptapTextEditorController";
|
|
14
|
+
|
|
15
|
+
export function focusTextEditorWhenMounted({
|
|
16
|
+
editor,
|
|
17
|
+
controller,
|
|
18
|
+
shapeId,
|
|
19
|
+
request,
|
|
20
|
+
onApplied,
|
|
21
|
+
attempt = 0,
|
|
22
|
+
}: {
|
|
23
|
+
editor: Editor;
|
|
24
|
+
controller: TiptapTextEditorController;
|
|
25
|
+
shapeId: TextShapeRecord["id"];
|
|
26
|
+
request: ShapeEditingEntryRequest | null;
|
|
27
|
+
onApplied?: (request: ShapeEditingEntryRequest | null) => void;
|
|
28
|
+
attempt?: number;
|
|
29
|
+
}): void {
|
|
30
|
+
if (controller.isDestroyed()) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!controller.isConnected()) {
|
|
35
|
+
if (attempt >= 4) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
window.requestAnimationFrame(() => {
|
|
39
|
+
focusTextEditorWhenMounted({
|
|
40
|
+
editor,
|
|
41
|
+
controller,
|
|
42
|
+
shapeId,
|
|
43
|
+
request,
|
|
44
|
+
onApplied,
|
|
45
|
+
attempt: attempt + 1,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
applyTextEditingEntryFocus({
|
|
52
|
+
editor,
|
|
53
|
+
controller,
|
|
54
|
+
shapeId,
|
|
55
|
+
request,
|
|
56
|
+
onApplied,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function applyTextEditingEntryFocus({
|
|
61
|
+
editor,
|
|
62
|
+
controller,
|
|
63
|
+
shapeId,
|
|
64
|
+
request,
|
|
65
|
+
onApplied,
|
|
66
|
+
}: {
|
|
67
|
+
editor: Editor;
|
|
68
|
+
controller: TiptapTextEditorController;
|
|
69
|
+
shapeId: TextShapeRecord["id"];
|
|
70
|
+
request: ShapeEditingEntryRequest | null;
|
|
71
|
+
onApplied?: (request: ShapeEditingEntryRequest | null) => void;
|
|
72
|
+
}): void {
|
|
73
|
+
if (controller.isDestroyed()) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
controller.focus();
|
|
78
|
+
|
|
79
|
+
if (!request || request.shapeId !== shapeId) {
|
|
80
|
+
controller.focusEnd();
|
|
81
|
+
onApplied?.(request);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (request.entryMode === "selectAll") {
|
|
86
|
+
window.requestAnimationFrame(() => {
|
|
87
|
+
if (controller.isDestroyed() || !controller.isConnected()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
controller.focusSelectAll();
|
|
91
|
+
});
|
|
92
|
+
editor.consumeShapeEditingEntryRequest(request.id);
|
|
93
|
+
onApplied?.(request);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (request.entryMode === "placeCaret" && request.caretPagePoint) {
|
|
97
|
+
controller.focusAtPagePoint(editor, request.caretPagePoint);
|
|
98
|
+
editor.consumeShapeEditingEntryRequest(request.id);
|
|
99
|
+
onApplied?.(request);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
controller.focusEnd();
|
|
104
|
+
editor.consumeShapeEditingEntryRequest(request.id);
|
|
105
|
+
onApplied?.(request);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function useTextEditingEntryFocus({
|
|
109
|
+
editor,
|
|
110
|
+
shapeId,
|
|
111
|
+
session,
|
|
112
|
+
tiptapEditor,
|
|
113
|
+
richTextController,
|
|
114
|
+
entryRequest,
|
|
115
|
+
consumedEntryRequestIdRef,
|
|
116
|
+
}: {
|
|
117
|
+
editor: Editor;
|
|
118
|
+
shapeId: TextShapeRecord["id"];
|
|
119
|
+
session: ShapeEditingSession;
|
|
120
|
+
tiptapEditor: TiptapEditor | null;
|
|
121
|
+
richTextController: TiptapTextEditorController | null;
|
|
122
|
+
entryRequest: ShapeEditingEntryRequest | null;
|
|
123
|
+
consumedEntryRequestIdRef: MutableRefObject<number | null>;
|
|
124
|
+
}): void {
|
|
125
|
+
const replayedSessionEntryRef = useRef<{
|
|
126
|
+
markId: string;
|
|
127
|
+
entryMode: ShapeEditingSession["entryMode"];
|
|
128
|
+
} | null>(null);
|
|
129
|
+
const appliedInitialSessionEntryRef = useRef<string | null>(null);
|
|
130
|
+
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
if (
|
|
133
|
+
!richTextController ||
|
|
134
|
+
richTextController.isDestroyed() ||
|
|
135
|
+
!richTextController.isConnected()
|
|
136
|
+
) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let cancelled = false;
|
|
141
|
+
let timeoutId: number | null = null;
|
|
142
|
+
let attempts = 0;
|
|
143
|
+
|
|
144
|
+
const ensureFocus = () => {
|
|
145
|
+
if (cancelled || richTextController.isDestroyed()) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (consumedEntryRequestIdRef.current !== null) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (richTextController.isFocused()) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
richTextController.focus();
|
|
156
|
+
attempts += 1;
|
|
157
|
+
if (richTextController.isFocused() || attempts >= 4) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
timeoutId = window.setTimeout(ensureFocus, 16);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const frameId = window.requestAnimationFrame(() => {
|
|
164
|
+
ensureFocus();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
return () => {
|
|
168
|
+
cancelled = true;
|
|
169
|
+
window.cancelAnimationFrame(frameId);
|
|
170
|
+
if (timeoutId !== null) {
|
|
171
|
+
window.clearTimeout(timeoutId);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}, [
|
|
175
|
+
consumedEntryRequestIdRef,
|
|
176
|
+
richTextController,
|
|
177
|
+
shapeId,
|
|
178
|
+
]);
|
|
179
|
+
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
if (
|
|
182
|
+
!tiptapEditor ||
|
|
183
|
+
tiptapEditor.isDestroyed ||
|
|
184
|
+
!richTextController ||
|
|
185
|
+
consumedEntryRequestIdRef.current !== null ||
|
|
186
|
+
appliedInitialSessionEntryRef.current === session.markId
|
|
187
|
+
) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
appliedInitialSessionEntryRef.current = session.markId;
|
|
192
|
+
window.setTimeout(() => {
|
|
193
|
+
focusTextEditorWhenMounted({
|
|
194
|
+
editor,
|
|
195
|
+
controller: richTextController,
|
|
196
|
+
shapeId,
|
|
197
|
+
request: {
|
|
198
|
+
id: -1,
|
|
199
|
+
shapeId: session.shapeId,
|
|
200
|
+
entryMode: session.entryMode,
|
|
201
|
+
caretPagePoint: session.caretPagePoint,
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}, 0);
|
|
205
|
+
}, [
|
|
206
|
+
consumedEntryRequestIdRef,
|
|
207
|
+
editor,
|
|
208
|
+
richTextController,
|
|
209
|
+
session.caretPagePoint,
|
|
210
|
+
session.entryMode,
|
|
211
|
+
session.markId,
|
|
212
|
+
session.shapeId,
|
|
213
|
+
shapeId,
|
|
214
|
+
tiptapEditor,
|
|
215
|
+
]);
|
|
216
|
+
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
if (
|
|
219
|
+
!tiptapEditor ||
|
|
220
|
+
tiptapEditor.isDestroyed ||
|
|
221
|
+
!richTextController ||
|
|
222
|
+
!entryRequest ||
|
|
223
|
+
entryRequest.shapeId !== shapeId ||
|
|
224
|
+
consumedEntryRequestIdRef.current === entryRequest.id
|
|
225
|
+
) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
focusTextEditorWhenMounted({
|
|
230
|
+
editor,
|
|
231
|
+
controller: richTextController,
|
|
232
|
+
shapeId,
|
|
233
|
+
request: entryRequest,
|
|
234
|
+
onApplied(appliedRequest: ShapeEditingEntryRequest | null) {
|
|
235
|
+
if (appliedRequest) {
|
|
236
|
+
consumedEntryRequestIdRef.current = appliedRequest.id;
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
}, [
|
|
241
|
+
consumedEntryRequestIdRef,
|
|
242
|
+
editor,
|
|
243
|
+
entryRequest,
|
|
244
|
+
richTextController,
|
|
245
|
+
shapeId,
|
|
246
|
+
tiptapEditor,
|
|
247
|
+
]);
|
|
248
|
+
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
if (
|
|
251
|
+
!tiptapEditor ||
|
|
252
|
+
!richTextController ||
|
|
253
|
+
tiptapEditor.isDestroyed ||
|
|
254
|
+
!richTextController.isConnected()
|
|
255
|
+
) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const controller = richTextController;
|
|
260
|
+
if (session.entryMode === "selectAll") {
|
|
261
|
+
window.requestAnimationFrame(() => {
|
|
262
|
+
const snapshot = controller.getSelectionSnapshot();
|
|
263
|
+
if (snapshot.type !== "Range") {
|
|
264
|
+
controller.focusSelectAll();
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const replayedSessionEntry = replayedSessionEntryRef.current;
|
|
269
|
+
const sessionCaretPagePoint = session.caretPagePoint;
|
|
270
|
+
const shouldReplayPlaceCaret =
|
|
271
|
+
session.entryMode === "placeCaret" &&
|
|
272
|
+
sessionCaretPagePoint &&
|
|
273
|
+
(replayedSessionEntry?.markId !== session.markId ||
|
|
274
|
+
replayedSessionEntry.entryMode !== session.entryMode);
|
|
275
|
+
if (shouldReplayPlaceCaret) {
|
|
276
|
+
replayedSessionEntryRef.current = {
|
|
277
|
+
markId: session.markId,
|
|
278
|
+
entryMode: session.entryMode,
|
|
279
|
+
};
|
|
280
|
+
const caretPagePoint = {
|
|
281
|
+
x: sessionCaretPagePoint.x,
|
|
282
|
+
y: sessionCaretPagePoint.y,
|
|
283
|
+
};
|
|
284
|
+
window.requestAnimationFrame(() => {
|
|
285
|
+
focusTextEditorWhenMounted({
|
|
286
|
+
editor,
|
|
287
|
+
controller,
|
|
288
|
+
shapeId: session.shapeId,
|
|
289
|
+
request: {
|
|
290
|
+
id: -1,
|
|
291
|
+
shapeId: session.shapeId,
|
|
292
|
+
entryMode: "placeCaret",
|
|
293
|
+
caretPagePoint,
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}, [
|
|
299
|
+
editor,
|
|
300
|
+
session.caretPagePoint,
|
|
301
|
+
session.entryMode,
|
|
302
|
+
session.markId,
|
|
303
|
+
session.shapeId,
|
|
304
|
+
richTextController,
|
|
305
|
+
tiptapEditor,
|
|
306
|
+
]);
|
|
307
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { TextShapeFont } from "../textTypes";
|
|
2
|
+
|
|
3
|
+
interface TextWebFontDefinition {
|
|
4
|
+
readonly font: TextShapeFont;
|
|
5
|
+
readonly family: string;
|
|
6
|
+
readonly cssUrl: string;
|
|
7
|
+
readonly weights: readonly number[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const TEXT_WEB_FONT_DEFINITIONS = {
|
|
11
|
+
"noto-sans-sc": {
|
|
12
|
+
font: "noto-sans-sc",
|
|
13
|
+
family: "Noto Sans SC",
|
|
14
|
+
cssUrl:
|
|
15
|
+
"https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap",
|
|
16
|
+
weights: [400, 700],
|
|
17
|
+
},
|
|
18
|
+
"noto-serif-sc": {
|
|
19
|
+
font: "noto-serif-sc",
|
|
20
|
+
family: "Noto Serif SC",
|
|
21
|
+
cssUrl:
|
|
22
|
+
"https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;700&display=swap",
|
|
23
|
+
weights: [400, 700],
|
|
24
|
+
},
|
|
25
|
+
"noto-sans-mono": {
|
|
26
|
+
font: "noto-sans-mono",
|
|
27
|
+
family: "Noto Sans Mono",
|
|
28
|
+
cssUrl:
|
|
29
|
+
"https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@400;700&display=swap",
|
|
30
|
+
weights: [400, 700],
|
|
31
|
+
},
|
|
32
|
+
"ma-shan-zheng": {
|
|
33
|
+
font: "ma-shan-zheng",
|
|
34
|
+
family: "Ma Shan Zheng",
|
|
35
|
+
cssUrl:
|
|
36
|
+
"https://fonts.googleapis.com/css2?family=Ma+Shan+Zheng&display=swap",
|
|
37
|
+
weights: [400],
|
|
38
|
+
},
|
|
39
|
+
} as const satisfies Record<TextShapeFont, TextWebFontDefinition>;
|
|
40
|
+
|
|
41
|
+
const fontLoadPromises = new WeakMap<
|
|
42
|
+
Document,
|
|
43
|
+
Map<TextShapeFont, Promise<void>>
|
|
44
|
+
>();
|
|
45
|
+
|
|
46
|
+
export function ensureTextFontLoaded(
|
|
47
|
+
font: TextShapeFont,
|
|
48
|
+
ownerDocument?: Document,
|
|
49
|
+
): Promise<void> {
|
|
50
|
+
const definition = getTextWebFontDefinition(font);
|
|
51
|
+
const documentForFont = ownerDocument ?? getGlobalDocument();
|
|
52
|
+
if (!definition || !documentForFont) {
|
|
53
|
+
return Promise.resolve();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const documentPromises = getDocumentFontLoadPromises(documentForFont);
|
|
57
|
+
const existingPromise = documentPromises.get(font);
|
|
58
|
+
if (existingPromise) {
|
|
59
|
+
return existingPromise;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const promise = loadTextWebFont(documentForFont, definition).catch(() => {
|
|
63
|
+
documentPromises.delete(font);
|
|
64
|
+
});
|
|
65
|
+
documentPromises.set(font, promise);
|
|
66
|
+
return promise;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getTextWebFontDefinition(
|
|
70
|
+
font: TextShapeFont,
|
|
71
|
+
): TextWebFontDefinition | undefined {
|
|
72
|
+
if (!hasTextWebFontDefinition(font)) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
return TEXT_WEB_FONT_DEFINITIONS[font];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function hasTextWebFontDefinition(
|
|
79
|
+
font: TextShapeFont,
|
|
80
|
+
): font is keyof typeof TEXT_WEB_FONT_DEFINITIONS {
|
|
81
|
+
return Object.prototype.hasOwnProperty.call(TEXT_WEB_FONT_DEFINITIONS, font);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getGlobalDocument(): Document | undefined {
|
|
85
|
+
return typeof document === "undefined" ? undefined : document;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function getDocumentFontLoadPromises(
|
|
89
|
+
ownerDocument: Document,
|
|
90
|
+
): Map<TextShapeFont, Promise<void>> {
|
|
91
|
+
const existing = fontLoadPromises.get(ownerDocument);
|
|
92
|
+
if (existing) {
|
|
93
|
+
return existing;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const next = new Map<TextShapeFont, Promise<void>>();
|
|
97
|
+
fontLoadPromises.set(ownerDocument, next);
|
|
98
|
+
return next;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function loadTextWebFont(
|
|
102
|
+
ownerDocument: Document,
|
|
103
|
+
definition: TextWebFontDefinition,
|
|
104
|
+
): Promise<void> {
|
|
105
|
+
await ensureFontStylesheet(ownerDocument, definition);
|
|
106
|
+
await waitForFontFaces(ownerDocument, definition);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function ensureFontStylesheet(
|
|
110
|
+
ownerDocument: Document,
|
|
111
|
+
definition: TextWebFontDefinition,
|
|
112
|
+
): Promise<void> {
|
|
113
|
+
const existing = ownerDocument.querySelector<HTMLLinkElement>(
|
|
114
|
+
`link[data-land-text-web-font="${definition.font}"]`,
|
|
115
|
+
);
|
|
116
|
+
if (existing) {
|
|
117
|
+
return Promise.resolve();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return new Promise((resolve) => {
|
|
121
|
+
const link = ownerDocument.createElement("link");
|
|
122
|
+
link.rel = "stylesheet";
|
|
123
|
+
link.href = definition.cssUrl;
|
|
124
|
+
link.dataset.landTextWebFont = definition.font;
|
|
125
|
+
|
|
126
|
+
const finish = () => resolve();
|
|
127
|
+
link.addEventListener("load", finish, { once: true });
|
|
128
|
+
link.addEventListener("error", finish, { once: true });
|
|
129
|
+
|
|
130
|
+
const target = ownerDocument.head ?? ownerDocument.documentElement;
|
|
131
|
+
target.appendChild(link);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function waitForFontFaces(
|
|
136
|
+
ownerDocument: Document,
|
|
137
|
+
definition: TextWebFontDefinition,
|
|
138
|
+
): Promise<void> {
|
|
139
|
+
const { fonts } = ownerDocument;
|
|
140
|
+
if (!fonts) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
await Promise.all(
|
|
145
|
+
definition.weights.map((weight) =>
|
|
146
|
+
fonts.load(`${weight} 16px "${definition.family}"`, "字体Aa"),
|
|
147
|
+
),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import type { Editor as TiptapEditor } from "@tiptap/react";
|
|
2
|
+
import type {
|
|
3
|
+
RichTextDocument,
|
|
4
|
+
TextShapeRecord,
|
|
5
|
+
} from "../textTypes";
|
|
6
|
+
import {
|
|
7
|
+
getPlainTextFromRichText,
|
|
8
|
+
renderRichTextDocumentToHtml,
|
|
9
|
+
} from "../richTextUtils";
|
|
10
|
+
import {
|
|
11
|
+
getTextAlign,
|
|
12
|
+
getTextFontFamily,
|
|
13
|
+
getTextFontSize,
|
|
14
|
+
getTextLineHeight,
|
|
15
|
+
getTextMinHeight,
|
|
16
|
+
} from "../textStyle";
|
|
17
|
+
|
|
18
|
+
export const TEXT_AUTO_SIZE_MIN_W = 16;
|
|
19
|
+
|
|
20
|
+
export function measureTextShapeFromEditor(
|
|
21
|
+
textEditor: TiptapEditor,
|
|
22
|
+
shape: TextShapeRecord,
|
|
23
|
+
): { w: number; h: number } | undefined {
|
|
24
|
+
if (textEditor.getText().trim().length === 0) {
|
|
25
|
+
return getEmptyTextShapeSize(shape);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const measuredSize = measureRichTextElement(textEditor.view.dom, {
|
|
29
|
+
shape,
|
|
30
|
+
width: shape.props.autoSize ? undefined : shape.props.w,
|
|
31
|
+
});
|
|
32
|
+
if (textEditor.view.dom.scrollHeight > shape.props.h + 1) {
|
|
33
|
+
measuredSize.h = Math.max(measuredSize.h, textEditor.view.dom.scrollHeight);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return getMeasuredTextShapeSize(shape, measuredSize);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function measureTextShapeFromRichText(
|
|
40
|
+
richText: RichTextDocument,
|
|
41
|
+
shape: TextShapeRecord,
|
|
42
|
+
): { w: number; h: number } | undefined {
|
|
43
|
+
if (getPlainTextFromRichText(richText).trim().length === 0) {
|
|
44
|
+
return getEmptyTextShapeSize(shape);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return getMeasuredTextShapeSize(
|
|
48
|
+
shape,
|
|
49
|
+
measureTextHtml(renderRichTextDocumentToHtml(richText), {
|
|
50
|
+
width: shape.props.autoSize ? undefined : shape.props.w,
|
|
51
|
+
shape,
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function applyTextShapeElementStyles(
|
|
57
|
+
element: HTMLElement,
|
|
58
|
+
shape: TextShapeRecord,
|
|
59
|
+
): void {
|
|
60
|
+
element.className =
|
|
61
|
+
"land-rich-text-editor h-full min-h-full w-full outline-none whitespace-pre-wrap break-words";
|
|
62
|
+
element.dir = "auto";
|
|
63
|
+
element.style.color = shape.props.color;
|
|
64
|
+
element.style.fontFamily = getTextFontFamily(shape.props.font);
|
|
65
|
+
element.style.fontSize = `${getTextFontSize(shape.props.size)}px`;
|
|
66
|
+
element.style.lineHeight = `${getTextLineHeight(shape.props.size)}px`;
|
|
67
|
+
element.style.textAlign = getTextAlign(shape.props.textAlign);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getEmptyTextShapeSize(
|
|
71
|
+
shape: TextShapeRecord,
|
|
72
|
+
): { w: number; h: number } | undefined {
|
|
73
|
+
const nextW = shape.props.autoSize ? TEXT_AUTO_SIZE_MIN_W : shape.props.w;
|
|
74
|
+
const nextH = getTextMinHeight(shape.props.size);
|
|
75
|
+
if (shape.props.w === nextW && shape.props.h === nextH) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
return { w: nextW, h: nextH };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getMeasuredTextShapeSize(
|
|
82
|
+
shape: TextShapeRecord,
|
|
83
|
+
measuredSize: { w: number; h: number },
|
|
84
|
+
): { w: number; h: number } | undefined {
|
|
85
|
+
const nextW = shape.props.autoSize
|
|
86
|
+
? Math.max(TEXT_AUTO_SIZE_MIN_W, Math.ceil(measuredSize.w))
|
|
87
|
+
: shape.props.w;
|
|
88
|
+
const nextH = Math.max(
|
|
89
|
+
getTextMinHeight(shape.props.size),
|
|
90
|
+
Math.ceil(measuredSize.h),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
if (nextW === shape.props.w && nextH === shape.props.h) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return { w: nextW, h: nextH };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function measureTextHtml(
|
|
101
|
+
html: string,
|
|
102
|
+
options?: { shape?: TextShapeRecord; width?: number },
|
|
103
|
+
): { w: number; h: number } {
|
|
104
|
+
const manager = getTextMeasurementManager(document);
|
|
105
|
+
return manager.measureHtml(html, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function measureRichTextElement(
|
|
109
|
+
element: HTMLElement,
|
|
110
|
+
options?: { shape?: TextShapeRecord; width?: number },
|
|
111
|
+
): { w: number; h: number } {
|
|
112
|
+
const computed = element.ownerDocument.defaultView?.getComputedStyle(element);
|
|
113
|
+
return getTextMeasurementManager(element.ownerDocument).measureElement(
|
|
114
|
+
element,
|
|
115
|
+
options,
|
|
116
|
+
computed,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
class TextMeasurementManager {
|
|
121
|
+
private readonly element: HTMLDivElement;
|
|
122
|
+
|
|
123
|
+
constructor(private readonly ownerDocument: Document) {
|
|
124
|
+
this.element = ownerDocument.createElement("div");
|
|
125
|
+
this.element.setAttribute("aria-hidden", "true");
|
|
126
|
+
this.element.style.position = "absolute";
|
|
127
|
+
this.element.style.left = "-10000px";
|
|
128
|
+
this.element.style.top = "0";
|
|
129
|
+
this.element.style.visibility = "hidden";
|
|
130
|
+
this.element.style.pointerEvents = "none";
|
|
131
|
+
ownerDocument.body.appendChild(this.element);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
measureHtml(
|
|
135
|
+
html: string,
|
|
136
|
+
options?: { shape?: TextShapeRecord; width?: number },
|
|
137
|
+
): { w: number; h: number } {
|
|
138
|
+
const element = this.prepareElement(options);
|
|
139
|
+
element.innerHTML = html;
|
|
140
|
+
return this.measurePreparedElement(element);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
measureElement(
|
|
144
|
+
source: HTMLElement,
|
|
145
|
+
options?: { shape?: TextShapeRecord; width?: number },
|
|
146
|
+
computed?: CSSStyleDeclaration,
|
|
147
|
+
): { w: number; h: number } {
|
|
148
|
+
const element = this.prepareElement(options);
|
|
149
|
+
element.replaceChildren(...Array.from(source.childNodes).map((child) => child.cloneNode(true)));
|
|
150
|
+
applyComputedStyleValue(element, "whiteSpace", computed?.whiteSpace);
|
|
151
|
+
applyComputedStyleValue(element, "overflowWrap", computed?.overflowWrap);
|
|
152
|
+
applyComputedStyleValue(element, "lineHeight", computed?.lineHeight);
|
|
153
|
+
applyComputedStyleValue(element, "letterSpacing", computed?.letterSpacing);
|
|
154
|
+
applyComputedStyleValue(element, "textAlign", computed?.textAlign);
|
|
155
|
+
return this.measurePreparedElement(element);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private prepareElement(
|
|
159
|
+
options?: { shape?: TextShapeRecord; width?: number },
|
|
160
|
+
): HTMLDivElement {
|
|
161
|
+
const element = this.element;
|
|
162
|
+
element.removeAttribute("contenteditable");
|
|
163
|
+
element.className = "";
|
|
164
|
+
element.textContent = "";
|
|
165
|
+
|
|
166
|
+
if (options?.shape) {
|
|
167
|
+
applyTextShapeElementStyles(element, options.shape);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
element.style.position = "absolute";
|
|
171
|
+
element.style.left = "-10000px";
|
|
172
|
+
element.style.top = "0";
|
|
173
|
+
element.style.visibility = "hidden";
|
|
174
|
+
element.style.pointerEvents = "none";
|
|
175
|
+
element.style.width =
|
|
176
|
+
typeof options?.width === "number" ? `${options.width}px` : "max-content";
|
|
177
|
+
element.style.minWidth = "0";
|
|
178
|
+
element.style.height = "auto";
|
|
179
|
+
element.style.minHeight = "0";
|
|
180
|
+
element.style.maxHeight = "none";
|
|
181
|
+
element.style.maxWidth =
|
|
182
|
+
typeof options?.width === "number" ? `${options.width}px` : "none";
|
|
183
|
+
return element;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private measurePreparedElement(
|
|
187
|
+
element: HTMLElement,
|
|
188
|
+
): { w: number; h: number } {
|
|
189
|
+
element.querySelectorAll("p").forEach((paragraph) => {
|
|
190
|
+
if (paragraph instanceof HTMLElement) {
|
|
191
|
+
paragraph.style.margin = "0";
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
const measuredElements = [
|
|
195
|
+
element,
|
|
196
|
+
...Array.from(element.querySelectorAll("*")).filter(
|
|
197
|
+
(child): child is HTMLElement => child instanceof HTMLElement,
|
|
198
|
+
),
|
|
199
|
+
];
|
|
200
|
+
const measuredSize = measuredElements.reduce(
|
|
201
|
+
(current, child) => {
|
|
202
|
+
const childRect = child.getBoundingClientRect();
|
|
203
|
+
return {
|
|
204
|
+
w: Math.max(
|
|
205
|
+
current.w,
|
|
206
|
+
childRect.width,
|
|
207
|
+
child.scrollWidth,
|
|
208
|
+
child.offsetWidth,
|
|
209
|
+
),
|
|
210
|
+
h: Math.max(
|
|
211
|
+
current.h,
|
|
212
|
+
childRect.height,
|
|
213
|
+
child.scrollHeight,
|
|
214
|
+
child.offsetHeight,
|
|
215
|
+
),
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
{ w: 0, h: 0 },
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
return measuredSize;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const textMeasurementManagers = new WeakMap<
|
|
226
|
+
Document,
|
|
227
|
+
TextMeasurementManager
|
|
228
|
+
>();
|
|
229
|
+
|
|
230
|
+
function getTextMeasurementManager(
|
|
231
|
+
ownerDocument: Document,
|
|
232
|
+
): TextMeasurementManager {
|
|
233
|
+
const existing = textMeasurementManagers.get(ownerDocument);
|
|
234
|
+
if (existing) {
|
|
235
|
+
return existing;
|
|
236
|
+
}
|
|
237
|
+
const manager = new TextMeasurementManager(ownerDocument);
|
|
238
|
+
textMeasurementManagers.set(ownerDocument, manager);
|
|
239
|
+
return manager;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function applyComputedStyleValue(
|
|
243
|
+
element: HTMLElement,
|
|
244
|
+
property:
|
|
245
|
+
| "whiteSpace"
|
|
246
|
+
| "overflowWrap"
|
|
247
|
+
| "font"
|
|
248
|
+
| "lineHeight"
|
|
249
|
+
| "letterSpacing"
|
|
250
|
+
| "textAlign",
|
|
251
|
+
value: string | undefined,
|
|
252
|
+
): void {
|
|
253
|
+
if (!value || value === "normal" || value === "auto") {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
element.style[property] = value;
|
|
258
|
+
}
|