feat(viewer): support editing chat messages

This commit is contained in:
coyotte508 2026-09-21 22:40:14 +02:00
commit db7cc6f558
4 changed files with 107 additions and 9 deletions

View file

@ -1,6 +1,6 @@
{
"name": "fuji-viewer",
"version": "0.1.2",
"version": "0.1.3",
"private": true,
"license": "AGPL-3.0-only",
"type": "module",
@ -11,7 +11,7 @@
"test:tutorial": "node --test src/tutorial/*.spec.ts"
},
"dependencies": {
"@boardgamers/protocol": "0.4.2",
"@boardgamers/protocol": "0.5.0",
"fuji-engine": "workspace:*"
},
"devDependencies": {

View file

@ -4,11 +4,14 @@
chatTranslationTitle,
chatTranslationLabel,
chatTranslationIconPath,
chatEditIconPath,
defaultChatEditLabels,
} from "@boardgamers/protocol/chat";
import type { Store } from "./store.svelte";
import { bindChatComposer, bindChatViewport, type ChatSuggestions } from "@boardgamers/protocol/chat/dom";
let { store }: { store: Store } = $props();
const chat = $derived(store.chatState);
const editLabels = $derived(chat.editLabels ?? defaultChatEditLabels);
const dates = $derived(chatDateSeparators(chat.messages));
let composer: HTMLInputElement | undefined = $state();
let list: HTMLDivElement | undefined = $state();
@ -121,6 +124,29 @@
>{/if}
{:else}{segment.text}{/if}
{/each}
{#if message._id && chat.canEdit && store.chat.canEdit(message)}
<button
type="button"
class="chat-edit"
title={editLabels.edit}
aria-label={editLabels.edit}
aria-pressed={chat.editingId === message._id}
disabled={!!chat.pending}
onclick={() => inputBinding?.edit(message._id!)}
>
<svg
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.7"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"><path d={chatEditIconPath} /></svg
>
</button>
{/if}
{#if message._id && message.type === "text" && chat.translationTarget}
{@const label = chatTranslationLabel(translation, chat.translationLabels)}
<button
@ -152,6 +178,21 @@
</div>
</div>
{#if chat.canSend && !chat.disabled}
{#if chat.editingId}
<div class="chat-editing">
<span role="status">{editLabels.edit}</span><button
class="chat-edit-cancel"
type="button"
title={editLabels.cancel}
aria-label={editLabels.cancel}
disabled={!!chat.pending}
onclick={() => {
store.chat.cancelEditing();
composer?.focus();
}}>×</button
>
</div>
{/if}
{#if candidates.length}<div class="mention-choices" aria-label="Mention a teammate">
{#each candidates as candidate, i}<button
type="button"
@ -166,7 +207,12 @@
class="chat-send"
disabled={!!chat.pending || !chat.draft.trim()}
type="button"
onclick={() => store.chat.submit()}>{chat.pending ? "Sending…" : "Send"}</button
onclick={() => store.chat.submit()}
>{chat.editingId
? `${editLabels.save}${chat.pending ? "…" : ""}`
: chat.pending
? "Sending…"
: "Send"}</button
>
</div>
{:else}<p class="notice">{notice}</p>{/if}
@ -176,6 +222,30 @@
{/if}
<style>
.chat-editing {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
font-size: 12px;
opacity: 0.85;
padding: 3px 0;
flex-shrink: 0;
}
.chat-edit-cancel {
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
min-height: 0;
padding: 0;
border: 0;
background: transparent;
color: inherit;
cursor: pointer;
}
.chat-translate {
display: inline-flex;
align-items: center;
@ -193,17 +263,45 @@
opacity: 0.55;
cursor: pointer;
}
.chat-edit {
display: inline-flex;
align-items: center;
justify-content: center;
vertical-align: middle;
width: 24px;
height: 24px;
min-height: 0;
padding: 4px;
margin-left: 3px;
border: 0;
border-radius: 3px;
background: transparent;
color: inherit;
opacity: 0.55;
cursor: pointer;
}
.chat-translate:hover,
.chat-translate:focus-visible {
opacity: 1;
}
.chat-edit:hover,
.chat-edit:focus-visible {
opacity: 1;
}
.chat-translate[aria-pressed="true"] {
opacity: 1;
background: #e6c78025;
}
.chat-edit[aria-pressed="true"] {
opacity: 1;
background: #e6c78025;
}
.chat-translate:disabled {
cursor: wait;
}
.chat-edit:disabled {
cursor: wait;
}
.chat-day {
display: flex;
align-items: center;

10
pnpm-lock.yaml generated
View file

@ -34,8 +34,8 @@ importers:
packages/viewer:
dependencies:
'@boardgamers/protocol':
specifier: 0.4.2
version: 0.4.2
specifier: 0.5.0
version: 0.5.0
fuji-engine:
specifier: workspace:*
version: link:../engine
@ -61,8 +61,8 @@ importers:
packages:
'@boardgamers/protocol@0.4.2':
resolution: {integrity: sha512-dFWwj/uIR8LZuiFSPX++63f4Ivd3cG4JFFWCCObOu6u5RYw5GBT95rcSIiiWRLkBkXWKXgv3awQ/RMrAf+uFDQ==}
'@boardgamers/protocol@0.5.0':
resolution: {integrity: sha512-9SjU0cSktwxeMLItCW5VmUeSFl3Kg965JkKFf+BKZBxR2a2dyFoMKE3RYN2+ZwrI6GNS1mA6Fki9HOHQnD2WRw==}
'@jridgewell/gen-mapping@0.3.13':
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
@ -631,7 +631,7 @@ packages:
snapshots:
'@boardgamers/protocol@0.4.2':
'@boardgamers/protocol@0.5.0':
dependencies:
zod: 4.6.2

View file

@ -1,5 +1,5 @@
packages:
- packages/*
minimumReleaseAgeExclude:
- "@boardgamers/protocol@0.4.0 || 0.4.2"
- "@boardgamers/protocol@0.4.0 || 0.4.2 || 0.5.0"
- "@boardgamers/protocol@0.2.2 || 0.3.0 || 0.3.1 || 0.3.3"