refactoring
This commit is contained in:
@@ -128,6 +128,12 @@ export interface ExtensionSpec<Options = {}, Commands = {}> {
|
|||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
}) => Commands,
|
}) => Commands,
|
||||||
|
createShortcuts?: (this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
}) => {
|
||||||
|
[key: string]: any
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & {
|
export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
} from '../types'
|
} from '../types'
|
||||||
|
|
||||||
export default function getAttributesFromExtensions(extensions: Extensions) {
|
export default function getAttributesFromExtensions(extensions: Extensions) {
|
||||||
const allAttributes: ExtensionAttribute[] = []
|
const extensionAttributes: ExtensionAttribute[] = []
|
||||||
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
||||||
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]
|
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]
|
||||||
const defaultAttribute: Required<Attribute> = {
|
const defaultAttribute: Required<Attribute> = {
|
||||||
@@ -27,8 +27,10 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
|
|||||||
|
|
||||||
globalAttributes.forEach(globalAttribute => {
|
globalAttributes.forEach(globalAttribute => {
|
||||||
globalAttribute.types.forEach(type => {
|
globalAttribute.types.forEach(type => {
|
||||||
Object.entries(globalAttribute.attributes).forEach(([name, attribute]) => {
|
Object
|
||||||
allAttributes.push({
|
.entries(globalAttribute.attributes)
|
||||||
|
.forEach(([name, attribute]) => {
|
||||||
|
extensionAttributes.push({
|
||||||
type,
|
type,
|
||||||
name,
|
name,
|
||||||
attribute: {
|
attribute: {
|
||||||
@@ -48,8 +50,10 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
|
|||||||
|
|
||||||
const attributes = extension.createAttributes.bind(context)() as Attributes
|
const attributes = extension.createAttributes.bind(context)() as Attributes
|
||||||
|
|
||||||
Object.entries(attributes).forEach(([name, attribute]) => {
|
Object
|
||||||
allAttributes.push({
|
.entries(attributes)
|
||||||
|
.forEach(([name, attribute]) => {
|
||||||
|
extensionAttributes.push({
|
||||||
type: extension.name,
|
type: extension.name,
|
||||||
name,
|
name,
|
||||||
attribute: {
|
attribute: {
|
||||||
@@ -60,5 +64,5 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return allAttributes
|
return extensionAttributes
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
import { Node, Mark } from 'prosemirror-model'
|
import { Node, Mark } from 'prosemirror-model'
|
||||||
import { ExtensionAttribute } from '../types'
|
import { ExtensionAttribute } from '../types'
|
||||||
|
|
||||||
export default function getRenderedAttributes(node: Node | Mark, attributes: ExtensionAttribute[]) {
|
export default function getRenderedAttributes(nodeOrMark: Node | Mark, extensionAttributes: ExtensionAttribute[]): { [key: string]: any } {
|
||||||
return attributes
|
return extensionAttributes
|
||||||
.filter(item => item.attribute.rendered)
|
.filter(item => item.attribute.rendered)
|
||||||
.map(item => {
|
.map(item => {
|
||||||
// TODO: fallback if renderHTML doesn’t exist
|
// TODO: fallback if renderHTML doesn’t 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
|
// TODO: add support for "class" and "style" merge
|
||||||
return {
|
return {
|
||||||
...accumulator,
|
...attributes,
|
||||||
...value,
|
...attribute,
|
||||||
}
|
}
|
||||||
}, {})
|
}, {})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,7 @@
|
|||||||
import { createNode } from '@tiptap/core'
|
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({
|
export default createNode({
|
||||||
name: 'document',
|
name: 'document',
|
||||||
|
|
||||||
topNode: true,
|
topNode: true,
|
||||||
|
|
||||||
content: 'block+',
|
content: 'block+',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { createNode } from '@tiptap/core'
|
import { createNode } from '@tiptap/core'
|
||||||
// import { DOMOutputSpecArray } from 'prosemirror-model'
|
|
||||||
// import ParagraphComponent from './paragraph.vue'
|
// import ParagraphComponent from './paragraph.vue'
|
||||||
|
|
||||||
// export type ParagraphCommand = () => Command
|
// 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({
|
export default createNode({
|
||||||
name: 'paragraph',
|
name: 'paragraph',
|
||||||
|
|
||||||
@@ -88,7 +39,7 @@ export default createNode({
|
|||||||
default: '123',
|
default: '123',
|
||||||
rendered: true,
|
rendered: true,
|
||||||
renderHTML: attributes => ({
|
renderHTML: attributes => ({
|
||||||
// class: `foo-${attributes.id}`,
|
class: `foo-${attributes.id}`,
|
||||||
id: 'foo',
|
id: 'foo',
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@@ -104,4 +55,18 @@ export default createNode({
|
|||||||
renderHTML({ attributes }) {
|
renderHTML({ attributes }) {
|
||||||
return ['p', attributes, 0]
|
return ['p', attributes, 0]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
createCommands() {
|
||||||
|
return {
|
||||||
|
paragraph: () => ({ commands }) => {
|
||||||
|
return commands.toggleBlockType('paragraph', 'paragraph')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
createShortcuts() {
|
||||||
|
return {
|
||||||
|
'Mod-Alt-0': () => this.editor.paragraph(),
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,22 +1,6 @@
|
|||||||
import { createNode } from '@tiptap/core'
|
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({
|
export default createNode({
|
||||||
name: 'text',
|
name: 'text',
|
||||||
|
|
||||||
group: 'inline',
|
group: 'inline',
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user