@coze-editor/react-components 0.1.0-alpha.c2621d → 0.1.0-alpha.c9f325

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/esm/index.js CHANGED
@@ -1172,6 +1172,9 @@ function mention(options) {
1172
1172
  },
1173
1173
  update(value, tr) {
1174
1174
  const options2 = tr.startState.facet(mentionConfig);
1175
+ if (!options2) {
1176
+ return value;
1177
+ }
1175
1178
  const { search = true, onOpenChange, onSearch, onTrigger } = options2;
1176
1179
  let { show } = value;
1177
1180
  let triggerContext = void 0;
@@ -1465,6 +1468,132 @@ var EmbededLineView = forwardRef4(function EmbededLineView2({ children }, ref) {
1465
1468
  }, []);
1466
1469
  return createPortal6(children, dom);
1467
1470
  });
1471
+
1472
+ // src/prefix-element/react.tsx
1473
+ import { createPortal as createPortal7 } from "react-dom";
1474
+ import { useLayoutEffect as useLayoutEffect8 } from "react";
1475
+ import { useHTMLElement as useHTMLElement6, useLatest as useLatest6 } from "@coze-editor/react-hooks";
1476
+ import { useInjector as useInjector9 } from "@coze-editor/react";
1477
+ import { Decoration as Decoration4, EditorView as EditorView8, keymap, WidgetType as WidgetType5 } from "@codemirror/view";
1478
+
1479
+ // src/prefix-element/dom.ts
1480
+ var scratchRange;
1481
+ function textRange(node, from, to = from) {
1482
+ const range = scratchRange || (scratchRange = document.createRange());
1483
+ range.setEnd(node, to);
1484
+ range.setStart(node, from);
1485
+ return range;
1486
+ }
1487
+ function flattenRect(rect, left) {
1488
+ const x = left ? rect.left : rect.right;
1489
+ return { left: x, right: x, top: rect.top, bottom: rect.bottom };
1490
+ }
1491
+ function clientRectsFor(dom) {
1492
+ if (dom.nodeType == 3) {
1493
+ return textRange(dom, 0, dom.nodeValue.length).getClientRects();
1494
+ } else if (dom.nodeType == 1) {
1495
+ return dom.getClientRects();
1496
+ } else {
1497
+ return [];
1498
+ }
1499
+ }
1500
+
1501
+ // src/prefix-element/react.tsx
1502
+ var PrefixElementWidget = class extends WidgetType5 {
1503
+ constructor(content) {
1504
+ super();
1505
+ this.content = content;
1506
+ }
1507
+ toDOM(view) {
1508
+ const wrap = document.createElement("span");
1509
+ wrap.className = "cm-prefix-element";
1510
+ wrap.style.cssText = "height: 0;";
1511
+ wrap.appendChild(
1512
+ typeof this.content === "string" ? document.createTextNode(this.content) : typeof this.content === "function" ? this.content(view) : this.content
1513
+ );
1514
+ if (typeof this.content === "string") {
1515
+ wrap.setAttribute("aria-label", `prefix ${this.content}`);
1516
+ } else {
1517
+ wrap.setAttribute("aria-hidden", "true");
1518
+ }
1519
+ return wrap;
1520
+ }
1521
+ coordsAt(dom) {
1522
+ const rects = dom.firstChild ? clientRectsFor(dom.firstChild) : [];
1523
+ if (!rects.length) {
1524
+ return null;
1525
+ }
1526
+ const style = window.getComputedStyle(dom.parentNode);
1527
+ const rect = flattenRect(rects[0], style.direction != "rtl");
1528
+ const lineHeight = parseInt(style.lineHeight);
1529
+ if (rect.bottom - rect.top > lineHeight * 1.5) {
1530
+ return {
1531
+ left: rect.left,
1532
+ right: rect.right,
1533
+ top: rect.top,
1534
+ bottom: rect.top + lineHeight
1535
+ };
1536
+ }
1537
+ return rect;
1538
+ }
1539
+ ignoreEvent() {
1540
+ return false;
1541
+ }
1542
+ };
1543
+ function PrefixElement({ deletable, onDelete, children }) {
1544
+ const element = useHTMLElement6("span");
1545
+ const latestElement = useLatest6(element);
1546
+ const latestDeletable = useLatest6(deletable);
1547
+ const latestOnDelete = useLatest6(onDelete);
1548
+ const injector = useInjector9();
1549
+ useLayoutEffect8(() => {
1550
+ function run(view) {
1551
+ if (view.state.selection.main.empty && view.state.selection.main.head === 0 && latestDeletable.current === true && typeof latestOnDelete.current === "function") {
1552
+ latestOnDelete.current();
1553
+ }
1554
+ return false;
1555
+ }
1556
+ return injector.inject([
1557
+ EditorView8.decorations.of(
1558
+ Decoration4.set([
1559
+ Decoration4.widget({
1560
+ side: -100,
1561
+ block: false,
1562
+ widget: new PrefixElementWidget(() => latestElement.current)
1563
+ }).range(0)
1564
+ ])
1565
+ ),
1566
+ keymap.of([
1567
+ { key: "Backspace", shift: run, run },
1568
+ { key: "Meta-Backspace", shift: run, run }
1569
+ ])
1570
+ ]);
1571
+ }, []);
1572
+ return createPortal7(children, element);
1573
+ }
1574
+
1575
+ // src/value-sync.tsx
1576
+ import { useEffect as useEffect6 } from "react";
1577
+ import { useEditor as useEditor6 } from "@coze-editor/react";
1578
+ function ValueSync({ value }) {
1579
+ const editor = useEditor6();
1580
+ useEffect6(() => {
1581
+ if (!editor) {
1582
+ return;
1583
+ }
1584
+ const { doc } = editor.$view.state;
1585
+ if (typeof value === "string" && value !== doc.toString()) {
1586
+ editor.$view.dispatch({
1587
+ changes: {
1588
+ from: 0,
1589
+ to: doc.length,
1590
+ insert: value
1591
+ }
1592
+ });
1593
+ }
1594
+ }, [editor, value]);
1595
+ return null;
1596
+ }
1468
1597
  export {
1469
1598
  ActiveLinePlaceholder,
1470
1599
  CursorInlayHint,
@@ -1482,8 +1611,10 @@ export {
1482
1611
  Mention,
1483
1612
  Placeholder,
1484
1613
  PositionMirror,
1614
+ PrefixElement,
1485
1615
  RefElement,
1486
1616
  SelectionSide,
1617
+ ValueSync,
1487
1618
  getCurrentMentionReplaceRange
1488
1619
  };
1489
1620
  //# sourceMappingURL=index.js.map