refactoring

This commit is contained in:
Philipp Kühn 2020-10-22 09:42:28 +02:00
parent 5a2417fa53
commit 9697d585fe
6 changed files with 51 additions and 112 deletions

View File

@ -128,6 +128,12 @@ export interface ExtensionSpec<Options = {}, Commands = {}> {
options: Options,
editor: Editor,
}) => Commands,
createShortcuts?: (this: {
options: Options,
editor: Editor,
}) => {
[key: string]: any
},
}
export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & {

View File

@ -8,7 +8,7 @@ import {
} from '../types'
export default function getAttributesFromExtensions(extensions: Extensions) {
const allAttributes: ExtensionAttribute[] = []
const extensionAttributes: ExtensionAttribute[] = []
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]
const defaultAttribute: Required<Attribute> = {
@ -27,16 +27,18 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
globalAttributes.forEach(globalAttribute => {
globalAttribute.types.forEach(type => {
Object.entries(globalAttribute.attributes).forEach(([name, attribute]) => {
allAttributes.push({
type,
name,
attribute: {
...defaultAttribute,
...attribute,
},
Object
.entries(globalAttribute.attributes)
.forEach(([name, attribute]) => {
extensionAttributes.push({
type,
name,
attribute: {
...defaultAttribute,
...attribute,
},
})
})
})
})
})
})
@ -48,17 +50,19 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
const attributes = extension.createAttributes.bind(context)() as Attributes
Object.entries(attributes).forEach(([name, attribute]) => {
allAttributes.push({
type: extension.name,
name,
attribute: {
...defaultAttribute,
...attribute,
},
Object
.entries(attributes)
.forEach(([name, attribute]) => {
extensionAttributes.push({
type: extension.name,
name,
attribute: {
...defaultAttribute,
...attribute,
},
})
})
})
})
return allAttributes
return extensionAttributes
}

View File

@ -1,18 +1,18 @@
import { Node, Mark } from 'prosemirror-model'
import { ExtensionAttribute } from '../types'
export default function getRenderedAttributes(node: Node | Mark, attributes: ExtensionAttribute[]) {
return attributes
export default function getRenderedAttributes(nodeOrMark: Node | Mark, extensionAttributes: ExtensionAttribute[]): { [key: string]: any } {
return extensionAttributes
.filter(item => item.attribute.rendered)
.map(item => {
// TODO: fallback if renderHTML doesnt exist
return item.attribute.renderHTML(node.attrs)
return item.attribute.renderHTML(nodeOrMark.attrs)
})
.reduce((accumulator, value) => {
.reduce((attributes, attribute) => {
// TODO: add support for "class" and "style" merge
return {
...accumulator,
...value,
...attributes,
...attribute,
}
}, {})
}

View File

@ -1,27 +1,7 @@
import { createNode } from '@tiptap/core'
// export default new Node()
// .name('document')
// .topNode()
// .schema(() => ({
// content: 'block+',
// }))
// .create()
// export default class Document extends Node {
// name = 'document'
// topNode = true
// content = 'block+'
// }
export default createNode({
name: 'document',
topNode: true,
content: 'block+',
})

View File

@ -1,5 +1,4 @@
import { createNode } from '@tiptap/core'
// import { DOMOutputSpecArray } from 'prosemirror-model'
// import ParagraphComponent from './paragraph.vue'
// export type ParagraphCommand = () => Command
@ -10,54 +9,6 @@ import { createNode } from '@tiptap/core'
// }
// }
// export default new Node()
// .name('paragraph')
// .schema(() => ({
// content: 'inline*',
// group: 'block',
// parseDOM: [{ tag: 'p' }],
// toDOM: () => ['p', 0],
// // toVue: ParagraphComponent,
// }))
// .commands(({ name }) => ({
// [name]: () => ({ commands }) => {
// return commands.toggleBlockType(name, 'paragraph')
// },
// }))
// .keys(({ editor }) => ({
// 'Mod-Alt-0': () => editor.paragraph(),
// }))
// .create()
// export default class Paragraph extends Node implements INode {
// name = 'paragraph'
// group = 'block'
// content = 'inline*'
// createAttributes() {
// return {
// // default rendering
// class: {
// default: 'jooo',
// },
// }
// }
// parseHTML() {
// return [
// { tag: 'p' },
// ]
// }
// renderHTML() {
// return ['p', 0] as const
// }
// }
export default createNode({
name: 'paragraph',
@ -88,7 +39,7 @@ export default createNode({
default: '123',
rendered: true,
renderHTML: attributes => ({
// class: `foo-${attributes.id}`,
class: `foo-${attributes.id}`,
id: 'foo',
}),
},
@ -104,4 +55,18 @@ export default createNode({
renderHTML({ attributes }) {
return ['p', attributes, 0]
},
createCommands() {
return {
paragraph: () => ({ commands }) => {
return commands.toggleBlockType('paragraph', 'paragraph')
},
}
},
createShortcuts() {
return {
'Mod-Alt-0': () => this.editor.paragraph(),
}
},
})

View File

@ -1,22 +1,6 @@
import { createNode } from '@tiptap/core'
// export default new Node()
// .name('text')
// .schema(() => ({
// group: 'inline',
// }))
// .create()
// export default class Text extends Node {
// name = 'text'
// group = 'inline'
// }
export default createNode({
name: 'text',
group: 'inline',
})