@dxos/react-ui-editor 0.3.11-main.870164f → 0.3.11-main.8a9dd1e
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 +1707 -1320
- 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 +9 -5
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +12 -17
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -6
- 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 -1
- 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/{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 -1
- 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 → extensions}/automerge/semaphore.d.ts +4 -4
- 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.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/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +6 -2
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- 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.map +1 -1
- package/dist/types/src/extensions/image.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +4 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/link.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/tasklist.d.ts +5 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- 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.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 +4 -8
- 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 +3 -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/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +24 -22
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +25 -23
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +69 -65
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/components/TextEditor/comments.stories.tsx +329 -0
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/extensions/autocomplete.ts +18 -17
- package/src/{hooks/automerge/plugin.ts → extensions/automerge/automerge.ts} +20 -58
- 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 → extensions}/automerge/semaphore.ts +14 -12
- package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
- package/src/extensions/awareness.ts +309 -0
- package/src/extensions/code.ts +14 -7
- package/src/extensions/comments.ts +95 -110
- package/src/extensions/cursor.ts +46 -0
- package/src/extensions/hr.ts +4 -5
- package/src/extensions/image.ts +40 -22
- package/src/extensions/index.ts +4 -1
- package/src/extensions/link.ts +86 -88
- package/src/extensions/markdown/highlight.ts +3 -12
- package/src/extensions/table.ts +10 -4
- package/src/extensions/tasklist.ts +53 -79
- package/src/extensions/yjs/cursor.ts +22 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/{hooks → extensions}/yjs/yjs.test.ts +2 -26
- 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 +20 -97
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +49 -19
- package/src/index.ts +3 -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 -15
- package/src/themes/default.ts +51 -78
- package/dist/types/src/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/dist/types/src/extensions/util.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/semaphore.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-automerge.d.ts +0 -6
- package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-codemirror.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/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -11
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/yjs/index.ts +0 -6
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/yjs/yjs.test.d.ts +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/dist/types/src/{extensions/util.d.ts → util.d.ts} +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{extensions/util.ts → util.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
13
|
export const updateAutomerge = (
|
|
15
|
-
field:
|
|
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,7 +16,7 @@ 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
21
|
export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
|
|
22
22
|
for (const patch of patches) {
|
|
@@ -56,6 +56,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
|
|
|
56
56
|
if (index == null) {
|
|
57
57
|
return [];
|
|
58
58
|
}
|
|
59
|
+
|
|
59
60
|
const text = patch.values.map((v) => (v ? v.toString() : '')).join('');
|
|
60
61
|
return [{ from: index, to: index, insert: text }];
|
|
61
62
|
};
|
|
@@ -65,6 +66,7 @@ const handleSplice = (target: Prop[], patch: SpliceTextPatch): Array<ChangeSpec>
|
|
|
65
66
|
if (index == null) {
|
|
66
67
|
return [];
|
|
67
68
|
}
|
|
69
|
+
|
|
68
70
|
return [{ from: index, insert: patch.value }];
|
|
69
71
|
};
|
|
70
72
|
|
|
@@ -73,6 +75,7 @@ const handleDel = (target: Prop[], patch: DelPatch): Array<ChangeSpec> => {
|
|
|
73
75
|
if (index == null) {
|
|
74
76
|
return [];
|
|
75
77
|
}
|
|
78
|
+
|
|
76
79
|
const length = patch.length || 1;
|
|
77
80
|
return [{ from: index, to: index + length }];
|
|
78
81
|
};
|
|
@@ -82,27 +85,32 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
|
|
|
82
85
|
if (index == null) {
|
|
83
86
|
return [];
|
|
84
87
|
}
|
|
88
|
+
|
|
85
89
|
const length = state.doc.length;
|
|
86
90
|
if (typeof patch.value !== 'string') {
|
|
87
91
|
return []; // TODO(dmaretskyi): How to handle non string values?
|
|
88
92
|
}
|
|
93
|
+
|
|
89
94
|
return [{ from: 0, to: length, insert: patch.value as any }];
|
|
90
95
|
};
|
|
91
96
|
|
|
92
|
-
// If the path of the patch is of the form [path, <index>] then we know this is
|
|
93
|
-
//
|
|
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.
|
|
94
99
|
const charPath = (textPath: Prop[], candidatePath: Prop[]): number | null => {
|
|
95
100
|
if (candidatePath.length !== textPath.length + 1) {
|
|
96
101
|
return null;
|
|
97
102
|
}
|
|
103
|
+
|
|
98
104
|
for (let i = 0; i < textPath.length; i++) {
|
|
99
105
|
if (textPath[i] !== candidatePath[i]) {
|
|
100
106
|
return null;
|
|
101
107
|
}
|
|
102
108
|
}
|
|
109
|
+
|
|
103
110
|
const index = candidatePath[candidatePath.length - 1];
|
|
104
111
|
if (typeof index === 'number') {
|
|
105
112
|
return index;
|
|
106
113
|
}
|
|
114
|
+
|
|
107
115
|
return null;
|
|
108
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
|
+
});
|
package/src/extensions/code.ts
CHANGED
|
@@ -11,24 +11,23 @@ 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
23
|
'& .cm-codeblock': {
|
|
25
|
-
fontFamily:
|
|
24
|
+
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
26
25
|
},
|
|
27
26
|
'&light [aria-readonly="true"] .cm-codeblock': {
|
|
28
|
-
background:
|
|
27
|
+
background: getToken('extend.colors.neutral.50'),
|
|
29
28
|
},
|
|
30
29
|
'&dark [aria-readonly="true"] .cm-codeblock': {
|
|
31
|
-
background:
|
|
30
|
+
background: getToken('extend.colors.neutral.850'),
|
|
32
31
|
},
|
|
33
32
|
'& [aria-readonly="true"] .cm-codeblock': {
|
|
34
33
|
display: 'block',
|
|
@@ -56,7 +55,7 @@ const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
|
56
55
|
export const code = () => {
|
|
57
56
|
const getDecorations = (view: EditorView) => {
|
|
58
57
|
const decorations: Range<Decoration>[] = [];
|
|
59
|
-
if (view.state.readOnly) {
|
|
58
|
+
if (!view.hasFocus || view.state.readOnly) {
|
|
60
59
|
const text = view.state.doc.sliceString(0);
|
|
61
60
|
const matches = text.matchAll(CODE_REGEX);
|
|
62
61
|
const blocks = view.viewportLineBlocks;
|
|
@@ -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
|
}
|