@dxos/react-ui-editor 0.3.11-main.24b414e → 0.3.11-main.264ec26

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 (78) hide show
  1. package/dist/lib/browser/index.mjs +607 -471
  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 +13 -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 +2 -0
  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/code.d.ts.map +1 -1
  25. package/dist/types/src/extensions/comments.d.ts +29 -16
  26. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  27. package/dist/types/src/extensions/cursor.d.ts +3 -3
  28. package/dist/types/src/extensions/cursor.d.ts.map +1 -1
  29. package/dist/types/src/extensions/hr.d.ts.map +1 -1
  30. package/dist/types/src/extensions/index.d.ts +1 -0
  31. package/dist/types/src/extensions/index.d.ts.map +1 -1
  32. package/dist/types/src/extensions/markdown/bundle.d.ts +2 -5
  33. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  34. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  35. package/dist/types/src/extensions/outliner.d.ts +4 -0
  36. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  37. package/dist/types/src/extensions/tasklist.d.ts +1 -5
  38. package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
  39. package/dist/types/src/hooks/automerge.d.ts +2 -1
  40. package/dist/types/src/hooks/automerge.d.ts.map +1 -1
  41. package/dist/types/src/hooks/defs.d.ts +28 -0
  42. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  43. package/dist/types/src/hooks/index.d.ts +2 -1
  44. package/dist/types/src/hooks/index.d.ts.map +1 -1
  45. package/dist/types/src/hooks/useTextModel.d.ts +2 -27
  46. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  47. package/dist/types/src/hooks/yjs.d.ts +2 -1
  48. package/dist/types/src/hooks/yjs.d.ts.map +1 -1
  49. package/dist/types/src/util.d.ts +2 -0
  50. package/dist/types/src/util.d.ts.map +1 -1
  51. package/package.json +31 -31
  52. package/src/components/TextEditor/MarkdownEditor.stories.tsx +64 -69
  53. package/src/components/TextEditor/TextEditor.tsx +85 -68
  54. package/src/components/TextEditor/comments.stories.tsx +111 -69
  55. package/src/extensions/autocomplete.ts +6 -3
  56. package/src/extensions/automerge/automerge.ts +23 -48
  57. package/src/extensions/automerge/defs.ts +10 -31
  58. package/src/extensions/automerge/index.ts +0 -1
  59. package/src/extensions/automerge/semaphore.ts +23 -20
  60. package/src/extensions/automerge/update-automerge.ts +2 -2
  61. package/src/extensions/awareness.ts +20 -2
  62. package/src/extensions/basic.ts +22 -17
  63. package/src/extensions/code.ts +15 -17
  64. package/src/extensions/comments.ts +235 -132
  65. package/src/extensions/cursor.ts +7 -3
  66. package/src/extensions/hr.ts +49 -50
  67. package/src/extensions/index.ts +1 -0
  68. package/src/extensions/link.ts +3 -3
  69. package/src/extensions/markdown/bundle.ts +18 -24
  70. package/src/extensions/mention.ts +9 -2
  71. package/src/extensions/outliner.ts +12 -0
  72. package/src/extensions/tasklist.ts +49 -33
  73. package/src/hooks/automerge.ts +2 -1
  74. package/src/hooks/defs.ts +47 -0
  75. package/src/hooks/index.ts +3 -1
  76. package/src/hooks/useTextModel.ts +2 -45
  77. package/src/hooks/yjs.ts +2 -1
  78. package/src/util.ts +26 -3
@@ -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]);
@@ -2,72 +2,71 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import {
6
- Decoration,
7
- type DecorationSet,
8
- EditorView,
9
- MatchDecorator,
10
- ViewPlugin,
11
- type ViewUpdate,
12
- WidgetType,
13
- } from '@codemirror/view';
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';
14
8
 
15
9
  import { getToken } from '../styles';
16
10
 
17
11
  // TODO(burdon): Reconcile with theme.
18
12
  const styles = EditorView.baseTheme({
19
13
  '& .cm-hr': {
20
- // TODO(burdon): ???
21
14
  // Note that block-level decorations should not have vertical margins,
22
- borderBottom: `1px solid ${getToken('extend.colors.neutral.200')}`,
15
+ borderTop: `1px solid ${getToken('extend.colors.neutral.200')}`,
23
16
  },
24
17
  });
