@dxos/react-ui-editor 0.3.10-main.3645708 → 0.3.10-main.396fa89
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 +1495 -489
- 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 +26 -2
- 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/TextEditor.stories.d.ts +3 -0
- package/dist/types/src/components/TextEditor/TextEditor.stories.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/code.d.ts +2 -0
- package/dist/types/src/components/TextEditor/extensions/code.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 +1 -1
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/highlight.d.ts +24 -0
- package/dist/types/src/components/TextEditor/extensions/highlight.d.ts.map +1 -0
- 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 +8 -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 +10 -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 +9 -3
- 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 +8 -3
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/{components/TextEditor → hooks/yjs}/yjs.stories.d.ts +2 -2
- 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 +22 -19
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +161 -36
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -1
- package/src/components/TextEditor/TextEditor.tsx +49 -24
- package/src/components/TextEditor/extensions/autocomplete.ts +9 -22
- package/src/components/TextEditor/extensions/basic.ts +6 -4
- package/src/components/TextEditor/extensions/blast.ts +373 -0
- package/src/components/TextEditor/extensions/code.ts +99 -0
- package/src/components/TextEditor/extensions/comments.ts +173 -0
- package/src/components/TextEditor/extensions/demo.ts +69 -0
- package/src/components/TextEditor/extensions/experimental.tsx +15 -35
- package/src/components/TextEditor/extensions/highlight.ts +128 -0
- package/src/components/TextEditor/extensions/image.ts +67 -0
- package/src/components/TextEditor/extensions/index.ts +8 -0
- package/src/components/TextEditor/extensions/link.ts +27 -27
- package/src/components/TextEditor/extensions/listener.ts +4 -3
- package/src/components/TextEditor/extensions/markdown/bundle.ts +46 -43
- package/src/components/TextEditor/extensions/markdown/highlight.ts +148 -107
- package/src/components/TextEditor/extensions/mention.ts +31 -0
- package/src/components/TextEditor/extensions/table.ts +119 -0
- package/src/components/TextEditor/extensions/tasklist.ts +89 -81
- package/src/components/TextEditor/themes/default.ts +112 -4
- package/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.ts +12 -9
- package/src/{automerge → hooks/automerge}/automerge.stories.tsx +6 -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 +58 -41
- package/src/{components/TextEditor → hooks/yjs}/yjs.stories.tsx +2 -2
- package/src/styles/markdown.ts +15 -8
- package/src/testing/replicator.ts +7 -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,119 @@
|
|
|
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
|
+
const cursor = state.selection.main.head;
|
|
36
|
+
|
|
37
|
+
const tables: Table[] = [];
|
|
38
|
+
const getTable = () => tables[tables.length - 1];
|
|
39
|
+
const getRow = () => {
|
|
40
|
+
const table = getTable();
|
|
41
|
+
return table.rows?.[table.rows.length - 1];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Parse table.
|
|
45
|
+
syntaxTree(state).iterate({
|
|
46
|
+
enter: (node) => {
|
|
47
|
+
// Check if cursor is inside text.
|
|
48
|
+
switch (node.name) {
|
|
49
|
+
case 'Table': {
|
|
50
|
+
tables.push({ from: node.from, to: node.to });
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case 'TableHeader': {
|
|
54
|
+
getTable().header = [];
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
case 'TableRow': {
|
|
58
|
+
(getTable().rows ??= []).push([]);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case 'TableCell': {
|
|
62
|
+
const row = getRow();
|
|
63
|
+
if (row) {
|
|
64
|
+
row.push(state.sliceDoc(node.from, node.to));
|
|
65
|
+
} else {
|
|
66
|
+
getTable().header?.push(state.sliceDoc(node.from, node.to));
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
tables.forEach((table) => {
|
|
75
|
+
const hide = state.readOnly || cursor < table.from || cursor > table.to;
|
|
76
|
+
hide && builder.add(table.from, table.to, Decoration.replace({ widget: new TableWidget(table) }));
|
|
77
|
+
builder.add(table.from, table.to, Decoration.mark({ class: 'cm-table' }));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return builder.finish();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
class TableWidget extends WidgetType {
|
|
84
|
+
constructor(readonly _table: Table) {
|
|
85
|
+
super();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
override eq(other: WidgetType) {
|
|
89
|
+
return this._table.from === (other as any as TableWidget)?._table?.from;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
toDOM(view: EditorView) {
|
|
93
|
+
const table = document.createElement('table');
|
|
94
|
+
|
|
95
|
+
{
|
|
96
|
+
const header = table.appendChild(document.createElement('thead'));
|
|
97
|
+
const tr = header.appendChild(document.createElement('tr'));
|
|
98
|
+
this._table.header?.forEach((cell) => {
|
|
99
|
+
const th = document.createElement('th');
|
|
100
|
+
th.setAttribute('class', 'cm-table-head');
|
|
101
|
+
tr.appendChild(th).textContent = cell;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
{
|
|
106
|
+
const body = table.appendChild(document.createElement('tbody'));
|
|
107
|
+
this._table.rows?.forEach((row) => {
|
|
108
|
+
const tr = body.appendChild(document.createElement('tr'));
|
|
109
|
+
row.forEach((cell) => {
|
|
110
|
+
const td = document.createElement('td');
|
|
111
|
+
td.setAttribute('class', 'cm-table-cell');
|
|
112
|
+
tr.appendChild(td).textContent = cell;
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return table;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -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';
|
|
14
|
+
|
|
15
|
+
// TODO(burdon): Reconcile with theme.
|
|
16
|
+
const styles = EditorView.baseTheme({
|
|
17
|
+
'& .cm-task-item': {
|
|
18
|
+
paddingLeft: '4px',
|
|
19
|
+
paddingRight: '8px',
|
|
20
|
+
},
|
|
21
|
+
});
|
|
15
22
|
|
|
16
23
|
class CheckboxWidget extends WidgetType {
|
|
17
|
-
constructor(
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly _pos: number,
|
|
26
|
+
private _checked: boolean,
|
|
27
|
+
private readonly _indent: number,
|
|
28
|
+
private readonly _onCheck?: (check: boolean) => void,
|
|
29
|
+
) {
|
|
18
30
|
super();
|
|
19
31
|
}
|
|
20
32
|
|
|
21
33
|
override eq(other: CheckboxWidget) {
|
|
22
|
-
|
|
23
|
-
// TODO(burdon): Is this efficient? Check life-cycle.
|
|
24
|
-
return this._pos === other._pos && this._checked === other._checked;
|
|
34
|
+
return this._pos === other._pos && this._checked === other._checked && this._indent === other._indent;
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
toDOM(view: EditorView) {
|
|
28
|
-
// console.log('toDom', this._pos);
|
|
29
38
|
const wrap = document.createElement('span');
|
|
30
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
31
39
|
wrap.className = 'cm-task-item';
|
|
40
|
+
wrap.setAttribute('aria-hidden', 'true');
|
|
41
|
+
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
32
42
|
|
|
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
|
-
};
|
|
43
|
+
const input = wrap.appendChild(document.createElement('input'));
|
|
44
|
+
input.type = 'checkbox';
|
|
45
|
+
input.checked = this._checked;
|
|
46
|
+
if (!this._onCheck) {
|
|
47
|
+
input.setAttribute('disabled', 'true');
|
|
48
|
+
}
|
|
49
|
+
if (this._onCheck) {
|
|
50
|
+
input.onchange = (event: Event) => {
|
|
51
|
+
this._onCheck?.((event.target as any).checked);
|
|
52
|
+
return true;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
52
55
|
|
|
53
56
|
return wrap;
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
// TODO(burdon): Remove listener?
|
|
57
|
-
// override destroy() {
|
|
58
|
-
// console.log('destroy', this._pos);
|
|
59
|
-
// }
|
|
60
|
-
|
|
61
59
|
override ignoreEvent() {
|
|
62
60
|
return false;
|
|
63
61
|
}
|
|
64
62
|
}
|
|
65
63
|
|
|
66
|
-
|
|
67
|
-
return state.sliceDoc(pos, pos + 3).toLowerCase() === '[x]';
|
|
68
|
-
};
|
|
64
|
+
export type TasklistOptions = {};
|
|
69
65
|
|
|
70
|
-
export const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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: (value) => value.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
|
*/
|
|
@@ -50,10 +55,10 @@ export const defaultTheme: {
|
|
|
50
55
|
//
|
|
51
56
|
// Cursor
|
|
52
57
|
//
|
|
53
|
-
'&light .cm-cursor': {
|
|
58
|
+
'&light .cm-cursor, &light .cm-dropCursor': {
|
|
54
59
|
borderLeft: '2px solid black',
|
|
55
60
|
},
|
|
56
|
-
'&dark .cm-cursor': {
|
|
61
|
+
'&dark .cm-cursor, &dark .cm-dropCursor': {
|
|
57
62
|
borderLeft: '2px solid white',
|
|
58
63
|
},
|
|
59
64
|
'& .cm-placeholder': {
|
|
@@ -136,8 +141,59 @@ 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 *': {
|
|
177
|
+
fontFamily: `${get(tokens, 'fontFamily.mono', []).join(',')} !important`,
|
|
178
|
+
textDecoration: 'none !important',
|
|
179
|
+
},
|
|
180
|
+
'& .cm-table-head': {
|
|
181
|
+
padding: '2px 16px 2px 0px',
|
|
182
|
+
borderBottom: `1px solid ${get(tokens, 'extend.colors.neutral.500')}`,
|
|
183
|
+
fontWeight: 100,
|
|
184
|
+
textAlign: 'left',
|
|
185
|
+
color: get(tokens, 'extend.colors.neutral.500'),
|
|
186
|
+
},
|
|
187
|
+
'& .cm-table-cell': {
|
|
188
|
+
padding: '2px 16px 2px 0px',
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
//
|
|
192
|
+
// image
|
|
193
|
+
//
|
|
194
|
+
'& .cm-image': {
|
|
195
|
+
margin: '0.5rem 0',
|
|
196
|
+
},
|
|
141
197
|
|
|
142
198
|
//
|
|
143
199
|
// font size
|
|
@@ -151,6 +207,43 @@ export const defaultTheme: {
|
|
|
151
207
|
acc[`& .text-${fontSize} + .cm-widgetBuffer + .cm-ySelectionCaret`] = { height };
|
|
152
208
|
return acc;
|
|
153
209
|
}, {}),
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Panels
|
|
213
|
+
* https://github.com/codemirror/search/blob/main/src/search.ts#L745
|
|
214
|
+
*
|
|
215
|
+
* Find/replace panel.
|
|
216
|
+
* <div class="cm-announced">...</div>
|
|
217
|
+
* <div class="cm-scroller">...</div>
|
|
218
|
+
* <div class="cm-panels cm-panels-bottom">
|
|
219
|
+
* <div class="cm-search cm-panel">
|
|
220
|
+
* <input class="cm-textfield" />
|
|
221
|
+
* <button class="cm-button">...</button>
|
|
222
|
+
* <label><input type="checkbox" />...</label>
|
|
223
|
+
* </div>
|
|
224
|
+
* </div
|
|
225
|
+
*/
|
|
226
|
+
'& .cm-panels': {
|
|
227
|
+
border: `1px solid ${get(tokens, 'extend.colors.neutral.200')}`,
|
|
228
|
+
borderBottom: 'none',
|
|
229
|
+
},
|
|
230
|
+
'& .cm-panel': {
|
|
231
|
+
background: get(tokens, 'extend.colors.neutral.50'),
|
|
232
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
233
|
+
},
|
|
234
|
+
'& .cm-button': {
|
|
235
|
+
margin: '4px',
|
|
236
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
237
|
+
background: get(tokens, 'extend.colors.neutral.100'),
|
|
238
|
+
border: 'none',
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
'& .cm-searchMatch': {
|
|
242
|
+
backgroundColor: get(tokens, 'extend.colors.yellow.100'),
|
|
243
|
+
},
|
|
244
|
+
'& .cm-searchMatch.cm-searchMatch-selected': {
|
|
245
|
+
backgroundColor: get(tokens, 'extend.colors.primary.100'),
|
|
246
|
+
},
|
|
154
247
|
};
|
|
155
248
|
|
|
156
249
|
export const textTheme: {
|
|
@@ -159,6 +252,18 @@ export const textTheme: {
|
|
|
159
252
|
'& .cm-scroller': {
|
|
160
253
|
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
161
254
|
},
|
|
255
|
+
'& .cm-placeholder': {
|
|
256
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export const markdownTheme: {
|
|
261
|
+
[selector: string]: StyleSpec;
|
|
262
|
+
} = {
|
|
263
|
+
// NOTE: Must leave base font family as is (i.e., monospace) due to fenced code blocks.
|
|
264
|
+
'& .cm-placeholder': {
|
|
265
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
266
|
+
},
|
|
162
267
|
};
|
|
163
268
|
|
|
164
269
|
export const codeTheme: {
|
|
@@ -167,4 +272,7 @@ export const codeTheme: {
|
|
|
167
272
|
'& .cm-scroller': {
|
|
168
273
|
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
169
274
|
},
|
|
275
|
+
'& .cm-placeholder': {
|
|
276
|
+
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
277
|
+
},
|
|
170
278
|
};
|
|
@@ -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),
|
|
@@ -9,10 +9,12 @@ import { basicSetup } from 'codemirror';
|
|
|
9
9
|
import get from 'lodash.get';
|
|
10
10
|
import React, { useEffect, useRef, useState } from 'react';
|
|
11
11
|
|
|
12
|
+
// TODO(burdon): Why separate imports?
|
|
12
13
|
import { type Prop } from '@dxos/automerge/automerge';
|
|
13
14
|
import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
|
|
14
15
|
|
|
15
|
-
import { type IDocHandle
|
|
16
|
+
import { type IDocHandle } from './handle';
|
|
17
|
+
import { automergePlugin } from './plugin';
|
|
16
18
|
|
|
17
19
|
type EditorProps = {
|
|
18
20
|
handle: IDocHandle;
|
|
@@ -81,9 +83,10 @@ const Story = () => {
|
|
|
81
83
|
};
|
|
82
84
|
|
|
83
85
|
export default {
|
|
84
|
-
title: 'Automerge',
|
|
86
|
+
title: 'react-ui-editor/Automerge',
|
|
87
|
+
component: Editor,
|
|
85
88
|
};
|
|
86
89
|
|
|
87
|
-
export const
|
|
90
|
+
export const Default = {
|
|
88
91
|
render: () => <Story />,
|
|
89
92
|
};
|
|
@@ -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
|
};
|