drawDB/src/components/note.jsx

99 lines
3.5 KiB
React
Raw Normal View History

2023-09-19 20:50:09 +08:00
import React, { useContext, useState } from "react";
import { NoteContext, UndoRedoContext } from "../pages/editor";
import { Action, ObjectType } from "../data/data";
2023-09-19 20:49:09 +08:00
export default function Note(props) {
2023-09-19 20:50:15 +08:00
const { updateNote } = 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;
2023-09-19 20:50:09 +08:00
const { setUndoStack, setRedoStack } = useContext(UndoRedoContext);
const [editField, setEditField] = useState({});
2023-09-19 20:49:09 +08:00
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:50:00 +08:00
const newHeight = textarea.scrollHeight + 41;
2023-09-19 20:50:15 +08:00
updateNote(props.data.id, { content: e.target.value, height: newHeight });
2023-09-19 20:49:09 +08:00
};
return (
<g>
<path
2023-09-19 20:50:00 +08:00
d={`M${props.data.x + fold} ${props.data.y} L${props.data.x + w - r} ${
props.data.y
} A${r} ${r} 0 0 1 ${props.data.x + w} ${props.data.y + r} L${
props.data.x + w
} ${props.data.y + props.data.height - r} A${r} ${r} 0 0 1 ${
2023-09-19 20:49:11 +08:00
props.data.x + w - r
2023-09-19 20:50:00 +08:00
} ${props.data.y + props.data.height} L${props.data.x + r} ${
2023-09-19 20:49:11 +08:00
props.data.y + props.data.height
2023-09-19 20:50:00 +08:00
} A${r} ${r} 0 0 1 ${props.data.x} ${
props.data.y + props.data.height - r
} L${props.data.x} ${props.data.y + fold}`}
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"
/>
<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">
2023-09-19 20:50:00 +08:00
<label htmlFor={`note_${props.data.id}`} className="ms-5">
{props.data.title}
</label>
2023-09-19 20:49:09 +08:00
<textarea
id={`note_${props.data.id}`}
value={props.data.content}
2023-09-19 20:50:00 +08:00
onChange={handleChange}
2023-09-19 20:50:09 +08:00
onFocus={(e) =>
setEditField({
content: e.target.value,
height: props.data.height,
})
}
onBlur={(e) => {
if (e.target.value === editField.name) return;
const textarea = document.getElementById(`note_${props.data.id}`);
textarea.style.height = "0";
textarea.style.height = textarea.scrollHeight + "px";
const newHeight = textarea.scrollHeight + 16 + 20 + 4;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.NOTE,
nid: props.data.id,
undo: editField,
redo: { content: e.target.value, height: newHeight },
},
]);
setRedoStack([]);
}}
2023-09-19 20:50:00 +08:00
className="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>
);
}