@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/index.js CHANGED
@@ -45,8 +45,10 @@ __export(index_exports, {
45
45
  Mention: () => Mention,
46
46
  Placeholder: () => Placeholder,
47
47
  PositionMirror: () => PositionMirror,
48
+ PrefixElement: () => PrefixElement,
48
49
  RefElement: () => RefElement,
49
50
  SelectionSide: () => import_extensions.SelectionSide,
51
+ ValueSync: () => ValueSync,
50
52
  getCurrentMentionReplaceRange: () => getCurrentMentionReplaceRange
51
53
  });
52
54
  module.exports = __toCommonJS(index_exports);
@@ -1174,6 +1176,9 @@ function mention(options) {
1174
1176
  },
1175
1177
  update(value, tr) {
1176
1178
  const options2 = tr.startState.facet(mentionConfig);
1179
+ if (!options2) {
1180
+ return value;
1181
+ }
1177
1182
  const { search = true, onOpenChange, onSearch, onTrigger } = options2;
1178
1183
  let { show } = value;
1179
1184
  let triggerContext = void 0;
@@ -1463,6 +1468,132 @@ var EmbededLineView = (0, import_react21.forwardRef)(function EmbededLineView2({
1463
1468
  }, []);
1464
1469
  return (0, import_react_dom6.createPortal)(children, dom);
1465
1470
  });
1471
+
1472
+ // src/prefix-element/react.tsx
1473
+ var import_react_dom7 = require("react-dom");
1474
+ var import_react23 = require("react");
1475
+ var import_react_hooks10 = require("@coze-editor/react-hooks");
1476
+ var import_react24 = require("@coze-editor/react");
1477
+ var import_view10 = require("@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 import_view10.WidgetType {
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 = (0, import_react_hooks10.useHTMLElement)("span");
1545
+ const latestElement = (0, import_react_hooks10.useLatest)(element);
1546
+ const latestDeletable = (0, import_react_hooks10.useLatest)(deletable);
1547
+ const latestOnDelete = (0, import_react_hooks10.useLatest)(onDelete);
1548
+ const injector = (0, import_react24.useInjector)();
1549
+ (0, import_react23.useLayoutEffect)(() => {
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
+ import_view10.EditorView.decorations.of(
1558
+ import_view10.Decoration.set([
1559
+ import_view10.Decoration.widget({
1560
+ side: -100,
1561
+ block: false,
1562
+ widget: new PrefixElementWidget(() => latestElement.current)
1563
+ }).range(0)
1564
+ ])
1565
+ ),
1566
+ import_view10.keymap.of([
1567
+ { key: "Backspace", shift: run, run },
1568
+ { key: "Meta-Backspace", shift: run, run }
1569
+ ])
1570
+ ]);
1571
+ }, []);
1572
+ return (0, import_react_dom7.createPortal)(children, element);
1573
+ }
1574
+
1575
+ // src/value-sync.tsx
1576
+ var import_react26 = require("react");
1577
+ var import_react27 = require("@coze-editor/react");
1578
+ function ValueSync({ value }) {
1579
+ const editor = (0, import_react27.useEditor)();
1580
+ (0, import_react26.useEffect)(() => {
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
+ }
1466
1597
  // Annotate the CommonJS export names for ESM import in node:
1467
1598
  0 && (module.exports = {
1468
1599
  ActiveLinePlaceholder,
@@ -1481,8 +1612,10 @@ var EmbededLineView = (0, import_react21.forwardRef)(function EmbededLineView2({
1481
1612
  Mention,
1482
1613
  Placeholder,
1483
1614
  PositionMirror,
1615
+ PrefixElement,
1484
1616
  RefElement,
1485
1617
  SelectionSide,
1618
+ ValueSync,
1486
1619
  getCurrentMentionReplaceRange
1487
1620
  });
1488
1621
  //# sourceMappingURL=index.js.map