@14ch/svelte-md-outliner 0.0.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.ja.md +190 -0
- package/README.md +190 -0
- package/dist/assets/styles/editor.css +1459 -0
- package/dist/assets/styles/theme.css +486 -0
- package/dist/components/ContextMenu.svelte +279 -0
- package/dist/components/ContextMenu.svelte.d.ts +9 -0
- package/dist/components/EditorSurface.svelte +99 -0
- package/dist/components/EditorSurface.svelte.d.ts +16 -0
- package/dist/components/FocusBreadcrumb.svelte +138 -0
- package/dist/components/FocusBreadcrumb.svelte.d.ts +16 -0
- package/dist/components/MarkdownEditor.svelte +393 -0
- package/dist/components/MarkdownEditor.svelte.d.ts +119 -0
- package/dist/components/ShortcutHelpDialog.svelte +155 -0
- package/dist/components/ShortcutHelpDialog.svelte.d.ts +12 -0
- package/dist/components/internal/Dialog.svelte +157 -0
- package/dist/components/internal/Dialog.svelte.d.ts +23 -0
- package/dist/components/internal/Modal.svelte +181 -0
- package/dist/components/internal/Modal.svelte.d.ts +18 -0
- package/dist/components/internal/Popup.svelte +248 -0
- package/dist/components/internal/Popup.svelte.d.ts +26 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +28 -0
- package/dist/markdown.d.ts +13 -0
- package/dist/markdown.js +10 -0
- package/dist/messages/en.d.ts +2 -0
- package/dist/messages/en.js +107 -0
- package/dist/messages/index.d.ts +13 -0
- package/dist/messages/index.js +31 -0
- package/dist/messages/ja.d.ts +2 -0
- package/dist/messages/ja.js +112 -0
- package/dist/messages/messages.types.d.ts +59 -0
- package/dist/messages/messages.types.js +1 -0
- package/dist/models/document/bodySelection.d.ts +32 -0
- package/dist/models/document/bodySelection.js +43 -0
- package/dist/models/document/document.types.d.ts +27 -0
- package/dist/models/document/document.types.js +8 -0
- package/dist/models/document/foldable.d.ts +18 -0
- package/dist/models/document/foldable.js +21 -0
- package/dist/models/document/schema.d.ts +2 -0
- package/dist/models/document/schema.js +428 -0
- package/dist/models/document/sectionTree.d.ts +15 -0
- package/dist/models/document/sectionTree.js +123 -0
- package/dist/models/markdown/blockOutline.d.ts +9 -0
- package/dist/models/markdown/blockOutline.js +82 -0
- package/dist/models/markdown/frontMatter.d.ts +98 -0
- package/dist/models/markdown/frontMatter.js +300 -0
- package/dist/models/markdown/markdown.types.d.ts +60 -0
- package/dist/models/markdown/markdown.types.js +1 -0
- package/dist/models/markdown/markdownIt.d.ts +1 -0
- package/dist/models/markdown/markdownIt.js +92 -0
- package/dist/models/markdown/parser.d.ts +4 -0
- package/dist/models/markdown/parser.js +505 -0
- package/dist/models/markdown/plainText.d.ts +1 -0
- package/dist/models/markdown/plainText.js +25 -0
- package/dist/models/markdown/serializer.d.ts +17 -0
- package/dist/models/markdown/serializer.js +453 -0
- package/dist/models/viewState/viewState.types.d.ts +38 -0
- package/dist/models/viewState/viewState.types.js +17 -0
- package/dist/models/viewState/viewStateCodec.d.ts +31 -0
- package/dist/models/viewState/viewStateCodec.js +173 -0
- package/dist/operations/autoformat.d.ts +9 -0
- package/dist/operations/autoformat.js +86 -0
- package/dist/operations/commands/block.d.ts +24 -0
- package/dist/operations/commands/block.js +139 -0
- package/dist/operations/commands/codeBlock.d.ts +7 -0
- package/dist/operations/commands/codeBlock.js +26 -0
- package/dist/operations/commands/element.d.ts +67 -0
- package/dist/operations/commands/element.js +269 -0
- package/dist/operations/commands/frontMatter.d.ts +35 -0
- package/dist/operations/commands/frontMatter.js +120 -0
- package/dist/operations/commands/lineBreak.d.ts +9 -0
- package/dist/operations/commands/lineBreak.js +16 -0
- package/dist/operations/commands/link.d.ts +7 -0
- package/dist/operations/commands/link.js +21 -0
- package/dist/operations/commands/list.d.ts +126 -0
- package/dist/operations/commands/list.js +652 -0
- package/dist/operations/commands/mermaidBlock.d.ts +9 -0
- package/dist/operations/commands/mermaidBlock.js +28 -0
- package/dist/operations/commands/section.d.ts +217 -0
- package/dist/operations/commands/section.js +865 -0
- package/dist/operations/commands/sectionFold.d.ts +36 -0
- package/dist/operations/commands/sectionFold.js +219 -0
- package/dist/operations/commands/sectionJump.d.ts +8 -0
- package/dist/operations/commands/sectionJump.js +144 -0
- package/dist/operations/commands/selectedBlock.d.ts +37 -0
- package/dist/operations/commands/selectedBlock.js +170 -0
- package/dist/operations/commands/table.d.ts +64 -0
- package/dist/operations/commands/table.js +437 -0
- package/dist/operations/keymap/assemble.d.ts +22 -0
- package/dist/operations/keymap/assemble.js +165 -0
- package/dist/operations/keymap/coreActions.d.ts +2 -0
- package/dist/operations/keymap/coreActions.js +711 -0
- package/dist/operations/keymap/menuItems.d.ts +52 -0
- package/dist/operations/keymap/menuItems.js +114 -0
- package/dist/operations/keymap/shortcutKeys.d.ts +42 -0
- package/dist/operations/keymap/shortcutKeys.js +114 -0
- package/dist/operations/keymap/viewActions.d.ts +30 -0
- package/dist/operations/keymap/viewActions.js +182 -0
- package/dist/operations/operations.types.d.ts +121 -0
- package/dist/operations/operations.types.js +1 -0
- package/dist/operations/plugins/blockHandle.d.ts +25 -0
- package/dist/operations/plugins/blockHandle.js +112 -0
- package/dist/operations/plugins/contextMenu.d.ts +33 -0
- package/dist/operations/plugins/contextMenu.js +204 -0
- package/dist/operations/plugins/focus.d.ts +39 -0
- package/dist/operations/plugins/focus.js +350 -0
- package/dist/operations/plugins/foldClick.d.ts +11 -0
- package/dist/operations/plugins/foldClick.js +117 -0
- package/dist/operations/plugins/foldGuard.d.ts +15 -0
- package/dist/operations/plugins/foldGuard.js +31 -0
- package/dist/operations/plugins/frontMatterGuard.d.ts +21 -0
- package/dist/operations/plugins/frontMatterGuard.js +150 -0
- package/dist/operations/plugins/frontMatterView.d.ts +34 -0
- package/dist/operations/plugins/frontMatterView.js +137 -0
- package/dist/operations/plugins/linkPaste.d.ts +3 -0
- package/dist/operations/plugins/linkPaste.js +20 -0
- package/dist/operations/plugins/listStructure.d.ts +13 -0
- package/dist/operations/plugins/listStructure.js +18 -0
- package/dist/operations/plugins/markdownClipboard.d.ts +8 -0
- package/dist/operations/plugins/markdownClipboard.js +208 -0
- package/dist/operations/plugins/mermaidView.d.ts +20 -0
- package/dist/operations/plugins/mermaidView.js +206 -0
- package/dist/operations/plugins/requiredSelectionEntry.d.ts +10 -0
- package/dist/operations/plugins/requiredSelectionEntry.js +124 -0
- package/dist/operations/plugins/sectionView.d.ts +12 -0
- package/dist/operations/plugins/sectionView.js +44 -0
- package/dist/operations/plugins/tabEscape.d.ts +21 -0
- package/dist/operations/plugins/tabEscape.js +41 -0
- package/dist/operations/plugins/tableHandles.d.ts +24 -0
- package/dist/operations/plugins/tableHandles.js +89 -0
- package/dist/operations/transaction.d.ts +26 -0
- package/dist/operations/transaction.js +51 -0
- package/dist/operations/transactionOrigin.d.ts +17 -0
- package/dist/operations/transactionOrigin.js +9 -0
- package/dist/states/contextMenuController.svelte.d.ts +57 -0
- package/dist/states/contextMenuController.svelte.js +206 -0
- package/dist/states/editorSession.d.ts +12 -0
- package/dist/states/editorSession.js +268 -0
- package/dist/states/editorSession.types.d.ts +57 -0
- package/dist/states/editorSession.types.js +1 -0
- package/dist/states/outlineViewController.svelte.d.ts +52 -0
- package/dist/states/outlineViewController.svelte.js +134 -0
- package/dist/utils/dateText.d.ts +25 -0
- package/dist/utils/dateText.js +71 -0
- package/dist/utils/geometry.types.d.ts +13 -0
- package/dist/utils/geometry.types.js +1 -0
- package/dist/utils/json.types.d.ts +4 -0
- package/dist/utils/json.types.js +1 -0
- package/dist/utils/platform.d.ts +8 -0
- package/dist/utils/platform.js +10 -0
- package/dist/utils/result.types.d.ts +7 -0
- package/dist/utils/result.types.js +1 -0
- package/package.json +109 -0
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import { joinTextblockBackward } from 'prosemirror-commands';
|
|
2
|
+
import { backspaceCommand, createIndentListCommand, createMoveListCommand, createSplitListCommand, createToggleListCommand, createUnwrapListCommand, doSplitList, findListsRange, isListNode } from 'prosemirror-flat-list';
|
|
3
|
+
import { TextSelection } from 'prosemirror-state';
|
|
4
|
+
import { outlineSchema } from '../../models/document/schema';
|
|
5
|
+
import { moveContent, setCursor } from '../transaction';
|
|
6
|
+
import { blockContentEnd, blockRunEnd, blockRunStart, coveredEnd, nextSection, previousContainer, topLevelBlock } from './section';
|
|
7
|
+
export const listCommands = {
|
|
8
|
+
indentList,
|
|
9
|
+
dedentList,
|
|
10
|
+
splitListItem,
|
|
11
|
+
toggleTaskChecked,
|
|
12
|
+
toggleListKind: (kind) => createToggleListCommand({ kind }),
|
|
13
|
+
unwrapList: createUnwrapListCommand(),
|
|
14
|
+
moveListItem,
|
|
15
|
+
backspace: listBackspace,
|
|
16
|
+
liftBlockOutOfItem,
|
|
17
|
+
isInList: (state) => enclosingListItem(state) !== null,
|
|
18
|
+
enclosingListItem
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* 段落を箇条書きの項目にする。
|
|
22
|
+
*
|
|
23
|
+
* これは段落に対する階層操作ではなく、`Tab` に割り当てたブロック種別の変換である。
|
|
24
|
+
* 最上位の項目で `Shift+Tab` を押すと段落へ戻るので、
|
|
25
|
+
* 同じキーの対で往復できる。片方だけだと、リストから出られるのに戻れなくなる。
|
|
26
|
+
*
|
|
27
|
+
* すでに項目の中にいるときは何もしない。ここまで来るのは「直前に兄弟がなくて
|
|
28
|
+
* 深くできなかった」場合であり、そこで種別を切り替えると項目が解除されてしまう。
|
|
29
|
+
*/
|
|
30
|
+
export const paragraphToListItem = (state, dispatch, view) => {
|
|
31
|
+
if (listCommands.isInList(state))
|
|
32
|
+
return false;
|
|
33
|
+
if (state.selection.$from.parent.type !== outlineSchema.nodes.paragraph)
|
|
34
|
+
return false;
|
|
35
|
+
return listCommands.toggleListKind('bullet')(state, dispatch, view);
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* 項目の本文より後ろにあるブロックを、その項目の外へ出す(段上げ)。
|
|
39
|
+
*
|
|
40
|
+
* 行き先は**項目の直後**であり、元の位置ではない。元の位置で外へ出すと、そのブロックの
|
|
41
|
+
* 後ろに配下が残っている場合に項目が 2 つへ割れる(編集基盤の `lift` はそうなる)。
|
|
42
|
+
* 直後へ移せば、配下の並びを保ったままブロックだけが 1 段外へ出る。
|
|
43
|
+
*/
|
|
44
|
+
function liftBlockOutOfItem(pos, state, dispatch) {
|
|
45
|
+
const $block = state.doc.resolve(pos);
|
|
46
|
+
const item = $block.parent;
|
|
47
|
+
if (!isListNode(item))
|
|
48
|
+
return false;
|
|
49
|
+
if (!dispatch)
|
|
50
|
+
return true;
|
|
51
|
+
const block = state.doc.nodeAt(pos);
|
|
52
|
+
if (!block)
|
|
53
|
+
return false;
|
|
54
|
+
const itemEnd = $block.before() + item.nodeSize;
|
|
55
|
+
const tr = state.tr;
|
|
56
|
+
tr.delete(pos, pos + block.nodeSize);
|
|
57
|
+
const at = tr.mapping.map(itemEnd);
|
|
58
|
+
tr.insert(at, block);
|
|
59
|
+
setCursor(tr, at + 1);
|
|
60
|
+
dispatch(tr.scrollIntoView());
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 項目の中での `Enter`。
|
|
65
|
+
*
|
|
66
|
+
* ライブラリの split は「最初のブロックが空なら項目を解いて格上げする」分岐を持つが、
|
|
67
|
+
* それを通さない。**空かどうかで結果を変えない**。空の項目は
|
|
68
|
+
* 「まだ書いていない項目」であって、段落にしたいものではない。
|
|
69
|
+
*
|
|
70
|
+
* 空のときは本文の先頭で押したときと同じ扱いにする。すなわち新しい空の項目が前へ入り、
|
|
71
|
+
* カーソルは元の項目に残る。どちらの項目も空なので、書き手からは
|
|
72
|
+
* 「項目が 1 つ増えて、カーソルは下の項目にいる」ように見える。
|
|
73
|
+
*/
|
|
74
|
+
function splitListItem(state, dispatch, view) {
|
|
75
|
+
const at = itemAtCursorStart(state);
|
|
76
|
+
if (at && at.node.firstChild?.content.size === 0) {
|
|
77
|
+
return doSplitList(state, at.node, dispatch);
|
|
78
|
+
}
|
|
79
|
+
if (splitIntoFirstChild(state, dispatch))
|
|
80
|
+
return true;
|
|
81
|
+
return librarySplitListItem(state, dispatch, view);
|
|
82
|
+
}
|
|
83
|
+
const librarySplitListItem = createSplitListCommand();
|
|
84
|
+
/**
|
|
85
|
+
* 配下を持つ項目の本文の末尾での `Enter`。**配下の先頭に新しい項目を作る。**
|
|
86
|
+
*
|
|
87
|
+
* 兄弟として作ると、書いたばかりの項目とその配下の間に別の項目が割り込む。書き手が
|
|
88
|
+
* 見ているのは「この項目の続きを書く」であって、配下より前に別の話を差し込むことでは
|
|
89
|
+
* ない。配下があるなら、続きが入るのは配下の先頭である。
|
|
90
|
+
*
|
|
91
|
+
* 種別は**配下から継ぐ**(親からではない)。新しい項目が並ぶのは配下の側であり、
|
|
92
|
+
* 番号付きの中に印付きが 1 つ混ざるようなことにならない。
|
|
93
|
+
*
|
|
94
|
+
* **畳んでいる項目では働かない。** 見えない場所に項目を作るとカーソルを見失うので、
|
|
95
|
+
* その場合は従来どおり兄弟が増える。
|
|
96
|
+
*/
|
|
97
|
+
function splitIntoFirstChild(state, dispatch) {
|
|
98
|
+
const $cursor = state.selection instanceof TextSelection ? state.selection.$cursor : null;
|
|
99
|
+
if (!$cursor || $cursor.parentOffset !== $cursor.parent.content.size)
|
|
100
|
+
return false;
|
|
101
|
+
const depth = $cursor.depth - 1;
|
|
102
|
+
if (depth < 1)
|
|
103
|
+
return false;
|
|
104
|
+
const item = $cursor.node(depth);
|
|
105
|
+
if (!isListNode(item) || item.attrs.collapsed)
|
|
106
|
+
return false;
|
|
107
|
+
// 本文(最初の子)の末尾にいて、その次が入れ子の項目であること
|
|
108
|
+
if ($cursor.index(depth) !== 0 || item.childCount < 2)
|
|
109
|
+
return false;
|
|
110
|
+
const child = item.child(1);
|
|
111
|
+
if (!isListNode(child))
|
|
112
|
+
return false;
|
|
113
|
+
const created = child.type.createAndFill({ ...child.attrs, checked: false, collapsed: false });
|
|
114
|
+
if (!created)
|
|
115
|
+
return false;
|
|
116
|
+
if (!dispatch)
|
|
117
|
+
return true;
|
|
118
|
+
const at = $cursor.after($cursor.depth);
|
|
119
|
+
const tr = state.tr;
|
|
120
|
+
tr.insert(at, created);
|
|
121
|
+
setCursor(tr, at + 2);
|
|
122
|
+
dispatch(tr.scrollIntoView());
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 項目の本文の先頭での `Backspace`。
|
|
127
|
+
*
|
|
128
|
+
* 吸収先があれば吸収し、無ければ**何も書き込まずに**従来の連鎖へ譲る。
|
|
129
|
+
* 格上げは自前で書かず、譲った先の `joinListUp` がそのまま担う。
|
|
130
|
+
*
|
|
131
|
+
* 吸収は「取り出す → 本文を結合する → 差し戻す」の 3 手で、**折りたたみによる分岐を持たない**
|
|
132
|
+
*。3 手を 1 つの `Transaction` に収めるので、取り消しは 1 回で戻る
|
|
133
|
+
*。
|
|
134
|
+
*/
|
|
135
|
+
function listBackspace(state, dispatch, view) {
|
|
136
|
+
const target = absorbTargetAt(state);
|
|
137
|
+
if (!target)
|
|
138
|
+
return removeEmptyItem(state, dispatch) || backspaceCommand(state, dispatch, view);
|
|
139
|
+
if (!dispatch)
|
|
140
|
+
return true;
|
|
141
|
+
const tr = state.tr;
|
|
142
|
+
const children = detachChildren(tr, target);
|
|
143
|
+
// 結合には**取り出しを済ませた状態**を渡す。取り出し前の状態を渡すと、組み立てられた
|
|
144
|
+
// 書き換えが取り出し前の位置を指し、積んだ時点で位置が壊れる
|
|
145
|
+
const merged = captureTransaction((dispatchTo) => joinTextblockBackward(state.apply(tr), dispatchTo));
|
|
146
|
+
if (!merged)
|
|
147
|
+
return false;
|
|
148
|
+
// 結合は自分で 1 つのセクションを作るため、書き換えだけを取り出して自前のセクションへ積む
|
|
149
|
+
for (const step of merged.steps)
|
|
150
|
+
tr.step(step);
|
|
151
|
+
// カーソルの指定は書き換えに含まれない。継ぎ目へ置く指定が
|
|
152
|
+
// 落ちないよう、差し戻しで動いたぶんを写してから置き直す
|
|
153
|
+
const beforeAttach = tr.steps.length;
|
|
154
|
+
const seam = merged.selection.from;
|
|
155
|
+
if (children)
|
|
156
|
+
attachChildren(tr, target, children);
|
|
157
|
+
setCursor(tr, tr.mapping.slice(beforeAttach).map(seam));
|
|
158
|
+
dispatch(tr.scrollIntoView());
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 中身の無い項目を、その場から取り除く。
|
|
163
|
+
*
|
|
164
|
+
* 吸収先があるときは吸収がこの形を作るので、ここが働くのは**吸収先が無いとき**だけである。
|
|
165
|
+
* 従来はここで格上げが起き、書き手が消したはずの項目が空の段落として残っていた。空の項目は
|
|
166
|
+
* 「まだ書いていない項目」であって段落にしたいものではない、という判断。
|
|
167
|
+
*
|
|
168
|
+
* 配下は消さずに、項目のあった場所へそのまま残す(1 段浅くなる)。カーソルは直前の
|
|
169
|
+
* ブロックの末尾へ置く。**取り除くとコンテナの中身が無くなる場合は何もしない。** 文書は
|
|
170
|
+
* 1 つ以上のブロックを持たねばならず、その場合は従来どおり格上げが空の段落を残す。
|
|
171
|
+
*/
|
|
172
|
+
function removeEmptyItem(state, dispatch) {
|
|
173
|
+
const at = itemAtCursorStart(state);
|
|
174
|
+
if (!at || at.node.firstChild?.content.size !== 0)
|
|
175
|
+
return false;
|
|
176
|
+
const children = at.node.childCount - 1;
|
|
177
|
+
if (children === 0 && $parentOf(at).childCount === 1)
|
|
178
|
+
return false;
|
|
179
|
+
if (!dispatch)
|
|
180
|
+
return true;
|
|
181
|
+
const tr = state.tr;
|
|
182
|
+
tr.delete(at.pos, at.pos + at.node.nodeSize);
|
|
183
|
+
if (children > 0)
|
|
184
|
+
tr.insert(at.pos, at.node.content.cut(at.node.firstChild.nodeSize));
|
|
185
|
+
// 直前のブロックの末尾へ。後ろ向きに探すので、見出しでも段落でも表のセルでも同じに働く
|
|
186
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(at.pos), -1));
|
|
187
|
+
dispatch(tr.scrollIntoView());
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
/** 項目を収めているコンテナ(セクション、または外側の項目) */
|
|
191
|
+
function $parentOf(at) {
|
|
192
|
+
return at.$cursor.node(at.depth - 1);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 捨てる受け口でコマンドを走らせ、組み立てられた `Transaction` だけを受け取る。
|
|
196
|
+
* 実行はしないので、中身を自前のセクションへ積み直せる。
|
|
197
|
+
*/
|
|
198
|
+
function captureTransaction(run) {
|
|
199
|
+
let captured = null;
|
|
200
|
+
const applied = run((tr) => {
|
|
201
|
+
captured = tr;
|
|
202
|
+
});
|
|
203
|
+
return applied ? captured : null;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* カーソルが箇条書きの項目の**最初の**ブロックの先頭にあるとき、その項目。
|
|
207
|
+
*
|
|
208
|
+
* 4 つの条件を 1 か所に持つ。選択が空であること、テキストブロックの
|
|
209
|
+
* 先頭にいること(3.2)、項目の中にいること(3.4)、そのブロックが項目の最初の子で
|
|
210
|
+
* あること(3.3)。最後の 1 つは、項目の末尾のブロックを持ち上げるもう 1 つの経路に
|
|
211
|
+
* 触れないための線引きである。
|
|
212
|
+
*/
|
|
213
|
+
function itemAtCursorStart(state) {
|
|
214
|
+
const $cursor = state.selection instanceof TextSelection ? state.selection.$cursor : null;
|
|
215
|
+
if (!$cursor || $cursor.parentOffset > 0)
|
|
216
|
+
return null;
|
|
217
|
+
const depth = $cursor.depth - 1;
|
|
218
|
+
if (depth < 1 || !isListNode($cursor.node(depth)))
|
|
219
|
+
return null;
|
|
220
|
+
if ($cursor.index(depth) > 0)
|
|
221
|
+
return null;
|
|
222
|
+
return { node: $cursor.node(depth), pos: $cursor.before(depth), depth, $cursor };
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* カーソル位置から吸収の対象を求める。無ければ null。文書は変えない。
|
|
226
|
+
*
|
|
227
|
+
* 吸収先と認めるのは「直前の兄弟の項目」と「親の項目の本文」の 2 つだけである
|
|
228
|
+
*。これ以外を真と判定すると、項目の文字が見出しへ結合される。
|
|
229
|
+
*
|
|
230
|
+
* **項目が空かどうかは見ない**。空の項目で起きることは、中身のある
|
|
231
|
+
* 項目で起きることの「結合する本文が空だった場合」でしかなく、規則は 1 つで足りる。
|
|
232
|
+
*/
|
|
233
|
+
export function absorbTargetAt(state) {
|
|
234
|
+
const at = itemAtCursorStart(state);
|
|
235
|
+
if (!at)
|
|
236
|
+
return null;
|
|
237
|
+
const { $cursor, depth } = at;
|
|
238
|
+
const parent = $cursor.node(depth - 1);
|
|
239
|
+
// 直前の兄弟をまず求める。**先頭の子はどちらの枝でも吸収しない。** `list` の内容モデルは
|
|
240
|
+
// `block+` であり、本文を持たず最初の子が項目である親を構成できる。この形で真を返すと、
|
|
241
|
+
// 結合先の探索がリストの外まで登り、見出しへ結合しうる
|
|
242
|
+
const index = $cursor.index(depth - 1);
|
|
243
|
+
const previous = index > 0 ? parent.child(index - 1) : null;
|
|
244
|
+
if (!previous)
|
|
245
|
+
return null;
|
|
246
|
+
const itemPos = at.pos;
|
|
247
|
+
if (isListNode(previous)) {
|
|
248
|
+
return { kind: 'previousItem', itemPos, previousItemPos: itemPos - previous.nodeSize };
|
|
249
|
+
}
|
|
250
|
+
if (isListNode(parent)) {
|
|
251
|
+
return { kind: 'parentContent', itemPos, parentPos: $cursor.before(depth - 1) };
|
|
252
|
+
}
|
|
253
|
+
// 見出し・セクション直下の段落など、リストの外にあるものが直前にある(1.4)
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 吸収される項目の配下(本文より後ろの子)を文書から取り出す。配下が無ければ null。
|
|
258
|
+
*
|
|
259
|
+
* 取り出しと差し戻しが 2 つに分かれているのは、**間に本文の結合が入る**ためである。
|
|
260
|
+
* 配下を文書に残したまま結合すると、配下が「文書順で直前のテキストブロック」になって
|
|
261
|
+
* そこへ文字がくっつくか、結合先の内側へ埋まる。取り出している間はどちらも起きない。
|
|
262
|
+
*/
|
|
263
|
+
export function detachChildren(tr, target) {
|
|
264
|
+
const item = tr.doc.nodeAt(target.itemPos);
|
|
265
|
+
if (!item || item.childCount < 2)
|
|
266
|
+
return null;
|
|
267
|
+
const contentSize = item.child(0).nodeSize;
|
|
268
|
+
tr.delete(target.itemPos + 1 + contentSize, target.itemPos + item.nodeSize - 1);
|
|
269
|
+
return item.content.cut(contentSize);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* 取り出しておいた配下を差し戻す。
|
|
273
|
+
*
|
|
274
|
+
* 差し戻し先は、直前の兄弟の項目があれば**その子の末尾**(元の深さのまま)、
|
|
275
|
+
* 無ければ**親の直下**(1 段浅くなる)。
|
|
276
|
+
*
|
|
277
|
+
* 相手の位置は `tr.mapping` を通して現在の文書へ写す。結合によって相手の大きさが
|
|
278
|
+
* 変わっているため、取り出し時点で求めた末尾をそのまま使うと別の場所へ入る。
|
|
279
|
+
*/
|
|
280
|
+
export function attachChildren(tr, target, children) {
|
|
281
|
+
const hostPos = tr.mapping.map(target.kind === 'previousItem' ? target.previousItemPos : target.parentPos);
|
|
282
|
+
const host = tr.doc.nodeAt(hostPos);
|
|
283
|
+
if (!host)
|
|
284
|
+
return;
|
|
285
|
+
// 親の直下へ戻すときに末尾を選ばないのは、項目の後ろに並んでいた兄弟を
|
|
286
|
+
// 飛び越してしまうためである。項目があった位置=入れ子が始まる位置へ戻す
|
|
287
|
+
const at = target.kind === 'previousItem' ? hostPos + host.nodeSize - 1 : childrenStart(host, hostPos);
|
|
288
|
+
tr.insert(at, children);
|
|
289
|
+
}
|
|
290
|
+
/** 項目の中で、入れ子の項目が始まる位置(本文の直後) */
|
|
291
|
+
function childrenStart(item, pos) {
|
|
292
|
+
let at = pos + 1;
|
|
293
|
+
for (let index = 0; index < item.childCount; index += 1) {
|
|
294
|
+
const child = item.child(index);
|
|
295
|
+
if (isListNode(child))
|
|
296
|
+
break;
|
|
297
|
+
at += child.nodeSize;
|
|
298
|
+
}
|
|
299
|
+
return at;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* 本文を持たない項目を解消した `Transaction`。補正が要らなければ null。
|
|
303
|
+
*
|
|
304
|
+
* `list` の内容モデルは `block+` であり、**本文を持たず最初の子が項目である項目**を
|
|
305
|
+
* 構成できてしまう。範囲選択での削除がこの形を作る(上位の項目だけを消したつもりが、
|
|
306
|
+
* 空のコンテナとして残る)。書き手にはコンテナが見えないため、配下だけが 1 段深い位置に
|
|
307
|
+
* 取り残されたように見える。
|
|
308
|
+
*
|
|
309
|
+
* コンテナを取り除き、配下をその位置へそのまま戻す。**配下は消さない**(消えるのは、
|
|
310
|
+
* 書き手が消した本文だけである)。深さだけが 1 段浅くなり、見た目と構造が一致する。
|
|
311
|
+
*
|
|
312
|
+
* 読み込みからこの形は来ない。マークダウンに書かれている項目は書き手が消したもので
|
|
313
|
+
* はないため、読む側は解消せずに空の 1 行を補う(→ パーサの `withOwnLine`)。
|
|
314
|
+
*/
|
|
315
|
+
export function correctItemDepth(state) {
|
|
316
|
+
const tr = state.tr;
|
|
317
|
+
// 1 つ解消すると外側の位置が変わるため、そのつど探し直す。
|
|
318
|
+
// 解消のたびに項目が 1 つ減るので必ず止まる
|
|
319
|
+
for (let at = contentlessItem(tr.doc); at !== null; at = contentlessItem(tr.doc)) {
|
|
320
|
+
const item = tr.doc.nodeAt(at);
|
|
321
|
+
if (!item)
|
|
322
|
+
break;
|
|
323
|
+
tr.replaceWith(at, at + item.nodeSize, item.content);
|
|
324
|
+
}
|
|
325
|
+
return tr.steps.length > 0 ? tr : null;
|
|
326
|
+
}
|
|
327
|
+
/** 本文を持たない項目(最初の子が項目である項目)の位置。無ければ null */
|
|
328
|
+
function contentlessItem(doc) {
|
|
329
|
+
let found = null;
|
|
330
|
+
doc.descendants((node, pos) => {
|
|
331
|
+
if (found !== null)
|
|
332
|
+
return false;
|
|
333
|
+
if (!isListNode(node) || !node.firstChild || !isListNode(node.firstChild))
|
|
334
|
+
return true;
|
|
335
|
+
found = pos;
|
|
336
|
+
return false;
|
|
337
|
+
});
|
|
338
|
+
return found;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* セクションの端にある項目を、隣のセクションへ送り出す。
|
|
342
|
+
*
|
|
343
|
+
* 同じセクションの中でもう動けなくなったときだけ働く。項目のまま移るので、
|
|
344
|
+
* 箇条書きの階層は変わらない。
|
|
345
|
+
*
|
|
346
|
+
* 「セクションの中で止まる」を選ばないのは、書き手から見て見出しと項目が
|
|
347
|
+
* ひと続きのアウトラインだからである。下へ押し続けたら下へ進んでほしい。
|
|
348
|
+
*
|
|
349
|
+
* 行き先は**文書順で隣にあるセクション**であり、同じ階層とは限らない。子セクションが
|
|
350
|
+
* あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
|
|
351
|
+
* ので、行った先から同じキーで必ず戻れる。
|
|
352
|
+
*/
|
|
353
|
+
export function moveItemAcrossSection(direction) {
|
|
354
|
+
const n = outlineSchema.nodes;
|
|
355
|
+
return (state, dispatch) => {
|
|
356
|
+
const origin = topLevelBlock(state);
|
|
357
|
+
if (!origin || origin.node.type !== n.list)
|
|
358
|
+
return false;
|
|
359
|
+
// まだ同じセクションの中で動けるなら、ここの出番ではない
|
|
360
|
+
const edge = direction === 'down' ? blockRunEnd(origin.container) - 1 : blockRunStart(origin.container);
|
|
361
|
+
if (origin.index !== edge)
|
|
362
|
+
return false;
|
|
363
|
+
const target = direction === 'down'
|
|
364
|
+
? nextSection(state.doc, origin.container, origin.containerPos)
|
|
365
|
+
: previousContainer(state.doc, origin.containerPos);
|
|
366
|
+
if (!target)
|
|
367
|
+
return false;
|
|
368
|
+
if (!dispatch)
|
|
369
|
+
return true;
|
|
370
|
+
// 下へ送るときは見出しの直後、上へ送るときは中身の末尾(子セクションの前)
|
|
371
|
+
const insertAt = direction === 'down'
|
|
372
|
+
? target.pos + 1 + target.node.child(0).nodeSize
|
|
373
|
+
: blockContentEnd(target.node, target.pos);
|
|
374
|
+
const tr = state.tr;
|
|
375
|
+
const offsetInItem = state.selection.from - origin.pos;
|
|
376
|
+
// 見出しを持たないセクションは中身が空だと成立しない。最後の 1 つならまるごと消す
|
|
377
|
+
if (origin.container.type === n.intro_section && origin.container.childCount === 1) {
|
|
378
|
+
tr.delete(origin.containerPos, origin.containerPos + origin.container.nodeSize);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
tr.delete(origin.pos, origin.pos + origin.node.nodeSize);
|
|
382
|
+
}
|
|
383
|
+
const at = tr.mapping.map(insertAt);
|
|
384
|
+
tr.insert(at, origin.node);
|
|
385
|
+
setCursor(tr, at + offsetInItem);
|
|
386
|
+
dispatch(tr.scrollIntoView());
|
|
387
|
+
return true;
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* 項目を配下ごと前後の兄弟と入れ替える。
|
|
392
|
+
*
|
|
393
|
+
* カーソルが項目の中にあるときだけ働く。段落や見出しの上では何もしないので、
|
|
394
|
+
* 見出しのセクションを動かす操作と同じキーに並べて使える。
|
|
395
|
+
*
|
|
396
|
+
* 動かす先に同じ階層の項目がある場合だけ実行する。ライブラリの実装は兄弟が
|
|
397
|
+
* いないと項目を親の外へ出してしまい、並べ替えのつもりの操作が昇格になる
|
|
398
|
+
*。
|
|
399
|
+
*/
|
|
400
|
+
function moveListItem(direction) {
|
|
401
|
+
const move = createMoveListCommand(direction);
|
|
402
|
+
return (state, dispatch, view) => {
|
|
403
|
+
if (!enclosingListItem(state))
|
|
404
|
+
return false;
|
|
405
|
+
const range = findListsRange(state.selection.$from, state.selection.$to);
|
|
406
|
+
if (!range)
|
|
407
|
+
return false;
|
|
408
|
+
// `endIndex` は範囲の終わりの次を指すため、下へ動かす相手はその位置にいる
|
|
409
|
+
const neighbor = direction === 'up' ? range.startIndex - 1 : range.endIndex;
|
|
410
|
+
const hasSibling = neighbor >= 0 &&
|
|
411
|
+
neighbor < range.parent.childCount &&
|
|
412
|
+
isListNode(range.parent.child(neighbor));
|
|
413
|
+
if (hasSibling)
|
|
414
|
+
return move(state, dispatch, view);
|
|
415
|
+
return moveIntoSiblingItem(direction, state, dispatch);
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* 親の端まで来た項目を、次に置ける項目の中へ入れる。
|
|
420
|
+
*
|
|
421
|
+
* 上下移動は「文書順で次(前)にある、同じ深さの置ける位置」へ進む。
|
|
422
|
+
* 同じ親の中に兄弟がいなければ、次に置けるのは**親と同じ深さの項目**の中である。
|
|
423
|
+
* 下へなら先頭、上へなら末尾。深さは変わらないため、昇格・降格は起きない
|
|
424
|
+
* 。
|
|
425
|
+
*
|
|
426
|
+
* **親の隣だけを見ない。** 親も端にいると(3 段目以降でよく起きる)そこで止まり、
|
|
427
|
+
* 文書順ではまだ先に置ける場所があるのに動かなくなる。行き先は外側をたどって探す。
|
|
428
|
+
* 探索は文書の並びをそのままたどるため、セクションをまたぐ先も同じ経路で見つかる。
|
|
429
|
+
*/
|
|
430
|
+
function moveIntoSiblingItem(direction, state, dispatch) {
|
|
431
|
+
const item = enclosingListItem(state);
|
|
432
|
+
if (!item)
|
|
433
|
+
return false;
|
|
434
|
+
const $item = state.doc.resolve(item.pos);
|
|
435
|
+
const parent = $item.parent;
|
|
436
|
+
// 親が項目でなければ、ここではなくセクションをまたぐ経路の担当
|
|
437
|
+
if (!isListNode(parent))
|
|
438
|
+
return false;
|
|
439
|
+
const edge = direction === 'down' ? parent.childCount - 1 : firstListIndex(parent);
|
|
440
|
+
if (edge < 0 || $item.index() !== edge)
|
|
441
|
+
return false;
|
|
442
|
+
// 行き先は親と同じ深さ。項目そのものの深さで探すと 1 段深いところへ入ってしまう
|
|
443
|
+
const host = findHost(state.doc, item, direction, enclosingItemCount($item) - 1);
|
|
444
|
+
if (!host)
|
|
445
|
+
return false;
|
|
446
|
+
if (!dispatch)
|
|
447
|
+
return true;
|
|
448
|
+
// 下へ入るときは先頭の子(本文の直後)、上へ入るときは末尾の子
|
|
449
|
+
const insertAt = direction === 'down'
|
|
450
|
+
? host.pos + 1 + host.node.child(0).nodeSize
|
|
451
|
+
: host.pos + host.node.nodeSize - 1;
|
|
452
|
+
const tr = state.tr;
|
|
453
|
+
const offsetInItem = state.selection.from - item.pos;
|
|
454
|
+
tr.delete(item.pos, item.pos + item.node.nodeSize);
|
|
455
|
+
const at = tr.mapping.map(insertAt);
|
|
456
|
+
tr.insert(at, item.node);
|
|
457
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(at + offsetInItem)));
|
|
458
|
+
dispatch(tr.scrollIntoView());
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* 文書順で次(前)にある、外側の項目の数が `depth` の項目。
|
|
463
|
+
*
|
|
464
|
+
* **自分を含む項目は選ばない。** 親も同じ深さにあり、そのまま選ぶと自分の中へ
|
|
465
|
+
* 自分を入れることになる。丸ごと前(後ろ)にあるものだけを行き先とする。
|
|
466
|
+
*/
|
|
467
|
+
function findHost(doc, item, direction, depth) {
|
|
468
|
+
const itemEnd = item.pos + item.node.nodeSize;
|
|
469
|
+
let found = null;
|
|
470
|
+
const visit = (node, pos) => {
|
|
471
|
+
if (found && direction === 'down')
|
|
472
|
+
return false;
|
|
473
|
+
if (!isListNode(node))
|
|
474
|
+
return true;
|
|
475
|
+
// 自分をまたいでいる項目(=先祖)は行き先にならない。中はまだ見る
|
|
476
|
+
if (pos < itemEnd && pos + node.nodeSize > item.pos)
|
|
477
|
+
return true;
|
|
478
|
+
const enclosing = enclosingItemCount(doc.resolve(pos));
|
|
479
|
+
if (enclosing === depth) {
|
|
480
|
+
found = { node, pos };
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
// 行き先より深い枝に用はない
|
|
484
|
+
return enclosing < depth;
|
|
485
|
+
};
|
|
486
|
+
// 上へは、直前にあるものが行き先。最後に見つけたものが最も近い
|
|
487
|
+
if (direction === 'down')
|
|
488
|
+
doc.nodesBetween(itemEnd, doc.content.size, visit);
|
|
489
|
+
else
|
|
490
|
+
doc.nodesBetween(0, item.pos, visit);
|
|
491
|
+
return found;
|
|
492
|
+
}
|
|
493
|
+
/** その位置より外側にある項目の数(=項目の深さから 1 を引いた数) */
|
|
494
|
+
function enclosingItemCount($pos) {
|
|
495
|
+
let count = 0;
|
|
496
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
497
|
+
if (isListNode($pos.node(depth)))
|
|
498
|
+
count += 1;
|
|
499
|
+
}
|
|
500
|
+
return count;
|
|
501
|
+
}
|
|
502
|
+
/** 項目の中で、入れ子の項目が始まるインデックス(本文の次) */
|
|
503
|
+
function firstListIndex(item) {
|
|
504
|
+
for (let index = 0; index < item.childCount; index += 1) {
|
|
505
|
+
if (isListNode(item.child(index)))
|
|
506
|
+
return index;
|
|
507
|
+
}
|
|
508
|
+
return -1;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* 項目を 1 段深くする。
|
|
512
|
+
*
|
|
513
|
+
* 直前に同じ階層の項目がある場合だけ実行する。ライブラリの実装は直前に兄弟がなくても
|
|
514
|
+
* 項目を包み直して深くするため、そのままでは「直前の項目より 2 段以上深い階層」が
|
|
515
|
+
* 生まれてしまう。
|
|
516
|
+
*/
|
|
517
|
+
function indentList(state, dispatch) {
|
|
518
|
+
const range = selectedListsRange(state);
|
|
519
|
+
if (!range || range.startIndex === 0)
|
|
520
|
+
return false;
|
|
521
|
+
if (!isListNode(range.parent.child(range.startIndex - 1)))
|
|
522
|
+
return false;
|
|
523
|
+
return createIndentListCommand(spanOf(range))(state, dispatch);
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* 項目を 1 段浅くする。最上位の項目では箇条書きを解除して段落に戻す。
|
|
527
|
+
*
|
|
528
|
+
* **持ち上げるのではなく移す。** 対象を元の親の配下すべての後ろへ出す。持ち上げる変換は
|
|
529
|
+
* 文書の並び順を変えないため、対象の後ろに並んでいた兄弟が対象の配下へ取り込まれてしまう。
|
|
530
|
+
* 書き手が触っていない項目の親子関係が黙って変わることになる。移り終えた時点で深さは
|
|
531
|
+
* 1 段浅くなっているので、包み直しは要らない。
|
|
532
|
+
*
|
|
533
|
+
* 分岐は**親が項目かどうか**の 1 つだけである。折りたたみ・種別・空かどうかを
|
|
534
|
+
* 分岐条件に持ち込まない。
|
|
535
|
+
*/
|
|
536
|
+
function dedentList(state, dispatch) {
|
|
537
|
+
const range = selectedListsRange(state);
|
|
538
|
+
if (!range)
|
|
539
|
+
return false;
|
|
540
|
+
if (!dispatch)
|
|
541
|
+
return true;
|
|
542
|
+
const nested = isListNode(range.parent);
|
|
543
|
+
const items = [];
|
|
544
|
+
for (let index = range.startIndex; index < range.endIndex; index += 1) {
|
|
545
|
+
items.push(range.parent.child(index));
|
|
546
|
+
}
|
|
547
|
+
const tr = state.tr;
|
|
548
|
+
const at = moveContent(tr, range.start, range.end, nested ? afterParent(range) : afterRun(range));
|
|
549
|
+
if (!nested)
|
|
550
|
+
unwrapItems(tr, at, items);
|
|
551
|
+
// 覆った範囲の先頭からの隔たりを、移り先の同じ隔たりへ置き直す。覆っていない側まで
|
|
552
|
+
// 伸びた選択は、移したブロックの端で止める
|
|
553
|
+
const movedPos = (pos) => {
|
|
554
|
+
const offset = Math.min(Math.max(pos - range.start, 1), range.end - range.start - 1);
|
|
555
|
+
return at + (nested ? offset : unwrappedOffset(items, offset));
|
|
556
|
+
};
|
|
557
|
+
tr.setSelection(TextSelection.between(tr.doc.resolve(movedPos(state.selection.from)), tr.doc.resolve(movedPos(state.selection.to))));
|
|
558
|
+
dispatch(tr.scrollIntoView());
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
/** 入れ子の項目の行き先。親のノードの直後=元の親の配下すべての後ろ */
|
|
562
|
+
function afterParent(range) {
|
|
563
|
+
return range.$from.after(range.depth);
|
|
564
|
+
}
|
|
565
|
+
/** 最上位の項目の行き先。連続する項目の並びの後ろ(→ `listRunEnd`) */
|
|
566
|
+
function afterRun(range) {
|
|
567
|
+
const runEnd = listRunEnd(range.parent, range.endIndex);
|
|
568
|
+
let at = range.end;
|
|
569
|
+
for (let index = range.endIndex; index < runEnd; index += 1) {
|
|
570
|
+
at += range.parent.child(index).nodeSize;
|
|
571
|
+
}
|
|
572
|
+
return at;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* 移した項目を解いて段落へ戻す。配下は 1 段浅い項目として、その段落の後ろへ並ぶ。
|
|
576
|
+
*
|
|
577
|
+
* **後ろの項目から順に解く。** 前から解くと、後ろの項目の位置が解いた分だけずれる。
|
|
578
|
+
*/
|
|
579
|
+
function unwrapItems(tr, at, items) {
|
|
580
|
+
const positions = [];
|
|
581
|
+
let pos = at;
|
|
582
|
+
for (const item of items) {
|
|
583
|
+
positions.push(pos);
|
|
584
|
+
pos += item.nodeSize;
|
|
585
|
+
}
|
|
586
|
+
for (let index = items.length - 1; index >= 0; index -= 1) {
|
|
587
|
+
const item = items[index];
|
|
588
|
+
tr.replaceWith(positions[index], positions[index] + item.nodeSize, item.content);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* 解いたあとの隔たり。項目を解くと入れ口と出口が消えるため、そのままの隔たりを使うと
|
|
593
|
+
* カーソルが後ろへずれる。カーソルの入っている項目までに消えた分だけ詰める。
|
|
594
|
+
*/
|
|
595
|
+
function unwrappedOffset(items, offset) {
|
|
596
|
+
let unwrapped = 0;
|
|
597
|
+
let rest = offset;
|
|
598
|
+
for (const item of items) {
|
|
599
|
+
if (rest < item.nodeSize)
|
|
600
|
+
return unwrapped + Math.max(rest - 1, 0);
|
|
601
|
+
unwrapped += item.content.size;
|
|
602
|
+
rest -= item.nodeSize;
|
|
603
|
+
}
|
|
604
|
+
return unwrapped + rest;
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* `from` 以降で、連続する項目が終わるインデックス。
|
|
608
|
+
*
|
|
609
|
+
* **セクションの中身の末尾ではない。** 間に段落や表が挟まっていれば、そこで止まる。飛び越えると、
|
|
610
|
+
* 書き手が見ている「このリスト」の外へ出てしまう。既存の `blockRunEnd` はブロック全体の
|
|
611
|
+
* 連なりを返すもので種別を見ないため流用できない。
|
|
612
|
+
*/
|
|
613
|
+
export function listRunEnd(parent, from) {
|
|
614
|
+
let index = from;
|
|
615
|
+
while (index < parent.childCount && isListNode(parent.child(index)))
|
|
616
|
+
index += 1;
|
|
617
|
+
return index;
|
|
618
|
+
}
|
|
619
|
+
/** 選択が覆っている項目の範囲。覆ったかどうかの決め方は見出しと共通(→ `coveredEnd`) */
|
|
620
|
+
function selectedListsRange(state) {
|
|
621
|
+
const { $from } = state.selection;
|
|
622
|
+
return findListsRange($from, state.doc.resolve(coveredEnd(state)));
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* 対象の項目を(子項目まで含めて)端から端まで覆う範囲。
|
|
626
|
+
*
|
|
627
|
+
* カーソルだけを渡すと、ライブラリは項目の途中で範囲が切れたものとして扱い、
|
|
628
|
+
* 子項目を置き去りにして親だけを動かす。子項目を追随させるため、
|
|
629
|
+
* 項目全体を覆う範囲を明示的に渡す。
|
|
630
|
+
*/
|
|
631
|
+
function spanOf(range) {
|
|
632
|
+
return { from: range.start + 1, to: range.end - 1 };
|
|
633
|
+
}
|
|
634
|
+
/** カーソルが属するチェック項目の完了状態を反転する */
|
|
635
|
+
function toggleTaskChecked(state, dispatch) {
|
|
636
|
+
const item = enclosingListItem(state);
|
|
637
|
+
if (!item || item.node.attrs.kind !== 'task')
|
|
638
|
+
return false;
|
|
639
|
+
if (!dispatch)
|
|
640
|
+
return true;
|
|
641
|
+
dispatch(state.tr.setNodeAttribute(item.pos, 'checked', !item.node.attrs.checked));
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
644
|
+
function enclosingListItem(state) {
|
|
645
|
+
const $from = state.selection.$from;
|
|
646
|
+
for (let depth = $from.depth; depth > 0; depth -= 1) {
|
|
647
|
+
const node = $from.node(depth);
|
|
648
|
+
if (isListNode(node))
|
|
649
|
+
return { node, pos: $from.before(depth) };
|
|
650
|
+
}
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EditorCommand } from '../operations.types';
|
|
2
|
+
/**
|
|
3
|
+
* 行頭からの ` ```mermaid ` に続く改行で、Mermaid ブロックを作る。
|
|
4
|
+
*
|
|
5
|
+
* 空白での変換は autoformat の InputRule が受け持つが、InputRule は文字入力でのみ
|
|
6
|
+
* 発火し、Enter では発火しない。改行の側だけをこの命令が受け持つ。
|
|
7
|
+
* カーソルより後ろの文字は、変換後のブロックのソースとして残る。
|
|
8
|
+
*/
|
|
9
|
+
export declare const mermaidBlockOnEnter: EditorCommand;
|