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/image compression tool: update the dialog defaults
Adorian Doran816e3442d8c0
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_dialog.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_dialog.spec.tsxindex fa869f5f3f..2eaff290c4 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_dialog.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_dialog.spec.tsx@@ -3,11 +3,7 @@ import { useEffect } from "preact/hooks";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-const { CONFIGURED_MAX_DIMENSIONS, CONFIGURED_QUALITY, EMPTY_RESULT } = vi.hoisted(() => ({- // Deliberately unlike the shipped defaults, so a field falling back to the image option cannot- // pass for one falling back to a constant of its own.- CONFIGURED_MAX_DIMENSIONS: 1600,- CONFIGURED_QUALITY: 60,+const { EMPTY_RESULT } = vi.hoisted(() => ({/** What a run reports when it found nothing; tests that care override it. */EMPTY_RESULT: {items: [], compressedCount: 0, skippedCount: 0, originalSize: 0, newSize: 0, savedSize: 0@@ -27,7 +23,6 @@ function resultOf(count: number, originalSize: number, newSize: number) {}const mocks = vi.hoisted(() => ({- getInt: vi.fn<(name: string) => number | null>(),storedOption: "{}",save: vi.fn(async () => {}),postWithTimeout: vi.fn<(...args: unknown[]) => Promise<unknown>>(async () => EMPTY_RESULT),@@ -39,7 +34,6 @@ const mocks = vi.hoisted(() => ({vi.mock("../../../../../services/options", () => ({default: {get: () => mocks.storedOption,- getInt: mocks.getInt,save: mocks.save}}));@@ -92,6 +86,11 @@ vi.mock("../../../../react/Modal", () => ({import { showImageCompressionDialog } from "./image_compression_dialog";import { IMAGE_COMPRESSION_TOAST_ID, type ImageCompressionTarget } from "./image_compression_operation";+import {+ DEFAULT_CONVERSION_QUALITY,+ DEFAULT_MAX_WIDTH_HEIGHT,+ DEFAULT_QUALITY+} from "./image_compression_options";/** A note holding whatever it holds; the collection case, where every setting is in play. */const NOTE_TARGET: ImageCompressionTarget = { type: "note", noteId: "n1" };@@ -199,11 +198,6 @@ beforeEach(() => {mocks.storedOption = "{}";vi.clearAllMocks();mocks.postWithTimeout.mockResolvedValue(EMPTY_RESULT);- mocks.getInt.mockImplementation((name) => (- name === "imageMaxWidthHeight" ? CONFIGURED_MAX_DIMENSIONS- : name === "imageJpegQuality" ? CONFIGURED_QUALITY- : null- ));});afterEach(() => {@@ -250,7 +244,7 @@ describe("showImageCompressionDialog", () => {expect(numberField()).toBeNull();await toggle(toggles()[0]);- expect(numberField()?.value).toBe(String(CONFIGURED_MAX_DIMENSIONS));+ expect(numberField()?.value).toBe(String(DEFAULT_MAX_WIDTH_HEIGHT));});it("shows each quality only while the choice it qualifies is the one taken", async () => {@@ -279,20 +273,20 @@ describe("showImageCompressionDialog", () => {"image-compression-section-value","slider"]);- expect(qualityReading()).toContain(String(CONFIGURED_QUALITY));+ expect(qualityReading()).toContain(String(DEFAULT_QUALITY));await drag(slider(), "45");expect(qualityReading()).toContain("45");});- it("opens on the image options when the setting has never been written", async () => {+ it("opens on its own defaults when the setting has never been written", async () => {await openDialog();- // Neither number has a default of its own: an unconfigured tool compresses the way- // automatic compression is already set to.- expect(numberField()?.value).toBe(String(CONFIGURED_MAX_DIMENSIONS));- expect(slider()?.value).toBe(String(CONFIGURED_QUALITY));+ // Defaults of the tool's own rather than the image options': those govern every image on+ // the way in, where this is a deliberate one-off on one that has already grown too heavy.+ expect(numberField()?.value).toBe(String(DEFAULT_MAX_WIDTH_HEIGHT));+ expect(slider()?.value).toBe(String(DEFAULT_QUALITY));// A JPEG is compressed and a PNG is made smaller without ceasing to be one — the least// surprising thing to do to each. Reaching into the subtree is not assumed: that widens@@ -331,20 +325,20 @@ describe("showImageCompressionDialog", () => {[ "out of range", { quality: 500 } ],[ "fractional", { quality: 62.5 } ],[ "of the wrong type", { quality: "high" } ]- ])("ignores a stored quality that is %s and falls back to the option", async (_label, stored) => {+ ])("ignores a stored quality that is %s and falls back to the default", async (_label, stored) => {mocks.storedOption = JSON.stringify(stored);await openDialog();- expect(slider()?.value).toBe(String(CONFIGURED_QUALITY));+ expect(slider()?.value).toBe(String(DEFAULT_QUALITY));});- it("ignores a nonsensical stored dimension and falls back to the option", async () => {+ it("ignores a nonsensical stored dimension and falls back to the default", async () => {mocks.storedOption = JSON.stringify({ maxWidthHeight: 0 });await openDialog();- expect(numberField()?.value).toBe(String(CONFIGURED_MAX_DIMENSIONS));+ expect(numberField()?.value).toBe(String(DEFAULT_MAX_WIDTH_HEIGHT));});it("writes every change straight back, so the next run opens where this one left off", async () => {@@ -363,7 +357,7 @@ describe("showImageCompressionDialog", () => {jpegHandling: "keep",pngHandling: "jpeg",quality: 45,- conversionQuality: 85,+ conversionQuality: DEFAULT_CONVERSION_QUALITY,processChildNotes: true}));});@@ -479,11 +473,11 @@ describe("running the compression", () => {expect(postedUrl()).toBe("notes/n1/compress-images");expect(postedBody()).toEqual({resize: true,- maxWidthHeight: CONFIGURED_MAX_DIMENSIONS,+ maxWidthHeight: DEFAULT_MAX_WIDTH_HEIGHT,jpegHandling: "compress",pngHandling: "optimize",- quality: CONFIGURED_QUALITY,- conversionQuality: 85,+ quality: DEFAULT_QUALITY,+ conversionQuality: DEFAULT_CONVERSION_QUALITY,recursive: true});});diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_options.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_options.tsindex 0220a0156e..05b8377ce1 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_options.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/image_compression_options.ts@@ -1,6 +1,5 @@import { IMAGE_JPEG_HANDLINGS, IMAGE_PNG_HANDLINGS, type ImageJpegHandling, type ImagePngHandling } from "@triliumnext/commons";-import optionService from "../../../../../services/options";import { compressibleFormatsOf, type ImageCompressionTarget } from "./image_compression_operation";/**@@ -46,20 +45,20 @@ export function hasWorkToDo(options: ImageCompressionToolOptions, target: ImageCexport const MIN_QUALITY = 10;export const MAX_QUALITY = 100;export const QUALITY_STEP = 5;-export const DEFAULT_QUALITY = 75;export const MIN_MAX_WIDTH_HEIGHT = 1;/*** Fills a stored setting out into the full set the dialog works with.*- * The dimension bound and the recompression quality fall back to the image options rather than to- * constants of their own, so a tool that has never been run opens on what automatic compression is- * already configured to do — the same fallbacks the server applies to a request that omits them.+ * The tool keeps defaults of its own rather than deriving them from the image options: those govern+ * what happens to every image on the way in, where this is a deliberate one-off on an image that has+ * already grown too heavy, and the two are not the same judgement. What is stored always wins, so+ * these are only ever what the very first run opens on.*- * PNGs start on optimizing, matching the server: it makes an image smaller without changing what it- * is, which is the least surprising thing to do by default. Reaching into the subtree does not start- * on: that widens what the run touches rather than how hard it compresses, and a descendant may be a- * clone shared with notes the user did not have in mind.+ * Resizing and compressing start on, PNGs on being made smaller without ceasing to be PNGs — the+ * least surprising thing to do to each. Reaching into the subtree does not: that widens what the run+ * touches rather than how hard it compresses, and a descendant may be a clone shared with notes the+ * user did not have in mind.*/export function readImageCompressionOptions(stored: Partial<ImageCompressionToolOptions> | null | undefined@@ -68,10 +67,10 @@ export function readImageCompressionOptions(resize: stored?.resize ?? true,maxWidthHeight: isPositiveInteger(stored?.maxWidthHeight)? Number(stored?.maxWidthHeight)- : defaultMaxWidthHeight(),+ : DEFAULT_MAX_WIDTH_HEIGHT,jpegHandling: readHandling(stored?.jpegHandling, IMAGE_JPEG_HANDLINGS, "compress"),pngHandling: readHandling(stored?.pngHandling, IMAGE_PNG_HANDLINGS, "optimize"),- quality: isQuality(stored?.quality) ? Number(stored?.quality) : defaultQuality(),+ quality: isQuality(stored?.quality) ? Number(stored?.quality) : DEFAULT_QUALITY,conversionQuality: isQuality(stored?.conversionQuality)? Number(stored?.conversionQuality): DEFAULT_CONVERSION_QUALITY,@@ -79,30 +78,16 @@ export function readImageCompressionOptions(};}-/** Where the dimension field starts: whatever automatic compression resizes to. */-export function defaultMaxWidthHeight(): number {- const configured = optionService.getInt("imageMaxWidthHeight");-- return isPositiveInteger(configured) ? Number(configured) : FALLBACK_MAX_WIDTH_HEIGHT;-}+/** The longest edge the first run offers to scale down to: the width of a Full HD screen. */+export const DEFAULT_MAX_WIDTH_HEIGHT = 1920;-/** Where the quality slider starts: whatever automatic compression encodes at. */-export function defaultQuality(): number {- const configured = optionService.getInt("imageJpegQuality");-- return isQuality(configured) ? Number(configured) : DEFAULT_QUALITY;-}--/**- * Stands in when the image option itself is unreadable or nonsensical — the same figure it ships- * with, so the field opens on a bound that resizes something rather than on one that resizes nothing.- */-const FALLBACK_MAX_WIDTH_HEIGHT = 2000;+/** Where recompressing an already-lossy image starts. */+export const DEFAULT_QUALITY = 75;/**- * Where converting starts, above {@link DEFAULT_QUALITY} and matching the server's own default:- * converting a lossless original gives up detail that was genuinely there, where recompressing an- * already-lossy image works on detail that is long gone.+ * Where converting starts, above {@link DEFAULT_QUALITY}: converting a lossless original gives up+ * detail that was genuinely there, where recompressing an already-lossy image works on detail that+ * is long gone.*/export const DEFAULT_CONVERSION_QUALITY = 85;