@14ch/svelte-md-outliner 0.0.25 → 0.0.26

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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,27 @@ 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.26] - 2026-11-02
9
+
10
+ ### Added
11
+
12
+ - While editing, a bubble showing the link target appears above a link whenever
13
+ the cursor touches it. Clicking or tapping the bubble opens the link, so links
14
+ can now be opened on touch devices, which have no ⌘/Ctrl key. ⌘-click
15
+ (Ctrl-click outside macOS) still opens a link directly. The bubble also calls
16
+ `onLinkClick` first.
17
+ - `linkOpen` message: the accessible name of the bubble. Add it if you pass a
18
+ full message set for a language that is not bundled.
19
+ - Theme tokens for the bubble: `--markdown-editor-url-bubble-color` and
20
+ `--markdown-editor-url-bubble-hover`.
21
+
22
+ ### Fixed
23
+
24
+ - ⌘-clicking a link now brings the new tab to the front instead of opening it
25
+ in the background.
26
+ - A plain click on a link while editing no longer navigates the page in apps
27
+ whose router handles link clicks (such as SvelteKit).
28
+
8
29
  ## [0.0.25] - 2026-10-29
9
30
 
10
31
  ### Added
package/README.ja.md CHANGED
@@ -91,7 +91,10 @@ const uploadImage = async (file: File) => {
91
91
 
92
92
  ### リンク
93
93
 
94
- リンクを押すと、リンク先が新しいタブで開く。編集中は ⌘+クリック(Mac 以外は Ctrl+クリック)、読み取り専用では素のクリックで開く。編集中の素のクリックはカーソルを置く操作のままである。
94
+ リンク先は新しいタブで開く。
95
+
96
+ - **編集中**は、カーソルがリンクに触れているあいだ、リンクの上にリンク先を見せる吹き出しが出て、吹き出しを押すと開く。リンクの素のクリック(タップ)はカーソルを置く操作のままなので、タッチ端末でも「リンクをタップして、吹き出しをタップ」で開ける。⌘+クリック(Mac 以外は Ctrl+クリック)なら吹き出しを経ずに開く
97
+ - **読み取り専用**では、素のクリックで開く
95
98
 
96
99
  開く前に `onLinkClick` が書かれたとおりの `href` を受け取る(相対のままでも解決しない)。`event.preventDefault()` を呼べば、新しいタブでは開かない。`./other.md` のように、ブラウザで開いても意味を持たない行き先を利用側で扱うときに使う。
97
100
 
package/README.md CHANGED
@@ -91,7 +91,10 @@ While it waits, a marker is shown at the paste position and **the document is no
91
91
 
92
92
  ### Links
93
93
 
94
- Clicking a link opens its target in a new tab: ⌘-click (Ctrl-click outside macOS) while editing, a plain click while `readonly`. A plain click while editing still just places the cursor.
94
+ Links open in a new tab.
95
+
96
+ - **While editing**, a bubble showing the target appears above the link whenever the cursor touches it; clicking (or tapping) the bubble opens the link. A plain click on the link still just places the cursor, so touch devices open links with "tap the link, then tap the bubble". ⌘-click (Ctrl-click outside macOS) opens directly, skipping the bubble
97
+ - **While `readonly`**, a plain click opens the link
95
98
 
96
99
  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.
97
100
 
@@ -382,6 +382,10 @@
382
382
  --markdown-editor-popup-shadow: var(--markdown-editor-shadow);
383
383
  --markdown-editor-popup-shadow-weak: var(--markdown-editor-shadow-weak);
384
384
 
385
+ /* カーソルがリンクにあるあいだ出す吹き出し */
386
+ --markdown-editor-url-bubble-color: var(--markdown-editor-hyperlink);
387
+ --markdown-editor-url-bubble-hover: var(--markdown-editor-hover-overlay);
388
+
385
389
  /* コンテキストメニュー */
386
390
  --markdown-editor-menu-separator: var(--markdown-editor-border);
387
391
  --markdown-editor-menu-group-title-color: var(--markdown-editor-text-subtle);
@@ -12,7 +12,7 @@
12
12
  import { formatShortcutKeys } from '../operations/keymap/shortcutKeys.js';
13
13
  import type { EditorAction, ShortcutCategory } from '../operations/operations.types';
14
14
  import type { ContextMenuController } from '../states/contextMenuController.svelte';
15
- import Popup from './internal/Popup.svelte';
15
+ import MenuPopup from './internal/MenuPopup.svelte';
16
16
 
17
17
  interface Props {
18
18
  controller: ContextMenuController;
@@ -84,8 +84,8 @@
84
84
  /**
85
85
  * 選んでいる項目へ実フォーカスを移す。
86
86
  *
87
- * 何も選んでいないあいだはコンテナにフォーカスが載ったままで、`Popup` が開くときに
88
- * そこへ移している。移し先がコンテナの中なら閉じない(`Popup` の `onFocusOut` が
87
+ * 何も選んでいないあいだはコンテナにフォーカスが載ったままで、`MenuPopup` が開くときに
88
+ * そこへ移している。移し先がコンテナの中なら閉じない(`MenuPopup` の `onFocusOut` が
89
89
  * 移り先を見ている)。
90
90
  */
91
91
  function focusActive(): void {
@@ -115,7 +115,7 @@
115
115
  });
116
116
  </script>
117
117
 
118
- <Popup
118
+ <MenuPopup
119
119
  open={controller.isOpen}
120
120
  anchor={controller.anchor}
121
121
  class="markdown-editor-context-menu"
@@ -167,7 +167,7 @@
167
167
  </ul>
168
168
  {/each}
169
169
  </div>
170
- </Popup>
170
+ </MenuPopup>
171
171
 
172
172
  <style>
173
173
  /**
@@ -0,0 +1,120 @@
1
+ <!--
2
+ カーソルがリンクにあるあいだ出す吹き出し。リンク先を見せ、押すとそこを開く。
3
+
4
+ 土台(`Popup`)に載せ、吹き出しの振る舞いをここに書く。
5
+
6
+ - **焦点を奪わない。** 焦点が編集エリアを離れると吹き出しは消えるので、奪うと押す
7
+ 押す前に消える。タッチ端末ではキーボードも引っ込む。押しても焦点が動かないよう、
8
+ `mousedown` を止める
9
+ - **リンクの上に出す。** タッチ端末では画面の下半分をキーボードが覆う。上に入らない
10
+ ときだけ下へ返す
11
+ - **外の動きでは閉じず、付いていく。** 閉じると、カーソルを動かし直すまで出てこない
12
+ -->
13
+ <script lang="ts">
14
+ import type { LinkBubbleController } from '../states/linkBubbleController.svelte';
15
+ import type { AnchorRect } from '../utils/geometry.types';
16
+ import Popup from './internal/Popup.svelte';
17
+
18
+ interface Props {
19
+ controller: LinkBubbleController;
20
+ /** 吹き出しのアクセシブルな名前。リンク先を後ろに続けて読ませる */
21
+ openLabel: string;
22
+ }
23
+
24
+ let { controller, openLabel }: Props = $props();
25
+
26
+ /** リンクと吹き出しのあいだ */
27
+ const GAP = 6;
28
+ /** 表示領域の端から空ける距離 */
29
+ const MARGIN = 8;
30
+
31
+ /** リンクの先頭に左端をそろえ、上に置く。はみ出すときは表示領域の中へ寄せる */
32
+ function place(element: HTMLDivElement, rect: AnchorRect): void {
33
+ const box = element.getBoundingClientRect();
34
+
35
+ const above = rect.top - GAP - box.height;
36
+ const top = above >= MARGIN ? above : rect.bottom + GAP;
37
+ const left = Math.max(MARGIN, Math.min(rect.left, window.innerWidth - box.width - MARGIN));
38
+
39
+ element.style.transform = `translate(${left}px, ${top}px)`;
40
+ }
41
+
42
+ function keepFocus(event: MouseEvent): void {
43
+ event.preventDefault();
44
+ }
45
+ </script>
46
+
47
+ <Popup
48
+ open={controller.href !== null}
49
+ anchor={controller.anchor}
50
+ {place}
51
+ class="markdown-editor-link-bubble"
52
+ onViewportChange={controller.refresh}
53
+ onmousedown={keepFocus}
54
+ >
55
+ <!-- 吹き出し全体を 1 つのボタンにする。押せる面が広いほどタッチ端末で押しやすい -->
56
+ <button
57
+ type="button"
58
+ class="markdown-editor-link-bubble__open"
59
+ aria-label="{openLabel}: {controller.href}"
60
+ title={controller.href}
61
+ onclick={controller.open}
62
+ >
63
+ <!-- 新しいタブで開くことの印。吹き出しはリンクの先頭にそろうので、左に置けば押す目当てが近い -->
64
+ <svg class="markdown-editor-link-bubble__icon" viewBox="0 0 16 16" aria-hidden="true">
65
+ <path d="M6 3H3v10h10v-3M9 3h4v4M13 3 7 9" />
66
+ </svg>
67
+ <span class="markdown-editor-link-bubble__target">{controller.href}</span>
68
+ </button>
69
+ </Popup>
70
+
71
+ <style>
72
+ /** 行間は本文のものを引き継がない(地の文ではない) */
73
+ .markdown-editor-link-bubble__open {
74
+ display: flex;
75
+ align-items: center;
76
+ gap: 6px;
77
+ max-width: min(360px, calc(100vw - 16px));
78
+ padding: 8px 12px;
79
+ border: 0;
80
+ background: none;
81
+ color: var(--markdown-editor-url-bubble-color);
82
+ font: inherit;
83
+ font-size: var(--markdown-editor-font-size-sm);
84
+ line-height: normal;
85
+ text-align: left;
86
+ cursor: pointer;
87
+ }
88
+
89
+ .markdown-editor-link-bubble__target {
90
+ min-width: 0;
91
+ overflow: hidden;
92
+ text-overflow: ellipsis;
93
+ white-space: nowrap;
94
+ }
95
+
96
+ .markdown-editor-link-bubble__icon {
97
+ flex: none;
98
+ width: 1em;
99
+ height: 1em;
100
+ fill: none;
101
+ stroke: currentColor;
102
+ stroke-width: 1.5;
103
+ stroke-linecap: round;
104
+ stroke-linejoin: round;
105
+ }
106
+
107
+ /* 触れる入力しかない環境では `:hover` が居座るため、囲って外す */
108
+ @media (hover: hover) {
109
+ .markdown-editor-link-bubble__open:hover {
110
+ background: var(--markdown-editor-url-bubble-hover);
111
+ }
112
+ }
113
+
114
+ /* 触れる端末でのターゲットの大きさ */
115
+ @media (max-width: 768px) {
116
+ .markdown-editor-link-bubble__open {
117
+ min-height: var(--markdown-editor-touch-target);
118
+ }
119
+ }
120
+ </style>
@@ -0,0 +1,9 @@
1
+ import type { LinkBubbleController } from '../states/linkBubbleController.svelte';
2
+ interface Props {
3
+ controller: LinkBubbleController;
4
+ /** 吹き出しのアクセシブルな名前。リンク先を後ろに続けて読ませる */
5
+ openLabel: string;
6
+ }
7
+ declare const LinkBubble: import("svelte").Component<Props, {}, "">;
8
+ type LinkBubble = ReturnType<typeof LinkBubble>;
9
+ export default LinkBubble;
@@ -93,7 +93,8 @@
93
93
  /**
94
94
  * リンクが押されたときの通知。`href` は書かれたとおりの値で、相対のままでも解決しない。
95
95
  *
96
- * 編集中は ⌘+クリック(Mac 以外は Ctrl+クリック)、読み取り専用では素のクリックで呼ばれる。
96
+ * 編集中はカーソルがリンクにあるあいだ出る吹き出しを押したときと、⌘+クリック
97
+ * (Mac 以外は Ctrl+クリック)で、読み取り専用では素のクリックで呼ばれる。
97
98
  * `event.preventDefault()` を呼べば、新しいタブで開く既定の動きを止められる。
98
99
  * 呼ばなければ(または渡さなければ)新しいタブで開く。
99
100
  */
