SPB Git

Repository Public

commands.ts 9.6 KB 132 lines typescript

1// Commandes de la palette (⌘K) : toute action dotée d'un raccourci, la navigation, l'apparence, les réglages.2// Les modules à venir ajoutent les leurs par `registerCommand` ; les libellés sont relus à chaque ouverture (langue).3import { t } from "../i18n"4import { S, routeHref, type AppState, type Density, type PreviewLines, type Theme } from "../state/app"5import { composeNew } from "../state/composer"6import type { Layout } from "../state/device"7import { closeThread, moveCursor, toggleSelect } from "../state/mail"8import { requestSnooze, type PaletteItem } from "../state/registry"9import { navigate, signOut, updateDevice, updatePrefs } from "../state/session"10import { currentTargets, doArchive, doDelete, doJunk, doMarkRead, doReply, doToggleFlag, doTogglePin, openRow } from "./actions"11import type { IconName } from "./kit"1213export type CommandSection = "actions" | "go" | "view" | "settings"14export interface Command extends PaletteItem {15  section: CommandSection16  /** absente = toujours proposée */17  when?: (s: AppState) => boolean18}1920const extra: (() => Command)[] = []21/** Point d'extension : ajoute une commande à la palette ; rend la fonction de retrait. */22export function registerCommand(make: () => Command): () => void {23  extra.push(make)24  return () => {25    const i = extra.indexOf(make)26    if (i >= 0) extra.splice(i, 1)27  }28}2930const inMail = (s: AppState) => s.route.name === "mail"31const hasThread = (s: AppState) => inMail(s) && !!s.open && s.open.status === "ready"32const hasTarget = (s: AppState) => inMail(s) && !s.all && (s.selected.size > 0 || hasThread(s) || !!s.cursor)3334export function goView(view: string) {35  const r = S.get().route36  navigate(routeHref({ name: "mail", mbIndex: r.name === "mail" ? r.mbIndex : 0, view, threadId: null, q: "" }))37}38export function focusSearch() {39  const s = S.get()40  // « Toutes les boîtes » : le champ de recherche d'un module (s'il y est monté) sert sur place41  if (s.route.name !== "mail" || (s.all && !document.getElementById("search-input"))) goView("inbox")42  else if (s.open && matchMedia("(max-width: 760px)").matches) closeThread()43  requestAnimationFrame(() => document.getElementById("search-input")?.focus())44}45export function toggleSidebar() {46  updateDevice({ sidebarCollapsed: !S.get().device.sidebarCollapsed })47}48export function snoozeTargets() {49  const tg = currentTargets()50  if (tg) requestSnooze(tg.ids, S.get().mb)51}5253export function builtinCommands(): Command[] {54  const s = S.get()55  const cmd = (id: string, section: CommandSection, title: string, icon: IconName, run: () => void, opts: { keys?: string; when?: (s: AppState) => boolean; keywords?: string[] } = {}): Command => ({ id, section, title, icon, run, ...opts })56  const theme = (v: Theme, label: string): Command => cmd(`theme.${v}`, "view", `${t("settings.appearance.theme")} : ${label}`, v === "light" ? "sun" : v === "system" ? "desktop" : "moon", () => updatePrefs({ theme: v }), { keywords: ["theme", "apparence", "appearance"] })57  const density = (v: Density, label: string): Command => cmd(`density.${v}`, "view", `${t("settings.appearance.density")} : ${label}`, "list", () => updatePrefs({ density: v }))58  const layout = (v: Layout, label: string, icon: IconName): Command => cmd(`layout.${v}`, "view", `${t("settings.appearance.layout")} : ${label}`, icon, () => updateDevice({ layout: v }), { keywords: ["disposition", "layout", "volet", "pane"] })59  const preview = (v: PreviewLines, label: string): Command => cmd(`preview.${v}`, "view", `${t("settings.appearance.preview")} : ${label}`, "text", () => updatePrefs({ previewLines: v }))60  const settings = (tab: string, label: string, icon: IconName): Command => cmd(`settings.${tab}`, "settings", `${t("nav.settings")} : ${label}`, icon, () => navigate(`/settings/${tab}`))61  const list: Command[] = [62    // ---- actions (celles des raccourcis clavier)63    cmd("compose", "actions", t("compose.new"), "edit", () => composeNew(), { keys: "c" }),64    cmd("search", "actions", t("help.search"), "search", focusSearch, { keys: "/" }),65    cmd("reply", "actions", t("action.reply"), "reply", () => doReply("reply"), { keys: "r", when: hasThread }),66    cmd("replyAll", "actions", t("action.replyAll"), "replyAll", () => doReply("replyAll"), { keys: "a", when: hasThread }),67    cmd("forward", "actions", t("action.forward"), "forward", () => doReply("forward"), { keys: "f", when: hasThread }),68    cmd("archive", "actions", t("action.archive"), "archive", () => void doArchive(), { keys: "e", when: hasTarget }),69    cmd("delete", "actions", t("action.delete"), "trash", () => void doDelete(), { keys: "#", when: hasTarget }),70    cmd("flag", "actions", t("help.flag"), "star", () => void doToggleFlag(), { keys: "s", when: hasTarget }),71    cmd("pin", "actions", t("palette.cmd.pin"), "pin", () => void doTogglePin(), { keys: "p", when: hasTarget }),72    cmd("read", "actions", t("action.markRead"), "mail", () => void doMarkRead(true), { keys: "Shift+I", when: hasTarget }),73    cmd("unread", "actions", t("action.markUnread"), "unread", () => void doMarkRead(false), { keys: "Shift+U", when: hasTarget }),74    cmd("junk", "actions", s.view === "junk" ? t("action.notJunk") : t("action.junk"), "junk", () => void doJunk(S.get().view !== "junk"), { keys: "!", when: hasTarget }),75    cmd("snooze", "actions", t("action.snooze"), "snooze", snoozeTargets, { keys: "b", when: hasTarget }),76    cmd("next", "actions", t("help.next"), "chevron", () => void moveCursor(1), { keys: "j", when: (x) => inMail(x) && !x.all }),77    cmd("prev", "actions", t("help.prev"), "chevron", () => void moveCursor(-1), { keys: "k", when: (x) => inMail(x) && !x.all }),78    cmd("open", "actions", t("help.open"), "eye", () => {79      const id = S.get().cursor ?? moveCursor(1)80      if (id) openRow(id)81    }, { keys: "o", when: (x) => inMail(x) && !x.all && !x.open }),82    cmd("select", "actions", t("help.select"), "check", () => {83      const id = S.get().cursor ?? moveCursor(1)84      if (id) toggleSelect(id)85    }, { keys: "x", when: (x) => inMail(x) && !x.all }),86    cmd("back", "actions", t("help.back"), "back", closeThread, { keys: "u", when: (x) => inMail(x) && !!x.open }),87    cmd("help", "actions", t("nav.shortcuts"), "help", () => S.set({ helpOpen: true }), { keys: "?", when: inMail }),88    // ---- navigation89    cmd("go.inbox", "go", t("help.goInbox"), "inbox", () => goView("inbox"), { keys: "g i" }),90    cmd("go.all", "go", t("help.goAll"), "inbox", () => navigate(routeHref({ name: "mail", all: true, mbIndex: 0, view: "inbox", threadId: null, q: "" })), { keys: "g a", when: (x) => (x.me?.mailboxes.length ?? 0) > 1 }),91    cmd("go.flagged", "go", t("help.goFlagged"), "star", () => goView("flagged"), { keys: "g s" }),92    cmd("go.drafts", "go", t("help.goDrafts"), "draft", () => goView("drafts"), { keys: "g d" }),93    cmd("go.sent", "go", t("help.goSent"), "sent", () => goView("sent"), { keys: "g t" }),94    cmd("go.unread", "go", `${t("palette.goTo")} ${t("view.unread")}`, "unread", () => goView("unread")),95    cmd("go.pinned", "go", `${t("palette.goTo")} ${t("view.pinned")}`, "pin", () => goView("pinned")),96    cmd("go.archive", "go", `${t("palette.goTo")} ${t("folder.archive")}`, "archive", () => goView("archive")),97    cmd("go.junk", "go", `${t("palette.goTo")} ${t("folder.junk")}`, "junk", () => goView("junk")),98    cmd("go.trash", "go", `${t("palette.goTo")} ${t("folder.trash")}`, "trash", () => goView("trash")),99    cmd("go.templates", "go", t("nav.templates"), "template", () => navigate("/templates")),100    cmd("go.admin", "go", t("nav.admin"), "shield", () => navigate("/admin/overview"), { when: (x) => x.me?.user.role === "admin" }),101    // ---- affichage102    theme("system", t("settings.appearance.themeSystem")),103    theme("light", t("settings.appearance.themeLight")),104    theme("dark", t("settings.appearance.themeDark")),105    theme("midnight", t("settings.appearance.themeMidnight")),106    density("comfortable", t("settings.appearance.densityComfortable")),107    density("cozy", t("settings.appearance.densityCozy")),108    density("compact", t("settings.appearance.densityCompact")),109    layout("right", t("settings.appearance.layoutRight"), "layoutRight"),110    layout("below", t("settings.appearance.layoutBelow"), "layoutBelow"),111    layout("none", t("settings.appearance.layoutNone"), "layoutNone"),112    preview(0, t("settings.appearance.preview0")),113    preview(1, t("settings.appearance.preview1")),114    preview(2, t("settings.appearance.preview2")),115    cmd("sidebar.toggle", "view", s.device.sidebarCollapsed ? t("nav.expand") : t("nav.collapse"), "panel", toggleSidebar, { keys: "[", when: inMail }),116    cmd("avatars.toggle", "view", s.prefs.avatars ? t("palette.cmd.avatarsOff") : t("palette.cmd.avatarsOn"), "user", () => updatePrefs({ avatars: !S.get().prefs.avatars })),117    cmd("darkMessages.toggle", "view", s.prefs.darkMessages ? t("palette.cmd.darkReadingOff") : t("palette.cmd.darkReadingOn"), "contrast", () => updatePrefs({ darkMessages: !S.get().prefs.darkMessages })),118    // ---- réglages119    settings("profile", t("settings.tab.profile"), "user"),120    settings("identities", t("settings.tab.identities"), "edit"),121    settings("vacation", t("settings.tab.vacation"), "clock"),122    settings("filters", t("settings.tab.filters"), "filter"),123    settings("appearance", t("settings.tab.appearance"), "eye"),124    settings("privacy", t("settings.tab.privacy"), "shield"),125    settings("security", t("settings.tab.security"), "key"),126    settings("export", t("settings.tab.export"), "download"),127    settings("storage", t("settings.tab.storage"), "server"),128    cmd("signout", "settings", t("nav.signOut"), "signout", () => void signOut()),129  ]130  return [...list, ...extra.map((make) => make())].filter((c) => !c.when || c.when(s))131}132
Files main
Menu

Theme