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

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,29 @@ 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.27] - 2026-11-02
9
+
10
+ ### Changed
11
+
12
+ - The link popover's frame no longer follows the `--markdown-editor-popup-*`
13
+ tokens. It has its own: `--markdown-editor-link-popover-background`,
14
+ `-border`, `-shadow-strong`, `-shadow` and `-shadow-weak`.
15
+ - Renamed the link bubble's theme tokens added in 0.0.26:
16
+ `--markdown-editor-url-bubble-color` → `--markdown-editor-link-popover-color`
17
+ and `--markdown-editor-url-bubble-hover` → `--markdown-editor-link-popover-hover`.
18
+ Update any overrides that use the old names.
19
+ - The link popover appears only when you click or tap a link. It no longer
20
+ appears when the cursor enters a link by keyboard, and typing or moving the
21
+ cursor with the keyboard hides it.
22
+ - The link popover is placed in the document and scrolls with the text, so it
23
+ stays attached to the link instead of floating over the page.
24
+
25
+ ### Fixed
26
+
27
+ - On phones, the link popover no longer appears far above the link when the
28
+ on-screen keyboard is shown.
29
+ - The link popover no longer overflows an editor narrower than the screen.
30
+
8
31
  ## [0.0.26] - 2026-11-02
9
32
 
10
33
  ### Added
