@bettercms-ai/kit 3.0.1 → 3.1.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.
@@ -1,7 +1,7 @@
1
1
  import { cn } from "../../lib/cn.js";
2
2
  import { useKitEnv, useKitServices } from "../../context.js";
3
3
  import { Button } from "../../ui/button.js";
4
- import { shortcutHintsOn } from "../../ui/shortcut-hint.js";
4
+ import { platformKeys, shortcutHintsOn } from "../../ui/shortcut-hint.js";
5
5
  import { Menu, MenuContent, MenuItem, MenuSeparator, MenuSub, MenuSubContent, MenuSubTrigger, MenuTrigger, Popover, PopoverAnchor, PopoverContent, PopoverTrigger, Tooltip } from "../../ui/overlay.js";
6
6
  import { Textarea } from "../../ui/input.js";
7
7
  import { LinkPopoverBody } from "../link.js";
@@ -10,7 +10,6 @@ import { COMPONENT_INSERT_TOAST } from "../component-services.js";
10
10
  import { closestBlock, createIsland, defaultModel, editingHost, esc, hydrate, isEmptyHtml, placeCaret, rangeRect, rid, sanitize, serialize, textBeforeCaret, topBlock } from "./dom.js";
11
11
  import { eventKey } from "../../lib/key-event.js";
12
12
  import { ALL_FEATURES } from "./codec.js";
13
- import { applyTextLink, fromLinkValue, textLinkOf, toLinkValue } from "./link-form.js";
14
13
  import { createOutlineStore, outlineOf } from "./toc.js";
15
14
  import { CatalogComponentsProvider, InsertMenuContent, SlashMenuPanel, blockAllowed, catalogKey, noteRecent, useCatalog } from "./catalog.js";
16
15
  import { openComponentPanel } from "./component-panel.js";
@@ -19,6 +18,7 @@ import { htmlToMarkdown, looksLikeMarkdown, markdownToHtml } from "./markdown.js
19
18
  import { duplicateBlock, ensureLineAfter, insertBasic, insertIsland, liveInlineCode, liveMarkdown, moveBlock, restrictHtml, turnInto } from "./ops.js";
20
19
  import { EMPTY_FMT, FormatToolbar, MARK_FEATURE, stylesFor } from "./toolbar.js";
21
20
  import { SelectionBubble } from "./floating.js";
21
+ import { applyTextLink, fromLinkValue, textLinkOf, toLinkValue } from "./link-form.js";
22
22
  import { checklistShortcut, insertChecklist, syncTaskA11y, toggleTaskAtPointer, toggleTaskFromKey } from "./checklist.js";
23
23
  import { FN_MARK, FOOTNOTE_POPOVER_CLASS, FOOTNOTE_REFUSED, FootnoteEditor, FootnotesList, backToMarker, caretAfterMarker, insertFootnote, renumberFootnotes } from "./footnote.js";
24
24
  import { ButtonLinkPanel, applyButton, buttonAt, buttonInitial, insertButton, removeButton } from "./button.js";
@@ -396,6 +396,14 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
396
396
  setStickyTop(top);
397
397
  }, [toolbarOpen, dense]);
398
398
  const hasSlash = !!slash;
399
+ const slashWasOpen = React.useRef(false);
400
+ React.useEffect(() => {
401
+ if (hasSlash) slashWasOpen.current = true;
402
+ else if (slashWasOpen.current) {
403
+ slashWasOpen.current = false;
404
+ emit();
405
+ }
406
+ }, [hasSlash, emit]);
399
407
  React.useEffect(() => {
400
408
  if (!hasSlash) return;
401
409
  let raf = 0;
@@ -814,15 +822,18 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
814
822
  return;
815
823
  }
816
824
  if (ne.inputType === "insertText" && ne.data === "`" && allows("code")) liveInlineCode();
817
- emit();
818
825
  const s = window.getSelection();
819
- if (!s || !s.rangeCount) return setSlash(null);
826
+ if (!s || !s.rangeCount) {
827
+ emit();
828
+ return setSlash(null);
829
+ }
820
830
  const r = s.getRangeAt(0);
831
+ const opensAt = (n) => {
832
+ const blk = closestBlock(ref.current, n);
833
+ return !!blk && textBeforeCaret(blk) === "/";
834
+ };
821
835
  if (!slash) {
822
- if (single || ne.inputType !== "insertText" || ne.data !== "/" || r.startContainer.nodeType !== 3) return;
823
- const blk = closestBlock(ref.current, r.startContainer);
824
- if (!blk || textBeforeCaret(blk) !== "/") return;
825
- const rect = caretRect();
836
+ const rect = !single && ne.inputType === "insertText" && ne.data === "/" && r.startContainer.nodeType === 3 && opensAt(r.startContainer) ? caretRect() : null;
826
837
  if (rect) setSlash({
827
838
  rect,
828
839
  node: r.startContainer,
@@ -831,6 +842,7 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
831
842
  folder: false,
832
843
  active: 0
833
844
  });
845
+ else emit();
834
846
  return;
835
847
  }
836
848
  if (r.startContainer !== slash.node || r.startOffset <= slash.offset || slash.node.data[slash.offset] !== "/") return setSlash(null);
@@ -849,7 +861,7 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
849
861
  const anchor = (s?.anchorNode?.nodeType === 1 ? s.anchorNode : s?.anchorNode?.parentElement)?.closest("a") ?? null;
850
862
  if (anchor && ref.current && buttonAt(ref.current, anchor)) return openButton(anchor);
851
863
  if (!anchor && (!s || s.isCollapsed || !s.toString().trim())) {
852
- notify("Select some text first", { description: hints ? "Then press ⌘K to link it." : "Then link it." });
864
+ notify("Select some text first", { description: hints ? platformKeys("Then press ⌘K to link it.") : "Then link it." });
853
865
  return;
854
866
  }
855
867
  const rect = s && s.rangeCount ? rangeRect(s.getRangeAt(0), ref.current) : ref.current.getBoundingClientRect();
@@ -914,7 +926,28 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
914
926
  return;
915
927
  }
