Files
Docwell/Web-App/frontend/src/components/CommentBox.vue
T
tajniak81andClaude Opus 5.5 2b78c167e2 Add comment threads, reactions, notifications and page watching
Comments are now threads: replies hang off a thread's first comment,
which alone points at text and can be resolved. Authors can edit their
own comments, and anyone who can comment can react with an emoji.
Resolved threads stop lighting text in the editor and fold away in the
panel. Comments can @mention people.

People watch the pages they create or comment on, and can watch or mute
a page from its menu. Muting is remembered. The inbox collects comments
on watched pages, replies in your threads, mentions (in comments and in
page text), pages shared with you and resolved threads. It shows an
unread badge in the sidebar, polled every minute, and links straight to
the thread. Nobody hears about their own actions or about pages they
cannot open, and a notification hides its text once access is lost.

Unread notifications are emailed as a digest every few minutes unless
the person turns that off. Read ones are pruned after 90 days. The jobs
registry can now run jobs on a timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 22:27:55 +01:00

164 lines
4.8 KiB
Vue

<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
<script setup lang="ts">
import type { MemberUser } from "@docwell/api-client";
import { DwButton } from "@docwell/ui";
import { computed, nextTick, ref, watch } from "vue";
import { useApi } from "../api";
// A comment text box with "@" mentions: typing @ and a few letters lists the
// workspace's people; picking one inserts "@Name" and remembers their id, so
// the server can notify them. Ctrl/Cmd+Enter sends, Esc cancels.
const props = defineProps<{
workspace: string;
placeholder: string;
submitLabel: string;
busy: boolean;
initial?: string;
autofocus?: boolean;
}>();
const emit = defineEmits<{ submit: [body: string, mentions: string[]]; cancel: [] }>();
const api = useApi();
const body = ref(props.initial ?? "");
const box = ref<HTMLTextAreaElement | null>(null);
const picked = ref<MemberUser[]>([]);
const matches = ref<MemberUser[]>([]);
const highlighted = ref(0);
// Where the "@" being completed starts, or -1.
const at = ref(-1);
let lookup = 0;
watch(
() => props.autofocus,
async (v) => {
if (!v) return;
await nextTick();
box.value?.focus();
},
{ immediate: true },
);
const label = (u: MemberUser) => u.name || u.email;
async function onInput() {
const el = box.value;
if (!el) return;
const before = body.value.slice(0, el.selectionStart);
const m = /(?:^|\s)@([^\s@]{0,30})$/.exec(before);
if (!m || !props.workspace) {
at.value = -1;
matches.value = [];
return;
}
at.value = before.length - m[1]!.length - 1;
const id = ++lookup;
const found = await api.workspaces.people(props.workspace, m[1]!, 6).catch(() => []);
if (id === lookup) {
matches.value = found;
highlighted.value = 0;
}
}
async function pick(u: MemberUser) {
const el = box.value;
if (!el || at.value < 0) return;
const end = el.selectionStart;
const text = `@${label(u)} `;
body.value = body.value.slice(0, at.value) + text + body.value.slice(end);
if (!picked.value.some((p) => p.id === u.id)) picked.value.push(u);
const caret = at.value + text.length;
at.value = -1;
matches.value = [];
await nextTick();
el.focus();
el.setSelectionRange(caret, caret);
}
function onKeydown(e: KeyboardEvent) {
if (matches.value.length) {
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
const n = matches.value.length;
highlighted.value = (highlighted.value + (e.key === "ArrowDown" ? 1 : n - 1)) % n;
return;
}
if (e.key === "Enter" || e.key === "Tab") {
e.preventDefault();
void pick(matches.value[highlighted.value]!);
return;
}
if (e.key === "Escape") {
e.preventDefault();
matches.value = [];
return;
}
}
if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
submit();
} else if (e.key === "Escape") {
e.preventDefault();
emit("cancel");
}
}
// Only people whose "@Name" is still in the text count as mentioned.
const mentions = computed(() => picked.value.filter((u) => body.value.includes(`@${label(u)}`)).map((u) => u.id));
function submit() {
if (!body.value.trim() || props.busy) return;
emit("submit", body.value, mentions.value);
}
/** Clears the box after the comment was saved. */
function reset() {
body.value = "";
picked.value = [];
}
defineExpose({ reset });
</script>
<template>
<form @submit.prevent="submit">
<div class="relative">
<textarea
ref="box"
v-model="body"
rows="2"
maxlength="5000"
class="dw-input w-full"
:aria-label="placeholder"
:placeholder="placeholder"
data-testid="comment-body"
@input="onInput"
@keydown="onKeydown"
@blur="matches = []"
/>
<ul
v-if="matches.length"
class="absolute left-0 top-full z-20 mt-1 w-64 rounded-sm border border-line bg-surface-raised py-1 shadow-dialog"
role="listbox"
:aria-label="$t('editor.mentions')"
>
<li v-for="(u, i) in matches" :key="u.id" role="option" :aria-selected="i === highlighted">
<button
type="button"
class="flex w-full flex-col px-3 py-1 text-left"
:class="i === highlighted ? 'bg-surface-hover' : ''"
@mousedown.prevent="pick(u)"
>
<span class="text-ui-md">{{ label(u) }}</span>
<span v-if="u.name" class="text-ui-xs text-ink-muted">{{ u.email }}</span>
</button>
</li>
</ul>
</div>
<div class="mt-2 flex justify-end gap-2">
<DwButton size="sm" variant="ghost" @click="emit('cancel')">{{ $t("comments.cancel") }}</DwButton>
<DwButton size="sm" type="submit" variant="primary" :disabled="busy || !body.trim()" data-testid="comment-send">
{{ submitLabel }}
</DwButton>
</div>
</form>
</template>