labellife-design-tool 2.5.7 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import { Box, Typography, IconButton, Button, TextField, FormGroup, FormControlL
5
5
  import CloseIcon from '@mui/icons-material/Close';
6
6
  import { jsxs, jsx, Fragment as Fragment$1 } from 'react/jsx-runtime';
7
7
  import Konva from 'konva';
8
+ import { getSnapshot } from 'mobx-state-tree';
8
9
  import CloudUploadIcon from '@mui/icons-material/CloudUpload';
9
10
  import ArrowBackIcon from '@mui/icons-material/ArrowBack';
10
11
 
@@ -201,6 +202,179 @@ function t(key, defaultValue) {
201
202
  return defaultValue || key;
202
203
  }
203
204
 
205
+ function clip(text, max) {
206
+ var s = String(text == null ? '' : text);
207
+ return s.length > max ? "".concat(s.slice(0, max), "\u2026") : s;
208
+ }
209
+ function imageSource(src) {
210
+ var s = String(src || '');
211
+ if (s.startsWith('data:') || s.startsWith('blob:')) return 'uploaded';
212
+ if (s.toLowerCase().includes('unsplash')) return 'stock photo';
213
+ return '';
214
+ }
215
+ var _short = function _short(v) {
216
+ var s = String(v == null ? '' : v);
217
+ return "".concat(s.length, ":").concat(s.slice(0, 40));
218
+ };
219
+ var isLocked = function isLocked(el) {
220
+ return el.draggable === false && el.resizable === false && el.contentEditable === false && el.styleEditable === false;
221
+ };
222
+ var quotedText = function quotedText(el, n, opts) {
223
+ return opts.includeTextContent && el.text ? "\"".concat(clip(el.text, opts.maxTextLength), "\"") : n;
224
+ };
225
+
226
+ // One row per property group: how to read its value, and how to word the change.
227
+ // A new property is one new row. `render(ctx)` returns (from, to) => sentence so a
228
+ // merged entry can be re-worded with its first `from` and last `to`.
229
+ var ROWS = [{
230
+ key: 'text',
231
+ props: ['text'],
232
+ render: function render(_ref8) {
233
+ var opts = _ref8.opts;
234
+ return function (f, t) {
235
+ return opts.includeTextContent ? "Changed the text \"".concat(clip(f, opts.maxTextLength), "\" to \"").concat(clip(t, opts.maxTextLength), "\".") : 'Changed the text.';
236
+ };
237
+ }
238
+ }, {
239
+ key: 'fontFamily',
240
+ props: ['fontFamily'],
241
+ render: function render(_ref9) {
242
+ var n = _ref9.n;
243
+ return function (f, t) {
244
+ return "Changed the font of ".concat(n, " to ").concat(t, ".");
245
+ };
246
+ }
247
+ }, {
248
+ key: 'fontSize',
249
+ props: ['fontSize'],
250
+ render: function render(_ref0) {
251
+ var n = _ref0.n;
252
+ return function (f, t) {
253
+ return "Changed the font size of ".concat(n, " from ").concat(f, " to ").concat(t, ".");
254
+ };
255
+ }
256
+ }, {
257
+ key: 'fill',
258
+ props: ['fill'],
259
+ render: function render(_ref1) {
260
+ var n = _ref1.n;
261
+ return function (f, t) {
262
+ return "Changed the colour of ".concat(n, " to ").concat(t, ".");
263
+ };
264
+ }
265
+ }, {
266
+ key: 'position',
267
+ props: ['x', 'y'],
268
+ render: function render(_ref10) {
269
+ var n = _ref10.n;
270
+ return function () {
271
+ return "Moved ".concat(n, ".");
272
+ };
273
+ }
274
+ }, {
275
+ key: 'size',
276
+ props: ['width', 'height'],
277
+ render: function render(_ref11) {
278
+ var n = _ref11.n;
279
+ return function () {
280
+ return "Resized ".concat(n, ".");
281
+ };
282
+ }
283
+ }, {
284
+ key: 'rotation',
285
+ props: ['rotation'],
286
+ render: function render(_ref12) {
287
+ var n = _ref12.n;
288
+ return function (f, t) {
289
+ return "Rotated ".concat(n, " to ").concat(t, " degrees.");
290
+ };
291
+ }
292
+ }, {
293
+ key: 'visible',
294
+ props: ['visible'],
295
+ render: function render(_ref13) {
296
+ var n = _ref13.n;
297
+ return function (f, t) {
298
+ return t ? "Showed ".concat(n, ".") : "Hid ".concat(n, ".");
299
+ };
300
+ }
301
+ }, {
302
+ key: 'lock',
303
+ props: ['draggable', 'resizable', 'contentEditable', 'styleEditable'],
304
+ value: isLocked,
305
+ render: function render(_ref14) {
306
+ var n = _ref14.n;
307
+ return function (f, t) {
308
+ return t ? "Locked ".concat(n, ".") : "Unlocked ".concat(n, ".");
309
+ };
310
+ }
311
+ }, {
312
+ key: 'crop',
313
+ props: ['cropX', 'cropY', 'cropWidth', 'cropHeight'],
314
+ render: function render(_ref15) {
315
+ var n = _ref15.n;
316
+ return function () {
317
+ return "Cropped ".concat(n, ".");
318
+ };
319
+ }
320
+ }, {
321
+ key: 'clipSrc',
322
+ props: ['clipSrc'],
323
+ render: function render(_ref16) {
324
+ var n = _ref16.n;
325
+ return function (f, t) {
326
+ return t ? "Applied a mask to ".concat(n, ".") : "Removed the mask from ".concat(n, ".");
327
+ };
328
+ }
329
+ }, {
330
+ key: 'curveEnabled',
331
+ props: ['curveEnabled'],
332
+ render: function render(_ref17) {
333
+ var n = _ref17.n,
334
+ next = _ref17.next,
335
+ opts = _ref17.opts;
336
+ return function (f, t) {
337
+ return t ? "Applied curved text to ".concat(quotedText(next, n, opts), ".") : "Removed the curve from ".concat(n, ".");
338
+ };
339
+ }
340
+ }, {
341
+ key: 'shadowEnabled',
342
+ props: ['shadowEnabled'],
343
+ render: function render(_ref18) {
344
+ var n = _ref18.n;
345
+ return function (f, t) {
346
+ return t ? "Added a shadow to ".concat(n, ".") : "Removed the shadow from ".concat(n, ".");
347
+ };
348
+ }
349
+ }, {
350
+ key: 'grayscaleEnabled',
351
+ props: ['grayscaleEnabled'],
352
+ render: function render(_ref19) {
353
+ var n = _ref19.n;
354
+ return function (f, t) {
355
+ return t ? "Made ".concat(n, " grayscale.") : "Removed the grayscale from ".concat(n, ".");
356
+ };
357
+ }
358
+ }, {
359
+ key: 'src',
360
+ props: ['src'],
361
+ value: function value(el) {
362
+ return _short(el.src);
363
+ },
364
+ render: function render(_ref20) {
365
+ var n = _ref20.n,
366
+ next = _ref20.next;
367
+ return function () {
368
+ var source = imageSource(next.src);
369
+ var note = source === 'uploaded' ? ' (uploaded by the user)' : source ? " (".concat(source, ")") : '';
370
+ return "Replaced ".concat(n).concat(note, ".");
371
+ };
372
+ }
373
+ }];
374
+ new Set(['id', 'type', 'custom', 'children'].concat(_toConsumableArray(ROWS.flatMap(function (r) {
375
+ return r.props;
376
+ }))));
377
+
204
378
  // ── Input Fields Dialog Config ───────────────────────────────────────
205
379
  var INPUT_FIELDS_CONFIG_KEY = '__labellife_input_fields_config__';
206
380
  if (typeof window !== 'undefined' && !window[INPUT_FIELDS_CONFIG_KEY]) {
@@ -1245,10 +1419,25 @@ function computeCurvedCacheBounds(element) {
1245
1419
  function syncHeightKeepingCenter(element, newHeight) {
1246
1420
  if (Math.abs(newHeight - element.height) <= 1) return;
1247
1421
  var deltaY = (newHeight - element.height) / 2;
1248
- element.set({
1422
+ var attrs = {
1249
1423
  height: newHeight,
1250
1424
  y: element.y - deltaY
1251
- });
1425
+ };
1426
+ element.set(attrs);
1427
+ // An automatic re-layout, not a user action: keep it out of the activity log.
1428
+ if (element.store && element.store.syncActivityBaseline) {
1429
+ element.store.syncActivityBaseline(element.id, attrs);
1430
+ }
1431
+ }
1432
+
1433
+ // Safari doesn't make a webfont available to canvas (not even to measureText) until some
1434
+ // canvas has actually drawn text with it, so loading alone isn't enough: draw once on a
1435
+ // hidden canvas to activate the face, then measure.
1436
+ var _scratchCtx = null;
1437
+ function primeCanvasFont(font, text) {
1438
+ if (!_scratchCtx) _scratchCtx = document.createElement('canvas').getContext('2d');
1439
+ _scratchCtx.font = font;
1440
+ _scratchCtx.fillText(text || 'Hamburgefonstiv', 0, 0);
1252
1441
  }
1253
1442
 
1254
1443
  // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
@@ -1268,6 +1457,7 @@ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
1268
1457
  var frames = 0;
1269
1458
  var _check = function check() {
1270
1459
  if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
1460
+ primeCanvasFont("".concat(fontSpec, " \"").concat(family, "\", serif"), text);
1271
1461
  var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
1272
1462
  var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
1273
1463
  var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
@@ -1278,6 +1468,28 @@ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
1278
1468
  _check();
1279
1469
  });
1280
1470
  }
1471
+
1472
+ // TEMPORARY font-timing diagnostics. Enable in the browser console with
1473
+ // window.__labellifeFontDebug = true
1474
+ // before loading a template, then copy the "[font-debug]" lines. Remove once Safari is sorted.
1475
+ function fontDebug(label, element, node) {
1476
+ if (typeof window === 'undefined' || !window.__labellifeFontDebug) return;
1477
+ try {
1478
+ var family = element.fontFamily;
1479
+ var faces = [];
1480
+ document.fonts.forEach(function (f) {
1481
+ if (f.family.replace(/["']/g, '') === family) faces.push("".concat(f.weight, "/").concat(f.status, "/").concat(f.unicodeRange.slice(0, 18)));
1482
+ });
1483
+ var lines = node && node.textArr ? node.textArr.map(function (l) {
1484
+ return Math.round(l.width);
1485
+ }) : null;
1486
+ // eslint-disable-next-line no-console
1487
+ console.log("[font-debug] t=".concat(Math.round(performance.now()), " ").concat(label, " family=\"").concat(family, "\" ") + "check=".concat(document.fonts.check("16px \"".concat(family, "\"")), " faces=").concat(JSON.stringify(faces), " ") + "boxW=".concat(Math.round(element.width), " nodeW=").concat(node ? Math.round(node.width()) : '-', " ") + "lineWidths=".concat(JSON.stringify(lines), " lines=").concat(lines ? lines.length : '-'));
1488
+ } catch (e) {
1489
+ // eslint-disable-next-line no-console
1490
+ console.log('[font-debug] failed', e);
1491
+ }
1492
+ }
1281
1493
  var TextElementComponent = observer(function (_ref) {
1282
1494
  var _element$backgroundOp;
1283
1495
  var element = _ref.element;
@@ -1305,6 +1517,7 @@ var TextElementComponent = observer(function (_ref) {
1305
1517
  var _node$getLayer2;
1306
1518
  var node = textRef.current;
1307
1519
  if (!node) return;
1520
+ fontDebug('relayout:before', element, node);
1308
1521
 
1309
1522
  // CurvedText's sceneFunc re-measures glyphs via ctx.measureText on
1310
1523
  // every draw (no internal cached layout like Konva.Text has), so
@@ -1315,9 +1528,21 @@ var TextElementComponent = observer(function (_ref) {
1315
1528
  (_node$getLayer = node.getLayer()) === null || _node$getLayer === void 0 || _node$getLayer.batchDraw();
1316
1529
  return;
1317
1530
  }
1318
- var f = node.fontFamily();
1319
- node.fontFamily('serif');
1320
- node.fontFamily(f);
1531
+
1532
+ // Re-run Konva's own layout directly — what changing `align` does, which is the manual
1533
+ // workaround that fixes this in Safari. Toggling fontFamily is the fallback if the
1534
+ // private method is ever removed.
1535
+ if (typeof node._getContextFont === 'function') {
1536
+ primeCanvasFont(node._getContextFont(), node.text());
1537
+ }
1538
+ if (typeof node._setTextData === 'function') {
1539
+ node._setTextData();
1540
+ } else {
1541
+ var f = node.fontFamily();
1542
+ node.fontFamily('serif');
1543
+ node.fontFamily(f);
1544
+ }
1545
+ fontDebug('relayout:after', element, node);
1321
1546
  var h = node.height();
1322
1547
  if (h > 0) {
1323
1548
  setTextHeight(h);
@@ -1329,12 +1554,16 @@ var TextElementComponent = observer(function (_ref) {
1329
1554
  }
1330
1555
  (_node$getLayer2 = node.getLayer()) === null || _node$getLayer2 === void 0 || _node$getLayer2.batchDraw();
1331
1556
  };
1557
+ useEffect(function () {
1558
+ fontDebug('mount', element, textRef.current);
1559
+ }, []);
1332
1560
 
1333
1561
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
1334
1562
  useEffect(function () {
1335
1563
  // Two frames' delay: Safari's canvas can still be on the fallback at event time.
1336
1564
  var onFontLoaded = function onFontLoaded() {
1337
- return requestAnimationFrame(function () {
1565
+ fontDebug('loadingdone', element, textRef.current);
1566
+ requestAnimationFrame(function () {
1338
1567
  return requestAnimationFrame(function () {
1339
1568
  var _relayoutRef$current;
1340
1569
  return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
@@ -1365,8 +1594,17 @@ var TextElementComponent = observer(function (_ref) {
1365
1594
  var _relayoutRef$current2;
1366
1595
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
1367
1596
  })["catch"](function () {});
1597
+ // Safari can still be on fallback metrics after the checks above, and re-measuring is
1598
+ // idempotent, so repeat a few times while the page settles.
1599
+ var retryTimers = [300, 1000, 2500].map(function (ms) {
1600
+ return setTimeout(function () {
1601
+ var _relayoutRef$current3;
1602
+ if (!cancelled) (_relayoutRef$current3 = relayoutRef.current) === null || _relayoutRef$current3 === void 0 || _relayoutRef$current3.call(relayoutRef);
1603
+ }, ms);
1604
+ });
1368
1605
  return function () {
1369
1606
  cancelled = true;
1607
+ retryTimers.forEach(clearTimeout);
1370
1608
  };
1371
1609
  }, [element.fontFamily, element.fontWeight, element.fontStyle]);
1372
1610
 
@@ -1548,10 +1786,10 @@ var TextElementComponent = observer(function (_ref) {
1548
1786
  var cancel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
1549
1787
  if (removed) return;
1550
1788
  removed = true;
1551
- element.set({
1552
- text: cancel ? originalText : textarea.value
1789
+ element.finishEdit({
1790
+ text: cancel ? originalText : textarea.value,
1791
+ originalText: originalText
1553
1792
  });
1554
- element.toggleEditMode(false);
1555
1793
  if (textareaRef.current === textarea) textareaRef.current = null;
1556
1794
  if (textarea.parentNode) {
1557
1795
  textarea.parentNode.removeChild(textarea);
@@ -2851,6 +3089,17 @@ var InputFieldsDialog = observer(function (_ref2) {
2851
3089
  var config = getInputFieldsConfig();
2852
3090
  var pendingFields = store._pendingInputFields || [];
2853
3091
 
3092
+ // Plain-English activity log (see model/activity-log.js). Called after the
3093
+ // element has been updated so the log can re-baseline past that change.
3094
+ var logAnswer = function logAnswer(action, el, value) {
3095
+ if (typeof store.logPopupAnswer !== 'function') return;
3096
+ store.logPopupAnswer({
3097
+ action: action,
3098
+ field: el ? getSnapshot(el) : undefined,
3099
+ value: value
3100
+ });
3101
+ };
3102
+
2854
3103
  // When disabled, clear any pending fields so they are treated as skipped
2855
3104
  useEffect(function () {
2856
3105
  if (!enabled && pendingFields.length > 0) {
@@ -2906,6 +3155,7 @@ var InputFieldsDialog = observer(function (_ref2) {
2906
3155
  text: previousValue || ''
2907
3156
  });
2908
3157
  }
3158
+ logAnswer('back', el);
2909
3159
  isGoingBackRef.current = true;
2910
3160
  store._unresolveInputField(el);
2911
3161
  }
@@ -2953,9 +3203,11 @@ var InputFieldsDialog = observer(function (_ref2) {
2953
3203
  });
2954
3204
  }
2955
3205
  }
3206
+ logAnswer('submit', el, value);
2956
3207
  store._resolveInputField(elementId);
2957
3208
  if (fields.length <= 1) {
2958
3209
  _flushRemovedElements();
3210
+ logAnswer('finish');
2959
3211
  store.history.addUndoState();
2960
3212
  if (_onComplete) _onComplete();
2961
3213
  } else {
@@ -2981,10 +3233,12 @@ var InputFieldsDialog = observer(function (_ref2) {
2981
3233
  skipped: true,
2982
3234
  removed: shouldRemove
2983
3235
  });
3236
+ logAnswer('skip', el);
2984
3237
  store._resolveInputField(elementId);
2985
3238
  if (fields.length <= 1) {
2986
3239
  var hadRemovals = removedDueToSkipRef.current.length > 0;
2987
3240
  _flushRemovedElements();
3241
+ logAnswer('finish');
2988
3242
  if (hadRemovals) store.history.addUndoState();
2989
3243
  if (_onComplete) _onComplete();
2990
3244
  } else {
@@ -3015,6 +3269,7 @@ var InputFieldsDialog = observer(function (_ref2) {
3015
3269
  text: previousValue || ''
3016
3270
  });
3017
3271
  }
3272
+ logAnswer('back', el);
3018
3273
  store._unresolveInputField(el);
3019
3274
  }
3020
3275
  setDisplayIndex(function (prev) {
@@ -3049,6 +3304,7 @@ var InputFieldsDialog = observer(function (_ref2) {
3049
3304
  canGoBack: canGoBack,
3050
3305
  onComplete: function onComplete() {
3051
3306
  _flushRemovedElements();
3307
+ logAnswer('finish');
3052
3308
  store.history.addUndoState();
3053
3309
  if (_onComplete) _onComplete();
3054
3310
  }