Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/assets/controlIcons/editIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/lib/components/Message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import Markdown from 'svelte-exmarkdown'
import { gfmPlugin } from 'svelte-exmarkdown/gfm'

import edit from '$lib/assets/controlIcons/editIcon.svg?raw'

let {
value,
ondelete,
onedit,
isLast,
}: {
value: Message
Expand Down Expand Up @@ -61,5 +64,13 @@
></svg
>
</button>

<button
class="cursor-pointer w-4 h-4 stroke-current"
onclick={() => onedit(value)}
title="Edit a note"
>
{@html edit}
</button>
</div>
</div>
4 changes: 4 additions & 0 deletions src/lib/index.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class Messages {
delete this.inner[id]
}

edit(message: Message, newContent: string) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я имел в виду что вообще метод не нужен, поскольку логика вряд ли поменяется, ведь сообщение - сигнал. Просто подсосемся на его обновление и в будущем будем с бэкендом взаимодействовать

message.data.content = newContent
}

get length(): number {
return Object.keys(this.inner).length
}
Expand Down
58 changes: 53 additions & 5 deletions src/routes/+page.svelte
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'
Expand Down Expand Up @@ -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() {
Copy link
Owner

Choose a reason for hiding this comment

The 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
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The 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) {
Copy link
Owner

Choose a reason for hiding this comment

The 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}
Expand All @@ -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}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Необязательно описывать оба варианта, можно в class описать ситуацию по умолчанию, когда !currentEditingMessage, а потом под ним описать дополнение с class:bg-orange-100. Оранжевый может оверрайдить серый, а приоритет у него будет выше, поскольку он указывается после дефолтного. В такой реализации пропадёт дублирование проверки currentEditingMessage

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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..."
Expand Down