@14ch/svelte-md-outliner 0.0.19 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.ja.md +47 -24
  3. package/README.md +47 -24
  4. package/dist/assets/styles/editor.css +42 -2
  5. package/dist/components/ContextMenu.svelte +1 -1
  6. package/dist/components/EditorSurface.svelte +2 -2
  7. package/dist/components/MarkdownEditor.svelte +69 -17
  8. package/dist/components/MarkdownEditor.svelte.d.ts +16 -3
  9. package/dist/components/ShortcutHelpDialog.svelte +3 -3
  10. package/dist/index.d.ts +12 -4
  11. package/dist/index.js +4 -2
  12. package/dist/markdown.d.ts +2 -0
  13. package/dist/markdown.js +1 -0
  14. package/dist/messages/index.d.ts +2 -2
  15. package/dist/messages/index.js +2 -2
  16. package/dist/models/document/bodySelection.js +1 -1
  17. package/dist/models/document/schema.d.ts +10 -1
  18. package/dist/models/document/schema.js +13 -2
  19. package/dist/models/document/sectionTree.d.ts +1 -1
  20. package/dist/models/document/sectionTree.js +2 -2
  21. package/dist/models/markdown/normalize.d.ts +20 -0
  22. package/dist/models/markdown/normalize.js +25 -0
  23. package/dist/models/markdown/parser.js +8 -6
  24. package/dist/models/markdown/serializer.js +12 -11
  25. package/dist/models/viewState/viewStateCodec.d.ts +1 -1
  26. package/dist/models/viewState/viewStateCodec.js +3 -3
  27. package/dist/operations/autoformat.js +3 -3
  28. package/dist/operations/commands/block.js +4 -4
  29. package/dist/operations/commands/element.js +9 -9
  30. package/dist/operations/commands/fencedBlock.js +2 -2
  31. package/dist/operations/commands/frontMatter.js +5 -5
  32. package/dist/operations/commands/lineBreak.js +1 -1
  33. package/dist/operations/commands/link.js +1 -1
  34. package/dist/operations/commands/list.js +59 -6
  35. package/dist/operations/commands/section.d.ts +1 -1
  36. package/dist/operations/commands/section.js +5 -5
  37. package/dist/operations/commands/sectionFold.d.ts +1 -1
  38. package/dist/operations/commands/sectionFold.js +5 -5
  39. package/dist/operations/commands/sectionJump.js +2 -2
  40. package/dist/operations/commands/selectedBlock.js +3 -3
  41. package/dist/operations/commands/table.js +43 -2
  42. package/dist/operations/documentDiff.d.ts +12 -0
  43. package/dist/operations/documentDiff.js +191 -0
  44. package/dist/operations/keymap/assemble.js +5 -5
  45. package/dist/operations/keymap/coreActions.js +13 -13
  46. package/dist/operations/keymap/menuItems.js +2 -2
  47. package/dist/operations/keymap/shortcutKeys.js +1 -1
  48. package/dist/operations/keymap/viewActions.js +5 -5
  49. package/dist/operations/plugins/blockHandle.js +1 -1
  50. package/dist/operations/plugins/compositionState.d.ts +30 -0
  51. package/dist/operations/plugins/compositionState.js +65 -0
  52. package/dist/operations/plugins/contextMenu.js +2 -2
  53. package/dist/operations/plugins/focus.js +19 -8
  54. package/dist/operations/plugins/foldClick.js +1 -1
  55. package/dist/operations/plugins/foldGuard.js +2 -2
  56. package/dist/operations/plugins/frontMatterGuard.js +4 -4
  57. package/dist/operations/plugins/frontMatterView.js +3 -3
  58. package/dist/operations/plugins/headingLevel.js +1 -1
  59. package/dist/operations/plugins/imagePaste.js +2 -2
  60. package/dist/operations/plugins/imageView.js +1 -1
  61. package/dist/operations/plugins/linkPaste.js +2 -2
  62. package/dist/operations/plugins/listStructure.js +1 -1
  63. package/dist/operations/plugins/markdownClipboard.js +8 -8
  64. package/dist/operations/plugins/mermaidView.js +2 -2
  65. package/dist/operations/plugins/requiredSelectionEntry.js +1 -1
  66. package/dist/operations/plugins/sectionView.js +1 -1
  67. package/dist/operations/plugins/tableHandles.js +2 -2
  68. package/dist/operations/transactionOrigin.d.ts +9 -5
  69. package/dist/operations/transactionOrigin.js +2 -1
  70. package/dist/states/contextMenuController.svelte.js +5 -5
  71. package/dist/states/editorSession.js +69 -50
  72. package/dist/states/editorSession.types.d.ts +41 -3
  73. package/dist/states/outlineViewController.svelte.d.ts +1 -7
  74. package/dist/states/outlineViewController.svelte.js +7 -22
  75. package/package.json +1 -1
