drawDB/src/components/note.jsx

289 lines
11 KiB
React
Raw Normal View History

2023-09-19 20:50:09 +08:00
import React, { useContext, useState } from "react";
2023-09-19 20:50:20 +08:00
import {
LayoutContext,
NoteContext,
UndoRedoContext,
TabContext,
2023-09-19 20:50:28 +08:00
SelectContext,
2023-09-19 20:50:20 +08:00
} from "../pages/editor";
import { Action, ObjectType, noteThemes, Tab } from "../data/data";
import { Input, Button, Popover, Toast } from "@douyinfe/semi-ui";
import {
IconEdit,
IconDeleteStroked,
IconCheckboxTick,
} from "@douyinfe/semi-icons";
2023-09-19 20:49:09 +08:00
export default function Note(props) {
2023-09-19 20:50:20 +08:00
const [editField, setEditField] = useState({});
const [hovered, setHovered] = useState(false);
const [saved, setSaved] = useState(false);
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:20 +08:00
const { updateNote, deleteNote } = useContext(NoteContext);
2023-09-19 20:50:09 +08:00
const { setUndoStack, setRedoStack } = useContext(UndoRedoContext);
2023-09-19 20:50:20 +08:00
const { layout } = useContext(LayoutContext);
const { tab, setTab } = useContext(TabContext);
2023-09-19 20:50:28 +08:00
const { selectedElement, setSelectedElement } = useContext(SelectContext);
2023-09-19 20:50:20 +08:00
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 (
2023-09-19 20:50:20 +08:00
<g
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
2023-09-19 20:49:09 +08:00
<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 },
2023-09-19 20:50:52 +08:00
message: `Edit note content to "${e.target.value}"`,
2023-09-19 20:50:09 +08:00
},
]);
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>
2023-09-19 20:50:28 +08:00
{(hovered ||
(selectedElement.element === ObjectType.NOTE &&
selectedElement.id === props.data.id &&
selectedElement.openDialogue &&
!layout.sidebar)) && (
2023-09-19 20:50:20 +08:00
<div className="absolute top-2 right-3">
<Popover
2023-09-19 20:50:28 +08:00
visible={
selectedElement.element === ObjectType.NOTE &&
selectedElement.id === props.data.id &&
selectedElement.openDialogue &&
!layout.sidebar
}
onClickOutSide={() => {
setSelectedElement((prev) => ({
...prev,
openDialogue: false,
}));
}}
stopPropagation
2023-09-19 20:50:20 +08:00
content={
2023-09-19 20:51:08 +08:00
<div className="popover-theme">
2023-09-19 20:50:20 +08:00
<div className="font-semibold mb-2 ms-1">Edit note</div>
<div className="w-[280px] flex items-center mb-2">
<Input
value={props.data.title}
placeholder="Title"
className="me-2"
onChange={(value) =>
updateNote(props.data.id, { title: value })
}
onFocus={(e) => setEditField({ title: e.target.value })}
onBlur={(e) => {
setSaved(true);
if (e.target.value === editField.title) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.NOTE,
nid: props.data.id,
undo: editField,
redo: { title: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit note title to "${e.target.name}"`,
2023-09-19 20:50:20 +08:00
},
]);
setRedoStack([]);
}}
/>
<Popover
content={
2023-09-19 20:51:08 +08:00
<div className="popover-theme">
<div className="font-medium mb-1">Theme</div>
2023-09-19 20:50:20 +08:00
<hr />
<div className="py-3">
{noteThemes.map((c) => (
<button
key={c}
style={{ backgroundColor: c }}
className="p-3 rounded-full mx-1"
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.NOTE,
nid: props.data.id,
undo: { color: props.data.color },
redo: { color: c },
2023-09-19 20:50:52 +08:00
message: `Edit note color to ${c}`,
2023-09-19 20:50:20 +08:00
},
]);
setRedoStack([]);
updateNote(props.data.id, { color: c });
}}
>
{props.data.color === c ? (
<IconCheckboxTick
style={{ color: "white" }}
/>
) : (
<IconCheckboxTick style={{ color: c }} />
)}
</button>
))}
</div>
</div>
}
position="rightTop"
showArrow
>
<div
className="h-[32px] w-[32px] rounded"
style={{ backgroundColor: props.data.color }}
/>
</Popover>
</div>
<div className="flex">
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={() => {
Toast.success(`Note deleted!`);
deleteNote(props.data.id, true);
}}
>
Delete
</Button>
<Button
block
style={{ marginLeft: "8px" }}
onClick={() => {
if (!saved) {
if (props.data.name === editField.name) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.NOTE,
aid: props.data.id,
undo: editField,
redo: { title: props.data.title },
2023-09-19 20:50:52 +08:00
message: `Edit note title to "${props.data.title}"`,
2023-09-19 20:50:20 +08:00
},
]);
setRedoStack([]);
setSaved(false);
}
}}
>
Save
</Button>
</div>
</div>
}
trigger="custom"
position="rightTop"
showArrow
>
<Button
icon={<IconEdit />}
size="small"
theme="solid"
style={{
backgroundColor: "#2f68ad",
opacity: "0.7",
}}
onClick={() => {
if (layout.sidebar) {
setTab(Tab.notes);
if (tab !== Tab.notes) return;
document
.getElementById(`scroll_note_${props.data.id}`)
.scrollIntoView({ behavior: "smooth" });
} else {
2023-09-19 20:50:28 +08:00
setSelectedElement({
element: ObjectType.NOTE,
id: props.data.id,
openDialogue: true,
openCollapse: false,
});
2023-09-19 20:50:20 +08:00
}
}}
></Button>
</Popover>
</div>
)}
2023-09-19 20:49:09 +08:00
</div>
</foreignObject>
</g>
);
}