mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-27 23:15:15 +08:00
add node view to details experiment
This commit is contained in:
parent
8c3b35d0c0
commit
de234adb8b
@ -11,7 +11,7 @@ export default Node.create({
|
||||
|
||||
content: 'inline*',
|
||||
|
||||
group: 'block',
|
||||
// group: 'block',
|
||||
|
||||
defaultOptions: <DetailsSummaryOptions>{
|
||||
HTMLAttributes: {},
|
||||
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user