import GEditor from '../geditor'; import { SCALE_RANGE, DRAGGING_NODE_CONFIG_KEY, EVENTS } from '../constants'; import './zoom-plugin.less'; import { EPERM } from 'constants'; import { zoom } from 'd3'; const template = `\
\ −\ \ +\
\ `; export interface InitialSettings { dom: Element; geditor: GEditor; } export default class ZoomPlugin { public el: Element; private geditor: GEditor; constructor (settings: InitialSettings) { const { dom, geditor } = settings; dom.innerHTML = template; this.el = dom.querySelector('.x-plugin-zoom'); this.geditor = geditor; this.initEvents(); } private initEvents () { this.el.addEventListener('click', (event) => { const className = (event.target as any).className; if (className === 'zoom-out') { let percent = this.getInputValue(); percent -= 1; if (percent < SCALE_RANGE.MIN * 100) { percent = SCALE_RANGE.MIN * 100; } const scale = percent / 100; this.setInputValue(percent); this.geditor.canvasView.resetZoomIdentity({ scale }); this.geditor.graphView.updateTransform({ scale }); } else if (className === 'zoom-in') { let percent = this.getInputValue(); percent += 1; if (percent > SCALE_RANGE.MAX * 100) { percent = SCALE_RANGE.MAX * 100; } const scale = percent / 100; this.setInputValue(percent); this.geditor.canvasView.resetZoomIdentity({ scale }); this.geditor.graphView.updateTransform({ scale }); } }); this.el.querySelector('input').addEventListener('input', (event) => { const target = event.target; const percent = this.getInputValue(); const scale = percent / 100; this.setInputValue(percent); this.geditor.canvasView.resetZoomIdentity({ scale }); this.geditor.graphView.updateTransform({ scale }); }); this.geditor.addListener(EVENTS.CANVAS_ZOOMED, (transform) => { this.setInputValue(Math.round(transform.scale * 100)); }); } private getInputValue () { const input = this.el.querySelector('input'); const value = input.value; let percent = parseInt(value, 10); if (isNaN(percent)) { percent = 100; } else if (percent > SCALE_RANGE.MAX * 100) { percent = SCALE_RANGE.MAX * 100; } else if (percent < SCALE_RANGE.MIN * 100) { percent = SCALE_RANGE.MIN * 100; } return percent; } private setInputValue (percent: number) { const input = this.el.querySelector('input'); input.value = percent + '%'; } }