@14ch/svelte-md-outliner 0.0.9 → 0.0.11

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 CHANGED
@@ -5,6 +5,41 @@ 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.11] - 2026-09-17
9
+
10
+ ### Fixed
11
+
12
+ - Selecting several list items or blocks and moving them up or down now moves
13
+ all of them. Only the item or block holding the cursor moved, so a selection
14
+ could be torn in two — one item crossing into the next section while the rest
15
+ stayed behind. Moving down could also do nothing at all, because whether the
16
+ selection sat at the edge of its run was judged from the wrong end. Every path
17
+ — swapping with a neighbour, moving into the neighbouring list item, and
18
+ crossing a section boundary — now moves the covered range as one.
19
+ - Moving list items up or down now goes by the lines the selection covers, as
20
+ indenting and outdenting already did. A selection that ends at the start of
21
+ the next line only touches that line, but the item there used to move along
22
+ with the rest.
23
+
24
+ ## [0.0.10] - 2026-09-15
25
+
26
+ ### Changed
27
+
28
+ - Moving a block up or down now crosses section boundaries. A paragraph, table,
29
+ quote or code block at the edge of a section used to stop there, while a list
30
+ item in the same place moved on into the neighbouring section. Every block now
31
+ takes the same path — down into the section that comes next in the document, up
32
+ into the end of the one before — and the same key in the opposite direction
33
+ brings it back. Blocks nested inside a list item stay where they are.
34
+
35
+ ### Fixed
36
+
37
+ - Selecting several sections and moving them up or down moves all of them. Only
38
+ the first section moved and the rest stayed behind. The headings the selection
39
+ covers now move as one block, keeping their order and their levels relative to
40
+ each other. When those headings sit at different depths, the range widens to
41
+ the depth that holds them all, the way it already does for list items.
42
+
8
43
  ## [0.0.9] - 2026-09-12
9
44
 
10
45
  ### Changed
@@ -15,8 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15
50
  to show an I-beam and open the built-in menu. They now show the ordinary
16
51
  pointer and leave right-click to the environment's own menu. Clicking there
17
52
  still puts the caret at the nearest position, as before. Quotes, tables and
18
- code blocks count as content in full — their bars, rules and fills read as
19
- part of the block, not as a gap.
53
+ code blocks count as content in full — their bars, rules and fills read as part
54
+ of the block, not as a gap.
20
55
  - Headings now leave 0.75em below them, the same gap paragraphs leave (was
21
56
  0.5em).
22
57
 
@@ -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, 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;
@@ -191,31 +191,32 @@ function moveElement(direction) {
191
191
  *
192
192
  * **相手になれるのはブロックだけである。** 見出しはセクションの先頭にしか置けず、
193
193
  * 子のセクションはブロックより後ろにしか置けない。どちらかに行き当たったら、
194
- * そこがブロックの並びの端であり、文書もカーソルも動かさない(要件 5.2)。
194
+ * そこがブロックの並びの端である。
195
+ *
196
+ * 端まで来たら、**隣のセクションの中へ入る**(→ `moveBlockAcrossSection`)。項目だけが
197
+ * 境を越えられて段落・表・引用が止まるのは、書き手から見て同じ 1 つのアウトラインの
198
+ * 中で種別によって進み方が変わることであり、規則として説明できない。
195
199
  */
196
200
  function moveBlock(direction, target, state, dispatch) {
197
- const $target = state.doc.resolve(target.pos);
198
- const index = $target.index();
199
- const neighborIndex = direction === 'up' ? index - 1 : index + 1;
200
- if (neighborIndex < 0 || neighborIndex >= $target.parent.childCount)
201
- return false;
202
- const neighbor = $target.parent.child(neighborIndex);
203
- if (neighbor.type === n.section || neighbor.type === n.heading)
201
+ const range = siblingRange(state.doc, target.pos, coveredEnd(state));
202
+ if (!range)
204
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)
207
+ : null;
205
208
  // 項目の先頭は、その項目の本文である。見出しと同じく、その前へは出られない
206
- if ($target.parent.type === n.list && neighborIndex === 0)
207
- return false;
209
+ const atRunEdge = !neighbor ||
210
+ neighbor.type === n.section ||
211
+ neighbor.type === n.heading ||
212
+ (range.parent.type === n.list && neighborIndex === 0);
213
+ if (atRunEdge)
214
+ return moveBlockAcrossSection(direction, range, state, dispatch);
208
215
  if (!dispatch)
209
216
  return true;
210
- const insertAt = direction === 'up' ? target.pos - neighbor.nodeSize : target.pos + neighbor.nodeSize;
211
217
  const tr = state.tr;
212
- const offsetInBlock = state.selection.from - target.pos;
213
- tr.delete(target.pos, target.pos + target.node.nodeSize);
214
- tr.insert(insertAt, target.node);
215
- if (target.kind === 'selectedBlock')
216
- selectBlockAt(tr, insertAt);
217
- else
218
- setCursor(tr, insertAt + offsetInBlock);
218
+ const at = moveRange(tr, range, neighborSide(range, neighbor, direction));
219
+ restoreMovedSelection(tr, state, range, at);
219
220
  dispatch(tr.scrollIntoView());
220
221
  return true;
221
222
  }