@@ -0,0 +1,191 @@
1
+ import { Fragment } from 'prosemirror-model';
2
+ /**
3
+ * 変わった箇所ごとに置き換えを積んだ Transaction。差分が無ければ `null`。
4
+ *
5
+ * 全体を 1 範囲で置き換えないのは、あいだの一致する箇所まで作り直してしまうためである。
6
+ * 作り直すと、そこにあったカーソル・取り消しの手順の写し先・折りたたみが失われる。
7
+ *
8
+ * 表示だけの属性(折りたたみ)は、比較の前に当てる文書へ写す。写さないと、中身が同じ
9
+ * セクションも「違うノード」として数えられ、範囲がその部分木へ広がる。
10
+ */
11
+ export function diffTransaction(state, next) {
12
+ const target = carryViewAttributes(state.doc, next);
13
+ const replacements = [];
14
+ collectFragment(state.doc.content, target.content, 0, replacements);
15
+ if (replacements.length === 0)
16
+ return null;
17
+ const transaction = state.tr;
18
+ // **後ろから積む。** 前を先に当てると後ろの位置がずれ、積んだ範囲を写し直すことになる
19
+ for (let index = replacements.length - 1; index >= 0; index -= 1) {
20
+ const { from, to, content } = replacements[index];
21
+ transaction.replaceWith(from, to, content);
22
+ }
23
+ return transaction;
24
+ }
25
+ /**
26
+ * 同じ段の子を添字で突き合わせる。
27
+ *
28
+ * 前後の一致を除いた中間の子の数が両側でそろっているときは、添字どおりに 1 対 1 で見る。
29
+ * これが「離れた 2 か所を、あいだの一致を含めずに積む」条件である。数が違うときは
30
+ * 子の差し込み・取り除きであり、中間をまとめて 1 つの置き換えにする。
31
+ */
32
+ function collectFragment(current, next, base, out) {
33
+ let head = 0;
34
+ while (head < current.childCount &&
35
+ head < next.childCount &&
36
+ current.child(head).eq(next.child(head))) {
37
+ head += 1;
38
+ }
39
+ let tail = 0;
40
+ while (tail < current.childCount - head &&
41
+ tail < next.childCount - head &&
42
+ current.child(current.childCount - 1 - tail).eq(next.child(next.childCount - 1 - tail))) {
43
+ tail += 1;
44
+ }
45
+ const currentEnd = current.childCount - tail;
46
+ const nextEnd = next.childCount - tail;
47
+ if (head === currentEnd && head === nextEnd)
48
+ return;
49
+ if (currentEnd - head === nextEnd - head && currentEnd > head) {
50
+ let pos = base + offsetOf(current, head);
51
+ for (let index = head; index < currentEnd; index += 1) {
52
+ const child = current.child(index);
53
+ collectNode(child, next.child(index), pos, out);
54
+ pos += child.nodeSize;
55
+ }
56
+ return;
57
+ }
58
+ out.push({
59
+ from: base + offsetOf(current, head),
60
+ to: base + offsetOf(current, currentEnd),
61
+ content: next.cut(offsetOf(next, head), offsetOf(next, nextEnd))
62
+ });
63
+ }
64
+ /** 同じ添字にある 2 つのノード。形が同じなら中へ降り、違えばノードごと置き換える */
65
+ function collectNode(current, next, pos, out) {
66
+ if (current.eq(next))
67
+ return;
68
+ if (!current.sameMarkup(next) || current.isLeaf) {
69
+ out.push({ from: pos, to: pos + current.nodeSize, content: Fragment.from(next) });
70
+ return;
71
+ }
72
+ // 文字の並びは子の添字で突き合わせられない(1 文字の違いでテキストノードが丸ごと違う)
73
+ if (current.isTextblock) {
74
+ collectInline(current.content, next.content, pos + 1, out);
75
+ return;
76
+ }
77
+ collectFragment(current.content, next.content, pos + 1, out);
78
+ }
79
+ /**
80
+ * 文字の並びの差分。両端の一致を除いた中間を 1 範囲にする。
81
+ *
82
+ * ひとつのブロックの中で複数の箇所が変わっても 1 範囲にまとめる。守りたいのは
83
+ * 「書いている段落が作り直されないこと」であり、段落の中での最小性ではない。
84
+ */
85
+ function collectInline(current, next, base, out) {
86
+ const start = current.findDiffStart(next, base);
87
+ if (start === null)
88
+ return;
89
+ const ends = current.findDiffEnd(next, base + current.size, base + next.size);
90
+ if (!ends)
91
+ return;
92
+ let { a: endInCurrent, b: endInNext } = ends;
93
+ // 置き換えの範囲が前方の一致部分へ食い込まないようにそろえる
94
+ const overlap = start - Math.min(endInCurrent, endInNext);
95
+ if (overlap > 0) {
96
+ endInCurrent += overlap;
97
+ endInNext += overlap;
98
+ }
99
+ out.push({
100
+ from: start,
101
+ to: endInCurrent,
102
+ content: next.cut(start - base, endInNext - base)
103
+ });
104
+ }
105
+ /** 先頭から `index` 番目の子の手前までの大きさ */
106
+ function offsetOf(fragment, index) {
107
+ let offset = 0;
108
+ for (let at = 0; at < index; at += 1)
109
+ offset += fragment.child(at).nodeSize;
110
+ return offset;
111
+ }
112
+ // --- 表示だけの属性の写し込み ---
113
+ const COLLAPSED = 'collapsed';
114
+ /**
115
+ * 現在の文書の折りたたみを、当てる文書の同じ**構造上の位置**へ写す。
116
+ *
117
+ * `viewState` の突合キー(ハッシュ)を使わない。`hashOfChain` は根からの見出しの連なりを
118
+ * 混ぜるため、見出しが 1 つ変わると配下の全セクションで写せなくなる。ハッシュは
119
+ * 「別の経路で編集されうる保存済みの表示状態」のための鍵であり、1 回の適用の中で
120
+ * 写すだけなら構造上の位置で足りる。
121
+ */
122
+ function carryViewAttributes(current, next) {
123
+ if (next.childCount === 0)
124
+ return next;
125
+ const sources = alignChildren(current, next);
126
+ const children = [];
127
+ let carried = false;
128
+ for (let index = 0; index < next.childCount; index += 1) {
129
+ const child = next.child(index);
130
+ const mirror = sources[index];
131
+ const replacement = mirror && mirror.type === child.type ? carryInto(mirror, child) : child;
132
+ if (replacement !== child)
133
+ carried = true;
134
+ children.push(replacement);
135
+ }
136
+ return carried ? next.copy(Fragment.fromArray(children)) : next;
137
+ }
138
+ /**
139
+ * 当てる側の子ごとに、折りたたみを写してくる相手を決める。
140
+ *
141
+ * **添字をそのまま使わない。** 他の書き手がセクションやブロックを 1 つ足すと、以降の
142
+ * 添字がずれ、折りたたみが隣のセクションへ移る。前後の両端から形の合う子をたどり、
143
+ * 残った中間は数がそろっているときだけ添字どおりに見る。
144
+ */
145
+ function alignChildren(current, next) {
146
+ const sources = new Array(next.childCount).fill(null);
147
+ let head = 0;
148
+ while (head < current.childCount &&
149
+ head < next.childCount &&
150
+ sameFoldShape(current.child(head), next.child(head))) {
151
+ sources[head] = current.child(head);
152
+ head += 1;
153
+ }
154
+ let tail = 0;
155
+ while (tail < current.childCount - head &&
156
+ tail < next.childCount - head &&
157
+ sameFoldShape(current.child(current.childCount - 1 - tail), next.child(next.childCount - 1 - tail))) {
158
+ sources[next.childCount - 1 - tail] = current.child(current.childCount - 1 - tail);
159
+ tail += 1;
160
+ }
161
+ // 数が違えば、どれとどれが同じものかを決める手がかりが無い。写さない
162
+ if (current.childCount - tail - head !== next.childCount - tail - head)
163
+ return sources;
164
+ for (let index = head; index < next.childCount - tail; index += 1) {
165
+ sources[index] = current.child(index);
166
+ }
167
+ return sources;
168
+ }
169
+ /**
170
+ * 折りたたみを写してくる相手として同じものか。
171
+ *
172
+ * セクションは見出しで見る。**中身では見ない**——他の書き手が中身を変えたセクションでも、
173
+ * 折りたたみは書き手が見ているものとして保たれなければならない(要件 3.4)。
174
+ */
175
+ function sameFoldShape(current, next) {
176
+ if (current.type !== next.type)
177
+ return false;
178
+ if (current.type !== next.type.schema.nodes.section)
179
+ return true;
180
+ return current.child(0).eq(next.child(0));
181
+ }
182
+ /** 折りたたみを写し、さらに中へ降りる。写すものが無ければ受け取ったノードをそのまま返す */
183
+ function carryInto(current, next) {
184
+ if (next.isTextblock || next.isLeaf)
185
+ return next;
186
+ const content = carryViewAttributes(current, next);
187
+ const folded = current.attrs[COLLAPSED] === true && next.attrs[COLLAPSED] === false;
188
+ if (!folded)
189
+ return content;
190
+ return next.type.create({ ...next.attrs, [COLLAPSED]: true }, content.content, next.marks);
191
+ }
@@ -3,11 +3,11 @@ import { closeHistory } from 'prosemirror-history';
3
3
  import { InputRule, inputRules } from 'prosemirror-inputrules';
