@innovastudio/contentbuilder 1.5.226 → 1.5.227

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/index.d.ts CHANGED
@@ -135,6 +135,10 @@ interface ContentBuilderOptions {
135
135
  /** Show the image editor's "Load .cube…" button (load a LUT from the user's computer). Default false. */
136
136
  imageEditorLoadCube?: boolean;
137
137
  imageEmbed?: boolean;
138
+ /** Show resize handles on a selected image. Default true. */
139
+ imageResize?: boolean;
140
+ /** Show the floating image tool on a selected image. Default true. */
141
+ imageTool?: boolean;
138
142
  imageselect?: string;
139
143
  fileselect?: string;
140
144
  videoselect?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@innovastudio/contentbuilder",
4
- "version": "1.5.226",
4
+ "version": "1.5.227",
5
5
  "description": "",
6
6
  "main": "public/contentbuilder/contentbuilder.esm.js",
7
7
  "types": "index.d.ts",
@@ -6284,11 +6284,275 @@ class ToolbarKeys {
6284
6284
  }
6285
6285
  }
6286
6286
 
6287
+ /*
6288
+ selectframe.js — a 1px frame that shows which element is selected.
6289
+
6290
+ Drawn as its own element above the page, never as an outline on the element:
6291
+ pictures usually sit in an overflow:hidden, rounded wrapper, often scale on
6292
+ hover, and design layers stack things on top of each other, so an outline on
6293
+ the element itself is cut away or painted over.
6294
+
6295
+ The frame covers the part of the element that is actually visible (its box
6296
+ clipped by every clipping ancestor) and takes the rounded corners of
6297
+ whatever clips it. Where something else lies on top of the element (a card
6298
+ stacked over it), that stretch of the line is left out, so the frame never
6299
+ runs across what is in front. It is followed every frame while shown, so it
6300
+ stays on the element through hover transitions, motion, scrolling and edits.
6301
+
6302
+ Usage:
6303
+ const frame = new SelectFrame();
6304
+ frame.show(el, () => stillSelected(el)); // hides itself once false
6305
+ frame.hide();
6306
+ */
6307
+ const SVGNS = 'http://www.w3.org/2000/svg';
6308
+ const COLOR = '#00da89';
6309
+ const STEP = 4; // px between coverage samples along the line
6310
+ const RECHECK = 200; // ms between coverage checks while nothing moves
6311
+
6312
+ class SelectFrame {
6313
+ show(el, isCurrent) {
6314
+ this.hide();
6315
+ if (!el || !el.isConnected) return;
6316
+ const doc = el.ownerDocument;
6317
+ const win = doc.defaultView;
6318
+ if (!this.frame || this.frame.ownerDocument !== doc) {
6319
+ if (this.frame) this.frame.remove();
6320
+ const frame = doc.createElement('div');
6321
+ frame.setAttribute('data-cb-ui', '');
6322
+ frame.style.cssText = 'position:fixed;pointer-events:none;z-index:1000;display:none;';
6323
+ const svg = doc.createElementNS(SVGNS, 'svg');
6324
+ svg.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;overflow:visible;';
6325
+ const rect = doc.createElementNS(SVGNS, 'rect');
6326
+ rect.setAttribute('fill', 'none');
6327
+ rect.setAttribute('stroke', COLOR);
6328
+ rect.setAttribute('stroke-width', '1');
6329
+ svg.appendChild(rect);
6330
+ frame.appendChild(svg);
6331
+ doc.body.appendChild(frame);
6332
+ this.frame = frame;
6333
+ this.rect = rect;
6334
+ }
6335
+ const frame = this.frame;
6336
+ const rect = this.rect;
6337
+
6338
+ // Rotation and scale of the element on screen: its own transform and
6339
+ // every ancestor's (eg. a fanned card in a design layer).
6340
+ const turn = () => {
6341
+ let angle = 0,
6342
+ scale = 1;
6343
+ for (let p = el; p && p !== doc.documentElement; p = p.parentElement) {
6344
+ const t = win.getComputedStyle(p).transform;
6345
+ if (!t || t === 'none') continue;
6346
+ const m = t.match(/matrix\(([^)]+)\)/);
6347
+ if (!m) continue; // 3D: leave as is
6348
+ const [a, b] = m[1].split(',').map(parseFloat);
6349
+ angle += Math.atan2(b, a);
6350
+ scale *= Math.sqrt(a * a + b * b);
6351
+ }
6352
+ return {
6353
+ angle,
6354
+ scale
6355
+ };
6356
+ };
6357
+
6358
+ // The element's corners: its own, or those of a rounded wrapper of the
6359
+ // same size that clips it (eg. a card frame around a video).
6360
+ const ownRadius = () => {
6361
+ let radius = win.getComputedStyle(el).borderRadius;
6362
+ const w = el.offsetWidth,
6363
+ h = el.offsetHeight;
6364
+ for (let p = el.parentElement; radius === '0px' && p && p !== doc.body; p = p.parentElement) {
6365
+ if (Math.abs(p.offsetWidth - w) > 1 || Math.abs(p.offsetHeight - h) > 1) break;
6366
+ const cs = win.getComputedStyle(p);
6367
+ if (cs.overflowX !== 'visible' || cs.overflowY !== 'visible' || cs.clipPath !== 'none') radius = cs.borderRadius;
6368
+ }
6369
+ return radius;
6370
+ };
6371
+ const measure = () => {
6372
+ const r = el.getBoundingClientRect();
6373
+
6374
+ // A turned element: frame its own box, turned the same way.
6375
+ const {
6376
+ angle,
6377
+ scale
6378
+ } = turn();
6379
+ if (Math.abs(angle) > 0.002) {
6380
+ const w = el.offsetWidth * scale,
6381
+ h = el.offsetHeight * scale;
6382
+ return {
6383
+ left: (r.left + r.right) / 2 - w / 2,
6384
+ top: (r.top + r.bottom) / 2 - h / 2,
6385
+ width: w,
6386
+ height: h,
6387
+ angle,
6388
+ scale,
6389
+ radius: ownRadius()
6390
+ };
6391
+ }
6392
+ let left = r.left,
6393
+ top = r.top,
6394
+ right = r.right,
6395
+ bottom = r.bottom;
6396
+ let radius = ownRadius();
6397
+ let p = el.parentElement;
6398
+ while (p && p !== doc.body) {
6399
+ const cs = win.getComputedStyle(p);
6400
+ if (cs.overflowX !== 'visible' || cs.overflowY !== 'visible' || cs.clipPath !== 'none') {
6401
+ const c = p.getBoundingClientRect();
6402
+ const cut = c.left > left || c.top > top || c.right < right || c.bottom < bottom;
6403
+ left = Math.max(left, c.left);
6404
+ top = Math.max(top, c.top);
6405
+ right = Math.min(right, c.right);
6406
+ bottom = Math.min(bottom, c.bottom);
6407
+ // The clipping box is the visible frame (cuts it, or wraps it
6408
+ // exactly): its corners are the ones on screen.
6409
+ const wraps = Math.abs(c.left - left) < 1 && Math.abs(c.top - top) < 1 && Math.abs(c.right - right) < 1 && Math.abs(c.bottom - bottom) < 1;
6410
+ if ((cut || wraps) && cs.borderRadius !== '0px') radius = cs.borderRadius;
6411
+ }
6412
+ p = p.parentElement;
6413
+ }
6414
+ return {
6415
+ left,
6416
+ top,
6417
+ width: right - left,
6418
+ height: bottom - top,
6419
+ angle: 0,
6420
+ scale: 1,
6421
+ radius
6422
+ };
6423
+ };
6424
+
6425
+ // The corner radius as SVG rx/ry (first value; % of each side).
6426
+ const corner = (radius, w, h, scale) => {
6427
+ const v = (radius || '0').split(/[\s/]/)[0];
6428
+ const n = parseFloat(v) || 0;
6429
+ const rx = v.endsWith('%') ? w * n / 100 : n * scale;
6430
+ const ry = v.endsWith('%') ? h * n / 100 : n * scale;
6431
+ // Too large for the box: shrink both by the same factor, as the
6432
+ // browser does (border-radius: 999px is a pill, not an ellipse).
6433
+ const f = Math.min(1, rx ? w / (2 * rx) : 1, ry ? h / (2 * ry) : 1);
6434
+ return [rx * f, ry * f];
6435
+ };
6436
+
6437
+ // Is the element itself on top at this screen point? Other content
6438
+ // covers it there; the editor's own tools and this frame do not count.
6439
+ const ownHit = hit => !hit || hit === el || el.contains(hit) || hit.contains(el) || !!(hit.closest && hit.closest('[data-cb-ui], .is-tool'));
6440
+ // An element that ignores the pointer can't be tested: draw it whole.
6441
+ const testable = win.getComputedStyle(el).pointerEvents !== 'none';
6442
+
6443
+ // Leave out the stretches of the line that lie under other content.
6444
+ const coverage = m => {
6445
+ if (!testable) {
6446
+ rect.removeAttribute('stroke-dasharray');
6447
+ return;
6448
+ }
6449
+ const len = rect.getTotalLength();
6450
+ if (!len) return;
6451
+ const cx = m.width / 2,
6452
+ cy = m.height / 2;
6453
+ const cos = Math.cos(m.angle),
6454
+ sin = Math.sin(m.angle);
6455
+ const n = Math.max(8, Math.ceil(len / STEP));
6456
+ const seen = [];
6457
+ for (let i = 0; i < n; i++) {
6458
+ const pt = rect.getPointAtLength(len * (i + 0.5) / n);
6459
+ // 2px inside the line, toward the middle: a point of the element.
6460
+ let dx = cx - pt.x,
6461
+ dy = cy - pt.y;
6462
+ const d = Math.hypot(dx, dy) || 1;
6463
+ const lx = pt.x + dx / d * 2 - cx,
6464
+ ly = pt.y + dy / d * 2 - cy;
6465
+ const x = m.left + cx + lx * cos - ly * sin;
6466
+ const y = m.top + cy + lx * sin + ly * cos;
6467
+ seen.push(ownHit(doc.elementFromPoint(x, y)));
6468
+ }
6469
+ if (seen.every(Boolean)) {
6470
+ rect.removeAttribute('stroke-dasharray');
6471
+ return;
6472
+ }
6473
+ const dash = [];
6474
+ let run = 0,
6475
+ on = true;
6476
+ if (!seen[0]) dash.push(0); // dasharray starts with a drawn stretch
6477
+ on = seen[0];
6478
+ for (let i = 0; i < n; i++) {
6479
+ if (seen[i] === on) {
6480
+ run++;
6481
+ continue;
6482
+ }
6483
+ dash.push(run * len / n);
6484
+ run = 1;
6485
+ on = seen[i];
6486
+ }
6487
+ dash.push(run * len / n);
6488
+ if (dash.length % 2) dash.push(0);
6489
+ rect.setAttribute('stroke-dasharray', dash.join(' '));
6490
+ };
6491
+ let last = '';
6492
+ let checked = 0;
6493
+ const loop = {
6494
+ win,
6495
+ id: 0
6496
+ };
6497
+ const tick = () => {
6498
+ if (!el.isConnected || isCurrent && !isCurrent()) {
6499
+ frame.style.display = 'none';
6500
+ this.loop = null;
6501
+ return;
6502
+ }
6503
+ const m = measure();
6504
+ const key = `${m.left},${m.top},${m.width},${m.height},${m.angle},${m.radius}`;
6505
+ const now = win.performance.now();
6506
+ if (key !== last) {
6507
+ last = key;
6508
+ if (m.width <= 0 || m.height <= 0) {
6509
+ frame.style.display = 'none';
6510
+ } else {
6511
+ frame.style.left = m.left + 'px';
6512
+ frame.style.top = m.top + 'px';
6513
+ frame.style.width = m.width + 'px';
6514
+ frame.style.height = m.height + 'px';
6515
+ frame.style.transform = m.angle ? `rotate(${m.angle}rad)` : '';
6516
+ const [rx, ry] = corner(m.radius, m.width, m.height, m.scale);
6517
+ rect.setAttribute('x', 0.5);
6518
+ rect.setAttribute('y', 0.5);
6519
+ rect.setAttribute('width', Math.max(0, m.width - 1));
6520
+ rect.setAttribute('height', Math.max(0, m.height - 1));
6521
+ rect.setAttribute('rx', Math.max(0, rx - 0.5));
6522
+ rect.setAttribute('ry', Math.max(0, ry - 0.5));
6523
+ frame.style.display = 'block';
6524
+ coverage(m);
6525
+ checked = now;
6526
+ }
6527
+ } else if (frame.style.display === 'block' && now - checked > RECHECK) {
6528
+ coverage(m); // things can move over it while it stays put
6529
+ checked = now;
6530
+ }
6531
+ loop.id = win.requestAnimationFrame(tick);
6532
+ };
6533
+ this.loop = loop;
6534
+ tick();
6535
+ }
6536
+ hide() {
6537
+ if (this.loop) {
6538
+ this.loop.win.cancelAnimationFrame(this.loop.id);
6539
+ this.loop = null;
6540
+ }
6541
+ if (this.frame) this.frame.style.display = 'none';
6542
+ }
6543
+ }
6544
+
6287
6545
  class Util {
6288
6546
  constructor(builder) {
6289
6547
  this.builder = builder;
6290
6548
  this.dom = this.builder.dom;
6291
6549
  }
6550
+
6551
+ // A 1px frame that shows which element is selected (see selectframe.js).
6552
+ // frame.show(el, isCurrent) / frame.hide().
6553
+ createSelectFrame() {
6554
+ return new SelectFrame();
6555
+ }
6292
6556
  paintColorButton(btn, value) {
6293
6557
  if (!btn) return;
6294
6558
  let fill = btn.querySelector('span');
@@ -7935,6 +8199,22 @@ class Util {
7935
8199
  this.builder.uo.saveForUndo();
7936
8200
  let element = elm;
7937
8201
  let newelement;
8202
+
8203
+ // An image usually sits in a wrapper that is only there for the
8204
+ // picture (a frame with overflow/aspect-ratio, a link): new content
8205
+ // goes after the wrapper, not into it. Step out of every wrapper
8206
+ // that holds nothing but this image, up to the column.
8207
+ const onlyPicture = el => el.tagName.toLowerCase() === 'img' || el.childElementCount === 1 && el.textContent.trim() === '' && onlyPicture(el.firstElementChild);
8208
+ if (onlyPicture(element) && !element.closest('[data-cb-type]')) {
8209
+ const col = element.closest('.is-builder > div > div');
8210
+ while (col && element.parentElement && element.parentElement !== col && element.parentElement.childElementCount === 1 && element.parentElement.textContent.trim() === '') {
8211
+ element = element.parentElement;
8212
+ }
8213
+ // An image inside a line of text: after the paragraph, never
8214
+ // splitting it.
8215
+ const textBlock = element.parentElement && element.parentElement.closest('p, h1, h2, h3, h4, h5, h6, pre');
8216
+ if (textBlock && col && col.contains(textBlock)) element = textBlock;
8217
+ }
7938
8218
  const plugin = element.closest('[data-cb-type]');
7939
8219
  if (plugin) {
7940
8220
  element.insertAdjacentHTML('afterend', html);
@@ -30343,7 +30623,7 @@ function openImageEditor(builder, options) {
30343
30623
  // Freshly-uploaded images can be sitting in the browser cache from before
30344
30624
  // the CORS headers mattered; the classic dialog busts that the same way.
30345
30625
  let src = options.src;
30346
- if (src.indexOf('base64') === -1) {
30626
+ if (src.indexOf('data:') !== 0 && src.indexOf('blob:') !== 0) {
30347
30627
  src += (src.indexOf('?') === -1 ? '?' : '&') + `timestamp=${new Date().getTime()}`;
30348
30628
  }
30349
30629
  editor.load(src).catch(err => {
@@ -30480,17 +30760,22 @@ async function apply(builder, state) {
30480
30760
  if (options.onApply) options.onApply(url, filename);
30481
30761
  }
30482
30762
 
30763
+ /** A data: or blob: URL carries the image itself, not a file name. */
30764
+ function isInlineUrl(src) {
30765
+ return /^(data|blob):/i.test(src);
30766
+ }
30767
+
30483
30768
  /** The encoder to use, and the extension the saved file gets. */
30484
30769
  function pickFormat(src, hint) {
30485
30770
  let ext = '';
30486
30771
  src = src || '';
30487
- if (src.indexOf('base64') === -1) {
30772
+ if (!isInlineUrl(src)) {
30488
30773
  const filename = src.substring(src.lastIndexOf('/') + 1).split('?')[0];
30489
30774
  ext = filename.substr(filename.lastIndexOf('.') + 1).toLowerCase();
30490
30775
  } else if (hint) {
30491
30776
  ext = hint.substr(hint.lastIndexOf('.') + 1).toLowerCase();
30492
30777
  } else {
30493
- const match = src.match(/^data:image\/(.*?);base64,/);
30778
+ const match = src.match(/^data:image\/([a-z0-9.+-]+)[;,]/i);
30494
30779
  if (match) ext = match[1].toLowerCase();
30495
30780
  }
30496
30781
  if (ext === 'jpe') ext = 'jpg';
@@ -30508,7 +30793,7 @@ function pickFormat(src, hint) {
30508
30793
  function editedName(src, hint, ext, util, dom) {
30509
30794
  let basename;
30510
30795
  src = src || '';
30511
- if (src.indexOf('base64') === -1) {
30796
+ if (!isInlineUrl(src)) {
30512
30797
  const filename = src.substring(src.lastIndexOf('/') + 1).split('?')[0];
30513
30798
  basename = dom.baseName(filename);
30514
30799
  } else if (hint) {
@@ -30804,7 +31089,7 @@ class Image$1 {
30804
31089
  }
30805
31090
 
30806
31091
  // imagePreview.src = img.src;
30807
- if (img.src.indexOf('base64') === -1) {
31092
+ if (!/^(data|blob):/i.test(img.src)) {
30808
31093
  imagePreview.src = img.src + (img.src.indexOf('?') === -1 ? '?' : '&') + `timestamp=${new Date().getTime()}`;
30809
31094
  } else {
30810
31095
  imagePreview.src = img.src;
@@ -31174,7 +31459,7 @@ class Image$1 {
31174
31459
  if (this.builder.opts.onImageUpload) {
31175
31460
  let basename, extension, newname, newfilename;
31176
31461
  let src = img.getAttribute('src');
31177
- if (src.indexOf('base64') === -1) {
31462
+ if (!/^(data|blob):/i.test(src)) {
31178
31463
  let filename = src.substring(src.lastIndexOf('/') + 1);
31179
31464
  basename = dom.baseName(filename);
31180
31465
  extension = filename.substr(filename.lastIndexOf('.') + 1).toLowerCase();
@@ -31185,7 +31470,7 @@ class Image$1 {
31185
31470
  } else {
31186
31471
  basename = util.makeId();
31187
31472
  extension = 'png';
31188
- const match = src.match(/^data:image\/(.*?);base64,/);
31473
+ const match = src.match(/^data:image\/([a-z0-9.+-]+)[;,]/i);
31189
31474
  if (match) {
31190
31475
  extension = match[1]; // e.g., "png", "jpeg", "gif"
31191
31476
  }
@@ -31287,12 +31572,14 @@ class Image$1 {
31287
31572
  }
31288
31573
  if (this.builder.opts.imageRenameOnEdit) {
31289
31574
  let basename = '';
31290
- if (img.src.indexOf('base64') === -1) {
31575
+ if (!/^(data|blob):/i.test(img.getAttribute('src'))) {
31291
31576
  let src = img.getAttribute('src');
31292
31577
  let filename = src.substring(src.lastIndexOf('/') + 1);
31293
31578
  basename = dom.baseName(filename);
31294
- } else {
31579
+ } else if (img.getAttribute('data-filename')) {
31295
31580
  basename = dom.baseName(img.getAttribute('data-filename'));
31581
+ } else {
31582
+ basename = util.makeId();
31296
31583
  }
31297
31584
  let newname = '';
31298
31585
  if (basename.indexOf('-edit') !== -1) {
@@ -31447,7 +31734,7 @@ class Image$1 {
31447
31734
  }
31448
31735
 
31449
31736
  // imagePreview.src = img.src;
31450
- if (img.src.indexOf('base64') === -1) {
31737
+ if (!/^(data|blob):/i.test(img.src)) {
31451
31738
  imagePreview.src = img.src + (img.src.indexOf('?') === -1 ? '?' : '&') + `timestamp=${new Date().getTime()}`;
31452
31739
  } else {
31453
31740
  imagePreview.src = img.src;
@@ -31509,6 +31796,7 @@ class Image$1 {
31509
31796
  setTimeout(() => {
31510
31797
  let elm = this.builder.activeImage;
31511
31798
  if (!elm) return;
31799
+ if (this.builder.opts.imageTool === false) return;
31512
31800
  if (dom.hasClass(elm.parentNode, 'img-circular')) {
31513
31801
  elm = elm.parentNode;
31514
31802
  }
@@ -31550,6 +31838,7 @@ class Image$1 {
31550
31838
  if (this.builder.activeImage) {
31551
31839
  let imageTool = this.imageTool;
31552
31840
  if (!imageTool) return;
31841
+ if (this.builder.opts.imageTool === false) return;
31553
31842
  let elm = this.builder.activeImage;
31554
31843
  if (dom.hasClass(elm.parentNode, 'img-circular')) {
31555
31844
  elm = elm.parentNode;
@@ -31640,6 +31929,14 @@ class Image$1 {
31640
31929
  if (dom.hasClass(elm.parentNode, 'img-circular')) {
31641
31930
  elm = elm.parentNode;
31642
31931
  }
31932
+
31933
+ // imageTool: false => the image is selected, but no floating tool.
31934
+ // The tool is still created (hidden): other code looks it up.
31935
+ if (this.builder.opts.imageTool === false) {
31936
+ this.renderTool();
31937
+ this.hideImageTool();
31938
+ return;
31939
+ }
31643
31940
  const newPos = util.getElementPosition(elm);
31644
31941
  let scaleX = newPos.scaleX;
31645
31942
  let extraScaleY = this.getScale(elm).scaleX;
@@ -31654,6 +31951,12 @@ class Image$1 {
31654
31951
  const minimalTool = this.builder.imageToolMinimal;
31655
31952
  const btnLinkTool = imageTool.querySelector('.image-link');
31656
31953
  if (btnLinkTool) btnLinkTool.style.display = minimalTool ? 'none' : '';
31954
+
31955
+ // An SVG (eg. a placeholder) is a drawing, not a photo: editing
31956
+ // would only flatten it to a PNG. Edit shows for every other image.
31957
+ const btnEditTool = imageTool.querySelector('.image-edit');
31958
+ const isSvg = /^data:image\/svg\+xml|\.svg([?#]|$)/i.test(elm.getAttribute('src') || '');
31959
+ if (btnEditTool) btnEditTool.style.display = isSvg ? 'none' : '';
31657
31960
  let _toolwidth = imageTool.offsetWidth; //to get value, element must not hidden (display:none). So set display:flex before this.
31658
31961
 
31659
31962
  let w = elm.getBoundingClientRect().width * scaleX; // rendered width: includes a scale on the image itself, on an ancestor (e.g. an animated tile) and the editor zoom
@@ -65318,7 +65621,8 @@ class InscribeRegion {
65318
65621
  const opts = {
65319
65622
  // 'none' => no blue selection wash, just border + handles, like legacy.
65320
65623
  images: {
65321
- tint: 'none'
65624
+ tint: 'none',
65625
+ resize: this.builder.opts.imageResize !== false
65322
65626
  },
65323
65627
  // ContentBuilder owns the PASTE gesture: the column `paste` listener
65324
65628
  // (contentbuilder.js) cleans per the user's preference (text /
@@ -94977,6 +95281,11 @@ class ContentBuilder {
94977
95281
  // own computer).
94978
95282
  imageEditorLoadCube: false,
94979
95283
  imageEmbed: true,
95284
+ // false: no resize handles on a selected image.
95285
+ imageResize: true,
95286
+ // false: no floating tool (Change Image, Browse, Link, Edit) on a
95287
+ // selected image. The image can still be changed from a panel.
95288
+ imageTool: true,
94980
95289
  imageselect: '',
94981
95290
  fileselect: '',
94982
95291
  videoselect: '',