@dxos/react-ui-editor 0.3.11-main.d7d4c52 → 0.3.11-main.d8f9aa0

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 (141) hide show
  1. package/dist/lib/browser/index.mjs +1009 -988
  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 +13 -14
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +14 -6
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
  9. package/dist/types/src/components/TextEditor/comments.stories.d.ts +1 -1
  10. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
  11. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  12. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  13. package/dist/types/src/extensions/automerge/defs.d.ts +7 -9
  14. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  15. package/dist/types/src/extensions/automerge/index.d.ts +0 -1
  16. package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
  17. package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -12
  18. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  19. package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
  20. package/dist/types/src/extensions/awareness.d.ts +1 -1
  21. package/dist/types/src/extensions/awareness.d.ts.map +1 -1
  22. package/dist/types/src/extensions/basic.d.ts +4 -5
  23. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  24. package/dist/types/src/extensions/comments.d.ts +30 -17
  25. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  26. package/dist/types/src/extensions/cursor.d.ts +3 -3
  27. package/dist/types/src/extensions/cursor.d.ts.map +1 -1
  28. package/dist/types/src/extensions/index.d.ts +1 -7
  29. package/dist/types/src/extensions/index.d.ts.map +1 -1
  30. package/dist/types/src/extensions/markdown/bundle.d.ts +2 -5
  31. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  32. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  33. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  34. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  35. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  36. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  37. package/dist/types/src/extensions/markdown/index.d.ts +7 -0
  38. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  39. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  40. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  41. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  42. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  43. package/dist/types/src/extensions/outliner.d.ts +4 -0
  44. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  45. package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
  46. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
  47. package/dist/types/src/hooks/defs.d.ts +25 -0
  48. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  49. package/dist/types/src/hooks/index.d.ts +2 -2
  50. package/dist/types/src/hooks/index.d.ts.map +1 -1
  51. package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
  52. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  53. package/dist/types/src/hooks/useTextModel.d.ts +2 -27
  54. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  55. package/dist/types/src/themes/default.d.ts.map +1 -1
  56. package/dist/types/src/util.d.ts +2 -0
  57. package/dist/types/src/util.d.ts.map +1 -1
  58. package/package.json +33 -36
  59. package/src/components/TextEditor/MarkdownEditor.stories.tsx +66 -69
  60. package/src/components/TextEditor/TextEditor.tsx +95 -70
  61. package/src/components/TextEditor/comments.stories.tsx +113 -71
  62. package/src/extensions/autocomplete.ts +6 -3
  63. package/src/extensions/automerge/automerge.ts +39 -45
  64. package/src/extensions/automerge/defs.ts +10 -31
  65. package/src/extensions/automerge/index.ts +0 -1
  66. package/src/extensions/automerge/semaphore.ts +23 -20
  67. package/src/extensions/automerge/update-automerge.ts +2 -2
  68. package/src/extensions/awareness.ts +15 -14
  69. package/src/extensions/basic.ts +22 -17
  70. package/src/extensions/comments.ts +239 -136
  71. package/src/extensions/cursor.ts +7 -3
  72. package/src/extensions/index.ts +1 -7
  73. package/src/extensions/markdown/bundle.ts +18 -24
  74. package/src/extensions/{code.ts → markdown/code.ts} +16 -18
  75. package/src/extensions/markdown/heading.ts +60 -0
  76. package/src/extensions/markdown/hr.ts +72 -0
  77. package/src/extensions/markdown/index.ts +7 -0
  78. package/src/extensions/markdown/tasklist.ts +111 -0
  79. package/src/extensions/mention.ts +9 -2
  80. package/src/extensions/outliner.ts +12 -0
  81. package/src/hooks/awareness-provider.ts +24 -20
  82. package/src/hooks/defs.ts +42 -0
  83. package/src/hooks/index.ts +3 -2
  84. package/src/hooks/useTextEditor.ts +29 -0
  85. package/src/hooks/useTextModel.ts +47 -56
  86. package/src/themes/default.ts +0 -8
  87. package/src/util.ts +26 -3
  88. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  89. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  90. package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
  91. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  92. package/dist/types/src/extensions/code.d.ts.map +0 -1
  93. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  94. package/dist/types/src/extensions/image.d.ts.map +0 -1
  95. package/dist/types/src/extensions/link.d.ts.map +0 -1
  96. package/dist/types/src/extensions/table.d.ts.map +0 -1
  97. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  98. package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
  99. package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
  100. package/dist/types/src/extensions/yjs/index.d.ts +0 -2
  101. package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
  102. package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
  103. package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
  104. package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
  105. package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
  106. package/dist/types/src/hooks/automerge.d.ts +0 -3
  107. package/dist/types/src/hooks/automerge.d.ts.map +0 -1
  108. package/dist/types/src/hooks/yjs.d.ts +0 -27
  109. package/dist/types/src/hooks/yjs.d.ts.map +0 -1
  110. package/dist/types/src/testing/index.d.ts +0 -3
  111. package/dist/types/src/testing/index.d.ts.map +0 -1
  112. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  113. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  114. package/dist/types/src/testing/proto/index.d.ts +0 -2
  115. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  116. package/dist/types/src/testing/replicator.d.ts +0 -45
  117. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  118. package/src/components/TextEditor/codemirror.stories.ts +0 -115
  119. package/src/components/TextEditor/yjs.stories.tsx +0 -56
  120. package/src/extensions/hr.ts +0 -73
  121. package/src/extensions/tasklist.ts +0 -119
  122. package/src/extensions/yjs/cursor.ts +0 -22
  123. package/src/extensions/yjs/index.ts +0 -5
  124. package/src/extensions/yjs/yjs.test.ts +0 -35
  125. package/src/extensions/yjs/yjs.ts +0 -16
  126. package/src/hooks/automerge.ts +0 -52
  127. package/src/hooks/yjs.ts +0 -156
  128. package/src/testing/index.ts +0 -6
  129. package/src/testing/proto/gen/schema.ts +0 -49
  130. package/src/testing/proto/index.ts +0 -5
  131. package/src/testing/proto/schema.proto +0 -15
  132. package/src/testing/replicator.ts +0 -184
  133. package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  134. package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  135. package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  136. package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  137. package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  138. package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
  139. package/src/extensions/{image.ts → markdown/image.ts} +0 -0
  140. package/src/extensions/{link.ts → markdown/link.ts} +3 -3
  141. /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { Facet } from '@codemirror/state';
