From bbaebfb146673044468d37d8ff48f411584bd52e Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Mon, 16 Nov 2026 20:53:43 +0800 Subject: [PATCH] :bug: Send the team's custom fonts to the worker (#13044) The worker draws local exports and looks custom fonts up in its own store to find their files. Only the main thread fetches the team's fonts, so the worker's store had none, and exports drew text set in a custom font with the fallback font. The main thread now sends the worker the team's fonts each time they change, and the worker keeps them in its store. AI-assisted-by: claude-opus-5-5 --- frontend/src/app/main/data/fonts.cljs | 12 +++++ frontend/src/app/worker/impl.cljs | 9 +++- frontend/test/frontend_tests/runner.cljs | 2 + .../worker/fonts_sync_test.cljs | 54 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 frontend/test/frontend_tests/worker/fonts_sync_test.cljs diff --git a/frontend/src/app/main/data/fonts.cljs b/frontend/src/app/main/data/fonts.cljs index ec67e752a6..252e95ebc1 100644 --- a/frontend/src/app/main/data/fonts.cljs +++ b/frontend/src/app/main/data/fonts.cljs @@ -18,6 +18,7 @@ [app.main.fonts :as fonts] [app.main.repo :as rp] [app.main.store :as st] + [app.main.worker :as mw] [app.util.i18n :refer [tr]] [app.util.storage :as storage] [app.util.webapi :as wa] @@ -85,6 +86,17 @@ (->> (rp/cmd! :get-font-variants {:team-id team-id}) (rx/map fonts-fetched))))) +(defn- sync-worker-fonts + "Store watcher that sends the worker the team's custom fonts each + time they change. The worker's render looks them up in its own + store, which nothing else fills." + [_ _ old-state new-state] + (let [fonts (:fonts new-state)] + (when-not (identical? fonts (:fonts old-state)) + (mw/emit! {:cmd :fonts/sync :fonts fonts})))) + +(add-watch st/state ::sync-worker-fonts sync-worker-fonts) + (defn upload-font-variant "Uploads a single font variant item using the chunked upload API. diff --git a/frontend/src/app/worker/impl.cljs b/frontend/src/app/worker/impl.cljs index c7f65dc1b7..aa060c7a6b 100644 --- a/frontend/src/app/worker/impl.cljs +++ b/frontend/src/app/worker/impl.cljs @@ -8,7 +8,8 @@ "Dispatcher for messages received from the main thread." (:require [app.common.logging :as log] - [app.config :as cf])) + [app.config :as cf] + [app.main.store :as st])) (log/set-level! :info) @@ -43,3 +44,9 @@ (set! cf/build-date build-date)) nil) + +(defmethod handler :fonts/sync + [{:keys [fonts]}] + ;; The render looks the team's custom fonts up in the store + (swap! st/state assoc :fonts fonts) + nil) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 395cb234b8..7dc29e4857 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -199,6 +199,7 @@ [frontend-tests.worker-snap-test] [frontend-tests.worker.component-thumbnails-test] [frontend-tests.worker.exports-test] + [frontend-tests.worker.fonts-sync-test] [frontend-tests.worker.index-test] [frontend-tests.worker.messages-test] [frontend-tests.worker.trim-test] @@ -245,6 +246,7 @@ 'frontend-tests.data.jobs-test 'frontend-tests.worker.component-thumbnails-test 'frontend-tests.worker.exports-test + 'frontend-tests.worker.fonts-sync-test 'frontend-tests.worker.index-test 'frontend-tests.worker.messages-test 'frontend-tests.worker.trim-test diff --git a/frontend/test/frontend_tests/worker/fonts_sync_test.cljs b/frontend/test/frontend_tests/worker/fonts_sync_test.cljs new file mode 100644 index 0000000000..6cdf76313d --- /dev/null +++ b/frontend/test/frontend_tests/worker/fonts_sync_test.cljs @@ -0,0 +1,54 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.worker.fonts-sync-test + "The main thread keeps the worker's copy of the team's custom fonts + current, for the render there to find them." + (:require + [app.common.uuid :as uuid] + [app.main.data.fonts :as df] + [app.main.store :as st] + [app.main.worker :as mw] + [app.worker.impl :as impl] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.mock :as mock] + [okulary.core :as l])) + +(defn- font-variant + [] + (let [id (uuid/next)] + {:id id + :font-id (uuid/next) + :font-family "Custom" + :font-weight 400 + :font-style "normal" + :ttf-file-id (uuid/next)})) + +(t/deftest ^:async the-worker-gets-the-fonts-each-time-they-change + (let [a (font-variant) + b (font-variant) + store (l/atom {}) + sent (atom [])] + (await + (mock/with-mocks* + {mw/emit! (mock/stub (fn [message] (swap! sent conj message) (rx/empty)))} + (add-watch store ::sync @#'df/sync-worker-fonts) + (swap! store assoc :fonts {(:id a) a}) + (swap! store assoc :selected #{}) + (swap! store update :fonts assoc (:id b) b))) + (t/is (= [{:cmd :fonts/sync :fonts {(:id a) a}} + {:cmd :fonts/sync :fonts {(:id a) a (:id b) b}}] + @sent)))) + +(t/deftest ^:async the-worker-keeps-the-fonts-it-gets + (let [a (font-variant) + b (font-variant)] + (await + (mock/with-mocks* + {st/state (l/atom {:fonts {(:id a) a}})} + (impl/handler {:cmd :fonts/sync :fonts {(:id b) b}}) + (t/is (= {(:id b) b} (:fonts @st/state)))))))