@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,36 @@
1
+ import type { Node } from 'prosemirror-model';
2
+ import { type EditorState } from 'prosemirror-state';
3
+ import { type HeadingLevel, type SectionPath } from '../../models/document/document.types';
4
+ import type { EditorCommand } from '../operations.types';
5
+ export interface FoldCommands {
6
+ /** カーソル位置の見出しまたは箇条書きの項目の折りたたみを反転する */
7
+ readonly toggleFold: EditorCommand;
8
+ /**
9
+ * カーソル位置のもっとも内側の対象が畳まれているか。対象が無ければ偽。
10
+ * 文言を「畳む」と「展開する」に決めるための問い合わせであり、状態を変えない
11
+ */
12
+ readonly isFolded: (state: EditorState) => boolean;
13
+ /**
14
+ * 渡された位置を包む、もっとも内側の対象の折りたたみを反転する。
15
+ * クリック操作の入口であり、**カーソル位置は見ない**
16
+ */
17
+ readonly toggleFoldAt: (pos: number) => EditorCommand;
18
+ readonly foldAll: EditorCommand;
19
+ readonly unfoldAll: EditorCommand;
20
+ /** 指定レベルより深い見出しを畳み、それ以下を展開する。箇条書きには触れない */
21
+ readonly foldToLevel: (level: HeadingLevel) => EditorCommand;
22
+ /**
23
+ * 保存された表示状態の復元に使う。指定された位置の見出しを畳み、それ以外を展開する。
24
+ * **箇条書きには触れない。** 外部へ出さないものは、戻すときにも触らない
25
+ */
26
+ readonly applyFoldedPaths: (paths: readonly SectionPath[]) => EditorCommand;
27
+ }
28
+ export declare const foldCommands: FoldCommands;
29
+ /**
30
+ * 位置を隠している折りたたみ済みのサブツリーの開始位置を、外側から順に返す。
31
+ *
32
+ * 折りたたまれていても**先頭の子は見えたまま**であり、見出しの上にカーソルを置くのは
33
+ * 正しい状態である。したがって「折りたたまれた中にいる」ではなく
34
+ * 「隠れる範囲にいる」で判定する。
35
+ */
36
+ export declare function hidingAncestors(doc: Node, pos: number): readonly number[];
@@ -0,0 +1,219 @@
1
+ import { isListNode } from 'prosemirror-flat-list';
2
+ import { TextSelection } from 'prosemirror-state';
3
+ import { hasFoldableChildren } from '../../models/document/foldable';
4
+ import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
5
+ import { outlineSchema } from '../../models/document/schema';
6
+ import { sectionTree } from '../../models/document/sectionTree';
7
+ import { markOrigin } from '../transactionOrigin';
8
+ const SECTION = outlineSchema.nodes.section;
9
+ export const foldCommands = {
10
+ toggleFold,
11
+ isFolded,
12
+ toggleFoldAt,
13
+ foldAll,
14
+ unfoldAll,
15
+ foldToLevel,
16
+ applyFoldedPaths
17
+ };
18
+ function isFolded(state) {
19
+ return foldTargetAt(state.doc, state.selection.from)?.node.attrs.collapsed === true;
20
+ }
21
+ /**
22
+ * カーソル位置のサブツリーを畳む・展開する。
23
+ *
24
+ * 変えるのはノードの属性だけであり、文書の内容は動かない。したがって書き出される
25
+ * マークダウンは変わらず、利用者への変更通知も飛ばない(1.19)。
26
+ */
27
+ function toggleFold(state, dispatch) {
28
+ return toggle(state, dispatch, foldTargetAt(state.doc, state.selection.from));
29
+ }
30
+ /**
31
+ * 位置を指定して畳む・展開する。クリックの入口であり、**カーソル位置は見ない**
32
+ *。
33
+ *
34
+ * 成立の条件も、履歴に積まないことも `toggleFold` と同じである。押した当人を対象に
35
+ * するかカーソルの位置を対象にするかだけが違う。
36
+ */
37
+ function toggleFoldAt(pos) {
38
+ return (state, dispatch) => toggle(state, dispatch, foldTargetAt(state.doc, pos));
39
+ }
40
+ function toggle(state, dispatch, target) {
41
+ // 子を持たない対象は畳めない。畳んでも隠れるものがない
42
+ if (!target || !hasFoldableChildren(target.node))
43
+ return false;
44
+ return applyChanges(state, dispatch, [
45
+ { pos: target.pos, collapsed: !target.node.attrs.collapsed }
46
+ ]);
47
+ }
48
+ function foldAll(state, dispatch) {
49
+ return applyChanges(state, dispatch, allFoldChanges(state.doc, true));
50
+ }
51
+ function unfoldAll(state, dispatch) {
52
+ return applyChanges(state, dispatch, allFoldChanges(state.doc, false));
53
+ }
54
+ /**
55
+ * 骨組みを見渡す高さを 1 つの操作で決める。
56
+ *
57
+ * 箇条書きには触れない。指定するのは見出しの深さであり、項目には対応する深さがない。
58
+ */
59
+ function foldToLevel(level) {
60
+ return (state, dispatch) => {
61
+ const changes = [];
62
+ forEachSection(state.doc, (node, pos, sectionLevel) => {
63
+ const collapsed = sectionLevel > level;
64
+ if (node.attrs.collapsed === collapsed)
65
+ return;
66
+ if (collapsed && !hasFoldableChildren(node))
67
+ return;
68
+ changes.push({ pos, collapsed });
69
+ });
70
+ return applyChanges(state, dispatch, changes);
71
+ };
72
+ }
73
+ /**
74
+ * 保存しておいた折りたたみを当て直す。
75
+ *
76
+ * **箇条書きの `collapsed` には触れない。** 箇条書きは外部化の対象ではない
77
+ *ため、ここで畳み直す材料もなく、触れば利用者が
78
+ * `viewState` を再設定するたびに書き手が畳んだ項目まで巻き添えで消える。
79
+ */
80
+ function applyFoldedPaths(paths) {
81
+ return (state, dispatch) => {
82
+ const wanted = new Set();
83
+ for (const path of paths) {
84
+ const ref = sectionTree.resolveByPath(state.doc, path);
85
+ if (ref)
86
+ wanted.add(ref.pos);
87
+ }
88
+ const changes = [];
89
+ forEachSection(state.doc, (node, pos) => {
90
+ const collapsed = wanted.has(pos);
91
+ if (node.attrs.collapsed === collapsed)
92
+ return;
93
+ if (collapsed && !hasFoldableChildren(node))
94
+ return;
95
+ changes.push({ pos, collapsed });
96
+ });
97
+ return applyChanges(state, dispatch, changes);
98
+ };
99
+ }
100
+ // --- 対象の選び方 ---
101
+ /**
102
+ * その位置で折りたたみの対象になるノード。**もっとも内側にあるものを選ぶ。**
103
+ *
104
+ * 見出しを持たないセクションは対象にしない。その中の箇条書きは
105
+ * 通常どおり見つかる。
106
+ */
107
+ function foldTargetAt(doc, pos) {
108
+ const $pos = doc.resolve(Math.max(0, Math.min(pos, doc.content.size)));
109
+ for (let depth = $pos.depth; depth >= 1; depth -= 1) {
110
+ const node = $pos.node(depth);
111
+ if (isFoldable(node))
112
+ return { pos: $pos.before(depth), node };
113
+ }
114
+ return null;
115
+ }
116
+ function allFoldChanges(doc, collapsed) {
117
+ const changes = [];
118
+ doc.descendants((node, pos) => {
119
+ if (!isFoldable(node))
120
+ return true;
121
+ if (node.attrs.collapsed === collapsed)
122
+ return true;
123
+ if (collapsed && !hasFoldableChildren(node))
124
+ return true;
125
+ changes.push({ pos, collapsed });
126
+ return true;
127
+ });
128
+ return changes;
129
+ }
130
+ /**
131
+ * セクションだけを辿る。ブロックの中へは降りない。
132
+ * 費用を見出しの数に留めるためであり、文書の行数とは独立させる。
133
+ */
134
+ function forEachSection(doc, visit) {
135
+ const walk = (parent, parentPos, depth) => {
136
+ let pos = parentPos + 1;
137
+ parent.forEach((child) => {
138
+ if (child.type === SECTION) {
139
+ visit(child, pos, clampLevel(depth + 1));
140
+ walk(child, pos, depth + 1);
141
+ }
142
+ pos += child.nodeSize;
143
+ });
144
+ };
145
+ walk(doc, -1, 0);
146
+ }
147
+ function isFoldable(node) {
148
+ return node.type === SECTION || isListNode(node);
149
+ }
150
+ function clampLevel(depth) {
151
+ return Math.min(Math.max(depth, 1), MAX_HEADING_LEVEL);
152
+ }
153
+ // --- 変更の当て方 ---
154
+ /**
155
+ * 対象がいくつあっても 1 つの Transaction にまとめる。
156
+ * 個別に投げると、大きな文書で反映が 100ms を超える。
157
+ *
158
+ * 属性の変更は位置をずらさないため、元の文書から集めた位置をそのまま使える。
159
+ */
160
+ function applyChanges(state, dispatch, changes) {
161
+ if (changes.length === 0)
162
+ return false;
163
+ if (!dispatch)
164
+ return true;
165
+ const tr = state.tr;
166
+ for (const change of changes)
167
+ tr.setNodeAttribute(change.pos, 'collapsed', change.collapsed);
168
+ pullSelectionIntoView(tr);
169
+ dispatch(asFoldTransaction(tr));
170
+ return true;
171
+ }
172
+ /**
173
+ * 畳んだ結果、カーソルが見えない位置に取り残されないようにする。
174
+ *
175
+ * 戻す先は**もっとも外側で隠しているサブツリー**の行(見出し・項目の本文)の**末尾**
176
+ * とする。内側へ移してもそこはまだ隠れており、行の先頭へ置くと、
177
+ * 畳んだ直後に打った文字が見出しの頭に入る。
178
+ */
179
+ function pullSelectionIntoView(tr) {
180
+ const hidden = hidingAncestors(tr.doc, tr.selection.from);
181
+ if (hidden.length === 0)
182
+ return;
183
+ const outermost = tr.doc.nodeAt(hidden[0]);
184
+ if (!outermost)
185
+ return;
186
+ // 先頭の子の終わり。ノードの開始位置 + 先頭の子の大きさが、その中身の末尾にあたる
187
+ const endOfLine = hidden[0] + outermost.child(0).nodeSize;
188
+ tr.setSelection(TextSelection.near(tr.doc.resolve(endOfLine), -1));
189
+ }
190
+ /**
191
+ * 位置を隠している折りたたみ済みのサブツリーの開始位置を、外側から順に返す。
192
+ *
193
+ * 折りたたまれていても**先頭の子は見えたまま**であり、見出しの上にカーソルを置くのは
194
+ * 正しい状態である。したがって「折りたたまれた中にいる」ではなく
195
+ * 「隠れる範囲にいる」で判定する。
196
+ */
197
+ export function hidingAncestors(doc, pos) {
198
+ const $pos = doc.resolve(Math.max(0, Math.min(pos, doc.content.size)));
199
+ const hiding = [];
200
+ for (let depth = 1; depth <= $pos.depth; depth += 1) {
201
+ const node = $pos.node(depth);
202
+ if (!isFoldable(node) || !node.attrs.collapsed)
203
+ continue;
204
+ const start = $pos.before(depth);
205
+ if (pos >= start + 1 + node.child(0).nodeSize)
206
+ hiding.push(start);
207
+ }
208
+ return hiding;
209
+ }
210
+ /**
211
+ * 折りたたみの Transaction に共通の印を付ける。
212
+ *
213
+ * - 取り消しの対象にしない
214
+ * - 表示だけを変える変更であることを示す。表示範囲を絞る仕組みはこれを見て素通しする
215
+ */
216
+ function asFoldTransaction(tr) {
217
+ tr.setMeta('addToHistory', false);
218
+ return markOrigin(tr, 'fold');
219
+ }
@@ -0,0 +1,8 @@
1
+ import type { SectionPath } from '../../models/document/document.types';
2
+ import type { EditorCommand } from '../operations.types';
3
+ export type JumpTarget = 'prev' | 'next' | 'prevSameLevel' | 'nextSameLevel';
4
+ export interface HeadingJump {
5
+ readonly jumpToHeading: (target: JumpTarget) => EditorCommand;
6
+ readonly jumpToPath: (path: SectionPath) => EditorCommand;
7
+ }
8
+ export declare const headingJump: HeadingJump;
@@ -0,0 +1,144 @@
1
+ import { TextSelection } from 'prosemirror-state';
2
+ import { outlineSchema } from '../../models/document/schema';
3
+ import { sectionTree } from '../../models/document/sectionTree';
4
+ const SECTION = outlineSchema.nodes.section;
5
+ export const headingJump = {
6
+ jumpToHeading,
7
+ jumpToPath
8
+ };
9
+ /**
10
+ * 前後の見出しへカーソルを移す。
11
+ *
12
+ * **見出しの並びを索引として持たない。** 目次一覧を持たない以上、常時保持する理由がなく、
13
+ * 保持すれば文書が変わるたびに作り直す費用が要る。移動先は実行のたびに、カーソル位置から
14
+ * **必要な範囲だけ**を辿って求める。
15
+ *
16
+ * 移動先が折りたたまれた範囲の中にあっても、ここでは何もしない。
17
+ * 隠れた位置に着いたことは FoldGuardPlugin が見つけて展開する。
18
+ */
19
+ function jumpToHeading(target) {
20
+ return (state, dispatch) => {
21
+ const destination = findDestination(state, target);
22
+ if (destination === null)
23
+ return false;
24
+ return placeCursor(state, dispatch, destination);
25
+ };
26
+ }
27
+ /** 位置を指定して見出しへ移る。表示状態の復元とパンくずからの移動に使う */
28
+ function jumpToPath(path) {
29
+ return (state, dispatch) => {
30
+ const ref = sectionTree.resolveByPath(state.doc, path);
31
+ if (!ref)
32
+ return false;
33
+ return placeCursor(state, dispatch, ref.pos);
34
+ };
35
+ }
36
+ /** 文書は変えない。動くのはカーソルだけである */
37
+ function placeCursor(state, dispatch, sectionPos) {
38
+ if (!dispatch)
39
+ return true;
40
+ const tr = state.tr.setSelection(TextSelection.near(state.doc.resolve(sectionPos + 1), 1));
41
+ dispatch(tr.scrollIntoView());
42
+ return true;
43
+ }
44
+ // --- 移動先の求め方 ---
45
+ function findDestination(state, target) {
46
+ const doc = state.doc;
47
+ const current = currentSectionPos(doc, state.selection.from);
48
+ const forward = target === 'next' || target === 'nextSameLevel';
49
+ const step = forward ? nextSectionPos : previousSectionPos;
50
+ let at = current === null ? (forward ? firstSectionPos(doc) : null) : step(doc, current.pos);
51
+ if (target === 'next' || target === 'prev')
52
+ return at;
53
+ // 同一レベルの移動は、深い階層を飛ばして同じ深さの見出しを探す
54
+ const level = current?.level ?? 0;
55
+ while (at !== null && levelOfSection(doc, at) !== level) {
56
+ at = step(doc, at);
57
+ }
58
+ return at;
59
+ }
60
+ /** 位置がもっとも内側で属するセクション。見出しを持たないセクションは数えない */
61
+ function currentSectionPos(doc, pos) {
62
+ const $pos = doc.resolve(Math.max(0, Math.min(pos, doc.content.size)));
63
+ for (let depth = $pos.depth; depth >= 1; depth -= 1) {
64
+ if ($pos.node(depth).type === SECTION)
65
+ return { pos: $pos.before(depth), level: depth };
66
+ }
67
+ return null;
68
+ }
69
+ function levelOfSection(doc, sectionPos) {
70
+ return doc.resolve(sectionPos).depth + 1;
71
+ }
72
+ /**
73
+ * 文書順で次のセクション。**辿るのは子・兄弟・祖先だけ**で、全体は走査しない。
74
+ */
75
+ function nextSectionPos(doc, from) {
76
+ const $from = doc.resolve(from);
77
+ const section = $from.nodeAfter;
78
+ if (!section)
79
+ return null;
80
+ const child = firstChildSectionPos(section, from);
81
+ if (child !== null)
82
+ return child;
83
+ let $at = $from;
84
+ for (;;) {
85
+ const sibling = nextSiblingSectionPos($at);
86
+ if (sibling !== null)
87
+ return sibling;
88
+ if ($at.depth === 0)
89
+ return null;
90
+ $at = doc.resolve($at.before($at.depth));
91
+ }
92
+ }
93
+ /** 文書順で前のセクション。直前の兄弟がいればその末端まで降り、いなければ親へ戻る */
94
+ function previousSectionPos(doc, from) {
95
+ const $from = doc.resolve(from);
96
+ const index = $from.index();
97
+ if (index > 0) {
98
+ const previous = $from.parent.child(index - 1);
99
+ if (previous.type === SECTION) {
100
+ return deepestLastSectionPos(previous, from - previous.nodeSize);
101
+ }
102
+ }
103
+ return $from.depth >= 1 && $from.node($from.depth).type === SECTION
104
+ ? $from.before($from.depth)
105
+ : null;
106
+ }
107
+ function firstSectionPos(doc) {
108
+ return firstChildSectionPos(doc, -1);
109
+ }
110
+ /** `parentPos` は親自身の開始位置。文書のときは -1(子は 0 から始まる) */
111
+ function firstChildSectionPos(parent, parentPos) {
112
+ let pos = parentPos + 1;
113
+ for (let i = 0; i < parent.childCount; i += 1) {
114
+ const child = parent.child(i);
115
+ if (child.type === SECTION)
116
+ return pos;
117
+ pos += child.nodeSize;
118
+ }
119
+ return null;
120
+ }
121
+ function deepestLastSectionPos(section, sectionPos) {
122
+ let node = section;
123
+ let pos = sectionPos;
124
+ for (;;) {
125
+ const last = lastChildSectionPos(node, pos);
126
+ if (last === null)
127
+ return pos;
128
+ pos = last;
129
+ node = node.child(node.childCount - 1);
130
+ }
131
+ }
132
+ function lastChildSectionPos(parent, parentPos) {
133
+ const last = parent.childCount > 0 ? parent.child(parent.childCount - 1) : null;
134
+ if (!last || last.type !== SECTION)
135
+ return null;
136
+ return parentPos + parent.nodeSize - 1 - last.nodeSize;
137
+ }
138
+ function nextSiblingSectionPos($at) {
139
+ const section = $at.nodeAfter;
140
+ if (!section)
141
+ return null;
142
+ const next = $at.parent.maybeChild($at.index() + 1);
143
+ return next?.type === SECTION ? $at.pos + section.nodeSize : null;
144
+ }
@@ -0,0 +1,37 @@
1
+ import type { Node, ResolvedPos } from 'prosemirror-model';
2
+ import { type EditorState, type Transaction } from 'prosemirror-state';
3
+ import type { EditorCommand, ElementTarget, SelectedBlock } from '../operations.types';
4
+ /**
5
+ * 要選択ブロック——**明示的に選ばないと、そのブロック自身を指せないブロック**を扱う。
6
+ *
7
+ * 表・引用・コードブロックが該当する。中にカーソルを置くと階層操作や `Tab` が中身に対して
8
+ * 働くため、カーソルの位置からブロック自身を対象にできない。段落・見出し・箇条書きの
9
+ * 項目は、カーソルの位置から対象を導けるので該当しない。
10
+ *
11
+ * 指している状態は編集基盤の `NodeSelection` そのものである。**中身がすべて選ばれて
12
+ * いることから推し量らない**。推し量る形にすると、中身を端から端までなぞった選択と
13
+ * ブロックを指した状態が区別できなくなる。
14
+ */
15
+ export interface RequiredSelectionBlockModule {
16
+ /** そのノードが要選択ブロックか(スキーマのフラグを見る) */
17
+ readonly isRequiredSelectionBlock: (node: Node) => boolean;
18
+ /** いま指している要選択ブロック。指していなければ null */
19
+ readonly selectedBlock: (state: EditorState) => SelectedBlock | null;
20
+ /** その位置の外側にある、もっとも内側の要選択ブロック。無ければ null */
21
+ readonly enclosingBlock: ($pos: ResolvedPos) => SelectedBlock | null;
22
+ /**
23
+ * 中身全体 → いるコンテナ(配下ごと)→ 文書全体、と 1 段ずつ広げる。
24
+ * 覆うのはもっとも内側のコンテナだけで、親は辿らない。**常に働く**
25
+ */
26
+ readonly expandSelection: EditorCommand;
27
+ }
28
+ export declare const requiredSelectionBlock: RequiredSelectionBlockModule;
29
+ /**
30
+ * 指している要選択ブロックを、直前の箇条書きの項目の中身の末尾へ移す(要件 5.3)。
31
+ *
32
+ * 直前にあるのが項目でなければ実行しない(要件 5.4)。段落を深くする操作は
33
+ * 「段落を項目にする」であり、ブロックを項目の中へ入れる操作とは別物である。
34
+ */
35
+ export declare function sinkSelectedBlock(target: ElementTarget, state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
36
+ /** 指している要選択ブロックを項目の外へ出し、その項目の次の兄弟にする(要件 5.5) */
37
+ export declare function liftSelectedBlock(target: ElementTarget, state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
@@ -0,0 +1,170 @@
1
+ import { AllSelection, NodeSelection, TextSelection } from 'prosemirror-state';
2
+ import { bodyStart } from '../../models/document/bodySelection';
3
+ import { outlineSchema } from '../../models/document/schema';
4
+ import { selectBlockAt } from '../transaction';
5
+ export const requiredSelectionBlock = {
6
+ isRequiredSelectionBlock,
7
+ selectedBlock,
8
+ enclosingBlock,
9
+ expandSelection
10
+ };
11
+ function isRequiredSelectionBlock(node) {
12
+ return node.type.spec.requiredSelectionBlock === true;
13
+ }
14
+ function selectedBlock(state) {
15
+ const selection = state.selection;
16
+ if (!(selection instanceof NodeSelection))
17
+ return null;
18
+ if (!isRequiredSelectionBlock(selection.node))
19
+ return null;
20
+ return { node: selection.node, pos: selection.from };
21
+ }
22
+ /**
23
+ * 選択を 1 段広げる。
24
+ *
25
+ * 中身全体 → いま入っているコンテナ(項目・セクション)→ 文書全体。**親を順に辿らない。**
26
+ * 覆いたいのはいつでも「いまいるコンテナ」であり、祖先を押した回数で数えさせない。
27
+ *
28
+ * コンテナを覆う段が要るのは、**コンテナごと消す・写す手段がここにしかない**ためである。
29
+ * 項目の配下は親を消しても残る(それ自体は意図した仕様)。配下ごと消したいときに、
30
+ * 配下まで手でなぞらせない。覆った状態は編集基盤の `NodeSelection` なので、そのまま
31
+ * `Backspace` でコンテナごと消え、写せば配下ごと写る。
32
+ *
33
+ * **何段目にいるかは選択の形から導く。** 別に控えを持つと、ポインタで選び直したときや
34
+ * 取り消しの後に、控えと実際の選択が食い違う。形から導けば、どこから来ても正しい段が
35
+ * 決まる。
36
+ *
37
+ * **要選択ブロック以外でも働く。** ここで譲ると、段落での全選択が壊れる。
38
+ */
39
+ function expandSelection(state, dispatch) {
40
+ const selection = state.selection;
41
+ if (selection instanceof AllSelection)
42
+ return false;
43
+ const next = stagesAt(state).find((stage) => extendsBeyond(stage, selection));
44
+ if (!next)
45
+ return false;
46
+ if (!dispatch)
47
+ return true;
48
+ dispatch(state.tr.setSelection(next.selection));
49
+ return true;
50
+ }
51
+ /**
52
+ * カーソルの位置から、広げていく先を内側から順に並べる。
53
+ *
54
+ * 並べるだけで、いまどの段にいるかは見ない。段を選ぶのは呼ぶ側の関心であり、
55
+ * 「どこまで広げられるか」と「いまどこにいるか」を混ぜない。
56
+ */
57
+ function stagesAt(state) {
58
+ const $from = state.selection.$from;
59
+ const stages = [];
60
+ if ($from.parent.isTextblock) {
61
+ stages.push(stageOf(TextSelection.create(state.doc, $from.start(), $from.end())));
62
+ }
63
+ // 覆う段は、もっとも内側のコンテナひとつだけ。**すでに覆っているなら親へは進まない**
64
+ // (次は文書全体になる)。これも選択の形から決まり、控えを持たない
65
+ let covering = !(state.selection instanceof NodeSelection && isCoveringContainer(state.selection.node));
66
+ for (let depth = $from.depth; depth > 0; depth -= 1) {
67
+ const node = $from.node(depth);
68
+ const pointing = isRequiredSelectionBlock(node);
69
+ if (!pointing) {
70
+ if (!covering || !isCoveringContainer(node))
71
+ continue;
72
+ // 見送るコンテナでも、いまいるコンテナには違いない。親へは進まない
73
+ covering = false;
74
+ if (!coversMoreThanContent(node))
75
+ continue;
76
+ }
77
+ const from = $from.before(depth);
78
+ // 本文と同じ範囲を覆うコンテナは段にしない。最後の段(文書全体)と同じものになる
79
+ if (from === bodyStart(state.doc) && $from.after(depth) === state.doc.content.size)
80
+ continue;
81
+ stages.push(stageOf(NodeSelection.create(state.doc, from)));
82
+ }
83
+ stages.push(stageOf(new AllSelection(state.doc)));
84
+ return stages;
85
+ }
86
+ function stageOf(selection) {
87
+ return { from: selection.from, to: selection.to, selection };
88
+ }
89
+ /**
90
+ * 配下ごと覆うコンテナか。**指す段(要選択ブロック)とは別の軸である。**
91
+ *
92
+ * 指す段は入れ子のぶんだけ経る。指した状態はそれ自体が構造操作の入口であり、外側の表・
93
+ * 引用を指せる必要がある。覆う段は「いまいるコンテナ」だけでよく、親までは辿らない。
94
+ */
95
+ function isCoveringContainer(node) {
96
+ const n = outlineSchema.nodes;
97
+ return node.type === n.list || node.type === n.section || node.type === n.intro_section;
98
+ }
99
+ /**
100
+ * 覆うと中身の段より広がるか。
101
+ *
102
+ * 中身を 1 つしか持たないコンテナは段にしない。覆っても直前の段と同じものしか覆わず、
103
+ * 同じ見た目で 2 回押させることになる(配下のない項目、見出しだけのセクション)。
104
+ */
105
+ function coversMoreThanContent(node) {
106
+ return node.childCount > 1;
107
+ }
108
+ /** その段が、いまの選択を覆ったうえで広がるか(=次の段か) */
109
+ function extendsBeyond(stage, selection) {
110
+ if (stage.from > selection.from || stage.to < selection.to)
111
+ return false;
112
+ return stage.from < selection.from || stage.to > selection.to;
113
+ }
114
+ /**
115
+ * 内側から外側へ向かって最初に見つかったものを返す。
116
+ *
117
+ * 入れ子(引用の中の表)で外側を返すと、表のセルにいるのに引用が対象になる。
118
+ * 書き手から見て「いまいるブロック」はいつでももっとも内側である。
119
+ */
120
+ function enclosingBlock($pos) {
121
+ for (let depth = $pos.depth; depth > 0; depth -= 1) {
122
+ const node = $pos.node(depth);
123
+ if (isRequiredSelectionBlock(node))
124
+ return { node, pos: $pos.before(depth) };
125
+ }
126
+ return null;
127
+ }
128
+ /**
129
+ * 指している要選択ブロックを、直前の箇条書きの項目の中身の末尾へ移す(要件 5.3)。
130
+ *
131
+ * 直前にあるのが項目でなければ実行しない(要件 5.4)。段落を深くする操作は
132
+ * 「段落を項目にする」であり、ブロックを項目の中へ入れる操作とは別物である。
133
+ */
134
+ export function sinkSelectedBlock(target, state, dispatch) {
135
+ const n = outlineSchema.nodes;
136
+ const $target = state.doc.resolve(target.pos);
137
+ const index = $target.index();
138
+ if (index === 0)
139
+ return false;
140
+ const previous = $target.parent.child(index - 1);
141
+ if (previous.type !== n.list)
142
+ return false;
143
+ if (!dispatch)
144
+ return true;
145
+ // 項目の中身の末尾。入れる位置は消す位置より手前なので、位置はずれない
146
+ const insertAt = target.pos - 1;
147
+ const tr = state.tr;
148
+ tr.delete(target.pos, target.pos + target.node.nodeSize);
149
+ tr.insert(insertAt, target.node);
150
+ selectBlockAt(tr, insertAt);
151
+ dispatch(tr.scrollIntoView());
152
+ return true;
153
+ }
154
+ /** 指している要選択ブロックを項目の外へ出し、その項目の次の兄弟にする(要件 5.5) */
155
+ export function liftSelectedBlock(target, state, dispatch) {
156
+ const $target = state.doc.resolve(target.pos);
157
+ if ($target.parent.type !== outlineSchema.nodes.list)
158
+ return false;
159
+ if (!dispatch)
160
+ return true;
161
+ const insertAt = $target.before() + $target.parent.nodeSize;
162
+ const tr = state.tr;
163
+ tr.delete(target.pos, target.pos + target.node.nodeSize);
164
+ // 出す先は消す位置より後ろにあるため、消したぶんだけ手前へ寄る
165
+ const at = tr.mapping.map(insertAt);
166
+ tr.insert(at, target.node);
167
+ selectBlockAt(tr, at);
168
+ dispatch(tr.scrollIntoView());
169
+ return true;
170
+ }
@@ -0,0 +1,64 @@
1
+ import { type EditorState, type Transaction } from 'prosemirror-state';
2
+ import type { AlignDirection, CellDirection, ColumnDirection, EditorCommand, MoveDirection, TableDeleteTarget } from '../operations.types';
3
+ /**
4
+ * 表の構造を変える操作。`prosemirror-tables` への依存をこのファイルに集約する。
5
+ *
6
+ * 既製のコマンドをそのまま割り当てない。**この spec の不変条件を守らないためである**
7
+ * (穴はそれぞれの実装に注記してある)。ここが守るのは次の 4 つ。
8
+ *
9
+ * - 表の 0 行目(見出し行)は常に残る
10
+ * - 列は 1 つ以上残る
11
+ * - すべての行の列数が等しい
12
+ * - 表の外では必ず実行しない(他の割り当てを妨げない)
13
+ *
14
+ * どのキーで呼ぶか・どう説明するかは知らない。`EditorCommand` の形だけを外へ出す。
15
+ */
16
+ export interface TableCommands {
17
+ /** 見出し行 1 行と本文行 1 行からなる 2 列の空の表を挿入する */
18
+ readonly createTable: EditorCommand;
19
+ /** カーソルのある行の上に空の行を挿入する。見出し行の上には作らない */
20
+ readonly insertRowBefore: EditorCommand;
21
+ /** カーソルのある行の下に空の行を挿入する */
22
+ readonly insertRowAfter: EditorCommand;
23
+ /** カーソルのある列の左に空の列を挿入する */
24
+ readonly insertColumnBefore: EditorCommand;
25
+ /** カーソルのある列の右に空の列を挿入する */
26
+ readonly insertColumnAfter: EditorCommand;
27
+ /** 選択の形に応じて 表・列・行 を削除する */
28
+ readonly deleteSelected: EditorCommand;
29
+ /** 行・列・表を丸ごと選んでいるときだけ、それを削除する。覆いきらない選択では譲る */
30
+ readonly deleteSelectedRowOrColumn: EditorCommand;
31
+ /** 選択している列の配置を 1 段動かす */
32
+ readonly align: (direction: AlignDirection) => EditorCommand;
33
+ /** 本文行を隣の本文行と入れ替える。見出し行と端では実行しない */
34
+ readonly moveRow: (direction: MoveDirection) => EditorCommand;
35
+ /** 列を隣の列と入れ替える。配置指定も一緒に動く。端では実行しない */
36
+ readonly moveColumn: (direction: ColumnDirection) => EditorCommand;
37
+ /** 次のセルへ移る。最後のセルなら行を足してから移る */
38
+ readonly goToNextCell: EditorCommand;
39
+ /** 前のセルへ移る。最初のセルでは何もしない */
40
+ readonly goToPreviousCell: EditorCommand;
41
+ /** 真上・真下のセルへ移る。表の端では表の外へ出る */
42
+ readonly goToCellAbove: EditorCommand;
43
+ readonly goToCellBelow: EditorCommand;
44
+ /** 選択をその向きの隣のセルまで広げる。表の端では広げない */
45
+ readonly extendSelection: (direction: CellDirection) => EditorCommand;
46
+ }
47
+ export declare const tableCommands: TableCommands;
48
+ /**
49
+ * 選択の形から削除の対象を決める。表の外では `null`。
50
+ *
51
+ * **判定の順序が仕様である。広い対象から先に見る**(表 → 列 → 行)。
52
+ * 逆にすると、表全体を選んでも行の削除として解釈される。
53
+ */
54
+ export declare function deleteTargetOf(state: EditorState): TableDeleteTarget | null;
55
+ /**
56
+ * 表の指定した位置へ空の列を挿入する(画面上の手がかりから呼ぶ)。
57
+ *
58
+ * カーソルに依存する `insertColumnBefore` / `insertColumnAfter` は使えない。
59
+ * 押した境目がそのまま挿入位置であり、**押す前にカーソルがどこにあってもよい**ため。
60
+ *
61
+ * @param tableStart 表の中身が始まる位置(表のノード位置 + 1)
62
+ * @param index 挿入する列の番号。0 なら左端、列数と同じなら右端
63
+ */
64
+ export declare function insertColumnAt(state: EditorState, dispatch: ((tr: Transaction) => void) | undefined, tableStart: number, index: number): boolean;