@14ch/svelte-md-outliner 0.0.27 → 0.0.29

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 (53) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.ja.md +5 -3
  3. package/README.md +5 -3
  4. package/dist/assets/styles/editor.css +7 -8
  5. package/dist/assets/styles/theme.css +13 -13
  6. package/dist/components/LinkDialog.svelte +136 -0
  7. package/dist/components/LinkDialog.svelte.d.ts +10 -0
  8. package/dist/components/LinkPopover.svelte +64 -94
  9. package/dist/components/LinkPopover.svelte.d.ts +2 -2
  10. package/dist/components/MarkdownEditor.svelte +49 -9
  11. package/dist/components/MarkdownEditor.svelte.d.ts +5 -0
  12. package/dist/components/SelectionToolbar.svelte +140 -0
  13. package/dist/components/SelectionToolbar.svelte.d.ts +9 -0
  14. package/dist/components/ShortcutHelpDialog.svelte +26 -18
  15. package/dist/components/internal/AnchoredLayer.svelte +102 -0
  16. package/dist/components/internal/AnchoredLayer.svelte.d.ts +16 -0
  17. package/dist/components/internal/Icon.svelte +26 -0
  18. package/dist/components/internal/Icon.svelte.d.ts +7 -0
  19. package/dist/components/internal/icons.d.ts +22 -0
  20. package/dist/components/internal/icons.js +22 -0
  21. package/dist/messages/en.js +10 -1
  22. package/dist/messages/index.js +9 -0
  23. package/dist/messages/ja.js +10 -1
  24. package/dist/messages/messages.types.d.ts +27 -0
  25. package/dist/operations/autoformat.js +17 -0
  26. package/dist/operations/commands/link.d.ts +27 -4
  27. package/dist/operations/commands/link.js +111 -14
  28. package/dist/operations/keymap/coreActions.js +0 -10
  29. package/dist/operations/keymap/linkActions.d.ts +13 -0
  30. package/dist/operations/keymap/linkActions.js +30 -0
  31. package/dist/operations/keymap/toolbarItems.d.ts +17 -0
  32. package/dist/operations/keymap/toolbarItems.js +62 -0
  33. package/dist/operations/operations.types.d.ts +51 -0
  34. package/dist/operations/plugins/compositionState.js +21 -1
  35. package/dist/operations/plugins/linkDialogGuard.d.ts +9 -0
  36. package/dist/operations/plugins/linkDialogGuard.js +18 -0
  37. package/dist/operations/plugins/linkPopover.d.ts +8 -10
  38. package/dist/operations/plugins/linkPopover.js +19 -39
  39. package/dist/operations/plugins/selectionToolbar.d.ts +15 -0
  40. package/dist/operations/plugins/selectionToolbar.js +96 -0
  41. package/dist/states/linkDialogController.svelte.d.ts +43 -0
  42. package/dist/states/linkDialogController.svelte.js +104 -0
  43. package/dist/states/linkPopoverController.svelte.d.ts +9 -2
  44. package/dist/states/linkPopoverController.svelte.js +13 -0
  45. package/dist/states/selectionToolbarController.svelte.d.ts +29 -0
  46. package/dist/states/selectionToolbarController.svelte.js +51 -0
  47. package/dist/utils/linkHref.d.ts +7 -0
  48. package/dist/utils/linkHref.js +16 -0
  49. package/package.json +5 -2
  50. package/dist/components/internal/Dialog.svelte +0 -157
  51. package/dist/components/internal/Dialog.svelte.d.ts +0 -23
  52. package/dist/components/internal/Modal.svelte +0 -181
  53. package/dist/components/internal/Modal.svelte.d.ts +0 -18
