@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
@@ -0,0 +1,13 @@
1
+ import type { EditorView } from 'prosemirror-view';
2
+ import type { EditorAction } from '../operations.types';
3
+ /**
4
+ * ⌘K(`format.link`)の宣言。**宣言はここに置き、ダイアログの状態はコントローラが持つ**
5
+ * (`createContextMenuShortcut` と同じ形)。
6
+ *
7
+ * 開く操作そのものは文書を変えないが、`changesDocument` を真にする。開いた先で文書を
8
+ * 変えるのが目的の操作であり、読み取り専用では覆いに塞がせるためである。
9
+ *
10
+ * 試し実行(`dispatch` なし)では開かず、開けるかだけを答える。帯がボタンを出すかの判定に
11
+ * 使う。開けない場所では偽を返し、キーを後段へ譲る。
12
+ */
13
+ export declare function createLinkActions(openLinkDialog: (view: EditorView) => boolean): readonly EditorAction[];
@@ -0,0 +1,30 @@
1
+ import { linkTargetOf } from '../commands/link.js';
2
+ /**
3
+ * ⌘K(`format.link`)の宣言。**宣言はここに置き、ダイアログの状態はコントローラが持つ**
4
+ * (`createContextMenuShortcut` と同じ形)。
5
+ *
6
+ * 開く操作そのものは文書を変えないが、`changesDocument` を真にする。開いた先で文書を
7
+ * 変えるのが目的の操作であり、読み取り専用では覆いに塞がせるためである。
8
+ *
9
+ * 試し実行(`dispatch` なし)では開かず、開けるかだけを答える。帯がボタンを出すかの判定に
10
+ * 使う。開けない場所では偽を返し、キーを後段へ譲る。
11
+ */
12
+ export function createLinkActions(openLinkDialog) {
13
+ return [
14
+ {
15
+ id: 'format.link',
16
+ category: 'format',
17
+ scope: 'selection',
18
+ listed: true,
19
+ changesDocument: true,
20
+ keys: [{ key: 'k', mod: true }],
21
+ command: (state, dispatch, view) => {
22
+ if (!linkTargetOf(state))
23
+ return false;
24
+ if (!dispatch || !view)
25
+ return true;
26
+ return openLinkDialog(view);
27
+ }
28
+ }
29
+ ];
30
+ }
@@ -0,0 +1,17 @@
1
+ import type { EditorView } from 'prosemirror-view';
2
+ import type { EditorAction, ToolbarButton } from '../operations.types';
3
+ /**
4
+ * 宣言と現在の選択から、帯へ並べるボタンを導く層(`menuItems.ts` と同じ位置づけ)。
5
+ * 帯専用の操作は持たず、宣言をそのまま実行する。
6
+ */
7
+ /** 帯に並べる操作の順。文字の書式を先に並べ、リンクは右端に置く */
8
+ export declare const TOOLBAR_ACTION_IDS: readonly string[];
9
+ /**
10
+ * いまの選択で働くボタンだけを並べる。働かないボタンは無効として残さず、出さない。
11
+ *
12
+ * 働くかは、覆い付きの宣言の試し実行(読み取り専用・変換中・マークを許さないブロックを落とす)
13
+ * に加えて、マークの 4 つは押すと文書が実際に変わることで決める。`toggleMark` はブロックが
14
+ * マークを許すかしか見ず、マークどうしの排他(インラインコードは他のマークと共存しない)を
15
+ * 見ないので、試し実行だけではインラインコードの上に太字のボタンが出てしまう。
16
+ */
17
+ export declare function toolbarButtonsFor(actions: readonly EditorAction[], view: EditorView): readonly ToolbarButton[];
@@ -0,0 +1,62 @@
1
+ import { outlineSchema } from '../../models/document/schema.js';
2
+ import { editorKeymap } from './assemble.js';
3
+ /**
4
+ * 宣言と現在の選択から、帯へ並べるボタンを導く層(`menuItems.ts` と同じ位置づけ)。
5
+ * 帯専用の操作は持たず、宣言をそのまま実行する。
6
+ */
7
+ /** 帯に並べる操作の順。文字の書式を先に並べ、リンクは右端に置く */
8
+ export const TOOLBAR_ACTION_IDS = [
9
+ 'format.bold',
10
+ 'format.italic',
11
+ 'format.strikethrough',
12
+ 'format.code',
13
+ 'format.link'
14
+ ];
15
+ const MARKS = {
16
+ 'format.link': outlineSchema.marks.link,
17
+ 'format.bold': outlineSchema.marks.strong,
18
+ 'format.italic': outlineSchema.marks.em,
19
+ 'format.strikethrough': outlineSchema.marks.strikethrough,
20
+ 'format.code': outlineSchema.marks.code
21
+ };
22
+ /**
23
+ * いまの選択で働くボタンだけを並べる。働かないボタンは無効として残さず、出さない。
24
+ *
25
+ * 働くかは、覆い付きの宣言の試し実行(読み取り専用・変換中・マークを許さないブロックを落とす)
26
+ * に加えて、マークの 4 つは押すと文書が実際に変わることで決める。`toggleMark` はブロックが
27
+ * マークを許すかしか見ず、マークどうしの排他(インラインコードは他のマークと共存しない)を
28
+ * 見ないので、試し実行だけではインラインコードの上に太字のボタンが出てしまう。
29
+ */
30
+ export function toolbarButtonsFor(actions, view) {
31
+ const { state } = view;
32
+ const { from, to } = state.selection;
33
+ return TOOLBAR_ACTION_IDS.flatMap((id) => {
34
+ const action = actions.find((entry) => entry.id === id);
35
+ if (!action || !editorKeymap.decorate(action)(state, undefined, view))
36
+ return [];
37
+ const type = MARKS[id];
38
+ if (id !== 'format.link' && !markTogglable(state, type))
39
+ return [];
40
+ return [{ action, active: state.doc.rangeHasMark(from, to, type) }];
41
+ });
42
+ }
43
+ /**
44
+ * 押すと文書が変わるか。**`toggleMark` の動きに合わせる**——範囲のどこかに付いていれば外し、
45
+ * どこにも無ければ付ける。付くのは、既存のマークのどれにも排他されない文字である。
46
+ * 付いている見た目と押した結果も同じ判定で揃う(一部だけ付いていれば、付いていると示して外す)。
47
+ */
48
+ function markTogglable(state, type) {
49
+ const { from, to } = state.selection;
50
+ if (state.doc.rangeHasMark(from, to, type))
51
+ return true;
52
+ let addable = false;
53
+ state.doc.nodesBetween(from, to, (node, _pos, parent) => {
54
+ if (addable)
55
+ return false;
56
+ if (!node.isText || !parent?.type.allowsMarkType(type))
57
+ return true;
58
+ addable = !node.marks.some((mark) => mark.type.excludes(type));
59
+ return true;
60
+ });
61
+ return addable;
62
+ }
@@ -117,3 +117,54 @@ export type TableDeleteTarget = 'table' | 'column' | 'row';
117
117
  export type ColumnDirection = 'left' | 'right';
