-
Notifications
You must be signed in to change notification settings - Fork 0
Added editMessage function #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| <script lang="ts"> | ||
| import Message from '$lib/components/Message.svelte' | ||
| import { createMessage, messages, type Id } from '$lib/index.svelte' | ||
| import MessageComponent from '$lib/components/Message.svelte' | ||
| import { | ||
| createMessage, | ||
| messages, | ||
| type Id, | ||
| type Message, | ||
| } from '$lib/index.svelte' | ||
| import { page } from '$app/stores' | ||
| import { goto } from '$app/navigation' | ||
| import { browser } from '$app/environment' | ||
|
|
@@ -30,17 +35,27 @@ | |
| let currentChat = $derived(messages.chatFrom(leafId!, 15).toReversed()) | ||
| let lastId = $derived(messages.untilBranchOrLeaf(leafId!)) | ||
| let currentEditingMessage: Message | null = $state(null) | ||
| let textInput = $state('') | ||
| // TODO: Make a multiline chat input that grows dynamically | ||
| let textInputElement: HTMLInputElement | null = $state(null) | ||
| function saveMessagesOnLocalStorage() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. saveMessagesToLocalStorage |
||
| localStorage.setItem('notes', JSON.stringify(messages.export())) | ||
| } | ||
| function appendMessage() { | ||
| if (currentEditingMessage) { | ||
| onEditMessage() | ||
| return | ||
| } | ||
| let message = createMessage(textInput) | ||
| leafId = messages.add(leafId, message) | ||
| textInput = '' | ||
| localStorage.setItem('notes', JSON.stringify(messages.export())) | ||
| saveMessagesOnLocalStorage() | ||
| let url = $page.url | ||
| url.hash = leafId | ||
|
|
@@ -57,16 +72,46 @@ | |
| messages.delete(value.id) | ||
| localStorage.setItem('notes', JSON.stringify(messages.export())) | ||
| } | ||
| function onEditMessage() { | ||
| if (!currentEditingMessage) return | ||
| messages.edit(currentEditingMessage, textInput) | ||
| saveMessagesOnLocalStorage() | ||
| currentEditingMessage = null | ||
| textInput = '' | ||
| setTimeout(() => textInputElement?.focus(), 0) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше поменять на tick, в svelte есть |
||
| } | ||
| function startEditing(value: Message) { | ||
| textInput = value.data.content | ||
| currentEditingMessage = value | ||
| textInputElement?.focus() | ||
| } | ||
| function startEditByShortcut(e: KeyboardEvent) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. startEditingByShortcut, как в функции выше |
||
| if (e.key === 'ArrowUp') { | ||
| const lastMessage = messages.get(lastId) | ||
| if (lastMessage) { | ||
| startEditing(lastMessage) | ||
| } | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <!-- TODO: Fullsize note editor on a separate page or in a bottom sheet --> | ||
| <div class="container mx-auto"> | ||
| <div class="h-screen flex flex-col justify-end overflow-y-auto p-4 pb-36"> | ||
| {#if currentChat} | ||
| {#each currentChat as message} | ||
| <Message | ||
| <MessageComponent | ||
| value={message} | ||
| ondelete={deleteMessage} | ||
| onedit={startEditing} | ||
| isLast={message.id === lastId} | ||
| /> | ||
| {/each} | ||
|
|
@@ -93,14 +138,17 @@ | |
| </div> | ||
|
|
||
| <footer | ||
| class="dark:text-white bg-slate-100 dark:bg-slate-700 border-t border-gray-300 dark:border-slate-600 p-4 absolute left-0 bottom-0 w-full" | ||
| class:bg-orange-100={currentEditingMessage} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Необязательно описывать оба варианта, можно в class описать ситуацию по умолчанию, когда !currentEditingMessage, а потом под ним описать дополнение с class:bg-orange-100. Оранжевый может оверрайдить серый, а приоритет у него будет выше, поскольку он указывается после дефолтного. В такой реализации пропадёт дублирование проверки currentEditingMessage
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я изначально так и сделал, это не сработало |
||
| class:bg-slate-100={!currentEditingMessage} | ||
| class="dark:text-white dark:bg-slate-700 border-t border-gray-300 dark:border-slate-600 p-4 absolute left-0 bottom-0 w-full" | ||
| > | ||
| <form | ||
| class="flex items-center container mx-auto" | ||
| onsubmit={appendMessage} | ||
| > | ||
| <input | ||
| type="text" | ||
| onkeydown={startEditByShortcut} | ||
| bind:this={textInputElement} | ||
| bind:value={textInput} | ||
| placeholder="Type a message..." | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я имел в виду что вообще метод не нужен, поскольку логика вряд ли поменяется, ведь сообщение - сигнал. Просто подсосемся на его обновление и в будущем будем с бэкендом взаимодействовать