Browse Source

Update selector manager TS

pull/4849/head
Artur Arseniev 4 years ago
parent
commit
a06e4443aa
  1. 8
      src/abstract/Module.ts
  2. 4
      src/asset_manager/index.ts
  3. 15
      src/block_manager/index.ts
  4. 2
      src/common/index.ts
  5. 2
      src/editor/model/Editor.ts
  6. 9
      src/i18n/index.ts
  7. 15
      src/keymaps/index.ts
  8. 11
      src/parser/index.ts
  9. 9
      src/rich_text_editor/index.ts
  10. 32
      src/selector_manager/index.ts
  11. 2
      src/storage_manager/index.ts
  12. 4
      src/trait_manager/index.ts
  13. 3
      src/undo_manager/index.ts

8
src/abstract/Module.ts

@ -34,7 +34,7 @@ export interface IStorableModule extends IModule {
export default abstract class Module<T extends ModuleConfig = ModuleConfig> implements IModule<T> { export default abstract class Module<T extends ModuleConfig = ModuleConfig> implements IModule<T> {
private _em: EditorModel; private _em: EditorModel;
private _config: T; private _config: T & { pStylePrefix?: string };
private _name: string; private _name: string;
cls: any[] = []; cls: any[] = [];
events: any; events: any;
@ -75,7 +75,9 @@ export default abstract class Module<T extends ModuleConfig = ModuleConfig> impl
return this._name; return this._name;
} }
getConfig(name?: string) { getConfig<P extends keyof T | undefined = undefined, R = P extends keyof T ? T[P] : T>(
name?: P
): R & { pStylePrefix?: string } {
// @ts-ignore // @ts-ignore
return name ? this.config[name] : this.config; return name ? this.config[name] : this.config;
} }
@ -96,7 +98,7 @@ export default abstract class Module<T extends ModuleConfig = ModuleConfig> impl
if (elTo) { if (elTo) {
const el = isElement(elTo) ? elTo : document.querySelector(elTo); const el = isElement(elTo) ? elTo : document.querySelector(elTo);
if (!el) return this.__logWarn('"appendTo" element not found'); if (!el) return this.__logWarn('"appendTo" element not found');
el.appendChild(this.render()); el.appendChild(this.render() as any);
} }
} }
} }

4
src/asset_manager/index.ts

