@dxos/react-ui-editor 0.3.10 → 0.3.11-main.0ac3ef5
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 +1645 -1066
- 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 +21 -13
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +15 -19
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -3
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/{hooks/automerge → components/TextEditor}/automerge.stories.d.ts +6 -2
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +26 -0
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/index.d.ts +0 -2
- package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
- package/dist/types/src/{hooks/yjs → components/TextEditor}/yjs.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +26 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +4 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/{hooks/automerge/PatchSemaphore.d.ts → extensions/automerge/semaphore.d.ts} +5 -5
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
- package/dist/types/src/extensions/awareness.d.ts +46 -0
- package/dist/types/src/extensions/awareness.d.ts.map +1 -0
- package/dist/types/src/extensions/basic.d.ts.map +1 -0
- package/dist/types/src/extensions/blast.d.ts.map +1 -0
- package/dist/types/src/extensions/code.d.ts.map +1 -0
- package/dist/types/src/extensions/comments.d.ts +50 -0
- package/dist/types/src/extensions/comments.d.ts.map +1 -0
- package/dist/types/src/extensions/cursor.d.ts +25 -0
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts +2 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/image.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/index.d.ts +6 -3
- package/dist/types/src/extensions/index.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/link.d.ts +1 -0
- package/dist/types/src/extensions/link.d.ts.map +1 -0
- package/dist/types/src/extensions/listener.d.ts +10 -0
- package/dist/types/src/extensions/listener.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/highlight.d.ts +0 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -0
- package/dist/types/src/extensions/table.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions/demo.d.ts → extensions/typewriter.d.ts} +2 -3
- package/dist/types/src/extensions/typewriter.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts +4 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/index.d.ts +2 -0
- package/dist/types/src/extensions/yjs/index.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts +4 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +2 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge.d.ts +3 -0
- package/dist/types/src/hooks/automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +28 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +15 -11
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/{yjs/space-provider.d.ts → yjs.d.ts} +8 -8
- package/dist/types/src/hooks/yjs.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/cursors.d.ts.map +1 -0
- package/dist/types/src/styles/index.d.ts +1 -0
- package/dist/types/src/styles/index.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/styles/tokens.d.ts +1 -0
- package/dist/types/src/styles/tokens.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts +2 -2
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/dist/types/src/{components/TextEditor/themes → themes}/default.d.ts +5 -13
- package/dist/types/src/themes/default.d.ts.map +1 -0
- package/dist/types/src/themes/index.d.ts.map +1 -0
- package/dist/types/src/util.d.ts +5 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +26 -20
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +140 -79
- package/src/components/TextEditor/TextEditor.stories.tsx +14 -35
- package/src/components/TextEditor/TextEditor.tsx +87 -79
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +67 -8
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/components/TextEditor/comments.stories.tsx +315 -0
- package/src/components/TextEditor/index.ts +0 -3
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/{components/TextEditor/extensions → extensions}/autocomplete.ts +18 -17
- package/src/extensions/automerge/automerge.ts +94 -0
- package/src/extensions/automerge/cursor.ts +52 -0
- package/src/extensions/automerge/defs.ts +62 -0
- package/src/extensions/automerge/index.ts +7 -0
- package/src/{hooks/automerge/PatchSemaphore.ts → extensions/automerge/semaphore.ts} +18 -15
- package/src/{hooks/automerge/translation/codemirror-to-automerge.ts → extensions/automerge/update-automerge.ts} +6 -7
- package/src/{hooks/automerge/translation/automerge-to-codemirror.ts → extensions/automerge/update-codemirror.ts} +14 -11
- package/src/extensions/awareness.ts +309 -0
- package/src/{components/TextEditor/extensions → extensions}/code.ts +25 -18
- package/src/extensions/comments.ts +325 -0
- package/src/extensions/cursor.ts +46 -0
- package/src/extensions/hr.ts +73 -0
- package/src/extensions/image.ts +87 -0
- package/src/{components/TextEditor/extensions → extensions}/index.ts +6 -3
- package/src/{components/TextEditor/extensions → extensions}/link.ts +116 -62
- package/src/extensions/listener.ts +35 -0
- package/src/{components/TextEditor/extensions → extensions}/markdown/highlight.ts +4 -16
- package/src/{components/TextEditor/extensions → extensions}/table.ts +23 -4
- package/src/{components/TextEditor/extensions → extensions}/tasklist.ts +3 -5
- package/src/{components/TextEditor/extensions/demo.ts → extensions/typewriter.ts} +2 -3
- package/src/extensions/yjs/cursor.ts +22 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/extensions/yjs/yjs.test.ts +35 -0
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +52 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useTextModel.ts +40 -79
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +49 -19
- package/src/index.ts +6 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/markdown.ts +0 -2
- package/src/styles/tokens.ts +3 -0
- package/src/testing/replicator.ts +10 -16
- package/src/themes/default.ts +275 -0
- package/src/util.ts +18 -0
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/code.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts +0 -18
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts +0 -10
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/highlight.d.ts +0 -24
- package/dist/types/src/components/TextEditor/extensions/highlight.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts +0 -9
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts +0 -12
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/themes/index.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/handle.d.ts +0 -9
- package/dist/types/src/hooks/automerge/handle.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/index.d.ts +0 -3
- package/dist/types/src/hooks/automerge/index.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/plugin.d.ts +0 -26
- package/dist/types/src/hooks/automerge/plugin.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts +0 -5
- package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts +0 -6
- package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/translation/index.d.ts +0 -3
- package/dist/types/src/hooks/automerge/translation/index.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/cursors.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/index.d.ts +0 -3
- package/dist/types/src/hooks/yjs/index.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/src/components/TextEditor/extensions/comments.ts +0 -173
- package/src/components/TextEditor/extensions/experimental.tsx +0 -114
- package/src/components/TextEditor/extensions/highlight.ts +0 -128
- package/src/components/TextEditor/extensions/image.ts +0 -67
- package/src/components/TextEditor/extensions/listener.ts +0 -23
- package/src/components/TextEditor/extensions/tooltip.tsx +0 -61
- package/src/components/TextEditor/themes/default.ts +0 -279
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/automerge/plugin.ts +0 -134
- package/src/hooks/automerge/translation/index.ts +0 -6
- package/src/hooks/yjs/index.ts +0 -6
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/autocomplete.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/basic.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/blast.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/code.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/image.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/bundle.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/index.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/mention.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/table.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/tasklist.d.ts +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/themes → themes}/index.d.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/basic.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/blast.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/markdown/bundle.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/markdown/index.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/mention.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{components/TextEditor/themes → themes}/index.ts +0 -0
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { type EditorState, type Text, type Transaction } from '@codemirror/state';
|
|
7
|
+
import { type EditorState, type StateField, type Text, type Transaction } from '@codemirror/state';
|
|
8
8
|
|
|
9
9
|
import { next as am, type Heads } from '@dxos/automerge/automerge';
|
|
10
10
|
|
|
11
|
-
import { type IDocHandle } from '
|
|
12
|
-
import { type Field } from '../plugin';
|
|
11
|
+
import { type IDocHandle, type Value } from './defs';
|
|
13
12
|
|
|
14
|
-
export const
|
|
15
|
-
field:
|
|
13
|
+
export const updateAutomerge = (
|
|
14
|
+
field: StateField<Value>,
|
|
16
15
|
handle: IDocHandle,
|
|
17
16
|
transactions: Transaction[],
|
|
18
17
|
state: EditorState,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
5
|
-
// TODO(burdon): Additional copyright?
|
|
6
|
-
|
|
7
7
|
import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
|
|
8
8
|
import { type EditorView } from '@codemirror/view';
|
|
9
9
|
|
|
@@ -16,14 +16,9 @@ import {
|
|
|
16
16
|
type SpliceTextPatch,
|
|
17
17
|
} from '@dxos/automerge/automerge';
|
|
18
18
|
|
|
19
|
-
import { reconcileAnnotationType } from '
|
|
19
|
+
import { reconcileAnnotationType } from './defs';
|
|
20
20
|
|
|
21
|
-
export const
|
|
22
|
-
view: EditorView,
|
|
23
|
-
selection: EditorSelection,
|
|
24
|
-
target: Prop[],
|
|
25
|
-
patches: Patch[],
|
|
26
|
-
) => {
|
|
21
|
+
export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
|
|
27
22
|
for (const patch of patches) {
|
|
28
23
|
const changeSpec = handlePatch(patch, target, view.state);
|
|
29
24
|
if (changeSpec != null) {
|
|
@@ -61,6 +56,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
|
|
|
61
56
|
if (index == null) {
|
|
62
57
|
return [];
|
|
63
58
|
}
|
|
59
|
+
|
|
64
60
|
const text = patch.values.map((v) => (v ? v.toString() : '')).join('');
|
|
65
61
|
return [{ from: index, to: index, insert: text }];
|
|
66
62
|
};
|
|
@@ -70,6 +66,7 @@ const handleSplice = (target: Prop[], patch: SpliceTextPatch): Array<ChangeSpec>
|
|
|
70
66
|
if (index == null) {
|
|
71
67
|
return [];
|
|
72
68
|
}
|
|
69
|
+
|
|
73
70
|
return [{ from: index, insert: patch.value }];
|
|
74
71
|
};
|
|
75
72
|
|
|
@@ -78,6 +75,7 @@ const handleDel = (target: Prop[], patch: DelPatch): Array<ChangeSpec> => {
|
|
|
78
75
|
if (index == null) {
|
|
79
76
|
return [];
|
|
80
77
|
}
|
|
78
|
+
|
|
81
79
|
const length = patch.length || 1;
|
|
82
80
|
return [{ from: index, to: index + length }];
|
|
83
81
|
};
|
|
@@ -87,27 +85,32 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
|
|
|
87
85
|
if (index == null) {
|
|
88
86
|
return [];
|
|
89
87
|
}
|
|
88
|
+
|
|
90
89
|
const length = state.doc.length;
|
|
91
90
|
if (typeof patch.value !== 'string') {
|
|
92
91
|
return []; // TODO(dmaretskyi): How to handle non string values?
|
|
93
92
|
}
|
|
93
|
+
|
|
94
94
|
return [{ from: 0, to: length, insert: patch.value as any }];
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
// If the path of the patch is of the form [path, <index>] then we know this is
|
|
98
|
-
//
|
|
97
|
+
// If the path of the patch is of the form [path, <index>] then we know this is a path to a character
|
|
98
|
+
// within the sequence given by path.
|
|
99
99
|
const charPath = (textPath: Prop[], candidatePath: Prop[]): number | null => {
|
|
100
100
|
if (candidatePath.length !== textPath.length + 1) {
|
|
101
101
|
return null;
|
|
102
102
|
}
|
|
103
|
+
|
|
103
104
|
for (let i = 0; i < textPath.length; i++) {
|
|
104
105
|
if (textPath[i] !== candidatePath[i]) {
|
|
105
106
|
return null;
|
|
106
107
|
}
|
|
107
108
|
}
|
|
109
|
+
|
|
108
110
|
const index = candidatePath[candidatePath.length - 1];
|
|
109
111
|
if (typeof index === 'number') {
|
|
110
112
|
return index;
|
|
111
113
|
}
|
|
114
|
+
|
|
112
115
|
return null;
|
|
113
116
|
};
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Annotation, Facet, type Extension, RangeSet, type Range } from '@codemirror/state';
|
|
6
|
+
import {
|
|
7
|
+
Decoration,
|
|
8
|
+
type DecorationSet,
|
|
9
|
+
EditorView,
|
|
10
|
+
type PluginValue,
|
|
11
|
+
ViewPlugin,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
WidgetType,
|
|
14
|
+
} from '@codemirror/view';
|
|
15
|
+
|
|
16
|
+
import { Event } from '@dxos/async';
|
|
17
|
+
import { Context } from '@dxos/context';
|
|
18
|
+
|
|
19
|
+
import { Cursor, type CursorConverter } from './cursor';
|
|
20
|
+
|
|
21
|
+
export interface AwarenessProvider {
|
|
22
|
+
remoteStateChange: Event<void>;
|
|
23
|
+
|
|
24
|
+
open(): void;
|
|
25
|
+
close(): void;
|
|
26
|
+
|
|
27
|
+
update(position: AwarenessPosition | undefined): void;
|
|
28
|
+
getRemoteStates(): AwarenessState[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const dummyProvider: AwarenessProvider = {
|
|
32
|
+
remoteStateChange: new Event(),
|
|
33
|
+
|
|
34
|
+
open: () => {},
|
|
35
|
+
close: () => {},
|
|
36
|
+
|
|
37
|
+
update: () => {},
|
|
38
|
+
getRemoteStates: () => [],
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const AwarenessProvider = Facet.define<AwarenessProvider, AwarenessProvider>({
|
|
42
|
+
combine: (providers) => providers[0] ?? dummyProvider,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// TODO(dmaretskyi): Specify the users that actually changed. Currently, we recalculate positions for every user.
|
|
46
|
+
const RemoteSelectionChangedAnnotation = Annotation.define();
|
|
47
|
+
|
|
48
|
+
export type AwarenessPosition = {
|
|
49
|
+
anchor?: string;
|
|
50
|
+
head?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type AwarenessInfo = {
|
|
54
|
+
displayName?: string;
|
|
55
|
+
// TODO(burdon): Rename light/dark.
|
|
56
|
+
color?: string;
|
|
57
|
+
lightColor?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type AwarenessState = {
|
|
61
|
+
position?: AwarenessPosition;
|
|
62
|
+
peerId: string;
|
|
63
|
+
info: AwarenessInfo;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extension provides presence information about other peers.
|
|
68
|
+
*/
|
|
69
|
+
// TODO(burdon): Why provide default?
|
|
70
|
+
export const awareness = (provider = dummyProvider): Extension => {
|
|
71
|
+
return [
|
|
72
|
+
AwarenessProvider.of(provider),
|
|
73
|
+
ViewPlugin.fromClass(RemoteSelectionsDecorator, {
|
|
74
|
+
decorations: (value) => value.decorations,
|
|
75
|
+
}),
|
|
76
|
+
styles,
|
|
77
|
+
];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Generates selection decorations from remote peers.
|
|
82
|
+
*/
|
|
83
|
+
export class RemoteSelectionsDecorator implements PluginValue {
|
|
84
|
+
public decorations: DecorationSet = RangeSet.of([]);
|
|
85
|
+
|
|
86
|
+
private readonly _ctx = new Context();
|
|
87
|
+
|
|
88
|
+
private _cursorConverter: CursorConverter;
|
|
89
|
+
private _provider: AwarenessProvider;
|
|
90
|
+
private _lastAnchor?: number = undefined;
|
|
91
|
+
private _lastHead?: number = undefined;
|
|
92
|
+
|
|
93
|
+
constructor(view: EditorView) {
|
|
94
|
+
this._cursorConverter = view.state.facet(Cursor.converter);
|
|
95
|
+
this._provider = view.state.facet(AwarenessProvider);
|
|
96
|
+
this._provider.open();
|
|
97
|
+
this._provider.remoteStateChange.on(this._ctx, () => {
|
|
98
|
+
view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
destroy() {
|
|
103
|
+
void this._ctx.dispose();
|
|
104
|
+
this._provider.close();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
update(update: ViewUpdate) {
|
|
108
|
+
this._updateLocalSelection(update);
|
|
109
|
+
this._updateRemoteSelections(update);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private _updateLocalSelection(update: ViewUpdate) {
|
|
113
|
+
const hasFocus = update.view.hasFocus && update.view.dom.ownerDocument.hasFocus();
|
|
114
|
+
const { anchor = undefined, head = undefined } = hasFocus ? update.state.selection.main : {};
|
|
115
|
+
if (this._lastAnchor === anchor && this._lastHead === head) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this._lastAnchor = anchor;
|
|
120
|
+
this._lastHead = head;
|
|
121
|
+
|
|
122
|
+
this._provider.update(
|
|
123
|
+
anchor !== undefined && head !== undefined
|
|
124
|
+
? {
|
|
125
|
+
anchor: this._cursorConverter.toCursor(anchor),
|
|
126
|
+
head: this._cursorConverter.toCursor(head),
|
|
127
|
+
}
|
|
128
|
+
: undefined,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private _updateRemoteSelections(update: ViewUpdate) {
|
|
133
|
+
const decorations: Range<Decoration>[] = [];
|
|
134
|
+
const awarenessStates = this._provider.getRemoteStates();
|
|
135
|
+
for (const state of awarenessStates) {
|
|
136
|
+
const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
|
|
137
|
+
const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
|
|
138
|
+
if (anchor == null || head == null) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const start = Math.min(Math.min(anchor, head), update.view.state.doc.length);
|
|
143
|
+
const end = Math.min(Math.max(anchor, head), update.view.state.doc.length);
|
|
144
|
+
|
|
145
|
+
const startLine = update.view.state.doc.lineAt(start);
|
|
146
|
+
const endLine = update.view.state.doc.lineAt(end);
|
|
147
|
+
|
|
148
|
+
// TODO(burdon): Factor out styles.
|
|
149
|
+
const color = state.info.color ?? '#30bced';
|
|
150
|
+
const lightColor = state.info.lightColor ?? color + '33';
|
|
151
|
+
|
|
152
|
+
if (startLine.number === endLine.number) {
|
|
153
|
+
// Selected content in a single line.
|
|
154
|
+
decorations.push({
|
|
155
|
+
from: start,
|
|
156
|
+
to: end,
|
|
157
|
+
value: Decoration.mark({
|
|
158
|
+
attributes: { style: `background-color: ${lightColor}` },
|
|
159
|
+
class: 'cm-collab-selection',
|
|
160
|
+
}),
|
|
161
|
+
});
|
|
162
|
+
} else {
|
|
163
|
+
// Selected content in multiple lines; first, render text-selection in the first line.
|
|
164
|
+
decorations.push({
|
|
165
|
+
from: start,
|
|
166
|
+
to: startLine.from + startLine.length,
|
|
167
|
+
value: Decoration.mark({
|
|
168
|
+
attributes: { style: `background-color: ${color}` },
|
|
169
|
+
class: 'cm-collab-selection',
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Render text-selection in the last line.
|
|
174
|
+
decorations.push({
|
|
175
|
+
from: endLine.from,
|
|
176
|
+
to: end,
|
|
177
|
+
value: Decoration.mark({
|
|
178
|
+
attributes: { style: `background-color: ${color}` },
|
|
179
|
+
class: 'cm-collab-selection',
|
|
180
|
+
}),
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
for (let i = startLine.number + 1; i < endLine.number; i++) {
|
|
184
|
+
const linePos = update.view.state.doc.line(i).from;
|
|
185
|
+
decorations.push({
|
|
186
|
+
from: linePos,
|
|
187
|
+
to: linePos,
|
|
188
|
+
value: Decoration.line({
|
|
189
|
+
attributes: { style: `background-color: ${color}`, class: 'cm-collab-selectionLine' },
|
|
190
|
+
}),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
decorations.push({
|
|
196
|
+
from: head,
|
|
197
|
+
to: head,
|
|
198
|
+
value: Decoration.widget({
|
|
199
|
+
side: head - anchor > 0 ? -1 : 1, // The local cursor should be rendered outside the remote selection.
|
|
200
|
+
block: false,
|
|
201
|
+
widget: new RemoteCaretWidget(state.info.displayName ?? 'Anonymous', color),
|
|
202
|
+
}),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.decorations = Decoration.set(decorations, true);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
class RemoteCaretWidget extends WidgetType {
|
|
211
|
+
constructor(private readonly _name: string, private readonly _color: string) {
|
|
212
|
+
super();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
override toDOM(): HTMLElement {
|
|
216
|
+
const span = document.createElement('span');
|
|
217
|
+
span.className = 'cm-collab-selectionCaret';
|
|
218
|
+
span.style.backgroundColor = this._color;
|
|
219
|
+
span.style.borderColor = this._color;
|
|
220
|
+
|
|
221
|
+
const dot = document.createElement('div');
|
|
222
|
+
dot.className = 'cm-collab-selectionCaretDot';
|
|
223
|
+
|
|
224
|
+
const info = document.createElement('div');
|
|
225
|
+
info.className = 'cm-collab-selectionInfo';
|
|
226
|
+
info.innerText = this._name;
|
|
227
|
+
|
|
228
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
229
|
+
span.appendChild(dot);
|
|
230
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
231
|
+
span.appendChild(info);
|
|
232
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
233
|
+
|
|
234
|
+
return span;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
override updateDOM() {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
override eq(widget: this) {
|
|
242
|
+
return widget._color === this._color;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
override get estimatedHeight() {
|
|
246
|
+
return -1;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
override ignoreEvent() {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const styles = EditorView.baseTheme({
|
|
255
|
+
'.cm-collab-selection': {},
|
|
256
|
+
'.cm-collab-selectionLine': {
|
|
257
|
+
padding: 0,
|
|
258
|
+
margin: '0px 2px 0px 4px',
|
|
259
|
+
},
|
|
260
|
+
'.cm-collab-selectionCaret': {
|
|
261
|
+
position: 'relative',
|
|
262
|
+
borderLeft: '1px solid black',
|
|
263
|
+
borderRight: '1px solid black',
|
|
264
|
+
marginLeft: '-1px',
|
|
265
|
+
marginRight: '-1px',
|
|
266
|
+
boxSizing: 'border-box',
|
|
267
|
+
display: 'inline',
|
|
268
|
+
},
|
|
269
|
+
'.cm-collab-selectionCaretDot': {
|
|
270
|
+
borderRadius: '50%',
|
|
271
|
+
position: 'absolute',
|
|
272
|
+
width: '.4em',
|
|
273
|
+
height: '.4em',
|
|
274
|
+
top: '-.2em',
|
|
275
|
+
left: '-.2em',
|
|
276
|
+
backgroundColor: 'inherit',
|
|
277
|
+
transition: 'transform .3s ease-in-out',
|
|
278
|
+
boxSizing: 'border-box',
|
|
279
|
+
},
|
|
280
|
+
'.cm-collab-selectionCaret:hover > .cm-collab-selectionCaretDot': {
|
|
281
|
+
transformOrigin: 'bottom center',
|
|
282
|
+
transform: 'scale(0)',
|
|
283
|
+
},
|
|
284
|
+
'.cm-collab-selectionInfo': {
|
|
285
|
+
position: 'absolute',
|
|
286
|
+
top: '-1.05em',
|
|
287
|
+
left: '-1px',
|
|
288
|
+
fontSize: '.75em',
|
|
289
|
+
fontFamily: 'serif',
|
|
290
|
+
fontStyle: 'normal',
|
|
291
|
+
fontWeight: 'normal',
|
|
292
|
+
lineHeight: 'normal',
|
|
293
|
+
userSelect: 'none',
|
|
294
|
+
color: 'white',
|
|
295
|
+
paddingLeft: '2px',
|
|
296
|
+
paddingRight: '2px',
|
|
297
|
+
zIndex: 101,
|
|
298
|
+
transition: 'opacity .3s ease-in-out',
|
|
299
|
+
backgroundColor: 'inherit',
|
|
300
|
+
// these should be separate
|
|
301
|
+
opacity: 0,
|
|
302
|
+
transitionDelay: '0s',
|
|
303
|
+
whiteSpace: 'nowrap',
|
|
304
|
+
},
|
|
305
|
+
'.cm-collab-selectionCaret:hover > .cm-collab-selectionInfo': {
|
|
306
|
+
opacity: 1,
|
|
307
|
+
transitionDelay: '0s',
|
|
308
|
+
},
|
|
309
|
+
});
|
|
@@ -11,38 +11,41 @@ import {
|
|
|
11
11
|
Decoration,
|
|
12
12
|
type BlockInfo,
|
|
13
13
|
} from '@codemirror/view';
|
|
14
|
-
import get from 'lodash.get';
|
|
15
14
|
|
|
16
15
|
import { mx } from '@dxos/react-ui-theme';
|
|
17
16
|
|
|
18
|
-
import {
|
|
17
|
+
import { getToken } from '../styles';
|
|
19
18
|
|
|
20
19
|
const CODE_REGEX = /```[\s\S]*?```/gs;
|
|
21
20
|
|
|
22
21
|
// TODO(burdon): Reconcile with theme.
|
|
23
22
|
const styles = EditorView.baseTheme({
|
|
24
|
-
'& .cm-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
'& .cm-codeblock': {
|
|
24
|
+
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
25
|
+
},
|
|
26
|
+
'&light [aria-readonly="true"] .cm-codeblock': {
|
|
27
|
+
background: getToken('extend.colors.neutral.50'),
|
|
27
28
|
},
|
|
28
|
-
'&
|
|
29
|
-
background:
|
|
29
|
+
'&dark [aria-readonly="true"] .cm-codeblock': {
|
|
30
|
+
background: getToken('extend.colors.neutral.850'),
|
|
30
31
|
},
|
|
31
|
-
'&
|
|
32
|
-
|
|
32
|
+
'& [aria-readonly="true"] .cm-codeblock': {
|
|
33
|
+
display: 'block',
|
|
34
|
+
paddingInline: '4px !important',
|
|
33
35
|
},
|
|
34
|
-
'& .cm-
|
|
36
|
+
'& [aria-readonly="true"] .cm-codeblock-first': {
|
|
35
37
|
paddingTop: '4px !important',
|
|
36
38
|
borderTopLeftRadius: '4px',
|
|
37
39
|
borderTopRightRadius: '4px',
|
|
38
40
|
},
|
|
39
|
-
'& .cm-
|
|
41
|
+
'& [aria-readonly="true"] .cm-codeblock-last': {
|
|
40
42
|
paddingBottom: '4px !important',
|
|
41
43
|
borderBottomLeftRadius: '4px',
|
|
42
44
|
borderBottomRightRadius: '4px',
|
|
43
45
|
},
|
|
44
46
|
});
|
|
45
47
|
|
|
48
|
+
// TODO(burdon): Start from EditorView.lineBlockAt(pos).
|
|
46
49
|
const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
47
50
|
const start = lines.findIndex((line) => line.from >= from);
|
|
48
51
|
const end = lines.findIndex((line) => line.to >= to);
|
|
@@ -52,7 +55,7 @@ const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
|
52
55
|
export const code = () => {
|
|
53
56
|
const getDecorations = (view: EditorView) => {
|
|
54
57
|
const decorations: Range<Decoration>[] = [];
|
|
55
|
-
if (view.state.readOnly) {
|
|
58
|
+
if (!view.hasFocus || view.state.readOnly) {
|
|
56
59
|
const text = view.state.doc.sliceString(0);
|
|
57
60
|
const matches = text.matchAll(CODE_REGEX);
|
|
58
61
|
const blocks = view.viewportLineBlocks;
|
|
@@ -62,11 +65,7 @@ export const code = () => {
|
|
|
62
65
|
const block = blocks[i];
|
|
63
66
|
decorations.push(
|
|
64
67
|
Decoration.line({
|
|
65
|
-
class: mx(
|
|
66
|
-
'cm-code-block',
|
|
67
|
-
i === range[0] && 'cm-code-block-first',
|
|
68
|
-
i === range[1] && 'cm-code-block-last',
|
|
69
|
-
),
|
|
68
|
+
class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
|
|
70
69
|
}).range(block.from),
|
|
71
70
|
);
|
|
72
71
|
}
|
|
@@ -79,13 +78,21 @@ export const code = () => {
|
|
|
79
78
|
return [
|
|
80
79
|
ViewPlugin.fromClass(
|
|
81
80
|
class {
|
|
81
|
+
hasFocus = false;
|
|
82
82
|
decorations: DecorationSet;
|
|
83
83
|
constructor(view: EditorView) {
|
|
84
84
|
this.decorations = getDecorations(view);
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
update(update: ViewUpdate) {
|
|
88
|
-
if (
|
|
88
|
+
if (
|
|
89
|
+
// TODO(burdon): Generalize for other extensions.
|
|
90
|
+
this.hasFocus !== update.view.hasFocus ||
|
|
91
|
+
update.startState.readOnly !== update.view.state.readOnly ||
|
|
92
|
+
update.docChanged ||
|
|
93
|
+
update.viewportChanged
|
|
94
|
+
) {
|
|
95
|
+
this.hasFocus = update.view.hasFocus;
|
|
89
96
|
this.decorations = getDecorations(update.view);
|
|
90
97
|
}
|
|
91
98
|
}
|