@dxos/react-ui-editor 0.3.10-main.dcfebef → 0.3.10-main.e948ee0
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 +1179 -372
- 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 +23 -2
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +2 -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.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 +2 -2
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts +2 -1
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +1 -1
- 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 +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.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 -1
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/themes/default.d.ts +5 -0
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +1 -1
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +6 -0
- 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 -20
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +124 -32
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -1
- package/src/components/TextEditor/TextEditor.tsx +31 -24
- package/src/components/TextEditor/extensions/autocomplete.ts +5 -20
- 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 +29 -46
- package/src/components/TextEditor/extensions/demo.ts +4 -1
- package/src/components/TextEditor/extensions/experimental.tsx +15 -36
- 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 +6 -0
- package/src/components/TextEditor/extensions/link.ts +17 -25
- package/src/components/TextEditor/extensions/listener.ts +3 -3
- package/src/components/TextEditor/extensions/markdown/bundle.ts +46 -47
- package/src/components/TextEditor/extensions/markdown/highlight.ts +148 -108
- 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 +42 -28
- package/src/components/TextEditor/themes/default.ts +97 -4
- package/src/hooks/automerge/automerge.stories.tsx +1 -0
- package/src/hooks/useTextModel.ts +9 -0
- package/src/{components/TextEditor → hooks/yjs}/yjs.stories.tsx +2 -2
- package/src/styles/markdown.ts +15 -8
- package/src/testing/replicator.ts +1 -0
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
import { markdownLanguage } from '@codemirror/lang-markdown';
|
|
6
6
|
import { HighlightStyle } from '@codemirror/language';
|
|
7
7
|
import { tags, styleTags, Tag } from '@lezer/highlight';
|
|
8
|
-
import { type MarkdownConfig } from '@lezer/markdown';
|
|
8
|
+
import { type MarkdownConfig, Table } from '@lezer/markdown';
|
|
9
9
|
import get from 'lodash.get';
|
|
10
10
|
|
|
11
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
12
|
+
|
|
11
13
|
import {
|
|
12
14
|
blockquote,
|
|
13
15
|
bold,
|
|
@@ -40,128 +42,166 @@ export const markdownTags = {
|
|
|
40
42
|
ListMark: Tag.define(),
|
|
41
43
|
QuoteMark: Tag.define(),
|
|
42
44
|
URL: Tag.define(),
|
|
43
|
-
};
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
// Custom.
|
|
47
|
+
TableCell: Tag.define(),
|
|
47
48
|
};
|
|
48
49
|
|
|
50
|
+
// TODO(burdon): Customize table parser (make all content monospace).
|
|
51
|
+
// https://github.com/lezer-parser/markdown/blob/main/src/extension.ts
|
|
52
|
+
Table.defineNodes?.forEach((node: any) => {
|
|
53
|
+
switch (node?.name) {
|
|
54
|
+
case 'TableCell': {
|
|
55
|
+
node.style = markdownTags.TableCell;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export const markdownTagsExtensions: MarkdownConfig[] = [
|
|
62
|
+
Table,
|
|
63
|
+
{
|
|
64
|
+
props: [styleTags(markdownTags)],
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
|
|
49
68
|
/**
|
|
50
69
|
* Styling based on `lezer` parser tags.
|
|
51
70
|
* https://codemirror.net/examples/styling
|
|
52
71
|
* https://github.com/lezer-parser/highlight
|
|
53
|
-
* https://
|
|
72
|
+
* https://github.com/lezer-parser/highlight/blob/main/src/highlight.ts#L427
|
|
73
|
+
* https://lezer.codemirror.net/docs/ref/#highlight.tags (list of tags)
|
|
74
|
+
*
|
|
75
|
+
* Examples:
|
|
76
|
+
* - https://github.com/codemirror/language/blob/main/src/highlight.ts#L194
|
|
77
|
+
* - https://github.com/codemirror/theme-one-dark/blob/main/src/one-dark.ts#L115
|
|
54
78
|
*/
|
|
55
|
-
export const markdownHighlightStyle =
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
79
|
+
export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
80
|
+
return HighlightStyle.define(
|
|
81
|
+
[
|
|
82
|
+
{
|
|
83
|
+
tag: [
|
|
84
|
+
tags.keyword,
|
|
85
|
+
tags.name,
|
|
86
|
+
tags.deleted,
|
|
87
|
+
tags.character,
|
|
88
|
+
tags.propertyName,
|
|
89
|
+
tags.macroName,
|
|
90
|
+
tags.color,
|
|
91
|
+
tags.constant(tags.name),
|
|
92
|
+
tags.standard(tags.name),
|
|
93
|
+
tags.definition(tags.name),
|
|
94
|
+
tags.separator,
|
|
95
|
+
tags.typeName,
|
|
96
|
+
tags.className,
|
|
97
|
+
tags.number,
|
|
98
|
+
tags.changed,
|
|
99
|
+
tags.annotation,
|
|
100
|
+
tags.modifier,
|
|
101
|
+
tags.self,
|
|
102
|
+
tags.namespace,
|
|
103
|
+
tags.operator,
|
|
104
|
+
tags.operatorKeyword,
|
|
105
|
+
tags.escape,
|
|
106
|
+
tags.regexp,
|
|
107
|
+
tags.special(tags.string),
|
|
108
|
+
tags.meta,
|
|
109
|
+
tags.comment,
|
|
110
|
+
tags.atom,
|
|
111
|
+
tags.bool,
|
|
112
|
+
tags.special(tags.variableName),
|
|
113
|
+
tags.processingInstruction,
|
|
114
|
+
tags.string,
|
|
115
|
+
tags.inserted,
|
|
116
|
+
tags.invalid,
|
|
117
|
+
],
|
|
118
|
+
color: 'inherit !important',
|
|
119
|
+
},
|
|
95
120
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
markdownTags.QuoteMark,
|
|
108
|
-
],
|
|
109
|
-
class: mark,
|
|
110
|
-
},
|
|
121
|
+
// Markdown marks.
|
|
122
|
+
{
|
|
123
|
+
tag: [
|
|
124
|
+
tags.meta,
|
|
125
|
+
tags.processingInstruction,
|
|
126
|
+
markdownTags.LinkLabel,
|
|
127
|
+
markdownTags.LinkReference,
|
|
128
|
+
markdownTags.ListMark,
|
|
129
|
+
],
|
|
130
|
+
class: mark,
|
|
131
|
+
},
|
|
111
132
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
{ tag: tags.heading5, class: heading[5] },
|
|
118
|
-
{ tag: tags.heading6, class: heading[6] },
|
|
133
|
+
// Markdown marks.
|
|
134
|
+
{
|
|
135
|
+
tag: [markdownTags.CodeMark, markdownTags.HeaderMark, markdownTags.QuoteMark, markdownTags.EmphasisMark],
|
|
136
|
+
class: readonly ? 'hidden' : mark,
|
|
137
|
+
},
|
|
119
138
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
139
|
+
// E.g., code block language (after ```).
|
|
140
|
+
{
|
|
141
|
+
tag: [tags.function(tags.variableName), tags.labelName],
|
|
142
|
+
class: readonly ? 'hidden' : codeMark,
|
|
143
|
+
},
|
|
124
144
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
},
|
|
145
|
+
{
|
|
146
|
+
tag: [tags.monospace],
|
|
147
|
+
class: 'font-mono',
|
|
148
|
+
},
|
|
130
149
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
tag:
|
|
134
|
-
class:
|
|
135
|
-
|
|
150
|
+
// Headings.
|
|
151
|
+
{ tag: tags.heading1, class: heading(1, readonly) },
|
|
152
|
+
{ tag: tags.heading2, class: heading(2, readonly) },
|
|
153
|
+
{ tag: tags.heading3, class: heading(3, readonly) },
|
|
154
|
+
{ tag: tags.heading4, class: heading(4, readonly) },
|
|
155
|
+
{ tag: tags.heading5, class: heading(5, readonly) },
|
|
156
|
+
{ tag: tags.heading6, class: heading(6, readonly) },
|
|
136
157
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// IMPORTANT: Therefore, the fenced code block will use the base editor font.
|
|
142
|
-
{
|
|
143
|
-
tag: [markdownTags.CodeText, markdownTags.InlineCode],
|
|
144
|
-
class: code,
|
|
145
|
-
},
|
|
158
|
+
// Emphasis.
|
|
159
|
+
{ tag: tags.emphasis, class: italic },
|
|
160
|
+
{ tag: tags.strong, class: bold },
|
|
161
|
+
{ tag: tags.strikethrough, class: strikethrough },
|
|
146
162
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
163
|
+
// Naked URLs.
|
|
164
|
+
{
|
|
165
|
+
tag: [markdownTags.URL],
|
|
166
|
+
class: inlineUrl,
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
// NOTE: The `markdown` extension configures extensions for `lezer` to parse markdown tokens (incl. below).
|
|
170
|
+
// However, since `codeLanguages` is also defined, the `lezer` will not parse fenced code blocks,
|
|
171
|
+
// when a language is specified. In this case, the syntax highlighting extensions will colorize
|
|
172
|
+
// the code, but all other CSS properties will be inherited.
|
|
173
|
+
// IMPORTANT: Therefore, the fenced code block will use the base editor font.
|
|
174
|
+
{
|
|
175
|
+
tag: [markdownTags.CodeText, markdownTags.InlineCode],
|
|
176
|
+
class: code,
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
{
|
|
180
|
+
tag: [markdownTags.QuoteMark],
|
|
181
|
+
class: blockquote,
|
|
182
|
+
},
|
|
151
183
|
|
|
184
|
+
// TODO(burdon): Replace with extension.
|
|
185
|
+
{
|
|
186
|
+
tag: [markdownTags.HorizontalRule],
|
|
187
|
+
class: mx(horizontalRule, readonly && '-indent-[100rem]'),
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
// TODO(burdon): Only if being edited.
|
|
191
|
+
{
|
|
192
|
+
tag: [markdownTags.TableCell],
|
|
193
|
+
class: 'font-mono',
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
// Main content, paragraphs, etc.
|
|
197
|
+
// {
|
|
198
|
+
// tag: [tags.content],
|
|
199
|
+
// fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
200
|
+
// },
|
|
201
|
+
],
|
|
152
202
|
{
|
|
153
|
-
|
|
154
|
-
|
|
203
|
+
scope: markdownLanguage,
|
|
204
|
+
all: { fontFamily: get(tokens, 'fontFamily.body', []).join(',') },
|
|
155
205
|
},
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
// {
|
|
159
|
-
// tag: [tags.content],
|
|
160
|
-
// fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
161
|
-
// },
|
|
162
|
-
],
|
|
163
|
-
{
|
|
164
|
-
scope: markdownLanguage,
|
|
165
|
-
all: { fontFamily: get(tokens, 'fontFamily.body', []).join(',') },
|
|
166
|
-
},
|
|
167
|
-
);
|
|
206
|
+
);
|
|
207
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { autocompletion, type CompletionContext, type CompletionResult } from '@codemirror/autocomplete';
|
|
6
|
+
import type { Extension } from '@codemirror/state';
|
|
7
|
+
|
|
8
|
+
export type MentionOptions = {
|
|
9
|
+
onSearch: (text: string) => string[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const mention = ({ onSearch }: MentionOptions): Extension => {
|
|
13
|
+
return autocompletion({
|
|
14
|
+
closeOnBlur: false, // For debugging.
|
|
15
|
+
override: [
|
|
16
|
+
(context: CompletionContext): CompletionResult | null => {
|
|
17
|
+
const match = context.matchBefore(/@(\w+)?/);
|
|
18
|
+
if (!match || (match.from === match.to && !context.explicit)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
console.log(match);
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
from: match.from,
|
|
26
|
+
options: onSearch(match.text.slice(1).toLowerCase()).map((value) => ({ label: `@${value}` })),
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
};
|
|
@@ -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
|
+
}
|
|
@@ -12,12 +12,20 @@ import {
|
|
|
12
12
|
type ViewUpdate,
|
|
13
13
|
} from '@codemirror/view';
|
|
14
14
|
|
|
15
|
+
// TODO(burdon): Reconcile with theme.
|
|
16
|
+
const styles = EditorView.baseTheme({
|
|
17
|
+
'& .cm-task-item': {
|
|
18
|
+
paddingLeft: '4px',
|
|
19
|
+
paddingRight: '8px',
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
15
23
|
class CheckboxWidget extends WidgetType {
|
|
16
24
|
constructor(
|
|
17
25
|
private readonly _pos: number,
|
|
18
26
|
private _checked: boolean,
|
|
19
27
|
private readonly _indent: number,
|
|
20
|
-
private readonly _onCheck
|
|
28
|
+
private readonly _onCheck?: (check: boolean) => void,
|
|
21
29
|
) {
|
|
22
30
|
super();
|
|
23
31
|
}
|
|
@@ -35,10 +43,15 @@ class CheckboxWidget extends WidgetType {
|
|
|
35
43
|
const input = wrap.appendChild(document.createElement('input'));
|
|
36
44
|
input.type = 'checkbox';
|
|
37
45
|
input.checked = this._checked;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
+
}
|
|
42
55
|
|
|
43
56
|
return wrap;
|
|
44
57
|
}
|
|
@@ -48,15 +61,9 @@ class CheckboxWidget extends WidgetType {
|
|
|
48
61
|
}
|
|
49
62
|
}
|
|
50
63
|
|
|
51
|
-
|
|
52
|
-
const styles = EditorView.baseTheme({
|
|
53
|
-
'& .cm-task-item': {
|
|
54
|
-
paddingLeft: '4px',
|
|
55
|
-
paddingRight: '8px',
|
|
56
|
-
},
|
|
57
|
-
});
|
|
64
|
+
export type TasklistOptions = {};
|
|
58
65
|
|
|
59
|
-
export const tasklist = () => {
|
|
66
|
+
export const tasklist = (options: TasklistOptions = {}) => {
|
|
60
67
|
// TODO(burdon): Matcher isn't as precise as syntax tree.
|
|
61
68
|
// Allows for indents to be greater than AST would allow.
|
|
62
69
|
const taskMatcher = new MatchDecorator({
|
|
@@ -65,20 +72,27 @@ export const tasklist = () => {
|
|
|
65
72
|
const indent = Math.floor(match[1].length / 2);
|
|
66
73
|
const checked = match[2] === 'x' || match[2] === 'X';
|
|
67
74
|
return Decoration.replace({
|
|
68
|
-
widget: new CheckboxWidget(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
),
|
|
82
96
|
});
|
|
83
97
|
},
|
|
84
98
|
});
|
|
@@ -95,7 +109,7 @@ export const tasklist = () => {
|
|
|
95
109
|
}
|
|
96
110
|
},
|
|
97
111
|
{
|
|
98
|
-
decorations: (
|
|
112
|
+
decorations: (value) => value.tasks,
|
|
99
113
|
provide: (plugin) =>
|
|
100
114
|
EditorView.atomicRanges.of((view) => {
|
|
101
115
|
return view.plugin(plugin)?.tasks || Decoration.none;
|
|
@@ -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: {
|
|
@@ -9,6 +9,7 @@ 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
|
|