33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
![]() |
import { Editor } from 'editor/editor'
|
||
|
import { EditorNode } from 'editor/types'
|
||
|
import { markNames, setAuxiliaryToolbar } from 'editor/utils'
|
||
|
|
||
|
export const link: EditorNode = {
|
||
|
selector: 'a',
|
||
|
allowedChildren: [...markNames.filter(n => n !== 'link'), 'text'],
|
||
|
handleEmpty: 'remove',
|
||
|
create: () => document.createElement('a'),
|
||
|
onClick (editor, el) {
|
||
|
if (!(el instanceof HTMLAnchorElement))
|
||
|
throw new Error('oh no')
|
||
|
el.dataset.editorSelected = ''
|
||
|
editor.toolbar.auxiliary.link.urlEl.value = el.href
|
||
|
setAuxiliaryToolbar(editor, editor.toolbar.auxiliary.link.parentEl)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function setupAuxiliaryToolbar (editor: Editor): void {
|
||
|
editor.toolbar.auxiliary.link.urlEl.addEventListener('input', event => {
|
||
|
const url = editor.toolbar.auxiliary.link.urlEl.value
|
||
|
const selectedEl = editor.contentEl
|
||
|
.querySelector<HTMLAnchorElement>('a[data-editor-selected]')
|
||
|
if (!selectedEl)
|
||
|
throw new Error('No pude encontrar el link para setear el enlace')
|
||
|
|
||
|
selectedEl.href = url
|
||
|
})
|
||
|
editor.toolbar.auxiliary.link.urlEl.addEventListener('keydown', event => {
|
||
|
if (event.keyCode == 13) event.preventDefault()
|
||
|
})
|
||
|
}
|