feat: add priority option to extensions

This commit is contained in:
Philipp Kühn
2021-04-07 18:29:16 +02:00
parent 6d83bef97d
commit bb1ae659a4
12 changed files with 64 additions and 11 deletions

View File

@@ -16,6 +16,11 @@ declare module '@tiptap/core' {
*/
name: string,
/**
* Priority
*/
priority?: number,
/**
* Default options
*/
@@ -180,6 +185,7 @@ export class Extension<Options = any> {
config: ExtensionConfig = {
name: 'extension',
priority: 100,
defaultOptions: {},
}

View File

@@ -25,7 +25,7 @@ export default class ExtensionManager {
constructor(extensions: Extensions, editor: Editor) {
this.editor = editor
this.extensions = extensions
this.extensions = this.sort(extensions)
this.schema = getSchema(this.extensions)
this.extensions.forEach(extension => {
@@ -77,6 +77,22 @@ export default class ExtensionManager {
})
}
private sort(extensions: Extensions) {
const defaultPriority = 100
return extensions.sort((a, b) => {
if ((a.config.priority || defaultPriority) > (b.config.priority || defaultPriority)) {
return -1
}
if ((a.config.priority || defaultPriority) < (b.config.priority || defaultPriority)) {
return 1
}
return 0
})
}
get commands(): RawCommands {
return this.extensions.reduce((commands, extension) => {
const context = {

View File

@@ -21,6 +21,11 @@ declare module '@tiptap/core' {
*/
name: string,
/**
* Priority
*/
priority?: number,
/**
* Default options
*/
@@ -254,6 +259,7 @@ export class Mark<Options = any> {
config: MarkConfig = {
name: 'mark',
priority: 100,
defaultOptions: {},
}

View File

@@ -26,6 +26,11 @@ declare module '@tiptap/core' {
*/
name: string,
/**
* Priority
*/
priority?: number,
/**
* Default options
*/
@@ -312,6 +317,7 @@ export class Node<Options = any> {
config: NodeConfig = {
name: 'node',
priority: 100,
defaultOptions: {},
}