916
928
  run(() => {
917
- const text = window.getSelection()?.toString() || v.href;
929
+ const s = window.getSelection();
930
+ const el = ref.current;
931
+ if (el && s && s.rangeCount && !s.isCollapsed) {
932
+ const sentinel = `https://bcms-link.invalid/${rid()}`;
933
+ if (exec("createLink", sentinel)) {
934
+ el.querySelectorAll("a[href]").forEach((a) => a.getAttribute("href") === sentinel && applyTextLink(a, v));
935
+ return;
936
+ }
937
+ const r = s.getRangeAt(0);
938
+ const blk = closestBlock(el, r.startContainer);
939
+ if (blk && blk === closestBlock(el, r.endContainer)) {
940
+ const a = document.createElement("a");
941
+ applyTextLink(a, v);
942
+ a.append(r.extractContents());
943
+ r.insertNode(a);
944
+ r.selectNodeContents(a);
945
+ s.removeAllRanges();
946
+ s.addRange(r);
947
+ return;
948
+ }
949
+ }
950
+ const text = s?.toString() || v.href;
918
951
  const attr = (s) => esc(s).replace(/"/g, "&quot;");
919
952
  insertHtmlAtCaret(`<a href="${attr(v.href)}"${v.newTab ? ` target="_blank"` : ""}${v.rel ? ` rel="${attr(v.rel)}"` : ""}>${esc(text)}</a>`);
920
953
  });
@@ -938,7 +971,7 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
938
971
  s?.addRange(range);
939
972
  insertTextAtCaret(out);
940
973
  emit();
941
- notify("Rewritten with AI", { description: hints ? "Press ⌘Z to undo" : void 0 });
974
+ notify("Rewritten with AI", { description: hints ? platformKeys("Press ⌘Z to undo") : void 0 });
942
975
  }, (err) => notify("Could not rewrite the selection", { description: err instanceof Error ? err.message : String(err) }));
943
976
  };
944
977
  const onGripDown = (e) => {
@@ -1232,6 +1265,8 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
1232
1265
  const btn = dense ? "size-5 [&_svg:not([data-rt-isolate]_*)]:size-3.5" : "size-[22px] [&_svg:not([data-rt-isolate]_*)]:size-3.5";
1233
1266
  const gutterMode = readOnly || dense || single ? "none" : gutterRoom ? "outside" : "inset";
1234
1267
  const showGutter = gutterMode !== "none";
1268
+ /** BL-540: a one-line or panel field counts no words */
1269
+ const counts = !single && !dense;
1235
1270
  const minH = expanded ? "min-h-[60vh]" : single ? "min-h-9" : dense ? "min-h-[120px]" : "min-h-[190px]";
1236
1271
  const turnStyles = stylesFor(allows).filter((s) => [
1237
1272
  "p",
@@ -1280,10 +1315,16 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
1280
1315
  if (o) inserted.current = false;
1281
1316
  setInsertOpen(o);
1282
1317
  },
1283
- onMenuOpen: (name, o) => setMenus((m) => ({
1284
- ...m,
1285
- [name]: o
1286
- })),
1318
+ onMenuOpen: (name, o) => {
1319
+ setMenus((m) => ({
1320
+ ...m,
1321
+ [name]: o
1322
+ }));
1323
+ if (!o) requestAnimationFrame(() => {
1324
+ const ae = document.activeElement;
1325
+ if (!ae || ae === document.body) restoreSel();
1326
+ });
1327
+ },
1287
1328
  onStyle,
1288
1329
  onMark,
1289
1330
  onList: (kind) => allows(kind) && run(() => {
@@ -1612,7 +1653,7 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
1612
1653
  onEdit: (m) => openFootnote(m),
1613
1654
  onBack: (m) => ref.current && backToMarker(ref.current, m)
1614
1655
  }),
1615
- /* @__PURE__ */ jsxs("div", {
1656
+ counts || notice ? /* @__PURE__ */ jsxs("div", {
1616
1657
  className: "flex h-7 items-center justify-end gap-3 rounded-b-kit-lg border-t border-kit-line-subtle px-3 text-kit-2xs text-kit-fg-3",
1617
1658
  children: [notice ? /* @__PURE__ */ jsxs("span", {
1618
1659
  role: "status",
@@ -1631,7 +1672,7 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
1631
1672
  children: notice.action.label
1632
1673
  }) : null
1633
1674
  ]
1634
- }) : null, /* @__PURE__ */ jsxs("span", {
1675
+ }) : null, counts ? /* @__PURE__ */ jsxs("span", {
1635
1676
  className: "tabular-nums",
1636
1677
  children: [
1637
1678
  words.toLocaleString("en-US"),
@@ -1642,8 +1683,8 @@ function RichTextEditor({ value, onChange, readOnly, placeholder, docKey, featur
1642
1683
  Math.max(1, Math.round(words / 220)),
1643
1684
  " min read"
1644
1685
  ]
1645
- })]
1646
- }),
1686
+ }) : null]
1687
+ }) : null,
1647
1688
  bubble && !quiet && !readOnly && !slash && !link && !md && !menus.style && !menus.more && !insertOpen ? /* @__PURE__ */ jsx(SelectionBubble, {
1648
1689
  rect: bubble,
1649
1690
  avoid: toolbarOpen ? wrap.current?.querySelector("[role=\"toolbar\"][aria-label=\"Text formatting\"]")?.getBoundingClientRect() : null,