5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-15 02:21:42 +00:00
panel/app/javascript/editor/utils.ts

102 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-04-28 18:48:50 +00:00
import { Editor } from "editor/editor";
2021-04-28 18:48:50 +00:00
export const blockNames = [
"paragraph",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"unordered_list",
"ordered_list",
];
export const markNames = [
"bold",
"italic",
"deleted",
"underline",
"sub",
"super",
"mark",
"link",
"small",
];
export const parentBlockNames = ["left", "center", "right"];
2021-04-28 18:48:50 +00:00
export function moveChildren(from: Element, to: Element, toRef: Node | null) {
while (from.firstChild) to.insertBefore(from.firstChild, toRef);
}
2021-04-28 18:48:50 +00:00
export function isDirectChild(node: Node, supposedChild: Node): boolean {
for (const child of node.childNodes) {
if (child == supposedChild) return true;
}
return false;
}
2021-04-28 18:48:50 +00:00
export function safeGetSelection(editor: Editor): Selection | null {
const sel = window.getSelection();
if (!sel) return null;
// XXX: no damos la selección si esta fuera o _es_ el contentEl, ¿quizás
// deberíamos mostrar un error?
if (
!editor.contentEl.contains(sel.anchorNode) ||
!editor.contentEl.contains(sel.focusNode) ||
sel.anchorNode == editor.contentEl ||
sel.focusNode == editor.contentEl
)
return null;
return sel;
}
2021-04-28 18:48:50 +00:00
export function safeGetRangeAt(selection: Selection, num = 0): Range | null {
try {
return selection.getRangeAt(num);
} catch (error) {
return null;
}
}
interface SplitNode {
2021-04-28 18:48:50 +00:00
range: Range;
node: Node;
}
2021-04-28 18:48:50 +00:00
export function splitNode(node: Element, range: Range): [SplitNode, SplitNode] {
const [left, right] = [
{ range: document.createRange(), node: node.cloneNode(false) },
{ range: document.createRange(), node: node.cloneNode(false) },
];
2021-04-28 18:48:50 +00:00
if (node.firstChild) left.range.setStartBefore(node.firstChild);
left.range.setEnd(range.startContainer, range.startOffset);
left.range.surroundContents(left.node);
2021-04-28 18:48:50 +00:00
right.range.setStart(range.endContainer, range.endOffset);
if (node.lastChild) right.range.setEndAfter(node.lastChild);
right.range.surroundContents(right.node);
2021-04-28 18:48:50 +00:00
if (!node.parentElement)
throw new Error("No pude separar los nodos por que no tiene parentNode");
2021-04-28 18:48:50 +00:00
moveChildren(node, node.parentElement, node);
node.parentElement.removeChild(node);
2021-04-28 18:48:50 +00:00
return [left, right];
}
2021-02-13 01:14:36 +00:00
2021-04-28 18:48:50 +00:00
export function setAuxiliaryToolbar(
editor: Editor,
bar: HTMLElement | null
): void {
for (const { parentEl } of Object.values(editor.toolbar.auxiliary)) {
delete parentEl.dataset.editorAuxiliaryActive;
}
if (bar) bar.dataset.editorAuxiliaryActive = "active";
2021-02-13 01:14:36 +00:00
}
2021-04-28 18:48:50 +00:00
export function clearSelected(editor: Editor): void {
const selectedEl = editor.contentEl.querySelector("[data-editor-selected]");
if (selectedEl) delete (selectedEl as HTMLElement).dataset.editorSelected;
}