@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,206 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
3
|
+
import { outlineSchema } from '../../models/document/schema';
|
|
4
|
+
const MERMAID_BLOCK = outlineSchema.nodes.mermaid_block;
|
|
5
|
+
// --- mermaid の遅延読み込み ---
|
|
6
|
+
//
|
|
7
|
+
// 設定はモジュール全体に効くグローバルであるため、固定値で 1 度だけ初期化し、
|
|
8
|
+
// インスタンスごとに変えない(変えられないことが複数エディタの非干渉を成立させる)。
|
|
9
|
+
// このキャッシュ以外のモジュールスコープ状態を持たない。
|
|
10
|
+
let loading = null;
|
|
11
|
+
let loaded = null;
|
|
12
|
+
/** 1 度だけ動的 import + 初期化する。失敗したら次の呼び出しで再試行する */
|
|
13
|
+
export function loadMermaid() {
|
|
14
|
+
if (!loading) {
|
|
15
|
+
loading = import('mermaid').then(({ default: mermaid }) => {
|
|
16
|
+
mermaid.initialize({
|
|
17
|
+
startOnLoad: false,
|
|
18
|
+
securityLevel: 'strict',
|
|
19
|
+
suppressErrorRendering: true
|
|
20
|
+
});
|
|
21
|
+
loaded = mermaid;
|
|
22
|
+
return mermaid;
|
|
23
|
+
});
|
|
24
|
+
loading.catch(() => {
|
|
25
|
+
loading = null;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return loading;
|
|
29
|
+
}
|
|
30
|
+
/** テストが「import しただけでは読み込まれない」ことを観測するための述語 */
|
|
31
|
+
export function mermaidLoadStarted() {
|
|
32
|
+
return loading !== null;
|
|
33
|
+
}
|
|
34
|
+
// --- プラグイン ---
|
|
35
|
+
/**
|
|
36
|
+
* `mermaid_block` の NodeView と編集中 decoration を 1 つの Plugin として返す。
|
|
37
|
+
* `getText` は呼び出し時点の文言を返す(言語・差し替えの変更に追随させるため関数で渡す)。
|
|
38
|
+
*/
|
|
39
|
+
export function mermaidViewPlugin(getText) {
|
|
40
|
+
return new Plugin({
|
|
41
|
+
props: {
|
|
42
|
+
decorations: editingDecorations,
|
|
43
|
+
nodeViews: {
|
|
44
|
+
mermaid_block: (node, view, _getPos, decorations) => new MermaidBlockView(node, view, getText, decorations)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
const EDITING_CLASS = 'markdown-editor__mermaid-block--editing';
|
|
50
|
+
/**
|
|
51
|
+
* 「選択がブロック内」の Mermaid ブロックへ編集中のフラグ(spec のみの装飾)を当てる。
|
|
52
|
+
* 判定はここに閉じ、NodeView は印を class に写すだけにする。
|
|
53
|
+
* attrs に class を入れると編集基盤が直接 DOM へ当ててしまい、
|
|
54
|
+
* 読み取り専用の判定(NodeView 側)を通れないため、attrs は空にする。
|
|
55
|
+
*/
|
|
56
|
+
function editingDecorations(state) {
|
|
57
|
+
const { $from, $to } = state.selection;
|
|
58
|
+
if ($from.parent.type !== MERMAID_BLOCK || !$from.sameParent($to))
|
|
59
|
+
return DecorationSet.empty;
|
|
60
|
+
return DecorationSet.create(state.doc, [
|
|
61
|
+
Decoration.node($from.before(), $from.after(), {}, { mermaidEditing: true })
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
/** ソース編集から描画までの待ち時間。打鍵ごとの描画を避ける */
|
|
65
|
+
const RENDER_DEBOUNCE_MS = 300;
|
|
66
|
+
class MermaidBlockView {
|
|
67
|
+
dom;
|
|
68
|
+
contentDOM;
|
|
69
|
+
view;
|
|
70
|
+
getText;
|
|
71
|
+
preview;
|
|
72
|
+
status;
|
|
73
|
+
renderId;
|
|
74
|
+
source;
|
|
75
|
+
/** 連続編集で古い描画結果が後から届いても、最新のソースの結果だけを画面へ出す */
|
|
76
|
+
generation = 0;
|
|
77
|
+
timer = null;
|
|
78
|
+
constructor(node, view, getText, decorations) {
|
|
79
|
+
this.view = view;
|
|
80
|
+
this.getText = getText;
|
|
81
|
+
this.renderId = `markdown-editor-mermaid-${Math.random().toString(36).slice(2)}`;
|
|
82
|
+
this.dom = document.createElement('div');
|
|
83
|
+
this.dom.className = 'markdown-editor__mermaid-block';
|
|
84
|
+
this.dom.dataset.mermaidState = 'idle';
|
|
85
|
+
const source = document.createElement('pre');
|
|
86
|
+
source.className = 'markdown-editor__mermaid-source';
|
|
87
|
+
this.contentDOM = document.createElement('code');
|
|
88
|
+
source.append(this.contentDOM);
|
|
89
|
+
// プレビューと状態表示は自前で管理する(編集の対象にしない)
|
|
90
|
+
this.preview = document.createElement('div');
|
|
91
|
+
this.preview.className = 'markdown-editor__mermaid-preview';
|
|
92
|
+
this.preview.contentEditable = 'false';
|
|
93
|
+
this.status = document.createElement('div');
|
|
94
|
+
this.status.className = 'markdown-editor__mermaid-status';
|
|
95
|
+
this.status.contentEditable = 'false';
|
|
96
|
+
this.dom.append(source, this.preview, this.status);
|
|
97
|
+
this.source = node.textContent;
|
|
98
|
+
// 作られた瞬間の選択がすでにブロック内のことがある(` ```mermaid ` の変換直後)。
|
|
99
|
+
// 次の transaction を待つと、その間の打鍵が非表示のソースへ向かい失われる
|
|
100
|
+
this.applyEditing(decorations);
|
|
101
|
+
// 初回は待たせない(読み込み・描画そのものが非同期のため十分)
|
|
102
|
+
this.scheduleRender(0);
|
|
103
|
+
}
|
|
104
|
+
update(node, decorations) {
|
|
105
|
+
if (node.type !== MERMAID_BLOCK)
|
|
106
|
+
return false;
|
|
107
|
+
this.applyEditing(decorations);
|
|
108
|
+
if (node.textContent !== this.source) {
|
|
109
|
+
this.source = node.textContent;
|
|
110
|
+
this.scheduleRender(RENDER_DEBOUNCE_MS);
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
/** プレビュー側の変更を編集基盤の監視から除外する(ソース側は除外しない) */
|
|
115
|
+
ignoreMutation(mutation) {
|
|
116
|
+
if (mutation.type === 'selection')
|
|
117
|
+
return false;
|
|
118
|
+
return !this.contentDOM.contains(mutation.target);
|
|
119
|
+
}
|
|
120
|
+
destroy() {
|
|
121
|
+
if (this.timer !== null)
|
|
122
|
+
clearTimeout(this.timer);
|
|
123
|
+
// 進行中の描画結果を捨てる
|
|
124
|
+
this.generation += 1;
|
|
125
|
+
}
|
|
126
|
+
/** 編集中のフラグは「編集可能 かつ 選択がブロック内(decoration 由来)」のときだけ付く */
|
|
127
|
+
applyEditing(decorations) {
|
|
128
|
+
const editing = this.view.editable && decorations.some((decoration) => decoration.spec.mermaidEditing);
|
|
129
|
+
this.dom.classList.toggle(EDITING_CLASS, editing);
|
|
130
|
+
}
|
|
131
|
+
scheduleRender(delay) {
|
|
132
|
+
if (this.timer !== null)
|
|
133
|
+
clearTimeout(this.timer);
|
|
134
|
+
this.timer = setTimeout(() => {
|
|
135
|
+
this.timer = null;
|
|
136
|
+
// すべての失敗をブロックの表示に閉じ、例外をプラグインの外へ漏らさない
|
|
137
|
+
void this.render().catch(() => this.setState('error'));
|
|
138
|
+
}, delay);
|
|
139
|
+
}
|
|
140
|
+
/** debounce → 構文検査 → 妥当なときだけ描画 → 世代の比較 → SVG 差し替え */
|
|
141
|
+
async render() {
|
|
142
|
+
const generation = ++this.generation;
|
|
143
|
+
const source = this.source;
|
|
144
|
+
if (source.trim() === '') {
|
|
145
|
+
this.preview.replaceChildren();
|
|
146
|
+
this.setState('idle', { empty: true });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
let mermaid = loaded;
|
|
150
|
+
if (!mermaid) {
|
|
151
|
+
// 読み込み中も編集は妨げない(表示だけを変える)
|
|
152
|
+
this.setState('loading');
|
|
153
|
+
try {
|
|
154
|
+
mermaid = await loadMermaid();
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
if (generation !== this.generation)
|
|
158
|
+
return;
|
|
159
|
+
this.setState('load-failed');
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (generation !== this.generation)
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const valid = await mermaid.parse(source, { suppressErrors: true });
|
|
167
|
+
if (generation !== this.generation)
|
|
168
|
+
return;
|
|
169
|
+
if (!valid) {
|
|
170
|
+
// 最後に描画できた図は preview に残したまま、エラー表示だけを付ける
|
|
171
|
+
this.setState('error');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const { svg } = await mermaid.render(`${this.renderId}-${generation}`, source);
|
|
175
|
+
if (generation !== this.generation)
|
|
176
|
+
return;
|
|
177
|
+
this.preview.innerHTML = svg;
|
|
178
|
+
this.setState('rendered');
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
if (generation !== this.generation)
|
|
182
|
+
return;
|
|
183
|
+
this.setState('error');
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/** 状態を modifier class と `data-mermaid-state` に写す。文言は表示を更新するたびに読み直す */
|
|
187
|
+
setState(state, options = {}) {
|
|
188
|
+
this.dom.dataset.mermaidState = state;
|
|
189
|
+
const empty = options.empty ?? false;
|
|
190
|
+
this.dom.classList.toggle('markdown-editor__mermaid-block--empty', empty);
|
|
191
|
+
this.dom.classList.toggle('markdown-editor__mermaid-block--loading', state === 'loading');
|
|
192
|
+
this.dom.classList.toggle('markdown-editor__mermaid-block--error', state === 'error');
|
|
193
|
+
this.dom.classList.toggle('markdown-editor__mermaid-block--load-failed', state === 'load-failed');
|
|
194
|
+
const text = this.getText();
|
|
195
|
+
if (empty)
|
|
196
|
+
this.status.textContent = text.empty;
|
|
197
|
+
else if (state === 'loading')
|
|
198
|
+
this.status.textContent = text.loading;
|
|
199
|
+
else if (state === 'error')
|
|
200
|
+
this.status.textContent = text.renderError;
|
|
201
|
+
else if (state === 'load-failed')
|
|
202
|
+
this.status.textContent = text.loadError;
|
|
203
|
+
else
|
|
204
|
+
this.status.textContent = '';
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import type { EditorCommand, MoveDirection } from '../operations.types';
|
|
3
|
+
export declare function requiredSelectionEntryPlugin(): Plugin<MoveDirection | null>;
|
|
4
|
+
/**
|
|
5
|
+
* 指している状態からの上下移動(要件 2.2, 2.3)。
|
|
6
|
+
*
|
|
7
|
+
* 入ってきたのと同じ向きなら中へ入り、逆の向きなら外へ出て指している状態を解く。
|
|
8
|
+
* ポインタや選択の拡大で指した場合は向きを持たないため、上から来たものとして扱う。
|
|
9
|
+
*/
|
|
10
|
+
export declare function verticalFromSelectedBlock(direction: MoveDirection): EditorCommand;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { keydownHandler } from 'prosemirror-keymap';
|
|
2
|
+
import { NodeSelection, Plugin, PluginKey, Selection, TextSelection } from 'prosemirror-state';
|
|
3
|
+
import { requiredSelectionBlock } from '../commands/selectedBlock';
|
|
4
|
+
/**
|
|
5
|
+
* 上下移動で要選択ブロックの境を越えたとき、中へ入らずそのブロックを指した状態にする。
|
|
6
|
+
*
|
|
7
|
+
* **規則は入られる側が持つ。移動元は何も知らない。** 段落から来ても、表を出て来ても、
|
|
8
|
+
* 箇条書きの項目から来ても、結果は同じになる。移動元ごとの手当てが要らず、
|
|
9
|
+
* ブロックの種別が増えても来る側を直さずに済む。
|
|
10
|
+
*
|
|
11
|
+
* **移動そのものには手を触れない。** 「入る手前で止める」ではなく「入った直後に指し直す」
|
|
12
|
+
* 形にする。同じ処理の中で完結するため、中へ入った状態は画面に現れない。表のセル移動も
|
|
13
|
+
* 含め、従来の上下移動はそのまま働く。
|
|
14
|
+
*
|
|
15
|
+
* 押されたキーの解読は `prosemirror-keymap` に任せる。修飾キー付き(`Shift-ArrowDown` など)
|
|
16
|
+
* は最初から別のキーとして扱われるため、範囲選択の途中で指し直すことがない。
|
|
17
|
+
*/
|
|
18
|
+
const key = new PluginKey('requiredSelectionEntry');
|
|
19
|
+
export function requiredSelectionEntryPlugin() {
|
|
20
|
+
// 押された向きは、その 1 回の移動の結果を見るまでの持ち越しである。
|
|
21
|
+
// プラグインの実体ごとに持つ(同じページに 2 つ置いても混ざらない)
|
|
22
|
+
let pressed = null;
|
|
23
|
+
const readDirection = keydownHandler({
|
|
24
|
+
ArrowUp: () => {
|
|
25
|
+
pressed = 'up';
|
|
26
|
+
return false;
|
|
27
|
+
},
|
|
28
|
+
ArrowDown: () => {
|
|
29
|
+
pressed = 'down';
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return new Plugin({
|
|
34
|
+
key,
|
|
35
|
+
/**
|
|
36
|
+
* 指した状態へ入った向きを覚える。同じ向きでもう一度押せば中へ入り、
|
|
37
|
+
* 逆の向きなら外へ出る(→ `verticalFromSelectedBlock`)。
|
|
38
|
+
* 指した状態でなくなったら忘れる。
|
|
39
|
+
*/
|
|
40
|
+
state: {
|
|
41
|
+
init: () => null,
|
|
42
|
+
apply(tr, value, _oldState, newState) {
|
|
43
|
+
const entered = tr.getMeta(key);
|
|
44
|
+
if (entered !== undefined)
|
|
45
|
+
return entered;
|
|
46
|
+
return requiredSelectionBlock.selectedBlock(newState) ? value : null;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
props: {
|
|
50
|
+
handleKeyDown: (view, event) => {
|
|
51
|
+
// どのキーでも持ち越しは切れる。上下キーなら、その向きを新しく持つ
|
|
52
|
+
pressed = null;
|
|
53
|
+
// 変換が確定していない間は指し直さない(要件 8.8)。キーの割り当てには
|
|
54
|
+
// 経路が一律に覆いを掛けるが、ここはその経路の外にあるので自分で見る
|
|
55
|
+
if (view.composing)
|
|
56
|
+
return false;
|
|
57
|
+
readDirection(view, event);
|
|
58
|
+
return false;
|
|
59
|
+
},
|
|
60
|
+
// ポインタで押した位置には、この規則を働かせない(要件 9.3)
|
|
61
|
+
handleDOMEvents: {
|
|
62
|
+
mousedown: () => {
|
|
63
|
+
pressed = null;
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
appendTransaction: (_transactions, oldState, newState) => {
|
|
69
|
+
const direction = pressed;
|
|
70
|
+
pressed = null;
|
|
71
|
+
if (!direction)
|
|
72
|
+
return null;
|
|
73
|
+
// 文書を変える操作の結果は移動ではない
|
|
74
|
+
if (newState.doc !== oldState.doc)
|
|
75
|
+
return null;
|
|
76
|
+
const entered = requiredSelectionBlock.enclosingBlock(newState.selection.$head);
|
|
77
|
+
if (!entered)
|
|
78
|
+
return null;
|
|
79
|
+
// 中での移動は素通り。入れ子ではもっとも内側どうしで比べる
|
|
80
|
+
const left = requiredSelectionBlock.enclosingBlock(oldState.selection.$head);
|
|
81
|
+
if (left && left.pos === entered.pos)
|
|
82
|
+
return null;
|
|
83
|
+
// そのブロックを指していたところから中へ入ったなら、それは要件 2.2 の移動である
|
|
84
|
+
const pointed = requiredSelectionBlock.selectedBlock(oldState);
|
|
85
|
+
if (pointed && pointed.pos === entered.pos)
|
|
86
|
+
return null;
|
|
87
|
+
return newState.tr
|
|
88
|
+
.setSelection(NodeSelection.create(newState.doc, entered.pos))
|
|
89
|
+
.setMeta(key, direction)
|
|
90
|
+
.scrollIntoView();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 指している状態からの上下移動(要件 2.2, 2.3)。
|
|
96
|
+
*
|
|
97
|
+
* 入ってきたのと同じ向きなら中へ入り、逆の向きなら外へ出て指している状態を解く。
|
|
98
|
+
* ポインタや選択の拡大で指した場合は向きを持たないため、上から来たものとして扱う。
|
|
99
|
+
*/
|
|
100
|
+
export function verticalFromSelectedBlock(direction) {
|
|
101
|
+
return (state, dispatch) => {
|
|
102
|
+
const block = requiredSelectionBlock.selectedBlock(state);
|
|
103
|
+
if (!block)
|
|
104
|
+
return false;
|
|
105
|
+
const entry = key.getState(state) ?? 'down';
|
|
106
|
+
const target = direction === entry
|
|
107
|
+
? insideOf(state.doc, block, entry)
|
|
108
|
+
: outsideOf(state.doc, block, direction);
|
|
109
|
+
if (!target)
|
|
110
|
+
return true;
|
|
111
|
+
dispatch?.(state.tr.setSelection(target).scrollIntoView());
|
|
112
|
+
return true;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** 中へ入る位置。下向きに入ってきたなら中身の先頭、上向きなら末尾 */
|
|
116
|
+
function insideOf(doc, block, entry) {
|
|
117
|
+
const at = entry === 'down' ? block.pos + 1 : block.pos + block.node.nodeSize - 1;
|
|
118
|
+
return TextSelection.near(doc.resolve(at), entry === 'down' ? 1 : -1);
|
|
119
|
+
}
|
|
120
|
+
/** 外へ出る位置。行き先が無ければ何もしない(キーは受け止める) */
|
|
121
|
+
function outsideOf(doc, block, direction) {
|
|
122
|
+
const at = direction === 'down' ? block.pos + block.node.nodeSize : block.pos;
|
|
123
|
+
return Selection.findFrom(doc.resolve(at), direction === 'down' ? 1 : -1);
|
|
124
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* セクションの描画を、**子の増減に追随させる。**
|
|
4
|
+
*
|
|
5
|
+
* 「畳めるか」は子を持つかどうかで決まるが、子が増減してもセクション自身の属性は
|
|
6
|
+
* 変わらない。基盤は属性が同じノードのコンテナを作り直さないため、そのままでは
|
|
7
|
+
* 中身を足しても畳めるようにならず、中身を消しても畳めるままに見える。
|
|
8
|
+
*
|
|
9
|
+
* 一覧ライブラリが項目に対して同じ手を当てている(`createListRenderingPlugin`)。
|
|
10
|
+
* コンテナの作り方は `toDOM` を唯一の出どころにし、ここでは属性を当て直すだけにする。
|
|
11
|
+
*/
|
|
12
|
+
export declare function sectionViewPlugin(): Plugin;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { outlineSchema } from '../../models/document/schema';
|
|
3
|
+
const SECTION = outlineSchema.nodes.section;
|
|
4
|
+
/**
|
|
5
|
+
* セクションの描画を、**子の増減に追随させる。**
|
|
6
|
+
*
|
|
7
|
+
* 「畳めるか」は子を持つかどうかで決まるが、子が増減してもセクション自身の属性は
|
|
8
|
+
* 変わらない。基盤は属性が同じノードのコンテナを作り直さないため、そのままでは
|
|
9
|
+
* 中身を足しても畳めるようにならず、中身を消しても畳めるままに見える。
|
|
10
|
+
*
|
|
11
|
+
* 一覧ライブラリが項目に対して同じ手を当てている(`createListRenderingPlugin`)。
|
|
12
|
+
* コンテナの作り方は `toDOM` を唯一の出どころにし、ここでは属性を当て直すだけにする。
|
|
13
|
+
*/
|
|
14
|
+
export function sectionViewPlugin() {
|
|
15
|
+
return new Plugin({ props: { nodeViews: { section: createSectionView } } });
|
|
16
|
+
}
|
|
17
|
+
function createSectionView(node) {
|
|
18
|
+
const [tag] = renderedSpec(node);
|
|
19
|
+
const dom = document.createElement(tag);
|
|
20
|
+
applyAttributes(dom, node);
|
|
21
|
+
return {
|
|
22
|
+
dom,
|
|
23
|
+
// `toDOM` はコンテナの直下を中身の置き場にしている
|
|
24
|
+
contentDOM: dom,
|
|
25
|
+
update(next) {
|
|
26
|
+
if (next.type !== SECTION)
|
|
27
|
+
return false;
|
|
28
|
+
applyAttributes(dom, next);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function applyAttributes(dom, node) {
|
|
34
|
+
const [, attrs] = renderedSpec(node);
|
|
35
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
36
|
+
if (value === undefined)
|
|
37
|
+
dom.removeAttribute(name);
|
|
38
|
+
else
|
|
39
|
+
dom.setAttribute(name, value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function renderedSpec(node) {
|
|
43
|
+
return SECTION.spec.toDOM(node);
|
|
44
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Plugin, PluginKey, type EditorState, type Transaction } from 'prosemirror-state';
|
|
2
|
+
export interface TabEscapeState {
|
|
3
|
+
readonly escaped: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare const tabEscapeKey: PluginKey<TabEscapeState>;
|
|
6
|
+
/**
|
|
7
|
+
* `Esc` による Tab の退避状態を保持する。
|
|
8
|
+
*
|
|
9
|
+
* 編集エリアは Tab を階層操作に使うため、そのままではキーボードだけで編集エリアから出られない。
|
|
10
|
+
* `Esc` を押した直後だけ Tab をブラウザへ渡し、フォーカスを移動できるようにする。
|
|
11
|
+
* 退避状態は文字入力・カーソル移動で解除される。
|
|
12
|
+
*
|
|
13
|
+
* プラグインは state を持つため、セッションごとに生成する(同一ページの複数エディタで共有しない)。
|
|
14
|
+
*/
|
|
15
|
+
export declare function tabEscapePlugin(): Plugin<TabEscapeState>;
|
|
16
|
+
export declare function isTabEscaped(state: EditorState): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* 退避状態へ移行する。文書・カーソル位置・選択範囲は変更しない。
|
|
19
|
+
* すでに退避状態なら何もしない(`Esc` の重ね押しで履歴を汚さない)。
|
|
20
|
+
*/
|
|
21
|
+
export declare function enterTabEscape(state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Plugin, PluginKey } from 'prosemirror-state';
|
|
2
|
+
export const tabEscapeKey = new PluginKey('markdown-editor/tab-escape');
|
|
3
|
+
const ENTER_ESCAPE = 'enter-tab-escape';
|
|
4
|
+
/**
|
|
5
|
+
* `Esc` による Tab の退避状態を保持する。
|
|
6
|
+
*
|
|
7
|
+
* 編集エリアは Tab を階層操作に使うため、そのままではキーボードだけで編集エリアから出られない。
|
|
8
|
+
* `Esc` を押した直後だけ Tab をブラウザへ渡し、フォーカスを移動できるようにする。
|
|
9
|
+
* 退避状態は文字入力・カーソル移動で解除される。
|
|
10
|
+
*
|
|
11
|
+
* プラグインは state を持つため、セッションごとに生成する(同一ページの複数エディタで共有しない)。
|
|
12
|
+
*/
|
|
13
|
+
export function tabEscapePlugin() {
|
|
14
|
+
return new Plugin({
|
|
15
|
+
key: tabEscapeKey,
|
|
16
|
+
state: {
|
|
17
|
+
init: () => ({ escaped: false }),
|
|
18
|
+
apply: (tr, value) => {
|
|
19
|
+
if (tr.getMeta(ENTER_ESCAPE))
|
|
20
|
+
return { escaped: true };
|
|
21
|
+
// 文字入力・カーソル移動で解除する
|
|
22
|
+
if (tr.docChanged || tr.selectionSet)
|
|
23
|
+
return { escaped: false };
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export function isTabEscaped(state) {
|
|
30
|
+
return tabEscapeKey.getState(state)?.escaped ?? false;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 退避状態へ移行する。文書・カーソル位置・選択範囲は変更しない。
|
|
34
|
+
* すでに退避状態なら何もしない(`Esc` の重ね押しで履歴を汚さない)。
|
|
35
|
+
*/
|
|
36
|
+
export function enterTabEscape(state, dispatch) {
|
|
37
|
+
if (isTabEscaped(state))
|
|
38
|
+
return false;
|
|
39
|
+
dispatch?.(state.tr.setMeta(ENTER_ESCAPE, true).setMeta('addToHistory', false));
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* 列の境目に、押すと列が増える手がかりを出す。
|
|
4
|
+
*
|
|
5
|
+
* 割り当てを覚えていなくても列を足せるようにするための補助である。
|
|
6
|
+
*
|
|
7
|
+
* **印は見出しセルの中へ置き、絶対配置でセルの角へ重ねる。** 境目はセルの縦の罫線
|
|
8
|
+
* そのものなので、**そのセルを基準にすれば位置が構造的に決まる**。表の外(直前の
|
|
9
|
+
* 兄弟)へ帯として置くと、帯・表・直前のブロックの余白が 1 つに合流し、高さを
|
|
10
|
+
* 持たない帯がその中に浮く。落ちる位置は直前の要素の下余白で決まるため、直前が
|
|
11
|
+
* 何かによって印が表から離れていた。
|
|
12
|
+
*
|
|
13
|
+
* 境目は列数 + 1 個ある。**各見出しセルが自分の左端の印を持ち、最後のセルだけ
|
|
14
|
+
* 右端の印をもう 1 つ持つ。**
|
|
15
|
+
*
|
|
16
|
+
* かつては「セルの中に置かない」と決めていた。理由は、境界線の全高を当たり判定に
|
|
17
|
+
* すると、セルの端を押したときにカーソルを置けず列が増えて文字入力と競合するため
|
|
18
|
+
* である。**角に置く小さなマーカーには、その理由は当てはまらない**(セルの端は従来どおり
|
|
19
|
+
* 押せる)。
|
|
20
|
+
*
|
|
21
|
+
* **`tbody` や `tr` の直下へは置けない。** ブラウザが名前のないセルとして数え、
|
|
22
|
+
* その行だけ列がずれる(行の中にカーソルの置き場所が立ったときと同じ崩れ)。
|
|
23
|
+
*/
|
|
24
|
+
export declare function tableHandlesPlugin(): Plugin;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { TableMap } from 'prosemirror-tables';
|
|
3
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
4
|
+
import { insertColumnAt } from '../commands/table';
|
|
5
|
+
import { outlineSchema } from '../../models/document/schema';
|
|
6
|
+
/**
|
|
7
|
+
* 列の境目に、押すと列が増える手がかりを出す。
|
|
8
|
+
*
|
|
9
|
+
* 割り当てを覚えていなくても列を足せるようにするための補助である。
|
|
10
|
+
*
|
|
11
|
+
* **印は見出しセルの中へ置き、絶対配置でセルの角へ重ねる。** 境目はセルの縦の罫線
|
|
12
|
+
* そのものなので、**そのセルを基準にすれば位置が構造的に決まる**。表の外(直前の
|
|
13
|
+
* 兄弟)へ帯として置くと、帯・表・直前のブロックの余白が 1 つに合流し、高さを
|
|
14
|
+
* 持たない帯がその中に浮く。落ちる位置は直前の要素の下余白で決まるため、直前が
|
|
15
|
+
* 何かによって印が表から離れていた。
|
|
16
|
+
*
|
|
17
|
+
* 境目は列数 + 1 個ある。**各見出しセルが自分の左端の印を持ち、最後のセルだけ
|
|
18
|
+
* 右端の印をもう 1 つ持つ。**
|
|
19
|
+
*
|
|
20
|
+
* かつては「セルの中に置かない」と決めていた。理由は、境界線の全高を当たり判定に
|
|
21
|
+
* すると、セルの端を押したときにカーソルを置けず列が増えて文字入力と競合するため
|
|
22
|
+
* である。**角に置く小さなマーカーには、その理由は当てはまらない**(セルの端は従来どおり
|
|
23
|
+
* 押せる)。
|
|
24
|
+
*
|
|
25
|
+
* **`tbody` や `tr` の直下へは置けない。** ブラウザが名前のないセルとして数え、
|
|
26
|
+
* その行だけ列がずれる(行の中にカーソルの置き場所が立ったときと同じ崩れ)。
|
|
27
|
+
*/
|
|
28
|
+
export function tableHandlesPlugin() {
|
|
29
|
+
return new Plugin({
|
|
30
|
+
props: {
|
|
31
|
+
decorations: (state) => {
|
|
32
|
+
const decorations = [];
|
|
33
|
+
state.doc.descendants((node, pos) => {
|
|
34
|
+
if (node.type !== outlineSchema.nodes.table)
|
|
35
|
+
return true;
|
|
36
|
+
const map = TableMap.get(node);
|
|
37
|
+
const tableStart = pos + 1;
|
|
38
|
+
for (let column = 0; column < map.width; column += 1) {
|
|
39
|
+
// 見出し行のセル。中身の先頭へ差し込む
|
|
40
|
+
const cellStart = tableStart + map.map[column];
|
|
41
|
+
decorations.push(Decoration.widget(cellStart + 1, (view) => cellHandles(view, tableStart, column, map.width), {
|
|
42
|
+
side: -1,
|
|
43
|
+
// 印は文書の内容ではない。カーソルの位置決めに数えさせない
|
|
44
|
+
ignoreSelection: true,
|
|
45
|
+
key: `table-handles-${column}-${map.width}`
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
});
|
|
50
|
+
return DecorationSet.create(state.doc, decorations);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 1 つの見出しセルが持つ印。自分の左端の境目と、最後の列なら右端の境目。
|
|
57
|
+
*
|
|
58
|
+
* コンテナは箱を作らない(`display: contents`)。セルの中身の位置を動かさないため、
|
|
59
|
+
* また印の絶対配置の基準を**セル**にするためである。
|
|
60
|
+
*/
|
|
61
|
+
function cellHandles(view, tableStart, column, columns) {
|
|
62
|
+
const holder = document.createElement('span');
|
|
63
|
+
holder.className = 'markdown-editor__table-handles';
|
|
64
|
+
holder.contentEditable = 'false';
|
|
65
|
+
// 読み取り専用では手がかりそのものを作らない
|
|
66
|
+
if (!view.editable)
|
|
67
|
+
return holder;
|
|
68
|
+
holder.append(handleButton(view, tableStart, column, false));
|
|
69
|
+
if (column === columns - 1) {
|
|
70
|
+
holder.append(handleButton(view, tableStart, columns, true));
|
|
71
|
+
}
|
|
72
|
+
return holder;
|
|
73
|
+
}
|
|
74
|
+
function handleButton(view, tableStart, boundary, atEnd) {
|
|
75
|
+
const button = document.createElement('button');
|
|
76
|
+
button.type = 'button';
|
|
77
|
+
button.className = 'markdown-editor__table-handle';
|
|
78
|
+
if (atEnd)
|
|
79
|
+
button.classList.add('markdown-editor__table-handle--end');
|
|
80
|
+
button.dataset.columnBoundary = String(boundary);
|
|
81
|
+
// `mousedown` で止める。押した時点で選択が動くと、表の外へカーソルが飛ぶ
|
|
82
|
+
button.addEventListener('mousedown', (event) => event.preventDefault());
|
|
83
|
+
button.addEventListener('click', (event) => {
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
insertColumnAt(view.state, view.dispatch.bind(view), tableStart, boundary);
|
|
86
|
+
view.focus();
|
|
87
|
+
});
|
|
88
|
+
return button;
|
|
89
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Transaction } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* 「文書まるごとの差し替え」であることの印。
|
|
4
|
+
*
|
|
5
|
+
* 全選択の見張り(`frontMatterGuard`)は、文書全体を選んだ状態でフロントマターへ入った
|
|
6
|
+
* 文字を本文へ移す。差し替えでは**メタデータが入れ替わるのが正しい**ので、その書き換え
|
|
7
|
+
* だけを見張りの外に置く。印が無ければ、書き換えの中身だけからは区別できない。
|
|
8
|
+
*/
|
|
9
|
+
export declare function markDocumentReplacement<T extends Transaction>(transaction: T): T;
|
|
10
|
+
export declare function isDocumentReplacement(transaction: Transaction): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* `from`〜`to` の中身を `insertAt` へ移す。移り先の中身の先頭の位置を返す。
|
|
13
|
+
*
|
|
14
|
+
* **入れてから消す。** 逆にすると、取り除いた瞬間にコンテナの中身が空になり、内容モデルを
|
|
15
|
+
* 満たすための空の段落が自動で補われてそのまま残る。行き先は必ず取り除く範囲より後ろに
|
|
16
|
+
* あるため(`to <= insertAt`)、先に入れても取り除く位置はずれない。
|
|
17
|
+
*
|
|
18
|
+
* 移り先の位置は、**この呼び出しが積んだ 2 つの書き換えだけを通して**写し取る。呼び出し前の
|
|
19
|
+
* 書き換えまで通すと二重に写り、複数の対象を同じセクションへ積む場面で文書の外を指す。
|
|
20
|
+
*/
|
|
21
|
+
export declare function moveContent(tr: Transaction, from: number, to: number, insertAt: number): number;
|
|
22
|
+
/** 移動先のセクションとその配下の見出しレベルを、新しい深さへ合わせ直す */
|
|
23
|
+
export declare function syncFrom(tr: Transaction, pos: number): void;
|
|
24
|
+
/** その位置のブロックを指した状態にする。操作の後も対象を指し続けるために使う(要件 5.8) */
|
|
25
|
+
export declare function selectBlockAt(tr: Transaction, pos: number): void;
|
|
26
|
+
export declare function setCursor(tr: Transaction, pos: number): void;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { NodeSelection, TextSelection } from 'prosemirror-state';
|
|
2
|
+
import { sectionTree } from '../models/document/sectionTree';
|
|
3
|
+
/**
|
|
4
|
+
* `Transaction` を組み立てる道具。コマンドでもプラグインでもない、棚全体で共有する葉。
|
|
5
|
+
* 依存は `prosemirror-state` と `models/document` だけに保つ。
|
|
6
|
+
*/
|
|
7
|
+
const DOCUMENT_REPLACEMENT = 'markdown-editor:document-replacement';
|
|
8
|
+
/**
|
|
9
|
+
* 「文書まるごとの差し替え」であることの印。
|
|
10
|
+
*
|
|
11
|
+
* 全選択の見張り(`frontMatterGuard`)は、文書全体を選んだ状態でフロントマターへ入った
|
|
12
|
+
* 文字を本文へ移す。差し替えでは**メタデータが入れ替わるのが正しい**ので、その書き換え
|
|
13
|
+
* だけを見張りの外に置く。印が無ければ、書き換えの中身だけからは区別できない。
|
|
14
|
+
*/
|
|
15
|
+
export function markDocumentReplacement(transaction) {
|
|
16
|
+
transaction.setMeta(DOCUMENT_REPLACEMENT, true);
|
|
17
|
+
return transaction;
|
|
18
|
+
}
|
|
19
|
+
export function isDocumentReplacement(transaction) {
|
|
20
|
+
return transaction.getMeta(DOCUMENT_REPLACEMENT) === true;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `from`〜`to` の中身を `insertAt` へ移す。移り先の中身の先頭の位置を返す。
|
|
24
|
+
*
|
|
25
|
+
* **入れてから消す。** 逆にすると、取り除いた瞬間にコンテナの中身が空になり、内容モデルを
|
|
26
|
+
* 満たすための空の段落が自動で補われてそのまま残る。行き先は必ず取り除く範囲より後ろに
|
|
27
|
+
* あるため(`to <= insertAt`)、先に入れても取り除く位置はずれない。
|
|
28
|
+
*
|
|
29
|
+
* 移り先の位置は、**この呼び出しが積んだ 2 つの書き換えだけを通して**写し取る。呼び出し前の
|
|
30
|
+
* 書き換えまで通すと二重に写り、複数の対象を同じセクションへ積む場面で文書の外を指す。
|
|
31
|
+
*/
|
|
32
|
+
export function moveContent(tr, from, to, insertAt) {
|
|
33
|
+
const before = tr.steps.length;
|
|
34
|
+
tr.insert(insertAt, tr.doc.slice(from, to).content);
|
|
35
|
+
tr.delete(from, to);
|
|
36
|
+
return tr.mapping.slice(before).map(insertAt, -1);
|
|
37
|
+
}
|
|
38
|
+
/** 移動先のセクションとその配下の見出しレベルを、新しい深さへ合わせ直す */
|
|
39
|
+
export function syncFrom(tr, pos) {
|
|
40
|
+
const ref = sectionTree.resolveSection(tr.doc, pos + 1);
|
|
41
|
+
if (ref)
|
|
42
|
+
sectionTree.syncSectionLevels(tr, ref);
|
|
43
|
+
}
|
|
44
|
+
/** その位置のブロックを指した状態にする。操作の後も対象を指し続けるために使う(要件 5.8) */
|
|
45
|
+
export function selectBlockAt(tr, pos) {
|
|
46
|
+
tr.setSelection(NodeSelection.create(tr.doc, pos));
|
|
47
|
+
}
|
|
48
|
+
export function setCursor(tr, pos) {
|
|
49
|
+
const clamped = Math.max(0, Math.min(pos, tr.doc.content.size));
|
|
50
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(clamped)));
|
|
51
|
+
}
|