@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.
Files changed (153) hide show
  1. package/README.ja.md +190 -0
  2. package/README.md +190 -0
  3. package/dist/assets/styles/editor.css +1459 -0
  4. package/dist/assets/styles/theme.css +486 -0
  5. package/dist/components/ContextMenu.svelte +279 -0
  6. package/dist/components/ContextMenu.svelte.d.ts +9 -0
  7. package/dist/components/EditorSurface.svelte +99 -0
  8. package/dist/components/EditorSurface.svelte.d.ts +16 -0
  9. package/dist/components/FocusBreadcrumb.svelte +138 -0
  10. package/dist/components/FocusBreadcrumb.svelte.d.ts +16 -0
  11. package/dist/components/MarkdownEditor.svelte +393 -0
  12. package/dist/components/MarkdownEditor.svelte.d.ts +119 -0
  13. package/dist/components/ShortcutHelpDialog.svelte +155 -0
  14. package/dist/components/ShortcutHelpDialog.svelte.d.ts +12 -0
  15. package/dist/components/internal/Dialog.svelte +157 -0
  16. package/dist/components/internal/Dialog.svelte.d.ts +23 -0
  17. package/dist/components/internal/Modal.svelte +181 -0
  18. package/dist/components/internal/Modal.svelte.d.ts +18 -0
  19. package/dist/components/internal/Popup.svelte +248 -0
  20. package/dist/components/internal/Popup.svelte.d.ts +26 -0
  21. package/dist/index.d.ts +32 -0
  22. package/dist/index.js +28 -0
  23. package/dist/markdown.d.ts +13 -0
  24. package/dist/markdown.js +10 -0
  25. package/dist/messages/en.d.ts +2 -0
  26. package/dist/messages/en.js +107 -0
  27. package/dist/messages/index.d.ts +13 -0
  28. package/dist/messages/index.js +31 -0
  29. package/dist/messages/ja.d.ts +2 -0
  30. package/dist/messages/ja.js +112 -0
  31. package/dist/messages/messages.types.d.ts +59 -0
  32. package/dist/messages/messages.types.js +1 -0
  33. package/dist/models/document/bodySelection.d.ts +32 -0
  34. package/dist/models/document/bodySelection.js +43 -0
  35. package/dist/models/document/document.types.d.ts +27 -0
  36. package/dist/models/document/document.types.js +8 -0
  37. package/dist/models/document/foldable.d.ts +18 -0
  38. package/dist/models/document/foldable.js +21 -0
  39. package/dist/models/document/schema.d.ts +2 -0
  40. package/dist/models/document/schema.js +428 -0
  41. package/dist/models/document/sectionTree.d.ts +15 -0
  42. package/dist/models/document/sectionTree.js +123 -0
  43. package/dist/models/markdown/blockOutline.d.ts +9 -0
  44. package/dist/models/markdown/blockOutline.js +82 -0
  45. package/dist/models/markdown/frontMatter.d.ts +98 -0
  46. package/dist/models/markdown/frontMatter.js +300 -0
  47. package/dist/models/markdown/markdown.types.d.ts +60 -0
  48. package/dist/models/markdown/markdown.types.js +1 -0
  49. package/dist/models/markdown/markdownIt.d.ts +1 -0
  50. package/dist/models/markdown/markdownIt.js +92 -0
  51. package/dist/models/markdown/parser.d.ts +4 -0
  52. package/dist/models/markdown/parser.js +505 -0
  53. package/dist/models/markdown/plainText.d.ts +1 -0
  54. package/dist/models/markdown/plainText.js +25 -0
  55. package/dist/models/markdown/serializer.d.ts +17 -0
  56. package/dist/models/markdown/serializer.js +453 -0
  57. package/dist/models/viewState/viewState.types.d.ts +38 -0
  58. package/dist/models/viewState/viewState.types.js +17 -0
  59. package/dist/models/viewState/viewStateCodec.d.ts +31 -0
  60. package/dist/models/viewState/viewStateCodec.js +173 -0
  61. package/dist/operations/autoformat.d.ts +9 -0
  62. package/dist/operations/autoformat.js +86 -0
  63. package/dist/operations/commands/block.d.ts +24 -0
  64. package/dist/operations/commands/block.js +139 -0
  65. package/dist/operations/commands/codeBlock.d.ts +7 -0
  66. package/dist/operations/commands/codeBlock.js +26 -0
  67. package/dist/operations/commands/element.d.ts +67 -0
  68. package/dist/operations/commands/element.js +269 -0
  69. package/dist/operations/commands/frontMatter.d.ts +35 -0
  70. package/dist/operations/commands/frontMatter.js +120 -0
  71. package/dist/operations/commands/lineBreak.d.ts +9 -0
  72. package/dist/operations/commands/lineBreak.js +16 -0
  73. package/dist/operations/commands/link.d.ts +7 -0
  74. package/dist/operations/commands/link.js +21 -0
  75. package/dist/operations/commands/list.d.ts +126 -0
  76. package/dist/operations/commands/list.js +652 -0
  77. package/dist/operations/commands/mermaidBlock.d.ts +9 -0
  78. package/dist/operations/commands/mermaidBlock.js +28 -0
  79. package/dist/operations/commands/section.d.ts +217 -0
  80. package/dist/operations/commands/section.js +865 -0
  81. package/dist/operations/commands/sectionFold.d.ts +36 -0
  82. package/dist/operations/commands/sectionFold.js +219 -0
  83. package/dist/operations/commands/sectionJump.d.ts +8 -0
  84. package/dist/operations/commands/sectionJump.js +144 -0
  85. package/dist/operations/commands/selectedBlock.d.ts +37 -0
  86. package/dist/operations/commands/selectedBlock.js +170 -0
  87. package/dist/operations/commands/table.d.ts +64 -0
  88. package/dist/operations/commands/table.js +437 -0
  89. package/dist/operations/keymap/assemble.d.ts +22 -0
  90. package/dist/operations/keymap/assemble.js +165 -0
  91. package/dist/operations/keymap/coreActions.d.ts +2 -0
  92. package/dist/operations/keymap/coreActions.js +711 -0
  93. package/dist/operations/keymap/menuItems.d.ts +52 -0
  94. package/dist/operations/keymap/menuItems.js +114 -0
  95. package/dist/operations/keymap/shortcutKeys.d.ts +42 -0
  96. package/dist/operations/keymap/shortcutKeys.js +114 -0
  97. package/dist/operations/keymap/viewActions.d.ts +30 -0
  98. package/dist/operations/keymap/viewActions.js +182 -0
  99. package/dist/operations/operations.types.d.ts +121 -0
  100. package/dist/operations/operations.types.js +1 -0
  101. package/dist/operations/plugins/blockHandle.d.ts +25 -0
  102. package/dist/operations/plugins/blockHandle.js +112 -0
  103. package/dist/operations/plugins/contextMenu.d.ts +33 -0
  104. package/dist/operations/plugins/contextMenu.js +204 -0
  105. package/dist/operations/plugins/focus.d.ts +39 -0
  106. package/dist/operations/plugins/focus.js +350 -0
  107. package/dist/operations/plugins/foldClick.d.ts +11 -0
  108. package/dist/operations/plugins/foldClick.js +117 -0
  109. package/dist/operations/plugins/foldGuard.d.ts +15 -0
  110. package/dist/operations/plugins/foldGuard.js +31 -0
  111. package/dist/operations/plugins/frontMatterGuard.d.ts +21 -0
  112. package/dist/operations/plugins/frontMatterGuard.js +150 -0
  113. package/dist/operations/plugins/frontMatterView.d.ts +34 -0
  114. package/dist/operations/plugins/frontMatterView.js +137 -0
  115. package/dist/operations/plugins/linkPaste.d.ts +3 -0
  116. package/dist/operations/plugins/linkPaste.js +20 -0
  117. package/dist/operations/plugins/listStructure.d.ts +13 -0
  118. package/dist/operations/plugins/listStructure.js +18 -0
  119. package/dist/operations/plugins/markdownClipboard.d.ts +8 -0
  120. package/dist/operations/plugins/markdownClipboard.js +208 -0
  121. package/dist/operations/plugins/mermaidView.d.ts +20 -0
  122. package/dist/operations/plugins/mermaidView.js +206 -0
  123. package/dist/operations/plugins/requiredSelectionEntry.d.ts +10 -0
  124. package/dist/operations/plugins/requiredSelectionEntry.js +124 -0
  125. package/dist/operations/plugins/sectionView.d.ts +12 -0
  126. package/dist/operations/plugins/sectionView.js +44 -0
  127. package/dist/operations/plugins/tabEscape.d.ts +21 -0
  128. package/dist/operations/plugins/tabEscape.js +41 -0
  129. package/dist/operations/plugins/tableHandles.d.ts +24 -0
  130. package/dist/operations/plugins/tableHandles.js +89 -0
  131. package/dist/operations/transaction.d.ts +26 -0
  132. package/dist/operations/transaction.js +51 -0
  133. package/dist/operations/transactionOrigin.d.ts +17 -0
  134. package/dist/operations/transactionOrigin.js +9 -0
  135. package/dist/states/contextMenuController.svelte.d.ts +57 -0
  136. package/dist/states/contextMenuController.svelte.js +206 -0
  137. package/dist/states/editorSession.d.ts +12 -0
  138. package/dist/states/editorSession.js +268 -0
  139. package/dist/states/editorSession.types.d.ts +57 -0
  140. package/dist/states/editorSession.types.js +1 -0
  141. package/dist/states/outlineViewController.svelte.d.ts +52 -0
  142. package/dist/states/outlineViewController.svelte.js +134 -0
  143. package/dist/utils/dateText.d.ts +25 -0
  144. package/dist/utils/dateText.js +71 -0
  145. package/dist/utils/geometry.types.d.ts +13 -0
  146. package/dist/utils/geometry.types.js +1 -0
  147. package/dist/utils/json.types.d.ts +4 -0
  148. package/dist/utils/json.types.js +1 -0
  149. package/dist/utils/platform.d.ts +8 -0
  150. package/dist/utils/platform.js +10 -0
  151. package/dist/utils/result.types.d.ts +7 -0
  152. package/dist/utils/result.types.js +1 -0
  153. package/package.json +109 -0
