editor.js/src/codex.js
Murod Khaydarov 7acf321454
Mutation callback (#444)
* modification observer initials

* add debouncer to callback execution

* change feature name

* update

* code improvements

* tslint fixes

* use debouncer from utils

* add types

* upgrade

* fix

* update
2018-09-05 00:34:11 +03:00

90 lines
1.9 KiB
JavaScript

'use strict';
/**
* Apply polyfills
*/
import 'babel-core/register';
if (!window || !window._babelPolyfill) require('babel-polyfill');
import 'components/polyfills';
import Core from './components/core';
/**
* Codex Editor
*
* Short Description (눈_눈;)
* @version 2.0
*
* @author CodeX-Team <https://ifmo.su>
*/
export default class CodexEditor {
/** Editor version */
static get version() {
return VERSION;
}
/**
* @constructor
*
* @param {EditorConfig|String} [configuration] - user configuration
*/
constructor(configuration) {
/**
* Set default onReady function
*/
let onReady = () => {};
/**
* If `onReady` was passed in `configuration` then redefine onReady function
*/
if (typeof configuration === 'object' && typeof configuration.onReady === 'function') {
onReady = configuration.onReady;
}
/**
* Create a CodeX Editor instance
*/
const editor = new Core(configuration);
/**
* We need to export isReady promise in the constructor
* as it can be used before other API methods are exported
* @type {Promise<any | never>}
*/
this.isReady = editor.isReady.then(() => {
this.exportAPI(editor);
onReady();
});
}
/**
* Export external API methods
*
* @param editor
*/
exportAPI(editor) {
const fieldsToExport = [ 'configuration' ];
const destroy = () => {
editor.moduleInstances.Listeners.removeAll();
editor.moduleInstances.UI.destroy();
editor.moduleInstances.ModificationsObserver.destroy();
editor = null;
for (const field in this) {
delete this[field];
}
Object.setPrototypeOf(this, null);
};
fieldsToExport.forEach(field => {
this[field] = editor[field];
});
this.destroy = destroy;
Object.setPrototypeOf(this, editor.moduleInstances.API.methods);
delete this['exportAPI'];
}
}