@dxos/react-ui-editor 0.3.11-main.5a4849d → 0.3.11-main.5cbcf4e
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 +2506 -1566
- 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 +16 -14
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -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/hooks.stories.d.ts +23 -0
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +25 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +23 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.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 +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.test.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 +24 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +3 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/semaphore.d.ts +15 -0
- 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/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +41 -20
- 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/index.d.ts +4 -7
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts +13 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
- package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts +8 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
- 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/hooks/awareness-provider.d.ts +31 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- 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 +5 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useEditorView.d.ts +17 -0
- package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +14 -31
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- 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 +2 -2
- 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/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +7 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +48 -36
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +84 -91
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +135 -95
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +65 -6
- package/src/components/TextEditor/hooks.stories.tsx +106 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +90 -0
- package/src/components/Toolbar/Toolbar.tsx +166 -0
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/autocomplete.ts +23 -19
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +103 -0
- package/src/extensions/automerge/cursor.ts +52 -0
- package/src/extensions/automerge/defs.ts +41 -0
- package/src/extensions/automerge/index.ts +6 -0
- package/src/extensions/automerge/semaphore.ts +81 -0
- package/src/extensions/automerge/update-automerge.ts +49 -0
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +17 -9
- package/src/extensions/awareness.ts +313 -0
- package/src/extensions/basic.ts +17 -10
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +336 -255
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/index.ts +4 -7
- package/src/extensions/markdown/bundle.ts +23 -28
- package/src/extensions/markdown/code.ts +150 -0
- package/src/extensions/markdown/formatting.ts +188 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +11 -26
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/image.ts +87 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +90 -83
- package/src/extensions/{table.ts → markdown/table.ts} +10 -4
- package/src/extensions/markdown/tasklist.ts +110 -0
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/hooks/awareness-provider.ts +124 -0
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +6 -1
- package/src/hooks/useActionHandler.ts +62 -0
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +153 -0
- package/src/hooks/useTextModel.ts +48 -115
- package/src/index.ts +3 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/markdown.ts +9 -12
- package/src/styles/tokens.ts +3 -0
- package/src/themes/default.ts +59 -83
- package/src/util.ts +41 -0
- 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/extensions/code.d.ts.map +0 -1
- package/dist/types/src/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/extensions/image.d.ts.map +0 -1
- package/dist/types/src/extensions/link.d.ts.map +0 -1
- 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/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/util.d.ts +0 -5
- 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 +0 -19
- 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 +0 -27
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts +0 -12
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/hooks/yjs/yjs.test.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 -114
- package/src/extensions/code.ts +0 -99
- package/src/extensions/hr.ts +0 -74
- package/src/extensions/image.ts +0 -69
- package/src/extensions/mermaid.ts +0 -11
- package/src/extensions/tasklist.ts +0 -121
- package/src/extensions/util.ts +0 -18
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/automerge/plugin.ts +0 -132
- package/src/hooks/automerge/semaphore.ts +0 -78
- package/src/hooks/automerge/update-automerge.ts +0 -44
- package/src/hooks/yjs/index.ts +0 -6
- package/src/hooks/yjs/space-provider.ts +0 -126
- package/src/hooks/yjs/yjs.stories.tsx +0 -56
- package/src/hooks/yjs/yjs.test.ts +0 -59
- 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 -189
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
- /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
- /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
- /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
- /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
- /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
import { type StateField } from '@codemirror/state';
|
|
8
|
+
import { type EditorView } from '@codemirror/view';
|
|
9
|
+
|
|
10
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
getLastHeads,
|
|
14
|
+
getPath,
|
|
15
|
+
isReconcile,
|
|
16
|
+
reconcileAnnotation,
|
|
17
|
+
updateHeads,
|
|
18
|
+
type IDocHandle,
|
|
19
|
+
type State,
|
|
20
|
+
} from './defs';
|
|
21
|
+
import { updateAutomerge } from './update-automerge';
|
|
22
|
+
import { updateCodeMirror } from './update-codemirror';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Implements three-way merge (on each mutation).
|
|
26
|
+
*/
|
|
27
|
+
export class PatchSemaphore {
|
|
28
|
+
private _inReconcile = false;
|
|
29
|
+
|
|
30
|
+
// prettier-ignore
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly _handle: IDocHandle,
|
|
33
|
+
private readonly _state: StateField<State>
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
reconcile(view: EditorView) {
|
|
37
|
+
if (!this._inReconcile) {
|
|
38
|
+
this._inReconcile = true;
|
|
39
|
+
this.doReconcile(view);
|
|
40
|
+
this._inReconcile = false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private doReconcile(view: EditorView) {
|
|
45
|
+
const path = getPath(view.state, this._state);
|
|
46
|
+
|
|
47
|
+
// Get the heads before the unreconciled transactions are applied.
|
|
48
|
+
const oldHeads = getLastHeads(view.state, this._state);
|
|
49
|
+
let selection = view.state.selection;
|
|
50
|
+
|
|
51
|
+
// First undo all the unreconciled transactions.
|
|
52
|
+
const transactions = view.state.field(this._state).unreconciledTransactions.filter((tx) => !isReconcile(tx));
|
|
53
|
+
const toInvert = transactions.slice().reverse();
|
|
54
|
+
for (const tr of toInvert) {
|
|
55
|
+
const inverted = tr.changes.invert(tr.startState.doc);
|
|
56
|
+
selection = selection.map(inverted);
|
|
57
|
+
view.dispatch({
|
|
58
|
+
changes: inverted,
|
|
59
|
+
annotations: reconcileAnnotation.of(true),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Apply the unreconciled transactions to the document.
|
|
64
|
+
// NOTE: null and undefined each come from automerge and repo respectively.
|
|
65
|
+
let newHeads = updateAutomerge(this._state, this._handle, transactions, view.state);
|
|
66
|
+
if (newHeads === null || newHeads === undefined) {
|
|
67
|
+
// TODO(alexjg): this is the call that's resetting the editor state on click.
|
|
68
|
+
newHeads = A.getHeads(this._handle.docSync()!);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
72
|
+
const diff = A.equals(oldHeads, newHeads) ? [] : A.diff(this._handle.docSync()!, oldHeads, newHeads);
|
|
73
|
+
updateCodeMirror(view, selection, path, diff);
|
|
74
|
+
|
|
75
|
+
// Update automerge state.
|
|
76
|
+
view.dispatch({
|
|
77
|
+
effects: updateHeads(newHeads),
|
|
78
|
+
annotations: reconcileAnnotation.of(false),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
import { type EditorState, type StateField, type Transaction, type Text } from '@codemirror/state';
|
|
8
|
+
|
|
9
|
+
import { next as A, type Heads } from '@dxos/automerge/automerge';
|
|
10
|
+
|
|
11
|
+
import { type IDocHandle, type State } from './defs';
|
|
12
|
+
|
|
13
|
+
export const updateAutomerge = (
|
|
14
|
+
field: StateField<State>,
|
|
15
|
+
handle: IDocHandle,
|
|
16
|
+
transactions: Transaction[],
|
|
17
|
+
state: EditorState, // TODO(burdon): Just pass in the state field value?
|
|
18
|
+
): Heads | undefined => {
|
|
19
|
+
const { lastHeads, path } = state.field(field);
|
|
20
|
+
|
|
21
|
+
// We don't want to call `automerge.updateAt` if there are no changes.
|
|
22
|
+
// Otherwise, later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
|
|
23
|
+
let hasChanges = false;
|
|
24
|
+
for (const tr of transactions) {
|
|
25
|
+
tr.changes.iterChanges(() => {
|
|
26
|
+
hasChanges = true;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!hasChanges) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const newHeads = handle.changeAt(lastHeads, (doc: A.Doc<unknown>) => {
|
|
35
|
+
const invertedTransactions: { from: number; del: number; insert: Text }[] = [];
|
|
36
|
+
for (const tr of transactions) {
|
|
37
|
+
tr.changes.iterChanges((fromA, toA, _fromB, _toB, insert) => {
|
|
38
|
+
invertedTransactions.push({ from: fromA, del: toA - fromA, insert });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// TODO(burdon): Hack to apply in reverse order to properly apply range.
|
|
43
|
+
invertedTransactions.reverse().forEach(({ from, del, insert }) => {
|
|
44
|
+
A.splice(doc, path.slice(), from, del, insert.toString());
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return newHeads ?? undefined;
|
|
49
|
+
};
|
|
@@ -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 {
|
|
19
|
+
import { reconcileAnnotation } from './defs';
|
|
20
20
|
|
|
21
21
|
export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
|
|
22
22
|
for (const patch of patches) {
|
|
@@ -26,14 +26,14 @@ export const updateCodeMirror = (view: EditorView, selection: EditorSelection, t
|
|
|
26
26
|
selection = selection.map(changeSet, 1);
|
|
27
27
|
view.dispatch({
|
|
28
28
|
changes: changeSet,
|
|
29
|
-
annotations:
|
|
29
|
+
annotations: reconcileAnnotation.of(false),
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
view.dispatch({
|
|
35
35
|
selection,
|
|
36
|
-
annotations:
|
|
36
|
+
annotations: reconcileAnnotation.of(false),
|
|
37
37
|
});
|
|
38
38
|
};
|
|
39
39
|
|
|
@@ -56,7 +56,8 @@ 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((value) => (value ? value.toString() : '')).join('');
|
|
60
61
|
return [{ from: index, to: index, insert: text }];
|
|
61
62
|
};
|
|
62
63
|
|
|
@@ -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
|
-
return []; // TODO(dmaretskyi): How to handle non
|
|
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,313 @@
|
|
|
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
|
+
getRemoteStates(): AwarenessState[];
|
|
28
|
+
update(position: AwarenessPosition | undefined): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const dummyProvider: AwarenessProvider = {
|
|
32
|
+
remoteStateChange: new Event(),
|
|
33
|
+
|
|
34
|
+
open: () => {},
|
|
35
|
+
close: () => {},
|
|
36
|
+
|
|
37
|
+
getRemoteStates: () => [],
|
|
38
|
+
update: () => {},
|
|
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
|
+
export const awareness = (provider = dummyProvider): Extension => {
|
|
70
|
+
return [
|
|
71
|
+
awarenessProvider.of(provider),
|
|
72
|
+
ViewPlugin.fromClass(RemoteSelectionsDecorator, {
|
|
73
|
+
decorations: (value) => value.decorations,
|
|
74
|
+
}),
|
|
75
|
+
styles,
|
|
76
|
+
];
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Generates selection decorations from remote peers.
|
|
81
|
+
*/
|
|
82
|
+
export class RemoteSelectionsDecorator implements PluginValue {
|
|
83
|
+
public decorations: DecorationSet = RangeSet.of([]);
|
|
84
|
+
|
|
85
|
+
private readonly _ctx = new Context();
|
|
86
|
+
|
|
87
|
+
private _cursorConverter: CursorConverter;
|
|
88
|
+
private _provider: AwarenessProvider;
|
|
89
|
+
private _lastAnchor?: number = undefined;
|
|
90
|
+
private _lastHead?: number = undefined;
|
|
91
|
+
|
|
92
|
+
constructor(view: EditorView) {
|
|
93
|
+
this._cursorConverter = view.state.facet(Cursor.converter);
|
|
94
|
+
this._provider = view.state.facet(awarenessProvider);
|
|
95
|
+
this._provider.open();
|
|
96
|
+
this._provider.remoteStateChange.on(this._ctx, () => {
|
|
97
|
+
view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
destroy() {
|
|
102
|
+
void this._ctx.dispose();
|
|
103
|
+
this._provider.close();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
update(update: ViewUpdate) {
|
|
107
|
+
this._updateLocalSelection(update);
|
|
108
|
+
this._updateRemoteSelections(update);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private _updateLocalSelection(update: ViewUpdate) {
|
|
112
|
+
const hasFocus = update.view.hasFocus && update.view.dom.ownerDocument.hasFocus();
|
|
113
|
+
const { anchor = undefined, head = undefined } = hasFocus ? update.state.selection.main : {};
|
|
114
|
+
if (this._lastAnchor === anchor && this._lastHead === head) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this._lastAnchor = anchor;
|
|
119
|
+
this._lastHead = head;
|
|
120
|
+
|
|
121
|
+
this._provider.update(
|
|
122
|
+
anchor !== undefined && head !== undefined
|
|
123
|
+
? {
|
|
124
|
+
anchor: this._cursorConverter.toCursor(anchor),
|
|
125
|
+
head: this._cursorConverter.toCursor(head),
|
|
126
|
+
}
|
|
127
|
+
: undefined,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private _updateRemoteSelections(update: ViewUpdate) {
|
|
132
|
+
const decorations: Range<Decoration>[] = [];
|
|
133
|
+
const awarenessStates = this._provider.getRemoteStates();
|
|
134
|
+
for (const state of awarenessStates) {
|
|
135
|
+
const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
|
|
136
|
+
const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
|
|
137
|
+
if (anchor == null || head == null) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const start = Math.min(Math.min(anchor, head), update.view.state.doc.length);
|
|
142
|
+
const end = Math.min(Math.max(anchor, head), update.view.state.doc.length);
|
|
143
|
+
|
|
144
|
+
const startLine = update.view.state.doc.lineAt(start);
|
|
145
|
+
const endLine = update.view.state.doc.lineAt(end);
|
|
146
|
+
|
|
147
|
+
// TODO(burdon): Factor out styles.
|
|
148
|
+
const color = state.info.color ?? '#30bced';
|
|
149
|
+
const lightColor = state.info.lightColor ?? color + '33';
|
|
150
|
+
|
|
151
|
+
if (startLine.number === endLine.number) {
|
|
152
|
+
// Selected content in a single line.
|
|
153
|
+
decorations.push({
|
|
154
|
+
from: start,
|
|
155
|
+
to: end,
|
|
156
|
+
value: Decoration.mark({
|
|
157
|
+
attributes: { style: `background-color: ${lightColor}` },
|
|
158
|
+
class: 'cm-collab-selection',
|
|
159
|
+
}),
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
// Selected content in multiple lines; first, render text-selection in the first line.
|
|
163
|
+
decorations.push({
|
|
164
|
+
from: start,
|
|
165
|
+
to: startLine.from + startLine.length,
|
|
166
|
+
value: Decoration.mark({
|
|
167
|
+
attributes: { style: `background-color: ${color}` },
|
|
168
|
+
class: 'cm-collab-selection',
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Render text-selection in the last line.
|
|
173
|
+
decorations.push({
|
|
174
|
+
from: endLine.from,
|
|
175
|
+
to: end,
|
|
176
|
+
value: Decoration.mark({
|
|
177
|
+
attributes: { style: `background-color: ${color}` },
|
|
178
|
+
class: 'cm-collab-selection',
|
|
179
|
+
}),
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
for (let i = startLine.number + 1; i < endLine.number; i++) {
|
|
183
|
+
const linePos = update.view.state.doc.line(i).from;
|
|
184
|
+
decorations.push({
|
|
185
|
+
from: linePos,
|
|
186
|
+
to: linePos,
|
|
187
|
+
value: Decoration.line({
|
|
188
|
+
attributes: { style: `background-color: ${color}`, class: 'cm-collab-selectionLine' },
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
decorations.push({
|
|
195
|
+
from: head,
|
|
196
|
+
to: head,
|
|
197
|
+
value: Decoration.widget({
|
|
198
|
+
side: head - anchor > 0 ? -1 : 1, // The local cursor should be rendered outside the remote selection.
|
|
199
|
+
block: false,
|
|
200
|
+
widget: new RemoteCaretWidget(state.info.displayName ?? 'Anonymous', color),
|
|
201
|
+
}),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.decorations = Decoration.set(decorations, true);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
class RemoteCaretWidget extends WidgetType {
|
|
210
|
+
constructor(
|
|
211
|
+
private readonly _name: string,
|
|
212
|
+
private readonly _color: string,
|
|
213
|
+
) {
|
|
214
|
+
super();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
override toDOM(): HTMLElement {
|
|
218
|
+
const span = document.createElement('span');
|
|
219
|
+
span.className = 'cm-collab-selectionCaret';
|
|
220
|
+
span.style.backgroundColor = this._color;
|
|
221
|
+
span.style.borderColor = this._color;
|
|
222
|
+
|
|
223
|
+
const dot = document.createElement('div');
|
|
224
|
+
dot.className = 'cm-collab-selectionCaretDot';
|
|
225
|
+
|
|
226
|
+
const info = document.createElement('div');
|
|
227
|
+
info.className = 'cm-collab-selectionInfo';
|
|
228
|
+
info.innerText = this._name;
|
|
229
|
+
|
|
230
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
231
|
+
span.appendChild(dot);
|
|
232
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
233
|
+
span.appendChild(info);
|
|
234
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
235
|
+
|
|
236
|
+
return span;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
override updateDOM() {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
override eq(widget: this) {
|
|
244
|
+
return widget._color === this._color;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
override get estimatedHeight() {
|
|
248
|
+
return -1;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
override ignoreEvent() {
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const styles = EditorView.baseTheme({
|
|
257
|
+
'.cm-collab-selection': {},
|
|
258
|
+
'.cm-collab-selectionLine': {
|
|
259
|
+
padding: 0,
|
|
260
|
+
margin: '0px 2px 0px 4px',
|
|
261
|
+
},
|
|
262
|
+
'.cm-collab-selectionCaret': {
|
|
263
|
+
position: 'relative',
|
|
264
|
+
borderLeft: '1px solid black',
|
|
265
|
+
borderRight: '1px solid black',
|
|
266
|
+
marginLeft: '-1px',
|
|
267
|
+
marginRight: '-1px',
|
|
268
|
+
boxSizing: 'border-box',
|
|
269
|
+
display: 'inline',
|
|
270
|
+
cursor: 'pointer',
|
|
271
|
+
},
|
|
272
|
+
'.cm-collab-selectionCaretDot': {
|
|
273
|
+
borderRadius: '50%',
|
|
274
|
+
position: 'absolute',
|
|
275
|
+
width: '.5em',
|
|
276
|
+
height: '.5em',
|
|
277
|
+
top: '-.25em',
|
|
278
|
+
left: '-.25em',
|
|
279
|
+
backgroundColor: 'inherit',
|
|
280
|
+
transition: 'transform .3s ease-in-out',
|
|
281
|
+
boxSizing: 'border-box',
|
|
282
|
+
},
|
|
283
|
+
'.cm-collab-selectionCaret:hover > .cm-collab-selectionCaretDot': {
|
|
284
|
+
transform: 'scale(0)',
|
|
285
|
+
transformOrigin: 'center',
|
|
286
|
+
},
|
|
287
|
+
'.cm-collab-selectionInfo': {
|
|
288
|
+
position: 'absolute',
|
|
289
|
+
transform: 'translate(-50%, 0)',
|
|
290
|
+
top: '-20px',
|
|
291
|
+
left: 0,
|
|
292
|
+
fontSize: '.75em',
|
|
293
|
+
fontFamily: 'sans-serif',
|
|
294
|
+
fontStyle: 'normal',
|
|
295
|
+
fontWeight: 'normal',
|
|
296
|
+
lineHeight: 'normal',
|
|
297
|
+
userSelect: 'none',
|
|
298
|
+
color: 'white',
|
|
299
|
+
padding: '2px',
|
|
300
|
+
zIndex: 101,
|
|
301
|
+
transition: 'opacity .3s ease-in-out',
|
|
302
|
+
backgroundColor: 'inherit',
|
|
303
|
+
borderRadius: '2px',
|
|
304
|
+
// These should be separate.
|
|
305
|
+
opacity: 0,
|
|
306
|
+
transitionDelay: '0s',
|
|
307
|
+
whiteSpace: 'nowrap',
|
|
308
|
+
},
|
|
309
|
+
'.cm-collab-selectionCaret:hover > .cm-collab-selectionInfo': {
|
|
310
|
+
opacity: 1,
|
|
311
|
+
transitionDelay: '0s',
|
|
312
|
+
},
|
|
313
|
+
});
|
package/src/extensions/basic.ts
CHANGED
|
@@ -3,30 +3,37 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { closeBrackets } from '@codemirror/autocomplete';
|
|
6
|
+
import { history } from '@codemirror/commands';
|
|
6
7
|
import { bracketMatching, defaultHighlightStyle, syntaxHighlighting } from '@codemirror/language';
|
|
7
8
|
import { type Extension } from '@codemirror/state';
|
|
8
9
|
import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';
|
|
9
|
-
import { drawSelection, EditorView, placeholder } from '@codemirror/view';
|
|
10
|
+
import { drawSelection, EditorView, highlightActiveLine, placeholder } from '@codemirror/view';
|
|
10
11
|
|
|
11
|
-
import type
|
|
12
|
+
import { type ThemeMode } from '@dxos/react-ui';
|
|
13
|
+
import { isNotFalsy } from '@dxos/util';
|
|
14
|
+
|
|
15
|
+
import { type TextEditorProps } from '../components';
|
|
12
16
|
|
|
13
17
|
export type BasicBundleOptions = {
|
|
14
|
-
readonly?: boolean;
|
|
15
18
|
themeMode?: ThemeMode;
|
|
16
|
-
|
|
17
|
-
};
|
|
19
|
+
} & Pick<TextEditorProps, 'placeholder' | 'lineWrapping'>;
|
|
18
20
|
|
|
19
|
-
export const
|
|
21
|
+
export const createBasicBundle = ({
|
|
22
|
+
themeMode,
|
|
23
|
+
placeholder: _placeholder,
|
|
24
|
+
lineWrapping = true,
|
|
25
|
+
}: BasicBundleOptions = {}): Extension[] =>
|
|
20
26
|
[
|
|
21
|
-
EditorView.lineWrapping,
|
|
27
|
+
lineWrapping && EditorView.lineWrapping,
|
|
22
28
|
|
|
23
|
-
// TODO(burdon): Compare with minimalSetup.
|
|
24
29
|
// https://codemirror.net/docs/ref/#codemirror.minimalSetup
|
|
25
30
|
bracketMatching(),
|
|
26
31
|
closeBrackets(),
|
|
27
32
|
drawSelection(),
|
|
33
|
+
highlightActiveLine(),
|
|
34
|
+
history(),
|
|
28
35
|
_placeholder && placeholder(_placeholder),
|
|
29
36
|
|
|
30
|
-
//
|
|
37
|
+
// https://github.com/codemirror/theme-one-dark
|
|
31
38
|
themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
|
|
32
|
-
].filter(
|
|
39
|
+
].filter(isNotFalsy);
|
package/src/extensions/blast.ts
CHANGED
|
@@ -125,7 +125,10 @@ class Blaster {
|
|
|
125
125
|
|
|
126
126
|
_lastPoint = { x: 0, y: 0 };
|
|
127
127
|
|
|
128
|
-
constructor(
|
|
128
|
+
constructor(
|
|
129
|
+
private readonly _node: HTMLElement,
|
|
130
|
+
private readonly _options: BlastOptions,
|
|
131
|
+
) {
|
|
129
132
|
this._effect = this._options.effect === 1 ? new Effect1(_options) : new Effect2(_options);
|
|
130
133
|
}
|
|
131
134
|
|