import {resizeDirections,resizeToolRect} from './resize-tool-rect.mjs'; const windowSelector = '[data-window], #graphicsPrintPanel, dialog'; const managed = new WeakSet(); let interaction; let topLayer = 45; function setup(pane) { if(managed.has(pane))return; managed.add(pane); pane.classList.add('adjustable-tool'); const title=pane.querySelector('[data-drag], :scope > header, :scope > h2, :scope > h3, :scope > form > h2, :scope > form > h3'); if(title)title.classList.add('tool-move-handle'); for(const direction of resizeDirections){ const grip=document.createElement('span'); grip.className='tool-size-handle'; grip.dataset.toolResize=direction; grip.setAttribute('aria-hidden','true'); pane.append(grip); } } function scan(root){ if(!(root instanceof Element))return; if(root.matches(windowSelector))setup(root); root.querySelectorAll(windowSelector).forEach(setup); } scan(document.body); new MutationObserver(mutations=>{ for(const mutation of mutations) for(const node of mutation.addedNodes)scan(node); }).observe(document.body,{childList:true,subtree:true}); const clamp=(value,low,high)=>Math.max(low,Math.min(high,value)); function bounds(pane){ const parent=pane.matches('[data-window]')?pane.offsetParent:null; if(parent){ const box=parent.getBoundingClientRect(); return {left:box.left+parent.clientLeft,top:box.top+parent.clientTop,width:parent.clientWidth,height:parent.clientHeight}; } return {left:0,top:0,width:document.documentElement.clientWidth,height:document.documentElement.clientHeight}; } document.addEventListener('pointerdown',event=>{ if(interaction||event.button!==0||!(event.target instanceof Element))return; const pane=event.target.closest(windowSelector); if(!pane||pane.matches('.show-graphical #weaveWindow[data-graph-panel]'))return; if(!pane.matches('[data-window],:modal'))pane.style.zIndex=String(++topLayer); const grip=event.target.closest('[data-tool-resize]'); const title=event.target.closest('.tool-move-handle'); if(!grip&&(!title||event.target.closest('button,input,select,textarea,a,summary,label')))return; if(grip&&pane.classList.contains('minimized'))return; const box=pane.getBoundingClientRect(), area=bounds(pane); const x=clamp(box.left-area.left,0,Math.max(0,area.width-box.width)); const y=clamp(box.top-area.top,0,Math.max(0,area.height-box.height)); Object.assign(pane.style,{left:x+'px',top:y+'px',right:'auto',bottom:'auto',margin:'0'}); if(pane.matches('dialog'))pane.style.position='fixed'; if(grip)Object.assign(pane.style,{maxWidth:'none',maxHeight:'none',width:box.width+'px',height:box.height+'px'}); const capture=grip||title; capture.setPointerCapture(event.pointerId); interaction={pane,capture,area,x,y,width:box.width,height:box.height, startX:event.clientX,startY:event.clientY,direction:grip?.dataset.toolResize,id:event.pointerId}; pane.classList.add('tool-adjusting'); event.preventDefault(); }); document.addEventListener('pointermove',event=>{ if(interaction?.id!==event.pointerId)return; const {pane,area,x,y,width,height,startX,startY,direction}=interaction; const dx=event.clientX-startX,dy=event.clientY-startY; if(!direction){ pane.style.left=clamp(x+dx,0,Math.max(0,area.width-width))+'px'; pane.style.top=clamp(y+dy,0,Math.max(0,area.height-height))+'px'; return; } const next=resizeToolRect({x,y,width,height},direction,dx,dy,area,230,145); Object.assign(pane.style,{left:next.x+'px',top:next.y+'px',width:next.width+'px',height:next.height+'px'}); }); function finish(event){ if(interaction?.id!==event.pointerId)return; const {pane,capture,id}=interaction; interaction=undefined; pane.classList.remove('tool-adjusting'); if(capture.hasPointerCapture(id))capture.releasePointerCapture(id); pane.dispatchEvent(new CustomEvent('cad:window-layout',{bubbles:true})); } document.addEventListener('pointerup',finish); document.addEventListener('pointercancel',finish); document.addEventListener('lostpointercapture',finish);