@milkdown/preset-gfm 5.4.1 → 6.0.0-next.1
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/README.md +4 -4
- package/lib/{src/auto-link.d.ts → auto-link.d.ts} +0 -0
- package/lib/auto-link.d.ts.map +1 -0
- package/lib/footnote/definition.d.ts +7 -0
- package/lib/footnote/definition.d.ts.map +1 -0
- package/lib/footnote/index.d.ts +3 -0
- package/lib/footnote/index.d.ts.map +1 -0
- package/lib/footnote/reference.d.ts +7 -0
- package/lib/footnote/reference.d.ts.map +1 -0
- package/lib/footnote/utils.d.ts +3 -0
- package/lib/footnote/utils.d.ts.map +1 -0
- package/lib/index.d.ts +37 -1
- package/lib/index.d.ts.map +1 -0
- package/lib/index.es.js +572 -443
- package/lib/index.es.js.map +1 -1
- package/lib/{src/strike-through.d.ts → strike-through.d.ts} +1 -1
- package/lib/strike-through.d.ts.map +1 -0
- package/lib/{src/supported-keys.d.ts → supported-keys.d.ts} +0 -0
- package/lib/supported-keys.d.ts.map +1 -0
- package/lib/{src/table → table}/command.d.ts +0 -0
- package/lib/table/command.d.ts.map +1 -0
- package/lib/{src/table → table}/index.d.ts +0 -0
- package/lib/table/index.d.ts.map +1 -0
- package/lib/{src/table → table}/nodes/index.d.ts +7 -6
- package/lib/table/nodes/index.d.ts.map +1 -0
- package/lib/{src/table → table}/nodes/schema.d.ts +0 -0
- package/lib/table/nodes/schema.d.ts.map +1 -0
- package/lib/{src/table → table}/operator-plugin/actions.d.ts +0 -0
- package/lib/table/operator-plugin/actions.d.ts.map +1 -0
- package/lib/{src/table → table}/operator-plugin/calc-pos.d.ts +0 -0
- package/lib/table/operator-plugin/calc-pos.d.ts.map +1 -0
- package/lib/{src/table → table}/operator-plugin/constant.d.ts +0 -0
- package/lib/table/operator-plugin/constant.d.ts.map +1 -0
- package/lib/{src/table → table}/operator-plugin/helper.d.ts +0 -0
- package/lib/table/operator-plugin/helper.d.ts.map +1 -0
- package/lib/{src/table → table}/operator-plugin/index.d.ts +0 -0
- package/lib/table/operator-plugin/index.d.ts.map +1 -0
- package/lib/table/operator-plugin/style.d.ts +3 -0
- package/lib/table/operator-plugin/style.d.ts.map +1 -0
- package/lib/{src/table → table}/operator-plugin/widget.d.ts +0 -0
- package/lib/table/operator-plugin/widget.d.ts.map +1 -0
- package/lib/{src/table → table}/utils.d.ts +1 -0
- package/lib/table/utils.d.ts.map +1 -0
- package/lib/{src/task-list-item.d.ts → task-list-item.d.ts} +1 -1
- package/lib/task-list-item.d.ts.map +1 -0
- package/package.json +36 -12
- package/src/auto-link.ts +1 -1
- package/src/footnote/definition.ts +181 -0
- package/src/footnote/index.ts +3 -0
- package/src/footnote/reference.ts +153 -0
- package/src/footnote/utils.ts +4 -0
- package/src/index.ts +5 -0
- package/src/strike-through.ts +2 -9
- package/src/table/nodes/index.ts +20 -14
- package/src/table/operator-plugin/actions.ts +9 -9
- package/src/table/operator-plugin/helper.ts +1 -1
- package/src/table/operator-plugin/index.ts +8 -6
- package/src/table/operator-plugin/style.ts +109 -37
- package/src/table/operator-plugin/widget.ts +3 -4
- package/src/table/utils.ts +9 -6
- package/src/task-list-item.ts +65 -130
- package/lib/src/auto-link.d.ts.map +0 -1
- package/lib/src/index.d.ts +0 -36
- package/lib/src/index.d.ts.map +0 -1
- package/lib/src/strike-through.d.ts.map +0 -1
- package/lib/src/supported-keys.d.ts.map +0 -1
- package/lib/src/table/command.d.ts.map +0 -1
- package/lib/src/table/index.d.ts.map +0 -1
- package/lib/src/table/nodes/index.d.ts.map +0 -1
- package/lib/src/table/nodes/schema.d.ts.map +0 -1
- package/lib/src/table/nodes/style.d.ts +0 -3
- package/lib/src/table/nodes/style.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/actions.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/calc-pos.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/constant.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/helper.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/index.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/style.d.ts +0 -3
- package/lib/src/table/operator-plugin/style.d.ts.map +0 -1
- package/lib/src/table/operator-plugin/widget.d.ts.map +0 -1
- package/lib/src/table/utils.d.ts.map +0 -1
- package/lib/src/task-list-item.d.ts.map +0 -1
- package/src/table/nodes/style.ts +0 -170
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
|
|
3
|
+
import { commandsCtx, createCmd, createCmdKey, editorViewCtx, ThemeInputChipType } from '@milkdown/core';
|
|
4
|
+
import {
|
|
5
|
+
EditorView,
|
|
6
|
+
findSelectedNodeOfType,
|
|
7
|
+
NodeSelection,
|
|
8
|
+
Plugin,
|
|
9
|
+
PluginKey,
|
|
10
|
+
wrappingInputRule,
|
|
11
|
+
} from '@milkdown/prose';
|
|
12
|
+
import { createNode } from '@milkdown/utils';
|
|
13
|
+
|
|
14
|
+
import { getFootnoteDefId, getFootnoteRefId } from './utils';
|
|
15
|
+
|
|
16
|
+
const key = new PluginKey('MILKDOWN_PLUGIN_FOOTNOTE_DEF_INPUT');
|
|
17
|
+
export const ModifyFootnoteDef = createCmdKey<string>('ModifyFootnoteDef');
|
|
18
|
+
|
|
19
|
+
export const footnoteDefinition = createNode((utils) => {
|
|
20
|
+
const id = 'footnote_definition';
|
|
21
|
+
const markdownId = 'footnoteDefinition';
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
id,
|
|
25
|
+
schema: (ctx) => ({
|
|
26
|
+
group: 'block',
|
|
27
|
+
content: 'block+',
|
|
28
|
+
defining: true,
|
|
29
|
+
attrs: {
|
|
30
|
+
label: {
|
|
31
|
+
default: '',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
parseDOM: [
|
|
35
|
+
{
|
|
36
|
+
tag: `div[data-type="${id}"]`,
|
|
37
|
+
getAttrs: (dom) => {
|
|
38
|
+
if (!(dom instanceof HTMLElement)) {
|
|
39
|
+
throw new Error();
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
label: dom.dataset['label'],
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
toDOM: (node) => {
|
|
48
|
+
const label = node.attrs['label'];
|
|
49
|
+
const className = utils.getClassName(node.attrs, 'footnote-definition');
|
|
50
|
+
|
|
51
|
+
const dt = document.createElement('dt');
|
|
52
|
+
dt.textContent = `[ ${label} ]:`;
|
|
53
|
+
dt.onclick = () => {
|
|
54
|
+
const view = ctx.get(editorViewCtx);
|
|
55
|
+
const selection = NodeSelection.create(view.state.doc, view.state.selection.from - 2);
|
|
56
|
+
view.dispatch(view.state.tr.setSelection(selection));
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return [
|
|
60
|
+
'div',
|
|
61
|
+
{
|
|
62
|
+
class: className,
|
|
63
|
+
'data-label': label,
|
|
64
|
+
'data-type': id,
|
|
65
|
+
id: getFootnoteDefId(label),
|
|
66
|
+
},
|
|
67
|
+
['div', { class: 'footnote-definition_content' }, dt, ['dd', 0]],
|
|
68
|
+
[
|
|
69
|
+
'div',
|
|
70
|
+
{ class: 'footnote-definition_anchor' },
|
|
71
|
+
[
|
|
72
|
+
'a',
|
|
73
|
+
{
|
|
74
|
+
href: `#${getFootnoteRefId(label)}`,
|
|
75
|
+
'content-editable': 'false',
|
|
76
|
+
class: className,
|
|
77
|
+
},
|
|
78
|
+
'↩',
|
|
79
|
+
],
|
|
80
|
+
],
|
|
81
|
+
];
|
|
82
|
+
},
|
|
83
|
+
parseMarkdown: {
|
|
84
|
+
match: ({ type }) => type === markdownId,
|
|
85
|
+
runner: (state, node, type) => {
|
|
86
|
+
state
|
|
87
|
+
.openNode(type, {
|
|
88
|
+
label: node['label'] as string,
|
|
89
|
+
})
|
|
90
|
+
.next(node.children)
|
|
91
|
+
.closeNode();
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
toMarkdown: {
|
|
95
|
+
match: (node) => node.type.name === id,
|
|
96
|
+
runner: (state, node) => {
|
|
97
|
+
state
|
|
98
|
+
.openNode(markdownId, undefined, {
|
|
99
|
+
label: node.attrs['label'],
|
|
100
|
+
identifier: node.attrs['label'],
|
|
101
|
+
})
|
|
102
|
+
.next(node.content)
|
|
103
|
+
.closeNode();
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
}),
|
|
107
|
+
commands: (nodeType) => [
|
|
108
|
+
createCmd(ModifyFootnoteDef, (label = '') => (state, dispatch) => {
|
|
109
|
+
const node = findSelectedNodeOfType(state.selection, nodeType);
|
|
110
|
+
if (!node) return false;
|
|
111
|
+
|
|
112
|
+
const { tr } = state;
|
|
113
|
+
const _tr = tr.setNodeMarkup(node.pos, undefined, { ...node.node.attrs, label });
|
|
114
|
+
dispatch?.(_tr.setSelection(NodeSelection.create(_tr.doc, node.pos)));
|
|
115
|
+
|
|
116
|
+
return true;
|
|
117
|
+
}),
|
|
118
|
+
],
|
|
119
|
+
inputRules: (nodeType) => [
|
|
120
|
+
wrappingInputRule(/(?:\[\^)([^:]+)(?::)$/, nodeType, (match) => {
|
|
121
|
+
const label = match[1] ?? 'footnote';
|
|
122
|
+
return {
|
|
123
|
+
label,
|
|
124
|
+
};
|
|
125
|
+
}),
|
|
126
|
+
],
|
|
127
|
+
prosePlugins: (type, ctx) => {
|
|
128
|
+
const inputChipRenderer = utils.themeManager.get<ThemeInputChipType>('input-chip', {
|
|
129
|
+
placeholder: 'Input Footnote Label',
|
|
130
|
+
onUpdate: (value) => {
|
|
131
|
+
ctx.get(commandsCtx).call(ModifyFootnoteDef, value);
|
|
132
|
+
},
|
|
133
|
+
isBindMode: true,
|
|
134
|
+
});
|
|
135
|
+
const shouldDisplay = (view: EditorView) => {
|
|
136
|
+
return Boolean(type && findSelectedNodeOfType(view.state.selection, type));
|
|
137
|
+
};
|
|
138
|
+
const getCurrentLabel = (view: EditorView) => {
|
|
139
|
+
const result = findSelectedNodeOfType(view.state.selection, type);
|
|
140
|
+
if (!result) return;
|
|
141
|
+
|
|
142
|
+
const value = result.node.attrs['label'];
|
|
143
|
+
return value;
|
|
144
|
+
};
|
|
145
|
+
const renderByView = (view: EditorView) => {
|
|
146
|
+
if (!view.editable) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const display = shouldDisplay(view);
|
|
150
|
+
if (display) {
|
|
151
|
+
inputChipRenderer.show(view);
|
|
152
|
+
inputChipRenderer.update(getCurrentLabel(view));
|
|
153
|
+
} else {
|
|
154
|
+
inputChipRenderer.hide();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
return [
|
|
158
|
+
new Plugin({
|
|
159
|
+
key,
|
|
160
|
+
view: (editorView) => {
|
|
161
|
+
inputChipRenderer.init(editorView);
|
|
162
|
+
renderByView(editorView);
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
update: (view, prevState) => {
|
|
166
|
+
const isEqualSelection =
|
|
167
|
+
prevState?.doc.eq(view.state.doc) && prevState.selection.eq(view.state.selection);
|
|
168
|
+
if (isEqualSelection) return;
|
|
169
|
+
|
|
170
|
+
renderByView(view);
|
|
171
|
+
},
|
|
172
|
+
destroy: () => {
|
|
173
|
+
inputChipRenderer.destroy();
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
}),
|
|
178
|
+
];
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
|
|
3
|
+
import { commandsCtx, createCmd, createCmdKey, ThemeInputChipType } from '@milkdown/core';
|
|
4
|
+
import { EditorView, findSelectedNodeOfType, InputRule, NodeSelection, Plugin, PluginKey } from '@milkdown/prose';
|
|
5
|
+
import { createNode } from '@milkdown/utils';
|
|
6
|
+
|
|
7
|
+
import { getFootnoteDefId, getFootnoteRefId } from './utils';
|
|
8
|
+
|
|
9
|
+
export const ModifyFootnoteRef = createCmdKey<string>('ModifyFootnoteRef');
|
|
10
|
+
const key = new PluginKey('MILKDOWN_PLUGIN_FOOTNOTE_REF_INPUT');
|
|
11
|
+
|
|
12
|
+
export const footnoteReference = createNode((utils) => {
|
|
13
|
+
const id = 'footnote_reference';
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
id,
|
|
17
|
+
schema: () => ({
|
|
18
|
+
group: 'inline',
|
|
19
|
+
inline: true,
|
|
20
|
+
atom: true,
|
|
21
|
+
attrs: {
|
|
22
|
+
label: {
|
|
23
|
+
default: '',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
parseDOM: [
|
|
27
|
+
{
|
|
28
|
+
tag: `sup[data-type="${id}"]`,
|
|
29
|
+
getAttrs: (dom) => {
|
|
30
|
+
if (!(dom instanceof HTMLElement)) {
|
|
31
|
+
throw new Error();
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
label: dom.dataset['label'],
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
toDOM: (node) => {
|
|
40
|
+
const label = node.attrs['label'];
|
|
41
|
+
return [
|
|
42
|
+
'sup',
|
|
43
|
+
{
|
|
44
|
+
'data-label': label,
|
|
45
|
+
'data-type': id,
|
|
46
|
+
id: getFootnoteRefId(label),
|
|
47
|
+
},
|
|
48
|
+
['a', { href: `#${getFootnoteDefId(label)}` }, `[${label}]`],
|
|
49
|
+
];
|
|
50
|
+
},
|
|
51
|
+
parseMarkdown: {
|
|
52
|
+
match: ({ type }) => type === 'footnoteReference',
|
|
53
|
+
runner: (state, node, type) => {
|
|
54
|
+
state.addNode(type, {
|
|
55
|
+
label: node['label'] as string,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
toMarkdown: {
|
|
60
|
+
match: (node) => node.type.name === id,
|
|
61
|
+
runner: (state, node) => {
|
|
62
|
+
state.addNode('footnoteReference', undefined, undefined, {
|
|
63
|
+
label: node.attrs['label'],
|
|
64
|
+
identifier: node.attrs['label'],
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
}),
|
|
69
|
+
commands: (nodeType) => [
|
|
70
|
+
createCmd(ModifyFootnoteRef, (label = '') => (state, dispatch) => {
|
|
71
|
+
const node = findSelectedNodeOfType(state.selection, nodeType);
|
|
72
|
+
if (!node) return false;
|
|
73
|
+
|
|
74
|
+
const { tr } = state;
|
|
75
|
+
const _tr = tr.setNodeMarkup(node.pos, undefined, { ...node.node.attrs, label });
|
|
76
|
+
dispatch?.(_tr.setSelection(NodeSelection.create(_tr.doc, node.pos)));
|
|
77
|
+
|
|
78
|
+
return true;
|
|
79
|
+
}),
|
|
80
|
+
],
|
|
81
|
+
inputRules: (nodeType) => [
|
|
82
|
+
new InputRule(/(?:\[\^)([^\]]+)(?:\])$/, (state, match, start, end) => {
|
|
83
|
+
const $start = state.doc.resolve(start);
|
|
84
|
+
const index = $start.index();
|
|
85
|
+
const $end = state.doc.resolve(end);
|
|
86
|
+
if (!$start.parent.canReplaceWith(index, $end.index(), nodeType)) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
const label = match[1];
|
|
90
|
+
return state.tr.replaceRangeWith(
|
|
91
|
+
start,
|
|
92
|
+
end,
|
|
93
|
+
nodeType.create({
|
|
94
|
+
label,
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
}),
|
|
98
|
+
],
|
|
99
|
+
prosePlugins: (type, ctx) => {
|
|
100
|
+
const inputChipRenderer = utils.themeManager.get<ThemeInputChipType>('input-chip', {
|
|
101
|
+
placeholder: 'Input Footnote Label',
|
|
102
|
+
onUpdate: (value) => {
|
|
103
|
+
ctx.get(commandsCtx).call(ModifyFootnoteRef, value);
|
|
104
|
+
},
|
|
105
|
+
isBindMode: true,
|
|
106
|
+
});
|
|
107
|
+
const shouldDisplay = (view: EditorView) => {
|
|
108
|
+
return Boolean(type && findSelectedNodeOfType(view.state.selection, type));
|
|
109
|
+
};
|
|
110
|
+
const getCurrentLabel = (view: EditorView) => {
|
|
111
|
+
const result = findSelectedNodeOfType(view.state.selection, type);
|
|
112
|
+
if (!result) return;
|
|
113
|
+
|
|
114
|
+
const value = result.node.attrs['label'];
|
|
115
|
+
return value;
|
|
116
|
+
};
|
|
117
|
+
const renderByView = (view: EditorView) => {
|
|
118
|
+
if (!view.editable) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const display = shouldDisplay(view);
|
|
122
|
+
if (display) {
|
|
123
|
+
inputChipRenderer.show(view);
|
|
124
|
+
inputChipRenderer.update(getCurrentLabel(view));
|
|
125
|
+
} else {
|
|
126
|
+
inputChipRenderer.hide();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
return [
|
|
130
|
+
new Plugin({
|
|
131
|
+
key,
|
|
132
|
+
view: (editorView) => {
|
|
133
|
+
inputChipRenderer.init(editorView);
|
|
134
|
+
renderByView(editorView);
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
update: (view, prevState) => {
|
|
138
|
+
const isEqualSelection =
|
|
139
|
+
prevState?.doc.eq(view.state.doc) && prevState.selection.eq(view.state.selection);
|
|
140
|
+
if (isEqualSelection) return;
|
|
141
|
+
|
|
142
|
+
renderByView(view);
|
|
143
|
+
},
|
|
144
|
+
destroy: () => {
|
|
145
|
+
inputChipRenderer.destroy();
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
}),
|
|
150
|
+
];
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { $remark, AtomList } from '@milkdown/utils';
|
|
|
4
4
|
import remarkGFM from 'remark-gfm';
|
|
5
5
|
|
|
6
6
|
import { urlPlugin } from './auto-link';
|
|
7
|
+
import { footnoteReference } from './footnote';
|
|
8
|
+
import { footnoteDefinition } from './footnote/definition';
|
|
7
9
|
import { strikeThrough, ToggleStrikeThrough } from './strike-through';
|
|
8
10
|
import { table } from './table';
|
|
9
11
|
import {
|
|
@@ -14,6 +16,7 @@ import {
|
|
|
14
16
|
TurnIntoTaskList,
|
|
15
17
|
} from './task-list-item';
|
|
16
18
|
|
|
19
|
+
export * from './footnote';
|
|
17
20
|
export * from './strike-through';
|
|
18
21
|
export { SupportedKeys } from './supported-keys';
|
|
19
22
|
export {
|
|
@@ -77,6 +80,8 @@ export const gfm = AtomList.create([
|
|
|
77
80
|
urlPlugin,
|
|
78
81
|
strikeThrough(),
|
|
79
82
|
taskListItem(),
|
|
83
|
+
footnoteReference(),
|
|
84
|
+
footnoteDefinition(),
|
|
80
85
|
]);
|
|
81
86
|
|
|
82
87
|
export const commands = {
|
package/src/strike-through.ts
CHANGED
|
@@ -9,15 +9,8 @@ type Keys = SupportedKeys['StrikeThrough'];
|
|
|
9
9
|
|
|
10
10
|
export const ToggleStrikeThrough = createCmdKey('ToggleStrikeThrough');
|
|
11
11
|
|
|
12
|
+
const id = 'strike_through';
|
|
12
13
|
export const strikeThrough = createMark<Keys>((utils) => {
|
|
13
|
-
const id = 'strike_through';
|
|
14
|
-
const style = utils.getStyle(
|
|
15
|
-
(themeTool, { css }) =>
|
|
16
|
-
css`
|
|
17
|
-
text-decoration-color: ${themeTool.palette('secondary')};
|
|
18
|
-
`,
|
|
19
|
-
);
|
|
20
|
-
|
|
21
14
|
return {
|
|
22
15
|
id,
|
|
23
16
|
schema: () => ({
|
|
@@ -25,7 +18,7 @@ export const strikeThrough = createMark<Keys>((utils) => {
|
|
|
25
18
|
{ tag: 'del' },
|
|
26
19
|
{ style: 'text-decoration', getAttrs: (value) => (value === 'line-through') as false },
|
|
27
20
|
],
|
|
28
|
-
toDOM: (mark) => ['del', { class: utils.getClassName(mark.attrs, 'strike-through'
|
|
21
|
+
toDOM: (mark) => ['del', { class: utils.getClassName(mark.attrs, 'strike-through') }],
|
|
29
22
|
parseMarkdown: {
|
|
30
23
|
match: (node) => node.type === 'delete',
|
|
31
24
|
runner: (state, node, markType) => {
|
package/src/table/nodes/index.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
import { createCmd, createCmdKey, MarkdownNode, schemaCtx } from '@milkdown/core';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
columnResizing,
|
|
5
|
+
goToNextCell,
|
|
6
|
+
InputRule,
|
|
7
|
+
NodeType,
|
|
8
|
+
Selection,
|
|
9
|
+
tableEditing,
|
|
10
|
+
TextSelection,
|
|
11
|
+
} from '@milkdown/prose';
|
|
4
12
|
import { createPlugin, createShortcut } from '@milkdown/utils';
|
|
5
13
|
|
|
6
14
|
import { exitTable } from '../command';
|
|
7
15
|
import { operatorPlugin } from '../operator-plugin';
|
|
8
16
|
import { createTable } from '../utils';
|
|
9
17
|
import { schema } from './schema';
|
|
10
|
-
import { injectStyle } from './style';
|
|
11
18
|
|
|
12
19
|
export const SupportedKeys = {
|
|
13
20
|
NextCell: 'NextCell',
|
|
@@ -24,7 +31,6 @@ export const BreakTable = createCmdKey('BreakTable');
|
|
|
24
31
|
export const InsertTable = createCmdKey('InsertTable');
|
|
25
32
|
|
|
26
33
|
export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof schema>((utils) => {
|
|
27
|
-
injectStyle(utils);
|
|
28
34
|
return {
|
|
29
35
|
schema: () => ({
|
|
30
36
|
node: {
|
|
@@ -33,7 +39,7 @@ export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof sc
|
|
|
33
39
|
parseMarkdown: {
|
|
34
40
|
match: (node) => node.type === 'table',
|
|
35
41
|
runner: (state, node, type) => {
|
|
36
|
-
const align = node
|
|
42
|
+
const align = node['align'] as (string | null)[];
|
|
37
43
|
const children = (node.children as MarkdownNode[]).map((x, i) => ({
|
|
38
44
|
...x,
|
|
39
45
|
align,
|
|
@@ -52,7 +58,7 @@ export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof sc
|
|
|
52
58
|
|
|
53
59
|
const align: (string | null)[] = [];
|
|
54
60
|
firstLine.forEach((cell) => {
|
|
55
|
-
align.push(cell.attrs
|
|
61
|
+
align.push(cell.attrs['alignment']);
|
|
56
62
|
});
|
|
57
63
|
state.openNode('table', undefined, { align });
|
|
58
64
|
state.next(node.content);
|
|
@@ -65,11 +71,11 @@ export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof sc
|
|
|
65
71
|
parseMarkdown: {
|
|
66
72
|
match: (node) => node.type === 'tableRow',
|
|
67
73
|
runner: (state, node, type) => {
|
|
68
|
-
const align = node
|
|
74
|
+
const align = node['align'] as (string | null)[];
|
|
69
75
|
const children = (node.children as MarkdownNode[]).map((x, i) => ({
|
|
70
76
|
...x,
|
|
71
77
|
align: align[i],
|
|
72
|
-
isHeader: node
|
|
78
|
+
isHeader: node['isHeader'],
|
|
73
79
|
}));
|
|
74
80
|
state.openNode(type);
|
|
75
81
|
state.next(children);
|
|
@@ -88,12 +94,12 @@ export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof sc
|
|
|
88
94
|
table_cell: {
|
|
89
95
|
...schema.table_cell,
|
|
90
96
|
parseMarkdown: {
|
|
91
|
-
match: (node) => node.type === 'tableCell' && !node
|
|
97
|
+
match: (node) => node.type === 'tableCell' && !node['isHeader'],
|
|
92
98
|
runner: (state, node, type) => {
|
|
93
|
-
const align = node
|
|
99
|
+
const align = node['align'] as string;
|
|
94
100
|
state
|
|
95
101
|
.openNode(type, { alignment: align })
|
|
96
|
-
.openNode(state.schema.nodes
|
|
102
|
+
.openNode(state.schema.nodes['paragraph'] as NodeType)
|
|
97
103
|
.next(node.children)
|
|
98
104
|
.closeNode()
|
|
99
105
|
.closeNode();
|
|
@@ -109,11 +115,11 @@ export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof sc
|
|
|
109
115
|
table_header: {
|
|
110
116
|
...schema.table_header,
|
|
111
117
|
parseMarkdown: {
|
|
112
|
-
match: (node) => node.type === 'tableCell' && !!node
|
|
118
|
+
match: (node) => node.type === 'tableCell' && !!node['isHeader'],
|
|
113
119
|
runner: (state, node, type) => {
|
|
114
|
-
const align = node
|
|
120
|
+
const align = node['align'] as string;
|
|
115
121
|
state.openNode(type, { alignment: align });
|
|
116
|
-
state.openNode(state.schema.nodes
|
|
122
|
+
state.openNode(state.schema.nodes['paragraph'] as NodeType);
|
|
117
123
|
state.next(node.children);
|
|
118
124
|
state.closeNode();
|
|
119
125
|
state.closeNode();
|
|
@@ -144,7 +150,7 @@ export const table = createPlugin<Keys, Record<string, unknown>, keyof typeof sc
|
|
|
144
150
|
commands: (_, ctx) => [
|
|
145
151
|
createCmd(PrevCell, () => goToNextCell(-1)),
|
|
146
152
|
createCmd(NextCell, () => goToNextCell(1)),
|
|
147
|
-
createCmd(BreakTable, () => exitTable(ctx.get(schemaCtx).nodes
|
|
153
|
+
createCmd(BreakTable, () => exitTable(ctx.get(schemaCtx).nodes['paragraph'] as NodeType)),
|
|
148
154
|
createCmd(InsertTable, () => (state, dispatch) => {
|
|
149
155
|
const { selection, tr } = state;
|
|
150
156
|
const { from } = selection;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
-
import { Ctx,
|
|
2
|
+
import { Ctx, ThemeIcon, themeManagerCtx } from '@milkdown/core';
|
|
3
3
|
import {
|
|
4
4
|
addColumnAfter,
|
|
5
5
|
addColumnBefore,
|
|
@@ -35,17 +35,17 @@ export enum Action {
|
|
|
35
35
|
|
|
36
36
|
export const createActions: (ctx: Ctx) => Record<Action, Item> = (ctx) => ({
|
|
37
37
|
[Action.AddColLeft]: {
|
|
38
|
-
$: ctx.get(
|
|
38
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'leftArrow')?.dom as HTMLElement,
|
|
39
39
|
command: () => addColumnBefore,
|
|
40
40
|
disable: (view) => !getCellSelection(view).isColSelection(),
|
|
41
41
|
},
|
|
42
42
|
[Action.AddColRight]: {
|
|
43
|
-
$: ctx.get(
|
|
43
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'rightArrow')?.dom as HTMLElement,
|
|
44
44
|
command: () => addColumnAfter,
|
|
45
45
|
disable: (view) => !getCellSelection(view).isColSelection(),
|
|
46
46
|
},
|
|
47
47
|
[Action.AddRowTop]: {
|
|
48
|
-
$: ctx.get(
|
|
48
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'upArrow')?.dom as HTMLElement,
|
|
49
49
|
command: () => (state, dispatch) => {
|
|
50
50
|
if (!isInTable(state)) return false;
|
|
51
51
|
if (dispatch) {
|
|
@@ -59,7 +59,7 @@ export const createActions: (ctx: Ctx) => Record<Action, Item> = (ctx) => ({
|
|
|
59
59
|
getCellSelection(view).$head.parent.type.name === 'table_header',
|
|
60
60
|
},
|
|
61
61
|
[Action.AddRowBottom]: {
|
|
62
|
-
$: ctx.get(
|
|
62
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'downArrow')?.dom as HTMLElement,
|
|
63
63
|
command: () => (state, dispatch) => {
|
|
64
64
|
if (!isInTable(state)) return false;
|
|
65
65
|
if (dispatch) {
|
|
@@ -71,22 +71,22 @@ export const createActions: (ctx: Ctx) => Record<Action, Item> = (ctx) => ({
|
|
|
71
71
|
disable: (view) => !getCellSelection(view).isRowSelection(),
|
|
72
72
|
},
|
|
73
73
|
[Action.AlignLeft]: {
|
|
74
|
-
$: ctx.get(
|
|
74
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'alignLeft')?.dom as HTMLElement,
|
|
75
75
|
command: () => setCellAttr('alignment', 'left'),
|
|
76
76
|
disable: (view) => !getCellSelection(view).isColSelection(),
|
|
77
77
|
},
|
|
78
78
|
[Action.AlignCenter]: {
|
|
79
|
-
$: ctx.get(
|
|
79
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'alignCenter')?.dom as HTMLElement,
|
|
80
80
|
command: () => setCellAttr('alignment', 'center'),
|
|
81
81
|
disable: (view) => !getCellSelection(view).isColSelection(),
|
|
82
82
|
},
|
|
83
83
|
[Action.AlignRight]: {
|
|
84
|
-
$: ctx.get(
|
|
84
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'alignRight')?.dom as HTMLElement,
|
|
85
85
|
command: () => setCellAttr('alignment', 'right'),
|
|
86
86
|
disable: (view) => !getCellSelection(view).isColSelection(),
|
|
87
87
|
},
|
|
88
88
|
[Action.Delete]: {
|
|
89
|
-
$: ctx.get(
|
|
89
|
+
$: ctx.get(themeManagerCtx).get(ThemeIcon, 'delete')?.dom as HTMLElement,
|
|
90
90
|
command: (_, view) => {
|
|
91
91
|
const selection = getCellSelection(view);
|
|
92
92
|
const isCol = selection.isColSelection();
|
|
@@ -19,7 +19,7 @@ export const isFirstRowSelected = (selection: CellSelection) => {
|
|
|
19
19
|
);
|
|
20
20
|
|
|
21
21
|
for (let i = 0, count = cells.length; i < count; i++) {
|
|
22
|
-
if (selectedCells.indexOf(cells[i]) === -1) {
|
|
22
|
+
if (selectedCells.indexOf(cells[i] as number) === -1) {
|
|
23
23
|
return false;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -4,7 +4,7 @@ import { Ctx } from '@milkdown/core';
|
|
|
4
4
|
import { CellSelection, Decoration, DecorationSet, Plugin, PluginKey } from '@milkdown/prose';
|
|
5
5
|
import { Utils } from '@milkdown/utils';
|
|
6
6
|
|
|
7
|
-
import { getCellsInColumn, getCellsInRow } from '../utils';
|
|
7
|
+
import { CellPos, getCellsInColumn, getCellsInRow } from '../utils';
|
|
8
8
|
import { createActions } from './actions';
|
|
9
9
|
import { calculatePosition } from './calc-pos';
|
|
10
10
|
import { ToolTipPos } from './constant';
|
|
@@ -17,10 +17,12 @@ export const key = 'MILKDOWN_PLUGIN_TABLE';
|
|
|
17
17
|
export const operatorPlugin = (ctx: Ctx, utils: Utils) => {
|
|
18
18
|
const items = createActions(ctx);
|
|
19
19
|
const tooltip = document.createElement('div');
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
utils.themeManager.onFlush(() => {
|
|
21
|
+
const style = utils.getStyle(injectStyle);
|
|
22
|
+
if (style) {
|
|
23
|
+
tooltip.classList.add(style);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
24
26
|
tooltip.classList.add('table-tooltip', 'hide');
|
|
25
27
|
|
|
26
28
|
return new Plugin({
|
|
@@ -35,7 +37,7 @@ export const operatorPlugin = (ctx: Ctx, utils: Utils) => {
|
|
|
35
37
|
|
|
36
38
|
const [topLeft] = leftCells;
|
|
37
39
|
|
|
38
|
-
decorations.push(createWidget(ctx, topLeft, ToolTipPos.Point));
|
|
40
|
+
decorations.push(createWidget(ctx, topLeft as CellPos, ToolTipPos.Point));
|
|
39
41
|
leftCells.forEach((cell, i) => {
|
|
40
42
|
decorations.push(createWidget(ctx, cell, ToolTipPos.Left, i));
|
|
41
43
|
});
|