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
client/options/media: upgrade to the new card style
Adorian Doranebc9103f5a28
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 54c7ce081c..e9ae5ded8d 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1877,7 +1877,6 @@"images_section_title": "Images","download_images_automatically": "Download images automatically","download_images_description": "Download referenced online images from pasted HTML so they are available offline.",- "enable_image_compression": "Image compression","automatic_image_compression": "Automatically compress images","enable_image_compression_description": "Compress and resize images when they are uploaded or pasted.","max_image_dimensions": "Max image dimensions",diff --git a/apps/client/src/widgets/type_widgets/options/media.css b/apps/client/src/widgets/type_widgets/options/media.cssindex 2c0041debf..c9b84a4414 100644--- a/apps/client/src/widgets/type_widgets/options/media.css+++ b/apps/client/src/widgets/type_widgets/options/media.css@@ -1,11 +1,18 @@-/* A real card where the rest of the page is options sections. It keeps the card styling the theme- already gives every other one — the wrapper is here for the page's own spacing, so the card sits- where a section would rather than flush against the one above it. */-.media-image-compression {- /* The choice buttons and their labels sit on one line in a dialog sized to its content; a- settings page is only as wide as its pane, and a narrow one has to be allowed to stack them- rather than push the card wider than the page. */- .image-compression-section {- flex-wrap: wrap;- }+/* The choice buttons and their labels sit on one line in a dialog sized to its content; a settings+ page is only as wide as its pane, and a narrow one has to be allowed to stack them rather than+ push the card wider than the page. */+.media-images .image-compression-section {+ flex-wrap: wrap;+}++/* The width the compression sliders were given, so the two cards read as one page. */+.media-ocr .slider {+ width: 140px;+}++/* Wide enough for the sentence written across it, and fixed: the label beside it would otherwise+ take the row's slack and leave the bar too narrow to read its own progress in. */+.media-batch-ocr .media-batch-ocr-progress {+ flex: 0 0 200px;+ height: 24px;}diff --git a/apps/client/src/widgets/type_widgets/options/media.spec.tsx b/apps/client/src/widgets/type_widgets/options/media.spec.tsxindex 881ac8c78f..99c637a801 100644--- a/apps/client/src/widgets/type_widgets/options/media.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/media.spec.tsx@@ -3,8 +3,9 @@ import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";/**- * The image compression card: the tool's own rows, driving the options that govern every image- * arriving in the database rather than a run the user is watching.+ * The images card: everything that happens to an image on its way in, compression included — the+ * tool's own rows, driving the options that govern every image arriving in the database rather than+ * a run the user is watching.** What is worth holding here is the wiring, since the rows themselves are tested where they live —* that each row reads and writes the option it stands for, that the group hangs off the switch@@ -73,13 +74,46 @@ function open(overrides: Record<string, string> = {}) {/** The card's rows, in the order they are drawn, named by their titles. */function rowTitles(): (string | undefined)[] {- return [ ...host.querySelectorAll(".media-image-compression .image-compression-section") ]- .map((row) => row.querySelector(".image-compression-section-title")?.textContent ?? undefined);+ return rows().map(titleOf);+}++function rows(): HTMLElement[] {+ return [ ...host.querySelectorAll<HTMLElement>(".media-images .tn-card-section") ];}function row(title: string): HTMLElement | undefined {- return [ ...host.querySelectorAll<HTMLElement>(".media-image-compression .image-compression-section") ]- .find((candidate) => candidate.querySelector(".image-compression-section-title")?.textContent === title);+ return rows().find((candidate) => titleOf(candidate) === title);+}++/**+ * What a row is called, whichever of the two kinds it is: the page's own option rows carry the+ * sentence inside the label, so the title is the text ahead of it, where the compression tool's+ * rows keep the title in an element of its own.+ */+function titleOf(row: Element): string | undefined {+ const label = row.querySelector(".tn-card-option-label");++ return label+ ? label.childNodes[0]?.textContent ?? undefined+ : row.querySelector(".image-compression-section-title")?.textContent ?? undefined;+}++/** The sentence beneath a row's title, from whichever of the two kinds of row it is. */+function describes(title: string): string | undefined {+ const row = rowOrFail(title);++ return row.querySelector(".tn-card-option-description")?.textContent+ ?? row.querySelector(".image-compression-section-description")?.textContent+ ?? undefined;+}++function rowOrFail(title: string): HTMLElement {+ const found = row(title);+ if (!found) {+ throw new Error(`No row titled "${title}".`);+ }++ return found;}/** Presses one of a row's choice buttons by its label. */@@ -102,15 +136,17 @@ afterEach(() => {vi.clearAllMocks();});-describe("the image compression card", () => {+describe("the images card", () => {it("hangs the whole group off its switch, nesting what qualifies each choice", () => {open();- // What an untouched install shows, in order: the switch, scaling with its bound, then one- // exclusive choice per format. Recompressing a JPEG brings a quality with it; optimizing a- // PNG does not, there being no quality to reducing it to a palette — so only one of the two- // choices carries a nested row here.+ // What an untouched install shows, in order: fetching a referenced image at all, then the+ // switch over what is done to the ones that arrive, scaling with its bound, and one+ // exclusive choice per format. Recompressing a JPEG brings a quality with it; optimizing+ // a PNG does not, there being no quality to reducing it to a palette — so only one of the+ // two choices carries a nested row here.expect(rowTitles()).toEqual([+ "images.download_images_automatically","images.automatic_image_compression","space_usage.compress_resize","space_usage.compress_max_dimensions",@@ -120,9 +156,13 @@ describe("the image compression card", () => {]);// Switched off, the settings are not merely greyed out but gone: there is nothing for them- // to govern, and a bound sitting there would read as one still in force.+ // to govern, and a bound sitting there would read as one still in force. What the switch+ // does not govern stays where it was — the card is not the compression alone.open({ compressImages: "false" });- expect(rowTitles()).toEqual([ "images.automatic_image_compression" ]);+ expect(rowTitles()).toEqual([+ "images.download_images_automatically",+ "images.automatic_image_compression"+ ]);});it("drops a quality that no longer qualifies anything", () => {@@ -131,6 +171,7 @@ describe("the image compression card", () => {// Neither format is being re-encoded, so neither quality is in force — and resizing is// still on offer, being the one step that reaches an image whatever its encoding.expect(rowTitles()).toEqual([+ "images.download_images_automatically","images.automatic_image_compression","space_usage.compress_resize","space_usage.compress_max_dimensions",@@ -146,9 +187,6 @@ describe("the image compression card", () => {it("carries the sentences the settings had before they were rows", () => {open();- const describes = (title: string) =>- row(title)?.querySelector(".image-compression-section-description")?.textContent;-expect(describes("images.automatic_image_compression")).toBe("images.enable_image_compression_description");expect(describes("space_usage.compress_resize")).toBe("images.max_image_dimensions_description");// Both qualities take the same advice, being the same scale read twice.diff --git a/apps/client/src/widgets/type_widgets/options/media.tsx b/apps/client/src/widgets/type_widgets/options/media.tsxindex 682bee83ab..d229b7ac27 100644--- a/apps/client/src/widgets/type_widgets/options/media.tsx+++ b/apps/client/src/widgets/type_widgets/options/media.tsx@@ -18,13 +18,13 @@ import {PngHandlingSection,ResizeImageSection} from "../../dialogs/image_compression/image_compression_sections";-import { Card, CardSection } from "../../react/Card";+import Button from "../../react/Button";+import { Card, CardOption } from "../../react/Card";import FormToggle from "../../react/FormToggle";+import HelpButton from "../../react/HelpButton";import { useTriliumOption, useTriliumOptionBool, useTriliumOptionInt } from "../../react/hooks";import Slider from "../../react/Slider";import OptionsPageHeader from "./components/OptionsPageHeader";-import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";-import OptionsSection from "./components/OptionsSection";import RelatedSettings from "./components/RelatedSettings";export default function MediaSettings() {@@ -32,40 +32,26 @@ export default function MediaSettings() {<><OptionsPageHeader /><ImageSettings />- <ImageCompressionSettings /><OcrSettings /></>);}-function ImageSettings() {- const [ downloadImagesAutomatically, setDownloadImagesAutomatically ] = useTriliumOptionBool("downloadImagesAutomatically");-- return (- <OptionsSection title={t("images.images_section_title")}>- <OptionsRowWithToggle- name="download-images-automatically"- label={t("images.download_images_automatically")}- description={t("images.download_images_description")}- currentValue={downloadImagesAutomatically}- onChange={setDownloadImagesAutomatically}- />- </OptionsSection>- );-}-/**- * What happens to an image on its way in, configured with the same rows the compression tool is- * built from — because behind them it is now the same compression. The one thing said here that the- * tool never has to say is *when* any of it happens: on upload, on paste, on import.+ * What happens to an image on its way in: whether one only referenced is fetched at all, and what+ * is done to the ones that arrive. Compression is part of the same card rather than a heading of+ * its own, being one more thing that happens to an arriving image rather than a subject beside it.*- * A real card rather than the options page's own section: these rows nest, a choice bringing its- * quality out from under it, and nesting is what a card section does. The descriptions are the ones- * the settings carried before they were rows — a settings page is read by someone deciding, where a- * dialog is read by someone who has already decided, so here the prose is on the page rather than- * behind the help marks the dialogs use.+ * The compression rows are the ones the compression tool is built from, because behind them it is+ * now the same compression. The one thing said here that the tool never has to say is *when* any of+ * it happens: on upload, on paste, on import. The descriptions are the ones the settings carried+ * before they were rows — a settings page is read by someone deciding, where a dialog is read by+ * someone who has already decided, so here the prose is on the page rather than behind the help+ * marks the dialogs use.*/-function ImageCompressionSettings() {+function ImageSettings() {+ const [ downloadImagesAutomatically, setDownloadImagesAutomatically ] =+ useTriliumOptionBool("downloadImagesAutomatically");const [ compressImages, setCompressImages ] = useTriliumOptionBool("compressImages");const [ options, update ] = useAutomaticCompressionOptions();const sectionProps: ImageCompressionSectionProps = {@@ -80,10 +66,26 @@ function ImageCompressionSettings() {};return (- <div className="options-section media-image-compression">- <Card heading={t("images.enable_image_compression")}>- <CardSection- className="image-compression-section"+ <div className="options-section media-images">+ <Card heading={t("images.images_section_title")}>+ <CardOption+ name="download-images-automatically"+ label={t("images.download_images_automatically")}+ description={t("images.download_images_description")}+ >+ <FormToggle+ currentValue={downloadImagesAutomatically}+ onChange={setDownloadImagesAutomatically}+ />+ </CardOption>++ {/* Not the tool's own "Compress images": that is something the user does to images+ already stored, where this is a standing instruction about every image still to+ arrive. Saying which is which is the whole of the label. */}+ <CardOption+ name="compress-images"+ label={t("images.automatic_image_compression")}+ description={t("images.enable_image_compression_description")}subSectionsVisible={compressImages}subSections={[<ResizeImageSection key="resize" {...sectionProps} />,@@ -91,19 +93,8 @@ function ImageCompressionSettings() {<PngHandlingSection key="png" {...sectionProps} />]}>- <span className="image-compression-section-label">- {/* Not the tool's own "Compress images": that is something the user does to- images already stored, where this is a standing instruction about every- image still to arrive. Saying which is which is the whole of the label. */}- <span className="image-compression-section-title">- {t("images.automatic_image_compression")}- </span>- <small className="image-compression-section-description">- {t("images.enable_image_compression_description")}- </small>- </span><FormToggle currentValue={compressImages} onChange={setCompressImages} />- </CardSection>+ </CardOption></Card></div>);@@ -152,28 +143,38 @@ function useAutomaticCompressionOptions(): [ ImageCompressionToolOptions, (patchfunction OcrSettings() {const [ ocrAutoProcess, setOcrAutoProcess ] = useTriliumOptionBool("ocrAutoProcessImages");const [ ocrMinConfidence, setOcrMinConfidence ] = useTriliumOption("ocrMinConfidence");+ const confidence = Math.round(parseFloat(ocrMinConfidence ?? "0.75") * 100);return (<>- <OptionsSection title={t("images.ocr_section_title")} helpUrl="TiQbQDgP8L5t">- <OptionsRowWithToggle- name="ocr-auto-process"- label={t("images.ocr_auto_process")}- description={t("images.ocr_auto_process_description")}- currentValue={ocrAutoProcess}- onChange={setOcrAutoProcess}- />+ <div className="options-section media-ocr">+ <Card+ heading={t("images.ocr_section_title")}+ actions={<HelpButton helpPage="TiQbQDgP8L5t" />}+ >+ <CardOption+ name="ocr-auto-process"+ label={t("images.ocr_auto_process")}+ description={t("images.ocr_auto_process_description")}+ >+ <FormToggle currentValue={ocrAutoProcess} onChange={setOcrAutoProcess} />+ </CardOption>- <OptionsRow name="ocr-min-confidence" label={`${t("images.ocr_min_confidence")} (${Math.round(parseFloat(ocrMinConfidence ?? "0.75") * 100)}%)`} description={t("images.ocr_confidence_description")}>- <Slider- min={0} max={100} step={5}- value={Math.round(parseFloat(ocrMinConfidence ?? "0.75") * 100)}- onChange={(v) => setOcrMinConfidence(String(v / 100))}- />- </OptionsRow>+ <CardOption+ name="ocr-min-confidence"+ label={`${t("images.ocr_min_confidence")} (${confidence}%)`}+ description={t("images.ocr_confidence_description")}+ >+ <Slider+ min={0} max={100} step={5}+ value={confidence}+ onChange={(value) => setOcrMinConfidence(String(value / 100))}+ />+ </CardOption>- <BatchProcessing />- </OptionsSection>+ <BatchProcessing />+ </Card>+ </div><RelatedSettings items={[{@@ -240,28 +241,30 @@ function BatchProcessing() {const isRunning = progress?.inProgress ?? false;return (- <OptionsRow name="batch-ocr" label={t("images.batch_ocr_title")} description={t("images.batch_ocr_description")}>+ <CardOption+ className="media-batch-ocr"+ label={t("images.batch_ocr_title")}+ description={t("images.batch_ocr_description")}+ >{isRunning ? (- <div style={{ width: "100%" }}>- <div className="progress" style={{ height: "24px" }}>- <div- className="progress-bar progress-bar-striped progress-bar-animated"- role="progressbar"- style={{ width: `${progress?.percentage ?? 0}%` }}- >- {t("images.batch_ocr_progress", { processed: progress?.processed ?? 0, total: progress?.total ?? 0 })}- </div>+ <div className="progress media-batch-ocr-progress">+ <div+ className="progress-bar progress-bar-striped progress-bar-animated"+ role="progressbar"+ style={{ width: `${progress?.percentage ?? 0}%` }}+ >+ {t("images.batch_ocr_progress", { processed: progress?.processed ?? 0, total: progress?.total ?? 0 })}</div></div>) : (- <button- type="button"- className="btn btn-secondary"+ <Button+ name="batch-ocr-start-button"+ text={t("images.batch_ocr_start")}+ icon="bx-play"+ size="micro"onClick={startBatch}- >- <span className="bx bx-play" />{" "}{t("images.batch_ocr_start")}- </button>+ />)}- </OptionsRow>+ </CardOption>);}