-
-
Notifications
You must be signed in to change notification settings - Fork 94
fix(zoom): making zoom in+out a usable feature #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pongstr
wants to merge
3
commits into
zmkfirmware:main
Choose a base branch
from
pongstr:fix-zoom-in-out
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,153 @@ | ||
| import { FC, PropsWithChildren, useEffect, useRef } from "react"; | ||
|
|
||
| type KeyboardViewportType = PropsWithChildren<{ | ||
| className?: string; | ||
| }>; | ||
|
|
||
| const KEYMAP_SCALE = "keymap:scale"; | ||
pongstr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const DEFAULT_SCALE = window.localStorage.getItem(KEYMAP_SCALE) ?? "1"; | ||
|
|
||
| export const KeyboardViewport: FC<KeyboardViewportType> = ({ | ||
pongstr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| children, | ||
| className, | ||
| }) => { | ||
| const targetRef = useRef<HTMLDivElement>(null); | ||
| const scaleRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const setScale = (param: "increase" | "decrease") => { | ||
| if (!targetRef.current || !scaleRef.current) return; | ||
|
|
||
| const current = scaleRef.current.value; | ||
|
|
||
| if (param === "increase" && Number(current) < 2) { | ||
| scaleRef.current.value = String(Number(scaleRef.current.value) + 0.2); | ||
| } | ||
|
|
||
| if (param === "decrease" && Number(current) > 0.2) { | ||
| scaleRef.current.value = String(Number(scaleRef.current.value) - 0.2); | ||
| } | ||
|
|
||
| localStorage.setItem(KEYMAP_SCALE, scaleRef.current.value); | ||
| targetRef.current.style.setProperty( | ||
| "transform", | ||
| `scale(${scaleRef.current.value})`, | ||
| ); | ||
| }; | ||
|
|
||
| const resetScale = () => { | ||
| if (!targetRef.current || !scaleRef.current) return; | ||
| targetRef.current.style.translate = "unset"; | ||
| targetRef.current.style.setProperty("transform", "scale(1)"); | ||
| scaleRef.current.value = "1"; | ||
| localStorage.setItem(KEYMAP_SCALE, "1"); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (!targetRef.current) return; | ||
|
|
||
| const target = targetRef.current; | ||
| const offset = { x: 0, y: 0 }; | ||
| let isPanningActive = false; | ||
|
|
||
| function panStart(e: KeyboardEvent) { | ||
pongstr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (e.key !== " ") return; | ||
| e.preventDefault(); | ||
|
|
||
| target.style.cursor = "grab"; | ||
| isPanningActive = true; | ||
| } | ||
|
|
||
| function panEnd(e: KeyboardEvent) { | ||
| if (e.key !== " ") return; | ||
| isPanningActive = false; | ||
| target.style.cursor = "unset"; | ||
| } | ||
|
|
||
| function panMove(e: PointerEvent) { | ||
| if (!isPanningActive) return; | ||
| offset.x += e.movementX; | ||
| offset.y += e.movementY; | ||
| target.style.translate = `${offset.x}px ${offset.y}px`; | ||
| } | ||
|
|
||
| document.addEventListener("keydown", panStart); | ||
| document.addEventListener("keyup", panEnd); | ||
| target.addEventListener("pointermove", panMove); | ||
|
|
||
| return () => { | ||
| document.removeEventListener("keydown", panStart); | ||
| document.removeEventListener("keyup", panEnd); | ||
| target.removeEventListener("pointermove", panMove); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!scaleRef.current || !targetRef.current) return; | ||
|
|
||
| const input = scaleRef.current; | ||
| const target = targetRef.current; | ||
|
|
||
| input.value = DEFAULT_SCALE; | ||
| target.style.setProperty("transform", `scale(${DEFAULT_SCALE})`); | ||
|
|
||
| function onInputChange(e: Event) { | ||
| const value = (e.currentTarget as HTMLInputElement).value; | ||
| target.style.setProperty("transform", `scale(${value})`); | ||
| localStorage.setItem(KEYMAP_SCALE, value); | ||
| } | ||
|
|
||
| input.addEventListener("change", onInputChange); | ||
| return () => { | ||
| input.removeEventListener("change", onInputChange); | ||
pongstr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div | ||
| className={[ | ||
| "relative size-full overflow-hidden p-0 touch-none", | ||
| className, | ||
| ].join(" ")} | ||
| > | ||
| <div | ||
| ref={targetRef} | ||
| className="flex size-full origin-center items-center justify-center transition-transform" | ||
| > | ||
| {children} | ||
| </div> | ||
|
|
||
| <div className="absolute bottom-[10px] left-1/2 ml-[-170px] flex justify-center items-center w-[298px] gap-1 rounded-xl bg-muted py-1 select-none bg-base-300"> | ||
| <button | ||
| className="block px-4 py-1.5 bg-base-100 rounded-l-lg" | ||
| onClick={() => setScale("decrease")} | ||
| > | ||
| - | ||
| </button> | ||
| <div className="flex h-9 px-2 justify-center items-center bg-base-100"> | ||
| <input | ||
| type="range" | ||
| name="scale" | ||
| min={0.25} | ||
| max={2} | ||
| step={0.01} | ||
| ref={scaleRef} | ||
| defaultValue={DEFAULT_SCALE} | ||
| className="mx-auto h-1 w-28 cursor-pointer appearance-none rounded-lg" | ||
| /> | ||
| </div> | ||
| <button | ||
| className="block px-4 py-1.5 bg-base-100" | ||
| onClick={() => setScale("increase")} | ||
| > | ||
| + | ||
| </button> | ||
| <button | ||
| className="block px-4 py-1.5 bg-base-100 rounded-r-lg" | ||
| onClick={resetScale} | ||
| > | ||
| Auto | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useLocalStorageStatewas created for future local storage uses. I don't think we should remove it #31 (comment)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
restored
useLocalStorageState, i slightly modified the hook to have default serialize+deserializer, imho, these options should only be required for specific serialize+deserializer functions. lmk what you think ^^