@dxos/react-ui-editor 0.3.10-main.5515dfe → 0.3.10-main.560f82b
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 +1049 -285
- 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 +22 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +3 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts +3 -2
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/basic.d.ts +2 -1
- package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/blast.d.ts +25 -0
- package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts +18 -0
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts +11 -0
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/image.d.ts +4 -0
- package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/index.d.ts +6 -0
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts +4 -2
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +2 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts +6 -2
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/mention.d.ts +6 -0
- package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/table.d.ts +8 -0
- package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts +2 -4
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/themes/default.d.ts +8 -0
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +1 -1
- package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.d.ts +3 -0
- package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts +16 -0
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/handle.d.ts.map +1 -0
- package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/index.d.ts +1 -1
- package/dist/types/src/hooks/automerge/index.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/plugin.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts +5 -0
- package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts +6 -0
- package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/translation/index.d.ts +3 -0
- package/dist/types/src/hooks/automerge/translation/index.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +0 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +3 -4
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/{components/TextEditor → hooks/yjs}/yjs.stories.d.ts +1 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/styles/markdown.d.ts +3 -2
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/package.json +20 -19
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +137 -31
- package/src/components/TextEditor/TextEditor.tsx +52 -25
- package/src/components/TextEditor/extensions/autocomplete.ts +9 -22
- package/src/components/TextEditor/extensions/basic.ts +2 -1
- package/src/components/TextEditor/extensions/blast.ts +375 -0
- package/src/components/TextEditor/extensions/comments.ts +190 -0
- package/src/components/TextEditor/extensions/demo.ts +69 -0
- package/src/components/TextEditor/extensions/experimental.tsx +1 -0
- package/src/components/TextEditor/extensions/image.ts +68 -0
- package/src/components/TextEditor/extensions/index.ts +6 -0
- package/src/components/TextEditor/extensions/link.ts +25 -25
- package/src/components/TextEditor/extensions/listener.ts +4 -3
- package/src/components/TextEditor/extensions/markdown/bundle.ts +18 -18
- package/src/components/TextEditor/extensions/markdown/highlight.ts +118 -103
- package/src/components/TextEditor/extensions/mention.ts +31 -0
- package/src/components/TextEditor/extensions/table.ts +123 -0
- package/src/components/TextEditor/extensions/tasklist.ts +89 -81
- package/src/components/TextEditor/themes/default.ts +69 -2
- package/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.ts +12 -9
- package/src/{automerge → hooks/automerge}/automerge.stories.tsx +5 -3
- package/src/{automerge/automerge-plugin → hooks/automerge}/plugin.ts +1 -0
- package/src/{automerge/automerge-plugin/amToCodemirror.ts → hooks/automerge/translation/automerge-to-codemirror.ts} +10 -2
- package/src/{automerge/automerge-plugin/codeMirrorToAm.ts → hooks/automerge/translation/codemirror-to-automerge.ts} +7 -4
- package/src/hooks/automerge/translation/index.ts +6 -0
- package/src/hooks/index.ts +0 -1
- package/src/hooks/useTextModel.ts +49 -41
- package/src/{components/TextEditor → hooks/yjs}/yjs.stories.tsx +1 -1
- package/src/styles/markdown.ts +14 -8
- package/src/testing/replicator.ts +6 -7
- 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.map +0 -1
- package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +0 -1
- 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/yjs.stories.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/src/hooks/useCollaboration.ts +0 -45
- package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/handle.d.ts +0 -0
- package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/plugin.d.ts +0 -0
- package/src/{automerge/automerge-plugin → hooks/automerge}/handle.ts +0 -0
- package/src/{automerge/automerge-plugin → hooks/automerge}/index.ts +1 -1
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { type EditorState, type RangeSet, RangeSetBuilder, StateField, type Transaction } from '@codemirror/state';
|
|
7
|
+
import { Decoration, EditorView, WidgetType } from '@codemirror/view';
|
|
8
|
+
|
|
9
|
+
// TODO(burdon): Snippet to create basic table.
|
|
10
|
+
// https://codemirror.net/docs/ref/#autocomplete.snippet
|
|
11
|
+
|
|
12
|
+
export type TableOptions = {};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* GFM tables.
|
|
16
|
+
* https://github.github.com/gfm/#tables-extension
|
|
17
|
+
*/
|
|
18
|
+
export const table = (options: TableOptions = {}) => {
|
|
19
|
+
return StateField.define<RangeSet<any>>({
|
|
20
|
+
create: (state) => update(state, options),
|
|
21
|
+
update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
|
|
22
|
+
provide: (field) => EditorView.decorations.from(field),
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type Table = {
|
|
27
|
+
from: number;
|
|
28
|
+
to?: number;
|
|
29
|
+
header?: string[];
|
|
30
|
+
rows?: string[][];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const update = (state: EditorState, options: TableOptions) => {
|
|
34
|
+
const builder = new RangeSetBuilder();
|
|
35
|
+
if (state.readOnly) {
|
|
36
|
+
const tables: Table[] = [];
|
|
37
|
+
const getTable = () => tables[tables.length - 1];
|
|
38
|
+
const getRow = () => {
|
|
39
|
+
const table = getTable();
|
|
40
|
+
return table.rows?.[table.rows.length - 1];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Parse table.
|
|
44
|
+
// TODO(burdon): Use remark?: https://www.npmjs.com/package/remark-gfm
|
|
45
|
+
syntaxTree(state).iterate({
|
|
46
|
+
enter: (node) => {
|
|
47
|
+
// console.log(node.name);
|
|
48
|
+
// Check if cursor is inside text.
|
|
49
|
+
switch (node.name) {
|
|
50
|
+
case 'Table': {
|
|
51
|
+
tables.push({ from: node.from });
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case 'TableHeader': {
|
|
55
|
+
getTable().header = [];
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case 'TableRow': {
|
|
59
|
+
(getTable().rows ??= []).push([]);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'TableCell': {
|
|
63
|
+
const row = getRow();
|
|
64
|
+
if (row) {
|
|
65
|
+
row.push(state.sliceDoc(node.from, node.to));
|
|
66
|
+
} else {
|
|
67
|
+
getTable().header?.push(state.sliceDoc(node.from, node.to));
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
case 'TableDelimiter': {
|
|
72
|
+
getTable().to = node.to;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
tables.forEach((table) =>
|
|
80
|
+
builder.add(table.from, table.to!, Decoration.replace({ widget: new TableWidget(table) })),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return builder.finish();
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
class TableWidget extends WidgetType {
|
|
88
|
+
constructor(readonly _table: Table) {
|
|
89
|
+
super();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
override eq(other: WidgetType) {
|
|
93
|
+
return this._table.from === (other as any as TableWidget)?._table?.from;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
toDOM(view: EditorView) {
|
|
97
|
+
const table = document.createElement('table');
|
|
98
|
+
|
|
99
|
+
{
|
|
100
|
+
const header = table.appendChild(document.createElement('thead'));
|
|
101
|
+
const row = header.appendChild(document.createElement('tr'));
|
|
102
|
+
this._table.header?.forEach((cell) => {
|
|
103
|
+
const th = document.createElement('th');
|
|
104
|
+
th.setAttribute('class', 'cm-table-head');
|
|
105
|
+
row.appendChild(th).textContent = cell;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
{
|
|
110
|
+
const body = table.appendChild(document.createElement('tbody'));
|
|
111
|
+
this._table.rows?.forEach((row) => {
|
|
112
|
+
const tr = body.appendChild(document.createElement('tr'));
|
|
113
|
+
row.forEach((cell) => {
|
|
114
|
+
const td = document.createElement('td');
|
|
115
|
+
td.setAttribute('class', 'cm-table-cell');
|
|
116
|
+
tr.appendChild(td).textContent = cell;
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return table;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -2,112 +2,120 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { syntaxTree } from '@codemirror/language';
|
|
6
5
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
EditorView,
|
|
7
|
+
Decoration,
|
|
8
|
+
WidgetType,
|
|
9
|
+
MatchDecorator,
|
|
10
|
+
ViewPlugin,
|
|
11
|
+
type DecorationSet,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
} from '@codemirror/view';
|
|
15
14
|
|
|
16
15
|
class CheckboxWidget extends WidgetType {
|
|
17
|
-
constructor(
|
|
16
|
+
constructor(
|
|
17
|
+
private readonly _pos: number,
|
|
18
|
+
private _checked: boolean,
|
|
19
|
+
private readonly _indent: number,
|
|
20
|
+
private readonly _onCheck?: (check: boolean) => void,
|
|
21
|
+
) {
|
|
18
22
|
super();
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
override eq(other: CheckboxWidget) {
|
|
22
|
-
|
|
23
|
-
// TODO(burdon): Is this efficient? Check life-cycle.
|
|
24
|
-
return this._pos === other._pos && this._checked === other._checked;
|
|
26
|
+
return this._pos === other._pos && this._checked === other._checked && this._indent === other._indent;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
toDOM(view: EditorView) {
|
|
28
|
-
// console.log('toDom', this._pos);
|
|
29
30
|
const wrap = document.createElement('span');
|
|
30
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
31
31
|
wrap.className = 'cm-task-item';
|
|
32
|
+
wrap.setAttribute('aria-hidden', 'true');
|
|
33
|
+
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
32
34
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
selection: {
|
|
46
|
-
anchor: this._pos + 4,
|
|
47
|
-
// anchor: this._getCursor(),
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
return true;
|
|
51
|
-
};
|
|
35
|
+
const input = wrap.appendChild(document.createElement('input'));
|
|
36
|
+
input.type = 'checkbox';
|
|
37
|
+
input.checked = this._checked;
|
|
38
|
+
if (!this._onCheck) {
|
|
39
|
+
input.setAttribute('disabled', 'true');
|
|
40
|
+
}
|
|
41
|
+
if (this._onCheck) {
|
|
42
|
+
input.onchange = (event: Event) => {
|
|
43
|
+
this._onCheck?.((event.target as any).checked);
|
|
44
|
+
return true;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
52
47
|
|
|
53
48
|
return wrap;
|
|
54
49
|
}
|
|
55
50
|
|
|
56
|
-
// TODO(burdon): Remove listener?
|
|
57
|
-
// override destroy() {
|
|
58
|
-
// console.log('destroy', this._pos);
|
|
59
|
-
// }
|
|
60
|
-
|
|
61
51
|
override ignoreEvent() {
|
|
62
52
|
return false;
|
|
63
53
|
}
|
|
64
54
|
}
|
|
65
55
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
// TODO(burdon): Reconcile with theme.
|
|
57
|
+
const styles = EditorView.baseTheme({
|
|
58
|
+
'& .cm-task-item': {
|
|
59
|
+
paddingLeft: '4px',
|
|
60
|
+
paddingRight: '8px',
|
|
61
|
+
},
|
|
62
|
+
});
|
|
69
63
|
|
|
70
|
-
export
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
export type TasklistOptions = {};
|
|
65
|
+
|
|
66
|
+
export const tasklist = (options: TasklistOptions = {}) => {
|
|
67
|
+
// TODO(burdon): Matcher isn't as precise as syntax tree.
|
|
68
|
+
// Allows for indents to be greater than AST would allow.
|
|
69
|
+
const taskMatcher = new MatchDecorator({
|
|
70
|
+
regexp: /^(\s*)- \[([ xX])\]\s/g,
|
|
71
|
+
decoration: (match, view, pos) => {
|
|
72
|
+
const indent = Math.floor(match[1].length / 2);
|
|
73
|
+
const checked = match[2] === 'x' || match[2] === 'X';
|
|
74
|
+
return Decoration.replace({
|
|
75
|
+
widget: new CheckboxWidget(
|
|
76
|
+
pos,
|
|
77
|
+
checked,
|
|
78
|
+
indent,
|
|
79
|
+
view.state.readOnly
|
|
80
|
+
? undefined
|
|
81
|
+
: (checked) => {
|
|
82
|
+
const idx = pos + match[0].indexOf('[') + 1;
|
|
83
|
+
view.dispatch({
|
|
84
|
+
changes: {
|
|
85
|
+
from: idx,
|
|
86
|
+
to: idx + 1,
|
|
87
|
+
insert: checked ? 'x' : ' ',
|
|
88
|
+
},
|
|
89
|
+
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
90
|
+
selection: {
|
|
91
|
+
anchor: idx + 3,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
),
|
|
96
|
+
});
|
|
97
|
+
},
|
|
74
98
|
});
|
|
75
99
|
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
builder.add(
|
|
83
|
-
node.from,
|
|
84
|
-
node.to,
|
|
85
|
-
Decoration.replace({
|
|
86
|
-
widget: new CheckboxWidget(node.from, () => lastPosition, isChecked(state, node.from)),
|
|
87
|
-
}),
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
});
|
|
100
|
+
const tasks = ViewPlugin.fromClass(
|
|
101
|
+
class {
|
|
102
|
+
tasks: DecorationSet;
|
|
103
|
+
constructor(view: EditorView) {
|
|
104
|
+
this.tasks = taskMatcher.createDeco(view);
|
|
105
|
+
}
|
|
92
106
|
|
|
93
|
-
|
|
94
|
-
|
|
107
|
+
update(update: ViewUpdate) {
|
|
108
|
+
this.tasks = taskMatcher.updateDeco(update, this.tasks);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
decorations: (instance) => instance.tasks,
|
|
113
|
+
provide: (plugin) =>
|
|
114
|
+
EditorView.atomicRanges.of((view) => {
|
|
115
|
+
return view.plugin(plugin)?.tasks || Decoration.none;
|
|
116
|
+
}),
|
|
117
|
+
},
|
|
118
|
+
);
|
|
95
119
|
|
|
96
|
-
return [
|
|
97
|
-
listener,
|
|
98
|
-
StateField.define<RangeSet<any>>({
|
|
99
|
-
create: (state) => checkbox(state),
|
|
100
|
-
update: (_: RangeSet<any>, tr: Transaction) => checkbox(tr.state),
|
|
101
|
-
provide: (field) => EditorView.decorations.from(field),
|
|
102
|
-
}),
|
|
103
|
-
];
|
|
120
|
+
return [tasks, styles];
|
|
104
121
|
};
|
|
105
|
-
|
|
106
|
-
// TODO(burdon): Reconcile with theme.
|
|
107
|
-
export const styles = EditorView.baseTheme({
|
|
108
|
-
'& .cm-task-item': {
|
|
109
|
-
padding: '0 4px',
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
export const tasklist = () => [statefield(), styles];
|
|
@@ -10,6 +10,11 @@ import { tokens } from '../../../styles';
|
|
|
10
10
|
/**
|
|
11
11
|
* Minimal styles.
|
|
12
12
|
* https://codemirror.net/examples/styling
|
|
13
|
+
*
|
|
14
|
+
* Examples:
|
|
15
|
+
* - https://github.com/codemirror/view/blob/main/src/theme.ts
|
|
16
|
+
* - https://github.com/codemirror/theme-one-dark/blob/main/src/one-dark.ts
|
|
17
|
+
*
|
|
13
18
|
* NOTE: Use one of '&', '&light', and '&dark' prefix to scope instance.
|
|
14
19
|
* NOTE: `light` and `dark` selectors are preprocessed by CodeMirror and can only be in the base theme.
|
|
15
20
|
*/
|
|
@@ -136,8 +141,55 @@ export const defaultTheme: {
|
|
|
136
141
|
'& .cm-tooltip': {
|
|
137
142
|
border: 'none',
|
|
138
143
|
},
|
|
139
|
-
|
|
140
|
-
|
|
144
|
+
'& .cm-tooltip-below': {},
|
|
145
|
+
|
|
146
|
+
//
|
|
147
|
+
// autocomplete
|
|
148
|
+
//
|
|
149
|
+
'& .cm-tooltip-autocomplete': {
|
|
150
|
+
marginTop: '4px',
|
|
151
|
+
marginLeft: '-3px',
|
|
152
|
+
},
|
|
153
|
+
'& .cm-tooltip-autocomplete ul li': {},
|
|
154
|
+
'& .cm-tooltip-autocomplete ul li[aria-selected]': {},
|
|
155
|
+
'& .cm-completionIcon': {
|
|
156
|
+
display: 'none',
|
|
157
|
+
},
|
|
158
|
+
'& .cm-completionLabel': {
|
|
159
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
160
|
+
},
|
|
161
|
+
'& .cm-completionMatchedText': {
|
|
162
|
+
textDecoration: 'none',
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
//
|
|
166
|
+
// widgets
|
|
167
|
+
//
|
|
168
|
+
'& .cm-widgetBuffer': {
|
|
169
|
+
display: 'none',
|
|
170
|
+
height: 0,
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
//
|
|
174
|
+
// table
|
|
175
|
+
//
|
|
176
|
+
'& .cm-table-head': {
|
|
177
|
+
padding: '2px 16px 2px 0px',
|
|
178
|
+
borderBottom: `1px solid ${get(tokens, 'extend.colors.neutral.500')}`,
|
|
179
|
+
fontWeight: 100,
|
|
180
|
+
textAlign: 'left',
|
|
181
|
+
color: get(tokens, 'extend.colors.neutral.500'),
|
|
182
|
+
},
|
|
183
|
+
'& .cm-table-cell': {
|
|
184
|
+
padding: '2px 16px 2px 0px',
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
//
|
|
188
|
+
// image
|
|
189
|
+
//
|
|
190
|
+
'& .cm-image': {
|
|
191
|
+
margin: '0.5rem 0',
|
|
192
|
+
},
|
|
141
193
|
|
|
142
194
|
//
|
|
143
195
|
// font size
|
|
@@ -159,6 +211,18 @@ export const textTheme: {
|
|
|
159
211
|
'& .cm-scroller': {
|
|
160
212
|
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
161
213
|
},
|
|
214
|
+
'& .cm-placeholder': {
|
|
215
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export const markdownTheme: {
|
|
220
|
+
[selector: string]: StyleSpec;
|
|
221
|
+
} = {
|
|
222
|
+
// NOTE: Must leave base font family as is (i.e., monospace) due to fenced code blocks.
|
|
223
|
+
'& .cm-placeholder': {
|
|
224
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
225
|
+
},
|
|
162
226
|
};
|
|
163
227
|
|
|
164
228
|
export const codeTheme: {
|
|
@@ -167,4 +231,7 @@ export const codeTheme: {
|
|
|
167
231
|
'& .cm-scroller': {
|
|
168
232
|
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
169
233
|
},
|
|
234
|
+
'& .cm-placeholder': {
|
|
235
|
+
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
236
|
+
},
|
|
170
237
|
};
|
|
@@ -2,20 +2,24 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
// TODO(burdon): Additional copyright?
|
|
6
|
+
|
|
5
7
|
import { type EditorView } from '@codemirror/view';
|
|
6
8
|
|
|
7
9
|
import { next as automerge } from '@dxos/automerge/automerge';
|
|
8
10
|
|
|
9
|
-
import amToCodemirror from './amToCodemirror';
|
|
10
|
-
import codeMirrorToAm from './codeMirrorToAm';
|
|
11
11
|
import { type IDocHandle } from './handle';
|
|
12
12
|
import { type Field, isReconcileTx, getPath, reconcileAnnotationType, updateHeads, getLastHeads } from './plugin';
|
|
13
|
+
import { automergeToCodemirror, codemirrorToAutomerge } from './translation';
|
|
13
14
|
|
|
14
15
|
type Doc<T> = automerge.Doc<T>;
|
|
15
16
|
type Heads = automerge.Heads;
|
|
16
17
|
|
|
17
18
|
type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* TODO(burdon): Comment.
|
|
22
|
+
*/
|
|
19
23
|
export class PatchSemaphore {
|
|
20
24
|
_field!: Field;
|
|
21
25
|
_inReconcile = false;
|
|
@@ -39,7 +43,7 @@ export class PatchSemaphore {
|
|
|
39
43
|
|
|
40
44
|
const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
|
|
41
45
|
|
|
42
|
-
// First undo all the unreconciled transactions
|
|
46
|
+
// First undo all the unreconciled transactions.
|
|
43
47
|
const toInvert = transactions.slice().reverse();
|
|
44
48
|
for (const tx of toInvert) {
|
|
45
49
|
const inverted = tx.changes.invert(tx.startState.doc);
|
|
@@ -50,19 +54,18 @@ export class PatchSemaphore {
|
|
|
50
54
|
});
|
|
51
55
|
}
|
|
52
56
|
|
|
53
|
-
//
|
|
54
|
-
let newHeads =
|
|
57
|
+
// Apply the unreconciled transactions to the document.
|
|
58
|
+
let newHeads = codemirrorToAutomerge(this._field, handle, transactions, view.state);
|
|
55
59
|
|
|
56
|
-
// NOTE: null and undefined each come from automerge and repo respectively
|
|
60
|
+
// NOTE: null and undefined each come from automerge and repo respectively.
|
|
57
61
|
if (newHeads === null || newHeads === undefined) {
|
|
58
62
|
// TODO: @alexjg this is the call that's resetting the editor state on click
|
|
59
63
|
newHeads = automerge.getHeads(handle.docSync()!);
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
//
|
|
63
|
-
// and apply that to the codemirror doc
|
|
66
|
+
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
64
67
|
const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
|
|
65
|
-
|
|
68
|
+
automergeToCodemirror(view, selection, path, diff);
|
|
66
69
|
|
|
67
70
|
view.dispatch({
|
|
68
71
|
effects: updateHeads(newHeads),
|
|
@@ -12,7 +12,8 @@ import React, { useEffect, useRef, useState } from 'react';
|
|
|
12
12
|
import { type Prop } from '@dxos/automerge/automerge';
|
|
13
13
|
import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
|
|
14
14
|
|
|
15
|
-
import { type IDocHandle
|
|
15
|
+
import { type IDocHandle } from './handle';
|
|
16
|
+
import { automergePlugin } from './plugin';
|
|
16
17
|
|
|
17
18
|
type EditorProps = {
|
|
18
19
|
handle: IDocHandle;
|
|
@@ -81,9 +82,10 @@ const Story = () => {
|
|
|
81
82
|
};
|
|
82
83
|
|
|
83
84
|
export default {
|
|
84
|
-
title: 'Automerge',
|
|
85
|
+
title: 'react-ui-editor/Automerge',
|
|
86
|
+
component: Editor,
|
|
85
87
|
};
|
|
86
88
|
|
|
87
|
-
export const
|
|
89
|
+
export const Default = {
|
|
88
90
|
render: () => <Story />,
|
|
89
91
|
};
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
// TODO(burdon): Additional copyright?
|
|
6
|
+
|
|
5
7
|
import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
|
|
6
8
|
import { type EditorView } from '@codemirror/view';
|
|
7
9
|
|
|
@@ -14,9 +16,14 @@ import {
|
|
|
14
16
|
type SpliceTextPatch,
|
|
15
17
|
} from '@dxos/automerge/automerge';
|
|
16
18
|
|
|
17
|
-
import { reconcileAnnotationType } from '
|
|
19
|
+
import { reconcileAnnotationType } from '../plugin';
|
|
18
20
|
|
|
19
|
-
export
|
|
21
|
+
export const automergeToCodemirror = (
|
|
22
|
+
view: EditorView,
|
|
23
|
+
selection: EditorSelection,
|
|
24
|
+
target: Prop[],
|
|
25
|
+
patches: Patch[],
|
|
26
|
+
) => {
|
|
20
27
|
for (const patch of patches) {
|
|
21
28
|
const changeSpec = handlePatch(patch, target, view.state);
|
|
22
29
|
if (changeSpec != null) {
|
|
@@ -28,6 +35,7 @@ export default (view: EditorView, selection: EditorSelection, target: Prop[], pa
|
|
|
28
35
|
});
|
|
29
36
|
}
|
|
30
37
|
}
|
|
38
|
+
|
|
31
39
|
view.dispatch({
|
|
32
40
|
selection,
|
|
33
41
|
annotations: reconcileAnnotationType.of({}),
|
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
// TODO(burdon): Additional copyright?
|
|
6
|
+
|
|
5
7
|
import { type EditorState, 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 '
|
|
11
|
+
import { type IDocHandle } from '../handle';
|
|
12
|
+
import { type Field } from '../plugin';
|
|
11
13
|
|
|
12
|
-
export
|
|
14
|
+
export const codemirrorToAutomerge = (
|
|
13
15
|
field: Field,
|
|
14
16
|
handle: IDocHandle,
|
|
15
17
|
transactions: Transaction[],
|
|
@@ -18,7 +20,7 @@ export default (
|
|
|
18
20
|
const { lastHeads, path } = state.field(field);
|
|
19
21
|
|
|
20
22
|
// 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.
|
|
23
|
+
// Otherwise, later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
|
|
22
24
|
let hasChanges = false;
|
|
23
25
|
for (const tr of transactions) {
|
|
24
26
|
tr.changes.iterChanges(() => {
|
|
@@ -37,5 +39,6 @@ export default (
|
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
41
|
});
|
|
42
|
+
|
|
40
43
|
return newHeads ?? undefined;
|
|
41
44
|
};
|