@broberg/cms-inline-edit 0.7.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -31,6 +31,7 @@ __export(index_exports, {
31
31
  isDangerousUrl: () => isDangerousUrl,
32
32
  isExternalHost: () => isExternalHost,
33
33
  isSchemeless: () => isSchemeless,
34
+ plainTextWithBreaks: () => plainTextWithBreaks,
34
35
  positionPopover: () => positionPopover,
35
36
  renderCurrentRefLine: () => renderCurrentRefLine,
36
37
  rescanFields: () => rescanFields,
@@ -439,7 +440,7 @@ function wireField(el, token, options) {
439
440
  e.preventDefault();
440
441
  e.stopPropagation();
441
442
  const tokenSafe = hasTokenChips(el);
442
- el.dataset.cmsOriginalValue = tokenSafe ? serializeTokenSafe(el) : el.textContent ?? "";
443
+ el.dataset.cmsOriginalValue = tokenSafe ? serializeTokenSafe(el) : plainTextWithBreaks(el);
443
444
  el.setAttribute("contenteditable", "true");
444
445
  if (tokenSafe) lockTokenChips(el);
445
446
  el.focus();
@@ -447,7 +448,7 @@ function wireField(el, token, options) {
447
448
  el.addEventListener("blur", () => {
448
449
  el.removeAttribute("contenteditable");
449
450
  const original = el.dataset.cmsOriginalValue ?? "";
450
- const current = hasTokenChips(el) ? serializeTokenSafe(el) : el.textContent ?? "";
451
+ const current = hasTokenChips(el) ? serializeTokenSafe(el) : plainTextWithBreaks(el);
451
452
  if (current.trim() === original.trim()) return;
452
453
  void saveField(el, current.trim(), token, options);
453
454
  });
@@ -457,12 +458,35 @@ function wireField(el, token, options) {
457
458
  document.execCommand("insertText", false, text);
458
459
  });
459
460
  el.addEventListener("keydown", (e) => {
460
- if (e.key === "Enter") {
461
- e.preventDefault();
462
- el.blur();
461
+ if (e.key !== "Enter") return;
462
+ e.preventDefault();
463
+ if (e.shiftKey) {
464
+ document.execCommand("insertLineBreak");
465
+ return;
463
466
  }
467
+ el.blur();
464
468
  });
465
469
  }
470
+ function plainTextWithBreaks(el) {
471
+ let out = "";
472
+ const walk = (node) => {
473
+ if (node.nodeType === Node.TEXT_NODE) {
474
+ out += node.textContent ?? "";
475
+ return;
476
+ }
477
+ if (node.nodeType !== Node.ELEMENT_NODE) return;
478
+ const tag = node.tagName.toLowerCase();
479
+ if (tag === "br") {
480
+ out += "\n";
481
+ return;
482
+ }
483
+ const blocky = tag === "div" || tag === "p";
484
+ if (blocky && out !== "" && !out.endsWith("\n")) out += "\n";
485
+ node.childNodes.forEach(walk);
486
+ };
487
+ el.childNodes.forEach(walk);
488
+ return out;
489
+ }
466
490
  var richCtx = null;
467
491
  var richToolbar = null;
468
492
  function wireRichField(el, token, options, mode) {
@@ -1575,6 +1599,7 @@ function injectStyles() {
1575
1599
  isDangerousUrl,
1576
1600
  isExternalHost,
1577
1601
  isSchemeless,
1602
+ plainTextWithBreaks,
1578
1603
  positionPopover,
1579
1604
  renderCurrentRefLine,
1580
1605
  rescanFields,