@dxos/react-ui-editor 0.3.11-main.68db74e → 0.3.11-main.69484a6

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 (180) hide show
  1. package/dist/lib/browser/index.mjs +1632 -1056
  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 -15
  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/automerge.stories.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/comments.stories.d.ts +1 -1
  11. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
  12. package/dist/types/src/components/TextEditor/{yjs.stories.d.ts → hooks.stories.d.ts} +13 -2
  13. package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
  14. package/dist/types/src/components/Toolbar/Toolbar.d.ts +25 -0
  15. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  16. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +23 -0
  17. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  18. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  19. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  20. package/dist/types/src/components/index.d.ts +1 -0
  21. package/dist/types/src/components/index.d.ts.map +1 -1
  22. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  23. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  24. package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
  25. package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
  26. package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
  27. package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
  28. package/dist/types/src/extensions/automerge/defs.d.ts +9 -11
  29. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  30. package/dist/types/src/extensions/automerge/index.d.ts +0 -1
  31. package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
  32. package/dist/types/src/extensions/automerge/semaphore.d.ts +8 -12
  33. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  34. package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
  35. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  36. package/dist/types/src/extensions/awareness.d.ts +2 -2
  37. package/dist/types/src/extensions/awareness.d.ts.map +1 -1
  38. package/dist/types/src/extensions/basic.d.ts +4 -5
  39. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  40. package/dist/types/src/extensions/comments.d.ts +35 -18
  41. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  42. package/dist/types/src/extensions/cursor.d.ts +3 -3
  43. package/dist/types/src/extensions/cursor.d.ts.map +1 -1
  44. package/dist/types/src/extensions/index.d.ts +1 -7
  45. package/dist/types/src/extensions/index.d.ts.map +1 -1
  46. package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
  47. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  48. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  49. package/dist/types/src/extensions/markdown/formatting.d.ts +13 -0
  50. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
  51. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  52. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  53. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  54. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  55. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  56. package/dist/types/src/extensions/markdown/index.d.ts +8 -0
  57. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  58. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  59. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  60. package/dist/types/src/extensions/markdown/tasklist.d.ts +3 -0
  61. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  62. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  63. package/dist/types/src/extensions/outliner.d.ts +4 -0
  64. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  65. package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
  66. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
  67. package/dist/types/src/hooks/defs.d.ts +25 -0
  68. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  69. package/dist/types/src/hooks/index.d.ts +4 -2
  70. package/dist/types/src/hooks/index.d.ts.map +1 -1
  71. package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
  72. package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
  73. package/dist/types/src/hooks/useEditorView.d.ts +17 -0
  74. package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
  75. package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
  76. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  77. package/dist/types/src/hooks/useTextModel.d.ts +2 -27
  78. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  79. package/dist/types/src/styles/markdown.d.ts +2 -2
  80. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  81. package/dist/types/src/util.d.ts +2 -0
  82. package/dist/types/src/util.d.ts.map +1 -1
  83. package/package.json +47 -37
  84. package/src/components/TextEditor/MarkdownEditor.stories.tsx +73 -82
  85. package/src/components/TextEditor/TextEditor.tsx +112 -75
  86. package/src/components/TextEditor/automerge.stories.tsx +1 -2
  87. package/src/components/TextEditor/comments.stories.tsx +113 -71
  88. package/src/components/TextEditor/hooks.stories.tsx +106 -0
  89. package/src/components/Toolbar/Toolbar.stories.tsx +90 -0
  90. package/src/components/Toolbar/Toolbar.tsx +166 -0
  91. package/src/components/Toolbar/index.ts +5 -0
  92. package/src/components/index.ts +1 -0
  93. package/src/extensions/autocomplete.ts +6 -3
  94. package/src/extensions/automerge/automerge.spec.tsx +76 -0
  95. package/src/extensions/automerge/automerge.test.ts +13 -0
  96. package/src/extensions/automerge/automerge.ts +54 -45
  97. package/src/extensions/automerge/cursor.ts +3 -3
  98. package/src/extensions/automerge/defs.ts +13 -34
  99. package/src/extensions/automerge/index.ts +0 -1
  100. package/src/extensions/automerge/semaphore.ts +33 -32
  101. package/src/extensions/automerge/update-automerge.ts +14 -8
  102. package/src/extensions/automerge/update-codemirror.ts +5 -5
  103. package/src/extensions/awareness.ts +18 -17
  104. package/src/extensions/basic.ts +17 -10
  105. package/src/extensions/comments.ts +316 -165
  106. package/src/extensions/cursor.ts +7 -3
  107. package/src/extensions/index.ts +1 -7
  108. package/src/extensions/markdown/bundle.ts +23 -28
  109. package/src/extensions/markdown/code.ts +150 -0
  110. package/src/extensions/markdown/formatting.ts +188 -0
  111. package/src/extensions/markdown/heading.ts +59 -0
  112. package/src/extensions/markdown/highlight.ts +9 -15
  113. package/src/extensions/markdown/hr.ts +71 -0
  114. package/src/extensions/markdown/index.ts +8 -0
  115. package/src/extensions/{link.ts → markdown/link.ts} +8 -5
  116. package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -33
  117. package/src/extensions/mention.ts +9 -2
  118. package/src/extensions/outliner.ts +12 -0
  119. package/src/hooks/awareness-provider.ts +24 -20
  120. package/src/hooks/defs.ts +42 -0
  121. package/src/hooks/index.ts +5 -2
  122. package/src/hooks/useActionHandler.ts +62 -0
  123. package/src/hooks/useEditorView.ts +29 -0
  124. package/src/hooks/useTextEditor.ts +153 -0
  125. package/src/hooks/useTextModel.ts +49 -55
  126. package/src/styles/markdown.ts +9 -10
  127. package/src/themes/default.ts +4 -4
  128. package/src/util.ts +26 -3
  129. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  130. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  131. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  132. package/dist/types/src/extensions/code.d.ts.map +0 -1
  133. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  134. package/dist/types/src/extensions/image.d.ts.map +0 -1
  135. package/dist/types/src/extensions/link.d.ts.map +0 -1
  136. package/dist/types/src/extensions/table.d.ts.map +0 -1
  137. package/dist/types/src/extensions/tasklist.d.ts +0 -7
  138. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  139. package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
  140. package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
  141. package/dist/types/src/extensions/yjs/index.d.ts +0 -2
  142. package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
  143. package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
  144. package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
  145. package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
  146. package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
  147. package/dist/types/src/hooks/automerge.d.ts +0 -3
  148. package/dist/types/src/hooks/automerge.d.ts.map +0 -1
  149. package/dist/types/src/hooks/yjs.d.ts +0 -27
  150. package/dist/types/src/hooks/yjs.d.ts.map +0 -1
  151. package/dist/types/src/testing/index.d.ts +0 -3
  152. package/dist/types/src/testing/index.d.ts.map +0 -1
  153. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  154. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  155. package/dist/types/src/testing/proto/index.d.ts +0 -2
  156. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  157. package/dist/types/src/testing/replicator.d.ts +0 -45
  158. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  159. package/src/components/TextEditor/codemirror.stories.ts +0 -115
  160. package/src/components/TextEditor/yjs.stories.tsx +0 -56
  161. package/src/extensions/code.ts +0 -106
  162. package/src/extensions/hr.ts +0 -73
  163. package/src/extensions/yjs/cursor.ts +0 -22
  164. package/src/extensions/yjs/index.ts +0 -5
  165. package/src/extensions/yjs/yjs.test.ts +0 -35
  166. package/src/extensions/yjs/yjs.ts +0 -16
  167. package/src/hooks/automerge.ts +0 -52
  168. package/src/hooks/yjs.ts +0 -156
  169. package/src/testing/index.ts +0 -6
  170. package/src/testing/proto/gen/schema.ts +0 -49
  171. package/src/testing/proto/index.ts +0 -5
  172. package/src/testing/proto/schema.proto +0 -15
  173. package/src/testing/replicator.ts +0 -184
  174. /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  175. /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  176. /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  177. /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  178. /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  179. /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
  180. /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
