Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(presentation): keep the presentation's toolbar in one module
Elian Doran5c736c3b2d03
diff --git a/apps/client/src/widgets/collections/presentation/index.tsx b/apps/client/src/widgets/collections/presentation/index.tsxindex 09198067f1..bf4c5163c5 100644--- a/apps/client/src/widgets/collections/presentation/index.tsx+++ b/apps/client/src/widgets/collections/presentation/index.tsx@@ -7,14 +7,11 @@ import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";import type { RevealApi } from "reveal.js";import slideBaseStylesheet from "reveal.js/reveal.css?raw";-import { openInCurrentNoteContext } from "../../../components/note_context";import FNote from "../../../entities/fnote";import froca from "../../../services/froca";-import { t } from "../../../services/i18n";import katexStylesheet from "../../../services/katex.scss?inline";import options from "../../../services/options";import CollectionProperties from "../../note_bars/CollectionProperties";-import ActionButton from "../../react/ActionButton";import {useEffectiveReadOnly, useNoteContext, useNoteLabelWithDefault, useTriliumEvent, useTriliumOptionBool} from "../../react/hooks";@@ -24,15 +21,15 @@ import { useClosingSlides } from "./closing_slides";import {applySlideMove, buildPresentationModel, createSlide, deleteSlide, duplicateSlide, getNewSlidePosition,getNewVerticalSlidePosition, getSlideMove, getSlideParentNoteId, isSlideRebuilt, moveSlideNote, NewSlidePosition,- PresentationModel, PresentationSlideBaseModel, SlideMove+ PresentationModel, SlideMove} from "./model";import { getNoteIdFromSlide, goToSlide, useActiveSlideNoteId } from "./reveal";import SlideSidebar from "./sidebar";-import SlideBackgroundAction from "./slide_background";import slideCustomStylesheet from "./slidejs.css?raw";import { SlideMenuActions } from "./slide_menu";import { CollectionSlides, Presentation } from "./slides";import { DEFAULT_THEME, loadPresentationTheme } from "./themes";+import PresentationToolbarActions, { SlideSidebarToggle } from "./toolbar";const SlideEditor = lazy(() => import("./slide_editor"));@@ -198,13 +195,11 @@ export default function PresentationView({ note, noteIds, media, onReady, onProg<div class="presentation-view"><CollectionPropertiesnote={note}- leftChildren={<ActionButton- icon="bx bx-sidebar"- text={t("presentation_view.slide-sidebar")}- active={isSidebarVisible}- onClick={() => void setSidebarVisible(!isSidebarVisible)}+ leftChildren={<SlideSidebarToggle+ isVisible={isSidebarVisible}+ onToggle={() => void setSidebarVisible(!isSidebarVisible)}/>}- rightChildren={<ButtonOverlay+ rightChildren={<PresentationToolbarActionscontainerRef={containerRef}api={api}isReadOnly={isReadOnly}@@ -308,83 +303,3 @@ function usePresentationStylesheets(note: FNote, media: ViewModeMedia) {return stylesheets;}-interface ButtonOverlayProps {- containerRef: RefObject<HTMLDivElement | null>;- api: RevealApi | undefined;- isReadOnly: boolean;- isEditing: boolean;- onFinishEditing(): void;- /** Adds a slide after the one being shown; left out when slides cannot be added. */- onNewSlide: (() => void) | undefined;- /** The slide being shown, for its background; left out when slides cannot be edited. */- backgroundSlide: { slide: PresentationSlideBaseModel | undefined } | undefined;-}--function ButtonOverlay({- containerRef, api, isReadOnly, isEditing, onFinishEditing, onNewSlide, backgroundSlide-}: ButtonOverlayProps) {- const [ isOverviewActive, setIsOverviewActive ] = useState(false);- useEffect(() => {- if (!api) return;- setIsOverviewActive(api.isOverview());- const onEnabled = () => setIsOverviewActive(true);- const onDisabled = () => setIsOverviewActive(false);- api.on("overviewshown", onEnabled);- api.on("overviewhidden", onDisabled);- return () => {- api.off("overviewshown", onEnabled);- api.off("overviewhidden", onDisabled);- };- }, [ api ]);-- return (- <>- {onNewSlide && (- <ActionButton- icon="bx bx-plus"- text={t("presentation_view.new-slide")}- onClick={onNewSlide}- />- )}-- {isEditing && (- <ActionButton- icon="bx bx-check"- text={t("presentation_view.finish-editing")}- onClick={onFinishEditing}- />- )}-- {!isReadOnly && (- <ActionButton- icon="bx bx-edit"- text={t("presentation_view.edit-slide")}- onClick={e => {- const currentSlide = api?.getCurrentSlide();- const noteId = getNoteIdFromSlide(currentSlide);-- if (noteId) {- openInCurrentNoteContext(e, noteId);- }- }}- />- )}-- {backgroundSlide && <SlideBackgroundAction slide={backgroundSlide.slide} />}-- <ActionButton- icon="bx bx-grid-horizontal"- text={t("presentation_view.slide-overview")}- active={isOverviewActive}- onClick={() => api?.toggleOverview()}- />-- <ActionButton- icon="bx bx-fullscreen"- text={t("presentation_view.start-presentation")}- onClick={() => containerRef.current?.requestFullscreen()}- />- </>- );-}-diff --git a/apps/client/src/widgets/collections/presentation/slide_background.tsx b/apps/client/src/widgets/collections/presentation/slide_background.tsxdeleted file mode 100644index d99b95471f..0000000000--- a/apps/client/src/widgets/collections/presentation/slide_background.tsx+++ /dev/null@@ -1,47 +0,0 @@-import FNote from "../../../entities/fnote";-import froca from "../../../services/froca";-import { t } from "../../../services/i18n";-import ColorPicker from "../../react/ColorPicker";-import { DropdownPanel } from "../../react/Dropdown";-import { useNoteLabel } from "../../react/hooks";-import type { PresentationSlideBaseModel } from "./model";--/**- * The toolbar's control for the background color of the slide being shown, a color picker in a- * dropdown. It is disabled for a slide that {@link canPickSlideBackground} rules out.- */-export default function SlideBackgroundAction({ slide }: { slide: PresentationSlideBaseModel | undefined }) {- const note = slide && canPickSlideBackground(slide) ? froca.getNoteFromCache(slide.noteId) : undefined;- return (- <DropdownPanel- buttonClassName="bx bx-palette"- title={t("presentation_view.slide-background")}- iconAction- hideToggleArrow- disabled={!note}- >- {note && <SlideBackgroundPicker note={note} />}- </DropdownPanel>- );-}--/** Reads and writes `#slide:background` of `note`. A gradient selects no color, and a pick replaces it. */-export function SlideBackgroundPicker({ note }: { note: FNote }) {- const [ background, setBackground ] = useNoteLabel(note, "slide:background");- const isGradient = !!background?.includes("gradient(");- return (- <ColorPicker- currentValue={background && !isGradient ? background : null}- indeterminate={isGradient}- onChange={(color) => void setBackground(color)}- />- );-}--/**- * Whether the slide can take a background: a web view or a collection covers the whole slide, so- * the background never shows, and a read-only slide cannot be changed.- */-export function canPickSlideBackground(slide: PresentationSlideBaseModel | undefined) {- return !!slide && !slide.webViewSrc && !slide.isCollection && !slide.isReadOnly;-}diff --git a/apps/client/src/widgets/collections/presentation/slide_background.spec.tsx b/apps/client/src/widgets/collections/presentation/toolbar.spec.tsxsimilarity index 99%rename from apps/client/src/widgets/collections/presentation/slide_background.spec.tsxrename to apps/client/src/widgets/collections/presentation/toolbar.spec.tsxindex 48c7d6f9b2..5a435ed4eb 100644--- a/apps/client/src/widgets/collections/presentation/slide_background.spec.tsx+++ b/apps/client/src/widgets/collections/presentation/toolbar.spec.tsx@@ -14,7 +14,7 @@ import { buildNote } from "../../../test/easy-froca";import { renderInto } from "../../../test/render";import { DEFAULT_COLOR_PALETTE } from "../../react/ColorPicker";import type { PresentationSlideBaseModel } from "./model";-import { canPickSlideBackground, SlideBackgroundPicker } from "./slide_background";+import { canPickSlideBackground, SlideBackgroundPicker } from "./toolbar";describe("SlideBackgroundPicker", () => {it("shows the slide's color and writes a pick or a reset to #slide:background", () => {diff --git a/apps/client/src/widgets/collections/presentation/toolbar.tsx b/apps/client/src/widgets/collections/presentation/toolbar.tsxnew file mode 100644index 0000000000..b2feabc5d8--- /dev/null+++ b/apps/client/src/widgets/collections/presentation/toolbar.tsx@@ -0,0 +1,147 @@+import { RefObject } from "preact";+import { useEffect, useState } from "preact/hooks";+import type { RevealApi } from "reveal.js";++import { openInCurrentNoteContext } from "../../../components/note_context";+import FNote from "../../../entities/fnote";+import froca from "../../../services/froca";+import { t } from "../../../services/i18n";+import ActionButton from "../../react/ActionButton";+import ColorPicker from "../../react/ColorPicker";+import { DropdownPanel } from "../../react/Dropdown";+import { useNoteLabel } from "../../react/hooks";+import type { PresentationSlideBaseModel } from "./model";+import { getNoteIdFromSlide } from "./reveal";++interface PresentationToolbarActionsProps {+ containerRef: RefObject<HTMLDivElement | null>;+ api: RevealApi | undefined;+ isReadOnly: boolean;+ isEditing: boolean;+ onFinishEditing(): void;+ /** Adds a slide after the one being shown; left out when slides cannot be added. */+ onNewSlide: (() => void) | undefined;+ /** The slide being shown, for its background; left out when slides cannot be edited. */+ backgroundSlide: { slide: PresentationSlideBaseModel | undefined } | undefined;+}++/** The presentation's actions on the right of the collection's toolbar. */+export default function PresentationToolbarActions({+ containerRef, api, isReadOnly, isEditing, onFinishEditing, onNewSlide, backgroundSlide+}: PresentationToolbarActionsProps) {+ const [ isOverviewActive, setIsOverviewActive ] = useState(false);+ useEffect(() => {+ if (!api) return;+ setIsOverviewActive(api.isOverview());+ const onEnabled = () => setIsOverviewActive(true);+ const onDisabled = () => setIsOverviewActive(false);+ api.on("overviewshown", onEnabled);+ api.on("overviewhidden", onDisabled);+ return () => {+ api.off("overviewshown", onEnabled);+ api.off("overviewhidden", onDisabled);+ };+ }, [ api ]);++ return (+ <>+ {onNewSlide && (+ <ActionButton+ icon="bx bx-plus"+ text={t("presentation_view.new-slide")}+ onClick={onNewSlide}+ />+ )}++ {isEditing && (+ <ActionButton+ icon="bx bx-check"+ text={t("presentation_view.finish-editing")}+ onClick={onFinishEditing}+ />+ )}++ {!isReadOnly && (+ <ActionButton+ icon="bx bx-edit"+ text={t("presentation_view.edit-slide")}+ onClick={e => {+ const currentSlide = api?.getCurrentSlide();+ const noteId = getNoteIdFromSlide(currentSlide);++ if (noteId) {+ openInCurrentNoteContext(e, noteId);+ }+ }}+ />+ )}++ {backgroundSlide && <SlideBackgroundAction slide={backgroundSlide.slide} />}++ <ActionButton+ icon="bx bx-grid-horizontal"+ text={t("presentation_view.slide-overview")}+ active={isOverviewActive}+ onClick={() => api?.toggleOverview()}+ />++ <ActionButton+ icon="bx bx-fullscreen"+ text={t("presentation_view.start-presentation")}+ onClick={() => containerRef.current?.requestFullscreen()}+ />+ </>+ );+}++/** The toggle of the slide sidebar, on the left of the collection's toolbar. */+export function SlideSidebarToggle({ isVisible, onToggle }: { isVisible: boolean; onToggle(): void }) {+ return (+ <ActionButton+ icon="bx bx-sidebar"+ text={t("presentation_view.slide-sidebar")}+ active={isVisible}+ onClick={onToggle}+ />+ );+}++/**+ * The control for the background color of the slide being shown, a color picker in a dropdown. It is+ * disabled for a slide that {@link canPickSlideBackground} rules out.+ */+function SlideBackgroundAction({ slide }: { slide: PresentationSlideBaseModel | undefined }) {+ const note = slide && canPickSlideBackground(slide) ? froca.getNoteFromCache(slide.noteId) : undefined;+ return (+ <DropdownPanel+ buttonClassName="bx bx-palette"+ title={t("presentation_view.slide-background")}+ iconAction+ hideToggleArrow+ disabled={!note}+ >+ {note && <SlideBackgroundPicker note={note} />}+ </DropdownPanel>+ );+}++/** Reads and writes `#slide:background` of `note`. A gradient selects no color, and a pick replaces it. */+export function SlideBackgroundPicker({ note }: { note: FNote }) {+ const [ background, setBackground ] = useNoteLabel(note, "slide:background");+ const isGradient = !!background?.includes("gradient(");+ return (+ <ColorPicker+ currentValue={background && !isGradient ? background : null}+ indeterminate={isGradient}+ onChange={(color) => void setBackground(color)}+ />+ );+}++/**+ * Whether the slide can take a background: a web view or a collection covers the whole slide, so+ * the background never shows, and a read-only slide cannot be changed.+ */+export function canPickSlideBackground(slide: PresentationSlideBaseModel | undefined) {+ return !!slide && !slide.webViewSrc && !slide.isCollection && !slide.isReadOnly;+}