Last active
March 15, 2022 15:30
-
-
Save colinfwren/db9a08bf2361856ce4213ff8816ac4d5 to your computer and use it in GitHub Desktop.
A component that uses the editable text component
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
import React, { useState } from "react"; | |
import { Group, Rect } from "react-konva"; | |
import { EditableText } from "./EditableText"; | |
export function EditableTextUsage({ selected, setSelected }) { | |
const [isEditing, setIsEditing] = useState(false); | |
const [isTransforming, setIsTransforming] = useState(false); | |
const [text, setText] = useState("Click to resize. Double click to edit."); | |
const [width, setWidth] = useState(200); | |
const [height, setHeight] = useState(200); | |
useEffect(() => { | |
if (!selected && isEditing) { | |
setIsEditing(false); | |
} else if (!selected && isTransforming) { | |
setIsTransforming(false); | |
} | |
}, [selected, isEditing, isTransforming]); | |
function toggleEdit() { | |
setIsEditing(!isEditing); | |
setSelected(!isEditing); | |
} | |
function toggleTransforming() { | |
setIsTransforming(!isTransforming); | |
setSelected(!isTransforming); | |
} | |
function onTextResize(newWidth, newHeight) { | |
setWidth(newWidth); | |
setHeight(newHeight); | |
} | |
function onTextChange(value) { | |
setText(value) | |
} | |
return ( | |
<EditableText | |
x={0} | |
y={0} | |
text={text} | |
width={width} | |
height={height} | |
onResize={onTextResize} | |
isEditing={isEditing} | |
isTransforming={isTransforming} | |
onToggleEdit={toggleEdit} | |
onToggleTransform={toggleTransforming} | |
onChange={onTextChange} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Colin @colinfwren !
I forked in
codesandbox.io
your excellent work to develop it further.The hurdle I encountered is how to edit only a specific
note
without affecting the text of the other notes, when we have more than one notesHere you can find the code:
https://codesandbox.io/s/react-konva-editable-resizable-text-forked-kz1uuo?file=/src/StickyNote.jsx
Looking forward to your kind help