mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-21 07:18:05 +08:00
6049985c04
* refactor(core): refactor extension manager * refactor(core): update comment for setupExtensions function
33 lines
941 B
TypeScript
33 lines
941 B
TypeScript
import { AnyExtension, MaybeThisParameterType, RemoveThis } from '../types.js'
|
|
|
|
/**
|
|
* Returns a field from an extension
|
|
* @param extension The Tiptap extension
|
|
* @param field The field, for example `renderHTML` or `priority`
|
|
* @param context The context object that should be passed as `this` into the function
|
|
* @returns The field value
|
|
*/
|
|
export function getExtensionField<T = any>(
|
|
extension: AnyExtension,
|
|
field: string,
|
|
context?: Omit<MaybeThisParameterType<T>, 'parent'>,
|
|
): RemoveThis<T> {
|
|
|
|
if (extension.config[field] === undefined && extension.parent) {
|
|
return getExtensionField(extension.parent, field, context)
|
|
}
|
|
|
|
if (typeof extension.config[field] === 'function') {
|
|
const value = extension.config[field].bind({
|
|
...context,
|
|
parent: extension.parent
|
|
? getExtensionField(extension.parent, field, context)
|
|
: null,
|
|
})
|
|
|
|
return value
|
|
}
|
|
|
|
return extension.config[field]
|
|
}
|