5
+ import { type EditorState, Facet } from '@codemirror/state';
6
6
 
7
7
  import type { Range } from '../hooks';
8
8
 
@@ -31,13 +31,17 @@ export class Cursor {
31
31
  combine: (providers) => providers[0] ?? defaultCursorConverter,
32
32
  });
33
33
 
34
- static readonly getCursorFromRange = (cursorConverter: CursorConverter, range: Range) => {
34
+ static readonly getCursorFromRange = (state: EditorState, range: Range) => {
35
+ const cursorConverter = state.facet(Cursor.converter);
36
+
35
37
  const from = cursorConverter.toCursor(range.from);
36
38
  const to = cursorConverter.toCursor(range.to, -1);
37
39
  return [from, to].join(':');
38
40
  };
39
41
 
40
- static readonly getRangeFromCursor = (cursorConverter: CursorConverter, cursor: string) => {
42
+ static readonly getRangeFromCursor = (state: EditorState, cursor: string) => {
43
+ const cursorConverter = state.facet(Cursor.converter);
44
+
41
45
  const parts = cursor.split(':');
42
46
  const from = cursorConverter.fromCursor(parts[0]);
43
47
  const to = cursorConverter.fromCursor(parts[1]);
@@ -7,16 +7,10 @@ export * from './automerge';
7
7
  export * from './awareness';
8
8
  export * from './basic';
9
9
  export * from './blast';
10
- export * from './code';
11
10
  export * from './comments';
12
11
  export * from './cursor';
13
- export * from './hr';
14
- export * from './image';
15
- export * from './link';
16
12
  export * from './listener';
17
13
  export * from './markdown';
18
14
  export * from './mention';
19
- export * from './table';
20
- export * from './tasklist';
15
+ export * from './outliner';
21
16
  export * from './typewriter';
22
- export * from './yjs';
@@ -23,18 +23,15 @@ import {
23
23
  import type { ThemeMode } from '@dxos/react-ui';
24
24
 
25
25
  import { markdownHighlightStyle, markdownTagsExtensions } from './highlight';
26
+ import { type TextEditorProps } from '../../components';
26
27
 
27
28
  export type MarkdownBundleOptions = {
28
- readonly?: boolean;
29
29
  themeMode?: ThemeMode;
30
- placeholder?: string;
31
- };
30
+ } & Pick<TextEditorProps, 'readonly' | 'placeholder'>;
32
31
 
33
32
  /**
34
33
  * Markdown bundle.
35
34
  * Refs:
36
- * https://github.com/codemirror/basic-setup
37
- * https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts#L50
38
35
  * https://codemirror.net/docs/community
39
36
  * https://codemirror.net/docs/ref/#codemirror.basicSetup
40
37
  */
@@ -49,8 +46,7 @@ export const markdownBundle = ({
49
46
  EditorState.tabSize.of(2),
50
47
  EditorView.lineWrapping,
51
48
 
52
- customKeymap(),
53
-
49
+ // https://github.com/codemirror/basic-setup
54
50
  bracketMatching(),
55
51
  closeBrackets(),
56
52
  crosshairCursor(),
@@ -59,7 +55,7 @@ export const markdownBundle = ({
59
55
  highlightActiveLine(),
60
56
  history(),
61
57
  indentOnInput(),
62
- _placeholder && placeholder(_placeholder),
58
+ _placeholder ? placeholder(_placeholder) : [],
63
59
 
64
60
  // Main extension.
65
61
  // https://github.com/codemirror/lang-markdown
@@ -87,20 +83,18 @@ export const markdownBundle = ({
87
83
 
88
84
  // Custom styles.
89
85
  syntaxHighlighting(markdownHighlightStyle(readonly)),
90
- ].filter(Boolean) as Extension[];
91
- };
92
86
 
93
- // https://codemirror.net/docs/ref/#view.keymap
94
- const customKeymap = () =>
95
- keymap.of([
96
- // https://codemirror.net/docs/ref/#commands.indentWithTab
97
- indentWithTab,
98
- // https://codemirror.net/docs/ref/#commands.standardKeymap
99
- ...standardKeymap,
100
- // https://codemirror.net/docs/ref/#commands.historyKeymap
101
- ...historyKeymap,
102
- // https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
103
- ...closeBracketsKeymap,
104
- // https://codemirror.net/docs/ref/#search.searchKeymap
105
- ...searchKeymap,
106
- ]);
87
+ keymap.of([
88
+ // https://codemirror.net/docs/ref/#commands.indentWithTab
89
+ indentWithTab,
90
+ // https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
91
+ ...closeBracketsKeymap,
92
+ // https://codemirror.net/docs/ref/#commands.historyKeymap
93
+ ...historyKeymap,
94
+ // https://codemirror.net/docs/ref/#search.searchKeymap
95
+ ...searchKeymap,
96
+ // https://codemirror.net/docs/ref/#commands.standardKeymap
97
+ ...standardKeymap,
98
+ ]),
99
+ ];
100
+ };
@@ -14,7 +14,7 @@ import {
14
14
 
15
15
  import { mx } from '@dxos/react-ui-theme';
16
16
 
17
- import { getToken } from '../styles';
17
+ import { getToken } from '../../styles';
18
18
 
19
19
  const CODE_REGEX = /```[\s\S]*?```/gs;
20
20
 
@@ -53,22 +53,20 @@ const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
53
53
  };
54
54
 
55
55
  export const code = () => {
56
- const getDecorations = (view: EditorView) => {
56
+ const buildDecorations = (view: EditorView) => {
57
57
  const decorations: Range<Decoration>[] = [];
58
- if (!view.hasFocus || view.state.readOnly) {
59
- const text = view.state.doc.sliceString(0);
60
- const matches = text.matchAll(CODE_REGEX);
61
- const blocks = view.viewportLineBlocks;
62
- for (const match of matches) {
63
- const range = getLineRange(blocks, match.index!, match.index! + match[0].length);
64
- for (let i = range[0]; i <= range[1]; i++) {
65
- const block = blocks[i];
66
- decorations.push(
67
- Decoration.line({
68
- class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
69
- }).range(block.from),
70
- );
71
- }
58
+ const text = view.state.doc.sliceString(0);
59
+ const matches = text.matchAll(CODE_REGEX);
60
+ const blocks = view.viewportLineBlocks;
61
+ for (const match of matches) {
62
+ const range = getLineRange(blocks, match.index!, match.index! + match[0].length);
63
+ for (let i = range[0]; i <= range[1]; i++) {
64
+ const block = blocks[i];
65
+ decorations.push(
66
+ Decoration.line({
67
+ class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
68
+ }).range(block.from),
69
+ );
72
70
  }
73
71
  }
74
72
 
@@ -81,7 +79,7 @@ export const code = () => {
81
79
  hasFocus = false;
82
80
  decorations: DecorationSet;
83
81
  constructor(view: EditorView) {
84
- this.decorations = getDecorations(view);
82
+ this.decorations = buildDecorations(view);
85
83
  }
86
84
 
87
85
  update(update: ViewUpdate) {
@@ -93,7 +91,7 @@ export const code = () => {
93
91
  update.viewportChanged
94
92
  ) {
95
93
  this.hasFocus = update.view.hasFocus;
96
- this.decorations = getDecorations(update.view);
94
+ this.decorations = buildDecorations(update.view);
97
95
  }
98
96
  }
99
97
  },
@@ -0,0 +1,60 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
7
+ import { Decoration, type DecorationSet, type EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view';
8
+
9
+ const buildDecorations = (view: EditorView): DecorationSet => {
10
+ const builder = new RangeSetBuilder<Decoration>();
11
+ const { state } = view;
12
+ const cursor = state.selection.main.head;
13
+
14
+ for (const { from, to } of view.visibleRanges) {
15
+ syntaxTree(state).iterate({
16
+ enter: (node) => {
17
+ switch (node.name) {
18
+ case 'ATXHeading1':
19
+ case 'ATXHeading2':
20
+ case 'ATXHeading3':
21
+ case 'ATXHeading4':
22
+ case 'ATXHeading5':
23
+ case 'ATXHeading6': {
24
+ const mark = node.node.getChild('HeaderMark');
25
+
26
+ // Check if cursor is inside text.
27
+ if (mark && (cursor < node.from || cursor > node.to)) {
28
+ builder.add(mark.from, mark.to + 1, Decoration.replace({}));
29
+ }
30
+ }
31
+ }
32
+ },
33
+ from,
34
+ to,
35
+ });
36
+ }
37
+
38
+ return builder.finish();
39
+ };
40
+
41
+ export const heading = () => {
42
+ return [
43
+ ViewPlugin.fromClass(
44
+ class {
45
+ decorations: DecorationSet;
46
+
47
+ constructor(view: EditorView) {
48
+ this.decorations = buildDecorations(view);
49
+ }
50
+
51
+ update(update: ViewUpdate) {
52
+ this.decorations = buildDecorations(update.view);
53
+ }
54
+ },
55
+ {
56
+ decorations: (v) => v.decorations,
57
+ },
58
+ ),
59
+ ];
60
+ };
@@ -0,0 +1,72 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
7
+ import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
8
+
9
+ import { getToken } from '../../styles';
10
+
11
+ // TODO(burdon): Reconcile with theme.
12
+ const styles = EditorView.baseTheme({
13
+ '& .cm-hr': {
14
+ // Note that block-level decorations should not have vertical margins,
15
+ borderTop: `1px solid ${getToken('extend.colors.neutral.200')}`,
16
+ },
17
+ });
18
+
19
+ class HorizontalRuleWidget extends WidgetType {
20
+ override toDOM() {
21
+ const el = document.createElement('div');
22
+ el.className = 'cm-hr';
23
+ return el;
24
+ }
25
+ }
26
+
27
+ const decoration = Decoration.replace({ widget: new HorizontalRuleWidget() });
28
+
29
+ const buildDecorations = (view: EditorView): DecorationSet => {
30
+ const builder = new RangeSetBuilder<Decoration>();
31
+ const { state } = view;
32
+ const cursor = state.selection.main.head;
33
+
34
+ for (const { from, to } of view.visibleRanges) {
35
+ syntaxTree(state).iterate({
36
+ enter: (node) => {
37
+ if (node.name === 'HorizontalRule') {
38
+ // Check if cursor is inside text.
39
+ if (cursor <= node.from || cursor >= node.to) {
40
+ builder.add(node.from, node.to, decoration);
41
+ }
42
+ }
43
+ },
44
+ from,
45
+ to,
46
+ });
47
+ }
48
+
49
+ return builder.finish();
50
+ };
51
+
52
+ export const hr = () => {
53
+ return [
54
+ styles,
55
+ ViewPlugin.fromClass(
56
+ class {
57
+ decorations: DecorationSet;
58
+
59
+ constructor(view: EditorView) {
60
+ this.decorations = buildDecorations(view);
61
+ }
62
+
63
+ update(update: ViewUpdate) {
64
+ this.decorations = buildDecorations(update.view);
65
+ }
66
+ },
67
+ {
68
+ decorations: (v) => v.decorations,
69
+ },
70
+ ),
71
+ ];
72
+ };
@@ -3,4 +3,11 @@
3
3
  //
4
4
 
5
5
  export * from './bundle';
6
+ export * from './code';
7
+ export * from './heading';
6
8
  export * from './highlight';
9
+ export * from './hr';
10
+ export * from './image';
11
+ export * from './link';
12
+ export * from './table';
13
+ export * from './tasklist';
@@ -0,0 +1,111 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
7
+ import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
8
+
9
+ import { getToken } from '../../styles';
10
+
11
+ // TODO(burdon): Reconcile with theme.
12
+ const styles = EditorView.baseTheme({
13
+ '& .cm-task': {
14
+ color: getToken('extend.colors.blue.500'),
15
+ },
16
+ '& .cm-task-checkbox': {
17
+ marginLeft: '4px',
18
+ marginRight: '4px',
19
+ },
20
+ });
21
+
22
+ class CheckboxWidget extends WidgetType {
23
+ constructor(private _checked: boolean) {
24
+ super();
25
+ }
26
+
27
+ override eq(other: this) {
28
+ return this._checked === other._checked;
29
+ }
30
+
31
+ override toDOM(view: EditorView) {
32
+ const input = document.createElement('input');
33
+ input.className = 'cm-task-checkbox';
34
+ input.type = 'checkbox';
35
+ input.checked = this._checked;
36
+ if (view.state.readOnly) {
37
+ input.setAttribute('disabled', 'true');
38
+ } else {
39
+ input.onmousedown = (event: Event) => {
40
+ const pos = view.posAtDOM(input);
41
+ const text = view.state.sliceDoc(pos, pos + 3);
42
+ if (text === (this._checked ? '[x]' : '[ ]')) {
43
+ view.dispatch({
44
+ changes: { from: pos + 1, to: pos + 2, insert: this._checked ? ' ' : 'x' },
45
+ });
46
+ event.preventDefault();
47
+ }
48
+ };
49
+ }
50
+
51
+ return input;
52
+ }
53
+
54
+ override ignoreEvent() {
55
+ return false;
56
+ }
57
+ }
58
+
59
+ const checkedDecoration = Decoration.replace({ widget: new CheckboxWidget(true) });
60
+ const uncheckedDecoration = Decoration.replace({ widget: new CheckboxWidget(false) });
61
+
62
+ const buildDecorations = (view: EditorView): DecorationSet => {
63
+ const builder = new RangeSetBuilder<Decoration>();
64
+ const { state } = view;
65
+ const cursor = state.selection.main.head;
66
+
67
+ for (const { from, to } of view.visibleRanges) {
68
+ syntaxTree(state).iterate({
69
+ enter: (node) => {
70
+ if (node.name === 'TaskMarker') {
71
+ // Check if cursor is inside text.
72
+ if (cursor < node.from || cursor > node.to) {
73
+ const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
74
+ builder.add(node.from - 2, node.from - 1, Decoration.mark({ class: 'cm-task' }));
75
+ builder.add(node.from, node.to, checked ? checkedDecoration : uncheckedDecoration);
76
+ }
77
+ }
78
+ },
79
+ from,
80
+ to,
81
+ });
82
+ }
83
+
84
+ return builder.finish();
85
+ };
86
+
87
+ export type TasklistOptions = {};
88
+
89
+ export const tasklist = (options: TasklistOptions = {}) => {
90
+ return [
91
+ ViewPlugin.fromClass(
92
+ class {
93
+ decorations: DecorationSet;
94
+
95
+ constructor(view: EditorView) {
96
+ this.decorations = buildDecorations(view);
97
+ }
98
+
99
+ update(update: ViewUpdate) {
100
+ if (update.docChanged || update.viewportChanged || update.selectionSet) {
101
+ this.decorations = buildDecorations(update.view);
102
+ }
103
+ }
104
+ },
105
+ {
106
+ decorations: (v) => v.decorations,
107
+ },
108
+ ),
109
+ styles,
110
+ ];
111
+ };
@@ -9,18 +9,25 @@ export type MentionOptions = {
9
9
  onSearch: (text: string) => string[];
10
10
  };
11
11
 
12
+ // TODO(burdon): Can only have a single autocompletion. Merge configuration with autocomplete.
12
13
  export const mention = ({ onSearch }: MentionOptions): Extension => {
13
14
  return autocompletion({
15
+ // TODO(burdon): Not working.
16
+ activateOnTyping: true,
17
+ // activateOnTypingDelay: 100,
18
+ // selectOnOpen: true,
14
19
  closeOnBlur: false, // For debugging.
20
+ // defaultKeymap: false,
21
+ icons: false,
15
22
  override: [
16
23
  (context: CompletionContext): CompletionResult | null => {
24
+ console.log(context);
25
+
17
26
  const match = context.matchBefore(/@(\w+)?/);
18
27
  if (!match || (match.from === match.to && !context.explicit)) {
19
28
  return null;
20
29
  }
21
30
 
22
- console.log(match);
23
-
24
31
  return {
25
32
  from: match.from,
26
33
  options: onSearch(match.text.slice(1).toLowerCase()).map((value) => ({ label: `@${value}` })),
@@ -0,0 +1,12 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Extension } from '@codemirror/state';
6
+
7
+ export type OutlinerOptions = {};
8
+
9
+ // TODO(burdon): Experimental component: multiple hierarchical draggable editor sections.
10
+ export const outliner = (options: OutlinerOptions = {}): Extension => {
11
+ return [];
12
+ };
@@ -29,9 +29,10 @@ export type AwarenessProviderParams = {
29
29
  info: AwarenessInfo;
30
30
  };
31
31
 
32
+ /**
33
+ * Receives and broadcasts profile and cursor position.
34
+ */
32
35
  export class SpaceAwarenessProvider implements AwarenessProvider {
33
- public readonly remoteStateChange = new Event<void>();
34
-
35
36
  private readonly _remoteStates = new Map<string, AwarenessState>();
36
37
 
37
38
  private readonly _space: Space;
@@ -39,9 +40,11 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
39
40
  private readonly _peerId: string;
40
41
  private readonly _info: AwarenessInfo;
41
42
 
42
- private _localState?: AwarenessState;
43
- private _postTask?: DeferredTask;
44
43
  private _ctx?: Context;
44
+ private _postTask?: DeferredTask;
45
+ private _localState?: AwarenessState;
46
+
47
+ public readonly remoteStateChange = new Event<void>();
45
48
 
46
49
  constructor(params: AwarenessProviderParams) {
47
50
  this._space = params.space;
@@ -63,19 +66,20 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
63
66
  }
64
67
  });
65
68
 
66
- const unsubscribe = this._space.listen(this._channel, (message: GossipMessage) => {
67
- switch (message.payload.kind) {
68
- case 'query': {
69
- this._handleQueryMessage();
70
- break;
69
+ this._ctx.onDispose(
70
+ this._space.listen(this._channel, (message: GossipMessage) => {
71
+ switch (message.payload.kind) {
72
+ case 'query': {
73
+ this._handleQueryMessage();
74
+ break;
75
+ }
76
+ case 'post': {
77
+ this._handlePostMessage(message.payload);
78
+ break;
79
+ }
71
80
  }
72
- case 'post': {
73
- this._handlePostMessage(message.payload);
74
- break;
75
- }
76
- }
77
- });
78
- this._ctx.onDispose(unsubscribe);
81
+ }),
82
+ );
79
83
 
80
84
  void this._space
81
85
  .postMessage(this._channel, {
@@ -92,6 +96,10 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
92
96
  this._postTask = undefined;
93
97
  }
94
98
 
99
+ getRemoteStates(): AwarenessState[] {
100
+ return Array.from(this._remoteStates.values());
101
+ }
102
+
95
103
  update(position: AwarenessPosition | undefined): void {
96
104
  invariant(this._postTask);
97
105
  this._localState = {
@@ -103,10 +111,6 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
103
111
  this._postTask.schedule();
104
112
  }
105
113
 
106
- getRemoteStates(): AwarenessState[] {
107
- return Array.from(this._remoteStates.values());
108
- }
109
-
110
114
  private _handleQueryMessage() {
111
115
  invariant(this._postTask);
112
116
  this._postTask.schedule();
@@ -0,0 +1,42 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Extension, StateField } from '@codemirror/state';
6
+
7
+ import { type DocAccessor } from '@dxos/echo-schema';
8
+
9
+ // Runtime data structure.
10
+ export type Range = {
11
+ from: number;
12
+ to: number;
13
+ };
14
+
15
+ // Persistent data structure.
16
+ export type Comment = {
17
+ id: string;
18
+ cursor?: string;
19
+ };
20
+
21
+ export type EditorModel = {
22
+ id: string;
23
+ text: () => string;
24
+ extension?: Extension;
25
+
26
+ // TODO(burdon): Remove.
27
+ content: string | DocAccessor;
28
+
29
+ // TODO(burdon): Remove.
30
+ peer?: {
31
+ id: string;
32
+ name?: string;
33
+ };
34
+ };
35
+
36
+ /**
37
+ * State field makes the model available to other extensions.
38
+ */
39
+ export const modelState = StateField.define<EditorModel | undefined>({
40
+ create: () => undefined,
41
+ update: (model) => model,
42
+ });
@@ -2,7 +2,8 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './automerge';
6
5
  export * from './awareness-provider';
6
+ export * from './defs';
7
+
8
+ export * from './useTextEditor';
7
9
  export * from './useTextModel';
8
- export * from './yjs';
@@ -0,0 +1,29 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type EditorView } from '@codemirror/view';
6
+ import { type RefCallback, useState } from 'react';
7
+
8
+ /**
9
+ * Hook for accessing the editor view.
10
+ *
11
+ * ```tsx
12
+ * const Test = () => {
13
+ * const [editorRef, editorView] = useTextEditor();
14
+ * useEffect(() => {
15
+ * editorView?.focus();
16
+ * }, [editorView]);
17
+ * return <TextEditor ref={editorRef} />;
18
+ * };
19
+ * ```
20
+ */
21
+ export const useTextEditor = (): [RefCallback<EditorView | null>, EditorView | null] => {
22
+ const [view, setView] = useState<EditorView | null>(null);
23
+ return [
24
+ (ref: EditorView | null) => {
25
+ setView(ref);
26
+ },
27
+ view,
28
+ ];
29
+ };