From 0a0f0f950ae95afb78d3fe71815b351f77f01eb9 Mon Sep 17 00:00:00 2001 From: Artemy Date: Sat, 29 Apr 2023 08:58:57 +0300 Subject: [PATCH] feat: noteEditing --- src/components/openai.js | 15 ++----- src/components/settingsInputs.jsx | 17 +++++--- src/localisation/en.js | 1 + src/localisation/ru.js | 1 + src/pages/create.jsx | 11 ++++- src/pages/note.jsx | 71 ++++++++++++++++++++++++++----- 6 files changed, 88 insertions(+), 28 deletions(-) diff --git a/src/components/openai.js b/src/components/openai.js index 258b6e5..4366455 100644 --- a/src/components/openai.js +++ b/src/components/openai.js @@ -17,10 +17,10 @@ import { Configuration, OpenAIApi } from "openai"; -async function Complete(setText, textUpdate) { +async function Complete(text) { document.body.style.cursor = "wait"; - let initText = localStorage.getItem("NoteText"); + let initText = text; const configuration = new Configuration({ apiKey: settings.openAiKey, @@ -37,16 +37,9 @@ async function Complete(setText, textUpdate) { logprobs: null, }); - let totalText = initText + response.data.choices[0].text; - - localStorage.setItem("NoteText", totalText); - setText(totalText); - - if (settings.CollabEdit === true) { - textUpdate(totalText, true); - } - document.body.style.cursor = "default"; + + return initText + response.data.choices[0].text; } export { Complete }; diff --git a/src/components/settingsInputs.jsx b/src/components/settingsInputs.jsx index 2a21e0f..a9374d5 100644 --- a/src/components/settingsInputs.jsx +++ b/src/components/settingsInputs.jsx @@ -112,7 +112,7 @@ function SettingsSection({ name, children }) { ); } -function NoteNameInput({ value, onChange, preview }) { +function NoteNameInput({ value, onChange, preview = false }) { return ( ); } -function NoteTextArea({ value, onChange, preview }) { +function NoteTextArea({ value, onChange, preview = false }) { return ( ); } -function NotesAdditionalSettings() { +function NotesAdditionalSettings({ + noteText = localStorage.getItem("NoteText"), + onClick, +}) { return ( <> {settings.additionalFeatures && ( @@ -157,7 +160,9 @@ function NotesAdditionalSettings() { className="m-1" w="w-full" onClick={() => { - Complete(setText, textUpdate); + let text = Complete(noteText); + + onClick(text); }} /> )} diff --git a/src/localisation/en.js b/src/localisation/en.js index 5c815cb..7fc8923 100644 --- a/src/localisation/en.js +++ b/src/localisation/en.js @@ -75,6 +75,7 @@ let en = { LocalNote: "Local", Menu: "Menu", SourceCode: "Source code", + Edit: "Edit", }; export default en; diff --git a/src/localisation/ru.js b/src/localisation/ru.js index 73c8689..9ddbc77 100644 --- a/src/localisation/ru.js +++ b/src/localisation/ru.js @@ -77,6 +77,7 @@ let ru = { PublishNote: "Публичная", Menu: "Меню", SourceCode: "Исходный код", + Edit: "Изменить", }; export default ru; diff --git a/src/pages/create.jsx b/src/pages/create.jsx index f3f521d..2304045 100644 --- a/src/pages/create.jsx +++ b/src/pages/create.jsx @@ -199,7 +199,16 @@ function CreateNote() { /> - + { + localStorage.setItem("NoteText", text); + setText(text); + + if (settings.CollabEdit === true) { + textUpdate(text, true); + } + }} + /> ); diff --git a/src/pages/note.jsx b/src/pages/note.jsx index 81ddc0c..6e84d49 100644 --- a/src/pages/note.jsx +++ b/src/pages/note.jsx @@ -16,14 +16,27 @@ */ import { useParams } from "react-router-dom"; -import { ChevronDoubleLeftIcon, TrashIcon } from "@heroicons/react/24/outline"; +import { + ChevronDoubleLeftIcon, + PencilIcon, + TrashIcon, +} from "@heroicons/react/24/outline"; import { ButtonWithIcon } from "../components/button"; import Note from "../components/note"; +import { useState } from "react"; +import { + NoteNameInput, + NoteTextArea, + NotesAdditionalSettings, +} from "../components/settingsInputs"; function NotePage() { let params = useParams(); - let note = localStorage.getObj("Notes")[params.id]; + let notes = localStorage.getObj("Notes"); + let note = notes[params.id]; + + let [edit, setEdit] = useState(false); return (
@@ -34,23 +47,61 @@ function NotePage() { text={locals.Notes} /> - {note ? :
{locals.NoteNotExists}
} + {note ? ( + edit ? ( + <> + (note.name = e.target.value)} + /> + (note.text = e.target.value)} + /> + + { + note.text = text; + }} + /> + + ) : ( + + ) + ) : ( +
{locals.NoteNotExists}
+ )} {note && (
{ - let notesObj = localStorage.getObj("Notes"); + setEdit(!edit); - delete notesObj[params.id]; - - localStorage.setObj("Notes", notesObj); + if (edit) { + localStorage.setObj("Notes", notes); + } }} /> + {!edit && ( + { + let notesObj = localStorage.getObj("Notes"); + + delete notesObj[params.id]; + + localStorage.setObj("Notes", notesObj); + }} + /> + )}
)}