118
118
  /** 表の中で選択を広げる向き。上下は行、左右は列をたどる */
119
119
  export type CellDirection = MoveDirection | ColumnDirection;
120
+ /** 文書の中の範囲 */
121
+ export interface LinkRange {
122
+ readonly from: number;
123
+ readonly to: number;
124
+ }
125
+ /** 同じリンクが続く区間。書式が混ざって文字の節が分かれていても 1 つに数える */
126
+ export interface LinkRun extends LinkRange {
127
+ readonly href: string;
128
+ readonly title: string | null;
129
+ /** 区間の文字(画像などの葉は含めない) */
130
+ readonly text: string;
131
+ }
132
+ /**
133
+ * リンクのダイアログの対象。**対象はいつも、触れているリンクの全体を含む**
134
+ * (ダイアログからリンクを途中で分けない)。
135
+ */
136
+ export type LinkTarget =
137
+ /** 選んだ範囲へ付ける(from < to)、またはカーソルの位置へ挿入する(from === to) */
138
+ {
139
+ readonly kind: 'add';
140
+ readonly range: LinkRange;
141
+ /** 「文字」の欄の初期値(選んだ文字。挿入なら空) */
142
+ readonly text: string;
143
+ /** 「URL」の欄の初期値(選んだ文字が URL ならそれ) */
144
+ readonly href: string;
145
+ }
146
+ /** 既存のリンクの全体を直す */
147
+ | {
148
+ readonly kind: 'edit';
149
+ readonly run: LinkRun;
150
+ };
151
+ /** ダイアログに入力された値。`href` は整えた後のもの */
152
+ export interface LinkInput {
153
+ readonly text: string;
154
+ readonly href: string;
155
+ }
156
+ /** 帯を選択範囲のどちら側に出すか */
157
+ export type ToolbarSide = 'above' | 'below';
158
+ /** 帯を出す選択範囲と、出す側 */
159
+ export interface SelectionToolbarAnchor {
160
+ readonly from: number;
161
+ readonly to: number;
162
+ /** ポインタで選んだなら上、タッチで選んだなら下(実行環境が上に出すメニューと重ねない) */
163
+ readonly side: ToolbarSide;
164
+ }
165
+ /** 帯に並べるボタン 1 つ */
166
+ export interface ToolbarButton {
167
+ readonly action: EditorAction;
168
+ /** 選んだ範囲のどこかにその書式が付いているか。真なら、押すと外れる(リンクを除く) */
169
+ readonly active: boolean;
170
+ }
@@ -1,6 +1,8 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
2
  /** 変換に由来する Transaction に ProseMirror が付ける印。`prosemirror-history` も読んでいる */
