@dxos/react-ui-editor 0.6.2-main.fb91371 → 0.6.2

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.
@@ -1481,37 +1481,53 @@ var commentsState = StateField4.define({
1481
1481
  }
1482
1482
  });
1483
1483
  var styles3 = EditorView7.baseTheme({
1484
+ ".cm-comment, .cm-comment-current": {
1485
+ cursor: "pointer",
1486
+ borderWidth: "1px",
1487
+ borderStyle: "solid",
1488
+ borderRadius: "2px",
1489
+ transition: "background-color 0.1s ease"
1490
+ },
1484
1491
  "&light .cm-comment, &light .cm-comment-current": {
1485
- mixBlendMode: "darken"
1492
+ mixBlendMode: "darken",
1493
+ borderColor: getToken("extend.colors.yellow.100")
1486
1494
  },
1487
1495
  "&dark .cm-comment, &dark .cm-comment-current": {
1488
- mixBlendMode: "plus-lighter"
1496
+ mixBlendMode: "plus-lighter",
1497
+ borderColor: getToken("extend.colors.yellow.900")
1489
1498
  },
1490
1499
  "&light .cm-comment": {
1491
1500
  backgroundColor: getToken("extend.colors.yellow.50")
1492
1501
  },
1502
+ "&light .cm-comment:hover": {
1503
+ backgroundColor: getToken("extend.colors.yellow.100")
1504
+ },
1493
1505
  "&light .cm-comment-current": {
1494
1506
  backgroundColor: getToken("extend.colors.yellow.100")
1495
1507
  },
1508
+ "&light .cm-comment-current:hover": {
1509
+ backgroundColor: getToken("extend.colors.yellow.150")
1510
+ },
1496
1511
  "&dark .cm-comment": {
1497
1512
  color: getToken("extend.colors.yellow.50"),
1498
1513
  backgroundColor: getToken("extend.colors.yellow.900")
1499
1514
  },
1515
+ "&dark .cm-comment:hover": {
1516
+ backgroundColor: getToken("extend.colors.yellow.800")
1517
+ },
1500
1518
  "&dark .cm-comment-current": {
1501
1519
  color: getToken("extend.colors.yellow.100"),
1502
1520
  backgroundColor: getToken("extend.colors.yellow.950")
1521
+ },
1522
+ "&dark .cm-comment-current:hover": {
1523
+ backgroundColor: getToken("extend.colors.yellow.900")
1503
1524
  }
1504
1525
  });
