@14ch/svelte-md-outliner 0.0.18 → 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.
- package/CHANGELOG.md +41 -0
- package/README.ja.md +47 -24
- package/README.md +47 -24
- package/dist/assets/styles/editor.css +42 -3
- package/dist/assets/styles/theme.css +2 -3
- package/dist/components/ContextMenu.svelte +1 -1
- package/dist/components/EditorSurface.svelte +2 -2
- package/dist/components/MarkdownEditor.svelte +69 -17
- package/dist/components/MarkdownEditor.svelte.d.ts +16 -3
- package/dist/components/ShortcutHelpDialog.svelte +3 -3
- package/dist/index.d.ts +12 -4
- package/dist/index.js +4 -2
- package/dist/markdown.d.ts +2 -0
- package/dist/markdown.js +1 -0
- package/dist/messages/en.js +1 -1
- package/dist/messages/index.d.ts +2 -2
- package/dist/messages/index.js +2 -2
- package/dist/messages/ja.js +1 -1
- package/dist/models/document/bodySelection.js +1 -1
- package/dist/models/document/schema.d.ts +10 -1
- package/dist/models/document/schema.js +13 -2
- package/dist/models/document/sectionTree.d.ts +1 -1
- package/dist/models/document/sectionTree.js +2 -2
- package/dist/models/markdown/normalize.d.ts +20 -0
- package/dist/models/markdown/normalize.js +25 -0
- package/dist/models/markdown/parser.js +8 -6
- package/dist/models/markdown/serializer.js +12 -11
- package/dist/models/viewState/viewStateCodec.d.ts +1 -1
- package/dist/models/viewState/viewStateCodec.js +3 -3
- package/dist/operations/autoformat.d.ts +1 -1
- package/dist/operations/autoformat.js +21 -33
- package/dist/operations/commands/block.d.ts +19 -1
- package/dist/operations/commands/block.js +44 -4
- package/dist/operations/commands/element.js +9 -9
- package/dist/operations/commands/fencedBlock.d.ts +21 -0
- package/dist/operations/commands/fencedBlock.js +49 -0
- package/dist/operations/commands/frontMatter.js +5 -5
- package/dist/operations/commands/lineBreak.js +1 -1
- package/dist/operations/commands/link.js +1 -1
- package/dist/operations/commands/list.js +59 -6
- package/dist/operations/commands/section.d.ts +1 -1
- package/dist/operations/commands/section.js +5 -5
- package/dist/operations/commands/sectionFold.d.ts +1 -1
- package/dist/operations/commands/sectionFold.js +5 -5
- package/dist/operations/commands/sectionJump.js +2 -2
- package/dist/operations/commands/selectedBlock.js +3 -3
- package/dist/operations/commands/table.js +43 -2
- package/dist/operations/documentDiff.d.ts +12 -0
- package/dist/operations/documentDiff.js +191 -0
- package/dist/operations/keymap/assemble.js +5 -5
- package/dist/operations/keymap/coreActions.js +16 -16
- package/dist/operations/keymap/menuItems.js +2 -2
- package/dist/operations/keymap/shortcutKeys.js +1 -1
- package/dist/operations/keymap/viewActions.js +5 -5
- package/dist/operations/plugins/blockHandle.js +1 -1
- package/dist/operations/plugins/compositionState.d.ts +30 -0
- package/dist/operations/plugins/compositionState.js +65 -0
- package/dist/operations/plugins/contextMenu.js +2 -2
- package/dist/operations/plugins/focus.js +19 -8
- package/dist/operations/plugins/foldClick.js +1 -1
- package/dist/operations/plugins/foldGuard.js +2 -2
- package/dist/operations/plugins/frontMatterGuard.js +4 -4
- package/dist/operations/plugins/frontMatterView.js +3 -3
- package/dist/operations/plugins/headingLevel.js +1 -1
- package/dist/operations/plugins/imagePaste.js +2 -2
- package/dist/operations/plugins/imageView.js +1 -1
- package/dist/operations/plugins/linkPaste.js +2 -2
- package/dist/operations/plugins/listStructure.js +1 -1
- package/dist/operations/plugins/markdownClipboard.js +8 -8
- package/dist/operations/plugins/mermaidView.js +2 -2
- package/dist/operations/plugins/requiredSelectionEntry.js +1 -1
- package/dist/operations/plugins/sectionView.js +1 -1
- package/dist/operations/plugins/tableHandles.js +2 -2
- package/dist/operations/transactionOrigin.d.ts +9 -5
- package/dist/operations/transactionOrigin.js +2 -1
- package/dist/states/contextMenuController.svelte.js +5 -5
- package/dist/states/editorSession.js +69 -50
- package/dist/states/editorSession.types.d.ts +41 -3
- package/dist/states/outlineViewController.svelte.d.ts +1 -7
- package/dist/states/outlineViewController.svelte.js +7 -22
- package/package.json +1 -1
- package/dist/operations/commands/mermaidBlock.d.ts +0 -9
- package/dist/operations/commands/mermaidBlock.js +0 -28
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { joinTextblockBackward, wrapIn } from 'prosemirror-commands';
|
|
2
|
-
import { backspaceCommand, createIndentListCommand, createSplitListCommand,
|
|
1
|
+
import { chainCommands, joinTextblockBackward, wrapIn } from 'prosemirror-commands';
|
|
2
|
+
import { backspaceCommand, createIndentListCommand, createSplitListCommand, createUnwrapListCommand, createWrapInListCommand, doSplitList, findListsRange, isListNode } from 'prosemirror-flat-list';
|
|
3
3
|
import { TextSelection } from 'prosemirror-state';
|
|
4
|
-
import { outlineSchema } from '../../models/document/schema';
|
|
5
|
-
import { moveContent, setCursor } from '../transaction';
|
|
6
|
-
import { coveredEnd, moveBlockAcrossSection, moveRange, neighborSide, restoreMovedSelection, siblingRange } from './section';
|
|
4
|
+
import { canBeItemLine, outlineSchema } from '../../models/document/schema.js';
|
|
5
|
+
import { moveContent, setCursor } from '../transaction.js';
|
|
6
|
+
import { coveredEnd, moveBlockAcrossSection, moveRange, neighborSide, restoreMovedSelection, siblingRange } from './section.js';
|
|
7
7
|
export const listCommands = {
|
|
8
8
|
indentList,
|
|
9
9
|
dedentList,
|
|
10
10
|
splitListItem,
|
|
11
11
|
toggleTaskChecked,
|
|
12
|
-
toggleListKind
|
|
12
|
+
toggleListKind,
|
|
13
13
|
unwrapList: createUnwrapListCommand(),
|
|
14
14
|
moveListItem,
|
|
15
15
|
backspace: listBackspace,
|
|
@@ -17,6 +17,59 @@ export const listCommands = {
|
|
|
17
17
|
isInList: (state) => enclosingListItem(state) !== null,
|
|
18
18
|
enclosingListItem
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* 箇条書きにする(同じ種別なら解除する)。
|
|
22
|
+
*
|
|
23
|
+
* **項目の行になれないブロックは、選択に混ざっていても項目にしない**
|
|
24
|
+
* (→ スキーマの `canBeItemLine`)。コードブロックを項目にすると「中黒の付いた
|
|
25
|
+
* コードブロック」になり、書き出して読み直すと空の項目行が 1 つ増える。
|
|
26
|
+
* 解除の判定は選択の全体で見る——ブロックごとに掛け外しすると、段落と項目が
|
|
27
|
+
* 混ざった選択で、触っていない項目まで解けてしまう。
|
|
28
|
+
*/
|
|
29
|
+
function toggleListKind(kind) {
|
|
30
|
+
return chainCommands(createUnwrapListCommand({ kind }), wrapItemLinesInList(kind));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 選択の中の**項目の行になれるブロックだけ**を項目にする。
|
|
34
|
+
*
|
|
35
|
+
* 1 ブロックずつ包む(ライブラリの包む操作も同じ単位で動く)。後ろから順に当てるのは、
|
|
36
|
+
* 後ろを包んでも前のブロックの位置が動かないためである。
|
|
37
|
+
*/
|
|
38
|
+
function wrapItemLinesInList(kind) {
|
|
39
|
+
const wrapOne = createWrapInListCommand({ kind });
|
|
40
|
+
return (state, dispatch) => {
|
|
41
|
+
const { $from, $to } = state.selection;
|
|
42
|
+
const range = $from.blockRange($to);
|
|
43
|
+
if (!range)
|
|
44
|
+
return false;
|
|
45
|
+
const targets = [];
|
|
46
|
+
for (let index = range.startIndex; index < range.endIndex; index += 1) {
|
|
47
|
+
const child = range.parent.child(index);
|
|
48
|
+
// すでに項目なら種別の変更として通す(判定はライブラリが持つ)
|
|
49
|
+
if (!isListNode(child) && !canBeItemLine(child.type))
|
|
50
|
+
continue;
|
|
51
|
+
targets.push($from.posAtIndex(index, range.depth) + 1);
|
|
52
|
+
}
|
|
53
|
+
if (targets.length === 0)
|
|
54
|
+
return false;
|
|
55
|
+
if (!dispatch)
|
|
56
|
+
return true;
|
|
57
|
+
const tr = state.tr;
|
|
58
|
+
let current = state;
|
|
59
|
+
for (const pos of targets.reverse()) {
|
|
60
|
+
const at = current.apply(current.tr.setSelection(TextSelection.near(current.doc.resolve(pos))));
|
|
61
|
+
wrapOne(at, (next) => {
|
|
62
|
+
for (const step of next.steps)
|
|
63
|
+
tr.step(step);
|
|
64
|
+
current = at.apply(next);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (!tr.docChanged)
|
|
68
|
+
return false;
|
|
69
|
+
dispatch(tr.scrollIntoView());
|
|
70
|
+
return true;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
20
73
|
/**
|
|
21
74
|
* 段落を箇条書きの項目にする。
|
|
22
75
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Node } from 'prosemirror-model';
|
|
2
2
|
import { type EditorState, type Transaction } from 'prosemirror-state';
|
|
3
|
-
import { type HeadingLevel, type SectionRef } from '../../models/document/document.types';
|
|
3
|
+
import { type HeadingLevel, type SectionRef } from '../../models/document/document.types.js';
|
|
4
4
|
import type { EditorCommand, MoveDirection } from '../operations.types';
|
|
5
5
|
/**
|
|
6
6
|
* 見出しを 1 段深くする。直前の兄弟セクションの中身の末尾へ移す。
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { joinTextblockBackward } from 'prosemirror-commands';
|
|
2
2
|
import { NodeSelection, Selection, TextSelection } from 'prosemirror-state';
|
|
3
|
-
import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
|
|
4
|
-
import { bodyCount, bodyIndex, bodyStart } from '../../models/document/bodySelection';
|
|
5
|
-
import { outlineSchema } from '../../models/document/schema';
|
|
6
|
-
import { sectionTree } from '../../models/document/sectionTree';
|
|
7
|
-
import { moveContent, selectBlockAt, setCursor } from '../transaction';
|
|
3
|
+
import { MAX_HEADING_LEVEL } from '../../models/document/document.types.js';
|
|
4
|
+
import { bodyCount, bodyIndex, bodyStart } from '../../models/document/bodySelection.js';
|
|
5
|
+
import { outlineSchema } from '../../models/document/schema.js';
|
|
6
|
+
import { sectionTree } from '../../models/document/sectionTree.js';
|
|
7
|
+
import { moveContent, selectBlockAt, setCursor } from '../transaction.js';
|
|
8
8
|
const n = outlineSchema.nodes;
|
|
9
9
|
/**
|
|
10
10
|
* 見出しを 1 段深くする。直前の兄弟セクションの中身の末尾へ移す。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Node } from 'prosemirror-model';
|
|
2
2
|
import { type EditorState } from 'prosemirror-state';
|
|
3
|
-
import { type HeadingLevel, type SectionPath } from '../../models/document/document.types';
|
|
3
|
+
import { type HeadingLevel, type SectionPath } from '../../models/document/document.types.js';
|
|
4
4
|
import type { EditorCommand } from '../operations.types';
|
|
5
5
|
export interface FoldCommands {
|
|
6
6
|
/** カーソル位置の見出しまたは箇条書きの項目の折りたたみを反転する */
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isListNode } from 'prosemirror-flat-list';
|
|
2
2
|
import { TextSelection } from 'prosemirror-state';
|
|
3
|
-
import { hasFoldableChildren } from '../../models/document/foldable';
|
|
4
|
-
import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
|
|
5
|
-
import { outlineSchema } from '../../models/document/schema';
|
|
6
|
-
import { sectionTree } from '../../models/document/sectionTree';
|
|
7
|
-
import { markOrigin } from '../transactionOrigin';
|
|
3
|
+
import { hasFoldableChildren } from '../../models/document/foldable.js';
|
|
4
|
+
import { MAX_HEADING_LEVEL } from '../../models/document/document.types.js';
|
|
5
|
+
import { outlineSchema } from '../../models/document/schema.js';
|
|
6
|
+
import { sectionTree } from '../../models/document/sectionTree.js';
|
|
7
|
+
import { markOrigin } from '../transactionOrigin.js';
|
|
8
8
|
const SECTION = outlineSchema.nodes.section;
|
|
9
9
|
export const foldCommands = {
|
|
10
10
|
toggleFold,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TextSelection } from 'prosemirror-state';
|
|
2
|
-
import { outlineSchema } from '../../models/document/schema';
|
|
3
|
-
import { sectionTree } from '../../models/document/sectionTree';
|
|
2
|
+
import { outlineSchema } from '../../models/document/schema.js';
|
|
3
|
+
import { sectionTree } from '../../models/document/sectionTree.js';
|
|
4
4
|
const SECTION = outlineSchema.nodes.section;
|
|
5
5
|
export const headingJump = {
|
|
6
6
|
jumpToHeading,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AllSelection, NodeSelection, TextSelection } from 'prosemirror-state';
|
|
2
|
-
import { bodyStart } from '../../models/document/bodySelection';
|
|
3
|
-
import { outlineSchema } from '../../models/document/schema';
|
|
4
|
-
import { selectBlockAt } from '../transaction';
|
|
2
|
+
import { bodyStart } from '../../models/document/bodySelection.js';
|
|
3
|
+
import { outlineSchema } from '../../models/document/schema.js';
|
|
4
|
+
import { selectBlockAt } from '../transaction.js';
|
|
5
5
|
export const requiredSelectionBlock = {
|
|
6
6
|
isRequiredSelectionBlock,
|
|
7
7
|
selectedBlock,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { GapCursor } from 'prosemirror-gapcursor';
|
|
2
2
|
import { Selection, TextSelection } from 'prosemirror-state';
|
|
3
3
|
import { addColumn, addRow, CellSelection, deleteTable, goToNextCell as goToAdjacentCell, isInTable, moveTableColumn, moveTableRow, nextCell, removeColumn, removeRow, selectedRect, selectionCell, TableMap } from 'prosemirror-tables';
|
|
4
|
-
import { bodyStart } from '../../models/document/bodySelection';
|
|
5
|
-
import { outlineSchema } from '../../models/document/schema';
|
|
4
|
+
import { bodyStart } from '../../models/document/bodySelection.js';
|
|
5
|
+
import { outlineSchema } from '../../models/document/schema.js';
|
|
6
6
|
const n = outlineSchema.nodes;
|
|
7
7
|
/** 見出し行は表の 0 行目である(`table_header` の有無では判定しない) */
|
|
8
8
|
const HEADER_ROW = 0;
|
|
@@ -367,6 +367,9 @@ const canPlaceGapCursor = GapCursor.valid;
|
|
|
367
367
|
* はじめてセル単位の選択へ移る。左右は文字の選択と同じ向きなので、これがないと
|
|
368
368
|
* セルの中を選べなくなる。
|
|
369
369
|
*
|
|
370
|
+
* **起点が表の外にある選択は、セル範囲にはならない**(要件 7.8 は「カーソルがセル内に
|
|
371
|
+
* あるとき」の規則である)。その選択では表を丸ごと覆う。
|
|
372
|
+
*
|
|
370
373
|
* 編集基盤にも同じ割り当てがあるが、`goToCellInDirection` と同じ理由で発火しない
|
|
371
374
|
* (端の判定が「セルがブロックを含む」前提の深さで行われる)。
|
|
372
375
|
*/
|
|
@@ -376,6 +379,12 @@ function extendSelectionToCell(direction) {
|
|
|
376
379
|
return (state, dispatch, view) => {
|
|
377
380
|
if (!isInTable(state))
|
|
378
381
|
return false;
|
|
382
|
+
// 表の外から掛かってきた選択は、セル範囲へ移さず表を丸ごと覆う(→ `coveringTable`)
|
|
383
|
+
const covering = coveringTable(state);
|
|
384
|
+
if (covering) {
|
|
385
|
+
dispatch?.(state.tr.setSelection(covering).scrollIntoView());
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
379
388
|
const current = cellSelectionToExtend(state, view, direction);
|
|
380
389
|
if (!current)
|
|
381
390
|
return false;
|
|
@@ -388,6 +397,38 @@ function extendSelectionToCell(direction) {
|
|
|
388
397
|
return true;
|
|
389
398
|
};
|
|
390
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* 表の外から掛かってきた選択を、**表を丸ごと覆う形**にして返す。表の中だけの選択なら
|
|
402
|
+
* `null`(セル範囲の選択として扱う)。
|
|
403
|
+
*
|
|
404
|
+
* 起点を保ったままセル範囲へ移ろうとすると、セル範囲は表の中しか指せないため
|
|
405
|
+
* **表の外にあった起点が失われる**(選んでいた範囲が消える。利用者の指摘、2026-10-06)。
|
|
406
|
+
* 表は丸ごと選ぶブロックである(→ `selectedBlock.ts` の要選択ブロック)ため、
|
|
407
|
+
* 覆う先は起点から遠い側の端——上から来たなら表の後ろ、下から来たなら表の前——である。
|
|
408
|
+
*/
|
|
409
|
+
function coveringTable(state) {
|
|
410
|
+
const { $anchor, $head, anchor } = state.selection;
|
|
411
|
+
const table = enclosingTable($head);
|
|
412
|
+
if (!table)
|
|
413
|
+
return null;
|
|
414
|
+
if (anchor > table.pos && anchor < table.pos + table.node.nodeSize)
|
|
415
|
+
return null;
|
|
416
|
+
// 行き先は表の外の文字の位置。表の中(最後のセルの末尾など)に置くと、もう一度
|
|
417
|
+
// 押しても同じ範囲になり、選択が表の先で伸びなくなる
|
|
418
|
+
const forward = anchor < table.pos;
|
|
419
|
+
const beyond = state.doc.resolve(forward ? table.pos + table.node.nodeSize : table.pos);
|
|
420
|
+
const outside = Selection.findFrom(beyond, forward ? 1 : -1, true);
|
|
421
|
+
return TextSelection.between($anchor, outside?.$head ?? beyond);
|
|
422
|
+
}
|
|
423
|
+
/** その位置を包む表。無ければ `null` */
|
|
424
|
+
function enclosingTable($pos) {
|
|
425
|
+
for (let depth = $pos.depth; depth > 0; depth -= 1) {
|
|
426
|
+
const node = $pos.node(depth);
|
|
427
|
+
if (node.type === n.table)
|
|
428
|
+
return { node, pos: $pos.before(depth) };
|
|
429
|
+
}
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
391
432
|
/**
|
|
392
433
|
* 広げる元になるセル範囲。すでにセル範囲を選んでいればそれ、カーソルがセルの中の
|
|
393
434
|
* その向きの端にあれば、そのセル 1 つを覆う範囲。どちらでもなければ `null`。
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Node } from 'prosemirror-model';
|
|
2
|
+
import type { EditorState, Transaction } from 'prosemirror-state';
|
|
3
|
+
/**
|
|
4
|
+
* 変わった箇所ごとに置き換えを積んだ Transaction。差分が無ければ `null`。
|
|
5
|
+
*
|
|
6
|
+
* 全体を 1 範囲で置き換えないのは、あいだの一致する箇所まで作り直してしまうためである。
|
|
7
|
+
* 作り直すと、そこにあったカーソル・取り消しの手順の写し先・折りたたみが失われる。
|
|
8
|
+
*
|
|
9
|
+
* 表示だけの属性(折りたたみ)は、比較の前に当てる文書へ写す。写さないと、中身が同じ
|
|
10
|
+
* セクションも「違うノード」として数えられ、範囲がその部分木へ広がる。
|
|
11
|
+
*/
|
|
12
|
+
export declare function diffTransaction(state: EditorState, next: Node): Transaction | null;
|
|
@@ -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 {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
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`)を
|
|
@@ -348,15 +348,15 @@ export const coreActions = [
|
|
|
348
348
|
command: enterTabEscape
|
|
349
349
|
},
|
|
350
350
|
{
|
|
351
|
-
// 行頭の ```
|
|
351
|
+
// 行頭の ``` に続く改行での変換(改行側。空白側は InputRule)。
|
|
352
352
|
// 項目の中の段落でも働くよう、Enter の並びで splitListItem より前に置く
|
|
353
|
-
id: 'editing.
|
|
353
|
+
id: 'editing.fencedBlockOnEnter',
|
|
354
354
|
category: 'format',
|
|
355
355
|
scope: 'input',
|
|
356
356
|
listed: false,
|
|
357
357
|
changesDocument: true,
|
|
358
358
|
keys: [{ key: 'Enter' }],
|
|
359
|
-
command:
|
|
359
|
+
command: fencedBlockOnEnter
|
|
360
360
|
},
|
|
361
361
|
{
|
|
362
362
|
id: 'editing.splitListItem',
|
|
@@ -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,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
|
+
}
|