@dxos/react-ui-editor 0.8.2-main.5885341 → 0.8.2-main.600d381
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 +1874 -1806
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +1386 -1319
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1874 -1806
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/EditorToolbar/EditorToolbar.d.ts +1 -1
- package/dist/types/src/components/EditorToolbar/EditorToolbar.d.ts.map +1 -1
- package/dist/types/src/{stories/InputMode.stories.d.ts → components/EditorToolbar/EditorToolbar.stories.d.ts} +3 -7
- package/dist/types/src/components/EditorToolbar/EditorToolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/EditorToolbar/blocks.d.ts +4 -3
- package/dist/types/src/components/EditorToolbar/blocks.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/comment.d.ts +4 -3
- package/dist/types/src/components/EditorToolbar/comment.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/formatting.d.ts +4 -3
- package/dist/types/src/components/EditorToolbar/formatting.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/headings.d.ts +4 -3
- package/dist/types/src/components/EditorToolbar/headings.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/image.d.ts +16 -0
- package/dist/types/src/components/EditorToolbar/image.d.ts.map +1 -0
- package/dist/types/src/components/EditorToolbar/lists.d.ts +4 -3
- package/dist/types/src/components/EditorToolbar/lists.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/search.d.ts +17 -0
- package/dist/types/src/components/EditorToolbar/search.d.ts.map +1 -0
- package/dist/types/src/components/EditorToolbar/util.d.ts +11 -17
- package/dist/types/src/components/EditorToolbar/util.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/view-mode.d.ts +4 -3
- package/dist/types/src/components/EditorToolbar/view-mode.d.ts.map +1 -1
- package/dist/types/src/defaults.d.ts.map +1 -1
- package/dist/types/src/extensions/annotations.d.ts.map +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.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -1
- package/dist/types/src/extensions/awareness/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/blast.d.ts.map +1 -1
- package/dist/types/src/extensions/command/command.d.ts.map +1 -1
- package/dist/types/src/extensions/command/hint.d.ts.map +1 -1
- package/dist/types/src/extensions/command/menu.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/debug.d.ts.map +1 -1
- package/dist/types/src/extensions/dnd.d.ts.map +1 -1
- package/dist/types/src/extensions/factories.d.ts.map +1 -1
- package/dist/types/src/extensions/folding.d.ts.map +1 -1
- package/dist/types/src/extensions/listener.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/changes.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/debug.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/decorate.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/link.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/table.d.ts.map +1 -1
- package/dist/types/src/extensions/mention.d.ts.map +1 -1
- package/dist/types/src/extensions/modes.d.ts +5 -5
- package/dist/types/src/extensions/modes.d.ts.map +1 -1
- package/dist/types/src/extensions/preview/preview.d.ts.map +1 -1
- package/dist/types/src/extensions/selection.d.ts.map +1 -1
- package/dist/types/src/extensions/typewriter.d.ts.map +1 -1
- 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/useTextEditor.d.ts.map +1 -1
- package/dist/types/src/stories/story-utils.d.ts.map +1 -1
- package/dist/types/src/util/cursor.d.ts.map +1 -1
- package/dist/types/src/util/debug.d.ts.map +1 -1
- package/dist/types/src/util/dom.d.ts.map +1 -1
- package/dist/types/src/util/facet.d.ts.map +1 -1
- package/dist/types/src/util/react.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +30 -28
- package/src/components/EditorToolbar/EditorToolbar.stories.tsx +90 -0
- package/src/components/EditorToolbar/EditorToolbar.tsx +30 -31
- package/src/components/EditorToolbar/blocks.ts +27 -6
- package/src/components/EditorToolbar/comment.ts +11 -4
- package/src/components/EditorToolbar/formatting.ts +34 -7
- package/src/components/EditorToolbar/headings.ts +9 -8
- package/src/components/EditorToolbar/image.ts +16 -0
- package/src/components/EditorToolbar/lists.ts +26 -7
- package/src/components/EditorToolbar/search.ts +19 -0
- package/src/components/EditorToolbar/util.ts +14 -14
- package/src/components/EditorToolbar/view-mode.ts +9 -8
- package/src/extensions/modes.ts +5 -6
- package/src/hooks/index.ts +0 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +0 -4
- package/dist/types/src/hooks/useActionHandler.d.ts.map +0 -1
- package/dist/types/src/stories/InputMode.stories.d.ts.map +0 -1
- package/src/hooks/useActionHandler.ts +0 -12
- package/src/stories/InputMode.stories.tsx +0 -124
@@ -0,0 +1,90 @@
|
|
1
|
+
//
|
2
|
+
// Copyright 2024 DXOS.org
|
3
|
+
//
|
4
|
+
|
5
|
+
import '@dxos-theme';
|
6
|
+
|
7
|
+
import React, { useCallback, useState } from 'react';
|
8
|
+
|
9
|
+
import { invariant } from '@dxos/invariant';
|
10
|
+
import { useThemeContext } from '@dxos/react-ui';
|
11
|
+
import { attentionSurface, mx } from '@dxos/react-ui-theme';
|
12
|
+
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
13
|
+
|
14
|
+
import { EditorToolbar, useEditorToolbarState } from '..';
|
15
|
+
import {
|
16
|
+
type EditorInputMode,
|
17
|
+
decorateMarkdown,
|
18
|
+
createMarkdownExtensions,
|
19
|
+
formattingKeymap,
|
20
|
+
useFormattingState,
|
21
|
+
createBasicExtensions,
|
22
|
+
createThemeExtensions,
|
23
|
+
InputModeExtensions,
|
24
|
+
type EditorViewMode,
|
25
|
+
} from '../../extensions';
|
26
|
+
import { useTextEditor, type UseTextEditorProps } from '../../hooks';
|
27
|
+
import translations from '../../translations';
|
28
|
+
|
29
|
+
type StoryProps = { placeholder?: string } & UseTextEditorProps;
|
30
|
+
|
31
|
+
const DefaultStory = ({ autoFocus, initialValue, placeholder }: StoryProps) => {
|
32
|
+
const { themeMode } = useThemeContext();
|
33
|
+
const toolbarState = useEditorToolbarState({ viewMode: 'source' });
|
34
|
+
const viewMode = toolbarState.viewMode;
|
35
|
+
const trackFormatting = useFormattingState(toolbarState);
|
36
|
+
// TODO(wittjosiah): Provide way to change the input mode.
|
37
|
+
const [editorInputMode, _setEditorInputMode] = useState<EditorInputMode>('default');
|
38
|
+
const { parentRef, view } = useTextEditor(
|
39
|
+
() => ({
|
40
|
+
autoFocus,
|
41
|
+
initialValue,
|
42
|
+
moveToEndOfLine: true,
|
43
|
+
extensions: [
|
44
|
+
editorInputMode ? InputModeExtensions[editorInputMode] : [],
|
45
|
+
createBasicExtensions({ placeholder, lineWrapping: true, readOnly: viewMode === 'readonly' }),
|
46
|
+
createMarkdownExtensions({ themeMode }),
|
47
|
+
createThemeExtensions({ themeMode, syntaxHighlighting: true }),
|
48
|
+
viewMode === 'source' ? [] : decorateMarkdown(),
|
49
|
+
formattingKeymap(),
|
50
|
+
trackFormatting,
|
51
|
+
],
|
52
|
+
}),
|
53
|
+
[editorInputMode, viewMode, themeMode, placeholder],
|
54
|
+
);
|
55
|
+
|
56
|
+
const getView = useCallback(() => {
|
57
|
+
invariant(view);
|
58
|
+
return view;
|
59
|
+
}, [view]);
|
60
|
+
|
61
|
+
const handleViewModeChange = useCallback((mode: EditorViewMode) => {
|
62
|
+
toolbarState.viewMode = mode;
|
63
|
+
}, []);
|
64
|
+
|
65
|
+
// TODO(marijn): This doesn't update the state on view changes.
|
66
|
+
// Also not sure if view is even guaranteed to exist at this point.
|
67
|
+
return (
|
68
|
+
<div role='none' className={mx('fixed inset-0 flex flex-col')}>
|
69
|
+
{toolbarState && <EditorToolbar state={toolbarState} getView={getView} viewMode={handleViewModeChange} />}
|
70
|
+
<div role='none' className='grow overflow-hidden'>
|
71
|
+
<div className={attentionSurface} ref={parentRef} />
|
72
|
+
</div>
|
73
|
+
</div>
|
74
|
+
);
|
75
|
+
};
|
76
|
+
|
77
|
+
export const Default = {
|
78
|
+
args: {
|
79
|
+
autoFocus: true,
|
80
|
+
placeholder: 'Text...',
|
81
|
+
initialValue: '# Demo\n\nThis is a document.\n\n',
|
82
|
+
},
|
83
|
+
};
|
84
|
+
|
85
|
+
export default {
|
86
|
+
title: 'ui/react-ui-editor/EditorToolbar',
|
87
|
+
render: DefaultStory,
|
88
|
+
decorators: [withTheme, withLayout({ fullscreen: true })],
|
89
|
+
parameters: { translations, layout: 'fullscreen' },
|
90
|
+
};
|
@@ -2,63 +2,60 @@
|
|
2
2
|
// Copyright 2024 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
-
import React, { useCallback } from 'react';
|
5
|
+
import React, { memo, useCallback } from 'react';
|
6
6
|
|
7
7
|
import { type NodeArg } from '@dxos/app-graph';
|
8
8
|
import { ElevationProvider } from '@dxos/react-ui';
|
9
|
-
import {
|
10
|
-
type MenuActionHandler,
|
11
|
-
MenuProvider,
|
12
|
-
ToolbarMenu,
|
13
|
-
createGapSeparator,
|
14
|
-
useMenuActions,
|
15
|
-
} from '@dxos/react-ui-menu';
|
9
|
+
import { MenuProvider, ToolbarMenu, createGapSeparator, useMenuActions } from '@dxos/react-ui-menu';
|
16
10
|
import { textBlockWidth } from '@dxos/react-ui-theme';
|
17
11
|
|
18
12
|
import { createBlocks } from './blocks';
|
19
13
|
import { createComment } from './comment';
|
20
14
|
import { createFormatting } from './formatting';
|
21
15
|
import { createHeadings } from './headings';
|
16
|
+
import { createImageUpload } from './image';
|
22
17
|
import { createLists } from './lists';
|
23
|
-
import {
|
24
|
-
|
25
|
-
type EditorToolbarFeatureFlags,
|
26
|
-
type EditorToolbarProps,
|
27
|
-
editorToolbarSearch,
|
28
|
-
} from './util';
|
18
|
+
import { createSearch } from './search';
|
19
|
+
import { type EditorToolbarActionGraphProps, type EditorToolbarFeatureFlags, type EditorToolbarProps } from './util';
|
29
20
|
import { createViewMode } from './view-mode';
|
30
21
|
import { stackItemContentToolbarClassNames } from '../../defaults';
|
31
22
|
|
32
23
|
const createToolbar = ({
|
24
|
+
getView,
|
33
25
|
state,
|
34
26
|
customActions,
|
35
27
|
...features
|
36
|
-
}: EditorToolbarFeatureFlags & Pick<EditorToolbarActionGraphProps, 'state' | 'customActions'>): {
|
28
|
+
}: EditorToolbarFeatureFlags & Pick<EditorToolbarActionGraphProps, 'getView' | 'state' | 'customActions'>): {
|
37
29
|
nodes: NodeArg<any>[];
|
38
30
|
edges: { source: string; target: string }[];
|
39
31
|
} => {
|
40
32
|
const nodes = [];
|
41
33
|
const edges = [];
|
42
34
|
if (features.headings ?? true) {
|
43
|
-
const headings = createHeadings(state);
|
35
|
+
const headings = createHeadings(state, getView);
|
44
36
|
nodes.push(...headings.nodes);
|
45
37
|
edges.push(...headings.edges);
|
46
38
|
}
|
47
39
|
if (features.formatting ?? true) {
|
48
|
-
const formatting = createFormatting(state);
|
40
|
+
const formatting = createFormatting(state, getView);
|
49
41
|
nodes.push(...formatting.nodes);
|
50
42
|
edges.push(...formatting.edges);
|
51
43
|
}
|
52
44
|
if (features.lists ?? true) {
|
53
|
-
const lists = createLists(state);
|
45
|
+
const lists = createLists(state, getView);
|
54
46
|
nodes.push(...lists.nodes);
|
55
47
|
edges.push(...lists.edges);
|
56
48
|
}
|
57
49
|
if (features.blocks ?? true) {
|
58
|
-
const blocks = createBlocks(state);
|
50
|
+
const blocks = createBlocks(state, getView);
|
59
51
|
nodes.push(...blocks.nodes);
|
60
52
|
edges.push(...blocks.edges);
|
61
53
|
}
|
54
|
+
if (features.image) {
|
55
|
+
const image = createImageUpload(features.image);
|
56
|
+
nodes.push(...image.nodes);
|
57
|
+
edges.push(...image.edges);
|
58
|
+
}
|
62
59
|
if (customActions) {
|
63
60
|
const custom = customActions();
|
64
61
|
nodes.push(...custom.nodes);
|
@@ -67,31 +64,33 @@ const createToolbar = ({
|
|
67
64
|
const editorToolbarGap = createGapSeparator();
|
68
65
|
nodes.push(...editorToolbarGap.nodes);
|
69
66
|
edges.push(...editorToolbarGap.edges);
|
70
|
-
if (features.comment
|
71
|
-
const comment = createComment(state);
|
67
|
+
if (features.comment) {
|
68
|
+
const comment = createComment(state, getView);
|
72
69
|
nodes.push(...comment.nodes);
|
73
70
|
edges.push(...comment.edges);
|
74
71
|
}
|
75
72
|
if (features.search ?? true) {
|
76
|
-
|
77
|
-
|
73
|
+
const search = createSearch(getView);
|
74
|
+
nodes.push(...search.nodes);
|
75
|
+
edges.push(...search.edges);
|
78
76
|
}
|
79
|
-
if (features.viewMode
|
80
|
-
const viewMode = createViewMode(state);
|
77
|
+
if (features.viewMode) {
|
78
|
+
const viewMode = createViewMode(state, features.viewMode);
|
81
79
|
nodes.push(...viewMode.nodes);
|
82
80
|
edges.push(...viewMode.edges);
|
83
81
|
}
|
84
82
|
return { nodes, edges };
|
85
83
|
};
|
86
84
|
|
87
|
-
|
85
|
+
// TODO(wittjosiah): Toolbar re-rendering is causing this graph to be recreated and breaking reactivity in some cases.
|
86
|
+
// E.g. for toolbar dropdowns which use active icon, the icon is not updated when the active item changes.
|
87
|
+
// This is currently only happening in the markdown plugin usage and should be reproduced in an editor story.
|
88
|
+
const useEditorToolbarActionGraph = (props: EditorToolbarProps) => {
|
88
89
|
const menuCreator = useCallback(() => createToolbar(props), [props]);
|
89
|
-
|
90
|
-
|
91
|
-
return { resolveGroupItems, onAction: onAction as MenuActionHandler };
|
90
|
+
return useMenuActions(menuCreator);
|
92
91
|
};
|
93
92
|
|
94
|
-
export const EditorToolbar = ({ classNames, attendableId, role, ...props }: EditorToolbarProps) => {
|
93
|
+
export const EditorToolbar = memo(({ classNames, attendableId, role, ...props }: EditorToolbarProps) => {
|
95
94
|
const menuProps = useEditorToolbarActionGraph(props);
|
96
95
|
return (
|
97
96
|
<div role='none' className={stackItemContentToolbarClassNames(role)}>
|
@@ -102,4 +101,4 @@ export const EditorToolbar = ({ classNames, attendableId, role, ...props }: Edit
|
|
102
101
|
</ElevationProvider>
|
103
102
|
</div>
|
104
103
|
);
|
105
|
-
};
|
104
|
+
});
|
@@ -2,11 +2,13 @@
|
|
2
2
|
// Copyright 2025 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
6
|
+
|
5
7
|
import { type NodeArg } from '@dxos/app-graph';
|
6
8
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
7
9
|
|
8
10
|
import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
|
9
|
-
import {
|
11
|
+
import { removeBlockquote, addBlockquote, removeCodeblock, addCodeblock, insertTable } from '../../extensions';
|
10
12
|
|
11
13
|
const createBlockGroupAction = (value: string) =>
|
12
14
|
createEditorActionGroup('block', {
|
@@ -15,22 +17,41 @@ const createBlockGroupAction = (value: string) =>
|
|
15
17
|
value,
|
16
18
|
} as ToolbarMenuActionGroupProperties);
|
17
19
|
|
18
|
-
const createBlockActions = (value: string, blankLine?: boolean) =>
|
20
|
+
const createBlockActions = (value: string, getView: () => EditorView, blankLine?: boolean) =>
|
19
21
|
Object.entries({
|
20
22
|
blockquote: 'ph--quotes--regular',
|
21
23
|
codeblock: 'ph--code-block--regular',
|
22
24
|
table: 'ph--table--regular',
|
23
25
|
}).map(([type, icon]) => {
|
26
|
+
const checked = type === value;
|
24
27
|
return createEditorAction(
|
25
|
-
|
26
|
-
|
28
|
+
type,
|
29
|
+
() => {
|
30
|
+
const view = getView();
|
31
|
+
if (!view) {
|
32
|
+
return;
|
33
|
+
}
|
34
|
+
|
35
|
+
switch (type) {
|
36
|
+
case 'blockquote':
|
37
|
+
checked ? removeBlockquote(view) : addBlockquote(view);
|
38
|
+
break;
|
39
|
+
case 'codeblock':
|
40
|
+
checked ? removeCodeblock(view) : addCodeblock(view);
|
41
|
+
break;
|
42
|
+
case 'table':
|
43
|
+
insertTable(view);
|
44
|
+
break;
|
45
|
+
}
|
46
|
+
},
|
47
|
+
{ checked, ...(type === 'table' && { disabled: !!blankLine }), icon },
|
27
48
|
);
|
28
49
|
});
|
29
50
|
|
30
|
-
export const createBlocks = (state: EditorToolbarState) => {
|
51
|
+
export const createBlocks = (state: EditorToolbarState, getView: () => EditorView) => {
|
31
52
|
const value = state?.blockQuote ? 'blockquote' : state.blockType ?? '';
|
32
53
|
const blockGroupAction = createBlockGroupAction(value);
|
33
|
-
const blockActions = createBlockActions(value, state.blankLine);
|
54
|
+
const blockActions = createBlockActions(value, getView, state.blankLine);
|
34
55
|
return {
|
35
56
|
nodes: [blockGroupAction as NodeArg<any>, ...blockActions],
|
36
57
|
edges: [
|
@@ -2,9 +2,12 @@
|
|
2
2
|
// Copyright 2025 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
6
|
+
|
5
7
|
import { type Label } from '@dxos/react-ui';
|
6
8
|
|
7
9
|
import { createEditorAction, type EditorToolbarState } from './util';
|
10
|
+
import { createComment as nativeCreateComment } from '../../extensions';
|
8
11
|
import { translationKey } from '../../translations';
|
9
12
|
|
10
13
|
const commentLabel = (comment?: boolean, selection?: boolean) =>
|
@@ -14,10 +17,14 @@ const commentLabel = (comment?: boolean, selection?: boolean) =>
|
|
14
17
|
? 'select text to comment label'
|
15
18
|
: 'comment label';
|
16
19
|
|
17
|
-
const createCommentAction = (label: Label) =>
|
18
|
-
createEditorAction(
|
20
|
+
const createCommentAction = (label: Label, getView: () => EditorView) =>
|
21
|
+
createEditorAction('comment', () => nativeCreateComment(getView()), {
|
22
|
+
testId: 'editor.toolbar.comment',
|
23
|
+
icon: 'ph--chat-text--regular',
|
24
|
+
label,
|
25
|
+
});
|
19
26
|
|
20
|
-
export const createComment = (state: EditorToolbarState) => ({
|
21
|
-
nodes: [createCommentAction([commentLabel(state.comment, state.selection), { ns: translationKey }])],
|
27
|
+
export const createComment = (state: EditorToolbarState, getView: () => EditorView) => ({
|
28
|
+
nodes: [createCommentAction([commentLabel(state.comment, state.selection), { ns: translationKey }], getView)],
|
22
29
|
edges: [{ source: 'root', target: 'comment' }],
|
23
30
|
});
|
@@ -2,11 +2,13 @@
|
|
2
2
|
// Copyright 2025 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
6
|
+
|
5
7
|
import { type NodeArg } from '@dxos/app-graph';
|
6
8
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
7
9
|
|
8
10
|
import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
|
9
|
-
import {
|
11
|
+
import { addLink, Inline, removeLink, setStyle, type Formatting } from '../../extensions';
|
10
12
|
|
11
13
|
const formats = {
|
12
14
|
strong: 'ph--text-b--regular',
|
@@ -23,14 +25,39 @@ const createFormattingGroup = (formatting: Formatting) =>
|
|
23
25
|
value: Object.keys(formats).filter((key) => !!formatting[key as keyof Formatting]),
|
24
26
|
} as ToolbarMenuActionGroupProperties);
|
25
27
|
|
26
|
-
const createFormattingActions = (formatting: Formatting) =>
|
27
|
-
Object.entries(formats).map(([type, icon]) =>
|
28
|
-
|
29
|
-
|
28
|
+
const createFormattingActions = (formatting: Formatting, getView: () => EditorView) =>
|
29
|
+
Object.entries(formats).map(([type, icon]) => {
|
30
|
+
const checked = !!formatting[type as keyof Formatting];
|
31
|
+
return createEditorAction(
|
32
|
+
type,
|
33
|
+
() => {
|
34
|
+
const view = getView();
|
35
|
+
if (!view) {
|
36
|
+
return;
|
37
|
+
}
|
38
|
+
|
39
|
+
if (type === 'link') {
|
40
|
+
checked ? removeLink(view) : addLink()(view);
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
|
44
|
+
const inlineType =
|
45
|
+
type === 'strong'
|
46
|
+
? Inline.Strong
|
47
|
+
: type === 'emphasis'
|
48
|
+
? Inline.Emphasis
|
49
|
+
: type === 'strikethrough'
|
50
|
+
? Inline.Strikethrough
|
51
|
+
: Inline.Code;
|
52
|
+
setStyle(inlineType, !checked)(view);
|
53
|
+
},
|
54
|
+
{ checked, icon },
|
55
|
+
);
|
56
|
+
});
|
30
57
|
|
31
|
-
export const createFormatting = (state: EditorToolbarState) => {
|
58
|
+
export const createFormatting = (state: EditorToolbarState, getView: () => EditorView) => {
|
32
59
|
const formattingGroupAction = createFormattingGroup(state);
|
33
|
-
const formattingActions = createFormattingActions(state);
|
60
|
+
const formattingActions = createFormattingActions(state, getView);
|
34
61
|
return {
|
35
62
|
nodes: [formattingGroupAction as NodeArg<any>, ...formattingActions],
|
36
63
|
edges: [
|
@@ -2,10 +2,13 @@
|
|
2
2
|
// Copyright 2025 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
6
|
+
|
5
7
|
import { type NodeArg } from '@dxos/app-graph';
|
6
8
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
7
9
|
|
8
10
|
import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
|
11
|
+
import { setHeading } from '../../extensions';
|
9
12
|
import { translationKey } from '../../translations';
|
10
13
|
|
11
14
|
const createHeadingGroupAction = (value: string) =>
|
@@ -20,7 +23,7 @@ const createHeadingGroupAction = (value: string) =>
|
|
20
23
|
'ph--text-h--regular',
|
21
24
|
);
|
22
25
|
|
23
|
-
const createHeadingActions = (
|
26
|
+
const createHeadingActions = (getView: () => EditorView) =>
|
24
27
|
Object.entries({
|
25
28
|
'0': 'ph--paragraph--regular',
|
26
29
|
'1': 'ph--text-h-one--regular',
|
@@ -31,12 +34,10 @@ const createHeadingActions = (value: string) =>
|
|
31
34
|
'6': 'ph--text-h-six--regular',
|
32
35
|
}).map(([levelStr, icon]) => {
|
33
36
|
const level = parseInt(levelStr);
|
34
|
-
return createEditorAction(
|
35
|
-
|
37
|
+
return createEditorAction(`heading--${levelStr}`, () => setHeading(level)(getView()), {
|
38
|
+
label: ['heading level label', { count: level, ns: translationKey }],
|
36
39
|
icon,
|
37
|
-
|
38
|
-
`heading--${levelStr}`,
|
39
|
-
);
|
40
|
+
});
|
40
41
|
});
|
41
42
|
|
42
43
|
const computeHeadingValue = (state: EditorToolbarState) => {
|
@@ -45,10 +46,10 @@ const computeHeadingValue = (state: EditorToolbarState) => {
|
|
45
46
|
return header ? header[1] : blockType === 'paragraph' || !blockType ? '0' : '';
|
46
47
|
};
|
47
48
|
|
48
|
-
export const createHeadings = (state: EditorToolbarState) => {
|
49
|
+
export const createHeadings = (state: EditorToolbarState, getView: () => EditorView) => {
|
49
50
|
const headingValue = computeHeadingValue(state);
|
50
51
|
const headingGroupAction = createHeadingGroupAction(headingValue);
|
51
|
-
const headingActions = createHeadingActions(
|
52
|
+
const headingActions = createHeadingActions(getView);
|
52
53
|
return {
|
53
54
|
nodes: [headingGroupAction as NodeArg<any>, ...headingActions],
|
54
55
|
edges: [
|
@@ -0,0 +1,16 @@
|
|
1
|
+
//
|
2
|
+
// Copyright 2025 DXOS.org
|
3
|
+
//
|
4
|
+
|
5
|
+
import { createEditorAction } from './util';
|
6
|
+
|
7
|
+
const createImageUploadAction = (onImageUpload: () => void) =>
|
8
|
+
createEditorAction('image', onImageUpload, {
|
9
|
+
testId: 'editor.toolbar.image',
|
10
|
+
icon: 'ph--image-square--regular',
|
11
|
+
});
|
12
|
+
|
13
|
+
export const createImageUpload = (onImageUpload: () => void) => ({
|
14
|
+
nodes: [createImageUploadAction(onImageUpload)],
|
15
|
+
edges: [{ source: 'root', target: 'image' }],
|
16
|
+
});
|
@@ -2,11 +2,13 @@
|
|
2
2
|
// Copyright 2025 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
6
|
+
|
5
7
|
import { type NodeArg } from '@dxos/app-graph';
|
6
8
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
7
9
|
|
8
10
|
import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
|
9
|
-
import {
|
11
|
+
import { addList, List, removeList } from '../../extensions';
|
10
12
|
|
11
13
|
const listStyles = {
|
12
14
|
bullet: 'ph--list-bullets--regular',
|
@@ -21,15 +23,32 @@ const createListGroupAction = (value: string) =>
|
|
21
23
|
value,
|
22
24
|
} as ToolbarMenuActionGroupProperties);
|
23
25
|
|
24
|
-
const createListActions = (value: string) =>
|
25
|
-
Object.entries(listStyles).map(([listStyle, icon]) =>
|
26
|
-
|
27
|
-
|
26
|
+
const createListActions = (value: string, getView: () => EditorView) =>
|
27
|
+
Object.entries(listStyles).map(([listStyle, icon]) => {
|
28
|
+
const checked = value === listStyle;
|
29
|
+
return createEditorAction(
|
30
|
+
`list-${listStyle}`,
|
31
|
+
() => {
|
32
|
+
const view = getView();
|
33
|
+
if (!view) {
|
34
|
+
return;
|
35
|
+
}
|
36
|
+
|
37
|
+
const listType = listStyle === 'ordered' ? List.Ordered : listStyle === 'bullet' ? List.Bullet : List.Task;
|
38
|
+
if (checked) {
|
39
|
+
removeList(listType)(view);
|
40
|
+
} else {
|
41
|
+
addList(listType)(view);
|
42
|
+
}
|
43
|
+
},
|
44
|
+
{ checked, icon },
|
45
|
+
);
|
46
|
+
});
|
28
47
|
|
29
|
-
export const createLists = (state: EditorToolbarState) => {
|
48
|
+
export const createLists = (state: EditorToolbarState, getView: () => EditorView) => {
|
30
49
|
const value = state.listStyle ?? '';
|
31
50
|
const listGroupAction = createListGroupAction(value);
|
32
|
-
const listActionsMap = createListActions(value);
|
51
|
+
const listActionsMap = createListActions(value, getView);
|
33
52
|
return {
|
34
53
|
nodes: [listGroupAction as NodeArg<any>, ...listActionsMap],
|
35
54
|
edges: [
|
@@ -0,0 +1,19 @@
|
|
1
|
+
//
|
2
|
+
// Copyright 2025 DXOS.org
|
3
|
+
//
|
4
|
+
|
5
|
+
import { openSearchPanel } from '@codemirror/search';
|
6
|
+
import { type EditorView } from '@codemirror/view';
|
7
|
+
|
8
|
+
import { createEditorAction } from './util';
|
9
|
+
|
10
|
+
const createSearchAction = (getView: () => EditorView) =>
|
11
|
+
createEditorAction('search', () => openSearchPanel(getView()), {
|
12
|
+
testId: 'editor.toolbar.search',
|
13
|
+
icon: 'ph--magnifying-glass--regular',
|
14
|
+
});
|
15
|
+
|
16
|
+
export const createSearch = (getView: () => EditorView) => ({
|
17
|
+
nodes: [createSearchAction(getView)],
|
18
|
+
edges: [{ source: 'root', target: 'search' }],
|
19
|
+
});
|
@@ -2,21 +2,23 @@
|
|
2
2
|
// Copyright 2025 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
5
6
|
import { useMemo } from 'react';
|
6
7
|
|
8
|
+
import { type Action } from '@dxos/app-graph';
|
7
9
|
import { live, type Live } from '@dxos/live-object';
|
8
|
-
import { type
|
10
|
+
import { type ThemedClassName } from '@dxos/react-ui';
|
9
11
|
import {
|
10
12
|
type MenuSeparator,
|
11
13
|
type MenuItemGroup,
|
12
14
|
type ToolbarMenuActionGroupProperties,
|
13
|
-
type MenuActionProperties,
|
14
15
|
createMenuAction,
|
15
16
|
createMenuItemGroup,
|
16
17
|
type ActionGraphProps,
|
18
|
+
type MenuActionProperties,
|
17
19
|
} from '@dxos/react-ui-menu';
|
18
20
|
|
19
|
-
import type { EditorAction,
|
21
|
+
import type { EditorAction, EditorViewMode, Formatting } from '../../extensions';
|
20
22
|
import { translationKey } from '../../translations';
|
21
23
|
|
22
24
|
export type EditorToolbarState = Formatting &
|
@@ -31,16 +33,18 @@ export type EditorToolbarFeatureFlags = Partial<{
|
|
31
33
|
formatting: boolean;
|
32
34
|
lists: boolean;
|
33
35
|
blocks: boolean;
|
34
|
-
comment: boolean;
|
35
36
|
search: boolean;
|
36
|
-
|
37
|
+
// TODO(wittjosiah): Factor out. Depend on plugin-level capabilities.
|
38
|
+
comment: boolean;
|
39
|
+
image: () => void;
|
40
|
+
viewMode: (mode: EditorViewMode) => void;
|
37
41
|
}>;
|
38
42
|
|
39
43
|
export type EditorToolbarActionGraphProps = {
|
40
44
|
state: Live<EditorToolbarState>;
|
45
|
+
getView: () => EditorView;
|
41
46
|
// TODO(wittjosiah): Control positioning.
|
42
47
|
customActions?: () => ActionGraphProps;
|
43
|
-
onAction: (action: EditorAction) => void;
|
44
48
|
};
|
45
49
|
|
46
50
|
export type EditorToolbarProps = ThemedClassName<
|
@@ -49,17 +53,13 @@ export type EditorToolbarProps = ThemedClassName<
|
|
49
53
|
|
50
54
|
export type EditorToolbarItem = EditorAction | MenuItemGroup | MenuSeparator;
|
51
55
|
|
52
|
-
export const createEditorAction = (
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
id: string = payload.type,
|
57
|
-
) => createMenuAction(id, { icon, label, ...payload }) as EditorAction;
|
56
|
+
export const createEditorAction = (id: string, invoke: () => void, properties: Partial<MenuActionProperties>) => {
|
57
|
+
const { label = [`${id} label`, { ns: translationKey }], ...rest } = properties;
|
58
|
+
return createMenuAction(id, invoke, { label, ...rest }) as Action<MenuActionProperties>;
|
59
|
+
};
|
58
60
|
|
59
61
|
export const createEditorActionGroup = (
|
60
62
|
id: string,
|
61
63
|
props: Omit<ToolbarMenuActionGroupProperties, 'icon'>,
|
62
64
|
icon?: string,
|
63
65
|
) => createMenuItemGroup(id, { icon, iconOnly: true, ...props });
|
64
|
-
|
65
|
-
export const editorToolbarSearch = createEditorAction({ type: 'search' }, 'ph--magnifying-glass--regular');
|
@@ -6,6 +6,7 @@ import { type NodeArg } from '@dxos/app-graph';
|
|
6
6
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
7
7
|
|
8
8
|
import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
|
9
|
+
import { type EditorViewMode } from '../../extensions';
|
9
10
|
import { translationKey } from '../../translations';
|
10
11
|
|
11
12
|
const createViewModeGroupAction = (value: string) =>
|
@@ -20,24 +21,24 @@ const createViewModeGroupAction = (value: string) =>
|
|
20
21
|
'ph--eye--regular',
|
21
22
|
);
|
22
23
|
|
23
|
-
const createViewModeActions = (value: string) =>
|
24
|
+
const createViewModeActions = (value: string, onViewModeChange: (mode: EditorViewMode) => void) =>
|
24
25
|
Object.entries({
|
25
26
|
preview: 'ph--eye--regular',
|
26
27
|
source: 'ph--pencil-simple--regular',
|
27
28
|
readonly: 'ph--pencil-slash--regular',
|
28
29
|
}).map(([viewMode, icon]) => {
|
29
|
-
|
30
|
-
|
30
|
+
const checked = viewMode === value;
|
31
|
+
return createEditorAction(`view-mode--${viewMode}`, () => onViewModeChange(viewMode as EditorViewMode), {
|
32
|
+
label: [`${viewMode} mode label`, { ns: translationKey }],
|
33
|
+
checked,
|
31
34
|
icon,
|
32
|
-
|
33
|
-
`view-mode--${viewMode}`,
|
34
|
-
);
|
35
|
+
});
|
35
36
|
});
|
36
37
|
|
37
|
-
export const createViewMode = (state: EditorToolbarState) => {
|
38
|
+
export const createViewMode = (state: EditorToolbarState, onViewModeChange: (mode: EditorViewMode) => void) => {
|
38
39
|
const value = state.viewMode ?? 'source';
|
39
40
|
const viewModeGroupAction = createViewModeGroupAction(value);
|
40
|
-
const viewModeActions = createViewModeActions(value);
|
41
|
+
const viewModeActions = createViewModeActions(value, onViewModeChange);
|
41
42
|
return {
|
42
43
|
nodes: [viewModeGroupAction as NodeArg<any>, ...viewModeActions],
|
43
44
|
edges: [
|
package/src/extensions/modes.ts
CHANGED
@@ -6,18 +6,17 @@ import { type Extension } from '@codemirror/state';
|
|
6
6
|
import { keymap } from '@codemirror/view';
|
7
7
|
import { vim } from '@replit/codemirror-vim';
|
8
8
|
import { vscodeKeymap } from '@replit/codemirror-vscode-keymap';
|
9
|
-
|
10
|
-
import { S } from '@dxos/echo-schema';
|
9
|
+
import { Schema } from 'effect';
|
11
10
|
|
12
11
|
import { singleValueFacet } from '../util';
|
13
12
|
|
14
13
|
export const EditorViewModes = ['preview', 'readonly', 'source'] as const;
|
15
|
-
export const EditorViewMode =
|
16
|
-
export type EditorViewMode =
|
14
|
+
export const EditorViewMode = Schema.Union(...EditorViewModes.map((mode) => Schema.Literal(mode)));
|
15
|
+
export type EditorViewMode = Schema.Schema.Type<typeof EditorViewMode>;
|
17
16
|
|
18
17
|
export const EditorInputModes = ['default', 'vim', 'vscode'] as const;
|
19
|
-
export const EditorInputMode =
|
20
|
-
export type EditorInputMode =
|
18
|
+
export const EditorInputMode = Schema.Union(...EditorInputModes.map((mode) => Schema.Literal(mode)));
|
19
|
+
export type EditorInputMode = Schema.Schema.Type<typeof EditorInputMode>;
|
21
20
|
|
22
21
|
export type EditorInputConfig = {
|
23
22
|
type?: string;
|