feat: Add extension storage (#2069)

This commit is contained in:
Philipp Kühn
2021-10-22 08:52:54 +02:00
committed by GitHub
parent 6987505fda
commit 7ffabf251c
26 changed files with 555 additions and 105 deletions

View File

@@ -5,7 +5,10 @@ import { Editor } from './Editor'
import { Node } from './Node'
import { Mark } from './Mark'
import mergeDeep from './utilities/mergeDeep'
import callOrReturn from './utilities/callOrReturn'
import getExtensionField from './helpers/getExtensionField'
import {
AnyConfig,
Extensions,
GlobalAttributes,
RawCommands,
@@ -15,7 +18,7 @@ import {
import { ExtensionConfig } from '.'
declare module '@tiptap/core' {
interface ExtensionConfig<Options = any> {
interface ExtensionConfig<Options = any, Storage = any> {
[key: string]: any;
/**
@@ -33,13 +36,23 @@ declare module '@tiptap/core' {
*/
defaultOptions?: Options,
/**
* Default Storage
*/
addStorage?: (this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addGlobalAttributes'],
}) => Storage,
/**
* Global attributes
*/
addGlobalAttributes?: (this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['addGlobalAttributes'],
storage: Storage,
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addGlobalAttributes'],
}) => GlobalAttributes | {},
/**
@@ -48,8 +61,9 @@ declare module '@tiptap/core' {
addCommands?: (this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addCommands'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addCommands'],
}) => Partial<RawCommands>,
/**
@@ -58,8 +72,9 @@ declare module '@tiptap/core' {
addKeyboardShortcuts?: (this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addKeyboardShortcuts'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addKeyboardShortcuts'],
}) => {
[key: string]: KeyboardShortcutCommand,
},
@@ -70,8 +85,9 @@ declare module '@tiptap/core' {
addInputRules?: (this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addInputRules'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addInputRules'],
}) => InputRule[],
/**
@@ -80,8 +96,9 @@ declare module '@tiptap/core' {
addPasteRules?: (this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addPasteRules'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addPasteRules'],
}) => PasteRule[],
/**
@@ -90,8 +107,9 @@ declare module '@tiptap/core' {
addProseMirrorPlugins?: (this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addProseMirrorPlugins'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addProseMirrorPlugins'],
}) => Plugin[],
/**
@@ -100,7 +118,8 @@ declare module '@tiptap/core' {
addExtensions?: (this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['addExtensions'],
storage: Storage,
parent: ParentConfig<ExtensionConfig<Options, Storage>>['addExtensions'],
}) => Extensions,
/**
@@ -110,7 +129,8 @@ declare module '@tiptap/core' {
this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendNodeSchema'],
storage: Storage,
parent: ParentConfig<ExtensionConfig<Options, Storage>>['extendNodeSchema'],
},
extension: Node,
) => Record<string, any>) | null,
@@ -122,7 +142,8 @@ declare module '@tiptap/core' {
this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendMarkSchema'],
storage: Storage,
parent: ParentConfig<ExtensionConfig<Options, Storage>>['extendMarkSchema'],
},
extension: Mark,
) => Record<string, any>) | null,
@@ -133,8 +154,9 @@ declare module '@tiptap/core' {
onBeforeCreate?: ((this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onBeforeCreate'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onBeforeCreate'],
}) => void) | null,
/**
@@ -143,8 +165,9 @@ declare module '@tiptap/core' {
onCreate?: ((this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onCreate'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onCreate'],
}) => void) | null,
/**
@@ -153,8 +176,9 @@ declare module '@tiptap/core' {
onUpdate?: ((this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onUpdate'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onUpdate'],
}) => void) | null,
/**
@@ -163,8 +187,9 @@ declare module '@tiptap/core' {
onSelectionUpdate?: ((this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onSelectionUpdate'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onSelectionUpdate'],
}) => void) | null,
/**
@@ -174,8 +199,9 @@ declare module '@tiptap/core' {
this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onTransaction'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onTransaction'],
},
props: {
transaction: Transaction,
@@ -189,8 +215,9 @@ declare module '@tiptap/core' {
this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onFocus'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onFocus'],
},
props: {
event: FocusEvent,
@@ -204,8 +231,9 @@ declare module '@tiptap/core' {
this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onBlur'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onBlur'],
},
props: {
event: FocusEvent,
@@ -218,13 +246,14 @@ declare module '@tiptap/core' {
onDestroy?: ((this: {
name: string,
options: Options,
storage: Storage,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onDestroy'],
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onDestroy'],
}) => void) | null,
}
}
export class Extension<Options = any> {
export class Extension<Options = any, Storage = any> {
type = 'extension'
name = 'extension'
@@ -235,12 +264,14 @@ export class Extension<Options = any> {
options: Options
storage: Storage
config: ExtensionConfig = {
name: this.name,
defaultOptions: {},
}
constructor(config: Partial<ExtensionConfig<Options>> = {}) {
constructor(config: Partial<ExtensionConfig<Options, Storage>> = {}) {
this.config = {
...this.config,
...config,
@@ -248,10 +279,18 @@ export class Extension<Options = any> {
this.name = this.config.name
this.options = this.config.defaultOptions
this.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
this,
'addStorage',
{
name: this.name,
options: this.options,
},
))
}
static create<O>(config: Partial<ExtensionConfig<O>> = {}) {
return new Extension<O>(config)
static create<O = any, S = any>(config: Partial<ExtensionConfig<O, S>> = {}) {
return new Extension<O, S>(config)
}
configure(options: Partial<Options> = {}) {
@@ -264,8 +303,8 @@ export class Extension<Options = any> {
return extension
}
extend<ExtendedOptions = Options>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions>> = {}) {
const extension = new Extension<ExtendedOptions>(extendedConfig)
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions, ExtendedStorage>> = {}) {
const extension = new Extension<ExtendedOptions, ExtendedStorage>(extendedConfig)
extension.parent = this
@@ -279,6 +318,15 @@ export class Extension<Options = any> {
? extendedConfig.defaultOptions
: extension.parent.options
extension.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
extension,
'addStorage',
{
name: extension.name,
options: extension.options,
},
))
return extension
}
}