feat: add addExtensions option
This commit is contained in:
@@ -5,6 +5,7 @@ import { Node } from './Node'
|
|||||||
import { Mark } from './Mark'
|
import { Mark } from './Mark'
|
||||||
import mergeDeep from './utilities/mergeDeep'
|
import mergeDeep from './utilities/mergeDeep'
|
||||||
import {
|
import {
|
||||||
|
AnyExtension,
|
||||||
GlobalAttributes,
|
GlobalAttributes,
|
||||||
RawCommands,
|
RawCommands,
|
||||||
ParentConfig,
|
ParentConfig,
|
||||||
@@ -92,6 +93,16 @@ declare module '@tiptap/core' {
|
|||||||
parent: ParentConfig<ExtensionConfig<Options>>['addProseMirrorPlugins'],
|
parent: ParentConfig<ExtensionConfig<Options>>['addProseMirrorPlugins'],
|
||||||
}) => Plugin[],
|
}) => Plugin[],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extensions
|
||||||
|
*/
|
||||||
|
addExtensions?: (this: {
|
||||||
|
name: string,
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
parent: ParentConfig<ExtensionConfig<Options>>['addExtensions'],
|
||||||
|
}) => AnyExtension[],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extend Node Schema
|
* Extend Node Schema
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export default class ExtensionManager {
|
|||||||
|
|
||||||
constructor(extensions: Extensions, editor: Editor) {
|
constructor(extensions: Extensions, editor: Editor) {
|
||||||
this.editor = editor
|
this.editor = editor
|
||||||
this.extensions = this.sort(extensions)
|
this.extensions = this.sort(this.flatten(extensions))
|
||||||
this.schema = getSchema(this.extensions)
|
this.schema = getSchema(this.extensions)
|
||||||
|
|
||||||
this.extensions.forEach(extension => {
|
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
|
const defaultPriority = 100
|
||||||
|
|
||||||
return extensions.sort((a, b) => {
|
return extensions.sort((a, b) => {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { Plugin, Transaction } from 'prosemirror-state'
|
|||||||
import { InputRule } from 'prosemirror-inputrules'
|
import { InputRule } from 'prosemirror-inputrules'
|
||||||
import mergeDeep from './utilities/mergeDeep'
|
import mergeDeep from './utilities/mergeDeep'
|
||||||
import {
|
import {
|
||||||
|
AnyExtension,
|
||||||
Attributes,
|
Attributes,
|
||||||
RawCommands,
|
RawCommands,
|
||||||
GlobalAttributes,
|
GlobalAttributes,
|
||||||
@@ -103,6 +104,16 @@ declare module '@tiptap/core' {
|
|||||||
parent: ParentConfig<MarkConfig<Options>>['addProseMirrorPlugins'],
|
parent: ParentConfig<MarkConfig<Options>>['addProseMirrorPlugins'],
|
||||||
}) => Plugin[],
|
}) => Plugin[],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extensions
|
||||||
|
*/
|
||||||
|
addExtensions?: (this: {
|
||||||
|
name: string,
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
parent: ParentConfig<MarkConfig<Options>>['addExtensions'],
|
||||||
|
}) => AnyExtension[],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extend Node Schema
|
* Extend Node Schema
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { Plugin, Transaction } from 'prosemirror-state'
|
|||||||
import { InputRule } from 'prosemirror-inputrules'
|
import { InputRule } from 'prosemirror-inputrules'
|
||||||
import mergeDeep from './utilities/mergeDeep'
|
import mergeDeep from './utilities/mergeDeep'
|
||||||
import {
|
import {
|
||||||
|
AnyExtension,
|
||||||
Attributes,
|
Attributes,
|
||||||
NodeViewRenderer,
|
NodeViewRenderer,
|
||||||
GlobalAttributes,
|
GlobalAttributes,
|
||||||
@@ -103,6 +104,16 @@ declare module '@tiptap/core' {
|
|||||||
parent: ParentConfig<NodeConfig<Options>>['addProseMirrorPlugins'],
|
parent: ParentConfig<NodeConfig<Options>>['addProseMirrorPlugins'],
|
||||||
}) => Plugin[],
|
}) => Plugin[],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extensions
|
||||||
|
*/
|
||||||
|
addExtensions?: (this: {
|
||||||
|
name: string,
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
parent: ParentConfig<NodeConfig<Options>>['addExtensions'],
|
||||||
|
}) => AnyExtension[],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extend Node Schema
|
* Extend Node Schema
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user