import { syncGraphicalTools } from './graphical-tools.mjs'; const $ = id => document.getElementById(id); const emit = (name, detail) => window.dispatchEvent(new CustomEvent(name, {detail})); const mode = $('mode'); const weave = $('weaveWindow'); let state, docked = false, weaveWasHidden = true; const banner = document.createElement('span'); banner.id = 'showModeStatus'; document.querySelector('footer').prepend(banner); const names = {graphical:'Graphical',cad:'Design',simulation:'Simulation · standard surface',fabric:'Fabric · design finish'}; function sync() { const graphical = mode.value === 'graphical' && state && !state.raster && !state.indexed; if (graphical && !docked) { weaveWasHidden = weave.hidden; weave.classList.remove('minimized'); } if (graphical) weave.hidden = false; else if (docked) weave.hidden = weaveWasHidden; docked = Boolean(graphical); document.body.classList.toggle('show-graphical',docked); syncGraphicalTools(state,docked); banner.textContent = state?.raster ? 'Saved image' : docked ? 'Graphical · ' + names[state.previewMode] : names[mode.value]; for (const button of document.querySelectorAll('[data-show-mode]')) { button.setAttribute('aria-pressed',String(!state?.raster && button.dataset.showMode === mode.value)); button.disabled = Boolean(mode.disabled); } emit('cad:repeat-frame'); } function showMode(value) { if (!(value in names) || mode.disabled) return; if (mode.value === value && !state?.raster) return; mode.value = value; mode.dispatchEvent(new Event('change')); } function stepZoom(direction) { const select = docked ? $('weaveCellSize') : $('zoom'); const values = [...select.options].map(option => Number(option.value)).sort((a,b) => a-b); const current = Number(select.value); const value = direction > 0 ? values.find(n => n > current) : values.reverse().find(n => n < current); if (value === undefined) return; select.value = String(value); select.dispatchEvent(new Event('change')); } function ruler() { document.body.classList.toggle('hide-design-rulers'); } function repeat() { document.body.classList.toggle('show-repeat'); emit('cad:repeat-frame'); } window.addEventListener('cad:document',event => { state=event.detail; sync(); }); window.addEventListener('cad:show-ruler',ruler); window.addEventListener('cad:show-repeat',repeat); mode.addEventListener('change',sync); document.addEventListener('click',event => { const button = event.target.closest('[data-show-mode]'); if (button) showMode(button.dataset.showMode); }); document.addEventListener('keydown',event => { if (event.defaultPrevented || event.metaKey || event.repeat || event.target.closest('input,textarea,select,[contenteditable]') || document.querySelector('dialog[open]')) return; let action; if (event.ctrlKey && event.altKey && !event.shiftKey && event.key.toLowerCase()==='o') { action = ()=>emit('cad:run-command','fabric-browser'); } else if (event.altKey) { return; } else if (event.ctrlKey && !event.shiftKey) { action = { d:()=>emit('cad:run-command','draft'), g:()=>emit('cad:run-command','peg-numeric'), i:()=>emit('cad:run-command','technical'), p:()=>emit('cad:run-command','print'), w:()=>emit('cad:run-command','library-weaves'), l:()=>emit('cad:run-command','weave-clip-book'), F5:()=>showMode('fabric') }[event.key]; } else if (!event.ctrlKey && event.shiftKey) { action = { d:()=>emit('cad:run-command','draft-graphical'), p:()=>emit('cad:run-command','peg-plan'), w:()=>emit('cad:show-window','weave') }[event.key.toLowerCase()]; } else if (!event.ctrlKey) { action = { F2:repeat,F4:()=>showMode('graphical'),F5:()=>showMode('cad'), F6:()=>showMode('simulation'),F7:()=>emit('cad:show-window','yarn'),F9:ruler, PageUp:()=>stepZoom(1),PageDown:()=>stepZoom(-1) }[event.key]; } if (action) { event.preventDefault(); action(); } }, true); emit('cad:request-state');