Files
Docwell/Web-App/frontend/src/stores/spaces.ts
T
tajniak81andClaude Opus 5.5 2a063a51be
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / desktop (ubuntu-latest) (push) Successful in 1h36m55s
CI / mobile (push) Failing after 1m47s
CI / go (Media) (push) Successful in 19m45s
CI / go (Server) (push) Failing after 26m1s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m49s
CI / docker (push) Failing after 37m4s
Add Turn into to the text toolbar, page icons from the sidebar, and steadier block handles
Turn into: the selection toolbar starts with the block's kind ("Heading 1")
and opens the list of kinds it can become, with a tick on the current
one. In a list it turns the whole list, as the block menu did; the grip
menu no longer has it.

Page icons: an editor clicks a page's icon in the sidebar tree to pick an
emoji from groups, recent picks or a search by name (any pasted emoji
works too), or to remove it. The emoji now stands in for the page glyph
rather than sitting before the title. Strings in all five languages.

Block handles: the link and grip buttons stay on the block the cursor is
in, which is highlighted while the editor has focus, sit centred on the
block's height, and no longer vanish crossing the gap from the text.

The sidebar shows the Docwell logo above the workspace switcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 08:47:47 +01:00

185 lines
6.0 KiB
TypeScript

// SPDX-License-Identifier: AGPL-3.0-or-later
import type { PageNode, Space, SpaceRole } from "@docwell/api-client";
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { useApi } from "../api";
import { applyMove, childrenByParent } from "../lib/tree";
import { useEditorPrefs } from "./preferences";
const rank: Record<SpaceRole, number> = { viewer: 1, commenter: 2, editor: 3, admin: 4 };
/** Whether space role has is at least need (mirrors the server's ranking). */
export function spaceAtLeast(has: SpaceRole | "" | undefined, need: SpaceRole): boolean {
return !!has && rank[has] >= rank[need];
}
const expandedKey = "docwell.expanded";
function rememberedExpanded(): Set<string> {
try {
return new Set(JSON.parse(localStorage.getItem(expandedKey) ?? "[]") as string[]);
} catch {
return new Set();
}
}
/** Spaces of the sidebar's workspace and their page trees. */
export const useSpacesStore = defineStore("spaces", () => {
const api = useApi();
const workspaceId = ref("");
const items = ref<Space[]>([]);
const loaded = ref(false);
/** Each space's live pages, flat in tree order. */
const trees = ref<Record<string, PageNode[]>>({});
/** Expanded page ids, plus "collapsed:<space id>" for spaces folded away (spaces start open). */
const expanded = ref(rememberedExpanded());
/** The page being dragged in the sidebar, if any. */
const dragging = ref<{ spaceId: string; id: string } | null>(null);
/** The space and page the main view shows, for highlighting and "New page". */
const active = ref({ space: "", page: "" });
const children = computed(() => {
const out: Record<string, Map<string, PageNode[]>> = {};
for (const [id, nodes] of Object.entries(trees.value)) out[id] = childrenByParent(nodes);
return out;
});
async function load(wsId: string) {
if (workspaceId.value !== wsId) {
workspaceId.value = wsId;
items.value = [];
trees.value = {};
loaded.value = false;
}
items.value = await api.workspaces.spaces(wsId);
loaded.value = true;
await Promise.all(items.value.map((s) => loadTree(s.id)));
}
async function loadTree(spaceId: string) {
trees.value[spaceId] = await api.spaces.pages(spaceId);
}
function space(id: string): Space | undefined {
return items.value.find((s) => s.id === id);
}
function childrenOf(spaceId: string, parent: string): PageNode[] {
return children.value[spaceId]?.get(parent) ?? [];
}
function canEdit(spaceId: string): boolean {
return spaceAtLeast(space(spaceId)?.role, "editor");
}
function persistExpanded() {
try {
localStorage.setItem(expandedKey, JSON.stringify([...expanded.value]));
} catch {
// per-browser convenience only
}
}
function isExpanded(id: string) {
return expanded.value.has(id);
}
function setExpanded(id: string, open: boolean) {
if (open === expanded.value.has(id)) return;
if (open) expanded.value.add(id);
else expanded.value.delete(id);
persistExpanded();
}
const spaceOpen = (id: string) => !expanded.value.has(`collapsed:${id}`);
const toggleSpace = (id: string) => setExpanded(`collapsed:${id}`, spaceOpen(id));
/** Opens every ancestor of a page so it is visible in the sidebar. */
function reveal(spaceId: string, pageId: string) {
const parentOf = new Map((trees.value[spaceId] ?? []).map((n) => [n.id, n.parent]));
for (let p = parentOf.get(pageId); p; p = parentOf.get(p)) setExpanded(p, true);
}
/** Makes a page; the editor preferences can title it with today's date, make it full width or a canvas. */
async function createPage(spaceId: string, parent = "", title?: string) {
const prefs = useEditorPrefs();
if (!title && prefs.dateTitles) {
const lang = document.documentElement.lang || undefined;
title = new Intl.DateTimeFormat(lang, { dateStyle: "long" }).format(new Date());
}
let page = await api.spaces.createPage(spaceId, title ? { parent, title } : { parent });
// The page starts in its workspace's mode; this device can choose otherwise.
const mode = prefs.newPageMode && prefs.newPageMode !== page.defaultMode ? prefs.newPageMode : null;
if (prefs.newPageFullWidth || mode)
page = await api.pages.update(page.id, {
...(prefs.newPageFullWidth ? { fullWidth: true } : {}),
...(mode ? { defaultMode: mode } : {}),
});
if (parent) setExpanded(parent, true);
await loadTree(spaceId);
return page;
}
/** Moves a page optimistically, then reloads the tree from the server either way. */
async function move(spaceId: string, id: string, parent: string, index: number) {
trees.value[spaceId] = applyMove(trees.value[spaceId] ?? [], id, parent, index);
if (parent) setExpanded(parent, true);
try {
await api.pages.move(id, parent, index);
} finally {
await loadTree(spaceId);
}
}
/** Mirrors a title change into the sidebar without a reload. */
function setTitle(spaceId: string, id: string, title: string) {
const node = trees.value[spaceId]?.find((n) => n.id === id);
if (node) node.title = title;
}
/** Sets a page's icon ("" removes it) and mirrors it into the sidebar. */
async function setIcon(spaceId: string, id: string, icon: string) {
const node = trees.value[spaceId]?.find((n) => n.id === id);
const before = node?.icon;
if (node) node.icon = icon;
try {
const saved = await api.pages.update(id, { icon });
if (node) node.icon = saved.icon;
return saved;
} catch (e) {
if (node) node.icon = before ?? "";
throw e;
}
}
async function trash(spaceId: string, id: string) {
await api.pages.trash(id);
await loadTree(spaceId);
}
return {
workspaceId,
dragging,
active,
items,
loaded,
trees,
load,
loadTree,
space,
childrenOf,
canEdit,
isExpanded,
setExpanded,
spaceOpen,
toggleSpace,
reveal,
createPage,
move,
setTitle,
setIcon,
trash,
};
});