25
18
 
26
19
  class HorizontalRuleWidget extends WidgetType {
27
- constructor(readonly _pos: number) {
28
- super();
29
- }
30
-
31
- override eq(other: this) {
32
- return this._pos === (other as any as HorizontalRuleWidget)._pos;
33
- }
34
-
35
- override toDOM(view: EditorView) {
36
- // TODO(burdon): Create <hr> element? Does this clash with markdown parser?
20
+ override toDOM() {
37
21
  const el = document.createElement('div');
38
22
  el.className = 'cm-hr';
39
23
  return el;
40
24
  }
41
25
  }
42
26
 
43
- const placeholderMatcher = new MatchDecorator({
44
- regexp: /^---$/g,
45
- decoration: (match, view, pos) =>
46
- Decoration.replace({
47
- widget: new HorizontalRuleWidget(pos),
48
- }),
49
- });
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;
50
58
 
51
- export const hr = () => [
52
- styles,
53
- ViewPlugin.fromClass(
54
- class {
55
- rules: DecorationSet;
56
- constructor(view: EditorView) {
57
- this.rules = placeholderMatcher.createDeco(view);
58
- }
59
+ constructor(view: EditorView) {
60
+ this.decorations = buildDecorations(view);
61
+ }
59
62
 
60
- update(update: ViewUpdate) {
61
- this.rules = placeholderMatcher.updateDeco(update, this.rules);
62
- }
63
- },
64
- {
65
- decorations: (instance) => instance.rules,
66
- // TODO(burdon): Is this really atomic (cursor can move into ---).
67
- provide: (plugin) =>
68
- EditorView.atomicRanges.of((view) => {
69
- return view.plugin(plugin)?.rules || Decoration.none;
70
- }),
71
- },
72
- ),
73
- ];
63
+ update(update: ViewUpdate) {
64
+ this.decorations = buildDecorations(update.view);
65
+ }
66
+ },
67
+ {
68
+ decorations: (v) => v.decorations,
69
+ },
70
+ ),
71
+ ];
72
+ };
@@ -16,6 +16,7 @@ export * from './link';
16
16
  export * from './listener';
17
17
  export * from './markdown';
18
18
  export * from './mention';
19
+ export * from './outliner';
19
20
  export * from './table';
20
21
  export * from './tasklist';
21
22
  export * from './typewriter';
@@ -6,13 +6,13 @@
6
6
  import { syntaxTree } from '@codemirror/language';
7
7
  import { type Extension, RangeSetBuilder } from '@codemirror/state';
8
8
  import {
9
+ hoverTooltip,
9
10
  Decoration,
11
+ type DecorationSet,
10
12
  type EditorView,
11
- WidgetType,
12
- hoverTooltip,
13
13
  ViewPlugin,
14
14
  type ViewUpdate,
15
- type DecorationSet,
15
+ WidgetType,
16
16
  } from '@codemirror/view';
17
17
  import { type SyntaxNode } from '@lezer/common';
18
18
 
@@ -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
+ };
@@ -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
+ };
@@ -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,29 @@ 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
+
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
+ };
@@ -8,7 +8,8 @@ import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
8
8
  import { isNotNullOrUndefined } from '@dxos/util';
9
9
 
10
10
  import { SpaceAwarenessProvider } from './awareness-provider';
11
- import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
11
+ import { type EditorModel, modelState } from './defs';
12
+ import { type UseTextModelProps } from './useTextModel';
12
13
  import { automerge, awareness, AwarenessProvider } from '../extensions';
13
14
  import { cursorColor } from '../styles';
14
15
 
