Add real-time collaboration with page history
Pages are now shared Yjs documents. docwell-server relays the y-websocket protocol at /collab (token sent as the first message), flushes updates to page_yupdates every second, and compacts them into page_ydoc with yrs compiled to WebAssembly under wazero (ADR-3) after 30 s idle or when the last editor leaves. Compaction refreshes content_json/content_text and records page_versions; versions can be listed, previewed and restored. The editor connects through a small y-protocols provider with carets and presence. Pages saved before this change are seeded from content_json. The yrs module lives in Server/ycrdt-wasm and is built by go generate, CI and the Dockerfiles rather than committed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
abf55a426b
commit
5e1dcf4291
45 files changed
+3480
-213
No files matched your search
@@ -0,0 +1,102 @@
|
||||
<script setup lang="ts">
|
||||
import type { Page, PageVersion } from "@docwell/api-client";
|
||||
import { DocwellEditor, type JSONContent } from "@docwell/editor";
|
||||
import { DwButton, DwIcon } from "@docwell/ui";
|
||||
import { ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { useApi } from "../api";
|
||||
|
||||
// The page's saved versions, with a read-only preview and restore. Restoring
|
||||
// is an ordinary edit to the live document, so open editors update at once.
|
||||
const props = defineProps<{ page: Page; canRestore: boolean }>();
|
||||
const emit = defineEmits<{ close: []; error: [err: unknown] }>();
|
||||
const api = useApi();
|
||||
const { t, locale } = useI18n();
|
||||
|
||||
const versions = ref<PageVersion[] | null>(null);
|
||||
const selected = ref<PageVersion | null>(null);
|
||||
const preview = ref<JSONContent | null>(null);
|
||||
const busy = ref(false);
|
||||
|
||||
async function load() {
|
||||
selected.value = null;
|
||||
preview.value = null;
|
||||
try {
|
||||
versions.value = await api.pages.versions(props.page.id);
|
||||
} catch (e) {
|
||||
emit("error", e);
|
||||
}
|
||||
}
|
||||
watch(() => props.page.id, load, { immediate: true });
|
||||
|
||||
async function select(v: PageVersion) {
|
||||
selected.value = v;
|
||||
preview.value = null;
|
||||
try {
|
||||
const c = await api.pages.version(props.page.id, v.id);
|
||||
if (selected.value?.id === v.id) preview.value = (c.content as JSONContent | null) ?? { type: "doc", content: [] };
|
||||
} catch (e) {
|
||||
emit("error", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function restore() {
|
||||
const v = selected.value;
|
||||
if (!v || !confirm(t("page.history.confirmRestore", { date: when(v.created) }))) return;
|
||||
busy.value = true;
|
||||
try {
|
||||
await api.pages.restoreVersion(props.page.id, v.id);
|
||||
emit("close");
|
||||
} catch (e) {
|
||||
emit("error", e);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// PocketBase dates look like "2026-10-30 21:05:00.000Z".
|
||||
const when = (s: string) =>
|
||||
new Date(s.replace(" ", "T")).toLocaleString(locale.value, { dateStyle: "medium", timeStyle: "short" });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="rounded-md border border-line p-4" data-testid="page-history" :aria-label="$t('page.history.title')">
|
||||
<div class="flex items-center justify-between gap-2">
|
||||
<h2 class="dw-label">{{ $t("page.history.title") }}</h2>
|
||||
<DwButton variant="ghost" size="sm" @click="emit('close')">{{ $t("page.history.close") }}</DwButton>
|
||||
</div>
|
||||
|
||||
<p v-if="versions && !versions.length" class="mt-3 flex items-center gap-2 text-ui-md text-ink-muted">
|
||||
<DwIcon name="drop" :size="18" class="text-spring" />
|
||||
{{ $t("page.history.empty") }}
|
||||
</p>
|
||||
<div v-else-if="versions" class="mt-3 grid gap-4 sm:grid-cols-[14rem_1fr]">
|
||||
<ul class="flex max-h-80 flex-col gap-1 overflow-y-auto" role="listbox" :aria-label="$t('page.history.title')">
|
||||
<li v-for="v in versions" :key="v.id">
|
||||
<button
|
||||
type="button"
|
||||
role="option"
|
||||
:aria-selected="selected?.id === v.id"
|
||||
class="w-full rounded-sm px-2 py-1.5 text-left text-ui-md hover:bg-surface-hover"
|
||||
:class="{ 'bg-surface-hover font-semibold': selected?.id === v.id }"
|
||||
@click="select(v)"
|
||||
>
|
||||
<span class="block">{{ when(v.created) }}</span>
|
||||
<span v-if="v.author" class="block text-ui-sm text-ink-muted">{{ v.author }}</span>
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="min-w-0">
|
||||
<p v-if="!selected" class="text-ui-md text-ink-muted">{{ $t("page.history.pick") }}</p>
|
||||
<template v-else>
|
||||
<div class="max-h-80 overflow-y-auto rounded-sm border border-line px-4 py-3">
|
||||
<DocwellEditor v-if="preview" :key="selected.id" :content="preview" :editable="false" data-testid="version-preview" />
|
||||
</div>
|
||||
<DwButton v-if="canRestore" class="mt-3" size="sm" icon="restore" :disabled="busy || !preview" @click="restore">
|
||||
{{ $t("page.history.restore") }}
|
||||
</DwButton>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
Reference in new issue
Block a user