package/README.ja.md CHANGED
@@ -93,7 +93,7 @@ const uploadImage = async (file: File) => {
93
93
 
94
94
  リンク先は新しいタブで開く。
95
95
 
96
- - **編集中**は、カーソルがリンクに触れているあいだ、リンクの上にリンク先を見せる吹き出しが出て、吹き出しを押すと開く。リンクの素のクリック(タップ)はカーソルを置く操作のままなので、タッチ端末でも「リンクをタップして、吹き出しをタップ」で開ける。⌘+クリック(Mac 以外は Ctrl+クリック)なら吹き出しを経ずに開く
96
+ - **編集中**は、リンクを押す(タップする)とカーソルが置かれ、リンクの上にリンク先を見せる吹き出しが出る。吹き出しを押すと開く。文字を打つ・キーでカーソルを動かすと消え、キーでリンクに入ったときは出ない。タッチ端末でも「リンクをタップして、吹き出しをタップ」で開ける。⌘+クリック(Mac 以外は Ctrl+クリック)なら吹き出しを経ずに開く
97
97
  - **読み取り専用**では、素のクリックで開く
98
98
 
99
99
  開く前に `onLinkClick` が書かれたとおりの `href` を受け取る(相対のままでも解決しない)。`event.preventDefault()` を呼べば、新しいタブでは開かない。`./other.md` のように、ブラウザで開いても意味を持たない行き先を利用側で扱うときに使う。
package/README.md CHANGED
@@ -93,7 +93,7 @@ While it waits, a marker is shown at the paste position and **the document is no
93
93
 
94
94
  Links open in a new tab.
95
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
96
+ - **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
97
97
  - **While `readonly`**, a plain click opens the link
98
98
 
99
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.
@@ -383,8 +383,13 @@
383
383
  --markdown-editor-popup-shadow-weak: var(--markdown-editor-shadow-weak);
384
384
 
385
385
  /* カーソルがリンクにあるあいだ出す吹き出し */
386
- --markdown-editor-url-bubble-color: var(--markdown-editor-hyperlink);
387
- --markdown-editor-url-bubble-hover: var(--markdown-editor-hover-overlay);
386
+ --markdown-editor-link-popover-background: var(--markdown-editor-surface-elevated);
387
+ --markdown-editor-link-popover-border: var(--markdown-editor-border-weak);
388
+ --markdown-editor-link-popover-shadow-strong: var(--markdown-editor-shadow-strong);
389
+ --markdown-editor-link-popover-shadow: var(--markdown-editor-shadow);
390
+ --markdown-editor-link-popover-shadow-weak: var(--markdown-editor-shadow-weak);
391
+ --markdown-editor-link-popover-color: var(--markdown-editor-hyperlink);
392
+ --markdown-editor-link-popover-hover: var(--markdown-editor-hover-overlay);
388
393
 
389
394
  /* コンテキストメニュー */
390
395
  --markdown-editor-menu-separator: var(--markdown-editor-border);
@@ -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 MenuPopup from './internal/MenuPopup.svelte';
15
+ import Popup from './internal/Popup.svelte';
16
16
 
17
17
  interface Props {
18
18
  controller: ContextMenuController;
@@ -84,8 +84,8 @@
84
84
  /**
85
85
  * 選んでいる項目へ実フォーカスを移す。
86
86
  *
87
- * 何も選んでいないあいだはコンテナにフォーカスが載ったままで、`MenuPopup` が開くときに
88
- * そこへ移している。移し先がコンテナの中なら閉じない(`MenuPopup` の `onFocusOut` が
87
+ * 何も選んでいないあいだはコンテナにフォーカスが載ったままで、`Popup` が開くときに
88
+ * そこへ移している。移し先がコンテナの中なら閉じない(`Popup` の `onFocusOut` が
89
89
  * 移り先を見ている)。
90
90
  */
91
91
  function focusActive(): void {
@@ -115,7 +115,7 @@
115
115
  });
116
116
  </script>
117
117
 
118
- <MenuPopup
118
+ <Popup
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
- </MenuPopup>
170
+ </Popup>
171
171
 
172
172
  <style>
173
173
  /**
@@ -0,0 +1,196 @@
1
+ <!--
2
+ リンクを押して(タップして)カーソルを入れたときに出す吹き出し。リンク先を見せ、押すとそこを開く。
3
+
4
+ **ポップアップ(`Popup`)には載せない。本文の中に絶対配置で置く。** 吹き出しはリンクに
5
+ 付いているものであり、本文と一緒に動くのが正しい。画面に固定して重ねると、スクロール
6
+ のたびに閉じるか置き直すかを迫られ、スマートフォンではキーボードが出るだけで本文が
7
+ 動いて閉じてしまう。本文の中に置けば、リンクとの相対位置はスクロールでもキーボードでも
8
+ 変わらない。
9
+
10
+ 位置は「リンクの矩形 − この層の矩形」で決める。**同じ瞬間に同じ測り方で測った差**なので、
11
+ 表示領域の基準(スクロール位置・キーボードで縮んだ範囲)がどうであっても打ち消し合う。
12
+
13
+ - **焦点を奪わない。** 焦点が編集エリアを離れると吹き出しは消えるので、奪うと
14
+ 押す前に消える。タッチ端末ではキーボードも引っ込む。押しても焦点が動かないよう、
15
+ `mousedown` を止める
16
+ - **リンクの上に出す。** タッチ端末では画面の下半分をキーボードが覆う。エディタの
17
+ 上端か表示領域の上端を越えるときだけ下へ返す
18
+ -->
19
+ <script lang="ts">
20
+ import type { LinkPopoverController } from '../states/linkPopoverController.svelte';
21
+
22
+ interface Props {
23
+ controller: LinkPopoverController;
24
+ /** 吹き出しのアクセシブルな名前。リンク先を後ろに続けて読ませる */
25
+ openLabel: string;
26
+ }
27
+
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
+ }
73
+
74
+ function keepFocus(event: MouseEvent): void {
75
+ event.preventDefault();
76
+ }
77
+ </script>
78
+
79
+ <!-- 位置の原点にする層。高さを持たず、並びを押し広げない -->
80
+ <div class="markdown-editor-link-popover-layer" bind:this={layerRef}>
81
+ {#if controller.link}
82
+ <!-- 押しても焦点を移さないための受け止め。キーで届く操作はボタンが持つ -->
83
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
84
+ <div class="markdown-editor-link-popover" bind:this={popoverRef} onmousedown={keepFocus}>
85
+ <!-- 吹き出し全体を 1 つのボタンにする。押せる面が広いほどタッチ端末で押しやすい -->
86
+ <button
87
+ type="button"
88
+ class="markdown-editor-link-popover__open"
89
+ aria-label="{openLabel}: {controller.link.href}"
90
+ title={controller.link.href}
91
+ onclick={controller.open}
92
+ >
93
+ <!-- 新しいタブで開くことの印。吹き出しはリンクの先頭にそろうので、左に置けば押す目当てが近い -->
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>
98
+ </button>
99
+ </div>
100
+ {/if}
101
+ </div>
102
+
103
+ <style>
104
+ /**
105
+ * 本文の上へ重ねるため、自分で重なりの文脈を作る(本文の中の位置付きの要素より手前)。
106
+ */
107
+ .markdown-editor-link-popover-layer {
108
+ position: relative;
109
+ z-index: 1;
110
+ height: 0;
111
+ }
112
+
113
+ /**
114
+ * 幅の上限は**エディタの幅**で抑える(層はエディタと同じ幅)。表示領域の幅で抑えると、
115
+ * エディタが画面より狭いとき(スマートフォンで左右に余白がある置き方)にはみ出す。
116
+ */
117
+ .markdown-editor-link-popover {
118
+ position: absolute;
119
+ box-sizing: border-box;
120
+ max-width: min(360px, 100%);
121
+ border: 1px solid var(--markdown-editor-link-popover-border);
122
+ border-radius: var(--markdown-editor-radius);
123
+ background: var(--markdown-editor-link-popover-background);
124
+ box-shadow:
125
+ 0 11px 15px -7px var(--markdown-editor-link-popover-shadow-strong),
126
+ 0 24px 38px 3px var(--markdown-editor-link-popover-shadow),
127
+ 0 9px 46px 8px var(--markdown-editor-link-popover-shadow-weak);
128
+ overflow: hidden;
129
+ animation: markdown-editor-link-popover-in 120ms forwards;
130
+ }
131
+
132
+ @keyframes markdown-editor-link-popover-in {
133
+ from {
134
+ opacity: 0;
135
+ }
136
+ to {
137
+ opacity: 1;
138
+ }
139
+ }
140
+
141
+ @media (prefers-reduced-motion: reduce) {
142
+ .markdown-editor-link-popover {
143
+ animation-duration: 0.01s;
144
+ }
145
+ }
146
+
147
+ /** 行間は本文のものを引き継がない(地の文ではない) */
148
+ .markdown-editor-link-popover__open {
149
+ box-sizing: border-box;
150
+ display: flex;
151
+ align-items: center;
152
+ gap: 6px;
153
+ max-width: 100%;
154
+ padding: 8px 12px;
155
+ border: 0;
156
+ background: none;
157
+ color: var(--markdown-editor-link-popover-color);
158
+ font: inherit;
159
+ font-size: var(--markdown-editor-font-size-sm);
160
+ line-height: normal;
161
+ text-align: left;
162
+ white-space: nowrap;
163
+ cursor: pointer;
164
+ }
165
+
166
+ .markdown-editor-link-popover__target {
167
+ min-width: 0;
168
+ overflow: hidden;
169
+ text-overflow: ellipsis;
170
+ }
171
+
172
+ .markdown-editor-link-popover__icon {
173
+ 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;
181
+ }
182
+
183
+ /* 触れる入力しかない環境では `:hover` が居座るため、囲って外す */
184
+ @media (hover: hover) {
185
+ .markdown-editor-link-popover__open:hover {
186
+ background: var(--markdown-editor-link-popover-hover);
187
+ }
188
+ }
189
+
190
+ /* 触れる端末でのターゲットの大きさ */
191
+ @media (max-width: 768px) {
192
+ .markdown-editor-link-popover__open {
193
+ min-height: var(--markdown-editor-touch-target);
194
+ }
195
+ }
196
+ </style>
@@ -0,0 +1,9 @@
1
+ import type { LinkPopoverController } from '../states/linkPopoverController.svelte';
2
+ interface Props {
3
+ controller: LinkPopoverController;
4
+ /** 吹き出しのアクセシブルな名前。リンク先を後ろに続けて読ませる */
5
+ openLabel: string;
6
+ }
7
+ declare const LinkPopover: import("svelte").Component<Props, {}, "">;
8
+ type LinkPopover = ReturnType<typeof LinkPopover>;
9
+ export default LinkPopover;
@@ -93,7 +93,7 @@
93
93
  /**
94
94
  * リンクが押されたときの通知。`href` は書かれたとおりの値で、相対のままでも解決しない。
95
95
  *
96
- * 編集中はカーソルがリンクにあるあいだ出る吹き出しを押したときと、⌘+クリック
96
+ * 編集中はリンクを押すと出る吹き出しを押したときと、⌘+クリック
97
97
  * (Mac 以外は Ctrl+クリック)で、読み取り専用では素のクリックで呼ばれる。
98
98
  * `event.preventDefault()` を呼べば、新しいタブで開く既定の動きを止められる。
99
99
  * 呼ばなければ(または渡さなければ)新しいタブで開く。
@@ -192,13 +192,13 @@
192
192
  import { resolveMessages } from '../messages/index.js';
193
193
  import { EMPTY_VIEW_STATE, isSameViewState } from '../models/viewState/viewStateCodec.js';
194
194
  import { createContextMenuController } from '../states/contextMenuController.svelte';
195
- import { createLinkBubbleController } from '../states/linkBubbleController.svelte';
195
+ import { createLinkPopoverController } from '../states/linkPopoverController.svelte';
196
196
  import { createOutlineViewController } from '../states/outlineViewController.svelte';
197
197
  import type { EditorSession } from '../states/editorSession.types';
198
198
  import ContextMenu from './ContextMenu.svelte';
199
199
  import EditorSurface from './EditorSurface.svelte';
200
200
  import FocusBreadcrumb from './FocusBreadcrumb.svelte';
201
- import LinkBubble from './LinkBubble.svelte';
201
+ import LinkPopover from './LinkPopover.svelte';
202
202
  import ShortcutHelpDialog from './ShortcutHelpDialog.svelte';
203
203
 
204
204
  // --- props ---
@@ -277,7 +277,7 @@
277
277
  isEnabled: () => contextMenu
278
278
  });
279
279
 
280
- const linkBubble = createLinkBubbleController({
280
+ const linkPopover = createLinkPopoverController({
281
281
  getSession: () => session,
282
282
  getLinkClick: () => onLinkClick
283
283
  });
@@ -303,7 +303,7 @@
303
303
  */
304
304
  imageUpload: () => uploadImage,
305
305
  linkClick: () => onLinkClick,
306
- extensions: [controller.extension, menuController.extension, linkBubble.extension]
306
+ extensions: [controller.extension, menuController.extension, linkPopover.extension]
307
307
  };