@@ -105,15 +105,8 @@ export declare function correctItemDepth(state: EditorState): Transaction | null
105
105
  /**
106
106
  * セクションの端にある項目を、隣のセクションへ送り出す。
107
107
  *
108
- * 同じセクションの中でもう動けなくなったときだけ働く。項目のまま移るので、
109
- * 箇条書きの階層は変わらない。
110
- *
111
- * 「セクションの中で止まる」を選ばないのは、書き手から見て見出しと項目が
112
- * ひと続きのアウトラインだからである。下へ押し続けたら下へ進んでほしい。
113
- *
114
- * 行き先は**文書順で隣にあるセクション**であり、同じ階層とは限らない。子セクションが
115
- * あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
116
- * ので、行った先から同じキーで必ず戻れる。
108
+ * 越え方そのものはブロックと同じ(→ `moveBlockAcrossSection`)。ここが持つのは
109
+ * **対象が項目であることの確かめ**だけである。
117
110
  */
118
111
  export declare function moveItemAcrossSection(direction: MoveDirection): EditorCommand;
119
112
  /**
@@ -1,9 +1,9 @@
1
1
  import { joinTextblockBackward } from 'prosemirror-commands';
2
- import { backspaceCommand, createIndentListCommand, createMoveListCommand, createSplitListCommand, createToggleListCommand, createUnwrapListCommand, doSplitList, findListsRange, isListNode } from 'prosemirror-flat-list';
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 { blockContentEnd, blockRunEnd, blockRunStart, coveredEnd, nextSection, previousContainer, topLevelBlock } from './section';
6
+ import { coveredEnd, moveBlockAcrossSection, moveRange, neighborSide, restoreMovedSelection, siblingRange } from './section';
7
7
  export const listCommands = {
8
8
  indentList,
9
9
  dedentList,
@@ -340,51 +340,17 @@ function contentlessItem(doc) {
340
340
  /**
341
341
  * セクションの端にある項目を、隣のセクションへ送り出す。
342
342
  *
343
- * 同じセクションの中でもう動けなくなったときだけ働く。項目のまま移るので、
344
- * 箇条書きの階層は変わらない。
345
- *
346
- * 「セクションの中で止まる」を選ばないのは、書き手から見て見出しと項目が
347
- * ひと続きのアウトラインだからである。下へ押し続けたら下へ進んでほしい。
348
- *
349
- * 行き先は**文書順で隣にあるセクション**であり、同じ階層とは限らない。子セクションが
350
- * あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
351
- * ので、行った先から同じキーで必ず戻れる。
343
+ * 越え方そのものはブロックと同じ(→ `moveBlockAcrossSection`)。ここが持つのは
344
+ * **対象が項目であることの確かめ**だけである。
352
345
  */
