@dxos/react-ui-editor 0.8.4-main.3a94e84 → 0.8.4-main.406dc2a
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/{chunk-22UMM3QJ.mjs → chunk-HL3YF6WC.mjs} +2 -2
- package/dist/lib/browser/chunk-HL3YF6WC.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +4239 -3098
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +71 -1
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/browser/types/index.mjs +1 -1
- package/dist/lib/node-esm/{chunk-YXYQPV6R.mjs → chunk-YJZGD3LY.mjs} +2 -2
- package/dist/lib/node-esm/chunk-YJZGD3LY.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +4239 -3098
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +71 -1
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/types/index.mjs +1 -1
- package/dist/types/src/components/Editor/Editor.d.ts +24 -9
- package/dist/types/src/components/Editor/Editor.d.ts.map +1 -1
- package/dist/types/src/components/Editor/Editor.stories.d.ts +30 -0
- package/dist/types/src/components/Editor/Editor.stories.d.ts.map +1 -0
- package/dist/types/src/components/EditorToolbar/EditorToolbar.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/blocks.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/formatting.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/headings.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/image.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/lists.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/search.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/util.d.ts +3 -3
- package/dist/types/src/components/EditorToolbar/util.d.ts.map +1 -1
- package/dist/types/src/components/EditorToolbar/view-mode.d.ts +1 -1
- package/dist/types/src/components/EditorToolbar/view-mode.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +0 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/defaults.d.ts.map +1 -1
- package/dist/types/src/extensions/autocomplete/autocomplete.d.ts +26 -0
- package/dist/types/src/extensions/autocomplete/autocomplete.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete/index.d.ts +5 -0
- package/dist/types/src/extensions/autocomplete/index.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete/match.d.ts +13 -0
- package/dist/types/src/extensions/autocomplete/match.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete/placeholder.d.ts +20 -0
- package/dist/types/src/extensions/autocomplete/placeholder.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete/typeahead.d.ts +10 -0
- package/dist/types/src/extensions/autocomplete/typeahead.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.stories.d.ts +10 -19
- package/dist/types/src/extensions/automerge/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/sync.d.ts +2 -2
- package/dist/types/src/extensions/automerge/sync.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/autoscroll.d.ts +10 -0
- package/dist/types/src/extensions/autoscroll.d.ts.map +1 -0
- package/dist/types/src/extensions/comments.d.ts +1 -1
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/dnd.d.ts.map +1 -1
- package/dist/types/src/extensions/factories.d.ts +8 -8
- package/dist/types/src/extensions/factories.d.ts.map +1 -1
- package/dist/types/src/extensions/focus.d.ts.map +1 -1
- package/dist/types/src/extensions/folding.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +4 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/json.d.ts +1 -1
- package/dist/types/src/extensions/json.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/action.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +8 -2
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/changes.d.ts +1 -1
- package/dist/types/src/extensions/markdown/changes.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/decorate.d.ts +9 -1
- package/dist/types/src/extensions/markdown/decorate.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/formatting.d.ts +1 -1
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/formatting.test.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/modes.d.ts +1 -1
- package/dist/types/src/extensions/modes.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner/menu.d.ts +8 -0
- package/dist/types/src/extensions/outliner/menu.d.ts.map +1 -0
- package/dist/types/src/extensions/outliner/outliner.d.ts +1 -1
- package/dist/types/src/extensions/outliner/outliner.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner/selection.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner/tree.d.ts +2 -2
- package/dist/types/src/extensions/outliner/tree.d.ts.map +1 -1
- package/dist/types/src/extensions/popover/PopoverMenuProvider.d.ts +36 -0
- package/dist/types/src/extensions/popover/PopoverMenuProvider.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/index.d.ts +8 -0
- package/dist/types/src/extensions/popover/index.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/menu-presets.d.ts +4 -0
- package/dist/types/src/extensions/popover/menu-presets.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/menu.d.ts +24 -0
- package/dist/types/src/extensions/popover/menu.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/modal.d.ts +7 -0
- package/dist/types/src/extensions/popover/modal.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/popover.d.ts +47 -0
- package/dist/types/src/extensions/popover/popover.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/usePopoverMenu.d.ts +34 -0
- package/dist/types/src/extensions/popover/usePopoverMenu.d.ts.map +1 -0
- package/dist/types/src/extensions/popover/util.d.ts +8 -0
- package/dist/types/src/extensions/popover/util.d.ts.map +1 -0
- package/dist/types/src/extensions/preview/preview.d.ts +2 -6
- package/dist/types/src/extensions/preview/preview.d.ts.map +1 -1
- package/dist/types/src/extensions/state.d.ts +2 -0
- package/dist/types/src/extensions/state.d.ts.map +1 -0
- package/dist/types/src/extensions/tags/extended-markdown.d.ts +10 -0
- package/dist/types/src/extensions/tags/extended-markdown.d.ts.map +1 -0
- package/dist/types/src/extensions/tags/extended-markdown.test.d.ts +2 -0
- package/dist/types/src/extensions/tags/extended-markdown.test.d.ts.map +1 -0
- package/dist/types/src/extensions/tags/index.d.ts +4 -0
- package/dist/types/src/extensions/tags/index.d.ts.map +1 -0
- package/dist/types/src/extensions/tags/streamer.d.ts +12 -0
- package/dist/types/src/extensions/tags/streamer.d.ts.map +1 -0
- package/dist/types/src/extensions/tags/xml-tags.d.ts +72 -0
- package/dist/types/src/extensions/tags/xml-tags.d.ts.map +1 -0
- package/dist/types/src/extensions/tags/xml-util.d.ts +10 -0
- package/dist/types/src/extensions/tags/xml-util.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +4 -8
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
- package/dist/types/src/stories/CommandDialog.stories.d.ts +14 -0
- package/dist/types/src/stories/CommandDialog.stories.d.ts.map +1 -0
- package/dist/types/src/stories/Comments.stories.d.ts +21 -10
- package/dist/types/src/stories/Comments.stories.d.ts.map +1 -1
- package/dist/types/src/stories/EditorToolbar.stories.d.ts +39 -3
- package/dist/types/src/stories/EditorToolbar.stories.d.ts.map +1 -1
- package/dist/types/src/stories/Experimental.stories.d.ts +22 -13
- package/dist/types/src/stories/Experimental.stories.d.ts.map +1 -1
- package/dist/types/src/stories/Markdown.stories.d.ts +32 -43
- package/dist/types/src/stories/Markdown.stories.d.ts.map +1 -1
- package/dist/types/src/stories/Outliner.stories.d.ts +15 -21
- package/dist/types/src/stories/Outliner.stories.d.ts.map +1 -1
- package/dist/types/src/stories/Popover.stories.d.ts +20 -0
- package/dist/types/src/stories/Popover.stories.d.ts.map +1 -0
- package/dist/types/src/stories/Preview.stories.d.ts +21 -7
- package/dist/types/src/stories/Preview.stories.d.ts.map +1 -1
- package/dist/types/src/stories/Tags.stories.d.ts +16 -0
- package/dist/types/src/stories/Tags.stories.d.ts.map +1 -0
- package/dist/types/src/stories/TextEditor.stories.d.ts +37 -52
- package/dist/types/src/stories/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/stories/components/EditorStory.d.ts +6 -9
- package/dist/types/src/stories/components/EditorStory.d.ts.map +1 -1
- package/dist/types/src/styles/theme.d.ts.map +1 -1
- package/dist/types/src/testing/PreviewPopover.d.ts +20 -0
- package/dist/types/src/testing/PreviewPopover.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/util.d.ts +1 -0
- package/dist/types/src/testing/util.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +1 -1
- package/dist/types/src/types/types.d.ts +2 -2
- package/dist/types/src/types/types.d.ts.map +1 -1
- package/dist/types/src/util/cursor.d.ts.map +1 -1
- package/dist/types/src/util/debug.d.ts +1 -1
- package/dist/types/src/util/debug.d.ts.map +1 -1
- package/dist/types/src/util/decorations.d.ts +4 -0
- package/dist/types/src/util/decorations.d.ts.map +1 -0
- package/dist/types/src/util/dom.d.ts +2 -12
- package/dist/types/src/util/dom.d.ts.map +1 -1
- package/dist/types/src/util/index.d.ts +1 -0
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/react.d.ts +1 -1
- package/dist/types/src/util/react.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +69 -61
- package/src/components/Editor/Editor.stories.tsx +72 -0
- package/src/components/Editor/Editor.tsx +58 -15
- package/src/components/EditorToolbar/EditorToolbar.tsx +41 -30
- package/src/components/EditorToolbar/blocks.ts +21 -24
- package/src/components/EditorToolbar/formatting.ts +22 -25
- package/src/components/EditorToolbar/headings.ts +10 -5
- package/src/components/EditorToolbar/image.ts +8 -4
- package/src/components/EditorToolbar/lists.ts +16 -19
- package/src/components/EditorToolbar/search.ts +8 -4
- package/src/components/EditorToolbar/util.ts +16 -5
- package/src/components/EditorToolbar/view-mode.ts +11 -6
- package/src/components/index.ts +0 -1
- package/src/defaults.ts +5 -2
- package/src/extensions/autocomplete/autocomplete.ts +220 -0
- package/src/extensions/autocomplete/index.ts +8 -0
- package/src/extensions/autocomplete/match.ts +46 -0
- package/src/extensions/{command → autocomplete}/placeholder.ts +22 -18
- package/src/extensions/{command → autocomplete}/typeahead.ts +8 -50
- package/src/extensions/automerge/automerge.stories.tsx +31 -24
- package/src/extensions/automerge/automerge.ts +31 -11
- package/src/extensions/automerge/defs.ts +1 -1
- package/src/extensions/automerge/sync.ts +8 -4
- package/src/extensions/automerge/update-automerge.ts +1 -1
- package/src/extensions/autoscroll.ts +157 -0
- package/src/extensions/awareness/awareness.ts +2 -2
- package/src/extensions/comments.ts +18 -13
- package/src/extensions/dnd.ts +1 -1
- package/src/extensions/factories.ts +48 -31
- package/src/extensions/focus.ts +5 -4
- package/src/extensions/folding.tsx +4 -6
- package/src/extensions/hashtag.tsx +2 -2
- package/src/extensions/index.ts +4 -1
- package/src/extensions/json.ts +1 -1
- package/src/extensions/markdown/action.ts +2 -1
- package/src/extensions/markdown/bundle.ts +29 -5
- package/src/extensions/markdown/changes.ts +1 -1
- package/src/extensions/markdown/decorate.ts +24 -14
- package/src/extensions/markdown/formatting.test.ts +6 -6
- package/src/extensions/markdown/formatting.ts +3 -3
- package/src/extensions/markdown/highlight.ts +1 -1
- package/src/extensions/markdown/image.ts +3 -4
- package/src/extensions/markdown/link.ts +3 -0
- package/src/extensions/markdown/table.ts +7 -1
- package/src/extensions/mention.ts +1 -1
- package/src/extensions/modes.ts +2 -2
- package/src/extensions/{command/floating-menu.ts → outliner/menu.ts} +16 -21
- package/src/extensions/outliner/outliner.test.ts +3 -2
- package/src/extensions/outliner/outliner.ts +7 -6
- package/src/extensions/outliner/selection.ts +1 -1
- package/src/extensions/outliner/tree.test.ts +2 -1
- package/src/extensions/outliner/tree.ts +2 -2
- package/src/extensions/popover/PopoverMenuProvider.tsx +221 -0
- package/src/extensions/popover/index.ts +12 -0
- package/src/extensions/popover/menu-presets.ts +124 -0
- package/src/extensions/popover/menu.ts +67 -0
- package/src/extensions/popover/modal.ts +24 -0
- package/src/extensions/popover/popover.ts +293 -0
- package/src/extensions/popover/usePopoverMenu.ts +173 -0
- package/src/extensions/popover/util.ts +29 -0
- package/src/extensions/preview/index.ts +1 -1
- package/src/extensions/preview/preview.ts +57 -62
- package/src/extensions/selection.ts +2 -2
- package/src/extensions/state.ts +7 -0
- package/src/extensions/tags/extended-markdown.test.ts +261 -0
- package/src/extensions/tags/extended-markdown.ts +78 -0
- package/src/extensions/tags/index.ts +7 -0
- package/src/extensions/tags/streamer.ts +243 -0
- package/src/extensions/tags/xml-tags.ts +393 -0
- package/src/extensions/tags/xml-util.ts +94 -0
- package/src/hooks/useTextEditor.ts +27 -39
- package/src/stories/CommandDialog.stories.tsx +78 -0
- package/src/stories/Comments.stories.tsx +14 -10
- package/src/stories/EditorToolbar.stories.tsx +13 -12
- package/src/stories/Experimental.stories.tsx +17 -13
- package/src/stories/Markdown.stories.tsx +25 -21
- package/src/stories/Outliner.stories.tsx +54 -35
- package/src/stories/Popover.stories.tsx +163 -0
- package/src/stories/Preview.stories.tsx +34 -33
- package/src/stories/Tags.stories.tsx +81 -0
- package/src/stories/TextEditor.stories.tsx +39 -58
- package/src/stories/components/EditorStory.tsx +16 -15
- package/src/styles/theme.ts +16 -12
- package/src/testing/PreviewPopover.tsx +80 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/util.ts +2 -0
- package/src/translations.ts +1 -1
- package/src/types/types.ts +1 -1
- package/src/util/cursor.ts +2 -1
- package/src/util/debug.ts +2 -2
- package/src/util/decorations.ts +21 -0
- package/src/util/dom.ts +5 -27
- package/src/util/index.ts +1 -0
- package/src/util/react.tsx +1 -1
- package/dist/lib/browser/chunk-22UMM3QJ.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-YXYQPV6R.mjs.map +0 -7
- package/dist/types/src/components/Popover/CommandMenu.d.ts +0 -34
- package/dist/types/src/components/Popover/CommandMenu.d.ts.map +0 -1
- package/dist/types/src/components/Popover/RefDropdownMenu.d.ts +0 -21
- package/dist/types/src/components/Popover/RefDropdownMenu.d.ts.map +0 -1
- package/dist/types/src/components/Popover/RefPopover.d.ts +0 -34
- package/dist/types/src/components/Popover/RefPopover.d.ts.map +0 -1
- package/dist/types/src/components/Popover/index.d.ts +0 -4
- package/dist/types/src/components/Popover/index.d.ts.map +0 -1
- package/dist/types/src/extensions/autocomplete.d.ts +0 -13
- package/dist/types/src/extensions/autocomplete.d.ts.map +0 -1
- package/dist/types/src/extensions/command/action.d.ts +0 -17
- package/dist/types/src/extensions/command/action.d.ts.map +0 -1
- package/dist/types/src/extensions/command/command-menu.d.ts +0 -20
- package/dist/types/src/extensions/command/command-menu.d.ts.map +0 -1
- package/dist/types/src/extensions/command/command.d.ts +0 -6
- package/dist/types/src/extensions/command/command.d.ts.map +0 -1
- package/dist/types/src/extensions/command/floating-menu.d.ts +0 -7
- package/dist/types/src/extensions/command/floating-menu.d.ts.map +0 -1
- package/dist/types/src/extensions/command/hint.d.ts +0 -24
- package/dist/types/src/extensions/command/hint.d.ts.map +0 -1
- package/dist/types/src/extensions/command/index.d.ts +0 -7
- package/dist/types/src/extensions/command/index.d.ts.map +0 -1
- package/dist/types/src/extensions/command/placeholder.d.ts +0 -10
- package/dist/types/src/extensions/command/placeholder.d.ts.map +0 -1
- package/dist/types/src/extensions/command/state.d.ts +0 -16
- package/dist/types/src/extensions/command/state.d.ts.map +0 -1
- package/dist/types/src/extensions/command/typeahead.d.ts +0 -22
- package/dist/types/src/extensions/command/typeahead.d.ts.map +0 -1
- package/dist/types/src/extensions/command/useCommandMenu.d.ts +0 -26
- package/dist/types/src/extensions/command/useCommandMenu.d.ts.map +0 -1
- package/dist/types/src/stories/Command.stories.d.ts +0 -7
- package/dist/types/src/stories/Command.stories.d.ts.map +0 -1
- package/dist/types/src/stories/CommandMenu.stories.d.ts +0 -13
- package/dist/types/src/stories/CommandMenu.stories.d.ts.map +0 -1
- package/src/components/Popover/CommandMenu.tsx +0 -279
- package/src/components/Popover/RefDropdownMenu.tsx +0 -85
- package/src/components/Popover/RefPopover.tsx +0 -99
- package/src/components/Popover/index.ts +0 -7
- package/src/extensions/autocomplete.ts +0 -69
- package/src/extensions/command/action.ts +0 -56
- package/src/extensions/command/command-menu.ts +0 -210
- package/src/extensions/command/command.ts +0 -34
- package/src/extensions/command/hint.ts +0 -102
- package/src/extensions/command/index.ts +0 -10
- package/src/extensions/command/state.ts +0 -89
- package/src/extensions/command/useCommandMenu.ts +0 -118
- package/src/stories/Command.stories.tsx +0 -97
- package/src/stories/CommandMenu.stories.tsx +0 -159
|
@@ -5,10 +5,14 @@
|
|
|
5
5
|
import { createEditorAction } from './util';
|
|
6
6
|
|
|
7
7
|
const createImageUploadAction = (onImageUpload: () => void) =>
|
|
8
|
-
createEditorAction(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
createEditorAction(
|
|
9
|
+
'image',
|
|
10
|
+
{
|
|
11
|
+
testId: 'editor.toolbar.image',
|
|
12
|
+
icon: 'ph--image-square--regular',
|
|
13
|
+
},
|
|
14
|
+
onImageUpload,
|
|
15
|
+
);
|
|
12
16
|
|
|
13
17
|
export const createImageUpload = (onImageUpload: () => void) => ({
|
|
14
18
|
nodes: [createImageUploadAction(onImageUpload)],
|
|
@@ -7,8 +7,9 @@ import { type EditorView } from '@codemirror/view';
|
|
|
7
7
|
import { type NodeArg } from '@dxos/app-graph';
|
|
8
8
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import { List, addList, removeList } from '../../extensions';
|
|
11
|
+
|
|
12
|
+
import { type EditorToolbarState, createEditorAction, createEditorActionGroup } from './util';
|
|
12
13
|
|
|
13
14
|
const listStyles = {
|
|
14
15
|
bullet: 'ph--list-bullets--regular',
|
|
@@ -26,23 +27,19 @@ const createListGroupAction = (value: string) =>
|
|
|
26
27
|
const createListActions = (value: string, getView: () => EditorView) =>
|
|
27
28
|
Object.entries(listStyles).map(([listStyle, icon]) => {
|
|
28
29
|
const checked = value === listStyle;
|
|
29
|
-
return createEditorAction(
|
|
30
|
-
|
|
31
|
-
()
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
{ checked, icon },
|
|
45
|
-
);
|
|
30
|
+
return createEditorAction(`list-${listStyle}`, { checked, icon }, () => {
|
|
31
|
+
const view = getView();
|
|
32
|
+
if (!view) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const listType = listStyle === 'ordered' ? List.Ordered : listStyle === 'bullet' ? List.Bullet : List.Task;
|
|
37
|
+
if (checked) {
|
|
38
|
+
removeList(listType)(view);
|
|
39
|
+
} else {
|
|
40
|
+
addList(listType)(view);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
46
43
|
});
|
|
47
44
|
|
|
48
45
|
export const createLists = (state: EditorToolbarState, getView: () => EditorView) => {
|
|
@@ -8,10 +8,14 @@ import { type EditorView } from '@codemirror/view';
|
|
|
8
8
|
import { createEditorAction } from './util';
|
|
9
9
|
|
|
10
10
|
const createSearchAction = (getView: () => EditorView) =>
|
|
11
|
-
createEditorAction(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
createEditorAction(
|
|
12
|
+
'search',
|
|
13
|
+
{
|
|
14
|
+
testId: 'editor.toolbar.search',
|
|
15
|
+
icon: 'ph--magnifying-glass--regular',
|
|
16
|
+
},
|
|
17
|
+
() => openSearchPanel(getView()),
|
|
18
|
+
);
|
|
15
19
|
|
|
16
20
|
export const createSearch = (getView: () => EditorView) => ({
|
|
17
21
|
nodes: [createSearchAction(getView)],
|
|
@@ -7,7 +7,7 @@ import { type Rx } from '@effect-rx/rx-react';
|
|
|
7
7
|
import { useMemo } from 'react';
|
|
8
8
|
|
|
9
9
|
import { type Action } from '@dxos/app-graph';
|
|
10
|
-
import {
|
|
10
|
+
import { type Live, live } from '@dxos/live-object';
|
|
11
11
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
12
12
|
import {
|
|
13
13
|
type ActionGraphProps,
|
|
@@ -53,13 +53,24 @@ export type EditorToolbarProps = ThemedClassName<
|
|
|
53
53
|
|
|
54
54
|
export type EditorToolbarItem = EditorAction | MenuItemGroup | MenuSeparator;
|
|
55
55
|
|
|
56
|
-
export const createEditorAction = (id: string, invoke: () => void
|
|
57
|
-
const { label = [`${id} label`, { ns: translationKey }], ...rest } =
|
|
58
|
-
return createMenuAction(id, invoke, {
|
|
56
|
+
export const createEditorAction = (id: string, props: Partial<MenuActionProperties>, invoke: () => void) => {
|
|
57
|
+
const { label = [`${id} label`, { ns: translationKey }], ...rest } = props;
|
|
58
|
+
return createMenuAction(id, invoke, {
|
|
59
|
+
label,
|
|
60
|
+
...rest,
|
|
61
|
+
}) as Action<MenuActionProperties>;
|
|
59
62
|
};
|
|
60
63
|
|
|
61
64
|
export const createEditorActionGroup = (
|
|
62
65
|
id: string,
|
|
63
66
|
props: Omit<ToolbarMenuActionGroupProperties, 'icon'>,
|
|
64
67
|
icon?: string,
|
|
65
|
-
) =>
|
|
68
|
+
) => {
|
|
69
|
+
const { label = [`${id} label`, { ns: translationKey }], ...rest } = props;
|
|
70
|
+
return createMenuItemGroup(id, {
|
|
71
|
+
label,
|
|
72
|
+
icon,
|
|
73
|
+
iconOnly: true,
|
|
74
|
+
...rest,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
import { type NodeArg } from '@dxos/app-graph';
|
|
6
6
|
import { type ToolbarMenuActionGroupProperties } from '@dxos/react-ui-menu';
|
|
7
7
|
|
|
8
|
-
import { createEditorAction, createEditorActionGroup, type EditorToolbarState } from './util';
|
|
9
8
|
import { translationKey } from '../../translations';
|
|
10
9
|
import { type EditorViewMode } from '../../types';
|
|
11
10
|
|
|
11
|
+
import { type EditorToolbarState, createEditorAction, createEditorActionGroup } from './util';
|
|
12
|
+
|
|
12
13
|
const createViewModeGroupAction = (value: string) =>
|
|
13
14
|
createEditorActionGroup(
|
|
14
15
|
'viewMode',
|
|
@@ -28,11 +29,15 @@ const createViewModeActions = (value: string, onViewModeChange: (mode: EditorVie
|
|
|
28
29
|
readonly: 'ph--pencil-slash--regular',
|
|
29
30
|
}).map(([viewMode, icon]) => {
|
|
30
31
|
const checked = viewMode === value;
|
|
31
|
-
return createEditorAction(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
return createEditorAction(
|
|
33
|
+
`view-mode--${viewMode}`,
|
|
34
|
+
{
|
|
35
|
+
label: [`${viewMode} mode label`, { ns: translationKey }],
|
|
36
|
+
checked,
|
|
37
|
+
icon,
|
|
38
|
+
},
|
|
39
|
+
() => onViewModeChange(viewMode as EditorViewMode),
|
|
40
|
+
);
|
|
36
41
|
});
|
|
37
42
|
|
|
38
43
|
export const createViewMode = (state: EditorToolbarState, onViewModeChange: (mode: EditorViewMode) => void) => {
|
package/src/components/index.ts
CHANGED
package/src/defaults.ts
CHANGED
|
@@ -28,7 +28,9 @@ export const editorSlots: ThemeExtensionsOptions['slots'] = {
|
|
|
28
28
|
|
|
29
29
|
export const editorGutter = EditorView.theme({
|
|
30
30
|
'.cm-gutters': {
|
|
31
|
-
|
|
31
|
+
// NOTE: Color required to cover content if scrolling horizontally.
|
|
32
|
+
// TODO(burdon): Non-transparent background clips the focus ring.
|
|
33
|
+
background: 'var(--dx-baseSurface) !important',
|
|
32
34
|
paddingRight: '1rem',
|
|
33
35
|
},
|
|
34
36
|
});
|
|
@@ -42,8 +44,9 @@ export const editorMonospace = EditorView.theme({
|
|
|
42
44
|
export const editorWithToolbarLayout =
|
|
43
45
|
'grid grid-cols-1 grid-rows-[min-content_1fr] data-[toolbar=disabled]:grid-rows-[1fr] justify-center content-start overflow-hidden';
|
|
44
46
|
|
|
47
|
+
// NOTE: Padding is added to the editor to account for the focus ring (since otherwise the CM gutter will clip it)
|
|
45
48
|
export const stackItemContentEditorClassNames = (role?: string) =>
|
|
46
49
|
mx(
|
|
47
|
-
'
|
|
50
|
+
'p-0.5 dx-focus-ring-inset attention-surface data-[toolbar=disabled]:pbs-2',
|
|
48
51
|
role === 'section' ? '[&_.cm-scroller]:overflow-hidden [&_.cm-scroller]:min-bs-24' : 'min-bs-0',
|
|
49
52
|
);
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension, Prec } from '@codemirror/state';
|
|
6
|
+
import {
|
|
7
|
+
Decoration,
|
|
8
|
+
type DecorationSet,
|
|
9
|
+
EditorView,
|
|
10
|
+
ViewPlugin,
|
|
11
|
+
type ViewUpdate,
|
|
12
|
+
WidgetType,
|
|
13
|
+
keymap,
|
|
14
|
+
} from '@codemirror/view';
|
|
15
|
+
|
|
16
|
+
export type AutocompleteOptions = {
|
|
17
|
+
fireIfEmpty?: boolean;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Callback triggered when Enter is pressed.
|
|
21
|
+
* @param text The current text in the editor
|
|
22
|
+
* @returns true if the editor should reset the document.
|
|
23
|
+
*/
|
|
24
|
+
onSubmit?: (text: string) => boolean | void;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Function that returns a list of suggestions based on the current text.
|
|
28
|
+
* @param text The current text before the cursor
|
|
29
|
+
* @returns Array of suggestion strings
|
|
30
|
+
*/
|
|
31
|
+
onSuggest?: (text: string) => string[];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* ESC pressed.
|
|
35
|
+
*/
|
|
36
|
+
onCancel?: () => void;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates an autocomplete extension that shows inline suggestions.
|
|
41
|
+
* Pressing Tab will complete the suggestion.
|
|
42
|
+
*/
|
|
43
|
+
// TODO(burdon): Reconcile with typeahead.
|
|
44
|
+
export const autocomplete = ({ fireIfEmpty, onSubmit, onSuggest, onCancel }: AutocompleteOptions = {}): Extension => {
|
|
45
|
+
const suggest = ViewPlugin.fromClass(
|
|
46
|
+
class {
|
|
47
|
+
_decorations: DecorationSet;
|
|
48
|
+
_currentSuggestion: string | null = null;
|
|
49
|
+
|
|
50
|
+
constructor(view: EditorView) {
|
|
51
|
+
this._decorations = this.computeDecorations(view);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
update(update: ViewUpdate) {
|
|
55
|
+
if (update.docChanged || update.selectionSet) {
|
|
56
|
+
this._decorations = this.computeDecorations(update.view);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private computeDecorations(view: EditorView): DecorationSet {
|
|
61
|
+
const text = view.state.doc.toString();
|
|
62
|
+
const suggestions = onSuggest?.(text) ?? [];
|
|
63
|
+
if (!suggestions.length) {
|
|
64
|
+
this._currentSuggestion = null;
|
|
65
|
+
return Decoration.none;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Get the first suggestion.
|
|
69
|
+
this._currentSuggestion = suggestions[0];
|
|
70
|
+
const suffix = this._currentSuggestion.slice(text.length);
|
|
71
|
+
if (!suffix) {
|
|
72
|
+
return Decoration.none;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Always show ghost text at the end of the document.
|
|
76
|
+
return Decoration.set([
|
|
77
|
+
Decoration.widget({
|
|
78
|
+
widget: new InlineSuggestionWidget(suffix),
|
|
79
|
+
side: 1,
|
|
80
|
+
}).range(view.state.doc.length),
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
completeSuggestion(view: EditorView): boolean {
|
|
85
|
+
if (!this._currentSuggestion) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const text = view.state.doc.toString();
|
|
90
|
+
const suffix = this._currentSuggestion.slice(text.length);
|
|
91
|
+
if (!suffix) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
view.dispatch({
|
|
96
|
+
changes: {
|
|
97
|
+
from: view.state.doc.length,
|
|
98
|
+
insert: suffix,
|
|
99
|
+
},
|
|
100
|
+
selection: {
|
|
101
|
+
anchor: view.state.doc.length + suffix.length,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
decorations: (v) => v._decorations,
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return [
|
|
114
|
+
suggest,
|
|
115
|
+
EditorView.theme({
|
|
116
|
+
'.cm-inline-suggestion': {
|
|
117
|
+
opacity: 0.4,
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
|
|
121
|
+
Prec.highest(
|
|
122
|
+
keymap.of([
|
|
123
|
+
{
|
|
124
|
+
key: 'Tab',
|
|
125
|
+
preventDefault: true,
|
|
126
|
+
run: (view) => {
|
|
127
|
+
const plugin = view.plugin(suggest);
|
|
128
|
+
return plugin?.completeSuggestion(view) ?? false;
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
key: 'ArrowRight',
|
|
133
|
+
preventDefault: true,
|
|
134
|
+
run: (view) => {
|
|
135
|
+
// Only complete if cursor is at the end
|
|
136
|
+
if (view.state.selection.main.head !== view.state.doc.length) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const plugin = view.plugin(suggest);
|
|
141
|
+
return plugin?.completeSuggestion(view) ?? false;
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
key: 'Enter',
|
|
146
|
+
preventDefault: true,
|
|
147
|
+
run: (view) => {
|
|
148
|
+
const text = view.state.doc.toString().trim();
|
|
149
|
+
if (onSubmit && (fireIfEmpty || text.length > 0)) {
|
|
150
|
+
const reset = onSubmit(text);
|
|
151
|
+
|
|
152
|
+
// Clear the document after calling onEnter.
|
|
153
|
+
if (reset) {
|
|
154
|
+
view.dispatch({
|
|
155
|
+
changes: {
|
|
156
|
+
from: 0,
|
|
157
|
+
to: view.state.doc.length,
|
|
158
|
+
insert: '',
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return true;
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
key: 'Shift-Enter',
|
|
169
|
+
preventDefault: true,
|
|
170
|
+
run: (view) => {
|
|
171
|
+
view.dispatch({
|
|
172
|
+
changes: {
|
|
173
|
+
from: view.state.selection.main.head,
|
|
174
|
+
insert: '\n',
|
|
175
|
+
},
|
|
176
|
+
selection: {
|
|
177
|
+
anchor: view.state.selection.main.head + 1,
|
|
178
|
+
head: view.state.selection.main.head + 1,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
return true;
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
key: 'Escape',
|
|
186
|
+
preventDefault: true,
|
|
187
|
+
run: (view) => {
|
|
188
|
+
// Clear the entire document.
|
|
189
|
+
view.dispatch({
|
|
190
|
+
changes: {
|
|
191
|
+
from: 0,
|
|
192
|
+
to: view.state.doc.length,
|
|
193
|
+
insert: '',
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
onCancel?.();
|
|
197
|
+
return true;
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
]),
|
|
201
|
+
),
|
|
202
|
+
];
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
class InlineSuggestionWidget extends WidgetType {
|
|
206
|
+
constructor(private suffix: string) {
|
|
207
|
+
super();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
override toDOM(): HTMLSpanElement {
|
|
211
|
+
const span = document.createElement('span');
|
|
212
|
+
span.textContent = this.suffix;
|
|
213
|
+
span.className = 'cm-inline-suggestion';
|
|
214
|
+
return span;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
override eq(other: InlineSuggestionWidget): boolean {
|
|
218
|
+
return other.suffix === this.suffix;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export type CompoetionContext = { line: string };
|
|
6
|
+
|
|
7
|
+
export type CompletionOptions = {
|
|
8
|
+
default?: string;
|
|
9
|
+
minLength?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Util to match current line to a static list of completions.
|
|
14
|
+
*/
|
|
15
|
+
export const staticCompletion =
|
|
16
|
+
(completions: string[], options: CompletionOptions = {}) =>
|
|
17
|
+
({ line }: CompoetionContext) => {
|
|
18
|
+
if (line.length === 0 && options.default) {
|
|
19
|
+
return options.default;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const parts = line.split(/\s+/).filter(Boolean);
|
|
23
|
+
if (parts.length) {
|
|
24
|
+
const str = parts.at(-1)!;
|
|
25
|
+
if (str.length >= (options.minLength ?? 0)) {
|
|
26
|
+
for (const completion of completions) {
|
|
27
|
+
const match = matchCompletion(completion, str);
|
|
28
|
+
if (match) {
|
|
29
|
+
return match;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const matchCompletion = (completion: string, str: string, minLength = 0): string | undefined => {
|
|
37
|
+
if (
|
|
38
|
+
str.length >= minLength &&
|
|
39
|
+
completion.length > str.length &&
|
|
40
|
+
completion.startsWith(str)
|
|
41
|
+
// TODO(burdon): If case insensitive, need to replace existing chars.
|
|
42
|
+
// completion.toLowerCase().startsWith(str.toLowerCase())
|
|
43
|
+
) {
|
|
44
|
+
return completion.slice(str.length);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
@@ -4,27 +4,30 @@
|
|
|
4
4
|
//
|
|
5
5
|
|
|
6
6
|
import { type Extension } from '@codemirror/state';
|
|
7
|
-
import { Decoration, EditorView,
|
|
7
|
+
import { Decoration, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
|
|
8
8
|
|
|
9
9
|
import { clientRectsFor, flattenRect } from '../../util';
|
|
10
10
|
|
|
11
11
|
type Content = string | HTMLElement | ((view: EditorView) => HTMLElement);
|
|
12
12
|
|
|
13
13
|
export type PlaceholderOptions = {
|
|
14
|
-
delay?: number;
|
|
15
14
|
content: Content;
|
|
15
|
+
delay?: number;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Shows a transient placeholder at the current cursor position.
|
|
20
|
+
*/
|
|
21
|
+
export const placeholder = ({ content, delay = 3_000 }: PlaceholderOptions): Extension => {
|
|
19
22
|
const plugin = ViewPlugin.fromClass(
|
|
20
23
|
class {
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
_timeout: ReturnType<typeof setTimeout> | undefined;
|
|
25
|
+
_decorations = Decoration.none;
|
|
23
26
|
|
|
24
27
|
update(update: ViewUpdate) {
|
|
25
|
-
if (this.
|
|
26
|
-
window.clearTimeout(this.
|
|
27
|
-
this.
|
|
28
|
+
if (this._timeout) {
|
|
29
|
+
window.clearTimeout(this._timeout);
|
|
30
|
+
this._timeout = undefined;
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
// Check if the active line (where cursor is) is empty.
|
|
@@ -33,10 +36,10 @@ export const placeholder = ({ delay = 3_000, content }: PlaceholderOptions): Ext
|
|
|
33
36
|
if (isEmpty) {
|
|
34
37
|
// Create widget decoration at the start of the current line.
|
|
35
38
|
const lineStart = activeLine.from;
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
39
|
+
this._timeout = setTimeout(() => {
|
|
40
|
+
this._decorations = Decoration.set([
|
|
38
41
|
Decoration.widget({
|
|
39
|
-
widget: new
|
|
42
|
+
widget: new PlaceholderWidget(content),
|
|
40
43
|
side: 1,
|
|
41
44
|
}).range(lineStart),
|
|
42
45
|
]);
|
|
@@ -45,18 +48,18 @@ export const placeholder = ({ delay = 3_000, content }: PlaceholderOptions): Ext
|
|
|
45
48
|
}, delay);
|
|
46
49
|
}
|
|
47
50
|
|
|
48
|
-
this.
|
|
51
|
+
this._decorations = Decoration.none;
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
destroy() {
|
|
52
|
-
if (this.
|
|
53
|
-
clearTimeout(this.
|
|
55
|
+
if (this._timeout) {
|
|
56
|
+
clearTimeout(this._timeout);
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
},
|
|
57
60
|
{
|
|
58
61
|
provide: (plugin) => {
|
|
59
|
-
return [EditorView.decorations.of((view) => view.plugin(plugin)?.
|
|
62
|
+
return [EditorView.decorations.of((view) => view.plugin(plugin)?._decorations ?? Decoration.none)];
|
|
60
63
|
},
|
|
61
64
|
},
|
|
62
65
|
);
|
|
@@ -66,7 +69,7 @@ export const placeholder = ({ delay = 3_000, content }: PlaceholderOptions): Ext
|
|
|
66
69
|
: plugin;
|
|
67
70
|
};
|
|
68
71
|
|
|
69
|
-
class
|
|
72
|
+
export class PlaceholderWidget extends WidgetType {
|
|
70
73
|
constructor(readonly content: Content) {
|
|
71
74
|
super();
|
|
72
75
|
}
|
|
@@ -75,6 +78,7 @@ class Placeholder extends WidgetType {
|
|
|
75
78
|
const wrap = document.createElement('span');
|
|
76
79
|
wrap.className = 'cm-placeholder';
|
|
77
80
|
wrap.style.pointerEvents = 'none';
|
|
81
|
+
wrap.setAttribute('aria-hidden', 'true');
|
|
78
82
|
wrap.appendChild(
|
|
79
83
|
typeof this.content === 'string'
|
|
80
84
|
? document.createTextNode(this.content)
|
|
@@ -82,7 +86,7 @@ class Placeholder extends WidgetType {
|
|
|
82
86
|
? this.content(view)
|
|
83
87
|
: this.content.cloneNode(true),
|
|
84
88
|
);
|
|
85
|
-
|
|
89
|
+
|
|
86
90
|
return wrap;
|
|
87
91
|
}
|
|
88
92
|
|
|
@@ -92,7 +96,7 @@ class Placeholder extends WidgetType {
|
|
|
92
96
|
return null;
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
const style =
|
|
99
|
+
const style = getComputedStyle(dom.parentNode as HTMLElement);
|
|
96
100
|
const rect = flattenRect(rects[0], style.direction !== 'rtl');
|
|
97
101
|
const lineHeight = parseInt(style.lineHeight);
|
|
98
102
|
if (rect.bottom - rect.top > lineHeight * 1.5) {
|
|
@@ -2,28 +2,27 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { EditorSelection, Prec, RangeSetBuilder
|
|
5
|
+
import { EditorSelection, type Extension, Prec, RangeSetBuilder } from '@codemirror/state';
|
|
6
6
|
import {
|
|
7
7
|
type Command,
|
|
8
8
|
Decoration,
|
|
9
9
|
type DecorationSet,
|
|
10
10
|
type EditorView,
|
|
11
|
-
keymap,
|
|
12
11
|
ViewPlugin,
|
|
13
12
|
type ViewUpdate,
|
|
13
|
+
keymap,
|
|
14
14
|
} from '@codemirror/view';
|
|
15
15
|
|
|
16
|
-
import {
|
|
16
|
+
import { type CompoetionContext } from './match';
|
|
17
|
+
import { PlaceholderWidget } from './placeholder';
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// TODO(burdon): Option to complete only at end of line?
|
|
19
|
+
// TODO(burdon): Option to complete only at end of line.
|
|
21
20
|
export type TypeaheadOptions = {
|
|
22
|
-
onComplete?: (context:
|
|
21
|
+
onComplete?: (context: CompoetionContext) => string | undefined;
|
|
23
22
|
};
|
|
24
23
|
|
|
25
24
|
/**
|
|
26
|
-
*
|
|
25
|
+
* Shows a completion placeholder.
|
|
27
26
|
*/
|
|
28
27
|
export const typeahead = ({ onComplete }: TypeaheadOptions = {}): Extension => {
|
|
29
28
|
let hint: string | undefined;
|
|
@@ -57,7 +56,7 @@ export const typeahead = ({ onComplete }: TypeaheadOptions = {}): Extension => {
|
|
|
57
56
|
const str = update.state.sliceDoc(line.from, selection.from);
|
|
58
57
|
hint = onComplete?.({ line: str });
|
|
59
58
|
if (hint) {
|
|
60
|
-
builder.add(selection.from, selection.to, Decoration.widget({ widget: new
|
|
59
|
+
builder.add(selection.from, selection.to, Decoration.widget({ widget: new PlaceholderWidget(hint) }));
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
62
|
|
|
@@ -86,44 +85,3 @@ export const typeahead = ({ onComplete }: TypeaheadOptions = {}): Extension => {
|
|
|
86
85
|
),
|
|
87
86
|
];
|
|
88
87
|
};
|
|
89
|
-
|
|
90
|
-
type CompletionOptions = {
|
|
91
|
-
default?: string;
|
|
92
|
-
minLength?: number;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Util to match current line to a static list of completions.
|
|
97
|
-
*/
|
|
98
|
-
export const staticCompletion =
|
|
99
|
-
(completions: string[], options: CompletionOptions = {}) =>
|
|
100
|
-
({ line }: TypeaheadContext) => {
|
|
101
|
-
if (line.length === 0 && options.default) {
|
|
102
|
-
return options.default;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const parts = line.split(/\s+/).filter(Boolean);
|
|
106
|
-
if (parts.length) {
|
|
107
|
-
const str = parts.at(-1)!;
|
|
108
|
-
if (str.length >= (options.minLength ?? 0)) {
|
|
109
|
-
for (const completion of completions) {
|
|
110
|
-
const match = matchCompletion(completion, str);
|
|
111
|
-
if (match) {
|
|
112
|
-
return match;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export const matchCompletion = (completion: string, str: string, minLength = 0): string | undefined => {
|
|
120
|
-
if (
|
|
121
|
-
str.length >= minLength &&
|
|
122
|
-
completion.length > str.length &&
|
|
123
|
-
completion.startsWith(str)
|
|
124
|
-
// TODO(burdon): If case insensitive, need to replace existing chars.
|
|
125
|
-
// completion.toLowerCase().startsWith(str.toLowerCase())
|
|
126
|
-
) {
|
|
127
|
-
return completion.slice(str.length);
|
|
128
|
-
}
|
|
129
|
-
};
|