forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodex.ts
143 lines (121 loc) · 3.26 KB
/
codex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
import { EditorConfig } from '../types';
/**
* Apply polyfills
*/
import '@babel/register';
import './components/polyfills';
import Core from './components/core';
import * as _ from './components/utils';
import { destroy as destroyTooltip } from './components/utils/tooltip';
declare const VERSION: string;
/**
* Editor.js
*
* @license Apache-2.0
* @see Editor.js <https://editorjs.io>
* @author CodeX Team <https://codex.so>
*/
export default class EditorJS {
/**
* Promise that resolves when core modules are ready and UI is rendered on the page
*/
public isReady: Promise<void>;
/**
* Stores destroy method implementation.
* Clear heap occupied by Editor and remove UI components from the DOM.
*/
public destroy: () => void;
/** Editor version */
public static get version(): string {
return VERSION;
}
/**
* @param {EditorConfig|string|undefined} [configuration] - user configuration
*/
constructor(configuration?: EditorConfig|string) {
/**
* Set default onReady function
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
let onReady = (): void => {};
/**
* If `onReady` was passed in `configuration` then redefine onReady function
*/
if (_.isObject(configuration) && _.isFunction(configuration.onReady)) {
onReady = configuration.onReady;
}
/**
* Create a Editor.js 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<void>}
*/
this.isReady = editor.isReady.then(() => {
this.exportAPI(editor);
/**
* @todo pass API as an argument. It will allow to use Editor's API when editor is ready
*/
onReady();
});
}
/**
* Export external API methods
*
* @param {Core} editor — Editor's instance
*/
public exportAPI(editor: Core): void {
const fieldsToExport = [ 'configuration' ];
const destroy = (): void => {
Object.values(editor.moduleInstances)
.forEach((moduleInstance) => {
if (_.isFunction(moduleInstance.destroy)) {
moduleInstance.destroy();
}
moduleInstance.listeners.removeAll();
});
destroyTooltip();
editor = null;
for (const field in this) {
if (Object.prototype.hasOwnProperty.call(this, field)) {
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;
const shorthands = {
blocks: {
clear: 'clear',
render: 'render',
},
caret: {
focus: 'focus',
},
events: {
on: 'on',
off: 'off',
emit: 'emit',
},
saver: {
save: 'save',
},
};
Object.entries(shorthands)
.forEach(([key, methods]) => {
Object.entries(methods)
.forEach(([name, alias]) => {
this[alias] = editor.moduleInstances.API.methods[key][name];
});
});
}
}