feat: add name to extension context

This commit is contained in:
Philipp Kühn
2021-04-20 23:11:35 +02:00
parent 92ced9f998
commit 12f60abb73
7 changed files with 85 additions and 3 deletions

View File

@@ -35,6 +35,7 @@ declare module '@tiptap/core' {
* Global attributes
*/
addGlobalAttributes?: (this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['addGlobalAttributes'],
}) => GlobalAttributes | {},
@@ -43,6 +44,7 @@ declare module '@tiptap/core' {
* Raw
*/
addCommands?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addCommands'],
@@ -52,6 +54,7 @@ declare module '@tiptap/core' {
* Keyboard shortcuts
*/
addKeyboardShortcuts?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addKeyboardShortcuts'],
@@ -63,6 +66,7 @@ declare module '@tiptap/core' {
* Input rules
*/
addInputRules?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addInputRules'],
@@ -72,6 +76,7 @@ declare module '@tiptap/core' {
* Paste rules
*/
addPasteRules?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addPasteRules'],
@@ -81,6 +86,7 @@ declare module '@tiptap/core' {
* ProseMirror plugins
*/
addProseMirrorPlugins?: (this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addProseMirrorPlugins'],
@@ -91,6 +97,7 @@ declare module '@tiptap/core' {
*/
extendNodeSchema?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendNodeSchema'],
},
@@ -104,6 +111,7 @@ declare module '@tiptap/core' {
*/
extendMarkSchema?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendMarkSchema'],
},
@@ -115,7 +123,8 @@ declare module '@tiptap/core' {
/**
* The editor is not ready yet.
*/
onBeforeCreate?: ((this: {
onBeforeCreate?: ((this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onBeforeCreate'],
@@ -125,6 +134,7 @@ declare module '@tiptap/core' {
* The editor is ready.
*/
onCreate?: ((this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onCreate'],
@@ -134,6 +144,7 @@ declare module '@tiptap/core' {
* The content has changed.
*/
onUpdate?: ((this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onUpdate'],
@@ -143,6 +154,7 @@ declare module '@tiptap/core' {
* The selection has changed.
*/
onSelectionUpdate?: ((this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onSelectionUpdate'],
@@ -153,6 +165,7 @@ declare module '@tiptap/core' {
*/
onTransaction?: ((
this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onTransaction'],
@@ -167,6 +180,7 @@ declare module '@tiptap/core' {
*/
onFocus?: ((
this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onFocus'],
@@ -181,6 +195,7 @@ declare module '@tiptap/core' {
*/
onBlur?: ((
this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onBlur'],
@@ -194,6 +209,7 @@ declare module '@tiptap/core' {
* The editor is destroyed.
*/
onDestroy?: ((this: {
name: string,
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onDestroy'],

View File

@@ -32,6 +32,7 @@ export default class ExtensionManager {
this.extensions.forEach(extension => {
const context = {
name: extension.name,
options: extension.options,
editor: this.editor,
type: getSchemaTypeByName(extension.name, this.schema),
@@ -149,6 +150,7 @@ export default class ExtensionManager {
get commands(): RawCommands {
return this.extensions.reduce((commands, extension) => {
const context = {
name: extension.name,
options: extension.options,
editor: this.editor,
type: getSchemaTypeByName(extension.name, this.schema),
@@ -176,6 +178,7 @@ export default class ExtensionManager {
.reverse()
.map(extension => {
const context = {
name: extension.name,
options: extension.options,
editor: this.editor,
type: getSchemaTypeByName(extension.name, this.schema),
@@ -260,6 +263,7 @@ export default class ExtensionManager {
.map(extension => {
const extensionAttributes = this.attributes.filter(attribute => attribute.type === extension.name)
const context = {
name: extension.name,
options: extension.options,
editor,
type: getNodeType(extension.name, this.schema),
@@ -304,6 +308,7 @@ export default class ExtensionManager {
.filter(extension => !!getExtensionField(extension, 'renderText'))
.map(extension => {
const context = {
name: extension.name,
options: extension.options,
editor,
type: getNodeType(extension.name, this.schema),

View File

@@ -41,6 +41,7 @@ declare module '@tiptap/core' {
* Global attributes
*/
addGlobalAttributes?: (this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['addGlobalAttributes'],
}) => GlobalAttributes | {},
@@ -49,6 +50,7 @@ declare module '@tiptap/core' {
* Raw
*/
addCommands?: (this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -59,6 +61,7 @@ declare module '@tiptap/core' {
* Keyboard shortcuts
*/
addKeyboardShortcuts?: (this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -71,6 +74,7 @@ declare module '@tiptap/core' {
* Input rules
*/
addInputRules?: (this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -81,6 +85,7 @@ declare module '@tiptap/core' {
* Paste rules
*/
addPasteRules?: (this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -91,6 +96,7 @@ declare module '@tiptap/core' {
* ProseMirror plugins
*/
addProseMirrorPlugins?: (this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -102,6 +108,7 @@ declare module '@tiptap/core' {
*/
extendNodeSchema?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['extendNodeSchema'],
},
@@ -115,6 +122,7 @@ declare module '@tiptap/core' {
*/
extendMarkSchema?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['extendMarkSchema'],
},
@@ -127,6 +135,7 @@ declare module '@tiptap/core' {
* The editor is not ready yet.
*/
onBeforeCreate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -137,6 +146,7 @@ declare module '@tiptap/core' {
* The editor is ready.
*/
onCreate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -147,6 +157,7 @@ declare module '@tiptap/core' {
* The content has changed.
*/
onUpdate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -157,6 +168,7 @@ declare module '@tiptap/core' {
* The selection has changed.
*/
onSelectionUpdate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -168,6 +180,7 @@ declare module '@tiptap/core' {
*/
onTransaction?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -183,6 +196,7 @@ declare module '@tiptap/core' {
*/
onFocus?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -198,6 +212,7 @@ declare module '@tiptap/core' {
*/
onBlur?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -212,6 +227,7 @@ declare module '@tiptap/core' {
* The editor is destroyed.
*/
onDestroy?: ((this: {
name: string,
options: Options,
editor: Editor,
type: MarkType,
@@ -227,6 +243,7 @@ declare module '@tiptap/core' {
* Inclusive
*/
inclusive?: MarkSpec['inclusive'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['inclusive'],
}) => MarkSpec['inclusive']),
@@ -235,6 +252,7 @@ declare module '@tiptap/core' {
* Excludes
*/
excludes?: MarkSpec['excludes'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['excludes'],
}) => MarkSpec['excludes']),
@@ -243,6 +261,7 @@ declare module '@tiptap/core' {
* Group
*/
group?: MarkSpec['group'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['group'],
}) => MarkSpec['group']),
@@ -251,6 +270,7 @@ declare module '@tiptap/core' {
* Spanning
*/
spanning?: MarkSpec['spanning'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['spanning'],
}) => MarkSpec['spanning']),
@@ -260,6 +280,7 @@ declare module '@tiptap/core' {
*/
parseHTML?: (
this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['parseHTML'],
},
@@ -270,6 +291,7 @@ declare module '@tiptap/core' {
*/
renderHTML?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['renderHTML'],
},
@@ -284,6 +306,7 @@ declare module '@tiptap/core' {
*/
addAttributes?: (
this: {
name: string,
options: Options,
parent: ParentConfig<MarkConfig<Options>>['addAttributes'],
},

View File

@@ -41,6 +41,7 @@ declare module '@tiptap/core' {
* Global attributes
*/
addGlobalAttributes?: (this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['addGlobalAttributes'],
}) => GlobalAttributes | {},
@@ -49,6 +50,7 @@ declare module '@tiptap/core' {
* Raw
*/
addCommands?: (this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -59,6 +61,7 @@ declare module '@tiptap/core' {
* Keyboard shortcuts
*/
addKeyboardShortcuts?: (this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -71,6 +74,7 @@ declare module '@tiptap/core' {
* Input rules
*/
addInputRules?: (this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -81,6 +85,7 @@ declare module '@tiptap/core' {
* Paste rules
*/
addPasteRules?: (this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -91,6 +96,7 @@ declare module '@tiptap/core' {
* ProseMirror plugins
*/
addProseMirrorPlugins?: (this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -102,6 +108,7 @@ declare module '@tiptap/core' {
*/
extendNodeSchema?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['extendNodeSchema'],
},
@@ -115,6 +122,7 @@ declare module '@tiptap/core' {
*/
extendMarkSchema?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['extendMarkSchema'],
},
@@ -126,7 +134,8 @@ declare module '@tiptap/core' {
/**
* The editor is not ready yet.
*/
onBeforeCreate?: ((this: {
onBeforeCreate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -137,6 +146,7 @@ declare module '@tiptap/core' {
* The editor is ready.
*/
onCreate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -147,6 +157,7 @@ declare module '@tiptap/core' {
* The content has changed.
*/
onUpdate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -157,6 +168,7 @@ declare module '@tiptap/core' {
* The selection has changed.
*/
onSelectionUpdate?: ((this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -168,6 +180,7 @@ declare module '@tiptap/core' {
*/
onTransaction?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -183,6 +196,7 @@ declare module '@tiptap/core' {
*/
onFocus?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -198,6 +212,7 @@ declare module '@tiptap/core' {
*/
onBlur?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -212,6 +227,7 @@ declare module '@tiptap/core' {
* The editor is destroyed.
*/
onDestroy?: ((this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -222,6 +238,7 @@ declare module '@tiptap/core' {
* Node View
*/
addNodeView?: ((this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -237,6 +254,7 @@ declare module '@tiptap/core' {
* Content
*/
content?: NodeSpec['content'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['content'],
}) => NodeSpec['content']),
@@ -245,6 +263,7 @@ declare module '@tiptap/core' {
* Marks
*/
marks?: NodeSpec['marks'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['marks'],
}) => NodeSpec['marks']),
@@ -253,6 +272,7 @@ declare module '@tiptap/core' {
* Group
*/
group?: NodeSpec['group'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['group'],
}) => NodeSpec['group']),
@@ -261,6 +281,7 @@ declare module '@tiptap/core' {
* Inline
*/
inline?: NodeSpec['inline'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['inline'],
}) => NodeSpec['inline']),
@@ -269,6 +290,7 @@ declare module '@tiptap/core' {
* Atom
*/
atom?: NodeSpec['atom'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['atom'],
}) => NodeSpec['atom']),
@@ -277,6 +299,7 @@ declare module '@tiptap/core' {
* Selectable
*/
selectable?: NodeSpec['selectable'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['selectable'],
}) => NodeSpec['selectable']),
@@ -285,6 +308,7 @@ declare module '@tiptap/core' {
* Draggable
*/
draggable?: NodeSpec['draggable'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['draggable'],
}) => NodeSpec['draggable']),
@@ -293,6 +317,7 @@ declare module '@tiptap/core' {
* Code
*/
code?: NodeSpec['code'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['code'],
}) => NodeSpec['code']),
@@ -301,6 +326,7 @@ declare module '@tiptap/core' {
* Defining
*/
defining?: NodeSpec['defining'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['defining'],
}) => NodeSpec['defining']),
@@ -309,6 +335,7 @@ declare module '@tiptap/core' {
* Isolating
*/
isolating?: NodeSpec['isolating'] | ((this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['isolating'],
}) => NodeSpec['isolating']),
@@ -318,6 +345,7 @@ declare module '@tiptap/core' {
*/
parseHTML?: (
this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['parseHTML'],
},
@@ -328,6 +356,7 @@ declare module '@tiptap/core' {
*/
renderHTML?: ((
this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['renderHTML'],
},
@@ -342,6 +371,7 @@ declare module '@tiptap/core' {
*/
renderText?: ((
this: {
name: string,
options: Options,
editor: Editor,
type: NodeType,
@@ -357,6 +387,7 @@ declare module '@tiptap/core' {
*/
addAttributes?: (
this: {
name: string,
options: Options,
parent: ParentConfig<NodeConfig<Options>>['addAttributes'],
},

View File

@@ -28,6 +28,7 @@ export default function getAttributesFromExtensions(extensions: Extensions): Ext
extensions.forEach(extension => {
const context = {
name: extension.name,
options: extension.options,
}
@@ -64,6 +65,7 @@ export default function getAttributesFromExtensions(extensions: Extensions): Ext
nodeAndMarkExtensions.forEach(extension => {
const context = {
name: extension.name,
options: extension.options,
}

View File

@@ -27,6 +27,7 @@ export default function getSchema(extensions: Extensions): Schema {
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name)
const context = {
name: extension.name,
options: extension.options,
}
@@ -82,6 +83,7 @@ export default function getSchema(extensions: Extensions): Schema {
const marks = Object.fromEntries(markExtensions.map(extension => {
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name)
const context = {
name: extension.name,
options: extension.options,
}

View File

@@ -12,7 +12,10 @@ export default function isList(name: string, extensions: Extensions): boolean {
return false
}
const context = { options: extension.options }
const context = {
name: extension.name,
options: extension.options,
}
const group = callOrReturn(getExtensionField<NodeConfig['group']>(extension, 'group', context))
if (typeof group !== 'string') {