Browse Source

Update Selectors tags in selector manager.

pull/5524/head
Artur Arseniev 3 years ago
parent
commit
8025b95a64
  1. 2
      src/dom_components/model/Component.ts
  2. 2
      src/dom_components/view/ComponentView.ts
  3. 22
      src/selector_manager/model/Selector.ts
  4. 13
      src/selector_manager/model/Selectors.ts
  5. 31
      src/selector_manager/view/ClassTagView.ts

2
src/dom_components/model/Component.ts

@ -655,7 +655,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
if (opts.noClass) { if (opts.noClass) {
delete attributes.class; delete attributes.class;
} else { } else {
this.classes.forEach(cls => classes.push(isString(cls) ? cls : cls.get('name'))); this.classes.forEach(cls => classes.push(isString(cls) ? cls : cls.getName()));
classes.length && (attributes.class = classes.join(' ')); classes.length && (attributes.class = classes.join(' '));
} }

2
src/dom_components/view/ComponentView.ts

@ -211,7 +211,7 @@ Component> {
importClasses() { importClasses() {
const { em, model } = this; const { em, model } = this;
const sm = em.Selectors; const sm = em.Selectors;
sm && model.classes.forEach(s => sm.add(s.get('name'))); sm && model.classes.forEach(s => sm.add(s.getName()));
} }
/** /**

22
src/selector_manager/model/Selector.ts

@ -23,7 +23,7 @@ export interface SelectorProps {
* @property {Boolean} [private=false] If true, it can't be seen by the Style Manager, but it will be rendered in the canvas and in export code. * @property {Boolean} [private=false] If true, it can't be seen by the Style Manager, but it will be rendered in the canvas and in export code.
* @property {Boolean} [protected=false] If true, it can't be removed from the attached component. * @property {Boolean} [protected=false] If true, it can't be removed from the attached component.
*/ */
export default class Selector extends Model { export default class Selector extends Model<SelectorProps & { [key: string]: unknown }> {
defaults() { defaults() {
return { return {
name: '', name: '',
@ -57,7 +57,7 @@ export default class Selector extends Model {
this.set('label', name); this.set('label', name);
} }
const namePreEsc = this.get('name'); const namePreEsc = this.get('name')!;
const { escapeName } = config; const { escapeName } = config;
const nameEsc = escapeName ? escapeName(namePreEsc) : Selector.escapeName(namePreEsc); const nameEsc = escapeName ? escapeName(namePreEsc) : Selector.escapeName(namePreEsc);
this.set('name', nameEsc); this.set('name', nameEsc);
@ -101,6 +101,18 @@ export default class Selector extends Model {
return this.getFullName(); return this.getFullName();
} }
/**
* Get selector name.
* @returns {String}
* @example
* // Given such selector: { name: 'my-selector', label: 'My selector' }
* console.log(selector.getLabel());
* // -> `my-selector`
*/
getName() {
return this.get('name') || '';
}
/** /**
* Get selector label. * Get selector label.
* @returns {String} * @returns {String}
@ -110,7 +122,7 @@ export default class Selector extends Model {
* // -> `My selector` * // -> `My selector`
*/ */
getLabel() { getLabel() {
return this.get('label'); return this.get('label') || '';
} }
/** /**
@ -130,8 +142,8 @@ export default class Selector extends Model {
* Get selector active state. * Get selector active state.
* @returns {Boolean} * @returns {Boolean}
*/ */
getActive(): boolean { getActive() {
return this.get('active'); return !!this.get('active');
} }
/** /**

13
src/selector_manager/model/Selectors.ts

@ -17,22 +17,17 @@ export default class Selectors extends Collection<Selector> {
} }
getStyleable() { getStyleable() {
return filter( return filter(this.models, item => item.getActive() && !item.get('private'));
this.models,
(item) => item.get('active') && !item.get('private')
);
} }
getValid({ noDisabled }: any = {}) { getValid({ noDisabled }: any = {}) {
return filter(this.models, (item) => !item.get('private')).filter((item) => return filter(this.models, item => !item.get('private')).filter(item => (noDisabled ? item.get('active') : 1));
noDisabled ? item.get('active') : 1
);
} }
getFullString(collection?: Selector[] | null, opts: { sort?: boolean } = {}) { getFullString(collection?: Selector[] | null, opts: { sort?: boolean } = {}) {
const result: string[] = []; const result: string[] = [];
const coll = collection || this; const coll = collection || this;
coll.forEach((selector) => result.push(selector.getFullName(opts))); coll.forEach(selector => result.push(selector.getFullName(opts)));
opts.sort && result.sort(); opts.sort && result.sort();
return result.join('').trim(); return result.join('').trim();
} }
@ -40,7 +35,7 @@ export default class Selectors extends Collection<Selector> {
getFullName(opts: any = {}) { getFullName(opts: any = {}) {
const { combination, array } = opts; const { combination, array } = opts;
let result: string[] = []; let result: string[] = [];
const sels = this.map((s) => s.getFullName(opts)).sort(); const sels = this.map(s => s.getFullName(opts)).sort();
if (combination) { if (combination) {
sels.forEach((sel, n) => { sels.forEach((sel, n) => {

31
src/selector_manager/view/ClassTagView.ts

@ -2,6 +2,8 @@ import { View } from '../../common';
import Selector from '../model/Selector'; import Selector from '../model/Selector';
import html from '../../utils/html'; import html from '../../utils/html';
import EditorModel from '../../editor/model/Editor'; import EditorModel from '../../editor/model/Editor';
import SelectorManager from '..';
import { SelectorManagerConfig } from '../config/config';
const inputProp = 'contentEditable'; const inputProp = 'contentEditable';
@ -13,7 +15,7 @@ export default class ClassTagView extends View<Selector> {
return html` return html`
<span id="${pfx}checkbox" class="${pfx}tag-status" data-tag-status></span> <span id="${pfx}checkbox" class="${pfx}tag-status" data-tag-status></span>
<span id="${pfx}tag-label" data-tag-name>${label}</span> <span id="${pfx}tag-label" data-tag-name>${label}</span>
<span id="${pfx}close" class="${pfx}tag-close" data-tag-remove> $${config.iconTagRemove} </span> <span id="${pfx}close" class="${pfx}tag-close" data-tag-remove> $${config.iconTagRemove!} </span>
`; `;
} }
@ -25,11 +27,11 @@ export default class ClassTagView extends View<Selector> {
'focusout [data-tag-name]': 'endEditTag', 'focusout [data-tag-name]': 'endEditTag',
}; };
} }
config: any; config: SelectorManagerConfig;
module: any; module: SelectorManager;
coll: any; coll: any;
pfx: any; pfx: string;
ppfx: any; ppfx: string;
em: EditorModel; em: EditorModel;
inputEl?: HTMLElement; inputEl?: HTMLElement;
@ -64,7 +66,6 @@ export default class ClassTagView extends View<Selector> {
startEditTag() { startEditTag() {
const { em } = this; const { em } = this;
const inputEl = this.getInputEl(); const inputEl = this.getInputEl();
inputEl;
inputEl[inputProp] = 'true'; inputEl[inputProp] = 'true';
inputEl.focus(); inputEl.focus();
em?.setEditing(true); em?.setEditing(true);
@ -84,7 +85,7 @@ export default class ClassTagView extends View<Selector> {
em?.setEditing(false); em?.setEditing(false);
if (sm && sm.rename(model, label) !== model) { if (sm && sm.rename(model, label) !== model) {
inputEl.innerText = model.get('label'); inputEl.innerText = model.getLabel();
} }
} }
@ -94,7 +95,7 @@ export default class ClassTagView extends View<Selector> {
*/ */
changeStatus() { changeStatus() {
const { model } = this; const { model } = this;
model.set('active', !model.get('active')); model.set('active', !model.getActive());
} }
/** /**
@ -116,19 +117,21 @@ export default class ClassTagView extends View<Selector> {
const $chk = $el.find('[data-tag-status]'); const $chk = $el.find('[data-tag-status]');
if (model.get('active')) { if (model.get('active')) {
$chk.html(iconTagOn); $chk.html(iconTagOn!);
$el.removeClass('opac50'); $el.removeClass('opac50');
} else { } else {
$chk.html(iconTagOff); $chk.html(iconTagOff!);
$el.addClass('opac50'); $el.addClass('opac50');
} }
} }
render() { render() {
const pfx = this.pfx; const { pfx, ppfx, $el, model } = this;
const ppfx = this.ppfx; const mainCls = `${pfx}tag`;
this.$el.html(this.template()); const classes = [`${mainCls} ${ppfx}three-bg`];
this.$el.attr('class', `${pfx}tag ${ppfx}three-bg`); model.get('protected') && classes.push(`${mainCls}-protected`);
$el.html(this.template());
$el.attr('class', classes.join(' '));
this.updateStatus(); this.updateStatus();
return this; return this;
} }

Loading…
Cancel
Save