import { isUndefined } from 'underscore'; import { CanvasSpotBuiltInTypes } from '../../canvas/model/CanvasSpot'; import { $ } from '../../common'; import { isTextNode } from '../../utils/dom'; import { CommandObject } from './CommandAbstract'; export default { getOffsetMethod(state: string) { var method = state || ''; return 'get' + method + 'OffsetViewerEl'; }, run(editor, sender, opts) { const { canvas } = this; const opt = opts || {}; const state = opt.state || ''; const config = editor.getConfig(); const zoom = this.em.getZoomDecimal(); const el = opt.el as HTMLElement | undefined; if (!config.showOffsets || !el || isTextNode(el) || (!config.showOffsetsSelected && state == 'Fixed')) { editor.stopCommand(`${this.id}`, opts); return; } if (canvas.hasCustomSpot(CanvasSpotBuiltInTypes.Spacing)) { return; } var pos = { ...(opt.elPos || canvas.getElementPos(el)) }; if (!isUndefined(opt.top)) { pos.top = opt.top; } if (!isUndefined(opt.left)) { pos.left = opt.left; } var style = window.getComputedStyle(el); var ppfx = this.ppfx; var stateVar = state + 'State'; var method = this.getOffsetMethod(state); // @ts-ignore var offsetViewer = canvas[method](opts.view); offsetViewer.style.opacity = ''; let marginT = this['marginT' + state]; let marginB = this['marginB' + state]; let marginL = this['marginL' + state]; let marginR = this['marginR' + state]; let padT = this['padT' + state]; let padB = this['padB' + state]; let padL = this['padL' + state]; let padR = this['padR' + state]; if (offsetViewer.childNodes.length) { this[stateVar] = '1'; marginT = offsetViewer.querySelector('[data-offset-m-t]'); marginB = offsetViewer.querySelector('[data-offset-m-b]'); marginL = offsetViewer.querySelector('[data-offset-m-l]'); marginR = offsetViewer.querySelector('[data-offset-m-r]'); padT = offsetViewer.querySelector('[data-offset-p-t]'); padB = offsetViewer.querySelector('[data-offset-p-b]'); padL = offsetViewer.querySelector('[data-offset-p-l]'); padR = offsetViewer.querySelector('[data-offset-p-r]'); } if (!this[stateVar]) { var stateLow = state.toLowerCase(); var marginName = stateLow + 'margin-v'; var paddingName = stateLow + 'padding-v'; var marginV = $(`