@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,173 @@
|
|
|
1
|
+
import { outlineSchema } from '../document/schema';
|
|
2
|
+
import { sectionTree } from '../document/sectionTree';
|
|
3
|
+
import { isViewState } from './viewState.types';
|
|
4
|
+
const SECTION = outlineSchema.nodes.section;
|
|
5
|
+
/** 形式の版。将来の変更で増やし、古い側は「未対応」として安全に無視できるようにする */
|
|
6
|
+
export const VIEW_STATE_VERSION = 1;
|
|
7
|
+
/** 何も畳んでおらず、フォーカスもしていない状態 */
|
|
8
|
+
export const EMPTY_VIEW_STATE = {
|
|
9
|
+
version: VIEW_STATE_VERSION,
|
|
10
|
+
folded: [],
|
|
11
|
+
focused: null
|
|
12
|
+
};
|
|
13
|
+
export const viewStateCodec = {
|
|
14
|
+
encode,
|
|
15
|
+
decode
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 表示状態を外部へ出せる値にする。
|
|
19
|
+
*
|
|
20
|
+
* **表示状態の復元で唯一の走査**なので、費用を抑える必要がある。
|
|
21
|
+
*
|
|
22
|
+
* - 歩くのは `section` だけ。ブロックの中へは降りない(費用は見出しの数に比例する)
|
|
23
|
+
* - ハッシュを計算するのは**折りたたまれているセクションとフォーカス中のセクションだけ**。
|
|
24
|
+
* `folded` に載るのは畳まれたものだけなので、全見出しのハッシュを作る理由がない。
|
|
25
|
+
* 連なりの材料(祖先のセクション)は歩きの途中で持ち回り、必要になった時点で確定させる
|
|
26
|
+
*/
|
|
27
|
+
function encode(state, focusedPath) {
|
|
28
|
+
const folded = [];
|
|
29
|
+
let focused = null;
|
|
30
|
+
const focusedKey = focusedPath === null ? null : keyOf(focusedPath);
|
|
31
|
+
walkSections(state.doc, (node, path, chain) => {
|
|
32
|
+
if (node.attrs.collapsed)
|
|
33
|
+
folded.push({ path: [...path], hash: hashOfChain(chain) });
|
|
34
|
+
if (focusedKey !== null && focused === null && keyOf(path) === focusedKey) {
|
|
35
|
+
focused = { path: [...path], hash: hashOfChain(chain) };
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return { version: VIEW_STATE_VERSION, folded, focused };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 保存された表示状態を、いまの文書に当てられる位置へ解決する。
|
|
42
|
+
*
|
|
43
|
+
* **照合であって探索ではない。** 保存されたパスが指す位置を見に行き、突合キーが一致すれば
|
|
44
|
+
* 適用し、一致しなければ破棄する。テキストが一致する別の場所を探しに行かない
|
|
45
|
+
* (探すと、同じ形式を繰り返す文書で別の項目を畳んでしまう)。
|
|
46
|
+
*/
|
|
47
|
+
function decode(value, doc) {
|
|
48
|
+
const version = versionOf(value);
|
|
49
|
+
if (version !== null && version !== VIEW_STATE_VERSION) {
|
|
50
|
+
return { ok: false, error: { kind: 'unsupported-version', version } };
|
|
51
|
+
}
|
|
52
|
+
if (!isViewState(value)) {
|
|
53
|
+
return { ok: false, error: { kind: 'invalid-format', detail: '表示状態の形が期待と違う' } };
|
|
54
|
+
}
|
|
55
|
+
const foldedPaths = [];
|
|
56
|
+
let discardedCount = 0;
|
|
57
|
+
for (const ref of value.folded) {
|
|
58
|
+
if (matches(doc, ref))
|
|
59
|
+
foldedPaths.push(ref.path);
|
|
60
|
+
else
|
|
61
|
+
discardedCount += 1;
|
|
62
|
+
}
|
|
63
|
+
let focusedPath = null;
|
|
64
|
+
if (value.focused) {
|
|
65
|
+
if (matches(doc, value.focused))
|
|
66
|
+
focusedPath = value.focused.path;
|
|
67
|
+
else
|
|
68
|
+
discardedCount += 1;
|
|
69
|
+
}
|
|
70
|
+
return { ok: true, value: { foldedPaths, focusedPath, discardedCount } };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 2 つの表示状態が同じ内容かを見る。
|
|
74
|
+
*
|
|
75
|
+
* **参照ではなく内容で比べる。** 外から設定された値と現在の状態を突き合わせるとき、
|
|
76
|
+
* 参照で比べると毎回「違う」と判定され、復元処理が止まらなくなる。
|
|
77
|
+
*/
|
|
78
|
+
export function isSameViewState(a, b) {
|
|
79
|
+
if (a === b)
|
|
80
|
+
return true;
|
|
81
|
+
if (!a || !b)
|
|
82
|
+
return false;
|
|
83
|
+
if (a.version !== b.version)
|
|
84
|
+
return false;
|
|
85
|
+
if (a.folded.length !== b.folded.length)
|
|
86
|
+
return false;
|
|
87
|
+
if (!sameRef(a.focused, b.focused))
|
|
88
|
+
return false;
|
|
89
|
+
return a.folded.every((ref, index) => sameRef(ref, b.folded[index]));
|
|
90
|
+
}
|
|
91
|
+
function sameRef(a, b) {
|
|
92
|
+
if (a === null || b === null)
|
|
93
|
+
return a === b;
|
|
94
|
+
if (a.hash !== b.hash)
|
|
95
|
+
return false;
|
|
96
|
+
if (a.path.length !== b.path.length)
|
|
97
|
+
return false;
|
|
98
|
+
return a.path.every((entry, index) => entry === b.path[index]);
|
|
99
|
+
}
|
|
100
|
+
/** 保存された位置に、保存されたときと同じ連なりのセクションがあるか */
|
|
101
|
+
function matches(doc, ref) {
|
|
102
|
+
const target = sectionTree.resolveByPath(doc, ref.path);
|
|
103
|
+
if (!target)
|
|
104
|
+
return false;
|
|
105
|
+
const inside = target.pos + 1;
|
|
106
|
+
const chain = [...sectionTree.resolveAncestors(doc, inside).map((ancestor) => ancestor.node)];
|
|
107
|
+
chain.push(target.node);
|
|
108
|
+
return hashOfChain(chain) === ref.hash;
|
|
109
|
+
}
|
|
110
|
+
/** 版だけを先に見る。未対応の版を「形が不正」と取り違えると、利用者が原因を掴めない */
|
|
111
|
+
function versionOf(value) {
|
|
112
|
+
if (typeof value !== 'object' || value === null)
|
|
113
|
+
return null;
|
|
114
|
+
const version = value.version;
|
|
115
|
+
return typeof version === 'number' && Number.isInteger(version) ? version : null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* セクションだけを辿る。`chain` は根から当人までのセクションで、ハッシュの材料になる。
|
|
119
|
+
* 見出しを持たないセクションは番地を持たないため、辿る対象にも数にも入らない。
|
|
120
|
+
*/
|
|
121
|
+
function walkSections(doc, visit) {
|
|
122
|
+
const path = [];
|
|
123
|
+
const chain = [];
|
|
124
|
+
const walk = (parent) => {
|
|
125
|
+
let index = 0;
|
|
126
|
+
parent.forEach((child) => {
|
|
127
|
+
if (child.type !== SECTION)
|
|
128
|
+
return;
|
|
129
|
+
path.push(index);
|
|
130
|
+
chain.push(child);
|
|
131
|
+
visit(child, path, chain);
|
|
132
|
+
walk(child);
|
|
133
|
+
chain.pop();
|
|
134
|
+
path.pop();
|
|
135
|
+
index += 1;
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
walk(doc);
|
|
139
|
+
}
|
|
140
|
+
function keyOf(path) {
|
|
141
|
+
return path.join(',');
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 突合キーのハッシュ。
|
|
145
|
+
*
|
|
146
|
+
* 対象をその見出し 1 つではなく**根からの見出しの連なり**にする。同じ形式を繰り返す文書
|
|
147
|
+
* (日付ごとの「概要」「詳細」など)では、テキストが一致しても別の項目だからである。
|
|
148
|
+
*
|
|
149
|
+
* **元の文字列を復元できない。** したがって本文を外へ出さないという約束は保たれる。
|
|
150
|
+
* 衝突しても失われるのは折りたたみの復元だけで、文書は壊れない。
|
|
151
|
+
*/
|
|
152
|
+
function hashOfChain(chain) {
|
|
153
|
+
let hash = 0x811c9dc5;
|
|
154
|
+
for (const section of chain) {
|
|
155
|
+
hash = mix(hash, section.child(0).textContent);
|
|
156
|
+
// 区切りを混ぜる。混ぜないと「AB / C」と「A / BC」が同じ値になる
|
|
157
|
+
hash = step(hash, 0x1f);
|
|
158
|
+
}
|
|
159
|
+
return (hash >>> 0).toString(36);
|
|
160
|
+
}
|
|
161
|
+
function mix(hash, text) {
|
|
162
|
+
let value = hash;
|
|
163
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
164
|
+
// 1 文字を 2 バイトに分けて混ぜる。下位だけだと日本語で取りこぼす
|
|
165
|
+
const code = text.charCodeAt(i);
|
|
166
|
+
value = step(value, code & 0xff);
|
|
167
|
+
value = step(value, code >>> 8);
|
|
168
|
+
}
|
|
169
|
+
return value;
|
|
170
|
+
}
|
|
171
|
+
function step(hash, byte) {
|
|
172
|
+
return Math.imul(hash ^ byte, 0x01000193);
|
|
173
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InputRule } from 'prosemirror-inputrules';
|
|
2
|
+
import { EditorState, type Transaction } from 'prosemirror-state';
|
|
3
|
+
/**
|
|
4
|
+
* 記法を打つと書式になる(オートフォーマット)。
|
|
5
|
+
*
|
|
6
|
+
* コードブロックの中では働かない。入力ルールの仕組みが `code` なブロックを除外する。
|
|
7
|
+
*/
|
|
8
|
+
export declare const autoformatRules: InputRule[];
|
|
9
|
+
export type InputRuleHandler = (state: EditorState, match: RegExpMatchArray, start: number, end: number) => Transaction | null;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { InputRule, textblockTypeInputRule, wrappingInputRule } from 'prosemirror-inputrules';
|
|
2
|
+
import { wrappingListInputRule } from 'prosemirror-flat-list';
|
|
3
|
+
import { EditorState, TextSelection } from 'prosemirror-state';
|
|
4
|
+
import { outlineSchema } from '../models/document/schema';
|
|
5
|
+
import { setBlockKind } from './commands/block';
|
|
6
|
+
/**
|
|
7
|
+
* 記法を打つと書式になる(オートフォーマット)。
|
|
8
|
+
*
|
|
9
|
+
* コードブロックの中では働かない。入力ルールの仕組みが `code` なブロックを除外する。
|
|
10
|
+
*/
|
|
11
|
+
export const autoformatRules = [
|
|
12
|
+
// 行頭の `#` は個数を問わない。レベルは構造上の深さから決まるため
|
|
13
|
+
new InputRule(/^#{1,6}\s$/, (state, _match, start, end) => headingFromMarkup(state, start, end)),
|
|
14
|
+
// 種別はマークダウンで表せるものだけを作る。ライブラリが用意する一式には
|
|
15
|
+
// 折りたたみ用の種別が含まれており、そのままでは書き出せない項目ができてしまう
|
|
16
|
+
wrappingListInputRule(/^\s?[*-]\s$/, { kind: 'bullet' }),
|
|
17
|
+
wrappingListInputRule(/^\s?(\d+)\.\s$/, ({ match }) => {
|
|
18
|
+
const order = Number(match[1]);
|
|
19
|
+
return { kind: 'ordered', order: Number.isFinite(order) && order >= 2 ? order : null };
|
|
20
|
+
}),
|
|
21
|
+
wrappingListInputRule(/^\s?\[([\sXx]?)\]\s$/, ({ match }) => ({
|
|
22
|
+
kind: 'task',
|
|
23
|
+
checked: /[Xx]/.test(match[1])
|
|
24
|
+
})),
|
|
25
|
+
wrappingInputRule(/^\s?>\s$/, outlineSchema.nodes.blockquote),
|
|
26
|
+
// 汎用の ``` ルールも `mermaid` に一致するため、専用ルールを前に置く(先に一致した規則が勝つ)
|
|
27
|
+
textblockTypeInputRule(/^```mermaid[\s\n]$/, outlineSchema.nodes.mermaid_block),
|
|
28
|
+
textblockTypeInputRule(/^```([a-zA-Z0-9_+#-]*)[\s\n]$/, outlineSchema.nodes.code_block, (match) => ({ language: match[1] ?? '' })),
|
|
29
|
+
new InputRule(/^(?:---|___|\*\*\*)$/, (state, _match, start, end) => state.tr.replaceWith(start, end, outlineSchema.nodes.horizontal_rule.create())),
|
|
30
|
+
markInputRule(/(\*\*([^*]+)\*\*)$/, outlineSchema.marks.strong),
|
|
31
|
+
markInputRule(/(?:^|[^*])(\*([^*]+)\*)$/, outlineSchema.marks.em),
|
|
32
|
+
markInputRule(/(~~([^~]+)~~)$/, outlineSchema.marks.strikethrough),
|
|
33
|
+
markInputRule(/(`([^`]+)`)$/, outlineSchema.marks.code)
|
|
34
|
+
];
|
|
35
|
+
/** 記法の文字を消してから見出しへ変換し、1 つの取り消し単位にまとめる */
|
|
36
|
+
function headingFromMarkup(state, start, end) {
|
|
37
|
+
const stripped = state.tr.delete(start, end);
|
|
38
|
+
// プラグインを持たない状態で変換を組み立てる。`state.apply` を使うと、
|
|
39
|
+
// 表や箇条書きの整合を取るプラグインが文書をさらに変えることがあり、
|
|
40
|
+
// その文書に対して作った手順を元の文書へ当てられなくなる
|
|
41
|
+
const base = EditorState.create({
|
|
42
|
+
schema: outlineSchema,
|
|
43
|
+
doc: stripped.doc,
|
|
44
|
+
selection: stripped.selection
|
|
45
|
+
});
|
|
46
|
+
let converted;
|
|
47
|
+
setBlockKind('heading')(base, (tr) => {
|
|
48
|
+
converted = tr;
|
|
49
|
+
});
|
|
50
|
+
if (!converted)
|
|
51
|
+
return null;
|
|
52
|
+
const merged = state.tr.delete(start, end);
|
|
53
|
+
for (const step of converted.steps)
|
|
54
|
+
merged.step(step);
|
|
55
|
+
// 位置は同じだが、選択は今の文書のものへ作り直す必要がある
|
|
56
|
+
merged.setSelection(TextSelection.near(merged.doc.resolve(converted.selection.from)));
|
|
57
|
+
return merged;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 閉じ記号の入力で、その範囲に文字装飾を適用する。
|
|
61
|
+
*
|
|
62
|
+
* パターンは 2 つの括弧を持つ。1 つ目が記号を含む全体、2 つ目が装飾する文字である。
|
|
63
|
+
* 記号の直前の 1 文字を見分けるためにパターンへ含めることがあるため、
|
|
64
|
+
* 「全体」を独立して取れないと、その 1 文字まで消してしまう。
|
|
65
|
+
*/
|
|
66
|
+
function markInputRule(pattern, mark) {
|
|
67
|
+
return new InputRule(pattern, (state, match, start, end) => {
|
|
68
|
+
const markup = match[1] ?? '';
|
|
69
|
+
const text = match[2] ?? '';
|
|
70
|
+
if (!markup || !text)
|
|
71
|
+
return null;
|
|
72
|
+
const markupStart = start + match[0].lastIndexOf(markup);
|
|
73
|
+
const textStart = markupStart + markup.indexOf(text);
|
|
74
|
+
const textEnd = textStart + text.length;
|
|
75
|
+
const tr = state.tr;
|
|
76
|
+
// 後ろから消す(前を消すと後ろの位置がずれる)
|
|
77
|
+
if (textEnd < end)
|
|
78
|
+
tr.delete(textEnd, end);
|
|
79
|
+
if (textStart > markupStart)
|
|
80
|
+
tr.delete(markupStart, textStart);
|
|
81
|
+
tr.addMark(markupStart, markupStart + text.length, mark.create());
|
|
82
|
+
// 続けて入力する文字に装飾が伸びないようにする
|
|
83
|
+
tr.removeStoredMark(mark);
|
|
84
|
+
return tr;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { EditorState, Transaction } from 'prosemirror-state';
|
|
2
|
+
import type { BlockKind } from '../../models/document/document.types';
|
|
3
|
+
import type { EditorCommand } from '../operations.types';
|
|
4
|
+
/**
|
|
5
|
+
* ブロックの種別を変える。
|
|
6
|
+
*
|
|
7
|
+
* 階層操作はツリーの中で完結するため、
|
|
8
|
+
* 箇条書きのツリーと見出しのツリーを行き来する手段はここが担う。
|
|
9
|
+
*/
|
|
10
|
+
export declare function setBlockKind(kind: BlockKind): EditorCommand;
|
|
11
|
+
/**
|
|
12
|
+
* 見出しとリスト項目を行き来する。
|
|
13
|
+
*
|
|
14
|
+
* 見出し行の上ではリスト項目へ、それ以外の行では見出しへ変える。同じキーが
|
|
15
|
+
* そのまま戻す操作にもなるので、覚えるのは 1 つで済み、押し間違えても戻せる。
|
|
16
|
+
*
|
|
17
|
+
* **どちら向きも階層まるごとを対象にする。** 1 つだけ変えると、書き手が作っていない
|
|
18
|
+
* 親子関係ができる(→ `convertToHeadings` / `dissolveLevel`)。まるごと変えるので、
|
|
19
|
+
* カーソルが階層のどこにあっても結果は同じで、往復すれば必ず元へ戻る。
|
|
20
|
+
*
|
|
21
|
+
* 見出しをやめたときの行き先を段落ではなくリスト項目にするのは、配下を保つため。
|
|
22
|
+
* 段落へ戻すと中身が親を失い、木の形が崩れる。
|
|
23
|
+
*/
|
|
24
|
+
export declare function toggleHeading(state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { chainCommands, lift, setBlockType, wrapIn } from 'prosemirror-commands';
|
|
2
|
+
import { outlineSchema } from '../../models/document/schema';
|
|
3
|
+
import { setCursor } from '../transaction';
|
|
4
|
+
import { listCommands } from './list';
|
|
5
|
+
import { blockContentEnd, convertToHeadings, dissolveLevel, previousSiblingSection, sectionToListItem, targetSection } from './section';
|
|
6
|
+
const n = outlineSchema.nodes;
|
|
7
|
+
/**
|
|
8
|
+
* ブロックの種別を変える。
|
|
9
|
+
*
|
|
10
|
+
* 階層操作はツリーの中で完結するため、
|
|
11
|
+
* 箇条書きのツリーと見出しのツリーを行き来する手段はここが担う。
|
|
12
|
+
*/
|
|
13
|
+
export function setBlockKind(kind) {
|
|
14
|
+
const changeBlock = blockKindCommand(kind);
|
|
15
|
+
return (state, dispatch, view) => {
|
|
16
|
+
if (kind === 'heading')
|
|
17
|
+
return convertToHeadings(state, dispatch);
|
|
18
|
+
if (targetSection(state)) {
|
|
19
|
+
// ツリーをまたぐ変換。項目へ戻すときは階層まるごと(→ `dissolveLevel`)。
|
|
20
|
+
// 段落は木から降りる変換なので、押した見出しだけを対象にする
|
|
21
|
+
const listKind = LIST_KINDS[kind];
|
|
22
|
+
return listKind
|
|
23
|
+
? dissolveLevel(state, dispatch, listKind)
|
|
24
|
+
: dissolveSection(state, dispatch, kind);
|
|
25
|
+
}
|
|
26
|
+
return changeBlock(state, dispatch, view);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 見出しとリスト項目を行き来する。
|
|
31
|
+
*
|
|
32
|
+
* 見出し行の上ではリスト項目へ、それ以外の行では見出しへ変える。同じキーが
|
|
33
|
+
* そのまま戻す操作にもなるので、覚えるのは 1 つで済み、押し間違えても戻せる。
|
|
34
|
+
*
|
|
35
|
+
* **どちら向きも階層まるごとを対象にする。** 1 つだけ変えると、書き手が作っていない
|
|
36
|
+
* 親子関係ができる(→ `convertToHeadings` / `dissolveLevel`)。まるごと変えるので、
|
|
37
|
+
* カーソルが階層のどこにあっても結果は同じで、往復すれば必ず元へ戻る。
|
|
38
|
+
*
|
|
39
|
+
* 見出しをやめたときの行き先を段落ではなくリスト項目にするのは、配下を保つため。
|
|
40
|
+
* 段落へ戻すと中身が親を失い、木の形が崩れる。
|
|
41
|
+
*/
|
|
42
|
+
export function toggleHeading(state, dispatch) {
|
|
43
|
+
if (targetSection(state))
|
|
44
|
+
return dissolveLevel(state, dispatch);
|
|
45
|
+
return convertToHeadings(state, dispatch);
|
|
46
|
+
}
|
|
47
|
+
const LIST_KINDS = {
|
|
48
|
+
bullet_list: 'bullet',
|
|
49
|
+
ordered_list: 'ordered',
|
|
50
|
+
task_list: 'task'
|
|
51
|
+
};
|
|
52
|
+
/** 見出し以外のブロックどうしの種別変換。文字の内容とインライン書式は保たれる */
|
|
53
|
+
function blockKindCommand(kind) {
|
|
54
|
+
const listKind = LIST_KINDS[kind];
|
|
55
|
+
if (listKind)
|
|
56
|
+
return listCommands.toggleListKind(listKind);
|
|
57
|
+
if (kind === 'blockquote') {
|
|
58
|
+
// 引用の中で押したら解除。それ以外は包む(引用の中にいるときだけ解くので、
|
|
59
|
+
// 項目の中で押しても「箇条書きが解けるだけ」にはならない)
|
|
60
|
+
return chainCommands(unwrapBlockquote, outOfList(wrapIn(n.blockquote)));
|
|
61
|
+
}
|
|
62
|
+
if (kind === 'code_block') {
|
|
63
|
+
return outOfList(setBlockType(n.code_block));
|
|
64
|
+
}
|
|
65
|
+
// 段落へ戻す。箇条書き・引用のいずれからでも解ける
|
|
66
|
+
return chainCommands(listCommands.unwrapList, lift, setBlockType(n.paragraph));
|
|
67
|
+
}
|
|
68
|
+
/** 引用の中にいるときだけ解く。同じ操作で掛け外しできるようにするための片側 */
|
|
69
|
+
const unwrapBlockquote = (state, dispatch, view) => state.selection.$from.node(-1)?.type === n.blockquote ? lift(state, dispatch, view) : false;
|
|
70
|
+
/**
|
|
71
|
+
* 箇条書きの中では、項目を解いてから種別を変える。
|
|
72
|
+
*
|
|
73
|
+
* 項目の中で種別だけを変えると、その行は「項目でもありコードブロックでもある」姿になる
|
|
74
|
+
* (中黒の付いたコードブロック)。ブロック種別の変換は行そのものを別の種別にする操作
|
|
75
|
+
* なので、コンテナの項目は残さない。段落へ戻すときに `unwrapList` を通すのと同じ規則である。
|
|
76
|
+
*
|
|
77
|
+
* 解くのと変えるのは 2 手だが、取り消しの単位は 1 つにする。前の手の書き換えを
|
|
78
|
+
* 積んだ状態を次の手へ渡し、まとめて 1 つの書き換えとして送る。
|
|
79
|
+
*/
|
|
80
|
+
function outOfList(command) {
|
|
81
|
+
return (state, dispatch, view) => {
|
|
82
|
+
if (!listCommands.isInList(state))
|
|
83
|
+
return command(state, dispatch, view);
|
|
84
|
+
const tr = state.tr;
|
|
85
|
+
let unwrapped = state;
|
|
86
|
+
const collect = (next) => {
|
|
87
|
+
for (const step of next.steps)
|
|
88
|
+
tr.step(step);
|
|
89
|
+
unwrapped = unwrapped.apply(next);
|
|
90
|
+
};
|
|
91
|
+
if (!listCommands.unwrapList(state, collect, view))
|
|
92
|
+
return command(state, dispatch, view);
|
|
93
|
+
if (!command(unwrapped, collect, view))
|
|
94
|
+
return false;
|
|
95
|
+
if (!dispatch)
|
|
96
|
+
return true;
|
|
97
|
+
dispatch(tr.scrollIntoView());
|
|
98
|
+
return true;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 見出しを解いて中身へ戻す(箇条書き、または段落)。
|
|
103
|
+
* 箇条書きへ戻す場合、中身の最上位項目は 1 段深くなる。
|
|
104
|
+
*
|
|
105
|
+
* 置き場所は直前の兄弟セクションの中身の末尾(なければ元の位置)。
|
|
106
|
+
* ブロックはセクションより前にしか置けないため、この位置にしないと
|
|
107
|
+
* 前後関係がマークダウン上で入れ替わってしまう。
|
|
108
|
+
*/
|
|
109
|
+
function dissolveSection(state, dispatch, kind) {
|
|
110
|
+
const target = targetSection(state);
|
|
111
|
+
if (!target)
|
|
112
|
+
return false;
|
|
113
|
+
const listKind = LIST_KINDS[kind];
|
|
114
|
+
if (!listKind && kind !== 'paragraph')
|
|
115
|
+
return false;
|
|
116
|
+
if (!dispatch)
|
|
117
|
+
return true;
|
|
118
|
+
const replacement = listKind
|
|
119
|
+
? [sectionToListItem(target.node, listKind)]
|
|
120
|
+
: sectionToBlocks(target.node);
|
|
121
|
+
const size = replacement.reduce((total, node) => total + node.nodeSize, 0);
|
|
122
|
+
const previous = previousSiblingSection(state.doc, target.pos);
|
|
123
|
+
const insertAt = previous ? blockContentEnd(previous.node, previous.pos) : target.pos;
|
|
124
|
+
const tr = state.tr;
|
|
125
|
+
tr.insert(insertAt, replacement);
|
|
126
|
+
tr.delete(target.pos + size, target.pos + size + target.node.nodeSize);
|
|
127
|
+
setCursor(tr, insertAt + 2);
|
|
128
|
+
dispatch(tr.scrollIntoView());
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
/** セクションを段落と中身の並びへ戻す。子セクションは入れ子の項目になる */
|
|
132
|
+
function sectionToBlocks(section) {
|
|
133
|
+
const blocks = [n.paragraph.createChecked(null, section.child(0).content)];
|
|
134
|
+
for (let i = 1; i < section.childCount; i += 1) {
|
|
135
|
+
const child = section.child(i);
|
|
136
|
+
blocks.push(child.type === n.section ? sectionToListItem(child, 'bullet') : child);
|
|
137
|
+
}
|
|
138
|
+
return blocks;
|
|
139
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EditorState } from 'prosemirror-state';
|
|
2
|
+
import type { EditorCommand } from '../operations.types';
|
|
3
|
+
/** カーソルがコードブロックの文字の中にあるか。字下げの分岐と同じ判定を 1 つにする */
|
|
4
|
+
export declare function isInCodeBlock(state: EditorState): boolean;
|
|
5
|
+
/** コードブロックの中では階層操作ではなく字下げとして扱う */
|
|
6
|
+
export declare const indentInCodeBlock: EditorCommand;
|
|
7
|
+
export declare const outdentInCodeBlock: EditorCommand;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** カーソルがコードブロックの文字の中にあるか。字下げの分岐と同じ判定を 1 つにする */
|
|
2
|
+
export function isInCodeBlock(state) {
|
|
3
|
+
return state.selection.$from.parent.type.spec.code === true;
|
|
4
|
+
}
|
|
5
|
+
/** コードブロックの中では階層操作ではなく字下げとして扱う */
|
|
6
|
+
export const indentInCodeBlock = (state, dispatch) => {
|
|
7
|
+
if (!isInCodeBlock(state))
|
|
8
|
+
return false;
|
|
9
|
+
dispatch?.(state.tr.insertText(' ').scrollIntoView());
|
|
10
|
+
return true;
|
|
11
|
+
};
|
|
12
|
+
export const outdentInCodeBlock = (state, dispatch) => {
|
|
13
|
+
if (!isInCodeBlock(state))
|
|
14
|
+
return false;
|
|
15
|
+
const $from = state.selection.$from;
|
|
16
|
+
if (!dispatch)
|
|
17
|
+
return true;
|
|
18
|
+
const text = $from.parent.textContent;
|
|
19
|
+
const lineStart = text.lastIndexOf('\n', $from.parentOffset - 1) + 1;
|
|
20
|
+
const leading = /^ {1,2}/.exec(text.slice(lineStart));
|
|
21
|
+
if (!leading)
|
|
22
|
+
return true;
|
|
23
|
+
const from = $from.start() + lineStart;
|
|
24
|
+
dispatch(state.tr.delete(from, from + leading[0].length).scrollIntoView());
|
|
25
|
+
return true;
|
|
26
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { EditorState } from 'prosemirror-state';
|
|
2
|
+
import type { BlockKind } from '../../models/document/document.types';
|
|
3
|
+
import type { EditorCommand, ElementTarget, MoveDirection } from '../operations.types';
|
|
4
|
+
/**
|
|
5
|
+
* 対象を跨ぐ操作の唯一の横断ファイル。対象の決定(`targetElement`)と、
|
|
6
|
+
* 決まった対象への振り分けだけを持つ。2 つ目の横断ファイルを作らない。
|
|
7
|
+
* 対象別ファイルはこのファイルを import しない。
|
|
8
|
+
*/
|
|
9
|
+
export interface SectionCommands {
|
|
10
|
+
readonly sinkSection: EditorCommand;
|
|
11
|
+
readonly liftSection: EditorCommand;
|
|
12
|
+
readonly moveSection: (direction: MoveDirection) => EditorCommand;
|
|
13
|
+
/** いま操作の対象になる要素。**対象の決め方はここだけにある** */
|
|
14
|
+
readonly targetElement: (state: EditorState) => ElementTarget | null;
|
|
15
|
+
/** 対象を 1 段深くする */
|
|
16
|
+
readonly sinkElement: EditorCommand;
|
|
17
|
+
/** 対象を 1 段浅くする */
|
|
18
|
+
readonly liftElement: EditorCommand;
|
|
19
|
+
/** 対象を前後の兄弟と入れ替える */
|
|
20
|
+
readonly moveElement: (direction: MoveDirection) => EditorCommand;
|
|
21
|
+
/** カーソルがいる要素(項目・セクション・ブロック)を複製する */
|
|
22
|
+
readonly duplicateElement: EditorCommand;
|
|
23
|
+
/** カーソルがいる要素を削除する */
|
|
24
|
+
readonly deleteElement: EditorCommand;
|
|
25
|
+
/** 見出しへの変換ではレベルを引数に取らない(直前の見出しから導出する) */
|
|
26
|
+
readonly setBlockKind: (kind: BlockKind) => EditorCommand;
|
|
27
|
+
/** 見出しとリスト項目を、階層まるごと行き来する */
|
|
28
|
+
readonly toggleHeading: EditorCommand;
|
|
29
|
+
/** セクションの端にある項目を、隣のセクションへ送り出す */
|
|
30
|
+
readonly moveItemAcrossSection: (direction: MoveDirection) => EditorCommand;
|
|
31
|
+
}
|
|
32
|
+
export declare const sectionCommands: SectionCommands;
|
|
33
|
+
/**
|
|
34
|
+
* `Tab` は文脈によって意味が変わる唯一のキーであり、判定順序が仕様そのものになる。
|
|
35
|
+
*
|
|
36
|
+
* IME 変換中 → 退避状態 → 表のセル内 → コードブロックの中 → 対象への階層操作
|
|
37
|
+
* → 段落を項目にする → 何もしない
|
|
38
|
+
*
|
|
39
|
+
* **前半の 3 つは階層操作ではない。** `Tab` が別の意味を持つ場面であり、対象を決める
|
|
40
|
+
* 前の関心事として残す。対象が決まってからの分岐(セクションか項目か)は
|
|
41
|
+
* `targetElement` が持つ。
|
|
42
|
+
*/
|
|
43
|
+
export declare const sinkByContext: EditorCommand;
|
|
44
|
+
export declare const liftByContext: EditorCommand;
|
|
45
|
+
/**
|
|
46
|
+
* 上下移動は、対象の中で完結させる。
|
|
47
|
+
*
|
|
48
|
+
* 項目にいるなら項目だけを動かし、動かせなくても**より大きなセクションへ落ちない**。
|
|
49
|
+
* 落とすと、末尾の項目でもう一度押したときにセクションごと動いてしまい、
|
|
50
|
+
* 「移動先に兄弟がなければ何もしない」に反する。対象を 1 つに
|
|
51
|
+
* 決めてから動かすことで、この性質が対象の決め方から自動的に出る。
|
|
52
|
+
*
|
|
53
|
+
* 動かせなかったときも**キーは受け止める**。手放すとブラウザ既定の動き
|
|
54
|
+
* (macOS の `Cmd` と矢印は文書の先頭・末尾へ移動)が走り、動かないどころか
|
|
55
|
+
* カーソルが飛ぶ。「何も起きない」は、文書もカーソルも動かないことを指す。
|
|
56
|
+
*/
|
|
57
|
+
export declare function moveByContext(direction: MoveDirection): EditorCommand;
|
|
58
|
+
/**
|
|
59
|
+
* 表の中では必ずキーを受け止め、表の外では譲る。
|
|
60
|
+
*
|
|
61
|
+
* 実行できなかったときに `false` を返すと、同じキーを持つ**後段の割り当てへ処理が渡る**。
|
|
62
|
+
* 表の端で行を動かせなかったのにセクションごと動く、見出し行を消せなかったのに
|
|
63
|
+
* セクションごと消える、といったことが起きる。
|
|
64
|
+
*
|
|
65
|
+
* 表の外では譲るため、既存の意味は変わらない。
|
|
66
|
+
*/
|
|
67
|
+
export declare function inTable(command: EditorCommand): EditorCommand;
|