add node view to details experiment

This commit is contained in:
Hans Pagel 2021-02-10 16:37:00 +01:00
parent 8c3b35d0c0
commit de234adb8b
2 changed files with 54 additions and 2 deletions

View File

@ -11,7 +11,7 @@ export default Node.create({
content: 'inline*',
group: 'block',
// group: 'block',
defaultOptions: <DetailsSummaryOptions>{
HTMLAttributes: {},

View File

@ -32,7 +32,7 @@ export default Node.create({
}
return {
open: '',
open: 'open',
}
},
},
@ -48,4 +48,56 @@ export default Node.create({
renderHTML({ HTMLAttributes }) {
return ['details', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
addNodeView() {
return ({
node,
HTMLAttributes,
getPos,
editor,
}) => {
const { view } = editor
const item = document.createElement('details')
item.addEventListener('click', event => {
// @ts-ignore
const { open } = event.target.parentElement as HTMLElement
// @ts-ignore
const { localName } = event.target
if (typeof getPos === 'function' && localName === 'summary') {
view.dispatch(view.state.tr.setNodeMarkup(getPos(), undefined, {
open: !open,
}))
editor.commands.focus()
}
})
if (node.attrs.open) {
item.setAttribute('open', 'open')
}
Object.entries(HTMLAttributes).forEach(([key, value]) => {
item.setAttribute(key, value)
})
return {
dom: item,
contentDOM: item,
update: updatedNode => {
if (updatedNode.type !== this.type) {
return false
}
if (updatedNode.attrs.open) {
item.setAttribute('open', 'open')
} else {
item.removeAttribute('open')
}
return true
},
}
}
},
})