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

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 (66) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.ja.md +6 -4
  3. package/README.md +6 -4
  4. package/dist/assets/styles/editor.css +7 -8
  5. package/dist/assets/styles/theme.css +20 -15
  6. package/dist/components/ContextMenu.svelte +5 -5
  7. package/dist/components/LinkDialog.svelte +136 -0
  8. package/dist/components/LinkDialog.svelte.d.ts +10 -0
  9. package/dist/components/LinkPopover.svelte +164 -0
  10. package/dist/components/LinkPopover.svelte.d.ts +9 -0
  11. package/dist/components/MarkdownEditor.svelte +53 -13
  12. package/dist/components/MarkdownEditor.svelte.d.ts +6 -1
  13. package/dist/components/SelectionToolbar.svelte +136 -0
  14. package/dist/components/SelectionToolbar.svelte.d.ts +9 -0
  15. package/dist/components/ShortcutHelpDialog.svelte +26 -18
  16. package/dist/components/internal/AnchoredLayer.svelte +102 -0
  17. package/dist/components/internal/AnchoredLayer.svelte.d.ts +16 -0
  18. package/dist/components/internal/Icon.svelte +26 -0
  19. package/dist/components/internal/Icon.svelte.d.ts +7 -0
  20. package/dist/components/internal/Popup.svelte +130 -40
  21. package/dist/components/internal/Popup.svelte.d.ts +17 -9
  22. package/dist/components/internal/icons.d.ts +22 -0
  23. package/dist/components/internal/icons.js +22 -0
  24. package/dist/messages/en.js +10 -1
  25. package/dist/messages/index.js +9 -0
  26. package/dist/messages/ja.js +10 -1
  27. package/dist/messages/messages.types.d.ts +27 -0
  28. package/dist/operations/autoformat.js +17 -0
  29. package/dist/operations/commands/link.d.ts +27 -4
  30. package/dist/operations/commands/link.js +111 -14
  31. package/dist/operations/keymap/coreActions.js +0 -10
  32. package/dist/operations/keymap/linkActions.d.ts +13 -0
  33. package/dist/operations/keymap/linkActions.js +30 -0
  34. package/dist/operations/keymap/toolbarItems.d.ts +17 -0
  35. package/dist/operations/keymap/toolbarItems.js +62 -0
  36. package/dist/operations/operations.types.d.ts +51 -0
  37. package/dist/operations/plugins/compositionState.js +21 -1
  38. package/dist/operations/plugins/linkClick.d.ts +2 -2
  39. package/dist/operations/plugins/linkClick.js +2 -2
  40. package/dist/operations/plugins/linkDialogGuard.d.ts +9 -0
  41. package/dist/operations/plugins/linkDialogGuard.js +18 -0
  42. package/dist/operations/plugins/linkPopover.d.ts +22 -0
  43. package/dist/operations/plugins/linkPopover.js +72 -0
  44. package/dist/operations/plugins/selectionToolbar.d.ts +15 -0
  45. package/dist/operations/plugins/selectionToolbar.js +96 -0
  46. package/dist/states/linkDialogController.svelte.d.ts +43 -0
  47. package/dist/states/linkDialogController.svelte.js +104 -0
  48. package/dist/states/linkPopoverController.svelte.d.ts +36 -0
  49. package/dist/states/linkPopoverController.svelte.js +48 -0
  50. package/dist/states/selectionToolbarController.svelte.d.ts +29 -0
  51. package/dist/states/selectionToolbarController.svelte.js +51 -0
  52. package/dist/utils/linkHref.d.ts +7 -0
  53. package/dist/utils/linkHref.js +16 -0
  54. package/package.json +5 -2
  55. package/dist/components/LinkBubble.svelte +0 -120
  56. package/dist/components/LinkBubble.svelte.d.ts +0 -9
  57. package/dist/components/internal/Dialog.svelte +0 -157
  58. package/dist/components/internal/Dialog.svelte.d.ts +0 -23
  59. package/dist/components/internal/MenuPopup.svelte +0 -145
  60. package/dist/components/internal/MenuPopup.svelte.d.ts +0 -26
  61. package/dist/components/internal/Modal.svelte +0 -181
  62. package/dist/components/internal/Modal.svelte.d.ts +0 -18
  63. package/dist/operations/plugins/linkBubble.d.ts +0 -14
  64. package/dist/operations/plugins/linkBubble.js +0 -55
  65. package/dist/states/linkBubbleController.svelte.d.ts +0 -27
  66. package/dist/states/linkBubbleController.svelte.js +0 -42
