diff --git a/frontend/playwright/ui/specs/typography.spec.js b/frontend/playwright/ui/specs/typography.spec.js new file mode 100644 index 0000000000..c74129fdad --- /dev/null +++ b/frontend/playwright/ui/specs/typography.spec.js @@ -0,0 +1,109 @@ +import { test, expect } from "@playwright/test"; +import { WasmWorkspacePage } from "../pages/WasmWorkspacePage"; + +// workspace/multiselection-typography.json: "Text with no typography" is a +// fixed 14px Rufina text, and the local library has the typographies +// "typography one" (Agdasima) and "typography 2" (IM Fell French Canon SC). +const FILE = { + id: "c7ce0794-0992-8105-8004-38f280443849", + pageId: "1062e0a0-8fe0-80ae-8007-e70b4993f5f0", +}; + +test.beforeEach(async ({ page }) => { + await WasmWorkspacePage.init(page); +}); + +async function setupTypographyFile(page) { + const workspace = new WasmWorkspacePage(page); + await workspace.setupEmptyFile(); + await workspace.mockRPC( + /get\-file\?/, + "workspace/multiselection-typography.json", + ); + await workspace.mockGoogleFont("rufina", "render-wasm/assets/ebgaramond.ttf"); + await workspace.mockGoogleFont( + "agdasima", + "render-wasm/assets/ebgaramond.ttf", + ); + await workspace.mockGoogleFont( + "im-fell-french-canon-sc", + "render-wasm/assets/ebgaramond.ttf", + ); + await workspace.goToWorkspace({ fileId: FILE.id, pageId: FILE.pageId }); + await workspace.waitForFirstRender(); + return workspace; +} + +function measureInput(workspace, name) { + return workspace.rightSidebar + .getByRole("region", { name: "shape-measures-section" }) + .getByRole("textbox", { name, exact: true }); +} + +// Selects "Text with no typography", switches it to auto-width and returns +// the Width input with the auto-width value. +async function setupAutoWidthText(workspace) { + // Exact match: "Text with no typography two" also exists and comes first. + await workspace.layers + .getByText("Text with no typography", { exact: true }) + .click(); + const widthInput = measureInput(workspace, "Width"); + const fixedWidth = await widthInput.inputValue(); + + await workspace.rightSidebar + .getByRole("button", { name: "Auto width" }) + .click(); + await expect( + workspace.rightSidebar.getByRole("button", { + name: "Auto width", + pressed: true, + }), + ).toBeVisible(); + + // The resize is async: wait for the width to change, then check it shrank. + await expect(widthInput).not.toHaveValue(fixedWidth); + const autoWidth = await widthInput.inputValue(); + expect(Number(autoWidth)).toBeLessThan(Number(fixedWidth)); + + return { widthInput, autoWidth }; +} + +// "typography 2" is larger (16px) than the text (14px) and uppercase, so the +// text must grow, and one undo must revert both the typography and the size. +async function expectGrowthAndUndo(page, widthInput, autoWidth) { + await expect(widthInput).not.toHaveValue(autoWidth); + expect(Number(await widthInput.inputValue())).toBeGreaterThan( + Number(autoWidth), + ); + + await page.keyboard.press("ControlOrMeta+z"); + await expect(widthInput).toHaveValue(autoWidth); +} + +test.describe("BUG 13124 - Applying a typography resizes an auto-width text", () => { + test("from the assets panel", async ({ page }) => { + const workspace = await setupTypographyFile(page); + const { widthInput, autoWidth } = await setupAutoWidthText(workspace); + + await workspace.clickAssets(); + // The Typographies group starts collapsed. + await workspace.sidebar + .getByRole("button", { name: /^Typographies/ }) + .click(); + await workspace.sidebar + .getByTitle("typography 2 (IM Fell French Canon SC)") + .click(); + + await expectGrowthAndUndo(page, widthInput, autoWidth); + }); + + test("from the typographies palette", async ({ page }) => { + const workspace = await setupTypographyFile(page); + const { widthInput, autoWidth } = await setupAutoWidthText(workspace); + + await page.getByRole("button", { name: "Typographies (Alt+T)" }).click(); + await page.getByTitle("typography 2", { exact: true }).click(); + + await expectGrowthAndUndo(page, widthInput, autoWidth); + }); +}); diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 617539aef9..f6ec58fdae 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -1272,14 +1272,19 @@ (assert (or (nil? ids) (and (set? ids) (every? uuid? ids)))) (ptk/reify ::apply-typography ptk/WatchEvent - (watch [_ state _] + (watch [_ state stream] (let [editor-state (:workspace-editor-state state) + objects (dsh/lookup-page-objects state) ids (d/nilv ids (dsh/lookup-selected state)) attrs (-> typography (assoc :typography-ref-file file-id) (assoc :typography-ref-id (:id typography)) (dissoc :id :name)) - undo-id (js/Symbol)] + auto-ids (into [] + (comp (mapcat #(resolve-text-ids objects %)) + (remove #(= :fixed (:grow-type (get objects %))))) + ids) + undo-id (js/Symbol)] (rx/concat (rx/of (dwu/start-undo-transaction undo-id)) @@ -1287,6 +1292,9 @@ (rx/map (fn [id] (let [editor (get editor-state id)] (update-text-attrs {:id id :editor editor :attrs attrs}))))) + ;; WASM does not re-measure on its own; the new font may still be loading. + (when (features/active-feature? state "render-wasm/v1") + (await-font-resize stream auto-ids)) (rx/of (dwu/commit-undo-transaction undo-id)))))))) (defn generate-typography-name diff --git a/frontend/src/app/main/ui/workspace/text_palette.cljs b/frontend/src/app/main/ui/workspace/text_palette.cljs index e951b6120b..412825429b 100644 --- a/frontend/src/app/main/ui/workspace/text_palette.cljs +++ b/frontend/src/app/main/ui/workspace/text_palette.cljs @@ -33,21 +33,11 @@ (mf/use-callback (mf/deps typography selected-ids file-id current-file-id) (fn [] - (let [attrs (merge - {:typography-ref-file file-id - :typography-ref-id (:id typography)} - (dissoc typography :id :name))] - - (st/emit! (ev/event - {::ev/name "use-library-typography" - ::ev/origin "text-palette" - :external-library (not= file-id current-file-id)})) - (run! #(st/emit! - (dwt/update-text-attrs - {:id % - :editor (get @refs/workspace-editor-state %) - :attrs attrs})) - selected-ids))))] + (st/emit! (ev/event + {::ev/name "use-library-typography" + ::ev/origin "text-palette" + :external-library (not= file-id current-file-id)}) + (dwt/apply-typography (into #{} selected-ids) typography file-id))))] [:div {:on-click handle-click :class (stl/css-case :typography-item true :mid-item (<= size 72)