353
346
  export function moveItemAcrossSection(direction) {
354
- const n = outlineSchema.nodes;
355
347
  return (state, dispatch) => {
356
- const origin = topLevelBlock(state);
357
- if (!origin || origin.node.type !== n.list)
358
- return false;
359
- // まだ同じセクションの中で動けるなら、ここの出番ではない
360
- const edge = direction === 'down' ? blockRunEnd(origin.container) - 1 : blockRunStart(origin.container);
361
- if (origin.index !== edge)
362
- return false;
363
- const target = direction === 'down'
364
- ? nextSection(state.doc, origin.container, origin.containerPos)
365
- : previousContainer(state.doc, origin.containerPos);
366
- if (!target)
348
+ const range = selectedItemRange(state);
349
+ if (!range)
367
350
  return false;
368
- if (!dispatch)
369
- return true;
370
- // 下へ送るときは見出しの直後、上へ送るときは中身の末尾(子セクションの前)
371
- const insertAt = direction === 'down'
372
- ? target.pos + 1 + target.node.child(0).nodeSize
373
- : blockContentEnd(target.node, target.pos);
374
- const tr = state.tr;
375
- const offsetInItem = state.selection.from - origin.pos;
376
- // 見出しを持たないセクションは中身が空だと成立しない。最後の 1 つならまるごと消す
377
- if (origin.container.type === n.intro_section && origin.container.childCount === 1) {
378
- tr.delete(origin.containerPos, origin.containerPos + origin.container.nodeSize);
379
- }
380
- else {
381
- tr.delete(origin.pos, origin.pos + origin.node.nodeSize);
382
- }
383
- const at = tr.mapping.map(insertAt);
384
- tr.insert(at, origin.node);
385
- setCursor(tr, at + offsetInItem);
386
- dispatch(tr.scrollIntoView());
387
- return true;
351
+ // セクション直下の項目でなければ越えない。入れ子の項目は、越えた先で階層を
352
+ // たどり直せない(`moveBlockAcrossSection` が親の種別で弾く)
353
+ return moveBlockAcrossSection(direction, range, state, dispatch);
388
354
  };
389
355
  }
390
356
  /**
@@ -398,21 +364,26 @@ export function moveItemAcrossSection(direction) {
398
364
  *。
399
365
  */
