|
|
@ -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'; |
|
|
|
|
|
} |
|
|
|
|
|
} |