@@ -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>;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 帯と吹き出しのアイコンの図形。
3
+ *
4
+ * 出どころ: Material Symbols(Outlined・太さ 300・24px)
5
+ * https://fonts.google.com/icons / https://github.com/google/material-design-icons
6
+ * Copyright Google LLC. Licensed under the Apache License, Version 2.0
7
+ * (https://www.apache.org/licenses/LICENSE-2.0)
8
+ *
9
+ * アイコンフォントは読み込まず、使う図形の `path` だけを写して持つ。外から読み込むと
10
+ * オフラインで出ず、フォントを同梱すると数個のために全体を抱えることになる。
11
+ * どれも `viewBox="0 -960 960 960"` の座標で描かれている。
12
+ */
13
+ export declare const ICON_PATHS: {
14
+ readonly link: "M432.31-298.46H281.54q-75.34 0-128.44-53.1Q100-404.65 100-479.98q0-75.33 53.1-128.44 53.1-53.12 128.44-53.12h150.77v60H281.54q-50.39 0-85.96 35.58Q160-530.38 160-480q0 50.38 35.58 85.96 35.57 35.58 85.96 35.58h150.77v60ZM330-450v-60h300v60H330Zm197.69 151.54v-60h150.77q50.39 0 85.96-35.58Q800-429.62 800-480q0-50.38-35.58-85.96-35.57-35.58-85.96-35.58H527.69v-60h150.77q75.34 0 128.44 53.1Q860-555.35 860-480.02q0 75.33-53.1 128.44-53.1 53.12-128.44 53.12H527.69Z";
15
+ readonly format_bold: "M293.54-215v-530H488q61.15 0 110.58 38.08Q648-668.85 648-604.38q0 44.84-21.66 73.11-21.65 28.27-46.65 41.04 30.77 10.61 58.77 41.96 28 31.35 28 84.42 0 76.69-56.54 112.77T496-215H293.54Zm86-79.69h113.23q47.23 0 66.77-26.23t19.54-50.31q0-24.08-19.54-50.31-19.54-26.23-68.62-26.23H379.54v153.08Zm0-230.31h103.77q36.46 0 57.81-20.85 21.34-20.84 21.34-49.92 0-30.92-22.57-50.54-22.58-19.61-55.35-19.61h-105V-525Z";
16
+ readonly format_italic: "M215.77-215v-72.31h152.69l129.62-385.38H345.39V-745h366.15v72.31H571.15L441.54-287.31h140.38V-215H215.77Z";
17
+ readonly format_strikethrough: "M100-410.39v-59.99h760v59.99H100Zm337.69-139.22V-695h-217.3v-85H740v85H522.69v145.39h-85Zm0 369.61v-151.15h85V-180h-85Z";
18
+ readonly code: "M320-253.85 93.85-480 320-706.15l42.77 42.77-184 184L362.15-296 320-253.85Zm320 0-42.77-42.77 184-184L597.85-664 640-706.15 866.15-480 640-253.85Z";
19
+ readonly edit: "M200-200h50.46l409.46-409.46-50.46-50.46L200-250.46V-200Zm-60 60v-135.38l527.62-527.39q9.07-8.24 20.03-12.73 10.97-4.5 23-4.5t23.3 4.27q11.28 4.27 19.97 13.58l48.85 49.46q9.31 8.69 13.27 20 3.96 11.31 3.96 22.62 0 12.07-4.12 23.03-4.12 10.97-13.11 20.04L275.38-140H140Zm620.38-570.15-50.23-50.23 50.23 50.23Zm-126.13 75.9-24.79-25.67 50.46 50.46-25.67-24.79Z";
20
+ readonly link_off: "M755.38-315.85 710-362.46q40.38-11 65.19-43.27Q800-438 800-480q0-50.38-35.38-85.96-35.39-35.58-85.39-35.58H526.92v-60h152.31q74.92 0 127.85 53.12Q860-555.31 860-480q0 53.15-28.54 97.11-28.54 43.97-76.08 67.04ZM623.23-450l-60-60h66v60h-6ZM805.85-69.85l-736-736L112-848l736 736-42.15 42.15ZM433.08-298.46H281.54q-75.31 0-128.42-53.12Q100-404.69 100-480q0-66.69 42.96-117.04 42.96-50.34 107.81-60.8H260l56.31 56.3h-34.77q-50.39 0-85.96 35.58Q160-530.38 160-480q0 50.38 35.58 85.96 35.57 35.58 85.96 35.58h151.54v60ZM330.77-450v-60h77.69l59 60H330.77Z";
21
+ readonly open_in_new: "M212.31-140Q182-140 161-161q-21-21-21-51.31v-535.38Q140-778 161-799q21-21 51.31-21h252.3v60h-252.3q-4.62 0-8.46 3.85-3.85 3.84-3.85 8.46v535.38q0 4.62 3.85 8.46 3.84 3.85 8.46 3.85h535.38q4.62 0 8.46-3.85 3.85-3.84 3.85-8.46v-252.3h60v252.3Q820-182 799-161q-21 21-51.31 21H212.31Zm176.46-206.62-42.15-42.15L717.85-760H560v-60h260v260h-60v-157.85L388.77-346.62Z";
22
+ };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 帯と吹き出しのアイコンの図形。
3
+ *
4
+ * 出どころ: Material Symbols(Outlined・太さ 300・24px)
5
+ * https://fonts.google.com/icons / https://github.com/google/material-design-icons
6
+ * Copyright Google LLC. Licensed under the Apache License, Version 2.0
7
+ * (https://www.apache.org/licenses/LICENSE-2.0)
8
+ *
9
+ * アイコンフォントは読み込まず、使う図形の `path` だけを写して持つ。外から読み込むと
10
+ * オフラインで出ず、フォントを同梱すると数個のために全体を抱えることになる。
11
+ * どれも `viewBox="0 -960 960 960"` の座標で描かれている。
12
+ */
13
+ export const ICON_PATHS = {
14
+ link: 'M432.31-298.46H281.54q-75.34 0-128.44-53.1Q100-404.65 100-479.98q0-75.33 53.1-128.44 53.1-53.12 128.44-53.12h150.77v60H281.54q-50.39 0-85.96 35.58Q160-530.38 160-480q0 50.38 35.58 85.96 35.57 35.58 85.96 35.58h150.77v60ZM330-450v-60h300v60H330Zm197.69 151.54v-60h150.77q50.39 0 85.96-35.58Q800-429.62 800-480q0-50.38-35.58-85.96-35.57-35.58-85.96-35.58H527.69v-60h150.77q75.34 0 128.44 53.1Q860-555.35 860-480.02q0 75.33-53.1 128.44-53.1 53.12-128.44 53.12H527.69Z',
15
+ format_bold: 'M293.54-215v-530H488q61.15 0 110.58 38.08Q648-668.85 648-604.38q0 44.84-21.66 73.11-21.65 28.27-46.65 41.04 30.77 10.61 58.77 41.96 28 31.35 28 84.42 0 76.69-56.54 112.77T496-215H293.54Zm86-79.69h113.23q47.23 0 66.77-26.23t19.54-50.31q0-24.08-19.54-50.31-19.54-26.23-68.62-26.23H379.54v153.08Zm0-230.31h103.77q36.46 0 57.81-20.85 21.34-20.84 21.34-49.92 0-30.92-22.57-50.54-22.58-19.61-55.35-19.61h-105V-525Z',
16
+ format_italic: 'M215.77-215v-72.31h152.69l129.62-385.38H345.39V-745h366.15v72.31H571.15L441.54-287.31h140.38V-215H215.77Z',
17
+ format_strikethrough: 'M100-410.39v-59.99h760v59.99H100Zm337.69-139.22V-695h-217.3v-85H740v85H522.69v145.39h-85Zm0 369.61v-151.15h85V-180h-85Z',
18
+ code: 'M320-253.85 93.85-480 320-706.15l42.77 42.77-184 184L362.15-296 320-253.85Zm320 0-42.77-42.77 184-184L597.85-664 640-706.15 866.15-480 640-253.85Z',
19
+ edit: 'M200-200h50.46l409.46-409.46-50.46-50.46L200-250.46V-200Zm-60 60v-135.38l527.62-527.39q9.07-8.24 20.03-12.73 10.97-4.5 23-4.5t23.3 4.27q11.28 4.27 19.97 13.58l48.85 49.46q9.31 8.69 13.27 20 3.96 11.31 3.96 22.62 0 12.07-4.12 23.03-4.12 10.97-13.11 20.04L275.38-140H140Zm620.38-570.15-50.23-50.23 50.23 50.23Zm-126.13 75.9-24.79-25.67 50.46 50.46-25.67-24.79Z',
20
+ link_off: 'M755.38-315.85 710-362.46q40.38-11 65.19-43.27Q800-438 800-480q0-50.38-35.38-85.96-35.39-35.58-85.39-35.58H526.92v-60h152.31q74.92 0 127.85 53.12Q860-555.31 860-480q0 53.15-28.54 97.11-28.54 43.97-76.08 67.04ZM623.23-450l-60-60h66v60h-6ZM805.85-69.85l-736-736L112-848l736 736-42.15 42.15ZM433.08-298.46H281.54q-75.31 0-128.42-53.12Q100-404.69 100-480q0-66.69 42.96-117.04 42.96-50.34 107.81-60.8H260l56.31 56.3h-34.77q-50.39 0-85.96 35.58Q160-530.38 160-480q0 50.38 35.58 85.96 35.57 35.58 85.96 35.58h151.54v60ZM330.77-450v-60h77.69l59 60H330.77Z',
21
+ open_in_new: 'M212.31-140Q182-140 161-161q-21-21-21-51.31v-535.38Q140-778 161-799q21-21 51.31-21h252.3v60h-252.3q-4.62 0-8.46 3.85-3.85 3.84-3.85 8.46v535.38q0 4.62 3.85 8.46 3.84 3.85 8.46 3.85h535.38q4.62 0 8.46-3.85 3.85-3.84 3.85-8.46v-252.3h60v252.3Q820-182 799-161q-21 21-51.31 21H212.31Zm176.46-206.62-42.15-42.15L717.85-760H560v-60h260v260h-60v-157.85L388.77-346.62Z'
22
+ };
@@ -12,6 +12,15 @@ export const en = {
12
12
  imageUploading: 'Uploading image…',
13
13
  imageLoadError: 'Failed to load the image',
14
14
  linkOpen: 'Open link',
15
+ selectionToolbarLabel: 'Formatting',
16
+ linkAddTitle: 'Add link',
17
+ linkEditTitle: 'Edit link',
18
+ linkTextLabel: 'Text',
19
+ linkHrefLabel: 'URL',
20
+ linkApply: 'Apply',
21
+ linkCancel: 'Cancel',
22
+ linkEdit: 'Edit',
23
+ linkRemove: 'Remove link',
15
24
  frontMatterLabel: 'Front Matter',
16
25
  shortcutCategories: {
17
26
  edit: 'Edit',
@@ -77,7 +86,7 @@ export const en = {
77
86
  'format.italic': 'Italic',
78
87
  'format.code': 'Inline code',
79
88
  'format.strikethrough': 'Strikethrough',
80
- 'format.link': 'Link',
89
+ 'format.link': 'Add or edit link',
81
90
  // The list is a dictionary of everything that can happen; ids with a trailing
82
91
  // `.` part are the direction the context menu shows for a known target
83
92
  'format.heading': 'Turn into a heading, or back into a list item',
@@ -29,6 +29,15 @@ export function resolveMessages(lang = DEFAULT_LANG, overrides) {
29
29
  imageUploading: overrides.imageUploading ?? base.imageUploading,
30
30
  imageLoadError: overrides.imageLoadError ?? base.imageLoadError,
31
31
  linkOpen: overrides.linkOpen ?? base.linkOpen,
32
+ selectionToolbarLabel: overrides.selectionToolbarLabel ?? base.selectionToolbarLabel,
33
+ linkAddTitle: overrides.linkAddTitle ?? base.linkAddTitle,
34
+ linkEditTitle: overrides.linkEditTitle ?? base.linkEditTitle,
35
+ linkTextLabel: overrides.linkTextLabel ?? base.linkTextLabel,
36
+ linkHrefLabel: overrides.linkHrefLabel ?? base.linkHrefLabel,
37
+ linkApply: overrides.linkApply ?? base.linkApply,
38
+ linkCancel: overrides.linkCancel ?? base.linkCancel,
39
+ linkEdit: overrides.linkEdit ?? base.linkEdit,
40
+ linkRemove: overrides.linkRemove ?? base.linkRemove,
32
41
  frontMatterLabel: overrides.frontMatterLabel ?? base.frontMatterLabel
33
42
  };
34
43
  }
@@ -12,6 +12,15 @@ export const ja = {
12
12
  imageUploading: '画像を読み込んでいます…',
13
13
  imageLoadError: '画像を表示できませんでした',
14
14
  linkOpen: 'リンク先を開く',
15
+ selectionToolbarLabel: '書式',
16
+ linkAddTitle: 'リンクを追加',
17
+ linkEditTitle: 'リンクを編集',
18
+ linkTextLabel: 'テキスト',
19
+ linkHrefLabel: 'URL',
20
+ linkApply: '適用',
21
+ linkCancel: 'キャンセル',
22
+ linkEdit: '編集',
23
+ linkRemove: 'リンクを外す',
15
24
  frontMatterLabel: 'Front Matter',
16
25
  shortcutCategories: {
17
26
  // グループは利用者の意図で分ける。「ブロックの操作」のような総称は使わない
@@ -82,7 +91,7 @@ export const ja = {
82
91
  'format.italic': '斜体',
83
92
  'format.code': 'インラインコード',
84
93
  'format.strikethrough': '取り消し線',
85
- 'format.link': 'リンク',
94
+ 'format.link': 'リンクを追加・編集',
86
95
  // 一覧は起きうることを全部載せる辞書。`.` を継ぎ足した id は、対象が決まっている
87
96
  // コンテキストメニューが引く「いま起きる向き」の説明である
88
97
  'format.heading': '見出しにする/リスト項目に戻す',
@@ -38,6 +38,24 @@ export interface EditorMessages {
38
38
  readonly imageLoadError: string;
39
39
  /** カーソルがリンクにあるときに出す吹き出し(押すとリンク先を開く)のアクセシブルな名前 */
40
40
  readonly linkOpen: string;
41
+ /** 文字を選んだときに出す帯(選択ツールバー)のアクセシブルな名前 */
42
+ readonly selectionToolbarLabel: string;
43
+ /** リンクを追加するダイアログの見出し */
44
+ readonly linkAddTitle: string;
45
+ /** 既存のリンクを編集するダイアログの見出し */
46
+ readonly linkEditTitle: string;
47
+ /** リンクのダイアログの「文字」の欄の名前 */
48
+ readonly linkTextLabel: string;
49
+ /** リンクのダイアログの「URL」の欄の名前 */
50
+ readonly linkHrefLabel: string;
51
+ /** リンクのダイアログの確定のボタン */
52
+ readonly linkApply: string;
53
+ /** リンクのダイアログの取りやめのボタン */
54
+ readonly linkCancel: string;
55
+ /** リンクの吹き出しの「編集」のボタンの名前 */
56
+ readonly linkEdit: string;
57
+ /** リンクの吹き出しと編集のダイアログの「リンクを外す」のボタンの名前 */
58
+ readonly linkRemove: string;
41
59
  /**
42
60
  * フロントマターにタイトルが無いときに、その場所へ薄く出す名前。
43
61
  * 状態の説明(「未設定です」)ではなく、**そこが何であるか**の表示である
@@ -64,5 +82,14 @@ export interface EditorMessagesOverride {
64
82
  readonly imageUploading?: string;
65
83
  readonly imageLoadError?: string;
66
84
  readonly linkOpen?: string;
85
+ readonly selectionToolbarLabel?: string;
86
+ readonly linkAddTitle?: string;
87
+ readonly linkEditTitle?: string;
88
+ readonly linkTextLabel?: string;
89
+ readonly linkHrefLabel?: string;
90
+ readonly linkApply?: string;
91
+ readonly linkCancel?: string;
92
+ readonly linkEdit?: string;
93
+ readonly linkRemove?: string;
67
94
  readonly frontMatterLabel?: string;
68
95
  }
@@ -56,6 +56,23 @@ export const autoformatRules = [
56
56
  alt: match[1] ?? '',
57
57
  title: match[3] ?? null
58
58
  }))),
59
+ /**
60
+ * 打ったリンク記法を、閉じ括弧まで打ち終えた時点でリンクにする。
61
+ *
62
+ * 範囲の限り方は画像と同じである。手前が `!`(画像)や `\`(エスケープ)のときは
63
+ * 働かない。リンクの文字は消さずに残すため、先に付けていた装飾はそのまま残る。
64
+ */
65
+ new InputRule(/(?<![!\\])\[([^[\]]+)\]\(([^\s()]+)(?:\s+"([^"]*)")?\)$/, (state, match, start, end) => {
66
+ const text = match[1] ?? '';
67
+ const textStart = start + 1;
68
+ const textEnd = textStart + text.length;
69
+ const tr = state.tr;
70
+ // 後ろから消す(前を消すと後ろの位置がずれる)
71
+ tr.delete(textEnd, end);
72
+ tr.delete(start, textStart);
73
+ tr.addMark(start, start + text.length, outlineSchema.marks.link.create({ href: match[2], title: match[3] ?? null }));
74
+ return tr;
75
+ }),
59
76
  markInputRule(/(\*\*([^*]+)\*\*)$/, outlineSchema.marks.strong),
60
77
  markInputRule(/(?:^|[^*])(\*([^*]+)\*)$/, outlineSchema.marks.em),
61
78
  markInputRule(/(~~([^~]+)~~)$/, outlineSchema.marks.strikethrough),
@@ -1,7 +1,30 @@
1
- import type { EditorState, Transaction } from 'prosemirror-state';
1
+ import { type EditorState } from 'prosemirror-state';
2
+ import type { EditorCommand, LinkInput, LinkRange, LinkRun, LinkTarget } from '../operations.types';
2
3
  export declare const URL_PATTERN: RegExp;
3
4
  /**
4
- * リンクの適用と解除。すでにリンクなら解除し、選択した文字が URL ならその URL へのリンクにする。
5
- * URL を入力させる画面はモジュールの責務ではないため、それ以外では何もしない。
5
+ * いまの選択から、リンクのダイアログの対象を決める。リンクを付けられない場所なら `null`。
6
+ *
7
+ * **対象はいつも、触れているリンクの全体を含む**(ダイアログからリンクを途中で分けない)。
8
+ * 同じリンクの中で行き先が分かれても下線は一続きに見え、書き手は気づけないためである。
9
+ * 選んだ範囲が 1 つのリンクの中に収まればそのリンクの編集、境目をまたげばまたいだリンクの
10
+ * 全体を合わせた範囲への追加になる。範囲の外で隣り合うだけのリンクは含めない。
6
11
  */
7
- export declare function toggleLink(state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
12
+ export declare function linkTargetOf(state: EditorState): LinkTarget | null;
13
+ /**
14
+ * 位置 `pos` に触れているリンクの区間。両端を含む(リンクの末尾をタップすると、
15
+ * カーソルはリンクの直後に立つ)。前後で別のリンクに触れているときは後ろを採る。
16
+ */
17
+ export declare function linkRunAt(state: EditorState, pos: number): LinkRun | null;
18
+ /**
19
+ * 対象へリンクを付ける・書き換える・挿入する。実行後のカーソルはリンクの直後に置く。
20
+ *
21
+ * 「文字」の欄を開いたときのまま当てたなら、範囲の文字と書式(太字など)を保ってリンクだけを
22
+ * 付け直す。欄は書式を持たない 1 行の文字なので、欄の中身で置き換えると書式を黙って落とす。
23
+ * 書き換えたとき・挿入のときは、欄の文字(空なら行き先)で置き換える。
24
+ *
25
+ * 直後に打った文字がリンクに入らないのは、リンクのマークが `inclusive: false` だからである。
26
+ * 取り消しの単位は呼び出し側が分ける。
27
+ */
28
+ export declare function applyLink(target: LinkTarget, input: LinkInput): EditorCommand;
29
+ /** 区間からリンクを外す。文字と他の書式は残す */
30
+ export declare function removeLink(range: LinkRange): EditorCommand;
@@ -1,21 +1,118 @@
1
+ import { TextSelection } from 'prosemirror-state';
1
2
  import { outlineSchema } from '../../models/document/schema.js';
3
+ const LINK = outlineSchema.marks.link;
4
+ const CODE = outlineSchema.marks.code;
2
5
  export const URL_PATTERN = /^(https?:\/\/|mailto:)\S+$/;
3
6
  /**
4
- * リンクの適用と解除。すでにリンクなら解除し、選択した文字が URL ならその URL へのリンクにする。
5
- * URL を入力させる画面はモジュールの責務ではないため、それ以外では何もしない。
7
+ * いまの選択から、リンクのダイアログの対象を決める。リンクを付けられない場所なら `null`。
8
+ *
9
+ * **対象はいつも、触れているリンクの全体を含む**(ダイアログからリンクを途中で分けない)。
10
+ * 同じリンクの中で行き先が分かれても下線は一続きに見え、書き手は気づけないためである。
11
+ * 選んだ範囲が 1 つのリンクの中に収まればそのリンクの編集、境目をまたげばまたいだリンクの
12
+ * 全体を合わせた範囲への追加になる。範囲の外で隣り合うだけのリンクは含めない。
6
13
  */
7
- export function toggleLink(state, dispatch) {
8
- const link = outlineSchema.marks.link;
9
- const { from, to, empty } = state.selection;
10
- if (state.doc.rangeHasMark(from, to, link)) {
11
- dispatch?.(state.tr.removeMark(from, to, link));
14
+ export function linkTargetOf(state) {
15
+ const { selection } = state;
16
+ if (!(selection instanceof TextSelection))
17
+ return null;
18
+ const { $from, $to, from, to, empty } = selection;
19
+ // リンクの文字は 1 行の欄で扱うので、段落をまたぐ範囲は表せない
20
+ if ($from.parent !== $to.parent || !$from.parent.isTextblock)
21
+ return null;
22
+ if (!$from.parent.type.allowsMarkType(LINK))
23
+ return null;
24
+ // インラインコードは他のマークと共存しない
25
+ if (empty ? CODE.isInSet($from.marks()) : state.doc.rangeHasMark(from, to, CODE))
26
+ return null;
27
+ if (empty) {
28
+ const run = linkRunAt(state, from);
29
+ return run ? { kind: 'edit', run } : { kind: 'add', range: { from, to }, text: '', href: '' };
30
+ }
31
+ const head = runFrom($from, linkOf($from.nodeAfter?.marks));
32
+ const tail = runFrom($to, linkOf($to.nodeBefore?.marks));
33
+ if (head && tail && head.from === tail.from)
34
+ return { kind: 'edit', run: head };
35
+ const range = { from: head?.from ?? from, to: tail?.to ?? to };
36
+ const text = state.doc.textBetween(range.from, range.to);
37
+ return { kind: 'add', range, text, href: URL_PATTERN.test(text) ? text : '' };
38
+ }
39
+ /**
40
+ * 位置 `pos` に触れているリンクの区間。両端を含む(リンクの末尾をタップすると、
41
+ * カーソルはリンクの直後に立つ)。前後で別のリンクに触れているときは後ろを採る。
42
+ */
43
+ export function linkRunAt(state, pos) {
44
+ const $pos = state.doc.resolve(pos);
45
+ return runFrom($pos, linkOf($pos.nodeAfter?.marks) ?? linkOf($pos.nodeBefore?.marks));
46
+ }
47
+ /**
48
+ * 対象へリンクを付ける・書き換える・挿入する。実行後のカーソルはリンクの直後に置く。
49
+ *
50
+ * 「文字」の欄を開いたときのまま当てたなら、範囲の文字と書式(太字など)を保ってリンクだけを
51
+ * 付け直す。欄は書式を持たない 1 行の文字なので、欄の中身で置き換えると書式を黙って落とす。
52
+ * 書き換えたとき・挿入のときは、欄の文字(空なら行き先)で置き換える。
53
+ *
54
+ * 直後に打った文字がリンクに入らないのは、リンクのマークが `inclusive: false` だからである。
55
+ * 取り消しの単位は呼び出し側が分ける。
56
+ */
57
+ export function applyLink(target, input) {
58
+ const range = target.kind === 'add' ? target.range : target.run;
59
+ const initialText = target.kind === 'add' ? target.text : target.run.text;
60
+ const title = target.kind === 'edit' ? target.run.title : null;
61
+ return (state, dispatch) => {
62
+ if (range.to > state.doc.content.size)
63
+ return false;
64
+ const mark = LINK.create({ href: input.href, title });
65
+ const tr = state.tr;
66
+ let end = range.to;
67
+ if (range.from < range.to && input.text === initialText) {
68
+ tr.removeMark(range.from, range.to, LINK).addMark(range.from, range.to, mark);
69
+ }
70
+ else {
71
+ const text = input.text || input.href;
72
+ tr.replaceWith(range.from, range.to, outlineSchema.text(text, [mark]));
73
+ end = range.from + text.length;
74
+ }
75
+ dispatch?.(tr.setSelection(TextSelection.create(tr.doc, end)).scrollIntoView());
12
76
  return true;
77
+ };
78
+ }
79
+ /** 区間からリンクを外す。文字と他の書式は残す */
80
+ export function removeLink(range) {
81
+ return (state, dispatch) => {
82
+ if (range.to > state.doc.content.size)
83
+ return false;
84
+ dispatch?.(state.tr.removeMark(range.from, range.to, LINK));
85
+ return true;
86
+ };
87
+ }
88
+ /**
89
+ * `$pos` に触れている、`mark` が続く区間。書式が混ざると文字の節は分かれるので、節をまたいで
90
+ * 数える。`mark` は `$pos` の前後どちらかの節から採ったものなので、区間は必ずある。
91
+ */
92
+ function runFrom($pos, mark) {
93
+ if (!mark)
94
+ return null;
95
+ const parent = $pos.parent;
96
+ let from = $pos.start();
97
+ let at = from;
98
+ for (let index = 0; index < parent.childCount; index += 1) {
99
+ const end = at + parent.child(index).nodeSize;
100
+ const next = parent.maybeChild(index + 1);
101
+ if (!mark.isInSet(parent.child(index).marks))
102
+ from = end;
103
+ else if ($pos.pos <= end && !(next && mark.isInSet(next.marks))) {
104
+ return {
105
+ from,
106
+ to: end,
107
+ href: mark.attrs.href,
108
+ title: mark.attrs.title,
109
+ text: $pos.doc.textBetween(from, end)
110
+ };
111
+ }
112
+ at = end;
13
113
  }
14
- if (empty)
15
- return false;
16
- const text = state.doc.textBetween(from, to);
17
- if (!URL_PATTERN.test(text))
18
- return false;
19
- dispatch?.(state.tr.addMark(from, to, link.create({ href: text })));
20
- return true;
114
+ return null;
115
+ }
116
+ function linkOf(marks) {
117
+ return marks?.find((mark) => mark.type === LINK);
21
118
  }
@@ -7,7 +7,6 @@ import { inTable, liftByContext, moveByContext, sectionCommands, sinkByContext }
7
7
  import { fencedBlockOnEnter } from '../commands/fencedBlock.js';
8
8
  import { holdBackspaceAtFrontMatter } from '../commands/frontMatter.js';
9
9
  import { insertLineBreak } from '../commands/lineBreak.js';
10
- import { toggleLink } from '../commands/link.js';
11
10
  import { blockInsideItem, listCommands } from '../commands/list.js';
12
11
  import { dissolveHeadingAtStart, insertHeadingBefore, insertSectionAfter, removeEmptyLeadingBlock, removeUnjoinableEmptyBlock, targetSection } from '../commands/section.js';
13
12
  import { requiredSelectionBlock } from '../commands/selectedBlock.js';
@@ -453,15 +452,6 @@ export const coreActions = [
453
452
  keys: [{ key: 'x', mod: true, shift: true }],
454
453
  command: toggleMark(outlineSchema.marks.strikethrough)
455
454
  },
456
- {
457
- id: 'format.link',
458
- category: 'format',
459
- scope: 'selection',
460
- listed: true,
461
- changesDocument: true,
462
- keys: [{ key: 'k', mod: true }],
463
- command: toggleLink
464
- },
465
455
  {
466
456
  id: 'format.heading',
467
457
  category: 'convert',