@@ -6,6 +6,19 @@ import { syntaxTree } from '@codemirror/language';
6
6
  import { RangeSetBuilder } from '@codemirror/state';
7
7
  import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
8
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
+
9
22
  class CheckboxWidget extends WidgetType {
10
23
  constructor(private _checked: boolean) {
11
24
  super();
@@ -17,6 +30,7 @@ class CheckboxWidget extends WidgetType {
17
30
 
18
31
  override toDOM(view: EditorView) {
19
32
  const input = document.createElement('input');
33
+ input.className = 'cm-task-checkbox';
20
34
  input.type = 'checkbox';
21
35
  input.checked = this._checked;
22
36
  if (view.state.readOnly) {
@@ -42,35 +56,8 @@ class CheckboxWidget extends WidgetType {
42
56
  }
43
57
  }
44
58
 
45
- const checkedDeco = Decoration.replace({ widget: new CheckboxWidget(true) });
46
- const uncheckedDeco = Decoration.replace({ widget: new CheckboxWidget(false) });
47
-
48
- export type TasklistOptions = {};
49
-
50
- export const tasklist = (options: TasklistOptions = {}) => {
51
- return ViewPlugin.fromClass(
52
- class {
53
- decorations: DecorationSet;
54
-
55
- constructor(view: EditorView) {
56
- this.decorations = buildDecorations(view);
57
- }
58
-
59
- update(update: ViewUpdate) {
60
- if (update.docChanged || update.viewportChanged || update.selectionSet) {
61
- this.decorations = buildDecorations(update.view);
62
- }
63
- }
64
- },
65
- {
66
- decorations: (v) => v.decorations,
67
- provide: (plugin) =>
68
- EditorView.atomicRanges.of((view) => {
69
- return view.plugin(plugin)?.decorations || Decoration.none;
70
- }),
71
- },
72
- );
73
- };
59
+ const checkedDecoration = Decoration.replace({ widget: new CheckboxWidget(true) });
60
+ const uncheckedDecoration = Decoration.replace({ widget: new CheckboxWidget(false) });
74
61
 
75
62
  const buildDecorations = (view: EditorView): DecorationSet => {
76
63
  const builder = new RangeSetBuilder<Decoration>();
@@ -80,10 +67,13 @@ const buildDecorations = (view: EditorView): DecorationSet => {
80
67
  for (const { from, to } of view.visibleRanges) {
81
68
  syntaxTree(state).iterate({
82
69
  enter: (node) => {
83
- // Check if cursor is inside text.
84
- if (node.name === 'TaskMarker' && (cursor < node.from || cursor > node.to)) {
85
- const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
86
- builder.add(node.from, node.to, checked ? checkedDeco : uncheckedDeco);
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
+ }
87
77
  }
88
78
  },
89
79
  from,
@@ -93,3 +83,28 @@ const buildDecorations = (view: EditorView): DecorationSet => {
93
83
 
94
84
  return builder.finish();
95
85
  };
86
+
87
+ export type TasklistOptions = {};
88
+
89
+ export const tasklist = (options: TasklistOptions = {}) => {
90
+ return [
91
+ ViewPlugin.fromClass(
92
+ class {
93
+ decorations: DecorationSet;
94
+ constructor(view: EditorView) {
95
+ this.decorations = buildDecorations(view);
96
+ }
97
+
98
+ update(update: ViewUpdate) {
99
+ if (update.docChanged || update.viewportChanged || update.selectionSet) {
100
+ this.decorations = buildDecorations(update.view);
101
+ }
102
+ }
103
+ },
104
+ {
105
+ decorations: (value) => value.decorations,
106
+ },
107
+ ),
108
+ styles,
109
+ ];
110
+ };
@@ -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,10 @@
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 './useActionHandler';
9
+ export * from './useEditorView';
10
+ export * from './useTextEditor';
7
11
  export * from './useTextModel';
8
- export * from './yjs';
@@ -0,0 +1,62 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import type { EditorView } from '@codemirror/view';
6
+
7
+ import type { ToolbarProps } from '../components';
8
+ import {
9
+ createComment,
10
+ insertCodeblock,
11
+ insertTable,
12
+ setHeading,
13
+ toggleBold,
14
+ toggleItalic,
15
+ toggleList,
16
+ toggleStrikethrough,
17
+ } from '../extensions';
18
+
19
+ export const useActionHandler = (view?: EditorView | null): ToolbarProps['onAction'] => {
20
+ return (action) => {
21
+ if (!view) {
22
+ return;
23
+ }
24
+
25
+ switch (action.type) {
26
+ case 'heading':
27
+ setHeading(parseInt(action.data))(view);
28
+ break;
29
+
30
+ case 'bold':
31
+ toggleBold(view);
32
+ break;
33
+ case 'italic':
34
+ toggleItalic(view);
35
+ break;
36
+ case 'strikethrough':
37
+ toggleStrikethrough(view);
38
+ break;
39
+
40
+ case 'list':
41
+ toggleList(view);
42
+ break;
43
+
44
+ case 'codeblock':
45
+ insertCodeblock(view);
46
+ break;
47
+ case 'table':
48
+ insertTable(view);
49
+ break;
50
+
51
+ case 'comment':
52
+ createComment(view);
53
+ break;
54
+ }
55
+
56
+ // TODO(burdon): Hack otherwise remains on heading selector.
57
+ setTimeout(() => {
58
+ view.focus();
59
+ view.dispatch({ selection: view.state.selection });
60
+ });
61
+ };
62
+ };
@@ -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] = useEditorView();
14
+ * useEffect(() => {
15
+ * editorView?.focus();
16
+ * }, [editorView]);
17
+ * return <TextEditor ref={editorRef} />;
18
+ * };
19
+ * ```
20
+ */
21
+ export const useEditorView = (): [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
+ };
@@ -0,0 +1,153 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import {
6
+ EditorSelection,
7
+ EditorState,
8
+ type EditorStateConfig,
9
+ type Extension,
10
+ type StateEffect,
11
+ } from '@codemirror/state';
12
+ import { EditorView } from '@codemirror/view';
13
+ import { type RefObject, useEffect, useRef, useState } from 'react';
14
+
15
+ import { log } from '@dxos/log';
16
+ import { type ThemeMode } from '@dxos/react-ui';
17
+ import { isNotFalsy } from '@dxos/util';
18
+
19
+ import { type ThemeStyles } from '..//styles';
20
+ import { defaultTheme } from '../themes';
21
+ import { logChanges } from '../util';
22
+
23
+ export type UseTextEditorOptions = {
24
+ autoFocus?: boolean;
25
+ scrollTo?: StateEffect<any>;
26
+ debug?: boolean;
27
+ } & EditorStateConfig;
28
+
29
+ export type UseTextEditor = {
30
+ parentRef: RefObject<HTMLDivElement>;
31
+ view?: EditorView;
32
+ };
33
+
34
+ /**
35
+ * Hook for creating editor.
36
+ */
37
+ export const useTextEditor = ({
38
+ autoFocus,
39
+ scrollTo,
40
+ debug,
41
+ doc,
42
+ selection,
43
+ extensions,
44
+ }: UseTextEditorOptions = {}): UseTextEditor => {
45
+ const onUpdate = useRef<() => void>();
46
+ const [view, setView] = useState<EditorView>();
47
+ const parentRef = useRef<HTMLDivElement>(null);
48
+ useEffect(() => {
49
+ if (parentRef.current) {
50
+ // https://codemirror.net/docs/ref/#state.EditorStateConfig
51
+ const state = EditorState.create({
52
+ doc,
53
+ selection,
54
+ extensions: [
55
+ // TODO(burdon): Doesn't catch errors in keymap functions.
56
+ EditorView.exceptionSink.of((err) => {
57
+ log.catch(err);
58
+ }),
59
+ extensions,
60
+ EditorView.updateListener.of(() => {
61
+ onUpdate.current?.();
62
+ }),
63
+ ].filter(isNotFalsy),
64
+ });
65
+
66
+ // https://codemirror.net/docs/ref/#view.EditorViewConfig
67
+ const view = new EditorView({
68
+ parent: parentRef.current,
69
+ scrollTo,
70
+ state,
71
+ // NOTE: Uncomment to debug/monitor all transactions.
72
+ // https://codemirror.net/docs/ref/#view.EditorView.dispatch
73
+ dispatchTransactions: (trs, view) => {
74
+ if (debug) {
75
+ logChanges(trs);
76
+ }
77
+ view.update(trs);
78
+ },
79
+ });
80
+
81
+ setView(view);
82
+ }
83
+
84
+ return () => {
85
+ view?.destroy();
86
+ };
87
+ }, [parentRef]);
88
+
89
+ useEffect(() => {
90
+ if (view) {
91
+ // Select end of line if not specified.
92
+ if (!selection && !view.state.selection.main.anchor) {
93
+ selection = EditorSelection.single(view.state.doc.line(1).to);
94
+ }
95
+
96
+ // Set selection after first update (since content may rerender on focus).
97
+ // TODO(burdon): BUG on first render may appear in middle of formatted heading.
98
+ // TODO(burdon): Make invisible until first render?
99
+ if (selection || scrollTo) {
100
+ onUpdate.current = () => {
101
+ onUpdate.current = undefined;
102
+ view.dispatch({ selection, effects: scrollTo && [scrollTo], scrollIntoView: !scrollTo });
103
+ };
104
+ }
105
+
106
+ if (autoFocus) {
107
+ view.focus();
108
+ }
109
+ }
110
+ }, [view, autoFocus, selection, scrollTo]);
111
+
112
+ return { parentRef, view };
113
+ };
114
+
115
+ // TODO(burdon): Factor out extension factories.
116
+
117
+ export type DataExtensionsOptions = {
118
+ readonly?: boolean;
119
+ };
120
+
121
+ // TODO(burdon): Pass in TextObject (remove model).
122
+ export const createDataExtensions = ({ readonly = false } = {}): Extension => {
123
+ return [
124
+ //
125
+ EditorState.readOnly.of(readonly),
126
+ EditorView.editable.of(!readonly),
127
+ ];
128
+ };
129
+
130
+ export type ThemeExtensionsOptions = {
131
+ theme?: ThemeStyles;
132
+ themeMode?: ThemeMode;
133
+ lineWrap?: boolean;
134
+ slots?: {
135
+ editor?: {
136
+ className?: string;
137
+ };
138
+ content?: {
139
+ className?: string;
140
+ };
141
+ };
142
+ };
143
+
144
+ export const createThemeExtensions = ({ theme, themeMode, slots }: ThemeExtensionsOptions = {}): Extension => {
145
+ return [
146
+ //
147
+ EditorView.baseTheme(defaultTheme),
148
+ theme && EditorView.theme(theme),
149
+ EditorView.darkTheme.of(themeMode === 'dark'),
150
+ EditorView.editorAttributes.of({ class: slots?.editor?.className ?? '' }),
151
+ EditorView.contentAttributes.of({ class: slots?.content?.className ?? '' }),
152
+ ].filter(isNotFalsy);
153
+ };
@@ -2,55 +2,19 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { StateField, type Extension } from '@codemirror/state';
5
+ import get from 'lodash.get';
6
6
  import { useEffect, useState } from 'react';
7
- import type * as YP from 'y-protocols/awareness';
8
7
 
9
- import { isAutomergeObject, type DocAccessor, type Space, type TextObject } from '@dxos/react-client/echo';
8
+ import { generateName } from '@dxos/display-name';
9
+ import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
10
+ import { invariant } from '@dxos/invariant';
11
+ import { isAutomergeObject, type Space, type TextObject } from '@dxos/react-client/echo';
10
12
  import { type Identity } from '@dxos/react-client/halo';
11
- import type { YText, YXmlFragment } from '@dxos/text-model';
12
13
 
13
- import { createAutomergeModel } from './automerge';
14
- import { createYjsModel } from './yjs';
15
-
16
- // TODO(burdon): Factor out defs.
17
-
18
- /**
19
- * State field makes the model available to other extensions.
20
- */
21
- export const modelState = StateField.define<EditorModel | undefined>({
22
- create: () => undefined,
23
- update: (model) => model,
24
- });
25
-
26
- export type Range = {
27
- from: number;
28
- to: number;
29
- };
30
-
31
- export type CommentRange = {
32
- id: string;
33
- cursor: string; // TODO(burdon): Rename range.
34
- };
35
-
36
- // TODO(wittjosiah): Factor out to common package? @dxos/react-client?
37
- export type EditorModel = {
38
- id: string;
39
- text: () => string;
40
- extension?: Extension;
41
-
42
- // TODO(burdon): Remove.
43
- content: string | YText | YXmlFragment | DocAccessor;
44
-
45
- // TODO(burdon): Move into extension.
46
- awareness?: YP.Awareness;
47
-
48
- // TODO(burdon): Remove.
49
- peer?: {
50
- id: string;
51
- name?: string;
52
- };
53
- };
14
+ import { SpaceAwarenessProvider } from './awareness-provider';
15
+ import { type EditorModel, modelState } from './defs';
16
+ import { automerge, awareness } from '../extensions';
17
+ import { cursorColor } from '../styles';
54
18
 
55
19
  // TODO(burdon): Remove space/identity dependency. Define interface for the framework re content and presence.
56
20
  export type UseTextModelProps = {
@@ -59,8 +23,6 @@ export type UseTextModelProps = {
59
23
  text?: TextObject;
60
24
  };
61
25
 
62
- // TODO(burdon): Remove YJS/Automerge deps (from UI component -- create abstraction; incl. all ECHO/Space deps).
63
- // TODO(wittjosiah): Factor out to common package? @dxos/react-client?
64
26
  export const useTextModel = (props: UseTextModelProps): EditorModel | undefined => {
65
27
  const { identity, space, text } = props;
66
28
  const [model, setModel] = useState<EditorModel | undefined>();
@@ -68,13 +30,45 @@ export const useTextModel = (props: UseTextModelProps): EditorModel | undefined
68
30
  return model;
69
31
  };
70
32
 
71
- const createModel = (props: UseTextModelProps) => {
72
- const { text } = props;
73
- if (isAutomergeObject(text)) {
74
- return createAutomergeModel(props);
75
- } else if (text?.doc) {
76
- return createYjsModel(props);
77
- } else {
78
- return undefined;
33
+ const createModel = ({ space, identity, text }: UseTextModelProps) => {
34
+ invariant(isAutomergeObject(text));
35
+ const obj = text as any as AutomergeTextCompat;
36
+ const doc = getRawDoc(obj, [obj.field]);
37
+
38
+ const awarenessProvider =
39
+ space &&
40
+ new SpaceAwarenessProvider({
41
+ space,
42
+ channel: `automerge.awareness.${obj.id}`,
43
+ info: {
44
+ displayName: identity ? identity.profile?.displayName ?? generateName(identity.identityKey.toHex()) : undefined,
45
+ color: cursorColor.color,
46
+ lightColor: cursorColor.light,
47
+ },
48
+ peerId: identity?.identityKey.toHex() ?? 'Anonymous',
49
+ });
50
+
51
+ const extensions = [
52
+ //
53
+ modelState.init(() => model),
54
+ automerge({ handle: doc.handle, path: doc.path }),
55
+ ];
56
+ if (awarenessProvider) {
57
+ extensions.push(awareness(awarenessProvider));
79
58
  }
59
+
60
+ const model: EditorModel = {
61
+ id: obj.id,
62
+ content: doc,
63
+ text: () => get(doc.handle.docSync(), doc.path),
64
+ extension: extensions,
65
+ peer: identity
66
+ ? {
67
+ id: identity.identityKey.toHex(),
68
+ name: identity.profile?.displayName,
69
+ }
70
+ : undefined,
71
+ };
72
+
73
+ return model;
80
74
  };
@@ -8,18 +8,17 @@ export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
8
8
 
9
9
  // TODO(burdon): Better way to align vertically than negative margin? Font-specific?
10
10
  // https://tailwindcss.com/docs/font-weight
11
- export const headings: Record<HeadingLevel, string[]> = {
12
- 1: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-4xl', '-ml-[10px]'],
13
- 2: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-3xl', '-ml-[8px]'],
14
- 3: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-2xl', '-ml-[7px]'],
15
- 4: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-xl', '-ml-[6px]'],
16
- 5: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-lg', '-ml-[5px]'],
17
- 6: ['mbs-4 mbe-2 font-medium text-inherit no-underline', '-ml-[4px]'],
11
+ export const headings: Record<HeadingLevel, string> = {
12
+ 1: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-4xl',
13
+ 2: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-3xl',
14
+ 3: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-2xl',
15
+ 4: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-xl',
16
+ 5: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-lg',
17
+ 6: 'mbs-4 mbe-2 font-medium text-inherit no-underline',
18
18
  };
19
19
 
20
- export const heading = (level: HeadingLevel, readonly?: boolean) => {
21
- const [style, offset] = headings[level];
22
- return mx(style, readonly && offset);
20
+ export const heading = (level: HeadingLevel) => {
21
+ return headings[level];
23
22
  };
24
23
 
25
24
  export const text = 'text-neutral-800 dark:text-neutral-200';