3
3
  const COMPOSITION_META = 'composition';
4
+ /** 上限で書き込める側へ移ったことを知らせる Transaction の印 */
5
+ const SETTLED_META = 'compositionSettled';
4
6
  /**
5
7
  * 確定の反映を待つ上限。ProseMirror は `compositionend` の 20ms 後に未反映の記録を流すため、
6
8
  * **それより後ろに置く**。前に置くと、確定した文字が入る前に書き込めることになる。
@@ -19,6 +21,7 @@ export const SETTLE_LIMIT_MS = 50;
19
21
  export function createCompositionState() {
20
22
  let writeState = 'ready';
21
23
  let settleLimit = null;
24
+ let currentView = null;
22
25
  function stopWaiting(next) {
23
26
  if (settleLimit !== null)
24
27
  clearTimeout(settleLimit);
@@ -37,11 +40,20 @@ export function createCompositionState() {
37
40
  * 入ったことは変換に由来する Transaction の到着で知り、上限はその取りこぼし
38
41
  * に対する保険である。どちらも「遅い側」であり、早める道は置かない。
39
42
  */
43
+ /**
44
+ * 上限で抜けたときは、抜けたことを Transaction で知らせる。確定した文字の取り込みは
45
+ * `compositionend` より前に済むことがあり(ページに `input` のリスナーがあると、
46
+ * その直後に取り込まれる)、そのときは後から Transaction が来ない。知らせないと、
47
+ * 見送った書き込みの当て直しが次の編集まで走らない。
48
+ */
40
49
  compositionend: () => {
41
50
  stopWaiting('pending');
42
51
  settleLimit = setTimeout(() => {
43
52
  settleLimit = null;
44
53
  writeState = 'ready';
54
+ if (currentView && !currentView.isDestroyed) {
55
+ currentView.dispatch(currentView.state.tr.setMeta(SETTLED_META, true));
56
+ }
45
57
  }, SETTLE_LIMIT_MS);
46
58
  return false;
47
59
  }
