@dxos/react-ui-editor 0.3.11-main.dafb7f2 → 0.3.11-main.df1e30a

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.
Files changed (166) hide show
  1. package/dist/lib/browser/index.mjs +1192 -1300
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +16 -13
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -17
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -6
  9. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/comments.stories.d.ts +26 -0
  11. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -0
  12. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  13. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  14. package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
  15. package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
  16. package/dist/types/src/extensions/automerge/defs.d.ts +7 -9
  17. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  18. package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -12
  19. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  20. package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
  21. package/dist/types/src/extensions/awareness.d.ts +1 -1
  22. package/dist/types/src/extensions/awareness.d.ts.map +1 -1
  23. package/dist/types/src/extensions/basic.d.ts +4 -5
  24. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  25. package/dist/types/src/extensions/comments.d.ts +32 -19
  26. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  27. package/dist/types/src/{util → extensions}/cursor.d.ts +10 -2
  28. package/dist/types/src/extensions/cursor.d.ts.map +1 -0
  29. package/dist/types/src/extensions/index.d.ts +2 -8
  30. package/dist/types/src/extensions/index.d.ts.map +1 -1
  31. package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
  32. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  33. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  34. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  35. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  36. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  37. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  38. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  39. package/dist/types/src/extensions/markdown/index.d.ts +7 -0
  40. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  41. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  42. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  43. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  44. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  45. package/dist/types/src/extensions/outliner.d.ts +4 -0
  46. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  47. package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
  48. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
  49. package/dist/types/src/hooks/defs.d.ts +25 -0
  50. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  51. package/dist/types/src/hooks/index.d.ts +2 -2
  52. package/dist/types/src/hooks/index.d.ts.map +1 -1
  53. package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
  54. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  55. package/dist/types/src/hooks/useTextModel.d.ts +2 -32
  56. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  57. package/dist/types/src/index.d.ts +3 -0
  58. package/dist/types/src/index.d.ts.map +1 -1
  59. package/dist/types/src/styles/markdown.d.ts +2 -2
  60. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  61. package/dist/types/src/styles/tokens.d.ts +1 -0
  62. package/dist/types/src/styles/tokens.d.ts.map +1 -1
  63. package/dist/types/src/themes/default.d.ts.map +1 -1
  64. package/dist/types/src/util.d.ts +7 -0
  65. package/dist/types/src/util.d.ts.map +1 -0
  66. package/package.json +34 -36
  67. package/src/components/TextEditor/MarkdownEditor.stories.tsx +82 -83
  68. package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
  69. package/src/components/TextEditor/TextEditor.tsx +122 -93
  70. package/src/components/TextEditor/comments.stories.tsx +357 -0
  71. package/src/extensions/autocomplete.ts +23 -19
  72. package/src/extensions/automerge/automerge.ts +40 -46
  73. package/src/extensions/automerge/cursor.ts +2 -1
  74. package/src/extensions/automerge/defs.ts +10 -31
  75. package/src/extensions/automerge/semaphore.ts +23 -20
  76. package/src/extensions/automerge/update-automerge.ts +2 -2
  77. package/src/extensions/awareness.ts +40 -46
  78. package/src/extensions/basic.ts +22 -17
  79. package/src/extensions/comments.ts +249 -242
  80. package/src/extensions/cursor.ts +50 -0
  81. package/src/extensions/index.ts +2 -8
  82. package/src/extensions/markdown/bundle.ts +20 -30
  83. package/src/extensions/markdown/code.ts +127 -0
  84. package/src/extensions/markdown/heading.ts +59 -0
  85. package/src/extensions/markdown/highlight.ts +11 -26
  86. package/src/extensions/markdown/hr.ts +71 -0
  87. package/src/extensions/{image.ts → markdown/image.ts} +2 -2
  88. package/src/extensions/markdown/index.ts +7 -0
  89. package/src/extensions/{link.ts → markdown/link.ts} +9 -10
  90. package/src/extensions/{table.ts → markdown/table.ts} +6 -3
  91. package/src/extensions/markdown/tasklist.ts +110 -0
  92. package/src/extensions/mention.ts +9 -2
  93. package/src/extensions/outliner.ts +12 -0
  94. package/src/hooks/awareness-provider.ts +24 -20
  95. package/src/hooks/defs.ts +42 -0
  96. package/src/hooks/index.ts +3 -2
  97. package/src/hooks/useTextEditor.ts +29 -0
  98. package/src/hooks/useTextModel.ts +48 -60
  99. package/src/index.ts +3 -0
  100. package/src/styles/markdown.ts +9 -12
  101. package/src/styles/tokens.ts +3 -0
  102. package/src/themes/default.ts +15 -50
  103. package/src/util.ts +41 -0
  104. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  105. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  106. package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
  107. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  108. package/dist/types/src/extensions/code.d.ts.map +0 -1
  109. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  110. package/dist/types/src/extensions/image.d.ts.map +0 -1
  111. package/dist/types/src/extensions/link.d.ts.map +0 -1
  112. package/dist/types/src/extensions/mermaid.d.ts +0 -3
  113. package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
  114. package/dist/types/src/extensions/table.d.ts.map +0 -1
  115. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  116. package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
  117. package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
  118. package/dist/types/src/extensions/yjs/index.d.ts +0 -2
  119. package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
  120. package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
  121. package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
  122. package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
  123. package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
  124. package/dist/types/src/hooks/automerge.d.ts +0 -3
  125. package/dist/types/src/hooks/automerge.d.ts.map +0 -1
  126. package/dist/types/src/hooks/yjs.d.ts +0 -27
  127. package/dist/types/src/hooks/yjs.d.ts.map +0 -1
  128. package/dist/types/src/testing/index.d.ts +0 -3
  129. package/dist/types/src/testing/index.d.ts.map +0 -1
  130. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  131. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  132. package/dist/types/src/testing/proto/index.d.ts +0 -2
  133. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  134. package/dist/types/src/testing/replicator.d.ts +0 -45
  135. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  136. package/dist/types/src/util/cursor.d.ts.map +0 -1
  137. package/dist/types/src/util/index.d.ts +0 -3
  138. package/dist/types/src/util/index.d.ts.map +0 -1
  139. package/dist/types/src/util/util.d.ts +0 -5
  140. package/dist/types/src/util/util.d.ts.map +0 -1
  141. package/src/components/TextEditor/codemirror.stories.ts +0 -115
  142. package/src/components/TextEditor/yjs.stories.tsx +0 -56
  143. package/src/extensions/code.ts +0 -99
  144. package/src/extensions/hr.ts +0 -74
  145. package/src/extensions/mermaid.ts +0 -11
  146. package/src/extensions/tasklist.ts +0 -119
  147. package/src/extensions/yjs/cursor.ts +0 -21
  148. package/src/extensions/yjs/index.ts +0 -5
  149. package/src/extensions/yjs/yjs.test.ts +0 -35
  150. package/src/extensions/yjs/yjs.ts +0 -16
  151. package/src/hooks/automerge.ts +0 -52
  152. package/src/hooks/yjs.ts +0 -178
  153. package/src/testing/index.ts +0 -6
  154. package/src/testing/proto/gen/schema.ts +0 -49
  155. package/src/testing/proto/index.ts +0 -5
  156. package/src/testing/proto/schema.proto +0 -15
  157. package/src/testing/replicator.ts +0 -184
  158. package/src/util/cursor.ts +0 -29
  159. package/src/util/index.ts +0 -6
  160. package/src/util/util.ts +0 -18
  161. /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  162. /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  163. /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  164. /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  165. /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  166. /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
