mirror of
https://github.com/outline/outline.git
synced 2026-12-04 18:34:54 +03:00
571 lines
15 KiB
TypeScript
571 lines
15 KiB
TypeScript
import { clamp } from "es-toolkit";
|
|
import { t } from "i18next";
|
|
import { action, computed, makeObservable, observable } from "mobx";
|
|
import { flushSync } from "react-dom";
|
|
import { toast } from "sonner";
|
|
import { light as defaultTheme } from "@shared/styles/theme";
|
|
import type { ProsemirrorData } from "@shared/types";
|
|
import Storage from "@shared/utils/Storage";
|
|
import Document from "~/models/Document";
|
|
import type Model from "~/models/base/Model";
|
|
import Collection from "~/models/Collection";
|
|
import type { SplitViewPane } from "~/utils/splitView";
|
|
import { isTruthyQueryValue } from "~/utils/urls";
|
|
import { startViewTransition } from "~/utils/viewTransition";
|
|
import type RootStore from "./RootStore";
|
|
|
|
const UI_STORE = "UI_STORE";
|
|
// Used by the static page before the UI store is available.
|
|
const THEME_STORAGE_KEY = "theme";
|
|
|
|
export enum Theme {
|
|
Light = "light",
|
|
Dark = "dark",
|
|
System = "system",
|
|
}
|
|
|
|
export enum SystemTheme {
|
|
Light = "light",
|
|
Dark = "dark",
|
|
}
|
|
|
|
export type ResolvedTheme = "light" | "dark" | "system";
|
|
|
|
/** The panels that can be displayed in the right sidebar. */
|
|
export type RightSidebarPanel = "comments" | "history";
|
|
|
|
type PersistedData = Pick<
|
|
UiStore,
|
|
| "languagePromptDismissed"
|
|
| "rightSidebar"
|
|
| "theme"
|
|
| "sidebarWidth"
|
|
| "sidebarRightWidth"
|
|
| "sidebarCollapsed"
|
|
| "tocVisible"
|
|
>;
|
|
|
|
class UiStore {
|
|
// has the user seen the prompt to change the UI language and actioned it
|
|
@observable
|
|
languagePromptDismissed: boolean | undefined = undefined;
|
|
|
|
// theme represents the users UI preference (defaults to system)
|
|
@observable
|
|
theme: Theme = Theme.System;
|
|
|
|
// themeOverride is set when a theme query parameter is detected, persists for the session
|
|
@observable
|
|
themeOverride: Theme | undefined = undefined;
|
|
|
|
// systemTheme represents the system UI theme (Settings -> General in macOS)
|
|
@observable
|
|
systemTheme: SystemTheme = SystemTheme.Light;
|
|
|
|
@observable
|
|
activeModels = observable.map<string, Model>();
|
|
|
|
@observable
|
|
observingUserId: string | undefined = undefined;
|
|
|
|
@observable
|
|
progressBarVisible = false;
|
|
|
|
@observable
|
|
tocVisible: boolean | undefined = undefined;
|
|
|
|
@observable
|
|
mobileSidebarVisible = false;
|
|
|
|
@observable
|
|
sidebarWidth: number = defaultTheme.sidebarWidth;
|
|
|
|
@observable
|
|
sidebarRightWidth: number = defaultTheme.sidebarRightWidth;
|
|
|
|
@observable
|
|
sidebarCollapsed = false;
|
|
|
|
// Whether the sidebar is hidden entirely, e.g. when embedding a document via
|
|
// the ?sidebarHidden=1 query parameter. Not persisted across reloads.
|
|
@observable
|
|
sidebarHidden = isTruthyQueryValue(
|
|
new URLSearchParams(window.location.search).get("sidebarHidden")
|
|
);
|
|
|
|
@observable
|
|
rightSidebar: RightSidebarPanel | null = null;
|
|
|
|
// The right sidebar panel displayed in the secondary split view pane. Not
|
|
// persisted as the pane itself only exists for the current session.
|
|
@observable
|
|
secondaryRightSidebar: RightSidebarPanel | null = null;
|
|
|
|
// The fraction of the split view's width occupied by the primary pane. Not
|
|
// persisted, reset when the split view closes.
|
|
@observable
|
|
splitViewRatio = 0.5;
|
|
|
|
@observable
|
|
sidebarIsResizing = false;
|
|
|
|
@observable
|
|
debugSafeArea = false;
|
|
|
|
/** Data for the currently active presentation, if any. */
|
|
@observable
|
|
presentationData: {
|
|
title: string;
|
|
icon?: string | null;
|
|
color?: string | null;
|
|
data: ProsemirrorData;
|
|
} | null = null;
|
|
|
|
/**
|
|
* Enter presentation mode for the given document.
|
|
*
|
|
* @param document the document to present, or null to exit.
|
|
*/
|
|
@action
|
|
setPresentingDocument = (document: Document | null): void => {
|
|
this.presentationData = document
|
|
? {
|
|
title: document.displayTitle,
|
|
icon: document.icon,
|
|
color: document.color,
|
|
data: document.data,
|
|
}
|
|
: null;
|
|
};
|
|
|
|
/** Tracks active export toasts for in-place updates when export completes */
|
|
exportToasts = observable.map<
|
|
string,
|
|
{ toastId: string; timeoutId: ReturnType<typeof setTimeout> }
|
|
>();
|
|
|
|
rootStore: RootStore;
|
|
|
|
constructor(rootStore: RootStore) {
|
|
this.rootStore = rootStore;
|
|
|
|
// Rehydrate
|
|
const data: PersistedData = Storage.get(UI_STORE) || {};
|
|
this.languagePromptDismissed = data.languagePromptDismissed;
|
|
this.sidebarCollapsed = !!data.sidebarCollapsed;
|
|
// Widths are clamped as a drag may have been interrupted while stretched beyond the bounds,
|
|
// or the bounds themselves may have since changed.
|
|
const { sidebarResizeMinWidth: minWidth, sidebarMaxWidth: maxWidth } =
|
|
defaultTheme;
|
|
this.sidebarWidth = clamp(
|
|
data.sidebarWidth || defaultTheme.sidebarWidth,
|
|
minWidth,
|
|
maxWidth
|
|
);
|
|
this.sidebarRightWidth = clamp(
|
|
data.sidebarRightWidth || defaultTheme.sidebarRightWidth,
|
|
minWidth,
|
|
maxWidth
|
|
);
|
|
this.tocVisible = data.tocVisible;
|
|
this.rightSidebar = data.rightSidebar ?? null;
|
|
this.theme = data.theme || Theme.System;
|
|
Storage.set(THEME_STORAGE_KEY, this.theme);
|
|
|
|
// system theme listeners
|
|
if (window.matchMedia) {
|
|
const colorSchemeQueryList = window.matchMedia(
|
|
"(prefers-color-scheme: dark)"
|
|
);
|
|
|
|
const setSystemTheme = (event: MediaQueryListEvent | MediaQueryList) => {
|
|
this.systemTheme = event.matches ? SystemTheme.Dark : SystemTheme.Light;
|
|
};
|
|
|
|
setSystemTheme(colorSchemeQueryList);
|
|
|
|
if (typeof colorSchemeQueryList.addEventListener === "function") {
|
|
colorSchemeQueryList.addEventListener("change", setSystemTheme);
|
|
} else if (typeof colorSchemeQueryList.addListener === "function") {
|
|
colorSchemeQueryList.addListener(setSystemTheme);
|
|
}
|
|
}
|
|
|
|
window.addEventListener("storage", (event) => {
|
|
if (event.key === UI_STORE && event.newValue) {
|
|
let newData: PersistedData | null;
|
|
try {
|
|
newData = JSON.parse(event.newValue);
|
|
} catch {
|
|
return;
|
|
}
|
|
|
|
// data may be null if key is deleted in localStorage
|
|
if (!newData) {
|
|
return;
|
|
}
|
|
|
|
// Note: we do not sync all properties here, sidebar widths and TOC
|
|
// visibility cause fighting between windows
|
|
this.theme = newData.theme;
|
|
this.languagePromptDismissed = newData.languagePromptDismissed;
|
|
this.sidebarCollapsed = !!newData.sidebarCollapsed;
|
|
}
|
|
});
|
|
|
|
makeObservable(this);
|
|
}
|
|
|
|
/**
|
|
* Add a model instance to the active set.
|
|
*
|
|
* @param model the model instance to add.
|
|
*/
|
|
@action
|
|
addActiveModel = (model: Model): void => {
|
|
this.activeModels.set(model.id, model);
|
|
};
|
|
|
|
/**
|
|
* Remove a model instance from the active set.
|
|
*
|
|
* @param model the model instance to remove.
|
|
*/
|
|
@action
|
|
removeActiveModel = (model: Model): void => {
|
|
this.activeModels.delete(model.id);
|
|
};
|
|
|
|
/**
|
|
* Get all active models of a specific type.
|
|
*
|
|
* @param modelClass the model class to filter by.
|
|
* @returns array of active models of the specified type.
|
|
*/
|
|
getActiveModels<T extends Model>(
|
|
modelClass: new (...args: never[]) => T
|
|
): T[] {
|
|
return Array.from(this.activeModels.values()).filter(
|
|
(model) => model.constructor === modelClass
|
|
) as T[];
|
|
}
|
|
|
|
/**
|
|
* Check if a model instance is in the active set.
|
|
*
|
|
* @param model the model instance to check.
|
|
* @returns true if the model is active.
|
|
*/
|
|
isModelActive(model: Model): boolean {
|
|
return this.activeModels.has(model.id);
|
|
}
|
|
|
|
/**
|
|
* Clear all active models, or only models of a specific type.
|
|
*
|
|
* @param modelClass optional model class to filter by.
|
|
*/
|
|
@action
|
|
clearActiveModels(modelClass?: new (...args: never[]) => Model): void {
|
|
if (modelClass) {
|
|
const modelsToRemove = this.getActiveModels(modelClass);
|
|
modelsToRemove.forEach((model) => this.activeModels.delete(model.id));
|
|
} else {
|
|
this.activeModels.clear();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the most recently added model of a specific type (primary).
|
|
*
|
|
* @param modelClass the model class to filter by.
|
|
* @returns the most recently added model of the specified type.
|
|
*/
|
|
getPrimaryActiveModel<T extends Model>(
|
|
modelClass: new (...args: never[]) => T
|
|
): T | undefined {
|
|
const models = this.getActiveModels<T>(modelClass);
|
|
return models[models.length - 1];
|
|
}
|
|
|
|
@computed
|
|
get activeDocumentId(): string | undefined {
|
|
return this.getPrimaryActiveModel<Document>(Document)?.id;
|
|
}
|
|
|
|
@computed
|
|
get activeCollectionId(): string | undefined {
|
|
// Derive from the active document so it resolves even if the collection
|
|
// loads after the document became active.
|
|
const activeDocument = this.getPrimaryActiveModel<Document>(Document);
|
|
if (activeDocument?.isActive && activeDocument.collectionId) {
|
|
return activeDocument.collectionId;
|
|
}
|
|
return this.getPrimaryActiveModel<Collection>(Collection)?.id;
|
|
}
|
|
|
|
@action
|
|
setTheme = (theme: Theme) => {
|
|
startViewTransition(() => {
|
|
flushSync(() => {
|
|
this.theme = theme;
|
|
this.persist();
|
|
Storage.set(THEME_STORAGE_KEY, this.theme);
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Set a theme override from a query parameter. This persists for the session
|
|
* but is not saved to localStorage.
|
|
*
|
|
* @param theme The theme to override with, or undefined to clear.
|
|
*/
|
|
@action
|
|
setThemeOverride = (theme: Theme | undefined) => {
|
|
this.themeOverride = theme;
|
|
};
|
|
|
|
@action
|
|
setActiveDocument = (document: Document | string): void => {
|
|
let model: Document | undefined;
|
|
|
|
if (typeof document === "string") {
|
|
model = this.rootStore.documents.get(document);
|
|
} else {
|
|
model = document;
|
|
}
|
|
|
|
if (!model) {
|
|
return;
|
|
}
|
|
|
|
this.clearActiveModels(Document);
|
|
this.addActiveModel(model);
|
|
this.observingUserId = undefined;
|
|
|
|
if (model.isActive && model.collectionId) {
|
|
const collection = this.rootStore.collections.get(model.collectionId);
|
|
if (collection) {
|
|
this.clearActiveModels(Collection);
|
|
this.addActiveModel(collection);
|
|
}
|
|
}
|
|
};
|
|
|
|
@action
|
|
setSidebarResizing = (sidebarIsResizing: boolean): void => {
|
|
this.sidebarIsResizing = sidebarIsResizing;
|
|
};
|
|
|
|
/**
|
|
* Sets the fraction of the split view's width occupied by the primary pane,
|
|
* clamped so that neither pane becomes unusably narrow.
|
|
*
|
|
* @param ratio the fraction of the split view's width for the primary pane.
|
|
*/
|
|
@action
|
|
setSplitViewRatio = (ratio: number): void => {
|
|
this.splitViewRatio = Math.min(0.8, Math.max(0.2, ratio));
|
|
};
|
|
|
|
/**
|
|
* Returns the right sidebar panel displayed in the given split view pane.
|
|
*
|
|
* @param pane the split view pane, defaults to the primary pane.
|
|
* @returns the panel displayed in the pane, or null when closed.
|
|
*/
|
|
getRightSidebar = (
|
|
pane: SplitViewPane = "primary"
|
|
): RightSidebarPanel | null =>
|
|
pane === "secondary" ? this.secondaryRightSidebar : this.rightSidebar;
|
|
|
|
/**
|
|
* Sets the right sidebar panel displayed in the given split view pane.
|
|
*
|
|
* @param panel the panel to display, or null to close the sidebar.
|
|
* @param pane the split view pane, defaults to the primary pane.
|
|
*/
|
|
@action
|
|
setRightSidebar = (
|
|
panel: RightSidebarPanel | null,
|
|
pane: SplitViewPane = "primary"
|
|
): void => {
|
|
if (pane === "secondary") {
|
|
this.secondaryRightSidebar = panel;
|
|
} else {
|
|
this.rightSidebar = panel;
|
|
this.persist();
|
|
}
|
|
};
|
|
|
|
@action
|
|
setActiveCollection = (collectionId: string | undefined): void => {
|
|
if (collectionId === undefined || collectionId === null) {
|
|
this.clearActiveModels(Collection);
|
|
return;
|
|
}
|
|
|
|
const model = this.rootStore.collections.get(collectionId);
|
|
if (model) {
|
|
this.clearActiveModels(Collection);
|
|
this.addActiveModel(model);
|
|
}
|
|
};
|
|
|
|
@action
|
|
setObservingUser = (userId: string | undefined): void => {
|
|
this.observingUserId = userId;
|
|
};
|
|
|
|
@action
|
|
clearActiveDocument = (): void => {
|
|
this.clearActiveModels(Document);
|
|
this.observingUserId = undefined;
|
|
|
|
// Unset when navigating away from a document (e.g. to another document, home, settings, etc.)
|
|
// Next document's onMount will set the right activeCollectionId.
|
|
this.clearActiveModels(Collection);
|
|
};
|
|
|
|
@action
|
|
collapseSidebar = () => {
|
|
this.set({ sidebarCollapsed: true });
|
|
};
|
|
|
|
@action
|
|
expandSidebar = () => {
|
|
this.sidebarHidden = false;
|
|
this.set({ sidebarCollapsed: false });
|
|
};
|
|
|
|
@action
|
|
set = (data: Partial<PersistedData>) => {
|
|
for (const key in data) {
|
|
// @ts-expect-error doesn't understand PersistedData is subset of keys
|
|
this[key] = data[key];
|
|
}
|
|
this.persist();
|
|
};
|
|
|
|
@action
|
|
toggleCollapsedSidebar = () => {
|
|
this.sidebarHidden = false;
|
|
this.set({ sidebarCollapsed: !this.sidebarCollapsed });
|
|
};
|
|
|
|
@action
|
|
enableProgressBar = () => {
|
|
this.progressBarVisible = true;
|
|
};
|
|
|
|
@action
|
|
disableProgressBar = () => {
|
|
this.progressBarVisible = false;
|
|
};
|
|
|
|
@action
|
|
toggleMobileSidebar = () => {
|
|
this.mobileSidebarVisible = !this.mobileSidebarVisible;
|
|
};
|
|
|
|
@action
|
|
hideMobileSidebar = () => {
|
|
this.mobileSidebarVisible = false;
|
|
};
|
|
|
|
@action
|
|
toggleDebugSafeArea = () => {
|
|
this.debugSafeArea = !this.debugSafeArea;
|
|
};
|
|
|
|
/**
|
|
* Display a toast for an export that is being prepared in the background,
|
|
* it is updated in place once the export completes or fails.
|
|
*
|
|
* @param fileOperationId The identifier of the export file operation.
|
|
*/
|
|
@action
|
|
showExportToast = (fileOperationId: string) => {
|
|
const toastId = `export-${fileOperationId}`;
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
toast.success(t("Export started"), {
|
|
id: toastId,
|
|
description: t("A link to your file will be sent through email soon"),
|
|
duration: 3000,
|
|
});
|
|
this.exportToasts.delete(fileOperationId);
|
|
}, 6000);
|
|
|
|
this.exportToasts.set(fileOperationId, { toastId, timeoutId });
|
|
|
|
toast.loading(t("Export started"), {
|
|
id: toastId,
|
|
description: `${t("Preparing your download")}…`,
|
|
duration: Infinity,
|
|
});
|
|
};
|
|
|
|
@action
|
|
removeExportToast = (fileOperationId: string) => {
|
|
const tracked = this.exportToasts.get(fileOperationId);
|
|
if (tracked) {
|
|
clearTimeout(tracked.timeoutId);
|
|
this.exportToasts.delete(fileOperationId);
|
|
}
|
|
};
|
|
|
|
@computed
|
|
get readyToShow() {
|
|
return (
|
|
!this.rootStore.auth.user ||
|
|
(this.rootStore.collections.isLoaded &&
|
|
this.rootStore.stars.isLoaded &&
|
|
this.rootStore.userMemberships.isLoaded)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns the current state of the sidebar taking into account user preference
|
|
* and whether the sidebar has been hidden as part of launching in a new
|
|
* desktop window.
|
|
*/
|
|
@computed
|
|
get sidebarIsClosed() {
|
|
return this.sidebarCollapsed || this.sidebarHidden;
|
|
}
|
|
|
|
@computed
|
|
get resolvedTheme(): Theme | SystemTheme {
|
|
if (this.themeOverride) {
|
|
return this.themeOverride;
|
|
}
|
|
|
|
if (this.theme === "system") {
|
|
return this.systemTheme;
|
|
}
|
|
|
|
return this.theme;
|
|
}
|
|
|
|
@computed
|
|
get asJson(): PersistedData {
|
|
return {
|
|
tocVisible: this.tocVisible,
|
|
sidebarCollapsed: this.sidebarCollapsed,
|
|
sidebarWidth: this.sidebarWidth,
|
|
sidebarRightWidth: this.sidebarRightWidth,
|
|
languagePromptDismissed: this.languagePromptDismissed,
|
|
rightSidebar: this.rightSidebar,
|
|
theme: this.theme,
|
|
};
|
|
}
|
|
|
|
private persist = () => {
|
|
Storage.set(UI_STORE, this.asJson);
|
|
};
|
|
}
|
|
|
|
export default UiStore;
|