@@ -0,0 +1,269 @@
1
+ import { chainCommands } from 'prosemirror-commands';
2
+ import { isInTable } from 'prosemirror-tables';
3
+ import { outlineSchema } from '../../models/document/schema';
4
+ import { selectBlockAt, setCursor } from '../transaction';
5
+ import { isTabEscaped } from '../plugins/tabEscape';
6
+ import { setBlockKind, toggleHeading } from './block';
7
+ import { indentInCodeBlock, outdentInCodeBlock } from './codeBlock';
8
+ import { listCommands, moveItemAcrossSection, paragraphToListItem } from './list';
9
+ import { liftSection, moveSection, sinkSection, targetSection, topLevelBlock } from './section';
10
+ import { liftSelectedBlock, requiredSelectionBlock, sinkSelectedBlock } from './selectedBlock';
11
+ import { tableCommands } from './table';
12
+ const n = outlineSchema.nodes;
13
+ export const sectionCommands = {
14
+ sinkSection,
15
+ liftSection,
16
+ moveSection,
17
+ targetElement,
18
+ sinkElement,
19
+ liftElement,
20
+ moveElement,
21
+ duplicateElement,
22
+ deleteElement,
23
+ setBlockKind,
24
+ toggleHeading,
25
+ moveItemAcrossSection
26
+ };
27
+ /**
28
+ * いま操作の対象になる要素を、カーソルの位置で決める。
29
+ *
30
+ * **移動・複製・削除・階層操作は、対象がどのブロックであっても同じ規則で働く。**
31
+ * 分かれるのは対象の決め方だけであり、その決め方はこの関数 1 つにある。以前は
32
+ * 複製・削除だけがここを通り、移動と階層の深浅は割り当ての側でそれぞれ独自に文脈を
33
+ * 見ていた。同じ関心事が離れた場所に 2 つある状態だった。
34
+ *
35
+ * セクションを対象にするには、その見出しにカーソルを置く。中身のどこにいても丸ごと
36
+ * 消える状態は、取り違えたときの損失が大きい(Decision 18)。
37
+ */
38
+ function targetElement(state) {
39
+ // **指している状態を最初に見る。** 逆にすると、要選択ブロックを指していても
40
+ // カーソルの位置から別の対象が選ばれる
41
+ const pointed = requiredSelectionBlock.selectedBlock(state);
42
+ if (pointed)
43
+ return { kind: 'selectedBlock', ...pointed };
44
+ const section = targetSection(state);
45
+ if (section)
46
+ return { kind: 'section', node: section.node, pos: section.pos };
47
+ // **項目を対象にするのは、その本文にカーソルがあるときだけ。** 2 つ目以降の
48
+ // ブロックではそのブロックを対象にする。見出しに置いたときだけセクションを対象に
49
+ // するのと同じ線引きで、中身のどこにいても丸ごと動く・消える状態を作らない
50
+ const inItem = blockInsideItem(state);
51
+ if (inItem)
52
+ return { kind: 'block', ...inItem };
53
+ const item = listCommands.enclosingListItem(state);
54
+ if (item)
55
+ return { kind: 'listItem', ...item };
56
+ const block = topLevelBlock(state);
57
+ return block ? { kind: 'block', node: block.node, pos: block.pos } : null;
58
+ }
59
+ /**
60
+ * カーソルが項目の**本文より後ろ**のブロックにあるとき、そのブロック。
61
+ * 本文(最初のブロック)にいるときは null(対象は項目そのもの)。
62
+ */
63
+ function blockInsideItem(state) {
64
+ const $from = state.selection.$from;
65
+ for (let depth = $from.depth; depth > 0; depth -= 1) {
66
+ if ($from.node(depth).type !== outlineSchema.nodes.list)
67
+ continue;
68
+ if ($from.index(depth) === 0 || depth + 1 > $from.depth)
69
+ return null;
70
+ return { node: $from.node(depth + 1), pos: $from.before(depth + 1) };
71
+ }
72
+ return null;
73
+ }
74
+ /**
75
+ * 対象を 1 段深くする。
76
+ *
77
+ * 種別ごとの手当ては、**対象が決まったあとの実行の違い**でしかない。
78
+ * 「いまどこにカーソルがあるか」をここで見直さない。
79
+ */
80
+ function sinkElement(state, dispatch) {
81
+ const target = targetElement(state);
82
+ if (!target)
83
+ return false;
84
+ if (target.kind === 'selectedBlock')
85
+ return sinkSelectedBlock(target, state, dispatch);
86
+ if (target.kind === 'section')
87
+ return sinkSection(state, dispatch);
88
+ if (target.kind === 'listItem')
89
+ return listCommands.indentList(state, dispatch);
90
+ return false;
91
+ }
92
+ function liftElement(state, dispatch) {
93
+ const target = targetElement(state);
94
+ if (!target)
95
+ return false;
96
+ if (target.kind === 'selectedBlock')
97
+ return liftSelectedBlock(target, state, dispatch);
98
+ if (target.kind === 'section')
99
+ return liftSection(state, dispatch);
100
+ if (target.kind === 'listItem')
101
+ return listCommands.dedentList(state, dispatch);
102
+ return listCommands.liftBlockOutOfItem(target.pos, state, dispatch);
103
+ }
104
+ /**
105
+ * 対象を 1 つ前後の兄弟と入れ替える。深さは変えない。
106
+ *
107
+ * 項目は、同じセクションの中で動けなくなったら隣のセクションへ送り出す。項目のまま移るので
108
+ * 箇条書きの階層は変わらない。
109
+ */
110
+ function moveElement(direction) {
111
+ const moveItem = listCommands.moveListItem(direction);
112
+ const moveAcrossSection = moveItemAcrossSection(direction);
113
+ const move = moveSection(direction);
114
+ return (state, dispatch, view) => {
115
+ const target = targetElement(state);
116
+ if (!target)
117
+ return false;
118
+ if (target.kind === 'section')
119
+ return move(state, dispatch, view);
120
+ if (target.kind === 'listItem') {
121
+ return moveItem(state, dispatch, view) || moveAcrossSection(state, dispatch, view);
122
+ }
123
+ return moveBlock(direction, target, state, dispatch);
124
+ };
125
+ }
126
+ /**
127
+ * ブロックを 1 つ前後の兄弟と入れ替える。深さは変えない。
128
+ *
129
+ * **相手になれるのはブロックだけである。** 見出しはセクションの先頭にしか置けず、
130
+ * 子のセクションはブロックより後ろにしか置けない。どちらかに行き当たったら、
131
+ * そこがブロックの並びの端であり、文書もカーソルも動かさない(要件 5.2)。
132
+ */
133
+ function moveBlock(direction, target, state, dispatch) {
134
+ const $target = state.doc.resolve(target.pos);
135
+ const index = $target.index();
136
+ const neighborIndex = direction === 'up' ? index - 1 : index + 1;
137
+ if (neighborIndex < 0 || neighborIndex >= $target.parent.childCount)
138
+ return false;
139
+ const neighbor = $target.parent.child(neighborIndex);
140
+ if (neighbor.type === n.section || neighbor.type === n.heading)
141
+ return false;
142
+ // 項目の先頭は、その項目の本文である。見出しと同じく、その前へは出られない
143
+ if ($target.parent.type === n.list && neighborIndex === 0)
144
+ return false;
145
+ if (!dispatch)
146
+ return true;
147
+ const insertAt = direction === 'up' ? target.pos - neighbor.nodeSize : target.pos + neighbor.nodeSize;
148
+ const tr = state.tr;
149
+ const offsetInBlock = state.selection.from - target.pos;
150
+ tr.delete(target.pos, target.pos + target.node.nodeSize);
151
+ tr.insert(insertAt, target.node);
152
+ if (target.kind === 'selectedBlock')
153
+ selectBlockAt(tr, insertAt);
154
+ else
155
+ setCursor(tr, insertAt + offsetInBlock);
156
+ dispatch(tr.scrollIntoView());
157
+ return true;
158
+ }
159
+ /**
160
+ * 対象の複製を直後に作る。
161
+ *
162
+ * 指している状態から複製したときは、**複製したほうを指す**。続けて押せば下へ増え、
163
+ * そのまま階層操作すれば今作ったものが動く。元のほうを指し続けると、増やした直後の
164
+ * 操作が上のブロックに効いてしまう。
165
+ */
166
+ function duplicateElement(state, dispatch) {
167
+ const target = targetElement(state);
168
+ if (!target)
169
+ return false;
170
+ if (!dispatch)
171
+ return true;
172
+ const copyAt = target.pos + target.node.nodeSize;
173
+ const tr = state.tr;
174
+ tr.insert(copyAt, target.node);
175
+ if (target.kind === 'selectedBlock')
176
+ selectBlockAt(tr, copyAt);
177
+ dispatch(tr.scrollIntoView());
178
+ return true;
179
+ }
180
+ /**
181
+ * `Tab` は文脈によって意味が変わる唯一のキーであり、判定順序が仕様そのものになる。
182
+ *
183
+ * IME 変換中 → 退避状態 → 表のセル内 → コードブロックの中 → 対象への階層操作
184
+ * → 段落を項目にする → 何もしない
185
+ *
186
+ * **前半の 3 つは階層操作ではない。** `Tab` が別の意味を持つ場面であり、対象を決める
187
+ * 前の関心事として残す。対象が決まってからの分岐(セクションか項目か)は
188
+ * `targetElement` が持つ。
189
+ */
190
+ export const sinkByContext = (state, dispatch, view) => {
191
+ if (isTabEscaped(state))
192
+ return false; // ブラウザ既定のフォーカス移動を許す
193
+ // 表の中では次のセルへ。最後のセルなら行を足してから移る
194
+ if (isInTable(state))
195
+ return tableCommands.goToNextCell(state, dispatch, view);
196
+ return chainCommands(indentInCodeBlock, sinkElement, paragraphToListItem,
197
+ // それ以外では何もしない。ただしフォーカスは動かさない
198
+ () => true)(state, dispatch, view);
199
+ };
200
+ export const liftByContext = (state, dispatch, view) => {
201
+ if (isTabEscaped(state))
202
+ return false;
203
+ if (isInTable(state))
204
+ return tableCommands.goToPreviousCell(state, dispatch, view);
205
+ return chainCommands(outdentInCodeBlock, liftElement, () => true)(state, dispatch, view);
206
+ };
207
+ /**
208
+ * 上下移動は、対象の中で完結させる。
209
+ *
210
+ * 項目にいるなら項目だけを動かし、動かせなくても**より大きなセクションへ落ちない**。
211
+ * 落とすと、末尾の項目でもう一度押したときにセクションごと動いてしまい、
212
+ * 「移動先に兄弟がなければ何もしない」に反する。対象を 1 つに
213
+ * 決めてから動かすことで、この性質が対象の決め方から自動的に出る。
214
+ *
215
+ * 動かせなかったときも**キーは受け止める**。手放すとブラウザ既定の動き
216
+ * (macOS の `Cmd` と矢印は文書の先頭・末尾へ移動)が走り、動かないどころか
217
+ * カーソルが飛ぶ。「何も起きない」は、文書もカーソルも動かないことを指す。
218
+ */
219
+ export function moveByContext(direction) {
220
+ const move = moveElement(direction);
221
+ return (state, dispatch, view) => {
222
+ move(state, dispatch, view);
223
+ return true;
224
+ };
225
+ }
226
+ /**
227
+ * 表の中では必ずキーを受け止め、表の外では譲る。
228
+ *
229
+ * 実行できなかったときに `false` を返すと、同じキーを持つ**後段の割り当てへ処理が渡る**。
230
+ * 表の端で行を動かせなかったのにセクションごと動く、見出し行を消せなかったのに
231
+ * セクションごと消える、といったことが起きる。
232
+ *
233
+ * 表の外では譲るため、既存の意味は変わらない。
234
+ */
235
+ export function inTable(command) {
236
+ return (state, dispatch, view) => {
237
+ if (!isInTable(state))
238
+ return false;
239
+ command(state, dispatch, view);
240
+ return true;
241
+ };
242
+ }
243
+ /**
244
+ * カーソルがいる要素を消す。
245
+ *
246
+ * **「文書が空になるなら空の段落へ置き換える」特例は持たない。** 文書が常に 1 行以上を
247
+ * 持つという不変条件は `doc` の content 式が持ち、それを守る仕組みは編集基盤の内容補完が
248
+ * 持っている。コマンド側で手で書き直すと、同じ決まりが 2 か所に置かれる。
249
+ */
250
+ function deleteElement(state, dispatch) {
251
+ const target = targetElement(state);
252
+ if (!target)
253
+ return false;
254
+ if (!dispatch)
255
+ return true;
256
+ let from = target.pos;
257
+ let to = target.pos + target.node.nodeSize;
258
+ // 見出しを持たないセクションは中身が空だと成立しない。最後の 1 つならまるごと消す
259
+ const $target = state.doc.resolve(from);
260
+ if ($target.parent.type === n.intro_section && $target.parent.childCount === 1) {
261
+ from = $target.before();
262
+ to = from + $target.parent.nodeSize;
263
+ }
264
+ const tr = state.tr;
265
+ tr.delete(from, to);
266
+ setCursor(tr, Math.min(from + 1, tr.doc.content.size));
267
+ dispatch(tr.scrollIntoView());
268
+ return true;
269
+ }
@@ -0,0 +1,35 @@
1
+ import { type EditorState, type Transaction } from 'prosemirror-state';
2
+ import type { FrontMatterFields, FrontMatterWriteError, FrontMatterWriteOutcome } from '../../models/markdown/markdown.types';
3
+ import type { Result } from '../../utils/result.types';
4
+ /** 書き込みの結果。見送ったとき、および中身が変わらないときは変更を作らない */
5
+ export interface FrontMatterWrite {
6
+ readonly outcome: FrontMatterWriteOutcome;
7
+ readonly transaction: Transaction | null;
8
+ }
9
+ /**
10
+ * フロントマターの中身を差し替える変更を組み立てる。
11
+ *
12
+ * **記法を知らない。** 中身の組み立ても項目の範囲も純関数に委ね、ここは「現在の中身を
13
+ * 取り出し、範囲を文書上の位置へ写し、新しい中身を入れる」だけを行う。
14
+ *
15
+ * 順序に意味がある。**キーと値の検査を最初に置く**——検査は純粋で文書に触れないため、
16
+ * 「利用者が編集している最中は触らない」という理由がここに掛からない。逆順にすると、
17
+ * 呼び出し側の誤りが見送りに紛れて返らない(利用側は見送りを失敗として扱わないため、
18
+ * 誤った値を渡し続けても何も起きない)。
19
+ */
20
+ export declare function writeFrontMatterFields(state: EditorState, fields: FrontMatterFields): Result<FrontMatterWrite, FrontMatterWriteError>;
21
+ /**
22
+ * 本文の先頭での `Backspace` を、フロントマターへ繋げずに受け止める。
23
+ *
24
+ * フロントマターは**本文の直前にある「文字を置ける場所」**である。受け止めないと編集基盤の
25
+ * 結合が働き、本文の 1 行目がメタデータの中へ吸い込まれる。スキーマの `isolating`
26
+ * では防げない——結合は繋ぎ目の前後のノードを見るだけで、繋ぎ先が文字を置ける場所なら
27
+ * 印を見ずに繋ぐ。
28
+ *
29
+ * フロントマターは本文と繋がらないので、**繋げる相手がいない=何も起きない**が正しい。
30
+ * これはフロントマターを持つ前の文書の先頭と同じ結果である。
31
+ *
32
+ * 箇条書きの中では譲る。項目の先頭は箇条書きの側が受け持ち(段上げ)、その経路は
33
+ * フロントマターへ繋がらない。
34
+ */
35
+ export declare function holdBackspaceAtFrontMatter(state: EditorState): boolean;
@@ -0,0 +1,120 @@
1
+ import { TextSelection } from 'prosemirror-state';
2
+ import { bodyStart, frontMatterContentStart } from '../../models/document/bodySelection';
3
+ import { outlineSchema } from '../../models/document/schema';
4
+ import { findFieldRanges, writeFields } from '../../models/markdown/frontMatter';
5
+ import { markOrigin } from '../transactionOrigin';
6
+ import { listCommands } from './list';
7
+ /**
8
+ * フロントマターの中身を差し替える変更を組み立てる。
9
+ *
10
+ * **記法を知らない。** 中身の組み立ても項目の範囲も純関数に委ね、ここは「現在の中身を
11
+ * 取り出し、範囲を文書上の位置へ写し、新しい中身を入れる」だけを行う。
12
+ *
13
+ * 順序に意味がある。**キーと値の検査を最初に置く**——検査は純粋で文書に触れないため、
14
+ * 「利用者が編集している最中は触らない」という理由がここに掛からない。逆順にすると、
15
+ * 呼び出し側の誤りが見送りに紛れて返らない(利用側は見送りを失敗として扱わないため、
16
+ * 誤った値を渡し続けても何も起きない)。
17
+ */
18
+ export function writeFrontMatterFields(state, fields) {
19
+ const start = frontMatterContentStart(state.doc);
20
+ // スキーマが先頭のフロントマターを保証するため、ここは通らない
21
+ if (start === null)
22
+ return { ok: true, value: { outcome: 'skipped', transaction: null } };
23
+ const content = state.doc.child(0).textContent;
24
+ const written = writeFields(content, fields);
25
+ if (!written.ok)
26
+ return written;
27
+ if (touchesSelection(state, start, content, fields)) {
28
+ return { ok: true, value: { outcome: 'skipped', transaction: null } };
29
+ }
30
+ // 作って dispatch すると、文書が変わらなくても購読者が呼ばれる。作らなければ何も起きない
31
+ if (written.value === content) {
32
+ return { ok: true, value: { outcome: 'applied', transaction: null } };
33
+ }
34
+ return {
35
+ ok: true,
36
+ value: { outcome: 'applied', transaction: replace(state, start, content, written.value) }
37
+ };
38
+ }
39
+ /**
40
+ * 利用者のカーソルまたは選択範囲が、書き込む項目のいずれかに掛かっているか。
41
+ *
42
+ * **範囲の規則を自前で持たない。** 記法の側が返した文字数を文書上の位置へ写すだけである。
43
+ * まだ無い項目は中身の末尾へ足されるため、その位置も見る。これにより、領域そのものを
44
+ * 指す選択・文書全体の選択・領域の内外にまたがる選択は、いずれも掛かるものになる。
45
+ */
46
+ function touchesSelection(state, start, content, fields) {
47
+ const keys = Object.keys(fields);
48
+ const ranges = findFieldRanges(content, keys).map((range) => ({
49
+ from: start + range.start,
50
+ to: start + range.end
51
+ }));
52
+ if (ranges.length < keys.length) {
53
+ ranges.push({ from: start + content.length, to: start + content.length });
54
+ }
55
+ const { from, to } = state.selection;
56
+ return ranges.some((range) => from <= range.to && to >= range.from);
57
+ }
58
+ /**
59
+ * 変わった箇所だけを置き換える。
60
+ *
61
+ * 全置換にしないのは、**触っていない項目にカーソルを置いている利用者を守る**ためである
62
+ * (中身をまるごと入れ替えると、そのカーソルが項目の末尾へ飛ぶ)。
63
+ */
64
+ function replace(state, start, current, next) {
65
+ let head = 0;
66
+ while (head < current.length && head < next.length && current[head] === next[head])
67
+ head += 1;
68
+ let tail = 0;
69
+ while (tail < current.length - head &&
70
+ tail < next.length - head &&
71
+ current[current.length - 1 - tail] === next[next.length - 1 - tail]) {
72
+ tail += 1;
73
+ }
74
+ const from = start + head;
75
+ const to = start + current.length - tail;
76
+ const text = next.slice(head, next.length - tail);
77
+ const transaction = text
78
+ ? state.tr.replaceWith(from, to, outlineSchema.text(text))
79
+ : state.tr.delete(from, to);
80
+ // 印は必須である。フォーカス中は範囲外の文書変更が拒まれ、領域は常に範囲外に当たる
81
+ return markOrigin(transaction.setMeta('addToHistory', false), 'metadata');
82
+ }
83
+ /**
84
+ * 本文の先頭での `Backspace` を、フロントマターへ繋げずに受け止める。
85
+ *
86
+ * フロントマターは**本文の直前にある「文字を置ける場所」**である。受け止めないと編集基盤の
87
+ * 結合が働き、本文の 1 行目がメタデータの中へ吸い込まれる。スキーマの `isolating`
88
+ * では防げない——結合は繋ぎ目の前後のノードを見るだけで、繋ぎ先が文字を置ける場所なら
89
+ * 印を見ずに繋ぐ。
90
+ *
91
+ * フロントマターは本文と繋がらないので、**繋げる相手がいない=何も起きない**が正しい。
92
+ * これはフロントマターを持つ前の文書の先頭と同じ結果である。
93
+ *
94
+ * 箇条書きの中では譲る。項目の先頭は箇条書きの側が受け持ち(段上げ)、その経路は
95
+ * フロントマターへ繋がらない。
96
+ */
97
+ export function holdBackspaceAtFrontMatter(state) {
98
+ const $cursor = state.selection instanceof TextSelection ? state.selection.$cursor : null;
99
+ if (!$cursor || $cursor.parentOffset !== 0)
100
+ return false;
101
+ if (listCommands.isInList(state))
102
+ return false;
103
+ return joinsIntoFrontMatter($cursor, bodyStart(state.doc));
104
+ }
105
+ /**
106
+ * カーソルの手前にある繋ぎ目が、フロントマターとの境目か。
107
+ *
108
+ * 繋ぎ目は**先頭でなくなる最初の高さ**にある。そこが文書の直下で、位置が本文の始まりと
109
+ * 一致するなら、手前にあるのはフロントマターだけである。
110
+ */
111
+ function joinsIntoFrontMatter($cursor, start) {
112
+ if (!$cursor)
113
+ return false;
114
+ for (let depth = $cursor.depth - 1; depth >= 0; depth -= 1) {
115
+ if ($cursor.index(depth) === 0)
116
+ continue;
117
+ return depth === 0 && $cursor.before(depth + 1) === start;
118
+ }
119
+ return false;
120
+ }
@@ -0,0 +1,9 @@
1
+ import type { EditorCommand } from '../operations.types';
2
+ /**
3
+ * 段落を分けずに改行する。
4
+ *
5
+ * 置けるかどうかはスキーマに聞き、種別で判定しない。見出しと表のセルは中身が `text*` で
6
+ * あるため、ここで名前を並べなくても偽が返る(→ スキーマの `hard_break`)。判定を
7
+ * 二重に持たないことで、置ける場所を増やしたときに直し忘れが起きない。
8
+ */
9
+ export declare const insertLineBreak: EditorCommand;
@@ -0,0 +1,16 @@
1
+ import { outlineSchema } from '../../models/document/schema';
2
+ /**
3
+ * 段落を分けずに改行する。
4
+ *
5
+ * 置けるかどうかはスキーマに聞き、種別で判定しない。見出しと表のセルは中身が `text*` で
6
+ * あるため、ここで名前を並べなくても偽が返る(→ スキーマの `hard_break`)。判定を
7
+ * 二重に持たないことで、置ける場所を増やしたときに直し忘れが起きない。
8
+ */
9
+ export const insertLineBreak = (state, dispatch) => {
10
+ const { $from } = state.selection;
11
+ const type = outlineSchema.nodes.hard_break;
12
+ if (!$from.parent.canReplaceWith($from.index(), $from.index(), type))
13
+ return false;
14
+ dispatch?.(state.tr.replaceSelectionWith(type.create()).scrollIntoView());
15
+ return true;
16
+ };
@@ -0,0 +1,7 @@
1
+ import type { EditorState, Transaction } from 'prosemirror-state';
2
+ export declare const URL_PATTERN: RegExp;
3
+ /**
4
+ * リンクの適用と解除。すでにリンクなら解除し、選択した文字が URL ならその URL へのリンクにする。
5
+ * URL を入力させる画面はモジュールの責務ではないため、それ以外では何もしない。
6
+ */
7
+ export declare function toggleLink(state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
@@ -0,0 +1,21 @@
1
+ import { outlineSchema } from '../../models/document/schema';
2
+ export const URL_PATTERN = /^(https?:\/\/|mailto:)\S+$/;
3
+ /**
4
+ * リンクの適用と解除。すでにリンクなら解除し、選択した文字が URL ならその URL へのリンクにする。
5
+ * URL を入力させる画面はモジュールの責務ではないため、それ以外では何もしない。
6
+ */
7
+ export function toggleLink(state, dispatch) {
8
+ const link = outlineSchema.marks.link;
9
+ const { from, to, empty } = state.selection;
10
+ if (state.doc.rangeHasMark(from, to, link)) {
11
+ dispatch?.(state.tr.removeMark(from, to, link));
12
+ return true;
13
+ }
14
+ if (empty)
15
+ return false;
16
+ const text = state.doc.textBetween(from, to);
17
+ if (!URL_PATTERN.test(text))
18
+ return false;
19
+ dispatch?.(state.tr.addMark(from, to, link.create({ href: text })));
20
+ return true;
21
+ }
@@ -0,0 +1,126 @@
1
+ import type { Fragment, Node } from 'prosemirror-model';
2
+ import { type EditorState, type Transaction } from 'prosemirror-state';
3
+ import type { EditorCommand, MoveDirection } from '../operations.types';
4
+ /**
5
+ * 箇条書きの階層操作。`prosemirror-flat-list` への依存をこのファイルに集約する。
6
+ * 他所からこのライブラリを直接 import しない(バージョン更新の影響範囲を 1 ファイルに閉じる)。
7
+ */
8
+ /** 箇条書きの種別。マークダウンで表せるものだけを扱う */
9
+ export type ListKind = 'bullet' | 'ordered' | 'task';
10
+ export interface ListCommands {
11
+ readonly indentList: EditorCommand;
12
+ readonly dedentList: EditorCommand;
13
+ readonly splitListItem: EditorCommand;
14
+ readonly toggleTaskChecked: EditorCommand;
15
+ /** 箇条書きにする/同じ種別なら解除する */
16
+ readonly toggleListKind: (kind: ListKind) => EditorCommand;
17
+ /** 箇条書きを解いて段落に戻す */
18
+ readonly unwrapList: EditorCommand;
19
+ /** 項目を配下ごと前後の兄弟と入れ替える */
20
+ readonly moveListItem: (direction: MoveDirection) => EditorCommand;
21
+ /** 項目の本文の先頭での `Backspace`。吸収先があれば吸収し、無ければ従来の連鎖へ譲る */
22
+ readonly backspace: EditorCommand;
23
+ /** 項目の本文より後ろにあるブロックを、その項目の外(直後)へ出す */
24
+ readonly liftBlockOutOfItem: (pos: number, state: EditorState, dispatch?: (tr: Transaction) => void) => boolean;
25
+ readonly isInList: (state: EditorState) => boolean;
26
+ /** カーソルが属する最も内側の項目(配下を含む)。無ければ null */
27
+ readonly enclosingListItem: (state: EditorState) => {
28
+ node: Node;
29
+ pos: number;
30
+ } | null;
31
+ }
32
+ export declare const listCommands: ListCommands;
33
+ /**
34
+ * 段落を箇条書きの項目にする。
35
+ *
36
+ * これは段落に対する階層操作ではなく、`Tab` に割り当てたブロック種別の変換である。
37
+ * 最上位の項目で `Shift+Tab` を押すと段落へ戻るので、
38
+ * 同じキーの対で往復できる。片方だけだと、リストから出られるのに戻れなくなる。
39
+ *
40
+ * すでに項目の中にいるときは何もしない。ここまで来るのは「直前に兄弟がなくて
41
+ * 深くできなかった」場合であり、そこで種別を切り替えると項目が解除されてしまう。
42
+ */
43
+ export declare const paragraphToListItem: EditorCommand;
44
+ /**
45
+ * 吸収の対象。判定が真のときだけ得られる。
46
+ *
47
+ * 相手の種別で分け、それぞれに必要な位置だけを持つ。**折りたたみの状態は持たない**。
48
+ * 畳んでいるかどうかで結果を変えないためである。
49
+ */
50
+ export type AbsorbTarget = {
51
+ readonly kind: 'previousItem';
52
+ /** 吸収される項目そのものの位置 */
53
+ readonly itemPos: number;
54
+ /** 直前の兄弟の項目の位置。配下の差し戻し先を導く */
55
+ readonly previousItemPos: number;
56
+ } | {
57
+ readonly kind: 'parentContent';
58
+ readonly itemPos: number;
59
+ /** 相手となる本文を持つ親の項目の位置 */
60
+ readonly parentPos: number;
61
+ };
62
+ /**
63
+ * カーソル位置から吸収の対象を求める。無ければ null。文書は変えない。
64
+ *
65
+ * 吸収先と認めるのは「直前の兄弟の項目」と「親の項目の本文」の 2 つだけである
66
+ *。これ以外を真と判定すると、項目の文字が見出しへ結合される。
67
+ *
68
+ * **項目が空かどうかは見ない**。空の項目で起きることは、中身のある
69
+ * 項目で起きることの「結合する本文が空だった場合」でしかなく、規則は 1 つで足りる。
70
+ */
71
+ export declare function absorbTargetAt(state: EditorState): AbsorbTarget | null;
72
+ /**
73
+ * 吸収される項目の配下(本文より後ろの子)を文書から取り出す。配下が無ければ null。
74
+ *
75
+ * 取り出しと差し戻しが 2 つに分かれているのは、**間に本文の結合が入る**ためである。
76
+ * 配下を文書に残したまま結合すると、配下が「文書順で直前のテキストブロック」になって
77
+ * そこへ文字がくっつくか、結合先の内側へ埋まる。取り出している間はどちらも起きない。
78
+ */
79
+ export declare function detachChildren(tr: Transaction, target: AbsorbTarget): Fragment | null;
80
+ /**
81
+ * 取り出しておいた配下を差し戻す。
82
+ *
83
+ * 差し戻し先は、直前の兄弟の項目があれば**その子の末尾**(元の深さのまま)、
84
+ * 無ければ**親の直下**(1 段浅くなる)。
85
+ *
86
+ * 相手の位置は `tr.mapping` を通して現在の文書へ写す。結合によって相手の大きさが
87
+ * 変わっているため、取り出し時点で求めた末尾をそのまま使うと別の場所へ入る。
88
+ */
89
+ export declare function attachChildren(tr: Transaction, target: AbsorbTarget, children: Fragment): void;
90
+ /**
91
+ * 本文を持たない項目を解消した `Transaction`。補正が要らなければ null。
92
+ *
93
+ * `list` の内容モデルは `block+` であり、**本文を持たず最初の子が項目である項目**を
94
+ * 構成できてしまう。範囲選択での削除がこの形を作る(上位の項目だけを消したつもりが、
95
+ * 空のコンテナとして残る)。書き手にはコンテナが見えないため、配下だけが 1 段深い位置に
96
+ * 取り残されたように見える。
97
+ *
98
+ * コンテナを取り除き、配下をその位置へそのまま戻す。**配下は消さない**(消えるのは、
99
+ * 書き手が消した本文だけである)。深さだけが 1 段浅くなり、見た目と構造が一致する。
100
+ *
101
+ * 読み込みからこの形は来ない。マークダウンに書かれている項目は書き手が消したもので
102
+ * はないため、読む側は解消せずに空の 1 行を補う(→ パーサの `withOwnLine`)。
103
+ */
104
+ export declare function correctItemDepth(state: EditorState): Transaction | null;
105
+ /**
106
+ * セクションの端にある項目を、隣のセクションへ送り出す。
107
+ *
108
+ * 同じセクションの中でもう動けなくなったときだけ働く。項目のまま移るので、
109
+ * 箇条書きの階層は変わらない。
110
+ *
111
+ * 「セクションの中で止まる」を選ばないのは、書き手から見て見出しと項目が
112
+ * ひと続きのアウトラインだからである。下へ押し続けたら下へ進んでほしい。
113
+ *
114
+ * 行き先は**文書順で隣にあるセクション**であり、同じ階層とは限らない。子セクションが
115
+ * あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
116
+ * ので、行った先から同じキーで必ず戻れる。
117
+ */
118
+ export declare function moveItemAcrossSection(direction: MoveDirection): EditorCommand;
119
+ /**
120
+ * `from` 以降で、連続する項目が終わるインデックス。
121
+ *
122
+ * **セクションの中身の末尾ではない。** 間に段落や表が挟まっていれば、そこで止まる。飛び越えると、
123
+ * 書き手が見ている「このリスト」の外へ出てしまう。既存の `blockRunEnd` はブロック全体の
124
+ * 連なりを返すもので種別を見ないため流用できない。
125
+ */
126
+ export declare function listRunEnd(parent: Node, from: number): number;