@@ -2,122 +2,104 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Extension, StateEffect, StateField } from '@codemirror/state';
6
- import {
7
- hoverTooltip,
8
- keymap,
9
- type Command,
10
- type DecorationSet,
11
- type ViewUpdate,
12
- Decoration,
13
- EditorView,
14
- MatchDecorator,
15
- ViewPlugin,
16
- WidgetType,
17
- type Rect,
18
- } from '@codemirror/view';
19
- import get from 'lodash.get';
5
+ import { type Extension, StateEffect, StateField, type Text } from '@codemirror/state';
6
+ import { hoverTooltip, keymap, type Command, Decoration, EditorView, type Rect } from '@codemirror/view';
20
7
  import sortBy from 'lodash.sortby';
8
+ import { useEffect } from 'react';
21
9
 
22
10
  import { debounce } from '@dxos/async';
23
11
  import { invariant } from '@dxos/invariant';
24
- import { log } from '@dxos/log';
25
12
  import { nonNullable } from '@dxos/util';
26
13
 
27
- import { type CommentRange, type EditorModel, type Range } from '../hooks';
28
- import { tokens } from '../styles';
29
- import { callbackWrapper, CursorConverter } from '../util';
30
-
31
- // TODO(burdon): Handle delete, cut, copy, and paste (separately) text that includes comment range.
32
- // TODO(burdon): Consider breaking into separate plugin (since not standalone)? Like mermaid?
14
+ import { Cursor } from './cursor';
15
+ import { type Comment, type Range } from '../hooks';
16
+ import { getToken } from '../styles';
17
+ import { callbackWrapper } from '../util';
33
18
 
