Skip to content

Commit

Permalink
feat: add parentConfig to extension context for more extendable exten…
Browse files Browse the repository at this point in the history
…sions, fix #259
  • Loading branch information
philippkuehn committed Apr 12, 2021
1 parent 8090bc6 commit 5e1ec5d
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 42 deletions.
26 changes: 24 additions & 2 deletions packages/core/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InputRule } from 'prosemirror-inputrules'
import { Editor } from './Editor'
import { Node } from './Node'
import mergeDeep from './utilities/mergeDeep'
import { GlobalAttributes, RawCommands } from './types'
import { GlobalAttributes, RawCommands, ParentConfig } from './types'
import { ExtensionConfig } from '.'

declare module '@tiptap/core' {
Expand All @@ -31,6 +31,7 @@ declare module '@tiptap/core' {
*/
addGlobalAttributes?: (this: {
options: Options,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => GlobalAttributes | {},

/**
Expand All @@ -39,6 +40,7 @@ declare module '@tiptap/core' {
addCommands?: (this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => Partial<RawCommands>,

/**
Expand All @@ -47,6 +49,7 @@ declare module '@tiptap/core' {
addKeyboardShortcuts?: (this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => {
[key: string]: ProseMirrorCommand,
},
Expand All @@ -57,6 +60,7 @@ declare module '@tiptap/core' {
addInputRules?: (this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => InputRule[],

/**
Expand All @@ -65,6 +69,7 @@ declare module '@tiptap/core' {
addPasteRules?: (this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => Plugin[],

/**
Expand All @@ -73,6 +78,7 @@ declare module '@tiptap/core' {
addProseMirrorPlugins?: (this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => Plugin[],

/**
Expand All @@ -81,6 +87,7 @@ declare module '@tiptap/core' {
extendNodeSchema?: ((
this: {
options: Options,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
},
extension: Node,
) => {
Expand All @@ -93,6 +100,7 @@ declare module '@tiptap/core' {
extendMarkSchema?: ((
this: {
options: Options,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
},
extension: Node,
) => {
Expand All @@ -105,6 +113,7 @@ declare module '@tiptap/core' {
onBeforeCreate?: ((this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => void) | null,

/**
Expand All @@ -113,6 +122,7 @@ declare module '@tiptap/core' {
onCreate?: ((this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => void) | null,

/**
Expand All @@ -121,6 +131,7 @@ declare module '@tiptap/core' {
onUpdate?: ((this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => void) | null,

/**
Expand All @@ -129,6 +140,7 @@ declare module '@tiptap/core' {
onSelectionUpdate?: ((this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => void) | null,

/**
Expand All @@ -138,6 +150,7 @@ declare module '@tiptap/core' {
this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
},
props: {
transaction: Transaction,
Expand All @@ -151,6 +164,7 @@ declare module '@tiptap/core' {
this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
},
props: {
event: FocusEvent,
Expand All @@ -164,6 +178,7 @@ declare module '@tiptap/core' {
this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
},
props: {
event: FocusEvent,
Expand All @@ -176,6 +191,7 @@ declare module '@tiptap/core' {
onDestroy?: ((this: {
options: Options,
editor: Editor,
parentConfig: ParentConfig<ExtensionConfig<Options>>,
}) => void) | null,
}
}
Expand All @@ -189,6 +205,8 @@ export class Extension<Options = any> {
defaultOptions: {},
}

parentConfig: Partial<ExtensionConfig> = {}

options!: Options

constructor(config: ExtensionConfig<Options>) {
Expand Down Expand Up @@ -217,9 +235,13 @@ export class Extension<Options = any> {
}

extend<ExtendedOptions = Options>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions>>) {
return new Extension<ExtendedOptions>({
const extension = new Extension<ExtendedOptions>({
...this.config,
...extendedConfig,
} as ExtensionConfig<ExtendedOptions>)

extension.parentConfig = this.config

return extension
}
}
21 changes: 11 additions & 10 deletions packages/core/src/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EditorView, Decoration } from 'prosemirror-view'
import { Plugin } from 'prosemirror-state'
import { Editor } from './Editor'
import { Extensions, NodeViewRenderer, RawCommands } from './types'
import createExtensionContext from './helpers/createExtensionContext'
import getSchema from './helpers/getSchema'
import getSchemaTypeByName from './helpers/getSchemaTypeByName'
import getNodeType from './helpers/getNodeType'
Expand All @@ -29,11 +30,11 @@ export default class ExtensionManager {
this.schema = getSchema(this.extensions)

this.extensions.forEach(extension => {
const context = {
const context = createExtensionContext(extension, {
options: extension.options,
editor: this.editor,
type: getSchemaTypeByName(extension.config.name, this.schema),
}
})

if (extension.type === 'mark') {
const keepOnSplit = callOrReturn(extension.config.keepOnSplit, context) ?? true
Expand Down Expand Up @@ -95,11 +96,11 @@ export default class ExtensionManager {

get commands(): RawCommands {
return this.extensions.reduce((commands, extension) => {
const context = {
const context = createExtensionContext(extension, {
options: extension.options,
editor: this.editor,
type: getSchemaTypeByName(extension.config.name, this.schema),
}
})

if (!extension.config.addCommands) {
return commands
Expand All @@ -116,11 +117,11 @@ export default class ExtensionManager {
return [...this.extensions]
.reverse()
.map(extension => {
const context = {
const context = createExtensionContext(extension, {
options: extension.options,
editor: this.editor,
type: getSchemaTypeByName(extension.config.name, this.schema),
}
})

const plugins: Plugin[] = []

Expand Down Expand Up @@ -168,11 +169,11 @@ export default class ExtensionManager {
.filter(extension => !!extension.config.addNodeView)
.map(extension => {
const extensionAttributes = this.attributes.filter(attribute => attribute.type === extension.config.name)
const context = {
const context = createExtensionContext(extension, {
options: extension.options,
editor,
type: getNodeType(extension.config.name, this.schema),
}
})
const renderer = extension.config.addNodeView?.call(context) as NodeViewRenderer

const nodeview = (
Expand Down Expand Up @@ -204,11 +205,11 @@ export default class ExtensionManager {
return Object.fromEntries(nodeExtensions
.filter(extension => !!extension.config.renderText)
.map(extension => {
const context = {
const context = createExtensionContext(extension, {
options: extension.options,
editor,
type: getNodeType(extension.config.name, this.schema),
}
})

const textSerializer = (props: { node: ProsemirrorNode }) => extension.config.renderText?.call(context, props)

Expand Down
Loading

0 comments on commit 5e1ec5d

Please sign in to comment.