Browse Source

Convert EditorModulue to ts

pull/4264/head
Alex Ritter 4 years ago
parent
commit
f112045d15
  1. 2
      src/abstract/View.ts
  2. 261
      src/editor/index.ts
  3. 36
      src/editor/model/Editor.js

2
src/abstract/View.ts

@ -6,7 +6,7 @@ export default class View<
TElement extends Element = HTMLElement TElement extends Element = HTMLElement
> extends Backbone.View<TModel, TElement> { > extends Backbone.View<TModel, TElement> {
protected get pfx() { protected get pfx() {
return this.model.module.em.config.stylePrefix || ""; return (this.model.module.em.config as any).stylePrefix || "";
} }
protected get ppfx() { protected get ppfx() {

261
src/editor/index.js → src/editor/index.ts

@ -54,13 +54,15 @@
* ## Methods * ## Methods
* @module Editor * @module Editor
*/ */
import { EventHandler } from 'backbone';
import cash from '../utils/cash-dom';
import html from '../utils/html'; import html from '../utils/html';
import defaults from './config/config'; import defaults from './config/config';
import EditorModel from './model/Editor'; import EditorModel from './model/Editor';
import EditorView from './view/EditorView'; import EditorView from './view/EditorView';
export default class EditorModule { export default class EditorModule {
constructor(config = {}, opts = {}) { constructor(config = {}, opts: any = {}) {
this.$ = opts.$; this.$ = opts.$;
this.c = { ...defaults, ...config }; this.c = { ...defaults, ...config };
this.c.pStylePrefix = this.c.stylePrefix; this.c.pStylePrefix = this.c.stylePrefix;
@ -68,51 +70,13 @@ export default class EditorModule {
this.em.init(this); this.em.init(this);
this.editor = this.em; this.editor = this.em;
this.modules = [
'I18n',
'Utils',
'Config',
'Commands',
'Keymaps',
'Modal',
'Panels',
'Canvas',
'Parser',
'CodeManager',
'UndoManager',
'RichTextEditor',
['Pages', 'PageManager'],
'DomComponents',
['Components', 'DomComponents'],
'LayerManager',
['Layers', 'LayerManager'],
'CssComposer',
['Css', 'CssComposer'],
'StorageManager',
['Storage', 'StorageManager'],
'AssetManager',
['Assets', 'AssetManager'],
'BlockManager',
['Blocks', 'BlockManager'],
'TraitManager',
['Traits', 'TraitManager'],
'SelectorManager',
['Selectors', 'SelectorManager'],
'StyleManager',
['Styles', 'StyleManager'],
'DeviceManager',
['Devices', 'DeviceManager'],
];
this.modules.forEach(prop => {
if (Array.isArray(prop)) {
this[prop[0]] = this.em.get(prop[1]);
} else {
this[prop] = this.em.get(prop);
}
});
} }
editorView; editorView?: EditorView;
em: EditorModel;
editor: EditorModel;
c: any;
$: cash;
/** /**
* @property {EditorModel} * @property {EditorModel}
@ -122,13 +86,145 @@ export default class EditorModule {
modules = []; modules = [];
//@ts-ignore
get I18n(): I18nModule {
return this.em.get("I18n");
}
//@ts-ignore
get Utils(): UtilsModule {
return this.em.get("Utils");
}
get Config(): any {
return this.em.config;
}
//@ts-ignore
get Commands(): CommandsModule {
return this.em.get("Commands");
}
//@ts-ignore
get Keymaps(): KeymapsModule {
return this.em.get("Keymaps");
}
//@ts-ignore
get Modal(): ModalModule {
return this.em.get("Modal");
}
//@ts-ignore
get Panels(): PanelsModule {
return this.em.get("Panels");
}
//@ts-ignore
get Canvas(): CanvasModule {
return this.em.get("Canvas");
}
//@ts-ignore
get Parser(): ParserModule {
return this.em.get("Parser");
}
//@ts-ignore
get CodeManager(): CodeManagerModule {
return this.em.get("CodeManager");
}
//@ts-ignore
get UndoManager(): UndoManagerModule {
return this.em.get("UndoManager");
}
//@ts-ignore
get RichTextEditor(): RichTextEditorModule {
return this.em.get("RichTextEditor");
}
//@ts-ignore
get Pages(): PageManagerModule {
return this.em.get("PageManager");
}
//@ts-ignore
get Components(): DomComponentsModule {
return this.em.get("DomComponents");
}
//@ts-ignore
get DomComponents(): DomComponentsModule {
return this.em.get("DomComponents");
}
//@ts-ignore
get Layers(): LayerManagerModule {
return this.em.get("LayerManager");
}
//@ts-ignore
get LayerManager(): LayerManagerModule {
return this.em.get("LayerManager");
}
//@ts-ignore
get Css(): CssComposerModule {
return this.em.get("CssComposer");
}
//@ts-ignore
get CssComposer(): CssComposerModule {
return this.em.get("CssComposer");
}
//@ts-ignore
get Storage(): StorageManagerModule {
return this.em.get("StorageManager");
}
//@ts-ignore
get StorageManager(): StorageManagerModule {
return this.em.get("StorageManager");
}
//@ts-ignore
get Assets(): AssetManagerModule {
return this.em.get("AssetManager");
}
//@ts-ignore
get AssetManager(): AssetManagerModule {
return this.em.get("AssetManager");
}
//@ts-ignore
get Blocks(): BlockManagerModule {
return this.em.get("BlockManager");
}
//@ts-ignore
get BlockManager(): BlockManagerModule {
return this.em.get("BlockManager");
}
//@ts-ignore
get Traits(): TraitManagerModule {
return this.em.get("TraitManager");
}
//@ts-ignore
get TraitManager(): TraitManagerModule {
return this.em.get("TraitManager");
}
//@ts-ignore
get Selectors(): SelectorManagerCollectionModule {
return this.em.get("SelectorManager");
}
//@ts-ignore
get SelectorManager(): SelectorManagerCollectionModule {
return this.em.get("SelectorManager");
}
//@ts-ignore
get Styles(): StyleManagerModule {
return this.em.get("StyleManager");
}
//@ts-ignore
get StyleManager(): StyleManagerModule {
return this.em.get("StyleManager");
}
//@ts-ignore
get Devices(): DeviceManagerModule {
return this.em.get("DeviceManager");
}
//@ts-ignore
get DeviceManager(): DeviceManagerModule {
return this.em.get("DeviceManager");
}
/** /**
* Returns configuration object * Returns configuration object
* @param {string} [prop] Property name * @param {string} [prop] Property name
* @returns {any} Returns the configuration object or * @returns {any} Returns the configuration object or
* the value of the specified property * the value of the specified property
*/ */
getConfig(prop) { getConfig(prop: string) {
return this.em.getConfig(prop); return this.em.getConfig(prop);
} }
@ -139,7 +235,7 @@ export default class EditorModule {
* @param {Boolean} [opts.cleanId=false] Remove unnecessary IDs (eg. those created automatically) * @param {Boolean} [opts.cleanId=false] Remove unnecessary IDs (eg. those created automatically)
* @returns {string} HTML string * @returns {string} HTML string
*/ */
getHtml(opts) { getHtml(opts: any) {
return this.em.getHtml(opts); return this.em.getHtml(opts);
} }
@ -153,7 +249,7 @@ export default class EditorModule {
* @param {Boolean} [opts.keepUnusedStyles=false] Force keep all defined rules. Toggle on in case output looks different inside/outside of the editor. * @param {Boolean} [opts.keepUnusedStyles=false] Force keep all defined rules. Toggle on in case output looks different inside/outside of the editor.
* @returns {String|Array<CssRule>} CSS string or array of CssRules * @returns {String|Array<CssRule>} CSS string or array of CssRules
*/ */
getCss(opts) { getCss(opts: any) {
return this.em.getCss(opts); return this.em.getCss(opts);
} }
@ -163,7 +259,7 @@ export default class EditorModule {
* @param {Component} [opts.component] Get the JS of a specific component * @param {Component} [opts.component] Get the JS of a specific component
* @returns {String} JS string * @returns {String} JS string
*/ */
getJs(opts) { getJs(opts: any) {
return this.em.getJs(opts); return this.em.getJs(opts);
} }
@ -197,7 +293,7 @@ export default class EditorModule {
* content: 'New component' * content: 'New component'
* }); * });
*/ */
setComponents(components, opt = {}) { setComponents(components: any, opt = {}) {
this.em.setComponents(components, opt); this.em.setComponents(components, opt);
return this; return this;
} }
@ -219,7 +315,7 @@ export default class EditorModule {
* content: 'New component' * content: 'New component'
* }); * });
*/ */
addComponents(components, opts) { addComponents(components: any, opts: any) {
return this.getWrapper().append(components, opts); return this.getWrapper().append(components, opts);
} }
@ -243,7 +339,7 @@ export default class EditorModule {
* style: { color: 'red' } * style: { color: 'red' }
* }); * });
*/ */
setStyle(style, opt = {}) { setStyle(style: any, opt = {}) {
this.em.setStyle(style, opt); this.em.setStyle(style, opt);
return this; return this;
} }
@ -255,7 +351,7 @@ export default class EditorModule {
* @example * @example
* editor.addStyle('.cls{color: red}'); * editor.addStyle('.cls{color: red}');
*/ */
addStyle(style, opts = {}) { addStyle(style: any, opts = {}) {
return this.em.addStyle(style, opts); return this.em.addStyle(style, opts);
} }
@ -303,7 +399,7 @@ export default class EditorModule {
* editor.select(model); * editor.select(model);
* }); * });
*/ */
select(el, opts) { select(el: any, opts: any) {
this.em.setSelected(el, opts); this.em.setSelected(el, opts);
return this; return this;
} }
@ -315,7 +411,7 @@ export default class EditorModule {
* @example * @example
* editor.selectAdd(model); * editor.selectAdd(model);
*/ */
selectAdd(el) { selectAdd(el: any) {
this.em.addSelected(el); this.em.addSelected(el);
return this; return this;
} }
@ -327,7 +423,7 @@ export default class EditorModule {
* @example * @example
* editor.selectRemove(model); * editor.selectRemove(model);
*/ */
selectRemove(el) { selectRemove(el: any) {
this.em.removeSelected(el); this.em.removeSelected(el);
return this; return this;
} }
@ -339,7 +435,7 @@ export default class EditorModule {
* @example * @example
* editor.selectToggle(model); * editor.selectToggle(model);
*/ */
selectToggle(el) { selectToggle(el: any) {
this.em.toggleSelected(el); this.em.toggleSelected(el);
return this; return this;
} }
@ -365,7 +461,7 @@ export default class EditorModule {
* @example * @example
* editor.setDevice('Tablet'); * editor.setDevice('Tablet');
*/ */
setDevice(name) { setDevice(name: string) {
this.em.set('device', name); this.em.set('device', name);
return this; return this;
} }
@ -390,7 +486,7 @@ export default class EditorModule {
* @example * @example
* editor.runCommand('myCommand', {someValue: 1}); * editor.runCommand('myCommand', {someValue: 1});
*/ */
runCommand(id, options = {}) { runCommand(id: string, options = {}) {
return this.em.get('Commands').run(id, options); return this.em.get('Commands').run(id, options);
} }
@ -402,7 +498,7 @@ export default class EditorModule {
* @example * @example
* editor.stopCommand('myCommand', {someValue: 1}); * editor.stopCommand('myCommand', {someValue: 1});
*/ */
stopCommand(id, options = {}) { stopCommand(id: string, options = {}) {
return this.em.get('Commands').stop(id, options); return this.em.get('Commands').stop(id, options);
} }
@ -414,7 +510,7 @@ export default class EditorModule {
* @example * @example
* const storedData = await editor.store(); * const storedData = await editor.store();
*/ */
async store(options) { async store(options: any) {
return await this.em.store(options); return await this.em.store(options);
} }
@ -425,7 +521,7 @@ export default class EditorModule {
* @example * @example
* const data = await editor.load(); * const data = await editor.load();
*/ */
async load(options) { async load(options: any) {
return await this.em.load(options); return await this.em.load(options);
} }
@ -446,7 +542,7 @@ export default class EditorModule {
* @example * @example
* editor.loadProjectData({ pages: [...], styles: [...], ... }) * editor.loadProjectData({ pages: [...], styles: [...], ... })
*/ */
loadProjectData(data) { loadProjectData(data: any) {
return this.em.loadData(data); return this.em.loadData(data);
} }
@ -454,7 +550,7 @@ export default class EditorModule {
return this.em.storeData(); return this.em.storeData();
} }
loadData(data) { loadData(data: any) {
return this.em.loadData(data); return this.em.loadData(data);
} }
@ -492,7 +588,7 @@ export default class EditorModule {
* @param {Object} [options] Options * @param {Object} [options] Options
* @param {Boolean} [options.tools=false] Update the position of tools (eg. rich text editor, component highlighter, etc.) * @param {Boolean} [options.tools=false] Update the position of tools (eg. rich text editor, component highlighter, etc.)
*/ */
refresh(opts) { refresh(opts?: any) {
this.em.refreshCanvas(opts); this.em.refreshCanvas(opts);
} }
@ -525,7 +621,7 @@ export default class EditorModule {
* } * }
* }); * });
*/ */
setCustomRte(obj) { setCustomRte(obj: any) {
this.RichTextEditor.customRte = obj; this.RichTextEditor.customRte = obj;
} }
@ -548,7 +644,7 @@ export default class EditorModule {
* return result; * return result;
* }); * });
*/ */
setCustomParserCss(parser) { setCustomParserCss(parser: any) {
this.Parser.getConfig().parserCss = parser; this.Parser.getConfig().parserCss = parser;
return this; return this;
} }
@ -559,7 +655,7 @@ export default class EditorModule {
* @param {String} value Drag mode, options: 'absolute' | 'translate' * @param {String} value Drag mode, options: 'absolute' | 'translate'
* @returns {this} * @returns {this}
*/ */
setDragMode(value) { setDragMode(value: string) {
this.em.setDragMode(value); this.em.setDragMode(value);
return this; return this;
} }
@ -579,7 +675,7 @@ export default class EditorModule {
* // options, as arguments, eg: * // options, as arguments, eg:
* // editor.on('log:info', (msg, opts) => console.info(msg, opts)) * // editor.on('log:info', (msg, opts) => console.info(msg, opts))
*/ */
log(msg, opts = {}) { log(msg: string, opts = {}) {
this.em.log(msg, opts); this.em.log(msg, opts);
return this; return this;
} }
@ -598,7 +694,7 @@ export default class EditorModule {
* // custom local * // custom local
* editor.t('msg2', { params: { test: 'hello' } l: 'it' }); * editor.t('msg2', { params: { test: 'hello' } l: 'it' });
*/ */
t(...args) { t(...args: any[]) {
return this.em.t(...args); return this.em.t(...args);
} }
@ -608,7 +704,7 @@ export default class EditorModule {
* @param {Function} callback Callback function * @param {Function} callback Callback function
* @return {this} * @return {this}
*/ */
on(event, callback) { on(event: string, callback: EventHandler) {
this.em.on(event, callback); this.em.on(event, callback);
return this; return this;
} }
@ -619,7 +715,7 @@ export default class EditorModule {
* @param {Function} callback Callback function * @param {Function} callback Callback function
* @return {this} * @return {this}
*/ */
once(event, callback) { once(event: string, callback: EventHandler) {
this.em.once(event, callback); this.em.once(event, callback);
return this; return this;
} }
@ -630,7 +726,7 @@ export default class EditorModule {
* @param {Function} callback Callback function * @param {Function} callback Callback function
* @return {this} * @return {this}
*/ */
off(event, callback) { off(event: string, callback: EventHandler) {
this.em.off(event, callback); this.em.off(event, callback);
return this; return this;
} }
@ -640,8 +736,8 @@ export default class EditorModule {
* @param {string} event Event to trigger * @param {string} event Event to trigger
* @return {this} * @return {this}
*/ */
trigger(event) { trigger(eventName: string, ...args: any[]) {
this.em.trigger.apply(this.em, arguments); this.em.trigger.apply(this.em, [eventName, ...args]);
return this; return this;
} }
@ -651,17 +747,7 @@ export default class EditorModule {
destroy() { destroy() {
if (!this.em) return; if (!this.em) return;
this.em.destroyAll(); this.em.destroyAll();
this.modules.forEach(prop => { this.editorView = undefined;
if (Array.isArray(prop)) {
this[prop[0]] = 0;
} else {
this[prop] = 0;
}
});
this.modules = 0;
this.editorView = 0;
this.em = 0;
this.c = 0;
} }
/** /**
@ -690,6 +776,7 @@ export default class EditorModule {
this.editorView?.remove(); this.editorView?.remove();
this.editorView = new EditorView({ this.editorView = new EditorView({
model: this.em, model: this.em,
//@ts-ignore
config: this.c, config: this.c,
}); });
return this.editorView.render().el; return this.editorView.render().el;
@ -704,7 +791,7 @@ export default class EditorModule {
* // perform actions * // perform actions
* }); * });
*/ */
onReady(clb) { onReady(clb: EventHandler) {
this.em.get('ready') ? clb(this) : this.em.on('load', clb); this.em.get('ready') ? clb(this) : this.em.on('load', clb);
} }

36
src/editor/model/Editor.js

@ -69,7 +69,7 @@ export default class EditorModel extends Model {
initialize(conf = {}) { initialize(conf = {}) {
this._config = conf; this._config = conf;
const { config } = this; const { config } = this;
this.set('Config', config); this.set('Config', conf);
this.set('modules', []); this.set('modules', []);
this.set('toLoad', []); this.set('toLoad', []);
this.set('storables', []); this.set('storables', []);
@ -159,7 +159,7 @@ export default class EditorModel extends Model {
// Defer for storage load events. // Defer for storage load events.
setTimeout(async () => { setTimeout(async () => {
const projectData = this.getConfig('projectData'); const projectData = this.getConfig().projectData;
if (projectData) { if (projectData) {
this.loadData(projectData); this.loadData(projectData);
@ -242,7 +242,7 @@ export default class EditorModel extends Model {
* Initialize editor model and set editor instance * Initialize editor model and set editor instance
* @param {Editor} editor Editor instance * @param {Editor} editor Editor instance
* @return {this} * @return {this}
* @private * @public
*/ */
init(editor, opts = {}) { init(editor, opts = {}) {
if (this.destroyed) { if (this.destroyed) {
@ -298,7 +298,7 @@ export default class EditorModel extends Model {
/** /**
* Returns model of the selected component * Returns model of the selected component
* @return {Component|null} * @return {Component|null}
* @private * @public
*/ */
getSelected() { getSelected() {
return this.get('selected').lastComponent(); return this.get('selected').lastComponent();
@ -307,7 +307,7 @@ export default class EditorModel extends Model {
/** /**
* Returns an array of all selected components * Returns an array of all selected components
* @return {Array} * @return {Array}
* @private * @public
*/ */
getSelectedAll() { getSelectedAll() {
return this.get('selected').allComponents(); return this.get('selected').allComponents();
@ -317,7 +317,7 @@ export default class EditorModel extends Model {
* Select a component * Select a component
* @param {Component|HTMLElement} el Component to select * @param {Component|HTMLElement} el Component to select
* @param {Object} [opts={}] Options, optional * @param {Object} [opts={}] Options, optional
* @private * @public
*/ */
setSelected(el, opts = {}) { setSelected(el, opts = {}) {
const { event } = opts; const { event } = opts;
@ -402,7 +402,7 @@ export default class EditorModel extends Model {
* Add component to selection * Add component to selection
* @param {Component|HTMLElement} el Component to select * @param {Component|HTMLElement} el Component to select
* @param {Object} [opts={}] Options, optional * @param {Object} [opts={}] Options, optional
* @private * @public
*/ */
addSelected(el, opts = {}) { addSelected(el, opts = {}) {
const model = getModel(el, $); const model = getModel(el, $);
@ -421,7 +421,7 @@ export default class EditorModel extends Model {
* Remove component from selection * Remove component from selection
* @param {Component|HTMLElement} el Component to select * @param {Component|HTMLElement} el Component to select
* @param {Object} [opts={}] Options, optional * @param {Object} [opts={}] Options, optional
* @private * @public
*/ */
removeSelected(el, opts = {}) { removeSelected(el, opts = {}) {
this.get('selected').removeComponent(getModel(el, $), opts); this.get('selected').removeComponent(getModel(el, $), opts);
@ -431,7 +431,7 @@ export default class EditorModel extends Model {
* Toggle component selection * Toggle component selection
* @param {Component|HTMLElement} el Component to select * @param {Component|HTMLElement} el Component to select
* @param {Object} [opts={}] Options, optional * @param {Object} [opts={}] Options, optional
* @private * @public
*/ */
toggleSelected(el, opts = {}) { toggleSelected(el, opts = {}) {
const model = getModel(el, $); const model = getModel(el, $);
@ -489,7 +489,7 @@ export default class EditorModel extends Model {
* @param {Object|string} components HTML string or components model * @param {Object|string} components HTML string or components model
* @param {Object} opt the options object to be used by the [setComponents]{@link setComponents} method * @param {Object} opt the options object to be used by the [setComponents]{@link setComponents} method
* @return {this} * @return {this}
* @private * @public
*/ */
setComponents(components, opt = {}) { setComponents(components, opt = {}) {
return this.get('DomComponents').setComponents(components, opt); return this.get('DomComponents').setComponents(components, opt);
@ -515,7 +515,7 @@ export default class EditorModel extends Model {
* @param {Object|string} style CSS string or style model * @param {Object|string} style CSS string or style model
* @param {Object} opt the options object to be used by the `CssRules.add` method * @param {Object} opt the options object to be used by the `CssRules.add` method
* @return {this} * @return {this}
* @private * @public
*/ */
setStyle(style, opt = {}) { setStyle(style, opt = {}) {
const cssc = this.get('CssComposer'); const cssc = this.get('CssComposer');
@ -528,7 +528,7 @@ export default class EditorModel extends Model {
* Add styles to the editor * Add styles to the editor
* @param {Array<Object>|Object|string} style CSS string or style model * @param {Array<Object>|Object|string} style CSS string or style model
* @returns {Array<CssRule>} * @returns {Array<CssRule>}
* @private * @public
*/ */
addStyle(style, opts = {}) { addStyle(style, opts = {}) {
const res = this.getStyle().add(style, opts); const res = this.getStyle().add(style, opts);
@ -566,7 +566,7 @@ export default class EditorModel extends Model {
* Returns HTML built inside canvas * Returns HTML built inside canvas
* @param {Object} [opts={}] Options * @param {Object} [opts={}] Options
* @returns {string} HTML string * @returns {string} HTML string
* @private * @public
*/ */
getHtml(opts = {}) { getHtml(opts = {}) {
const { config } = this; const { config } = this;
@ -587,7 +587,7 @@ export default class EditorModel extends Model {
* Returns CSS built inside canvas * Returns CSS built inside canvas
* @param {Object} [opts={}] Options * @param {Object} [opts={}] Options
* @returns {string} CSS string * @returns {string} CSS string
* @private * @public
*/ */
getCss(opts = {}) { getCss(opts = {}) {
const config = this.config; const config = this.config;
@ -611,7 +611,7 @@ export default class EditorModel extends Model {
/** /**
* Returns JS of all components * Returns JS of all components
* @return {string} JS string * @return {string} JS string
* @private * @public
*/ */
getJs(opts = {}) { getJs(opts = {}) {
var wrp = opts.component || this.get('DomComponents').getWrapper(); var wrp = opts.component || this.get('DomComponents').getWrapper();
@ -620,7 +620,7 @@ export default class EditorModel extends Model {
/** /**
* Store data to the current storage. * Store data to the current storage.
* @private * @public
*/ */
async store(options) { async store(options) {
const data = this.storeData(); const data = this.storeData();
@ -631,7 +631,7 @@ export default class EditorModel extends Model {
/** /**
* Load data from the current storage. * Load data from the current storage.
* @private * @public
*/ */
async load(options) { async load(options) {
const result = await this.get('StorageManager').load(options); const result = await this.get('StorageManager').load(options);
@ -696,7 +696,7 @@ export default class EditorModel extends Model {
/** /**
* Update canvas dimensions and refresh data useful for tools positioning * Update canvas dimensions and refresh data useful for tools positioning
* @private * @public
*/ */
refreshCanvas(opts = {}) { refreshCanvas(opts = {}) {
this.set('canvasOffset', null); this.set('canvasOffset', null);

Loading…
Cancel
Save