34
19
  // TODO(burdon): Reconcile with theme.
35
20
  const styles = EditorView.baseTheme({
36
- '& .cm-bookmark': {
37
- cursor: 'pointer',
38
- margin: '4px',
39
- padding: '4px',
40
- backgroundColor: 'yellow',
41
- },
42
- '& .cm-bookmark-selected': {
43
- backgroundColor: 'orange',
44
- },
45
21
  '& .cm-comment': {
46
- backgroundColor: get(tokens, 'extend.colors.yellow.50'),
22
+ backgroundColor: getToken('extend.colors.yellow.50'),
47
23
  },
48
- '& .cm-comment-active': {
49
- backgroundColor: get(tokens, 'extend.colors.yellow.100'),
24
+ '& .cm-comment-current': {
25
+ backgroundColor: getToken('extend.colors.yellow.100'),
50
26
  },
51
27
  });
52
28
 
53
29
  const marks = {
54
30
  highlight: Decoration.mark({ class: 'cm-comment' }),
55
- highlightActive: Decoration.mark({ class: 'cm-comment-active' }),
31
+ highlightActive: Decoration.mark({ class: 'cm-comment-current' }),
56
32
  };
57
33
 
58
- // TODO(burdon): Should this be part of state?
59
- type CommentSelected = {
60
- active?: string;
61
- closest?: string;
34
+ type CommentState = {
35
+ comment: Comment;
36
+ range: Range;
37
+ location?: Rect | null;
62
38
  };
63
39
 
64
- // TODO(burdon): Rename.
65
- type ExtendedCommentRange = Range &
66
- CommentRange & {
67
- // TODO(burdon): Not part of state; just required for callback.
68
- location?: Rect | null;
69
- };
40
+ type SelectionState = {
41
+ current?: string;
42
+ closest?: string;
43
+ };
70
44
 
71
- export type CommentsState = CommentSelected & {
72
- ranges: ExtendedCommentRange[];
45
+ export type CommentsState = {
46
+ comments: CommentState[];
47
+ selection: SelectionState;
73
48
  };
74
49
 
75
- export const setFocus = (view: EditorView, thread: string) => {
76
- const range = view.state.field(commentsStateField).ranges.find((range) => range.id === thread);
77
- if (!range) {
50
+ export const setFocus = (view: EditorView, id: string, center = true) => {
51
+ const comment = view.state.field(commentsState).comments.find((range) => range.comment.id === id);
52
+ if (!comment?.comment.cursor) {
78
53
  return;
79
54
  }
80
55
 
81
- view.dispatch({
82
- effects: setSelection.of({ active: thread }),
83
- selection: { anchor: range.from },
84
- scrollIntoView: true,
85
- });
56
+ const range = Cursor.getRangeFromCursor(view.state, comment.comment.cursor);
57
+ if (range) {
58
+ view.dispatch({
59
+ selection: { anchor: range.from },
60
+ effects: [
61
+ //
62
+ EditorView.scrollIntoView(range.from, center ? { y: 'center' } : undefined),
63
+ setSelection.of({ current: id }),
64
+ ],
65
+ });
66
+ }
86
67
  };
87
68
 
88
- export const setCommentRange = StateEffect.define<{ model: EditorModel; comments: CommentRange[] }>();
69
+ export const setComments = StateEffect.define<Comment[]>();
89
70
 
90
- export const setSelection = StateEffect.define<CommentSelected>();
71
+ export const setSelection = StateEffect.define<SelectionState>();
91
72
 
92
73
  const setCommentState = StateEffect.define<CommentsState>();
93
74
 
94
75
  /**
95
76
  * State field (reducer) that tracks comment ranges.
96
- * The ranges are tracked as codemirror ranges (i.e., not relative YJS/Automerge positions), and
97
- * therefore must be updated when the document changes.
77
+ * The ranges are tracked as Automerge cursors from which the absolute indexed ranges can be computed.
98
78
  */
99
- const commentsStateField = StateField.define<CommentsState>({
100
- create: () => ({ ranges: [] }),
79
+ const commentsState = StateField.define<CommentsState>({
80
+ create: () => ({ comments: [], selection: {} }),
101
81
  update: (value, tr) => {
102
- const cursorConverter = tr.state.facet(CursorConverter);
103
-
104
82
  for (const effect of tr.effects) {
105
83
  // Update selection.
106
84
  if (effect.is(setSelection)) {
107
- return { ...value, ...effect.value };
85
+ return { ...value, selection: effect.value };
108
86
  }
109
87
 
110
88
  // Update range from store.
111
- if (effect.is(setCommentRange)) {
112
- const { comments } = effect.value;
113
- const ranges: ExtendedCommentRange[] = comments
89
+ if (effect.is(setComments)) {
90
+ const comments: CommentState[] = effect.value
114
91
  .map((comment) => {
115
- const range = getRangeFromCursor(cursorConverter, comment.cursor);
116
- return range && { ...comment, ...range };
92
+ // Skip cut/deleted comments.
93
+ if (!comment.cursor) {
94
+ return undefined;
95
+ }
96
+
97
+ const range = Cursor.getRangeFromCursor(tr.state, comment.cursor);
98
+ return range && { comment, range };
117
99
  })
118
100
  .filter(nonNullable);
119
101
 
120
- return { ...value, ranges };
102
+ return { ...value, comments };
121
103
  }
122
104
 
123
105
  // Update entire state.
@@ -133,64 +115,48 @@ const commentsStateField = StateField.define<CommentsState>({
133
115
  /**
134
116
  * Decorate ranges.
135
117
  */
136
- const highlightDecorations = EditorView.decorations.compute([commentsStateField], (state) => {
137
- const { active, ranges } = state.field(commentsStateField);
138
- const decorations =
139
- sortBy(ranges ?? [], (range) => range.from)
140
- ?.flatMap((selection) => {
141
- // TODO(burdon): Invalid range (e.g., deleted).
142
- if (selection.from === selection.to) {
143
- return undefined;
144
- }
118
+ const highlightDecorations = EditorView.decorations.compute([commentsState], (state) => {
119
+ const {
120
+ selection: { current },
121
+ comments,
122
+ } = state.field(commentsState);
123
+
124
+ const decorations = sortBy(comments ?? [], (range) => range.range.from)
125
+ ?.flatMap((comment) => {
126
+ const range = comment.range;
127
+ if (!range || range.from === range.to) {
128
+ console.warn('Invalid range:', range);
129
+ return undefined;
130
+ }
145
131
 
146
- const range = { from: selection.from, to: selection.to };
147
- if (selection.id === active) {
148
- return marks.highlightActive.range(range.from, range.to);
149
- } else {
150
- return marks.highlight.range(range.from, range.to);
151
- }
152
- })
153
- .filter(nonNullable) ?? [];
132
+ if (comment.comment.id === current) {
133
+ return marks.highlightActive.range(range.from, range.to);
134
+ } else {
135
+ return marks.highlight.range(range.from, range.to);
136
+ }
137
+ })
138
+ .filter(nonNullable);
154
139
 
155
140
  return Decoration.set(decorations);
156
141
  });
157
142
 
158
- /**
159
- * Optional bookmark widget at the start/end of the selection.
160
- * May correspond to a markdown bookmark.
161
- */
162
- class BookmarkWidget extends WidgetType {
163
- constructor(
164
- private readonly _anchor: number,
165
- private readonly _id: string,
166
- private readonly _handleClick?: () => void,
167
- ) {
168
- super();
169
- invariant(this._id);
170
- }
171
-
172
- override eq(other: BookmarkWidget) {
173
- return this._anchor === other._anchor && this._id === other._id;
174
- }
175
-
176
- override toDOM() {
177
- const span = document.createElement('span');
178
- span.className = 'cm-bookmark';
179
- span.textContent = '※';
180
- if (this._handleClick) {
181
- span.onclick = () => this._handleClick!();
182
- }
183
- return span;
184
- }
185
- }
186
-
187
143
  export type CommentsOptions = {
144
+ /**
145
+ * Key shortcut to create a new thread.
146
+ */
188
147
  key?: string;
189
- footnote?: boolean;
190
148
  /**
191
149
  * Called to create a new thread and return the thread id.
192
150
  */
193
- onCreate?: (range: string) => string | undefined;
151
+ onCreate?: (cursor: string, location?: Rect | null) => string | undefined;
152
+ /**
153
+ * Selection cut/deleted.
154
+ */
155
+ onDelete?: (thread: string) => void;
156
+ /**
157
+ * Called when a comment is moved.
158
+ */
159
+ onUpdate?: (thread: string, cursor: string) => void;
194
160
  /**
195
161
  * Called to notify which thread is currently closest to the cursor.
196
162
  */
@@ -198,7 +164,76 @@ export type CommentsOptions = {
198
164
  /**
199
165
  * Called to render tooltip.
200
166
  */
201
- onHover?: (el: Element) => void;
167
+ onHover?: (el: Element, shortcut: string) => void;
168
+ };
169
+
170
+ // TODO(burdon): Handle cut/restore via undo (need to integrate with history?)
171
+ const trackPastedComments = (onUpdate: NonNullable<CommentsOptions['onUpdate']>) => {
172
+ // Tracks indexed selections within text.
173
+ // TODO(burdon): Move to main state field?
174
+ let tracked: { text: Text; comments: { id: string; from: number; to: number }[] } | null = null;
175
+
176
+ // Track cut or copy (enables cut-and-paste and copy-delete-paste to restore comment selection).
177
+ const handleTrack = (event: Event, view: EditorView) => {
178
+ const comments = view.state.field(commentsState);
179
+ const { main } = view.state.selection;
180
+ const selectedRanges = comments.comments.filter(
181
+ ({ range }) => range.from >= main.from && range.to <= main.to && range.from < range.to,
182
+ );
183
+
184
+ if (!selectedRanges.length) {
185
+ tracked = null;
186
+ } else {
187
+ tracked = {
188
+ text: view.state.doc.slice(main.from, main.to),
189
+ comments: selectedRanges.map(({ comment, range }) => ({
190
+ id: comment.id,
191
+ from: range.from - main.from,
192
+ to: range.to - main.from,
193
+ })),
194
+ };
195
+ }
196
+ };
197
+
198
+ return [
199
+ EditorView.domEventHandlers({
200
+ cut: handleTrack,
201
+ copy: handleTrack,
202
+ }),
203
+
204
+ // Handle paste.
205
+ EditorView.updateListener.of(({ view, state, transactions }) => {
206
+ if (tracked) {
207
+ const paste = transactions.find((tr) => tr.isUserEvent('input.paste'));
208
+ if (paste) {
209
+ let found = -1;
210
+ paste.changes.iterChanges((fromA, toA, fromB, toB, text) => {
211
+ if (text.eq(tracked!.text)) {
212
+ for (let i = transactions.indexOf(paste!) + 1; i < transactions.length; i++) {
213
+ fromB = transactions[i].changes.mapPos(fromB);
214
+ }
215
+
216
+ found = fromB;
217
+ }
218
+ });
219
+
220
+ if (found > -1) {
221
+ for (const moved of tracked.comments) {
222
+ const range = {
223
+ from: found + moved.from,
224
+ to: found + moved.to,
225
+ };
226
+
227
+ const cursor = Cursor.getCursorFromRange(state, range);
228
+ onUpdate(moved.id, cursor);
229
+ }
230
+ }
231
+
232
+ tracked = null;
233
+ }
234
+ }
235
+ }),
236
+ ];
202
237
  };
203
238
 
204
239
  /**
@@ -210,44 +245,44 @@ export type CommentsOptions = {
210
245
  * 4). Tracks the current cursor position to:
211
246
  * a). Update the decoration to show if the cursor is within a current selection.
212
247
  * b). Calls a handler to indicate which is the closest selection (e.g., to update the thread sidebar).
213
- * 5). Optionally, inserts a markdown footnote when creating a comment thread.
214
- * 6). Optionally, implements a hoverTooltip to show hints when creating a selection range.
248
+ * 5). Optionally, implements a hoverTooltip to show hints when creating a selection range.
215
249
  */
216
250
  export const comments = (options: CommentsOptions = {}): Extension => {
251
+ const { key: shortcut = "meta-'" } = options;
252
+
217
253
  const handleSelect = debounce((state: CommentsState) => options.onSelect?.(state), 200);
218
254
 
219
255
  /**
220
256
  * Create comment thread action.
221
257
  */
222
258
  const createCommentThread: Command = (view) => {
223
- const cursorConverter = view.state.facet(CursorConverter);
224
-
225
259
  invariant(options.onCreate);
226
- const { head, from, to } = view.state.selection.main;
260
+ const { from, to } = view.state.selection.main;
227
261
  if (from === to) {
228
262
  return false;
229
263
  }
230
264
 
231
- const relPos = getCursorFromRange(cursorConverter, { from, to });
232
- if (relPos) {
265
+ // Don't allow selection at end of document.
266
+ if (to === view.state.doc.length) {
267
+ view.dispatch({
268
+ changes: {
269
+ from: to,
270
+ insert: '\n',
271
+ },
272
+ });
273
+ }
274
+
275
+ const cursor = Cursor.getCursorFromRange(view.state, { from, to });
276
+ if (cursor) {
233
277
  // Create thread via callback.
234
- const id = callbackWrapper(options.onCreate)(relPos);
278
+ const id = options.onCreate?.(cursor, view.coordsAtPos(from));
235
279
  if (id) {
236
280
  // Update range.
237
281
  view.dispatch({
238
- effects: setSelection.of({ active: id }),
282
+ effects: setSelection.of({ current: id }),
239
283
  selection: { anchor: from },
240
284
  });
241
285
 
242
- // Insert footnote.
243
- if (options.footnote) {
244
- const tag = `[^${id}]`;
245
- view.dispatch({
246
- changes: { from: head, insert: tag },
247
- selection: { anchor: head + tag.length },
248
- });
249
- }
250
-
251
286
  return true;
252
287
  }
253
288
  }
@@ -255,45 +290,8 @@ export const comments = (options: CommentsOptions = {}): Extension => {
255
290
  return false;
256
291
  };
257
292
 
258
- /**
259
- * Bookmark widget decoration.
260
- */
261
- const bookmarksViewPlugin = ViewPlugin.fromClass(
262
- class BookmarkViewPlugin {
263
- // Match markdown footnotes (option).
264
- // https://www.markdownguide.org/extended-syntax/#footnotes
265
- static bookmarkMatcher = new MatchDecorator({
266
- regexp: /\[\^(\w+)\]/g,
267
- decoration: (match, view, pos) => {
268
- const id = match[1];
269
- return Decoration.replace({
270
- id,
271
- widget: new BookmarkWidget(pos, id),
272
- });
273
- },
274
- });
275
-
276
- bookmarks: DecorationSet;
277
- constructor(view: EditorView) {
278
- this.bookmarks = BookmarkViewPlugin.bookmarkMatcher.createDeco(view);
279
- }
280
-
281
- update(update: ViewUpdate) {
282
- this.bookmarks = BookmarkViewPlugin.bookmarkMatcher.updateDeco(update, this.bookmarks);
283
- }
284
- },
285
- {
286
- decorations: (instance) => instance.bookmarks,
287
- provide: (plugin) =>
288
- EditorView.atomicRanges.of((view) => {
289
- return view.plugin(plugin)?.bookmarks || Decoration.none;
290
- }),
291
- },
292
- );
293
-
294
293
  return [
295
- bookmarksViewPlugin,
296
- commentsStateField,
294
+ commentsState,
297
295
  highlightDecorations,
298
296
  styles,
299
297
 
@@ -303,7 +301,7 @@ export const comments = (options: CommentsOptions = {}): Extension => {
303
301
  options.onCreate
304
302
  ? keymap.of([
305
303
  {
306
- key: options?.key ?? "meta-'",
304
+ key: shortcut,
307
305
  run: callbackWrapper(createCommentThread),
308
306
  },
309
307
  ])
@@ -323,9 +321,8 @@ export const comments = (options: CommentsOptions = {}): Extension => {
323
321
  end: selection.to,
324
322
  above: true,
325
323
  create: () => {
326
- // TODO(burdon): Dispatch to react callback to render (or use SSR)?
327
324
  const el = document.createElement('div');
328
- options.onHover?.(el);
325
+ options.onHover!(el, shortcut);
329
326
  return { dom: el, offset: { x: 0, y: 8 } };
330
327
  },
331
328
  };
@@ -333,89 +330,99 @@ export const comments = (options: CommentsOptions = {}): Extension => {
333
330
 
334
331
  return null;
335
332
  },
336
- { hideOnChange: true, hoverTime: 1000 },
333
+ {
334
+ // TODO(burdon): Hide on change triggered immediately?
335
+ // hideOnChange: true,
336
+ hoverTime: 1_000,
337
+ },
337
338
  )
338
339
  : [],
339
340
 
340
341
  //
341
- // Monitor cursor movement and text updates.
342
- // TODO(burdon): Is there a better (finer grained) way to do this?
342
+ // Track deleted ranges and update ranges for decorations.
343
343
  //
344
344
  EditorView.updateListener.of(({ view, state, changes }) => {
345
- const cursorConverter = view.state.facet(CursorConverter);
346
-
347
- //
348
- // Test if need to recompute indexed range if document changes before the end of the range.
349
- //
350
- {
351
- let mod = false;
352
- const { active, ranges } = state.field(commentsStateField);
353
- changes.iterChanges((from, to, from2, to2) => {
354
- ranges.forEach((range) => {
355
- if (from2 === to2) {
356
- const newRange = getRangeFromCursor(cursorConverter, range.cursor);
357
- if (!newRange || newRange.to - newRange.from === 0) {
358
- // TODO(burdon): Delete range if empty.
359
- log.info('deleted comment', { thread: range.id });
360
- }
345
+ let mod = false;
346
+ const { comments, ...value } = state.field(commentsState);
347
+ changes.iterChanges((from, to, from2, to2) => {
348
+ comments.forEach(({ comment, range }) => {
349
+ // Test if range deleted.
350
+ if (from2 === to2) {
351
+ const newRange = Cursor.getRangeFromCursor(view.state, comment.cursor!);
352
+ if (!newRange || newRange.to - newRange.from === 0) {
353
+ options.onDelete?.(comment.id);
361
354
  }
355
+ }
362
356
 
363
- if (from <= range.to) {
364
- const newRange = getRangeFromCursor(cursorConverter, range.cursor);
365
- Object.assign(range, newRange);
366
- mod = true;
367
- }
368
- });
357
+ // Update range.
358
+ if (from <= range.to) {
359
+ const newRange = Cursor.getRangeFromCursor(view.state, comment.cursor!);
360
+ Object.assign(range, newRange);
361
+ mod = true;
362
+ }
369
363
  });
364
+ });
370
365
 
371
- if (mod) {
372
- view.dispatch({ effects: setCommentState.of({ active, ranges }) });
373
- }
366
+ if (mod) {
367
+ view.dispatch({ effects: setCommentState.of({ comments, ...value }) });
374
368
  }
369
+ }),
375
370
 
376
- //
377
- // Track the current selection.
378
- //
379
- {
380
- const { head } = state.selection.main;
381
-
382
- let min = Infinity;
383
- const { active, closest, ranges } = state.field(commentsStateField);
384
- const selected: CommentSelected = { active: undefined, closest: undefined };
385
- ranges.forEach((comment) => {
386
- const d = Math.min(Math.abs(head - comment.from), Math.abs(head - comment.to));
387
- if (head >= comment.from && head <= comment.to) {
388
- selected.active = comment.id;
389
- }
371
+ //
372
+ // Track selection/proximity.
373
+ //
374
+ EditorView.updateListener.of(({ view, state }) => {
375
+ let min = Infinity;
376
+ const {
377
+ selection: { current, closest },
378
+ comments,
379
+ } = state.field(commentsState);
380
+
381
+ const { head } = state.selection.main;
382
+ const selection: SelectionState = {};
383
+ comments.forEach(({ comment, range }) => {
384
+ if (head >= range.from && head <= range.to) {
385
+ selection.current = comment.id;
386
+ selection.closest = undefined;
387
+ }
388
+
389
+ if (!selection.current) {
390
+ const d = Math.min(Math.abs(head - range.from), Math.abs(head - range.to));
390
391
  if (d < min) {
391
- selected.closest = comment.id;
392
+ selection.closest = comment.id;
392
393
  min = d;
393
394
  }
394
- });
395
-
396
- if (selected.active !== active || selected.closest !== closest) {
397
- view.dispatch({ effects: setSelection.of(selected) });
398
-
399
- // Update callback.
400
- handleSelect({
401
- ...selected,
402
- ranges: ranges.map((range) => ({ ...range, location: view.coordsAtPos(range.from) })),
403
- });
404
395
  }
396
+ });
397
+
398
+ if (selection.current !== current || selection.closest !== closest) {
399
+ view.dispatch({ effects: setSelection.of(selection) });
400
+
401
+ // Update callback.
402
+ handleSelect({
403
+ selection,
404
+ comments: comments.map(({ comment, range }) => ({
405
+ comment,
406
+ range,
407
+ location: view.coordsAtPos(range.from),
408
+ })),
409
+ });
405
410
  }
406
411
  }),
407
- ];
408
- };
409
412
 
410
- const getCursorFromRange = (cursorConverter: CursorConverter, range: Range) => {
411
- const from = cursorConverter.toCursor(range.from);
412
- const to = cursorConverter.toCursor(range.to, -1);
413
- return [from, to].join(':');
413
+ options.onUpdate ? trackPastedComments(options.onUpdate) : [],
414
+ ];
414
415
  };
415
416
 
416
- const getRangeFromCursor = (cursorConverter: CursorConverter, cursor: string) => {
417
- const parts = cursor.split(':');
418
- const from = cursorConverter.fromCursor(parts[0]);
419
- const to = cursorConverter.fromCursor(parts[1]);
420
- return from && to ? { from, to } : undefined;
417
+ /**
418
+ * Update comments state field.
419
+ */
420
+ export const useComments = (view: EditorView | null, comments: Comment[] = []) => {
421
+ useEffect(() => {
422
+ if (view) {
423
+ view.dispatch({
424
+ effects: setComments.of(comments),
425
+ });
426
+ }
427
+ }, [view, comments]);
421
428
  };