@@ -0,0 +1,47 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Extension, StateField } from '@codemirror/state';
6
+ import type * as YP from 'y-protocols/awareness';
7
+
8
+ import { type DocAccessor } from '@dxos/echo-schema';
9
+ import type { YText, YXmlFragment } from '@dxos/text-model';
10
+
11
+ // Runtime data structure.
12
+ export type Range = {
13
+ from: number;
14
+ to: number;
15
+ };
16
+
17
+ // Persistent data structure.
18
+ export type Comment = {
19
+ id: string;
20
+ cursor?: string;
21
+ };
22
+
23
+ export type EditorModel = {
24
+ id: string;
25
+ text: () => string;
26
+ extension?: Extension;
27
+
28
+ // TODO(burdon): Remove.
29
+ content: string | YText | YXmlFragment | DocAccessor;
30
+
31
+ // TODO(burdon): Move into extension.
32
+ awareness?: YP.Awareness;
33
+
34
+ // TODO(burdon): Remove.
35
+ peer?: {
36
+ id: string;
37
+ name?: string;
38
+ };
39
+ };
40
+
41
+ /**
42
+ * State field makes the model available to other extensions.
43
+ */
44
+ export const modelState = StateField.define<EditorModel | undefined>({
45
+ create: () => undefined,
46
+ update: (model) => model,
47
+ });
@@ -4,5 +4,7 @@
4
4
 
5
5
  export * from './automerge';
6
6
  export * from './awareness-provider';
7
- export * from './useTextModel';
7
+ export * from './defs';
8
8
  export * from './yjs';
9
+
10
+ export * from './useTextModel';
@@ -2,56 +2,15 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { StateField, type Extension } from '@codemirror/state';
6
5
  import { useEffect, useState } from 'react';
7
- import type * as YP from 'y-protocols/awareness';
8
6
 
9
- import { isAutomergeObject, type DocAccessor, type Space, type TextObject } from '@dxos/react-client/echo';
7
+ import { isAutomergeObject, type Space, type TextObject } from '@dxos/react-client/echo';
10
8
  import { type Identity } from '@dxos/react-client/halo';
11
- import type { YText, YXmlFragment } from '@dxos/text-model';
12
9
 
13
10
  import { createAutomergeModel } from './automerge';
11
+ import { type EditorModel } from './defs';
14
12
  import { createYjsModel } from './yjs';
15
13
 
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
- };
54
-
55
14
  // TODO(burdon): Remove space/identity dependency. Define interface for the framework re content and presence.
56
15
  export type UseTextModelProps = {
57
16
  identity?: Identity | null;
@@ -59,8 +18,6 @@ export type UseTextModelProps = {
59
18
  text?: TextObject;
60
19
  };
61
20
 
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
21
  export const useTextModel = (props: UseTextModelProps): EditorModel | undefined => {
65
22
  const { identity, space, text } = props;
66
23
  const [model, setModel] = useState<EditorModel | undefined>();
package/src/hooks/yjs.ts CHANGED
@@ -14,7 +14,8 @@ import { type Space } from '@dxos/react-client/echo';
14
14
  import { type GossipMessage } from '@dxos/react-client/mesh';
15
15
  import type { YText } from '@dxos/text-model';
16
16
 
17
- import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
17
+ import { type EditorModel, modelState } from './defs';
18
+ import { type UseTextModelProps } from './useTextModel';
18
19
  import { yjs } from '../extensions';
19
20
 
20
21
  export const createYjsModel = ({ identity, space, text }: UseTextModelProps): EditorModel => {
package/src/util.ts CHANGED
@@ -2,17 +2,40 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ import type { Transaction } from '@codemirror/state';
6
+
5
7
  import { log } from '@dxos/log';
6
8
 
7
9
  /**
8
10
  * CodeMirror callbacks swallow errors so wrap handlers.
9
11
  */
10
- // TODO(burdon): Figure out how to systematize this.
12
+ // TODO(burdon): EditorView.exceptionSink should catch errors.
11
13
  export const callbackWrapper = <T extends Function>(fn: T): T =>
12
14
  ((...args: any[]) => {
13
15
  try {
14
16
  return fn(...args);
15
- } catch (error) {
16
- log.catch(error);
17
+ } catch (err) {
18
+ log.catch(err);
17
19
  }
18
20
  }) as unknown as T;
21
+
22
+ export const logChanges = (trs: readonly Transaction[]) => {
23
+ const changes = trs
24
+ .flatMap((tr) => {
25
+ if (tr.changes.empty) {
26
+ return undefined;
27
+ }
28
+
29
+ const changes: any[] = [];
30
+ tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) =>
31
+ changes.push(JSON.stringify({ fromA, toA, fromB, toB, inserted: inserted.toString() })),
32
+ );
33
+
34
+ return changes;
35
+ })
36
+ .filter(Boolean);
37
+
38
+ if (changes.length) {
39
+ console.log('changes', changes);
40
+ }
41
+ };