4
4
  import { keydownHandler, keymap } from 'prosemirror-keymap';
5
5
  import { Plugin } from 'prosemirror-state';
6
- import { autoformatRules } from '../autoformat';
7
- import { linkPastePlugin } from '../plugins/linkPaste';
8
- import { tabEscapePlugin } from '../plugins/tabEscape';
9
- import { coreActions } from './coreActions';
10
- import { isMacPlatform, toBindingString } from './shortcutKeys';
6
+ import { autoformatRules } from '../autoformat.js';
7
+ import { linkPastePlugin } from '../plugins/linkPaste.js';
8
+ import { tabEscapePlugin } from '../plugins/tabEscape.js';
9
+ import { coreActions } from './coreActions.js';
10
+ import { isMacPlatform, toBindingString } from './shortcutKeys.js';
11
11
  export const editorKeymap = {
12
12
  coreActions,
13
13
  createPlugins,
@@ -1,19 +1,19 @@
1
1
  import { toggleMark } from 'prosemirror-commands';
2
2
  import { redo, undo } from 'prosemirror-history';
3
3
  import { undoInputRule } from 'prosemirror-inputrules';
4
- import { outlineSchema } from '../../models/document/schema';
5
- import { isInCodeBlock } from '../commands/codeBlock';
6
- import { inTable, liftByContext, moveByContext, sectionCommands, sinkByContext } from '../commands/element';
7
- import { fencedBlockOnEnter } from '../commands/fencedBlock';
8
- import { holdBackspaceAtFrontMatter } from '../commands/frontMatter';
9
- import { insertLineBreak } from '../commands/lineBreak';
10
- import { toggleLink } from '../commands/link';
11
- import { blockInsideItem, listCommands } from '../commands/list';
12
- import { dissolveHeadingAtStart, insertHeadingBefore, insertSectionAfter, removeEmptyLeadingBlock, removeUnjoinableEmptyBlock, targetSection } from '../commands/section';
13
- import { requiredSelectionBlock } from '../commands/selectedBlock';
14
- import { tableCommands } from '../commands/table';
15
- import { verticalFromSelectedBlock } from '../plugins/requiredSelectionEntry';
16
- import { enterTabEscape } from '../plugins/tabEscape';
4
+ import { outlineSchema } from '../../models/document/schema.js';
5
+ import { isInCodeBlock } from '../commands/codeBlock.js';
6
+ import { inTable, liftByContext, moveByContext, sectionCommands, sinkByContext } from '../commands/element.js';
7
+ import { fencedBlockOnEnter } from '../commands/fencedBlock.js';
8
+ import { holdBackspaceAtFrontMatter } from '../commands/frontMatter.js';
9
+ import { insertLineBreak } from '../commands/lineBreak.js';
10
+ import { toggleLink } from '../commands/link.js';
11
+ import { blockInsideItem, listCommands } from '../commands/list.js';
12
+ import { dissolveHeadingAtStart, insertHeadingBefore, insertSectionAfter, removeEmptyLeadingBlock, removeUnjoinableEmptyBlock, targetSection } from '../commands/section.js';
13
+ import { requiredSelectionBlock } from '../commands/selectedBlock.js';
14
+ import { tableCommands } from '../commands/table.js';
15
+ import { verticalFromSelectedBlock } from '../plugins/requiredSelectionEntry.js';
16
+ import { enterTabEscape } from '../plugins/tabEscape.js';
17
17
  /**
18
18
  * 基盤の操作の宣言。**コマンド本体はここに書かない**(対象別の
19
19
  * `operations/commands/` から import する)。宣言と組み立て(`assemble.ts`)を
@@ -1,5 +1,5 @@
1
- import { editorKeymap } from './assemble';
2
- import { isMacPlatform, toBindingString } from './shortcutKeys';
1
+ import { editorKeymap } from './assemble.js';
2
+ import { isMacPlatform, toBindingString } from './shortcutKeys.js';
3
3
  /**
4
4
  * 宣言と現在の状態から、コンテキストメニューへ並べる項目を導く層。
5
5
  *
@@ -1,5 +1,5 @@
1
1
  import { keydownHandler } from 'prosemirror-keymap';
2
- import { looksLikeMac } from '../../utils/platform';
2
+ import { looksLikeMac } from '../../utils/platform.js';
3
3
  /**
4
4
  * キーの組み合わせの「持ち方」を 1 か所に集める層。
5
5
  *
@@ -1,8 +1,8 @@
1
- import { sectionCommands } from '../commands/element';
2
- import { targetSection } from '../commands/section';
3
- import { foldCommands } from '../commands/sectionFold';
4
- import { headingJump } from '../commands/sectionJump';
5
- import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
1
+ import { sectionCommands } from '../commands/element.js';
2
+ import { targetSection } from '../commands/section.js';
3
+ import { foldCommands } from '../commands/sectionFold.js';
4
+ import { headingJump } from '../commands/sectionJump.js';
5
+ import { MAX_HEADING_LEVEL } from '../../models/document/document.types.js';
6
6
  /**
7
7
  * 表示操作の宣言。
8
8
  *
@@ -1,6 +1,6 @@
1
1
  import { NodeSelection, Plugin, PluginKey } from 'prosemirror-state';
2
2
  import { Decoration, DecorationSet } from 'prosemirror-view';
3
- import { requiredSelectionBlock } from '../commands/selectedBlock';
3
+ import { requiredSelectionBlock } from '../commands/selectedBlock.js';
4
4
  /**
5
5
  * 帯を描くための印。**要選択ブロックだと CSS へ伝える唯一の手段である。**
6
6
  *
@@ -0,0 +1,30 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import type { EditorView } from 'prosemirror-view';
3
+ /**
4
+ * 確定の反映を待つ上限。ProseMirror は `compositionend` の 20ms 後に未反映の記録を流すため、
5
+ * **それより後ろに置く**。前に置くと、確定した文字が入る前に書き込めることになる。
6
+ */
7
+ export declare const SETTLE_LIMIT_MS = 50;
8
+ export interface CompositionStateApi {
9
+ readonly plugin: Plugin;
10
+ /**
11
+ * 外から文書へ書き込めるか。**真を返したときは、それまでに変換で確定した文字が
12
+ * すべて文書に入っている。**
13
+ *
14
+ * 覆うのは「ホストが外から書き込む経路」だけである。利用者の操作(キー・メニュー)への
15
+ * 変換中の抑止は `keymap/assemble.ts` の `decorate` が持ち、そちらは確定の反映を
16
+ * 待つ分の補正を要しない。
17
+ */
18
+ readonly canWriteFromOutside: (view: EditorView) => boolean;
19
+ }
20
+ /**
21
+ * 変換の状態を持つプラグインと、外から書き込めるかの述語を一緒に返す。
22
+ *
23
+ * **状態は生成関数の closure に持つ。** `composing` は `EditorView` にあって `EditorState`
24
+ * から辿れず、`compositionstart` は Transaction を作らないため、プラグインの state には
25
+ * 置けない。置こうとすると変換の出来事ごとに中身の無い Transaction を流すことになり、
26
+ * **変換中に dispatch するという、避けている行為そのもの**になる。
27
+ *
28
+ * エディタ 1 インスタンスにつき 1 つ生成する(モジュールに共有の状態を置かない)。
29
+ */
30
+ export declare function createCompositionState(): CompositionStateApi;
@@ -0,0 +1,65 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ /** 変換に由来する Transaction に ProseMirror が付ける印。`prosemirror-history` も読んでいる */
3
+ const COMPOSITION_META = 'composition';
4
+ /**
5
+ * 確定の反映を待つ上限。ProseMirror は `compositionend` の 20ms 後に未反映の記録を流すため、
6
+ * **それより後ろに置く**。前に置くと、確定した文字が入る前に書き込めることになる。
7
+ */
8
+ export const SETTLE_LIMIT_MS = 50;
9
+ /**
10
+ * 変換の状態を持つプラグインと、外から書き込めるかの述語を一緒に返す。
11
+ *
12
+ * **状態は生成関数の closure に持つ。** `composing` は `EditorView` にあって `EditorState`
13
+ * から辿れず、`compositionstart` は Transaction を作らないため、プラグインの state には
14
+ * 置けない。置こうとすると変換の出来事ごとに中身の無い Transaction を流すことになり、
15
+ * **変換中に dispatch するという、避けている行為そのもの**になる。
16
+ *
17
+ * エディタ 1 インスタンスにつき 1 つ生成する(モジュールに共有の状態を置かない)。
18
+ */
19
+ export function createCompositionState() {
20
+ let writeState = 'ready';
21
+ let settleLimit = null;
22
+ function stopWaiting(next) {
23
+ if (settleLimit !== null)
24
+ clearTimeout(settleLimit);
25
+ settleLimit = null;
26
+ writeState = next;
27
+ }
28
+ const plugin = new Plugin({
29
+ props: {
30
+ handleDOMEvents: {
31
+ compositionstart: () => {
32
+ stopWaiting('composing');
33
+ return false;
34
+ },
35
+ /**
36
+ * 確定した文字はまだ文書に入っていない。**ここで書き込める側へ移さない。**
37
+ * 入ったことは変換に由来する Transaction の到着で知り、上限はその取りこぼし
38
+ * に対する保険である。どちらも「遅い側」であり、早める道は置かない。
39
+ */
40
+ compositionend: () => {
41
+ stopWaiting('pending');
42
+ settleLimit = setTimeout(() => {
43
+ settleLimit = null;
44
+ writeState = 'ready';
45
+ }, SETTLE_LIMIT_MS);
46
+ return false;
47
+ }
48
+ }
49
+ },
50
+ appendTransaction: (transactions) => {
51
+ if (writeState !== 'pending')
52
+ return null;
53
+ if (!transactions.some((tr) => tr.getMeta(COMPOSITION_META) !== undefined))
54
+ return null;
55
+ stopWaiting('ready');
56
+ return null;
57
+ },
58
+ view: () => ({ destroy: () => stopWaiting('ready') })
59
+ });
60
+ return {
61
+ plugin,
62
+ // `composing` も見るのは、`compositionstart` を取りこぼしても偽へ倒れるようにするため
63
+ canWriteFromOutside: (view) => writeState === 'ready' && !view.composing
64
+ };
65
+ }
@@ -1,8 +1,8 @@
1
1
  import { NodeSelection, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
2
2
  import { CellSelection, cellAround } from 'prosemirror-tables';
3
3
  import { Decoration, DecorationSet } from 'prosemirror-view';
4
- import { sectionCommands } from '../commands/element';
5
- import { blockAtBand } from './blockHandle';
4
+ import { sectionCommands } from '../commands/element.js';
5
+ import { blockAtBand } from './blockHandle.js';
6
6
  /** 対象アイテムのマーカー。見た目は `editor.css` が持つ(利用者が上書きできる) */
7
7
  const TARGET_CLASS = 'markdown-editor__menu-target';
8
8
  /**
@@ -1,8 +1,8 @@
1
1
  import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
2
2
  import { Decoration, DecorationSet } from 'prosemirror-view';
3
- import { outlineSchema } from '../../models/document/schema';
4
- import { headingText, sectionTree } from '../../models/document/sectionTree';
5
- import { originOf } from '../transactionOrigin';
3
+ import { outlineSchema } from '../../models/document/schema.js';
4
+ import { headingText, sectionTree } from '../../models/document/sectionTree.js';
5
+ import { originOf } from '../transactionOrigin.js';
6
6
  const SECTION = outlineSchema.nodes.section;
7
7
  /** 範囲外のブロックに付ける印。隠すかどうかの決定は CSS に委ねる */
8
8
  export const OUT_OF_FOCUS_CLASS = 'markdown-editor__out-of-focus';
@@ -35,7 +35,7 @@ export function createFocusPlugin() {
35
35
  key,
36
36
  state: {
37
37
  init: () => IDLE,
38
- apply: (tr, value, _oldState, newState) => nextState(key, tr, value, newState.doc)
38
+ apply: (tr, value, oldState, newState) => nextState(key, tr, value, oldState.doc, newState.doc)
39
39
  },
40
40
  props: {
41
41
  decorations: (state) => key.getState(state)?.decorations ?? DecorationSet.empty
@@ -167,7 +167,7 @@ function requestFocus(state, key, request) {
167
167
  tr.setMeta('addToHistory', false);
168
168
  return tr;
169
169
  }
170
- function nextState(key, tr, value, doc) {
170
+ function nextState(key, tr, value, oldDoc, doc) {
171
171
  const meta = tr.getMeta(key);
172
172
  if (meta === RESTORE_DONE)
173
173
  return { ...value, pendingRestore: null };
@@ -187,19 +187,30 @@ function nextState(key, tr, value, doc) {
187
187
  };
188
188
  }
189
189
  const restorePos = value.restorePos === null ? null : tr.mapping.map(value.restorePos);
190
- // 連なりの導出は写した先が変わったときだけ行う(文字入力のたびに行わない)
191
- if (mapped.pos === value.anchorPos && originOf(tr) !== 'host') {
190
+ // 対象の中身だけの変更は、連なりもきょうだいの並びも変えない(文字入力がこれに当たる)
191
+ const range = focusRange(oldDoc, value.anchorPos);
192
+ if (range && staysInside(tr, range)) {
192
193
  return { ...value, restorePos, decorations: value.decorations.map(tr.mapping, doc) };
193
194
  }
195
+ const breadcrumb = breadcrumbOf(doc, mapped.pos);
194
196
  return {
195
197
  anchorPos: mapped.pos,
196
- breadcrumb: breadcrumbOf(doc, mapped.pos),
198
+ // 連なりが同じなら前のものを使う。**同期反映は入力の合間ごとに当たる**ため、
199
+ // 同じ中身で作り直すと、そのたびに画面の組み直しが走る
200
+ breadcrumb: sameBreadcrumb(value.breadcrumb, breadcrumb) ? value.breadcrumb : breadcrumb,
197
201
  restorePos,
198
202
  restoreScrollTop: value.restoreScrollTop,
199
203
  decorations: outOfFocusDecorations(doc, mapped.pos),
200
204
  pendingRestore: null
201
205
  };
202
206
  }
207
+ function sameBreadcrumb(a, b) {
208
+ if (a.length !== b.length)
209
+ return false;
210
+ return a.every((item, index) => item.title === b[index].title &&
211
+ item.path.length === b[index].path.length &&
212
+ item.path.every((step, depth) => step === b[index].path[depth]));
213
+ }
203
214
  function fromRequest(request, doc) {
204
215
  if (request === null)
205
216
  return IDLE;
@@ -1,5 +1,5 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { foldCommands } from '../commands/sectionFold';
2
+ import { foldCommands } from '../commands/sectionFold.js';
3
3
  /**
4
4
  * 記号を出している対象。**「畳めるか」は描画された要素に既に出ている**ため、
5
5
  * 選択子をそこにそろえる。記号を描く CSS と同じ根拠であり、
@@ -1,6 +1,6 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { hidingAncestors } from '../commands/sectionFold';
3
- import { markOrigin } from '../transactionOrigin';
2
+ import { hidingAncestors } from '../commands/sectionFold.js';
3
+ import { markOrigin } from '../transactionOrigin.js';
4
4
  /**
5
5
  * カーソルが折りたたまれた範囲の内側に取り残されないようにする。
6
6
  *
@@ -1,8 +1,8 @@
1
1
  import { AllSelection, Plugin, TextSelection } from 'prosemirror-state';
2
- import { bodySelection } from '../../models/document/bodySelection';
3
- import { outlineSchema } from '../../models/document/schema';
4
- import { isDocumentReplacement } from '../transaction';
5
- import { originOf } from '../transactionOrigin';
2
+ import { bodySelection } from '../../models/document/bodySelection.js';
3
+ import { outlineSchema } from '../../models/document/schema.js';
4
+ import { isDocumentReplacement } from '../transaction.js';
5
+ import { originOf } from '../transactionOrigin.js';
6
6
  const FRONT_MATTER = outlineSchema.nodes.front_matter;
7
7
  /**
8
8
  * 全選択の置き換えを「文書まるごとの差し替え」として成立させる見張り。
@@ -1,8 +1,8 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
2
  import { Decoration, DecorationSet } from 'prosemirror-view';
3
- import { outlineSchema } from '../../models/document/schema';
4
- import { isEditorFocused } from './editorFocus';
5
- import { findDateText } from '../../utils/dateText';
3
+ import { outlineSchema } from '../../models/document/schema.js';
4
+ import { isEditorFocused } from './editorFocus.js';
5
+ import { findDateText } from '../../utils/dateText.js';
6
6
  const FRONT_MATTER = outlineSchema.nodes.front_matter;
7
7
  /**
8
8
  * `front_matter` の NodeView と編集中 decoration を 1 つの Plugin として返す。
@@ -1,5 +1,5 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { sectionTree } from '../../models/document/sectionTree';
2
+ import { sectionTree } from '../../models/document/sectionTree.js';
3
3
  /**
4
4
  * 見出しのレベルを、書き換えのたびに構造上の深さへ合わせ直す。
5
5
  *
@@ -1,8 +1,8 @@
1
1
  import { closeHistory } from 'prosemirror-history';
2
2
  import { AllSelection, NodeSelection, Plugin, PluginKey } from 'prosemirror-state';
3
3
  import { Decoration, DecorationSet } from 'prosemirror-view';
4
- import { outlineSchema } from '../../models/document/schema';
5
- import { hasPastedTable } from '../../utils/pastedTable';
4
+ import { outlineSchema } from '../../models/document/schema.js';
5
+ import { hasPastedTable } from '../../utils/pastedTable.js';
6
6
  const IMAGE = outlineSchema.nodes.image;
7
7
  const imagePasteKey = new PluginKey('imagePaste');
8
8
  /**
@@ -1,5 +1,5 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { outlineSchema } from '../../models/document/schema';
2
+ import { outlineSchema } from '../../models/document/schema.js';
3
3
  const IMAGE = outlineSchema.nodes.image;
4
4
  /**
5
5
  * 画像の NodeView。
@@ -1,6 +1,6 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { outlineSchema } from '../../models/document/schema';
3
- import { URL_PATTERN } from '../commands/link';
2
+ import { outlineSchema } from '../../models/document/schema.js';
3
+ import { URL_PATTERN } from '../commands/link.js';
4
4
  /** 選択した範囲に URL を貼ると、その範囲をリンクの文字にする */
5
5
  export function linkPastePlugin() {
6
6
  return new Plugin({
@@ -1,5 +1,5 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { correctItemDepth } from '../commands/list';
2
+ import { correctItemDepth } from '../commands/list.js';
3
3
  /**
4
4
  * 箇条書きの形を、書き換えのたびに整える。
5
5
  *
@@ -1,14 +1,14 @@
1
1
  import { Fragment, Slice } from 'prosemirror-model';
2
2
  import { AllSelection, Plugin, TextSelection } from 'prosemirror-state';
3
3
  import { isInTable } from 'prosemirror-tables';
4
- import { bodySelection } from '../../models/document/bodySelection';
5
- import { parseMarkdown } from '../../models/markdown/parser';
6
- import { serializeSlice } from '../../models/markdown/serializer';
7
- import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
8
- import { outlineSchema } from '../../models/document/schema';
9
- import { sectionTree } from '../../models/document/sectionTree';
10
- import { markDocumentReplacement } from '../transaction';
11
- import { hasPastedTable } from '../../utils/pastedTable';
4
+ import { bodySelection } from '../../models/document/bodySelection.js';
5
+ import { parseMarkdown } from '../../models/markdown/parser.js';
6
+ import { serializeSlice } from '../../models/markdown/serializer.js';
7
+ import { MAX_HEADING_LEVEL } from '../../models/document/document.types.js';
8
+ import { outlineSchema } from '../../models/document/schema.js';
9
+ import { sectionTree } from '../../models/document/sectionTree.js';
10
+ import { markDocumentReplacement } from '../transaction.js';
11
+ import { hasPastedTable } from '../../utils/pastedTable.js';
12
12
  const n = outlineSchema.nodes;
13
13
  /**
14
14
  * クリップボードとのやり取りをマークダウンで行う。
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
2
  import { Decoration, DecorationSet } from 'prosemirror-view';
3
- import { outlineSchema } from '../../models/document/schema';
4
- import { isEditorFocused } from './editorFocus';
3
+ import { outlineSchema } from '../../models/document/schema.js';
4
+ import { isEditorFocused } from './editorFocus.js';
5
5
  const MERMAID_BLOCK = outlineSchema.nodes.mermaid_block;
6
6
  // --- mermaid の遅延読み込み ---
7
7
  //
@@ -1,6 +1,6 @@
1
1
  import { keydownHandler } from 'prosemirror-keymap';
2
2
  import { NodeSelection, Plugin, PluginKey, Selection, TextSelection } from 'prosemirror-state';
3
- import { requiredSelectionBlock } from '../commands/selectedBlock';
3
+ import { requiredSelectionBlock } from '../commands/selectedBlock.js';
4
4
  /**
5
5
  * 上下移動で要選択ブロックの境を越えたとき、中へ入らずそのブロックを指した状態にする。
6
6
  *
@@ -1,5 +1,5 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
- import { outlineSchema } from '../../models/document/schema';
2
+ import { outlineSchema } from '../../models/document/schema.js';
3
3
  const SECTION = outlineSchema.nodes.section;
4
4
  /**
5
5
  * セクションの描画を、**子の増減に追随させる。**
@@ -1,8 +1,8 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
2
  import { TableMap } from 'prosemirror-tables';
3
3
  import { Decoration, DecorationSet } from 'prosemirror-view';
4
- import { insertColumnAt } from '../commands/table';
5
- import { outlineSchema } from '../../models/document/schema';
4
+ import { insertColumnAt } from '../commands/table.js';
5
+ import { outlineSchema } from '../../models/document/schema.js';
6
6
  /**
7
7
  * 列の境目に、押すと列が増える手がかりを出す。
8
8
  *