308
308
 
309
309
  // --- 関数 ---
@@ -485,5 +485,5 @@
485
485
  {#if contextMenu}
486
486
  <ContextMenu controller={menuController} {text} />
487
487
  {/if}
488
- <LinkBubble controller={linkBubble} openLabel={text.linkOpen} />
488
+ <LinkPopover controller={linkPopover} openLabel={text.linkOpen} />
489
489
  </div>
@@ -87,7 +87,7 @@ export interface MarkdownEditorProps {
87
87
  /**
88
88
  * リンクが押されたときの通知。`href` は書かれたとおりの値で、相対のままでも解決しない。
89
89
  *
90
- * 編集中はカーソルがリンクにあるあいだ出る吹き出しを押したときと、⌘+クリック
90
+ * 編集中はリンクを押すと出る吹き出しを押したときと、⌘+クリック
91
91
  * (Mac 以外は Ctrl+クリック)で、読み取り専用では素のクリックで呼ばれる。
92
92
  * `event.preventDefault()` を呼べば、新しいタブで開く既定の動きを止められる。
93
93
  * 呼ばなければ(または渡さなければ)新しいタブで開く。
@@ -1,45 +1,53 @@
1
1
  <!--
2
- 最前面へ出すコンテナの土台。`popover="manual"` で重ね、錨の矩形(AnchorRect)に対して置く。
2
+ 最前面へ出すコンテナ。`popover="manual"` で重ね、錨の矩形(AnchorRect)に対して置く。
3
3
  錨を要素ではなく矩形で受けるのは、ポインタの座標と要素の箱を同じ型に落とすため。
4
4
 
5
- 持つのは、最前面への出し入れ・見た目・外の動きの通知だけである。**焦点・キー・
6
- 置き場所は使う側が持つ。** メニューは開くと焦点を移して錨の右上に出るが、リンクの
7
- 吹き出しは焦点を奪わずにリンクの上へ出る。それぞれの振る舞いは使う側の部品に書く
8
- (→ `MenuPopup`・`LinkBubble`)。
9
-
10
- 開閉は `open` prop で受ける。外の動き(スクロール・リサイズ)は `onViewportChange` で
11
- 知らせるだけで、閉じるか付いていくかは使う側が決める。
5
+ 開閉は `open` prop で受ける(状態はエディタ 1 つにつき 1 つのコントローラが持つ)。
6
+ スクロール・リサイズで閉じたいことは `onClose` で外へ伝えるだけで、自分では閉じない。
12
7
  -->
13
8
  <script lang="ts">
14
9
  import type { Snippet } from 'svelte';
15
- import type { HTMLAttributes } from 'svelte/elements';
16
10
 
17
11
  import type { AnchorRect } from '../../utils/geometry.types';
18
12
 
19
- interface Props extends HTMLAttributes<HTMLDivElement> {
13
+ interface Props {
20
14
  children: Snippet;
21
- /** 錨の矩形。変わるたびに置き直す */
15
+ /** 錨の矩形。開くたびに読み直す */
22
16
  anchor: AnchorRect | null;
23
17
  open: boolean;
24
- /** 錨に対して置く。寸法を測れるよう、最前面へ出した後に呼ぶ */
25
- place: (element: HTMLDivElement, anchor: AnchorRect) => void;
26
- /** 出した直後の通知。焦点を移すかどうかは使う側が決める */
27
- onOpen?: (element: HTMLDivElement) => void;
28
- /** 外の動き(スクロール・リサイズ)の通知。中身のスクロールでは呼ばない */
29
- onViewportChange?: () => void;
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;
30
33
  }
31
34
 
32
35
  let {
33
36
  children,
34
37
  anchor,
35
38
  open,
36
- place,
37
- onOpen,
38
- onViewportChange,
39
+ role = 'menu',
40
+ ariaLabel,
41
+ ariaActivedescendant,
39
42
  class: className = '',
40
- ...attributes
43
+ onKeyDown,
44
+ onFocusOut,
45
+ onClose
41
46
  }: Props = $props();
42
47
 
48
+ /** 錨と表示領域の端から空ける距離 */
49
+ const MARGIN = 8;
50
+
43
51
  let popupRef: HTMLDivElement | undefined = $state();
44
52
 
45
53
  $effect(() => {
@@ -47,37 +55,113 @@
47
55
  if (!element) return;
48
56
 
49
57
  if (!open || !anchor) {
50
- if (element.matches(':popover-open')) element.hidePopover();
58
+ hide(element);
51
59
  return;
52
60
  }
53
61
 
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);
62
+ show(element, anchor);
63
+ // 開いているあいだだけ、外の動きを見る
64
+ window.addEventListener('resize', requestClose);
65
65
  document.addEventListener('scroll', handleScroll, true);
66
66
  return () => {
67
- window.removeEventListener('resize', notifyViewportChange);
67
+ window.removeEventListener('resize', requestClose);
68
68
  document.removeEventListener('scroll', handleScroll, true);
69
69
  };
70
70
  });
