import Moveable from "lit-moveable"; import { LitElement, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ref, createRef } from "lit/directives/ref.js"; @customElement("lit-app") export default class App extends LitElement { resizable: any = "$preview_resizable"; keepRatio: any = "$preview_keepRatio"; throttleResize: any = "$preview_throttleResize"; renderDirections: any = "$preview_renderDirections"; minWidth: any = "$preview_minWidth"; minHeight: any = "$preview_minHeight"; maxWidth: any = "$preview_maxWidth"; maxHeight: any = "$preview_maxHeight"; widthInputRef: Ref = createRef(null); heightInputRef: Ref = createRef(null); moveableRef: Ref = createRef(null); onInput: any = (e: any) => { const ev = (e.nativeEvent || e) as InputEvent; const horizontal = JSON.parse((ev.target as HTMLElement).getAttribute("data-horizontal")!); if (typeof ev.data === "undefined") { this.moveableRef.value!.request("resizable", { offsetWidth: parseFloat(this.widthInputRef.value!.value), offsetHeight: parseFloat(this.heightInputRef.value!.value), horizontal }, true); } }; onKeyUp: any = (e: any) => { const ev = (e.nativeEvent || e) as InputEvent; const horizontal = JSON.parse((ev.target as HTMLElement).getAttribute("data-horizontal")!); e.stopPropagation(); if (e.keyCode === 13) { this.moveableRef.value!.request("resizable", { offsetWidth: parseFloat(this.widthInputRef.value!.value), offsetHeight: parseFloat(this.heightInputRef.value!.value), horizontal }, true); } }; render() { return html `
width:   height:
Target1
Target2
Target3
`; } onResizeGroupStart({ setMin, setMax }) { setMin([this.minWidth, this.minHeight]); setMax([this.maxWidth, this.maxHeight]); } onResizeGroup({ events }) { events.forEach(ev => { ev.target.style.width = `${ev.boundingWidth}px`; ev.target.style.height = `${ev.boundingHeight}px`; ev.target.style.transform = `translate(${ev.drag.beforeTranslate[0]}px, ${ev.drag.beforeTranslate[1]}px)`; }); } onResizeGroupEnd(e) { requestAnimationFrame(() => { const rect = e.moveable.getRect(); this.widthInputRef.value!.value = `${rect.offsetWidth}`; this.heightInputRef.value!.value = `${rect.offsetHeight}`; }); } onDragGroup({ events }) { events.forEach(ev => { ev.target.style.transform = ev.transform; }); } }