mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-28 15:49:23 +08:00
refactoring
This commit is contained in:
parent
2ebf4bf790
commit
10e16208f9
@ -79,6 +79,11 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Extensio
|
||||
group?: NodeSpec['group'],
|
||||
inline?: NodeSpec['inline'],
|
||||
atom?: NodeSpec['atom'],
|
||||
selectable?: NodeSpec['selectable'],
|
||||
draggable?: NodeSpec['draggable'],
|
||||
code?: NodeSpec['code'],
|
||||
defining?: NodeSpec['defining'],
|
||||
isolating?: NodeSpec['isolating'],
|
||||
parseHTML?: () => NodeSpec['parseDOM'],
|
||||
renderHTML?: (props: {
|
||||
node: Node,
|
||||
@ -105,6 +110,11 @@ const defaultNode: NodeExtension = {
|
||||
group: null,
|
||||
inline: null,
|
||||
atom: null,
|
||||
selectable: null,
|
||||
draggable: null,
|
||||
code: null,
|
||||
defining: null,
|
||||
isolating: null,
|
||||
createCommands: () => ({}),
|
||||
parseHTML: () => null,
|
||||
renderHTML: () => null,
|
||||
|
@ -1,54 +1,43 @@
|
||||
import { NodeSpec, Schema } from 'prosemirror-model'
|
||||
|
||||
import { Extensions } from '../types'
|
||||
import getTopNodeFromExtensions from './getTopNodeFromExtensions'
|
||||
import getNodesFromExtensions from './getNodesFromExtensions'
|
||||
import getMarksFromExtensions from './getMarksFromExtensions'
|
||||
import resolveExtensionConfig from './resolveExtensionConfig'
|
||||
import { NodeExtension } from '../Node'
|
||||
import Mark from '../Mark'
|
||||
import Extension from '../Extension'
|
||||
// import getTopNodeFromExtensions from './getTopNodeFromExtensions'
|
||||
// import getNodesFromExtensions from './getNodesFromExtensions'
|
||||
// import getMarksFromExtensions from './getMarksFromExtensions'
|
||||
// import resolveExtensionConfig from './resolveExtensionConfig'
|
||||
import splitExtensions from './splitExtensions'
|
||||
|
||||
export default function getSchema(extensions: Extensions): Schema {
|
||||
// const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[]
|
||||
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as NodeExtension[]
|
||||
// const markExtensions = extensions.filter(extension => extension.type === 'mark') as Mark[]
|
||||
const { nodeExtensions } = splitExtensions(extensions)
|
||||
|
||||
// console.log({ extensions })
|
||||
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
|
||||
|
||||
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
|
||||
const context = {
|
||||
options: extension.options,
|
||||
}
|
||||
|
||||
const attributes = {
|
||||
class: 'test',
|
||||
}
|
||||
|
||||
const schema: NodeSpec = {
|
||||
content: extension.content,
|
||||
marks: extension.marks,
|
||||
group: extension.group,
|
||||
parseDOM: extension.parseHTML(),
|
||||
toDOM: node => extension.renderHTML({ node, attributes: { class: 'test' } }),
|
||||
inline: extension.inline,
|
||||
atom: extension.atom,
|
||||
selectable: extension.selectable,
|
||||
draggable: extension.draggable,
|
||||
code: extension.code,
|
||||
defining: extension.defining,
|
||||
isolating: extension.isolating,
|
||||
parseDOM: extension.parseHTML.bind(context)(),
|
||||
toDOM: node => extension.renderHTML.bind(context)({ node, attributes }),
|
||||
}
|
||||
|
||||
return [extension.name, schema]
|
||||
}))
|
||||
|
||||
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
|
||||
|
||||
// extensions.forEach(extension => {
|
||||
// resolveExtensionConfig(extension, 'name')
|
||||
// resolveExtensionConfig(extension, 'defaults')
|
||||
// resolveExtensionConfig(extension, 'topNode')
|
||||
|
||||
// const { name } = extension.config
|
||||
// const options = {
|
||||
// ...extension.config.defaults,
|
||||
// ...extension.options,
|
||||
// }
|
||||
|
||||
// resolveExtensionConfig(extension, 'schema', { name, options })
|
||||
// })
|
||||
|
||||
// return new Schema({
|
||||
// topNode: getTopNodeFromExtensions(extensions),
|
||||
// nodes: getNodesFromExtensions(extensions),
|
||||
// marks: getMarksFromExtensions(extensions),
|
||||
// })
|
||||
|
||||
return new Schema({
|
||||
topNode,
|
||||
nodes,
|
||||
|
16
packages/core/src/utils/splitExtensions.ts
Normal file
16
packages/core/src/utils/splitExtensions.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { Extensions } from '../types'
|
||||
import { Extension } from '../Extension'
|
||||
import { NodeExtension } from '../Node'
|
||||
// import { MarkExtension } from '../Node'
|
||||
|
||||
export default function splitExtensions(extensions: Extensions) {
|
||||
const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[]
|
||||
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as NodeExtension[]
|
||||
// const markExtensions = extensions.filter(extension => extension.type === 'mark') as MarkExtension[]
|
||||
|
||||
return {
|
||||
baseExtensions,
|
||||
nodeExtensions,
|
||||
// markExtensions,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user