2023-09-19 20:49:11 +08:00
|
|
|
import React, { useContext } from "react";
|
2023-09-19 20:49:09 +08:00
|
|
|
import { NoteContext } from "../pages/editor";
|
|
|
|
|
|
|
|
export default function Note(props) {
|
|
|
|
const { setNotes } = useContext(NoteContext);
|
2023-09-19 20:49:11 +08:00
|
|
|
const w = 180;
|
2023-09-19 20:49:09 +08:00
|
|
|
const r = 3;
|
|
|
|
const fold = 24;
|
|
|
|
|
|
|
|
const handleChange = (e) => {
|
|
|
|
const textarea = document.getElementById(`note_${props.data.id}`);
|
|
|
|
textarea.style.height = "0";
|
|
|
|
textarea.style.height = textarea.scrollHeight + "px";
|
2023-09-19 20:49:11 +08:00
|
|
|
const newHeight = textarea.scrollHeight + 16 + 20 + 4;
|
2023-09-19 20:49:09 +08:00
|
|
|
setNotes((prev) =>
|
|
|
|
prev.map((n) => {
|
|
|
|
if (n.id === props.data.id) {
|
2023-09-19 20:49:11 +08:00
|
|
|
return { ...n, content: e.target.value, height: newHeight };
|
2023-09-19 20:49:09 +08:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<g>
|
|
|
|
<path
|
|
|
|
d={`M${props.data.x + fold} ${props.data.y} L${
|
2023-09-19 20:49:11 +08:00
|
|
|
props.data.x + w - r
|
|
|
|
} ${props.data.y} A${r} ${r} 0 0 1 ${props.data.x + w} ${
|
2023-09-19 20:49:09 +08:00
|
|
|
props.data.y + r
|
2023-09-19 20:49:11 +08:00
|
|
|
} L${props.data.x + w} ${
|
|
|
|
props.data.y + props.data.height - r
|
|
|
|
} A${r} ${r} 0 0 1 ${props.data.x + w - r} ${
|
|
|
|
props.data.y + props.data.height
|
|
|
|
} L${props.data.x + r} ${props.data.y + props.data.height} A${r} ${r} 0 0 1 ${
|
2023-09-19 20:49:09 +08:00
|
|
|
props.data.x
|
2023-09-19 20:49:11 +08:00
|
|
|
} ${props.data.y + props.data.height - r} L${props.data.x} ${props.data.y + fold}`}
|
|
|
|
fill={props.data.color}
|
2023-09-19 20:49:09 +08:00
|
|
|
stroke="#665b25"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
strokeWidth="0.6"
|
|
|
|
/>
|
|
|
|
<path
|
|
|
|
d={`M${props.data.x} ${props.data.y + fold} L${
|
|
|
|
props.data.x + fold - r
|
|
|
|
} ${props.data.y + fold} A${r} ${r} 0 0 0 ${props.data.x + fold} ${
|
|
|
|
props.data.y + fold - r
|
|
|
|
} L${props.data.x + fold} ${props.data.y} L${props.data.x} ${
|
|
|
|
props.data.y + fold
|
|
|
|
} Z`}
|
2023-09-19 20:49:11 +08:00
|
|
|
fill={props.data.color}
|
2023-09-19 20:49:09 +08:00
|
|
|
stroke="#665b25"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
strokeWidth="0.6"
|
|
|
|
/>
|
|
|
|
<foreignObject
|
|
|
|
x={props.data.x}
|
|
|
|
y={props.data.y}
|
2023-09-19 20:49:11 +08:00
|
|
|
width={w}
|
|
|
|
height={props.data.height}
|
2023-09-19 20:49:09 +08:00
|
|
|
onMouseDown={props.onMouseDown}
|
|
|
|
>
|
|
|
|
<div className="text-gray-900 select-none w-full h-full cursor-move px-3 py-2">
|
|
|
|
<label htmlFor={`note_${props.data.id}`} className="ms-5">{props.data.title}</label>
|
|
|
|
<textarea
|
|
|
|
id={`note_${props.data.id}`}
|
|
|
|
value={props.data.content}
|
|
|
|
onInput={handleChange}
|
2023-09-19 20:49:11 +08:00
|
|
|
className="mt-1 w-full resize-none outline-none overflow-y-hidden border-none select-none"
|
|
|
|
style={{backgroundColor: props.data.color}}
|
2023-09-19 20:49:09 +08:00
|
|
|
></textarea>
|
|
|
|
</div>
|
|
|
|
</foreignObject>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
}
|