Browse Source

Move ComponentImage to TS

ts-components
Artur Arseniev 3 years ago
parent
commit
9ba17a6201
  1. 3
      src/dom_components/model/Component.ts
  2. 49
      src/dom_components/model/ComponentImage.ts
  3. 21
      src/dom_components/model/ToolbarButton.ts
  4. 13
      src/dom_components/model/types.ts

3
src/dom_components/model/Component.ts

@ -35,6 +35,7 @@ import ComponentView from '../view/ComponentView';
import { AddOptions, ObjectAny, ObjectStrings, SetOptions } from '../../common'; import { AddOptions, ObjectAny, ObjectStrings, SetOptions } from '../../common';
import CssRule, { CssRuleJSON, CssRuleProperties } from '../../css_composer/model/CssRule'; import CssRule, { CssRuleJSON, CssRuleProperties } from '../../css_composer/model/CssRule';
import { TraitProperties } from '../../trait_manager/model/Trait'; import { TraitProperties } from '../../trait_manager/model/Trait';
import { ToolbarButtonProps } from './ToolbarButton';
const escapeRegExp = (str: string) => { const escapeRegExp = (str: string) => {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
@ -1170,7 +1171,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
const ppfx = (em && em.getConfig().stylePrefix) || ''; const ppfx = (em && em.getConfig().stylePrefix) || '';
if (!model.get('toolbar') && em) { if (!model.get('toolbar') && em) {
const tb = []; const tb: ToolbarButtonProps[] = [];
model.collection && model.collection &&
tb.push({ tb.push({
label: em.getIcon('arrowUp'), label: em.getIcon('arrowUp'),

49
src/dom_components/model/ComponentImage.js → src/dom_components/model/ComponentImage.ts

@ -1,6 +1,7 @@
import { result } from 'underscore'; import { result } from 'underscore';
import Component from './Component'; import Component from './Component';
import { toLowerCase, buildBase64UrlFromSvg, hasWin } from '../../utils/mixins'; import { toLowerCase, buildBase64UrlFromSvg, hasWin } from '../../utils/mixins';
import { ObjectStrings } from '../../common';
const svgAttrs = const svgAttrs =
'xmlns="http://www.w3.org/2000/svg" width="100" viewBox="0 0 24 24" style="fill: rgba(0,0,0,0.15); transform: scale(0.75)"'; 'xmlns="http://www.w3.org/2000/svg" width="100" viewBox="0 0 24 24" style="fill: rgba(0,0,0,0.15); transform: scale(0.75)"';
@ -8,6 +9,7 @@ const svgAttrs =
export default class ComponentImage extends Component { export default class ComponentImage extends Component {
get defaults() { get defaults() {
return { return {
// @ts-ignore
...super.defaults, ...super.defaults,
type: 'image', type: 'image',
tagName: 'img', tagName: 'img',
@ -33,26 +35,26 @@ export default class ComponentImage extends Component {
}; };
} }
initialize(o, opt) { initialize(props: any, opts: any) {
Component.prototype.initialize.apply(this, arguments); super.initialize(props, opts);
const { src } = this.get('attributes'); const { src } = this.get('attributes')!;
if (src && buildBase64UrlFromSvg(result(this, 'defaults').src) !== src) { if (src && buildBase64UrlFromSvg(result(this, 'defaults').src) !== src) {
this.set('src', src, { silent: 1 }); this.set('src', src, { silent: true });
} }
} }
initToolbar(...args) { initToolbar() {
Component.prototype.initToolbar.apply(this, args); super.initToolbar();
const em = this.em; const { em } = this;
if (em) { if (em) {
var cmd = em.get('Commands'); const cmd = em.Commands;
var cmdName = 'image-editor'; const cmdName = 'image-editor';
// Add Image Editor button only if the default command exists // Add Image Editor button only if the default command exists
if (cmd.has(cmdName)) { if (cmd.has(cmdName)) {
let hasButtonBool = false; let hasButtonBool = false;
var tb = this.get('toolbar'); const tb = this.get('toolbar')!;
for (let i = 0; i < tb.length; i++) { for (let i = 0; i < tb.length; i++) {
if (tb[i].command === 'image-editor') { if (tb[i].command === 'image-editor') {
@ -77,14 +79,14 @@ export default class ComponentImage extends Component {
* @return {Object} * @return {Object}
* @private * @private
*/ */
getAttrToHTML(...args) { getAttrToHTML() {
const attr = Component.prototype.getAttrToHTML.apply(this, args); const attr = super.getAttrToHTML();
const src = this.getSrcResult(); const src = this.getSrcResult();
if (src) attr.src = src; if (src) attr.src = src;
return attr; return attr;
} }
getSrcResult(opt = {}) { getSrcResult(opt: { fallback?: boolean } = {}) {
const src = this.get(opt.fallback ? 'fallback' : 'src') || ''; const src = this.get(opt.fallback ? 'fallback' : 'src') || '';
let result = src; let result = src;
@ -107,9 +109,11 @@ export default class ComponentImage extends Component {
* @return {Object} * @return {Object}
* @private * @private
*/ */
toJSON(...args) { toJSON(opts: Parameters<Component['toJSON']>[0]) {
const obj = Component.prototype.toJSON.apply(this, args); const obj = super.toJSON(opts);
if (obj.attributes && obj.src === obj.attributes.src) { const { attributes } = obj;
if (attributes && obj.src === attributes.src) {
delete obj.src; delete obj.src;
} }
@ -122,10 +126,11 @@ export default class ComponentImage extends Component {
* @return {object} * @return {object}
* @private * @private
*/ */
parseUri(uri) { parseUri(uri: string) {
let result = {}; let result: HTMLAnchorElement | URL | ObjectStrings = {};
const getQueryObject = (search = '') => { const getQueryObject = (search = '') => {
const query = {}; const query: ObjectStrings = {};
const qrs = search.substring(1).split('&'); const qrs = search.substring(1).split('&');
for (let i = 0; i < qrs.length; i++) { for (let i = 0; i < qrs.length; i++) {
@ -156,6 +161,8 @@ export default class ComponentImage extends Component {
query: getQueryObject(result.search), query: getQueryObject(result.search),
}; };
} }
}
ComponentImage.isComponent = el => toLowerCase(el.tagName) === 'img'; static isComponent(el: HTMLElement) {
return toLowerCase(el.tagName) === 'img';
}
}

21
src/dom_components/model/ToolbarButton.ts

@ -1,6 +1,23 @@
import { Model } from '../../common'; import { CommandFunction } from '../../commands/view/CommandAbstract';
import { Model, ObjectAny } from '../../common';
export default class ToolbarButton extends Model { export interface ToolbarButtonProps {
/**
* Command name.
*/
command: CommandFunction | string;
/**
* Button label.
*/
label?: string;
id?: string;
attributes?: ObjectAny;
events?: ObjectAny;
}
export default class ToolbarButton extends Model<ToolbarButtonProps> {
defaults() { defaults() {
return { return {
command: '', command: '',

13
src/dom_components/model/types.ts

@ -7,6 +7,7 @@ import { ResizerOptions } from '../../utils/Resizer';
import { DomComponentsConfig } from '../config/config'; import { DomComponentsConfig } from '../config/config';
import Component from './Component'; import Component from './Component';
import Components from './Components'; import Components from './Components';
import { ToolbarButtonProps } from './ToolbarButton';
export type DragMode = 'translate' | 'absolute'; export type DragMode = 'translate' | 'absolute';
@ -152,12 +153,13 @@ export interface ComponentProperties {
* @defaultValue [] * @defaultValue []
*/ */
propagate?: (keyof ComponentProperties)[]; propagate?: (keyof ComponentProperties)[];
/** /**
* Set an array of items to show up inside the toolbar when the component is selected (move, clone, delete). * Set an array of items to show up inside the toolbar when the component is selected (move, clone, delete).
Eg. `toolbar: [ { attributes: {class: 'fa fa-arrows'}, command: 'tlb-move' }, ... ]`. * Eg. `toolbar: [ { attributes: {class: 'fa fa-arrows'}, command: 'tlb-move' }, ... ]`.
By default, when `toolbar` property is falsy the editor will add automatically commands `core:component-exit` (select parent component, added if there is one), `tlb-move` (added if `draggable`) , `tlb-clone` (added if `copyable`), `tlb-delete` (added if `removable`). * By default, when `toolbar` property is falsy the editor will add automatically commands `core:component-exit` (select parent component, added if there is one), `tlb-move` (added if `draggable`) , `tlb-clone` (added if `copyable`), `tlb-delete` (added if `removable`).
*/ */
toolbar?: object[]; toolbar?: ToolbarButtonProps[];
///** ///**
// * Children components. Default: `null` // * Children components. Default: `null`
// */ // */
@ -182,6 +184,7 @@ export interface ComponentDefinition extends Omit<ComponentProperties, 'componen
*/ */
components?: string | ComponentDefinition | (string | ComponentDefinition)[]; components?: string | ComponentDefinition | (string | ComponentDefinition)[];
traits?: (Partial<TraitProperties> | string)[]; traits?: (Partial<TraitProperties> | string)[];
attributes?: Record<string, any>;
[key: string]: unknown; [key: string]: unknown;
} }

Loading…
Cancel
Save