Repository navigation
refactor(canvas): split KonvaObject god component #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
61cc1ad
refactor(canvas): split KonvaObject god component
u8array 01ef568
refactor(canvas): name the 1.3 fontHeight ratio and dedupe renderer P…
u8array a783674
fix(canvas): commit ImageObject drag only on end, snap during move
u8array 0e32253
fix(canvas): snap whole-line drag and guard image div-by-zero
u8array 2243f0c
fix(canvas): snap whole-line drag to absolute grid, not delta
u8array 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,136 @@ | ||
| import { useState, useEffect, useRef } from "react"; | ||
| import { Group, Image as KImage, Rect, Text } from "react-konva"; | ||
| import type Konva from "konva"; | ||
| import type { LabelObject } from "../../registry"; | ||
| import { dotsToPx, pxToDots } from "../../lib/coordinates"; | ||
| import { getImage } from "../../lib/imageCache"; | ||
| import type { KonvaObjectProps } from "./konvaObjectProps"; | ||
|
|
||
| type ImageLabelObject = Extract<LabelObject, { type: "image" }>; | ||
| type Props = Omit<KonvaObjectProps, "obj"> & { obj: ImageLabelObject }; | ||
|
|
||
| /** Image renderer. Hosted as its own component so hooks (useState/ | ||
| * useEffect for async image loading) can run without violating | ||
| * rules-of-hooks. The dispatcher in KonvaObject narrows `obj` | ||
| * before passing — no runtime cast needed here. */ | ||
| export function ImageObject({ | ||
| obj, | ||
| scale, | ||
| dpmm, | ||
| offsetX, | ||
| offsetY, | ||
| isSelected, | ||
| onSelect, | ||
| onChange, | ||
| snap, | ||
| }: Props) { | ||
|
u8array marked this conversation as resolved.
|
||
| const p = obj.props; | ||
| const cached = getImage(p.imageId); | ||
| const w = dotsToPx(p.widthDots, scale, dpmm); | ||
| // Guard against a 0-width cached image: the imageCache pipeline | ||
| // doesn't normally produce one, but a malformed file could leak | ||
| // through and div-by-zero would render NaN-sized canvas nodes. | ||
| const h = cached && cached.width > 0 | ||
| ? w * (cached.height / cached.width) | ||
| : w; | ||
| const x = offsetX + dotsToPx(obj.x, scale, dpmm); | ||
| const y = offsetY + dotsToPx(obj.y, scale, dpmm); | ||
|
|
||
| const [htmlImg, setHtmlImg] = useState<HTMLImageElement | null>(null); | ||
| // Reset the cached HTMLImageElement during render when the source changes, | ||
| // instead of inside an effect. The "set state during render on prop change" | ||
| // pattern is the official React workaround for what would otherwise be a | ||
| // setState-in-effect anti-pattern. The next render observes prevDataUrl | ||
| // already updated, so this does not loop. | ||
| const prevDataUrlRef = useRef<string | undefined>(cached?.dataUrl); | ||
| if (prevDataUrlRef.current !== cached?.dataUrl) { | ||
| prevDataUrlRef.current = cached?.dataUrl; | ||
| setHtmlImg(null); | ||
| } | ||
| useEffect(() => { | ||
| if (!cached) return; | ||
| let active = true; | ||
| const img = new window.Image(); | ||
| img.src = cached.dataUrl; | ||
| img.onload = () => { | ||
| if (active) setHtmlImg(img); | ||
| }; | ||
| return () => { | ||
| active = false; | ||
| }; | ||
| }, [cached]); | ||
|
|
||
| // Snap during drag for visual feedback; commit only on dragEnd so | ||
| // the store doesn't update on every mouse pixel. Mirrors the | ||
| // pattern KonvaObjectInner uses for shape/text objects. | ||
| const handleDragMove = (e: Konva.KonvaEventObject<DragEvent>) => { | ||
| e.target.position({ | ||
| x: | ||
| offsetX + | ||
| dotsToPx(snap(pxToDots(e.target.x() - offsetX, scale, dpmm)), scale, dpmm), | ||
| y: | ||
| offsetY + | ||
| dotsToPx(snap(pxToDots(e.target.y() - offsetY, scale, dpmm)), scale, dpmm), | ||
| }); | ||
| }; | ||
|
|
||
| const handleDragEnd = (e: Konva.KonvaEventObject<DragEvent>) => { | ||
| onChange({ | ||
| x: pxToDots(e.target.x() - offsetX, scale, dpmm), | ||
| y: pxToDots(e.target.y() - offsetY, scale, dpmm), | ||
| }); | ||
| }; | ||
|
u8array marked this conversation as resolved.
u8array marked this conversation as resolved.
|
||
|
|
||
| if (htmlImg && cached) { | ||
| return ( | ||
| <KImage | ||
| id={obj.id} | ||
| x={x} | ||
| y={y} | ||
| image={htmlImg} | ||
| width={w} | ||
| height={h} | ||
| stroke={isSelected ? "#6366f1" : undefined} | ||
| strokeWidth={isSelected ? 2 : 0} | ||
| draggable | ||
| onClick={(e) => | ||
| onSelect(e.evt.shiftKey || e.evt.ctrlKey || e.evt.metaKey) | ||
| } | ||
| onTap={() => onSelect(false)} | ||
| onDragMove={handleDragMove} | ||
| onDragEnd={handleDragEnd} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Group | ||
| id={obj.id} | ||
| x={x} | ||
| y={y} | ||
| draggable | ||
| onClick={(e) => | ||
| onSelect(e.evt.shiftKey || e.evt.ctrlKey || e.evt.metaKey) | ||
| } | ||
| onTap={() => onSelect(false)} | ||
| onDragMove={handleDragMove} | ||
| onDragEnd={handleDragEnd} | ||
| > | ||
| <Rect | ||
| width={w} | ||
| height={h} | ||
| fill="#f9fafb" | ||
| stroke={isSelected ? "#6366f1" : "#9ca3af"} | ||
| strokeWidth={isSelected ? 2 : 1} | ||
| dash={[4, 2]} | ||
| /> | ||
| <Text | ||
| x={6} | ||
| y={6} | ||
| text="🖼" | ||
| fontSize={Math.max(w * 0.3, 12)} | ||
| fill="#374151" | ||
| /> | ||
| </Group> | ||
| ); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.