package/CHANGELOG.md CHANGED
@@ -5,6 +5,56 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.0.29] - 2026-11-04
9
+
10
+ ### Changed
11
+
12
+ - Design tweaks to the formatting bar and the link popover.
13
+
14
+ ## [0.0.28] - 2026-11-04
15
+
16
+ ### Added
17
+
18
+ - Selecting text shows a small formatting bar next to the selection with bold,
19
+ italic, strikethrough, inline code and link buttons. It appears above a
20
+ mouse selection and below a touch selection, and `Escape` hides it. Turn it
21
+ off with `selectionToolbar={false}`.
22
+ - A link dialog for adding and editing links, with separate text and URL
23
+ fields, so the link text and its target can differ. Open it from the
24
+ formatting bar, with `Mod-k`, or from the link popover. With nothing
25
+ selected, `Mod-k` inserts a new link at the cursor. A URL without a scheme
26
+ (`example.com`) becomes `https://example.com`.
27
+ - The link popover has **Edit** and **Remove link** buttons.
28
+ - Typing link syntax (`[text](url)`) turns it into a link when you type the
29
+ closing parenthesis.
30
+ - Messages: `selectionToolbarLabel`, `linkAddTitle`, `linkEditTitle`,
31
+ `linkTextLabel`, `linkHrefLabel`, `linkApply`, `linkCancel`, `linkEdit`,
32
+ `linkRemove`. Add them if you pass a full message set for a language that is
33
+ not bundled.
34
+ - Theme tokens: `--markdown-editor-selection-toolbar-*` and
35
+ `--markdown-editor-link-popover-icon`.
36
+
37
+ ### Changed
38
+
39
+ - **`@14ch/svelte-ui` (`^0.0.83`) is now a peer dependency.** Dialogs, buttons
40
+ and fields are svelte-ui components. `editor.css` loads only svelte-ui's
41
+ tokens (no fonts, no global element rules).
42
+ - **Removed the `--markdown-editor-dialog-*` tokens.** Dialog colors now follow
43
+ the `--svelte-ui-*` tokens, so they match your app's theme if it uses
44
+ svelte-ui.
45
+ - `Mod-k` opens the link dialog instead of toggling a link. Removing a link is
46
+ done from the dialog or the popover; to link a selected URL, press `Mod-k`
47
+ and then `Enter`. Its description in the shortcut list is now
48
+ "Add or edit link".
49
+
50
+ ### Fixed
51
+
52
+ - A `value` set during IME composition is now applied when the composition is
53
+ cancelled, even when the page has `input` listeners (as most Svelte apps do).
54
+ Before, it waited until the next edit, and could be overwritten.
55
+ - Clicking or tapping a link in an unfocused editor now reliably shows the link
56
+ popover.
57
+
8
58
  ## [0.0.27] - 2026-11-02
9
59
 
10
60
  ### Changed
package/README.ja.md CHANGED
@@ -36,10 +36,10 @@ English: [README.md](README.md)
36
36
  ## 入れる
37
37
 
38
38
  ```sh
39
- npm install @14ch/svelte-md-outliner
39
+ npm install @14ch/svelte-md-outliner @14ch/svelte-ui
40
40
  ```
41
41
 
42
- `svelte@^5` が peer dependency。スタイルは自分で読み込む(既定のテーマは中で読み込まれる)。
42
+ `svelte@^5` と `@14ch/svelte-ui` が peer dependency(ダイアログ・ボタン・入力欄は svelte-ui の部品)。スタイルは自分で読み込む(既定のテーマと svelte-ui のトークンは中で読み込まれる。読み込むのはトークンだけで、フォントや要素への規則は含まない)。
43
43
 
44
44
  ```ts
45
45
  import '@14ch/svelte-md-outliner/styles/editor.css';
@@ -67,6 +67,7 @@ import '@14ch/svelte-md-outliner/styles/editor.css';
67
67
  | `messages` | `EditorMessagesOverride` | — | 文字列の差し替え。同梱しない言語は一式を渡す |
68
68
  | `shortcutHelp` | `boolean` | `true` | 組み込みのショートカット一覧(`Mod-Alt-/`) |
69
69
  | `contextMenu` | `boolean` | `true` | 組み込みのコンテキストメニュー(右クリック / `Mod-/`) |
70
+ | `selectionToolbar` | `boolean` | `true` | 文字を選んだときに、そばに出す書式の帯 |
70
71
  | `uploadImage` | `(file: File) => Promise<string \| null>` | — | 貼り付けられた画像を引き受け、表示に使う URL を返す |
71
72
  | `onLinkClick` | `(href: string, event: MouseEvent) => void` | — | リンクが押されたときの通知。止めなければ新しいタブで開く |
72
73
 
@@ -94,6 +95,7 @@ const uploadImage = async (file: File) => {
94
95
  リンク先は新しいタブで開く。
95
96
 
96
97
  - **編集中**は、リンクを押す(タップする)とカーソルが置かれ、リンクの上にリンク先を見せる吹き出しが出る。吹き出しを押すと開く。文字を打つ・キーでカーソルを動かすと消え、キーでリンクに入ったときは出ない。タッチ端末でも「リンクをタップして、吹き出しをタップ」で開ける。⌘+クリック(Mac 以外は Ctrl+クリック)なら吹き出しを経ずに開く
98
+ - **編集中**は、吹き出しに「編集」「リンクを外す」のボタンも並ぶ。`Mod-k` で、選んだ文字へのリンクの追加・カーソルのあるリンクの編集・新しいリンクの挿入のダイアログが開く(リンクの文字と行き先は別々に書ける)。`example.com` のようにスキームの無い行き先は `https://example.com` になる
97
99
  - **読み取り専用**では、素のクリックで開く
