@dxos/react-ui-editor 0.4.8-next.fff1521 → 0.4.8
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 +1453 -1449
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +1 -6
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/types/src/extensions/dnd.d.ts +7 -1
- package/dist/types/src/extensions/dnd.d.ts.map +1 -1
- package/dist/types/src/extensions/factories.d.ts +5 -1
- package/dist/types/src/extensions/factories.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/action.d.ts +9 -0
- package/dist/types/src/extensions/markdown/action.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/index.d.ts +1 -0
- package/dist/types/src/extensions/markdown/index.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 +2 -2
- 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/themes/default.d.ts.map +1 -1
- package/package.json +25 -24
- package/src/components/TextEditor/TextEditor.stories.tsx +14 -2
- package/src/components/Toolbar/Toolbar.tsx +8 -27
- package/src/extensions/automerge/automerge.ts +4 -4
- package/src/extensions/command/state.ts +2 -2
- package/src/extensions/dnd.ts +31 -9
- package/src/extensions/factories.ts +41 -15
- package/src/extensions/index.ts +0 -1
- package/src/extensions/markdown/action.ts +112 -0
- package/src/extensions/markdown/bundle.ts +8 -11
- package/src/extensions/markdown/formatting.ts +11 -11
- package/src/extensions/markdown/index.ts +1 -0
- package/src/extensions/modes.ts +8 -1
- package/src/hooks/useActionHandler.ts +4 -89
- package/src/hooks/useDocAccessor.ts +8 -2
- package/src/themes/default.ts +24 -8
- package/dist/types/src/extensions/cursor-line-margin.d.ts +0 -7
- package/dist/types/src/extensions/cursor-line-margin.d.ts.map +0 -1
- package/src/extensions/cursor-line-margin.ts +0 -94
|
@@ -2,96 +2,11 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
|
6
6
|
|
|
7
|
-
import type
|
|
8
|
-
import {
|
|
9
|
-
Inline,
|
|
10
|
-
List,
|
|
11
|
-
addBlockquote,
|
|
12
|
-
addCodeblock,
|
|
13
|
-
addLink,
|
|
14
|
-
addList,
|
|
15
|
-
createComment,
|
|
16
|
-
insertTable,
|
|
17
|
-
removeBlockquote,
|
|
18
|
-
removeCodeblock,
|
|
19
|
-
removeLink,
|
|
20
|
-
removeList,
|
|
21
|
-
setHeading,
|
|
22
|
-
setStyle,
|
|
23
|
-
toggleBlockquote,
|
|
24
|
-
toggleList,
|
|
25
|
-
toggleStyle,
|
|
26
|
-
} from '../extensions';
|
|
7
|
+
import { type ToolbarProps } from '../components';
|
|
8
|
+
import { processAction } from '../extensions';
|
|
27
9
|
|
|
28
10
|
export const useActionHandler = (view?: EditorView | null): ToolbarProps['onAction'] => {
|
|
29
|
-
return (action) =>
|
|
30
|
-
if (!view) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let inlineType, listType;
|
|
35
|
-
switch (action.type) {
|
|
36
|
-
case 'heading':
|
|
37
|
-
setHeading(parseInt(action.data))(view);
|
|
38
|
-
break;
|
|
39
|
-
|
|
40
|
-
case 'strong':
|
|
41
|
-
case 'emphasis':
|
|
42
|
-
case 'strikethrough':
|
|
43
|
-
case 'code':
|
|
44
|
-
inlineType =
|
|
45
|
-
action.type === 'strong'
|
|
46
|
-
? Inline.Strong
|
|
47
|
-
: action.type === 'emphasis'
|
|
48
|
-
? Inline.Emphasis
|
|
49
|
-
: action.type === 'strikethrough'
|
|
50
|
-
? Inline.Strikethrough
|
|
51
|
-
: Inline.Code;
|
|
52
|
-
(typeof action.data === 'boolean' ? setStyle(inlineType, action.data) : toggleStyle(inlineType))(view);
|
|
53
|
-
break;
|
|
54
|
-
|
|
55
|
-
case 'list-ordered':
|
|
56
|
-
case 'list-bullet':
|
|
57
|
-
case 'list-task':
|
|
58
|
-
listType =
|
|
59
|
-
action.type === 'list-ordered' ? List.Ordered : action.type === 'list-bullet' ? List.Bullet : List.Task;
|
|
60
|
-
(action.data === false
|
|
61
|
-
? removeList(listType)
|
|
62
|
-
: action.data === true
|
|
63
|
-
? addList(listType)
|
|
64
|
-
: toggleList(listType))(view);
|
|
65
|
-
break;
|
|
66
|
-
|
|
67
|
-
case 'blockquote':
|
|
68
|
-
(action.data === false ? removeBlockquote : action.data === true ? addBlockquote : toggleBlockquote)(view);
|
|
69
|
-
break;
|
|
70
|
-
case 'codeblock':
|
|
71
|
-
(action.data === false ? removeCodeblock : addCodeblock)(view);
|
|
72
|
-
break;
|
|
73
|
-
case 'table':
|
|
74
|
-
insertTable(view);
|
|
75
|
-
break;
|
|
76
|
-
|
|
77
|
-
case 'link':
|
|
78
|
-
(action.data === false ? removeLink : addLink())(view);
|
|
79
|
-
break;
|
|
80
|
-
|
|
81
|
-
case 'image':
|
|
82
|
-
addLink({ url: action.data, image: true })(view);
|
|
83
|
-
break;
|
|
84
|
-
|
|
85
|
-
case 'comment':
|
|
86
|
-
createComment(view);
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// TODO(burdon): Hack otherwise remains on heading selector.
|
|
91
|
-
setTimeout(() => {
|
|
92
|
-
if (!view.hasFocus) {
|
|
93
|
-
view.focus();
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
};
|
|
11
|
+
return (action) => view && processAction(view, action);
|
|
97
12
|
};
|
|
@@ -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
|
() => ({
|
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
|
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cursor-line-margin.d.ts","sourceRoot":"","sources":["../../../../src/extensions/cursor-line-margin.ts"],"names":[],"mappings":"AAOA,OAAO,EAAoB,KAAK,SAAS,EAAe,MAAM,mBAAmB,CAAC;AAQlF,KAAK,OAAO,GAAG;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAGF,eAAO,MAAM,gBAAgB,aAAa,OAAO,KAAkB,SAyElE,CAAC"}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// Based upon @acheronfail’s implementation, fetched 16 Feb 2024
|
|
6
|
-
// https://discuss.codemirror.net/t/cursorscrollmargin-for-v6/7448/5
|
|
7
|
-
|
|
8
|
-
import { type EditorState, type Extension, Transaction } from '@codemirror/state';
|
|
9
|
-
import { EditorView } from '@codemirror/view';
|
|
10
|
-
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
|
|
13
|
-
const annotation = 'cursorLineMargin';
|
|
14
|
-
const lineAtPos = (s: EditorState, pos: number) => s.doc.lineAt(pos).number;
|
|
15
|
-
|
|
16
|
-
type Options = {
|
|
17
|
-
lines?: number;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// TODO(burdon): Remove. Buggy and creates scrolling problems. Find better (more general) way to deal with HALO notch.
|
|
21
|
-
export const cursorLineMargin = (options: Options = { lines: 3 }): Extension => {
|
|
22
|
-
const { lines } = options;
|
|
23
|
-
if (!lines || lines <= 0) {
|
|
24
|
-
return [];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return EditorView.updateListener.of(({ transactions, state, selectionSet, startState, view }) => {
|
|
28
|
-
// make sure we don't trigger an infinite loop and ignore our own changes
|
|
29
|
-
if (transactions.length < 1 || transactions.some((tr) => tr.isUserEvent(annotation))) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const s = state;
|
|
34
|
-
const { main } = s.selection;
|
|
35
|
-
|
|
36
|
-
// editor rect
|
|
37
|
-
const rect = view.dom.getBoundingClientRect();
|
|
38
|
-
// top and bottom pixel positions of the visible region of the editor
|
|
39
|
-
const viewportTop = rect.top - view.documentTop;
|
|
40
|
-
const viewportBottom = rect.bottom - view.documentTop;
|
|
41
|
-
|
|
42
|
-
// line with main selection
|
|
43
|
-
const mainLine = lineAtPos(s, main.head);
|
|
44
|
-
// top most visible line
|
|
45
|
-
const _visTopLine = lineAtPos(s, view.lineBlockAtHeight(viewportTop).from);
|
|
46
|
-
|
|
47
|
-
// bottom most visible line
|
|
48
|
-
// NOTE: calculating this is slightly more complex - if the editor is
|
|
49
|
-
// larger than all the lines in it, and the `scrollPastEnd()` extension is
|
|
50
|
-
// enabled, then we need to calculate where the bottom most visible line
|
|
51
|
-
// should be if it extended all the way to the bottom of the editor
|
|
52
|
-
const botLineBlk = view.lineBlockAtHeight(viewportBottom);
|
|
53
|
-
const visBotLineDoc = lineAtPos(s, Math.min(botLineBlk.to, s.doc.length));
|
|
54
|
-
const visBotLine =
|
|
55
|
-
botLineBlk.bottom >= viewportBottom
|
|
56
|
-
? visBotLineDoc
|
|
57
|
-
: visBotLineDoc + Math.floor((viewportBottom - botLineBlk.bottom) / view.defaultLineHeight);
|
|
58
|
-
|
|
59
|
-
// const needsScrollTop = mainLine <= visTopLine + cursorLineMargin;
|
|
60
|
-
const needsScrollTop = false;
|
|
61
|
-
const needsScrollBot = mainLine >= visBotLine - lines;
|
|
62
|
-
|
|
63
|
-
// the scroll margins are overlapping
|
|
64
|
-
if (needsScrollTop && needsScrollBot) {
|
|
65
|
-
log.warn('is cursorScrollMargin too large for the editor size?');
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// no need to scroll, we're in between scroll regions
|
|
70
|
-
if (!needsScrollTop && !needsScrollBot) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// if we're within the margin, but moving the other way, then don't scroll
|
|
75
|
-
if (selectionSet) {
|
|
76
|
-
const { head } = startState.selection.main;
|
|
77
|
-
const oldMainLine = lineAtPos(startState, head);
|
|
78
|
-
if (needsScrollTop && oldMainLine < mainLine) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (needsScrollBot && oldMainLine > mainLine) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
view.dispatch({
|
|
87
|
-
annotations: Transaction.userEvent.of(annotation),
|
|
88
|
-
effects: EditorView.scrollIntoView(s.selection.main.head, {
|
|
89
|
-
y: needsScrollTop ? 'start' : 'end',
|
|
90
|
-
yMargin: view.defaultLineHeight * lines,
|
|
91
|
-
}),
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
};
|