All files / mutxt-react/src/MuTxt/behavior blockOps.ts

90.47% Statements 38/42
64.28% Branches 9/14
100% Functions 4/4
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x 1x 1x   1x 2x 2x 2x 2x 1x 1x     1x 3x 3x 3x 3x 3x 3x 3x 2x 2x     1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x    
import {Editor, Path, Transforms} from 'slate';
import {getCurrentBlockEntry} from '../behavior';
import {cloneBinary} from '@jsonjoy.com/util/lib/json-clone/cloneBinary';
 
export const moveBlockUp = (editor: Editor): boolean => {
  const entry = getCurrentBlockEntry(editor);
  Iif (!entry) return false;
  const [, path] = entry;
  if (!Path.hasPrevious(path)) return false;
  Transforms.moveNodes(editor, {at: path, to: Path.previous(path)});
  return true;
};
 
export const moveBlockDown = (editor: Editor): boolean => {
  const entry = getCurrentBlockEntry(editor);
  Iif (!entry) return false;
  const [, path] = entry;
  const [parent] = Editor.parent(editor, path);
  const lastIndex = path[path.length - 1];
  const siblingCount = (parent as {children?: unknown[]}).children?.length ?? 0;
  if (lastIndex >= siblingCount - 1) return false;
  Transforms.moveNodes(editor, {at: path, to: Path.next(path)});
  return true;
};
 
export const duplicateBlock = (editor: Editor): boolean => {
  const entry = getCurrentBlockEntry(editor);
  Iif (!entry) return false;
  const [block, path] = entry;
  Transforms.insertNodes(editor, cloneBinary(block), {at: Path.next(path)});
  return true;
};
 
export const deleteBlock = (editor: Editor): boolean => {
  const entry = getCurrentBlockEntry(editor);
  Iif (!entry) return false;
  const [, path] = entry;
  Transforms.removeNodes(editor, {at: path});
  return true;
};