|
| 1 | +import { |
| 2 | + DomObject, |
| 3 | + DomObjectRenderingEngine, |
| 4 | +} from '../../plugin-renderer-dom-object/src/DomObjectRenderingEngine'; |
| 5 | +import { ResizerNode } from './ResizerNode'; |
| 6 | +import { AbstractRenderer } from '../../plugin-renderer/src/AbstractRenderer'; |
| 7 | +import { Layout } from '../../plugin-layout/src/Layout'; |
| 8 | +import { DomLayoutEngine } from '../../plugin-dom-layout/src/DomLayoutEngine'; |
| 9 | +import { ZoneNode } from '../../plugin-layout/src/ZoneNode'; |
| 10 | + |
| 11 | +export class ResizerDomObjectRenderer extends AbstractRenderer<DomObject> { |
| 12 | + static id = DomObjectRenderingEngine.id; |
| 13 | + engine: DomObjectRenderingEngine; |
| 14 | + predicate = ResizerNode; |
| 15 | + |
| 16 | + domEngine: DomLayoutEngine; |
| 17 | + targetToResize: HTMLElement; |
| 18 | + |
| 19 | + async render(node: ResizerNode): Promise<DomObject> { |
| 20 | + const objectResizer: DomObject = { |
| 21 | + tag: 'JW-RESIZER', |
| 22 | + }; |
| 23 | + // This should become obsolete when we refactor the resiser (see _initTargetToResize() comment). |
| 24 | + this.domEngine = this.engine.editor.plugins.get(Layout).engines.dom as DomLayoutEngine; |
| 25 | + |
| 26 | + objectResizer.attach = (el: HTMLElement): void => { |
| 27 | + el.addEventListener('mousedown', this.startResize.bind(this)); |
| 28 | + el.addEventListener('touchstart', this.startResize.bind(this)); |
| 29 | + }; |
| 30 | + return objectResizer; |
| 31 | + } |
| 32 | + |
| 33 | + //-------------------------------------------------------------------------- |
| 34 | + // Public |
| 35 | + //-------------------------------------------------------------------------- |
| 36 | + /** |
| 37 | + * Drag the Resizer to change the editor size. |
| 38 | + * |
| 39 | + * @param {MouseEvent} event |
| 40 | + */ |
| 41 | + startResize(event: MouseEvent | TouchEvent): void { |
| 42 | + event.preventDefault(); |
| 43 | + this._initTargetToResize(); |
| 44 | + if (!this.targetToResize) return; |
| 45 | + |
| 46 | + const startHeight = this.targetToResize.clientHeight; |
| 47 | + const startY: number = |
| 48 | + event instanceof MouseEvent ? event.pageY : event.targetTouches[0].pageY; // Y position of the mousedown |
| 49 | + |
| 50 | + /** |
| 51 | + * Perform the resizing on every mouse mouvement. |
| 52 | + * |
| 53 | + * @param e |
| 54 | + */ |
| 55 | + const doResize = (e: MouseEvent | TouchEvent): void => { |
| 56 | + const currentY: number = e instanceof MouseEvent ? e.pageY : e.targetTouches[0].pageY; |
| 57 | + const offset: number = currentY - startY; |
| 58 | + this._resizeTargetHeight(startHeight + offset); |
| 59 | + }; |
| 60 | + /** |
| 61 | + * Stop resizing on mouse up. |
| 62 | + */ |
| 63 | + const stopResize = (): void => { |
| 64 | + window.removeEventListener('mousemove', doResize, false); |
| 65 | + window.removeEventListener('mouseup', stopResize, false); |
| 66 | + window.removeEventListener('touchmove', doResize, false); |
| 67 | + window.removeEventListener('touchend', stopResize, false); |
| 68 | + }; |
| 69 | + |
| 70 | + window.addEventListener('mousemove', doResize); |
| 71 | + window.addEventListener('mouseup', stopResize); |
| 72 | + window.addEventListener('touchmove', doResize); |
| 73 | + window.addEventListener('touchend', stopResize); |
| 74 | + } |
| 75 | + |
| 76 | + //-------------------------------------------------------------------------- |
| 77 | + // Private |
| 78 | + //-------------------------------------------------------------------------- |
| 79 | + /** |
| 80 | + * Discover the HTMLElement to resize and set it as a class property. |
| 81 | + */ |
| 82 | + _initTargetToResize(): void { |
| 83 | + // This way of HTMLElement discovery is far from ideal. |
| 84 | + // The Resizer should never be aware of the HTMLElement. |
| 85 | + // |
| 86 | + // TODO: We should change this to use a shared variable whose value would be listen to by another plugin. |
| 87 | + // The other plugin can then use the shared height value to change the height of his children element. |
| 88 | + // |
| 89 | + // Result: the resizer plugin will become agnostic of the HTMLElement afected by the resize. |
| 90 | + // Problem: We don't yet have a way to do this properly. |
| 91 | + if (this.targetToResize) return; |
| 92 | + |
| 93 | + const mainZone = this.domEngine.root.descendants( |
| 94 | + node => node instanceof ZoneNode && node.managedZones.includes('main'), |
| 95 | + )[0]; |
| 96 | + this.targetToResize = (this.domEngine.getDomNodes( |
| 97 | + mainZone, |
| 98 | + )[0] as HTMLElement)?.parentElement; |
| 99 | + |
| 100 | + // Force the overflow on the targetElement. |
| 101 | + // Necesary to make the resizer works out of the box. |
| 102 | + this.targetToResize.style.overflow = 'auto'; |
| 103 | + } |
| 104 | + /** |
| 105 | + * Change the height of the target HTMLElement. |
| 106 | + * |
| 107 | + * @param {number} height |
| 108 | + */ |
| 109 | + _resizeTargetHeight(height: number): void { |
| 110 | + height = Math.max(height, 50); // todo : implement a way to force the min-height with resizer parameters ? |
| 111 | + if (this.targetToResize) this.targetToResize.style.height = height + 'px'; |
| 112 | + } |
| 113 | +} |
0 commit comments