2021-02-12 15:57:23 +00:00
|
|
|
import { Editor } from 'editor/editor'
|
|
|
|
|
2021-02-12 22:18:48 +00:00
|
|
|
export const blockNames = ['paragraph', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'unordered_list', 'ordered_list']
|
2021-03-26 15:31:36 +00:00
|
|
|
export const markNames = ['bold', 'italic', 'deleted', 'underline', 'sub', 'super', 'mark', 'link', 'small']
|
2021-02-12 22:18:48 +00:00
|
|
|
export const parentBlockNames = ['left', 'center', 'right']
|
|
|
|
|
2021-02-12 15:57:23 +00:00
|
|
|
export function moveChildren (from: Element, to: Element, toRef: Node | null) {
|
|
|
|
while (from.firstChild) to.insertBefore(from.firstChild, toRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isDirectChild (node: Node, supposedChild: Node): boolean {
|
|
|
|
for (const child of node.childNodes) {
|
|
|
|
if (child == supposedChild) return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
export function safeGetRangeAt (selection: Selection, num = 0): Range | null {
|
|
|
|
try {
|
|
|
|
return selection.getRangeAt(num)
|
|
|
|
} catch (error) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SplitNode {
|
|
|
|
range: Range,
|
|
|
|
node: Node,
|
|
|
|
}
|
|
|
|
|
|
|
|
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) },
|
|
|
|
]
|
|
|
|
|
|
|
|
if (node.firstChild) left.range.setStartBefore(node.firstChild)
|
|
|
|
left.range.setEnd(range.startContainer, range.startOffset)
|
|
|
|
left.range.surroundContents(left.node)
|
|
|
|
|
|
|
|
right.range.setStart(range.endContainer, range.endOffset)
|
|
|
|
if (node.lastChild) right.range.setEndAfter(node.lastChild)
|
|
|
|
right.range.surroundContents(right.node)
|
|
|
|
|
|
|
|
if (!node.parentElement)
|
|
|
|
throw new Error('No pude separar los nodos por que no tiene parentNode')
|
|
|
|
|
|
|
|
moveChildren(node, node.parentElement, node)
|
|
|
|
node.parentElement.removeChild(node)
|
|
|
|
|
|
|
|
return [left, right]
|
|
|
|
}
|
2021-02-13 01:14:36 +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-03-22 20:54:26 +00:00
|
|
|
export function clearSelected (editor: Editor): void {
|
|
|
|
const selectedEl = editor.contentEl.querySelector('[data-editor-selected]')
|
|
|
|
if (selectedEl) delete (selectedEl as HTMLElement).dataset.editorSelected
|
|
|
|
}
|