drawDB/src/components/NotesOverview.jsx

231 lines
7.9 KiB
React
Raw Normal View History

2024-03-13 05:36:49 +08:00
import { useState } from "react";
2023-09-19 20:49:09 +08:00
import {
Empty,
Row,
Col,
Button,
2023-09-19 20:49:11 +08:00
Collapse,
AutoComplete,
TextArea,
Popover,
2023-09-19 20:51:13 +08:00
Input,
2023-09-19 20:49:11 +08:00
Toast,
2023-09-19 20:49:09 +08:00
} from "@douyinfe/semi-ui";
import {
IllustrationNoContent,
IllustrationNoContentDark,
} from "@douyinfe/semi-illustrations";
2023-09-19 20:49:11 +08:00
import {
IconDeleteStroked,
IconPlus,
IconSearch,
IconCheckboxTick,
} from "@douyinfe/semi-icons";
2024-03-15 22:37:22 +08:00
import { noteThemes, Action, ObjectType } from "../data/constants";
import useUndoRedo from "../hooks/useUndoRedo";
2024-03-13 05:36:49 +08:00
import useNotes from "../hooks/useNotes";
2023-09-19 20:49:09 +08:00
2023-12-16 11:39:13 +08:00
export default function NotesOverview() {
2024-03-13 05:36:49 +08:00
const { notes, updateNote, addNote, deleteNote } = useNotes();
const { setUndoStack, setRedoStack } = useUndoRedo();
2023-09-19 20:49:11 +08:00
const [value, setValue] = useState("");
2023-09-19 20:50:09 +08:00
const [editField, setEditField] = useState({});
2023-09-19 20:49:11 +08:00
const [activeKey, setActiveKey] = useState("");
const [filteredResult, setFilteredResult] = useState(
notes.map((t) => {
return t.title;
})
);
const handleStringSearch = (value) => {
setFilteredResult(
notes
.map((t) => {
return t.title;
})
.filter((i) => i.includes(value))
);
};
2023-09-19 20:49:09 +08:00
return (
<div>
<Row gutter={6}>
2023-09-19 20:49:11 +08:00
<Col span={16}>
<AutoComplete
data={filteredResult}
value={value}
showClear
prefix={<IconSearch />}
placeholder="Search..."
2023-09-19 20:51:08 +08:00
emptyContent={
<div className="p-3 popover-theme">No notes found</div>
}
2023-09-19 20:49:11 +08:00
onSearch={(v) => handleStringSearch(v)}
onChange={(v) => setValue(v)}
onSelect={(v) => {
const { id } = notes.find((t) => t.title === v);
setActiveKey(`${id}`);
document
.getElementById(`scroll_note_${id}`)
.scrollIntoView({ behavior: "smooth" });
}}
className="w-full"
/>
</Col>
<Col span={8}>
2023-09-19 20:49:57 +08:00
<Button icon={<IconPlus />} block onClick={() => addNote()}>
2023-09-19 20:49:09 +08:00
Add note
</Button>
</Col>
</Row>
{notes.length <= 0 ? (
2023-09-19 20:51:08 +08:00
<div className="select-none mt-2">
2023-09-19 20:49:09 +08:00
<Empty
image={
2023-09-19 20:51:08 +08:00
<IllustrationNoContent style={{ width: 154, height: 154 }} />
2023-09-19 20:49:09 +08:00
}
darkModeImage={
2023-09-19 20:51:08 +08:00
<IllustrationNoContentDark style={{ width: 154, height: 154 }} />
2023-09-19 20:49:09 +08:00
}
title="No text notes"
description="Add notes cuz why not!"
/>
</div>
) : (
2023-09-19 20:49:11 +08:00
<Collapse
activeKey={activeKey}
onChange={(k) => setActiveKey(k)}
accordion
>
2023-09-19 20:49:09 +08:00
{notes.map((n, i) => (
2023-09-19 20:49:11 +08:00
<Collapse.Panel
header={<div>{n.title}</div>}
itemKey={`${n.id}`}
id={`scroll_note_${n.id}`}
key={n.id}
>
2023-09-19 20:51:13 +08:00
<div className="flex items-center mb-2">
<div className="font-semibold me-2">Title:</div>
<Input
value={n.title}
placeholder="Title"
onChange={(value) => updateNote(n.id, { title: value })}
onFocus={(e) => setEditField({ title: e.target.value })}
onBlur={(e) => {
if (e.target.value === editField.title) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.NOTE,
nid: n.id,
undo: editField,
redo: { title: e.target.value },
message: `Edit note title to "${e.target.name}"`,
},
]);
setRedoStack([]);
}}
/>
</div>
2023-09-19 20:49:11 +08:00
<div className="flex justify-between align-top">
<TextArea
placeholder="Add content"
value={n.content}
autosize
onChange={(value) => {
const textarea = document.getElementById(`note_${n.id}`);
textarea.style.height = "0";
textarea.style.height = textarea.scrollHeight + "px";
const newHeight = textarea.scrollHeight + 16 + 20 + 4;
updateNote(n.id, { height: newHeight, content: value });
}}
2023-09-19 20:50:09 +08:00
onFocus={(e) =>
setEditField({ content: e.target.value, height: n.height })
}
onBlur={(e) => {
if (e.target.value === editField.name) return;
const textarea = document.getElementById(`note_${n.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: i,
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:49:11 +08:00
rows={3}
/>
<div className="ms-2">
<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:49:11 +08:00
<hr />
<div className="py-3">
{noteThemes.map((c) => (
<button
key={c}
style={{ backgroundColor: c }}
className="p-3 rounded-full mx-1"
2023-09-19 20:50:09 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.NOTE,
nid: i,
undo: { color: n.color },
redo: { color: c },
2023-09-19 20:50:52 +08:00
message: `Edit note color to ${c}`,
2023-09-19 20:50:09 +08:00
},
]);
setRedoStack([]);
updateNote(i, { color: c });
}}
2023-09-19 20:49:11 +08:00
>
{n.color === c ? (
<IconCheckboxTick style={{ color: "white" }} />
) : (
<IconCheckboxTick style={{ color: c }} />
)}
</button>
))}
</div>
</div>
}
trigger="click"
position="rightTop"
showArrow
>
<div
className="h-[32px] w-[32px] rounded mb-2"
style={{ backgroundColor: n.color }}
/>
</Popover>
<Button
icon={<IconDeleteStroked />}
type="danger"
onClick={() => {
Toast.success(`Note deleted!`);
2023-09-19 20:49:57 +08:00
deleteNote(i, true);
2023-09-19 20:49:11 +08:00
}}
></Button>
</div>
</div>
</Collapse.Panel>
2023-09-19 20:49:09 +08:00
))}
2023-09-19 20:49:11 +08:00
</Collapse>
2023-09-19 20:49:09 +08:00
)}
</div>
);
}