add basic node interface

This commit is contained in:
Philipp Kühn 2020-10-12 10:32:54 +02:00
parent 8324f57528
commit 41d68ec859
4 changed files with 38 additions and 13 deletions

View File

@ -5,6 +5,7 @@ export { Editor, Command, CommandsSpec }
export { default as ComponentRenderer } from './src/ComponentRenderer'
export { default as Extension } from './src/Extension'
export { default as Node } from './src/Node'
export * from './src/Node'
export { default as Mark } from './src/Mark'
export { Extensions } from './src/types'

View File

@ -32,9 +32,20 @@
// }
// }
import { DOMOutputSpec, DOMOutputSpecArray } from 'prosemirror-model'
import Extension from './Extension'
export default class Node<Options = {}> extends Extension<Options> {
export interface INode {
type: string
topNode: boolean
group: string
content: string
createAttributes(): any
parseHTML(): any
renderHTML(props: number): DOMOutputSpec
}
export default class Node<Options = {}> extends Extension<Options> implements INode {
type = 'node'
@ -53,7 +64,7 @@ export default class Node<Options = {}> extends Extension<Options> {
}
renderHTML() {
return []
return null
}
}

View File

@ -14,15 +14,18 @@ export default function getSchema(extensions: Extensions): Schema {
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as Node[]
const markExtensions = extensions.filter(extension => extension.type === 'mark') as Mark[]
const nodes = Object.fromEntries(nodeExtensions.map(node => {
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
const schema: NodeSpec = {
content: extension.content,
group: extension.group,
parseDOM: extension.parseHTML(),
toDOM: node => extension.renderHTML({ node, attributes: { class: 'hee' } }),
}
return [
node.name,
{
content: node.content,
group: node.group,
parseDOM: node.parseHTML(),
toDOM: node.renderHTML,
} as unknown as NodeSpec,
extension.name,
schema,
]
}))

View File

@ -1,4 +1,5 @@
import { Command, Node } from '@tiptap/core'
import { Command, Node, INode } from '@tiptap/core'
import { DOMOutputSpecArray } from 'prosemirror-model'
// import ParagraphComponent from './paragraph.vue'
// export type ParagraphCommand = () => Command
@ -28,7 +29,7 @@ import { Command, Node } from '@tiptap/core'
// }))
// .create()
export default class Paragraph extends Node {
export default class Paragraph extends Node implements INode {
name = 'paragraph'
@ -36,6 +37,15 @@ export default class Paragraph extends Node {
content = 'inline*'
createAttributes() {
return {
// default rendering
class: {
default: 'jooo',
},
}
}
parseHTML() {
return [
{ tag: 'p' },
@ -43,7 +53,7 @@ export default class Paragraph extends Node {
}
renderHTML() {
return ['p', 0]
return ['p', 0] as const
}
}