@dxos/react-ui-editor 0.3.11-main.d7d4c52 → 0.3.11-main.d8b8a39
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.
- package/dist/lib/browser/index.mjs +656 -680
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +13 -14
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +14 -6
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +7 -9
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/index.d.ts +0 -1
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -12
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts +1 -1
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +30 -17
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +3 -3
- package/dist/types/src/extensions/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/hr.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +1 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +2 -5
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/mention.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner.d.ts +4 -0
- package/dist/types/src/extensions/outliner.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +25 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +2 -27
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +33 -36
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +64 -69
- package/src/components/TextEditor/TextEditor.tsx +95 -70
- package/src/components/TextEditor/comments.stories.tsx +113 -71
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.ts +39 -45
- package/src/extensions/automerge/defs.ts +10 -31
- package/src/extensions/automerge/index.ts +0 -1
- package/src/extensions/automerge/semaphore.ts +23 -20
- package/src/extensions/automerge/update-automerge.ts +2 -2
- package/src/extensions/awareness.ts +15 -14
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/code.ts +15 -17
- package/src/extensions/comments.ts +239 -136
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/hr.ts +49 -50
- package/src/extensions/index.ts +1 -1
- package/src/extensions/link.ts +3 -3
- package/src/extensions/markdown/bundle.ts +18 -24
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/extensions/tasklist.ts +76 -84
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +3 -2
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +47 -56
- package/src/themes/default.ts +0 -8
- package/src/util.ts +26 -3
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/yjs/cursor.ts +0 -22
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -156
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
package/src/extensions/index.ts
CHANGED
|
@@ -16,7 +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';
|
|
22
|
-
export * from './yjs';
|
package/src/extensions/link.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
+
};
|
|
@@ -2,57 +2,53 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type DecorationSet,
|
|
12
|
-
type ViewUpdate,
|
|
13
|
-
} from '@codemirror/view';
|
|
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';
|
|
14
10
|
|
|
15
11
|
// TODO(burdon): Reconcile with theme.
|
|
16
12
|
const styles = EditorView.baseTheme({
|
|
17
|
-
'& .cm-task
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
'& .cm-task': {
|
|
14
|
+
color: getToken('extend.colors.blue.500'),
|
|
15
|
+
},
|
|
16
|
+
'& .cm-task-checkbox': {
|
|
17
|
+
marginLeft: '4px',
|
|
18
|
+
marginRight: '4px',
|
|
20
19
|
},
|
|
21
20
|
});
|
|
22
21
|
|
|
23
22
|
class CheckboxWidget extends WidgetType {
|
|
24
|
-
constructor(
|
|
25
|
-
private _checked: boolean,
|
|
26
|
-
private readonly _indent: number,
|
|
27
|
-
private readonly _onCheck?: (check: boolean) => void,
|
|
28
|
-
) {
|
|
23
|
+
constructor(private _checked: boolean) {
|
|
29
24
|
super();
|
|
30
25
|
}
|
|
31
26
|
|
|
32
27
|
override eq(other: this) {
|
|
33
|
-
return this._checked === other._checked
|
|
28
|
+
return this._checked === other._checked;
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
override toDOM(view: EditorView) {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
40
|
-
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
41
|
-
|
|
42
|
-
const input = wrap.appendChild(document.createElement('input'));
|
|
32
|
+
const input = document.createElement('input');
|
|
33
|
+
input.className = 'cm-task-checkbox';
|
|
43
34
|
input.type = 'checkbox';
|
|
44
35
|
input.checked = this._checked;
|
|
45
|
-
if (
|
|
36
|
+
if (view.state.readOnly) {
|
|
46
37
|
input.setAttribute('disabled', 'true');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
}
|
|
52
48
|
};
|
|
53
49
|
}
|
|
54
50
|
|
|
55
|
-
return
|
|
51
|
+
return input;
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
override ignoreEvent() {
|
|
@@ -60,60 +56,56 @@ class CheckboxWidget extends WidgetType {
|
|
|
60
56
|
}
|
|
61
57
|
}
|
|
62
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
|
+
|
|
63
87
|
export type TasklistOptions = {};
|
|
64
88
|
|
|
65
89
|
export const tasklist = (options: TasklistOptions = {}) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
88
|
-
selection: {
|
|
89
|
-
anchor: idx + 3,
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
},
|
|
93
|
-
),
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
const tasks = ViewPlugin.fromClass(
|
|
99
|
-
class {
|
|
100
|
-
tasks: DecorationSet;
|
|
101
|
-
constructor(view: EditorView) {
|
|
102
|
-
this.tasks = taskMatcher.createDeco(view);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
update(update: ViewUpdate) {
|
|
106
|
-
this.tasks = taskMatcher.updateDeco(update, this.tasks);
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
decorations: (value) => value.tasks,
|
|
111
|
-
provide: (plugin) =>
|
|
112
|
-
EditorView.atomicRanges.of((view) => {
|
|
113
|
-
return view.plugin(plugin)?.tasks || Decoration.none;
|
|
114
|
-
}),
|
|
115
|
-
},
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
return [tasks, styles];
|
|
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
|
+
];
|
|
119
111
|
};
|
|
@@ -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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
73
|
-
|
|
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
|
+
});
|
package/src/hooks/index.ts
CHANGED
|
@@ -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
|
+
};
|
|
@@ -2,55 +2,20 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
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 {
|
|
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
|
|
13
|
+
import { isNotNullOrUndefined } from '@dxos/util';
|
|
12
14
|
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
};
|
|
15
|
+
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
16
|
+
import { type EditorModel, modelState } from './defs';
|
|
17
|
+
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
18
|
+
import { cursorColor } from '../styles';
|
|
54
19
|
|
|
55
20
|
// TODO(burdon): Remove space/identity dependency. Define interface for the framework re content and presence.
|
|
56
21
|
export type UseTextModelProps = {
|
|
@@ -59,8 +24,6 @@ export type UseTextModelProps = {
|
|
|
59
24
|
text?: TextObject;
|
|
60
25
|
};
|
|
61
26
|
|
|
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
27
|
export const useTextModel = (props: UseTextModelProps): EditorModel | undefined => {
|
|
65
28
|
const { identity, space, text } = props;
|
|
66
29
|
const [model, setModel] = useState<EditorModel | undefined>();
|
|
@@ -68,13 +31,41 @@ export const useTextModel = (props: UseTextModelProps): EditorModel | undefined
|
|
|
68
31
|
return model;
|
|
69
32
|
};
|
|
70
33
|
|
|
71
|
-
const createModel = (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
34
|
+
const createModel = ({ space, identity, text }: UseTextModelProps) => {
|
|
35
|
+
invariant(isAutomergeObject(text));
|
|
36
|
+
const obj = text as any as AutomergeTextCompat;
|
|
37
|
+
const doc = getRawDoc(obj, [obj.field]);
|
|
38
|
+
|
|
39
|
+
const awarenessProvider =
|
|
40
|
+
space &&
|
|
41
|
+
new SpaceAwarenessProvider({
|
|
42
|
+
space,
|
|
43
|
+
channel: `automerge.awareness.${obj.id}`,
|
|
44
|
+
info: {
|
|
45
|
+
displayName: identity ? identity.profile?.displayName ?? generateName(identity.identityKey.toHex()) : undefined,
|
|
46
|
+
color: cursorColor.color,
|
|
47
|
+
lightColor: cursorColor.light,
|
|
48
|
+
},
|
|
49
|
+
peerId: identity?.identityKey.toHex() ?? 'Anonymous',
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const model: EditorModel = {
|
|
53
|
+
id: obj.id,
|
|
54
|
+
content: doc,
|
|
55
|
+
text: () => get(doc.handle.docSync(), doc.path),
|
|
56
|
+
extension: [
|
|
57
|
+
modelState.init(() => model),
|
|
58
|
+
automerge({ handle: doc.handle, path: doc.path }),
|
|
59
|
+
awarenessProvider && AwarenessProvider.of(awarenessProvider),
|
|
60
|
+
awareness(),
|
|
61
|
+
].filter(isNotNullOrUndefined),
|
|
62
|
+
peer: identity
|
|
63
|
+
? {
|
|
64
|
+
id: identity.identityKey.toHex(),
|
|
65
|
+
name: identity.profile?.displayName,
|
|
66
|
+
}
|
|
67
|
+
: undefined,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return model;
|
|
80
71
|
};
|
package/src/themes/default.ts
CHANGED