@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,350 @@
|
|
|
1
|
+
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
|
2
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
3
|
+
import { outlineSchema } from '../../models/document/schema';
|
|
4
|
+
import { sectionTree } from '../../models/document/sectionTree';
|
|
5
|
+
import { originOf } from '../transactionOrigin';
|
|
6
|
+
const SECTION = outlineSchema.nodes.section;
|
|
7
|
+
/** 範囲外のブロックに付ける印。隠すかどうかの決定は CSS に委ねる */
|
|
8
|
+
export const OUT_OF_FOCUS_CLASS = 'markdown-editor__out-of-focus';
|
|
9
|
+
const EMPTY = {
|
|
10
|
+
anchorPos: null,
|
|
11
|
+
breadcrumb: [],
|
|
12
|
+
restorePos: null,
|
|
13
|
+
restoreScrollTop: null
|
|
14
|
+
};
|
|
15
|
+
/** 復元を済ませたことを状態へ伝える印。フォーカスの指示とは別物である */
|
|
16
|
+
const RESTORE_DONE = { restored: true };
|
|
17
|
+
const IDLE = {
|
|
18
|
+
...EMPTY,
|
|
19
|
+
decorations: DecorationSet.empty,
|
|
20
|
+
pendingRestore: null
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* ひとつのサブツリーだけを表示する(ズーム)。
|
|
24
|
+
*
|
|
25
|
+
* **フォーカスは文書に 1 つしかない状態**なので、折りたたみと違って属性では持たない。
|
|
26
|
+
* 属性にすると複製や貼り付けで「2 つフォーカスされている」というあってはならない形を
|
|
27
|
+
* 表現できてしまい、そのたびに 1 つへ戻す見張りが要る。単一の値として持てば、
|
|
28
|
+
* その状態は最初から作れない。
|
|
29
|
+
*
|
|
30
|
+
* エディタ 1 インスタンスにつき 1 つ生成する(モジュールに共有の状態を置かない)。
|
|
31
|
+
*/
|
|
32
|
+
export function createFocusPlugin() {
|
|
33
|
+
const key = new PluginKey('markdownEditorFocus');
|
|
34
|
+
const plugin = new Plugin({
|
|
35
|
+
key,
|
|
36
|
+
state: {
|
|
37
|
+
init: () => IDLE,
|
|
38
|
+
apply: (tr, value, _oldState, newState) => nextState(key, tr, value, newState.doc)
|
|
39
|
+
},
|
|
40
|
+
props: {
|
|
41
|
+
decorations: (state) => key.getState(state)?.decorations ?? DecorationSet.empty
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* 文書を変える操作は、範囲外に触るものを通さない。
|
|
45
|
+
*
|
|
46
|
+
* 選択を閉じ込めるだけでは足りない。カーソルが範囲の内側にあっても、削除キーは
|
|
47
|
+
* 境界を越えて外側の構造を変える。画面には対象しか映っていないため、
|
|
48
|
+
* 書き手には何が起きたか分からない。
|
|
49
|
+
*/
|
|
50
|
+
filterTransaction: (tr, state) => {
|
|
51
|
+
if (!tr.docChanged)
|
|
52
|
+
return true;
|
|
53
|
+
const range = focusRange(state.doc, key.getState(state)?.anchorPos ?? null);
|
|
54
|
+
if (!range)
|
|
55
|
+
return true;
|
|
56
|
+
// 折りたたみは表示だけを変える。利用者のアプリ由来の差し替えを止めると、
|
|
57
|
+
// 渡された本文が黙って落ちる(誰にも伝わらないまま食い違いが残る)
|
|
58
|
+
if (originOf(tr) !== null)
|
|
59
|
+
return true;
|
|
60
|
+
return staysInside(tr, range);
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* 選択は通したうえで**境界の内側へ丸める**。
|
|
64
|
+
*
|
|
65
|
+
* 可否しか返せない `filterTransaction` で拒否すると、「境界で止まる」ではなく
|
|
66
|
+
* 「選択がまったく動かない」になる。全選択のように步(ステップ)を持たない操作も、
|
|
67
|
+
* ここでなければ捕まえられない。
|
|
68
|
+
*/
|
|
69
|
+
appendTransaction: (_transactions, _oldState, newState) => {
|
|
70
|
+
const current = key.getState(newState);
|
|
71
|
+
// 対象が消えて解除されたときの復元
|
|
72
|
+
if (current?.pendingRestore != null) {
|
|
73
|
+
const pos = Math.min(current.pendingRestore, newState.doc.content.size);
|
|
74
|
+
const restored = newState.tr.setSelection(TextSelection.near(newState.doc.resolve(pos), 1));
|
|
75
|
+
restored.setMeta(key, RESTORE_DONE);
|
|
76
|
+
restored.setMeta('addToHistory', false);
|
|
77
|
+
return restored.scrollIntoView();
|
|
78
|
+
}
|
|
79
|
+
const range = focusRange(newState.doc, current?.anchorPos ?? null);
|
|
80
|
+
if (!range)
|
|
81
|
+
return null;
|
|
82
|
+
const selection = newState.selection;
|
|
83
|
+
const anchor = clampInside(newState.doc, selection.anchor, range);
|
|
84
|
+
const head = clampInside(newState.doc, selection.head, range);
|
|
85
|
+
if (anchor === selection.anchor && head === selection.head)
|
|
86
|
+
return null;
|
|
87
|
+
const tr = newState.tr.setSelection(TextSelection.create(newState.doc, anchor, head));
|
|
88
|
+
tr.setMeta('addToHistory', false);
|
|
89
|
+
return tr;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
function stateOf(state) {
|
|
93
|
+
return key.getState(state) ?? IDLE;
|
|
94
|
+
}
|
|
95
|
+
const focusSection = (state, dispatch, view) => {
|
|
96
|
+
const target = enclosingSectionPos(state.doc, state.selection.from);
|
|
97
|
+
// 見出しを持たないセクションはフォーカスの対象にしない(アドレスを持たないため)
|
|
98
|
+
if (target === null)
|
|
99
|
+
return false;
|
|
100
|
+
if (stateOf(state).anchorPos === target)
|
|
101
|
+
return false;
|
|
102
|
+
if (!dispatch)
|
|
103
|
+
return true;
|
|
104
|
+
dispatch(requestFocus(state, key, {
|
|
105
|
+
anchorPos: target,
|
|
106
|
+
restorePos: state.selection.from,
|
|
107
|
+
restoreScrollTop: scrollTopOf(view)
|
|
108
|
+
}));
|
|
109
|
+
return true;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* フォーカスを解き、開始前のカーソル位置とスクロール位置へ戻す。
|
|
113
|
+
*
|
|
114
|
+
* 解除と選択の復元を 1 つの Transaction にまとめる。分けると、解除された直後の状態で
|
|
115
|
+
* 範囲外の選択を内側へ丸める仕組みが働き、戻したい位置が失われる。
|
|
116
|
+
*/
|
|
117
|
+
const clearFocus = (state, dispatch, view) => {
|
|
118
|
+
const current = stateOf(state);
|
|
119
|
+
if (current.anchorPos === null)
|
|
120
|
+
return false;
|
|
121
|
+
if (!dispatch)
|
|
122
|
+
return true;
|
|
123
|
+
const tr = requestFocus(state, key, null);
|
|
124
|
+
if (current.restorePos !== null) {
|
|
125
|
+
const pos = Math.min(current.restorePos, tr.doc.content.size);
|
|
126
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1));
|
|
127
|
+
}
|
|
128
|
+
const scroller = view && current.restoreScrollTop !== null ? scrollParentOf(view.dom) : null;
|
|
129
|
+
dispatch(scroller ? tr : tr.scrollIntoView());
|
|
130
|
+
// 表示が組み直された後でなければ、巻き取り位置を戻しても上書きされる
|
|
131
|
+
if (scroller)
|
|
132
|
+
scroller.scrollTop = current.restoreScrollTop;
|
|
133
|
+
return true;
|
|
134
|
+
};
|
|
135
|
+
const applyFocus = (path) => {
|
|
136
|
+
return (state, dispatch, view) => {
|
|
137
|
+
if (path === null)
|
|
138
|
+
return clearFocus(state, dispatch, view);
|
|
139
|
+
const ref = sectionTree.resolveByPath(state.doc, path);
|
|
140
|
+
if (!ref)
|
|
141
|
+
return false;
|
|
142
|
+
if (stateOf(state).anchorPos === ref.pos)
|
|
143
|
+
return false;
|
|
144
|
+
if (!dispatch)
|
|
145
|
+
return true;
|
|
146
|
+
dispatch(requestFocus(state, key, {
|
|
147
|
+
anchorPos: ref.pos,
|
|
148
|
+
restorePos: state.selection.from,
|
|
149
|
+
restoreScrollTop: scrollTopOf(view)
|
|
150
|
+
}));
|
|
151
|
+
return true;
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
plugin,
|
|
156
|
+
focusSection,
|
|
157
|
+
clearFocus,
|
|
158
|
+
applyFocus,
|
|
159
|
+
getState: stateOf,
|
|
160
|
+
getFocusedPath: (state) => pathOfAnchor(state.doc, stateOf(state).anchorPos)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
// --- 状態の移り変わり ---
|
|
164
|
+
function requestFocus(state, key, request) {
|
|
165
|
+
// 表示だけを変えるため、取り消しの対象にしない
|
|
166
|
+
const tr = state.tr.setMeta(key, request);
|
|
167
|
+
tr.setMeta('addToHistory', false);
|
|
168
|
+
return tr;
|
|
169
|
+
}
|
|
170
|
+
function nextState(key, tr, value, doc) {
|
|
171
|
+
const meta = tr.getMeta(key);
|
|
172
|
+
if (meta === RESTORE_DONE)
|
|
173
|
+
return { ...value, pendingRestore: null };
|
|
174
|
+
// メタ情報が無いことと、解除の指示(`null`)とを取り違えない
|
|
175
|
+
if (meta !== undefined)
|
|
176
|
+
return fromRequest(meta, doc);
|
|
177
|
+
if (!tr.docChanged)
|
|
178
|
+
return value;
|
|
179
|
+
if (value.anchorPos === null)
|
|
180
|
+
return value;
|
|
181
|
+
const mapped = tr.mapping.mapResult(value.anchorPos, 1);
|
|
182
|
+
// 対象が消えたと分かったときだけ、維持を諦める
|
|
183
|
+
if (mapped.deleted) {
|
|
184
|
+
return {
|
|
185
|
+
...IDLE,
|
|
186
|
+
pendingRestore: value.restorePos === null ? null : tr.mapping.map(value.restorePos)
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const restorePos = value.restorePos === null ? null : tr.mapping.map(value.restorePos);
|
|
190
|
+
// 連なりの導出は写した先が変わったときだけ行う(文字入力のたびに行わない)
|
|
191
|
+
if (mapped.pos === value.anchorPos && originOf(tr) !== 'host') {
|
|
192
|
+
return { ...value, restorePos, decorations: value.decorations.map(tr.mapping, doc) };
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
anchorPos: mapped.pos,
|
|
196
|
+
breadcrumb: breadcrumbOf(doc, mapped.pos),
|
|
197
|
+
restorePos,
|
|
198
|
+
restoreScrollTop: value.restoreScrollTop,
|
|
199
|
+
decorations: outOfFocusDecorations(doc, mapped.pos),
|
|
200
|
+
pendingRestore: null
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function fromRequest(request, doc) {
|
|
204
|
+
if (request === null)
|
|
205
|
+
return IDLE;
|
|
206
|
+
return {
|
|
207
|
+
anchorPos: request.anchorPos,
|
|
208
|
+
breadcrumb: breadcrumbOf(doc, request.anchorPos),
|
|
209
|
+
restorePos: request.restorePos,
|
|
210
|
+
restoreScrollTop: request.restoreScrollTop,
|
|
211
|
+
decorations: outOfFocusDecorations(doc, request.anchorPos),
|
|
212
|
+
pendingRestore: null
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function focusRange(doc, anchorPos) {
|
|
216
|
+
if (anchorPos === null)
|
|
217
|
+
return null;
|
|
218
|
+
const section = doc.nodeAt(anchorPos);
|
|
219
|
+
if (!section || section.type !== SECTION)
|
|
220
|
+
return null;
|
|
221
|
+
return { from: anchorPos + 1, to: anchorPos + section.nodeSize - 1 };
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 各ステップが書き換える**元の範囲**が、フォーカス範囲に収まるかを見る。
|
|
225
|
+
* この 1 か所で削除・貼り付け・入力のすべてを塞げる。
|
|
226
|
+
*/
|
|
227
|
+
function staysInside(tr, range) {
|
|
228
|
+
let { from, to } = range;
|
|
229
|
+
for (const step of tr.steps) {
|
|
230
|
+
const map = step.getMap();
|
|
231
|
+
let inside = true;
|
|
232
|
+
map.forEach((oldStart, oldEnd) => {
|
|
233
|
+
if (oldStart < from || oldEnd > to)
|
|
234
|
+
inside = false;
|
|
235
|
+
});
|
|
236
|
+
if (!inside)
|
|
237
|
+
return false;
|
|
238
|
+
// 次のステップは 1 つ前の結果の位置系で書かれている
|
|
239
|
+
from = map.map(from, -1);
|
|
240
|
+
to = map.map(to, 1);
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* 範囲の外へ出た位置を、内側の直近の置ける位置へ引き戻す。
|
|
246
|
+
*
|
|
247
|
+
* **探す向きの指定が要る。** 既定のまま前方へ探すと、範囲の終わりで丸めたつもりが
|
|
248
|
+
* そのまま範囲外へ抜ける。始まりでは前方、終わりでは後方と、常に内側へ向けて探す。
|
|
249
|
+
*/
|
|
250
|
+
function clampInside(doc, pos, range) {
|
|
251
|
+
if (pos < range.from)
|
|
252
|
+
return TextSelection.near(doc.resolve(range.from), 1).from;
|
|
253
|
+
if (pos > range.to)
|
|
254
|
+
return TextSelection.near(doc.resolve(range.to), -1).from;
|
|
255
|
+
return pos;
|
|
256
|
+
}
|
|
257
|
+
// --- 表示範囲の抑制 ---
|
|
258
|
+
/**
|
|
259
|
+
* 範囲外のブロックへ印を付ける。**文書からは何も取り除かない**。
|
|
260
|
+
*
|
|
261
|
+
* 対象までの道筋を降りながら、各段で道筋にないきょうだいへ印を付ける。
|
|
262
|
+
* 費用は道筋の深さときょうだいの数に比例し、文書全体を歩かない。
|
|
263
|
+
*/
|
|
264
|
+
function outOfFocusDecorations(doc, anchorPos) {
|
|
265
|
+
const $anchor = doc.resolve(anchorPos);
|
|
266
|
+
const decorations = [];
|
|
267
|
+
/**
|
|
268
|
+
* フォーカスしたセクションを**最上位として見せる**ための段数。
|
|
269
|
+
*
|
|
270
|
+
* 隠すだけでは足りない。3 階層目だけを表示しても見出しは 3 階層目の大きさのままで、
|
|
271
|
+
* 「いちばん外側を見ている」ようには読めない。繰り上げる段数を印として渡し、
|
|
272
|
+
* 見た目(見出しの大きさ)は `editor.css` が決める。文書は変えない。
|
|
273
|
+
*
|
|
274
|
+
* **繰り上げが 0 段でも印を付ける。** 印は「ここがフォーカスの根である」ことも兼ねており、
|
|
275
|
+
* 画面の最上端に立つセクションを CSS から指す唯一の手がかりになる(→ `editor.css` の
|
|
276
|
+
* 「本文の先頭に立つブロックは上の余白を持たない」)。0 段のときだけ印が無いと、
|
|
277
|
+
* 最上位のセクションを見ているあいだだけ本文の頭の位置が変わる。
|
|
278
|
+
*/
|
|
279
|
+
const section = doc.nodeAt(anchorPos);
|
|
280
|
+
const lift = $anchor.depth;
|
|
281
|
+
if (section) {
|
|
282
|
+
decorations.push(Decoration.node(anchorPos, anchorPos + section.nodeSize, {
|
|
283
|
+
'data-focus-lift': String(lift)
|
|
284
|
+
}));
|
|
285
|
+
}
|
|
286
|
+
for (let depth = 0; depth <= $anchor.depth; depth += 1) {
|
|
287
|
+
const parent = $anchor.node(depth);
|
|
288
|
+
const keep = $anchor.index(depth);
|
|
289
|
+
const start = $anchor.start(depth);
|
|
290
|
+
parent.forEach((child, offset, index) => {
|
|
291
|
+
if (index === keep)
|
|
292
|
+
return;
|
|
293
|
+
decorations.push(Decoration.node(start + offset, start + offset + child.nodeSize, {
|
|
294
|
+
class: OUT_OF_FOCUS_CLASS
|
|
295
|
+
}));
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return DecorationSet.create(doc, decorations);
|
|
299
|
+
}
|
|
300
|
+
// --- 位置と連なり ---
|
|
301
|
+
function enclosingSectionPos(doc, pos) {
|
|
302
|
+
const $pos = doc.resolve(Math.max(0, Math.min(pos, doc.content.size)));
|
|
303
|
+
for (let depth = $pos.depth; depth >= 1; depth -= 1) {
|
|
304
|
+
if ($pos.node(depth).type === SECTION)
|
|
305
|
+
return $pos.before(depth);
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
function pathOfAnchor(doc, anchorPos) {
|
|
310
|
+
if (anchorPos === null)
|
|
311
|
+
return null;
|
|
312
|
+
return sectionTree.resolveSection(doc, anchorPos + 1)?.path ?? null;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 上位見出しの連なり。**末尾にフォーカスしている当人を含める。**
|
|
316
|
+
* 含めないと、最上位のセクションをフォーカスしたとき連なりが空になり、
|
|
317
|
+
* 「今どこを見ているか」を示すという役目を果たさない。
|
|
318
|
+
*/
|
|
319
|
+
function breadcrumbOf(doc, anchorPos) {
|
|
320
|
+
const inside = anchorPos + 1;
|
|
321
|
+
const chain = [...sectionTree.resolveAncestors(doc, inside)];
|
|
322
|
+
const self = sectionTree.resolveSection(doc, inside);
|
|
323
|
+
if (self)
|
|
324
|
+
chain.push(self);
|
|
325
|
+
return chain
|
|
326
|
+
.filter((ref) => ref.path !== null)
|
|
327
|
+
.map((ref) => ({ path: ref.path, title: ref.node.child(0).textContent }));
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* 解除時に戻すためのスクロール位置。
|
|
331
|
+
* 巻き取っている要素は利用者の版面で決まるため、実行のたびに探し直す。
|
|
332
|
+
*/
|
|
333
|
+
function scrollTopOf(view) {
|
|
334
|
+
if (!view)
|
|
335
|
+
return null;
|
|
336
|
+
const scroller = scrollParentOf(view.dom);
|
|
337
|
+
return scroller ? scroller.scrollTop : null;
|
|
338
|
+
}
|
|
339
|
+
export function scrollParentOf(element) {
|
|
340
|
+
if (typeof document === 'undefined')
|
|
341
|
+
return null;
|
|
342
|
+
let current = element.parentElement;
|
|
343
|
+
while (current) {
|
|
344
|
+
const overflow = getComputedStyle(current).overflowY;
|
|
345
|
+
if (overflow === 'auto' || overflow === 'scroll')
|
|
346
|
+
return current;
|
|
347
|
+
current = current.parentElement;
|
|
348
|
+
}
|
|
349
|
+
return document.scrollingElement;
|
|
350
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* 記号を押して折りたたみを切り替える。
|
|
4
|
+
*
|
|
5
|
+
* **状態を持たない。** 折りたたみは文書に乗っており、畳めるかどうかは描画された要素に
|
|
6
|
+
* 既に出ている。見出しごとの部品も作らない。
|
|
7
|
+
*
|
|
8
|
+
* `mousedown` を `handleDOMEvents` で受ける。この経路は編集可否の判定より前に呼ばれる
|
|
9
|
+
* ため、読み取り専用でも届く(表示分類のキーで採ったのと同じ経路)。
|
|
10
|
+
*/
|
|
11
|
+
export declare function foldClickPlugin(): Plugin;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { foldCommands } from '../commands/sectionFold';
|
|
3
|
+
/**
|
|
4
|
+
* 記号を出している対象。**「畳めるか」は描画された要素に既に出ている**ため、
|
|
5
|
+
* 選択子をそこにそろえる。記号を描く CSS と同じ根拠であり、
|
|
6
|
+
* 「見えるのに押せない記号」「見えないのに反応する領域」が生まれない。
|
|
7
|
+
*/
|
|
8
|
+
const FOLDABLE = '.markdown-editor__section[aria-expanded], .prosemirror-flat-list[aria-expanded]';
|
|
9
|
+
/**
|
|
10
|
+
* 記号を持つのは、対象の**1 行目**(見出し・項目の本文)である。
|
|
11
|
+
* コンテナの高さは配下すべてを含むため、記号を合わせる先にも触る先にもならない。
|
|
12
|
+
*/
|
|
13
|
+
const MARKER_OWNER = ':scope > .markdown-editor__heading, ' +
|
|
14
|
+
':scope > .list-content > :first-child:not(.prosemirror-flat-list)';
|
|
15
|
+
/**
|
|
16
|
+
* 記号を押して折りたたみを切り替える。
|
|
17
|
+
*
|
|
18
|
+
* **状態を持たない。** 折りたたみは文書に乗っており、畳めるかどうかは描画された要素に
|
|
19
|
+
* 既に出ている。見出しごとの部品も作らない。
|
|
20
|
+
*
|
|
21
|
+
* `mousedown` を `handleDOMEvents` で受ける。この経路は編集可否の判定より前に呼ばれる
|
|
22
|
+
* ため、読み取り専用でも届く(表示分類のキーで採ったのと同じ経路)。
|
|
23
|
+
*/
|
|
24
|
+
export function foldClickPlugin() {
|
|
25
|
+
return new Plugin({
|
|
26
|
+
props: {
|
|
27
|
+
handleDOMEvents: {
|
|
28
|
+
mousedown: (view, event) => handleMouseDown(view, event)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function handleMouseDown(view, event) {
|
|
34
|
+
if (event.button !== 0)
|
|
35
|
+
return false;
|
|
36
|
+
const target = event.target;
|
|
37
|
+
if (!(target instanceof Element))
|
|
38
|
+
return false;
|
|
39
|
+
const element = target.closest(FOLDABLE);
|
|
40
|
+
if (!(element instanceof HTMLElement) || !view.dom.contains(element))
|
|
41
|
+
return false;
|
|
42
|
+
const owner = element.querySelector(MARKER_OWNER);
|
|
43
|
+
if (!owner)
|
|
44
|
+
return false;
|
|
45
|
+
if (!isInLeadingMarker(owner, event.clientX) && !isInTrailingMark(owner, event))
|
|
46
|
+
return false;
|
|
47
|
+
// 押された当人を対象にする。カーソル位置は見ない
|
|
48
|
+
const pos = view.posAtDOM(element, 0);
|
|
49
|
+
if (!foldCommands.toggleFoldAt(pos)(view.state, view.dispatch.bind(view), view))
|
|
50
|
+
return false;
|
|
51
|
+
// 止めないとカーソルが記号の位置へ移る
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 押された横位置が、行頭の記号(−/+、点)の箱の中か。
|
|
57
|
+
*
|
|
58
|
+
* 記号は行の内容の**外**にあり、置き場所は CSS 変数で決まる。**寸法は実測する**
|
|
59
|
+
* (変数の値を実装側に持たない)。記号が出ていなければ幅が 0 になり、範囲は空になる。
|
|
60
|
+
*/
|
|
61
|
+
function isInLeadingMarker(element, clientX) {
|
|
62
|
+
const marker = getComputedStyle(element, '::before');
|
|
63
|
+
const width = parseFloat(marker.width);
|
|
64
|
+
if (!(width > 0))
|
|
65
|
+
return false;
|
|
66
|
+
// 記号の箱は要素の枠の左外にある。`right` が両者の隔たりにあたる
|
|
67
|
+
const gap = parseFloat(marker.right) || 0;
|
|
68
|
+
const rect = element.getBoundingClientRect();
|
|
69
|
+
const right = rect.left + element.clientLeft + element.clientWidth - gap;
|
|
70
|
+
return clientX >= right - width && clientX < right;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 押された位置が、畳んでいる行の末尾の `…` の中か。
|
|
74
|
+
*
|
|
75
|
+
* 畳んでいないあいだは描画されず、幅が 0 になって範囲も空になる。
|
|
76
|
+
*/
|
|
77
|
+
function isInTrailingMark(element, event) {
|
|
78
|
+
const mark = getComputedStyle(element, '::after');
|
|
79
|
+
const width = parseFloat(mark.width);
|
|
80
|
+
if (!(width > 0))
|
|
81
|
+
return false;
|
|
82
|
+
const box = trailingMarkBox(element, mark);
|
|
83
|
+
if (!box)
|
|
84
|
+
return false;
|
|
85
|
+
return (event.clientX >= box.left &&
|
|
86
|
+
event.clientX < box.left + width &&
|
|
87
|
+
event.clientY >= box.top &&
|
|
88
|
+
event.clientY < box.bottom);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 行末の `…` が描かれている箱。
|
|
92
|
+
*
|
|
93
|
+
* `…` は文字の並びで置き場所が決まるため、**最後の行の右端**を実測して起点にする
|
|
94
|
+
* (`…` 自体は文書に属さないので、範囲では選べない)。
|
|
95
|
+
*
|
|
96
|
+
* ただし**残りの幅に収まらなければ、`…` だけが次の行へ落ちる。** そのときは文字の
|
|
97
|
+
* 並びの延長ではなく、行の頭へ回り込む。落ちたかどうかは予想せず、
|
|
98
|
+
* 文字の最終行より下に `…` 1 行ぶんの高さが残っているかで見る(描画の結果から読む)。
|
|
99
|
+
*/
|
|
100
|
+
function trailingMarkBox(element, mark) {
|
|
101
|
+
const range = element.ownerDocument.createRange();
|
|
102
|
+
range.selectNodeContents(element);
|
|
103
|
+
const lines = range.getClientRects();
|
|
104
|
+
const line = lines[lines.length - 1];
|
|
105
|
+
if (!line)
|
|
106
|
+
return null;
|
|
107
|
+
const gap = parseFloat(mark.marginLeft) || 0;
|
|
108
|
+
const height = parseFloat(mark.height);
|
|
109
|
+
const own = getComputedStyle(element);
|
|
110
|
+
const rect = element.getBoundingClientRect();
|
|
111
|
+
const contentBottom = rect.bottom - parseFloat(own.borderBottomWidth) - parseFloat(own.paddingBottom);
|
|
112
|
+
if (!(height > 0) || contentBottom - line.bottom < height) {
|
|
113
|
+
return { left: line.right + gap, top: line.top, bottom: line.bottom };
|
|
114
|
+
}
|
|
115
|
+
const contentLeft = rect.left + parseFloat(own.borderLeftWidth) + parseFloat(own.paddingLeft);
|
|
116
|
+
return { left: contentLeft + gap, top: contentBottom - height, bottom: contentBottom };
|
|
117
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* カーソルが折りたたまれた範囲の内側に取り残されないようにする。
|
|
4
|
+
*
|
|
5
|
+
* **状態を持たない。** 折りたたみは文書に乗っているため、見張るだけでよい。
|
|
6
|
+
*
|
|
7
|
+
* 判定は**変更後のカーソル位置だけ**を見る。カーソルを動かすのは基盤のコマンド
|
|
8
|
+
* (サブツリー移動・上下移動・見出しジャンプ)であり、それらは折りたたみを知らない。
|
|
9
|
+
* 原因を問わず結果だけを見れば、基盤のコマンドに手を入れずに済み、差し込み箇所も
|
|
10
|
+
* 1 つで足りる。
|
|
11
|
+
*
|
|
12
|
+
* 裏返せば、**折りたたまれたサブツリーを操作の対象にしただけでは展開しない**
|
|
13
|
+
*。移動・複製・削除ではカーソルが隠れる位置へ入らないためである。
|
|
14
|
+
*/
|
|
15
|
+
export declare function foldGuardPlugin(): Plugin;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { hidingAncestors } from '../commands/sectionFold';
|
|
3
|
+
import { markOrigin } from '../transactionOrigin';
|
|
4
|
+
/**
|
|
5
|
+
* カーソルが折りたたまれた範囲の内側に取り残されないようにする。
|
|
6
|
+
*
|
|
7
|
+
* **状態を持たない。** 折りたたみは文書に乗っているため、見張るだけでよい。
|
|
8
|
+
*
|
|
9
|
+
* 判定は**変更後のカーソル位置だけ**を見る。カーソルを動かすのは基盤のコマンド
|
|
10
|
+
* (サブツリー移動・上下移動・見出しジャンプ)であり、それらは折りたたみを知らない。
|
|
11
|
+
* 原因を問わず結果だけを見れば、基盤のコマンドに手を入れずに済み、差し込み箇所も
|
|
12
|
+
* 1 つで足りる。
|
|
13
|
+
*
|
|
14
|
+
* 裏返せば、**折りたたまれたサブツリーを操作の対象にしただけでは展開しない**
|
|
15
|
+
*。移動・複製・削除ではカーソルが隠れる位置へ入らないためである。
|
|
16
|
+
*/
|
|
17
|
+
export function foldGuardPlugin() {
|
|
18
|
+
return new Plugin({
|
|
19
|
+
appendTransaction(_transactions, _oldState, newState) {
|
|
20
|
+
const hiding = hidingAncestors(newState.doc, newState.selection.from);
|
|
21
|
+
// すでに見えているなら打ち切る。自身が積んだ Transaction でも再び呼ばれる
|
|
22
|
+
if (hiding.length === 0)
|
|
23
|
+
return null;
|
|
24
|
+
const tr = newState.tr;
|
|
25
|
+
for (const pos of hiding)
|
|
26
|
+
tr.setNodeAttribute(pos, 'collapsed', false);
|
|
27
|
+
tr.setMeta('addToHistory', false);
|
|
28
|
+
return markOrigin(tr, 'fold');
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* 全選択の置き換えを「文書まるごとの差し替え」として成立させる見張り。
|
|
4
|
+
*
|
|
5
|
+
* **状態を持たない。** 見るのは「直前の選択が文書全体だったか」という選択の形と、その
|
|
6
|
+
* 書き換えでフロントマターに何が入ったかだけで、意図の推測も控えの状態も持たない。
|
|
7
|
+
* フロントマターは文書の最初のノードなので、テキストの選択がその手前から始まることは
|
|
8
|
+
* 構造上ありえず、**外からフロントマターへ流れ込む選択は文書全体の選択ただ 1 つ**である。
|
|
9
|
+
* 塞ぐ門はここにしかない。
|
|
10
|
+
*
|
|
11
|
+
* 見張りが要るのは、フロントマターが**文書の最初の「文字を置ける場所」**だからである。
|
|
12
|
+
* 全選択への置き換えで、編集基盤は文字を最初に置ける位置——フロントマターの中——へ入れる
|
|
13
|
+
* 。スキーマの `isolating` では防げない(充填処理は挿入先の判定に `isolating` を使わない)。
|
|
14
|
+
*
|
|
15
|
+
* **もう 1 つ、選択が境界をまたぐ道がある。** 中から外へ伸びた選択を消すと、外に残った
|
|
16
|
+
* 文字がフロントマターへ流れ込む(`Shift+↓` で行ごと選び、消したときに起きる)。ここでも
|
|
17
|
+
* `isolating` は効かない——`deleteRange` は見るが、そこで畳めずに素の削除へ落ちた先の
|
|
18
|
+
* 充填処理が見ていない。**またぐ選択そのものを作らせない**ことで、削除・打鍵・貼り付け・
|
|
19
|
+
* 切り取り・引きずりのどこから来ても同じ 1 か所で塞ぐ。
|
|
20
|
+
*/
|
|
21
|
+
export declare function frontMatterGuardPlugin(): Plugin;
|