2021-02-26 13:21:53 +00:00
|
|
|
import { storeContent, restoreContent, forgetContent } from 'editor/storage'
|
2021-03-22 20:54:26 +00:00
|
|
|
import { isDirectChild, moveChildren, safeGetSelection, safeGetRangeAt, setAuxiliaryToolbar, parentBlockNames, clearSelected } from 'editor/utils'
|
2021-02-12 22:18:48 +00:00
|
|
|
import { types, getValidChildren, getType } from 'editor/types'
|
2021-02-12 15:57:23 +00:00
|
|
|
import { setupButtons as setupMarksButtons } from 'editor/types/marks'
|
2021-02-12 22:18:48 +00:00
|
|
|
import { setupButtons as setupBlocksButtons } from 'editor/types/blocks'
|
|
|
|
import { setupButtons as setupParentBlocksButtons } from 'editor/types/parentBlocks'
|
2021-02-13 01:14:36 +00:00
|
|
|
import { setupAuxiliaryToolbar as setupLinkAuxiliaryToolbar } from 'editor/types/link'
|
2021-02-14 16:01:41 +00:00
|
|
|
import {
|
|
|
|
setupAuxiliaryToolbar as setupMultimediaAuxiliaryToolbar,
|
|
|
|
setupButtons as setupMultimediaButtons,
|
|
|
|
} from 'editor/types/multimedia'
|
2021-02-14 00:07:38 +00:00
|
|
|
import { setupAuxiliaryToolbar as setupMarkAuxiliaryToolbar } from 'editor/types/mark'
|
2021-02-12 15:57:23 +00:00
|
|
|
|
|
|
|
// Esta funcion corrije errores que pueden haber como:
|
|
|
|
// * que un nodo que no tiene 'text' permitido no tenga children (se les
|
|
|
|
// inserta un allowedChildren[0])
|
2021-02-25 22:29:14 +00:00
|
|
|
// * que haya una imágen sin <figure> o que no esté como bloque (se ponen
|
2021-02-12 15:57:23 +00:00
|
|
|
// después del bloque en el que están como bloque de por si)
|
2021-02-12 20:33:23 +00:00
|
|
|
// * convierte <i> y <b> en <em> y <strong>
|
2021-02-12 15:57:23 +00:00
|
|
|
// Lo hace para que siga la estructura del documento y que no se borren por
|
|
|
|
// cleanContent luego.
|
|
|
|
function fixContent (editor: Editor, node: Element = editor.contentEl): void {
|
2021-02-12 21:01:22 +00:00
|
|
|
if (node.tagName === 'SCRIPT' || node.tagName === 'STYLE') {
|
|
|
|
node.parentElement?.removeChild(node)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:33:23 +00:00
|
|
|
if (node.tagName === 'I') {
|
|
|
|
const el = document.createElement('em')
|
|
|
|
moveChildren(node, el, null)
|
|
|
|
node.parentElement?.replaceChild(el, node)
|
|
|
|
node = el
|
|
|
|
}
|
|
|
|
if (node.tagName === 'B') {
|
|
|
|
const el = document.createElement('strong')
|
|
|
|
moveChildren(node, el, null)
|
|
|
|
node.parentElement?.replaceChild(el, node)
|
|
|
|
node = el
|
|
|
|
}
|
|
|
|
|
2021-02-25 22:29:14 +00:00
|
|
|
if (node instanceof HTMLImageElement) {
|
|
|
|
node.dataset.multimediaInner = ''
|
|
|
|
const figureEl = types.multimedia.create(editor)
|
|
|
|
|
|
|
|
let targetEl = node.parentElement
|
|
|
|
if (!targetEl) throw new Error('No encontré lx objetivo')
|
|
|
|
while (true) {
|
|
|
|
const type = getType(targetEl)
|
|
|
|
if (!type) throw new Error('lx objetivo tiene tipo')
|
|
|
|
if (type.type.allowedChildren.includes('multimedia')) break
|
|
|
|
if (!targetEl.parentElement) throw new Error('No encontré lx objetivo')
|
|
|
|
targetEl = targetEl.parentElement
|
|
|
|
}
|
|
|
|
|
|
|
|
let parentEl = [...targetEl.childNodes].find(
|
|
|
|
el => el.contains(node)
|
|
|
|
)
|
|
|
|
if (!parentEl) throw new Error('no encontré lx pariente')
|
2021-02-26 17:54:12 +00:00
|
|
|
targetEl.insertBefore(figureEl, parentEl)
|
2021-02-25 22:29:14 +00:00
|
|
|
|
|
|
|
const innerEl = figureEl.querySelector('[data-multimedia-inner]')
|
|
|
|
if (!innerEl) throw new Error('Raro.')
|
|
|
|
figureEl.replaceChild(node, innerEl)
|
|
|
|
|
|
|
|
node = figureEl
|
|
|
|
}
|
|
|
|
|
2021-02-12 15:57:23 +00:00
|
|
|
const _type = getType(node)
|
|
|
|
if (!_type) return
|
|
|
|
|
|
|
|
const { typeName, type } = _type
|
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
if (type.allowedChildren !== 'ignore-children') {
|
|
|
|
const sel = safeGetSelection(editor)
|
|
|
|
const range = sel && safeGetRangeAt(sel)
|
|
|
|
|
|
|
|
if (getValidChildren(node, type).length == 0) {
|
|
|
|
if (typeof type.handleEmpty !== 'string') {
|
|
|
|
const el = type.handleEmpty.create(editor)
|
|
|
|
// mover cosas que pueden haber
|
|
|
|
// por ejemplo: cuando convertís a un <ul>, queda texto fuera del li que
|
|
|
|
// creamos acá
|
|
|
|
moveChildren(node, el, null)
|
|
|
|
node.appendChild(el)
|
|
|
|
if (range?.intersectsNode(node))
|
|
|
|
sel?.collapse(el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const child of node.childNodes) {
|
|
|
|
if (!(child instanceof Element)) continue
|
|
|
|
fixContent(editor, child)
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Esta funcion hace que los elementos del editor sigan la estructura.
|
|
|
|
// TODO: nos falta borrar atributos (style, y básicamente cualquier otra cosa)
|
|
|
|
// Edge cases:
|
|
|
|
// * no borramos los <br> por que se requieren para que los navegadores
|
|
|
|
// funcionen bien al escribir. no se deberían mostrar de todas maneras
|
|
|
|
function cleanContent (editor: Editor, node: Element = editor.contentEl): void {
|
|
|
|
const _type = getType(node)
|
|
|
|
if (!_type) {
|
|
|
|
node.parentElement?.removeChild(node)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const { type } = _type
|
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
if (type.allowedChildren !== 'ignore-children') {
|
|
|
|
for (const child of node.childNodes) {
|
|
|
|
if (child.nodeType === Node.TEXT_NODE
|
|
|
|
&& !type.allowedChildren.includes('text')
|
|
|
|
) {
|
|
|
|
node.removeChild(child)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
if (!(child instanceof Element)) continue
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
const childType = getType(child)
|
|
|
|
if (childType?.typeName === 'br') continue
|
|
|
|
if (!childType || !type.allowedChildren.includes(childType.typeName)) {
|
|
|
|
// XXX: esto extrae las cosas de adentro para que no sea destructivo
|
|
|
|
moveChildren(child, node, child)
|
|
|
|
node.removeChild(child)
|
|
|
|
return
|
|
|
|
}
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
cleanContent(editor, child)
|
|
|
|
}
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
// solo contar children válido para ese nodo
|
|
|
|
const validChildrenLength = getValidChildren(node, type).length
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-02-14 16:01:41 +00:00
|
|
|
const sel = safeGetSelection(editor)
|
|
|
|
const range = sel && safeGetRangeAt(sel)
|
|
|
|
if (type.handleEmpty === 'remove'
|
|
|
|
&& validChildrenLength == 0
|
|
|
|
//&& (!range || !range.intersectsNode(node))
|
|
|
|
) {
|
|
|
|
node.parentNode?.removeChild(node)
|
|
|
|
return
|
|
|
|
}
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function routine (editor: Editor): void {
|
|
|
|
try {
|
|
|
|
fixContent(editor)
|
|
|
|
cleanContent(editor)
|
|
|
|
storeContent(editor)
|
|
|
|
|
|
|
|
editor.htmlEl.value = editor.contentEl.innerHTML
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Hubo un problema corriendo la rutina', editor, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:14:36 +00:00
|
|
|
export interface Editor {
|
|
|
|
editorEl: HTMLElement,
|
|
|
|
toolbarEl: HTMLElement,
|
|
|
|
toolbar: {
|
|
|
|
auxiliary: {
|
|
|
|
mark: {
|
|
|
|
parentEl: HTMLElement,
|
|
|
|
colorEl: HTMLInputElement,
|
|
|
|
},
|
|
|
|
multimedia: {
|
|
|
|
parentEl: HTMLElement,
|
|
|
|
fileEl: HTMLInputElement,
|
|
|
|
uploadEl: HTMLButtonElement,
|
|
|
|
altEl: HTMLInputElement,
|
2021-02-14 16:01:41 +00:00
|
|
|
removeEl: HTMLButtonElement,
|
2021-02-13 01:14:36 +00:00
|
|
|
},
|
|
|
|
link: {
|
|
|
|
parentEl: HTMLElement,
|
|
|
|
urlEl: HTMLInputElement,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
contentEl: HTMLElement,
|
|
|
|
wordAlertEl: HTMLElement,
|
|
|
|
htmlEl: HTMLTextAreaElement,
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSel<T extends Element>(parentEl: HTMLElement, selector: string): T {
|
|
|
|
const el = parentEl.querySelector<T>(selector)
|
|
|
|
if (!el) throw new Error(`No pude encontrar un componente \`${selector}\``)
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
|
2021-02-12 15:57:23 +00:00
|
|
|
function setupEditor (editorEl: HTMLElement): void {
|
|
|
|
// XXX: ¡Esto afecta a todo el documento! ¿Quizás usar un iframe para el editor?
|
|
|
|
document.execCommand('defaultParagraphSeparator', false, 'p')
|
|
|
|
|
|
|
|
const editor: Editor = {
|
|
|
|
editorEl,
|
2021-02-13 01:14:36 +00:00
|
|
|
toolbarEl: getSel(editorEl, '.editor-toolbar'),
|
|
|
|
toolbar: {
|
|
|
|
auxiliary: {
|
|
|
|
mark: {
|
|
|
|
parentEl: getSel(editorEl, '[data-editor-auxiliary=mark]'),
|
|
|
|
colorEl: getSel(editorEl, '[data-editor-auxiliary=mark] [name=mark-color]'),
|
|
|
|
},
|
|
|
|
multimedia: {
|
|
|
|
parentEl: getSel(editorEl, '[data-editor-auxiliary=multimedia]'),
|
|
|
|
fileEl: getSel(editorEl, '[data-editor-auxiliary=multimedia] [name=multimedia-file]'),
|
|
|
|
uploadEl: getSel(editorEl, '[data-editor-auxiliary=multimedia] [name=multimedia-file-upload]'),
|
|
|
|
altEl: getSel(editorEl, '[data-editor-auxiliary=multimedia] [name=multimedia-alt]'),
|
2021-02-14 16:01:41 +00:00
|
|
|
removeEl: getSel(editorEl, '[data-editor-auxiliary=multimedia] [name=multimedia-remove]'),
|
2021-02-13 01:14:36 +00:00
|
|
|
},
|
|
|
|
link: {
|
|
|
|
parentEl: getSel(editorEl, '[data-editor-auxiliary=link]'),
|
|
|
|
urlEl: getSel(editorEl, '[data-editor-auxiliary=link] [name=link-url]'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
contentEl: getSel(editorEl, '.editor-content'),
|
|
|
|
wordAlertEl: getSel(editorEl, '.editor-aviso-word'),
|
|
|
|
htmlEl: getSel(editorEl, 'textarea'),
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
console.debug('iniciando editor', editor)
|
|
|
|
|
|
|
|
// Recuperar el contenido si hay algo guardado, si tuviéramos un campo
|
|
|
|
// de última edición podríamos saber si el artículo fue editado
|
|
|
|
// después o la versión local es la última.
|
|
|
|
//
|
|
|
|
// TODO: Preguntar si se lo quiere recuperar.
|
|
|
|
restoreContent(editor)
|
|
|
|
|
|
|
|
// Word alert
|
|
|
|
editor.contentEl.addEventListener('paste', () => {
|
|
|
|
editor.wordAlertEl.style.display = 'block'
|
|
|
|
})
|
|
|
|
|
|
|
|
// Setup routine listeners
|
|
|
|
const observer = new MutationObserver(() => routine(editor))
|
2021-02-13 01:14:36 +00:00
|
|
|
observer.observe(editor.contentEl, {
|
2021-02-12 15:57:23 +00:00
|
|
|
childList: true,
|
|
|
|
attributes: true,
|
|
|
|
subtree: true,
|
|
|
|
characterData: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
document.addEventListener("selectionchange", () => routine(editor))
|
|
|
|
|
2021-02-13 01:14:36 +00:00
|
|
|
// Capture onClick
|
|
|
|
editor.contentEl.addEventListener('click', event => {
|
|
|
|
const target = event.target! as Element
|
|
|
|
const type = getType(target)
|
|
|
|
if (!type || !type.type.onClick) {
|
|
|
|
setAuxiliaryToolbar(editor, null)
|
2021-03-22 20:54:26 +00:00
|
|
|
clearSelected(editor)
|
2021-02-13 01:14:36 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
type.type.onClick(editor, target)
|
|
|
|
return false
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
// Clean seleted
|
|
|
|
const selectedEl = editor.contentEl.querySelector('[data-editor-selected]')
|
|
|
|
if (selectedEl) delete (selectedEl as HTMLElement).dataset.editorSelected
|
|
|
|
|
2021-02-12 15:57:23 +00:00
|
|
|
// Setup botones
|
|
|
|
setupMarksButtons(editor)
|
|
|
|
setupBlocksButtons(editor)
|
2021-02-12 22:18:48 +00:00
|
|
|
setupParentBlocksButtons(editor)
|
2021-02-14 16:01:41 +00:00
|
|
|
setupMultimediaButtons(editor)
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-02-13 01:14:36 +00:00
|
|
|
setupLinkAuxiliaryToolbar(editor)
|
2021-02-14 16:01:41 +00:00
|
|
|
setupMultimediaAuxiliaryToolbar(editor)
|
2021-02-14 00:07:38 +00:00
|
|
|
setupMarkAuxiliaryToolbar(editor)
|
2021-02-13 01:14:36 +00:00
|
|
|
|
2021-02-12 15:57:23 +00:00
|
|
|
// Finally...
|
|
|
|
routine(editor)
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("turbolinks:load", () => {
|
2021-02-26 13:21:53 +00:00
|
|
|
const flash = document.querySelector<HTMLElement>('.js-flash')
|
|
|
|
|
|
|
|
if (flash) {
|
2021-03-23 14:35:49 +00:00
|
|
|
const keys = JSON.parse(flash.dataset.keys || '[]')
|
2021-02-26 13:21:53 +00:00
|
|
|
|
|
|
|
switch (flash.dataset.target) {
|
|
|
|
case 'editor':
|
|
|
|
switch (flash.dataset.action) {
|
|
|
|
case 'forget-content':
|
|
|
|
keys.forEach(forgetContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:14:36 +00:00
|
|
|
for (const editorEl of document.querySelectorAll<HTMLElement>('.editor[data-editor]')) {
|
|
|
|
try {
|
|
|
|
setupEditor(editorEl)
|
|
|
|
} catch (error) {
|
|
|
|
// TODO: mostrar error
|
|
|
|
console.error('no se pudo iniciar el editor, error completo', error)
|
|
|
|
}
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
})
|