add global attributes
This commit is contained in:
@@ -113,9 +113,16 @@
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
import { GlobalAttributes } from './types'
|
||||||
|
|
||||||
export interface ExtensionSpec<Options = {}, Commands = {}> {
|
export interface ExtensionSpec<Options = {}, Commands = {}> {
|
||||||
name: string,
|
name: string,
|
||||||
defaultOptions?: Options,
|
defaultOptions?: Options,
|
||||||
|
createGlobalAttributes?: (
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
) => GlobalAttributes,
|
||||||
createCommands?(this: {
|
createCommands?(this: {
|
||||||
options: Options,
|
options: Options,
|
||||||
// editor: Editor,
|
// editor: Editor,
|
||||||
@@ -133,6 +140,7 @@ const defaultExtension: Extension = {
|
|||||||
type: 'extension',
|
type: 'extension',
|
||||||
name: 'extension',
|
name: 'extension',
|
||||||
options: {},
|
options: {},
|
||||||
|
createGlobalAttributes: () => [],
|
||||||
createCommands: () => ({}),
|
createCommands: () => ({}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,7 @@
|
|||||||
|
|
||||||
import { DOMOutputSpec, MarkSpec, Mark } from 'prosemirror-model'
|
import { DOMOutputSpec, MarkSpec, Mark } from 'prosemirror-model'
|
||||||
import { ExtensionSpec } from './Extension'
|
import { ExtensionSpec } from './Extension'
|
||||||
|
import { Attributes } from './types'
|
||||||
|
|
||||||
export interface MarkExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
|
export interface MarkExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
|
||||||
inclusive?: MarkSpec['inclusive'],
|
inclusive?: MarkSpec['inclusive'],
|
||||||
@@ -71,6 +72,11 @@ export interface MarkExtensionSpec<Options = {}, Commands = {}> extends Extensio
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
) => DOMOutputSpec,
|
) => DOMOutputSpec,
|
||||||
|
createAttributes?: (
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
) => Attributes,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MarkExtension = Required<Omit<MarkExtensionSpec, 'defaultOptions'> & {
|
export type MarkExtension = Required<Omit<MarkExtensionSpec, 'defaultOptions'> & {
|
||||||
@@ -88,6 +94,7 @@ const defaultMark: MarkExtension = {
|
|||||||
excludes: null,
|
excludes: null,
|
||||||
group: null,
|
group: null,
|
||||||
spanning: null,
|
spanning: null,
|
||||||
|
createGlobalAttributes: () => [],
|
||||||
createCommands: () => ({}),
|
createCommands: () => ({}),
|
||||||
parseHTML: () => null,
|
parseHTML: () => null,
|
||||||
renderHTML: () => null,
|
renderHTML: () => null,
|
||||||
|
|||||||
@@ -130,6 +130,7 @@ const defaultNode: NodeExtension = {
|
|||||||
code: null,
|
code: null,
|
||||||
defining: null,
|
defining: null,
|
||||||
isolating: null,
|
isolating: null,
|
||||||
|
createGlobalAttributes: () => [],
|
||||||
createCommands: () => ({}),
|
createCommands: () => ({}),
|
||||||
parseHTML: () => null,
|
parseHTML: () => null,
|
||||||
renderHTML: () => null,
|
renderHTML: () => null,
|
||||||
|
|||||||
@@ -14,11 +14,16 @@ export type Attribute = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type Attributes = {
|
export type Attributes = {
|
||||||
[key: string]: Attribute
|
[key: string]: Attribute,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ExtensionAttribute = {
|
export type ExtensionAttribute = {
|
||||||
type: string,
|
type: string,
|
||||||
name: string,
|
name: string,
|
||||||
attribute: Attribute
|
attribute: Attribute,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type GlobalAttributes = {
|
||||||
|
types: string[],
|
||||||
|
attributes: Attributes,
|
||||||
|
}[]
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
import {
|
|
||||||
Extensions, Attributes, Attribute, ExtensionAttribute,
|
|
||||||
} from '../types'
|
|
||||||
import splitExtensions from './splitExtensions'
|
import splitExtensions from './splitExtensions'
|
||||||
|
import {
|
||||||
|
Extensions,
|
||||||
|
GlobalAttributes,
|
||||||
|
Attributes,
|
||||||
|
Attribute,
|
||||||
|
ExtensionAttribute,
|
||||||
|
} from '../types'
|
||||||
|
|
||||||
export default function getAttributesFromExtensions(extensions: Extensions) {
|
export default function getAttributesFromExtensions(extensions: Extensions) {
|
||||||
const allAttributes: ExtensionAttribute[] = []
|
const allAttributes: ExtensionAttribute[] = []
|
||||||
|
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
||||||
const { nodeExtensions } = splitExtensions(extensions)
|
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]
|
||||||
|
|
||||||
const defaultAttribute: Required<Attribute> = {
|
const defaultAttribute: Required<Attribute> = {
|
||||||
default: null,
|
default: null,
|
||||||
rendered: true,
|
rendered: true,
|
||||||
@@ -15,7 +18,30 @@ export default function getAttributesFromExtensions(extensions: Extensions) {
|
|||||||
parseHTML: () => null,
|
parseHTML: () => null,
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeExtensions.forEach(extension => {
|
extensions.forEach(extension => {
|
||||||
|
const context = {
|
||||||
|
options: extension.options,
|
||||||
|
}
|
||||||
|
|
||||||
|
const globalAttributes = extension.createGlobalAttributes.bind(context)() as GlobalAttributes
|
||||||
|
|
||||||
|
globalAttributes.forEach(globalAttribute => {
|
||||||
|
globalAttribute.types.forEach(type => {
|
||||||
|
Object.entries(globalAttribute.attributes).forEach(([name, attribute]) => {
|
||||||
|
allAttributes.push({
|
||||||
|
type,
|
||||||
|
name,
|
||||||
|
attribute: {
|
||||||
|
...defaultAttribute,
|
||||||
|
...attribute,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
nodeAndMarkExtensions.forEach(extension => {
|
||||||
const context = {
|
const context = {
|
||||||
options: extension.options,
|
options: extension.options,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export default function getRenderedAttributes(node: Node, attributes: ExtensionA
|
|||||||
return item.attribute.renderHTML(node.attrs)
|
return item.attribute.renderHTML(node.attrs)
|
||||||
})
|
})
|
||||||
.reduce((accumulator, value) => {
|
.reduce((accumulator, value) => {
|
||||||
// TODO: add support for "class" merge
|
// TODO: add support for "class" and "style" merge
|
||||||
return {
|
return {
|
||||||
...accumulator,
|
...accumulator,
|
||||||
...value,
|
...value,
|
||||||
|
|||||||
@@ -65,12 +65,31 @@ export default createNode({
|
|||||||
|
|
||||||
content: 'inline*',
|
content: 'inline*',
|
||||||
|
|
||||||
|
createGlobalAttributes() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
types: ['paragraph'],
|
||||||
|
attributes: {
|
||||||
|
align: {
|
||||||
|
default: 'right',
|
||||||
|
renderHTML: attributes => ({
|
||||||
|
style: `text-align: ${attributes.align}`,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
createAttributes() {
|
createAttributes() {
|
||||||
return {
|
return {
|
||||||
id: {
|
id: {
|
||||||
default: '123',
|
default: '123',
|
||||||
rendered: true,
|
rendered: true,
|
||||||
renderHTML: attributes => ({ class: `foo-${attributes.id}`, id: 'foo' }),
|
renderHTML: attributes => ({
|
||||||
|
class: `foo-${attributes.id}`,
|
||||||
|
id: 'foo',
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user