@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,393 @@
1
+ <script lang="ts" module>
2
+ import type { EditorLang, EditorMessagesOverride } from '../messages/messages.types';
3
+ import type {
4
+ FrontMatterFields,
5
+ FrontMatterWriteError,
6
+ FrontMatterWriteOutcome,
7
+ MarkdownParseError
8
+ } from '../models/markdown/markdown.types';
9
+ import type { Result } from '../utils/result.types';
10
+ import type { ViewState } from '../models/viewState/viewState.types';
11
+
12
+ export interface MarkdownEditorProps {
13
+ /**
14
+ * 本文の Markdown。`bind:value` で双方向に扱う。
15
+ * 内部編集はこの値を更新し、外部からの変更は差分として文書へ適用される。
16
+ * 初期化ではこの値を書き換えない。最初の内部編集以降、正規化された Markdown で
17
+ * 更新される(記法は正規化されうるため、入力と文字列一致するとは限らない)。
18
+ */
19
+ value?: string;
20
+ /**
21
+ * 文書が変わったときの任意の通知。保存・未保存フラグなど副作用の起点として使う。
22
+ * 原因(内部編集・外部からの設定・resetDocument)を問わず発火する。
23
+ * 内容が変わらなかった場合は発火しない。
24
+ * `bind:value` を使う場合、値の同期にこのハンドラは不要。
25
+ */
26
+ onChange?: (markdown: string) => void;
27
+ /** 編集エリアのアクセシブルな名前 */
28
+ label?: string;
29
+ /** 読み取り専用。true のとき入力・編集コマンド・貼り付けをすべて受け付けない */
30
+ readonly?: boolean;
31
+ /** マウント時に編集エリアへフォーカスする */
32
+ autofocus?: boolean;
33
+ /** 追加のクラス名 */
34
+ class?: string;
35
+ /**
36
+ * 編集エリアの内側の余白。**CSS の `padding` と同じ書き方**で、
37
+ * `1em` も `0 64px 16px` も書ける。未指定なら `--markdown-editor-padding` に従う。
38
+ */
39
+ padding?: string;
40
+ /**
41
+ * 最大幅の上限(CSS の長さ)。未指定なら `--markdown-editor-max-width` に従う。
42
+ *
43
+ * 狭めても**押せる面は箱の幅のまま**で、行の長さだけが収まる。置いた側のコンテナへ
44
+ * `max-width` を掛けると、その外側が「見た目は同じなのに反応しない領域」になる。
45
+ */
46
+ maxWidth?: string;
47
+ /**
48
+ * 同梱の言語を選ぶ。既定は 'en'。
49
+ * 編集エリアの言語としても使われ、支援技術が正しい発音で読む助けになる。
50
+ */
51
+ lang?: EditorLang;
52
+ /**
53
+ * 文字列の部分的な差し替え。`lang` で選んだ文字列の上に重ねる。
54
+ * 同梱していない言語を使う場合は、ここに一式を渡す。
55
+ */
56
+ messages?: EditorMessagesOverride;
57
+ /**
58
+ * 表示状態(見出しの折りたたみ・フォーカス)。`bind:viewState` で双方向に扱う。
59
+ * 本文(`value`)と同じ作法であり、内部の変更はこの値を更新し、外部からの設定は
60
+ * そのまま表示へ反映される。未指定なら全展開・フォーカスなしで始まる。
61
+ * 現在の文書構造に対応しない項目は破棄され、残りが適用される。
62
+ * リスト項目の折りたたみは含まれない。
63
+ */
64
+ viewState?: ViewState;
65
+ /**
66
+ * 表示状態が変わったときの任意の通知。保存などの副作用の起点として使う。
67
+ * `bind:viewState` を使う場合、値の同期にこのハンドラは不要。
68
+ */
69
+ onViewStateChange?: (viewState: ViewState) => void;
70
+ /** 組み込みのショートカット一覧を使うか。既定は使う */
71
+ shortcutHelp?: boolean;
72
+ /**
73
+ * 組み込みのコンテキストメニュー(右クリック・`Mod-/`)を使うか。既定は使う。
74
+ * 使わない設定では、右クリックに対して実行環境の既定のメニューを妨げない。
75
+ */
76
+ contextMenu?: boolean;
77
+ }
78
+
79
+ /** `padding` を四辺へ展開したもの。CSS 側へは辺ごとに渡す(→ `expandPadding`) */
80
+ export interface EditorPadding {
81
+ readonly top: string;
82
+ readonly right: string;
83
+ readonly bottom: string;
84
+ readonly left: string;
85
+ }
86
+
87
+ /**
88
+ * `padding` の並びを四辺へ展開する。規則は CSS そのままで、
89
+ * 1 つなら全部、2 つなら上下・左右、3 つなら上・左右・下、4 つなら上・右・下・左。
90
+ *
91
+ * **展開が要るのは、モジュールが辺ごとの値を使う場所を持っているためである。**
92
+ * 左は折りたたみの記号がぶら下がる場所なので、記号より狭ければ広げる。上は本文との
93
+ * 距離の計算に入る(→ `FocusBreadcrumb`)。CSS には並びの一部を取り出す手立てがない。
94
+ *
95
+ * 区切りは**括弧の外側の空白だけ**で見る。`calc(1rem + 2px)` のように、値そのものが
96
+ * 空白を含むためである。
97
+ */
98
+ export function expandPadding(padding: string): EditorPadding | null {
99
+ const parts: string[] = [];
100
+ let depth = 0;
101
+ let current = '';
102
+
103
+ for (const character of padding.trim()) {
104
+ if (character === '(') depth += 1;
105
+ if (character === ')') depth -= 1;
106
+ if (depth === 0 && /\s/.test(character)) {
107
+ if (current) parts.push(current);
108
+ current = '';
109
+ continue;
110
+ }
111
+ current += character;
112
+ }
113
+ if (current) parts.push(current);
114
+ if (parts.length === 0) return null;
115
+
116
+ const [top, right = top, bottom = top, left = right] = parts;
117
+ return { top, right, bottom, left };
118
+ }
119
+
120
+ export interface MarkdownEditorApi {
121
+ /** 現在の Markdown を取得する */
122
+ getMarkdown(): string;
123
+ /** 現在の表示状態を取得する(バインドを使わない場合の入口) */
124
+ getViewState(): ViewState;
125
+ /**
126
+ * 文書全体を入れ替え、取り消し履歴を初期化する(別の文書を開く用途)。
127
+ * バインドされた値を新しい文書の Markdown で更新し、`onChange` を発火する。
128
+ */
129
+ resetDocument(markdown: string): Result<void, MarkdownParseError>;
130
+ /**
131
+ * フロントマターへ項目を書き込む。キーと JSON の値を渡し、記法はモジュールが持つ。
132
+ *
133
+ * 取り消し履歴に積まず、カーソル・選択・折りたたみ・領域の外の本文を変えない。
134
+ * **利用者がその項目を編集している最中は書き込みを見送り**、`skipped` を返す
135
+ * (繰り返し呼ばれても、打った文字がそのつど消えることがない)。
136
+ */
137
+ writeFrontMatterFields(
138
+ fields: FrontMatterFields
139
+ ): Result<FrontMatterWriteOutcome, FrontMatterWriteError>;
140
+ /** 編集エリアへフォーカスする */
141
+ focus(): void;
142
+ }
143
+ </script>
144
+
145
+ <script lang="ts">
146
+ import { untrack } from 'svelte';
147
+ import { resolveMessages } from '../messages';
148
+ import { EMPTY_VIEW_STATE, isSameViewState } from '../models/viewState/viewStateCodec';
149
+ import { createContextMenuController } from '../states/contextMenuController.svelte';
150
+ import { createOutlineViewController } from '../states/outlineViewController.svelte';
151
+ import type { EditorSession } from '../states/editorSession.types';
152
+ import ContextMenu from './ContextMenu.svelte';
153
+ import EditorSurface from './EditorSurface.svelte';
154
+ import FocusBreadcrumb from './FocusBreadcrumb.svelte';
155
+ import ShortcutHelpDialog from './ShortcutHelpDialog.svelte';
156
+
157
+ // --- props ---
158
+
159
+ let {
160
+ value = $bindable(''),
161
+ onChange,
162
+ label,
163
+ readonly = false,
164
+ autofocus = false,
165
+ class: className = '',
166
+ padding,
167
+ maxWidth,
168
+ lang,
169
+ messages,
170
+ viewState = $bindable(),
171
+ onViewStateChange,
172
+ shortcutHelp = true,
173
+ contextMenu = true
174
+ }: MarkdownEditorProps = $props();
175
+
176
+ // --- 状態 ---
177
+
178
+ let session: EditorSession | null = $state(null);
179
+
180
+ // --- 導出 ---
181
+
182
+ const text = $derived(resolveMessages(lang, messages));
183
+ const accessibleName = $derived(label ?? text.editorLabel);
184
+ const sides = $derived(padding === undefined ? null : expandPadding(padding));
185
+ const mermaidText = $derived({
186
+ empty: text.mermaidEmpty,
187
+ loading: text.mermaidLoading,
188
+ renderError: text.mermaidRenderError,
189
+ loadError: text.mermaidLoadError
190
+ });
191
+ const frontMatterText = $derived(text.frontMatterLabel);
192
+ const presentation = $derived({
193
+ readonly,
194
+ label: accessibleName,
195
+ lang,
196
+ mermaidText,
197
+ frontMatterText
198
+ });
199
+
200
+ // --- 1 度だけ組み立てるもの ---
201
+
202
+ const controller = createOutlineViewController({
203
+ getSession: () => session,
204
+ isShortcutHelpEnabled: () => shortcutHelp
205
+ });
206
+
207
+ /**
208
+ * 表示状態のまとめ役を**先に**作り、その操作をメニューへ渡す。
209
+ *
210
+ * アクションの宣言は実行時に 2 か所にある。静的な基盤のぶんと、
211
+ * `outlineViewController` がエディタごとに生成するぶん(畳む・このセクションだけ表示。
212
+ * フォーカスのプラグインの実体を掴むため静的に書けない)である。**この配線を忘れても
213
+ * 型エラーにならず、畳む項目が黙って消え、読み取り専用ではメニューが開かなくなる。**
214
+ */
215
+ const menuController = createContextMenuController({
216
+ getSession: () => session,
217
+ getExtensionActions: () => controller.extension.actions,
218
+ isEnabled: () => contextMenu
219
+ });
220
+
221
+ /** 一覧へ渡す拡張の宣言。**両方のコントローラのぶんを渡す**(メニューの割り当ても一覧に出す) */
222
+ const listedActions = $derived([
223
+ ...controller.extension.actions,
224
+ ...menuController.extension.actions
225
+ ]);
226
+
227
+ /** 編集の仕組みを作るときの材料。作り直しを起こさないよう、反応する値を読まない */
228
+ const options = {
229
+ initialMarkdown: untrack(() => value),
230
+ readonly: untrack(() => readonly),
231
+ label: untrack(() => accessibleName),
232
+ lang: untrack(() => lang),
233
+ mermaidText: untrack(() => mermaidText),
234
+ frontMatterText: untrack(() => frontMatterText),
235
+ extensions: [controller.extension, menuController.extension]
236
+ };
237
+
238
+ // --- 関数 ---
239
+
240
+ /** 編集の仕組みができた・なくなったときに、通知の配線をつなぎ直す */
241
+ function handleSession(next: EditorSession | null): void {
242
+ session = next;
243
+ if (!next) {
244
+ controller.sync();
245
+ return;
246
+ }
247
+
248
+ // 初期化ではバインド値を書き換えない。ただし正規化で内容が変わったことは知らせる
249
+ const normalized = next.getMarkdown();
250
+ if (normalized !== value) onChange?.(normalized);
251
+
252
+ // 渡された表示状態を先に当てる。ここでは通知しない(変化ではなく復元だから)
253
+ if (viewState !== undefined) controller.applyViewState(viewState);
254
+ controller.sync();
255
+
256
+ next.subscribe(({ markdown, docChanged }) => {
257
+ // 表示状態の変化は文書を変えない。`docChanged` で捨てると画面へ届かなくなる
258
+ controller.sync();
259
+ publishViewState();
260
+
261
+ if (!docChanged) return;
262
+ value = markdown;
263
+ onChange?.(markdown);
264
+ });
265
+
266
+ if (autofocus) next.focus();
267
+ }
268
+
269
+ /** パンくずの操作で全体表示へ戻す。押した後は編集エリアへ戻す(キーボードだけで続けられる) */
270
+ function returnToWholeDocument(): void {
271
+ controller.clearFocus();
272
+ publishViewState();
273
+ session?.focus();
274
+ }
275
+
276
+ /**
277
+ * 表示状態が実際に変わったときだけ、束縛された値を更新して通知する。
278
+ *
279
+ * 一致の判定に控えを持たず、常に現在の真実(`getViewState()`)と突き合わせる。
280
+ * 控えを持つと、書き込みと読み出しが別の経路に分かれ、食い違いが往復として現れる。
281
+ */
282
+ function publishViewState(): void {
283
+ const current = controller.getViewState();
284
+ // 未指定は「全展開・フォーカスなし」と同じ。書き換えるのは本当に変わったときだけにする
285
+ if (isSameViewState(viewState ?? EMPTY_VIEW_STATE, current)) return;
286
+
287
+ viewState = current;
288
+ onViewStateChange?.(current);
289
+ }
290
+
291
+ // --- 外からの変更に追随する ---
292
+
293
+ /**
294
+ * 本文を差分として当てる。
295
+ *
296
+ * 「自身が直近に書き込んだ値」はセッションが持っている内容そのものなので、それと突き合わせる。
297
+ * 別の変数に控えておく作りにすると、書き込みと読み出しが別の経路(購読の中と効果の中)に
298
+ * 分かれるぶん食い違いが起きやすく、外部変更として扱い直す往復が止まらなくなる。
299
+ *
300
+ * 差分適用は変更範囲のノードを作り直すため、そのままでは間にあるセクションの
301
+ * 折りたたみまで失われる。控えて当て直す(`preserveFolds`)。
302
+ */
303
+ $effect(() => {
304
+ const next = value;
305
+ if (!session) return;
306
+ untrack(() => {
307
+ if (!session || next === session.getMarkdown()) return;
308
+ controller.preserveFolds(() => session!.applyMarkdown(next));
309
+ });
310
+ });
311
+
312
+ /**
313
+ * 表示状態を当てる。
314
+ *
315
+ * 内容で突き合わせ、一致するなら何もしない(自身の書き込みを外部変更と誤認しない)。
316
+ * 不正な値は当てず、束縛された値も書き換えない(利用者の値を勝手に正規化しない)。
317
+ */
318
+ $effect(() => {
319
+ const next = viewState;
320
+ if (!session) return;
321
+ untrack(() => {
322
+ if (next === undefined || isSameViewState(next, controller.getViewState())) return;
323
+ controller.applyViewState(next);
324
+ });
325
+ });
326
+
327
+ // --- 公開 API(MarkdownEditorApi)---
328
+
329
+ export function getMarkdown(): string {
330
+ return session?.getMarkdown() ?? value;
331
+ }
332
+
333
+ export function getViewState(): ViewState {
334
+ return controller.getViewState();
335
+ }
336
+
337
+ export function resetDocument(markdown: string): Result<void, MarkdownParseError> {
338
+ if (!session) {
339
+ return { ok: false, error: { kind: 'invalid-structure', detail: 'まだ準備できていない' } };
340
+ }
341
+ return session.resetDocument(markdown);
342
+ }
343
+
344
+ export function writeFrontMatterFields(
345
+ fields: FrontMatterFields
346
+ ): Result<FrontMatterWriteOutcome, FrontMatterWriteError> {
347
+ if (!session) return { ok: false, error: { kind: 'not-ready' } };
348
+ return session.writeFrontMatterFields(fields);
349
+ }
350
+
351
+ export function focus(): void {
352
+ session?.focus();
353
+ }
354
+ </script>
355
+
356
+ <!--
357
+ 版面の指定は、**この要素へ変数として置く**(同じアプリに複数置いたとき、それぞれで
358
+ 変えられる必要がある)。カスタムプロパティは継承するので、これだけで中の全部へ届く。
359
+ 渡されなければ何も置かず、CSS 側の値がそのまま残る。
360
+
361
+ 余白は**辺ごとに**渡す。CSS の並び(`0 64px 16px`)をそのまま 1 つの変数へ入れると、
362
+ 辺ごとの値を使う場所(記号のぶんを確保する左、本文との距離に入る上)で解けない。
363
+ -->
364
+ <div
365
+ class="markdown-editor {className}"
366
+ style:--markdown-editor-padding={padding}
367
+ style:--markdown-editor-padding-top={sides?.top}
368
+ style:--markdown-editor-padding-right={sides?.right}
369
+ style:--markdown-editor-padding-bottom={sides?.bottom}
370
+ style:--markdown-editor-padding-left={sides?.left}
371
+ style:--markdown-editor-max-width={maxWidth}
372
+ data-readonly={readonly ? 'true' : undefined}
373
+ >
374
+ <FocusBreadcrumb
375
+ items={controller.breadcrumb}
376
+ label={text.focusBreadcrumbLabel}
377
+ clearLabel={text.focusClear}
378
+ onClear={returnToWholeDocument}
379
+ onSelect={(path) => controller.focusPath(path)}
380
+ />
381
+ <EditorSurface {options} {presentation} onSession={handleSession} />
382
+ {#if shortcutHelp}
383
+ <ShortcutHelpDialog
384
+ open={controller.isShortcutHelpOpen}
385
+ actions={listedActions}
386
+ {text}
387
+ onClose={controller.closeShortcutHelp}
388
+ />
389
+ {/if}
390
+ {#if contextMenu}
391
+ <ContextMenu controller={menuController} {text} />
392
+ {/if}
393
+ </div>
@@ -0,0 +1,119 @@
1
+ import type { EditorLang, EditorMessagesOverride } from '../messages/messages.types';
2
+ import type { FrontMatterFields, FrontMatterWriteError, FrontMatterWriteOutcome, MarkdownParseError } from '../models/markdown/markdown.types';
3
+ import type { Result } from '../utils/result.types';
4
+ import type { ViewState } from '../models/viewState/viewState.types';
5
+ export interface MarkdownEditorProps {
6
+ /**
7
+ * 本文の Markdown。`bind:value` で双方向に扱う。
8
+ * 内部編集はこの値を更新し、外部からの変更は差分として文書へ適用される。
9
+ * 初期化ではこの値を書き換えない。最初の内部編集以降、正規化された Markdown で
10
+ * 更新される(記法は正規化されうるため、入力と文字列一致するとは限らない)。
11
+ */
12
+ value?: string;
13
+ /**
14
+ * 文書が変わったときの任意の通知。保存・未保存フラグなど副作用の起点として使う。
15
+ * 原因(内部編集・外部からの設定・resetDocument)を問わず発火する。
16
+ * 内容が変わらなかった場合は発火しない。
17
+ * `bind:value` を使う場合、値の同期にこのハンドラは不要。
18
+ */
19
+ onChange?: (markdown: string) => void;
20
+ /** 編集エリアのアクセシブルな名前 */
21
+ label?: string;
22
+ /** 読み取り専用。true のとき入力・編集コマンド・貼り付けをすべて受け付けない */
23
+ readonly?: boolean;
24
+ /** マウント時に編集エリアへフォーカスする */
25
+ autofocus?: boolean;
26
+ /** 追加のクラス名 */
27
+ class?: string;
28
+ /**
29
+ * 編集エリアの内側の余白。**CSS の `padding` と同じ書き方**で、
30
+ * `1em` も `0 64px 16px` も書ける。未指定なら `--markdown-editor-padding` に従う。
31
+ */
32
+ padding?: string;
33
+ /**
34
+ * 最大幅の上限(CSS の長さ)。未指定なら `--markdown-editor-max-width` に従う。
35
+ *
36
+ * 狭めても**押せる面は箱の幅のまま**で、行の長さだけが収まる。置いた側のコンテナへ
37
+ * `max-width` を掛けると、その外側が「見た目は同じなのに反応しない領域」になる。
38
+ */
39
+ maxWidth?: string;
40
+ /**
41
+ * 同梱の言語を選ぶ。既定は 'en'。
42
+ * 編集エリアの言語としても使われ、支援技術が正しい発音で読む助けになる。
43
+ */
44
+ lang?: EditorLang;
45
+ /**
46
+ * 文字列の部分的な差し替え。`lang` で選んだ文字列の上に重ねる。
47
+ * 同梱していない言語を使う場合は、ここに一式を渡す。
48
+ */
49
+ messages?: EditorMessagesOverride;
50
+ /**
51
+ * 表示状態(見出しの折りたたみ・フォーカス)。`bind:viewState` で双方向に扱う。
52
+ * 本文(`value`)と同じ作法であり、内部の変更はこの値を更新し、外部からの設定は
53
+ * そのまま表示へ反映される。未指定なら全展開・フォーカスなしで始まる。
54
+ * 現在の文書構造に対応しない項目は破棄され、残りが適用される。
55
+ * リスト項目の折りたたみは含まれない。
56
+ */
57
+ viewState?: ViewState;
58
+ /**
59
+ * 表示状態が変わったときの任意の通知。保存などの副作用の起点として使う。
60
+ * `bind:viewState` を使う場合、値の同期にこのハンドラは不要。
61
+ */
62
+ onViewStateChange?: (viewState: ViewState) => void;
63
+ /** 組み込みのショートカット一覧を使うか。既定は使う */
64
+ shortcutHelp?: boolean;
65
+ /**
66
+ * 組み込みのコンテキストメニュー(右クリック・`Mod-/`)を使うか。既定は使う。
67
+ * 使わない設定では、右クリックに対して実行環境の既定のメニューを妨げない。
68
+ */
69
+ contextMenu?: boolean;
70
+ }
71
+ /** `padding` を四辺へ展開したもの。CSS 側へは辺ごとに渡す(→ `expandPadding`) */
72
+ export interface EditorPadding {
73
+ readonly top: string;
74
+ readonly right: string;
75
+ readonly bottom: string;
76
+ readonly left: string;
77
+ }
78
+ /**
79
+ * `padding` の並びを四辺へ展開する。規則は CSS そのままで、
80
+ * 1 つなら全部、2 つなら上下・左右、3 つなら上・左右・下、4 つなら上・右・下・左。
81
+ *
82
+ * **展開が要るのは、モジュールが辺ごとの値を使う場所を持っているためである。**
83
+ * 左は折りたたみの記号がぶら下がる場所なので、記号より狭ければ広げる。上は本文との
84
+ * 距離の計算に入る(→ `FocusBreadcrumb`)。CSS には並びの一部を取り出す手立てがない。
85
+ *
86
+ * 区切りは**括弧の外側の空白だけ**で見る。`calc(1rem + 2px)` のように、値そのものが
87
+ * 空白を含むためである。
88
+ */
89
+ export declare function expandPadding(padding: string): EditorPadding | null;
90
+ export interface MarkdownEditorApi {
91
+ /** 現在の Markdown を取得する */
92
+ getMarkdown(): string;
93
+ /** 現在の表示状態を取得する(バインドを使わない場合の入口) */
94
+ getViewState(): ViewState;
95
+ /**
96
+ * 文書全体を入れ替え、取り消し履歴を初期化する(別の文書を開く用途)。
97
+ * バインドされた値を新しい文書の Markdown で更新し、`onChange` を発火する。
98
+ */
99
+ resetDocument(markdown: string): Result<void, MarkdownParseError>;
100
+ /**
101
+ * フロントマターへ項目を書き込む。キーと JSON の値を渡し、記法はモジュールが持つ。
102
+ *
103
+ * 取り消し履歴に積まず、カーソル・選択・折りたたみ・領域の外の本文を変えない。
104
+ * **利用者がその項目を編集している最中は書き込みを見送り**、`skipped` を返す
105
+ * (繰り返し呼ばれても、打った文字がそのつど消えることがない)。
106
+ */
107
+ writeFrontMatterFields(fields: FrontMatterFields): Result<FrontMatterWriteOutcome, FrontMatterWriteError>;
108
+ /** 編集エリアへフォーカスする */
109
+ focus(): void;
110
+ }
111
+ declare const MarkdownEditor: import("svelte").Component<MarkdownEditorProps, {
112
+ getMarkdown: () => string;
113
+ getViewState: () => ViewState;
114
+ resetDocument: (markdown: string) => Result<void, MarkdownParseError>;
115
+ writeFrontMatterFields: (fields: FrontMatterFields) => Result<FrontMatterWriteOutcome, FrontMatterWriteError>;
116
+ focus: () => void;
117
+ }, "value" | "viewState">;
118
+ type MarkdownEditor = ReturnType<typeof MarkdownEditor>;
119
+ export default MarkdownEditor;
@@ -0,0 +1,155 @@
1
+ <script lang="ts">
2
+ import type { EditorMessages } from '../messages/messages.types';
3
+ import { editorKeymap } from '../operations/keymap/assemble';
4
+ import { SHORTCUT_CATEGORY_ORDER } from '../operations/keymap/menuItems';
5
+ import { formatAllShortcutKeys } from '../operations/keymap/shortcutKeys';
6
+ import type { EditorAction } from '../operations/operations.types';
7
+ import Dialog from './internal/Dialog.svelte';
8
+
9
+ interface Props {
10
+ open: boolean;
11
+ /** 拡張の操作。基盤のぶんはここで足す */
12
+ actions: readonly EditorAction[];
13
+ text: EditorMessages;
14
+ onClose: () => void;
15
+ }
16
+
17
+ let { open, actions, text, onClose }: Props = $props();
18
+
19
+ let dialog: Dialog | undefined = $state();
20
+
21
+ /**
22
+ * 表記は実行環境で決まるため、**組み立てるのはブラウザで表示された後だけ**にする。
23
+ * サーバー側で出すと、macOS の利用者には差し替わるまで誤った表記が見える。
24
+ */
25
+ const groups = $derived.by(() => {
26
+ if (!open) return [];
27
+
28
+ // 一覧に出すかは定義の宣言で決まる。説明文の有無から推し量らない
29
+ const all = [...editorKeymap.coreActions, ...actions].filter((action) => action.listed);
30
+ return SHORTCUT_CATEGORY_ORDER.map((category) => ({
31
+ category,
32
+ title: text.shortcutCategories[category],
33
+ // 1 操作 1 行。代替のキーは別の行にせず、その操作が持つキーをすべて並べる
34
+ items: all
35
+ .filter((action) => action.category === category)
36
+ .map((action) => ({
37
+ id: action.id,
38
+ description: text.shortcuts[action.id] ?? action.id,
39
+ keys: formatAllShortcutKeys(action).join(' / ')
40
+ }))
41
+ })).filter((group) => group.items.length > 0);
42
+ });
43
+
44
+ $effect(() => {
45
+ if (open) dialog?.open();
46
+ else dialog?.close();
47
+ });
48
+ </script>
49
+
50
+ <Dialog
51
+ bind:this={dialog}
52
+ class="markdown-editor-shortcut-help-dialog"
53
+ ariaLabel={text.shortcutHelpTitle}
54
+ width="min(40em, 90vw)"
55
+ scrollable
56
+ {onClose}
57
+ >
58
+ {#snippet header()}
59
+ <!-- 中身は開いた後だけ描画する(サーバー側で一覧の文言を出さない) -->
60
+ {#if open}
61
+ <h2 class="markdown-editor-shortcut-help-dialog__title">{text.shortcutHelpTitle}</h2>
62
+ {/if}
63
+ {/snippet}
64
+
65
+ {#if open}
66
+ {#each groups as group (group.category)}
67
+ <section class="markdown-editor-shortcut-help-dialog__group" data-category={group.category}>
68
+ <h3 class="markdown-editor-shortcut-help-dialog__group-title">{group.title}</h3>
69
+ <dl class="markdown-editor-shortcut-help-dialog__list">
70
+ {#each group.items as item (item.id)}
71
+ <dt class="markdown-editor-shortcut-help-dialog__description">{item.description}</dt>
72
+ <dd class="markdown-editor-shortcut-help-dialog__keys">{item.keys}</dd>
73
+ {/each}
74
+ </dl>
75
+ </section>
76
+ {/each}
77
+ {/if}
78
+ </Dialog>
79
+
80
+ <style>
81
+ .markdown-editor-shortcut-help-dialog__title {
82
+ margin: 0;
83
+ font-size: var(--markdown-editor-font-size-xl);
84
+ font-weight: 600;
85
+ flex-grow: 1;
86
+ }
87
+
88
+ /**
89
+ * **左右の空きは分類の側で取る。** コンテナ(`.dialog__body`)に持たせると、分類の箱が
90
+ * そのぶん内側へ寄り、下の仕切り線が**ダイアログの端まで届かない**。
91
+ */
92
+ .markdown-editor-shortcut-help-dialog__group {
93
+ margin: 0;
94
+ padding: 0 var(--markdown-editor-dialog-inset);
95
+ }
96
+
97
+ /**
98
+ * 分類のあいだに仕切り線を引く。**コンテキストメニューと同じ組み立て**である——線は分類の箱の
99
+ * 上辺として引き、上の空きは `margin`、下の空きは `padding` で取る。
100
+ *
101
+ * 隣り合うときだけ引くので、**最初の分類の上と最後の分類の下には線が出ない。**
102
+ */
103
+ .markdown-editor-shortcut-help-dialog__group + .markdown-editor-shortcut-help-dialog__group {
104
+ border-top: 1px solid var(--markdown-editor-shortcut-separator);
105
+ margin-top: 1.2em;
106
+ padding-top: 1.2em;
107
+ }
108
+
109
+ /**
110
+ * 分類の見出しは**どれも同じ見た目**にし、項目との差は文字の大きさだけで付ける。
111
+ *
112
+ * 一部の分類にだけ色や罫線を当てると、その分類が特別なのか、単に見た目が揃って
113
+ * いないだけなのかが読み取れない。
114
+ *
115
+ * **見た目はコンテキストメニューの分類の見出しに揃える。** 同じものを 2 か所から見せている
116
+ * のだから、片方だけ大きい・太いという状態を作らない。太さを明に書くのは、この
117
+ * 要素が `h3` で、書かなければブラウザの既定(太字)が出るためである。
118
+ */
119
+ .markdown-editor-shortcut-help-dialog__group-title {
120
+ margin: 0 0 0.4em;
121
+ color: var(--markdown-editor-shortcut-group-title-color);
122
+ font-size: var(--markdown-editor-font-size-sm);
123
+ font-weight: 400;
124
+ }
125
+
126
+ .markdown-editor-shortcut-help-dialog__list {
127
+ display: grid;
128
+ grid-template-columns: 1fr auto;
129
+ gap: 0.25em 1em;
130
+ margin: 0;
131
+ }
132
+
133
+ .markdown-editor-shortcut-help-dialog__description {
134
+ margin: 0;
135
+ }
136
+
137
+ /**
138
+ * キーの表記は**右端で揃える**。
139
+ *
140
+ * 一覧全体が 1 つの表であり、キーの列の幅はいちばん長い表記(`⌘⇧Backspace`)で
141
+ * 決まる。左で揃えると短い表記が列の中で取り残され、**右端がばらつく**。
142
+ * コンテキストメニューでは項目ごとに列が閉じているため、同じ指定をしなくても右端で揃う。
143
+ * **見え方をそちらに合わせる。**
144
+ *
145
+ * 色もコンテキストメニューと同じ扱いにする(キーの表記は本文ではなく添えものである)。
146
+ * トークンを分けてあるのは、**値が同じでも意味が違うものを 1 つで賄わない**ため。
147
+ */
148
+ .markdown-editor-shortcut-help-dialog__keys {
149
+ margin: 0;
150
+ color: var(--markdown-editor-shortcut-keys-color);
151
+ font-family: var(--markdown-editor-code-font);
152
+ white-space: nowrap;
153
+ text-align: right;
154
+ }
155
+ </style>
@@ -0,0 +1,12 @@
1
+ import type { EditorMessages } from '../messages/messages.types';
2
+ import type { EditorAction } from '../operations/operations.types';
3
+ interface Props {
4
+ open: boolean;
5
+ /** 拡張の操作。基盤のぶんはここで足す */
6
+ actions: readonly EditorAction[];
7
+ text: EditorMessages;
8
+ onClose: () => void;
9
+ }
10
+ declare const ShortcutHelpDialog: import("svelte").Component<Props, {}, "">;
11
+ type ShortcutHelpDialog = ReturnType<typeof ShortcutHelpDialog>;
12
+ export default ShortcutHelpDialog;