generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds basic pan zoom functionality to the modules page. Next up #421 **Note:** This requires some tweaking honestly especially on the reset. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
92db013
commit 9d05682
Showing
9 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
export const callIconID = 'call-icon' | ||
|
||
export const callIcon = `<defs> | ||
<symbol id="${callIconID}" viewBox="0 0 24 24" stroke-width="1.5"> | ||
<path stroke-linecap="round" stroke-linejoin="round" fill="currentColor" d="M20.25 3.75v4.5m0-4.5h-4.5m4.5 0l-6 6m3 12c-8.284 0-15-6.716-15-15V4.5A2.25 2.25 0 014.5 2.25h1.372c.516 0 .966.351 1.091.852l1.106 4.423c.11.44-.054.902-.417 1.173l-1.293.97a1.062 1.062 0 00-.38 1.21 12.035 12.035 0 007.143 7.143c.441.162.928-.004 1.21-.38l.97-1.293a1.125 1.125 0 011.173-.417l4.423 1.106c.5.125.852.575.852 1.091V19.5a2.25 2.25 0 01-2.25 2.25h-2.25z" /> | ||
<symbol id="${callIconID}" fill="currentColor" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M20.25 3.75v4.5m0-4.5h-4.5m4.5 0l-6 6m3 12c-8.284 0-15-6.716-15-15V4.5A2.25 2.25 0 014.5 2.25h1.372c.516 0 .966.351 1.091.852l1.106 4.423c.11.44-.054.902-.417 1.173l-1.293.97a1.062 1.062 0 00-.38 1.21 12.035 12.035 0 007.143 7.143c.441.162.928-.004 1.21-.38l.97-1.293a1.125 1.125 0 011.173-.417l4.423 1.106c.5.125.852.575.852 1.091V19.5a2.25 2.25 0 01-2.25 2.25h-2.25z" /> | ||
</symbol> | ||
</defs> | ||
` | ||
|
||
export const controlIcons = { | ||
in: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" /> | ||
</svg> | ||
`, | ||
out: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" /> | ||
</svg>`, | ||
reset: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M7.5 3.75H6A2.25 2.25 0 003.75 6v1.5M16.5 3.75H18A2.25 2.25 0 0120.25 6v1.5m0 9V18A2.25 2.25 0 0118 20.25h-1.5m-9 0H6A2.25 2.25 0 013.75 18v-1.5M15 12a3 3 0 11-6 0 3 3 0 016 0z" /> | ||
</svg> | ||
`, | ||
} | ||
|
||
export const moduleVerbCls = 'module-verb' | ||
export const moduleTitleCls = 'module-title' | ||
export const vizID = 'modules-flow-chart' | ||
export const controlsID = 'pan-zoom-controls' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { svgZoom } from './svg-zoom' | ||
import { controlIcons } from './constants' | ||
|
||
export const createControls = ( | ||
zoom: ReturnType<typeof svgZoom>, | ||
): [Map<'in' | 'out' | 'reset', HTMLButtonElement>, () => void] => { | ||
const actions = ['in', 'out', 'reset'] as const | ||
const buttons: Map<(typeof actions)[number], HTMLButtonElement> = new Map() | ||
for (const action of actions) { | ||
const btn = document.createElement('button') | ||
btn.classList.add( | ||
...'relative inline-flex items-center bg-white dark:hover:bg-indigo-700 dark:bg-gray-700/40 px-2 py-2 text-gray-500 dark:text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-10'.split( | ||
' ', | ||
), | ||
) | ||
const scr = document.createElement('span') | ||
scr.classList.add('sr-only') | ||
scr.innerText = action | ||
const parser = new DOMParser() | ||
const doc = parser.parseFromString(controlIcons[action], 'image/svg+xml') | ||
const svg = doc.documentElement | ||
btn.replaceChildren(scr, svg) | ||
btn.addEventListener('click', zoom[action]) | ||
buttons.set(action, btn) | ||
} | ||
|
||
const removeEventListeners = () => { | ||
for (const action of actions) { | ||
buttons.get(action)?.removeEventListener('click', zoom[action]) | ||
} | ||
} | ||
return [buttons, removeEventListeners] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { SVG } from '@svgdotjs/svg.js' | ||
import '@svgdotjs/svg.panzoom.js/dist/svg.panzoom.esm.js' | ||
import { vizID } from './constants' | ||
|
||
export const svgZoom = () => { | ||
// enables panZoom | ||
const canvas = SVG(`#${vizID}`) | ||
//@ts-ignore: lib types bad | ||
?.panZoom() | ||
const box = canvas.bbox() | ||
return { | ||
to(id: string) { | ||
const module = canvas.findOne(`#${id}`) | ||
//@ts-ignore: lib types bad | ||
const bbox = module?.bbox() | ||
if (bbox) { | ||
canvas.zoom(2, { x: bbox.x, y: bbox.y }) | ||
} | ||
}, | ||
in() { | ||
const zoomLevel = canvas.zoom() | ||
canvas.zoom(zoomLevel + 0.1) // Increase the zoom level by 0.1 | ||
}, | ||
out() { | ||
const zoomLevel = canvas.zoom() | ||
canvas.zoom(zoomLevel - 0.1) // Decrease the zoom level by 0.1 | ||
}, | ||
reset() { | ||
canvas.viewbox(box).zoom(1, { x: 0, y: 0 }) // Reset zoom level to 1 and pan to origin | ||
}, | ||
} | ||
} |