2023-07-01 03:03:49 +08:00
|
|
|
import { AnyExtension, MaybeThisParameterType, RemoveThis } from '../types.js'
|
2021-04-16 03:14:33 +08:00
|
|
|
|
2024-05-14 20:43:53 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-12-06 19:00:09 +08:00
|
|
|
export function getExtensionField<T = any>(
|
2021-04-16 03:14:33 +08:00
|
|
|
extension: AnyExtension,
|
|
|
|
field: string,
|
2021-10-22 14:52:54 +08:00
|
|
|
context?: Omit<MaybeThisParameterType<T>, 'parent'>,
|
2021-04-16 03:14:33 +08:00
|
|
|
): 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]
|
|
|
|
}
|