@@ -191,11 +192,13 @@
191
192
  import { resolveMessages } from '../messages/index.js';
192
193
  import { EMPTY_VIEW_STATE, isSameViewState } from '../models/viewState/viewStateCodec.js';
193
194
  import { createContextMenuController } from '../states/contextMenuController.svelte';
195
+ import { createLinkBubbleController } from '../states/linkBubbleController.svelte';
194
196
  import { createOutlineViewController } from '../states/outlineViewController.svelte';
195
197
  import type { EditorSession } from '../states/editorSession.types';
196
198
  import ContextMenu from './ContextMenu.svelte';
197
199
  import EditorSurface from './EditorSurface.svelte';
198
200
  import FocusBreadcrumb from './FocusBreadcrumb.svelte';
201
+ import LinkBubble from './LinkBubble.svelte';
199
202
  import ShortcutHelpDialog from './ShortcutHelpDialog.svelte';
200
203
 
201
204
  // --- props ---
@@ -274,6 +277,11 @@
274
277
  isEnabled: () => contextMenu
275
278
  });
276
279
 
280
+ const linkBubble = createLinkBubbleController({
281
+ getSession: () => session,
282
+ getLinkClick: () => onLinkClick
283
+ });
284
+
277
285
  /** 一覧へ渡す拡張の宣言。**両方のコントローラのぶんを渡す**(メニューの割り当ても一覧に出す) */
