refactoring
This commit is contained in:
@@ -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'> & {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 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
|
||||
return {
|
||||
...accumulator,
|
||||
...value,
|
||||
...attributes,
|
||||
...attribute,
|
||||
}
|
||||
}, {})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user