refactoring
This commit is contained in:
@@ -8,15 +8,13 @@ import { baseKeymap } from 'prosemirror-commands'
|
|||||||
import { dropCursor } from 'prosemirror-dropcursor'
|
import { dropCursor } from 'prosemirror-dropcursor'
|
||||||
import { gapCursor } from 'prosemirror-gapcursor'
|
import { gapCursor } from 'prosemirror-gapcursor'
|
||||||
|
|
||||||
|
import magicMethods from './utils/magicMethods'
|
||||||
import elementFromString from './utils/elementFromString'
|
import elementFromString from './utils/elementFromString'
|
||||||
import injectCSS from './utils/injectCSS'
|
import injectCSS from './utils/injectCSS'
|
||||||
import ExtensionManager from './ExtensionManager'
|
import ExtensionManager from './ExtensionManager'
|
||||||
import Extension from './Extension'
|
import Extension from './Extension'
|
||||||
import Node from './Node'
|
import Node from './Node'
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
import magicMethods from './utils/magicMethods'
|
|
||||||
|
|
||||||
type EditorContent = string | JSON
|
type EditorContent = string | JSON
|
||||||
type Command = (next: Function, editor: Editor, ...args: any) => any
|
type Command = (next: Function, editor: Editor, ...args: any) => any
|
||||||
|
|
||||||
@@ -29,6 +27,7 @@ interface Options {
|
|||||||
@magicMethods
|
@magicMethods
|
||||||
export class Editor extends EventEmitter {
|
export class Editor extends EventEmitter {
|
||||||
|
|
||||||
|
proxy!: any
|
||||||
element = document.createElement('div')
|
element = document.createElement('div')
|
||||||
extensionManager!: ExtensionManager
|
extensionManager!: ExtensionManager
|
||||||
schema!: Schema
|
schema!: Schema
|
||||||
@@ -78,7 +77,6 @@ export class Editor extends EventEmitter {
|
|||||||
return new Promise(resolve => callback(resolve, this, ...args))
|
return new Promise(resolve => callback(resolve, this, ...args))
|
||||||
})
|
})
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
return this.proxy
|
return this.proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +121,6 @@ export class Editor extends EventEmitter {
|
|||||||
.then(() => method.apply(this, args))
|
.then(() => method.apply(this, args))
|
||||||
.catch(console.error)
|
.catch(console.error)
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
return this.proxy
|
return this.proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,94 +0,0 @@
|
|||||||
// export default function magicMethods (clazz) {
|
|
||||||
// const classHandler = Object.create(null)
|
|
||||||
|
|
||||||
// // Trap for class instantiation
|
|
||||||
// classHandler.construct = (target, args) => {
|
|
||||||
// // Wrapped class instance
|
|
||||||
// const instance = new clazz(...args)
|
|
||||||
// // Instance traps
|
|
||||||
// const instanceHandler = Object.create(null)
|
|
||||||
|
|
||||||
// const get = Object.getOwnPropertyDescriptor(clazz.prototype, 'command')
|
|
||||||
// if (get) {
|
|
||||||
// instanceHandler.get = (target, name) => {
|
|
||||||
// const exists = name in target
|
|
||||||
|
|
||||||
// if (exists) {
|
|
||||||
// return target[name]
|
|
||||||
// } else {
|
|
||||||
// return get.value.call(target, name)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return new Proxy(instance, instanceHandler)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return new Proxy(clazz, classHandler)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// export default function magicMethods (clazz) {
|
|
||||||
// const classHandler = Object.create(null)
|
|
||||||
|
|
||||||
// // Trap for class instantiation
|
|
||||||
// classHandler.construct = (target, args) => {
|
|
||||||
// // Wrapped class instance
|
|
||||||
// const instance = new clazz(...args)
|
|
||||||
// // Instance traps
|
|
||||||
// const instanceHandler = Object.create(null)
|
|
||||||
|
|
||||||
// const get = Object.getOwnPropertyDescriptor(clazz.prototype, 'command')
|
|
||||||
// if (get) {
|
|
||||||
// instanceHandler.get = (target, name) => {
|
|
||||||
// const exists = name in target
|
|
||||||
|
|
||||||
// if (exists) {
|
|
||||||
// return target[name]
|
|
||||||
// } else {
|
|
||||||
// return get.value.call(target, name)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // return new Proxy(instance, instanceHandler)
|
|
||||||
// const proxy = new Proxy(instance, instanceHandler)
|
|
||||||
// instance.proxy = proxy
|
|
||||||
|
|
||||||
// return proxy
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return new Proxy(clazz, classHandler)
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
export default function magicMethods (clazz) {
|
|
||||||
const classHandler = Object.create(null)
|
|
||||||
|
|
||||||
classHandler.construct = (target, args) => {
|
|
||||||
const instance = new clazz(...args)
|
|
||||||
const instanceHandler = Object.create(null)
|
|
||||||
const get = Object.getOwnPropertyDescriptor(clazz.prototype, '__get')
|
|
||||||
|
|
||||||
if (get) {
|
|
||||||
instanceHandler.get = (target, name) => {
|
|
||||||
if (typeof name !== 'string') {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const exists = name in target || name.startsWith('_')
|
|
||||||
|
|
||||||
if (exists) {
|
|
||||||
return target[name]
|
|
||||||
} else {
|
|
||||||
return get.value.call(target, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
instance.proxy = new Proxy(instance, instanceHandler)
|
|
||||||
|
|
||||||
return instance.proxy
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Proxy(clazz, classHandler)
|
|
||||||
}
|
|
||||||
31
packages/tiptap-core/src/utils/magicMethods.ts
Normal file
31
packages/tiptap-core/src/utils/magicMethods.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
export default function magicMethods (clazz: any) {
|
||||||
|
const classHandler = Object.create(null)
|
||||||
|
|
||||||
|
classHandler.construct = (target: 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('_')
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
return target[name]
|
||||||
|
} else {
|
||||||
|
return get.value.call(target, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.proxy = new Proxy(instance, instanceHandler)
|
||||||
|
|
||||||
|
return instance.proxy
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Proxy(clazz, classHandler)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user