278
286
  const listedActions = $derived([
279
287
  ...controller.extension.actions,
@@ -295,7 +303,7 @@
295
303
  */
296
304
  imageUpload: () => uploadImage,
297
305
  linkClick: () => onLinkClick,
298
- extensions: [controller.extension, menuController.extension]
306
+ extensions: [controller.extension, menuController.extension, linkBubble.extension]
299
307
  };
300
308
 
301
309
  // --- 関数 ---
@@ -477,4 +485,5 @@
477
485
  {#if contextMenu}
478
486
  <ContextMenu controller={menuController} {text} />
479
487
  {/if}
488
+ <LinkBubble controller={linkBubble} openLabel={text.linkOpen} />
480
489
  </div>
@@ -87,7 +87,8 @@ export interface MarkdownEditorProps {
87
87
  /**
88
88
  * リンクが押されたときの通知。`href` は書かれたとおりの値で、相対のままでも解決しない。
89
89
  *
90
- * 編集中は ⌘+クリック(Mac 以外は Ctrl+クリック)、読み取り専用では素のクリックで呼ばれる。
90
+ * 編集中はカーソルがリンクにあるあいだ出る吹き出しを押したときと、⌘+クリック
91
+ * (Mac 以外は Ctrl+クリック)で、読み取り専用では素のクリックで呼ばれる。
91
92
  * `event.preventDefault()` を呼べば、新しいタブで開く既定の動きを止められる。
92
93
  * 呼ばなければ(または渡さなければ)新しいタブで開く。
93
94
  */
@@ -0,0 +1,145 @@
1
+ <!--
2
+ メニューとして開くポップアップ。土台(`Popup`)に、メニューの振る舞いを足す。
3
+
4
+ - 開くと焦点を中へ移し、`Tab` を中で循環させる
5
+ - 錨の右上に置く
6
+ - 外の動き(スクロール・リサイズ)で閉じたいことを `onClose` で伝える。自分では閉じない
7
+ -->
8
+ <script lang="ts">
9
+ import type { Snippet } from 'svelte';
10
+
11
+ import type { AnchorRect } from '../../utils/geometry.types';
12
+ import Popup from './Popup.svelte';
13
+
14
+ interface Props {
15
+ children: Snippet;
16
+ /** 錨の矩形。開くたびに読み直す */
17
+ anchor: AnchorRect | null;
18
+ open: boolean;
19
+ /** コンテナの役割。既定は menu */
20
+ role?: string;
21
+ ariaLabel?: string;
22
+ /** いま選んでいる子要素の id。中身の側は実フォーカスもそこへ移す */
23
+ ariaActivedescendant?: string;
24
+ class?: string;
25
+ /**
26
+ * 中で押されたキー。`Tab` の循環より**先**に呼ぶ。
27
+ * 既定動作を止めた(受け止めた)ときは、循環の判定へ進まない
28
+ */
29
+ onKeyDown?: (event: KeyboardEvent) => void;
30
+ /** 操作対象がポップアップの外へ移ったときの通知。中での移動では呼ばない */
31
+ onFocusOut?: () => void;
32
+ /** 外の動き(スクロール・リサイズ)で閉じたいときの通知。自分では閉じない */
33
+ onClose?: () => void;
34
+ }
35
+
36
+ let {
37
+ children,
38
+ anchor,
39
+ open,
40
+ role = 'menu',
41
+ ariaLabel,
42
+ ariaActivedescendant,
43
+ class: className = '',
44
+ onKeyDown,
45
+ onFocusOut,
46
+ onClose
47
+ }: Props = $props();
48
+
49
+ /** 錨と表示領域の端から空ける距離 */
50
+ const MARGIN = 8;
51
+
52
+ /** 開いた直後は何も選んでいないので、操作対象の行き先はコンテナになる */
53
+ function focusOnOpen(element: HTMLDivElement): void {
54
+ element.focus();
55
+ }
56
+
57
+ /**
58
+ * 錨の**右上**に置き、表示領域からはみ出すときは中へ寄せる。
59
+ * 高さが収まらないときは、押し込めるだけ押し込んで中をスクロールさせる。
60
+ *
61
+ * 右へ出すのは、錨そのもの(対象にしている行)を覆わないためである。
62
+ * 上端をそろえるので、どの行に対して開いたのかが目で追える。
63
+ */
64
+ function place(element: HTMLDivElement, rect: AnchorRect): void {
65
+ // 前回の寄せの跡を消してから測る(残すと 2 回目以降が前回の高さに縛られる)
66
+ element.style.maxHeight = '';
67
+ element.style.overflowY = '';
68
+
69
+ const box = element.getBoundingClientRect();
70
+ const viewport = { width: window.innerWidth, height: window.innerHeight };
71
+
72
+ let top = rect.top;
73
+ // 下に入らず、上のほうが広ければ、錨の下端に合わせて上へ返す
74
+ const below = viewport.height - top - MARGIN;
75
+ const above = rect.bottom - MARGIN * 2;
76
+ if (box.height > below && above > below) top = rect.bottom - box.height;
77
+
78
+ const left = clamp(rect.right + MARGIN, MARGIN, viewport.width - box.width - MARGIN);
79
+ top = clamp(top, MARGIN, viewport.height - box.height - MARGIN);
80
+
81
+ const available = viewport.height - top - MARGIN;
82
+ if (box.height > available) {
83
+ element.style.maxHeight = `${available}px`;
84
+ element.style.overflowY = 'auto';
85
+ }
86
+
87
+ element.style.transform = `translate(${left}px, ${top}px)`;
88
+ }
89
+
90
+ /** 収まらない値を渡されたら手前の端に付ける(`max < min` でも表示領域の外へ出さない) */
91
+ function clamp(value: number, min: number, max: number): number {
92
+ return Math.max(min, Math.min(value, max));
93
+ }
94
+
95
+ /** 移り先がポップアップの中なら、外へ出たことにはしない */
96
+ function handleFocusOut(event: FocusEvent & { currentTarget: HTMLDivElement }): void {
97
+ const next = event.relatedTarget;
98
+ if (next instanceof Node && event.currentTarget.contains(next)) return;
99
+ onFocusOut?.();
100
+ }
101
+
102
+ /** 中で押されたキー。使う側へ先に渡し、残りは Tab の循環だけを見る */
103
+ function handleKeydown(event: KeyboardEvent & { currentTarget: HTMLDivElement }): void {
104
+ onKeyDown?.(event);
105
+ if (event.defaultPrevented) return;
106
+ cycleTab(event);
107
+ }
108
+
109
+ /** Tab を中で循環させる */
110
+ function cycleTab(event: KeyboardEvent & { currentTarget: HTMLDivElement }): void {
111
+ if (event.key !== 'Tab') return;
112
+
113
+ const focusable = event.currentTarget.querySelectorAll<HTMLElement>(
114
+ 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
115
+ );
116
+ if (focusable.length === 0) return;
117
+
118
+ const first = focusable[0];
119
+ const last = focusable[focusable.length - 1];
120
+ if (event.shiftKey && document.activeElement === first) {
121
+ event.preventDefault();
122
+ last.focus();
123
+ } else if (!event.shiftKey && document.activeElement === last) {
124
+ event.preventDefault();
125
+ first.focus();
126
+ }
127
+ }
128
+ </script>
129
+
130
+ <Popup
131
+ {anchor}
132
+ {open}
133
+ {place}
134
+ class={className}
135
+ {role}
136
+ tabindex={-1}
137
+ aria-label={ariaLabel}
138
+ aria-activedescendant={ariaActivedescendant}
139
+ onOpen={focusOnOpen}
140
+ onViewportChange={onClose}
141
+ onkeydown={handleKeydown}
142
+ onfocusout={handleFocusOut}
143
+ >
144
+ {@render children()}
145
+ </Popup>
@@ -0,0 +1,26 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { AnchorRect } from '../../utils/geometry.types';
3
+ interface Props {
4
+ children: Snippet;
5
+ /** 錨の矩形。開くたびに読み直す */
6
+ anchor: AnchorRect | null;
7
+ open: boolean;
8
+ /** コンテナの役割。既定は menu */
9
+ role?: string;
10
+ ariaLabel?: string;
11
+ /** いま選んでいる子要素の id。中身の側は実フォーカスもそこへ移す */
12
+ ariaActivedescendant?: string;
13
+ class?: string;
14
+ /**
15
+ * 中で押されたキー。`Tab` の循環より**先**に呼ぶ。
16
+ * 既定動作を止めた(受け止めた)ときは、循環の判定へ進まない
17
+ */
18
+ onKeyDown?: (event: KeyboardEvent) => void;
19
+ /** 操作対象がポップアップの外へ移ったときの通知。中での移動では呼ばない */
20
+ onFocusOut?: () => void;
21
+ /** 外の動き(スクロール・リサイズ)で閉じたいときの通知。自分では閉じない */
22
+ onClose?: () => void;
23
+ }
24
+ declare const MenuPopup: import("svelte").Component<Props, {}, "">;
25
+ type MenuPopup = ReturnType<typeof MenuPopup>;
26
+ export default MenuPopup;
@@ -1,53 +1,45 @@
1
1
  <!--
2
- 最前面へ出すコンテナ。`popover="manual"` で重ね、錨の矩形(AnchorRect)に対して置く。
2
+ 最前面へ出すコンテナの土台。`popover="manual"` で重ね、錨の矩形(AnchorRect)に対して置く。
3
3
  錨を要素ではなく矩形で受けるのは、ポインタの座標と要素の箱を同じ型に落とすため。
4
4
 
5
- 開閉は `open` prop で受ける(状態はエディタ 1 つにつき 1 つのコントローラが持つ)。
6
- スクロール・リサイズで閉じたいことは `onClose` で外へ伝えるだけで、自分では閉じない。
5
+ 持つのは、最前面への出し入れ・見た目・外の動きの通知だけである。**焦点・キー・
6
+ 置き場所は使う側が持つ。** メニューは開くと焦点を移して錨の右上に出るが、リンクの
7
+ 吹き出しは焦点を奪わずにリンクの上へ出る。それぞれの振る舞いは使う側の部品に書く
8
+ (→ `MenuPopup`・`LinkBubble`)。
9
+
10
+ 開閉は `open` prop で受ける。外の動き(スクロール・リサイズ)は `onViewportChange` で
11
+ 知らせるだけで、閉じるか付いていくかは使う側が決める。
7
12
  -->
8
13
  <script lang="ts">
9
14
  import type { Snippet } from 'svelte';
15
+ import type { HTMLAttributes } from 'svelte/elements';
10
16
 
11
17
  import type { AnchorRect } from '../../utils/geometry.types';
12
18
 
13
- interface Props {
19
+ interface Props extends HTMLAttributes<HTMLDivElement> {
14
20
  children: Snippet;
15
- /** 錨の矩形。開くたびに読み直す */
21
+ /** 錨の矩形。変わるたびに置き直す */
16
22
  anchor: AnchorRect | null;
17
23
  open: boolean;
18
- /** コンテナの役割。既定は menu */
19
- role?: string;
20
- ariaLabel?: string;
21
- /** いま選んでいる子要素の id。中身の側は実フォーカスもそこへ移す */
22
- ariaActivedescendant?: string;
23
- class?: string;
24
- /**
25
- * 中で押されたキー。`Tab` の循環より**先**に呼ぶ。
26
- * 既定動作を止めた(受け止めた)ときは、循環の判定へ進まない
27
- */
28
- onKeyDown?: (event: KeyboardEvent) => void;
29
- /** 操作対象がポップアップの外へ移ったときの通知。中での移動では呼ばない */
30
- onFocusOut?: () => void;
31
- /** 外の動き(スクロール・リサイズ)で閉じたいときの通知。自分では閉じない */
32
- onClose?: () => void;
24
+ /** 錨に対して置く。寸法を測れるよう、最前面へ出した後に呼ぶ */
25
+ place: (element: HTMLDivElement, anchor: AnchorRect) => void;
26
+ /** 出した直後の通知。焦点を移すかどうかは使う側が決める */
27
+ onOpen?: (element: HTMLDivElement) => void;
28
+ /** 外の動き(スクロール・リサイズ)の通知。中身のスクロールでは呼ばない */
29
+ onViewportChange?: () => void;
33
30
  }
34
31
 
35
32
  let {
36
33
  children,
37
34
  anchor,
38
35
  open,
39
- role = 'menu',
40
- ariaLabel,
41
- ariaActivedescendant,
36
+ place,
37
+ onOpen,
38
+ onViewportChange,
42
39
  class: className = '',
43
- onKeyDown,
44
- onFocusOut,
45
- onClose
40
+ ...attributes
46
41
  }: Props = $props();
47
42
 
48
- /** 錨と表示領域の端から空ける距離 */
49
- const MARGIN = 8;
50
-
51
43
  let popupRef: HTMLDivElement | undefined = $state();
52
44
 
53
45
  $effect(() => {
@@ -55,113 +47,37 @@
55
47
  if (!element) return;
56
48
 
57
49
  if (!open || !anchor) {
58
- hide(element);
50
+ if (element.matches(':popover-open')) element.hidePopover();
59
51
  return;
60
52
  }
61
53
 
62
- show(element, anchor);
63
- // 開いているあいだだけ、外の動きを見る
64
- window.addEventListener('resize', requestClose);
54
+ const opening = !element.matches(':popover-open');
55
+ if (opening) element.showPopover();
56
+ place(element, anchor);
57
+ if (opening) onOpen?.(element);
58
+ });
59
+
60
+ // 出しているあいだだけ、外の動きを見る
61
+ $effect(() => {
62
+ if (!open || !anchor) return;
63
+
64
+ window.addEventListener('resize', notifyViewportChange);
65
65
  document.addEventListener('scroll', handleScroll, true);
66
66
  return () => {
67
- window.removeEventListener('resize', requestClose);
67
+ window.removeEventListener('resize', notifyViewportChange);
68
68
  document.removeEventListener('scroll', handleScroll, true);
69
69
  };
70
70
  });
71
71
 
72
- function show(element: HTMLDivElement, rect: AnchorRect): void {
73
- if (!element.matches(':popover-open')) {
74
- element.showPopover();
75
- // 操作対象を中へ移す。開いた直後は何も選んでいないので、行き先はコンテナになる
76
- element.focus();
77
- }
78
- // 前回の寄せの跡を消してから測る(残すと 2 回目以降が前回の高さに縛られる)
79
- element.style.maxHeight = '';
80
- element.style.overflowY = '';
81
- place(element, rect);
82
- }
83
-
84
- function hide(element: HTMLDivElement): void {
85
- if (element.matches(':popover-open')) element.hidePopover();
86
- }
87
-
88
- /**
89
- * 錨の**右上**に置き、表示領域からはみ出すときは中へ寄せる。
90
- * 高さが収まらないときは、押し込めるだけ押し込んで中をスクロールさせる。
91
- *
92
- * 右へ出すのは、錨そのもの(対象にしている行)を覆わないためである。
93
- * 上端をそろえるので、どの行に対して開いたのかが目で追える。
94
- */
95
- function place(element: HTMLDivElement, rect: AnchorRect): void {
96
- const box = element.getBoundingClientRect();
97
- const viewport = { width: window.innerWidth, height: window.innerHeight };
98
-
99
- let top = rect.top;
100
- // 下に入らず、上のほうが広ければ、錨の下端に合わせて上へ返す
101
- const below = viewport.height - top - MARGIN;
102
- const above = rect.bottom - MARGIN * 2;
103
- if (box.height > below && above > below) top = rect.bottom - box.height;
104
-
105
- const left = clamp(rect.right + MARGIN, MARGIN, viewport.width - box.width - MARGIN);
106
- top = clamp(top, MARGIN, viewport.height - box.height - MARGIN);
107
-
108
- const available = viewport.height - top - MARGIN;
109
- if (box.height > available) {
110
- element.style.maxHeight = `${available}px`;
111
- element.style.overflowY = 'auto';
112
- }
113
-
114
- element.style.transform = `translate(${left}px, ${top}px)`;
115
- }
116
-
117
- /** 収まらない値を渡されたら手前の端に付ける(`max < min` でも表示領域の外へ出さない) */
118
- function clamp(value: number, min: number, max: number): number {
119
- return Math.max(min, Math.min(value, max));
72
+ function notifyViewportChange(): void {
73
+ onViewportChange?.();
120
74
  }
121
75
 
122
- function requestClose(): void {
123
- onClose?.();
124
- }
125
-
126
- /** 中身のスクロールは閉じるきっかけにしない */
76
+ /** 中身のスクロールは外の動きにしない */
127
77
  function handleScroll(event: Event): void {
128
78
  const target = event.target;
129
79
  if (target instanceof Node && popupRef?.contains(target)) return;
130
- requestClose();
131
- }
132
-
133
- /** 移り先がポップアップの中なら、外へ出たことにはしない */
134
- function handleFocusOut(event: FocusEvent): void {
135
- const next = event.relatedTarget;
136
- if (next instanceof Node && popupRef?.contains(next)) return;
137
- onFocusOut?.();
138
- }
139
-
140
- /** 中で押されたキー。使う側へ先に渡し、残りは Tab の循環だけを見る */
141
- function handleKeydown(event: KeyboardEvent): void {
142
- onKeyDown?.(event);
143
- if (event.defaultPrevented) return;
144
- cycleTab(event);
145
- }
146
-
147
- /** Tab を中で循環させる */
148
- function cycleTab(event: KeyboardEvent): void {
149
- if (event.key !== 'Tab' || !popupRef) return;
150
-
151
- const focusable = popupRef.querySelectorAll<HTMLElement>(
152
- 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
153
- );
154
- if (focusable.length === 0) return;
155
-
156
- const first = focusable[0];
157
- const last = focusable[focusable.length - 1];
158
- if (event.shiftKey && document.activeElement === first) {
159
- event.preventDefault();
160
- last.focus();
161
- } else if (!event.shiftKey && document.activeElement === last) {
162
- event.preventDefault();
163
- first.focus();
164
- }
80
+ notifyViewportChange();
165
81
  }
166
82
  </script>
167
83
 
@@ -169,12 +85,7 @@
169
85
  bind:this={popupRef}
170
86
  popover="manual"
171
87
  class="markdown-editor-popup {className}"
172
- {role}
173
- tabindex="-1"
174
- aria-label={ariaLabel}
175
- aria-activedescendant={ariaActivedescendant}
176
- onkeydown={handleKeydown}
177
- onfocusout={handleFocusOut}
88
+ {...attributes}
178
89
  >
179
90
  {@render children()}
180
91
  </div>
@@ -201,8 +112,7 @@
201
112
  overflow: hidden;
202
113
  /*
203
114
  * 巻き取りを中で止める。端まで来た続きが背後のページへ流れると、
204
- * ページが動いたぶんを**外側のスクロール**として受け取り(→ handleScroll)、
205
- * 中を読んでいるだけなのに閉じてしまう。
115
+ * ページが動いたぶんを**外側のスクロール**として受け取ってしまう(→ handleScroll)。
206
116
  */
207
117
  overscroll-behavior: contain;
208
118
  }
@@ -1,25 +1,17 @@
1
1
  import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
2
3
  import type { AnchorRect } from '../../utils/geometry.types';
3
- interface Props {
4
+ interface Props extends HTMLAttributes<HTMLDivElement> {
4
5
  children: Snippet;
5
- /** 錨の矩形。開くたびに読み直す */
6
+ /** 錨の矩形。変わるたびに置き直す */
6
7
  anchor: AnchorRect | null;
7
8
  open: boolean;
8
- /** コンテナの役割。既定は menu */
9
- role?: string;
10
- ariaLabel?: string;
11
- /** いま選んでいる子要素の id。中身の側は実フォーカスもそこへ移す */
12
- ariaActivedescendant?: string;
13
- class?: string;
14
- /**
15
- * 中で押されたキー。`Tab` の循環より**先**に呼ぶ。
16
- * 既定動作を止めた(受け止めた)ときは、循環の判定へ進まない
17
- */
18
- onKeyDown?: (event: KeyboardEvent) => void;
19
- /** 操作対象がポップアップの外へ移ったときの通知。中での移動では呼ばない */
20
- onFocusOut?: () => void;
21
- /** 外の動き(スクロール・リサイズ)で閉じたいときの通知。自分では閉じない */
22
- onClose?: () => void;
9
+ /** 錨に対して置く。寸法を測れるよう、最前面へ出した後に呼ぶ */
10
+ place: (element: HTMLDivElement, anchor: AnchorRect) => void;
11
+ /** 出した直後の通知。焦点を移すかどうかは使う側が決める */
12
+ onOpen?: (element: HTMLDivElement) => void;
13
+ /** 外の動き(スクロール・リサイズ)の通知。中身のスクロールでは呼ばない */
14
+ onViewportChange?: () => void;
23
15
  }
24
16
  declare const Popup: import("svelte").Component<Props, {}, "">;
25
17
  type Popup = ReturnType<typeof Popup>;
@@ -11,6 +11,7 @@ export const en = {
11
11
  mermaidLoadError: 'Failed to load the diagram renderer',
12
12
  imageUploading: 'Uploading image…',
13
13
  imageLoadError: 'Failed to load the image',
14
+ linkOpen: 'Open link',
14
15
  frontMatterLabel: 'Front Matter',
15
16
  shortcutCategories: {
16
17
  edit: 'Edit',
@@ -28,6 +28,7 @@ export function resolveMessages(lang = DEFAULT_LANG, overrides) {
28
28
  mermaidLoadError: overrides.mermaidLoadError ?? base.mermaidLoadError,
29
29
  imageUploading: overrides.imageUploading ?? base.imageUploading,
30
30
  imageLoadError: overrides.imageLoadError ?? base.imageLoadError,
31
+ linkOpen: overrides.linkOpen ?? base.linkOpen,
31
32
  frontMatterLabel: overrides.frontMatterLabel ?? base.frontMatterLabel
32
33
  };
33
34
  }
@@ -11,6 +11,7 @@ export const ja = {
11
11
  mermaidLoadError: '図の描画機能を読み込めませんでした',
12
12
  imageUploading: '画像を読み込んでいます…',
13
13
  imageLoadError: '画像を表示できませんでした',
14
+ linkOpen: 'リンク先を開く',
14
15
  frontMatterLabel: 'Front Matter',
15
16
  shortcutCategories: {
16
17
  // グループは利用者の意図で分ける。「ブロックの操作」のような総称は使わない
@@ -36,6 +36,8 @@ export interface EditorMessages {
36
36
  readonly imageUploading: string;
37
37
  /** 画像の読み込みに失敗したことを伝える表示 */
38
38
  readonly imageLoadError: string;
39
+ /** カーソルがリンクにあるときに出す吹き出し(押すとリンク先を開く)のアクセシブルな名前 */
40
+ readonly linkOpen: string;
39
41
  /**
40
42
  * フロントマターにタイトルが無いときに、その場所へ薄く出す名前。
41
43
  * 状態の説明(「未設定です」)ではなく、**そこが何であるか**の表示である
@@ -61,5 +63,6 @@ export interface EditorMessagesOverride {
61
63
  readonly mermaidLoadError?: string;
62
64
  readonly imageUploading?: string;
63
65
  readonly imageLoadError?: string;
66
+ readonly linkOpen?: string;
64
67
  readonly frontMatterLabel?: string;
65
68
  }
@@ -0,0 +1,14 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import type { EditorView } from 'prosemirror-view';
3
+ /** カーソルが触れているリンク。`from` はリンクの先頭の位置 */
4
+ export interface LinkAtCursor {
5
+ readonly href: string;
6
+ readonly from: number;
7
+ }
8
+ /**
9
+ * 吹き出しを出すリンクを見張り、変わるたびに知らせる。表示は持たない(→ `LinkBubble`)。
10
+ *
11
+ * 出すのは、編集中に焦点があり、カーソルがリンクに触れているときだけである。
12
+ * 読み取り専用では素のクリックで開くので出さない。変換の最中も出さない。
13
+ */
14
+ export declare function linkBubblePlugin(onChange: (view: EditorView, link: LinkAtCursor | null) => void): Plugin;
@@ -0,0 +1,55 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import { outlineSchema } from '../../models/document/schema.js';
3
+ import { isEditorFocused } from './editorFocus.js';
4
+ const LINK = outlineSchema.marks.link;
5
+ /**
6
+ * 吹き出しを出すリンクを見張り、変わるたびに知らせる。表示は持たない(→ `LinkBubble`)。
7
+ *
8
+ * 出すのは、編集中に焦点があり、カーソルがリンクに触れているときだけである。
9
+ * 読み取り専用では素のクリックで開くので出さない。変換の最中も出さない。
10
+ */
11
+ export function linkBubblePlugin(onChange) {
12
+ const notify = (view) => onChange(view, view.editable && !view.composing && isEditorFocused(view.state)
13
+ ? linkAtCursor(view.state)
14
+ : null);
15
+ return new Plugin({
16
+ view: (view) => {
17
+ notify(view);
18
+ return {
19
+ update: notify,
20
+ destroy: () => onChange(view, null)
21
+ };
22
+ }
23
+ });
24
+ }
25
+ /**
26
+ * カーソルが触れているリンクを返す。範囲を選んでいるときは返さない(選ぶのは直すためである)。
27
+ *
28
+ * 両端も含める。リンクの末尾をタップすると、カーソルはリンクの直後に立つ。
29
+ */
30
+ function linkAtCursor(state) {
31
+ const { selection } = state;
32
+ if (!selection.empty)
33
+ return null;
34
+ const $pos = selection.$from;
35
+ const mark = linkOf($pos.nodeAfter?.marks) ?? linkOf($pos.nodeBefore?.marks);
36
+ if (!mark)
37
+ return null;
38
+ // 同じリンクが続く区間の先頭を探す(書式が混ざると文字の節は分かれる)
39
+ const parent = $pos.parent;
40
+ let runStart = $pos.start();
41
+ let at = runStart;
42
+ for (let index = 0; index < parent.childCount; index += 1) {
43
+ const child = parent.child(index);
44
+ const end = at + child.nodeSize;
45
+ if (!mark.isInSet(child.marks))
46
+ runStart = end;
47
+ else if (at <= $pos.pos && $pos.pos <= end)
48
+ return { href: mark.attrs.href, from: runStart };
49
+ at = end;
50
+ }
51
+ return null;
52
+ }
53
+ function linkOf(marks) {
54
+ return marks?.find((mark) => mark.type === LINK);
55
+ }
@@ -11,7 +11,8 @@ export type LinkClickSource = () => LinkClick | undefined;
11
11
  *
12
12
  * 編集中は**修飾キー(Mac は ⌘、それ以外は Ctrl)を添えたときだけ**開く。
13
13
  * 素のクリックはカーソルを置く操作であり、奪うとリンクの文字を直せなくなる。
14
- * 読み取り専用では素のクリックで開く。
14
+ * 修飾キーの無いタッチ端末では、カーソルがリンクにあるあいだ出る吹き出しから開く
15
+ * (→ `linkBubble`)。読み取り専用では素のクリックで開く。
15
16
  *
16
17
  * 開く前に利用側へ知らせる。相対のリンク(`./other.md`)のように、ブラウザで開いても
17
18
  * 意味を持たない行き先は、利用側が引き取って扱う。
@@ -22,3 +23,12 @@ export type LinkClickSource = () => LinkClick | undefined;
22
23
  * ポインタの形で見せるためで、CSS からはキーの状態が読めない。
23
24
  */
24
25
  export declare function linkClickPlugin(getLinkClick: LinkClickSource): Plugin;
26
+ /**
27
+ * 利用側へ知らせ、止められなければ新しいタブで開く。
28
+ *
29
+ * **開くのは押した出来事を抜けてから。** 出来事の最中に開くと、実行環境は押されている
30
+ * 修飾キーを見て開き方を決め、⌘+クリックでは新しいタブを背後に回す。利用者は元の
31
+ * タブに残されたまま、何が起きたのかが分からない。抜けた後なら修飾キーは見られず、
32
+ * 新しいタブが前に出る(押した直後なので、ポップアップの制限にも掛からない)。
33
+ */
34
+ export declare function openLink(href: string, event: MouseEvent, getLinkClick: LinkClickSource): void;
@@ -5,7 +5,8 @@ import { isMacPlatform } from '../keymap/shortcutKeys.js';
5
5
  *
6
6
  * 編集中は**修飾キー(Mac は ⌘、それ以外は Ctrl)を添えたときだけ**開く。
7
7
  * 素のクリックはカーソルを置く操作であり、奪うとリンクの文字を直せなくなる。
8
- * 読み取り専用では素のクリックで開く。
8
+ * 修飾キーの無いタッチ端末では、カーソルがリンクにあるあいだ出る吹き出しから開く
9
+ * (→ `linkBubble`)。読み取り専用では素のクリックで開く。
9
10
  *
10
11
  * 開く前に利用側へ知らせる。相対のリンク(`./other.md`)のように、ブラウザで開いても
11
12
  * 意味を持たない行き先は、利用側が引き取って扱う。
@@ -28,24 +29,40 @@ export function linkClickPlugin(getLinkClick) {
28
29
  function handleClick(view, event, getLinkClick) {
29
30
  if (event.button !== 0)
30
31
  return false;
31
- if (view.editable && !isModifierHeld(event))
32
- return false;
33
32
  const target = event.target;
34
33
  if (!(target instanceof Element))
35
34
  return false;
36
35
  const link = target.closest('a.markdown-editor__link');
37
36
  if (!(link instanceof HTMLAnchorElement) || !view.dom.contains(link))
38
37
  return false;
38
+ // カーソルを置くだけのクリックでも、ページの遷移は止める。編集中のリンクを実行環境は
39
+ // たどらないが、置いた側のルーター(SvelteKit など)は文書全体でクリックを拾ってたどる。
40
+ // カーソルは押した時点(`mousedown`)で置かれているので、止めても妨げない
41
+ if (view.editable && !isModifierHeld(event)) {
42
+ event.preventDefault();
43
+ return false;
44
+ }
39
45
  const href = link.getAttribute('href');
40
46
  if (!href)
41
47
  return false;
42
- getLinkClick()?.(href, event);
43
- if (event.defaultPrevented)
44
- return true;
48
+ openLink(href, event, getLinkClick);
45
49
  event.preventDefault();
46
- window.open(href, '_blank', 'noopener,noreferrer');
47
50
  return true;
48
51
  }
52
+ /**
53
+ * 利用側へ知らせ、止められなければ新しいタブで開く。
54
+ *
55
+ * **開くのは押した出来事を抜けてから。** 出来事の最中に開くと、実行環境は押されている
56
+ * 修飾キーを見て開き方を決め、⌘+クリックでは新しいタブを背後に回す。利用者は元の
57
+ * タブに残されたまま、何が起きたのかが分からない。抜けた後なら修飾キーは見られず、
58
+ * 新しいタブが前に出る(押した直後なので、ポップアップの制限にも掛からない)。
59
+ */
60
+ export function openLink(href, event, getLinkClick) {
61
+ getLinkClick()?.(href, event);
62
+ if (event.defaultPrevented)
63
+ return;
64
+ setTimeout(() => window.open(href, '_blank', 'noopener,noreferrer'));
65
+ }
49
66
  function isModifierHeld(event) {
50
67
  return isMacPlatform() ? event.metaKey : event.ctrlKey;
51
68
  }
@@ -0,0 +1,27 @@
1
+ import { type LinkClickSource } from '../operations/plugins/linkClick.js';
2
+ import type { AnchorRect } from '../utils/geometry.types';
3
+ import type { EditorExtension, EditorSession } from './editorSession.types';
4
+ export interface LinkBubbleControllerOptions {
5
+ /** 現在のセッション。生き死にの管理はコンポーネントが持つ */
6
+ readonly getSession: () => EditorSession | null;
7
+ /** リンクが押されたときの受け手。呼ぶ時点の値を読む */
8
+ readonly getLinkClick: LinkClickSource;
9
+ }
10
+ export interface LinkBubbleController {
11
+ /** 基盤へ渡す拡張。セッション生成より前に取得する */
12
+ readonly extension: EditorExtension;
13
+ /** 吹き出しを出しているリンクの行き先。出していなければ `null` */
14
+ readonly href: string | null;
15
+ /** リンクの先頭の矩形 */
16
+ readonly anchor: AnchorRect | null;
17
+ /** 吹き出しが押された。利用側へ知らせ、止められなければ新しいタブで開く */
18
+ open(event: MouseEvent): void;
19
+ /** 錨を読み直す。外の動き(スクロール・リサイズ)に付いていく */
20
+ refresh(): void;
21
+ }
22
+ /**
23
+ * カーソルがリンクにあるあいだ出す吹き出しの状態を、エディタ 1 インスタンスに閉じて持つ。
24
+ *
25
+ * **エディタ 1 つにつき 1 つ生成する。** セッションは都度たどるだけで抱え込まない。
26
+ */
27
+ export declare function createLinkBubbleController(options: LinkBubbleControllerOptions): LinkBubbleController;
@@ -0,0 +1,42 @@
1
+ import { linkBubblePlugin } from '../operations/plugins/linkBubble.js';
2
+ import { openLink } from '../operations/plugins/linkClick.js';
3
+ /**
4
+ * カーソルがリンクにあるあいだ出す吹き出しの状態を、エディタ 1 インスタンスに閉じて持つ。
5
+ *
6
+ * **エディタ 1 つにつき 1 つ生成する。** セッションは都度たどるだけで抱え込まない。
7
+ */
8
+ export function createLinkBubbleController(options) {
9
+ let link = $state(null);
10
+ let anchor = $state(null);
11
+ const extension = {
12
+ plugins: [
13
+ linkBubblePlugin((view, next) => {
14
+ link = next;
15
+ anchor = next ? anchorOf(view, next) : null;
16
+ })
17
+ ],
18
+ actions: []
19
+ };
20
+ function anchorOf(view, target) {
21
+ const coords = view.coordsAtPos(target.from);
22
+ return { left: coords.left, right: coords.left, top: coords.top, bottom: coords.bottom };
23
+ }
24
+ return {
25
+ extension,
26
+ get href() {
27
+ return link?.href ?? null;
28
+ },
29
+ get anchor() {
30
+ return anchor;
31
+ },
32
+ open(event) {
33
+ if (link)
34
+ openLink(link.href, event, options.getLinkClick);
35
+ },
36
+ refresh() {
37
+ const view = options.getSession()?.getView();
38
+ if (view && link)
39
+ anchor = anchorOf(view, link);
40
+ }
41
+ };
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@14ch/svelte-md-outliner",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "An outliner-style Markdown editor for Svelte 5. Headings form a real section tree you can fold, focus, and re-nest from the keyboard.",