@dxos/react-ui-editor 0.3.11-main.4eab977 → 0.3.11-main.508f304
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 +779 -760
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +16 -13
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -17
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -6
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +26 -0
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +7 -9
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/index.d.ts +0 -1
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -12
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts +2 -0
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +31 -18
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/{util → extensions}/cursor.d.ts +10 -2
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +2 -0
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +2 -5
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/mention.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner.d.ts +4 -0
- package/dist/types/src/extensions/outliner.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -1
- package/dist/types/src/hooks/automerge.d.ts +2 -1
- package/dist/types/src/hooks/automerge.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +28 -0
- package/dist/types/src/hooks/defs.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 +2 -32
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/yjs.d.ts +2 -1
- package/dist/types/src/hooks/yjs.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +7 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +32 -31
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +77 -79
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +124 -92
- package/src/components/TextEditor/comments.stories.tsx +357 -0
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.ts +40 -46
- package/src/extensions/automerge/cursor.ts +2 -1
- package/src/extensions/automerge/defs.ts +10 -31
- package/src/extensions/automerge/index.ts +0 -1
- package/src/extensions/automerge/semaphore.ts +23 -20
- package/src/extensions/automerge/update-automerge.ts +2 -2
- package/src/extensions/awareness.ts +45 -34
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/code.ts +24 -18
- package/src/extensions/comments.ts +242 -234
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/hr.ts +49 -50
- package/src/extensions/image.ts +2 -2
- package/src/extensions/index.ts +2 -0
- package/src/extensions/link.ts +7 -7
- package/src/extensions/markdown/bundle.ts +18 -24
- package/src/extensions/markdown/highlight.ts +0 -8
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/extensions/table.ts +2 -2
- package/src/extensions/tasklist.ts +78 -86
- package/src/extensions/yjs/cursor.ts +2 -1
- package/src/extensions/yjs/yjs.test.ts +1 -1
- package/src/extensions/yjs/yjs.ts +2 -2
- package/src/hooks/automerge.ts +2 -1
- package/src/hooks/defs.ts +47 -0
- package/src/hooks/index.ts +3 -1
- package/src/hooks/useTextModel.ts +3 -49
- package/src/hooks/yjs.ts +2 -23
- package/src/index.ts +1 -0
- package/src/styles/markdown.ts +0 -2
- package/src/themes/default.ts +17 -50
- package/src/util.ts +41 -0
- package/dist/types/src/util/cursor.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -3
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/util.d.ts +0 -5
- package/dist/types/src/util/util.d.ts.map +0 -1
- package/src/util/cursor.ts +0 -29
- package/src/util/index.ts +0 -6
- package/src/util/util.ts +0 -18
package/src/extensions/hr.ts
CHANGED
|
@@ -2,72 +2,71 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
EditorView,
|
|
9
|
-
MatchDecorator,
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type ViewUpdate,
|
|
12
|
-
WidgetType,
|
|
13
|
-
} from '@codemirror/view';
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
|
|
14
8
|
|
|
15
9
|
import { getToken } from '../styles';
|
|
16
10
|
|
|
17
11
|
// TODO(burdon): Reconcile with theme.
|
|
18
12
|
const styles = EditorView.baseTheme({
|
|
19
13
|
'& .cm-hr': {
|
|
20
|
-
// TODO(burdon): ???
|
|
21
14
|
// Note that block-level decorations should not have vertical margins,
|
|
22
|
-
|
|
15
|
+
borderTop: `1px solid ${getToken('extend.colors.neutral.200')}`,
|
|
23
16
|
},
|
|
24
17
|
});
|
|
25
18
|
|
|
26
19
|
class HorizontalRuleWidget extends WidgetType {
|
|
27
|
-
|
|
28
|
-
super();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
override eq(other: WidgetType) {
|
|
32
|
-
return this._pos === (other as any as HorizontalRuleWidget)._pos;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
toDOM(view: EditorView) {
|
|
36
|
-
// TODO(burdon): Create <hr> element? Does this clash with markdown parser?
|
|
20
|
+
override toDOM() {
|
|
37
21
|
const el = document.createElement('div');
|
|
38
22
|
el.className = 'cm-hr';
|
|
39
23
|
return el;
|
|
40
24
|
}
|
|
41
25
|
}
|
|
42
26
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
27
|
+
const decoration = Decoration.replace({ widget: new HorizontalRuleWidget() });
|
|
28
|
+
|
|
29
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
30
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
31
|
+
const { state } = view;
|
|
32
|
+
const cursor = state.selection.main.head;
|
|
33
|
+
|
|
34
|
+
for (const { from, to } of view.visibleRanges) {
|
|
35
|
+
syntaxTree(state).iterate({
|
|
36
|
+
enter: (node) => {
|
|
37
|
+
if (node.name === 'HorizontalRule') {
|
|
38
|
+
// Check if cursor is inside text.
|
|
39
|
+
if (cursor <= node.from || cursor >= node.to) {
|
|
40
|
+
builder.add(node.from, node.to, decoration);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
from,
|
|
45
|
+
to,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return builder.finish();
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const hr = () => {
|
|
53
|
+
return [
|
|
54
|
+
styles,
|
|
55
|
+
ViewPlugin.fromClass(
|
|
56
|
+
class {
|
|
57
|
+
decorations: DecorationSet;
|
|
50
58
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
class {
|
|
55
|
-
rules: DecorationSet;
|
|
56
|
-
constructor(view: EditorView) {
|
|
57
|
-
this.rules = placeholderMatcher.createDeco(view);
|
|
58
|
-
}
|
|
59
|
+
constructor(view: EditorView) {
|
|
60
|
+
this.decorations = buildDecorations(view);
|
|
61
|
+
}
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}),
|
|
71
|
-
},
|
|
72
|
-
),
|
|
73
|
-
];
|
|
63
|
+
update(update: ViewUpdate) {
|
|
64
|
+
this.decorations = buildDecorations(update.view);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
decorations: (v) => v.decorations,
|
|
69
|
+
},
|
|
70
|
+
),
|
|
71
|
+
];
|
|
72
|
+
};
|
package/src/extensions/image.ts
CHANGED
|
@@ -72,11 +72,11 @@ class ImageWidget extends WidgetType {
|
|
|
72
72
|
super();
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
override eq(other:
|
|
75
|
+
override eq(other: this) {
|
|
76
76
|
return this._url === (other as any as ImageWidget)._url;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
toDOM(view: EditorView) {
|
|
79
|
+
override toDOM(view: EditorView) {
|
|
80
80
|
const img = document.createElement('img');
|
|
81
81
|
img.setAttribute('src', this._url);
|
|
82
82
|
img.setAttribute('class', 'cm-image');
|
package/src/extensions/index.ts
CHANGED
|
@@ -9,12 +9,14 @@ export * from './basic';
|
|
|
9
9
|
export * from './blast';
|
|
10
10
|
export * from './code';
|
|
11
11
|
export * from './comments';
|
|
12
|
+
export * from './cursor';
|
|
12
13
|
export * from './hr';
|
|
13
14
|
export * from './image';
|
|
14
15
|
export * from './link';
|
|
15
16
|
export * from './listener';
|
|
16
17
|
export * from './markdown';
|
|
17
18
|
export * from './mention';
|
|
19
|
+
export * from './outliner';
|
|
18
20
|
export * from './table';
|
|
19
21
|
export * from './tasklist';
|
|
20
22
|
export * from './typewriter';
|
package/src/extensions/link.ts
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
import { syntaxTree } from '@codemirror/language';
|
|
7
7
|
import { type Extension, RangeSetBuilder } from '@codemirror/state';
|
|
8
8
|
import {
|
|
9
|
+
hoverTooltip,
|
|
9
10
|
Decoration,
|
|
11
|
+
type DecorationSet,
|
|
10
12
|
type EditorView,
|
|
11
|
-
WidgetType,
|
|
12
|
-
hoverTooltip,
|
|
13
13
|
ViewPlugin,
|
|
14
14
|
type ViewUpdate,
|
|
15
|
-
|
|
15
|
+
WidgetType,
|
|
16
16
|
} from '@codemirror/view';
|
|
17
17
|
import { type SyntaxNode } from '@lezer/common';
|
|
18
18
|
|
|
@@ -93,11 +93,11 @@ class LinkButton extends WidgetType {
|
|
|
93
93
|
super();
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
override eq(other:
|
|
96
|
+
override eq(other: this) {
|
|
97
97
|
return this._url === other._url;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
toDOM(view: EditorView) {
|
|
100
|
+
override toDOM(view: EditorView) {
|
|
101
101
|
const el = document.createElement('span');
|
|
102
102
|
this._onAttach!(el, this._url);
|
|
103
103
|
return el;
|
|
@@ -109,11 +109,11 @@ class LinkText extends WidgetType {
|
|
|
109
109
|
super();
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
override eq(other:
|
|
112
|
+
override eq(other: this) {
|
|
113
113
|
return this._url === other._url;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
toDOM(view: EditorView) {
|
|
116
|
+
override toDOM(view: EditorView) {
|
|
117
117
|
const link = document.createElement('a');
|
|
118
118
|
link.setAttribute('class', 'cm-link');
|
|
119
119
|
link.textContent = this._text;
|
|
@@ -23,18 +23,15 @@ import {
|
|
|
23
23
|
import type { ThemeMode } from '@dxos/react-ui';
|
|
24
24
|
|
|
25
25
|
import { markdownHighlightStyle, markdownTagsExtensions } from './highlight';
|
|
26
|
+
import { type TextEditorProps } from '../../components';
|
|
26
27
|
|
|
27
28
|
export type MarkdownBundleOptions = {
|
|
28
|
-
readonly?: boolean;
|
|
29
29
|
themeMode?: ThemeMode;
|
|
30
|
-
|
|
31
|
-
};
|
|
30
|
+
} & Pick<TextEditorProps, 'readonly' | 'placeholder'>;
|
|
32
31
|
|
|
33
32
|
/**
|
|
34
33
|
* Markdown bundle.
|
|
35
34
|
* Refs:
|
|
36
|
-
* https://github.com/codemirror/basic-setup
|
|
37
|
-
* https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts#L50
|
|
38
35
|
* https://codemirror.net/docs/community
|
|
39
36
|
* https://codemirror.net/docs/ref/#codemirror.basicSetup
|
|
40
37
|
*/
|
|
@@ -49,8 +46,7 @@ export const markdownBundle = ({
|
|
|
49
46
|
EditorState.tabSize.of(2),
|
|
50
47
|
EditorView.lineWrapping,
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
// https://github.com/codemirror/basic-setup
|
|
54
50
|
bracketMatching(),
|
|
55
51
|
closeBrackets(),
|
|
56
52
|
crosshairCursor(),
|
|
@@ -59,7 +55,7 @@ export const markdownBundle = ({
|
|
|
59
55
|
highlightActiveLine(),
|
|
60
56
|
history(),
|
|
61
57
|
indentOnInput(),
|
|
62
|
-
_placeholder
|
|
58
|
+
_placeholder ? placeholder(_placeholder) : [],
|
|
63
59
|
|
|
64
60
|
// Main extension.
|
|
65
61
|
// https://github.com/codemirror/lang-markdown
|
|
@@ -87,20 +83,18 @@ export const markdownBundle = ({
|
|
|
87
83
|
|
|
88
84
|
// Custom styles.
|
|
89
85
|
syntaxHighlighting(markdownHighlightStyle(readonly)),
|
|
90
|
-
].filter(Boolean) as Extension[];
|
|
91
|
-
};
|
|
92
86
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
87
|
+
keymap.of([
|
|
88
|
+
// https://codemirror.net/docs/ref/#commands.indentWithTab
|
|
89
|
+
indentWithTab,
|
|
90
|
+
// https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
|
|
91
|
+
...closeBracketsKeymap,
|
|
92
|
+
// https://codemirror.net/docs/ref/#commands.historyKeymap
|
|
93
|
+
...historyKeymap,
|
|
94
|
+
// https://codemirror.net/docs/ref/#search.searchKeymap
|
|
95
|
+
...searchKeymap,
|
|
96
|
+
// https://codemirror.net/docs/ref/#commands.standardKeymap
|
|
97
|
+
...standardKeymap,
|
|
98
|
+
]),
|
|
99
|
+
];
|
|
100
|
+
};
|
|
@@ -31,7 +31,6 @@ export const markdownTags = {
|
|
|
31
31
|
CodeText: Tag.define(),
|
|
32
32
|
EmphasisMark: Tag.define(),
|
|
33
33
|
HeaderMark: Tag.define(),
|
|
34
|
-
// HorizontalRule: Tag.define(),
|
|
35
34
|
InlineCode: Tag.define(),
|
|
36
35
|
LinkLabel: Tag.define(),
|
|
37
36
|
LinkReference: Tag.define(),
|
|
@@ -177,13 +176,6 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
177
176
|
class: blockquote,
|
|
178
177
|
},
|
|
179
178
|
|
|
180
|
-
// TODO(burdon): Replace with extension.
|
|
181
|
-
// {
|
|
182
|
-
// tag: [markdownTags.HorizontalRule],
|
|
183
|
-
// class: mx(horizontalRule, readonly && '-indent-[100rem]'),
|
|
184
|
-
// },
|
|
185
|
-
|
|
186
|
-
// TODO(burdon): Only if being edited.
|
|
187
179
|
{
|
|
188
180
|
tag: [markdownTags.TableCell],
|
|
189
181
|
class: 'font-mono',
|
|
@@ -9,18 +9,25 @@ export type MentionOptions = {
|
|
|
9
9
|
onSearch: (text: string) => string[];
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
// TODO(burdon): Can only have a single autocompletion. Merge configuration with autocomplete.
|
|
12
13
|
export const mention = ({ onSearch }: MentionOptions): Extension => {
|
|
13
14
|
return autocompletion({
|
|
15
|
+
// TODO(burdon): Not working.
|
|
16
|
+
activateOnTyping: true,
|
|
17
|
+
// activateOnTypingDelay: 100,
|
|
18
|
+
// selectOnOpen: true,
|
|
14
19
|
closeOnBlur: false, // For debugging.
|
|
20
|
+
// defaultKeymap: false,
|
|
21
|
+
icons: false,
|
|
15
22
|
override: [
|
|
16
23
|
(context: CompletionContext): CompletionResult | null => {
|
|
24
|
+
console.log(context);
|
|
25
|
+
|
|
17
26
|
const match = context.matchBefore(/@(\w+)?/);
|
|
18
27
|
if (!match || (match.from === match.to && !context.explicit)) {
|
|
19
28
|
return null;
|
|
20
29
|
}
|
|
21
30
|
|
|
22
|
-
console.log(match);
|
|
23
|
-
|
|
24
31
|
return {
|
|
25
32
|
from: match.from,
|
|
26
33
|
options: onSearch(match.text.slice(1).toLowerCase()).map((value) => ({ label: `@${value}` })),
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension } from '@codemirror/state';
|
|
6
|
+
|
|
7
|
+
export type OutlinerOptions = {};
|
|
8
|
+
|
|
9
|
+
// TODO(burdon): Experimental component: multiple hierarchical draggable editor sections.
|
|
10
|
+
export const outliner = (options: OutlinerOptions = {}): Extension => {
|
|
11
|
+
return [];
|
|
12
|
+
};
|
package/src/extensions/table.ts
CHANGED
|
@@ -97,14 +97,14 @@ class TableWidget extends WidgetType {
|
|
|
97
97
|
super();
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
override eq(other:
|
|
100
|
+
override eq(other: this) {
|
|
101
101
|
return (
|
|
102
102
|
this._table.header?.join() === other._table.header?.join() &&
|
|
103
103
|
this._table.rows?.join() === other._table.rows?.join()
|
|
104
104
|
);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
toDOM(view: EditorView) {
|
|
107
|
+
override toDOM(view: EditorView) {
|
|
108
108
|
const table = document.createElement('table');
|
|
109
109
|
|
|
110
110
|
{
|
|
@@ -2,57 +2,53 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type DecorationSet,
|
|
12
|
-
type ViewUpdate,
|
|
13
|
-
} from '@codemirror/view';
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
|
|
8
|
+
|
|
9
|
+
import { getToken } from '../styles';
|
|
14
10
|
|
|
15
11
|
// TODO(burdon): Reconcile with theme.
|
|
16
12
|
const styles = EditorView.baseTheme({
|
|
17
|
-
'& .cm-task
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
'& .cm-task': {
|
|
14
|
+
color: getToken('extend.colors.blue.500'),
|
|
15
|
+
},
|
|
16
|
+
'& .cm-task-checkbox': {
|
|
17
|
+
marginLeft: '4px',
|
|
18
|
+
marginRight: '4px',
|
|
20
19
|
},
|
|
21
20
|
});
|
|
22
21
|
|
|
23
22
|
class CheckboxWidget extends WidgetType {
|
|
24
|
-
constructor(
|
|
25
|
-
private _checked: boolean,
|
|
26
|
-
private readonly _indent: number,
|
|
27
|
-
private readonly _onCheck?: (check: boolean) => void,
|
|
28
|
-
) {
|
|
23
|
+
constructor(private _checked: boolean) {
|
|
29
24
|
super();
|
|
30
25
|
}
|
|
31
26
|
|
|
32
|
-
override eq(other:
|
|
33
|
-
return this._checked === other._checked
|
|
27
|
+
override eq(other: this) {
|
|
28
|
+
return this._checked === other._checked;
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
toDOM(view: EditorView) {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
40
|
-
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
41
|
-
|
|
42
|
-
const input = wrap.appendChild(document.createElement('input'));
|
|
31
|
+
override toDOM(view: EditorView) {
|
|
32
|
+
const input = document.createElement('input');
|
|
33
|
+
input.className = 'cm-task-checkbox';
|
|
43
34
|
input.type = 'checkbox';
|
|
44
35
|
input.checked = this._checked;
|
|
45
|
-
if (
|
|
36
|
+
if (view.state.readOnly) {
|
|
46
37
|
input.setAttribute('disabled', 'true');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
} else {
|
|
39
|
+
input.onmousedown = (event: Event) => {
|
|
40
|
+
const pos = view.posAtDOM(input);
|
|
41
|
+
const text = view.state.sliceDoc(pos, pos + 3);
|
|
42
|
+
if (text === (this._checked ? '[x]' : '[ ]')) {
|
|
43
|
+
view.dispatch({
|
|
44
|
+
changes: { from: pos + 1, to: pos + 2, insert: this._checked ? ' ' : 'x' },
|
|
45
|
+
});
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
}
|
|
52
48
|
};
|
|
53
49
|
}
|
|
54
50
|
|
|
55
|
-
return
|
|
51
|
+
return input;
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
override ignoreEvent() {
|
|
@@ -60,60 +56,56 @@ class CheckboxWidget extends WidgetType {
|
|
|
60
56
|
}
|
|
61
57
|
}
|
|
62
58
|
|
|
59
|
+
const checkedDecoration = Decoration.replace({ widget: new CheckboxWidget(true) });
|
|
60
|
+
const uncheckedDecoration = Decoration.replace({ widget: new CheckboxWidget(false) });
|
|
61
|
+
|
|
62
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
63
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
64
|
+
const { state } = view;
|
|
65
|
+
const cursor = state.selection.main.head;
|
|
66
|
+
|
|
67
|
+
for (const { from, to } of view.visibleRanges) {
|
|
68
|
+
syntaxTree(state).iterate({
|
|
69
|
+
enter: (node) => {
|
|
70
|
+
if (node.name === 'TaskMarker') {
|
|
71
|
+
// Check if cursor is inside text.
|
|
72
|
+
if (cursor < node.from || cursor > node.to) {
|
|
73
|
+
const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
|
|
74
|
+
builder.add(node.from - 2, node.from - 1, Decoration.mark({ class: 'cm-task' }));
|
|
75
|
+
builder.add(node.from, node.to, checked ? checkedDecoration : uncheckedDecoration);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
from,
|
|
80
|
+
to,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return builder.finish();
|
|
85
|
+
};
|
|
86
|
+
|
|
63
87
|
export type TasklistOptions = {};
|
|
64
88
|
|
|
65
89
|
export const tasklist = (options: TasklistOptions = {}) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
88
|
-
selection: {
|
|
89
|
-
anchor: idx + 3,
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
},
|
|
93
|
-
),
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
const tasks = ViewPlugin.fromClass(
|
|
99
|
-
class {
|
|
100
|
-
tasks: DecorationSet;
|
|
101
|
-
constructor(view: EditorView) {
|
|
102
|
-
this.tasks = taskMatcher.createDeco(view);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
update(update: ViewUpdate) {
|
|
106
|
-
this.tasks = taskMatcher.updateDeco(update, this.tasks);
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
decorations: (value) => value.tasks,
|
|
111
|
-
provide: (plugin) =>
|
|
112
|
-
EditorView.atomicRanges.of((view) => {
|
|
113
|
-
return view.plugin(plugin)?.tasks || Decoration.none;
|
|
114
|
-
}),
|
|
115
|
-
},
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
return [tasks, styles];
|
|
90
|
+
return [
|
|
91
|
+
ViewPlugin.fromClass(
|
|
92
|
+
class {
|
|
93
|
+
decorations: DecorationSet;
|
|
94
|
+
|
|
95
|
+
constructor(view: EditorView) {
|
|
96
|
+
this.decorations = buildDecorations(view);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
update(update: ViewUpdate) {
|
|
100
|
+
if (update.docChanged || update.viewportChanged || update.selectionSet) {
|
|
101
|
+
this.decorations = buildDecorations(update.view);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
decorations: (v) => v.decorations,
|
|
107
|
+
},
|
|
108
|
+
),
|
|
109
|
+
styles,
|
|
110
|
+
];
|
|
119
111
|
};
|
|
@@ -6,12 +6,13 @@ import * as Y from 'yjs';
|
|
|
6
6
|
|
|
7
7
|
import { arrayToString, stringToArray } from '@dxos/util';
|
|
8
8
|
|
|
9
|
-
import { type CursorConverter } from '
|
|
9
|
+
import { type CursorConverter } from '../cursor';
|
|
10
10
|
|
|
11
11
|
export const cursorConverter = (text: Y.Text): CursorConverter => ({
|
|
12
12
|
toCursor: (index, assoc) => {
|
|
13
13
|
return arrayToString(Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text, index, assoc)));
|
|
14
14
|
},
|
|
15
|
+
|
|
15
16
|
fromCursor: (cursor) => {
|
|
16
17
|
return (
|
|
17
18
|
Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(cursor)), text.doc!)?.index ??
|
|
@@ -12,7 +12,7 @@ import { type YText } from '@dxos/text-model';
|
|
|
12
12
|
describe('YJS', () => {
|
|
13
13
|
// https://docs.yjs.dev/api/shared-types/y.text
|
|
14
14
|
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
15
|
-
test('absolute position', () => {
|
|
15
|
+
test.skip('absolute position', () => {
|
|
16
16
|
const obj = new TextObject('hello world');
|
|
17
17
|
const index = obj.text.indexOf('world');
|
|
18
18
|
const relPos = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(obj.content as YText, index));
|
|
@@ -8,9 +8,9 @@ import type * as YP from 'y-protocols/awareness';
|
|
|
8
8
|
import type { YText } from '@dxos/text-model';
|
|
9
9
|
|
|
10
10
|
import { cursorConverter } from './cursor';
|
|
11
|
-
import {
|
|
11
|
+
import { Cursor } from '../cursor';
|
|
12
12
|
|
|
13
13
|
export const yjs = (content: YText, awareness?: YP.Awareness) => [
|
|
14
|
-
|
|
14
|
+
Cursor.converter.of(cursorConverter(content)),
|
|
15
15
|
yCollab(content, awareness),
|
|
16
16
|
];
|
package/src/hooks/automerge.ts
CHANGED
|
@@ -8,7 +8,8 @@ import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
|
8
8
|
import { isNotNullOrUndefined } from '@dxos/util';
|
|
9
9
|
|
|
10
10
|
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
11
|
-
import { type EditorModel, modelState
|
|
11
|
+
import { type EditorModel, modelState } from './defs';
|
|
12
|
+
import { type UseTextModelProps } from './useTextModel';
|
|
12
13
|
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
13
14
|
import { cursorColor } from '../styles';
|
|
14
15
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension, StateField } from '@codemirror/state';
|
|
6
|
+
import type * as YP from 'y-protocols/awareness';
|
|
7
|
+
|
|
8
|
+
import { type DocAccessor } from '@dxos/echo-schema';
|
|
9
|
+
import type { YText, YXmlFragment } from '@dxos/text-model';
|
|
10
|
+
|
|
11
|
+
// Runtime data structure.
|
|
12
|
+
export type Range = {
|
|
13
|
+
from: number;
|
|
14
|
+
to: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// Persistent data structure.
|
|
18
|
+
export type Comment = {
|
|
19
|
+
id: string;
|
|
20
|
+
cursor?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type EditorModel = {
|
|
24
|
+
id: string;
|
|
25
|
+
text: () => string;
|
|
26
|
+
extension?: Extension;
|
|
27
|
+
|
|
28
|
+
// TODO(burdon): Remove.
|
|
29
|
+
content: string | YText | YXmlFragment | DocAccessor;
|
|
30
|
+
|
|
31
|
+
// TODO(burdon): Move into extension.
|
|
32
|
+
awareness?: YP.Awareness;
|
|
33
|
+
|
|
34
|
+
// TODO(burdon): Remove.
|
|
35
|
+
peer?: {
|
|
36
|
+
id: string;
|
|
37
|
+
name?: string;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* State field makes the model available to other extensions.
|
|
43
|
+
*/
|
|
44
|
+
export const modelState = StateField.define<EditorModel | undefined>({
|
|
45
|
+
create: () => undefined,
|
|
46
|
+
update: (model) => model,
|
|
47
|
+
});
|