remove proxy

This commit is contained in:
Philipp Kühn
2021-02-16 18:54:44 +01:00
parent 21b93e65f8
commit 744dab5601
2 changed files with 3 additions and 58 deletions

View File

@@ -1,7 +1,6 @@
import { EditorState, Plugin, Transaction } from 'prosemirror-state' import { EditorState, Plugin, Transaction } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view' import { EditorView } from 'prosemirror-view'
import { Schema, DOMParser, Node } from 'prosemirror-model' import { Schema, DOMParser, Node } from 'prosemirror-model'
import magicMethods from './utilities/magicMethods'
import elementFromString from './utilities/elementFromString' import elementFromString from './utilities/elementFromString'
import getNodeAttributes from './helpers/getNodeAttributes' import getNodeAttributes from './helpers/getNodeAttributes'
import getMarkAttributes from './helpers/getMarkAttributes' import getMarkAttributes from './helpers/getMarkAttributes'
@@ -29,11 +28,8 @@ export interface HTMLElement {
editor?: Editor editor?: Editor
} }
@magicMethods
export class Editor extends EventEmitter { export class Editor extends EventEmitter {
private proxy!: Editor
private commandManager!: CommandManager private commandManager!: CommandManager
public extensionManager!: ExtensionManager public extensionManager!: ExtensionManager
@@ -69,13 +65,6 @@ export class Editor extends EventEmitter {
constructor(options: Partial<EditorOptions> = {}) { constructor(options: Partial<EditorOptions> = {}) {
super() super()
this.options = { ...this.options, ...options } this.options = { ...this.options, ...options }
this.on('createdProxy', this.init)
}
/**
* This method is called after the proxy is initialized.
*/
private init(): void {
this.createExtensionManager() this.createExtensionManager()
this.createCommandManager() this.createCommandManager()
this.createSchema() this.createSchema()
@@ -95,16 +84,6 @@ export class Editor extends EventEmitter {
}, 0) }, 0)
} }
/**
* A magic method to call commands.
*
* @param name The name of the command
*/
// eslint-disable-next-line
private __get(name: string) {
// TODO: maybe remove proxy
}
/** /**
* An object of all registered commands. * An object of all registered commands.
*/ */
@@ -201,14 +180,14 @@ export class Editor extends EventEmitter {
return ['extension', 'node', 'mark'].includes(extension?.type) return ['extension', 'node', 'mark'].includes(extension?.type)
}) })
this.extensionManager = new ExtensionManager(allExtensions, this.proxy) this.extensionManager = new ExtensionManager(allExtensions, this)
} }
/** /**
* Creates an command manager. * Creates an command manager.
*/ */
private createCommandManager(): void { private createCommandManager(): void {
this.commandManager = new CommandManager(this.proxy, this.extensionManager.commands) this.commandManager = new CommandManager(this, this.extensionManager.commands)
} }
/** /**
@@ -243,7 +222,7 @@ export class Editor extends EventEmitter {
// Lets store the editor instance in the DOM element. // Lets store the editor instance in the DOM element.
// So well have access to it for tests. // So well have access to it for tests.
const dom = this.view.dom as HTMLElement const dom = this.view.dom as HTMLElement
dom.editor = this.proxy dom.editor = this
} }
/** /**

View File

@@ -1,34 +0,0 @@
export default function magicMethods(Clazz: any): 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)
}