71
71
 
72
- function notifyViewportChange(): void {
73
- onViewportChange?.();
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));
74
120
  }
75
121
 
76
- /** 中身のスクロールは外の動きにしない */
122
+ function requestClose(): void {
123
+ onClose?.();
124
+ }
125
+
126
+ /** 中身のスクロールは閉じるきっかけにしない */
77
127
  function handleScroll(event: Event): void {
78
128
  const target = event.target;
79
129
  if (target instanceof Node && popupRef?.contains(target)) return;
80
- notifyViewportChange();
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
+ }
81
165
  }
82
166
  </script>
83
167
 
@@ -85,7 +169,12 @@
85
169
  bind:this={popupRef}
86
170
  popover="manual"
87
171
  class="markdown-editor-popup {className}"
88
- {...attributes}
172
+ {role}
173
+ tabindex="-1"
174
+ aria-label={ariaLabel}
175
+ aria-activedescendant={ariaActivedescendant}
176
+ onkeydown={handleKeydown}
177
+ onfocusout={handleFocusOut}
89
178
  >
90
179
  {@render children()}
91
180
  </div>
@@ -112,7 +201,8 @@
112
201
  overflow: hidden;
113
202
  /*
114
203
  * 巻き取りを中で止める。端まで来た続きが背後のページへ流れると、
115
- * ページが動いたぶんを**外側のスクロール**として受け取ってしまう(→ handleScroll)。
204
+ * ページが動いたぶんを**外側のスクロール**として受け取り(→ handleScroll)、
205
+ * 中を読んでいるだけなのに閉じてしまう。
116
206
  */
