0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 16:21:57 -05:00

Add some utilities for fonts in plugins

This commit is contained in:
alonso.torres 2024-06-13 15:25:09 +02:00
parent 2da5dcb619
commit 69fad7a920
2 changed files with 90 additions and 0 deletions

View file

@ -21,6 +21,7 @@
[app.main.store :as st]
[app.plugins.events :as events]
[app.plugins.file :as file]
[app.plugins.fonts :as fonts]
[app.plugins.library :as library]
[app.plugins.page :as page]
[app.plugins.shape :as shape]
@ -206,4 +207,5 @@
{:name "viewport" :get #(.getViewport ^js %)}
{:name "currentUser" :get #(.getCurrentUser ^js %)}
{:name "activeUsers" :get #(.getActiveUsers ^js %)}
{:name "fonts" :get (fn [_] (fonts/fonts-subcontext plugin-id))}
{:name "library" :get (fn [_] (library/library-subcontext plugin-id))}))

View file

@ -0,0 +1,88 @@
;; 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 INC
(ns app.plugins.fonts
(:require
[app.common.data :as d]
[app.common.record :as cr]
[app.main.data.workspace.texts :as dwt]
[app.main.fonts :as fonts]
[app.main.store :as st]
[app.util.object :as obj]
[cuerdas.core :as str]))
(deftype PenpotFontVariant [name fontVariantId fontWeight fontStyle])
(deftype PenpotFont [name fontId fontFamily fontStyle fontVariantId fontWeight variants]
Object
(applyToText [_ text variant]
(let [id (obj/get text "$id")
values {:font-id fontId
:font-family fontFamily
:font-style (d/nilv (obj/get variant "fontStyle") fontStyle)
:font-variant-id (d/nilv (obj/get variant "fontVariantId") fontVariantId)
:font-weight (d/nilv (obj/get variant "fontWeight") fontWeight)}]
(st/emit! (dwt/update-attrs id values))))
(applyToRange [_ range variant]
(let [id (obj/get range "$id")
start (obj/get range "start")
end (obj/get range "end")
values {:font-id fontId
:font-family fontFamily
:font-style (d/nilv (obj/get variant "fontStyle") fontStyle)
:font-variant-id (d/nilv (obj/get variant "fontVariantId") fontVariantId)
:font-weight (d/nilv (obj/get variant "fontWeight") fontWeight)}]
(st/emit! (dwt/update-text-range id start end values)))))
(defn font-proxy
[{:keys [id name variants] :as font}]
(when (some? font)
(let [default-variant (fonts/get-default-variant font)]
(PenpotFont.
name
id
id
(:style default-variant)
(:id default-variant)
(:weight default-variant)
(apply
array
(->> variants
(map (fn [{:keys [id name style weight]}]
(PenpotFontVariant. name id weight style)))))))))
(deftype PenpotFontsSubcontext [$plugin]
Object
(findById
[_ id]
(font-proxy (d/seek #(str/includes? (str/lower (:id %)) (str/lower id)) (vals @fonts/fontsdb))))
(findByName
[_ name]
(font-proxy (d/seek #(str/includes? (str/lower (:name %)) (str/lower name)) (vals @fonts/fontsdb))))
(findAllById
[_ id]
(apply array (->> (vals @fonts/fontsdb)
(filter #(str/includes? (str/lower (:id %)) (str/lower id)))
(map font-proxy))))
(findAllByName
[_ name]
(apply array (->> (vals @fonts/fontsdb)
(filter #(str/includes? (str/lower (:name %)) (str/lower name)))
(map font-proxy)))))
(defn fonts-subcontext
[plugin-id]
(cr/add-properties!
(PenpotFontsSubcontext. plugin-id)
{:name "$plugin" :enumerable false :get (constantly plugin-id)}
{:name "all" :get
(fn [_]
(apply array (->> @fonts/fontsdb vals (map font-proxy))))}))