@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,52 @@
1
+ import type { EditorState } from 'prosemirror-state';
2
+ import type { EditorView } from 'prosemirror-view';
3
+ import type { EditorAction, MenuGroup, ShortcutCategory } from '../operations.types';
4
+ /**
5
+ * 宣言と現在の状態から、コンテキストメニューへ並べる項目を導く層。
6
+ *
7
+ * **文言も座標も知らない。** 返すのは操作の並びであり、説明文とキー表記の解決は
8
+ * 描画側が行う(`ShortcutHelpDialog` と同じ作法)。
9
+ *
10
+ * メニュー専用の一覧を持たないため、操作が増えれば宣言 1 か所を足すだけで
11
+ * メニュー・キー・ショートカット一覧の 3 つが揃う。
12
+ */
13
+ /**
14
+ * 分類の並び順。**ショートカット一覧とメニューが同じ表を使う。**
15
+ * 2 つ持つと、同じ操作が場所によって違う順で並ぶ。
16
+ *
17
+ * **汎用性が高いものから並べる。** どのブロックにも効くもの(ブロックの操作・書式)が先で、
18
+ * 効く場所が限られるもの(表)、画面の話(表示)、文書全体(取り消し)と続く。
19
+ *
20
+ * `hierarchy` はいま宣言している操作が無い(ブロックの操作へ寄せた)。**表から外さない** ——
21
+ * 外すと、その分類を宣言した拡張の操作が一覧にもメニューにも出なくなる。
22
+ */
23
+ export declare const SHORTCUT_CATEGORY_ORDER: readonly ShortcutCategory[];
24
+ /**
25
+ * いまの状態で並べる項目を、分類ごとにまとめて返す。
26
+ * 空の分類は含まない。1 つも無ければ空配列(呼び出し側は開かない)。
27
+ *
28
+ * 文書・選択・プラグインの状態のいずれも変えない。変換中は呼ばない
29
+ * (`view.composing` が真のとき、覆いが実行そのものを止めてしまう)。
30
+ */
31
+ export declare function menuGroupsFor(actions: readonly EditorAction[], view: EditorView): readonly MenuGroup[];
32
+ /**
33
+ * 並ぶ項目の説明を引く id。**操作の id とは限らない。**
34
+ *
35
+ * 文脈で意味が決まる操作(見出しにする/やめる、畳む/展開する)は、対象が決まっている
36
+ * この場では片方しか起きない。宣言に「いま起きる向き」を答えさせ、その id を返す。
37
+ * 向きを持たない操作は操作の id のままなので、結果に現れない。
38
+ */
39
+ export declare function describedIds(groups: readonly MenuGroup[], state: EditorState): Readonly<Record<string, string>>;
40
+ /**
41
+ * 代表のキーを**表記してよい**操作の id。
42
+ *
43
+ * 同じキーを複数の操作が分け合う文脈では、キー入力で実際に実行されるのは連鎖で先に来る
44
+ * ものだけである(表の中の `⌘⇧Backspace` は「行・列・表を削除」に当たり、「カーソル位置の
45
+ * 要素を削除」には届かない)。押せば別のことが起きるキーを表記すると、「表記されたキーを
46
+ * 押せばその項目の操作が起きる」という約束が破れる。
47
+ *
48
+ * 連鎖の順序は**宣言順**であり、分類ごとにまとめた後の並びからは読み取れない。そのため
49
+ * まとめる前の並び(`actions`)を併せて受け取る。並んでいない操作は、キーを押しても
50
+ * 実行できない(だから並んでいない)ので、数に入れない。
51
+ */
52
+ export declare function keyLabelledActions(actions: readonly EditorAction[], groups: readonly MenuGroup[]): readonly string[];
@@ -0,0 +1,114 @@
1
+ import { editorKeymap } from './assemble';
2
+ import { isMacPlatform, toBindingString } from './shortcutKeys';
3
+ /**
4
+ * 宣言と現在の状態から、コンテキストメニューへ並べる項目を導く層。
5
+ *
6
+ * **文言も座標も知らない。** 返すのは操作の並びであり、説明文とキー表記の解決は
7
+ * 描画側が行う(`ShortcutHelpDialog` と同じ作法)。
8
+ *
9
+ * メニュー専用の一覧を持たないため、操作が増えれば宣言 1 か所を足すだけで
10
+ * メニュー・キー・ショートカット一覧の 3 つが揃う。
11
+ */
12
+ /**
13
+ * 分類の並び順。**ショートカット一覧とメニューが同じ表を使う。**
14
+ * 2 つ持つと、同じ操作が場所によって違う順で並ぶ。
15
+ *
16
+ * **汎用性が高いものから並べる。** どのブロックにも効くもの(ブロックの操作・書式)が先で、
17
+ * 効く場所が限られるもの(表)、画面の話(表示)、文書全体(取り消し)と続く。
18
+ *
19
+ * `hierarchy` はいま宣言している操作が無い(ブロックの操作へ寄せた)。**表から外さない** ——
20
+ * 外すと、その分類を宣言した拡張の操作が一覧にもメニューにも出なくなる。
21
+ */
22
+ export const SHORTCUT_CATEGORY_ORDER = [
23
+ 'edit',
24
+ 'subtree',
25
+ 'convert',
26
+ 'insert',
27
+ 'hierarchy',
28
+ 'table',
29
+ 'view',
30
+ 'format',
31
+ 'document'
32
+ ];
33
+ /**
34
+ * いまの状態で並べる項目を、分類ごとにまとめて返す。
35
+ * 空の分類は含まない。1 つも無ければ空配列(呼び出し側は開かない)。
36
+ *
37
+ * 文書・選択・プラグインの状態のいずれも変えない。変換中は呼ばない
38
+ * (`view.composing` が真のとき、覆いが実行そのものを止めてしまう)。
39
+ */
40
+ export function menuGroupsFor(actions, view) {
41
+ // 働く先で先に絞る。メニューは「このアイテムに何ができるか」に答えるものであり、
42
+ // 「いま押せるキーは何か」ではない。文書全体を走る操作を試し実行せずに済む。
43
+ // 宣言が「この文脈では別の項目と同じ結果」と答えたもの(variantId が null)も出さない
44
+ const available = actions.filter((action) => action.scope === 'item' && canRun(action, view) && action.variantId?.(view.state) !== null);
45
+ return SHORTCUT_CATEGORY_ORDER.map((category) => ({
46
+ category,
47
+ actions: available.filter((action) => action.category === category)
48
+ })).filter((group) => group.actions.length > 0);
49
+ }
50
+ /**
51
+ * 並ぶ項目の説明を引く id。**操作の id とは限らない。**
52
+ *
53
+ * 文脈で意味が決まる操作(見出しにする/やめる、畳む/展開する)は、対象が決まっている
54
+ * この場では片方しか起きない。宣言に「いま起きる向き」を答えさせ、その id を返す。
55
+ * 向きを持たない操作は操作の id のままなので、結果に現れない。
56
+ */
57
+ export function describedIds(groups, state) {
58
+ const resolved = {};
59
+ for (const group of groups) {
60
+ for (const action of group.actions) {
61
+ const id = action.variantId?.(state);
62
+ if (id)
63
+ resolved[action.id] = id;
64
+ }
65
+ }
66
+ return resolved;
67
+ }
68
+ /**
69
+ * 代表のキーを**表記してよい**操作の id。
70
+ *
71
+ * 同じキーを複数の操作が分け合う文脈では、キー入力で実際に実行されるのは連鎖で先に来る
72
+ * ものだけである(表の中の `⌘⇧Backspace` は「行・列・表を削除」に当たり、「カーソル位置の
73
+ * 要素を削除」には届かない)。押せば別のことが起きるキーを表記すると、「表記されたキーを
74
+ * 押せばその項目の操作が起きる」という約束が破れる。
75
+ *
76
+ * 連鎖の順序は**宣言順**であり、分類ごとにまとめた後の並びからは読み取れない。そのため
77
+ * まとめる前の並び(`actions`)を併せて受け取る。並んでいない操作は、キーを押しても
78
+ * 実行できない(だから並んでいない)ので、数に入れない。
79
+ */
80
+ export function keyLabelledActions(actions, groups) {
81
+ const shown = groups.flatMap((group) => group.actions.map((action) => action.id));
82
+ const mac = isMacPlatform();
83
+ const owners = new Map();
84
+ for (const action of actions) {
85
+ const representative = action.keys[0];
86
+ if (!representative || !shown.includes(action.id))
87
+ continue;
88
+ const binding = toBindingString(representative, mac);
89
+ if (!owners.has(binding))
90
+ owners.set(binding, action.id);
91
+ }
92
+ return [...owners.values()];
93
+ }
94
+ /**
95
+ * 覆い付きのコマンドを**試し実行**して、いま実行できるかを見る。
96
+ *
97
+ * 戻り値では判定できない。移動・階層操作・表の操作は「動かせなくてもキーを受け止める」
98
+ * ため常に真を返す。一方 `dispatch` は実際に効くときだけ呼ばれるので、捨てる `dispatch`
99
+ * を渡せば文書を変えずに可否が分かる。
100
+ *
101
+ * 文書を変える宣言の操作は、書き込まれた変更が**実際に文書を変えた**ときだけ合とする。
102
+ * 文脈でカーソル移動に化ける操作があるためである(表の中の `Tab` は次のセルへの移動として
103
+ * 書き込む)。
104
+ *
105
+ * 覆いを先に掛けることで、読み取り専用では文書を変える操作がここで落ちる。
106
+ */
107
+ function canRun(action, view) {
108
+ let runnable = false;
109
+ editorKeymap.decorate(action)(view.state, (tr) => {
110
+ if (!action.changesDocument || tr.docChanged)
111
+ runnable = true;
112
+ }, view);
113
+ return runnable;
114
+ }
@@ -0,0 +1,42 @@
1
+ import type { EditorView } from 'prosemirror-view';
2
+ import type { EditorAction, KeyStroke } from '../operations.types';
3
+ /**
4
+ * エディタへ渡す照合用の文字列を組み立てる。
5
+ *
6
+ * 修飾キーの並べ方は `prosemirror-keymap` 側で整えられるため、ここでの順序は
7
+ * 読みやすさのためのもので、照合結果には影響しない。
8
+ */
9
+ export declare function toBindingString(stroke: KeyStroke, mac: boolean): string;
10
+ /** 画面に出す表記を組み立てる。修飾キーの表記は言語ではなく実行環境で決まる */
11
+ export declare function formatKeyStroke(stroke: KeyStroke, mac: boolean): string;
12
+ /**
13
+ * 操作を代表する 1 つのキーの表記。**キーの並びの先頭**を代表とする。
14
+ * 修飾キーの表記は言語ではなく実行環境で決まるため、表示文言とは独立して扱う。
15
+ *
16
+ * キーを 1 つも持たない操作では空文字になる(欄を空にして並びを崩さないため)。
17
+ */
18
+ export declare function formatShortcutKeys(action: EditorAction): string;
19
+ /** 操作が持つキーをすべて表記する。ショートカット一覧のように 1 操作 1 行で出す場面で使う */
20
+ export declare function formatAllShortcutKeys(action: EditorAction): readonly string[];
21
+ /** キーの並びに対する照合の 1 つ分。受け止めたら真、偽なら後段へ譲る */
22
+ export interface KeyStrokeBinding {
23
+ readonly keys: KeyStroke;
24
+ readonly run: () => boolean;
25
+ }
26
+ /**
27
+ * `KeyStroke` の並びに対する照合器を作る。**編集エリアの外で使う。**
28
+ *
29
+ * メニューは `view.dom` の外に描かれ、フォーカスもそこへ移るため、編集基盤の
30
+ * キーバインドは届かない。とはいえ規則を 2 通りにはしない —— 照合は
31
+ * `prosemirror-keymap` の `keydownHandler` に委ね、押されたキーの解読
32
+ * (修飾キーの当たり方、`Shift` を伴う記号キーの解決)を自前で書かない。
33
+ * キーの持ち方も `KeyStroke` 1 つのままで、メニューのために別の書式を作らない。
34
+ *
35
+ * 既定動作の抑止は呼び出し側が行う。
36
+ */
37
+ export declare function createKeyStrokeMatcher(bindings: readonly KeyStrokeBinding[]): (view: EditorView, event: KeyboardEvent) => boolean;
38
+ /**
39
+ * 実行環境に応じて修飾キーを選ぶ。判定はモジュール読み込み時ではなくここで行う
40
+ * (読み込みだけで `navigator` に触れないため)。
41
+ */
42
+ export declare function isMacPlatform(): boolean;
@@ -0,0 +1,114 @@
1
+ import { keydownHandler } from 'prosemirror-keymap';
2
+ import { looksLikeMac } from '../../utils/platform';
3
+ /**
4
+ * キーの組み合わせの「持ち方」を 1 か所に集める層。
5
+ *
6
+ * 割り当ての定義は文字列の書式ではなく構造(`KeyStroke`)で持ち、用途ごとに
7
+ * ここで組み立て直す。
8
+ *
9
+ * - エディタへ渡す照合用の文字列(`toBindingString`)
10
+ * - 画面に出す表記(`formatKeyStroke`)
11
+ *
12
+ * 同じ定義から作るため、一覧と実物がずれない。実行環境ごとの書き分けも
13
+ * 定義側には持たせない(書き分けると、片方だけ直す取りこぼしが起きる)。
14
+ *
15
+ * **押されたキーがどのキーなのかの解読は、ここでは行わない。**
16
+ * それは `prosemirror-keymap` が照合のために内部で行っており、同じ判断を
17
+ * 2 か所に置くと、いつの間にか食い違っても誰も気づけない。
18
+ */
19
+ /** 表示に使う修飾キーの記号。macOS の慣習に合わせる */
20
+ const MAC_SYMBOLS = { mod: '⌘', shift: '⇧', alt: '⌥' };
21
+ const OTHER_LABELS = { mod: 'Ctrl', shift: 'Shift', alt: 'Alt' };
22
+ /** 名前を持つキーの表示。押した感じが分かる記号にする */
23
+ const KEY_LABELS = {
24
+ ArrowUp: '↑',
25
+ ArrowDown: '↓',
26
+ ArrowLeft: '←',
27
+ ArrowRight: '→',
28
+ Enter: 'Enter',
29
+ Tab: 'Tab',
30
+ Escape: 'Esc',
31
+ Backspace: 'Backspace',
32
+ Delete: 'Delete',
33
+ ' ': 'Space'
34
+ };
35
+ /**
36
+ * エディタへ渡す照合用の文字列を組み立てる。
37
+ *
38
+ * 修飾キーの並べ方は `prosemirror-keymap` 側で整えられるため、ここでの順序は
39
+ * 読みやすさのためのもので、照合結果には影響しない。
40
+ */
41
+ export function toBindingString(stroke, mac) {
42
+ const parts = [];
43
+ if (stroke.alt)
44
+ parts.push('Alt');
45
+ if (stroke.mod)
46
+ parts.push(mac ? 'Cmd' : 'Ctrl');
47
+ if (stroke.shift)
48
+ parts.push('Shift');
49
+ parts.push(stroke.key);
50
+ return parts.join('-');
51
+ }
52
+ /** 画面に出す表記を組み立てる。修飾キーの表記は言語ではなく実行環境で決まる */
53
+ export function formatKeyStroke(stroke, mac) {
54
+ const symbols = mac ? MAC_SYMBOLS : OTHER_LABELS;
55
+ const separator = mac ? '' : '+';
56
+ const parts = [];
57
+ if (stroke.alt)
58
+ parts.push(symbols.alt);
59
+ if (stroke.mod)
60
+ parts.push(symbols.mod);
61
+ if (stroke.shift)
62
+ parts.push(symbols.shift);
63
+ parts.push(labelOf(stroke.key));
64
+ return parts.join(separator);
65
+ }
66
+ function labelOf(key) {
67
+ if (KEY_LABELS[key])
68
+ return KEY_LABELS[key];
69
+ // 英字は大文字で見せる(`⌘b` より `⌘B` の方が読み取りやすい)
70
+ return key.length === 1 ? key.toUpperCase() : key;
71
+ }
72
+ /**
73
+ * 操作を代表する 1 つのキーの表記。**キーの並びの先頭**を代表とする。
74
+ * 修飾キーの表記は言語ではなく実行環境で決まるため、表示文言とは独立して扱う。
75
+ *
76
+ * キーを 1 つも持たない操作では空文字になる(欄を空にして並びを崩さないため)。
77
+ */
78
+ export function formatShortcutKeys(action) {
79
+ const representative = action.keys[0];
80
+ return representative ? formatKeyStroke(representative, isMacPlatform()) : '';
81
+ }
82
+ /** 操作が持つキーをすべて表記する。ショートカット一覧のように 1 操作 1 行で出す場面で使う */
83
+ export function formatAllShortcutKeys(action) {
84
+ const mac = isMacPlatform();
85
+ return action.keys.map((keys) => formatKeyStroke(keys, mac));
86
+ }
87
+ /**
88
+ * `KeyStroke` の並びに対する照合器を作る。**編集エリアの外で使う。**
89
+ *
90
+ * メニューは `view.dom` の外に描かれ、フォーカスもそこへ移るため、編集基盤の
91
+ * キーバインドは届かない。とはいえ規則を 2 通りにはしない —— 照合は
92
+ * `prosemirror-keymap` の `keydownHandler` に委ね、押されたキーの解読
93
+ * (修飾キーの当たり方、`Shift` を伴う記号キーの解決)を自前で書かない。
94
+ * キーの持ち方も `KeyStroke` 1 つのままで、メニューのために別の書式を作らない。
95
+ *
96
+ * 既定動作の抑止は呼び出し側が行う。
97
+ */
98
+ export function createKeyStrokeMatcher(bindings) {
99
+ const mac = isMacPlatform();
100
+ const handler = keydownHandler(Object.fromEntries(bindings.map((binding) => [toBindingString(binding.keys, mac), () => binding.run()])));
101
+ return (view, event) => handler(view, event);
102
+ }
103
+ /**
104
+ * 実行環境に応じて修飾キーを選ぶ。判定はモジュール読み込み時ではなくここで行う
105
+ * (読み込みだけで `navigator` に触れないため)。
106
+ */
107
+ export function isMacPlatform() {
108
+ if (typeof navigator === 'undefined')
109
+ return false;
110
+ const platform = navigator.userAgentData?.platform ??
111
+ navigator.platform ??
112
+ '';
113
+ return looksLikeMac(platform);
114
+ }
@@ -0,0 +1,30 @@
1
+ import type { EditorAction } from '../operations.types';
2
+ import type { FocusPluginApi } from '../plugins/focus';
3
+ /**
4
+ * 表示操作の宣言。
5
+ *
6
+ * すべて `view` 分類にする。この分類だけが編集可否の判定より前に配られ、
7
+ * 読み取り専用の編集エリアでも働く。
8
+ *
9
+ * 説明文は持たない。表示文言は `EditorMessages.shortcuts[id]` から引く(基盤の規約)。
10
+ * ショートカット一覧はこの定義から作るため、一覧と実物が食い違わない。
11
+ *
12
+ * すべて一覧に出し、文書を変えないものとして宣言する。表示状態の変更は文書の内容を
13
+ * 変えないため、読み取り専用でも塞がない。
14
+ *
15
+ * フォーカスだけが状態を持つため引数に取る。折りたたみと見出しジャンプは状態を持たない。
16
+ * 一覧の開閉も、画面の状態を持つ側から手続きを受け取る。ここに置くのは、
17
+ * **キーの定義を 1 か所に集める**ためである(押されたキーの解読を自前で書かずに済む)。
18
+ */
19
+ export declare function createViewActions(focus: FocusPluginApi, toggleShortcutHelp: () => boolean): readonly EditorAction[];
20
+ /**
21
+ * コンテキストメニューを開く割り当て。**宣言はここに置き、状態はコントローラが持つ。**
22
+ *
23
+ * `view` 分類にするのは、この分類だけが編集可否の判定より前に配られ、読み取り専用の
24
+ * 編集エリアでも働くためである。働く先は文書全体であり、
25
+ * 「このアイテムに何ができるか」への答えではないので、メニュー自身には出ない。
26
+ *
27
+ * 別のコントローラが使う割り当てでも、宣言を `createViewActions` と同じファイルへ置く。
28
+ * キーの定義が散らばると、重複の検査が届かない場所ができる。
29
+ */
30
+ export declare function createContextMenuShortcut(toggleContextMenu: () => boolean): readonly EditorAction[];
@@ -0,0 +1,182 @@
1
+ import { targetSection } from '../commands/section';
2
+ import { foldCommands } from '../commands/sectionFold';
3
+ import { headingJump } from '../commands/sectionJump';
4
+ import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
5
+ /**
6
+ * 表示操作の宣言。
7
+ *
8
+ * すべて `view` 分類にする。この分類だけが編集可否の判定より前に配られ、
9
+ * 読み取り専用の編集エリアでも働く。
10
+ *
11
+ * 説明文は持たない。表示文言は `EditorMessages.shortcuts[id]` から引く(基盤の規約)。
12
+ * ショートカット一覧はこの定義から作るため、一覧と実物が食い違わない。
13
+ *
14
+ * すべて一覧に出し、文書を変えないものとして宣言する。表示状態の変更は文書の内容を
15
+ * 変えないため、読み取り専用でも塞がない。
16
+ *
17
+ * フォーカスだけが状態を持つため引数に取る。折りたたみと見出しジャンプは状態を持たない。
18
+ * 一覧の開閉も、画面の状態を持つ側から手続きを受け取る。ここに置くのは、
19
+ * **キーの定義を 1 か所に集める**ためである(押されたキーの解読を自前で書かずに済む)。
20
+ */
21
+ export function createViewActions(focus, toggleShortcutHelp) {
22
+ return [
23
+ {
24
+ id: 'view.toggleFold',
25
+ category: 'view',
26
+ scope: 'item',
27
+ listed: true,
28
+ changesDocument: false,
29
+ keys: [{ key: '.', mod: true }],
30
+ command: foldCommands.toggleFold,
31
+ variantId: (state) => foldCommands.isFolded(state) ? 'view.toggleFold.unfold' : 'view.toggleFold.fold'
32
+ },
33
+ // `,` は `<`、`.` は `>` の位置にあり、閉じる・開くの向きとして読める
34
+ {
35
+ id: 'view.foldAll',
36
+ category: 'view',
37
+ scope: 'document',
38
+ listed: true,
39
+ changesDocument: false,
40
+ keys: [{ key: ',', mod: true, alt: true }],
41
+ command: foldCommands.foldAll
42
+ },
43
+ {
44
+ id: 'view.unfoldAll',
45
+ category: 'view',
46
+ scope: 'document',
47
+ listed: true,
48
+ changesDocument: false,
49
+ keys: [{ key: '.', mod: true, alt: true }],
50
+ command: foldCommands.unfoldAll
51
+ },
52
+ // 段落へ戻す操作(⌘⌥0)の隣に、骨組みを見渡す高さを並べる
53
+ ...levels().map((level) => ({
54
+ id: `view.foldToLevel${level}`,
55
+ category: 'view',
56
+ scope: 'document',
57
+ listed: true,
58
+ changesDocument: false,
59
+ keys: [{ key: String(level), mod: true, alt: true }],
60
+ command: foldCommands.foldToLevel(level)
61
+ })),
62
+ // セクションを動かす(⌘↑↓)に対して、カーソルだけが動く移動を ⌥ 付きで並べる
63
+ {
64
+ id: 'view.jumpPrevHeading',
65
+ category: 'view',
66
+ scope: 'document',
67
+ listed: true,
68
+ changesDocument: false,
69
+ keys: [{ key: 'ArrowUp', mod: true, alt: true }],
70
+ command: headingJump.jumpToHeading('prev')
71
+ },
72
+ {
73
+ id: 'view.jumpNextHeading',
74
+ category: 'view',
75
+ scope: 'document',
76
+ listed: true,
77
+ changesDocument: false,
78
+ keys: [{ key: 'ArrowDown', mod: true, alt: true }],
79
+ command: headingJump.jumpToHeading('next')
80
+ },
81
+ {
82
+ id: 'view.jumpPrevSameLevel',
83
+ category: 'view',
84
+ scope: 'document',
85
+ listed: true,
86
+ changesDocument: false,
87
+ keys: [{ key: 'ArrowUp', mod: true, alt: true, shift: true }],
88
+ command: headingJump.jumpToHeading('prevSameLevel')
89
+ },
90
+ {
91
+ id: 'view.jumpNextSameLevel',
92
+ category: 'view',
93
+ scope: 'document',
94
+ listed: true,
95
+ changesDocument: false,
96
+ keys: [{ key: 'ArrowDown', mod: true, alt: true, shift: true }],
97
+ command: headingJump.jumpToHeading('nextSameLevel')
98
+ },
99
+ /**
100
+ * **キーを持たない操作。** 掘り下げ・解除はコンテキストメニューからのみ実行する。
101
+ *
102
+ * 左右の向きで表す ⌘⌥← / ⌘⌥→ を割り当てていたが、macOS のブラウザ
103
+ * (Chrome / Safari / Firefox)はこれをタブ切り替えに使っており、ページ側の
104
+ * JavaScript では奪い返せない。押しても何も起きないキーを一覧に載せ続けるより、
105
+ * 持たないことを宣言する(`keys` は 0 個を許す)。
106
+ */
107
+ {
108
+ id: 'view.focusSection',
109
+ category: 'view',
110
+ scope: 'item',
111
+ listed: true,
112
+ changesDocument: false,
113
+ keys: [],
114
+ command: focus.focusSection,
115
+ /**
116
+ * フォーカスはセクション単位の操作。段落や項目の中から出すと、対象
117
+ * (どのセクションか)が画面から読み取れない。見出しの上でだけ出す。
118
+ * キーは従来どおりどこからでも働く。
119
+ */
120
+ variantId: (state) => (targetSection(state) ? 'view.focusSection' : null)
121
+ },
122
+ // 同じ理由でキーを持たない。解除はパン屑の「全体を表示」から行う
123
+ {
124
+ id: 'view.clearFocus',
125
+ category: 'view',
126
+ scope: 'document',
127
+ listed: true,
128
+ changesDocument: false,
129
+ keys: [],
130
+ command: focus.clearFocus
131
+ },
132
+ /**
133
+ * 一覧そのものも同じ定義から出す。ここに無いキーが画面に出ることはない。
134
+ *
135
+ * キーは `Mod-Alt-/`。**打ちやすい `Mod-/` はコンテキストメニューに譲った**
136
+ * (一覧はたまに引く辞書、メニューは対象ごとに何度も出す。頻度が釣り合わない)。
137
+ * 移した先は、一覧を開けば一覧自身の項目として確認できる。
138
+ *
139
+ * 修飾キーに `Shift` ではなく `Alt` を選ぶのは、**`Mod-Shift-/`(=`⌘?`)を
140
+ * macOS が「ヘルプメニューを表示」として全アプリで押さえている**ためである。
141
+ * メニューバーがページより先にキーを取るので、エディタの割り当ては呼ばれない。
142
+ * 一覧はキー以外に開く道を持たない(アイテムに働く操作ではないためメニューに
143
+ * 出ない)ので、届かないキーを置くと到達手段が無くなる。
144
+ */
145
+ {
146
+ id: 'view.shortcutHelp',
147
+ category: 'view',
148
+ scope: 'document',
149
+ listed: true,
150
+ changesDocument: false,
151
+ keys: [{ key: '/', mod: true, alt: true }],
152
+ command: () => toggleShortcutHelp()
153
+ }
154
+ ];
155
+ }
156
+ /**
157
+ * コンテキストメニューを開く割り当て。**宣言はここに置き、状態はコントローラが持つ。**
158
+ *
159
+ * `view` 分類にするのは、この分類だけが編集可否の判定より前に配られ、読み取り専用の
160
+ * 編集エリアでも働くためである。働く先は文書全体であり、
161
+ * 「このアイテムに何ができるか」への答えではないので、メニュー自身には出ない。
162
+ *
163
+ * 別のコントローラが使う割り当てでも、宣言を `createViewActions` と同じファイルへ置く。
164
+ * キーの定義が散らばると、重複の検査が届かない場所ができる。
165
+ */
166
+ export function createContextMenuShortcut(toggleContextMenu) {
167
+ return [
168
+ {
169
+ id: 'view.contextMenu',
170
+ category: 'view',
171
+ scope: 'document',
172
+ listed: true,
173
+ changesDocument: false,
174
+ keys: [{ key: '/', mod: true }],
175
+ // 偽を返したときはキーを受け止めない(使わない設定のとき、後段の処理へ譲る)
176
+ command: () => toggleContextMenu()
177
+ }
178
+ ];
179
+ }
180
+ function levels() {
181
+ return Array.from({ length: MAX_HEADING_LEVEL }, (_, index) => (index + 1));
182
+ }
@@ -0,0 +1,121 @@
1
+ import type { Node } from 'prosemirror-model';
2
+ import type { EditorState, Transaction } from 'prosemirror-state';
3
+ import type { EditorView } from 'prosemirror-view';
4
+ import type { EditorShortcutCategory } from '../messages/messages.types';
5
+ export type EditorCommand = (state: EditorState, dispatch?: (tr: Transaction) => void, view?: EditorView) => boolean;
6
+ /**
7
+ * 分類は表示文言と対になるため、公開側(`messages`)の定義を唯一の出どころにする。
8
+ * 公開する型が内部の型を参照しないよう、依存の向きは内部 → 公開の一方向にする。
9
+ */
10
+ export type ShortcutCategory = EditorShortcutCategory;
11
+ /**
12
+ * キーの組み合わせ。文字列の書式ではなく構造で持つ。
13
+ *
14
+ * 照合用の文字列・画面の表記・押されたキーからの生成を、すべてこの 1 つから
15
+ * 組み立てる(`shortcutKeys.ts`)。書式で持つと、用途ごとに書き分けが増え、
16
+ * 一覧と実物がずれる余地ができる。
17
+ */
18
+ export interface KeyStroke {
19
+ /**
20
+ * 修飾キーを外した状態のキー。刻印どおりの 1 文字か、名前を持つキーの名前。
21
+ * 例: `'d'` / `'8'` / `']'` / `'ArrowUp'` / `'Enter'` / `'Tab'`
22
+ */
23
+ readonly key: string;
24
+ /** 実行環境の主修飾キー。macOS は Command、その他は Control */
25
+ readonly mod?: boolean;
26
+ readonly shift?: boolean;
27
+ readonly alt?: boolean;
28
+ }
29
+ /**
30
+ * 操作の働く先。**省略できない。**
31
+ *
32
+ * コンテキストメニューへ出すかどうかはここから導かれ、メニュー専用の宣言を別に持たない。
33
+ * 「一覧に出すか」(`listed`)とは**別の軸**である。一覧は全操作を並べる辞書、
34
+ * メニューは「このアイテムに何ができるか」への答えであり、働く先が入力の振る舞いでも
35
+ * 一覧には出すもの(`editing.escapeTab`)が実在する。
36
+ */
37
+ export type ActionScope =
38
+ /** いま操作の対象になっているアイテムと、その文脈(表の行・列を含む) */
39
+ 'item'
40
+ /** 文字列の選択・カーソル位置の文字(書式のマーク) */
41
+ | 'selection'
42
+ /** 文書・画面の全体(取り消し、全折りたたみ、ジャンプ、一覧・メニューの開閉) */
43
+ | 'document'
44
+ /** 文字入力・カーソル移動の振る舞い(Enter の分割、Backspace の結合、Tab の退避、セル移動) */
45
+ | 'input';
46
+ /**
47
+ * 操作 1 つ分の宣言。**キー入力を受け取る経路はこの形だけを通る。**
48
+ *
49
+ * 軸は**キーではなく操作**である。キーは操作の属性であり、0 個以上の並びとして持つ。
50
+ * キーバインド・ショートカット一覧・コンテキストメニューはすべてこの 1 つから導出される。
51
+ *
52
+ * 直接キーとコマンドを書く道を残すと、変換中の抑止・読み取り専用の遮断・取り消し単位が
53
+ * 掛からない割り当てができる。宣言はどれも**省略できない**。省略できる形にすると
54
+ * 「書かなければ既定値が推測される」が残り、推測をやめるという目的が達成されない。
55
+ */
56
+ export interface EditorAction {
57
+ readonly id: string;
58
+ readonly category: ShortcutCategory;
59
+ /** 働く先。コンテキストメニューへ出るのは `'item'` だけ */
60
+ readonly scope: ActionScope;
61
+ /**
62
+ * 0 個以上。宣言順にキーへ割り当てられ、**先頭が代表**(1 つだけ出す場面で使う)。
63
+ * 実行環境ごとの書き分けは持たない。`mod` が Command と Control を吸収する
64
+ */
65
+ readonly keys: readonly KeyStroke[];
66
+ /** 表示文言は持たない。説明は `EditorMessages.shortcuts[id]` から引く */
67
+ readonly command: EditorCommand;
68
+ /**
69
+ * 文脈で意味が決まる操作の、**この場で実際に起きること**を指す id。
70
+ *
71
+ * ショートカット一覧は起きうることを全部載せる辞書なので `id` の説明のままにし、
72
+ * 対象が決まっているコンテキストメニューだけがこちらを引く(「見出しにする/見出しをやめる」の
73
+ * ように両方見せても、片方は決して起きない)。
74
+ *
75
+ * **`null` は「この文脈では独立した項目として出さない」**。実行はできるが、
76
+ * 別の宣言と同じ結果になる文脈で使う(段落での `Tab` は「箇条書きにする」と
77
+ * 同じ結果になる。同じ結果の項目を 2 つ並べない)。
78
+ *
79
+ * 文脈で意味が変わらない操作は宣言しない。文言そのものではなく id を返すのは、
80
+ * 宣言の層が文言を知らないままでいるためである。
81
+ */
82
+ readonly variantId?: (state: EditorState) => string | null;
83
+ /**
84
+ * ショートカット一覧に出すか。ふるまいは何も決めない、表示だけの宣言である。
85
+ * 真のとき `EditorMessages.shortcuts[id]` に説明文が要る。
86
+ */
87
+ readonly listed: boolean;
88
+ /** 文書を変える操作か。偽なら読み取り専用でも通す */
89
+ readonly changesDocument: boolean;
90
+ }
91
+ /** コンテキストメニューへ並べる項目の 1 セクション。分類の見出しと、その分類に出す操作 */
92
+ export interface MenuGroup {
93
+ readonly category: ShortcutCategory;
94
+ readonly actions: readonly EditorAction[];
95
+ }
96
+ /**
97
+ * 移動・複製・削除・階層操作の対象。
98
+ *
99
+ * `kind` は**対象が何であるか**だけを表し、どこにカーソルがあったかは表さない。
100
+ * 操作の側は種別ごとに実行の仕方だけを変え、対象の決め直しをしない。
101
+ */
102
+ export type ElementTargetKind = 'selectedBlock' | 'section' | 'listItem' | 'block';
103
+ export interface ElementTarget {
104
+ readonly kind: ElementTargetKind;
105
+ readonly node: Node;
106
+ readonly pos: number;
107
+ }
108
+ /** 指している要選択ブロック、またはカーソルを包む要選択ブロックの位置と実体 */
109
+ export interface SelectedBlock {
110
+ readonly node: Node;
111
+ readonly pos: number;
112
+ }
113
+ export type MoveDirection = 'up' | 'down';
114
+ /** 削除の対象。選択の形から決まる(広い対象から順に、表 → 列 → 行と判定する) */
115
+ export type TableDeleteTarget = 'table' | 'column' | 'row';
116
+ /** 列の配置を動かす向き。「指定なし ← 左 ← 中央 ← 右」の線上を 1 段ずつ動く */
117
+ export type AlignDirection = 'leftward' | 'rightward';
118
+ /** 列を動かす向き。行を動かす向きは `MoveDirection` と同じもので足りる */
119
+ export type ColumnDirection = 'left' | 'right';
120
+ /** 表の中で選択を広げる向き。上下は行、左右は列をたどる */
121
+ export type CellDirection = MoveDirection | ColumnDirection;
@@ -0,0 +1 @@
1
+ export {};