mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-23 00:27:49 +08:00
35 lines
858 B
TypeScript
35 lines
858 B
TypeScript
export default function magicMethods(Clazz: any) {
|
|
const classHandler = Object.create(null)
|
|
|
|
classHandler.construct = (_: any, args: any) => {
|
|
const instance = new Clazz(...args)
|
|
const instanceHandler = Object.create(null)
|
|
const get = Object.getOwnPropertyDescriptor(Clazz.prototype, '__get')
|
|
|
|
if (get) {
|
|
instanceHandler.get = (target: any, name: any) => {
|
|
if (typeof name !== 'string') {
|
|
return
|
|
}
|
|
|
|
const exists = name in target
|
|
|| name.startsWith('_')
|
|
|| ['then', 'catch'].includes(name)
|
|
|
|
if (exists) {
|
|
return target[name]
|
|
}
|
|
|
|
return get.value.call(target, name)
|
|
}
|
|
}
|
|
|
|
instance.proxy = new Proxy(instance, instanceHandler)
|
|
instance.emit('createdProxy')
|
|
|
|
return instance.proxy
|
|
}
|
|
|
|
return new Proxy(Clazz, classHandler)
|
|
}
|