@widgetic/canvas 0.5.26 → 0.5.27
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/dist/canvas/Canvas.svelte +545 -141
- package/dist/canvas/props-panel/PropsPanel.svelte +11 -6
- package/dist/components/CanvasBoardPlaceholder.svelte +61 -0
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +16 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +2 -0
- package/dist/i18n/messages.d.ts +1 -1
- package/dist/i18n/messages.js +1 -1
- package/package.json +1 -1
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
import type { WidgetConversionPayload, StrokeRecognizer } from './types';
|
|
56
56
|
import { provideCanvasLanguage } from '../i18n/language.js';
|
|
57
57
|
import { makeShapeWords, setShapeWords, shapeWordsOf } from '../i18n/shapeWords.js';
|
|
58
|
+
import CanvasBoardPlaceholder, { CANVAS_BOARD_BACKGROUND } from '../components/CanvasBoardPlaceholder.svelte';
|
|
58
59
|
|
|
59
60
|
// ── Library shape path fingerprints (migration for old saves) ──────────────
|
|
60
61
|
// Maps a normalized path string → shape key. Used to identify library shapes
|
|
@@ -126,6 +127,8 @@
|
|
|
126
127
|
'_boxStroke', '_boxStrokeWidth', '_boxStrokeDashArray',
|
|
127
128
|
'_boxCornerRadius', '_boxFill',
|
|
128
129
|
'_textPadding', '_textStrokeColor', '_textStrokeWidth',
|
|
130
|
+
// Click-created text grows with the line. A dragged text box keeps a fixed wrap width.
|
|
131
|
+
'_textAutoSize',
|
|
129
132
|
'_hasImage', '_imageUrl', '_originalFilename', '_originalAspectRatio',
|
|
130
133
|
// Frame-specific (also in FRAME_CUSTOM_PROPS)
|
|
131
134
|
'_isFrame', '_isFrameBackground', '_labelText', '_labelFontSize',
|
|
@@ -371,8 +374,17 @@
|
|
|
371
374
|
type CanvasLoadPhase = 'initializing' | 'ready' | 'loading' | 'error';
|
|
372
375
|
let canvasLoadPhase: CanvasLoadPhase = 'initializing';
|
|
373
376
|
$: isCanvasLoading = canvasLoadPhase !== 'ready' || isExternalDataLoading;
|
|
377
|
+
|
|
378
|
+
/** Open-time marks (DevTools Performance panel, or `performance.getEntriesByType('mark')`). */
|
|
379
|
+
function markCanvasOpenStep(stepName: string): void {
|
|
380
|
+
try {
|
|
381
|
+
performance.mark(stepName);
|
|
382
|
+
} catch {
|
|
383
|
+
// Marks are diagnostics only.
|
|
384
|
+
}
|
|
385
|
+
}
|
|
374
386
|
let canvasInitializationError: Error | null = null;
|
|
375
|
-
let canvasBackgroundColor =
|
|
387
|
+
let canvasBackgroundColor = CANVAS_BOARD_BACKGROUND;
|
|
376
388
|
|
|
377
389
|
// Selected object state (for PropsPanel)
|
|
378
390
|
let selectedObject: any = null;
|
|
@@ -504,6 +516,8 @@
|
|
|
504
516
|
const GROUP_IDLE_STROKE_WIDTH = 1.5;
|
|
505
517
|
/** Hide the idle dashed outline during toDataURL / convert screenshots. */
|
|
506
518
|
let skipGroupIdleOutline = false;
|
|
519
|
+
/** Hide the empty-text placeholder during screenshots so it is not read as real copy. */
|
|
520
|
+
let skipTextPlaceholder = false;
|
|
507
521
|
|
|
508
522
|
/** Stamp top-level paint order before ActiveSelection swallows children. */
|
|
509
523
|
function stampTopLevelPaintOrder() {
|
|
@@ -725,7 +739,11 @@
|
|
|
725
739
|
let textFontSize = 20;
|
|
726
740
|
let textFontFamily = 'Arial';
|
|
727
741
|
let textFillColor = '#111827';
|
|
728
|
-
let textBackgroundColor = '
|
|
742
|
+
let textBackgroundColor = '';
|
|
743
|
+
const TEXT_PLACEHOLDER_FILL = '#9ca3af';
|
|
744
|
+
const TEXT_CURSOR_DURATION = 550;
|
|
745
|
+
// Room so the last glyph and the caret stay on the line. Larger than this reads as padding.
|
|
746
|
+
const TEXT_FIT_SLACK = 4;
|
|
729
747
|
|
|
730
748
|
// Shapes settings
|
|
731
749
|
export function shapeFillColor () {
|
|
@@ -815,6 +833,7 @@
|
|
|
815
833
|
const selectionBounds = target.getBoundingRect();
|
|
816
834
|
|
|
817
835
|
skipGroupIdleOutline = true;
|
|
836
|
+
skipTextPlaceholder = true;
|
|
818
837
|
try {
|
|
819
838
|
let dataUrl = '';
|
|
820
839
|
|
|
@@ -864,6 +883,7 @@
|
|
|
864
883
|
return '';
|
|
865
884
|
} finally {
|
|
866
885
|
skipGroupIdleOutline = false;
|
|
886
|
+
skipTextPlaceholder = false;
|
|
867
887
|
canvas.requestRenderAll();
|
|
868
888
|
}
|
|
869
889
|
}
|
|
@@ -1887,6 +1907,17 @@
|
|
|
1887
1907
|
|
|
1888
1908
|
const activeObj = canvas!.getActiveObject();
|
|
1889
1909
|
if (!activeObj || !activeObj.oCoords) return;
|
|
1910
|
+
// A hidden rotate handle must not still show the rotate cursor.
|
|
1911
|
+
const rotationHandleHidden =
|
|
1912
|
+
activeObj.type === 'textbox' &&
|
|
1913
|
+
typeof (activeObj as any).isControlVisible === 'function' &&
|
|
1914
|
+
!(activeObj as any).isControlVisible('mtr');
|
|
1915
|
+
if (rotationHandleHidden) {
|
|
1916
|
+
if (!isRotating && canvas!.upperCanvasEl) {
|
|
1917
|
+
canvas!.upperCanvasEl.classList.remove('fabric-rotate-cursor');
|
|
1918
|
+
}
|
|
1919
|
+
return;
|
|
1920
|
+
}
|
|
1890
1921
|
const mtrCoord = (activeObj.oCoords as any).mtr;
|
|
1891
1922
|
if (!mtrCoord) return;
|
|
1892
1923
|
|
|
@@ -1980,6 +2011,19 @@
|
|
|
1980
2011
|
target.set({ strokeUniform: true });
|
|
1981
2012
|
}
|
|
1982
2013
|
|
|
2014
|
+
// Text scale is decided by the handle, before this event.
|
|
2015
|
+
// Snapping scaleX/scaleY here runs after the anchor is placed,
|
|
2016
|
+
// so the edge jumps away from the pointer.
|
|
2017
|
+
if (target.type === 'textbox') {
|
|
2018
|
+
target.lockSkewingX = true;
|
|
2019
|
+
target.lockSkewingY = true;
|
|
2020
|
+
target.lockScalingFlip = true;
|
|
2021
|
+
if (target.scaleX <= 0) target.scaleX = 0.01;
|
|
2022
|
+
if (target.scaleY <= 0) target.scaleY = 0.01;
|
|
2023
|
+
target.setCoords?.();
|
|
2024
|
+
return;
|
|
2025
|
+
}
|
|
2026
|
+
|
|
1983
2027
|
// Check which control is being used
|
|
1984
2028
|
// Corner controls: 'tl', 'tr', 'bl', 'br' - these should support uniform scaling
|
|
1985
2029
|
// Middle controls: 'mt', 'mb', 'ml', 'mr' - these are for skew/stretch, no uniform scaling
|
|
@@ -2299,12 +2343,27 @@
|
|
|
2299
2343
|
target.set({ strokeUniform: true });
|
|
2300
2344
|
}
|
|
2301
2345
|
|
|
2346
|
+
if (target.type === 'textbox') {
|
|
2347
|
+
const textAction = opt?.transform?.action || '';
|
|
2348
|
+
const textCorner = opt?.transform?.corner;
|
|
2349
|
+
if (textCorner === 'ml' || textCorner === 'mr' || textAction === 'resizing') {
|
|
2350
|
+
target._textAutoSize = false;
|
|
2351
|
+
target.initDimensions?.();
|
|
2352
|
+
target.setCoords?.();
|
|
2353
|
+
} else if (target._lockAspectRatio !== false) {
|
|
2354
|
+
bakeTextboxScaleIntoFont(target);
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2302
2358
|
// Reset uniform scaling after scale operation completes
|
|
2303
2359
|
canvas!.uniformScaling = false;
|
|
2304
2360
|
|
|
2305
|
-
// Reset skew locks after scale operation completes
|
|
2306
|
-
|
|
2307
|
-
target.
|
|
2361
|
+
// Reset skew locks after scale operation completes.
|
|
2362
|
+
// Text stays locked: a free middle-handle drag would stretch the glyphs.
|
|
2363
|
+
if (target.type !== 'textbox') {
|
|
2364
|
+
target.lockSkewingX = false;
|
|
2365
|
+
target.lockSkewingY = false;
|
|
2366
|
+
}
|
|
2308
2367
|
|
|
2309
2368
|
// Clear original corners stored at transform start
|
|
2310
2369
|
delete target._originalCorners;
|
|
@@ -2461,6 +2520,18 @@
|
|
|
2461
2520
|
}
|
|
2462
2521
|
|
|
2463
2522
|
const action = transform.action;
|
|
2523
|
+
const transformTarget = transform.target as any;
|
|
2524
|
+
// Locked text stays proportional. Unlocked text may stretch.
|
|
2525
|
+
// Left and right handles always reflow the wrap width.
|
|
2526
|
+
if (transformTarget?.type === 'textbox') {
|
|
2527
|
+
const textCorner = transform.corner;
|
|
2528
|
+
const textLocked = transformTarget._lockAspectRatio !== false;
|
|
2529
|
+
if (textCorner !== 'ml' && textCorner !== 'mr') {
|
|
2530
|
+
canvas!.uniformScaling = textLocked;
|
|
2531
|
+
}
|
|
2532
|
+
transformTarget.lockSkewingX = true;
|
|
2533
|
+
transformTarget.lockSkewingY = true;
|
|
2534
|
+
}
|
|
2464
2535
|
// Check if this is a scaling action (corner or edge resize)
|
|
2465
2536
|
if (action === 'scale' || action === 'scaleX' || action === 'scaleY') {
|
|
2466
2537
|
isCurrentlyScaling = true;
|
|
@@ -2698,6 +2769,16 @@
|
|
|
2698
2769
|
if ((obj as any)._textPadding > 0) {
|
|
2699
2770
|
syncTextboxFabricPadding(obj);
|
|
2700
2771
|
}
|
|
2772
|
+
normalizeTextboxBackground(obj);
|
|
2773
|
+
if ((obj as any)._lockAspectRatio === undefined) {
|
|
2774
|
+
(obj as any)._lockAspectRatio = true;
|
|
2775
|
+
}
|
|
2776
|
+
if ((obj as any)._fillOpacity == null && !obj.backgroundColor) {
|
|
2777
|
+
(obj as any)._fillOpacity = 0;
|
|
2778
|
+
}
|
|
2779
|
+
applyTextShapeChrome(obj);
|
|
2780
|
+
obj.cursorDelay = 0;
|
|
2781
|
+
obj.cursorDuration = TEXT_CURSOR_DURATION;
|
|
2701
2782
|
canvasLog('Canvas: object:added — restored box render for textbox', (obj as any)._objectId || '(new)');
|
|
2702
2783
|
}
|
|
2703
2784
|
|
|
@@ -2815,7 +2896,8 @@
|
|
|
2815
2896
|
// Re-apply control visibility for proportionally locked shapes.
|
|
2816
2897
|
// Edge handles (ml/mr/mt/mb) are hidden to prevent resize drift and to signal
|
|
2817
2898
|
// that the shape can only be resized uniformly from corner handles.
|
|
2818
|
-
|
|
2899
|
+
// Text keeps those handles: they stay hittable and are not drawn.
|
|
2900
|
+
if ((obj as any)._lockAspectRatio && obj.type !== 'textbox') {
|
|
2819
2901
|
obj.setControlsVisibility({ ml: false, mr: false, mt: false, mb: false });
|
|
2820
2902
|
}
|
|
2821
2903
|
|
|
@@ -2833,9 +2915,12 @@
|
|
|
2833
2915
|
strokeWidth: WIREFRAME_STROKE_WIDTH,
|
|
2834
2916
|
});
|
|
2835
2917
|
} else if (obj.type === 'textbox') {
|
|
2836
|
-
//
|
|
2837
|
-
//
|
|
2838
|
-
obj.
|
|
2918
|
+
// Text has no box fill until one is chosen. White here
|
|
2919
|
+
// shows up as Fill Opacity 100% and paints the box.
|
|
2920
|
+
if (!obj.backgroundColor || obj.backgroundColor === 'transparent') {
|
|
2921
|
+
obj.set({ backgroundColor: '' });
|
|
2922
|
+
obj._fillOpacity = 0;
|
|
2923
|
+
}
|
|
2839
2924
|
} else if (String(obj.type || '').toLowerCase() !== 'group') {
|
|
2840
2925
|
obj.set({
|
|
2841
2926
|
fill: WIREFRAME_FILL,
|
|
@@ -2867,7 +2952,7 @@
|
|
|
2867
2952
|
if (isHandlingContainment) return;
|
|
2868
2953
|
if (!canvas) return;
|
|
2869
2954
|
const target = opt.target;
|
|
2870
|
-
if (!target) return;
|
|
2955
|
+
if (!target || target._bakingTextScale) return;
|
|
2871
2956
|
// Skip the frame's background rect
|
|
2872
2957
|
if ((target as any)._isFrameBackground) return;
|
|
2873
2958
|
|
|
@@ -3238,17 +3323,82 @@
|
|
|
3238
3323
|
// ─── History: snapshot after each completed move/scale/rotate ───
|
|
3239
3324
|
// object:modified fires once at mouseup — ideal for undo snapshots.
|
|
3240
3325
|
// Also fires after arrow-key nudges.
|
|
3241
|
-
canvas.on('object:modified', () => {
|
|
3326
|
+
canvas.on('object:modified', (opt: any) => {
|
|
3327
|
+
const moved = opt?.target;
|
|
3328
|
+
if (moved?.type === 'textbox' && (moved.hasControls === false || moved.hasBorders === false)) {
|
|
3329
|
+
moved.hasControls = true;
|
|
3330
|
+
moved.hasBorders = true;
|
|
3331
|
+
}
|
|
3242
3332
|
queueWidgetPreviewOverlaySync();
|
|
3243
3333
|
takeHistorySnapshot();
|
|
3244
3334
|
});
|
|
3245
3335
|
|
|
3336
|
+
// Unselected text draws the same selection stroke while the pointer is over it.
|
|
3337
|
+
let hoveredTextShape: any = null;
|
|
3338
|
+
const paintHoveredTextOutline = (ctx: CanvasRenderingContext2D | undefined) => {
|
|
3339
|
+
const hovered = hoveredTextShape;
|
|
3340
|
+
if (!ctx || !hovered || !canvas) return;
|
|
3341
|
+
if (!hovered.canvas || hovered.group || hovered.isMoving || hovered.isEditing) return;
|
|
3342
|
+
const active = canvas.getActiveObject() as any;
|
|
3343
|
+
if (active === hovered) return;
|
|
3344
|
+
if (active && typeof active.getObjects === 'function' && active.getObjects().includes(hovered)) return;
|
|
3345
|
+
try {
|
|
3346
|
+
hovered._renderControls(ctx, { hasBorders: true, hasControls: false });
|
|
3347
|
+
} catch (err) {
|
|
3348
|
+
console.error('Canvas: hover text outline failed', err);
|
|
3349
|
+
hoveredTextShape = null;
|
|
3350
|
+
}
|
|
3351
|
+
};
|
|
3352
|
+
|
|
3353
|
+
// Text handles and the selection stroke hide for the move and come back on release.
|
|
3354
|
+
canvas.on('object:moving', (opt: any) => {
|
|
3355
|
+
const moving = opt?.target;
|
|
3356
|
+
if (!moving || moving.type !== 'textbox' || moving.isEditing) return;
|
|
3357
|
+
if (hoveredTextShape === moving) hoveredTextShape = null;
|
|
3358
|
+
if (moving.hasControls !== false || moving.hasBorders !== false) {
|
|
3359
|
+
moving.hasControls = false;
|
|
3360
|
+
moving.hasBorders = false;
|
|
3361
|
+
}
|
|
3362
|
+
});
|
|
3363
|
+
canvas.on('mouse:up', () => {
|
|
3364
|
+
const active = canvas?.getActiveObject() as any;
|
|
3365
|
+
if (active?.type === 'textbox' && (active.hasControls === false || active.hasBorders === false)) {
|
|
3366
|
+
active.hasControls = true;
|
|
3367
|
+
active.hasBorders = true;
|
|
3368
|
+
canvas?.requestRenderAll();
|
|
3369
|
+
}
|
|
3370
|
+
});
|
|
3371
|
+
|
|
3372
|
+
// An unselected text box shows the selection stroke while the pointer is over it.
|
|
3373
|
+
canvas.on('mouse:over', (hoverEvt: any) => {
|
|
3374
|
+
const target = hoverEvt?.target;
|
|
3375
|
+
if (!target || target.type !== 'textbox' || target.group) return;
|
|
3376
|
+
if (canvas!.getActiveObject() === target) return;
|
|
3377
|
+
if (hoveredTextShape === target) return;
|
|
3378
|
+
hoveredTextShape = target;
|
|
3379
|
+
canvas!.requestRenderAll();
|
|
3380
|
+
});
|
|
3381
|
+
canvas.on('mouse:out', (hoverEvt: any) => {
|
|
3382
|
+
if (hoverEvt?.target && hoverEvt.target === hoveredTextShape) {
|
|
3383
|
+
hoveredTextShape = null;
|
|
3384
|
+
canvas?.requestRenderAll();
|
|
3385
|
+
}
|
|
3386
|
+
});
|
|
3387
|
+
canvas.on('after:render', (renderEvt: any) => {
|
|
3388
|
+
if (!canvas || renderEvt?.ctx !== canvas.getContext()) return;
|
|
3389
|
+
paintHoveredTextOutline(renderEvt.ctx);
|
|
3390
|
+
});
|
|
3391
|
+
|
|
3246
3392
|
// ─── History: snapshot when text editing is exited ───────────────
|
|
3247
3393
|
// text:editing:exited fires after Fabric internally exits editing
|
|
3248
3394
|
// (Escape key, click outside, programmatic). This is the most
|
|
3249
3395
|
// reliable hook because Fabric may stopPropagation on the keydown
|
|
3250
3396
|
// event, preventing our window-level handlers from seeing it.
|
|
3251
|
-
canvas.on('text:editing:exited', () => {
|
|
3397
|
+
canvas.on('text:editing:exited', (opt: any) => {
|
|
3398
|
+
const exited = opt?.target;
|
|
3399
|
+
if (exited?.type === 'textbox') {
|
|
3400
|
+
settleTextShapeAfterEdit(exited);
|
|
3401
|
+
}
|
|
3252
3402
|
takeHistorySnapshot();
|
|
3253
3403
|
takeFocusBackAfterTextEditing();
|
|
3254
3404
|
|
|
@@ -3857,6 +4007,7 @@
|
|
|
3857
4007
|
'_boxCornerRadius', '_boxFill',
|
|
3858
4008
|
// Textbox padding (custom inner padding drawn by drawObject)
|
|
3859
4009
|
'_textPadding',
|
|
4010
|
+
'_textAutoSize',
|
|
3860
4011
|
// Text stroke properties
|
|
3861
4012
|
'_textStrokeColor', '_textStrokeWidth',
|
|
3862
4013
|
// Group chrome (fill/stroke around children, like Frame)
|
|
@@ -5072,16 +5223,16 @@
|
|
|
5072
5223
|
const target = e.target;
|
|
5073
5224
|
if (!target) return;
|
|
5074
5225
|
|
|
5075
|
-
// Textbox:
|
|
5226
|
+
// Textbox: edit on double-click. Empty text gets a caret; existing text is selected.
|
|
5076
5227
|
if (target.type === 'textbox' && target.editable !== false) {
|
|
5077
|
-
|
|
5078
|
-
target
|
|
5079
|
-
|
|
5080
|
-
|
|
5081
|
-
|
|
5082
|
-
|
|
5083
|
-
|
|
5084
|
-
}
|
|
5228
|
+
const hasText = !textShapeIsEmpty(target);
|
|
5229
|
+
beginTextEditing(target, { selectAll: hasText });
|
|
5230
|
+
if (hasText) {
|
|
5231
|
+
requestAnimationFrame(() => {
|
|
5232
|
+
if (target.isEditing) target.selectAll();
|
|
5233
|
+
canvas!.requestRenderAll();
|
|
5234
|
+
});
|
|
5235
|
+
}
|
|
5085
5236
|
return;
|
|
5086
5237
|
}
|
|
5087
5238
|
|
|
@@ -5907,6 +6058,10 @@
|
|
|
5907
6058
|
needsRender = true;
|
|
5908
6059
|
}
|
|
5909
6060
|
}
|
|
6061
|
+
if (hoveredTextShape) {
|
|
6062
|
+
hoveredTextShape = null;
|
|
6063
|
+
needsRender = true;
|
|
6064
|
+
}
|
|
5910
6065
|
if (needsRender) canvas!.requestRenderAll();
|
|
5911
6066
|
// WidgetShape drag bar must also hide when the pointer leaves the canvas.
|
|
5912
6067
|
setHoveredWidgetShape(null);
|
|
@@ -5969,6 +6124,7 @@
|
|
|
5969
6124
|
// Use silent snapshot — don't mark dirty or trigger autosave for the initial state
|
|
5970
6125
|
takeHistorySnapshot(true);
|
|
5971
6126
|
}
|
|
6127
|
+
markCanvasOpenStep('canvas:fabric-ready');
|
|
5972
6128
|
onReady?.();
|
|
5973
6129
|
} catch (fabricInitializationError) {
|
|
5974
6130
|
console.error(`${initializationLogContext}: Failed to create Fabric canvas`, fabricInitializationError);
|
|
@@ -6251,27 +6407,252 @@
|
|
|
6251
6407
|
};
|
|
6252
6408
|
};
|
|
6253
6409
|
|
|
6254
|
-
/** Width
|
|
6255
|
-
|
|
6256
|
-
|
|
6257
|
-
|
|
6258
|
-
|
|
6259
|
-
|
|
6260
|
-
|
|
6410
|
+
/** Width of one logical line in the text shape's own font. */
|
|
6411
|
+
const measureCanvasTextWidth = (
|
|
6412
|
+
text: string,
|
|
6413
|
+
fontSize: number,
|
|
6414
|
+
fontFamily: string,
|
|
6415
|
+
fontWeight?: string,
|
|
6416
|
+
fontStyle?: string
|
|
6417
|
+
) => {
|
|
6418
|
+
const size = fontSize || textFontSize;
|
|
6419
|
+
const family = fontFamily || textFontFamily;
|
|
6420
|
+
const weight = fontWeight && fontWeight !== 'normal' ? fontWeight : '';
|
|
6421
|
+
const style = fontStyle && fontStyle !== 'normal' ? fontStyle : '';
|
|
6261
6422
|
try {
|
|
6262
6423
|
const fabricCtx = canvas && typeof canvas.getContext === 'function' ? canvas.getContext() : null;
|
|
6263
6424
|
const ctx = fabricCtx ?? document.createElement('canvas').getContext('2d');
|
|
6264
6425
|
if (ctx) {
|
|
6265
|
-
ctx.font = `${
|
|
6266
|
-
|
|
6267
|
-
return Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(measured + extra));
|
|
6426
|
+
ctx.font = `${style} ${weight} ${size}px ${family}`.replace(/\s+/g, ' ').trim();
|
|
6427
|
+
return ctx.measureText(text || '').width;
|
|
6268
6428
|
}
|
|
6269
6429
|
} catch {
|
|
6270
6430
|
/* fall through to heuristic */
|
|
6271
6431
|
}
|
|
6272
|
-
return
|
|
6432
|
+
return (text || '').length * size * 0.6;
|
|
6273
6433
|
};
|
|
6274
6434
|
|
|
6435
|
+
/** Width that fits the placeholder on one line. The stored text stays empty. */
|
|
6436
|
+
const measureDefaultTextboxWidth = () => {
|
|
6437
|
+
const text = textBoxPlaceholder || '';
|
|
6438
|
+
const extra = 2;
|
|
6439
|
+
if (!text) return MIN_TEXTBOX_WIDTH;
|
|
6440
|
+
return Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(measureCanvasTextWidth(text, textFontSize, textFontFamily) + extra));
|
|
6441
|
+
};
|
|
6442
|
+
|
|
6443
|
+
const widestLogicalLine = (tb: any) => {
|
|
6444
|
+
const lines = String(tb?.text || '').split('\n');
|
|
6445
|
+
let maxWidth = 0;
|
|
6446
|
+
for (const line of lines) {
|
|
6447
|
+
maxWidth = Math.max(
|
|
6448
|
+
maxWidth,
|
|
6449
|
+
measureCanvasTextWidth(line, tb.fontSize, tb.fontFamily, tb.fontWeight, tb.fontStyle)
|
|
6450
|
+
);
|
|
6451
|
+
}
|
|
6452
|
+
return maxWidth;
|
|
6453
|
+
};
|
|
6454
|
+
|
|
6455
|
+
const textShapeIsEmpty = (tb: any) => !String(tb?.text || '').trim();
|
|
6456
|
+
|
|
6457
|
+
/**
|
|
6458
|
+
* Width of the text with wrapping turned off, in the text box's own units.
|
|
6459
|
+
* Canvas measureText is a little short, and that extra shortfall wraps the last glyph.
|
|
6460
|
+
*/
|
|
6461
|
+
function unwrappedTextWidth(tb: any) {
|
|
6462
|
+
if (!tb) return MIN_TEXTBOX_WIDTH;
|
|
6463
|
+
const savedWidth = tb.width;
|
|
6464
|
+
try {
|
|
6465
|
+
tb.set('width', 100000);
|
|
6466
|
+
tb.initDimensions?.();
|
|
6467
|
+
const measured = tb.calcTextWidth?.() || 0;
|
|
6468
|
+
return measured > 0 ? measured : widestLogicalLine(tb);
|
|
6469
|
+
} catch (err) {
|
|
6470
|
+
console.error('Canvas: text width measure failed', err);
|
|
6471
|
+
return widestLogicalLine(tb);
|
|
6472
|
+
} finally {
|
|
6473
|
+
tb.set('width', savedWidth);
|
|
6474
|
+
tb.initDimensions?.();
|
|
6475
|
+
tb.setCoords?.();
|
|
6476
|
+
}
|
|
6477
|
+
}
|
|
6478
|
+
|
|
6479
|
+
/** A transparent box. An explicit paint chosen later is left alone. */
|
|
6480
|
+
function normalizeTextboxBackground(tb: any) {
|
|
6481
|
+
const bg = tb?.backgroundColor;
|
|
6482
|
+
if (!bg || bg === 'transparent' || /rgba\(\s*255\s*,\s*255\s*,\s*255\s*,\s*0\s*\)/.test(String(bg))) {
|
|
6483
|
+
tb.set?.({ backgroundColor: '' });
|
|
6484
|
+
}
|
|
6485
|
+
}
|
|
6486
|
+
|
|
6487
|
+
/**
|
|
6488
|
+
* Locked top/bottom scale follows the pointer on that axis and keeps the
|
|
6489
|
+
* other axis in step, so the edge stays under the mouse. Unlocked top/bottom
|
|
6490
|
+
* stretches that axis only. Left and right always reflow the wrap width.
|
|
6491
|
+
*/
|
|
6492
|
+
function scaleTextUniformFromVertical(_eventData: any, transform: any, x: number, y: number) {
|
|
6493
|
+
const target = transform.target;
|
|
6494
|
+
const localPoint = controlsUtils.getLocalPoint(transform, transform.originX, transform.originY, x, y);
|
|
6495
|
+
const signY = Math.sign(localPoint.y || transform.signY || 1);
|
|
6496
|
+
if (!transform.signY) transform.signY = signY;
|
|
6497
|
+
if (target.lockScalingFlip && transform.signY !== signY) return false;
|
|
6498
|
+
const dim = target._getTransformedDimensions();
|
|
6499
|
+
if (!dim?.y) return false;
|
|
6500
|
+
let nextScale = Math.abs((localPoint.y * target.scaleY) / dim.y);
|
|
6501
|
+
if (transform.originX === 'center' && transform.originY === 'center') nextScale *= 2;
|
|
6502
|
+
if (!Number.isFinite(nextScale) || nextScale < 0.01) return false;
|
|
6503
|
+
const oldScaleX = target.scaleX;
|
|
6504
|
+
const oldScaleY = target.scaleY;
|
|
6505
|
+
target.set({
|
|
6506
|
+
scaleX: nextScale * Math.sign(oldScaleX || 1),
|
|
6507
|
+
scaleY: nextScale * Math.sign(oldScaleY || 1),
|
|
6508
|
+
});
|
|
6509
|
+
return oldScaleX !== target.scaleX || oldScaleY !== target.scaleY;
|
|
6510
|
+
}
|
|
6511
|
+
|
|
6512
|
+
const scaleTextUniformVertical = controlsUtils.wrapWithFireEvent(
|
|
6513
|
+
'scale',
|
|
6514
|
+
controlsUtils.wrapWithFixedAnchor(scaleTextUniformFromVertical)
|
|
6515
|
+
);
|
|
6516
|
+
|
|
6517
|
+
function scaleTextVerticalHandle(eventData: any, transform: any, x: number, y: number) {
|
|
6518
|
+
const target = transform?.target;
|
|
6519
|
+
if (target && target._lockAspectRatio === false) {
|
|
6520
|
+
return controlsUtils.scalingY(eventData, transform, x, y);
|
|
6521
|
+
}
|
|
6522
|
+
return scaleTextUniformVertical(eventData, transform, x, y);
|
|
6523
|
+
}
|
|
6524
|
+
|
|
6525
|
+
/**
|
|
6526
|
+
* Middle handles stay hittable. They draw only while advanced tools are on.
|
|
6527
|
+
* Corner handles always show. The rotate handle shows only while advanced tools are on.
|
|
6528
|
+
* Top and bottom follow the pointer. Left and right reflow the width.
|
|
6529
|
+
*/
|
|
6530
|
+
function applyTextShapeChrome(tb: any) {
|
|
6531
|
+
if (!tb || tb.type !== 'textbox' || !tb.controls) return;
|
|
6532
|
+
const drawNothing = () => {};
|
|
6533
|
+
const remember = (control: any) => {
|
|
6534
|
+
if (control && !control._widgeticDefaultRender) {
|
|
6535
|
+
control._widgeticDefaultRender = control.render;
|
|
6536
|
+
}
|
|
6537
|
+
};
|
|
6538
|
+
const middle = ['ml', 'mr', 'mt', 'mb'];
|
|
6539
|
+
const corners = ['tl', 'tr', 'bl', 'br'];
|
|
6540
|
+
for (const key of [...middle, ...corners, 'mtr']) remember(tb.controls[key]);
|
|
6541
|
+
for (const key of middle) {
|
|
6542
|
+
const control = tb.controls[key];
|
|
6543
|
+
if (!control) continue;
|
|
6544
|
+
// Hittable either way. Drawn only while advanced tools are on.
|
|
6545
|
+
control.render = advancedFeatures ? control._widgeticDefaultRender : drawNothing;
|
|
6546
|
+
}
|
|
6547
|
+
if (tb.controls.mt) tb.controls.mt.actionHandler = scaleTextVerticalHandle;
|
|
6548
|
+
if (tb.controls.mb) tb.controls.mb.actionHandler = scaleTextVerticalHandle;
|
|
6549
|
+
for (const key of corners) {
|
|
6550
|
+
const control = tb.controls[key];
|
|
6551
|
+
if (!control) continue;
|
|
6552
|
+
control.actionHandler = controlsUtils.scalingEqually;
|
|
6553
|
+
control.render = control._widgeticDefaultRender;
|
|
6554
|
+
}
|
|
6555
|
+
tb.setControlsVisibility({
|
|
6556
|
+
ml: true,
|
|
6557
|
+
mr: true,
|
|
6558
|
+
mt: true,
|
|
6559
|
+
mb: true,
|
|
6560
|
+
tl: true,
|
|
6561
|
+
tr: true,
|
|
6562
|
+
bl: true,
|
|
6563
|
+
br: true,
|
|
6564
|
+
mtr: !!advancedFeatures,
|
|
6565
|
+
});
|
|
6566
|
+
tb.lockSkewingX = true;
|
|
6567
|
+
tb.lockSkewingY = true;
|
|
6568
|
+
tb.lockScalingFlip = true;
|
|
6569
|
+
tb.borderOpacityWhenMoving = 0;
|
|
6570
|
+
if (tb.padding) tb.set({ padding: 0 });
|
|
6571
|
+
}
|
|
6572
|
+
|
|
6573
|
+
function refreshTextShapeChrome() {
|
|
6574
|
+
if (!canvas) return;
|
|
6575
|
+
const walk = (objects: any[]) => {
|
|
6576
|
+
for (const obj of objects || []) {
|
|
6577
|
+
if (obj?.type === 'textbox') applyTextShapeChrome(obj);
|
|
6578
|
+
const children = obj?.getObjects?.();
|
|
6579
|
+
if (children?.length) walk(children);
|
|
6580
|
+
}
|
|
6581
|
+
};
|
|
6582
|
+
walk(canvas.getObjects());
|
|
6583
|
+
canvas.requestRenderAll();
|
|
6584
|
+
}
|
|
6585
|
+
|
|
6586
|
+
let textChromeAdvanced: boolean | null = null;
|
|
6587
|
+
$: if (canvas && textChromeAdvanced !== advancedFeatures) {
|
|
6588
|
+
textChromeAdvanced = advancedFeatures;
|
|
6589
|
+
refreshTextShapeChrome();
|
|
6590
|
+
}
|
|
6591
|
+
|
|
6592
|
+
/** Fold a uniform scale into fontSize so the glyphs are redrawn, not stretched. */
|
|
6593
|
+
function bakeTextboxScaleIntoFont(tb: any) {
|
|
6594
|
+
if (!tb || tb.type !== 'textbox' || tb._bakingTextScale) return;
|
|
6595
|
+
const scaleX = tb.scaleX || 1;
|
|
6596
|
+
const scaleY = tb.scaleY || 1;
|
|
6597
|
+
const scale = Math.max(Math.abs(scaleX), Math.abs(scaleY));
|
|
6598
|
+
tb.lockSkewingX = true;
|
|
6599
|
+
tb.lockSkewingY = true;
|
|
6600
|
+
if (Math.abs(scale - 1) < 0.001 && !tb.skewX && !tb.skewY) return;
|
|
6601
|
+
const fontSize = Math.max(1, (tb.fontSize || textFontSize) * scale);
|
|
6602
|
+
const width = Math.max(MIN_TEXTBOX_WIDTH, (tb.width || MIN_TEXTBOX_WIDTH) * scale);
|
|
6603
|
+
const styles = tb.styles;
|
|
6604
|
+
if (styles && Math.abs(scale - 1) >= 0.001) {
|
|
6605
|
+
for (const lineIndex of Object.keys(styles)) {
|
|
6606
|
+
const line = styles[lineIndex];
|
|
6607
|
+
if (!line) continue;
|
|
6608
|
+
for (const charIndex of Object.keys(line)) {
|
|
6609
|
+
const style = line[charIndex];
|
|
6610
|
+
if (style && typeof style.fontSize === 'number') {
|
|
6611
|
+
style.fontSize = Math.max(1, style.fontSize * scale);
|
|
6612
|
+
}
|
|
6613
|
+
}
|
|
6614
|
+
}
|
|
6615
|
+
}
|
|
6616
|
+
tb._bakingTextScale = true;
|
|
6617
|
+
try {
|
|
6618
|
+
tb.set({ fontSize, width, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0 });
|
|
6619
|
+
tb.initDimensions?.();
|
|
6620
|
+
tb.setCoords?.();
|
|
6621
|
+
} finally {
|
|
6622
|
+
tb._bakingTextScale = false;
|
|
6623
|
+
}
|
|
6624
|
+
canvasLog('Canvas: text scale baked into font size', { fontSize: Math.round(fontSize), width: Math.round(width) });
|
|
6625
|
+
}
|
|
6626
|
+
|
|
6627
|
+
/** Gray "Type here" drawn only while the shape is empty and not being edited. */
|
|
6628
|
+
function paintEmptyTextPlaceholder(tb: any, ctx: CanvasRenderingContext2D) {
|
|
6629
|
+
if (skipTextPlaceholder || tb.isEditing || !textShapeIsEmpty(tb)) return;
|
|
6630
|
+
const label = textBoxPlaceholder || '';
|
|
6631
|
+
if (!label) return;
|
|
6632
|
+
const fontSize = tb.fontSize || textFontSize;
|
|
6633
|
+
const weight = tb.fontWeight && tb.fontWeight !== 'normal' ? tb.fontWeight : '';
|
|
6634
|
+
const style = tb.fontStyle && tb.fontStyle !== 'normal' ? tb.fontStyle : '';
|
|
6635
|
+
ctx.save();
|
|
6636
|
+
ctx.fillStyle = TEXT_PLACEHOLDER_FILL;
|
|
6637
|
+
ctx.font = `${style} ${weight} ${fontSize}px ${tb.fontFamily || textFontFamily}`.replace(/\s+/g, ' ').trim();
|
|
6638
|
+
ctx.textBaseline = 'alphabetic';
|
|
6639
|
+
const fraction = tb._fontSizeFraction || 0.222;
|
|
6640
|
+
const baseline = -tb.height / 2 + fontSize - fontSize * fraction;
|
|
6641
|
+
const align = tb.textAlign || 'left';
|
|
6642
|
+
let x = -tb.width / 2;
|
|
6643
|
+
if (align === 'center' || align === 'justify-center') {
|
|
6644
|
+
ctx.textAlign = 'center';
|
|
6645
|
+
x = 0;
|
|
6646
|
+
} else if (align === 'right' || align === 'justify-right' || align === 'end') {
|
|
6647
|
+
ctx.textAlign = 'right';
|
|
6648
|
+
x = tb.width / 2;
|
|
6649
|
+
} else {
|
|
6650
|
+
ctx.textAlign = 'left';
|
|
6651
|
+
}
|
|
6652
|
+
ctx.fillText(label, x, baseline);
|
|
6653
|
+
ctx.restore();
|
|
6654
|
+
}
|
|
6655
|
+
|
|
6275
6656
|
const createArrowGeometry = (
|
|
6276
6657
|
startPoint: { x: number; y: number },
|
|
6277
6658
|
endPoint: { x: number; y: number }
|
|
@@ -6585,65 +6966,49 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
6585
6966
|
activateSelectTool();
|
|
6586
6967
|
};
|
|
6587
6968
|
|
|
6588
|
-
//
|
|
6589
|
-
|
|
6590
|
-
let _textboxOriginalWidth: number | null = null;
|
|
6591
|
-
const TEXTBOX_AUTO_WIDTH_PADDING = 4;
|
|
6592
|
-
|
|
6593
|
-
/** Measure the widest logical line (split by \n only) and expand textbox width.
|
|
6594
|
-
* Uses a temporary large width to get unwrapped line measurements. */
|
|
6595
|
-
const autoExpandTextboxWidth = () => {
|
|
6969
|
+
// Click-created text takes the width of its widest line, and can shrink when that line gets shorter.
|
|
6970
|
+
const growTextShapeWidth = () => {
|
|
6596
6971
|
const tb = activeEditingTextbox;
|
|
6597
|
-
if (!tb || !tb.isEditing) return;
|
|
6598
|
-
|
|
6599
|
-
const
|
|
6600
|
-
tb.
|
|
6601
|
-
|
|
6602
|
-
|
|
6603
|
-
|
|
6604
|
-
|
|
6605
|
-
|
|
6606
|
-
|
|
6607
|
-
|
|
6608
|
-
|
|
6609
|
-
|
|
6610
|
-
const desiredWidth = maxLineWidth + TEXTBOX_AUTO_WIDTH_PADDING + (tb.padding || 0) * 2;
|
|
6611
|
-
const newWidth = Math.max(desiredWidth, MIN_TEXTBOX_WIDTH);
|
|
6612
|
-
tb.set({ width: newWidth });
|
|
6613
|
-
tb.initDimensions();
|
|
6614
|
-
tb.setCoords();
|
|
6615
|
-
if (newWidth !== prevWidth) {
|
|
6616
|
-
canvas?.requestRenderAll();
|
|
6972
|
+
if (!tb || !tb.isEditing || tb._textAutoSize === false) return;
|
|
6973
|
+
const selectionStart = tb.selectionStart;
|
|
6974
|
+
const selectionEnd = tb.selectionEnd;
|
|
6975
|
+
const caretSlack = TEXT_FIT_SLACK + (tb.cursorWidth || 2);
|
|
6976
|
+
const needed = Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(unwrappedTextWidth(tb) + caretSlack));
|
|
6977
|
+
if (Math.abs(needed - tb.width) > 0.5) {
|
|
6978
|
+
tb.set('width', needed);
|
|
6979
|
+
tb.initDimensions?.();
|
|
6980
|
+
tb.setCoords?.();
|
|
6981
|
+
}
|
|
6982
|
+
if (tb.selectionStart !== selectionStart || tb.selectionEnd !== selectionEnd) {
|
|
6983
|
+
tb.selectionStart = selectionStart;
|
|
6984
|
+
tb.selectionEnd = selectionEnd;
|
|
6617
6985
|
}
|
|
6986
|
+
canvas?.requestRenderAll();
|
|
6618
6987
|
};
|
|
6619
6988
|
|
|
6620
|
-
/** On text:changed, expand width to fit content (Tldraw behavior). */
|
|
6621
6989
|
const handleTextChanged = () => {
|
|
6622
|
-
|
|
6990
|
+
growTextShapeWidth();
|
|
6623
6991
|
};
|
|
6624
6992
|
|
|
6625
|
-
/**
|
|
6626
|
-
|
|
6627
|
-
|
|
6628
|
-
if (
|
|
6629
|
-
|
|
6630
|
-
|
|
6631
|
-
|
|
6632
|
-
|
|
6633
|
-
|
|
6634
|
-
|
|
6635
|
-
|
|
6636
|
-
|
|
6637
|
-
|
|
6638
|
-
|
|
6639
|
-
|
|
6640
|
-
|
|
6641
|
-
|
|
6642
|
-
|
|
6643
|
-
|
|
6644
|
-
tb.setCoords();
|
|
6645
|
-
if (tb.width !== prevWidth) canvas?.requestRenderAll();
|
|
6646
|
-
};
|
|
6993
|
+
/** Empty text shows the gray placeholder again. Real text hugs its line. */
|
|
6994
|
+
function settleTextShapeAfterEdit(tb: any) {
|
|
6995
|
+
if (!tb || tb.type !== 'textbox') return;
|
|
6996
|
+
if (textShapeIsEmpty(tb)) {
|
|
6997
|
+
if (tb.text) tb.set('text', '');
|
|
6998
|
+
const width = Math.max(
|
|
6999
|
+
MIN_TEXTBOX_WIDTH,
|
|
7000
|
+
Math.ceil(measureCanvasTextWidth(textBoxPlaceholder || '', tb.fontSize, tb.fontFamily, tb.fontWeight, tb.fontStyle) + 2)
|
|
7001
|
+
);
|
|
7002
|
+
tb.set({ width, scaleX: 1, scaleY: 1, skewX: 0, skewY: 0 });
|
|
7003
|
+
} else if (tb._textAutoSize !== false) {
|
|
7004
|
+
const width = Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(unwrappedTextWidth(tb) + TEXT_FIT_SLACK));
|
|
7005
|
+
tb.set({ width });
|
|
7006
|
+
}
|
|
7007
|
+
tb.initDimensions?.();
|
|
7008
|
+
tb.setCoords?.();
|
|
7009
|
+
tb.dirty = true;
|
|
7010
|
+
canvas?.requestRenderAll();
|
|
7011
|
+
}
|
|
6647
7012
|
|
|
6648
7013
|
const attachActiveTextListeners = () => {
|
|
6649
7014
|
detachActiveTextListeners();
|
|
@@ -6653,10 +7018,6 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
6653
7018
|
canvas.on('text:changed', handleTextChanged);
|
|
6654
7019
|
}
|
|
6655
7020
|
|
|
6656
|
-
if (activeEditingTextbox) {
|
|
6657
|
-
_textboxOriginalWidth = activeEditingTextbox.width;
|
|
6658
|
-
}
|
|
6659
|
-
|
|
6660
7021
|
if (isBrowser) {
|
|
6661
7022
|
window.addEventListener('keydown', handleGlobalKeydownDuringTextEdit);
|
|
6662
7023
|
}
|
|
@@ -6679,13 +7040,11 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
6679
7040
|
}
|
|
6680
7041
|
|
|
6681
7042
|
try {
|
|
6682
|
-
shrinkTextboxToFit();
|
|
6683
7043
|
activeEditingTextbox.exitEditing();
|
|
6684
7044
|
} catch (exitError) {
|
|
6685
7045
|
console.error('Failed to exit text editing mode', exitError);
|
|
6686
7046
|
}
|
|
6687
7047
|
|
|
6688
|
-
_textboxOriginalWidth = null;
|
|
6689
7048
|
canvas?.requestRenderAll();
|
|
6690
7049
|
activeEditingTextbox = null;
|
|
6691
7050
|
detachActiveTextListeners();
|
|
@@ -7450,6 +7809,80 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
7450
7809
|
});
|
|
7451
7810
|
}
|
|
7452
7811
|
|
|
7812
|
+
function createCanvasTextShape(bounds: { left: number; top: number; width: number; autoSize: boolean }) {
|
|
7813
|
+
const text = new Textbox('', {
|
|
7814
|
+
left: bounds.left,
|
|
7815
|
+
top: bounds.top,
|
|
7816
|
+
width: Math.max(bounds.width, MIN_TEXTBOX_WIDTH),
|
|
7817
|
+
originX: 'left',
|
|
7818
|
+
originY: 'top',
|
|
7819
|
+
fontSize: textFontSize,
|
|
7820
|
+
fontFamily: textFontFamily,
|
|
7821
|
+
fill: textFillColor,
|
|
7822
|
+
editable: true,
|
|
7823
|
+
backgroundColor: textBackgroundColor,
|
|
7824
|
+
padding: 0,
|
|
7825
|
+
splitByGrapheme: true,
|
|
7826
|
+
lockSkewingX: true,
|
|
7827
|
+
lockSkewingY: true,
|
|
7828
|
+
lockScalingFlip: true,
|
|
7829
|
+
cursorDelay: 0,
|
|
7830
|
+
cursorDuration: TEXT_CURSOR_DURATION,
|
|
7831
|
+
});
|
|
7832
|
+
(text as any)._textAutoSize = bounds.autoSize;
|
|
7833
|
+
(text as any)._lockAspectRatio = true;
|
|
7834
|
+
(text as any)._fillOpacity = 0;
|
|
7835
|
+
return text;
|
|
7836
|
+
}
|
|
7837
|
+
|
|
7838
|
+
/** Empty text starts with a blinking caret. Existing text can be selected instead. */
|
|
7839
|
+
function beginTextEditing(text: any, options?: { selectAll?: boolean }) {
|
|
7840
|
+
if (!text) return;
|
|
7841
|
+
activeEditingTextbox = text;
|
|
7842
|
+
text.cursorDelay = 0;
|
|
7843
|
+
text.cursorDuration = TEXT_CURSOR_DURATION;
|
|
7844
|
+
// Auto-size text hugs its widest line. A dragged box keeps the width the user set.
|
|
7845
|
+
if (!textShapeIsEmpty(text) && text._textAutoSize !== false && !text.isEditing) {
|
|
7846
|
+
const fitted = Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(unwrappedTextWidth(text) + TEXT_FIT_SLACK));
|
|
7847
|
+
if (Math.abs(fitted - text.width) > 0.5) {
|
|
7848
|
+
text.set('width', fitted);
|
|
7849
|
+
text.initDimensions?.();
|
|
7850
|
+
text.setCoords?.();
|
|
7851
|
+
}
|
|
7852
|
+
}
|
|
7853
|
+
text.enterEditing();
|
|
7854
|
+
const selectAll = !!options?.selectAll && !textShapeIsEmpty(text);
|
|
7855
|
+
if (selectAll) {
|
|
7856
|
+
text.selectAll();
|
|
7857
|
+
} else {
|
|
7858
|
+
text.selectionStart = 0;
|
|
7859
|
+
text.selectionEnd = 0;
|
|
7860
|
+
const area = text.hiddenTextarea as HTMLTextAreaElement | undefined;
|
|
7861
|
+
if (area) {
|
|
7862
|
+
area.focus({ preventScroll: true });
|
|
7863
|
+
try { area.setSelectionRange(0, 0); } catch { /* textarea may not be ready */ }
|
|
7864
|
+
}
|
|
7865
|
+
text.initDelayedCursor?.(true);
|
|
7866
|
+
}
|
|
7867
|
+
attachActiveTextListeners();
|
|
7868
|
+
canvas?.requestRenderAll();
|
|
7869
|
+
// The creating click can steal focus after mouseup. Put it back once that event has finished.
|
|
7870
|
+
requestAnimationFrame(() => {
|
|
7871
|
+
requestAnimationFrame(() => {
|
|
7872
|
+
if (activeEditingTextbox !== text || !text.isEditing) return;
|
|
7873
|
+
const area = text.hiddenTextarea as HTMLTextAreaElement | undefined;
|
|
7874
|
+
area?.focus({ preventScroll: true });
|
|
7875
|
+
if (!selectAll) {
|
|
7876
|
+
text.selectionStart = 0;
|
|
7877
|
+
text.selectionEnd = 0;
|
|
7878
|
+
try { area?.setSelectionRange(0, 0); } catch { /* ignore */ }
|
|
7879
|
+
text.initDelayedCursor?.(true);
|
|
7880
|
+
}
|
|
7881
|
+
canvas?.requestRenderAll();
|
|
7882
|
+
});
|
|
7883
|
+
});
|
|
7884
|
+
}
|
|
7885
|
+
|
|
7453
7886
|
// Text object method
|
|
7454
7887
|
export function onSelectTextTool() {
|
|
7455
7888
|
setActiveTool('text', () => {
|
|
@@ -7559,12 +7992,11 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
7559
7992
|
|
|
7560
7993
|
const pointer = canvas.getScenePoint(event.e);
|
|
7561
7994
|
|
|
7562
|
-
//
|
|
7563
|
-
//
|
|
7564
|
-
// so the text fits nicely without wrapping.
|
|
7995
|
+
// A click starts just wide enough for the caret. Typing widens it to the longest line.
|
|
7996
|
+
// A drag keeps the width the pointer drew, and the text wraps inside it.
|
|
7565
7997
|
const width = Math.abs(pointer.x - drawingStartPoint.x);
|
|
7566
7998
|
const textboxWidth = width < MIN_DRAW_DISTANCE
|
|
7567
|
-
?
|
|
7999
|
+
? Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(textFontSize))
|
|
7568
8000
|
: Math.max(width, MIN_TEXTBOX_WIDTH);
|
|
7569
8001
|
|
|
7570
8002
|
// Calculate position (support drag in any direction)
|
|
@@ -7574,19 +8006,11 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
7574
8006
|
removePreviewObject();
|
|
7575
8007
|
|
|
7576
8008
|
try {
|
|
7577
|
-
const text =
|
|
7578
|
-
left
|
|
7579
|
-
top
|
|
8009
|
+
const text = createCanvasTextShape({
|
|
8010
|
+
left,
|
|
8011
|
+
top,
|
|
7580
8012
|
width: textboxWidth,
|
|
7581
|
-
|
|
7582
|
-
fontFamily: textFontFamily,
|
|
7583
|
-
fill: textFillColor,
|
|
7584
|
-
editable: true,
|
|
7585
|
-
backgroundColor: textBackgroundColor,
|
|
7586
|
-
padding: 6,
|
|
7587
|
-
splitByGrapheme: true, // Enable character-level wrapping for long words
|
|
7588
|
-
lockSkewingX: false, // Allow skew with Shift + middle controls
|
|
7589
|
-
lockSkewingY: false
|
|
8013
|
+
autoSize: width < MIN_DRAW_DISTANCE,
|
|
7590
8014
|
});
|
|
7591
8015
|
|
|
7592
8016
|
canvas.add(text);
|
|
@@ -7596,10 +8020,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
7596
8020
|
// Auto-add to frame if drawn inside one
|
|
7597
8021
|
checkAndAddNewShapeToFrame(text);
|
|
7598
8022
|
|
|
7599
|
-
|
|
7600
|
-
text.enterEditing();
|
|
7601
|
-
text.selectAll();
|
|
7602
|
-
attachActiveTextListeners();
|
|
8023
|
+
beginTextEditing(text);
|
|
7603
8024
|
|
|
7604
8025
|
takeHistorySnapshot();
|
|
7605
8026
|
|
|
@@ -9355,6 +9776,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
9355
9776
|
'_boxStroke', '_boxStrokeWidth', '_boxStrokeDashArray',
|
|
9356
9777
|
'_boxCornerRadius', '_boxFill',
|
|
9357
9778
|
'_textPadding',
|
|
9779
|
+
'_textAutoSize',
|
|
9358
9780
|
'_textStrokeColor', '_textStrokeWidth',
|
|
9359
9781
|
// Proportional resize lock (set for icon/symbol library shapes)
|
|
9360
9782
|
'_lockAspectRatio',
|
|
@@ -9652,6 +10074,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
9652
10074
|
*/
|
|
9653
10075
|
export async function loadCanvas(jsonString: string): Promise<void> {
|
|
9654
10076
|
if (!canvas) return;
|
|
10077
|
+
markCanvasOpenStep('canvas:load-json-start');
|
|
9655
10078
|
canvasLoadPhase = 'loading';
|
|
9656
10079
|
// Suppress history snapshots while loading (object:added etc. would fire)
|
|
9657
10080
|
isRestoringHistory = true;
|
|
@@ -9796,6 +10219,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
9796
10219
|
historyIndex = 0;
|
|
9797
10220
|
isRestoringHistory = false;
|
|
9798
10221
|
canvasLoadPhase = 'ready';
|
|
10222
|
+
markCanvasOpenStep('canvas:load-json-end');
|
|
9799
10223
|
} catch (err) {
|
|
9800
10224
|
console.error('Canvas: loadCanvas failed', err);
|
|
9801
10225
|
isRestoringHistory = false;
|
|
@@ -9828,24 +10252,18 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
9828
10252
|
}
|
|
9829
10253
|
|
|
9830
10254
|
export function addText() {
|
|
9831
|
-
|
|
9832
|
-
|
|
10255
|
+
const textBoxWidth = Math.max(MIN_TEXTBOX_WIDTH, Math.ceil(textFontSize));
|
|
10256
|
+
const text = createCanvasTextShape({
|
|
9833
10257
|
width: textBoxWidth,
|
|
9834
10258
|
top: Math.min(Math.random() * canvas!.height, canvas!.height - textBoxWidth),
|
|
9835
10259
|
left: Math.min(Math.random() * canvas!.width, canvas!.width - textBoxWidth),
|
|
9836
|
-
|
|
9837
|
-
fontSize: 20,
|
|
9838
|
-
fontFamily: 'Arial',
|
|
9839
|
-
padding: 6,
|
|
9840
|
-
splitByGrapheme: true
|
|
10260
|
+
autoSize: true,
|
|
9841
10261
|
});
|
|
9842
|
-
|
|
10262
|
+
text.set({ fill: textFillColor });
|
|
10263
|
+
|
|
9843
10264
|
canvas!.add(text);
|
|
9844
10265
|
canvas!.setActiveObject(text);
|
|
9845
|
-
|
|
9846
|
-
text.enterEditing();
|
|
9847
|
-
text.selectAll();
|
|
9848
|
-
attachActiveTextListeners();
|
|
10266
|
+
beginTextEditing(text);
|
|
9849
10267
|
canvas!.requestRenderAll();
|
|
9850
10268
|
}
|
|
9851
10269
|
|
|
@@ -12202,6 +12620,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
12202
12620
|
ctx.restore();
|
|
12203
12621
|
}
|
|
12204
12622
|
}
|
|
12623
|
+
if (!forClipping) paintEmptyTextPlaceholder(this, ctx);
|
|
12205
12624
|
};
|
|
12206
12625
|
|
|
12207
12626
|
textbox._hasBoxStrokeRender = true;
|
|
@@ -12218,7 +12637,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
12218
12637
|
}
|
|
12219
12638
|
</script>
|
|
12220
12639
|
|
|
12221
|
-
<div class="{className ? className + ' ' : ''}canvas-container relative w-full h-full overflow-hidden {showContainerFrame ? 'border-2 border-gray-300 rounded-md' : 'border-0 rounded-none'}" style={style} use:resizeObserver>
|
|
12640
|
+
<div class="{className ? className + ' ' : ''}canvas-container relative w-full h-full overflow-hidden {showContainerFrame ? 'border-2 border-gray-300 rounded-md' : 'border-0 rounded-none'}" style="background-color: {canvasBackgroundColor}; {style}" use:resizeObserver>
|
|
12222
12641
|
|
|
12223
12642
|
<!-- Pan/Zoom Panel: Position configurable via panZoomPosition prop -->
|
|
12224
12643
|
<div class="canvas-pan-zoom-overlay {getPanZoomPositionClass(panZoomPosition)} {isCanvasLoading ? 'pointer-events-none opacity-60 transition-opacity duration-200' : 'transition-opacity duration-200'}">
|
|
@@ -12247,11 +12666,9 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
12247
12666
|
<canvas class="canvas-el" dir="ltr" bind:this={canvasEl}></canvas>
|
|
12248
12667
|
|
|
12249
12668
|
{#if isCanvasLoading}
|
|
12250
|
-
|
|
12251
|
-
|
|
12252
|
-
|
|
12253
|
-
<span style="font-size:14px;font-weight:500;color:#6b7280;letter-spacing:0.02em;">{$t('canvas.state.loading')}</span>
|
|
12254
|
-
</div>
|
|
12669
|
+
<!-- The board stays visible while its content loads. Input waits, so a late load cannot replace what was just drawn. -->
|
|
12670
|
+
<div class="canvas-loader absolute inset-0 z-30 cursor-progress">
|
|
12671
|
+
<CanvasBoardPlaceholder showSurface={false} />
|
|
12255
12672
|
</div>
|
|
12256
12673
|
{/if}
|
|
12257
12674
|
|
|
@@ -12449,19 +12866,6 @@ function checkAndAddNewShapeToFrame(shape: any) {
|
|
|
12449
12866
|
-ms-user-select: none;
|
|
12450
12867
|
}
|
|
12451
12868
|
|
|
12452
|
-
.canvas-loader-spinner {
|
|
12453
|
-
width: 36px;
|
|
12454
|
-
height: 36px;
|
|
12455
|
-
border: 3px solid #e5e7eb;
|
|
12456
|
-
border-top-color: #374151;
|
|
12457
|
-
border-radius: 50%;
|
|
12458
|
-
animation: canvas-spin 0.8s linear infinite;
|
|
12459
|
-
}
|
|
12460
|
-
|
|
12461
|
-
@keyframes canvas-spin {
|
|
12462
|
-
to { transform: rotate(360deg); }
|
|
12463
|
-
}
|
|
12464
|
-
|
|
12465
12869
|
.canvas-pan-zoom-overlay {
|
|
12466
12870
|
width: max-content;
|
|
12467
12871
|
max-width: calc(100% - 2rem);
|
|
@@ -319,7 +319,9 @@
|
|
|
319
319
|
const curId = selectedObject;
|
|
320
320
|
if (curId !== prevSelectedId) {
|
|
321
321
|
prevSelectedId = curId;
|
|
322
|
-
|
|
322
|
+
// Read the flag on this object. dimensionLocked can still be the previous
|
|
323
|
+
// selection's value in the same update, which left text looking unlocked.
|
|
324
|
+
userDimensionLock = !!(curId as any)?._lockAspectRatio;
|
|
323
325
|
}
|
|
324
326
|
}
|
|
325
327
|
$: effectiveDimensionLock = forcedLock || userDimensionLock;
|
|
@@ -355,10 +357,13 @@
|
|
|
355
357
|
userDimensionLock = !userDimensionLock;
|
|
356
358
|
if (selectedObject) {
|
|
357
359
|
(selectedObject as any)._lockAspectRatio = userDimensionLock;
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
360
|
+
// Text keeps its side handles. The lock only decides whether a resize may stretch the letters.
|
|
361
|
+
if (selectedObject.type !== 'textbox') {
|
|
362
|
+
if (userDimensionLock) {
|
|
363
|
+
selectedObject.setControlsVisibility?.({ ml: false, mr: false, mt: false, mb: false });
|
|
364
|
+
} else {
|
|
365
|
+
selectedObject.setControlsVisibility?.({ ml: true, mr: true, mt: true, mb: true });
|
|
366
|
+
}
|
|
362
367
|
}
|
|
363
368
|
selectedObject.canvas?.requestRenderAll?.();
|
|
364
369
|
}
|
|
@@ -864,7 +869,7 @@
|
|
|
864
869
|
{#if !wireframeMode}
|
|
865
870
|
<ColorIC
|
|
866
871
|
label={$t('canvas.field.fillColor')}
|
|
867
|
-
value={textboxBoxFill === 'transparent' ? '#FFFFFF' : textboxBoxFill}
|
|
872
|
+
value={!textboxBoxFill || textboxBoxFill === 'transparent' ? '#FFFFFF' : textboxBoxFill}
|
|
868
873
|
onChange={onTextboxBoxFillChange}
|
|
869
874
|
/>
|
|
870
875
|
{/if}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
/** Colour of an empty board. Canvas.svelte paints Fabric with the same value. */
|
|
3
|
+
export const CANVAS_BOARD_BACKGROUND = '#f0f0f0';
|
|
4
|
+
</script>
|
|
5
|
+
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
import { provideCanvasLanguage, useCanvasLanguage } from '../i18n/language.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The empty board, drawn without Fabric.
|
|
11
|
+
*
|
|
12
|
+
* A host shows it while the canvas code is still downloading, so the page
|
|
13
|
+
* has its board at first paint. The real canvas replaces it with the same
|
|
14
|
+
* surface, so the swap is not visible.
|
|
15
|
+
*/
|
|
16
|
+
let {
|
|
17
|
+
showLoadingLabel = true,
|
|
18
|
+
showSurface = true,
|
|
19
|
+
locale = undefined,
|
|
20
|
+
messages = undefined,
|
|
21
|
+
class: className = '',
|
|
22
|
+
style = ''
|
|
23
|
+
}: {
|
|
24
|
+
showLoadingLabel?: boolean;
|
|
25
|
+
/** False draws only the loading label, over a board that is already painted. */
|
|
26
|
+
showSurface?: boolean;
|
|
27
|
+
/** Same as Canvas: give these outside a canvas so the label speaks the page's language. */
|
|
28
|
+
locale?: string | null;
|
|
29
|
+
messages?: Readonly<Record<string, unknown>> | null;
|
|
30
|
+
class?: string;
|
|
31
|
+
style?: string;
|
|
32
|
+
} = $props();
|
|
33
|
+
|
|
34
|
+
// Inside a canvas the root already set the language; on its own, the page passes it.
|
|
35
|
+
// svelte-ignore state_referenced_locally
|
|
36
|
+
const ownsLanguage = locale !== undefined || messages !== undefined;
|
|
37
|
+
// svelte-ignore state_referenced_locally
|
|
38
|
+
const languageHandle = ownsLanguage
|
|
39
|
+
? provideCanvasLanguage({ locale: locale ?? null, messages: messages ?? null })
|
|
40
|
+
: null;
|
|
41
|
+
const { t } = languageHandle ?? useCanvasLanguage();
|
|
42
|
+
|
|
43
|
+
$effect(() => {
|
|
44
|
+
languageHandle?.update({ locale: locale ?? null, messages: messages ?? null });
|
|
45
|
+
});
|
|
46
|
+
</script>
|
|
47
|
+
|
|
48
|
+
<div
|
|
49
|
+
class="canvas-board-placeholder relative h-full w-full overflow-hidden {className}"
|
|
50
|
+
style="{showSurface ? `background-color: ${CANVAS_BOARD_BACKGROUND};` : ''} {style}"
|
|
51
|
+
aria-busy={showLoadingLabel}
|
|
52
|
+
>
|
|
53
|
+
{#if showLoadingLabel}
|
|
54
|
+
<div class="canvas-board-placeholder-status absolute inset-0 flex items-center justify-center pointer-events-none">
|
|
55
|
+
<div class="canvas-board-placeholder-status-inner flex items-center gap-2 text-sm font-medium text-gray-500">
|
|
56
|
+
<span class="canvas-board-placeholder-spinner h-4 w-4 rounded-full border-2 border-gray-300 border-t-gray-600 animate-spin"></span>
|
|
57
|
+
<span>{$t('canvas.state.loading')}</span>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
{/if}
|
|
61
|
+
</div>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
/** Colour of an empty board. Canvas.svelte paints Fabric with the same value. */
|
|
3
|
+
export declare const CANVAS_BOARD_BACKGROUND = "#f0f0f0";
|
|
4
|
+
declare const __propDef: {
|
|
5
|
+
props: Record<string, never>;
|
|
6
|
+
events: {
|
|
7
|
+
[evt: string]: CustomEvent<any>;
|
|
8
|
+
};
|
|
9
|
+
slots: {};
|
|
10
|
+
};
|
|
11
|
+
export type CanvasBoardPlaceholderProps = typeof __propDef.props;
|
|
12
|
+
export type CanvasBoardPlaceholderEvents = typeof __propDef.events;
|
|
13
|
+
export type CanvasBoardPlaceholderSlots = typeof __propDef.slots;
|
|
14
|
+
export default class CanvasBoardPlaceholder extends SvelteComponentTyped<CanvasBoardPlaceholderProps, CanvasBoardPlaceholderEvents, CanvasBoardPlaceholderSlots> {
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Import: import { Tooltip, ColorIC } from '@widgetic/canvas/components';
|
|
5
5
|
*/
|
|
6
6
|
export { default as Tooltip } from './Tooltip.svelte';
|
|
7
|
+
export { default as CanvasBoardPlaceholder, CANVAS_BOARD_BACKGROUND } from './CanvasBoardPlaceholder.svelte';
|
|
7
8
|
export { setCanvasPreferenceStorage, getCanvasPreferenceStorage } from './preferenceStorage';
|
|
8
9
|
export type { CanvasPreferenceStorage } from './preferenceStorage';
|
|
9
10
|
export { default as ColorIC } from './input-controls/ColorIC.svelte';
|
package/dist/components/index.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
// General Components
|
|
7
7
|
export { default as Tooltip } from './Tooltip.svelte';
|
|
8
|
+
// The empty board without Fabric: shown while the canvas code downloads
|
|
9
|
+
export { default as CanvasBoardPlaceholder, CANVAS_BOARD_BACKGROUND } from './CanvasBoardPlaceholder.svelte';
|
|
8
10
|
// Where the canvas keeps a visitor's choices (the host can install a store of its own)
|
|
9
11
|
export { setCanvasPreferenceStorage, getCanvasPreferenceStorage } from './preferenceStorage';
|
|
10
12
|
// Input Controllers
|
package/dist/i18n/messages.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare const CANVAS_MESSAGES: {
|
|
|
21
21
|
/** Said in a dialog of the browser when a picture cannot be loaded from the address the visitor typed. */
|
|
22
22
|
readonly 'canvas.error.imageFromUrl': "Failed to load image from URL. Make sure the URL is correct and allows cross-origin requests.";
|
|
23
23
|
readonly 'canvas.defaults.text': "Hello World!";
|
|
24
|
-
/**
|
|
24
|
+
/** Gray hint drawn on an empty text shape. It is not stored as the text. */
|
|
25
25
|
readonly 'canvas.defaults.textBox': "Type here";
|
|
26
26
|
/** The name written above a new frame. */
|
|
27
27
|
readonly 'canvas.defaults.frameLabel': "Frame";
|
package/dist/i18n/messages.js
CHANGED
|
@@ -23,7 +23,7 @@ export const CANVAS_MESSAGES = {
|
|
|
23
23
|
'canvas.error.imageFromUrl': 'Failed to load image from URL. Make sure the URL is correct and allows cross-origin requests.',
|
|
24
24
|
// ===== What a new thing says before the visitor changes it (saved with the canvas, in the language it was made in) =====
|
|
25
25
|
'canvas.defaults.text': 'Hello World!',
|
|
26
|
-
/**
|
|
26
|
+
/** Gray hint drawn on an empty text shape. It is not stored as the text. */
|
|
27
27
|
'canvas.defaults.textBox': 'Type here',
|
|
28
28
|
/** The name written above a new frame. */
|
|
29
29
|
'canvas.defaults.frameLabel': 'Frame',
|