feat: add addExtensions option

This commit is contained in:
Philipp Kühn 2021-05-06 18:39:47 +02:00
parent 9718002875
commit 26e672e2f0
4 changed files with 60 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import { Node } from './Node'
import { Mark } from './Mark'
import mergeDeep from './utilities/mergeDeep'
import {
AnyExtension,
GlobalAttributes,
RawCommands,
ParentConfig,
@ -92,6 +93,16 @@ declare module '@tiptap/core' {
parent: ParentConfig<ExtensionConfig<Options>>['addProseMirrorPlugins'],
}) => Plugin[],
/**
* Extensions
*/
addExtensions?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addExtensions'],
}) => AnyExtension[],
/**
* Extend Node Schema
*/

View File

@ -27,7 +27,7 @@ export default class ExtensionManager {
constructor(extensions: Extensions, editor: Editor) {
this.editor = editor
this.extensions = this.sort(extensions)
this.extensions = this.sort(this.flatten(extensions))
this.schema = getSchema(this.extensions)
this.extensions.forEach(extension => {
@ -128,7 +128,32 @@ export default class ExtensionManager {
})
}
private sort(extensions: Extensions) {
private flatten(extensions: Extensions): Extensions {
return extensions
.map(extension => {
const context = {
name: extension.name,
options: extension.options,
editor: this.editor,
}
const addExtensions = getExtensionField<AnyConfig['addExtensions']>(
extension,
'addExtensions',
context,
)
if (addExtensions) {
return this.flatten(addExtensions())
}
return extension
})
// `Infinity` will break TypeScript so we set a number that is probably high enough
.flat(10)
}
private sort(extensions: Extensions): Extensions {
const defaultPriority = 100
return extensions.sort((a, b) => {

View File

@ -8,6 +8,7 @@ import { Plugin, Transaction } from 'prosemirror-state'
import { InputRule } from 'prosemirror-inputrules'
import mergeDeep from './utilities/mergeDeep'
import {
AnyExtension,
Attributes,
RawCommands,
GlobalAttributes,
@ -103,6 +104,16 @@ declare module '@tiptap/core' {
parent: ParentConfig<MarkConfig<Options>>['addProseMirrorPlugins'],
}) => Plugin[],
/**
* Extensions
*/
addExtensions?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<MarkConfig<Options>>['addExtensions'],
}) => AnyExtension[],
/**
* Extend Node Schema
*/

View File

@ -8,6 +8,7 @@ import { Plugin, Transaction } from 'prosemirror-state'
import { InputRule } from 'prosemirror-inputrules'
import mergeDeep from './utilities/mergeDeep'
import {
AnyExtension,
Attributes,
NodeViewRenderer,
GlobalAttributes,
@ -103,6 +104,16 @@ declare module '@tiptap/core' {
parent: ParentConfig<NodeConfig<Options>>['addProseMirrorPlugins'],
}) => Plugin[],
/**
* Extensions
*/
addExtensions?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<NodeConfig<Options>>['addExtensions'],
}) => AnyExtension[],
/**
* Extend Node Schema
*/