@14ch/svelte-md-outliner 0.0.10 → 0.0.12
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 +29 -0
- package/dist/operations/commands/element.js +11 -16
- package/dist/operations/commands/list.d.ts +1 -10
- package/dist/operations/commands/list.js +57 -68
- package/dist/operations/commands/section.d.ts +44 -2
- package/dist/operations/commands/section.js +98 -73
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,35 @@ 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.12] - 2026-09-18
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- Outdenting a top-level list item now turns it into a paragraph where it
|
|
13
|
+
stands. The item used to move to the end of the list first, so the line you
|
|
14
|
+
pressed on jumped away from where you were reading. Turning a line into a
|
|
15
|
+
paragraph from the block-type menu has always left it in place and split the
|
|
16
|
+
list in two; outdenting now does the same. The list does split, and an ordered
|
|
17
|
+
list restarts its numbering after the paragraph — moving the paragraph out of
|
|
18
|
+
the run joins the list again. In exchange, Shift+Tab followed by Tab now
|
|
19
|
+
returns the line to where it was.
|
|
20
|
+
|
|
21
|
+
## [0.0.11] - 2026-09-17
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- Selecting several list items or blocks and moving them up or down now moves
|
|
26
|
+
all of them. Only the item or block holding the cursor moved, so a selection
|
|
27
|
+
could be torn in two — one item crossing into the next section while the rest
|
|
28
|
+
stayed behind. Moving down could also do nothing at all, because whether the
|
|
29
|
+
selection sat at the edge of its run was judged from the wrong end. Every path
|
|
30
|
+
— swapping with a neighbour, moving into the neighbouring list item, and
|
|
31
|
+
crossing a section boundary — now moves the covered range as one.
|
|
32
|
+
- Moving list items up or down now goes by the lines the selection covers, as
|
|
33
|
+
indenting and outdenting already did. A selection that ends at the start of
|
|
34
|
+
the next line only touches that line, but the item there used to move along
|
|
35
|
+
with the rest.
|
|
36
|
+
|
|
8
37
|
## [0.0.10] - 2026-09-15
|
|
9
38
|
|
|
10
39
|
### Changed
|
|
@@ -7,7 +7,7 @@ import { isTabEscaped } from '../plugins/tabEscape';
|
|
|
7
7
|
import { setBlockKind, toggleHeading } from './block';
|
|
8
8
|
import { indentInCodeBlock, outdentInCodeBlock } from './codeBlock';
|
|
9
9
|
import { listCommands, moveItemAcrossSection, paragraphToListItem } from './list';
|
|
10
|
-
import { liftSection, moveBlockAcrossSection, moveSection, sinkSection, targetSection, topLevelBlock } from './section';
|
|
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';
|
|
13
13
|
const n = outlineSchema.nodes;
|
|
@@ -198,30 +198,25 @@ function moveElement(direction) {
|
|
|
198
198
|
* 中で種別によって進み方が変わることであり、規則として説明できない。
|
|
199
199
|
*/
|
|
200
200
|
function moveBlock(direction, target, state, dispatch) {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
|
|
201
|
+
const range = siblingRange(state.doc, target.pos, coveredEnd(state));
|
|
202
|
+
if (!range)
|
|
203
|
+
return false;
|
|
204
|
+
const neighborIndex = direction === 'up' ? range.startIndex - 1 : range.endIndex;
|
|
205
|
+
const neighbor = neighborIndex >= 0 && neighborIndex < range.parent.childCount
|
|
206
|
+
? range.parent.child(neighborIndex)
|
|
206
207
|
: null;
|
|
207
208
|
// 項目の先頭は、その項目の本文である。見出しと同じく、その前へは出られない
|
|
208
209
|
const atRunEdge = !neighbor ||
|
|
209
210
|
neighbor.type === n.section ||
|
|
210
211
|
neighbor.type === n.heading ||
|
|
211
|
-
(
|
|
212
|
+
(range.parent.type === n.list && neighborIndex === 0);
|
|
212
213
|
if (atRunEdge)
|
|
213
|
-
return moveBlockAcrossSection(direction,
|
|
214
|
+
return moveBlockAcrossSection(direction, range, state, dispatch);
|
|
214
215
|
if (!dispatch)
|
|
215
216
|
return true;
|
|
216
|
-
const insertAt = direction === 'up' ? target.pos - neighbor.nodeSize : target.pos + neighbor.nodeSize;
|
|
217
217
|
const tr = state.tr;
|
|
218
|
-
const
|
|
219
|
-
tr
|
|
220
|
-
tr.insert(insertAt, target.node);
|
|
221
|
-
if (target.kind === 'selectedBlock')
|
|
222
|
-
selectBlockAt(tr, insertAt);
|
|
223
|
-
else
|
|
224
|
-
setCursor(tr, insertAt + offsetInBlock);
|
|
218
|
+
const at = moveRange(tr, range, neighborSide(range, neighbor, direction));
|
|
219
|
+
restoreMovedSelection(tr, state, range, at);
|
|
225
220
|
dispatch(tr.scrollIntoView());
|
|
226
221
|
return true;
|
|
227
222
|
}
|
|
@@ -106,15 +106,6 @@ export declare function correctItemDepth(state: EditorState): Transaction | null
|
|
|
106
106
|
* セクションの端にある項目を、隣のセクションへ送り出す。
|
|
107
107
|
*
|
|
108
108
|
* 越え方そのものはブロックと同じ(→ `moveBlockAcrossSection`)。ここが持つのは
|
|
109
|
-
*
|
|
110
|
-
* 境を越えた先で階層をたどり直せないため、越えるのはセクション直下の項目に限る。
|
|
109
|
+
* **対象が項目であることの確かめ**だけである。
|
|
111
110
|
*/
|
|
112
111
|
export declare function moveItemAcrossSection(direction: MoveDirection): EditorCommand;
|
|
113
|
-
/**
|
|
114
|
-
* `from` 以降で、連続する項目が終わるインデックス。
|
|
115
|
-
*
|
|
116
|
-
* **セクションの中身の末尾ではない。** 間に段落や表が挟まっていれば、そこで止まる。飛び越えると、
|
|
117
|
-
* 書き手が見ている「このリスト」の外へ出てしまう。既存の `blockRunEnd` はブロック全体の
|
|
118
|
-
* 連なりを返すもので種別を見ないため流用できない。
|
|
119
|
-
*/
|
|
120
|
-
export declare function listRunEnd(parent: Node, from: number): number;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { joinTextblockBackward } from 'prosemirror-commands';
|
|
2
|
-
import { backspaceCommand, createIndentListCommand,
|
|
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';
|
|
5
5
|
import { moveContent, setCursor } from '../transaction';
|
|
6
|
-
import { coveredEnd, moveBlockAcrossSection,
|
|
6
|
+
import { coveredEnd, moveBlockAcrossSection, moveRange, neighborSide, restoreMovedSelection, siblingRange } from './section';
|
|
7
7
|
export const listCommands = {
|
|
8
8
|
indentList,
|
|
9
9
|
dedentList,
|
|
@@ -341,16 +341,16 @@ function contentlessItem(doc) {
|
|
|
341
341
|
* セクションの端にある項目を、隣のセクションへ送り出す。
|
|
342
342
|
*
|
|
343
343
|
* 越え方そのものはブロックと同じ(→ `moveBlockAcrossSection`)。ここが持つのは
|
|
344
|
-
*
|
|
345
|
-
* 境を越えた先で階層をたどり直せないため、越えるのはセクション直下の項目に限る。
|
|
344
|
+
* **対象が項目であることの確かめ**だけである。
|
|
346
345
|
*/
|
|
347
346
|
export function moveItemAcrossSection(direction) {
|
|
348
|
-
const n = outlineSchema.nodes;
|
|
349
347
|
return (state, dispatch) => {
|
|
350
|
-
const
|
|
351
|
-
if (!
|
|
348
|
+
const range = selectedItemRange(state);
|
|
349
|
+
if (!range)
|
|
352
350
|
return false;
|
|
353
|
-
|
|
351
|
+
// セクション直下の項目でなければ越えない。入れ子の項目は、越えた先で階層を
|
|
352
|
+
// たどり直せない(`moveBlockAcrossSection` が親の種別で弾く)
|
|
353
|
+
return moveBlockAcrossSection(direction, range, state, dispatch);
|
|
354
354
|
};
|
|
355
355
|
}
|
|
356
356
|
/**
|
|
@@ -364,21 +364,26 @@ export function moveItemAcrossSection(direction) {
|
|
|
364
364
|
*。
|
|
365
365
|
*/
|
|
366
366
|
function moveListItem(direction) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
if (!enclosingListItem(state))
|
|
370
|
-
return false;
|
|
371
|
-
const range = findListsRange(state.selection.$from, state.selection.$to);
|
|
367
|
+
return (state, dispatch) => {
|
|
368
|
+
const range = selectedItemRange(state);
|
|
372
369
|
if (!range)
|
|
373
370
|
return false;
|
|
374
371
|
// `endIndex` は範囲の終わりの次を指すため、下へ動かす相手はその位置にいる
|
|
375
|
-
const
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
372
|
+
const neighborIndex = direction === 'up' ? range.startIndex - 1 : range.endIndex;
|
|
373
|
+
const neighbor = neighborIndex >= 0 && neighborIndex < range.parent.childCount
|
|
374
|
+
? range.parent.child(neighborIndex)
|
|
375
|
+
: null;
|
|
376
|
+
// 相手になれるのは項目だけである。段落などを飛び越すのは並べ替えではない
|
|
377
|
+
if (!neighbor || !isListNode(neighbor)) {
|
|
378
|
+
return moveIntoSiblingItem(direction, range, state, dispatch);
|
|
379
|
+
}
|
|
380
|
+
if (!dispatch)
|
|
381
|
+
return true;
|
|
382
|
+
const tr = state.tr;
|
|
383
|
+
const at = moveRange(tr, range, neighborSide(range, neighbor, direction));
|
|
384
|
+
restoreMovedSelection(tr, state, range, at);
|
|
385
|
+
dispatch(tr.scrollIntoView());
|
|
386
|
+
return true;
|
|
382
387
|
};
|
|
383
388
|
}
|
|
384
389
|
/**
|
|
@@ -393,20 +398,20 @@ function moveListItem(direction) {
|
|
|
393
398
|
* 文書順ではまだ先に置ける場所があるのに動かなくなる。行き先は外側をたどって探す。
|
|
394
399
|
* 探索は文書の並びをそのままたどるため、セクションをまたぐ先も同じ経路で見つかる。
|
|
395
400
|
*/
|
|
396
|
-
function moveIntoSiblingItem(direction, state, dispatch) {
|
|
397
|
-
const
|
|
398
|
-
if (!item)
|
|
399
|
-
return false;
|
|
400
|
-
const $item = state.doc.resolve(item.pos);
|
|
401
|
-
const parent = $item.parent;
|
|
401
|
+
function moveIntoSiblingItem(direction, range, state, dispatch) {
|
|
402
|
+
const parent = range.parent;
|
|
402
403
|
// 親が項目でなければ、ここではなくセクションをまたぐ経路の担当
|
|
403
404
|
if (!isListNode(parent))
|
|
404
405
|
return false;
|
|
405
|
-
|
|
406
|
-
|
|
406
|
+
// **動く側の端で見る**——下へなら範囲の末尾、上へなら範囲の先頭が並びの端にいるか
|
|
407
|
+
const atEdge = direction === 'down'
|
|
408
|
+
? range.endIndex === parent.childCount
|
|
409
|
+
: range.startIndex === firstListIndex(parent);
|
|
410
|
+
if (!atEdge)
|
|
407
411
|
return false;
|
|
408
412
|
// 行き先は親と同じ深さ。項目そのものの深さで探すと 1 段深いところへ入ってしまう
|
|
409
|
-
const
|
|
413
|
+
const $range = state.doc.resolve(range.start);
|
|
414
|
+
const host = findHost(state.doc, range, direction, enclosingItemCount($range) - 1);
|
|
410
415
|
if (!host)
|
|
411
416
|
return false;
|
|
412
417
|
if (!dispatch)
|
|
@@ -416,11 +421,8 @@ function moveIntoSiblingItem(direction, state, dispatch) {
|
|
|
416
421
|
? host.pos + 1 + host.node.child(0).nodeSize
|
|
417
422
|
: host.pos + host.node.nodeSize - 1;
|
|
418
423
|
const tr = state.tr;
|
|
419
|
-
const
|
|
420
|
-
tr
|
|
421
|
-
const at = tr.mapping.map(insertAt);
|
|
422
|
-
tr.insert(at, item.node);
|
|
423
|
-
tr.setSelection(TextSelection.near(tr.doc.resolve(at + offsetInItem)));
|
|
424
|
+
const at = moveRange(tr, range, insertAt);
|
|
425
|
+
restoreMovedSelection(tr, state, range, at);
|
|
424
426
|
dispatch(tr.scrollIntoView());
|
|
425
427
|
return true;
|
|
426
428
|
}
|
|
@@ -430,16 +432,15 @@ function moveIntoSiblingItem(direction, state, dispatch) {
|
|
|
430
432
|
* **自分を含む項目は選ばない。** 親も同じ深さにあり、そのまま選ぶと自分の中へ
|
|
431
433
|
* 自分を入れることになる。丸ごと前(後ろ)にあるものだけを行き先とする。
|
|
432
434
|
*/
|
|
433
|
-
function findHost(doc,
|
|
434
|
-
const itemEnd = item.pos + item.node.nodeSize;
|
|
435
|
+
function findHost(doc, span, direction, depth) {
|
|
435
436
|
let found = null;
|
|
436
437
|
const visit = (node, pos) => {
|
|
437
438
|
if (found && direction === 'down')
|
|
438
439
|
return false;
|
|
439
440
|
if (!isListNode(node))
|
|
440
441
|
return true;
|
|
441
|
-
//
|
|
442
|
-
if (pos <
|
|
442
|
+
// 動かす範囲をまたいでいる項目(=先祖)は行き先にならない。中はまだ見る
|
|
443
|
+
if (pos < span.end && pos + node.nodeSize > span.start)
|
|
443
444
|
return true;
|
|
444
445
|
const enclosing = enclosingItemCount(doc.resolve(pos));
|
|
445
446
|
if (enclosing === depth) {
|
|
@@ -451,9 +452,9 @@ function findHost(doc, item, direction, depth) {
|
|
|
451
452
|
};
|
|
452
453
|
// 上へは、直前にあるものが行き先。最後に見つけたものが最も近い
|
|
453
454
|
if (direction === 'down')
|
|
454
|
-
doc.nodesBetween(
|
|
455
|
+
doc.nodesBetween(span.end, doc.content.size, visit);
|
|
455
456
|
else
|
|
456
|
-
doc.nodesBetween(0,
|
|
457
|
+
doc.nodesBetween(0, span.start, visit);
|
|
457
458
|
return found;
|
|
458
459
|
}
|
|
459
460
|
/** その位置より外側にある項目の数(=項目の深さから 1 を引いた数) */
|
|
@@ -491,10 +492,15 @@ function indentList(state, dispatch) {
|
|
|
491
492
|
/**
|
|
492
493
|
* 項目を 1 段浅くする。最上位の項目では箇条書きを解除して段落に戻す。
|
|
493
494
|
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* 1 段浅くなっているので、包み直しは要らない。
|
|
495
|
+
* **入れ子の項目は、持ち上げるのではなく移す。** 対象を元の親の配下すべての後ろへ出す。
|
|
496
|
+
* 持ち上げる変換は文書の並び順を変えないため、対象の後ろに並んでいた兄弟が対象の配下へ
|
|
497
|
+
* 取り込まれてしまう。書き手が触っていない項目の親子関係が黙って変わることになる。
|
|
498
|
+
* 移り終えた時点で深さは 1 段浅くなっているので、包み直しは要らない。
|
|
499
|
+
*
|
|
500
|
+
* **最上位の項目は、その場で解く。** 行き先が無いので動かす必要がない。リストは
|
|
501
|
+
* 押した行で 2 つに割れ、番号付きなら後半の連番は振り直される。それは
|
|
502
|
+
* 「この行を段落にする」(ブロック種別の変換)と同じ結果であり、**同じことをする 2 つの
|
|
503
|
+
* 操作が違う結果を出さない**。割れたリストは、段落を並びの外へ動かせばまた 1 つに戻る。
|
|
498
504
|
*
|
|
499
505
|
* 分岐は**親が項目かどうか**の 1 つだけである。折りたたみ・種別・空かどうかを
|
|
500
506
|
* 分岐条件に持ち込まない。
|
|
@@ -511,7 +517,7 @@ function dedentList(state, dispatch) {
|
|
|
511
517
|
items.push(range.parent.child(index));
|
|
512
518
|
}
|
|
513
519
|
const tr = state.tr;
|
|
514
|
-
const at = moveContent(tr, range.start, range.end,
|
|
520
|
+
const at = nested ? moveContent(tr, range.start, range.end, afterParent(range)) : range.start;
|
|
515
521
|
if (!nested)
|
|
516
522
|
unwrapItems(tr, at, items);
|
|
517
523
|
// 覆った範囲の先頭からの隔たりを、移り先の同じ隔たりへ置き直す。覆っていない側まで
|
|
@@ -528,17 +534,8 @@ function dedentList(state, dispatch) {
|
|
|
528
534
|
function afterParent(range) {
|
|
529
535
|
return range.$from.after(range.depth);
|
|
530
536
|
}
|
|
531
|
-
/** 最上位の項目の行き先。連続する項目の並びの後ろ(→ `listRunEnd`) */
|
|
532
|
-
function afterRun(range) {
|
|
533
|
-
const runEnd = listRunEnd(range.parent, range.endIndex);
|
|
534
|
-
let at = range.end;
|
|
535
|
-
for (let index = range.endIndex; index < runEnd; index += 1) {
|
|
536
|
-
at += range.parent.child(index).nodeSize;
|
|
537
|
-
}
|
|
538
|
-
return at;
|
|
539
|
-
}
|
|
540
537
|
/**
|
|
541
|
-
*
|
|
538
|
+
* 項目を解いて段落へ戻す。配下は 1 段浅い項目として、その段落の後ろへ並ぶ。
|
|
542
539
|
*
|
|
543
540
|
* **後ろの項目から順に解く。** 前から解くと、後ろの項目の位置が解いた分だけずれる。
|
|
544
541
|
*/
|
|
@@ -569,24 +566,16 @@ function unwrappedOffset(items, offset) {
|
|
|
569
566
|
}
|
|
570
567
|
return unwrapped + rest;
|
|
571
568
|
}
|
|
572
|
-
/**
|
|
573
|
-
* `from` 以降で、連続する項目が終わるインデックス。
|
|
574
|
-
*
|
|
575
|
-
* **セクションの中身の末尾ではない。** 間に段落や表が挟まっていれば、そこで止まる。飛び越えると、
|
|
576
|
-
* 書き手が見ている「このリスト」の外へ出てしまう。既存の `blockRunEnd` はブロック全体の
|
|
577
|
-
* 連なりを返すもので種別を見ないため流用できない。
|
|
578
|
-
*/
|
|
579
|
-
export function listRunEnd(parent, from) {
|
|
580
|
-
let index = from;
|
|
581
|
-
while (index < parent.childCount && isListNode(parent.child(index)))
|
|
582
|
-
index += 1;
|
|
583
|
-
return index;
|
|
584
|
-
}
|
|
585
569
|
/** 選択が覆っている項目の範囲。覆ったかどうかの決め方は見出しと共通(→ `coveredEnd`) */
|
|
586
570
|
function selectedListsRange(state) {
|
|
587
571
|
const { $from } = state.selection;
|
|
588
572
|
return findListsRange($from, state.doc.resolve(coveredEnd(state)));
|
|
589
573
|
}
|
|
574
|
+
/** 覆った項目を、動かす範囲へ。中身がすべて項目であることは `findListsRange` が保証する */
|
|
575
|
+
function selectedItemRange(state) {
|
|
576
|
+
const range = selectedListsRange(state);
|
|
577
|
+
return range ? siblingRange(state.doc, range.start, range.end) : null;
|
|
578
|
+
}
|
|
590
579
|
/**
|
|
591
580
|
* 対象の項目を(子項目まで含めて)端から端まで覆う範囲。
|
|
592
581
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Node } from 'prosemirror-model';
|
|
2
2
|
import { type EditorState, type Transaction } from 'prosemirror-state';
|
|
3
3
|
import { type HeadingLevel, type SectionRef } from '../../models/document/document.types';
|
|
4
|
-
import type { EditorCommand,
|
|
4
|
+
import type { EditorCommand, MoveDirection } from '../operations.types';
|
|
5
5
|
/**
|
|
6
6
|
* 見出しを 1 段深くする。直前の兄弟セクションの中身の末尾へ移す。
|
|
7
7
|
*
|
|
@@ -61,7 +61,9 @@ export declare function moveSection(direction: MoveDirection): EditorCommand;
|
|
|
61
61
|
* あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
|
|
62
62
|
* ので、行った先から同じキーで必ず戻れる。
|
|
63
63
|
*/
|
|
64
|
-
export declare function moveBlockAcrossSection(direction: MoveDirection,
|
|
64
|
+
export declare function moveBlockAcrossSection(direction: MoveDirection, range: SiblingRange, state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
|
|
65
|
+
/** 入れ替える相手の向こう側(動かす前の座標) */
|
|
66
|
+
export declare function neighborSide(range: SiblingRange, neighbor: Node, direction: MoveDirection): number;
|
|
65
67
|
/**
|
|
66
68
|
* 同じ階層の見出しをすべてリスト項目へ戻す。
|
|
67
69
|
*
|
|
@@ -192,6 +194,46 @@ export declare function removeUnjoinableEmptyBlock(state: EditorState, dispatch?
|
|
|
192
194
|
* 内容が見出しの中身になるのは、書き手の作った木を変えることにならない。
|
|
193
195
|
*/
|
|
194
196
|
export declare function convertToHeadings(state: EditorState, dispatch: ((tr: Transaction) => void) | undefined): boolean;
|
|
197
|
+
/**
|
|
198
|
+
* 動かす範囲。**同じ親の下に連続して並ぶ兄弟の列**である。
|
|
199
|
+
*
|
|
200
|
+
* 上下移動はセクション・項目・ブロックのいずれでもこの形を動かす。対象の決め方
|
|
201
|
+
* (どこからどこまでが覆われたか)は種別ごとに違うが、**動かし方は 1 つ**である。
|
|
202
|
+
*/
|
|
203
|
+
export interface SiblingRange {
|
|
204
|
+
readonly parent: Node;
|
|
205
|
+
/** 親の位置。親が文書そのものなら -1 */
|
|
206
|
+
readonly parentPos: number;
|
|
207
|
+
readonly startIndex: number;
|
|
208
|
+
readonly endIndex: number;
|
|
209
|
+
readonly start: number;
|
|
210
|
+
readonly end: number;
|
|
211
|
+
readonly nodes: readonly Node[];
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* `from`(動かす先頭の要素が始まる位置)から `to`(覆った終わりの位置)までを、
|
|
215
|
+
* **同じ親の下に連続して並ぶ兄弟の列**へ正規化する。
|
|
216
|
+
*
|
|
217
|
+
* 覆ったものの深さが揃わないときは、揃う深さまで浅い側へ広げる。上下移動は深さを
|
|
218
|
+
* 変えない操作なので、深さの違うものを同時に動かす形が無い。**階層の違う選択のための
|
|
219
|
+
* 特別な規則は持たない**(`dedent-sibling-preservation` Requirement 4.7 と同じ決め方)。
|
|
220
|
+
*/
|
|
221
|
+
export declare function siblingRange(doc: Node, from: number, to: number): SiblingRange | null;
|
|
222
|
+
/**
|
|
223
|
+
* 範囲を `insertAt`(**動かす前の座標**)へ移す。移り先の先頭の位置を返す。
|
|
224
|
+
*
|
|
225
|
+
* 取り除いてから入れ直す。行き先の位置は取り除きを通して写し取るので、呼ぶ側は
|
|
226
|
+
* 前後どちらへ動かすかで座標の取り方を変えなくてよい。
|
|
227
|
+
*/
|
|
228
|
+
export declare function moveRange(tr: Transaction, range: SiblingRange, insertAt: number): number;
|
|
229
|
+
/**
|
|
230
|
+
* 動かした先で、もとの選択を張り直す。
|
|
231
|
+
*
|
|
232
|
+
* 取り除いて入れ直すので位置の写し取りでは追えない。中身は変わらないため、範囲の先頭
|
|
233
|
+
* からの隔たりをそのまま移り先へ当てる。**指した状態(`NodeSelection`)で動かしたときは、
|
|
234
|
+
* 動かした先を指し続ける**(要件 4.5)。
|
|
235
|
+
*/
|
|
236
|
+
export declare function restoreMovedSelection(tr: Transaction, state: EditorState, range: SiblingRange, insertAt: number): void;
|
|
195
237
|
/** ブロックの連なりが始まる位置。セクションは先頭が見出しなので 1 から */
|
|
196
238
|
export declare function blockRunStart(container: Node): number;
|
|
197
239
|
/** ブロックの連なりが終わる位置(最初の子セクションのインデックス) */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { joinTextblockBackward } from 'prosemirror-commands';
|
|
2
|
-
import { Selection, TextSelection } from 'prosemirror-state';
|
|
2
|
+
import { NodeSelection, Selection, TextSelection } from 'prosemirror-state';
|
|
3
3
|
import { MAX_HEADING_LEVEL } from '../../models/document/document.types';
|
|
4
4
|
import { bodyCount, bodyIndex, bodyStart } from '../../models/document/bodySelection';
|
|
5
5
|
import { outlineSchema } from '../../models/document/schema';
|
|
@@ -177,68 +177,28 @@ export function moveSection(direction) {
|
|
|
177
177
|
}
|
|
178
178
|
if (!dispatch)
|
|
179
179
|
return true;
|
|
180
|
-
const insertAt = direction === 'up' ? range.start - neighbor.nodeSize : range.start + neighbor.nodeSize;
|
|
181
180
|
const tr = state.tr;
|
|
182
|
-
tr
|
|
183
|
-
tr
|
|
184
|
-
restoreMovedSelection(tr, state, range, insertAt);
|
|
181
|
+
const at = moveRange(tr, range, neighborSide(range, neighbor, direction));
|
|
182
|
+
restoreMovedSelection(tr, state, range, at);
|
|
185
183
|
dispatch(tr.scrollIntoView());
|
|
186
184
|
return true;
|
|
187
185
|
};
|
|
188
186
|
}
|
|
189
187
|
/**
|
|
190
|
-
*
|
|
188
|
+
* 覆った見出しを、動かす範囲へ正規化する。
|
|
191
189
|
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* `findListsRange`(`NodeRange`)で同じ正規化をしているのに合わせる。**階層の違う選択の
|
|
195
|
-
* ための特別な規則は持たない**(`dedent-sibling-preservation` Requirement 4.7 と同じ決め方)。
|
|
190
|
+
* 対象は覆った見出しのセクションだけである(→ `selectedSections`)。それを
|
|
191
|
+
* `siblingRange` に渡し、同じ親の下に連続して並ぶ兄弟の列にする。
|
|
196
192
|
*/
|
|
197
193
|
function selectedSectionRange(state) {
|
|
198
194
|
const targets = selectedSections(state);
|
|
199
195
|
if (targets.length === 0)
|
|
200
196
|
return null;
|
|
201
197
|
const last = targets[targets.length - 1];
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
depth -= 1;
|
|
207
|
-
const parent = $start.node(depth);
|
|
208
|
-
const startIndex = $start.index(depth);
|
|
209
|
-
const endIndex = $end.indexAfter(depth);
|
|
210
|
-
const start = depth === $start.depth ? $start.pos : $start.before(depth + 1);
|
|
211
|
-
const nodes = [];
|
|
212
|
-
let end = start;
|
|
213
|
-
for (let index = startIndex; index < endIndex; index += 1) {
|
|
214
|
-
const child = parent.child(index);
|
|
215
|
-
if (child.type !== n.section)
|
|
216
|
-
return null;
|
|
217
|
-
nodes.push(child);
|
|
218
|
-
end += child.nodeSize;
|
|
219
|
-
}
|
|
220
|
-
return {
|
|
221
|
-
parent,
|
|
222
|
-
parentPos: depth > 0 ? $start.before(depth) : -1,
|
|
223
|
-
startIndex,
|
|
224
|
-
endIndex,
|
|
225
|
-
start,
|
|
226
|
-
end,
|
|
227
|
-
nodes
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* 動かした先で、もとの選択を張り直す。
|
|
232
|
-
*
|
|
233
|
-
* 取り除いて入れ直すので位置の写し取りでは追えない。中身は変わらないため、範囲の先頭
|
|
234
|
-
* からの隔たりをそのまま移り先へ当てる。
|
|
235
|
-
*/
|
|
236
|
-
function restoreMovedSelection(tr, state, range, insertAt) {
|
|
237
|
-
const size = range.end - range.start;
|
|
238
|
-
const moved = (pos) => insertAt + Math.min(Math.max(pos - range.start, 1), size - 1);
|
|
239
|
-
const $from = tr.doc.resolve(moved(state.selection.from));
|
|
240
|
-
const $to = tr.doc.resolve(moved(state.selection.to));
|
|
241
|
-
tr.setSelection(TextSelection.between($from, $to));
|
|
198
|
+
const range = siblingRange(state.doc, targets[0].pos, last.pos + last.node.nodeSize);
|
|
199
|
+
if (!range)
|
|
200
|
+
return null;
|
|
201
|
+
return range.nodes.every((node) => node.type === n.section) ? range : null;
|
|
242
202
|
}
|
|
243
203
|
/**
|
|
244
204
|
* 親の端まで来た範囲を、親の隣のセクションの中へ入れる。
|
|
@@ -266,9 +226,7 @@ function moveIntoSiblingSection(direction, range, state, dispatch) {
|
|
|
266
226
|
? blockContentEnd(neighbor, neighborPos)
|
|
267
227
|
: neighborPos + neighbor.nodeSize - 1;
|
|
268
228
|
const tr = state.tr;
|
|
269
|
-
tr
|
|
270
|
-
const at = tr.mapping.map(insertAt);
|
|
271
|
-
tr.insert(at, range.nodes);
|
|
229
|
+
const at = moveRange(tr, range, insertAt);
|
|
272
230
|
restoreMovedSelection(tr, state, range, at);
|
|
273
231
|
dispatch(tr.scrollIntoView());
|
|
274
232
|
return true;
|
|
@@ -288,16 +246,18 @@ function moveIntoSiblingSection(direction, range, state, dispatch) {
|
|
|
288
246
|
* あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
|
|
289
247
|
* ので、行った先から同じキーで必ず戻れる。
|
|
290
248
|
*/
|
|
291
|
-
export function moveBlockAcrossSection(direction,
|
|
292
|
-
const
|
|
293
|
-
const container = $block.parent;
|
|
249
|
+
export function moveBlockAcrossSection(direction, range, state, dispatch) {
|
|
250
|
+
const container = range.parent;
|
|
294
251
|
if (container.type !== n.section && container.type !== n.intro_section)
|
|
295
252
|
return false;
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
253
|
+
// まだ同じセクションの中で動けるなら、ここの出番ではない。**動く側の端で見る**——
|
|
254
|
+
// 下へなら範囲の末尾、上へなら範囲の先頭が並びの端にいるかどうかである
|
|
255
|
+
const atEdge = direction === 'down'
|
|
256
|
+
? range.endIndex === blockRunEnd(container)
|
|
257
|
+
: range.startIndex === blockRunStart(container);
|
|
258
|
+
if (!atEdge)
|
|
299
259
|
return false;
|
|
300
|
-
const containerPos =
|
|
260
|
+
const containerPos = range.parentPos;
|
|
301
261
|
const destination = direction === 'down'
|
|
302
262
|
? nextSection(state.doc, container, containerPos)
|
|
303
263
|
: previousContainer(state.doc, containerPos);
|
|
@@ -310,23 +270,22 @@ export function moveBlockAcrossSection(direction, target, state, dispatch) {
|
|
|
310
270
|
? destination.pos + 1 + destination.node.child(0).nodeSize
|
|
311
271
|
: blockContentEnd(destination.node, destination.pos);
|
|
312
272
|
const tr = state.tr;
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
if (
|
|
273
|
+
// 見出しを持たないセクションは中身が空だと成立しない。中身が全部出ていくならまるごと消す
|
|
274
|
+
const emptied = container.type === n.intro_section && range.nodes.length === container.childCount;
|
|
275
|
+
if (emptied)
|
|
316
276
|
tr.delete(containerPos, containerPos + container.nodeSize);
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
tr.delete(target.pos, target.pos + target.node.nodeSize);
|
|
320
|
-
}
|
|
321
|
-
const at = tr.mapping.map(insertAt);
|
|
322
|
-
tr.insert(at, target.node);
|
|
323
|
-
if (target.kind === 'selectedBlock')
|
|
324
|
-
selectBlockAt(tr, at);
|
|
325
277
|
else
|
|
326
|
-
|
|
278
|
+
tr.delete(range.start, range.end);
|
|
279
|
+
const at = tr.mapping.map(insertAt);
|
|
280
|
+
tr.insert(at, range.nodes);
|
|
281
|
+
restoreMovedSelection(tr, state, range, at);
|
|
327
282
|
dispatch(tr.scrollIntoView());
|
|
328
283
|
return true;
|
|
329
284
|
}
|
|
285
|
+
/** 入れ替える相手の向こう側(動かす前の座標) */
|
|
286
|
+
export function neighborSide(range, neighbor, direction) {
|
|
287
|
+
return direction === 'up' ? range.start - neighbor.nodeSize : range.end + neighbor.nodeSize;
|
|
288
|
+
}
|
|
330
289
|
/**
|
|
331
290
|
* 同じ階層の見出しをすべてリスト項目へ戻す。
|
|
332
291
|
*
|
|
@@ -808,7 +767,73 @@ function buildSections(container, startIndex, level, wholeLevel) {
|
|
|
808
767
|
flush();
|
|
809
768
|
return sections;
|
|
810
769
|
}
|
|
811
|
-
|
|
770
|
+
/**
|
|
771
|
+
* `from`(動かす先頭の要素が始まる位置)から `to`(覆った終わりの位置)までを、
|
|
772
|
+
* **同じ親の下に連続して並ぶ兄弟の列**へ正規化する。
|
|
773
|
+
*
|
|
774
|
+
* 覆ったものの深さが揃わないときは、揃う深さまで浅い側へ広げる。上下移動は深さを
|
|
775
|
+
* 変えない操作なので、深さの違うものを同時に動かす形が無い。**階層の違う選択のための
|
|
776
|
+
* 特別な規則は持たない**(`dedent-sibling-preservation` Requirement 4.7 と同じ決め方)。
|
|
777
|
+
*/
|
|
778
|
+
export function siblingRange(doc, from, to) {
|
|
779
|
+
const $start = doc.resolve(from);
|
|
780
|
+
const $end = doc.resolve(Math.max(to, from));
|
|
781
|
+
let depth = $start.depth;
|
|
782
|
+
while (depth > 0 && $end.pos > $start.end(depth))
|
|
783
|
+
depth -= 1;
|
|
784
|
+
const parent = $start.node(depth);
|
|
785
|
+
const startIndex = $start.index(depth);
|
|
786
|
+
const endIndex = Math.max($end.indexAfter(depth), startIndex + 1);
|
|
787
|
+
if (startIndex >= parent.childCount)
|
|
788
|
+
return null;
|
|
789
|
+
const start = depth === $start.depth ? $start.pos : $start.before(depth + 1);
|
|
790
|
+
const nodes = [];
|
|
791
|
+
let end = start;
|
|
792
|
+
for (let index = startIndex; index < endIndex; index += 1) {
|
|
793
|
+
const child = parent.child(index);
|
|
794
|
+
nodes.push(child);
|
|
795
|
+
end += child.nodeSize;
|
|
796
|
+
}
|
|
797
|
+
return {
|
|
798
|
+
parent,
|
|
799
|
+
parentPos: depth > 0 ? $start.before(depth) : -1,
|
|
800
|
+
startIndex,
|
|
801
|
+
endIndex,
|
|
802
|
+
start,
|
|
803
|
+
end,
|
|
804
|
+
nodes
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* 範囲を `insertAt`(**動かす前の座標**)へ移す。移り先の先頭の位置を返す。
|
|
809
|
+
*
|
|
810
|
+
* 取り除いてから入れ直す。行き先の位置は取り除きを通して写し取るので、呼ぶ側は
|
|
811
|
+
* 前後どちらへ動かすかで座標の取り方を変えなくてよい。
|
|
812
|
+
*/
|
|
813
|
+
export function moveRange(tr, range, insertAt) {
|
|
814
|
+
tr.delete(range.start, range.end);
|
|
815
|
+
const at = tr.mapping.map(insertAt);
|
|
816
|
+
tr.insert(at, range.nodes);
|
|
817
|
+
return at;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* 動かした先で、もとの選択を張り直す。
|
|
821
|
+
*
|
|
822
|
+
* 取り除いて入れ直すので位置の写し取りでは追えない。中身は変わらないため、範囲の先頭
|
|
823
|
+
* からの隔たりをそのまま移り先へ当てる。**指した状態(`NodeSelection`)で動かしたときは、
|
|
824
|
+
* 動かした先を指し続ける**(要件 4.5)。
|
|
825
|
+
*/
|
|
826
|
+
export function restoreMovedSelection(tr, state, range, insertAt) {
|
|
827
|
+
if (state.selection instanceof NodeSelection) {
|
|
828
|
+
selectBlockAt(tr, insertAt);
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
const size = range.end - range.start;
|
|
832
|
+
const moved = (pos) => insertAt + Math.min(Math.max(pos - range.start, 1), size - 1);
|
|
833
|
+
const $from = tr.doc.resolve(moved(state.selection.from));
|
|
834
|
+
const $to = tr.doc.resolve(moved(state.selection.to));
|
|
835
|
+
tr.setSelection(TextSelection.between($from, $to));
|
|
836
|
+
}
|
|
812
837
|
/** ブロックの連なりが始まる位置。セクションは先頭が見出しなので 1 から */
|
|
813
838
|
export function blockRunStart(container) {
|
|
814
839
|
return container.type === n.section ? 1 : 0;
|
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.12",
|
|
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.",
|