@broberg/cms-inline-edit 0.5.5 → 0.6.1

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.d.cts CHANGED
@@ -43,6 +43,9 @@ interface InlineEditLabels {
43
43
  linkLiveHint?: string;
44
44
  linkUrlHint?: string;
45
45
  linkUrl?: string;
46
+ linkNewTab?: string;
47
+ linkSchemeless?: string;
48
+ linkSchemelessFix?: string;
46
49
  linkInsert?: string;
47
50
  linkSave?: string;
48
51
  linkCancel?: string;
@@ -155,6 +158,18 @@ declare function resolveExistingRef(ref: string | null | undefined, pages: Linka
155
158
  * shows its raw reference rather than saying nothing.
156
159
  */
157
160
  declare function renderCurrentRefLine(box: HTMLElement, ref: string | null | undefined, pages: LinkablePage[]): void;
161
+ /**
162
+ * Open-in-a-new-tab, written as ONE decision.
163
+ *
164
+ * target and rel travel together: a `_blank` link without `rel="noopener"`
165
+ * hands the page it opens a live handle back to ours. Setting one without the
166
+ * other is not a smaller version of this feature, it is a security hole — so
167
+ * the pair is set and cleared in one place rather than at each call site.
168
+ *
169
+ * F157.7 already taught the serializer to emit an anchor carrying extras as
170
+ * inline HTML, so both attributes survive a save.
171
+ */
172
+ declare function setLinkTarget(el: HTMLElement, newTab: boolean): void;
158
173
  /**
159
174
  * Converts an edited contenteditable region's innerHTML back to Markdown so a
160
175
  * rich article body round-trips through the cms `richtext` contract (fields
@@ -168,4 +183,4 @@ declare function renderCurrentRefLine(box: HTMLElement, ref: string | null | und
168
183
  */
169
184
  declare function htmlToMarkdown(html: string): string;
170
185
 
171
- export { type InlineEditLabels, type InlineEditOptions, applyFieldSlice, buildConnectUrl, disconnect, getConnectedToken, htmlToMarkdown, initInlineEdit, positionPopover, renderCurrentRefLine, rescanFields, resolveExistingRef };
186
+ export { type InlineEditLabels, type InlineEditOptions, applyFieldSlice, buildConnectUrl, disconnect, getConnectedToken, htmlToMarkdown, initInlineEdit, positionPopover, renderCurrentRefLine, rescanFields, resolveExistingRef, setLinkTarget };
package/dist/index.d.ts CHANGED
@@ -43,6 +43,9 @@ interface InlineEditLabels {
43
43
  linkLiveHint?: string;
44
44
  linkUrlHint?: string;
45
45
  linkUrl?: string;
46
+ linkNewTab?: string;
47
+ linkSchemeless?: string;
48
+ linkSchemelessFix?: string;
46
49
  linkInsert?: string;
47
50
  linkSave?: string;
48
51
  linkCancel?: string;
@@ -155,6 +158,18 @@ declare function resolveExistingRef(ref: string | null | undefined, pages: Linka
155
158
  * shows its raw reference rather than saying nothing.
156
159
  */
157
160
  declare function renderCurrentRefLine(box: HTMLElement, ref: string | null | undefined, pages: LinkablePage[]): void;
161
+ /**
162
+ * Open-in-a-new-tab, written as ONE decision.
163
+ *
164
+ * target and rel travel together: a `_blank` link without `rel="noopener"`
165
+ * hands the page it opens a live handle back to ours. Setting one without the
166
+ * other is not a smaller version of this feature, it is a security hole — so
167
+ * the pair is set and cleared in one place rather than at each call site.
168
+ *
169
+ * F157.7 already taught the serializer to emit an anchor carrying extras as
170
+ * inline HTML, so both attributes survive a save.
171
+ */
172
+ declare function setLinkTarget(el: HTMLElement, newTab: boolean): void;
158
173
  /**
159
174
  * Converts an edited contenteditable region's innerHTML back to Markdown so a
160
175
  * rich article body round-trips through the cms `richtext` contract (fields
@@ -168,4 +183,4 @@ declare function renderCurrentRefLine(box: HTMLElement, ref: string | null | und
168
183
  */
169
184
  declare function htmlToMarkdown(html: string): string;
170
185
 
171
- export { type InlineEditLabels, type InlineEditOptions, applyFieldSlice, buildConnectUrl, disconnect, getConnectedToken, htmlToMarkdown, initInlineEdit, positionPopover, renderCurrentRefLine, rescanFields, resolveExistingRef };
186
+ export { type InlineEditLabels, type InlineEditOptions, applyFieldSlice, buildConnectUrl, disconnect, getConnectedToken, htmlToMarkdown, initInlineEdit, positionPopover, renderCurrentRefLine, rescanFields, resolveExistingRef, setLinkTarget };
package/dist/index.js CHANGED
@@ -8,6 +8,30 @@ function applyFieldSlice(current, original, next) {
8
8
  return current.slice(0, first) + next + current.slice(first + original.length);
9
9
  }
10
10
 
11
+ // src/link-target.ts
12
+ function isSchemeless(raw) {
13
+ const v = (raw || "").trim();
14
+ if (!v) return false;
15
+ if (/^[a-z][a-z0-9+.-]*:/i.test(v)) return false;
16
+ if (v.startsWith("//")) return false;
17
+ if (v.startsWith("/")) return false;
18
+ if (v.startsWith("#")) return false;
19
+ if (v.startsWith("?")) return false;
20
+ return true;
21
+ }
22
+ function withHttps(raw) {
23
+ return `https://${(raw || "").trim().replace(/^\/+/, "")}`;
24
+ }
25
+ function isExternalHost(raw, currentHost) {
26
+ const v = (raw || "").trim();
27
+ if (!/^https?:\/\//i.test(v)) return false;
28
+ try {
29
+ return new URL(v).host.toLowerCase() !== (currentHost || "").toLowerCase();
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+
11
35
  // src/token-safe.ts
12
36
  var TEXT_NODE = 3;
13
37
  var ELEMENT_NODE = 1;
@@ -62,6 +86,9 @@ var DEFAULT_LABELS = {
62
86
  linkLiveHint: "Linket peger p\xE5 <b>siden</b> \u2014 ikke p\xE5 en adresse. Flyttes siden, eller f\xE5r den et nyt navn, retter linket sig selv.",
63
87
  linkUrlHint: "En fri adresse peger pr\xE6cis d\xE9r, du skriver. Den f\xF8lger <b>ikke</b> med, hvis m\xE5let flytter sig.",
64
88
  linkUrl: "Adresse",
89
+ linkNewTab: "\xC5bn i ny fane",
90
+ linkSchemeless: "<b>{v}</b> l\xE6ses som en side p\xE5 <b>dette</b> site \u2014 ikke som en adresse ude p\xE5 nettet.",
91
+ linkSchemelessFix: "Tilf\xF8j https://",
65
92
  linkInsert: "Inds\xE6t link",
66
93
  linkSave: "Gem \xE6ndring",
67
94
  linkCancel: "Annull\xE9r",
@@ -264,10 +291,12 @@ function showIdlePill() {
264
291
  removeIdlePill();
265
292
  const wrap = document.createElement("div");
266
293
  wrap.setAttribute("data-cms-inline-edit-idle", "");
294
+ wrap.setAttribute("data-testid", "inline-edit-idle");
267
295
  wrap.style.cssText = "position:fixed;bottom:16px;left:16px;display:inline-flex;align-items:stretch;z-index:2147483647;box-shadow:0 4px 16px rgba(0,0,0,.3);border-radius:999px;overflow:hidden;";
268
296
  const edit = document.createElement("button");
269
297
  edit.type = "button";
270
298
  edit.setAttribute("data-cms-inline-edit-enter", "");
299
+ edit.setAttribute("data-testid", "inline-edit-enter");
271
300
  edit.style.cssText = "display:inline-flex;align-items:center;gap:7px;background:#1c2027;color:#fff;font:600 12px system-ui,sans-serif;padding:8px 14px;border:none;cursor:pointer;";
272
301
  const editText = document.createElement("span");
273
302
  editText.textContent = options.connectLabel;
@@ -276,6 +305,7 @@ function showIdlePill() {
276
305
  const out = document.createElement("button");
277
306
  out.type = "button";
278
307
  out.setAttribute("data-cms-inline-edit-logout", "");
308
+ out.setAttribute("data-testid", "inline-edit-logout");
279
309
  out.textContent = options.logoutLabel;
280
310
  out.style.cssText = "background:#141821;color:#9aa4b2;font:600 11px system-ui,sans-serif;padding:8px 12px;border:none;border-left:1px solid rgba(255,255,255,.12);cursor:pointer;";
281
311
  out.addEventListener("click", () => {
@@ -439,6 +469,8 @@ var savedRange = null;
439
469
  var linkDialog = null;
440
470
  var linkPages = null;
441
471
  var linkPicked = null;
472
+ var linkNewTab = false;
473
+ var linkNewTabTouched = false;
442
474
  var linkEditing = null;
443
475
  function hideLinkDialog() {
444
476
  if (linkDialog) linkDialog.style.display = "none";
@@ -520,9 +552,13 @@ function renderLinkDialog() {
520
552
  if (!d) return;
521
553
  const L = uiLabels;
522
554
  const editing = !!linkEditing;
555
+ if (!editing) {
556
+ linkNewTab = false;
557
+ linkNewTabTouched = false;
558
+ }
523
559
  const existingRef = linkEditing?.getAttribute("data-cms-ref") ?? "";
524
560
  const onPageTab = !editing || !!existingRef;
525
- d.innerHTML = `<div style="display:flex;gap:4px;padding:8px 8px 0"><button type="button" data-testid="inline-link-tab-page" data-tab="page" style="${tabCss(onPageTab)}">${L.linkTabPage}</button><button type="button" data-testid="inline-link-tab-url" data-tab="url" style="${tabCss(!onPageTab)}">${L.linkTabUrl}</button></div><div style="padding:10px 12px 12px"><div data-pane="page" style="display:${onPageTab ? "block" : "none"}"><input data-testid="inline-link-search" data-role="search" placeholder="${L.linkSearch}" style="${fieldCss()}"><div data-role="current" data-testid="inline-link-current" style="display:none;margin-top:8px;padding:7px 10px;background:rgba(0,178,255,.08);border:1px solid rgba(0,178,255,.28);border-radius:8px;font-size:11.5px;color:#c9d1dd;line-height:1.45"></div><div data-role="list" data-testid="inline-link-list" style="margin-top:8px;max-height:196px;overflow-y:auto;border:1px solid #3a3f4a;border-radius:8px;background:#141821"></div></div><div data-pane="url" style="display:${onPageTab ? "none" : "block"}"><label style="${labelCss()}">${L.linkUrl}</label><input data-testid="inline-link-url" data-role="url" placeholder="https://\u2026" style="${fieldCss()}"></div><label style="${labelCss()}">${L.linkText}</label><input data-testid="inline-link-text" data-role="text" placeholder="${L.linkTextAuto}" style="${fieldCss()}"><p data-role="hint" style="font-size:11.5px;color:#9aa3b2;margin:6px 0 0;line-height:1.5"></p><div data-role="live" style="display:flex;gap:8px;margin-top:12px;padding:9px 10px;background:rgba(0,178,255,.08);border:1px solid rgba(0,178,255,.28);border-radius:8px"><p style="margin:0;font-size:11.5px;color:#c9d1dd;line-height:1.55">${L.linkLiveHint}</p></div><div style="display:flex;gap:8px;align-items:center;margin-top:14px"><div data-role="remove"></div><div style="flex:1"></div><button type="button" data-testid="inline-link-cancel" data-role="cancel" style="${btnCss(false)}">${L.linkCancel}</button><button type="button" data-testid="inline-link-submit" data-role="submit" style="${btnCss(true)}" disabled>${editing ? L.linkSave : L.linkInsert}</button></div></div>`;
561
+ d.innerHTML = `<div style="display:flex;gap:4px;padding:8px 8px 0"><button type="button" data-testid="inline-link-tab-page" data-tab="page" style="${tabCss(onPageTab)}">${L.linkTabPage}</button><button type="button" data-testid="inline-link-tab-url" data-tab="url" style="${tabCss(!onPageTab)}">${L.linkTabUrl}</button></div><div style="padding:10px 12px 12px"><div data-pane="page" style="display:${onPageTab ? "block" : "none"}"><input data-testid="inline-link-search" data-role="search" placeholder="${L.linkSearch}" style="${fieldCss()}"><div data-role="current" data-testid="inline-link-current" style="display:none;margin-top:8px;padding:7px 10px;background:rgba(0,178,255,.08);border:1px solid rgba(0,178,255,.28);border-radius:8px;font-size:11.5px;color:#c9d1dd;line-height:1.45"></div><div data-role="list" data-testid="inline-link-list" style="margin-top:8px;max-height:196px;overflow-y:auto;border:1px solid #3a3f4a;border-radius:8px;background:#141821"></div></div><div data-pane="url" style="display:${onPageTab ? "none" : "block"}"><label style="${labelCss()}">${L.linkUrl}</label><input data-testid="inline-link-url" data-role="url" placeholder="https://\u2026" style="${fieldCss()}"><div data-role="schemeless" data-testid="inline-link-schemeless" style="display:none;margin-top:8px;padding:8px 10px;background:rgba(255,176,32,.10);border:1px solid rgba(255,176,32,.34);border-radius:8px"><p data-role="schemeless-text" style="margin:0;font-size:11.5px;color:#e8d6b0;line-height:1.5"></p><button type="button" data-testid="inline-link-add-scheme" data-role="add-scheme" style="${btnCss(false)};margin-top:7px;height:26px;font-size:12px">${L.linkSchemelessFix}</button></div></div><label style="${labelCss()}">${L.linkText}</label><input data-testid="inline-link-text" data-role="text" placeholder="${L.linkTextAuto}" style="${fieldCss()}"><p data-role="hint" style="font-size:11.5px;color:#9aa3b2;margin:6px 0 0;line-height:1.5"></p><button type="button" data-role="newtab" data-testid="inline-link-newtab" aria-pressed="false" style="display:flex;align-items:center;gap:8px;margin-top:12px;background:none;border:none;padding:0;cursor:pointer;font-family:inherit;font-size:12.5px;color:#c9d1dd"><span data-role="newtab-box" style="${checkboxCss(false)}"></span><span>${L.linkNewTab}</span></button><div data-role="live" style="display:flex;gap:8px;margin-top:12px;padding:9px 10px;background:rgba(0,178,255,.08);border:1px solid rgba(0,178,255,.28);border-radius:8px"><p style="margin:0;font-size:11.5px;color:#c9d1dd;line-height:1.55">${L.linkLiveHint}</p></div><div style="display:flex;gap:8px;align-items:center;margin-top:14px"><div data-role="remove"></div><div style="flex:1"></div><button type="button" data-testid="inline-link-cancel" data-role="cancel" style="${btnCss(false)}">${L.linkCancel}</button><button type="button" data-testid="inline-link-submit" data-role="submit" style="${btnCss(true)}" disabled>${editing ? L.linkSave : L.linkInsert}</button></div></div>`;
526
562
  const q = (role) => d.querySelector(`[data-role="${role}"]`);
527
563
  const text = q("text");
528
564
  const urlIn = q("url");
@@ -530,6 +566,8 @@ function renderLinkDialog() {
530
566
  if (editing) {
531
567
  text.value = linkEditing?.getAttribute("data-cms-ref-label") === "auto" ? "" : linkEditing?.textContent ?? "";
532
568
  if (!existingRef) urlIn.value = linkEditing?.getAttribute("href") ?? "";
569
+ linkNewTab = linkEditing?.getAttribute("target") === "_blank";
570
+ linkNewTabTouched = true;
533
571
  q("remove").appendChild(buildRemoveLink());
534
572
  }
535
573
  d.querySelectorAll("[data-tab]").forEach((btn) => {
@@ -549,6 +587,16 @@ function renderLinkDialog() {
549
587
  urlIn.oninput = syncLinkDialog;
550
588
  q("cancel").onclick = hideLinkDialog;
551
589
  q("submit").onclick = applyLink;
590
+ q("newtab").onclick = () => {
591
+ linkNewTab = !linkNewTab;
592
+ linkNewTabTouched = true;
593
+ syncLinkDialog();
594
+ };
595
+ q("add-scheme").onclick = () => {
596
+ urlIn.value = withHttps(urlIn.value);
597
+ urlIn.focus();
598
+ syncLinkDialog();
599
+ };
552
600
  renderLinkList("");
553
601
  syncLinkDialog();
554
602
  }
@@ -654,6 +702,30 @@ function syncLinkDialog() {
654
702
  q("hint").innerHTML = own ? uiLabels.linkTextOwnHint : onPage ? uiLabels.linkTextAutoHint : uiLabels.linkUrlHint;
655
703
  q("live").style.display = onPage ? "flex" : "none";
656
704
  q("submit").disabled = onPage ? !linkPicked : !q("url").value.trim();
705
+ const raw = q("url").value;
706
+ const doubtful = !onPage && isSchemeless(raw);
707
+ q("schemeless").style.display = doubtful ? "block" : "none";
708
+ if (doubtful) {
709
+ q("schemeless-text").innerHTML = uiLabels.linkSchemeless.replace(
710
+ "{v}",
711
+ escapeHtml(raw.trim())
712
+ );
713
+ }
714
+ if (!onPage && !linkNewTabTouched) {
715
+ linkNewTab = isExternalHost(raw, typeof location !== "undefined" ? location.host : "");
716
+ }
717
+ const box = q("newtab-box");
718
+ if (box) box.setAttribute("style", checkboxCss(linkNewTab));
719
+ q("newtab").setAttribute("aria-pressed", String(linkNewTab));
720
+ }
721
+ function setLinkTarget(el, newTab) {
722
+ if (newTab) {
723
+ el.setAttribute("target", "_blank");
724
+ el.setAttribute("rel", "noopener");
725
+ } else {
726
+ el.removeAttribute("target");
727
+ el.removeAttribute("rel");
728
+ }
657
729
  }
658
730
  function applyLink() {
659
731
  const d = linkDialog;
@@ -665,18 +737,21 @@ function applyLink() {
665
737
  if (!href) return;
666
738
  const ref = onPage && linkPicked ? `${linkPicked.collection}:${linkPicked.slug}` : "";
667
739
  const label = own || (onPage ? linkPicked?.title ?? href : href);
740
+ const applyTarget = (el) => setLinkTarget(el, linkNewTab);
668
741
  if (linkEditing) {
669
742
  linkEditing.setAttribute("href", href);
670
743
  if (ref) linkEditing.setAttribute("data-cms-ref", ref);
671
744
  else linkEditing.removeAttribute("data-cms-ref");
672
745
  if (ref && !own) linkEditing.setAttribute("data-cms-ref-label", "auto");
673
746
  else linkEditing.removeAttribute("data-cms-ref-label");
747
+ applyTarget(linkEditing);
674
748
  linkEditing.textContent = label;
675
749
  hideLinkDialog();
676
750
  return;
677
751
  }
678
752
  const a = document.createElement("a");
679
753
  a.setAttribute("href", href);
754
+ applyTarget(a);
680
755
  if (ref) {
681
756
  a.setAttribute("data-cms-ref", ref);
682
757
  if (!own) a.setAttribute("data-cms-ref-label", "auto");
@@ -701,6 +776,7 @@ function escapeHtml(v) {
701
776
  return v.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
702
777
  }
703
778
  var tabCss = (on) => "flex:1;background:" + (on ? "#2a2f38" : "none") + ";border:1px solid " + (on ? "#3a3f4a" : "transparent") + ";color:" + (on ? "#fff" : "#9aa3b2") + ";height:32px;border-radius:7px;font-size:13px;cursor:pointer;font-weight:500;font-family:inherit;";
779
+ var checkboxCss = (on) => "width:16px;height:16px;flex:0 0 16px;border-radius:4px;box-sizing:border-box;display:inline-block;" + (on ? `background:#00b2ff;border:1px solid #00b2ff;background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path d='M4 8.5l2.6 2.6L12 5.8' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/></svg>");background-size:100% 100%;` : "background:#141821;border:1px solid #3a3f4a;");
704
780
  var fieldCss = () => "width:100%;background:#141821;border:1px solid #3a3f4a;color:#fff;height:34px;border-radius:7px;padding:0 10px;font-size:13.5px;outline:none;font-family:inherit;box-sizing:border-box;";
705
781
  var labelCss = () => "font-size:11px;letter-spacing:.06em;text-transform:uppercase;color:#9aa3b2;margin:12px 0 5px;display:block;";
706
782
  var btnCss = (primary) => "height:33px;border-radius:7px;font-size:13px;cursor:pointer;padding:0 14px;font-weight:600;font-family:inherit;" + (primary ? "background:#00b2ff;color:#04121b;border:none;" : "background:none;border:1px solid #3a3f4a;color:#fff;font-weight:500;");
@@ -1064,6 +1140,7 @@ function showPill(el, state) {
1064
1140
  if (!pill) {
1065
1141
  pill = document.createElement("span");
1066
1142
  pill.setAttribute("data-cms-inline-edit-pill", "");
1143
+ pill.setAttribute("data-testid", "inline-edit-pill");
1067
1144
  document.body.appendChild(pill);
1068
1145
  pills.set(el, pill);
1069
1146
  }
@@ -1091,6 +1168,7 @@ function showActiveBadge(options) {
1091
1168
  const badge = document.createElement("button");
1092
1169
  badge.type = "button";
1093
1170
  badge.setAttribute("data-cms-inline-edit-badge", "");
1171
+ badge.setAttribute("data-testid", "inline-edit-badge");
1094
1172
  badge.style.cssText = "position:fixed;bottom:16px;left:16px;display:inline-flex;align-items:center;gap:7px;background:#1c2027;color:#fff;font:600 12px system-ui,sans-serif;padding:8px 14px;border-radius:999px;z-index:2147483647;box-shadow:0 4px 16px rgba(0,0,0,.3);border:none;cursor:pointer;opacity:.9;transition:opacity .15s;";
1095
1173
  const text = document.createElement("span");
1096
1174
  text.textContent = options.disconnectLabel;
@@ -1130,6 +1208,7 @@ export {
1130
1208
  positionPopover,
1131
1209
  renderCurrentRefLine,
1132
1210
  rescanFields,
1133
- resolveExistingRef
1211
+ resolveExistingRef,
1212
+ setLinkTarget
1134
1213
  };
1135
1214
  //# sourceMappingURL=index.js.map