@ -135,7 +135,9 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
__trgCustom() { __trgCustom() {
const bhv = this.__getBehaviour(); const bhv = this.__getBehaviour();
if (!bhv.container && !this.getConfig().custom.open) { const custom = this.getConfig().custom;
if (!bhv.container && !(custom as any).open) {
return; return;
} }
this.em.trigger(this.events.custom, this.__customData()); this.em.trigger(this.events.custom, this.__customData());

15
src/block_manager/index.ts

@ -112,6 +112,13 @@ export default class BlockManager extends ItemManagerModule<BlockManagerConfig,
return this; return this;
} }
/**
* Get configuration object
* @name getConfig
* @function
* @return {Object}
*/
__trgCustom() { __trgCustom() {
this.em.trigger(this.events.custom, this.__customData()); this.em.trigger(this.events.custom, this.__customData());
} }
@ -204,14 +211,6 @@ export default class BlockManager extends ItemManagerModule<BlockManagerConfig,
this.__endDrag(); this.__endDrag();
} }
/**
* Get configuration object
* @return {Object}
*/
getConfig() {
return this.config;
}
postRender() { postRender() {
const { categories, config, em } = this; const { categories, config, em } = this;
const collection = this.blocksVisible; const collection = this.blocksVisible;

2
src/common/index.ts

@ -5,3 +5,5 @@ export { default as View } from './View';
export type SetOptions = Backbone.ModelSetOptions & { avoidStore?: boolean }; export type SetOptions = Backbone.ModelSetOptions & { avoidStore?: boolean };
export type AddOptions = Backbone.AddOptions; export type AddOptions = Backbone.AddOptions;
export type RemoveOptions = Backbone.Silenceable;

2
src/editor/model/Editor.ts

@ -760,7 +760,7 @@ export default class EditorModel extends Model {
* Get the current selector state * Get the current selector state
* @returns {String} * @returns {String}
*/ */
getState() { getState(): string {
return this.get('state') || ''; return this.get('state') || '';
} }

9
src/i18n/index.ts

@ -51,12 +51,11 @@ export default class I18nModule extends Module<I18nConfig & { stylePrefix?: stri
} }
/** /**
* Get module configurations * Get configuration object
* @returns {Object} Configuration object * @name getConfig
* @function
* @return {Object}
*/ */
getConfig() {
return this.config;
}
/** /**
* Update current locale * Update current locale

15
src/keymaps/index.ts

@ -61,14 +61,6 @@ export default class KeymapsModule extends Module<KeymapsConfig & { name?: strin
this.keymaps = {}; this.keymaps = {};
} }
/**
* Get module configurations
* @return {Object} Configuration object
*/
getConfig() {
return this.config;
}
onLoad() { onLoad() {
const defKeys = this.config.defaults; const defKeys = this.config.defaults;
@ -78,6 +70,13 @@ export default class KeymapsModule extends Module<KeymapsConfig & { name?: strin
} }
} }
/**
* Get configuration object
* @name getConfig
* @function
* @return {Object}
*/
/** /**
* Add new keymap * Add new keymap
* @param {string} id Keymap id * @param {string} id Keymap id

11
src/parser/index.ts

@ -42,14 +42,11 @@ export default class ParserModule extends Module<ParserConfig & { name?: string
} }
/** /**
* Get the configuration object * Get configuration object
* @returns {Object} Configuration object * @name getConfig
* @example * @function
* console.log(Parser.getConfig()) * @return {Object}
*/ */
getConfig() {
return this.config;
}
/** /**
* Parse HTML string and return the object containing the Component Definition * Parse HTML string and return the object containing the Component Definition

9
src/rich_text_editor/index.ts

@ -61,9 +61,12 @@ export default class RichTextEditorModule extends Module<RichTextEditorConfig &
actions?: (RichTextEditorAction | string)[]; actions?: (RichTextEditorAction | string)[];
customRte?: CustomRTE; customRte?: CustomRTE;
getConfig() { /**
return this.config; * Get configuration object
} * @name getConfig
* @function
* @return {Object}
*/
constructor(em: EditorModel) { constructor(em: EditorModel) {
super(em, 'RichTextEditor', defaults); super(em, 'RichTextEditor', defaults);

32
src/selector_manager/index.ts

@ -74,7 +74,7 @@
import { isString, debounce, isObject, isArray, bindAll } from 'underscore'; import { isString, debounce, isObject, isArray, bindAll } from 'underscore';
import { isComponent, isRule } from '../utils/mixins'; import { isComponent, isRule } from '../utils/mixins';
import { Model, Collection } from '../common'; import { Model, Collection, RemoveOptions } from '../common';
import defaults, { SelectorManagerConfig } from './config/config'; import defaults, { SelectorManagerConfig } from './config/config';
import Selector from './model/Selector'; import Selector from './model/Selector';
import Selectors from './model/Selectors'; import Selectors from './model/Selectors';
@ -107,6 +107,8 @@ const events = {
custom: evCustom, custom: evCustom,
}; };
type SelectorStringObject = string | { name?: string; label?: string };
export default class SelectorManager extends ItemManagerModule<SelectorManagerConfig & { pStylePrefix?: string }> { export default class SelectorManager extends ItemManagerModule<SelectorManagerConfig & { pStylePrefix?: string }> {
Selector = Selector; Selector = Selector;
@ -198,11 +200,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
return this; return this;
} }
addSelector( addSelector(name: SelectorStringObject | Selector, opts = {}, cOpts = {}): Selector {
name: string | { name?: string; label?: string } | Selector,
opts = {},
cOpts = {}
): Selector | Selector[] {
let props: any = { ...opts }; let props: any = { ...opts };
if (isObject(name)) { if (isObject(name)) {
@ -225,7 +223,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
const cname = props.name; const cname = props.name;
const config = this.getConfig(); const config = this.getConfig();
const { all, em } = this; const { all, em } = this;
const selector = cname ? this.get(cname, props.type) : all.where(props)[0]; const selector = cname ? (this.get(cname, props.type) as Selector) : all.where(props)[0];
if (!selector) { if (!selector) {
const selModel = props instanceof Selector ? props : new Selector(props, { ...cOpts, config, em }); const selModel = props instanceof Selector ? props : new Selector(props, { ...cOpts, config, em });
@ -235,7 +233,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
return selector; return selector;
} }
getSelector(name: string, type = Selector.TYPE_CLASS) { getSelector(name: string, type = Selector.TYPE_CLASS): Selector | undefined {
if (isId(name)) { if (isId(name)) {
name = name.substr(1); name = name.substr(1);
type = Selector.TYPE_ID; type = Selector.TYPE_ID;
@ -259,7 +257,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* const selector = selectorManager.add('.my-class'); * const selector = selectorManager.add('.my-class');
* console.log(selector.toString()) // `.my-class` * console.log(selector.toString()) // `.my-class`
* */ * */
add(props: string | { name?: string; label?: string }, opts = {}) { add(props: SelectorStringObject, opts = {}) {
const cOpts = isString(props) ? {} : opts; const cOpts = isString(props) ? {} : opts;
// Keep support for arrays but avoid it in docs // Keep support for arrays but avoid it in docs
if (isArray(props)) { if (isArray(props)) {
@ -281,13 +279,13 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* // -> [SelectorObject, ...] * // -> [SelectorObject, ...]
*/ */
addClass(classes: string | string[]) { addClass(classes: string | string[]) {
const added: any = []; const added: Selector[] = [];
if (isString(classes)) { if (isString(classes)) {
classes = classes.trim().split(' '); classes = classes.trim().split(' ');
} }
classes.forEach(name => added.push(this.addSelector(name))); classes.forEach(name => added.push(this.addSelector(name) as Selector));
return added; return added;
} }
@ -304,7 +302,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
// Keep support for arrays but avoid it in docs // Keep support for arrays but avoid it in docs
if (isArray(name)) { if (isArray(name)) {
const result: Selector[] = []; const result: Selector[] = [];
const selectors = name.map(item => this.getSelector(item)).filter(item => item); const selectors = name.map(item => this.getSelector(item)).filter(Boolean) as Selector[];
selectors.forEach(item => result.indexOf(item) < 0 && result.push(item)); selectors.forEach(item => result.indexOf(item) < 0 && result.push(item));
return result; return result;
} else { } else {
@ -321,7 +319,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* // or by passing the Selector * // or by passing the Selector
* selectorManager.remove(selectorManager.get('.myclass')); * selectorManager.remove(selectorManager.get('.myclass'));
*/ */
remove(selector: string | Selector, opts?: any) { remove(selector: string | Selector, opts?: RemoveOptions) {
return this.__remove(selector, opts); return this.__remove(selector, opts);
} }
@ -398,7 +396,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* @example * @example
* selectorManager.addSelected('.new-class'); * selectorManager.addSelected('.new-class');
*/ */
addSelected(props: string | { name?: string; label?: string }) { addSelected(props: SelectorStringObject) {
const added = this.add(props); const added = this.add(props);
// TODO: target should be the one from StyleManager // TODO: target should be the one from StyleManager
this.em.getSelectedAll().forEach(target => { this.em.getSelectedAll().forEach(target => {
@ -413,7 +411,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* @example * @example
* selectorManager.removeSelected('.myclass'); * selectorManager.removeSelected('.myclass');
*/ */
removeSelected(selector: any) { removeSelected(selector: Selector) {
this.em.getSelectedAll().forEach(trg => { this.em.getSelectedAll().forEach(trg => {
!selector.get('protected') && trg && trg.getSelectors().remove(selector); !selector.get('protected') && trg && trg.getSelectors().remove(selector);
}); });
@ -427,7 +425,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* console.log(targetsToStyle.map(target => target.getSelectorsString())) * console.log(targetsToStyle.map(target => target.getSelectorsString()))
*/ */
getSelectedTargets() { getSelectedTargets() {
return this.em.get('StyleManager').getSelectedAll(); return this.em.Styles.getSelectedAll();
} }
/** /**
@ -446,7 +444,7 @@ export default class SelectorManager extends ItemManagerModule<SelectorManagerCo
* @return {Boolean} * @return {Boolean}
*/ */
getComponentFirst() { getComponentFirst() {
return this.getConfig().componentFirst; return this.getConfig().componentFirst!;
} }
/** /**

2
src/storage_manager/index.ts

@ -325,7 +325,7 @@ export default class StorageManager extends Module<
getCurrentOptions(type?: string): StorageOptions { getCurrentOptions(type?: string): StorageOptions {
const config = this.getConfig(); const config = this.getConfig();
const current = type || this.getCurrent(); const current = type || this.getCurrent();
return config.options[current] || {}; return config.options![current] || {};
} }
/** /**

4
src/trait_manager/index.ts

@ -147,14 +147,14 @@ export default class TraitManager extends Module<TraitManagerConfig & { pStylePr
} }
render() { render() {
let { view } = this; let { view, em } = this;
const config = this.getConfig(); const config = this.getConfig();
const el = view && view.el; const el = view && view.el;
view = new TraitsView( view = new TraitsView(
{ {
el, el,
collection: [], collection: [],
editor: config.em, editor: em,
config, config,
}, },
this.getTypes() this.getTypes()

3
src/undo_manager/index.ts

@ -151,9 +151,6 @@ export default class UndoManagerModule extends Module<UndoManagerConfig & { name
* const config = um.getConfig(); * const config = um.getConfig();
* // { ... } * // { ... }
*/ */
getConfig() {
return this.config;
}
/** /**
* Add an entity (Model/Collection) to track * Add an entity (Model/Collection) to track

Loading…
Cancel
Save