@dxos/react-ui-editor 0.3.10-next.ef70620 → 0.3.11-main.008b7c6
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 +2814 -1096
- 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 +63 -0
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +9 -4
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +15 -4
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts +20 -0
- 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/index.d.ts +1 -1
- package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +1 -0
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts +10 -0
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +26 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +3 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/{automerge/automerge-plugin/PatchSemaphore.d.ts → extensions/automerge/semaphore.d.ts} +8 -5
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
- package/dist/types/src/extensions/awareness.d.ts +46 -0
- package/dist/types/src/extensions/awareness.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions/basic/bundle.d.ts → extensions/basic.d.ts} +3 -2
- package/dist/types/src/extensions/basic.d.ts.map +1 -0
- package/dist/types/src/extensions/blast.d.ts +25 -0
- package/dist/types/src/extensions/blast.d.ts.map +1 -0
- package/dist/types/src/extensions/code.d.ts +2 -0
- package/dist/types/src/extensions/code.d.ts.map +1 -0
- package/dist/types/src/extensions/comments.d.ts +50 -0
- package/dist/types/src/extensions/comments.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts +2 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/image.d.ts +4 -0
- package/dist/types/src/extensions/image.d.ts.map +1 -0
- package/dist/types/src/extensions/index.d.ts +19 -0
- package/dist/types/src/extensions/index.d.ts.map +1 -0
- package/dist/types/src/extensions/link.d.ts +12 -0
- package/dist/types/src/extensions/link.d.ts.map +1 -0
- package/dist/types/src/extensions/listener.d.ts +10 -0
- package/dist/types/src/extensions/listener.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/bundle.d.ts +17 -0
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts +36 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts +3 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts +6 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -0
- package/dist/types/src/extensions/mermaid.d.ts +3 -0
- package/dist/types/src/extensions/mermaid.d.ts.map +1 -0
- package/dist/types/src/extensions/table.d.ts +8 -0
- package/dist/types/src/extensions/table.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts +3 -0
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/typewriter.d.ts +10 -0
- package/dist/types/src/extensions/typewriter.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts +4 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/index.d.ts +2 -0
- package/dist/types/src/extensions/yjs/index.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts +4 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +2 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge.d.ts +3 -0
- package/dist/types/src/hooks/automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +28 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +23 -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 +14 -11
- package/dist/types/src/styles/markdown.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 +17 -0
- package/dist/types/src/themes/default.d.ts.map +1 -0
- package/dist/types/src/themes/index.d.ts +2 -0
- package/dist/types/src/themes/index.d.ts.map +1 -0
- package/dist/types/src/util/cursor.d.ts +17 -0
- package/dist/types/src/util/cursor.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/util.d.ts +5 -0
- package/dist/types/src/util/util.d.ts.map +1 -0
- package/package.json +29 -33
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +426 -0
- package/src/components/TextEditor/TextEditor.stories.tsx +70 -43
- package/src/components/TextEditor/TextEditor.tsx +98 -52
- package/src/{automerge → components/TextEditor}/automerge.stories.tsx +69 -7
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/components/TextEditor/index.ts +1 -1
- package/src/components/TextEditor/yjs.stories.tsx +1 -0
- package/src/extensions/autocomplete.ts +59 -0
- package/src/extensions/automerge/automerge.ts +94 -0
- package/src/extensions/automerge/cursor.ts +51 -0
- package/src/extensions/automerge/defs.ts +62 -0
- package/src/extensions/automerge/index.ts +6 -0
- package/src/{automerge/automerge-plugin/PatchSemaphore.ts → extensions/automerge/semaphore.ts} +24 -18
- package/src/{automerge/automerge-plugin/codeMirrorToAm.ts → extensions/automerge/update-automerge.ts} +8 -6
- package/src/{automerge/automerge-plugin/amToCodemirror.ts → extensions/automerge/update-codemirror.ts} +15 -4
- package/src/extensions/awareness.ts +316 -0
- package/src/{components/TextEditor/extensions/basic/bundle.ts → extensions/basic.ts} +8 -4
- package/src/extensions/blast.ts +373 -0
- package/src/extensions/code.ts +99 -0
- package/src/extensions/comments.ts +421 -0
- package/src/extensions/hr.ts +74 -0
- package/src/extensions/image.ts +69 -0
- package/src/extensions/index.ts +22 -0
- package/src/extensions/link.ts +192 -0
- package/src/extensions/listener.ts +35 -0
- package/src/extensions/markdown/bundle.ts +106 -0
- package/src/extensions/markdown/highlight.ts +204 -0
- package/src/{components/TextEditor/extensions → extensions}/markdown/index.ts +1 -1
- package/src/extensions/mention.ts +31 -0
- package/src/extensions/mermaid.ts +11 -0
- package/src/extensions/table.ts +132 -0
- package/src/extensions/tasklist.ts +121 -0
- package/src/extensions/typewriter.ts +68 -0
- package/src/extensions/yjs/cursor.ts +21 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/extensions/yjs/yjs.test.ts +35 -0
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +52 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useTextModel.ts +53 -67
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +71 -19
- package/src/index.ts +3 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/markdown.ts +33 -24
- package/src/testing/replicator.ts +16 -21
- package/src/themes/default.ts +302 -0
- package/src/themes/index.ts +5 -0
- package/src/util/cursor.ts +29 -0
- package/src/util/index.ts +6 -0
- package/src/util/util.ts +18 -0
- package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts.map +0 -1
- package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts +0 -6
- package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts.map +0 -1
- package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts +0 -7
- package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts.map +0 -1
- package/dist/types/src/automerge/automerge-plugin/handle.d.ts +0 -9
- package/dist/types/src/automerge/automerge-plugin/handle.d.ts.map +0 -1
- package/dist/types/src/automerge/automerge-plugin/index.d.ts +0 -3
- package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +0 -1
- package/dist/types/src/automerge/automerge-plugin/plugin.d.ts +0 -26
- package/dist/types/src/automerge/automerge-plugin/plugin.d.ts.map +0 -1
- package/dist/types/src/automerge/automerge.stories.d.ts +0 -9
- package/dist/types/src/automerge/automerge.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts +0 -3
- package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts +0 -10
- package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/change.d.ts +0 -7
- package/dist/types/src/components/TextEditor/extensions/change.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts +0 -11
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts +0 -8
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts +0 -10
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts +0 -4
- package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/index.d.ts +0 -5
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +0 -8
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts +0 -3
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts +0 -17
- package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts +0 -21
- package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts.map +0 -1
- package/dist/types/src/hooks/useCollaboration.d.ts +0 -9
- package/dist/types/src/hooks/useCollaboration.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/src/automerge/automerge-plugin/handle.ts +0 -14
- package/src/automerge/automerge-plugin/index.ts +0 -6
- package/src/automerge/automerge-plugin/plugin.ts +0 -133
- package/src/components/TextEditor/extensions/basic/index.ts +0 -6
- package/src/components/TextEditor/extensions/basic/theme.ts +0 -49
- package/src/components/TextEditor/extensions/change.ts +0 -22
- package/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.ts +0 -99
- package/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.tsx +0 -61
- package/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.tsx +0 -119
- package/src/components/TextEditor/extensions/hyperlink/index.ts +0 -7
- package/src/components/TextEditor/extensions/index.ts +0 -8
- package/src/components/TextEditor/extensions/markdown/bundle.ts +0 -99
- package/src/components/TextEditor/extensions/markdown/tags.ts +0 -38
- package/src/components/TextEditor/extensions/markdown/theme.ts +0 -265
- package/src/hooks/useCollaboration.ts +0 -45
- package/src/hooks/yjs/index.ts +0 -6
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
import { Facet, StateField, type Extension, type Transaction } from '@codemirror/state';
|
|
8
|
+
import { ViewPlugin, type EditorView, type PluginValue, type ViewUpdate } from '@codemirror/view';
|
|
9
|
+
|
|
10
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
11
|
+
import { type Prop } from '@dxos/automerge/automerge';
|
|
12
|
+
|
|
13
|
+
import { cursorConverter } from './cursor';
|
|
14
|
+
import { effectType, type IDocHandle, isReconcileTx, type Value } from './defs';
|
|
15
|
+
import { PatchSemaphore } from './semaphore';
|
|
16
|
+
import { CursorConverter } from '../../util';
|
|
17
|
+
|
|
18
|
+
const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
19
|
+
combine: (values) => values.at(-1)!, // Take last.
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export type AutomergeOptions = {
|
|
23
|
+
handle: IDocHandle;
|
|
24
|
+
path: Prop[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
28
|
+
// TODO(burdon): Rename.
|
|
29
|
+
const stateField: StateField<Value> = StateField.define({
|
|
30
|
+
create: () => ({
|
|
31
|
+
lastHeads: A.getHeads(handle.docSync()!),
|
|
32
|
+
unreconciledTransactions: [],
|
|
33
|
+
path: path.slice(),
|
|
34
|
+
}),
|
|
35
|
+
update: (value: Value, tr: Transaction) => {
|
|
36
|
+
const result: Value = {
|
|
37
|
+
lastHeads: value.lastHeads,
|
|
38
|
+
unreconciledTransactions: value.unreconciledTransactions.slice(),
|
|
39
|
+
path: path.slice(),
|
|
40
|
+
};
|
|
41
|
+
let clearUnreconciled = false;
|
|
42
|
+
for (const effect of tr.effects) {
|
|
43
|
+
if (effect.is(effectType)) {
|
|
44
|
+
result.lastHeads = effect.value.newHeads;
|
|
45
|
+
clearUnreconciled = true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (clearUnreconciled) {
|
|
49
|
+
result.unreconciledTransactions = [];
|
|
50
|
+
} else {
|
|
51
|
+
if (!isReconcileTx(tr)) {
|
|
52
|
+
result.unreconciledTransactions.push(tr);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const semaphore = new PatchSemaphore(stateField);
|
|
60
|
+
|
|
61
|
+
const viewPlugin = ViewPlugin.fromClass(
|
|
62
|
+
class AutomergeCodemirrorViewPlugin implements PluginValue {
|
|
63
|
+
constructor(private readonly _view: EditorView) {
|
|
64
|
+
handle.addListener('change', this._handleChange);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
update(update: ViewUpdate) {
|
|
68
|
+
if (update.transactions.length > 0 && update.transactions.some((tr) => !isReconcileTx(tr))) {
|
|
69
|
+
// TODO(burdon): This is a hack to ensure that the view is updated after the transaction is applied.
|
|
70
|
+
queueMicrotask(() => {
|
|
71
|
+
this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
destroy() {
|
|
77
|
+
handle.addListener('change', this._handleChange);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private _handleChange = () => {
|
|
81
|
+
this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
extension: [
|
|
88
|
+
CursorConverter.of(cursorConverter(handle, path)),
|
|
89
|
+
semaphoreFacet.of(semaphore),
|
|
90
|
+
stateField,
|
|
91
|
+
viewPlugin,
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import get from 'lodash.get';
|
|
6
|
+
|
|
7
|
+
import type { Prop } from '@dxos/automerge/automerge';
|
|
8
|
+
import { next as automerge } from '@dxos/automerge/automerge';
|
|
9
|
+
|
|
10
|
+
import { type IDocHandle } from './defs';
|
|
11
|
+
import { type CursorConverter } from '../../util';
|
|
12
|
+
|
|
13
|
+
export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConverter => ({
|
|
14
|
+
// TODO(burdon): Handle assoc to associate with a previous character.
|
|
15
|
+
toCursor: (pos) => {
|
|
16
|
+
const doc = handle.docSync();
|
|
17
|
+
if (!doc) {
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const value = get(doc, path);
|
|
22
|
+
if (typeof value === 'string' && value.length <= pos) {
|
|
23
|
+
return 'end';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// NOTE: Slice is needed because getCursor mutates the array.
|
|
27
|
+
return automerge.getCursor(doc, path.slice(), pos);
|
|
28
|
+
},
|
|
29
|
+
fromCursor: (cursor) => {
|
|
30
|
+
if (cursor === '') {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const doc = handle.docSync();
|
|
35
|
+
if (!doc) {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (cursor === 'end') {
|
|
40
|
+
const value = get(doc, path);
|
|
41
|
+
if (typeof value === 'string') {
|
|
42
|
+
return value.length;
|
|
43
|
+
} else {
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// NOTE: Slice is needed because getCursor mutates the array.
|
|
49
|
+
return automerge.getCursorPosition(doc, path.slice(), cursor);
|
|
50
|
+
},
|
|
51
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Annotation,
|
|
8
|
+
StateEffect,
|
|
9
|
+
type StateField,
|
|
10
|
+
type EditorState,
|
|
11
|
+
type Transaction,
|
|
12
|
+
type TransactionSpec,
|
|
13
|
+
} from '@codemirror/state';
|
|
14
|
+
|
|
15
|
+
import { type ChangeFn, type ChangeOptions, type Doc, type Heads, type Prop } from '@dxos/automerge/automerge';
|
|
16
|
+
|
|
17
|
+
export type Value = {
|
|
18
|
+
lastHeads: Heads;
|
|
19
|
+
path: Prop[];
|
|
20
|
+
unreconciledTransactions: Transaction[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type UpdateHeads = {
|
|
24
|
+
newHeads: Heads;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const effectType = StateEffect.define<UpdateHeads>({});
|
|
28
|
+
|
|
29
|
+
export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => effectType.of({ newHeads });
|
|
30
|
+
|
|
31
|
+
export const getLastHeads = (state: EditorState, field: StateField<Value>): Heads => state.field(field).lastHeads;
|
|
32
|
+
|
|
33
|
+
export const getPath = (state: EditorState, field: StateField<Value>): Prop[] => state.field(field).path;
|
|
34
|
+
|
|
35
|
+
export const reconcileAnnotationType = Annotation.define<unknown>();
|
|
36
|
+
|
|
37
|
+
export const isReconcileTx = (tr: Transaction): boolean => !!tr.annotation(reconcileAnnotationType);
|
|
38
|
+
|
|
39
|
+
export const makeReconcile = (tr: TransactionSpec) => {
|
|
40
|
+
if (tr.annotations != null) {
|
|
41
|
+
if (tr.annotations instanceof Array) {
|
|
42
|
+
tr.annotations = [...tr.annotations, reconcileAnnotationType.of({})];
|
|
43
|
+
} else {
|
|
44
|
+
tr.annotations = [tr.annotations, reconcileAnnotationType.of({})];
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
tr.annotations = [reconcileAnnotationType.of({})];
|
|
48
|
+
}
|
|
49
|
+
// return {
|
|
50
|
+
// ...tr,
|
|
51
|
+
// annotations: reconcileAnnotationType.of({})
|
|
52
|
+
// }
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type IDocHandle<T = any> = {
|
|
56
|
+
docSync(): Doc<T> | undefined;
|
|
57
|
+
change(callback: ChangeFn<T>, options?: ChangeOptions<T>): void;
|
|
58
|
+
changeAt(heads: Heads, callback: ChangeFn<T>, options?: ChangeOptions<T>): string[] | undefined;
|
|
59
|
+
|
|
60
|
+
addListener(event: 'change', listener: () => void): void;
|
|
61
|
+
removeListener(event: 'change', listener: () => void): void;
|
|
62
|
+
};
|
package/src/{automerge/automerge-plugin/PatchSemaphore.ts → extensions/automerge/semaphore.ts}
RENAMED
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
7
|
+
import { type StateField } from '@codemirror/state';
|
|
5
8
|
import { type EditorView } from '@codemirror/view';
|
|
6
9
|
|
|
7
10
|
import { next as automerge } from '@dxos/automerge/automerge';
|
|
8
11
|
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
import {
|
|
13
|
+
getLastHeads,
|
|
14
|
+
getPath,
|
|
15
|
+
isReconcileTx,
|
|
16
|
+
reconcileAnnotationType,
|
|
17
|
+
updateHeads,
|
|
18
|
+
type IDocHandle,
|
|
19
|
+
type Value,
|
|
20
|
+
} from './defs';
|
|
21
|
+
import { updateAutomerge } from './update-automerge';
|
|
22
|
+
import { updateCodeMirror } from './update-codemirror';
|
|
13
23
|
|
|
14
24
|
type Doc<T> = automerge.Doc<T>;
|
|
15
25
|
type Heads = automerge.Heads;
|
|
16
26
|
|
|
17
27
|
type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
|
|
18
28
|
|
|
29
|
+
/**
|
|
30
|
+
* TODO(burdon): Comment.
|
|
31
|
+
*/
|
|
19
32
|
export class PatchSemaphore {
|
|
20
|
-
_field!: Field;
|
|
21
33
|
_inReconcile = false;
|
|
22
34
|
_queue: Array<ChangeFn> = [];
|
|
23
35
|
|
|
24
|
-
constructor(
|
|
25
|
-
if (field !== undefined) {
|
|
26
|
-
this._field = field;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
36
|
+
constructor(private readonly _field: StateField<Value>) {}
|
|
29
37
|
|
|
30
38
|
reconcile = (handle: IDocHandle, view: EditorView) => {
|
|
31
39
|
if (this._inReconcile) {
|
|
@@ -37,9 +45,8 @@ export class PatchSemaphore {
|
|
|
37
45
|
const oldHeads = getLastHeads(view.state, this._field);
|
|
38
46
|
let selection = view.state.selection;
|
|
39
47
|
|
|
48
|
+
// First undo all the unreconciled transactions.
|
|
40
49
|
const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
|
|
41
|
-
|
|
42
|
-
// First undo all the unreconciled transactions
|
|
43
50
|
const toInvert = transactions.slice().reverse();
|
|
44
51
|
for (const tx of toInvert) {
|
|
45
52
|
const inverted = tx.changes.invert(tx.startState.doc);
|
|
@@ -50,19 +57,18 @@ export class PatchSemaphore {
|
|
|
50
57
|
});
|
|
51
58
|
}
|
|
52
59
|
|
|
53
|
-
//
|
|
54
|
-
let newHeads =
|
|
60
|
+
// Apply the unreconciled transactions to the document.
|
|
61
|
+
let newHeads = updateAutomerge(this._field, handle, transactions, view.state);
|
|
55
62
|
|
|
56
|
-
// NOTE: null and undefined each come from automerge and repo respectively
|
|
63
|
+
// NOTE: null and undefined each come from automerge and repo respectively.
|
|
57
64
|
if (newHeads === null || newHeads === undefined) {
|
|
58
65
|
// TODO: @alexjg this is the call that's resetting the editor state on click
|
|
59
66
|
newHeads = automerge.getHeads(handle.docSync()!);
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
//
|
|
63
|
-
// and apply that to the codemirror doc
|
|
69
|
+
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
64
70
|
const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
|
|
65
|
-
|
|
71
|
+
updateCodeMirror(view, selection, path, diff);
|
|
66
72
|
|
|
67
73
|
view.dispatch({
|
|
68
74
|
effects: updateHeads(newHeads),
|
|
@@ -1,16 +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
|
-
import { type EditorState, type Text, type Transaction } from '@codemirror/state';
|
|
7
|
+
import { type EditorState, type StateField, type Text, type Transaction } from '@codemirror/state';
|
|
6
8
|
|
|
7
9
|
import { next as am, type Heads } from '@dxos/automerge/automerge';
|
|
8
10
|
|
|
9
|
-
import { type IDocHandle } from './
|
|
10
|
-
import { type Field } from './plugin';
|
|
11
|
+
import { type IDocHandle, type Value } from './defs';
|
|
11
12
|
|
|
12
|
-
export
|
|
13
|
-
field:
|
|
13
|
+
export const updateAutomerge = (
|
|
14
|
+
field: StateField<Value>,
|
|
14
15
|
handle: IDocHandle,
|
|
15
16
|
transactions: Transaction[],
|
|
16
17
|
state: EditorState,
|
|
@@ -18,7 +19,7 @@ export default (
|
|
|
18
19
|
const { lastHeads, path } = state.field(field);
|
|
19
20
|
|
|
20
21
|
// We don't want to call `automerge.updateAt` if there are no changes.
|
|
21
|
-
// Otherwise later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
|
|
22
|
+
// Otherwise, later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
|
|
22
23
|
let hasChanges = false;
|
|
23
24
|
for (const tr of transactions) {
|
|
24
25
|
tr.changes.iterChanges(() => {
|
|
@@ -37,5 +38,6 @@ export default (
|
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
});
|
|
41
|
+
|
|
40
42
|
return newHeads ?? undefined;
|
|
41
43
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
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
7
|
import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
|
|
@@ -14,9 +16,9 @@ import {
|
|
|
14
16
|
type SpliceTextPatch,
|
|
15
17
|
} from '@dxos/automerge/automerge';
|
|
16
18
|
|
|
17
|
-
import { reconcileAnnotationType } from './
|
|
19
|
+
import { reconcileAnnotationType } from './defs';
|
|
18
20
|
|
|
19
|
-
export
|
|
21
|
+
export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
|
|
20
22
|
for (const patch of patches) {
|
|
21
23
|
const changeSpec = handlePatch(patch, target, view.state);
|
|
22
24
|
if (changeSpec != null) {
|
|
@@ -28,6 +30,7 @@ export default (view: EditorView, selection: EditorSelection, target: Prop[], pa
|
|
|
28
30
|
});
|
|
29
31
|
}
|
|
30
32
|
}
|
|
33
|
+
|
|
31
34
|
view.dispatch({
|
|
32
35
|
selection,
|
|
33
36
|
annotations: reconcileAnnotationType.of({}),
|
|
@@ -53,6 +56,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
|
|
|
53
56
|
if (index == null) {
|
|
54
57
|
return [];
|
|
55
58
|
}
|
|
59
|
+
|
|
56
60
|
const text = patch.values.map((v) => (v ? v.toString() : '')).join('');
|
|
57
61
|
return [{ from: index, to: index, insert: text }];
|
|
58
62
|
};
|
|
@@ -62,6 +66,7 @@ const handleSplice = (target: Prop[], patch: SpliceTextPatch): Array<ChangeSpec>
|
|
|
62
66
|
if (index == null) {
|
|
63
67
|
return [];
|
|
64
68
|
}
|
|
69
|
+
|
|
65
70
|
return [{ from: index, insert: patch.value }];
|
|
66
71
|
};
|
|
67
72
|
|
|
@@ -70,6 +75,7 @@ const handleDel = (target: Prop[], patch: DelPatch): Array<ChangeSpec> => {
|
|
|
70
75
|
if (index == null) {
|
|
71
76
|
return [];
|
|
72
77
|
}
|
|
78
|
+
|
|
73
79
|
const length = patch.length || 1;
|
|
74
80
|
return [{ from: index, to: index + length }];
|
|
75
81
|
};
|
|
@@ -79,27 +85,32 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
|
|
|
79
85
|
if (index == null) {
|
|
80
86
|
return [];
|
|
81
87
|
}
|
|
88
|
+
|
|
82
89
|
const length = state.doc.length;
|
|
83
90
|
if (typeof patch.value !== 'string') {
|
|
84
91
|
return []; // TODO(dmaretskyi): How to handle non string values?
|
|
85
92
|
}
|
|
93
|
+
|
|
86
94
|
return [{ from: 0, to: length, insert: patch.value as any }];
|
|
87
95
|
};
|
|
88
96
|
|
|
89
|
-
// If the path of the patch is of the form [path, <index>] then we know this is
|
|
90
|
-
//
|
|
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.
|
|
91
99
|
const charPath = (textPath: Prop[], candidatePath: Prop[]): number | null => {
|
|
92
100
|
if (candidatePath.length !== textPath.length + 1) {
|
|
93
101
|
return null;
|
|
94
102
|
}
|
|
103
|
+
|
|
95
104
|
for (let i = 0; i < textPath.length; i++) {
|
|
96
105
|
if (textPath[i] !== candidatePath[i]) {
|
|
97
106
|
return null;
|
|
98
107
|
}
|
|
99
108
|
}
|
|
109
|
+
|
|
100
110
|
const index = candidatePath[candidatePath.length - 1];
|
|
101
111
|
if (typeof index === 'number') {
|
|
102
112
|
return index;
|
|
103
113
|
}
|
|
114
|
+
|
|
104
115
|
return null;
|
|
105
116
|
};
|