@14ch/svelte-md-outliner 0.0.10 → 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,22 @@ 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
+
8
24
  ## [0.0.10] - 2026-09-15
9
25
 
10
26
  ### 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 $target = state.doc.resolve(target.pos);
202
- const index = $target.index();
203
- const neighborIndex = direction === 'up' ? index - 1 : index + 1;
204
- const neighbor = neighborIndex >= 0 && neighborIndex < $target.parent.childCount
205
- ? $target.parent.child(neighborIndex)
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
- ($target.parent.type === n.list && neighborIndex === 0);
212
+ (range.parent.type === n.list && neighborIndex === 0);
212
213
  if (atRunEdge)
213
- return moveBlockAcrossSection(direction, target, state, dispatch);
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 offsetInBlock = state.selection.from - target.pos;
219
- tr.delete(target.pos, target.pos + target.node.nodeSize);
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,8 +106,7 @@ export declare function correctItemDepth(state: EditorState): Transaction | null
106
106
  * セクションの端にある項目を、隣のセクションへ送り出す。
107
107
  *
108
108
  * 越え方そのものはブロックと同じ(→ `moveBlockAcrossSection`)。ここが持つのは
109
- * **対象が項目であることの確かめ**だけである。入れ子の項目は `topLevelBlock` が弾く。
110
- * 境を越えた先で階層をたどり直せないため、越えるのはセクション直下の項目に限る。
109
+ * **対象が項目であることの確かめ**だけである。
111
110
  */
112
111
  export declare function moveItemAcrossSection(direction: MoveDirection): EditorCommand;
113
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 { coveredEnd, moveBlockAcrossSection, topLevelBlock } from './section';
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
- * **対象が項目であることの確かめ**だけである。入れ子の項目は `topLevelBlock` が弾く。
345
- * 境を越えた先で階層をたどり直せないため、越えるのはセクション直下の項目に限る。
344
+ * **対象が項目であることの確かめ**だけである。
346
345
  */
347
346
  export function moveItemAcrossSection(direction) {
348
- const n = outlineSchema.nodes;
349
347
  return (state, dispatch) => {
350
- const origin = topLevelBlock(state);
351
- if (!origin || origin.node.type !== n.list)
348
+ const range = selectedItemRange(state);
349
+ if (!range)
352
350
  return false;
353
- return moveBlockAcrossSection(direction, { kind: 'listItem', node: origin.node, pos: origin.pos }, state, dispatch);
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
- const move = createMoveListCommand(direction);
368
- return (state, dispatch, view) => {
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 neighbor = direction === 'up' ? range.startIndex - 1 : range.endIndex;
376
- const hasSibling = neighbor >= 0 &&
377
- neighbor < range.parent.childCount &&
378
- isListNode(range.parent.child(neighbor));
379
- if (hasSibling)
380
- return move(state, dispatch, view);
381
- 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;
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 item = enclosingListItem(state);
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
- const edge = direction === 'down' ? parent.childCount - 1 : firstListIndex(parent);
406
- 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)
407
411
  return false;
408
412
  // 行き先は親と同じ深さ。項目そのものの深さで探すと 1 段深いところへ入ってしまう
409
- 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);
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 offsetInItem = state.selection.from - item.pos;
420
- tr.delete(item.pos, item.pos + item.node.nodeSize);
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, item, direction, depth) {
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 < itemEnd && pos + node.nodeSize > item.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(itemEnd, doc.content.size, visit);
455
+ doc.nodesBetween(span.end, doc.content.size, visit);
455
456
  else
456
- doc.nodesBetween(0, item.pos, visit);
457
+ doc.nodesBetween(0, span.start, visit);
457
458
  return found;
458
459
  }
459
460
  /** その位置より外側にある項目の数(=項目の深さから 1 を引いた数) */
@@ -587,6 +588,11 @@ function selectedListsRange(state) {
587
588
  const { $from } = state.selection;
588
589
  return findListsRange($from, state.doc.resolve(coveredEnd(state)));
589
590
  }
591
+ /** 覆った項目を、動かす範囲へ。中身がすべて項目であることは `findListsRange` が保証する */
592
+ function selectedItemRange(state) {
593
+ const range = selectedListsRange(state);
594
+ return range ? siblingRange(state.doc, range.start, range.end) : null;
595
+ }
590
596
  /**
591
597
  * 対象の項目を(子項目まで含めて)端から端まで覆う範囲。
592
598
  *
@@ -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, ElementTarget, MoveDirection } from '../operations.types';
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, target: ElementTarget, state: EditorState, dispatch?: (tr: Transaction) => void): boolean;
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.delete(range.start, range.end);
183
- tr.insert(insertAt, range.nodes);
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 $start = state.doc.resolve(targets[0].pos);
203
- const $end = state.doc.resolve(last.pos + last.node.nodeSize);
204
- let depth = $start.depth;
205
- while (depth > 0 && $end.pos > $start.end(depth))
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.delete(range.start, range.end);
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, target, state, dispatch) {
292
- const $block = state.doc.resolve(target.pos);
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
- const edge = direction === 'down' ? blockRunEnd(container) - 1 : blockRunStart(container);
298
- if ($block.index() !== edge)
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 = $block.before();
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
- const offsetInBlock = state.selection.from - target.pos;
314
- // 見出しを持たないセクションは中身が空だと成立しない。最後の 1 つならまるごと消す
315
- if (container.type === n.intro_section && container.childCount === 1) {
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
- setCursor(tr, at + offsetInBlock);
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
- // --- 並びを歩く照会(`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
+ }
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.10",
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.",