400
366
  function moveListItem(direction) {
401
- const move = createMoveListCommand(direction);
402
- return (state, dispatch, view) => {
403
- if (!enclosingListItem(state))
404
- return false;
405
- const range = findListsRange(state.selection.$from, state.selection.$to);
367
+ return (state, dispatch) => {
368
+ const range = selectedItemRange(state);
406
369
  if (!range)
407
370
  return false;
408
371
  // `endIndex` は範囲の終わりの次を指すため、下へ動かす相手はその位置にいる
409
- const neighbor = direction === 'up' ? range.startIndex - 1 : range.endIndex;
410
- const hasSibling = neighbor >= 0 &&
411
- neighbor < range.parent.childCount &&
412
- isListNode(range.parent.child(neighbor));
413
- if (hasSibling)
414
- return move(state, dispatch, view);
415
- return moveIntoSiblingItem(direction, state, dispatch);
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;
416
387
  };
417
388
  }
418
389
  /**
@@ -427,20 +398,20 @@ function moveListItem(direction) {
427
398
  * 文書順ではまだ先に置ける場所があるのに動かなくなる。行き先は外側をたどって探す。
428
399
  * 探索は文書の並びをそのままたどるため、セクションをまたぐ先も同じ経路で見つかる。
429
400
  */
430
- function moveIntoSiblingItem(direction, state, dispatch) {
431
- const item = enclosingListItem(state);
432
- if (!item)
433
- return false;
434
- const $item = state.doc.resolve(item.pos);
435
- const parent = $item.parent;
401
+ function moveIntoSiblingItem(direction, range, state, dispatch) {
402
+ const parent = range.parent;
436
403
  // 親が項目でなければ、ここではなくセクションをまたぐ経路の担当
437
404
  if (!isListNode(parent))
438
405
  return false;
439
- const edge = direction === 'down' ? parent.childCount - 1 : firstListIndex(parent);
440
- if (edge < 0 || $item.index() !== edge)
406
+ // **動く側の端で見る**——下へなら範囲の末尾、上へなら範囲の先頭が並びの端にいるか
407
+ const atEdge = direction === 'down'
408
+ ? range.endIndex === parent.childCount
409
+ : range.startIndex === firstListIndex(parent);
410
+ if (!atEdge)
441
411
  return false;
442
412
  // 行き先は親と同じ深さ。項目そのものの深さで探すと 1 段深いところへ入ってしまう
443
- const host = findHost(state.doc, item, direction, enclosingItemCount($item) - 1);
413
+ const $range = state.doc.resolve(range.start);
414
+ const host = findHost(state.doc, range, direction, enclosingItemCount($range) - 1);
444
415
  if (!host)
445
416
  return false;
446
417
  if (!dispatch)
@@ -450,11 +421,8 @@ function moveIntoSiblingItem(direction, state, dispatch) {
450
421
  ? host.pos + 1 + host.node.child(0).nodeSize
451
422
  : host.pos + host.node.nodeSize - 1;
452
423
  const tr = state.tr;
453
- const offsetInItem = state.selection.from - item.pos;
454
- tr.delete(item.pos, item.pos + item.node.nodeSize);
455
- const at = tr.mapping.map(insertAt);
456
- tr.insert(at, item.node);
457
- tr.setSelection(TextSelection.near(tr.doc.resolve(at + offsetInItem)));
424
+ const at = moveRange(tr, range, insertAt);
425
+ restoreMovedSelection(tr, state, range, at);
458
426
  dispatch(tr.scrollIntoView());
459
427
  return true;
460
428
  }
@@ -464,16 +432,15 @@ function moveIntoSiblingItem(direction, state, dispatch) {
464
432
  * **自分を含む項目は選ばない。** 親も同じ深さにあり、そのまま選ぶと自分の中へ
465
433
  * 自分を入れることになる。丸ごと前(後ろ)にあるものだけを行き先とする。
466
434
  */
467
- function findHost(doc, item, direction, depth) {
468
- const itemEnd = item.pos + item.node.nodeSize;
435
+ function findHost(doc, span, direction, depth) {
469
436
  let found = null;
470
437
  const visit = (node, pos) => {
471
438
  if (found && direction === 'down')
472
439
  return false;
473
440
  if (!isListNode(node))
474
441
  return true;
475
- // 自分をまたいでいる項目(=先祖)は行き先にならない。中はまだ見る
476
- if (pos < itemEnd && pos + node.nodeSize > item.pos)
442
+ // 動かす範囲をまたいでいる項目(=先祖)は行き先にならない。中はまだ見る
443
+ if (pos < span.end && pos + node.nodeSize > span.start)
477
444
  return true;
478
445
  const enclosing = enclosingItemCount(doc.resolve(pos));
479
446
  if (enclosing === depth) {
@@ -485,9 +452,9 @@ function findHost(doc, item, direction, depth) {
485
452
  };
486
453
  // 上へは、直前にあるものが行き先。最後に見つけたものが最も近い
487
454
  if (direction === 'down')
488
- doc.nodesBetween(itemEnd, doc.content.size, visit);
455
+ doc.nodesBetween(span.end, doc.content.size, visit);
489
456
  else
490
- doc.nodesBetween(0, item.pos, visit);
457
+ doc.nodesBetween(0, span.start, visit);
491
458
  return found;
492
459
  }
493
460
  /** その位置より外側にある項目の数(=項目の深さから 1 を引いた数) */
@@ -621,6 +588,11 @@ function selectedListsRange(state) {
621
588
  const { $from } = state.selection;
622
589
  return findListsRange($from, state.doc.resolve(coveredEnd(state)));
623
590
  }
591
+ /** 覆った項目を、動かす範囲へ。中身がすべて項目であることは `findListsRange` が保証する */
592
+ function selectedItemRange(state) {
593
+ const range = selectedListsRange(state);
594
+ return range ? siblingRange(state.doc, range.start, range.end) : null;
595
+ }
624
596
  /**
625
597
  * 対象の項目を(子項目まで含めて)端から端まで覆う範囲。
626
598
  *
@@ -41,8 +41,29 @@ export declare function coveredEnd(state: EditorState): number;
41
41
  * 対象になるのは**カーソルが見出し行にあるとき**だけである。中身のどこにいても
42
42
  * 動かせるようにすると、項目や段落を動かすつもりの操作でセクションごと動いて
43
43
  * しまう。「セクションを動かすには、その見出しにカーソルを置く」を規則にする。
44
+ *
45
+ * 選択が複数の見出しにまたがるときは、**覆った見出しをひとまとまりとして動かす**。
46
+ * 1 つずつ動かすと、隣と入れ替えた先が次の対象になり、対象どうしの並びが崩れる。
44
47
  */
45
48
  export declare function moveSection(direction: MoveDirection): EditorCommand;
49
+ /**
50
+ * セクションの端にあるブロックを、隣のセクションへ送り出す。
51
+ *
52
+ * 同じセクションの中でもう動けなくなったときだけ働く。ブロックはそのまま移るので、
53
+ * 種別も箇条書きの階層も変わらない。
54
+ *
55
+ * 「セクションの中で止まる」を選ばないのは、書き手から見て見出しと中身がひと続きの
56
+ * アウトラインだからである。下へ押し続けたら下へ進んでほしい(Decision 22)。**この理由は
57
+ * 種別を選ばない。** 項目だけが越えられて段落・表・引用が止まるのは、内部の構造が
58
+ * そのまま見えているだけで、書き手の意図には対応していない。
59
+ *
60
+ * 行き先は**文書順で隣にあるセクション**であり、同じ階層とは限らない。子セクションが
61
+ * あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
62
+ * ので、行った先から同じキーで必ず戻れる。
63
+ */
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;
46
67
  /**
47
68
  * 同じ階層の見出しをすべてリスト項目へ戻す。
48
69
  *
@@ -173,6 +194,46 @@ export declare function removeUnjoinableEmptyBlock(state: EditorState, dispatch?
173
194
  * 内容が見出しの中身になるのは、書き手の作った木を変えることにならない。
174
195
  */
175
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;
176
237
  /** ブロックの連なりが始まる位置。セクションは先頭が見出しなので 1 から */
177
238
  export declare function blockRunStart(container: Node): number;
178
239
  /** ブロックの連なりが終わる位置(最初の子セクションのインデックス) */
@@ -1,10 +1,10 @@
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';
6
6
  import { sectionTree } from '../../models/document/sectionTree';
7
- import { moveContent, setCursor } from '../transaction';
7
+ import { moveContent, selectBlockAt, setCursor } from '../transaction';
8
8
  const n = outlineSchema.nodes;
9
9
  /**
10
10
  * 見出しを 1 段深くする。直前の兄弟セクションの中身の末尾へ移す。
@@ -158,51 +158,60 @@ export function coveredEnd(state) {
158
158
  * 対象になるのは**カーソルが見出し行にあるとき**だけである。中身のどこにいても
159
159
  * 動かせるようにすると、項目や段落を動かすつもりの操作でセクションごと動いて
160
160
  * しまう。「セクションを動かすには、その見出しにカーソルを置く」を規則にする。
161
+ *
162
+ * 選択が複数の見出しにまたがるときは、**覆った見出しをひとまとまりとして動かす**。
163
+ * 1 つずつ動かすと、隣と入れ替えた先が次の対象になり、対象どうしの並びが崩れる。
161
164
  */
162
165
  export function moveSection(direction) {
163
166
  return (state, dispatch) => {
164
- const target = targetSection(state);
165
- if (!target)
167
+ const range = selectedSectionRange(state);
168
+ if (!range)
166
169
  return false;
167
- const $target = state.doc.resolve(target.pos);
168
- const index = $target.index();
169
- const neighborIndex = direction === 'up' ? index - 1 : index + 1;
170
- const neighbor = neighborIndex >= 0 && neighborIndex < $target.parent.childCount
171
- ? $target.parent.child(neighborIndex)
170
+ const neighborIndex = direction === 'up' ? range.startIndex - 1 : range.endIndex;
171
+ const neighbor = neighborIndex >= 0 && neighborIndex < range.parent.childCount
172
+ ? range.parent.child(neighborIndex)
172
173
  : null;
173
174
  // 同じ親の中に兄弟がいなければ、隣のセクションの中へ入る
174
175
  if (!neighbor || neighbor.type !== n.section) {
175
- return moveIntoSiblingSection(direction, state, dispatch);
176
+ return moveIntoSiblingSection(direction, range, state, dispatch);
176
177
  }
177
178
  if (!dispatch)
178
179
  return true;
179
- const size = target.node.nodeSize;
180
- const insertAt = direction === 'up' ? target.pos - neighbor.nodeSize : target.pos + neighbor.nodeSize;
181
180
  const tr = state.tr;
182
- const offsetInSection = state.selection.from - target.pos;
183
- tr.delete(target.pos, target.pos + size);
184
- tr.insert(insertAt, target.node);
185
- setCursor(tr, insertAt + offsetInSection);
181
+ const at = moveRange(tr, range, neighborSide(range, neighbor, direction));
182
+ restoreMovedSelection(tr, state, range, at);
186
183
  dispatch(tr.scrollIntoView());
187
184
  return true;
188
185
  };
189
186
  }
190
187
  /**
191
- * 親の端まで来たセクションを、親の隣のセクションの中へ入れる。
188
+ * 覆った見出しを、動かす範囲へ正規化する。
189
+ *
190
+ * 対象は覆った見出しのセクションだけである(→ `selectedSections`)。それを
191
+ * `siblingRange` に渡し、同じ親の下に連続して並ぶ兄弟の列にする。
192
+ */
193
+ function selectedSectionRange(state) {
194
+ const targets = selectedSections(state);
195
+ if (targets.length === 0)
196
+ return null;
197
+ const last = targets[targets.length - 1];
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;
202
+ }
203
+ /**
204
+ * 親の端まで来た範囲を、親の隣のセクションの中へ入れる。
192
205
  *
193
206
  * 上下移動は「文書順で次(前)にある、同じ深さの置ける位置」へ進む。同じ親の中に
194
207
  * 兄弟がいなければ、次に置けるのは隣のセクションの中である。下へなら子セクションの
195
208
  * 先頭、上へなら末尾。深さは変わらないため、昇格・降格は起きない。
196
209
  */
197
- function moveIntoSiblingSection(direction, state, dispatch) {
198
- const target = targetSection(state);
199
- if (!target)
200
- return false;
201
- const $target = state.doc.resolve(target.pos);
202
- const parent = $target.parent;
210
+ function moveIntoSiblingSection(direction, range, state, dispatch) {
211
+ const parent = range.parent;
203
212
  if (parent.type !== n.section)
204
213
  return false;
205
- const parentPos = $target.before();
214
+ const parentPos = range.parentPos;
206
215
  const $parent = state.doc.resolve(parentPos);
207
216
  const neighborIndex = direction === 'down' ? $parent.index() + 1 : $parent.index() - 1;
208
217
  if (neighborIndex < 0 || neighborIndex >= $parent.parent.childCount)
@@ -217,14 +226,66 @@ function moveIntoSiblingSection(direction, state, dispatch) {
217
226
  ? blockContentEnd(neighbor, neighborPos)
218
227
  : neighborPos + neighbor.nodeSize - 1;
219
228
  const tr = state.tr;
220
- const offsetInSection = state.selection.from - target.pos;
221
- tr.delete(target.pos, target.pos + target.node.nodeSize);
229
+ const at = moveRange(tr, range, insertAt);
230
+ restoreMovedSelection(tr, state, range, at);
231
+ dispatch(tr.scrollIntoView());
232
+ return true;
233
+ }
234
+ /**
235
+ * セクションの端にあるブロックを、隣のセクションへ送り出す。
236
+ *
237
+ * 同じセクションの中でもう動けなくなったときだけ働く。ブロックはそのまま移るので、
238
+ * 種別も箇条書きの階層も変わらない。
239
+ *
240
+ * 「セクションの中で止まる」を選ばないのは、書き手から見て見出しと中身がひと続きの
241
+ * アウトラインだからである。下へ押し続けたら下へ進んでほしい(Decision 22)。**この理由は
242
+ * 種別を選ばない。** 項目だけが越えられて段落・表・引用が止まるのは、内部の構造が
243
+ * そのまま見えているだけで、書き手の意図には対応していない。
244
+ *
245
+ * 行き先は**文書順で隣にあるセクション**であり、同じ階層とは限らない。子セクションが
246
+ * あればそこへ入り、自分のセクションの先頭からはその親へ出る。上下は互いに逆をたどる
247
+ * ので、行った先から同じキーで必ず戻れる。
248
+ */
249
+ export function moveBlockAcrossSection(direction, range, state, dispatch) {
250
+ const container = range.parent;
251
+ if (container.type !== n.section && container.type !== n.intro_section)
252
+ return false;
253
+ // まだ同じセクションの中で動けるなら、ここの出番ではない。**動く側の端で見る**——
254
+ // 下へなら範囲の末尾、上へなら範囲の先頭が並びの端にいるかどうかである
255
+ const atEdge = direction === 'down'
256
+ ? range.endIndex === blockRunEnd(container)
257
+ : range.startIndex === blockRunStart(container);
258
+ if (!atEdge)
259
+ return false;
260
+ const containerPos = range.parentPos;
261
+ const destination = direction === 'down'
262
+ ? nextSection(state.doc, container, containerPos)
263
+ : previousContainer(state.doc, containerPos);
264
+ if (!destination)
265
+ return false;
266
+ if (!dispatch)
267
+ return true;
268
+ // 下へ送るときは見出しの直後、上へ送るときは中身の末尾(子セクションの前)
269
+ const insertAt = direction === 'down'
270
+ ? destination.pos + 1 + destination.node.child(0).nodeSize
271
+ : blockContentEnd(destination.node, destination.pos);
272
+ const tr = state.tr;
273
+ // 見出しを持たないセクションは中身が空だと成立しない。中身が全部出ていくならまるごと消す
274
+ const emptied = container.type === n.intro_section && range.nodes.length === container.childCount;
275
+ if (emptied)
276
+ tr.delete(containerPos, containerPos + container.nodeSize);
277
+ else
278
+ tr.delete(range.start, range.end);
222
279
  const at = tr.mapping.map(insertAt);
223
- tr.insert(at, target.node);
224
- setCursor(tr, at + offsetInSection);
280
+ tr.insert(at, range.nodes);
281
+ restoreMovedSelection(tr, state, range, at);
225
282
  dispatch(tr.scrollIntoView());
226
283
  return true;
227
284
  }
285
+ /** 入れ替える相手の向こう側(動かす前の座標) */
286
+ export function neighborSide(range, neighbor, direction) {
287
+ return direction === 'up' ? range.start - neighbor.nodeSize : range.end + neighbor.nodeSize;
288
+ }
228
289
  /**
229
290
  * 同じ階層の見出しをすべてリスト項目へ戻す。
230
291
  *
@@ -706,7 +767,73 @@ function buildSections(container, startIndex, level, wholeLevel) {
706
767
  flush();
707
768
  return sections;
708
769
  }
709
- // --- 並びを歩く照会(`list.ts` と `block.ts` が横に参照する) ---
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
+ }
710
837
  /** ブロックの連なりが始まる位置。セクションは先頭が見出しなので 1 から */
711
838
  export function blockRunStart(container) {
712
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.9",
3
+ "version": "0.0.11",
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.",