@14ch/svelte-md-outliner 0.0.14 → 0.0.16
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 +24 -0
- package/dist/components/EditorSurface.svelte +1 -1
- package/dist/operations/commands/block.js +31 -3
- package/dist/operations/commands/element.js +1 -16
- package/dist/operations/commands/list.d.ts +22 -0
- package/dist/operations/commands/list.js +64 -8
- package/dist/operations/keymap/coreActions.js +5 -1
- package/dist/operations/plugins/editorFocus.d.ts +20 -0
- package/dist/operations/plugins/editorFocus.js +66 -0
- package/dist/operations/plugins/frontMatterView.js +8 -4
- package/dist/operations/plugins/mermaidView.js +8 -3
- package/dist/states/editorSession.js +3 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.0.16] - 2026-09-24
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Front matter and Mermaid blocks now leave their editing view when the editor
|
|
13
|
+
loses focus. The switch was tied to where the selection sat, and the editor
|
|
14
|
+
keeps that selection after a blur, so clicking away left the front matter
|
|
15
|
+
showing its input surface and raw timestamps, and a Mermaid block showing its
|
|
16
|
+
source instead of the diagram — with no caret anywhere on screen. Focus is now
|
|
17
|
+
part of that judgement, so the surface, the readable dates and the diagram all
|
|
18
|
+
come back together.
|
|
19
|
+
|
|
20
|
+
## [0.0.15] - 2026-09-20
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
|
|
24
|
+
- A paragraph inside a list item can be turned back into a list item. Turning a
|
|
25
|
+
nested item into a paragraph left it stranded: Tab did nothing, "Turn into a
|
|
26
|
+
bullet list" was missing from the context menu, and pressing its shortcut
|
|
27
|
+
unwrapped the surrounding item instead — as did "Turn into a paragraph" on a
|
|
28
|
+
line that already was one. Tab, ⌘], ⌘→ and the bullet/numbered/task list
|
|
29
|
+
conversions now all act on that block alone and leave the item that holds it
|
|
30
|
+
untouched.
|
|
31
|
+
|
|
8
32
|
## [0.0.14] - 2026-09-19
|
|
9
33
|
|
|
10
34
|
### Changed
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { chainCommands, lift, setBlockType, wrapIn } from 'prosemirror-commands';
|
|
2
2
|
import { outlineSchema } from '../../models/document/schema';
|
|
3
3
|
import { setCursor } from '../transaction';
|
|
4
|
-
import { listCommands } from './list';
|
|
4
|
+
import { blockInsideItem, blockToListItem, itemToParagraph, listCommands } from './list';
|
|
5
5
|
import { blockContentEnd, convertToHeadings, dissolveLevel, previousSiblingSection, sectionToListItem, targetSection } from './section';
|
|
6
6
|
const n = outlineSchema.nodes;
|
|
7
7
|
/**
|
|
@@ -23,9 +23,36 @@ export function setBlockKind(kind) {
|
|
|
23
23
|
? dissolveLevel(state, dispatch, listKind)
|
|
24
24
|
: dissolveSection(state, dispatch, kind);
|
|
25
25
|
}
|
|
26
|
+
// 項目の本文より後ろのブロックでは、そのブロックだけを変える(→ `blockKindInItem`)
|
|
27
|
+
if (blockInsideItem(state))
|
|
28
|
+
return blockKindInItem(kind)(state, dispatch, view);
|
|
26
29
|
return changeBlock(state, dispatch, view);
|
|
27
30
|
};
|
|
28
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* 項目の本文より後ろのブロックの種別変換。**親の項目には触れない。**
|
|
34
|
+
*
|
|
35
|
+
* 通常の経路はカーソルを包む項目を解いてから変えるため、この位置で押すと書き手が
|
|
36
|
+
* 触っていない親の項目が解除される。対象は `targetElement` が答えるとおりそのブロック
|
|
37
|
+
* であり、変換もそのブロックだけに当てる。
|
|
38
|
+
*
|
|
39
|
+
* 箇条書きにすると、そのブロックは同じ位置の項目になる(段落にする操作の逆)。
|
|
40
|
+
*/
|
|
41
|
+
function blockKindInItem(kind) {
|
|
42
|
+
const listKind = LIST_KINDS[kind];
|
|
43
|
+
if (listKind)
|
|
44
|
+
return blockToListItem(listKind);
|
|
45
|
+
if (kind === 'blockquote')
|
|
46
|
+
return chainCommands(unwrapBlockquote, wrapIn(n.blockquote));
|
|
47
|
+
if (kind === 'code_block')
|
|
48
|
+
return setBlockType(n.code_block);
|
|
49
|
+
return (state, dispatch, view) => {
|
|
50
|
+
const block = blockInsideItem(state);
|
|
51
|
+
if (!block || block.node.type === n.paragraph)
|
|
52
|
+
return false;
|
|
53
|
+
return chainCommands(setBlockType(n.paragraph), lift)(state, dispatch, view);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
29
56
|
/**
|
|
30
57
|
* 見出しとリスト項目を行き来する。
|
|
31
58
|
*
|
|
@@ -62,8 +89,9 @@ function blockKindCommand(kind) {
|
|
|
62
89
|
if (kind === 'code_block') {
|
|
63
90
|
return outOfList(setBlockType(n.code_block));
|
|
64
91
|
}
|
|
65
|
-
//
|
|
66
|
-
|
|
92
|
+
// 段落へ戻す。箇条書き・引用のいずれからでも解ける。**項目を解くのは、階層を浅くする
|
|
93
|
+
// キーで段落になるのと同じことなので、実装も同じものを通す**(→ `itemToParagraph`)
|
|
94
|
+
return chainCommands(itemToParagraph, lift, setBlockType(n.paragraph));
|
|
67
95
|
}
|
|
68
96
|
/** 引用の中にいるときだけ解く。同じ操作で掛け外しできるようにするための片側 */
|
|
69
97
|
const unwrapBlockquote = (state, dispatch, view) => state.selection.$from.node(-1)?.type === n.blockquote ? lift(state, dispatch, view) : false;
|
|
@@ -6,7 +6,7 @@ import { selectBlockAt, setCursor } from '../transaction';
|
|
|
6
6
|
import { isTabEscaped } from '../plugins/tabEscape';
|
|
7
7
|
import { setBlockKind, toggleHeading } from './block';
|
|
8
8
|
import { indentInCodeBlock, outdentInCodeBlock } from './codeBlock';
|
|
9
|
-
import { listCommands, moveItemAcrossSection, paragraphToListItem } from './list';
|
|
9
|
+
import { blockInsideItem, listCommands, moveItemAcrossSection, paragraphToListItem } from './list';
|
|
10
10
|
import { coveredEnd, liftSection, moveBlockAcrossSection, moveRange, moveSection, neighborSide, restoreMovedSelection, siblingRange, sinkSection, targetSection, topLevelBlock } from './section';
|
|
11
11
|
import { liftSelectedBlock, requiredSelectionBlock, sinkSelectedBlock } from './selectedBlock';
|
|
12
12
|
import { tableCommands } from './table';
|
|
@@ -119,21 +119,6 @@ function ownedByParent(node) {
|
|
|
119
119
|
const role = node.type.spec.tableRole;
|
|
120
120
|
return role === 'cell' || role === 'header_cell' || role === 'row';
|
|
121
121
|
}
|
|
122
|
-
/**
|
|
123
|
-
* カーソルが項目の**本文より後ろ**のブロックにあるとき、そのブロック。
|
|
124
|
-
* 本文(最初のブロック)にいるときは null(対象は項目そのもの)。
|
|
125
|
-
*/
|
|
126
|
-
function blockInsideItem(state) {
|
|
127
|
-
const $from = state.selection.$from;
|
|
128
|
-
for (let depth = $from.depth; depth > 0; depth -= 1) {
|
|
129
|
-
if ($from.node(depth).type !== outlineSchema.nodes.list)
|
|
130
|
-
continue;
|
|
131
|
-
if ($from.index(depth) === 0 || depth + 1 > $from.depth)
|
|
132
|
-
return null;
|
|
133
|
-
return { node: $from.node(depth + 1), pos: $from.before(depth + 1) };
|
|
134
|
-
}
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
122
|
/**
|
|
138
123
|
* 対象を 1 段深くする。
|
|
139
124
|
*
|
|
@@ -41,6 +41,13 @@ export declare const listCommands: ListCommands;
|
|
|
41
41
|
* 深くできなかった」場合であり、そこで種別を切り替えると項目が解除されてしまう。
|
|
42
42
|
*/
|
|
43
43
|
export declare const paragraphToListItem: EditorCommand;
|
|
44
|
+
/**
|
|
45
|
+
* 項目の本文より後ろのブロックを、**その位置の項目にする**。包む項目には触れない。
|
|
46
|
+
*
|
|
47
|
+
* 段落にする操作の逆であり、階層を深くするキー(`Tab` / `⌘]` / `⌘→`)と
|
|
48
|
+
* 書式の変換(箇条書きにする)の**どちらもここを通る**。経路ごとに書かない。
|
|
49
|
+
*/
|
|
50
|
+
export declare function blockToListItem(kind: ListKind): EditorCommand;
|
|
44
51
|
/**
|
|
45
52
|
* 吸収の対象。判定が真のときだけ得られる。
|
|
46
53
|
*
|
|
@@ -109,3 +116,18 @@ export declare function correctItemDepth(state: EditorState): Transaction | null
|
|
|
109
116
|
* **対象が項目であることの確かめ**だけである。
|
|
110
117
|
*/
|
|
111
118
|
export declare function moveItemAcrossSection(direction: MoveDirection): EditorCommand;
|
|
119
|
+
/**
|
|
120
|
+
* 覆った最上位の項目を、**その場で**段落へ戻す。
|
|
121
|
+
*
|
|
122
|
+
* 入口は 2 つある——階層を浅くするキー(最上位の項目には浅くする先が無いので段落になる)と、
|
|
123
|
+
* 書式の「段落にする」。**同じことをするので実装も 1 つにする。**
|
|
124
|
+
*/
|
|
125
|
+
export declare function itemToParagraph(state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
|
|
126
|
+
/**
|
|
127
|
+
* カーソルが項目の**本文より後ろ**のブロックにあるとき、そのブロック。
|
|
128
|
+
* 本文(最初のブロック)にいるときは null(対象は項目そのもの)。
|
|
129
|
+
*/
|
|
130
|
+
export declare function blockInsideItem(state: EditorState): {
|
|
131
|
+
node: Node;
|
|
132
|
+
pos: number;
|
|
133
|
+
} | null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { joinTextblockBackward } from 'prosemirror-commands';
|
|
1
|
+
import { joinTextblockBackward, wrapIn } from 'prosemirror-commands';
|
|
2
2
|
import { backspaceCommand, createIndentListCommand, createSplitListCommand, createToggleListCommand, createUnwrapListCommand, doSplitList, findListsRange, isListNode } from 'prosemirror-flat-list';
|
|
3
3
|
import { TextSelection } from 'prosemirror-state';
|
|
4
4
|
import { outlineSchema } from '../../models/document/schema';
|
|
@@ -28,12 +28,25 @@ export const listCommands = {
|
|
|
28
28
|
* 深くできなかった」場合であり、そこで種別を切り替えると項目が解除されてしまう。
|
|
29
29
|
*/
|
|
30
30
|
export const paragraphToListItem = (state, dispatch, view) => {
|
|
31
|
-
if (listCommands.isInList(state))
|
|
32
|
-
return false;
|
|
33
31
|
if (state.selection.$from.parent.type !== outlineSchema.nodes.paragraph)
|
|
34
32
|
return false;
|
|
33
|
+
if (blockInsideItem(state))
|
|
34
|
+
return blockToListItem('bullet')(state, dispatch, view);
|
|
35
|
+
// 項目の本文では何もしない。ここまで来るのは「直前に兄弟がなくて深くできなかった」
|
|
36
|
+
// 場合であり、そこで種別を切り替えると項目が解除される
|
|
37
|
+
if (listCommands.isInList(state))
|
|
38
|
+
return false;
|
|
35
39
|
return listCommands.toggleListKind('bullet')(state, dispatch, view);
|
|
36
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* 項目の本文より後ろのブロックを、**その位置の項目にする**。包む項目には触れない。
|
|
43
|
+
*
|
|
44
|
+
* 段落にする操作の逆であり、階層を深くするキー(`Tab` / `⌘]` / `⌘→`)と
|
|
45
|
+
* 書式の変換(箇条書きにする)の**どちらもここを通る**。経路ごとに書かない。
|
|
46
|
+
*/
|
|
47
|
+
export function blockToListItem(kind) {
|
|
48
|
+
return wrapIn(outlineSchema.nodes.list, { kind });
|
|
49
|
+
}
|
|
37
50
|
/**
|
|
38
51
|
* 項目の本文より後ろにあるブロックを、その項目の外へ出す(段上げ)。
|
|
39
52
|
*
|
|
@@ -509,22 +522,50 @@ function dedentList(state, dispatch) {
|
|
|
509
522
|
const range = selectedListsRange(state);
|
|
510
523
|
if (!range)
|
|
511
524
|
return false;
|
|
525
|
+
// 最上位の項目には浅くする先が無い。**段落へ戻すのは種別の変換そのもの**なので、
|
|
526
|
+
// 書式の「段落にする」と同じ実装を通す(→ `itemToParagraph`)
|
|
527
|
+
if (!isListNode(range.parent))
|
|
528
|
+
return itemToParagraph(state, dispatch);
|
|
512
529
|
if (!dispatch)
|
|
513
530
|
return true;
|
|
514
|
-
const nested = isListNode(range.parent);
|
|
515
531
|
const items = [];
|
|
516
532
|
for (let index = range.startIndex; index < range.endIndex; index += 1) {
|
|
517
533
|
items.push(range.parent.child(index));
|
|
518
534
|
}
|
|
519
535
|
const tr = state.tr;
|
|
520
|
-
const at =
|
|
521
|
-
if (!nested)
|
|
522
|
-
unwrapItems(tr, at, items);
|
|
536
|
+
const at = moveContent(tr, range.start, range.end, afterParent(range));
|
|
523
537
|
// 覆った範囲の先頭からの隔たりを、移り先の同じ隔たりへ置き直す。覆っていない側まで
|
|
524
538
|
// 伸びた選択は、移したブロックの端で止める
|
|
525
539
|
const movedPos = (pos) => {
|
|
526
540
|
const offset = Math.min(Math.max(pos - range.start, 1), range.end - range.start - 1);
|
|
527
|
-
return at +
|
|
541
|
+
return at + offset;
|
|
542
|
+
};
|
|
543
|
+
tr.setSelection(TextSelection.between(tr.doc.resolve(movedPos(state.selection.from)), tr.doc.resolve(movedPos(state.selection.to))));
|
|
544
|
+
dispatch(tr.scrollIntoView());
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* 覆った最上位の項目を、**その場で**段落へ戻す。
|
|
549
|
+
*
|
|
550
|
+
* 入口は 2 つある——階層を浅くするキー(最上位の項目には浅くする先が無いので段落になる)と、
|
|
551
|
+
* 書式の「段落にする」。**同じことをするので実装も 1 つにする。**
|
|
552
|
+
*/
|
|
553
|
+
export function itemToParagraph(state, dispatch) {
|
|
554
|
+
const range = selectedListsRange(state);
|
|
555
|
+
if (!range || isListNode(range.parent))
|
|
556
|
+
return false;
|
|
557
|
+
if (!dispatch)
|
|
558
|
+
return true;
|
|
559
|
+
const items = [];
|
|
560
|
+
for (let index = range.startIndex; index < range.endIndex; index += 1) {
|
|
561
|
+
items.push(range.parent.child(index));
|
|
562
|
+
}
|
|
563
|
+
const tr = state.tr;
|
|
564
|
+
unwrapItems(tr, range.start, items);
|
|
565
|
+
// 項目を解くと入れ口が消えるぶん、隔たりを詰める
|
|
566
|
+
const movedPos = (pos) => {
|
|
567
|
+
const offset = Math.min(Math.max(pos - range.start, 1), range.end - range.start - 1);
|
|
568
|
+
return range.start + unwrappedOffset(items, offset);
|
|
528
569
|
};
|
|
529
570
|
tr.setSelection(TextSelection.between(tr.doc.resolve(movedPos(state.selection.from)), tr.doc.resolve(movedPos(state.selection.to))));
|
|
530
571
|
dispatch(tr.scrollIntoView());
|
|
@@ -596,6 +637,21 @@ function toggleTaskChecked(state, dispatch) {
|
|
|
596
637
|
dispatch(state.tr.setNodeAttribute(item.pos, 'checked', !item.node.attrs.checked));
|
|
597
638
|
return true;
|
|
598
639
|
}
|
|
640
|
+
/**
|
|
641
|
+
* カーソルが項目の**本文より後ろ**のブロックにあるとき、そのブロック。
|
|
642
|
+
* 本文(最初のブロック)にいるときは null(対象は項目そのもの)。
|
|
643
|
+
*/
|
|
644
|
+
export function blockInsideItem(state) {
|
|
645
|
+
const $from = state.selection.$from;
|
|
646
|
+
for (let depth = $from.depth; depth > 0; depth -= 1) {
|
|
647
|
+
if ($from.node(depth).type !== outlineSchema.nodes.list)
|
|
648
|
+
continue;
|
|
649
|
+
if ($from.index(depth) === 0 || depth + 1 > $from.depth)
|
|
650
|
+
return null;
|
|
651
|
+
return { node: $from.node(depth + 1), pos: $from.before(depth + 1) };
|
|
652
|
+
}
|
|
653
|
+
return null;
|
|
654
|
+
}
|
|
599
655
|
function enclosingListItem(state) {
|
|
600
656
|
const $from = state.selection.$from;
|
|
601
657
|
for (let depth = $from.depth; depth > 0; depth -= 1) {
|
|
@@ -7,7 +7,7 @@ import { inTable, liftByContext, moveByContext, sectionCommands, sinkByContext }
|
|
|
7
7
|
import { holdBackspaceAtFrontMatter } from '../commands/frontMatter';
|
|
8
8
|
import { insertLineBreak } from '../commands/lineBreak';
|
|
9
9
|
import { toggleLink } from '../commands/link';
|
|
10
|
-
import { listCommands } from '../commands/list';
|
|
10
|
+
import { blockInsideItem, listCommands } from '../commands/list';
|
|
11
11
|
import { mermaidBlockOnEnter } from '../commands/mermaidBlock';
|
|
12
12
|
import { dissolveHeadingAtStart, insertHeadingBefore, insertSectionAfter, removeEmptyLeadingBlock, removeUnjoinableEmptyBlock, targetSection } from '../commands/section';
|
|
13
13
|
import { requiredSelectionBlock } from '../commands/selectedBlock';
|
|
@@ -24,6 +24,10 @@ import { enterTabEscape } from '../plugins/tabEscape';
|
|
|
24
24
|
*/
|
|
25
25
|
/** カーソルが属する箇条書きの項目の種別。項目の中でなければ null */
|
|
26
26
|
function listKindAt(state) {
|
|
27
|
+
// 項目の本文より後ろのブロックは項目そのものではない。包む項目の種別を名乗ると、
|
|
28
|
+
// そのブロックを項目へ戻す項目がメニューから消える
|
|
29
|
+
if (blockInsideItem(state))
|
|
30
|
+
return null;
|
|
27
31
|
const kind = listCommands.enclosingListItem(state)?.node.attrs.kind;
|
|
28
32
|
return typeof kind === 'string' ? kind : null;
|
|
29
33
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Plugin, type EditorState } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* 編集基盤が焦点を持っているかを state へ写す。
|
|
4
|
+
*
|
|
5
|
+
* **焦点の移動は transaction を伴わない。** decoration が計算し直されるのは state が
|
|
6
|
+
* 更新されたときだけなので、焦点だけを見て見せ方を変える仕組みは、その時点で自分から
|
|
7
|
+
* state を動かさないかぎり画面へ届かない。ここで焦点を受け、印だけの transaction に
|
|
8
|
+
* 写すことで、焦点を見る側は state を読むだけで済む。
|
|
9
|
+
*
|
|
10
|
+
* 保持する値を closure にも置くのは、文書の差し替え(`resetDocument`)で state を
|
|
11
|
+
* 作り直しても焦点は動かないためである。作り直しのたびに「焦点が無い」へ戻すと、
|
|
12
|
+
* その後クリックで中へ入っても焦点の移動が起きず、見せ方が変わらないままになる。
|
|
13
|
+
*/
|
|
14
|
+
export declare function editorFocusPlugin(): Plugin;
|
|
15
|
+
/**
|
|
16
|
+
* 焦点の有無を読む。**無い場合を作らない。** このプラグインを置き忘れた state は
|
|
17
|
+
* 組み立ての誤りであり、「焦点が無い」として読むと、編集中の見せ方が永久に出ない
|
|
18
|
+
* という分かりにくい形で現れる
|
|
19
|
+
*/
|
|
20
|
+
export declare function isEditorFocused(state: EditorState): boolean;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Plugin, PluginKey } from 'prosemirror-state';
|
|
2
|
+
const editorFocusKey = new PluginKey('editorFocus');
|
|
3
|
+
/**
|
|
4
|
+
* 編集基盤が焦点を持っているかを state へ写す。
|
|
5
|
+
*
|
|
6
|
+
* **焦点の移動は transaction を伴わない。** decoration が計算し直されるのは state が
|
|
7
|
+
* 更新されたときだけなので、焦点だけを見て見せ方を変える仕組みは、その時点で自分から
|
|
8
|
+
* state を動かさないかぎり画面へ届かない。ここで焦点を受け、印だけの transaction に
|
|
9
|
+
* 写すことで、焦点を見る側は state を読むだけで済む。
|
|
10
|
+
*
|
|
11
|
+
* 保持する値を closure にも置くのは、文書の差し替え(`resetDocument`)で state を
|
|
12
|
+
* 作り直しても焦点は動かないためである。作り直しのたびに「焦点が無い」へ戻すと、
|
|
13
|
+
* その後クリックで中へ入っても焦点の移動が起きず、見せ方が変わらないままになる。
|
|
14
|
+
*/
|
|
15
|
+
export function editorFocusPlugin() {
|
|
16
|
+
let focused = false;
|
|
17
|
+
return new Plugin({
|
|
18
|
+
key: editorFocusKey,
|
|
19
|
+
state: {
|
|
20
|
+
init: () => focused,
|
|
21
|
+
apply: (tr, current) => tr.getMeta(editorFocusKey) ?? current
|
|
22
|
+
},
|
|
23
|
+
props: {
|
|
24
|
+
handleDOMEvents: {
|
|
25
|
+
focus: (view) => {
|
|
26
|
+
focused = true;
|
|
27
|
+
syncLater(view, true);
|
|
28
|
+
return false;
|
|
29
|
+
},
|
|
30
|
+
blur: (view) => {
|
|
31
|
+
focused = false;
|
|
32
|
+
syncLater(view, false);
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 焦点を state へ写す。**その場では投げず、1 周置く。**
|
|
41
|
+
*
|
|
42
|
+
* 焦点は編集基盤が画面を更新している最中にも動く(DOM へ選択を置くと、その要素へ
|
|
43
|
+
* 焦点が移る)。更新の最中に transaction を投げると更新が入れ子になり、1 回の移動の
|
|
44
|
+
* 結果を見てから判断する仕組み(`requiredSelectionEntry` の持ち越しなど)が、
|
|
45
|
+
* 見るべき transaction を取り違える。
|
|
46
|
+
*/
|
|
47
|
+
function syncLater(view, next) {
|
|
48
|
+
queueMicrotask(() => {
|
|
49
|
+
if (view.isDestroyed)
|
|
50
|
+
return;
|
|
51
|
+
if (isEditorFocused(view.state) === next)
|
|
52
|
+
return;
|
|
53
|
+
view.dispatch(view.state.tr.setMeta(editorFocusKey, next));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 焦点の有無を読む。**無い場合を作らない。** このプラグインを置き忘れた state は
|
|
58
|
+
* 組み立ての誤りであり、「焦点が無い」として読むと、編集中の見せ方が永久に出ない
|
|
59
|
+
* という分かりにくい形で現れる
|
|
60
|
+
*/
|
|
61
|
+
export function isEditorFocused(state) {
|
|
62
|
+
const focused = editorFocusKey.getState(state);
|
|
63
|
+
if (focused === undefined)
|
|
64
|
+
throw new Error('editorFocusPlugin が state に無い');
|
|
65
|
+
return focused;
|
|
66
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Plugin } from 'prosemirror-state';
|
|
2
2
|
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
3
3
|
import { outlineSchema } from '../../models/document/schema';
|
|
4
|
+
import { isEditorFocused } from './editorFocus';
|
|
4
5
|
import { findDateText } from '../../utils/dateText';
|
|
5
6
|
const FRONT_MATTER = outlineSchema.nodes.front_matter;
|
|
6
7
|
/**
|
|
@@ -26,7 +27,7 @@ const FRONT_MATTER = outlineSchema.nodes.front_matter;
|
|
|
26
27
|
export function frontMatterViewPlugin(getDisplay) {
|
|
27
28
|
return new Plugin({
|
|
28
29
|
props: {
|
|
29
|
-
decorations: (state) => decorationsFor(state, getDisplay()),
|
|
30
|
+
decorations: (state) => decorationsFor(state, getDisplay(), isEditorFocused(state)),
|
|
30
31
|
nodeViews: {
|
|
31
32
|
front_matter: (node, view, _getPos, decorations) => new FrontMatterView(node, view, getDisplay, decorations)
|
|
32
33
|
}
|
|
@@ -40,16 +41,19 @@ const RAW_CLASS = 'markdown-editor__front-matter-raw';
|
|
|
40
41
|
/**
|
|
41
42
|
* 編集中のフラグと、日時の置き換えを組み立てる。
|
|
42
43
|
*
|
|
43
|
-
*
|
|
44
|
+
* 「カーソルがフロントマターの中」の判定はここに閉じ、NodeView は印を class へ写すだけに
|
|
44
45
|
* する。日時の置き換えは、その印が付かないあいだ(=読むあいだ)だけ当てる。
|
|
46
|
+
*
|
|
47
|
+
* **焦点を失えばカーソルは消える。** 選択は state に残るが、書き手から見ればそこに
|
|
48
|
+
* カーソルは無く、入力欄の面だけが居座って見える。焦点も条件に入れる。
|
|
45
49
|
*/
|
|
46
|
-
function decorationsFor(state, display) {
|
|
50
|
+
function decorationsFor(state, display, focused) {
|
|
47
51
|
const front = state.doc.child(0);
|
|
48
52
|
if (front.type !== FRONT_MATTER)
|
|
49
53
|
return DecorationSet.empty;
|
|
50
54
|
const { $from, $to } = state.selection;
|
|
51
55
|
const inside = $from.parent.type === FRONT_MATTER && $from.sameParent($to);
|
|
52
|
-
if (inside && display.editable) {
|
|
56
|
+
if (inside && display.editable && focused) {
|
|
53
57
|
return DecorationSet.create(state.doc, [
|
|
54
58
|
Decoration.node($from.before(), $from.after(), {}, { frontMatterEditing: true })
|
|
55
59
|
]);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Plugin } from 'prosemirror-state';
|
|
2
2
|
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
3
3
|
import { outlineSchema } from '../../models/document/schema';
|
|
4
|
+
import { isEditorFocused } from './editorFocus';
|
|
4
5
|
const MERMAID_BLOCK = outlineSchema.nodes.mermaid_block;
|
|
5
6
|
// --- mermaid の遅延読み込み ---
|
|
6
7
|
//
|
|
@@ -39,7 +40,7 @@ export function mermaidLoadStarted() {
|
|
|
39
40
|
export function mermaidViewPlugin(getText) {
|
|
40
41
|
return new Plugin({
|
|
41
42
|
props: {
|
|
42
|
-
decorations: editingDecorations,
|
|
43
|
+
decorations: (state) => editingDecorations(state, isEditorFocused(state)),
|
|
43
44
|
nodeViews: {
|
|
44
45
|
mermaid_block: (node, view, _getPos, decorations) => new MermaidBlockView(node, view, getText, decorations)
|
|
45
46
|
}
|
|
@@ -48,12 +49,16 @@ export function mermaidViewPlugin(getText) {
|
|
|
48
49
|
}
|
|
49
50
|
const EDITING_CLASS = 'markdown-editor__mermaid-block--editing';
|
|
50
51
|
/**
|
|
51
|
-
*
|
|
52
|
+
* 「カーソルがブロックの中」の Mermaid ブロックへ編集中のフラグ(spec のみの装飾)を当てる。
|
|
52
53
|
* 判定はここに閉じ、NodeView は印を class に写すだけにする。
|
|
54
|
+
* **焦点を失えばカーソルは消える。** 選択は state に残るが、書き手から見ればそこに
|
|
55
|
+
* カーソルは無く、図に戻すのが筋である。焦点も条件に入れる。
|
|
53
56
|
* attrs に class を入れると編集基盤が直接 DOM へ当ててしまい、
|
|
54
57
|
* 読み取り専用の判定(NodeView 側)を通れないため、attrs は空にする。
|
|
55
58
|
*/
|
|
56
|
-
function editingDecorations(state) {
|
|
59
|
+
function editingDecorations(state, focused) {
|
|
60
|
+
if (!focused)
|
|
61
|
+
return DecorationSet.empty;
|
|
57
62
|
const { $from, $to } = state.selection;
|
|
58
63
|
if ($from.parent.type !== MERMAID_BLOCK || !$from.sameParent($to))
|
|
59
64
|
return DecorationSet.empty;
|
|
@@ -14,6 +14,7 @@ import { imagePastePlugin } from '../operations/plugins/imagePaste';
|
|
|
14
14
|
import { imageViewPlugin } from '../operations/plugins/imageView';
|
|
15
15
|
import { blockHandlePlugin } from '../operations/plugins/blockHandle';
|
|
16
16
|
import { frontMatterGuardPlugin } from '../operations/plugins/frontMatterGuard';
|
|
17
|
+
import { editorFocusPlugin } from '../operations/plugins/editorFocus';
|
|
17
18
|
import { frontMatterViewPlugin } from '../operations/plugins/frontMatterView';
|
|
18
19
|
import { headingLevelPlugin } from '../operations/plugins/headingLevel';
|
|
19
20
|
import { listStructurePlugin } from '../operations/plugins/listStructure';
|
|
@@ -86,6 +87,8 @@ export function createEditorSession(element, options) {
|
|
|
86
87
|
// `appendTransaction` を回すため、他が足した書き換えもここを通る
|
|
87
88
|
headingLevelPlugin(),
|
|
88
89
|
sectionViewPlugin(),
|
|
90
|
+
// 焦点の有無を state へ写す。見せ方を焦点で変えるもの(下の 2 つ)が読む
|
|
91
|
+
editorFocusPlugin(),
|
|
89
92
|
// NodeView を持つ表示の仕組み。どうしも相互作用しないため、同じ場所へまとめる。
|
|
90
93
|
// 文言は表示を更新するたびに読み直す(差し替えの反映は次の表示更新から)
|
|
91
94
|
frontMatterViewPlugin(() => ({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@14ch/svelte-md-outliner",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "An outliner-style Markdown editor for Svelte 5. Headings form a real section tree you can fold, focus, and re-nest from the keyboard.",
|