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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 3x 3x 3x 9x 3x 7x 3x 3x 2x 2x 2x 2x 3x 3x 3x 3x 3x 6x 3x 3x 2x 2x 2x 6x 2x 3x 1x 1x 1x 1x 1x 1x 1x 3x 2x 2x 2x 2x 2x 1x 2x 3x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 3x 2x 3x 3x 1x 1x 1x 1x 3x 1x 1x 3x 12x 12x 32x 32x 18x 12x 42x 37x 12x 1x 1x 12x 12x 12x | import {Editor, Element as SlateElement, Node, Path, Transforms} from 'slate';
import {insertVoidBlock} from './voidInsert';
import type {CustomElement, CustomText, MathElement, MathInlineElement} from '../types';
export const isMathBlock = (node: unknown): node is MathElement =>
SlateElement.isElement(node) && (node as MathElement).type === 'math';
export const isMathInline = (node: unknown): node is MathInlineElement =>
SlateElement.isElement(node) && (node as MathInlineElement).type === 'math-inline';
const createParagraphElement = (): CustomElement => ({
type: 'p',
children: [{text: ''}] as CustomText[],
});
const createMathBlockElement = (thingId: string, caption?: string): MathElement => {
const element: MathElement = {
type: 'math',
'@thing': thingId,
children: [{text: ''}],
};
const trimmed = caption?.trim();
if (trimmed) element.caption = trimmed;
return element;
};
const createMathInlineElement = (thingId: string): MathInlineElement => ({
type: 'math-inline',
'@thing': thingId,
children: [{text: ''}],
});
export const getActiveMathBlockEntry = (editor: Editor): [MathElement, Path] | null => {
const {selection} = editor;
Iif (!selection) return null;
const match = Editor.above(editor, {
at: Editor.unhangRange(editor, selection),
match: (node) => isMathBlock(node),
});
return (match as [MathElement, Path] | undefined) ?? null;
};
export const getActiveMathInlineEntry = (editor: Editor): [MathInlineElement, Path] | null => {
const {selection} = editor;
Iif (!selection) return null;
const [match] = Editor.nodes(editor, {
at: Editor.unhangRange(editor, selection),
match: (node) => isMathInline(node),
});
return (match as [MathInlineElement, Path] | undefined) ?? null;
};
export const insertParagraphNearActiveMathBlock = (
editor: Editor,
position: 'above' | 'below' = 'below',
): Path | null => {
const entry = getActiveMathBlockEntry(editor);
Iif (!entry) return null;
const [, path] = entry;
const targetPath = position === 'above' ? path : Path.next(path);
Transforms.insertNodes(editor, createParagraphElement(), {at: targetPath});
Transforms.select(editor, Editor.start(editor, targetPath));
return targetPath;
};
export const updateMathBlockCaption = (editor: Editor, path: Path, caption?: string): boolean => {
Iif (!Node.has(editor, path)) return false;
const node = Node.get(editor, path);
Iif (!isMathBlock(node)) return false;
const trimmed = caption?.trim();
if (trimmed) Transforms.setNodes(editor, {caption: trimmed} as Partial<MathElement>, {at: path});
else Transforms.unsetNodes(editor, 'caption', {at: path});
return true;
};
export const removeMathBlockAtPath = (editor: Editor, path: Path): boolean => {
Iif (!Node.has(editor, path)) return false;
const node = Node.get(editor, path);
Iif (!isMathBlock(node)) return false;
Transforms.removeNodes(editor, {at: path});
return true;
};
export const removeMathInlineAtPath = (editor: Editor, path: Path): boolean => {
Iif (!Node.has(editor, path)) return false;
const node = Node.get(editor, path);
Iif (!isMathInline(node)) return false;
Transforms.removeNodes(editor, {at: path});
return true;
};
export const insertMathBlock = (editor: Editor, thingId: string, caption?: string): MathElement | null =>
insertVoidBlock(editor, createMathBlockElement(thingId, caption));
export const insertEmptyMathBlock = (mutxt: {editor: Editor; things: {add: (t: any) => string}}): string | null => {
const id = mutxt.things.add({'@type': 'math', val: '', lang: 'latex'});
const inserted = insertMathBlock(mutxt.editor, id);
return inserted ? id : null;
};
export const insertMathInline = (editor: Editor, thingId: string): MathInlineElement | null => {
const node = createMathInlineElement(thingId);
Transforms.insertNodes(editor, node);
Transforms.move(editor, {distance: 1, unit: 'offset'});
return node;
};
export type OpenInlineMathEdit = (element: MathInlineElement, path: Path) => void;
const tryOpenInlineMathEdit = (editor: Editor): boolean => {
const hook = (editor as any).onOpenInlineMathEdit as OpenInlineMathEdit | undefined;
Eif (!hook) return false;
const entry = getActiveMathInlineEntry(editor);
if (!entry) return false;
hook(entry[0], entry[1]);
return true;
};
export const withMath = <T extends Editor>(editor: T): T => {
const {isVoid, isInline, insertBreak, insertSoftBreak, insertText} = editor;
editor.isVoid = (element) => {
const type = (element as any).type;
if (type === 'math' || type === 'math-inline') return true;
return isVoid(element);
};
editor.isInline = (element) => {
if ((element as any).type === 'math-inline') return true;
return isInline(element);
};
editor.insertBreak = () => {
Iif (tryOpenInlineMathEdit(editor)) return;
Eif (insertParagraphNearActiveMathBlock(editor, 'below')) return;
insertBreak();
};
editor.insertSoftBreak = () => {
if (tryOpenInlineMathEdit(editor)) return;
if (insertParagraphNearActiveMathBlock(editor, 'above')) return;
insertSoftBreak();
};
editor.insertText = (text) => {
if (text === ' ' && tryOpenInlineMathEdit(editor)) return;
if (text && insertParagraphNearActiveMathBlock(editor, 'below')) {
insertText(text);
return;
}
insertText(text);
};
return editor;
};
|