98
100
 
99
101
  開く前に `onLinkClick` が書かれたとおりの `href` を受け取る(相対のままでも解決しない)。`event.preventDefault()` を呼べば、新しいタブでは開かない。`./other.md` のように、ブラウザで開いても意味を持たない行き先を利用側で扱うときに使う。
@@ -185,7 +187,7 @@ import './my-theme.css';
185
187
  }
186
188
  ```
187
189
 
188
- `theme.css` にあるのは色とフォント関連。余白と最大幅は `editor.css` 側にあり、上書きの仕方は同じ。
190
+ `theme.css` にあるのは色とフォント関連。余白と最大幅は `editor.css` 側にあり、上書きの仕方は同じ。ダイアログ・ボタン・入力欄は svelte-ui の部品で、色は `--svelte-ui-*` に従う。アプリが svelte-ui を使っていれば、そのテーマに揃う。
189
191
 
190
192
  ## エディタを使わずに読む
191
193
 
package/README.md CHANGED
@@ -36,10 +36,10 @@ Being an outline editor, the heading hierarchy **is** the document structure rat
36
36
  ## Install
37
37
 
38
38
  ```sh
39
- npm install @14ch/svelte-md-outliner
39
+ npm install @14ch/svelte-md-outliner @14ch/svelte-ui
40
40
  ```
41
41
 
42
- `svelte@^5` is a peer dependency. Import the stylesheet yourself — it already pulls in the default theme:
42
+ `svelte@^5` and `@14ch/svelte-ui` are peer dependencies (dialogs, buttons and fields are svelte-ui components). Import the stylesheet yourself — it already pulls in the default theme and the svelte-ui tokens (tokens only: no fonts, no global element rules):
43
43
 
44
44
  ```ts
45
45
  import '@14ch/svelte-md-outliner/styles/editor.css';
@@ -67,6 +67,7 @@ import '@14ch/svelte-md-outliner/styles/editor.css';
67
67
  | `messages` | `EditorMessagesOverride` | — | Replace individual strings, or supply a whole language |
68
68
  | `shortcutHelp` | `boolean` | `true` | Built-in shortcut sheet (`Mod-Alt-/`) |
69
69
  | `contextMenu` | `boolean` | `true` | Built-in context menu (right click / `Mod-/`) |
70
+ | `selectionToolbar` | `boolean` | `true` | Formatting bar shown next to a text selection |
70
71
  | `uploadImage` | `(file: File) => Promise<string \| null>` | — | Takes a pasted image, returns the URL to display |
71
72
  | `onLinkClick` | `(href: string, event: MouseEvent) => void` | — | Called when a link is clicked; opens in a new tab unless prevented |
72
73
 
@@ -94,6 +95,7 @@ While it waits, a marker is shown at the paste position and **the document is no
94
95
  Links open in a new tab.
95
96
 
96
97
  - **While editing**, clicking (or tapping) a link places the cursor and shows a bubble with the target above it; clicking the bubble opens the link. Typing or moving the cursor with the keyboard hides the bubble, and it does not appear when the cursor enters a link by keyboard, so touch devices open links with "tap the link, then tap the bubble". ⌘-click (Ctrl-click outside macOS) opens directly, skipping the bubble
98
+ - **While editing**, the bubble also has **Edit** and **Remove link** buttons. `Mod-k` opens a dialog to add a link to the selection, edit the link at the cursor, or insert a new one — the link text and the target can differ. A target without a scheme such as `example.com` becomes `https://example.com`
97
99
  - **While `readonly`**, a plain click opens the link
98
100
 
99
101
  Before opening, `onLinkClick` receives the `href` exactly as written (relative links are not resolved). Call `event.preventDefault()` to stop the new tab — useful for targets such as `./other.md` that mean nothing to the browser but something to your app.
@@ -185,7 +187,7 @@ import './my-theme.css';
185
187
  }