@@ -55,7 +67,15 @@ export function createCompositionState() {
55
67
  stopWaiting('ready');
56
68
  return null;
57
69
  },
58
- view: () => ({ destroy: () => stopWaiting('ready') })
70
+ view: (view) => {
71
+ currentView = view;
72
+ return {
73
+ destroy: () => {
74
+ currentView = null;
75
+ stopWaiting('ready');
76
+ }
77
+ };
78
+ }
59
79
  });
60
80
  return {
61
81
  plugin,
@@ -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,9 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ /**
3
+ * 文書が変わった更新のたびに知らせる。ダイアログを開いているかは呼ばれた側が見る。
4
+ *
5
+ * ダイアログを開いているあいだは焦点が編集エリアの外にあり、書き手はエディタを変えられない。
6
+ * このあいだの文書の変化は外からのもの(`bind:value` / `applyMarkdown` / 他の書き手の変更)
7
+ * であり、ダイアログが控えた位置を古くする。
8
+ */
9
+ export declare function linkDialogGuardPlugin(onDocChange: () => void): Plugin;
@@ -0,0 +1,18 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ /**
3
+ * 文書が変わった更新のたびに知らせる。ダイアログを開いているかは呼ばれた側が見る。
4
+ *
5
+ * ダイアログを開いているあいだは焦点が編集エリアの外にあり、書き手はエディタを変えられない。
6
+ * このあいだの文書の変化は外からのもの(`bind:value` / `applyMarkdown` / 他の書き手の変更)
7
+ * であり、ダイアログが控えた位置を古くする。
8
+ */
9
+ export function linkDialogGuardPlugin(onDocChange) {
10
+ return new Plugin({
11
+ view: () => ({
12
+ update: (view, previous) => {
13
+ if (view.state.doc !== previous.doc)
14
+ onDocChange();
15
+ }
16
+ })
17
+ });
18
+ }
@@ -0,0 +1,22 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import type { EditorView } from 'prosemirror-view';
3
+ import type { LinkRun } from '../operations.types';
4
+ /**
5
+ * 吹き出しを出すリンクを見張り、変わるたびに知らせる。表示は持たない(→ `LinkPopover`)。
6
+ *
7
+ * **出すのは、リンクを押して(タップして)カーソルを入れたときだけである。** 開きたいかも
8
+ * しれないという合図はそれしかない。矢印キーでリンクを通り過ぎるとき・リンクの文字を
9
+ * 直しているときに出ると邪魔になる。押した後に文字を打つ・キーでカーソルを動かすと消える。
10
+ *
11
+ * 押したかどうかは、押したこと(`mousedown`)で立て、押していないことの合図——キーを押した・
12
+ * 文書が変わった——で降ろす。選択が動いたことでは降ろさない。編集基盤が
13
+ * 押して動いた選択に付ける印(`pointer`)は、押してから 50ms を過ぎて読まれた選択には
14
+ * 付かず、重いページや実機のタップでは押したのに印が無いことがあるためである。キーで
15
+ * カーソルを動かすときは、必ずその前にキーが押される。`click` では受けない——リンクの
16
+ * クリックは `linkClick` が既定の動きを止めるので、後ろのプラグインへ届かない。
17
+ * タップでも `mousedown` は届く。
18
+ *
19
+ * 編集中に焦点があるときだけ出す。読み取り専用では素のクリックで開くので出さない。
20
+ * 変換の最中も出さない。
21
+ */
22
+ export declare function linkPopoverPlugin(onChange: (view: EditorView, link: LinkRun | null) => void): Plugin;
@@ -0,0 +1,72 @@
1
+ import { Plugin, PluginKey } from 'prosemirror-state';
2
+ import { linkRunAt } from '../commands/link.js';
3
+ import { isEditorFocused } from './editorFocus.js';
4
+ /**
5
+ * 吹き出しを出すリンクを見張り、変わるたびに知らせる。表示は持たない(→ `LinkPopover`)。
6
+ *
7
+ * **出すのは、リンクを押して(タップして)カーソルを入れたときだけである。** 開きたいかも
8
+ * しれないという合図はそれしかない。矢印キーでリンクを通り過ぎるとき・リンクの文字を
9
+ * 直しているときに出ると邪魔になる。押した後に文字を打つ・キーでカーソルを動かすと消える。
10
+ *
11
+ * 押したかどうかは、押したこと(`mousedown`)で立て、押していないことの合図——キーを押した・
12
+ * 文書が変わった——で降ろす。選択が動いたことでは降ろさない。編集基盤が
13
+ * 押して動いた選択に付ける印(`pointer`)は、押してから 50ms を過ぎて読まれた選択には
14
+ * 付かず、重いページや実機のタップでは押したのに印が無いことがあるためである。キーで
15
+ * カーソルを動かすときは、必ずその前にキーが押される。`click` では受けない——リンクの
16
+ * クリックは `linkClick` が既定の動きを止めるので、後ろのプラグインへ届かない。
17
+ * タップでも `mousedown` は届く。
18
+ *
19
+ * 編集中に焦点があるときだけ出す。読み取り専用では素のクリックで開くので出さない。
20
+ * 変換の最中も出さない。
21
+ */
22
+ export function linkPopoverPlugin(onChange) {
23
+ const notify = (view) => onChange(view, view.editable && !view.composing && isEditorFocused(view.state) && pointed(view.state)
24
+ ? linkAtCursor(view.state)
25
+ : null);
26
+ return new Plugin({
27
+ key: linkPopoverKey,
28
+ state: {
29
+ init: () => false,
30
+ // 押したときに立て、文書の変更で降ろす。キーの合図は印で届く
31
+ apply: (tr, current) => {
32
+ const signal = tr.getMeta(linkPopoverKey);
33
+ if (signal !== undefined)
34
+ return signal;
35
+ if (tr.docChanged)
36
+ return false;
37
+ return current;
38
+ }
39
+ },
40
+ props: {
41
+ handleDOMEvents: {
42
+ mousedown: (view) => {
43
+ if (!pointed(view.state))
44
+ view.dispatch(view.state.tr.setMeta(linkPopoverKey, true));
45
+ return false;
46
+ },
47
+ keydown: (view) => {
48
+ if (pointed(view.state))
49
+ view.dispatch(view.state.tr.setMeta(linkPopoverKey, false));
50
+ return false;
51
+ }
52
+ }
53
+ },
54
+ view: (view) => {
55
+ notify(view);
56
+ return {
57
+ update: notify,
58
+ destroy: () => onChange(view, null)
59
+ };
60
+ }
61
+ });
62
+ }
63
+ const linkPopoverKey = new PluginKey('linkPopover');
64
+ /** カーソルが、押して(タップして)置かれたままか */
65
+ function pointed(state) {
66
+ return linkPopoverKey.getState(state);
67
+ }
68
+ /** カーソルが触れているリンク。範囲を選んでいるときは返さない(選ぶのは直すためである) */
69
+ function linkAtCursor(state) {
70
+ const { selection } = state;
71
+ return selection.empty ? linkRunAt(state, selection.from) : null;
72
+ }
@@ -0,0 +1,15 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import type { EditorView } from 'prosemirror-view';
3
+ import type { SelectionToolbarAnchor } from '../operations.types';
4
+ /**
5
+ * 帯を出すかを見張り、変わるたびに知らせる。表示は持たない(→ `SelectionToolbar`)。
6
+ *
7
+ * 出すのは、編集できて・焦点があって・変換の最中でなく・空でない文字の範囲選択があるとき
8
+ * だけである。ポインタで広げているあいだは出さず、離した時点で出す。離す場所は編集エリアの
9
+ * 外のこともあるので、離したことは `window` で受ける。
10
+ *
11
+ * `Escape` は基盤の `editing.escapeTab` が持っているので、宣言にせずここで照合して
12
+ * 帯を消す。**偽を返して後段へ譲る**——`Escape` の既存の働き(次の `Tab` を外へ出す)は
13
+ * 止めない。
14
+ */
15
+ export declare function selectionToolbarPlugin(onChange: (view: EditorView, anchor: SelectionToolbarAnchor | null) => void): Plugin;
@@ -0,0 +1,96 @@
1
+ import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
2
+ import { createKeyStrokeMatcher } from '../keymap/shortcutKeys.js';
3
+ import { isEditorFocused } from './editorFocus.js';
4
+ const selectionToolbarKey = new PluginKey('selectionToolbar');
5
+ /**
6
+ * 帯を出すかを見張り、変わるたびに知らせる。表示は持たない(→ `SelectionToolbar`)。
7
+ *
8
+ * 出すのは、編集できて・焦点があって・変換の最中でなく・空でない文字の範囲選択があるとき
9
+ * だけである。ポインタで広げているあいだは出さず、離した時点で出す。離す場所は編集エリアの
10
+ * 外のこともあるので、離したことは `window` で受ける。
11
+ *
12
+ * `Escape` は基盤の `editing.escapeTab` が持っているので、宣言にせずここで照合して
13
+ * 帯を消す。**偽を返して後段へ譲る**——`Escape` の既存の働き(次の `Tab` を外へ出す)は
14
+ * 止めない。
15
+ */
16
+ export function selectionToolbarPlugin(onChange) {
17
+ /**
18
+ * 変換の最中か。`view.composing` だけでは足りない——編集基盤は変換の始まりで画面を
19
+ * 描き直し、そのあいだはまだ `composing` が偽なので、消した帯がそこで出し直される。
20
+ */
21
+ let composing = false;
22
+ const notify = (view) => onChange(view, composing ? null : anchorOf(view));
23
+ const matchEscape = createKeyStrokeMatcher([{ keys: { key: 'Escape' }, run: () => true }]);
24
+ return new Plugin({
25
+ key: selectionToolbarKey,
26
+ state: {
27
+ init: () => ({ dragging: false, dismissed: false, pointer: 'mouse' }),
28
+ apply: (tr, current, before, after) => {
29
+ const next = { ...current, ...tr.getMeta(selectionToolbarKey) };
30
+ return after.selection.eq(before.selection) ? next : { ...next, dismissed: false };
31
+ }
32
+ },
33
+ props: {
34
+ handleDOMEvents: {
35
+ pointerdown: (view, event) => {
36
+ const pointer = event.pointerType === 'touch' ? 'touch' : 'mouse';
37
+ view.dispatch(view.state.tr.setMeta(selectionToolbarKey, { dragging: true, pointer }));
38
+ return false;
39
+ },
40
+ keydown: (view, event) => {
41
+ if (anchorOf(view) && matchEscape(view, event)) {
42
+ view.dispatch(view.state.tr.setMeta(selectionToolbarKey, { dismissed: true }));
43
+ }
44
+ return false;
45
+ },
46
+ // 変換の始まりは transaction を伴わないので、ここで消す
47
+ compositionstart: (view) => {
48
+ composing = true;
49
+ onChange(view, null);
50
+ return false;
51
+ },
52
+ compositionend: () => {
53
+ composing = false;
54
+ return false;
55
+ }
56
+ }
57
+ },
58
+ view: (view) => {
59
+ const release = () => {
60
+ if (!watchOf(view.state).dragging)
61
+ return;
62
+ view.dispatch(view.state.tr.setMeta(selectionToolbarKey, { dragging: false }));
63
+ };
64
+ const window = view.dom.ownerDocument.defaultView;
65
+ window?.addEventListener('pointerup', release);
66
+ window?.addEventListener('pointercancel', release);
67
+ notify(view);
68
+ return {
69
+ update: notify,
70
+ destroy: () => {
71
+ window?.removeEventListener('pointerup', release);
72
+ window?.removeEventListener('pointercancel', release);
73
+ onChange(view, null);
74
+ }
75
+ };
76
+ }
77
+ });
78
+ }
79
+ function anchorOf(view) {
80
+ const { selection } = view.state;
81
+ const watch = watchOf(view.state);
82
+ if (!view.editable || view.composing || !isEditorFocused(view.state))
83
+ return null;
84
+ if (watch.dragging || watch.dismissed)
85
+ return null;
86
+ if (!(selection instanceof TextSelection) || selection.empty)
87
+ return null;
88
+ return {
89
+ from: selection.from,
90
+ to: selection.to,
91
+ side: watch.pointer === 'touch' ? 'below' : 'above'
92
+ };
93
+ }
94
+ function watchOf(state) {
95
+ return selectionToolbarKey.getState(state);
96
+ }
@@ -0,0 +1,43 @@
1
+ import type { EditorView } from 'prosemirror-view';
2
+ import type { LinkRange, LinkRun, LinkTarget } from '../operations/operations.types';
3
+ import type { EditorExtension, EditorSession } from './editorSession.types';
4
+ export interface LinkDialogControllerOptions {
5
+ /** 現在のセッション。生き死にの管理はコンポーネントが持つ */
6
+ readonly getSession: () => EditorSession | null;
7
+ }
8
+ export interface LinkDialogRequest {
9
+ readonly target: LinkTarget;
10
+ /** 取りやめたときに戻す選択 */
11
+ readonly restore: LinkRange;
12
+ }
13
+ export interface LinkDialogController {
14
+ /** 基盤へ渡す拡張(⌘K の宣言と、開いているあいだの文書の変化の見張り) */
15
+ readonly extension: EditorExtension;
16
+ /** 開いているダイアログの対象。`target.kind` で出すダイアログが決まる */
17
+ readonly request: LinkDialogRequest | null;
18
+ /** いまの選択に対して開く(帯のリンクのボタン・⌘K)。開けなければ偽 */
19
+ openFromSelection(view: EditorView): boolean;
20
+ /** 吹き出しの「編集」から、そのリンクに対して開く */
21
+ openForLink(view: EditorView, run: LinkRun): void;
22
+ /** 適用する。`href` は整える前の入力 */
23
+ submit(input: {
24
+ readonly text: string;
25
+ readonly href: string;
26
+ }): void;
27
+ /** リンクを外す(編集のダイアログだけが呼ぶ) */
28
+ remove(): void;
29
+ /** ダイアログが閉じた(経路を問わない) */
30
+ closed(): void;
31
+ }
32
+ /**
33
+ * リンクのダイアログの開閉と適用を、エディタ 1 インスタンスに閉じて持つ。
34
+ *
35
+ * **閉じたときの扱いは、閉じた理由(局面)で決まる。** 開く前の選択へ戻すのは取りやめ
36
+ * (`open` から閉じた)ときだけである。`open` のあいだに文書が変われば必ず `invalidated`
37
+ * へ移るので、戻す位置は常に今の文書の中にある。`applying` へは文書を変える**前に**移る。
38
+ * 自分の適用が起こす文書の変化を、外からの変化と取り違えないためである。
39
+ *
40
+ * `request` が `null` でないのは `open` のあいだだけである。ダイアログの部品は `request` が
41
+ * 消えたら自分から閉じ、閉じ終えたら `closed` を呼ぶ。
42
+ */
43
+ export declare function createLinkDialogController(options: LinkDialogControllerOptions): LinkDialogController;
@@ -0,0 +1,104 @@
1
+ import { closeHistory } from 'prosemirror-history';
2
+ import { TextSelection } from 'prosemirror-state';
3
+ import { applyLink, linkTargetOf, removeLink } from '../operations/commands/link.js';
4
+ import { createLinkActions } from '../operations/keymap/linkActions.js';
5
+ import { linkDialogGuardPlugin } from '../operations/plugins/linkDialogGuard.js';
6
+ import { normalizeLinkHref } from '../utils/linkHref.js';
7
+ /**
8
+ * リンクのダイアログの開閉と適用を、エディタ 1 インスタンスに閉じて持つ。
9
+ *
10
+ * **閉じたときの扱いは、閉じた理由(局面)で決まる。** 開く前の選択へ戻すのは取りやめ
11
+ * (`open` から閉じた)ときだけである。`open` のあいだに文書が変われば必ず `invalidated`
12
+ * へ移るので、戻す位置は常に今の文書の中にある。`applying` へは文書を変える**前に**移る。
13
+ * 自分の適用が起こす文書の変化を、外からの変化と取り違えないためである。
14
+ *
15
+ * `request` が `null` でないのは `open` のあいだだけである。ダイアログの部品は `request` が
16
+ * 消えたら自分から閉じ、閉じ終えたら `closed` を呼ぶ。
17
+ */
18
+ export function createLinkDialogController(options) {
19
+ let request = $state(null);
20
+ let phase = 'closed';
21
+ const extension = {
22
+ plugins: [linkDialogGuardPlugin(invalidate)],
23
+ actions: createLinkActions(openFromSelection)
24
+ };
25
+ function viewOf() {
26
+ return options.getSession()?.getView() ?? null;
27
+ }
28
+ function openFromSelection(view) {
29
+ if (!view.editable || view.composing)
30
+ return false;
31
+ const target = linkTargetOf(view.state);
32
+ if (!target)
33
+ return false;
34
+ const { from, to } = view.state.selection;
35
+ const range = target.kind === 'add' ? target.range : target.run;
36
+ // 対象が選択より広がったことを、本文の選択で見せる
37
+ if (range.from < from || range.to > to) {
38
+ view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, range.from, range.to)));
39
+ }
40
+ begin(target, { from, to });
41
+ return true;
42
+ }
43
+ function openForLink(view, run) {
44
+ if (!view.editable || view.composing)
45
+ return;
46
+ const { from, to } = view.state.selection;
47
+ begin({ kind: 'edit', run }, { from, to });
48
+ }
49
+ function begin(target, restore) {
50
+ request = { target, restore };
51
+ phase = 'open';
52
+ }
53
+ function submit(input) {
54
+ const href = normalizeLinkHref(input.href);
55
+ if (!request || phase !== 'open' || href === null)
56
+ return;
57
+ apply(applyLink(request.target, { text: input.text, href }));
58
+ }
59
+ function remove() {
60
+ if (!request || phase !== 'open' || request.target.kind !== 'edit')
61
+ return;
62
+ apply(removeLink(request.target.run));
63
+ }
64
+ /** 1 つの取り消しの単位として当てる */
65
+ function apply(command) {
66
+ const view = viewOf();
67
+ phase = 'applying';
68
+ request = null;
69
+ if (!view)
70
+ return;
71
+ command(view.state, (tr) => view.dispatch(closeHistory(tr)), view);
72
+ }
73
+ function invalidate() {
74
+ if (phase !== 'open')
75
+ return;
76
+ phase = 'invalidated';
77
+ request = null;
78
+ }
79
+ function closed() {
80
+ if (phase === 'closed')
81
+ return;
82
+ const view = viewOf();
83
+ const restore = phase === 'open' ? request?.restore : null;
84
+ phase = 'closed';
85
+ request = null;
86
+ if (!view)
87
+ return;
88
+ if (restore) {
89
+ view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, restore.from, restore.to)));
90
+ }
91
+ view.focus();
92
+ }
93
+ return {
94
+ extension,
95
+ get request() {
96
+ return request;
97
+ },
98
+ openFromSelection,
99
+ openForLink,
100
+ submit,
101
+ remove,
102
+ closed
103
+ };
104
+ }