1505
- var commentMark = Decoration4.mark({
1506
- class: "cm-comment",
1507
- attributes: {
1508
- "data-testid": "cm-comment"
1509
- }
1510
- });
1511
- var commentCurrentMark = Decoration4.mark({
1512
- class: "cm-comment-current",
1526
+ var createCommentMark = (id, isCurrent) => Decoration4.mark({
1527
+ class: isCurrent ? "cm-comment-current" : "cm-comment",
1513
1528
  attributes: {
1514
- "data-testid": "cm-comment"
1529
+ "data-testid": "cm-comment",
1530
+ "data-comment-id": id
1515
1531
  }
1516
1532
  });
1517
1533
  var commentsDecorations = EditorView7.decorations.compute([
@@ -1523,7 +1539,7 @@ var commentsDecorations = EditorView7.decorations.compute([
1523
1539
  if (!range) {
1524
1540
  log4.warn("Invalid range:", range, {
1525
1541
  F: __dxlog_file6,
1526
- L: 141,
1542
+ L: 159,
1527
1543
  S: void 0,
1528
1544
  C: (f, a) => f(...a)
1529
1545
  });
@@ -1531,14 +1547,31 @@ var commentsDecorations = EditorView7.decorations.compute([
1531
1547
  } else if (range.from === range.to) {
1532
1548
  return void 0;
1533
1549
  }
1534
- if (comment.comment.id === current) {
1535
- return commentCurrentMark.range(range.from, range.to);
1536
- } else {
1537
- return commentMark.range(range.from, range.to);
1538
- }
1550
+ const mark2 = createCommentMark(comment.comment.id, comment.comment.id === current);
1551
+ return mark2.range(range.from, range.to);
1539
1552
  }).filter(nonNullable);
1540
1553
  return Decoration4.set(decorations);
1541
1554
  });
1555
+ var commentClickedEffect = StateEffect3.define();
1556
+ var handleCommentClick = EditorView7.domEventHandlers({
1557
+ click: (event, view) => {
1558
+ let target = event.target;
1559
+ const editorRoot = view.dom;
1560
+ while (target && target !== editorRoot && !target.hasAttribute("data-comment-id")) {
1561
+ target = target.parentElement;
1562
+ }
1563
+ if (target && target !== editorRoot) {
1564
+ const commentId = target.getAttribute("data-comment-id");
1565
+ if (commentId) {
1566
+ view.dispatch({
1567
+ effects: commentClickedEffect.of(commentId)
1568
+ });
1569
+ return true;
1570
+ }
1571
+ }
1572
+ return false;
1573
+ }
1574
+ });
1542
1575
  var trackPastedComments = (onUpdate) => {
1543
1576
  let tracked = null;
1544
1577
  const handleTrack = (event, view) => {
@@ -1689,6 +1722,7 @@ var comments = (options = {}) => {
1689
1722
  documentId.of(options.id),
1690
1723
  commentsState,
1691
1724
  commentsDecorations,
1725
+ handleCommentClick,
1692
1726
  styles3,
1693
1727
  //
1694
1728
  // Keymap.
@@ -1807,20 +1841,26 @@ var scrollThreadIntoView = (view, id, center = true) => {
1807
1841
  }
1808
1842
  const range = Cursor.getRangeFromCursor(view.state, comment.comment.cursor);
1809
1843
  if (range) {
1810
- view.dispatch({
1811
- selection: {
1812
- anchor: range.from
1813
- },
1814
- effects: [
1815
- //
1816
- EditorView7.scrollIntoView(range.from, center ? {
1817
- y: "center"
1818
- } : void 0),
1819
- setSelection.of({
1820
- current: id
1821
- })
1822
- ]
1823
- });
1844
+ const currentSelection = view.state.selection.main;
1845
+ const currentScrollPosition = view.scrollDOM.scrollTop;
1846
+ const targetScrollPosition = view.coordsAtPos(range.from)?.top;
1847
+ const needsScroll = targetScrollPosition !== void 0 && (targetScrollPosition < currentScrollPosition || targetScrollPosition > currentScrollPosition + view.scrollDOM.clientHeight);
1848
+ const needsSelectionUpdate = currentSelection.from !== range.from || currentSelection.to !== range.from;
1849
+ if (needsScroll || needsSelectionUpdate) {
1850
+ view.dispatch({
1851
+ selection: needsSelectionUpdate ? {
1852
+ anchor: range.from
1853
+ } : void 0,
1854
+ effects: [
1855
+ needsScroll ? EditorView7.scrollIntoView(range.from, center ? {
1856
+ y: "center"
1857
+ } : void 0) : [],
1858
+ needsSelectionUpdate ? setSelection.of({
1859
+ current: id
1860
+ }) : []
1861
+ ].flat()
1862
+ });
1863
+ }
1824
1864
  }
1825
1865
  };
1826
1866
  var selectionOverlapsComment = (state2) => {
@@ -1833,6 +1873,9 @@ var selectionOverlapsComment = (state2) => {
1833
1873
  }
1834
1874
  return false;
1835
1875
  };
1876
+ var hasActiveSelection = (state2) => {
1877
+ return state2.selection.ranges.some((range) => !range.empty);
1878
+ };
1836
1879
  var useComments = (view, id, comments2) => {
1837
1880
  useEffect(() => {
1838
1881
  if (view) {
@@ -1852,17 +1895,37 @@ var useComments = (view, id, comments2) => {
1852
1895
  ]);
1853
1896
  };
1854
1897
  var useCommentState = () => {
1855
- const [comment, setComment] = useState(false);
1898
+ const [state2, setState] = useState({
1899
+ comment: false,
1900
+ selection: false
1901
+ });
1856
1902
  const observer = useMemo(() => EditorView7.updateListener.of((update2) => {
1857
1903
  if (update2.docChanged || update2.selectionSet) {
1858
- setComment(() => selectionOverlapsComment(update2.state));
1904
+ setState({
1905
+ comment: selectionOverlapsComment(update2.state),
1906
+ selection: hasActiveSelection(update2.state)
1907
+ });
1859
1908
  }
1860
1909
  }), []);
1861
1910
  return [
1862
- comment,
1911
+ state2,
1863
1912
  observer
1864
1913
  ];
1865
1914
  };
1915
+ var useCommentClickListener = (onCommentClick) => {
1916
+ const observer = useMemo(() => EditorView7.updateListener.of((update2) => {
1917
+ update2.transactions.forEach((transaction) => {
1918
+ transaction.effects.forEach((effect) => {
1919
+ if (effect.is(commentClickedEffect)) {
1920
+ onCommentClick(effect.value);
1921
+ }
1922
+ });
1923
+ });
1924
+ }), [
1925
+ onCommentClick
1926
+ ]);
1927
+ return observer;
1928
+ };
1866
1929
 
1867
1930
  // packages/ui/react-ui-editor/src/extensions/doc.ts
1868
1931
  import { Facet as Facet5 } from "@codemirror/state";
@@ -4960,7 +5023,7 @@ var MarkdownActions = () => {
4960
5023
  onClick: () => onAction?.({
4961
5024
  type: "comment"
4962
5025
  }),
4963
- disabled: !state2 || state2.comment
5026
+ disabled: !state2 || state2.comment || !state2.selection
4964
5027
  }, t("comment label")));
4965
5028
  };
4966
5029
  var Toolbar = {
@@ -5184,6 +5247,7 @@ export {
5184
5247
  translations_default as translations,
5185
5248
  typewriter,
5186
5249
  useActionHandler,
5250
+ useCommentClickListener,
5187
5251
  useCommentState,
5188
5252
  useComments,
5189
5253
  useFormattingState,