@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,437 @@
1
+ import { GapCursor } from 'prosemirror-gapcursor';
2
+ import { Selection, TextSelection } from 'prosemirror-state';
3
+ import { addColumn, addRow, CellSelection, deleteTable, goToNextCell as goToAdjacentCell, isInTable, moveTableColumn, moveTableRow, nextCell, removeColumn, removeRow, selectedRect, selectionCell, TableMap } from 'prosemirror-tables';
4
+ import { bodyStart } from '../../models/document/bodySelection';
5
+ import { outlineSchema } from '../../models/document/schema';
6
+ const n = outlineSchema.nodes;
7
+ /** 見出し行は表の 0 行目である(`table_header` の有無では判定しない) */
8
+ const HEADER_ROW = 0;
9
+ /** 配置の線。両端では止まり、押し続けても巡回しない */
10
+ const ALIGN_LINE = [null, 'left', 'center', 'right'];
11
+ export const tableCommands = {
12
+ createTable,
13
+ insertRowBefore: (state, dispatch) => insertRow(state, dispatch, 'before'),
14
+ insertRowAfter: (state, dispatch) => insertRow(state, dispatch, 'after'),
15
+ insertColumnBefore: (state, dispatch) => insertColumn(state, dispatch, 'before'),
16
+ insertColumnAfter: (state, dispatch) => insertColumn(state, dispatch, 'after'),
17
+ deleteSelected,
18
+ deleteSelectedRowOrColumn,
19
+ align: (direction) => (state, dispatch) => applyAlign(state, dispatch, direction),
20
+ moveRow: (direction) => (state, dispatch) => shiftRow(state, dispatch, direction),
21
+ moveColumn: (direction) => (state, dispatch) => shiftColumn(state, dispatch, direction),
22
+ goToNextCell,
23
+ goToPreviousCell: goToAdjacentCell(-1),
24
+ goToCellAbove: goToCellInDirection('up'),
25
+ goToCellBelow: goToCellInDirection('down'),
26
+ extendSelection: (direction) => extendSelectionToCell(direction)
27
+ };
28
+ /**
29
+ * 選択の形から削除の対象を決める。表の外では `null`。
30
+ *
31
+ * **判定の順序が仕様である。広い対象から先に見る**(表 → 列 → 行)。
32
+ * 逆にすると、表全体を選んでも行の削除として解釈される。
33
+ */
34
+ export function deleteTargetOf(state) {
35
+ if (!isInTable(state))
36
+ return null;
37
+ if (!(state.selection instanceof CellSelection))
38
+ return 'row';
39
+ const rect = selectedRect(state);
40
+ const coversAllRows = rect.top === 0 && rect.bottom === rect.map.height;
41
+ const coversAllColumns = rect.left === 0 && rect.right === rect.map.width;
42
+ if (coversAllRows && coversAllColumns)
43
+ return 'table';
44
+ if (coversAllRows)
45
+ return 'column';
46
+ return 'row';
47
+ }
48
+ // --- 表そのもの ---
49
+ function createTable(state, dispatch) {
50
+ // 入れ子の表を作らない
51
+ if (isInTable(state))
52
+ return false;
53
+ // コードブロックの中では記法を解釈しない
54
+ if (state.selection.$from.parent.type.spec.code)
55
+ return false;
56
+ if (dispatch) {
57
+ const at = state.selection.from;
58
+ const tr = state.tr.replaceSelectionWith(emptyTable());
59
+ const tableStart = firstTableAfter(tr.doc, at);
60
+ dispatch(tableStart === null ? tr : cursorInCell(tr, tableStart, HEADER_ROW, 0));
61
+ }
62
+ return true;
63
+ }
64
+ function emptyTable() {
65
+ const header = () => n.table_header.createAndFill();
66
+ const cell = () => n.table_cell.createAndFill();
67
+ return n.table.createChecked(null, [
68
+ n.table_row.createChecked(null, [header(), header()]),
69
+ n.table_row.createChecked(null, [cell(), cell()])
70
+ ]);
71
+ }
72
+ /** 指定の位置以降で最初に現れる表の、中身が始まる位置 */
73
+ function firstTableAfter(doc, from) {
74
+ let start = null;
75
+ doc.nodesBetween(from, doc.content.size, (node, pos) => {
76
+ if (start !== null)
77
+ return false;
78
+ if (node.type === n.table) {
79
+ start = pos + 1;
80
+ return false;
81
+ }
82
+ return true;
83
+ });
84
+ return start;
85
+ }
86
+ // --- 行と列の追加 ---
87
+ function insertRow(state, dispatch, where) {
88
+ if (!isInTable(state))
89
+ return false;
90
+ const rect = selectedRect(state);
91
+ // 見出し行の上へ挿入すると、足した空の行が見出し行になる(配置指定も失われる)
92
+ if (where === 'before' && rect.top === HEADER_ROW)
93
+ return false;
94
+ if (dispatch) {
95
+ const at = where === 'before' ? rect.top : rect.bottom;
96
+ const tr = addRow(state.tr, rect, at);
97
+ dispatch(cursorInCell(tr, rect.tableStart, at, rect.left));
98
+ }
99
+ return true;
100
+ }
101
+ function insertColumn(state, dispatch, where) {
102
+ if (!isInTable(state))
103
+ return false;
104
+ if (dispatch) {
105
+ const rect = selectedRect(state);
106
+ const at = where === 'before' ? rect.left : rect.right;
107
+ // 足したセルは属性の既定値を持つ。新しい列の配置は「指定なし」から始まる
108
+ const tr = addColumn(state.tr, rect, at);
109
+ dispatch(cursorInCell(tr, rect.tableStart, rect.top, at));
110
+ }
111
+ return true;
112
+ }
113
+ // --- 削除 ---
114
+ function deleteSelected(state, dispatch) {
115
+ const target = deleteTargetOf(state);
116
+ if (target === null)
117
+ return false;
118
+ if (target === 'table')
119
+ return deleteTable(state, dispatch);
120
+ if (target === 'column')
121
+ return deleteColumns(state, dispatch);
122
+ return deleteRows(state, dispatch);
123
+ }
124
+ /**
125
+ * 選択が覆う本文行を取り除く。
126
+ *
127
+ * 既製の `deleteRow` は使えない。見出し行を消すうえ、すべての行が選ばれていると
128
+ * 何もせずに `true` を返す(本文行を全部消して見出し行だけを残せない)。
129
+ */
130
+ function deleteRows(state, dispatch) {
131
+ const rect = selectedRect(state);
132
+ const from = Math.max(rect.top, HEADER_ROW + 1);
133
+ if (from >= rect.bottom)
134
+ return false;
135
+ if (dispatch) {
136
+ const tr = state.tr;
137
+ const target = { ...rect };
138
+ for (let row = rect.bottom - 1; row >= from; row -= 1) {
139
+ removeRow(tr, target, row);
140
+ refreshTable(tr, target);
141
+ }
142
+ // 削除した行の次の行、それがなければ前の行へ(`cursorInCell` が端で丸める)
143
+ dispatch(cursorInCell(tr, rect.tableStart, from, rect.left));
144
+ }
145
+ return true;
146
+ }
147
+ /**
148
+ * 選択が覆う列を取り除く。
149
+ *
150
+ * 既製の `deleteColumn` は、列を 1 つしか持たない表でも問い合わせには `true` を返す
151
+ * (実行できない判定を dispatch のときにしか行わない)。答えと振る舞いを一致させる。
152
+ */
153
+ function deleteColumns(state, dispatch) {
154
+ const rect = selectedRect(state);
155
+ if (rect.right - rect.left >= rect.map.width)
156
+ return false;
157
+ if (dispatch) {
158
+ const tr = state.tr;
159
+ const target = { ...rect };
160
+ for (let column = rect.right - 1; column >= rect.left; column -= 1) {
161
+ removeColumn(tr, target, column);
162
+ refreshTable(tr, target);
163
+ }
164
+ dispatch(cursorInCell(tr, rect.tableStart, rect.top, rect.left));
165
+ }
166
+ return true;
167
+ }
168
+ /**
169
+ * 行・列・表を丸ごと選んでいるときだけ、それを取り除く。
170
+ *
171
+ * 修飾キーを伴わない削除に当てる。**なぞって選んで消す**が成り立てば、割り当てを
172
+ * 覚えていなくても行と列を減らせる。
173
+ *
174
+ * 覆いきらない選択では譲る。そちらは「選んだセルの中身を空にする」という別の意味を
175
+ * 持っており、編集基盤の既定の削除がそれを行う。
176
+ */
177
+ function deleteSelectedRowOrColumn(state, dispatch) {
178
+ const selection = state.selection;
179
+ if (!(selection instanceof CellSelection))
180
+ return false;
181
+ if (!selection.isRowSelection() && !selection.isColSelection())
182
+ return false;
183
+ return deleteSelected(state, dispatch);
184
+ }
185
+ // --- 並べ替え ---
186
+ /**
187
+ * 選択が覆う本文行を、1 つ隣の本文行と入れ替える。
188
+ *
189
+ * **隣の行を、選んだセクションの反対側へ動かす。** セクション全体を 1 つずつ動かすのと
190
+ * 同じ結果になり、複数行にまたがる選択も 1 回の入れ替えで済む。
191
+ */
192
+ function shiftRow(state, dispatch, direction) {
193
+ if (!isInTable(state))
194
+ return false;
195
+ const rect = selectedRect(state);
196
+ const step = direction === 'up' ? -1 : 1;
197
+ // 見出し行は動かさず、入れ替える相手も本文行に限る(端の外へは動かさない)。
198
+ // **端の判定は呼ぶ前に行う。** 既製の入れ替えは範囲外の番号で例外を投げる
199
+ if (rect.top === HEADER_ROW)
200
+ return false;
201
+ if (step < 0 ? rect.top - 1 <= HEADER_ROW : rect.bottom + 1 > rect.map.height)
202
+ return false;
203
+ if (dispatch) {
204
+ const from = step < 0 ? rect.top - 1 : rect.bottom;
205
+ const to = step < 0 ? rect.bottom - 1 : rect.top;
206
+ moveTableRow({ from, to, select: false })(state, (tr) => {
207
+ dispatch(keepSelection(tr, rect, step, 0));
208
+ });
209
+ }
210
+ return true;
211
+ }
212
+ function shiftColumn(state, dispatch, direction) {
213
+ if (!isInTable(state))
214
+ return false;
215
+ const rect = selectedRect(state);
216
+ const step = direction === 'left' ? -1 : 1;
217
+ if (step < 0 ? rect.left === 0 : rect.right + 1 > rect.map.width)
218
+ return false;
219
+ if (dispatch) {
220
+ const from = step < 0 ? rect.left - 1 : rect.right;
221
+ const to = step < 0 ? rect.right - 1 : rect.left;
222
+ // 配置指定はセルの属性なので、列と一緒に動く
223
+ moveTableColumn({ from, to, select: false })(state, (tr) => {
224
+ dispatch(keepSelection(tr, rect, 0, step));
225
+ });
226
+ }
227
+ return true;
228
+ }
229
+ /**
230
+ * 入れ替えた先で、操作前と同じセルを指し直す。
231
+ *
232
+ * 既製の入れ替えは表そのものを差し替えるため、位置の対応が失われる
233
+ * (行や列を選んでいた状態にされるか、選択が表の外へ落ちる)。
234
+ */
235
+ function keepSelection(tr, rect, rowStep, columnStep) {
236
+ const top = rect.top + rowStep;
237
+ const left = rect.left + columnStep;
238
+ if (rect.bottom - rect.top === 1 && rect.right - rect.left === 1) {
239
+ return cursorInCell(tr, rect.tableStart, top, left);
240
+ }
241
+ const table = tr.doc.nodeAt(rect.tableStart - 1);
242
+ if (!table)
243
+ return tr;
244
+ const map = TableMap.get(table);
245
+ const anchor = rect.tableStart + map.positionAt(top, left, table);
246
+ const head = rect.tableStart + map.positionAt(rect.bottom - 1 + rowStep, rect.right - 1 + columnStep, table);
247
+ return tr.setSelection(CellSelection.create(tr.doc, anchor, head));
248
+ }
249
+ // --- 列の配置 ---
250
+ function applyAlign(state, dispatch, direction) {
251
+ if (!isInTable(state))
252
+ return false;
253
+ const rect = selectedRect(state);
254
+ const current = cellAt(rect, HEADER_ROW, rect.left)?.attrs.align ?? null;
255
+ const next = ALIGN_LINE[ALIGN_LINE.indexOf(current) + (direction === 'rightward' ? 1 : -1)];
256
+ // 線の端。押し続けても巡回しない
257
+ if (next === undefined)
258
+ return false;
259
+ if (dispatch) {
260
+ const tr = state.tr;
261
+ for (let column = rect.left; column < rect.right; column += 1) {
262
+ for (let row = 0; row < rect.map.height; row += 1) {
263
+ const pos = rect.tableStart + rect.map.positionAt(row, column, rect.table);
264
+ const cell = tr.doc.nodeAt(pos);
265
+ // 属性の差し替えは大きさを変えないため、位置は取り直さなくてよい
266
+ if (cell)
267
+ tr.setNodeMarkup(pos, null, { ...cell.attrs, align: next });
268
+ }
269
+ }
270
+ dispatch(tr);
271
+ }
272
+ return true;
273
+ }
274
+ // --- セル間の移動 ---
275
+ /**
276
+ * 次のセルへ移る。表の最後のセルでは、末尾に空の行を足してその先頭セルへ移る。
277
+ * 既製の移動は最後のセルで `false` を返すだけで、行を足す手段を持たない。
278
+ */
279
+ function goToNextCell(state, dispatch) {
280
+ if (!isInTable(state))
281
+ return false;
282
+ if (goToAdjacentCell(1)(state, dispatch))
283
+ return true;
284
+ if (dispatch) {
285
+ const rect = selectedRect(state);
286
+ const at = rect.map.height;
287
+ const tr = addRow(state.tr, rect, at);
288
+ dispatch(cursorInCell(tr, rect.tableStart, at, 0));
289
+ }
290
+ return true;
291
+ }
292
+ /**
293
+ * 真上・真下のセルへ移る。表の端では表の外の行へ出る。
294
+ *
295
+ * **ここを手放すとブラウザ既定の動きになり、上下の代わりに横へ動く。** セルの中には
296
+ * 上下の行がないため、ブラウザは文書順で次(前)の位置——つまり同じ行の隣のセル——へ
297
+ * カーソルを送る。
298
+ *
299
+ * 編集基盤にも表の矢印処理はあるが、「セルがブロックを含む」前提の深さで端を判定して
300
+ * おり、セルがインラインだけを含む本モジュールでは発火しない。
301
+ */
302
+ function goToCellInDirection(direction) {
303
+ const step = direction === 'up' ? -1 : 1;
304
+ return (state, dispatch, view) => {
305
+ // 端かどうかは実際に描かれた文字の位置から決まるため、描画されたエディタが要る。
306
+ // 変換中の抑止はキーを受け取る経路が一律に掛けるので、ここでは見ない
307
+ if (!view)
308
+ return false;
309
+ if (!isInTable(state))
310
+ return false;
311
+ const selection = state.selection;
312
+ if (!(selection instanceof TextSelection) || !selection.empty)
313
+ return false;
314
+ // セルの中で行が折り返しているときは、まずセルの中を動く
315
+ if (!view.endOfTextblock(direction))
316
+ return false;
317
+ const cell = selectionCell(state);
318
+ const next = nextCell(cell, 'vert', step);
319
+ const target = next ? Selection.near(next, 1) : outsideTable(state, cell, step);
320
+ if (!target)
321
+ return false;
322
+ dispatch?.(state.tr.setSelection(target).scrollIntoView());
323
+ return true;
324
+ };
325
+ }
326
+ /**
327
+ * 表の外の、その向きにある行へ出る。行き先が無ければ表の前後のカーソルを立てる。
328
+ *
329
+ * `Selection.near` は使えない。**行き先が無いと逆側へ落ち、表の中へ戻ってしまう**
330
+ * ため、表の先頭にある表で上へ出ようとすると左上のセルへ吸い込まれる。
331
+ *
332
+ * 本文の外(フロントマター)へも出ない。本文の先頭にある表の手前は「表の前に段落を足す」
333
+ * ための置き場所であり、そこを飛び越すとその手がかりに届かなくなる。カーソルはそこから
334
+ * もう一度上へ押せばフロントマターへ入る。
335
+ */
336
+ function outsideTable(state, cell, step) {
337
+ const at = state.doc.resolve(step < 0 ? cell.before(-1) : cell.after(-1));
338
+ const found = Selection.findFrom(at, step);
339
+ if (found && found.from >= bodyStart(state.doc))
340
+ return found;
341
+ return canPlaceGapCursor(at) ? new GapCursor(at) : null;
342
+ }
343
+ /**
344
+ * その位置に、文字の入らないカーソルを置いてよいか。
345
+ * 編集基盤が矢印の行き先を決めるのに使う判定だが、型定義に現れないため形を与える。
346
+ */
347
+ const canPlaceGapCursor = GapCursor.valid;
348
+ // --- セル範囲の選択 ---
349
+ /**
350
+ * 選択をその向きの隣のセルまで広げる。セル範囲の選択をキーボードだけで作る手段である。
351
+ *
352
+ * **セルの中に文字が残っている間は譲る。** まずセルの中を文字単位で選び、端まで来て
353
+ * はじめてセル単位の選択へ移る。左右は文字の選択と同じ向きなので、これがないと
354
+ * セルの中を選べなくなる。
355
+ *
356
+ * 編集基盤にも同じ割り当てがあるが、`goToCellInDirection` と同じ理由で発火しない
357
+ * (端の判定が「セルがブロックを含む」前提の深さで行われる)。
358
+ */
359
+ function extendSelectionToCell(direction) {
360
+ const axis = direction === 'up' || direction === 'down' ? 'vert' : 'horiz';
361
+ const step = direction === 'up' || direction === 'left' ? -1 : 1;
362
+ return (state, dispatch, view) => {
363
+ if (!isInTable(state))
364
+ return false;
365
+ const current = cellSelectionToExtend(state, view, direction);
366
+ if (!current)
367
+ return false;
368
+ const head = nextCell(current.$headCell, axis, step);
369
+ // 表の端。選択は表の外へは広がらない
370
+ if (!head)
371
+ return false;
372
+ const next = new CellSelection(current.$anchorCell, head);
373
+ dispatch?.(state.tr.setSelection(next).scrollIntoView());
374
+ return true;
375
+ };
376
+ }
377
+ /**
378
+ * 広げる元になるセル範囲。すでにセル範囲を選んでいればそれ、カーソルがセルの中の
379
+ * その向きの端にあれば、そのセル 1 つを覆う範囲。どちらでもなければ `null`。
380
+ */
381
+ function cellSelectionToExtend(state, view, direction) {
382
+ const selection = state.selection;
383
+ if (selection instanceof CellSelection)
384
+ return selection;
385
+ if (!(selection instanceof TextSelection))
386
+ return null;
387
+ // 端かどうかは実際に描かれた文字の位置から決まるため、描画されたエディタが要る
388
+ if (!view || !view.endOfTextblock(direction))
389
+ return null;
390
+ return new CellSelection(selectionCell(state));
391
+ }
392
+ /**
393
+ * 表の指定した位置へ空の列を挿入する(画面上の手がかりから呼ぶ)。
394
+ *
395
+ * カーソルに依存する `insertColumnBefore` / `insertColumnAfter` は使えない。
396
+ * 押した境目がそのまま挿入位置であり、**押す前にカーソルがどこにあってもよい**ため。
397
+ *
398
+ * @param tableStart 表の中身が始まる位置(表のノード位置 + 1)
399
+ * @param index 挿入する列の番号。0 なら左端、列数と同じなら右端
400
+ */
401
+ export function insertColumnAt(state, dispatch, tableStart, index) {
402
+ const table = state.doc.nodeAt(tableStart - 1);
403
+ if (!table || table.type !== n.table)
404
+ return false;
405
+ const map = TableMap.get(table);
406
+ if (index < 0 || index > map.width)
407
+ return false;
408
+ if (dispatch) {
409
+ const tr = addColumn(state.tr, { map, tableStart, table }, index);
410
+ dispatch(cursorInCell(tr, tableStart, HEADER_ROW, index));
411
+ }
412
+ return true;
413
+ }
414
+ // --- 位置の取り回し ---
415
+ /**
416
+ * 表の中の指定のセルへカーソルを置く。
417
+ * 行・列の番号は表の端で丸める(削除の後、次の行が無ければ前の行へ置くため)。
418
+ */
419
+ function cursorInCell(tr, tableStart, row, column) {
420
+ const table = tr.doc.nodeAt(tableStart - 1);
421
+ if (!table)
422
+ return tr;
423
+ const map = TableMap.get(table);
424
+ const at = map.positionAt(Math.min(row, map.height - 1), Math.min(column, map.width - 1), table);
425
+ return tr.setSelection(TextSelection.near(tr.doc.resolve(tableStart + at + 1)));
426
+ }
427
+ function cellAt(rect, row, column) {
428
+ return rect.table.nodeAt(rect.map.positionAt(row, column, rect.table));
429
+ }
430
+ /** 表を差し替えた後、以降の操作が見る表と対応表を取り直す */
431
+ function refreshTable(tr, rect) {
432
+ const table = tr.doc.nodeAt(rect.tableStart - 1);
433
+ if (!table)
434
+ return;
435
+ rect.table = table;
436
+ rect.map = TableMap.get(table);
437
+ }
@@ -0,0 +1,22 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import type { EditorAction, EditorCommand } from '../operations.types';
3
+ /**
4
+ * 宣言(`coreActions.ts`)を 1 つの `Plugin[]` へ組み立てる唯一の場所。
5
+ *
6
+ * 覆い(IME 変換中の抑止・読み取り専用の遮断・取り消し単位の分離)を掛けるのは
7
+ * ここ(`decorate` / `ruleAsOwnUndoStep`)だけである。他の場所で掛けない。
8
+ * キー以外の入口も `decorate` を通すため、入口の違いが挙動の違いにならない。
9
+ */
10
+ export interface EditorKeymapModule {
11
+ readonly coreActions: readonly EditorAction[];
12
+ /** 拡張の操作は基盤の後段で解決される(基盤 → 拡張) */
13
+ createPlugins(extraActions: readonly EditorAction[]): readonly Plugin[];
14
+ /** 基盤のキーと衝突した拡張の操作 id。衝突したものは基盤が先に解決する */
15
+ findConflicts(extraActions: readonly EditorAction[]): readonly string[];
16
+ /**
17
+ * 覆い(変換中の抑止・読み取り専用の遮断・取り消し単位の分離)を掛けたコマンド。
18
+ * キーの組み立てと、キー以外の入口(コンテキストメニュー)が同じものを使う。
19
+ */
20
+ decorate(action: EditorAction): EditorCommand;
21
+ }
22
+ export declare const editorKeymap: EditorKeymapModule;
@@ -0,0 +1,165 @@
1
+ import { baseKeymap, chainCommands } from 'prosemirror-commands';
2
+ import { closeHistory } from 'prosemirror-history';
3
+ import { InputRule, inputRules } from 'prosemirror-inputrules';
4
+ import { keydownHandler, keymap } from 'prosemirror-keymap';
5
+ import { Plugin } from 'prosemirror-state';
6
+ import { autoformatRules } from '../autoformat';
7
+ import { linkPastePlugin } from '../plugins/linkPaste';
8
+ import { tabEscapePlugin } from '../plugins/tabEscape';
9
+ import { coreActions } from './coreActions';
10
+ import { isMacPlatform, toBindingString } from './shortcutKeys';
11
+ export const editorKeymap = {
12
+ coreActions,
13
+ createPlugins,
14
+ findConflicts,
15
+ decorate
16
+ };
17
+ /**
18
+ * 基盤と同じキーを使っている拡張の操作を挙げる。
19
+ *
20
+ * 実行環境で修飾キーの当たり方が変わるため、両方の環境で照合する。
21
+ * 見つかった操作は配られず、基盤が先に解決する(→ `withoutCoreKeys`)。
22
+ */
23
+ function findConflicts(extraActions) {
24
+ const conflicts = new Set();
25
+ for (const mac of [true, false]) {
26
+ const coreKeys = new Set(expand(coreActions).map(([, keys]) => toBindingString(keys, mac)));
27
+ for (const [action, keys] of expand(extraActions)) {
28
+ if (coreKeys.has(toBindingString(keys, mac)))
29
+ conflicts.add(action.id);
30
+ }
31
+ }
32
+ return [...conflicts];
33
+ }
34
+ function createPlugins(extraActions) {
35
+ const coreBindings = bindingsOf(coreActions);
36
+ const viewBindings = bindingsOf(extraActions.filter((action) => action.category === 'view'));
37
+ return [
38
+ tabEscapePlugin(),
39
+ // 表示操作は編集可否の判定より前に配る(→ viewKeymap)
40
+ viewKeymap(withoutCoreKeys(viewBindings, coreBindings)),
41
+ // 塞ぐかどうかは各定義の「文書を変えるか」で決まる。基盤と拡張で扱いを分けない
42
+ keymap(coreBindings),
43
+ keymap(bindingsOf(extraActions.filter((action) => action.category !== 'view'))),
44
+ keymap(baseKeymap),
45
+ inputRules({ rules: autoformatRules.map(ruleAsOwnUndoStep) }),
46
+ linkPastePlugin()
47
+ ];
48
+ }
49
+ /**
50
+ * 表示操作のキーを、編集できるかどうかの判定より前に配る。
51
+ *
52
+ * 通常のキーバインドは `handleKeyDown` として登録される。この口は `prosemirror-view` が
53
+ * 「編集できるときだけ呼ぶ」ものに数えているため、読み取り専用の編集エリアでは呼ばれない。
54
+ * 折りたたみやフォーカスは文書を変えないので、読むだけの画面でも働く必要がある
55
+ * `handleDOMEvents` は編集可否を問わず先に呼ばれる。
56
+ *
57
+ * 受け止めたときは既定動作を止める。`handleKeyDown` の口と違い、こちらは
58
+ * `prosemirror-view` が自動では止めてくれない。
59
+ */
60
+ function viewKeymap(bindings) {
61
+ const handler = keydownHandler(bindings);
62
+ return new Plugin({
63
+ props: {
64
+ handleDOMEvents: {
65
+ keydown: (view, event) => {
66
+ if (!handler(view, event))
67
+ return false;
68
+ event.preventDefault();
69
+ return true;
70
+ }
71
+ }
72
+ }
73
+ });
74
+ }
75
+ /**
76
+ * 基盤と同じキーは基盤に譲る。
77
+ *
78
+ * この口は基盤より先に呼ばれるため、そのまま置くと拡張が基盤を上書きしてしまう。
79
+ * 衝突そのものは割り当ての定義から検出できる(→ `findShortcutConflicts`)。
80
+ */
81
+ function withoutCoreKeys(bindings, coreBindings) {
82
+ const result = {};
83
+ for (const [key, command] of Object.entries(bindings)) {
84
+ if (!(key in coreBindings))
85
+ result[key] = command;
86
+ }
87
+ return result;
88
+ }
89
+ /**
90
+ * 覆い(変換中の抑止・読み取り専用の遮断・取り消し単位)を掛ける**唯一の場所**。
91
+ *
92
+ * 掛ける・掛けないは**操作自身の宣言と分類から決まる**。呼び出し側の事情で変えない。
93
+ * 1 つの操作だけを扱い、同じキーの連鎖は `bindingsOf` の関心とする。
94
+ */
95
+ function decorate(action) {
96
+ const command = OWN_UNDO_STEP.has(action.category)
97
+ ? asOwnUndoStep(action.command)
98
+ : action.command;
99
+ return withoutComposition(action.changesDocument ? whenEditable(command) : command);
100
+ }
101
+ /**
102
+ * 実行環境に応じて修飾キーを選ぶ。判定はモジュール読み込み時ではなくここで行う
103
+ * (読み込みだけで `navigator` に触れないため)。
104
+ */
105
+ function bindingsOf(actions) {
106
+ const mac = isMacPlatform();
107
+ const bindings = {};
108
+ for (const [action, keys] of expand(actions)) {
109
+ const key = toBindingString(keys, mac);
110
+ const previous = bindings[key];
111
+ // 同じキーに複数の操作があるときは、先に登録された方を優先する(基盤 → 拡張)
112
+ bindings[key] = previous ? chainCommands(previous, decorate(action)) : decorate(action);
113
+ }
114
+ return bindings;
115
+ }
116
+ /**
117
+ * 宣言を(操作, キー)の組へ展開する。
118
+ *
119
+ * 束ねる前に平らにすることで、同じキーを分け合う操作の**連鎖の順序が宣言順のまま**
120
+ * 保たれる。順序は優先順位そのものであり、表の操作を先に置く並べ方(→ `coreActions.ts`)が
121
+ * ここで意味を持つ。
122
+ */
123
+ function expand(actions) {
124
+ return actions.flatMap((action) => action.keys.map((keys) => [action, keys]));
125
+ }
126
+ /**
127
+ * 編集(複製・削除・チェック)・移動・変換・表・書式は、それぞれ独立した取り消し単位に
128
+ * する。連続した文字入力だけがひとまとまりになる。
129
+ *
130
+ * **分類を増やしたら、ここに足すかを必ず判断する。** 漏れると直前の文字入力と
131
+ * ひとまとまりになり、1 回の取り消しで打った文字まで消える(実際に `edit` の漏れを
132
+ * E2E が検出した)。外にあるのは `document`(取り消し・やり直し自身)と `view`
133
+ * (文書を変えない)だけである。
134
+ */
135
+ const OWN_UNDO_STEP = new Set(['edit', 'hierarchy', 'subtree', 'convert', 'table', 'format']);
136
+ /** 読み取り専用のときは実行しない。編集可否が分からない場合は妨げない */
137
+ function whenEditable(command) {
138
+ return (state, dispatch, view) => {
139
+ if (view?.editable === false)
140
+ return false;
141
+ return command(state, dispatch, view);
142
+ };
143
+ }
144
+ function asOwnUndoStep(command) {
145
+ return (state, dispatch, view) => command(state, dispatch && ((tr) => dispatch(closeHistory(tr))), view);
146
+ }
147
+ /** 自動変換も独立した取り消し単位にする */
148
+ function ruleAsOwnUndoStep(rule) {
149
+ const handler = rule.handler;
150
+ return new InputRule(rule.match, (state, match, start, end) => {
151
+ const tr = handler(state, match, start, end);
152
+ return tr ? closeHistory(tr) : null;
153
+ });
154
+ }
155
+ /**
156
+ * IME で変換が確定していない間は、ショートカット・階層操作・既定の Enter 処理を実行しない。
157
+ * `true` を返して既定動作も止める(変換確定の Enter で段落が割れる事故を防ぐ)。
158
+ */
159
+ function withoutComposition(command) {
160
+ return (state, dispatch, view) => {
161
+ if (view?.composing)
162
+ return true;
163
+ return command(state, dispatch, view);
164
+ };
165
+ }
@@ -0,0 +1,2 @@
1
+ import type { EditorAction } from '../operations.types';
2
+ export declare const coreActions: readonly EditorAction[];