@milkdown/plugin-slash 4.11.2 → 4.13.2
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/package.json +4 -3
- package/src/config.ts +96 -0
- package/src/index.ts +34 -0
- package/src/item.ts +27 -0
- package/src/prose-plugin/dropdown.ts +49 -0
- package/src/prose-plugin/index.ts +21 -0
- package/src/prose-plugin/input.ts +120 -0
- package/src/prose-plugin/props.ts +97 -0
- package/src/prose-plugin/status.ts +47 -0
- package/src/prose-plugin/view.ts +75 -0
- package/src/style.ts +63 -0
- package/src/utility.ts +76 -0
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkdown/plugin-slash",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.13.2",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"module": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"files": [
|
|
10
|
-
"lib"
|
|
10
|
+
"lib",
|
|
11
|
+
"src"
|
|
11
12
|
],
|
|
12
13
|
"keywords": [
|
|
13
14
|
"milkdown",
|
|
@@ -19,7 +20,7 @@
|
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"@emotion/css": "^11.1.3",
|
|
22
|
-
"@milkdown/utils": "4.
|
|
23
|
+
"@milkdown/utils": "4.13.2",
|
|
23
24
|
"smooth-scroll-into-view-if-needed": "^1.1.32",
|
|
24
25
|
"tslib": "^2.2.0"
|
|
25
26
|
},
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { commandsCtx, themeToolCtx } from '@milkdown/core';
|
|
3
|
+
import {
|
|
4
|
+
InsertHr,
|
|
5
|
+
InsertImage,
|
|
6
|
+
InsertTable,
|
|
7
|
+
TurnIntoCodeFence,
|
|
8
|
+
TurnIntoHeading,
|
|
9
|
+
TurnIntoTaskList,
|
|
10
|
+
WrapInBlockquote,
|
|
11
|
+
WrapInBulletList,
|
|
12
|
+
WrapInOrderedList,
|
|
13
|
+
} from '@milkdown/preset-gfm';
|
|
14
|
+
|
|
15
|
+
import type { SlashConfig } from '.';
|
|
16
|
+
import { createDropdownItem, nodeExists } from './utility';
|
|
17
|
+
|
|
18
|
+
export const config: SlashConfig = ({ ctx }) => [
|
|
19
|
+
{
|
|
20
|
+
id: 'h1',
|
|
21
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Large Heading', 'h1'),
|
|
22
|
+
command: () => ctx.get(commandsCtx).call(TurnIntoHeading, 1),
|
|
23
|
+
keyword: ['h1', 'large heading'],
|
|
24
|
+
enable: nodeExists('heading'),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: 'h2',
|
|
28
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Medium Heading', 'h2'),
|
|
29
|
+
command: () => ctx.get(commandsCtx).call(TurnIntoHeading, 2),
|
|
30
|
+
keyword: ['h2', 'medium heading'],
|
|
31
|
+
enable: nodeExists('heading'),
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'h3',
|
|
35
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Small Heading', 'h3'),
|
|
36
|
+
command: () => ctx.get(commandsCtx).call(TurnIntoHeading, 3),
|
|
37
|
+
keyword: ['h3', 'small heading'],
|
|
38
|
+
enable: nodeExists('heading'),
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: 'bulletList',
|
|
42
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Bullet List', 'bulletList'),
|
|
43
|
+
command: () => ctx.get(commandsCtx).call(WrapInBulletList),
|
|
44
|
+
keyword: ['bullet list', 'ul'],
|
|
45
|
+
enable: nodeExists('bullet_list'),
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: 'orderedList',
|
|
49
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Ordered List', 'orderedList'),
|
|
50
|
+
command: () => ctx.get(commandsCtx).call(WrapInOrderedList),
|
|
51
|
+
keyword: ['ordered list', 'ol'],
|
|
52
|
+
enable: nodeExists('ordered_list'),
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'taskList',
|
|
56
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Task List', 'taskList'),
|
|
57
|
+
command: () => ctx.get(commandsCtx).call(TurnIntoTaskList),
|
|
58
|
+
keyword: ['task list', 'task'],
|
|
59
|
+
enable: nodeExists('task_list_item'),
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: 'image',
|
|
63
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Image', 'image'),
|
|
64
|
+
command: () => ctx.get(commandsCtx).call(InsertImage),
|
|
65
|
+
keyword: ['image'],
|
|
66
|
+
enable: nodeExists('image'),
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: 'blockquote',
|
|
70
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Quote', 'quote'),
|
|
71
|
+
command: () => ctx.get(commandsCtx).call(WrapInBlockquote),
|
|
72
|
+
keyword: ['quote', 'blockquote'],
|
|
73
|
+
enable: nodeExists('blockquote'),
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: 'table',
|
|
77
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Table', 'table'),
|
|
78
|
+
command: () => ctx.get(commandsCtx).call(InsertTable),
|
|
79
|
+
keyword: ['table'],
|
|
80
|
+
enable: nodeExists('table'),
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
id: 'code',
|
|
84
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Code Fence', 'code'),
|
|
85
|
+
command: () => ctx.get(commandsCtx).call(TurnIntoCodeFence),
|
|
86
|
+
keyword: ['code'],
|
|
87
|
+
enable: nodeExists('fence'),
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: 'divider',
|
|
91
|
+
dom: createDropdownItem(ctx.get(themeToolCtx), 'Divide Line', 'divider'),
|
|
92
|
+
command: () => ctx.get(commandsCtx).call(InsertHr),
|
|
93
|
+
keyword: ['divider', 'hr'],
|
|
94
|
+
enable: nodeExists('hr'),
|
|
95
|
+
},
|
|
96
|
+
];
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { AtomList, createProsePlugin, Utils } from '@milkdown/utils';
|
|
3
|
+
|
|
4
|
+
import { config } from './config';
|
|
5
|
+
import { WrappedAction } from './item';
|
|
6
|
+
import { createSlashPlugin } from './prose-plugin';
|
|
7
|
+
import { CursorStatus } from './prose-plugin/status';
|
|
8
|
+
|
|
9
|
+
export { config } from './config';
|
|
10
|
+
export { CursorStatus } from './prose-plugin/status';
|
|
11
|
+
export { createDropdownItem, nodeExists } from './utility';
|
|
12
|
+
|
|
13
|
+
export type SlashConfig = (utils: Utils) => WrappedAction[];
|
|
14
|
+
|
|
15
|
+
export type Options = {
|
|
16
|
+
config: SlashConfig;
|
|
17
|
+
placeholder: {
|
|
18
|
+
[CursorStatus.Empty]: string;
|
|
19
|
+
[CursorStatus.Slash]: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export const slashPlugin = createProsePlugin<Options>((options, utils) => {
|
|
23
|
+
const slashConfig = options?.config ?? config;
|
|
24
|
+
const placeholder = {
|
|
25
|
+
[CursorStatus.Empty]: 'Type / to use the slash commands...',
|
|
26
|
+
[CursorStatus.Slash]: 'Type to filter...',
|
|
27
|
+
...(options?.placeholder ?? {}),
|
|
28
|
+
};
|
|
29
|
+
const cfg = slashConfig(utils);
|
|
30
|
+
|
|
31
|
+
return createSlashPlugin(utils, cfg, placeholder);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const slash = AtomList.create([slashPlugin()]);
|
package/src/item.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import type { Command } from 'prosemirror-commands';
|
|
3
|
+
import type { Schema } from 'prosemirror-model';
|
|
4
|
+
|
|
5
|
+
import { cleanUpAndCreateNode } from './utility';
|
|
6
|
+
|
|
7
|
+
export type Action = {
|
|
8
|
+
id: string;
|
|
9
|
+
$: HTMLElement;
|
|
10
|
+
keyword: string[];
|
|
11
|
+
command: Command;
|
|
12
|
+
enable: (schema: Schema) => boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type WrappedAction = Pick<Action, 'keyword' | 'id'> & {
|
|
16
|
+
enable: (schema: Schema) => boolean;
|
|
17
|
+
command: () => void;
|
|
18
|
+
dom: HTMLElement;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const transformAction = (action: WrappedAction): Action => ({
|
|
22
|
+
id: action.id,
|
|
23
|
+
keyword: action.keyword,
|
|
24
|
+
$: action.dom,
|
|
25
|
+
command: cleanUpAndCreateNode(action.command),
|
|
26
|
+
enable: action.enable,
|
|
27
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import scrollIntoView from 'smooth-scroll-into-view-if-needed';
|
|
3
|
+
|
|
4
|
+
import { Action } from '../item';
|
|
5
|
+
import { Status } from './status';
|
|
6
|
+
|
|
7
|
+
export const renderDropdown = (status: Status, dropdownElement: HTMLElement, items: Action[]): boolean => {
|
|
8
|
+
const { filter } = status.get();
|
|
9
|
+
|
|
10
|
+
if (!status.isSlash()) {
|
|
11
|
+
dropdownElement.classList.add('hide');
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const activeList = items
|
|
16
|
+
.filter((item) => {
|
|
17
|
+
item.$.classList.remove('active');
|
|
18
|
+
const result = item.keyword.some((key) => key.includes(filter.toLocaleLowerCase()));
|
|
19
|
+
if (result) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
item.$.classList.add('hide');
|
|
23
|
+
return false;
|
|
24
|
+
})
|
|
25
|
+
.map((item) => {
|
|
26
|
+
item.$.classList.remove('hide');
|
|
27
|
+
return item;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
status.setActions(activeList);
|
|
31
|
+
|
|
32
|
+
if (activeList.length === 0) {
|
|
33
|
+
dropdownElement.classList.add('hide');
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
dropdownElement.classList.remove('hide');
|
|
38
|
+
|
|
39
|
+
activeList[0].$.classList.add('active');
|
|
40
|
+
requestAnimationFrame(() => {
|
|
41
|
+
scrollIntoView(activeList[0].$, {
|
|
42
|
+
scrollMode: 'if-needed',
|
|
43
|
+
block: 'nearest',
|
|
44
|
+
inline: 'nearest',
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return true;
|
|
49
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { Utils } from '@milkdown/utils';
|
|
3
|
+
import { Plugin, PluginKey } from 'prosemirror-state';
|
|
4
|
+
|
|
5
|
+
import { transformAction, WrappedAction } from '../item';
|
|
6
|
+
import { createProps } from './props';
|
|
7
|
+
import { createStatus, CursorStatus } from './status';
|
|
8
|
+
import { createView } from './view';
|
|
9
|
+
|
|
10
|
+
export const key = 'MILKDOWN_PLUGIN_SLASH';
|
|
11
|
+
|
|
12
|
+
export const createSlashPlugin = (utils: Utils, items: WrappedAction[], placeholder: Record<CursorStatus, string>) => {
|
|
13
|
+
const status = createStatus();
|
|
14
|
+
const actions = items.map(transformAction);
|
|
15
|
+
|
|
16
|
+
return new Plugin({
|
|
17
|
+
key: new PluginKey(key),
|
|
18
|
+
props: createProps(status, utils, placeholder),
|
|
19
|
+
view: (view) => createView(status, actions, view, utils),
|
|
20
|
+
});
|
|
21
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
|
|
3
|
+
import { EditorView } from 'prosemirror-view';
|
|
4
|
+
import scrollIntoView from 'smooth-scroll-into-view-if-needed';
|
|
5
|
+
|
|
6
|
+
import { Action } from '../item';
|
|
7
|
+
import { Status } from './status';
|
|
8
|
+
|
|
9
|
+
export const createMouseManager = () => {
|
|
10
|
+
let mouseLock = false;
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
isLock: () => mouseLock,
|
|
14
|
+
lock: () => {
|
|
15
|
+
mouseLock = true;
|
|
16
|
+
},
|
|
17
|
+
unlock: () => {
|
|
18
|
+
mouseLock = false;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export type MouseManager = ReturnType<typeof createMouseManager>;
|
|
23
|
+
|
|
24
|
+
export const handleMouseMove = (mouseManager: MouseManager) => () => {
|
|
25
|
+
mouseManager.unlock();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const handleMouseEnter = (status: Status, mouseManager: MouseManager) => (e: MouseEvent) => {
|
|
29
|
+
if (mouseManager.isLock()) return;
|
|
30
|
+
const active = status.get().activeActions.findIndex((x) => x.$.classList.contains('active'));
|
|
31
|
+
if (active >= 0) {
|
|
32
|
+
status.get().activeActions[active].$.classList.remove('active');
|
|
33
|
+
}
|
|
34
|
+
const { target } = e;
|
|
35
|
+
if (!(target instanceof HTMLElement)) return;
|
|
36
|
+
target.classList.add('active');
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const handleMouseLeave = () => (e: MouseEvent) => {
|
|
40
|
+
const { target } = e;
|
|
41
|
+
if (!(target instanceof HTMLElement)) return;
|
|
42
|
+
target.classList.remove('active');
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const handleClick =
|
|
46
|
+
(status: Status, items: Action[], view: EditorView, dropdownElement: HTMLElement) =>
|
|
47
|
+
(e: Event): void => {
|
|
48
|
+
const { target } = e;
|
|
49
|
+
if (!(target instanceof HTMLElement)) return;
|
|
50
|
+
if (!view) return;
|
|
51
|
+
|
|
52
|
+
const stop = () => {
|
|
53
|
+
e.stopPropagation();
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const el = Object.values(items).find(({ $ }) => $.contains(target));
|
|
58
|
+
if (!el) {
|
|
59
|
+
if (status.isEmpty()) return;
|
|
60
|
+
|
|
61
|
+
status.clearStatus();
|
|
62
|
+
dropdownElement.classList.add('hide');
|
|
63
|
+
stop();
|
|
64
|
+
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
stop();
|
|
69
|
+
el.command(view.state, view.dispatch, view);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const handleKeydown =
|
|
73
|
+
(status: Status, view: EditorView, dropdownElement: HTMLElement, mouseManager: MouseManager) => (e: Event) => {
|
|
74
|
+
if (!(e instanceof KeyboardEvent)) return;
|
|
75
|
+
if (!mouseManager.isLock()) mouseManager.lock();
|
|
76
|
+
|
|
77
|
+
const { key } = e;
|
|
78
|
+
if (!status.isSlash()) return;
|
|
79
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Escape'].includes(key)) return;
|
|
80
|
+
|
|
81
|
+
const { activeActions } = status.get();
|
|
82
|
+
|
|
83
|
+
let active = activeActions.findIndex(({ $ }) => $.classList.contains('active'));
|
|
84
|
+
if (active < 0) active = 0;
|
|
85
|
+
|
|
86
|
+
const moveActive = (next: number) => {
|
|
87
|
+
activeActions[active].$.classList.remove('active');
|
|
88
|
+
activeActions[next].$.classList.add('active');
|
|
89
|
+
scrollIntoView(activeActions[next].$, {
|
|
90
|
+
scrollMode: 'if-needed',
|
|
91
|
+
block: 'nearest',
|
|
92
|
+
inline: 'nearest',
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
if (key === 'ArrowDown') {
|
|
97
|
+
const next = active === activeActions.length - 1 ? 0 : active + 1;
|
|
98
|
+
|
|
99
|
+
moveActive(next);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (key === 'ArrowUp') {
|
|
104
|
+
const next = active === 0 ? activeActions.length - 1 : active - 1;
|
|
105
|
+
|
|
106
|
+
moveActive(next);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (key === 'Escape') {
|
|
111
|
+
if (status.isEmpty()) return;
|
|
112
|
+
|
|
113
|
+
status.clearStatus();
|
|
114
|
+
dropdownElement.classList.add('hide');
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
activeActions[active].command(view.state, view.dispatch, view);
|
|
119
|
+
activeActions[active].$.classList.remove('active');
|
|
120
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { css } from '@emotion/css';
|
|
3
|
+
import { ThemeTool } from '@milkdown/core';
|
|
4
|
+
import { findParentNode, Utils } from '@milkdown/utils';
|
|
5
|
+
import { EditorState } from 'prosemirror-state';
|
|
6
|
+
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view';
|
|
7
|
+
|
|
8
|
+
import { CursorStatus, Status } from './status';
|
|
9
|
+
|
|
10
|
+
export type Props = ReturnType<typeof createProps>;
|
|
11
|
+
|
|
12
|
+
const createEmptyStyle = ({ font, palette }: ThemeTool) => css`
|
|
13
|
+
position: relative;
|
|
14
|
+
&::before {
|
|
15
|
+
position: absolute;
|
|
16
|
+
cursor: text;
|
|
17
|
+
font-family: ${font.typography};
|
|
18
|
+
font-size: 0.875rem;
|
|
19
|
+
color: ${palette('neutral', 0.6)};
|
|
20
|
+
content: attr(data-text);
|
|
21
|
+
height: 100%;
|
|
22
|
+
display: flex;
|
|
23
|
+
align-items: center;
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
const createSlashStyle = () => css`
|
|
28
|
+
&::before {
|
|
29
|
+
left: 0.5rem;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
export const createProps = (status: Status, utils: Utils, placeholder: Record<CursorStatus, string>) => {
|
|
34
|
+
const emptyStyle = utils.getStyle(createEmptyStyle);
|
|
35
|
+
const slashStyle = utils.getStyle(createSlashStyle);
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
handleKeyDown: (_: EditorView, event: Event) => {
|
|
39
|
+
const { cursorStatus, activeActions } = status.get();
|
|
40
|
+
if (cursorStatus !== CursorStatus.Slash || activeActions.length === 0) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!['ArrowUp', 'ArrowDown', 'Enter'].includes(event.key)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return true;
|
|
52
|
+
},
|
|
53
|
+
decorations: (state: EditorState) => {
|
|
54
|
+
const parent = findParentNode(({ type }) => type.name === 'paragraph')(state.selection);
|
|
55
|
+
const isTopLevel = state.selection.$from.depth === 1;
|
|
56
|
+
|
|
57
|
+
if (!parent || parent.node.childCount > 1 || !isTopLevel) {
|
|
58
|
+
status.clearStatus();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const isEmpty = parent.node.content.size === 0;
|
|
63
|
+
const isSlash = parent.node.textContent === '/' && state.selection.$from.parentOffset > 0;
|
|
64
|
+
const isSearch = parent.node.textContent.startsWith('/') && state.selection.$from.parentOffset > 1;
|
|
65
|
+
|
|
66
|
+
const createDecoration = (text: string, className: (string | undefined)[]) => {
|
|
67
|
+
const pos = parent.pos;
|
|
68
|
+
return DecorationSet.create(state.doc, [
|
|
69
|
+
Decoration.node(pos, pos + parent.node.nodeSize, {
|
|
70
|
+
class: className.filter((x) => x).join(' '),
|
|
71
|
+
'data-text': text,
|
|
72
|
+
}),
|
|
73
|
+
]);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (isEmpty) {
|
|
77
|
+
status.clearStatus();
|
|
78
|
+
const text = placeholder[CursorStatus.Empty];
|
|
79
|
+
return createDecoration(text, [emptyStyle, 'empty-node']);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (isSlash) {
|
|
83
|
+
status.setSlash();
|
|
84
|
+
const text = placeholder[CursorStatus.Slash];
|
|
85
|
+
return createDecoration(text, [emptyStyle, slashStyle, 'empty-node', 'is-slash']);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (isSearch) {
|
|
89
|
+
status.setSlash(parent.node.textContent.slice(1));
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
status.clearStatus();
|
|
94
|
+
return null;
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { Action } from '../item';
|
|
3
|
+
|
|
4
|
+
export enum CursorStatus {
|
|
5
|
+
Empty = 'empty',
|
|
6
|
+
Slash = 'slash',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type StatusCtx = {
|
|
10
|
+
cursorStatus: CursorStatus;
|
|
11
|
+
filter: string;
|
|
12
|
+
activeActions: Action[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const createStatusCtx = (): StatusCtx => {
|
|
16
|
+
return {
|
|
17
|
+
cursorStatus: CursorStatus.Empty,
|
|
18
|
+
filter: '',
|
|
19
|
+
activeActions: [],
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const clearStatus = (status: StatusCtx) => {
|
|
24
|
+
status.cursorStatus = CursorStatus.Empty;
|
|
25
|
+
status.filter = '';
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const setSlash = (status: StatusCtx, filter = '') => {
|
|
29
|
+
status.cursorStatus = CursorStatus.Slash;
|
|
30
|
+
status.filter = filter;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type Status = ReturnType<typeof createStatus>;
|
|
34
|
+
|
|
35
|
+
export const createStatus = () => {
|
|
36
|
+
const statusCtx = createStatusCtx();
|
|
37
|
+
return {
|
|
38
|
+
clearStatus: () => clearStatus(statusCtx),
|
|
39
|
+
setSlash: (filter = '') => setSlash(statusCtx, filter),
|
|
40
|
+
setActions: (actions: Action[]) => {
|
|
41
|
+
statusCtx.activeActions = actions;
|
|
42
|
+
},
|
|
43
|
+
get: () => statusCtx,
|
|
44
|
+
isEmpty: () => statusCtx.cursorStatus === CursorStatus.Empty,
|
|
45
|
+
isSlash: () => statusCtx.cursorStatus === CursorStatus.Slash,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { calculateNodePosition, Utils } from '@milkdown/utils';
|
|
3
|
+
import { EditorView } from 'prosemirror-view';
|
|
4
|
+
|
|
5
|
+
import { Action } from '../item';
|
|
6
|
+
import { createDropdown } from '../utility';
|
|
7
|
+
import { renderDropdown } from './dropdown';
|
|
8
|
+
import {
|
|
9
|
+
createMouseManager,
|
|
10
|
+
handleClick,
|
|
11
|
+
handleKeydown,
|
|
12
|
+
handleMouseEnter,
|
|
13
|
+
handleMouseLeave,
|
|
14
|
+
handleMouseMove,
|
|
15
|
+
} from './input';
|
|
16
|
+
import { Status } from './status';
|
|
17
|
+
|
|
18
|
+
const calculatePosition = (view: EditorView, dropdownElement: HTMLElement) => {
|
|
19
|
+
calculateNodePosition(view, dropdownElement, (selected, target, parent) => {
|
|
20
|
+
let left = selected.left - parent.left;
|
|
21
|
+
let top = selected.bottom - parent.top + 14;
|
|
22
|
+
|
|
23
|
+
if (left < 0) {
|
|
24
|
+
left = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (window.innerHeight - selected.bottom < target.height) {
|
|
28
|
+
top = selected.top - parent.top - target.height - 14;
|
|
29
|
+
}
|
|
30
|
+
return [top, left];
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const createView = (status: Status, items: Action[], view: EditorView, utils: Utils) => {
|
|
35
|
+
const wrapper = view.dom.parentNode;
|
|
36
|
+
if (!wrapper) return {};
|
|
37
|
+
|
|
38
|
+
const dropdownElement = createDropdown(utils);
|
|
39
|
+
const mouseManager = createMouseManager();
|
|
40
|
+
wrapper.appendChild(dropdownElement);
|
|
41
|
+
|
|
42
|
+
const _mouseMove = handleMouseMove(mouseManager);
|
|
43
|
+
const _mouseDown = handleClick(status, items, view, dropdownElement);
|
|
44
|
+
const _keydown = handleKeydown(status, view, dropdownElement, mouseManager);
|
|
45
|
+
const _mouseEnter = handleMouseEnter(status, mouseManager);
|
|
46
|
+
const _mouseLeave = handleMouseLeave();
|
|
47
|
+
|
|
48
|
+
items
|
|
49
|
+
.filter((item) => item.enable(view.state.schema))
|
|
50
|
+
.forEach(({ $ }) => {
|
|
51
|
+
$.addEventListener('mouseenter', _mouseEnter);
|
|
52
|
+
$.addEventListener('mouseleave', _mouseLeave);
|
|
53
|
+
dropdownElement.appendChild($);
|
|
54
|
+
});
|
|
55
|
+
wrapper.addEventListener('mousemove', _mouseMove);
|
|
56
|
+
wrapper.addEventListener('mousedown', _mouseDown);
|
|
57
|
+
wrapper.addEventListener('keydown', _keydown);
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
update: (view: EditorView) => {
|
|
61
|
+
const show = renderDropdown(status, dropdownElement, items);
|
|
62
|
+
|
|
63
|
+
if (!show) return;
|
|
64
|
+
|
|
65
|
+
calculatePosition(view, dropdownElement);
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
destroy: () => {
|
|
69
|
+
wrapper.removeEventListener('mousemove', _mouseMove);
|
|
70
|
+
wrapper.removeEventListener('mousedown', _mouseDown);
|
|
71
|
+
wrapper.removeEventListener('keydown', _keydown);
|
|
72
|
+
dropdownElement.remove();
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
};
|
package/src/style.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { css } from '@emotion/css';
|
|
3
|
+
import { ThemeTool } from '@milkdown/core';
|
|
4
|
+
|
|
5
|
+
const itemStyle = ({ font, palette }: ThemeTool) => {
|
|
6
|
+
return css`
|
|
7
|
+
.slash-dropdown-item {
|
|
8
|
+
display: flex;
|
|
9
|
+
gap: 2rem;
|
|
10
|
+
height: 3rem;
|
|
11
|
+
padding: 0 1rem;
|
|
12
|
+
align-items: center;
|
|
13
|
+
justify-content: flex-start;
|
|
14
|
+
cursor: pointer;
|
|
15
|
+
line-height: 2;
|
|
16
|
+
font-family: ${font.typography};
|
|
17
|
+
font-size: 0.875rem;
|
|
18
|
+
|
|
19
|
+
transition: all 0.2s ease-in-out;
|
|
20
|
+
|
|
21
|
+
&,
|
|
22
|
+
.icon {
|
|
23
|
+
color: ${palette('neutral', 0.87)};
|
|
24
|
+
transition: all 0.2s ease-in-out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
&.hide {
|
|
28
|
+
display: none;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
&.active {
|
|
32
|
+
background: ${palette('secondary', 0.12)};
|
|
33
|
+
&,
|
|
34
|
+
.icon {
|
|
35
|
+
color: ${palette('primary')};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const injectStyle = (themeTool: ThemeTool) => {
|
|
42
|
+
const { mixin, size, palette } = themeTool;
|
|
43
|
+
const style = css`
|
|
44
|
+
width: 20.5rem;
|
|
45
|
+
max-height: 20.5rem;
|
|
46
|
+
overflow-y: auto;
|
|
47
|
+
${mixin.border?.()};
|
|
48
|
+
border-radius: ${size.radius};
|
|
49
|
+
position: absolute;
|
|
50
|
+
background: ${palette('surface')};
|
|
51
|
+
|
|
52
|
+
${mixin.shadow?.()};
|
|
53
|
+
|
|
54
|
+
&.hide {
|
|
55
|
+
display: none;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
${mixin.scrollbar?.()};
|
|
59
|
+
|
|
60
|
+
${itemStyle(themeTool)}
|
|
61
|
+
`;
|
|
62
|
+
return style;
|
|
63
|
+
};
|
package/src/utility.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import type { ThemeTool } from '@milkdown/core';
|
|
3
|
+
import type { Icon } from '@milkdown/design-system';
|
|
4
|
+
import type { Utils } from '@milkdown/utils';
|
|
5
|
+
import type { Command } from 'prosemirror-commands';
|
|
6
|
+
import type { Node, Schema } from 'prosemirror-model';
|
|
7
|
+
|
|
8
|
+
import { injectStyle } from './style';
|
|
9
|
+
|
|
10
|
+
export const createDropdown = (utils: Utils) => {
|
|
11
|
+
const div = document.createElement('div');
|
|
12
|
+
div.setAttribute('role', 'listbox');
|
|
13
|
+
div.setAttribute('tabindex', '-1');
|
|
14
|
+
const style = utils.getStyle(injectStyle);
|
|
15
|
+
|
|
16
|
+
if (style) {
|
|
17
|
+
div.classList.add(style);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
div.classList.add('slash-dropdown', 'hide');
|
|
21
|
+
|
|
22
|
+
return div;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type ItemOptions = {
|
|
26
|
+
textClassName: string;
|
|
27
|
+
};
|
|
28
|
+
export const createDropdownItem = (themeTool: ThemeTool, text: string, icon: Icon, options?: Partial<ItemOptions>) => {
|
|
29
|
+
const textClassName = options?.textClassName ?? 'text';
|
|
30
|
+
|
|
31
|
+
const div = document.createElement('div');
|
|
32
|
+
div.setAttribute('role', 'option');
|
|
33
|
+
div.classList.add('slash-dropdown-item');
|
|
34
|
+
|
|
35
|
+
const iconSpan = themeTool.slots.icon(icon);
|
|
36
|
+
|
|
37
|
+
const textSpan = document.createElement('span');
|
|
38
|
+
textSpan.textContent = text;
|
|
39
|
+
textSpan.className = textClassName;
|
|
40
|
+
|
|
41
|
+
div.appendChild(iconSpan);
|
|
42
|
+
div.appendChild(textSpan);
|
|
43
|
+
|
|
44
|
+
return div;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const getDepth = (node: Node) => {
|
|
48
|
+
let cur = node;
|
|
49
|
+
let depth = 0;
|
|
50
|
+
while (cur.childCount) {
|
|
51
|
+
cur = cur.child(0);
|
|
52
|
+
depth += 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return depth;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const cleanUp: Command = (state, dispatch) => {
|
|
59
|
+
const { selection } = state;
|
|
60
|
+
const { $from } = selection;
|
|
61
|
+
const tr = state.tr.deleteRange($from.start(), $from.pos);
|
|
62
|
+
dispatch?.(tr);
|
|
63
|
+
return false;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const cleanUpAndCreateNode =
|
|
67
|
+
(createCommand: () => void): Command =>
|
|
68
|
+
(state, dispatch, view) => {
|
|
69
|
+
if (view) {
|
|
70
|
+
cleanUp(state, dispatch, view);
|
|
71
|
+
createCommand();
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const nodeExists = (name: string) => (schema: Schema) => Boolean(schema.nodes[name]);
|