186
188
  ```
187
189
 
188
- `theme.css` holds colors and typography. Spacing and line width live in `editor.css` and override the same way.
190
+ `theme.css` holds colors and typography. Spacing and line width live in `editor.css` and override the same way. Dialogs, buttons and fields are svelte-ui components and follow the `--svelte-ui-*` tokens — if your app already uses svelte-ui, they match its theme.
189
191
 
190
192
  ## Reading Markdown without an editor
191
193
 
@@ -8,6 +8,13 @@
8
8
  * クラス名は BEM で書き、他所の CSS と同居しても衝突しにくい接頭辞から始める。
9
9
  */
10
10
 
11
+ /**
12
+ * ダイアログ・ボタン・入力欄は同梱の svelte-ui の部品を使う。部品が読むトークン
13
+ * (`--svelte-ui-*`)だけを読み込む。フォントの読み込みや要素への規則は含まないので、
14
+ * 置いた先の画面へ持ち込むものは無い。色の差し替えは `--svelte-ui-*` で行う。
15
+ */
16
+ @import '@14ch/svelte-ui/styles/tokens';
17
+
11
18
  /* 既定のテーマ(トークンの値)。利用者は同じ形式のファイルを後から読んで上書きできる */
12
19
  @import './theme.css';
13
20
 
@@ -95,14 +102,6 @@
95
102
  * 1 つの値で書くなら、辺ごとのトークンは要らない。
96
103
  */
97
104
  --markdown-editor-padding: 1em;
98
- /**
99
- * ダイアログの中身の左右の空き。**コンテナではなく中身が持つ。**
100
- *
101
- * コンテナ(`.dialog__body`)に持たせると、中身の箱がそのぶん内側へ寄り、
102
- * **区切りの線が端まで届かない**。中身の側で取れば、線は箱の幅いっぱいに引ける。
103
- * 値を 1 つにしてあるのは、コンテナの上下の空きと中身の左右の空きを揃えるためである。
104
- */
105
- --markdown-editor-dialog-inset: 24px;
106
105
  /**
107
106
  * 最大幅(行の長さの上限)。**既定は箱いっぱい**で、狭めたい置き先が値を入れる。
108
107
  *
@@ -390,6 +390,19 @@
390
390
  --markdown-editor-link-popover-shadow-weak: var(--markdown-editor-shadow-weak);
391
391
  --markdown-editor-link-popover-color: var(--markdown-editor-hyperlink);
392
392
  --markdown-editor-link-popover-hover: var(--markdown-editor-hover-overlay);
393
+ /* 吹き出しのアイコンだけのボタン(編集・リンクを外す) */
394
+ --markdown-editor-link-popover-icon: var(--markdown-editor-text-subtle);
395
+
396
+ /* 文字を選んだときに出す帯(選択ツールバー) */
397
+ --markdown-editor-selection-toolbar-background: var(--markdown-editor-surface-elevated);
398
+ --markdown-editor-selection-toolbar-border: var(--markdown-editor-border-weak);
399
+ --markdown-editor-selection-toolbar-shadow-strong: var(--markdown-editor-shadow-strong);
400
+ --markdown-editor-selection-toolbar-shadow: var(--markdown-editor-shadow);
401
+ --markdown-editor-selection-toolbar-shadow-weak: var(--markdown-editor-shadow-weak);
402
+ --markdown-editor-selection-toolbar-icon: var(--markdown-editor-text);
403
+ --markdown-editor-selection-toolbar-hover: var(--markdown-editor-hover-overlay);
404
+ /* 選んだ範囲にその書式が付いているボタンのアイコン */
405
+ --markdown-editor-selection-toolbar-active: var(--markdown-editor-accent);
393
406
 
394
407
  /* コンテキストメニュー */
395
408
  --markdown-editor-menu-separator: var(--markdown-editor-border);
@@ -401,19 +414,6 @@
401
414
  /* メニューが対象にしているブロックへ敷く印(編集エリアの側に出る) */
402
415
  --markdown-editor-menu-target-fill: var(--markdown-editor-target-fill);
403
416
 
404
- /* ダイアログ */
405
- --markdown-editor-dialog-background: var(--markdown-editor-surface-elevated);
406
- --markdown-editor-dialog-backdrop: var(--markdown-editor-scrim);
407
- --markdown-editor-dialog-text: var(--markdown-editor-text);
408
- --markdown-editor-dialog-border: var(--markdown-editor-border);
409
- --markdown-editor-dialog-title-color: var(--markdown-editor-text);
410
- /* 見出しと脚を本文から分ける線(巻き取るときだけ出る) */
411
- --markdown-editor-dialog-divider: var(--markdown-editor-border);
412
- /* 浮き上がりの影。メニューとは別の部品なので別に持つ(同じ濃さなのは結果) */
413
- --markdown-editor-dialog-shadow-strong: var(--markdown-editor-shadow-strong);
414
- --markdown-editor-dialog-shadow: var(--markdown-editor-shadow);
415
- --markdown-editor-dialog-shadow-weak: var(--markdown-editor-shadow-weak);
416
-
417
417
  /* ショートカット一覧 */
418
418
  --markdown-editor-shortcut-group-title-color: var(--markdown-editor-text-subtle);
419
419
  --markdown-editor-shortcut-keys-color: var(--markdown-editor-text-subtle);
@@ -0,0 +1,136 @@
1
+ <!--
2
+ リンクのダイアログ。選んだ範囲へリンクを付ける・カーソルの位置へ挿入する(追加)と、
3
+ 既存のリンクを直す・外す(編集)を受け持つ。
4
+
5
+ `Enter` はフォームの送信として受ける(キーを判定しない)。`Escape`・背景・キャンセルは
6
+ ダイアログの既定の閉じ方に任せる。どの経路で閉じても、閉じ終えたらコントローラへ知らせる。
7
+ 適用で対象を手放した・外から文書が変わって対象が古くなったときは、`request` が消えるので
8
+ 自分から閉じる。
9
+
10
+ URL の欄は `type="url"` にしない。スキームの無い入力(`example.com`)を実行環境が弾き、
11
+ `https://` を補う規則(→ `normalizeLinkHref`)まで届かないためである。
12
+ -->
13
+ <script lang="ts">
14
+ import { Button, Dialog, Input } from '@14ch/svelte-ui';
15
+ import type { EditorMessages } from '../messages/messages.types';
16
+ import type { LinkTarget } from '../operations/operations.types';
17
+ import type {
18
+ LinkDialogController,
19
+ LinkDialogRequest
20
+ } from '../states/linkDialogController.svelte';
21
+ import { normalizeLinkHref } from '../utils/linkHref.js';
22
+
23
+ interface Props {
24
+ request: LinkDialogRequest | null;
25
+ controller: LinkDialogController;
26
+ text: EditorMessages;
27
+ }
28
+
29
+ let { request, controller, text }: Props = $props();
30
+
31
+ const id = $props.id();
32
+
33
+ /** 出している対象。閉じる動きのあいだも見出しとボタンを保つため、`request` が消えても残す */
34
+ let shown = $state<LinkTarget | null>(null);
35
+ let textValue = $state('');
36
+ let hrefValue = $state('');
37
+
38
+ let dialog: Dialog | undefined = $state();
39
+ let textRef: Input | undefined = $state();
40
+ let hrefRef: Input | undefined = $state();
41
+
42
+ const applicable = $derived(normalizeLinkHref(hrefValue) !== null);
43
+
44
+ function submit(event: SubmitEvent): void {
45
+ event.preventDefault();
46
+ controller.submit({ text: textValue, href: hrefValue });
47
+ }
48
+
49
+ /**
50
+ * 開いた直後の欄へ焦点を移す。ダイアログは開くと自分自身へ焦点を移すので、その後に呼ぶ。
51
+ * 何も選ばずに開いたら、まず文字を打つ
52
+ */
53
+ function focusInitial(): void {
54
+ (shown?.kind === 'add' && !shown.text ? textRef : hrefRef)?.focus();
55
+ }
56
+
57
+ $effect(() => {
58
+ const target = request?.target;
59
+ if (!target) {
60
+ dialog?.close();
61
+ return;
62
+ }
63
+
64
+ shown = target;
65
+ textValue = target.kind === 'add' ? target.text : target.run.text;
66
+ hrefValue = target.kind === 'add' ? target.href : target.run.href;
67
+ dialog?.open();
68
+ });
69
+ </script>
70
+
71
+ <Dialog
72
+ bind:this={dialog}
73
+ title={shown?.kind === 'edit' ? text.linkEditTitle : text.linkAddTitle}
74
+ width="min(28em, 90vw)"
75
+ onOpen={focusInitial}
76
+ onClose={() => controller.closed()}
77
+ >
78
+ <!-- 適用は脚にあるので、フォームの外から `form` で結ぶ。欄の中の `Enter` もこのボタンの送信になる -->
79
+ <form id="{id}-form" class="markdown-editor-link-dialog__fields" onsubmit={submit}>
80
+ <label class="markdown-editor-link-dialog__label" for="{id}-text">{text.linkTextLabel}</label>
81
+ <Input
82
+ bind:this={textRef}
83
+ bind:value={textValue}
84
+ id="{id}-text"
85
+ fullWidth
86
+ enterkeyhint="next"
87
+ />
88
+ <label class="markdown-editor-link-dialog__label" for="{id}-href">{text.linkHrefLabel}</label>
89
+ <Input
90
+ bind:this={hrefRef}
91
+ bind:value={hrefValue}
92
+ id="{id}-href"
93
+ fullWidth
94
+ inputmode="url"
95
+ autocapitalize="off"
96
+ autocomplete="off"
97
+ spellcheck={false}
98
+ enterkeyhint="done"
99
+ />
100
+ </form>
101
+
102
+ {#snippet footer()}
103
+ {#if shown?.kind === 'edit'}
104
+ <!-- 取り返しのつかない操作なので、キャンセル・適用から離して左端に置く -->
105
+ <Button color="var(--svelte-ui-danger-color)" onclick={controller.remove}>
106
+ {text.linkRemove}
107
+ </Button>
108
+ <span class="markdown-editor-link-dialog__spacer"></span>
109
+ {/if}
110
+ <Button onclick={() => dialog?.close()}>{text.linkCancel}</Button>
111
+ <Button type="submit" form="{id}-form" variant="filled" disabled={!applicable}>
112
+ {text.linkApply}
113
+ </Button>
114
+ {/snippet}
115
+ </Dialog>
116
+
117
+ <style>
118
+ /**
119
+ * 名前の列は 2 行で揃える。列の幅は長いほうの名前で決まる(言語で長さが変わるため、
120
+ * 幅を値で決めない)
121
+ */
122
+ .markdown-editor-link-dialog__fields {
123
+ display: grid;
124
+ grid-template-columns: auto minmax(0, 1fr);
125
+ gap: 16px 8px;
126
+ align-items: center;
127
+ }
128
+
129
+ .markdown-editor-link-dialog__label {
130
+ white-space: nowrap;
131
+ }
132
+
133
+ .markdown-editor-link-dialog__spacer {
134
+ flex: 1;
135
+ }
136
+ </style>
@@ -0,0 +1,10 @@
1
+ import type { EditorMessages } from '../messages/messages.types';
2
+ import type { LinkDialogController, LinkDialogRequest } from '../states/linkDialogController.svelte';
3
+ interface Props {
4
+ request: LinkDialogRequest | null;
5
+ controller: LinkDialogController;
6
+ text: EditorMessages;
7
+ }
8
+ declare const LinkDialog: import("svelte").Component<Props, {}, "">;
9
+ type LinkDialog = ReturnType<typeof LinkDialog>;
10
+ export default LinkDialog;
@@ -1,121 +1,80 @@
1
1
  <!--
2
2
  リンクを押して(タップして)カーソルを入れたときに出す吹き出し。リンク先を見せ、押すとそこを開く。
3
+ 右に、リンクを直す「編集」と「リンクを外す」をアイコンだけで並べる。
3
4
 
4
- **ポップアップ(`Popup`)には載せない。本文の中に絶対配置で置く。** 吹き出しはリンクに
5
- 付いているものであり、本文と一緒に動くのが正しい。画面に固定して重ねると、スクロール
6
- のたびに閉じるか置き直すかを迫られ、スマートフォンではキーボードが出るだけで本文が
7
- 動いて閉じてしまう。本文の中に置けば、リンクとの相対位置はスクロールでもキーボードでも
8
- 変わらない。
5
+ 本文の中に浮かせる(→ `AnchoredLayer`)。リンクの先頭に左端をそろえ、上に出す。
6
+ タッチ端末では画面の下半分をキーボードが覆う。上に収まらないときだけ下へ返る。
9
7
 
10
- 位置は「リンクの矩形 − この層の矩形」で決める。**同じ瞬間に同じ測り方で測った差**なので、
11
- 表示領域の基準(スクロール位置・キーボードで縮んだ範囲)がどうであっても打ち消し合う。
12
-
13
- - **焦点を奪わない。** 焦点が編集エリアを離れると吹き出しは消えるので、奪うと
14
- 押す前に消える。タッチ端末ではキーボードも引っ込む。押しても焦点が動かないよう、
15
- `mousedown` を止める
16
- - **リンクの上に出す。** タッチ端末では画面の下半分をキーボードが覆う。エディタの
17
- 上端か表示領域の上端を越えるときだけ下へ返す
8
+ **焦点を奪わない。** 焦点が編集エリアを離れると吹き出しは消えるので、奪うと押す前に
9
+ 消える。タッチ端末ではキーボードも引っ込む。押しても焦点が動かないよう、`mousedown` を止める。
18
10
  -->
19
11
  <script lang="ts">
12
+ import type { EditorMessages } from '../messages/messages.types';
20
13
  import type { LinkPopoverController } from '../states/linkPopoverController.svelte';
14
+ import AnchoredLayer from './internal/AnchoredLayer.svelte';
15
+ import Icon from './internal/Icon.svelte';
21
16
 
22
17
  interface Props {
23
18
  controller: LinkPopoverController;
24
- /** 吹き出しのアクセシブルな名前。リンク先を後ろに続けて読ませる */
25
- openLabel: string;
19
+ text: EditorMessages;
26
20
  }
27
21
 
28
- let { controller, openLabel }: Props = $props();
29
-
30
- /** リンクと吹き出しのあいだ */
31
- const GAP = 6;
32
- /** 表示領域の端から空ける距離 */
33
- const MARGIN = 8;
34
-
35
- let layerRef: HTMLDivElement | undefined = $state();
36
- let popoverRef: HTMLDivElement | undefined = $state();
37
-
38
- /**
39
- * 出しているあいだ、リンクが動くたびに置き直す。本文が折り返し直す(表示領域の幅が
40
- * 変わる)ときも置き直す。スクロールでは置き直さない——本文と一緒に動くからである。
41
- */
42
- $effect(() => {
43
- if (!controller.link || !popoverRef) return;
44
-
45
- place();
46
- window.addEventListener('resize', place);
47
- return () => window.removeEventListener('resize', place);
48
- });
49
-
50
- /** リンクの先頭に左端をそろえ、上に置く。収まらないときは下へ返し、横は中へ寄せる */
51
- function place(): void {
52
- const anchor = controller.measure();
53
- const layer = layerRef;
54
- const popover = popoverRef;
55
- const editor = layer?.parentElement;
56
- if (!anchor || !layer || !popover || !editor) return;
57
-
58
- const origin = layer.getBoundingClientRect();
59
- const bounds = editor.getBoundingClientRect();
60
- const width = popover.offsetWidth;
61
- const height = popover.offsetHeight;
62
-
63
- const above = anchor.top - GAP - height;
64
- const top = above >= Math.max(bounds.top, 0) ? above : anchor.bottom + GAP;
65
-
66
- const minLeft = Math.max(bounds.left, MARGIN);
67
- const maxLeft = Math.min(bounds.right, window.innerWidth - MARGIN) - width;
68
- const left = Math.max(minLeft, Math.min(anchor.left, maxLeft));
69
-
70
- popover.style.top = `${top - origin.top}px`;
71
- popover.style.left = `${left - origin.left}px`;
72
- }
22
+ let { controller, text }: Props = $props();
73
23
 
74
24
  function keepFocus(event: MouseEvent): void {
75
25
  event.preventDefault();
76
26
  }
77
27
  </script>
78
28
 
79
- <!-- 位置の原点にする層。高さを持たず、並びを押し広げない -->
80
- <div class="markdown-editor-link-popover-layer" bind:this={layerRef}>
81
- {#if controller.link}
29
+ <AnchoredLayer anchor={controller.link} measure={controller.measure} side="above">
30
+ {#snippet children(attach)}
82
31
  <!-- 押しても焦点を移さないための受け止め。キーで届く操作はボタンが持つ -->
83
32
  <!-- svelte-ignore a11y_no_static_element_interactions -->
84
- <div class="markdown-editor-link-popover" bind:this={popoverRef} onmousedown={keepFocus}>
85
- <!-- 吹き出し全体を 1 つのボタンにする。押せる面が広いほどタッチ端末で押しやすい -->
33
+ <div class="markdown-editor-link-popover" {@attach attach} onmousedown={keepFocus}>
34
+ <!-- リンク先の表示ごと 1 つのボタンにする。押せる面が広いほどタッチ端末で押しやすい -->
86
35
  <button
87
36
  type="button"
88
37
  class="markdown-editor-link-popover__open"
89
- aria-label="{openLabel}: {controller.link.href}"
90
- title={controller.link.href}
38
+ aria-label="{text.linkOpen}: {controller.link?.href}"
39
+ title={controller.link?.href}
91
40
  onclick={controller.open}
92
41
  >
93
42
  <!-- 新しいタブで開くことの印。吹き出しはリンクの先頭にそろうので、左に置けば押す目当てが近い -->
94
- <svg class="markdown-editor-link-popover__icon" viewBox="0 0 16 16" aria-hidden="true">
95
- <path d="M6 3H3v10h10v-3M9 3h4v4M13 3 7 9" />
96
- </svg>
97
- <span class="markdown-editor-link-popover__target">{controller.link.href}</span>
43
+ <Icon name="open_in_new" />
44
+ <span class="markdown-editor-link-popover__target">{controller.link?.href}</span>
45
+ </button>
46
+ <button
47
+ type="button"
48
+ class="markdown-editor-link-popover__action"
49
+ aria-label={text.linkEdit}
50
+ title={text.linkEdit}
51
+ onclick={controller.edit}
52
+ >
53
+ <Icon name="edit" />
54
+ </button>
55
+ <button
56
+ type="button"
57
+ class="markdown-editor-link-popover__action"
58
+ aria-label={text.linkRemove}
59
+ title={text.linkRemove}
60
+ onclick={controller.remove}
61
+ >
62
+ <Icon name="link_off" />
98
63
  </button>
99
64
  </div>
100
- {/if}
101
- </div>
65
+ {/snippet}
66
+ </AnchoredLayer>
102
67
 
103
68
  <style>
104
69
  /**
105
- * 本文の上へ重ねるため、自分で重なりの文脈を作る(本文の中の位置付きの要素より手前)。
106
- */
107
- .markdown-editor-link-popover-layer {
108
- position: relative;
109
- z-index: 1;
110
- height: 0;
111
- }
112
-
113
- /**
70
+ * 位置は `AnchoredLayer` が決める。
71
+ *
114
72
  * 幅の上限は**エディタの幅**で抑える(層はエディタと同じ幅)。表示領域の幅で抑えると、
115
73
  * エディタが画面より狭いとき(スマートフォンで左右に余白がある置き方)にはみ出す。
116
74
  */
117
75
  .markdown-editor-link-popover {
118
76
  position: absolute;
77
+ display: flex;
119
78
  box-sizing: border-box;
120
79
  max-width: min(360px, 100%);
121
80
  border: 1px solid var(--markdown-editor-link-popover-border);
@@ -148,10 +107,13 @@
148
107
  .markdown-editor-link-popover__open {
149
108
  box-sizing: border-box;
150
109
  display: flex;
110
+ flex: 1;
151
111
  align-items: center;
152
112
  gap: 6px;
153
- max-width: 100%;
154
- padding: 8px 12px;
113
+ min-width: 0;
114
+ /* 高さは選択ツールバーと揃える */
115
+ height: 36px;
116
+ padding: 0 12px;
155
117
  border: 0;
156
118
  background: none;
157
119
  color: var(--markdown-editor-link-popover-color);
@@ -169,20 +131,24 @@
169
131
  text-overflow: ellipsis;
170
132
  }
171
133
 
172
- .markdown-editor-link-popover__icon {
134
+ .markdown-editor-link-popover__action {
135
+ display: flex;
173
136
  flex: none;
174
- width: 1em;
175
- height: 1em;
176
- fill: none;
177
- stroke: currentColor;
178
- stroke-width: 1.5;
179
- stroke-linecap: round;
180
- stroke-linejoin: round;
137
+ align-items: center;
138
+ justify-content: center;
139
+ width: 36px;
140
+ padding: 0;
141
+ border: 0;
142
+ background: none;
143
+ color: var(--markdown-editor-link-popover-icon);
144
+ font: inherit;
145
+ cursor: pointer;
181
146
  }
182
147
 
183
148
  /* 触れる入力しかない環境では `:hover` が居座るため、囲って外す */
184
149
  @media (hover: hover) {
185
- .markdown-editor-link-popover__open:hover {
150
+ .markdown-editor-link-popover__open:hover,
151
+ .markdown-editor-link-popover__action:hover {
186
152
  background: var(--markdown-editor-link-popover-hover);
187
153
  }
188
154
  }
@@ -190,7 +156,11 @@
190
156
  /* 触れる端末でのターゲットの大きさ */
191
157
  @media (max-width: 768px) {
192
158
  .markdown-editor-link-popover__open {
193
- min-height: var(--markdown-editor-touch-target);
159
+ height: var(--markdown-editor-touch-target);
160
+ }
161
+
162
+ .markdown-editor-link-popover__action {
163
+ width: var(--markdown-editor-touch-target);
194
164
  }
195
165
  }
196
166
  </style>
@@ -1,8 +1,8 @@
1
+ import type { EditorMessages } from '../messages/messages.types';
1
2
  import type { LinkPopoverController } from '../states/linkPopoverController.svelte';
2
3
  interface Props {
3
4
  controller: LinkPopoverController;
4
- /** 吹き出しのアクセシブルな名前。リンク先を後ろに続けて読ませる */
5
- openLabel: string;
5
+ text: EditorMessages;
6
6
  }
7
7
  declare const LinkPopover: import("svelte").Component<Props, {}, "">;
8
8
  type LinkPopover = ReturnType<typeof LinkPopover>;