mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 14:13:21 +08:00
add basic implementation for attributes
This commit is contained in:
parent
7fffe97532
commit
6752d52541
@ -71,6 +71,7 @@
|
||||
|
||||
import { DOMOutputSpec, NodeSpec, Node } from 'prosemirror-model'
|
||||
import { ExtensionSpec } from './Extension'
|
||||
import { Attributes } from './types'
|
||||
|
||||
export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
|
||||
topNode?: boolean,
|
||||
@ -100,6 +101,11 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Extensio
|
||||
},
|
||||
}
|
||||
) => DOMOutputSpec,
|
||||
createAttributes?: (
|
||||
this: {
|
||||
options: Options,
|
||||
},
|
||||
) => Attributes,
|
||||
}
|
||||
|
||||
export type NodeExtension = Required<Omit<NodeExtensionSpec, 'defaultOptions'> & {
|
||||
@ -127,6 +133,7 @@ const defaultNode: NodeExtension = {
|
||||
createCommands: () => ({}),
|
||||
parseHTML: () => null,
|
||||
renderHTML: () => null,
|
||||
createAttributes: () => ({}),
|
||||
}
|
||||
|
||||
export function createNode<Options extends {}, Commands extends {}>(config: NodeExtensionSpec<Options, Commands>) {
|
||||
|
@ -1,5 +1,24 @@
|
||||
import Extension from './Extension'
|
||||
import Node from './Node'
|
||||
import Mark from './Mark'
|
||||
import { Extension } from './Extension'
|
||||
import { NodeExtension } from './Node'
|
||||
import { MarkExtension } from './Mark'
|
||||
|
||||
export type Extensions = (Extension | Node | Mark)[]
|
||||
export type Extensions = (Extension | NodeExtension | MarkExtension)[]
|
||||
|
||||
export type Attribute = {
|
||||
default: any,
|
||||
rendered?: boolean,
|
||||
renderHTML?: (attributes: {
|
||||
[key: string]: any,
|
||||
}) => any,
|
||||
parseHTML?: () => any,
|
||||
}
|
||||
|
||||
export type Attributes = {
|
||||
[key: string]: Attribute
|
||||
}
|
||||
|
||||
export type ExtensionAttribute = {
|
||||
type: string,
|
||||
name: string,
|
||||
attribute: Attribute
|
||||
}
|
||||
|
38
packages/core/src/utils/getAttributesFromExtensions.ts
Normal file
38
packages/core/src/utils/getAttributesFromExtensions.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import {
|
||||
Extensions, Attributes, Attribute, ExtensionAttribute,
|
||||
} from '../types'
|
||||
import splitExtensions from './splitExtensions'
|
||||
|
||||
export default function getAttributesFromExtensions(extensions: Extensions) {
|
||||
const allAttributes: ExtensionAttribute[] = []
|
||||
|
||||
const { nodeExtensions } = splitExtensions(extensions)
|
||||
|
||||
const defaultAttribute: Required<Attribute> = {
|
||||
default: null,
|
||||
rendered: true,
|
||||
renderHTML: () => ({}),
|
||||
parseHTML: () => null,
|
||||
}
|
||||
|
||||
nodeExtensions.forEach(extension => {
|
||||
const context = {
|
||||
options: extension.options,
|
||||
}
|
||||
|
||||
const attributes = extension.createAttributes.bind(context)() as Attributes
|
||||
|
||||
Object.entries(attributes).forEach(([name, attribute]) => {
|
||||
allAttributes.push({
|
||||
type: extension.name,
|
||||
name,
|
||||
attribute: {
|
||||
...defaultAttribute,
|
||||
...attribute,
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return allAttributes
|
||||
}
|
17
packages/core/src/utils/getRenderedAttributes.ts
Normal file
17
packages/core/src/utils/getRenderedAttributes.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Node } from 'prosemirror-model'
|
||||
import { ExtensionAttribute } from '../types'
|
||||
|
||||
export default function getRenderedAttributes(node: Node, attributes: ExtensionAttribute[]) {
|
||||
return attributes
|
||||
.map(attribute => {
|
||||
// TODO: fallback if renderHTML doesn’t exist
|
||||
return attribute.attribute.renderHTML(node.attrs)
|
||||
})
|
||||
.reduce((accumulator, value) => {
|
||||
// TODO: add support for "class" merge
|
||||
return {
|
||||
...accumulator,
|
||||
...value,
|
||||
}
|
||||
}, {})
|
||||
}
|
@ -5,8 +5,12 @@ import { Extensions } from '../types'
|
||||
// import getMarksFromExtensions from './getMarksFromExtensions'
|
||||
// import resolveExtensionConfig from './resolveExtensionConfig'
|
||||
import splitExtensions from './splitExtensions'
|
||||
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
||||
import getRenderedAttributes from './getRenderedAttributes'
|
||||
|
||||
export default function getSchema(extensions: Extensions): Schema {
|
||||
const allAttributes = getAttributesFromExtensions(extensions)
|
||||
|
||||
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
||||
|
||||
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
|
||||
@ -16,9 +20,7 @@ export default function getSchema(extensions: Extensions): Schema {
|
||||
options: extension.options,
|
||||
}
|
||||
|
||||
const attributes = {
|
||||
class: 'test',
|
||||
}
|
||||
const attributes = allAttributes.filter(attribute => attribute.type === extension.name)
|
||||
|
||||
const schema: NodeSpec = {
|
||||
content: extension.content,
|
||||
@ -32,20 +34,30 @@ export default function getSchema(extensions: Extensions): Schema {
|
||||
defining: extension.defining,
|
||||
isolating: extension.isolating,
|
||||
parseDOM: extension.parseHTML.bind(context)(),
|
||||
toDOM: node => extension.renderHTML.bind(context)({ node, attributes }),
|
||||
toDOM: node => {
|
||||
return extension.renderHTML.bind(context)({
|
||||
node,
|
||||
attributes: getRenderedAttributes(node, attributes),
|
||||
})
|
||||
},
|
||||
attrs: Object.fromEntries(attributes.map(attribute => {
|
||||
return [attribute.name, { default: attribute?.attribute?.default }]
|
||||
})),
|
||||
}
|
||||
|
||||
return [extension.name, schema]
|
||||
}))
|
||||
|
||||
// console.log({ nodes })
|
||||
|
||||
const marks = Object.fromEntries(markExtensions.map(extension => {
|
||||
const context = {
|
||||
options: extension.options,
|
||||
}
|
||||
|
||||
const attributes = {
|
||||
class: 'test',
|
||||
}
|
||||
// const attributes = {
|
||||
// class: 'test',
|
||||
// }
|
||||
|
||||
const schema: MarkSpec = {
|
||||
inclusive: extension.inclusive,
|
||||
@ -53,7 +65,7 @@ export default function getSchema(extensions: Extensions): Schema {
|
||||
group: extension.group,
|
||||
spanning: extension.spanning,
|
||||
parseDOM: extension.parseHTML.bind(context)(),
|
||||
toDOM: node => extension.renderHTML.bind(context)({ node, attributes }),
|
||||
toDOM: node => extension.renderHTML.bind(context)({ node, attributes: {} }),
|
||||
}
|
||||
|
||||
return [extension.name, schema]
|
||||
|
@ -65,6 +65,15 @@ export default createNode({
|
||||
|
||||
content: 'inline*',
|
||||
|
||||
createAttributes() {
|
||||
return {
|
||||
id: {
|
||||
default: '123',
|
||||
renderHTML: attributes => ({ class: `foo-${attributes.id}`, id: 'foo' }),
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
parseHTML() {
|
||||
return [
|
||||
{ tag: 'p' },
|
||||
|
Loading…
Reference in New Issue
Block a user