117
207
  overscroll-behavior: contain;
118
208
  }
@@ -1,17 +1,25 @@
1
1
  import type { Snippet } from 'svelte';
2
- import type { HTMLAttributes } from 'svelte/elements';
3
2
  import type { AnchorRect } from '../../utils/geometry.types';
4
- interface Props extends HTMLAttributes<HTMLDivElement> {
3
+ interface Props {
5
4
  children: Snippet;
6
- /** 錨の矩形。変わるたびに置き直す */
5
+ /** 錨の矩形。開くたびに読み直す */
7
6
  anchor: AnchorRect | null;
8
7
  open: boolean;
9
- /** 錨に対して置く。寸法を測れるよう、最前面へ出した後に呼ぶ */
10
- place: (element: HTMLDivElement, anchor: AnchorRect) => void;
11
- /** 出した直後の通知。焦点を移すかどうかは使う側が決める */
12
- onOpen?: (element: HTMLDivElement) => void;
13
- /** 外の動き(スクロール・リサイズ)の通知。中身のスクロールでは呼ばない */
14
- onViewportChange?: () => void;
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;
15
23
  }
16
24
  declare const Popup: import("svelte").Component<Props, {}, "">;
17
25
  type Popup = ReturnType<typeof Popup>;
@@ -11,8 +11,8 @@ export type LinkClickSource = () => LinkClick | undefined;
11
11
  *
12
12
  * 編集中は**修飾キー(Mac は ⌘、それ以外は Ctrl)を添えたときだけ**開く。
13
13
  * 素のクリックはカーソルを置く操作であり、奪うとリンクの文字を直せなくなる。
14
- * 修飾キーの無いタッチ端末では、カーソルがリンクにあるあいだ出る吹き出しから開く
15
- * (→ `linkBubble`)。読み取り専用では素のクリックで開く。
14
+ * 修飾キーの無いタッチ端末では、リンクを押すと出る吹き出しから開く
15
+ * (→ `linkPopover`)。読み取り専用では素のクリックで開く。
16
16
  *
17
17
  * 開く前に利用側へ知らせる。相対のリンク(`./other.md`)のように、ブラウザで開いても
18
18
  * 意味を持たない行き先は、利用側が引き取って扱う。
@@ -5,8 +5,8 @@ import { isMacPlatform } from '../keymap/shortcutKeys.js';
5
5
  *
6
6
  * 編集中は**修飾キー(Mac は ⌘、それ以外は Ctrl)を添えたときだけ**開く。
7
7
  * 素のクリックはカーソルを置く操作であり、奪うとリンクの文字を直せなくなる。
8
- * 修飾キーの無いタッチ端末では、カーソルがリンクにあるあいだ出る吹き出しから開く
9
- * (→ `linkBubble`)。読み取り専用では素のクリックで開く。
8
+ * 修飾キーの無いタッチ端末では、リンクを押すと出る吹き出しから開く
9
+ * (→ `linkPopover`)。読み取り専用では素のクリックで開く。
10
10
  *
11
11
  * 開く前に利用側へ知らせる。相対のリンク(`./other.md`)のように、ブラウザで開いても
12
12
  * 意味を持たない行き先は、利用側が引き取って扱う。
@@ -0,0 +1,24 @@
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
+ * 吹き出しを出すリンクを見張り、変わるたびに知らせる。表示は持たない(→ `LinkPopover`)。
10
+ *
11
+ * **出すのは、リンクを押して(タップして)カーソルを入れたときだけである。** 開きたいかも
12
+ * しれないという合図はそれしかない。矢印キーでリンクを通り過ぎるとき・リンクの文字を
13
+ * 直しているときに出ると邪魔になる。押した後に文字を打つ・キーでカーソルを動かすと消える。
14
+ *
15
+ * 押したかどうかは、編集基盤が押して動いた選択に付ける印(`pointer`)と、押したこと
16
+ * そのもの(`mousedown`)で見る。後者が要るのは、カーソルのある位置を押し直しても
17
+ * 選択は動かず、印の付いた transaction が来ないためである。`click` では受けない——
18
+ * リンクのクリックは `linkClick` が既定の動きを止めるので、後ろのプラグインへ届かない。
19
+ * タップでも `mousedown` は届く。
20
+ *
21
+ * 編集中に焦点があるときだけ出す。読み取り専用では素のクリックで開くので出さない。
22
+ * 変換の最中も出さない。
23
+ */
24
+ export declare function linkPopoverPlugin(onChange: (view: EditorView, link: LinkAtCursor | null) => void): Plugin;
@@ -0,0 +1,92 @@
1
+ import { Plugin, PluginKey } 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
+ * 吹き出しを出すリンクを見張り、変わるたびに知らせる。表示は持たない(→ `LinkPopover`)。
7
+ *
8
+ * **出すのは、リンクを押して(タップして)カーソルを入れたときだけである。** 開きたいかも
9
+ * しれないという合図はそれしかない。矢印キーでリンクを通り過ぎるとき・リンクの文字を
10
+ * 直しているときに出ると邪魔になる。押した後に文字を打つ・キーでカーソルを動かすと消える。
11
+ *
12
+ * 押したかどうかは、編集基盤が押して動いた選択に付ける印(`pointer`)と、押したこと
13
+ * そのもの(`mousedown`)で見る。後者が要るのは、カーソルのある位置を押し直しても
14
+ * 選択は動かず、印の付いた transaction が来ないためである。`click` では受けない——
15
+ * リンクのクリックは `linkClick` が既定の動きを止めるので、後ろのプラグインへ届かない。
16
+ * タップでも `mousedown` は届く。
17
+ *
18
+ * 編集中に焦点があるときだけ出す。読み取り専用では素のクリックで開くので出さない。
19
+ * 変換の最中も出さない。
20
+ */
21
+ export function linkPopoverPlugin(onChange) {
22
+ const notify = (view) => onChange(view, view.editable && !view.composing && isEditorFocused(view.state) && pointed(view.state)
23
+ ? linkAtCursor(view.state)
24
+ : null);
25
+ return new Plugin({
26
+ key: linkPopoverKey,
27
+ state: {
28
+ init: () => false,
29
+ // 押したとき・押して動いた選択で立て、文書の変更・押さずに動いた選択で降ろす。
30
+ // 焦点の出入りのような、どちらでもない transaction では保つ
31
+ apply: (tr, current) => {
32
+ if (tr.getMeta(linkPopoverKey) || tr.getMeta('pointer'))
33
+ return true;
34
+ if (tr.docChanged || tr.selectionSet)
35
+ return false;
36
+ return current;
37
+ }
38
+ },
39
+ props: {
40
+ handleDOMEvents: {
41
+ mousedown: (view) => {
42
+ if (!pointed(view.state))
43
+ view.dispatch(view.state.tr.setMeta(linkPopoverKey, true));
44
+ return false;
45
+ }
46
+ }
47
+ },
48
+ view: (view) => {
49
+ notify(view);
50
+ return {
51
+ update: notify,
52
+ destroy: () => onChange(view, null)
53
+ };
54
+ }
55
+ });
56
+ }
57
+ const linkPopoverKey = new PluginKey('linkPopover');
58
+ /** カーソルが、押して(タップして)置かれたままか */
59
+ function pointed(state) {
60
+ return linkPopoverKey.getState(state);
61
+ }
62
+ /**
63
+ * カーソルが触れているリンクを返す。範囲を選んでいるときは返さない(選ぶのは直すためである)。
64
+ *
65
+ * 両端も含める。リンクの末尾をタップすると、カーソルはリンクの直後に立つ。
66
+ */
67
+ function linkAtCursor(state) {
68
+ const { selection } = state;
69
+ if (!selection.empty)
70
+ return null;
71
+ const $pos = selection.$from;
72
+ const mark = linkOf($pos.nodeAfter?.marks) ?? linkOf($pos.nodeBefore?.marks);
73
+ if (!mark)
74
+ return null;
75
+ // 同じリンクが続く区間の先頭を探す(書式が混ざると文字の節は分かれる)
76
+ const parent = $pos.parent;
77
+ let runStart = $pos.start();
78
+ let at = runStart;
79
+ for (let index = 0; index < parent.childCount; index += 1) {
80
+ const child = parent.child(index);
81
+ const end = at + child.nodeSize;
82
+ if (!mark.isInSet(child.marks))
83
+ runStart = end;
84
+ else if (at <= $pos.pos && $pos.pos <= end)
85
+ return { href: mark.attrs.href, from: runStart };
86
+ at = end;
87
+ }
88
+ return null;
89
+ }
90
+ function linkOf(marks) {
91
+ return marks?.find((mark) => mark.type === LINK);
92
+ }
@@ -1,27 +1,29 @@
1
+ import { type LinkAtCursor } from '../operations/plugins/linkPopover.js';
1
2
  import { type LinkClickSource } from '../operations/plugins/linkClick.js';
2
3
  import type { AnchorRect } from '../utils/geometry.types';
3
4
  import type { EditorExtension, EditorSession } from './editorSession.types';
4
- export interface LinkBubbleControllerOptions {
5
+ export interface LinkPopoverControllerOptions {
5
6
  /** 現在のセッション。生き死にの管理はコンポーネントが持つ */
6
7
  readonly getSession: () => EditorSession | null;
7
8
  /** リンクが押されたときの受け手。呼ぶ時点の値を読む */
8
9
  readonly getLinkClick: LinkClickSource;
9
10
  }
10
- export interface LinkBubbleController {
11
+ export interface LinkPopoverController {
11
12
  /** 基盤へ渡す拡張。セッション生成より前に取得する */
12
13
  readonly extension: EditorExtension;
13
- /** 吹き出しを出しているリンクの行き先。出していなければ `null` */
14
- readonly href: string | null;
15
- /** リンクの先頭の矩形 */
16
- readonly anchor: AnchorRect | null;
14
+ /**
15
+ * 吹き出しを出しているリンク。出していなければ `null`。
16
+ * 編集基盤が更新するたびに新しい値になる(同じリンクでも、位置は動きうる)
17
+ */
18
+ readonly link: LinkAtCursor | null;
19
+ /** リンクの先頭の矩形を、いま測る。吹き出しの側と同じ瞬間に測るため、値で持たない */
20
+ measure(): AnchorRect | null;
17
21
  /** 吹き出しが押された。利用側へ知らせ、止められなければ新しいタブで開く */
18
22
  open(event: MouseEvent): void;
19
- /** 錨を読み直す。外の動き(スクロール・リサイズ)に付いていく */
20
- refresh(): void;
21
23
  }
22
24
  /**
23
- * カーソルがリンクにあるあいだ出す吹き出しの状態を、エディタ 1 インスタンスに閉じて持つ。
25
+ * リンクを押したときに出す吹き出しの状態を、エディタ 1 インスタンスに閉じて持つ。
24
26
  *
25
27
  * **エディタ 1 つにつき 1 つ生成する。** セッションは都度たどるだけで抱え込まない。
26
28
  */
27
- export declare function createLinkBubbleController(options: LinkBubbleControllerOptions): LinkBubbleController;
29
+ export declare function createLinkPopoverController(options: LinkPopoverControllerOptions): LinkPopoverController;
@@ -0,0 +1,35 @@
1
+ import { linkPopoverPlugin } from '../operations/plugins/linkPopover.js';
2
+ import { openLink } from '../operations/plugins/linkClick.js';
3
+ /**
4
+ * リンクを押したときに出す吹き出しの状態を、エディタ 1 インスタンスに閉じて持つ。
5
+ *
6
+ * **エディタ 1 つにつき 1 つ生成する。** セッションは都度たどるだけで抱え込まない。
7
+ */
8
+ export function createLinkPopoverController(options) {
9
+ let link = $state(null);
10
+ const extension = {
11
+ plugins: [
12
+ linkPopoverPlugin((_view, next) => {
13
+ link = next;
14
+ })
15
+ ],
16
+ actions: []
17
+ };
18
+ return {
19
+ extension,
20
+ get link() {
21
+ return link;
22
+ },
23
+ measure() {
24
+ const view = options.getSession()?.getView();
25
+ if (!view || !link)
26
+ return null;
27
+ const coords = view.coordsAtPos(link.from);
28
+ return { left: coords.left, right: coords.left, top: coords.top, bottom: coords.bottom };
29
+ },
30
+ open(event) {
31
+ if (link)
32
+ openLink(link.href, event, options.getLinkClick);
33
+ }
34
+ };
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@14ch/svelte-md-outliner",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
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.",
@@ -1,120 +0,0 @@
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>
@@ -1,9 +0,0 @@
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;
@@ -1,145 +0,0 @@
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>
@@ -1,26 +0,0 @@
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,14 +0,0 @@
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;
@@ -1,55 +0,0 @@
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
- }
@@ -1,42 +0,0 @@
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
- }