2021-02-12 15:57:23 +00:00
|
|
|
import { Editor } from 'editor/editor'
|
2021-02-12 22:18:48 +00:00
|
|
|
import {
|
|
|
|
safeGetSelection, safeGetRangeAt,
|
|
|
|
moveChildren,
|
2021-03-27 18:44:24 +00:00
|
|
|
markNames, blockNames, parentBlockNames,
|
2021-02-12 22:18:48 +00:00
|
|
|
} from 'editor/utils'
|
2021-03-27 18:44:24 +00:00
|
|
|
import { EditorNode, getType, getValidParentInSelection } from 'editor/types'
|
2021-02-12 15:57:23 +00:00
|
|
|
|
|
|
|
export interface EditorBlock extends EditorNode {
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeBlock (tag: string): EditorBlock {
|
|
|
|
return {
|
|
|
|
selector: tag,
|
|
|
|
allowedChildren: [...markNames, 'text'],
|
|
|
|
handleEmpty: 'do-nothing',
|
2021-02-12 22:18:48 +00:00
|
|
|
create: () => document.createElement(tag),
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const li: EditorBlock = makeBlock('li')
|
|
|
|
|
|
|
|
// XXX: si agregás algo acá, agregalo a blockNames
|
|
|
|
// (y probablemente le quieras hacer un botón en app/views/posts/attributes/_content.haml)
|
|
|
|
export const blocks: { [propName: string]: EditorBlock } = {
|
|
|
|
paragraph: makeBlock('p'),
|
|
|
|
h1: makeBlock('h1'),
|
|
|
|
h2: makeBlock('h2'),
|
|
|
|
h3: makeBlock('h3'),
|
|
|
|
h4: makeBlock('h4'),
|
|
|
|
h5: makeBlock('h5'),
|
|
|
|
h6: makeBlock('h6'),
|
|
|
|
unordered_list: {
|
|
|
|
...makeBlock('ul'),
|
|
|
|
allowedChildren: ['li'],
|
|
|
|
handleEmpty: li,
|
|
|
|
},
|
|
|
|
ordered_list: {
|
|
|
|
...makeBlock('ol'),
|
|
|
|
allowedChildren: ['li'],
|
|
|
|
handleEmpty: li,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupButtons (editor: Editor): void {
|
|
|
|
for (const [ name, type ] of Object.entries(blocks)) {
|
|
|
|
const buttonEl = editor.toolbarEl.querySelector(`[data-editor-button="block-${name}"]`)
|
|
|
|
if (!buttonEl) continue
|
|
|
|
buttonEl.addEventListener("click", event => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2021-03-27 18:44:24 +00:00
|
|
|
const list = getValidParentInSelection({ editor, type: name })
|
2021-02-12 15:57:23 +00:00
|
|
|
|
2021-03-27 18:44:24 +00:00
|
|
|
// No borrar cosas como multimedia
|
|
|
|
if (blockNames.indexOf(getType(list[1])!.typeName) === -1) {
|
|
|
|
return
|
2021-02-12 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 18:44:24 +00:00
|
|
|
let replacementType = list[1].matches(type.selector)
|
2021-02-12 15:57:23 +00:00
|
|
|
? blocks.paragraph
|
|
|
|
: type
|
|
|
|
|
2021-02-13 01:14:36 +00:00
|
|
|
const el = replacementType.create(editor)
|
2021-03-22 20:54:26 +00:00
|
|
|
replacementType.onClick && replacementType.onClick(editor, el)
|
2021-03-27 18:44:24 +00:00
|
|
|
moveChildren(list[1], el, null)
|
|
|
|
list[0].replaceChild(el, list[1])
|
|
|
|
window.getSelection()?.collapse(el)
|
2021-02-12 15:57:23 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|