@dxos/react-ui-editor 0.3.11-main.7ff9773 → 0.3.11-main.80db41b
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 +1402 -934
- 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 +1 -2
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +3 -2
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/{yjs.stories.d.ts → hooks.stories.d.ts} +13 -2
- 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/automerge/automerge.d.ts.map +1 -1
- 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/defs.d.ts +5 -5
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +2 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/awareness.d.ts +2 -4
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +2 -2
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +9 -5
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -7
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +2 -2
- 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/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +1 -4
- package/dist/types/src/hooks/defs.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +3 -2
- 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 +12 -0
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/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/themes/default.d.ts.map +1 -1
- package/package.json +39 -29
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +15 -19
- package/src/components/TextEditor/TextEditor.tsx +46 -27
- package/src/components/TextEditor/automerge.stories.tsx +1 -2
- 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/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +44 -10
- package/src/extensions/automerge/cursor.ts +3 -3
- package/src/extensions/automerge/defs.ts +9 -9
- package/src/extensions/automerge/semaphore.ts +17 -19
- package/src/extensions/automerge/update-automerge.ts +12 -6
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +21 -35
- package/src/extensions/basic.ts +17 -15
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +137 -79
- package/src/extensions/index.ts +0 -7
- package/src/extensions/markdown/bundle.ts +9 -8
- 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 +9 -15
- package/src/extensions/{hr.ts → markdown/hr.ts} +3 -4
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +13 -4
- package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +2 -3
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +1 -6
- package/src/hooks/index.ts +3 -2
- 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 +65 -12
- package/src/index.ts +1 -1
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +11 -8
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +0 -26
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.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/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -4
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -28
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/comments.stories.tsx +0 -357
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -104
- package/src/extensions/yjs/cursor.ts +0 -22
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -53
- package/src/hooks/yjs.ts +0 -157
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- /package/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/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
package/src/components/index.ts
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { EditorState } from '@codemirror/state';
|
|
6
|
+
import { EditorView } from '@codemirror/view';
|
|
7
|
+
import { render, screen } from '@testing-library/react';
|
|
8
|
+
import chai, { expect } from 'chai';
|
|
9
|
+
import chaiDom from 'chai-dom';
|
|
10
|
+
import get from 'lodash.get';
|
|
11
|
+
import React, { type FC, useEffect, useRef, useState } from 'react';
|
|
12
|
+
import { describe, test } from 'vitest';
|
|
13
|
+
|
|
14
|
+
import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
|
|
15
|
+
|
|
16
|
+
import { automerge } from './automerge';
|
|
17
|
+
|
|
18
|
+
type TestObject = {
|
|
19
|
+
text: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const path = ['text'];
|
|
23
|
+
|
|
24
|
+
class Generator {
|
|
25
|
+
constructor(private readonly _handle: DocHandle<TestObject>) {}
|
|
26
|
+
update(text: string) {
|
|
27
|
+
this._handle.change((doc: TestObject) => {
|
|
28
|
+
doc.text = text;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Test: FC<{ handle: DocHandle<TestObject>; generator: Generator }> = ({ handle, generator }) => {
|
|
34
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
35
|
+
const [view, setView] = useState<EditorView>();
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const extensions = [
|
|
38
|
+
automerge({ handle, path }),
|
|
39
|
+
EditorView.updateListener.of((update) => {
|
|
40
|
+
if (view.state.doc.toString() === 'hello!') {
|
|
41
|
+
// Update editor.
|
|
42
|
+
update.view.dispatch({ changes: { from: view.state.doc.length - 1, insert: ' world' } });
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const view = new EditorView({
|
|
48
|
+
state: EditorState.create({ doc: get(handle.docSync()!, path), extensions }),
|
|
49
|
+
parent: ref.current!,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
setView(view);
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {}, [view]);
|
|
56
|
+
|
|
57
|
+
return <div ref={ref} />;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
chai.use(chaiDom);
|
|
61
|
+
|
|
62
|
+
describe('Automerge', () => {
|
|
63
|
+
test('basic sync', () => {
|
|
64
|
+
const repo = new Repo({ network: [] });
|
|
65
|
+
const handle = repo.create<TestObject>();
|
|
66
|
+
const generator = new Generator(handle);
|
|
67
|
+
render(<Test handle={handle} generator={generator} />);
|
|
68
|
+
expect(screen.getByRole('textbox')).to.have.text('');
|
|
69
|
+
|
|
70
|
+
generator.update('hello!');
|
|
71
|
+
expect(screen.getByRole('textbox')).to.have.text('hello world!');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// TODO(burdon): Test history/undo.
|
|
75
|
+
// TODO(burdon): https://testing-library.com/docs/react-testing-library/example-intro/
|
|
76
|
+
});
|
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
5
|
//
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { invertedEffects } from '@codemirror/commands';
|
|
8
|
+
import { StateField, type Extension, type StateEffect } from '@codemirror/state';
|
|
9
|
+
import { EditorView, ViewPlugin } from '@codemirror/view';
|
|
9
10
|
|
|
10
|
-
import {
|
|
11
|
+
import { next as A, type Prop } from '@dxos/automerge/automerge';
|
|
11
12
|
|
|
12
13
|
import { cursorConverter } from './cursor';
|
|
13
|
-
import {
|
|
14
|
+
import { updateHeadsEffect, type IDocHandle, isReconcile, type State } from './defs';
|
|
14
15
|
import { PatchSemaphore } from './semaphore';
|
|
15
16
|
import { Cursor } from '../cursor';
|
|
16
17
|
|
|
@@ -20,7 +21,7 @@ export type AutomergeOptions = {
|
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
23
|
-
const
|
|
24
|
+
const syncState = StateField.define<State>({
|
|
24
25
|
create: () => ({
|
|
25
26
|
path: path.slice(),
|
|
26
27
|
lastHeads: A.getHeads(handle.docSync()!),
|
|
@@ -36,7 +37,7 @@ export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
|
36
37
|
|
|
37
38
|
let clearUnreconciled = false;
|
|
38
39
|
for (const effect of tr.effects) {
|
|
39
|
-
if (effect.is(
|
|
40
|
+
if (effect.is(updateHeadsEffect)) {
|
|
40
41
|
result.lastHeads = effect.value.newHeads;
|
|
41
42
|
clearUnreconciled = true;
|
|
42
43
|
}
|
|
@@ -45,7 +46,7 @@ export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
|
45
46
|
if (clearUnreconciled) {
|
|
46
47
|
result.unreconciledTransactions = [];
|
|
47
48
|
} else {
|
|
48
|
-
if (!
|
|
49
|
+
if (!isReconcile(tr)) {
|
|
49
50
|
result.unreconciledTransactions.push(tr);
|
|
50
51
|
}
|
|
51
52
|
}
|
|
@@ -54,16 +55,49 @@ export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
|
54
55
|
},
|
|
55
56
|
});
|
|
56
57
|
|
|
57
|
-
const semaphore = new PatchSemaphore(handle,
|
|
58
|
+
const semaphore = new PatchSemaphore(handle, syncState);
|
|
58
59
|
|
|
59
60
|
return [
|
|
60
61
|
Cursor.converter.of(cursorConverter(handle, path)),
|
|
62
|
+
syncState,
|
|
63
|
+
|
|
64
|
+
// Reconcile external updates.
|
|
65
|
+
ViewPlugin.fromClass(
|
|
66
|
+
class {
|
|
67
|
+
constructor(private readonly _view: EditorView) {
|
|
68
|
+
handle.addListener('change', this._handleChange.bind(this));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
destroy() {
|
|
72
|
+
handle.removeListener('change', this._handleChange.bind(this));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_handleChange() {
|
|
76
|
+
semaphore.reconcile(this._view);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
),
|
|
80
|
+
|
|
81
|
+
// Reconcile local updates.
|
|
61
82
|
EditorView.updateListener.of(({ view, changes }) => {
|
|
62
83
|
if (!changes.empty) {
|
|
63
|
-
// TODO(burdon): Loses cursor position if auto closing brackets. Call explicitly?
|
|
64
84
|
semaphore.reconcile(view);
|
|
65
85
|
}
|
|
66
86
|
}),
|
|
67
|
-
|
|
87
|
+
|
|
88
|
+
// TODO(burdon): Record undo transactions.
|
|
89
|
+
// See https://github.com/yjs/y-codemirror.next/tree/main
|
|
90
|
+
// https://codemirror.net/examples/inverted-effect
|
|
91
|
+
invertedEffects.of((tr) => {
|
|
92
|
+
// Each change results it three transactions: insert, delete, insert.
|
|
93
|
+
const effects: StateEffect<any>[] = [];
|
|
94
|
+
if (!tr.changes.empty) {
|
|
95
|
+
tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
|
|
96
|
+
// effects.push(effectType.of({});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return effects;
|
|
101
|
+
}),
|
|
68
102
|
];
|
|
69
103
|
};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import get from 'lodash.get';
|
|
6
6
|
|
|
7
7
|
import type { Prop } from '@dxos/automerge/automerge';
|
|
8
|
-
import { next as
|
|
8
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
9
9
|
|
|
10
10
|
import { type IDocHandle } from './defs';
|
|
11
11
|
import { type CursorConverter } from '../cursor';
|
|
@@ -24,7 +24,7 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
// NOTE: Slice is needed because getCursor mutates the array.
|
|
27
|
-
return
|
|
27
|
+
return A.getCursor(doc, path.slice(), pos);
|
|
28
28
|
},
|
|
29
29
|
|
|
30
30
|
fromCursor: (cursor) => {
|
|
@@ -47,6 +47,6 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
// NOTE: Slice is needed because getCursor mutates the array.
|
|
50
|
-
return
|
|
50
|
+
return A.getCursorPosition(doc, path.slice(), cursor);
|
|
51
51
|
},
|
|
52
52
|
});
|
|
@@ -8,28 +8,28 @@ import { Annotation, StateEffect, type StateField, type EditorState, type Transa
|
|
|
8
8
|
|
|
9
9
|
import { type ChangeFn, type ChangeOptions, type Doc, type Heads, type Prop } from '@dxos/automerge/automerge';
|
|
10
10
|
|
|
11
|
-
// TODO(burdon): Rename.
|
|
12
11
|
export type State = {
|
|
13
12
|
path: Prop[];
|
|
14
13
|
lastHeads: Heads;
|
|
15
14
|
unreconciledTransactions: Transaction[];
|
|
16
15
|
};
|
|
17
16
|
|
|
17
|
+
export const getPath = (state: EditorState, field: StateField<State>): Prop[] => state.field(field).path;
|
|
18
|
+
export const getLastHeads = (state: EditorState, field: StateField<State>): Heads => state.field(field).lastHeads;
|
|
19
|
+
|
|
18
20
|
export type UpdateHeads = {
|
|
19
21
|
newHeads: Heads;
|
|
20
22
|
};
|
|
21
23
|
|
|
22
|
-
export const
|
|
23
|
-
|
|
24
|
-
export const reconcileAnnotationType = Annotation.define<unknown>();
|
|
24
|
+
export const updateHeadsEffect = StateEffect.define<UpdateHeads>({});
|
|
25
25
|
|
|
26
|
-
export const
|
|
27
|
-
|
|
28
|
-
export const getLastHeads = (state: EditorState, field: StateField<State>): Heads => state.field(field).lastHeads;
|
|
26
|
+
export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => updateHeadsEffect.of({ newHeads });
|
|
29
27
|
|
|
30
|
-
export const
|
|
28
|
+
export const reconcileAnnotation = Annotation.define<boolean>();
|
|
31
29
|
|
|
32
|
-
export const
|
|
30
|
+
export const isReconcile = (tr: Transaction): boolean => {
|
|
31
|
+
return !!tr.annotation(reconcileAnnotation);
|
|
32
|
+
};
|
|
33
33
|
|
|
34
34
|
export type IDocHandle<T = any> = {
|
|
35
35
|
docSync(): Doc<T> | undefined;
|
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
import { type StateField } from '@codemirror/state';
|
|
8
8
|
import { type EditorView } from '@codemirror/view';
|
|
9
9
|
|
|
10
|
-
import { next as
|
|
10
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
11
11
|
|
|
12
12
|
import {
|
|
13
13
|
getLastHeads,
|
|
14
14
|
getPath,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
isReconcile,
|
|
16
|
+
reconcileAnnotation,
|
|
17
17
|
updateHeads,
|
|
18
18
|
type IDocHandle,
|
|
19
19
|
type State,
|
|
@@ -22,7 +22,7 @@ import { updateAutomerge } from './update-automerge';
|
|
|
22
22
|
import { updateCodeMirror } from './update-codemirror';
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* Implements three-way merge (on each mutation
|
|
25
|
+
* Implements three-way merge (on each mutation).
|
|
26
26
|
*/
|
|
27
27
|
export class PatchSemaphore {
|
|
28
28
|
private _inReconcile = false;
|
|
@@ -33,13 +33,15 @@ export class PatchSemaphore {
|
|
|
33
33
|
private readonly _state: StateField<State>
|
|
34
34
|
) {}
|
|
35
35
|
|
|
36
|
-
// NOTE: Cannot destruct view.state.
|
|
37
36
|
reconcile(view: EditorView) {
|
|
38
|
-
if (this._inReconcile) {
|
|
39
|
-
|
|
37
|
+
if (!this._inReconcile) {
|
|
38
|
+
this._inReconcile = true;
|
|
39
|
+
this.doReconcile(view);
|
|
40
|
+
this._inReconcile = false;
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
+
}
|
|
42
43
|
|
|
44
|
+
private doReconcile(view: EditorView) {
|
|
43
45
|
const path = getPath(view.state, this._state);
|
|
44
46
|
|
|
45
47
|
// Get the heads before the unreconciled transactions are applied.
|
|
@@ -47,14 +49,14 @@ export class PatchSemaphore {
|
|
|
47
49
|
let selection = view.state.selection;
|
|
48
50
|
|
|
49
51
|
// First undo all the unreconciled transactions.
|
|
50
|
-
const transactions = view.state.field(this._state).unreconciledTransactions.filter((tx) => !
|
|
52
|
+
const transactions = view.state.field(this._state).unreconciledTransactions.filter((tx) => !isReconcile(tx));
|
|
51
53
|
const toInvert = transactions.slice().reverse();
|
|
52
|
-
for (const
|
|
53
|
-
const inverted =
|
|
54
|
+
for (const tr of toInvert) {
|
|
55
|
+
const inverted = tr.changes.invert(tr.startState.doc);
|
|
54
56
|
selection = selection.map(inverted);
|
|
55
57
|
view.dispatch({
|
|
56
58
|
changes: inverted,
|
|
57
|
-
annotations:
|
|
59
|
+
annotations: reconcileAnnotation.of(true),
|
|
58
60
|
});
|
|
59
61
|
}
|
|
60
62
|
|
|
@@ -63,21 +65,17 @@ export class PatchSemaphore {
|
|
|
63
65
|
let newHeads = updateAutomerge(this._state, this._handle, transactions, view.state);
|
|
64
66
|
if (newHeads === null || newHeads === undefined) {
|
|
65
67
|
// TODO(alexjg): this is the call that's resetting the editor state on click.
|
|
66
|
-
newHeads =
|
|
68
|
+
newHeads = A.getHeads(this._handle.docSync()!);
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
70
|
-
const diff =
|
|
71
|
-
? []
|
|
72
|
-
: automerge.diff(this._handle.docSync()!, oldHeads, newHeads);
|
|
72
|
+
const diff = A.equals(oldHeads, newHeads) ? [] : A.diff(this._handle.docSync()!, oldHeads, newHeads);
|
|
73
73
|
updateCodeMirror(view, selection, path, diff);
|
|
74
74
|
|
|
75
75
|
// Update automerge state.
|
|
76
76
|
view.dispatch({
|
|
77
77
|
effects: updateHeads(newHeads),
|
|
78
|
-
annotations:
|
|
78
|
+
annotations: reconcileAnnotation.of(false),
|
|
79
79
|
});
|
|
80
|
-
|
|
81
|
-
this._inReconcile = false;
|
|
82
80
|
}
|
|
83
81
|
}
|
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
5
|
//
|
|
6
6
|
|
|
7
|
-
import { type EditorState, type StateField, type
|
|
7
|
+
import { type EditorState, type StateField, type Transaction, type Text } from '@codemirror/state';
|
|
8
8
|
|
|
9
|
-
import { next as
|
|
9
|
+
import { next as A, type Heads } from '@dxos/automerge/automerge';
|
|
10
10
|
|
|
11
11
|
import { type IDocHandle, type State } from './defs';
|
|
12
12
|
|
|
@@ -14,7 +14,7 @@ export const updateAutomerge = (
|
|
|
14
14
|
field: StateField<State>,
|
|
15
15
|
handle: IDocHandle,
|
|
16
16
|
transactions: Transaction[],
|
|
17
|
-
state: EditorState,
|
|
17
|
+
state: EditorState, // TODO(burdon): Just pass in the state field value?
|
|
18
18
|
): Heads | undefined => {
|
|
19
19
|
const { lastHeads, path } = state.field(field);
|
|
20
20
|
|
|
@@ -31,12 +31,18 @@ export const updateAutomerge = (
|
|
|
31
31
|
return undefined;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
const newHeads = handle.changeAt(lastHeads, (doc:
|
|
34
|
+
const newHeads = handle.changeAt(lastHeads, (doc: A.Doc<unknown>) => {
|
|
35
|
+
const invertedTransactions: { from: number; del: number; insert: Text }[] = [];
|
|
35
36
|
for (const tr of transactions) {
|
|
36
|
-
tr.changes.iterChanges((fromA
|
|
37
|
-
|
|
37
|
+
tr.changes.iterChanges((fromA, toA, _fromB, _toB, insert) => {
|
|
38
|
+
invertedTransactions.push({ from: fromA, del: toA - fromA, insert });
|
|
38
39
|
});
|
|
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
|
+
});
|
|
40
46
|
});
|
|
41
47
|
|
|
42
48
|
return newHeads ?? undefined;
|
|
@@ -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
|
|
|
@@ -57,7 +57,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
|
|
|
57
57
|
return [];
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
const text = patch.values.map((
|
|
60
|
+
const text = patch.values.map((value) => (value ? value.toString() : '')).join('');
|
|
61
61
|
return [{ from: index, to: index, insert: text }];
|
|
62
62
|
};
|
|
63
63
|
|
|
@@ -88,7 +88,7 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
|
|
|
88
88
|
|
|
89
89
|
const length = state.doc.length;
|
|
90
90
|
if (typeof patch.value !== 'string') {
|
|
91
|
-
return []; // TODO(dmaretskyi): How to handle non
|
|
91
|
+
return []; // TODO(dmaretskyi): How to handle non-string values?
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
return [{ from: 0, to: length, insert: patch.value as any }];
|
|
@@ -12,16 +12,11 @@ import {
|
|
|
12
12
|
type ViewUpdate,
|
|
13
13
|
WidgetType,
|
|
14
14
|
} from '@codemirror/view';
|
|
15
|
-
import { useEffect } from 'react';
|
|
16
15
|
|
|
17
16
|
import { Event } from '@dxos/async';
|
|
18
17
|
import { Context } from '@dxos/context';
|
|
19
|
-
import { generateName } from '@dxos/display-name';
|
|
20
|
-
import { useThemeContext } from '@dxos/react-ui';
|
|
21
|
-
import { getColorForValue } from '@dxos/react-ui-theme';
|
|
22
18
|
|
|
23
19
|
import { Cursor, type CursorConverter } from './cursor';
|
|
24
|
-
import { type EditorModel } from '../hooks';
|
|
25
20
|
|
|
26
21
|
export interface AwarenessProvider {
|
|
27
22
|
remoteStateChange: Event<void>;
|
|
@@ -29,8 +24,8 @@ export interface AwarenessProvider {
|
|
|
29
24
|
open(): void;
|
|
30
25
|
close(): void;
|
|
31
26
|
|
|
32
|
-
update(position: AwarenessPosition | undefined): void;
|
|
33
27
|
getRemoteStates(): AwarenessState[];
|
|
28
|
+
update(position: AwarenessPosition | undefined): void;
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
const dummyProvider: AwarenessProvider = {
|
|
@@ -39,11 +34,11 @@ const dummyProvider: AwarenessProvider = {
|
|
|
39
34
|
open: () => {},
|
|
40
35
|
close: () => {},
|
|
41
36
|
|
|
42
|
-
update: () => {},
|
|
43
37
|
getRemoteStates: () => [],
|
|
38
|
+
update: () => {},
|
|
44
39
|
};
|
|
45
40
|
|
|
46
|
-
export const
|
|
41
|
+
export const awarenessProvider = Facet.define<AwarenessProvider, AwarenessProvider>({
|
|
47
42
|
combine: (providers) => providers[0] ?? dummyProvider,
|
|
48
43
|
});
|
|
49
44
|
|
|
@@ -73,7 +68,7 @@ export type AwarenessState = {
|
|
|
73
68
|
*/
|
|
74
69
|
export const awareness = (provider = dummyProvider): Extension => {
|
|
75
70
|
return [
|
|
76
|
-
|
|
71
|
+
awarenessProvider.of(provider),
|
|
77
72
|
ViewPlugin.fromClass(RemoteSelectionsDecorator, {
|
|
78
73
|
decorations: (value) => value.decorations,
|
|
79
74
|
}),
|
|
@@ -81,19 +76,6 @@ export const awareness = (provider = dummyProvider): Extension => {
|
|
|
81
76
|
];
|
|
82
77
|
};
|
|
83
78
|
|
|
84
|
-
export const useAwareness = ({ awareness, peer }: EditorModel) => {
|
|
85
|
-
const { themeMode } = useThemeContext();
|
|
86
|
-
useEffect(() => {
|
|
87
|
-
if (awareness && peer) {
|
|
88
|
-
awareness.setLocalStateField('user', {
|
|
89
|
-
name: peer.name ?? generateName(peer.id),
|
|
90
|
-
color: getColorForValue({ value: peer.id, type: 'color' }),
|
|
91
|
-
colorLight: getColorForValue({ value: peer.id, themeMode, type: 'highlight' }),
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}, [awareness, peer, themeMode]);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
79
|
/**
|
|
98
80
|
* Generates selection decorations from remote peers.
|
|
99
81
|
*/
|
|
@@ -109,7 +91,7 @@ export class RemoteSelectionsDecorator implements PluginValue {
|
|
|
109
91
|
|
|
110
92
|
constructor(view: EditorView) {
|
|
111
93
|
this._cursorConverter = view.state.facet(Cursor.converter);
|
|
112
|
-
this._provider = view.state.facet(
|
|
94
|
+
this._provider = view.state.facet(awarenessProvider);
|
|
113
95
|
this._provider.open();
|
|
114
96
|
this._provider.remoteStateChange.on(this._ctx, () => {
|
|
115
97
|
view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
|
|
@@ -225,7 +207,10 @@ export class RemoteSelectionsDecorator implements PluginValue {
|
|
|
225
207
|
}
|
|
226
208
|
|
|
227
209
|
class RemoteCaretWidget extends WidgetType {
|
|
228
|
-
constructor(
|
|
210
|
+
constructor(
|
|
211
|
+
private readonly _name: string,
|
|
212
|
+
private readonly _color: string,
|
|
213
|
+
) {
|
|
229
214
|
super();
|
|
230
215
|
}
|
|
231
216
|
|
|
@@ -268,7 +253,6 @@ class RemoteCaretWidget extends WidgetType {
|
|
|
268
253
|
}
|
|
269
254
|
}
|
|
270
255
|
|
|
271
|
-
// TODO(burdon): Mixed case?
|
|
272
256
|
const styles = EditorView.baseTheme({
|
|
273
257
|
'.cm-collab-selection': {},
|
|
274
258
|
'.cm-collab-selectionLine': {
|
|
@@ -283,38 +267,40 @@ const styles = EditorView.baseTheme({
|
|
|
283
267
|
marginRight: '-1px',
|
|
284
268
|
boxSizing: 'border-box',
|
|
285
269
|
display: 'inline',
|
|
270
|
+
cursor: 'pointer',
|
|
286
271
|
},
|
|
287
272
|
'.cm-collab-selectionCaretDot': {
|
|
288
273
|
borderRadius: '50%',
|
|
289
274
|
position: 'absolute',
|
|
290
|
-
width: '.
|
|
291
|
-
height: '.
|
|
292
|
-
top: '-.
|
|
293
|
-
left: '-.
|
|
275
|
+
width: '.5em',
|
|
276
|
+
height: '.5em',
|
|
277
|
+
top: '-.25em',
|
|
278
|
+
left: '-.25em',
|
|
294
279
|
backgroundColor: 'inherit',
|
|
295
280
|
transition: 'transform .3s ease-in-out',
|
|
296
281
|
boxSizing: 'border-box',
|
|
297
282
|
},
|
|
298
283
|
'.cm-collab-selectionCaret:hover > .cm-collab-selectionCaretDot': {
|
|
299
|
-
transformOrigin: 'bottom center',
|
|
300
284
|
transform: 'scale(0)',
|
|
285
|
+
transformOrigin: 'center',
|
|
301
286
|
},
|
|
302
287
|
'.cm-collab-selectionInfo': {
|
|
303
288
|
position: 'absolute',
|
|
304
|
-
|
|
305
|
-
|
|
289
|
+
transform: 'translate(-50%, 0)',
|
|
290
|
+
top: '-20px',
|
|
291
|
+
left: 0,
|
|
306
292
|
fontSize: '.75em',
|
|
307
|
-
fontFamily: 'serif',
|
|
293
|
+
fontFamily: 'sans-serif',
|
|
308
294
|
fontStyle: 'normal',
|
|
309
295
|
fontWeight: 'normal',
|
|
310
296
|
lineHeight: 'normal',
|
|
311
297
|
userSelect: 'none',
|
|
312
298
|
color: 'white',
|
|
313
|
-
|
|
314
|
-
paddingRight: '2px',
|
|
299
|
+
padding: '2px',
|
|
315
300
|
zIndex: 101,
|
|
316
301
|
transition: 'opacity .3s ease-in-out',
|
|
317
302
|
backgroundColor: 'inherit',
|
|
303
|
+
borderRadius: '2px',
|
|
318
304
|
// These should be separate.
|
|
319
305
|
opacity: 0,
|
|
320
306
|
transitionDelay: '0s',
|
package/src/extensions/basic.ts
CHANGED
|
@@ -10,28 +10,30 @@ import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';
|
|
|
10
10
|
import { drawSelection, EditorView, highlightActiveLine, placeholder } from '@codemirror/view';
|
|
11
11
|
|
|
12
12
|
import { type ThemeMode } from '@dxos/react-ui';
|
|
13
|
+
import { isNotFalsy } from '@dxos/util';
|
|
13
14
|
|
|
14
15
|
import { type TextEditorProps } from '../components';
|
|
15
16
|
|
|
16
17
|
export type BasicBundleOptions = {
|
|
17
18
|
themeMode?: ThemeMode;
|
|
18
|
-
} & Pick<TextEditorProps, 'placeholder' | '
|
|
19
|
+
} & Pick<TextEditorProps, 'placeholder' | 'lineWrapping'>;
|
|
19
20
|
|
|
20
|
-
export const
|
|
21
|
+
export const createBasicBundle = ({
|
|
21
22
|
themeMode,
|
|
22
23
|
placeholder: _placeholder,
|
|
23
|
-
|
|
24
|
-
}: BasicBundleOptions): Extension[] =>
|
|
25
|
-
|
|
24
|
+
lineWrapping = true,
|
|
25
|
+
}: BasicBundleOptions = {}): Extension[] =>
|
|
26
|
+
[
|
|
27
|
+
lineWrapping && EditorView.lineWrapping,
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
// https://codemirror.net/docs/ref/#codemirror.minimalSetup
|
|
30
|
+
bracketMatching(),
|
|
31
|
+
closeBrackets(),
|
|
32
|
+
drawSelection(),
|
|
33
|
+
highlightActiveLine(),
|
|
34
|
+
history(),
|
|
35
|
+
_placeholder && placeholder(_placeholder),
|
|
34
36
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
];
|
|
37
|
+
// https://github.com/codemirror/theme-one-dark
|
|
38
|
+
themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
|
|
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
|
|