import { useState } from "react"; import { Action, ObjectType, Tab, State, noteThemes, } from "../../data/constants"; import { Input, Button, Popover } from "@douyinfe/semi-ui"; import { IconEdit, IconDeleteStroked, IconCheckboxTick, } from "@douyinfe/semi-icons"; import { useLayout, useUndoRedo, useSelect, useNotes, useSaveState, } from "../../hooks"; import { useTranslation } from "react-i18next"; export default function Note({ data, onPointerDown }) { const w = 180; const r = 3; const fold = 24; const [editField, setEditField] = useState({}); const [hovered, setHovered] = useState(false); const { layout } = useLayout(); const { t } = useTranslation(); const { setSaveState } = useSaveState(); const { updateNote, deleteNote } = useNotes(); const { setUndoStack, setRedoStack } = useUndoRedo(); const { selectedElement, setSelectedElement } = useSelect(); const handleChange = (e) => { const textarea = document.getElementById(`note_${data.id}`); textarea.style.height = "0"; textarea.style.height = textarea.scrollHeight + "px"; const newHeight = textarea.scrollHeight + 42; updateNote(data.id, { content: e.target.value, height: newHeight }); }; const handleBlur = (e) => { if (e.target.value === editField.content) return; const textarea = document.getElementById(`note_${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: data.id, undo: editField, redo: { content: e.target.value, height: newHeight }, message: t("edit_note", { noteTitle: e.target.value, extra: "[content]", }), }, ]); setRedoStack([]); }; const edit = () => { setSelectedElement((prev) => ({ ...prev, ...(layout.sidebar && { currentTab: Tab.NOTES }), ...(!layout.sidebar && { element: ObjectType.NOTE }), id: data.id, open: true, })); if (layout.sidebar && selectedElement.currentTab === Tab.NOTES) { document .getElementById(`scroll_note_${data.id}`) .scrollIntoView({ behavior: "smooth" }); } }; return ( e.isPrimary && setHovered(true)} onPointerLeave={(e) => e.isPrimary && setHovered(false)} onPointerDown={(e) => { // Required for onPointerLeave to trigger when a touch pointer leaves // https://stackoverflow.com/a/70976017/1137077 e.target.releasePointerCapture(e.pointerId); }} >
{(hovered || (selectedElement.element === ObjectType.NOTE && selectedElement.id === data.id && selectedElement.open && !layout.sidebar)) && (
{ if (selectedElement.editFromToolbar) { setSelectedElement((prev) => ({ ...prev, editFromToolbar: false, })); return; } setSelectedElement((prev) => ({ ...prev, open: false, })); setSaveState(State.SAVING); }} stopPropagation content={
{t("edit")}
updateNote(data.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: data.id, undo: editField, redo: { title: e.target.value }, message: t("edit_note", { noteTitle: e.target.value, extra: "[title]", }), }, ]); setRedoStack([]); }} />
{t("theme")}

{noteThemes.map((c) => ( ))}
} position="rightTop" showArrow >
} trigger="custom" position="rightTop" showArrow >
)}