@dxos/react-ui-editor 0.4.8-main.3a03fb7 → 0.4.8-main.46f461d
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 +215 -86
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -0
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +11 -5
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +4 -2
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.stories.d.ts +1 -0
- package/dist/types/src/extensions/automerge/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/factories.d.ts +3 -0
- package/dist/types/src/extensions/factories.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/formatting.d.ts +7 -4
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/image.d.ts +6 -0
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -1
- package/dist/types/src/extensions/modes.d.ts +1 -1
- package/dist/types/src/extensions/modes.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -1
- package/dist/types/src/hooks/useDocAccessor.d.ts +4 -2
- package/dist/types/src/hooks/useDocAccessor.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextEditor.stories.d.ts +1 -0
- package/dist/types/src/hooks/useTextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +1 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/package.json +26 -24
- package/src/components/TextEditor/TextEditor.tsx +2 -2
- package/src/components/Toolbar/Toolbar.stories.tsx +23 -24
- package/src/components/Toolbar/Toolbar.tsx +101 -35
- package/src/extensions/automerge/automerge.ts +4 -4
- package/src/extensions/comments.ts +2 -2
- package/src/extensions/factories.ts +30 -7
- package/src/extensions/markdown/bundle.ts +8 -11
- package/src/extensions/markdown/formatting.test.ts +13 -13
- package/src/extensions/markdown/formatting.ts +95 -87
- package/src/extensions/markdown/image.ts +6 -0
- package/src/extensions/modes.ts +8 -1
- package/src/hooks/useActionHandler.ts +5 -1
- package/src/hooks/useDocAccessor.ts +8 -2
- package/src/hooks/useTextEditor.ts +3 -0
- package/src/themes/default.ts +24 -8
- package/src/translations.ts +1 -0
|
@@ -9,20 +9,20 @@ import { expect } from 'chai';
|
|
|
9
9
|
import { describe, test } from '@dxos/test';
|
|
10
10
|
|
|
11
11
|
import {
|
|
12
|
-
|
|
12
|
+
addBlockquote,
|
|
13
|
+
addCodeblock,
|
|
14
|
+
addLink,
|
|
15
|
+
addList,
|
|
13
16
|
addStyle,
|
|
17
|
+
getFormatting,
|
|
14
18
|
removeStyle,
|
|
15
|
-
addLink,
|
|
16
19
|
removeLink,
|
|
17
|
-
addList,
|
|
18
20
|
removeList,
|
|
19
|
-
addBlockquote,
|
|
20
21
|
removeBlockquote,
|
|
21
|
-
addCodeblock,
|
|
22
22
|
removeCodeblock,
|
|
23
|
+
setHeading,
|
|
23
24
|
Inline,
|
|
24
25
|
List,
|
|
25
|
-
getFormatting,
|
|
26
26
|
type Formatting,
|
|
27
27
|
} from './formatting';
|
|
28
28
|
|
|
@@ -204,22 +204,22 @@ describe('removeStyle', () => {
|
|
|
204
204
|
});
|
|
205
205
|
|
|
206
206
|
describe('addLink', () => {
|
|
207
|
-
testCommand('adds a link', '{}', addLink, '[]({})');
|
|
207
|
+
testCommand('adds a link', '{}', addLink(), '[]({})');
|
|
208
208
|
|
|
209
|
-
testCommand('adds a link around text', 'hello {world}', addLink, 'hello [world]({})');
|
|
209
|
+
testCommand('adds a link around text', 'hello {world}', addLink(), 'hello [world]({})');
|
|
210
210
|
|
|
211
|
-
testCommand('clears existing links', '[hello {world}](foo)', addLink, 'hello [world]({})');
|
|
211
|
+
testCommand('clears existing links', '[hello {world}](foo)', addLink(), 'hello [world]({})');
|
|
212
212
|
|
|
213
|
-
testCommand('does nothing across blocks', '{one\n\ntwo}', addLink, null);
|
|
213
|
+
testCommand('does nothing across blocks', '{one\n\ntwo}', addLink(), null);
|
|
214
214
|
|
|
215
|
-
testCommand('does nothing in code blocks', '```\n{one}\n```', addLink, null);
|
|
215
|
+
testCommand('does nothing in code blocks', '```\n{one}\n```', addLink(), null);
|
|
216
216
|
|
|
217
|
-
testCommand('patches up overlapping styles before', '*foo {bar* baz}', addLink, '*foo* [*bar* baz]({})');
|
|
217
|
+
testCommand('patches up overlapping styles before', '*foo {bar* baz}', addLink(), '*foo* [*bar* baz]({})');
|
|
218
218
|
|
|
219
219
|
testCommand(
|
|
220
220
|
'patches up overlapping styles after',
|
|
221
221
|
'one {two ~~three} four~~',
|
|
222
|
-
addLink,
|
|
222
|
+
addLink(),
|
|
223
223
|
'one [two ~~three~~]({}) ~~four~~',
|
|
224
224
|
);
|
|
225
225
|
});
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
} from '@codemirror/state';
|
|
16
16
|
import { EditorView, keymap } from '@codemirror/view';
|
|
17
17
|
import { type SyntaxNodeRef, type SyntaxNode } from '@lezer/common';
|
|
18
|
-
import {
|
|
18
|
+
import { useMemo, useState } from 'react';
|
|
19
19
|
|
|
20
20
|
// Markdown refs:
|
|
21
21
|
// https://github.github.com/gfm
|
|
@@ -56,7 +56,7 @@ export type Formatting = {
|
|
|
56
56
|
listStyle: null | 'ordered' | 'bullet' | 'task';
|
|
57
57
|
};
|
|
58
58
|
|
|
59
|
-
export const
|
|
59
|
+
export const formattingEquals = (a: Formatting, b: Formatting) =>
|
|
60
60
|
a.blockType === b.blockType &&
|
|
61
61
|
a.strong === b.strong &&
|
|
62
62
|
a.emphasis === b.emphasis &&
|
|
@@ -470,90 +470,94 @@ export const removeLink: StateCommand = ({ state, dispatch }) => {
|
|
|
470
470
|
};
|
|
471
471
|
|
|
472
472
|
// Add link markup around the selection
|
|
473
|
-
export const addLink
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
473
|
+
export const addLink =
|
|
474
|
+
({ url, image }: { url?: string; image?: boolean } = {}): StateCommand =>
|
|
475
|
+
({ state, dispatch }) => {
|
|
476
|
+
const changes = state.changeByRange((range) => {
|
|
477
|
+
let { from, to } = range;
|
|
478
|
+
const cutStyles: SyntaxNode[] = [];
|
|
479
|
+
let okay: boolean | null = null;
|
|
480
|
+
// Check whether this range is in a position where a link makes sense.
|
|
481
|
+
syntaxTree(state).iterate({
|
|
482
|
+
from,
|
|
483
|
+
to,
|
|
484
|
+
enter: (node) => {
|
|
485
|
+
if (Object.hasOwn(Textblocks, node.name)) {
|
|
486
|
+
// If the selection spans multiple textblocks or is in a code block, abort.
|
|
487
|
+
okay =
|
|
488
|
+
Textblocks[node.name] !== 'codeblock' &&
|
|
489
|
+
from >= blockContentStart(node) &&
|
|
490
|
+
to <= blockContentEnd(node, state.doc);
|
|
491
|
+
} else if (Object.hasOwn(InlineMarker, node.name)) {
|
|
492
|
+
// Look for inline styles that partially overlap the range.
|
|
493
|
+
// Expand the range over them if they start directly outside, otherwise mark them for later.
|
|
494
|
+
const sNode = node.node;
|
|
495
|
+
if (node.from < from && node.to <= to) {
|
|
496
|
+
if (sNode.firstChild!.to === from) {
|
|
497
|
+
from = node.from;
|
|
498
|
+
} else {
|
|
499
|
+
cutStyles.push(sNode);
|
|
500
|
+
}
|
|
501
|
+
} else if (node.from >= from && node.to > to) {
|
|
502
|
+
if (sNode.lastChild!.from === to) {
|
|
503
|
+
to = node.to;
|
|
504
|
+
} else {
|
|
505
|
+
cutStyles.push(sNode);
|
|
506
|
+
}
|
|
506
507
|
}
|
|
507
508
|
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
});
|
|
509
|
+
},
|
|
510
|
+
});
|
|
511
511
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
512
|
+
if (okay === null) {
|
|
513
|
+
// No textblock found around selection. Check if the rest of the line is empty.
|
|
514
|
+
const line = state.doc.lineAt(from);
|
|
515
|
+
okay = to <= line.to && !/\S/.test(line.text.slice(from - line.from));
|
|
516
|
+
}
|
|
517
|
+
if (!okay) {
|
|
518
|
+
return { range };
|
|
519
|
+
}
|
|
520
520
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
521
|
+
const changes: ChangeSpec[] = [];
|
|
522
|
+
// Some changes must be moved to end of change array so that they are applied in the right order
|
|
523
|
+
const changesAfter: ChangeSpec[] = [];
|
|
524
|
+
// Clear existing links.
|
|
525
|
+
removeLinkInner(from, to, changesAfter, state);
|
|
526
|
+
let cursorOffset = 1;
|
|
527
|
+
// Close and reopen inline styles that partially overlap the range.
|
|
528
|
+
for (const style of cutStyles) {
|
|
529
|
+
const type = InlineMarker[style.name];
|
|
530
|
+
const mark = inlineMarkerText(type);
|
|
531
|
+
if (style.from < from) {
|
|
532
|
+
// Extends before.
|
|
533
|
+
changes.push({ from: skipSpaces(from, state.doc, -1), insert: mark });
|
|
534
|
+
changesAfter.push({ from: skipSpaces(from, state.doc, 1, to), insert: mark });
|
|
535
|
+
} else {
|
|
536
|
+
changes.push({ from: skipSpaces(to, state.doc, -1, from), insert: mark });
|
|
537
|
+
const after = skipSpaces(to, state.doc, 1);
|
|
538
|
+
if (after === to) {
|
|
539
|
+
cursorOffset += mark.length;
|
|
540
|
+
}
|
|
541
|
+
changesAfter.push({ from: after, insert: mark });
|
|
540
542
|
}
|
|
541
|
-
changesAfter.push({ from: after, insert: mark });
|
|
542
543
|
}
|
|
544
|
+
|
|
545
|
+
// Add the link markup.
|
|
546
|
+
changes.push({ from, insert: image ? '` });
|
|
547
|
+
const changeSet = state.changes(changes.concat(changesAfter));
|
|
548
|
+
// Put the cursor between the title or parenthesis.
|
|
549
|
+
return {
|
|
550
|
+
changes: changeSet,
|
|
551
|
+
range: EditorSelection.cursor(changeSet.mapPos(to, 1) - cursorOffset - (url ? url.length + 2 : 0)),
|
|
552
|
+
};
|
|
553
|
+
});
|
|
554
|
+
if (changes.changes.empty) {
|
|
555
|
+
return false;
|
|
543
556
|
}
|
|
544
|
-
// Add the link markup.
|
|
545
|
-
changes.push({ from, insert: '[' }, { from: to, insert: ']()' });
|
|
546
|
-
const changeSet = state.changes(changes.concat(changesAfter));
|
|
547
|
-
// Put the cursor between the parenthesis.
|
|
548
|
-
return { changes: changeSet, range: EditorSelection.cursor(changeSet.mapPos(to, 1) - cursorOffset) };
|
|
549
|
-
});
|
|
550
|
-
if (changes.changes.empty) {
|
|
551
|
-
return false;
|
|
552
|
-
}
|
|
553
557
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
};
|
|
558
|
+
dispatch(state.update(changes, { userEvent: 'format.link.add', scrollIntoView: true }));
|
|
559
|
+
return true;
|
|
560
|
+
};
|
|
557
561
|
|
|
558
562
|
//
|
|
559
563
|
// Lists
|
|
@@ -1057,7 +1061,7 @@ export const getFormatting = (state: EditorState): Formatting => {
|
|
|
1057
1061
|
const inline: (boolean | null)[] = [null, null, null, null];
|
|
1058
1062
|
let link: boolean = false;
|
|
1059
1063
|
let blockQuote: boolean | null = null;
|
|
1060
|
-
// False indicates mixed list styles
|
|
1064
|
+
// False indicates mixed list styles.
|
|
1061
1065
|
let listStyle: Formatting['listStyle'] | null | false = null;
|
|
1062
1066
|
|
|
1063
1067
|
// Track block context for list/blockquote handling.
|
|
@@ -1216,21 +1220,25 @@ export const getFormatting = (state: EditorState): Formatting => {
|
|
|
1216
1220
|
};
|
|
1217
1221
|
|
|
1218
1222
|
/**
|
|
1219
|
-
* Hook
|
|
1223
|
+
* Hook provides an extension to compute the current formatting state.
|
|
1220
1224
|
*/
|
|
1221
|
-
export const useFormattingState = (): [Formatting |
|
|
1222
|
-
const [state, setState] = useState<Formatting
|
|
1225
|
+
export const useFormattingState = (): [Formatting | undefined, Extension] => {
|
|
1226
|
+
const [state, setState] = useState<Formatting>();
|
|
1227
|
+
|
|
1223
1228
|
const observer = useMemo(
|
|
1224
1229
|
() =>
|
|
1225
1230
|
EditorView.updateListener.of((update) => {
|
|
1226
1231
|
if (update.docChanged || update.selectionSet) {
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1232
|
+
setState((prevState) => {
|
|
1233
|
+
const newState = getFormatting(update.state);
|
|
1234
|
+
if (!prevState || !formattingEquals(prevState, newState)) {
|
|
1235
|
+
return newState;
|
|
1236
|
+
}
|
|
1237
|
+
return prevState;
|
|
1238
|
+
});
|
|
1231
1239
|
}
|
|
1232
1240
|
}),
|
|
1233
|
-
[],
|
|
1241
|
+
[setState],
|
|
1234
1242
|
);
|
|
1235
1243
|
|
|
1236
1244
|
return [state, observer];
|
package/src/extensions/modes.ts
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
import { type Extension, Facet } from '@codemirror/state';
|
|
6
6
|
import { keymap } from '@codemirror/view';
|
|
7
7
|
import { vim } from '@replit/codemirror-vim';
|
|
8
|
+
import { vscodeKeymap } from '@replit/codemirror-vscode-keymap';
|
|
8
9
|
|
|
9
10
|
export const focusEvent = 'focus.container';
|
|
10
11
|
|
|
11
|
-
export type EditorMode = 'default' | 'vim' | undefined;
|
|
12
|
+
export type EditorMode = 'default' | 'vim' | 'vscode' | undefined;
|
|
12
13
|
|
|
13
14
|
export type EditorConfig = {
|
|
14
15
|
type: string;
|
|
@@ -21,7 +22,13 @@ export const editorMode = Facet.define<EditorConfig, EditorConfig>({
|
|
|
21
22
|
|
|
22
23
|
export const EditorModes: { [mode: string]: Extension } = {
|
|
23
24
|
default: [],
|
|
25
|
+
vscode: [
|
|
26
|
+
// https://github.com/replit/codemirror-vscode-keymap
|
|
27
|
+
editorMode.of({ type: 'vscode' }),
|
|
28
|
+
keymap.of(vscodeKeymap),
|
|
29
|
+
],
|
|
24
30
|
vim: [
|
|
31
|
+
// https://github.com/replit/codemirror-vim
|
|
25
32
|
vim(),
|
|
26
33
|
editorMode.of({ type: 'vim', noTabster: true }),
|
|
27
34
|
keymap.of([
|
|
@@ -75,7 +75,11 @@ export const useActionHandler = (view?: EditorView | null): ToolbarProps['onActi
|
|
|
75
75
|
break;
|
|
76
76
|
|
|
77
77
|
case 'link':
|
|
78
|
-
(action.data === false ? removeLink : addLink)(view);
|
|
78
|
+
(action.data === false ? removeLink : addLink())(view);
|
|
79
|
+
break;
|
|
80
|
+
|
|
81
|
+
case 'image':
|
|
82
|
+
addLink({ url: action.data, image: true })(view);
|
|
79
83
|
break;
|
|
80
84
|
|
|
81
85
|
case 'comment':
|
|
@@ -4,11 +4,17 @@
|
|
|
4
4
|
|
|
5
5
|
import { useMemo } from 'react';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
createDocAccessor,
|
|
9
|
+
type DocAccessor,
|
|
10
|
+
getTextContent,
|
|
11
|
+
type TextObject,
|
|
12
|
+
type EchoReactiveObject,
|
|
13
|
+
} from '@dxos/echo-schema';
|
|
8
14
|
|
|
9
15
|
// TODO(burdon): Factor out.
|
|
10
16
|
export const useDocAccessor = <T = any>(
|
|
11
|
-
text: TextObject
|
|
17
|
+
text: TextObject | EchoReactiveObject<{ content: string }>,
|
|
12
18
|
): { id: string; doc: string | undefined; accessor: DocAccessor<T> } => {
|
|
13
19
|
return useMemo(
|
|
14
20
|
() => ({
|
|
@@ -33,6 +33,8 @@ export const useTextEditor = (cb: () => UseTextEditorProps = () => ({}), deps: D
|
|
|
33
33
|
useEffect(() => {
|
|
34
34
|
let view: EditorView;
|
|
35
35
|
if (parentRef.current) {
|
|
36
|
+
log('create', { id });
|
|
37
|
+
|
|
36
38
|
// https://codemirror.net/docs/ref/#state.EditorStateConfig
|
|
37
39
|
const state = EditorState.create({
|
|
38
40
|
doc,
|
|
@@ -69,6 +71,7 @@ export const useTextEditor = (cb: () => UseTextEditorProps = () => ({}), deps: D
|
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
return () => {
|
|
74
|
+
log('destroy', { id });
|
|
72
75
|
view?.destroy();
|
|
73
76
|
};
|
|
74
77
|
}, deps);
|
package/src/themes/default.ts
CHANGED
|
@@ -236,28 +236,44 @@ export const defaultTheme: ThemeStyles = {
|
|
|
236
236
|
* </div>
|
|
237
237
|
* </div
|
|
238
238
|
*/
|
|
239
|
-
'.cm-panels': {
|
|
240
|
-
border: `1px solid ${get(tokens, 'extend.colors.neutral.200')}`,
|
|
241
|
-
},
|
|
239
|
+
'.cm-panels': {},
|
|
242
240
|
'.cm-panel': {
|
|
243
|
-
background: get(tokens, 'extend.colors.neutral.50'),
|
|
244
241
|
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
245
242
|
},
|
|
243
|
+
'.cm-panel input[type=checkbox]': {
|
|
244
|
+
marginRight: '0.4rem !important',
|
|
245
|
+
},
|
|
246
|
+
'&light .cm-panel': {
|
|
247
|
+
background: get(tokens, 'extend.colors.neutral.50'),
|
|
248
|
+
},
|
|
249
|
+
'&dark .cm-panel': {
|
|
250
|
+
background: get(tokens, 'extend.colors.neutral.850'),
|
|
251
|
+
},
|
|
246
252
|
'.cm-button': {
|
|
247
253
|
margin: '4px',
|
|
248
254
|
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
249
|
-
background: get(tokens, 'extend.colors.neutral.100'),
|
|
250
255
|
backgroundImage: 'none',
|
|
251
256
|
border: 'none',
|
|
257
|
+
'&:active': {
|
|
258
|
+
backgroundImage: 'none',
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
'&light .cm-button': {
|
|
262
|
+
background: get(tokens, 'extend.colors.neutral.100'),
|
|
252
263
|
'&:hover': {
|
|
253
264
|
background: get(tokens, 'extend.colors.neutral.200'),
|
|
254
265
|
},
|
|
255
266
|
'&:active': {
|
|
256
267
|
background: get(tokens, 'extend.colors.neutral.300'),
|
|
257
|
-
backgroundImage: 'none',
|
|
258
268
|
},
|
|
259
269
|
},
|
|
260
|
-
'.cm-
|
|
261
|
-
|
|
270
|
+
'&dark .cm-button': {
|
|
271
|
+
background: get(tokens, 'extend.colors.neutral.800'),
|
|
272
|
+
'&:hover': {
|
|
273
|
+
background: get(tokens, 'extend.colors.neutral.700'),
|
|
274
|
+
},
|
|
275
|
+
'&:active': {
|
|
276
|
+
background: get(tokens, 'extend.colors.neutral.600'),
|
|
277
|
+
},
|
|
262
278
|
},
|
|
263
279
|
};
|
package/src/translations.ts
CHANGED