@14ch/svelte-md-outliner 0.0.1
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/README.ja.md +190 -0
- package/README.md +190 -0
- package/dist/assets/styles/editor.css +1459 -0
- package/dist/assets/styles/theme.css +486 -0
- package/dist/components/ContextMenu.svelte +279 -0
- package/dist/components/ContextMenu.svelte.d.ts +9 -0
- package/dist/components/EditorSurface.svelte +99 -0
- package/dist/components/EditorSurface.svelte.d.ts +16 -0
- package/dist/components/FocusBreadcrumb.svelte +138 -0
- package/dist/components/FocusBreadcrumb.svelte.d.ts +16 -0
- package/dist/components/MarkdownEditor.svelte +393 -0
- package/dist/components/MarkdownEditor.svelte.d.ts +119 -0
- package/dist/components/ShortcutHelpDialog.svelte +155 -0
- package/dist/components/ShortcutHelpDialog.svelte.d.ts +12 -0
- package/dist/components/internal/Dialog.svelte +157 -0
- package/dist/components/internal/Dialog.svelte.d.ts +23 -0
- package/dist/components/internal/Modal.svelte +181 -0
- package/dist/components/internal/Modal.svelte.d.ts +18 -0
- package/dist/components/internal/Popup.svelte +248 -0
- package/dist/components/internal/Popup.svelte.d.ts +26 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +28 -0
- package/dist/markdown.d.ts +13 -0
- package/dist/markdown.js +10 -0
- package/dist/messages/en.d.ts +2 -0
- package/dist/messages/en.js +107 -0
- package/dist/messages/index.d.ts +13 -0
- package/dist/messages/index.js +31 -0
- package/dist/messages/ja.d.ts +2 -0
- package/dist/messages/ja.js +112 -0
- package/dist/messages/messages.types.d.ts +59 -0
- package/dist/messages/messages.types.js +1 -0
- package/dist/models/document/bodySelection.d.ts +32 -0
- package/dist/models/document/bodySelection.js +43 -0
- package/dist/models/document/document.types.d.ts +27 -0
- package/dist/models/document/document.types.js +8 -0
- package/dist/models/document/foldable.d.ts +18 -0
- package/dist/models/document/foldable.js +21 -0
- package/dist/models/document/schema.d.ts +2 -0
- package/dist/models/document/schema.js +428 -0
- package/dist/models/document/sectionTree.d.ts +15 -0
- package/dist/models/document/sectionTree.js +123 -0
- package/dist/models/markdown/blockOutline.d.ts +9 -0
- package/dist/models/markdown/blockOutline.js +82 -0
- package/dist/models/markdown/frontMatter.d.ts +98 -0
- package/dist/models/markdown/frontMatter.js +300 -0
- package/dist/models/markdown/markdown.types.d.ts +60 -0
- package/dist/models/markdown/markdown.types.js +1 -0
- package/dist/models/markdown/markdownIt.d.ts +1 -0
- package/dist/models/markdown/markdownIt.js +92 -0
- package/dist/models/markdown/parser.d.ts +4 -0
- package/dist/models/markdown/parser.js +505 -0
- package/dist/models/markdown/plainText.d.ts +1 -0
- package/dist/models/markdown/plainText.js +25 -0
- package/dist/models/markdown/serializer.d.ts +17 -0
- package/dist/models/markdown/serializer.js +453 -0
- package/dist/models/viewState/viewState.types.d.ts +38 -0
- package/dist/models/viewState/viewState.types.js +17 -0
- package/dist/models/viewState/viewStateCodec.d.ts +31 -0
- package/dist/models/viewState/viewStateCodec.js +173 -0
- package/dist/operations/autoformat.d.ts +9 -0
- package/dist/operations/autoformat.js +86 -0
- package/dist/operations/commands/block.d.ts +24 -0
- package/dist/operations/commands/block.js +139 -0
- package/dist/operations/commands/codeBlock.d.ts +7 -0
- package/dist/operations/commands/codeBlock.js +26 -0
- package/dist/operations/commands/element.d.ts +67 -0
- package/dist/operations/commands/element.js +269 -0
- package/dist/operations/commands/frontMatter.d.ts +35 -0
- package/dist/operations/commands/frontMatter.js +120 -0
- package/dist/operations/commands/lineBreak.d.ts +9 -0
- package/dist/operations/commands/lineBreak.js +16 -0
- package/dist/operations/commands/link.d.ts +7 -0
- package/dist/operations/commands/link.js +21 -0
- package/dist/operations/commands/list.d.ts +126 -0
- package/dist/operations/commands/list.js +652 -0
- package/dist/operations/commands/mermaidBlock.d.ts +9 -0
- package/dist/operations/commands/mermaidBlock.js +28 -0
- package/dist/operations/commands/section.d.ts +217 -0
- package/dist/operations/commands/section.js +865 -0
- package/dist/operations/commands/sectionFold.d.ts +36 -0
- package/dist/operations/commands/sectionFold.js +219 -0
- package/dist/operations/commands/sectionJump.d.ts +8 -0
- package/dist/operations/commands/sectionJump.js +144 -0
- package/dist/operations/commands/selectedBlock.d.ts +37 -0
- package/dist/operations/commands/selectedBlock.js +170 -0
- package/dist/operations/commands/table.d.ts +64 -0
- package/dist/operations/commands/table.js +437 -0
- package/dist/operations/keymap/assemble.d.ts +22 -0
- package/dist/operations/keymap/assemble.js +165 -0
- package/dist/operations/keymap/coreActions.d.ts +2 -0
- package/dist/operations/keymap/coreActions.js +711 -0
- package/dist/operations/keymap/menuItems.d.ts +52 -0
- package/dist/operations/keymap/menuItems.js +114 -0
- package/dist/operations/keymap/shortcutKeys.d.ts +42 -0
- package/dist/operations/keymap/shortcutKeys.js +114 -0
- package/dist/operations/keymap/viewActions.d.ts +30 -0
- package/dist/operations/keymap/viewActions.js +182 -0
- package/dist/operations/operations.types.d.ts +121 -0
- package/dist/operations/operations.types.js +1 -0
- package/dist/operations/plugins/blockHandle.d.ts +25 -0
- package/dist/operations/plugins/blockHandle.js +112 -0
- package/dist/operations/plugins/contextMenu.d.ts +33 -0
- package/dist/operations/plugins/contextMenu.js +204 -0
- package/dist/operations/plugins/focus.d.ts +39 -0
- package/dist/operations/plugins/focus.js +350 -0
- package/dist/operations/plugins/foldClick.d.ts +11 -0
- package/dist/operations/plugins/foldClick.js +117 -0
- package/dist/operations/plugins/foldGuard.d.ts +15 -0
- package/dist/operations/plugins/foldGuard.js +31 -0
- package/dist/operations/plugins/frontMatterGuard.d.ts +21 -0
- package/dist/operations/plugins/frontMatterGuard.js +150 -0
- package/dist/operations/plugins/frontMatterView.d.ts +34 -0
- package/dist/operations/plugins/frontMatterView.js +137 -0
- package/dist/operations/plugins/linkPaste.d.ts +3 -0
- package/dist/operations/plugins/linkPaste.js +20 -0
- package/dist/operations/plugins/listStructure.d.ts +13 -0
- package/dist/operations/plugins/listStructure.js +18 -0
- package/dist/operations/plugins/markdownClipboard.d.ts +8 -0
- package/dist/operations/plugins/markdownClipboard.js +208 -0
- package/dist/operations/plugins/mermaidView.d.ts +20 -0
- package/dist/operations/plugins/mermaidView.js +206 -0
- package/dist/operations/plugins/requiredSelectionEntry.d.ts +10 -0
- package/dist/operations/plugins/requiredSelectionEntry.js +124 -0
- package/dist/operations/plugins/sectionView.d.ts +12 -0
- package/dist/operations/plugins/sectionView.js +44 -0
- package/dist/operations/plugins/tabEscape.d.ts +21 -0
- package/dist/operations/plugins/tabEscape.js +41 -0
- package/dist/operations/plugins/tableHandles.d.ts +24 -0
- package/dist/operations/plugins/tableHandles.js +89 -0
- package/dist/operations/transaction.d.ts +26 -0
- package/dist/operations/transaction.js +51 -0
- package/dist/operations/transactionOrigin.d.ts +17 -0
- package/dist/operations/transactionOrigin.js +9 -0
- package/dist/states/contextMenuController.svelte.d.ts +57 -0
- package/dist/states/contextMenuController.svelte.js +206 -0
- package/dist/states/editorSession.d.ts +12 -0
- package/dist/states/editorSession.js +268 -0
- package/dist/states/editorSession.types.d.ts +57 -0
- package/dist/states/editorSession.types.js +1 -0
- package/dist/states/outlineViewController.svelte.d.ts +52 -0
- package/dist/states/outlineViewController.svelte.js +134 -0
- package/dist/utils/dateText.d.ts +25 -0
- package/dist/utils/dateText.js +71 -0
- package/dist/utils/geometry.types.d.ts +13 -0
- package/dist/utils/geometry.types.js +1 -0
- package/dist/utils/json.types.d.ts +4 -0
- package/dist/utils/json.types.js +1 -0
- package/dist/utils/platform.d.ts +8 -0
- package/dist/utils/platform.js +10 -0
- package/dist/utils/result.types.d.ts +7 -0
- package/dist/utils/result.types.js +1 -0
- package/package.json +109 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { DecorationSet, type EditorView } from 'prosemirror-view';
|
|
3
|
+
import type { SelectedBlock } from '../operations.types';
|
|
4
|
+
/**
|
|
5
|
+
* 要選択ブロックの外周(押せる帯)を押したとき、そのブロックを指した状態にする。
|
|
6
|
+
* あわせて、帯を描くための印を要選択ブロックへ配る。
|
|
7
|
+
*
|
|
8
|
+
* **帯は擬似要素であり、押されたときに届くのはブロックそのものである。** そのため
|
|
9
|
+
* 「押された位置がブロックの箱の外か中か」で、帯を押したのか中身を押したのかが分かれる。
|
|
10
|
+
* 中を押したときは何もしない(カーソルが置かれる。要件 9.3)。
|
|
11
|
+
*
|
|
12
|
+
* 入れ子(引用の中の表)では、押された位置をもっとも内側のブロックのものとして扱う。
|
|
13
|
+
* 内側の帯は外側のブロックの中にあるため、内側から順に見て最初に当たったものを採る。
|
|
14
|
+
*
|
|
15
|
+
* 既存のホバーの手がかり(折りたたみの記号、表の列を足すハンドル)は、それぞれの要素が
|
|
16
|
+
* 押された位置を含むため、この判定に届かない。手前にある状態がそのまま保たれる。
|
|
17
|
+
*/
|
|
18
|
+
export declare function blockHandlePlugin(): Plugin<DecorationSet>;
|
|
19
|
+
/**
|
|
20
|
+
* 押された位置が帯の上にある要選択ブロック。中身の上や、どこでもなければ null。
|
|
21
|
+
*
|
|
22
|
+
* コンテキストメニューも同じ判定を要る(帯の上で右クリックしたらそのブロックを指す)。
|
|
23
|
+
* 帯の当たり判定は**ここ 1 つ**にし、押下の種類ごとに書き分けない。
|
|
24
|
+
*/
|
|
25
|
+
export declare function blockAtBand(view: EditorView, event: MouseEvent): SelectedBlock | null;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { NodeSelection, Plugin, PluginKey } from 'prosemirror-state';
|
|
2
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
3
|
+
import { requiredSelectionBlock } from '../commands/selectedBlock';
|
|
4
|
+
/**
|
|
5
|
+
* 帯を描くための印。**要選択ブロックだと CSS へ伝える唯一の手段である。**
|
|
6
|
+
*
|
|
7
|
+
* スキーマのフラグ(`requiredSelectionBlock`)は文書の性質であって、`toDOM` にも
|
|
8
|
+
* マークダウンの入出力にも現れない。一方、帯は画面の話でしかない。両者をつなぐ場所が
|
|
9
|
+
* 無いと、CSS 側は種別ごとの selector を手で並べることになり、ノードを増やしたときに
|
|
10
|
+
* 並べ忘れる(Mermaid で実際に 2 度起きた)。**画面用の印は画サーフェスの層で配る。**
|
|
11
|
+
*/
|
|
12
|
+
const BAND_CLASS = 'markdown-editor__required-selection-block';
|
|
13
|
+
const key = new PluginKey('blockHandle');
|
|
14
|
+
/**
|
|
15
|
+
* 要選択ブロックの外周(押せる帯)を押したとき、そのブロックを指した状態にする。
|
|
16
|
+
* あわせて、帯を描くための印を要選択ブロックへ配る。
|
|
17
|
+
*
|
|
18
|
+
* **帯は擬似要素であり、押されたときに届くのはブロックそのものである。** そのため
|
|
19
|
+
* 「押された位置がブロックの箱の外か中か」で、帯を押したのか中身を押したのかが分かれる。
|
|
20
|
+
* 中を押したときは何もしない(カーソルが置かれる。要件 9.3)。
|
|
21
|
+
*
|
|
22
|
+
* 入れ子(引用の中の表)では、押された位置をもっとも内側のブロックのものとして扱う。
|
|
23
|
+
* 内側の帯は外側のブロックの中にあるため、内側から順に見て最初に当たったものを採る。
|
|
24
|
+
*
|
|
25
|
+
* 既存のホバーの手がかり(折りたたみの記号、表の列を足すハンドル)は、それぞれの要素が
|
|
26
|
+
* 押された位置を含むため、この判定に届かない。手前にある状態がそのまま保たれる。
|
|
27
|
+
*/
|
|
28
|
+
export function blockHandlePlugin() {
|
|
29
|
+
return new Plugin({
|
|
30
|
+
key,
|
|
31
|
+
// 印は文書の形だけで決まる。選択やカーソルの移動では作り直さない
|
|
32
|
+
state: {
|
|
33
|
+
init: (_config, state) => bandDecorations(state.doc),
|
|
34
|
+
apply: (tr, value) => (tr.docChanged ? bandDecorations(tr.doc) : value)
|
|
35
|
+
},
|
|
36
|
+
props: {
|
|
37
|
+
decorations: (state) => key.getState(state),
|
|
38
|
+
handleDOMEvents: {
|
|
39
|
+
mousedown: (view, event) => {
|
|
40
|
+
// 読み取り専用では指す状態にしない。指せたところで構造操作も編集も塞がれている
|
|
41
|
+
if (!view.editable)
|
|
42
|
+
return false;
|
|
43
|
+
const block = blockAtBand(view, event);
|
|
44
|
+
if (!block)
|
|
45
|
+
return false;
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, block.pos)));
|
|
48
|
+
view.focus();
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/** 文書の中の要選択ブロックすべてに印を配る。入れ子(引用の中の表)は両方に付く */
|
|
56
|
+
function bandDecorations(doc) {
|
|
57
|
+
const marks = [];
|
|
58
|
+
doc.descendants((node, pos) => {
|
|
59
|
+
if (requiredSelectionBlock.isRequiredSelectionBlock(node))
|
|
60
|
+
marks.push(Decoration.node(pos, pos + node.nodeSize, { class: BAND_CLASS }));
|
|
61
|
+
// 文字の中へは降りない。ブロックを入れられない場所であり、
|
|
62
|
+
// 降りると打鍵のたびに文書中の全テキストを辿ることになる
|
|
63
|
+
return !node.isTextblock;
|
|
64
|
+
});
|
|
65
|
+
return DecorationSet.create(doc, marks);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 押された位置が帯の上にある要選択ブロック。中身の上や、どこでもなければ null。
|
|
69
|
+
*
|
|
70
|
+
* コンテキストメニューも同じ判定を要る(帯の上で右クリックしたらそのブロックを指す)。
|
|
71
|
+
* 帯の当たり判定は**ここ 1 つ**にし、押下の種類ごとに書き分けない。
|
|
72
|
+
*/
|
|
73
|
+
export function blockAtBand(view, event) {
|
|
74
|
+
const target = event.target;
|
|
75
|
+
if (!(target instanceof Element))
|
|
76
|
+
return null;
|
|
77
|
+
const pos = view.posAtDOM(target, 0, -1);
|
|
78
|
+
if (pos < 0 || pos > view.state.doc.content.size)
|
|
79
|
+
return null;
|
|
80
|
+
const band = bandWidth(view.dom);
|
|
81
|
+
const $pos = view.state.doc.resolve(pos);
|
|
82
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
83
|
+
const node = $pos.node(depth);
|
|
84
|
+
if (!requiredSelectionBlock.isRequiredSelectionBlock(node))
|
|
85
|
+
continue;
|
|
86
|
+
const blockPos = $pos.before(depth);
|
|
87
|
+
const dom = view.nodeDOM(blockPos);
|
|
88
|
+
if (!(dom instanceof Element))
|
|
89
|
+
continue;
|
|
90
|
+
const box = dom.getBoundingClientRect();
|
|
91
|
+
// 中身を押したなら、外側のブロックの帯でもない(内側が手前にある)
|
|
92
|
+
if (within(box, event, 0))
|
|
93
|
+
return null;
|
|
94
|
+
if (within(box, event, band))
|
|
95
|
+
return { node, pos: blockPos };
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
function within(box, event, margin) {
|
|
100
|
+
return (event.clientX >= box.left - margin &&
|
|
101
|
+
event.clientX <= box.right + margin &&
|
|
102
|
+
event.clientY >= box.top - margin &&
|
|
103
|
+
event.clientY <= box.bottom + margin);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 帯の太さは利用者が差し替えられる(`0` にすれば帯を使わない選択になる)。
|
|
107
|
+
* 当たり判定と見た目を同じ値から導くため、描かれている値をそのまま読む。
|
|
108
|
+
*/
|
|
109
|
+
function bandWidth(dom) {
|
|
110
|
+
const value = getComputedStyle(dom).getPropertyValue('--markdown-editor-block-band');
|
|
111
|
+
return Number.parseFloat(value) || 0;
|
|
112
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { type EditorView } from 'prosemirror-view';
|
|
3
|
+
import type { ElementTarget } from '../operations.types';
|
|
4
|
+
import type { AnchorRect } from '../../utils/geometry.types';
|
|
5
|
+
/** 開く要求。何を対象に、どこへ出すか */
|
|
6
|
+
export interface MenuOpenRequest {
|
|
7
|
+
readonly target: ElementTarget;
|
|
8
|
+
readonly anchor: AnchorRect;
|
|
9
|
+
}
|
|
10
|
+
export interface ContextMenuPluginApi {
|
|
11
|
+
readonly plugin: Plugin;
|
|
12
|
+
/** 対象のマーカーを立てる・消す。文書は変えない */
|
|
13
|
+
setTarget(view: EditorView, target: ElementTarget | null): void;
|
|
14
|
+
/** いまの選択から対象と錨を決める。決められなければ null */
|
|
15
|
+
resolveFromSelection(view: EditorView): MenuOpenRequest | null;
|
|
16
|
+
}
|
|
17
|
+
export interface ContextMenuPluginOptions {
|
|
18
|
+
/** 開く要求。受け取った側が開いたなら真を返す */
|
|
19
|
+
readonly onRequest: (view: EditorView, request: MenuOpenRequest) => boolean;
|
|
20
|
+
/** 開いているか。開いているあいだのポインタ操作を閉じるために見る */
|
|
21
|
+
readonly isOpen: () => boolean;
|
|
22
|
+
readonly close: () => void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 右クリックを受け止めて対象を決め、対象のマーカーを出す。
|
|
26
|
+
*
|
|
27
|
+
* `handleDOMEvents.contextmenu` で受ける。この経路は編集可否を問わず呼ばれるため、
|
|
28
|
+
* 読み取り専用でも届く(表示分類のキーや折りたたみの押下と同じ経路)。
|
|
29
|
+
*
|
|
30
|
+
* **エディタ 1 つにつき 1 つ生成する。** プラグインの状態(対象のマーカー)をインスタンスへ
|
|
31
|
+
* 閉じるため、キーもここで作る。
|
|
32
|
+
*/
|
|
33
|
+
export declare function createContextMenuPlugin(options: ContextMenuPluginOptions): ContextMenuPluginApi;
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { NodeSelection, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
|
2
|
+
import { CellSelection, cellAround } from 'prosemirror-tables';
|
|
3
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
4
|
+
import { sectionCommands } from '../commands/element';
|
|
5
|
+
import { blockAtBand } from './blockHandle';
|
|
6
|
+
/** 対象アイテムのマーカー。見た目は `editor.css` が持つ(利用者が上書きできる) */
|
|
7
|
+
const TARGET_CLASS = 'markdown-editor__menu-target';
|
|
8
|
+
/**
|
|
9
|
+
* 右クリックを受け止めて対象を決め、対象のマーカーを出す。
|
|
10
|
+
*
|
|
11
|
+
* `handleDOMEvents.contextmenu` で受ける。この経路は編集可否を問わず呼ばれるため、
|
|
12
|
+
* 読み取り専用でも届く(表示分類のキーや折りたたみの押下と同じ経路)。
|
|
13
|
+
*
|
|
14
|
+
* **エディタ 1 つにつき 1 つ生成する。** プラグインの状態(対象のマーカー)をインスタンスへ
|
|
15
|
+
* 閉じるため、キーもここで作る。
|
|
16
|
+
*/
|
|
17
|
+
export function createContextMenuPlugin(options) {
|
|
18
|
+
const key = new PluginKey('markdownEditorContextMenu');
|
|
19
|
+
/**
|
|
20
|
+
* 右押しの時点の選択。**実行環境は右押しの直後に、押した位置の単語を選ぶ。**
|
|
21
|
+
* その選択を利用者が持っていたものと見なすと、既定のメニューへ譲る規則が誤って
|
|
22
|
+
* 発火し、こちらのメニューが出ないまま単語だけが選ばれた状態になる。
|
|
23
|
+
*
|
|
24
|
+
* エディタ 1 つにつき 1 つのプラグインなので、ここに持てる。
|
|
25
|
+
*/
|
|
26
|
+
let selectionAtPress = null;
|
|
27
|
+
const plugin = new Plugin({
|
|
28
|
+
key,
|
|
29
|
+
state: {
|
|
30
|
+
init: () => ({ target: null }),
|
|
31
|
+
apply(tr, value, _oldState, newState) {
|
|
32
|
+
const next = tr.getMeta(key);
|
|
33
|
+
if (next !== undefined)
|
|
34
|
+
return { target: next };
|
|
35
|
+
if (!tr.docChanged || !value.target)
|
|
36
|
+
return value;
|
|
37
|
+
// 開いているあいだ文書は変わらない。写像は取り消しや本文差し替えが
|
|
38
|
+
// 挟まった場合の保険である
|
|
39
|
+
const pos = tr.mapping.map(value.target.pos, -1);
|
|
40
|
+
const node = newState.doc.nodeAt(pos);
|
|
41
|
+
return node ? { target: { ...value.target, node, pos } } : { target: null };
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
props: {
|
|
45
|
+
decorations(state) {
|
|
46
|
+
const target = key.getState(state)?.target;
|
|
47
|
+
if (!target)
|
|
48
|
+
return DecorationSet.empty;
|
|
49
|
+
return DecorationSet.create(state.doc, [
|
|
50
|
+
Decoration.node(target.pos, target.pos + target.node.nodeSize, {
|
|
51
|
+
class: TARGET_CLASS
|
|
52
|
+
})
|
|
53
|
+
]);
|
|
54
|
+
},
|
|
55
|
+
handleDOMEvents: {
|
|
56
|
+
contextmenu: (view, event) => {
|
|
57
|
+
// 押した時点の選択で判定する。キーから開いた場合は押下が無いので今の選択
|
|
58
|
+
const pressed = selectionAtPress ?? view.state.selection;
|
|
59
|
+
selectionAtPress = null;
|
|
60
|
+
return handleContextMenu(view, event, options, pressed);
|
|
61
|
+
},
|
|
62
|
+
mousedown: (view, event) => {
|
|
63
|
+
// 開いているあいだの押下は閉じるためだけに使う。カーソルは動かさない
|
|
64
|
+
if (options.isOpen()) {
|
|
65
|
+
options.close();
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 保つべき選択の内側での**右押し**は、既定の処理へ渡さない。
|
|
71
|
+
*
|
|
72
|
+
* `contextmenu` は `mousedown` の後に届く。そのまま渡すと、届く前に
|
|
73
|
+
* 実行環境がカーソルを押された位置へ移し、セル選択が潰れてしまう
|
|
74
|
+
* (行・列の削除は選択があって初めて効く)。
|
|
75
|
+
*/
|
|
76
|
+
if (event.button !== 2)
|
|
77
|
+
return false;
|
|
78
|
+
selectionAtPress = view.state.selection;
|
|
79
|
+
const pos = posAt(view, event);
|
|
80
|
+
if (pos === undefined || !isInsideKeptSelection(view.state, pos))
|
|
81
|
+
return false;
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
plugin,
|
|
90
|
+
setTarget(view, target) {
|
|
91
|
+
// 文書を変えず、取り消しの対象にもしない
|
|
92
|
+
view.dispatch(view.state.tr.setMeta(key, target).setMeta('addToHistory', false));
|
|
93
|
+
},
|
|
94
|
+
resolveFromSelection
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 押された場所を解決してから開く要求を出す。
|
|
99
|
+
*
|
|
100
|
+
* **解決は上から順に、最初に当たった規則で決まる。**
|
|
101
|
+
*
|
|
102
|
+
* | 順 | 押された場所 | 挙動 |
|
|
103
|
+
* | -- | -------------------------------------- | ---------------------------------------- |
|
|
104
|
+
* | 1 | 要選択ブロックの押せる帯の上 | そのブロックを指して開く |
|
|
105
|
+
* | 2 | 空でない文字列選択の内側 | 受け止めない(実行環境の既定に委ねる) |
|
|
106
|
+
* | 3 | セル選択・ブロックを指す状態の内側 | 選択を保って開く |
|
|
107
|
+
* | 4 | それ以外 | その位置へカーソルを置いて開く |
|
|
108
|
+
*
|
|
109
|
+
* 2 は既定のメニューにしかできないこと(コピー・辞書引き・翻訳)へ譲るためである。
|
|
110
|
+
* 3 はカーソルを移すと行・列の削除が消えるためである。
|
|
111
|
+
*
|
|
112
|
+
* **帯を最初に見る。** 帯はブロックの箱の外にあり、そこには文書の位置が無い
|
|
113
|
+
* (`posAtCoords` が答えない)。選択の内側かどうかは位置が要るため、後段では判定できない。
|
|
114
|
+
* 帯は中身の外なので、選択の内側と重なることもない。
|
|
115
|
+
*/
|
|
116
|
+
function handleContextMenu(view, event, options, pressedSelection) {
|
|
117
|
+
const band = blockAtBand(view, event);
|
|
118
|
+
if (band) {
|
|
119
|
+
view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, band.pos)));
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const pos = posAt(view, event);
|
|
123
|
+
if (pos === undefined)
|
|
124
|
+
return false;
|
|
125
|
+
if (isInsideTextSelection(pressedSelection, pos))
|
|
126
|
+
return false;
|
|
127
|
+
if (!isInsideKeptSelection(view.state, pos)) {
|
|
128
|
+
view.dispatch(view.state.tr.setSelection(TextSelection.near(view.state.doc.resolve(pos))));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// 対象は既存の決め方をそのまま呼ぶ。メニューのための別の決め方を作らない
|
|
132
|
+
const target = sectionCommands.targetElement(view.state);
|
|
133
|
+
if (!target)
|
|
134
|
+
return false;
|
|
135
|
+
const anchor = {
|
|
136
|
+
left: event.clientX,
|
|
137
|
+
right: event.clientX,
|
|
138
|
+
top: event.clientY,
|
|
139
|
+
bottom: event.clientY
|
|
140
|
+
};
|
|
141
|
+
if (!options.onRequest(view, { target, anchor }))
|
|
142
|
+
return false;
|
|
143
|
+
event.preventDefault();
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
/** 押された座標が指す文書の位置。ブロックの箱の外(帯の上)では答えが無い */
|
|
147
|
+
function posAt(view, event) {
|
|
148
|
+
return view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos;
|
|
149
|
+
}
|
|
150
|
+
/** 空でない文字列選択の内側か */
|
|
151
|
+
function isInsideTextSelection(selection, pos) {
|
|
152
|
+
if (!(selection instanceof TextSelection) || selection.empty)
|
|
153
|
+
return false;
|
|
154
|
+
return pos >= selection.from && pos <= selection.to;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* 保つべき選択(セル選択・ブロックを指している状態)の内側か。
|
|
158
|
+
*
|
|
159
|
+
* どちらもカーソルを移すと潰れ、行・列の削除やブロックへの操作が消える。
|
|
160
|
+
*/
|
|
161
|
+
function isInsideKeptSelection(state, pos) {
|
|
162
|
+
const selection = state.selection;
|
|
163
|
+
if (selection instanceof CellSelection)
|
|
164
|
+
return isInSelectedCell(selection, state, pos);
|
|
165
|
+
if (selection instanceof NodeSelection)
|
|
166
|
+
return pos >= selection.from && pos <= selection.to;
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* 押された位置を含むセルが、選ばれているセルのひとつか。
|
|
171
|
+
*
|
|
172
|
+
* **位置の大小では見ない。** セル選択の範囲はセルの*中身*であり、押された位置はセルの
|
|
173
|
+
* 境目(中身の外)に落ちることがある。含むセルまで広げてから照合する。
|
|
174
|
+
*/
|
|
175
|
+
function isInSelectedCell(selection, state, pos) {
|
|
176
|
+
const $cell = cellAround(state.doc.resolve(pos));
|
|
177
|
+
const cell = $cell?.nodeAfter;
|
|
178
|
+
if (!$cell || !cell)
|
|
179
|
+
return false;
|
|
180
|
+
const to = $cell.pos + cell.nodeSize;
|
|
181
|
+
return selection.ranges.some((range) => range.$from.pos >= $cell.pos && range.$to.pos <= to);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* いまの選択から対象と錨を決める(キーからの経路)。
|
|
185
|
+
*
|
|
186
|
+
* セクションが対象でも錨はキャレットにする。セクションの箱は配下すべてを含むため、
|
|
187
|
+
* 箱を錨にするとメニューが本文のはるか下に出る。
|
|
188
|
+
*/
|
|
189
|
+
function resolveFromSelection(view) {
|
|
190
|
+
const target = sectionCommands.targetElement(view.state);
|
|
191
|
+
if (!target)
|
|
192
|
+
return null;
|
|
193
|
+
return { target, anchor: anchorFromSelection(view) };
|
|
194
|
+
}
|
|
195
|
+
function anchorFromSelection(view) {
|
|
196
|
+
const selection = view.state.selection;
|
|
197
|
+
if (selection instanceof NodeSelection) {
|
|
198
|
+
const dom = view.nodeDOM(selection.from);
|
|
199
|
+
if (dom instanceof Element)
|
|
200
|
+
return dom.getBoundingClientRect();
|
|
201
|
+
}
|
|
202
|
+
const coords = view.coordsAtPos(selection.from);
|
|
203
|
+
return { left: coords.left, right: coords.left, top: coords.top, bottom: coords.bottom };
|
|
204
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Plugin, type EditorState } from 'prosemirror-state';
|
|
2
|
+
import type { SectionPath } from '../../models/document/document.types';
|
|
3
|
+
import type { EditorCommand } from '../operations.types';
|
|
4
|
+
/** 範囲外のブロックに付ける印。隠すかどうかの決定は CSS に委ねる */
|
|
5
|
+
export declare const OUT_OF_FOCUS_CLASS = "markdown-editor__out-of-focus";
|
|
6
|
+
export interface FocusBreadcrumbItem {
|
|
7
|
+
readonly path: SectionPath;
|
|
8
|
+
readonly title: string;
|
|
9
|
+
}
|
|
10
|
+
export interface FocusState {
|
|
11
|
+
/** フォーカス対象の section の開始位置。Transaction ごとに写像する */
|
|
12
|
+
readonly anchorPos: number | null;
|
|
13
|
+
/** 上位見出しの連なり。末尾がフォーカスしている当人 */
|
|
14
|
+
readonly breadcrumb: readonly FocusBreadcrumbItem[];
|
|
15
|
+
/** 解除時に戻す位置。これも写像の対象 */
|
|
16
|
+
readonly restorePos: number | null;
|
|
17
|
+
readonly restoreScrollTop: number | null;
|
|
18
|
+
}
|
|
19
|
+
export interface FocusPluginApi {
|
|
20
|
+
readonly plugin: Plugin;
|
|
21
|
+
readonly focusSection: EditorCommand;
|
|
22
|
+
readonly clearFocus: EditorCommand;
|
|
23
|
+
readonly applyFocus: (path: SectionPath | null) => EditorCommand;
|
|
24
|
+
getState(state: EditorState): FocusState;
|
|
25
|
+
/** 外部化のときだけ呼ぶ。位置から `SectionPath` を導出する */
|
|
26
|
+
getFocusedPath(state: EditorState): SectionPath | null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* ひとつのサブツリーだけを表示する(ズーム)。
|
|
30
|
+
*
|
|
31
|
+
* **フォーカスは文書に 1 つしかない状態**なので、折りたたみと違って属性では持たない。
|
|
32
|
+
* 属性にすると複製や貼り付けで「2 つフォーカスされている」というあってはならない形を
|
|
33
|
+
* 表現できてしまい、そのたびに 1 つへ戻す見張りが要る。単一の値として持てば、
|
|
34
|
+
* その状態は最初から作れない。
|
|
35
|
+
*
|
|
36
|
+
* エディタ 1 インスタンスにつき 1 つ生成する(モジュールに共有の状態を置かない)。
|
|
37
|
+
*/
|
|
38
|
+
export declare function createFocusPlugin(): FocusPluginApi;
|
|
39
|
+
export declare function scrollParentOf(element: HTMLElement): HTMLElement | null;
|