diff --git a/frontend/resources/images/icons/text-align-bottom.svg b/frontend/resources/images/icons/text-align-bottom.svg new file mode 100644 index 0000000000..590e7a053f --- /dev/null +++ b/frontend/resources/images/icons/text-align-bottom.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-align-middle.svg b/frontend/resources/images/icons/text-align-middle.svg new file mode 100644 index 0000000000..9480d4a6b0 --- /dev/null +++ b/frontend/resources/images/icons/text-align-middle.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-align-top.svg b/frontend/resources/images/icons/text-align-top.svg new file mode 100644 index 0000000000..c3179a781a --- /dev/null +++ b/frontend/resources/images/icons/text-align-top.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-auto-height-vertical.svg b/frontend/resources/images/icons/text-auto-height-vertical.svg new file mode 100644 index 0000000000..8de6bd7048 --- /dev/null +++ b/frontend/resources/images/icons/text-auto-height-vertical.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-auto-width-vertical.svg b/frontend/resources/images/icons/text-auto-width-vertical.svg new file mode 100644 index 0000000000..51a0e12e80 --- /dev/null +++ b/frontend/resources/images/icons/text-auto-width-vertical.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-combine-upright-all.svg b/frontend/resources/images/icons/text-combine-upright-all.svg new file mode 100644 index 0000000000..594b7f49fd --- /dev/null +++ b/frontend/resources/images/icons/text-combine-upright-all.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-combine-upright-digits.svg b/frontend/resources/images/icons/text-combine-upright-digits.svg new file mode 100644 index 0000000000..3e367a3fd3 --- /dev/null +++ b/frontend/resources/images/icons/text-combine-upright-digits.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-combine-upright-none.svg b/frontend/resources/images/icons/text-combine-upright-none.svg new file mode 100644 index 0000000000..f54cf6f1e3 --- /dev/null +++ b/frontend/resources/images/icons/text-combine-upright-none.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-emphasis-filled-circle.svg b/frontend/resources/images/icons/text-emphasis-filled-circle.svg new file mode 100644 index 0000000000..fb5269a95a --- /dev/null +++ b/frontend/resources/images/icons/text-emphasis-filled-circle.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-emphasis-filled-dot.svg b/frontend/resources/images/icons/text-emphasis-filled-dot.svg new file mode 100644 index 0000000000..ffce4048e2 --- /dev/null +++ b/frontend/resources/images/icons/text-emphasis-filled-dot.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-emphasis-open-circle.svg b/frontend/resources/images/icons/text-emphasis-open-circle.svg new file mode 100644 index 0000000000..eee36c9989 --- /dev/null +++ b/frontend/resources/images/icons/text-emphasis-open-circle.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-emphasis-open-dot.svg b/frontend/resources/images/icons/text-emphasis-open-dot.svg new file mode 100644 index 0000000000..81108caf2e --- /dev/null +++ b/frontend/resources/images/icons/text-emphasis-open-dot.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-emphasis-open-sesame.svg b/frontend/resources/images/icons/text-emphasis-open-sesame.svg new file mode 100644 index 0000000000..2c55c5baa7 --- /dev/null +++ b/frontend/resources/images/icons/text-emphasis-open-sesame.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-emphasis.svg b/frontend/resources/images/icons/text-emphasis.svg new file mode 100644 index 0000000000..965015e1fa --- /dev/null +++ b/frontend/resources/images/icons/text-emphasis.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-horizontal-center.svg b/frontend/resources/images/icons/text-horizontal-center.svg new file mode 100644 index 0000000000..782b8d6592 --- /dev/null +++ b/frontend/resources/images/icons/text-horizontal-center.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-justify-vertical.svg b/frontend/resources/images/icons/text-justify-vertical.svg new file mode 100644 index 0000000000..2ddbbef9c3 --- /dev/null +++ b/frontend/resources/images/icons/text-justify-vertical.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-left.svg b/frontend/resources/images/icons/text-left.svg new file mode 100644 index 0000000000..f489023b88 --- /dev/null +++ b/frontend/resources/images/icons/text-left.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/text-orientation-mixed.svg b/frontend/resources/images/icons/text-orientation-mixed.svg new file mode 100644 index 0000000000..72cd592478 --- /dev/null +++ b/frontend/resources/images/icons/text-orientation-mixed.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-orientation-upright.svg b/frontend/resources/images/icons/text-orientation-upright.svg new file mode 100644 index 0000000000..67d241b2bb --- /dev/null +++ b/frontend/resources/images/icons/text-orientation-upright.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/text-right.svg b/frontend/resources/images/icons/text-right.svg new file mode 100644 index 0000000000..a7f77b7b31 --- /dev/null +++ b/frontend/resources/images/icons/text-right.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/resources/images/icons/warichu-none.svg b/frontend/resources/images/icons/warichu-none.svg new file mode 100644 index 0000000000..7ede0d3d0a --- /dev/null +++ b/frontend/resources/images/icons/warichu-none.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/warichu.svg b/frontend/resources/images/icons/warichu.svg new file mode 100644 index 0000000000..3348550253 --- /dev/null +++ b/frontend/resources/images/icons/warichu.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/writing-mode-horizontal.svg b/frontend/resources/images/icons/writing-mode-horizontal.svg new file mode 100644 index 0000000000..204e9190e7 --- /dev/null +++ b/frontend/resources/images/icons/writing-mode-horizontal.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/resources/images/icons/writing-mode-vertical.svg b/frontend/resources/images/icons/writing-mode-vertical.svg new file mode 100644 index 0000000000..e89a00a562 --- /dev/null +++ b/frontend/resources/images/icons/writing-mode-vertical.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 3b7a9c40b5..ebfd2ae8b3 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -421,7 +421,7 @@ (assoc :recent-fonts (:recent-fonts storage/user)) (assoc :current-file-id file-id) (assoc :workspace-presence {}) - (update :workspace-global dissoc :default-font) + (update :workspace-global dissoc :default-font :default-text-language) (update :comments-local dcmt/merge-persisted-filters))) ptk/WatchEvent @@ -627,13 +627,14 @@ :current-file-id :workspace-editor-state :workspace-wasm-editor-styles + :workspace-wasm-editor-selection-rects :workspace-media-objects :workspace-presence :workspace-tokens :workspace-undo :workspace-versions) (update :workspace-global dissoc :read-only? :preview-id :default-font - :persistence-lock) + :default-text-language :persistence-lock) (assoc-in [:workspace-global :options-mode] :design) (update :files d/update-vals #(dissoc % :data)))) @@ -696,6 +697,20 @@ (dm/export layout/toggle-layout-flag) (dm/export layout/remove-layout-flag) +(defn set-default-text-language + "Set the language new text layers of the file take; nil removes it. + Existing texts keep theirs. The language remembered for new texts is + dropped, so they take the new file language." + [language] + (ptk/reify ::set-default-text-language + ptk/WatchEvent + (watch [it state _] + (let [changes (-> (pcb/empty-changes it) + (pcb/with-file-data (dsh/lookup-file-data state)) + (pcb/set-default-text-language language))] + (rx/of (dch/commit-changes changes) + (dwtxt/remember-text-language nil)))))) + ;; --- Profile (defn update-nudge diff --git a/frontend/src/app/main/data/workspace/edition.cljs b/frontend/src/app/main/data/workspace/edition.cljs index 185289276e..41a57e4126 100644 --- a/frontend/src/app/main/data/workspace/edition.cljs +++ b/frontend/src/app/main/data/workspace/edition.cljs @@ -64,7 +64,8 @@ (not path-editing?) (update :workspace-drawing dissoc :object :lock)) (dissoc :workspace-grid-edition) - (dissoc :workspace-wasm-editor-styles)))) + (dissoc :workspace-wasm-editor-styles) + (dissoc :workspace-wasm-editor-selection-rects)))) ptk/WatchEvent (watch [_ _ _] diff --git a/frontend/src/app/main/data/workspace/texts_v3.cljs b/frontend/src/app/main/data/workspace/texts_v3.cljs index d9c288eac3..d91fccd7b7 100644 --- a/frontend/src/app/main/data/workspace/texts_v3.cljs +++ b/frontend/src/app/main/data/workspace/texts_v3.cljs @@ -23,3 +23,20 @@ (update-in state [:workspace-wasm-editor-styles id] (fn [current-styles] (merge defaults current-styles new-styles)))))))) + +(defn v3-update-text-editor-selection-rect + "Store the box around the text selected in the editor of the text `id`, in + page coordinates, or drop it when `rect` is nil: the selection collapsed." + [id rect] + (ptk/reify ::v3-update-text-editor-selection-rect + ptk/UpdateEvent + (update [_ state] + (cond + (not= id (get-in state [:workspace-local :edition])) + state + + (nil? rect) + (update state :workspace-wasm-editor-selection-rects dissoc id) + + :else + (assoc-in state [:workspace-wasm-editor-selection-rects id] rect))))) diff --git a/frontend/src/app/main/refs.cljs b/frontend/src/app/main/refs.cljs index 61baf8e3af..5a0000e9e5 100644 --- a/frontend/src/app/main/refs.cljs +++ b/frontend/src/app/main/refs.cljs @@ -11,6 +11,7 @@ [app.common.data.macros :as dm] [app.common.files.helpers :as cph] [app.common.files.tokens :as cfo] + [app.common.types.file :as ctf] [app.common.types.shape-tree :as ctt] [app.common.types.shape.layout :as ctl] [app.common.types.tokens-lib :as ctob] @@ -20,6 +21,7 @@ [app.main.data.workspace.tokens.selected-set :as dwts] [app.main.store :as st] [app.main.streams :as ms] + [app.util.text.layout :as tl] [beicon.v2.core :as rx] [clojure.string :as str] [okulary.core :as l])) @@ -246,6 +248,10 @@ (def default-font (l/derived :default-font workspace-global)) +(def default-text-language + "Language the last text option change picked, for the next new texts." + (l/derived :default-text-language workspace-global)) + (def inspect-expanded (l/derived :inspect-expanded workspace-local)) @@ -355,6 +361,35 @@ [ids] (l/derived #(into [] (keep (d/getf %)) ids) workspace-page-objects =)) +(def file-text-language + "Language the new text layers of the open file take." + (l/derived ctf/get-default-text-language workspace-data)) + +(defn texts-language + "Language of the texts among `ids` and of the texts inside the groups + among them (see `tl/get-texts-language`)." + [ids] + (l/derived + (fn [state] + (let [objects (dsh/lookup-page-objects state) + contents (into [] + (mapcat + (fn [id] + (let [shape (get objects id)] + (cond + (cph/text-shape? shape) + [(:content shape)] + + (cph/group-shape? shape) + (into [] + (comp (map (d/getf objects)) + (filter cph/text-shape?) + (map :content)) + (cph/get-children-ids objects id)))))) + ids)] + (tl/get-texts-language contents))) + st/state =)) + (defn parents-by-ids [ids] (l/derived @@ -406,6 +441,9 @@ (def workspace-wasm-editor-styles (l/derived :workspace-wasm-editor-styles st/state)) +(def workspace-wasm-editor-selection-rects + (l/derived :workspace-wasm-editor-selection-rects st/state)) + (def workspace-wasm-modifiers (let [a (atom nil)] (rx/sub! ms/wasm-modifiers #(reset! a %)) diff --git a/frontend/src/app/main/ui/components/grouped_dropdown.cljs b/frontend/src/app/main/ui/components/grouped_dropdown.cljs new file mode 100644 index 0000000000..a136a74063 --- /dev/null +++ b/frontend/src/app/main/ui/components/grouped_dropdown.cljs @@ -0,0 +1,84 @@ +;; 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 app.main.ui.components.grouped-dropdown + "Dropdown list of options split in groups by non-selectable header rows, + with an optional search bar. The caller owns the trigger and the open + state, and positions the list from the nearest positioned ancestor." + (:require-macros [app.main.style :as stl]) + (:require + [app.main.ui.components.dropdown :refer [dropdown]] + [app.main.ui.components.search-bar :refer [search-bar*]] + [app.main.ui.icons :as deprecated-icon] + [app.util.dom :as dom] + [app.util.keyboard :as kbd] + [rumext.v2 :as mf])) + +(mf/defc option-row* + {::mf/private true} + [{:keys [option on-select]}] + (let [{:keys [id label detail selected]} option + + handle-click + (mf/use-fn + (mf/deps id on-select) + (fn [event] + (on-select id event))) + + handle-key-down + (mf/use-fn + (mf/deps id on-select) + (fn [event] + (when (kbd/enter? event) + (dom/prevent-default event) + (on-select id event))))] + [:li {:class (stl/css-case :option true + :selected selected) + :role "option" + :aria-selected (boolean selected) + :tab-index 0 + :data-testid (str "grouped-dropdown-option-" id) + :on-click handle-click + :on-key-down handle-key-down} + [:div {:class (stl/css :option-text)} + [:span {:class (stl/css :option-label)} label] + (when (some? detail) + [:span {:class (stl/css :option-detail)} detail])] + (when selected + [:span {:class (stl/css :check-icon)} deprecated-icon/tick])])) + +(mf/defc grouped-dropdown* + "`options` holds the rows in display order: a header row is + `{:id :label :header true}`, an option row `{:id :label}` with an optional + `:detail` text and `:selected` flag. `on-select` receives the option id and + the event. Passing `on-search-change` shows the search bar; `empty-label` + is the row shown when `options` is empty." + [{:keys [show on-close container options on-select class + search-term search-placeholder on-search-change empty-label]}] + [:& dropdown {:show show + :on-close on-close + :container container} + [:div {:class [class (stl/css :dropdown)] + :on-click dom/stop-propagation} + (when (some? on-search-change) + [:div {:class (stl/css :search)} + [:> search-bar* {:on-change on-search-change + :value search-term + :auto-focus true + :placeholder search-placeholder}]]) + [:ul {:class (stl/css :option-list) + :role "listbox"} + (if (empty? options) + [:li {:class (stl/css :empty)} empty-label] + (for [option options] + (if (:header option) + [:li {:key (:id option) + :class (stl/css :header) + :role "presentation"} + (:label option)] + [:> option-row* {:key (:id option) + :option option + :on-select on-select}])))]]]) diff --git a/frontend/src/app/main/ui/components/grouped_dropdown.scss b/frontend/src/app/main/ui/components/grouped_dropdown.scss new file mode 100644 index 0000000000..10e30dae95 --- /dev/null +++ b/frontend/src/app/main/ui/components/grouped_dropdown.scss @@ -0,0 +1,93 @@ +// 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 + +@use "refactor/common-refactor.scss" as deprecated; + +.dropdown { + @extend %dropdown-wrapper; + + margin-top: deprecated.$s-2; + max-height: 70vh; + width: deprecated.$s-252; + display: flex; + flex-direction: column; + overflow: hidden; +} + +.search { + padding: deprecated.$s-4; + border-bottom: deprecated.$s-1 solid var(--menu-border-color-rest, transparent); +} + +.option-list { + flex: 1 1 auto; + min-height: 0; + overflow-y: auto; + margin: 0; + padding: 0; + list-style: none; +} + +.header, +.empty { + @extend %dropdown-element-base; + + pointer-events: none; + cursor: default; + color: var(--menu-foreground-color); +} + +.header { + text-transform: uppercase; +} + +.option { + @extend %dropdown-element-base; + + .option-text { + display: flex; + gap: deprecated.$s-8; + flex-grow: 1; + } + + .option-label, + .option-detail { + color: var(--menu-foreground-color-rest); + } + + .check-icon { + @include deprecated.flex-center; + + svg { + @extend %button-icon-small; + + stroke: var(--icon-foreground); + } + } + + &.selected { + .option-label { + color: var(--menu-foreground-color-hover); + } + + .check-icon svg { + stroke: var(--menu-foreground-color-hover); + } + } + + &:hover, + &:focus-visible { + background-color: var(--menu-background-color-hover); + + .option-label { + color: var(--menu-foreground-color-hover); + } + + .check-icon svg { + stroke: var(--menu-foreground-color-hover); + } + } +} diff --git a/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs b/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs index 126cb7b4bc..3248395941 100644 --- a/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs +++ b/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs @@ -275,25 +275,45 @@ (def ^:icon-id swatches "swatches") (def ^:icon-id switch "switch") (def ^:icon-id text "text") +(def ^:icon-id text-align-bottom "text-align-bottom") (def ^:icon-id text-align-center "text-align-center") (def ^:icon-id text-align-left "text-align-left") +(def ^:icon-id text-align-middle "text-align-middle") (def ^:icon-id text-align-right "text-align-right") +(def ^:icon-id text-align-top "text-align-top") (def ^:icon-id text-auto-height "text-auto-height") +(def ^:icon-id text-auto-height-vertical "text-auto-height-vertical") (def ^:icon-id text-auto-width "text-auto-width") +(def ^:icon-id text-auto-width-vertical "text-auto-width-vertical") (def ^:icon-id text-bottom "text-bottom") +(def ^:icon-id text-combine-upright-all "text-combine-upright-all") +(def ^:icon-id text-combine-upright-digits "text-combine-upright-digits") +(def ^:icon-id text-combine-upright-none "text-combine-upright-none") +(def ^:icon-id text-emphasis "text-emphasis") +(def ^:icon-id text-emphasis-filled-circle "text-emphasis-filled-circle") +(def ^:icon-id text-emphasis-filled-dot "text-emphasis-filled-dot") +(def ^:icon-id text-emphasis-open-circle "text-emphasis-open-circle") +(def ^:icon-id text-emphasis-open-dot "text-emphasis-open-dot") +(def ^:icon-id text-emphasis-open-sesame "text-emphasis-open-sesame") (def ^:icon-id text-fixed "text-fixed") (def ^:icon-id text-font-family "text-font-family") (def ^:icon-id text-font-size "text-font-size") (def ^:icon-id text-font-weight "text-font-weight") +(def ^:icon-id text-horizontal-center "text-horizontal-center") (def ^:icon-id text-justify "text-justify") +(def ^:icon-id text-justify-vertical "text-justify-vertical") +(def ^:icon-id text-left "text-left") (def ^:icon-id text-letterspacing "text-letterspacing") (def ^:icon-id text-lineheight "text-lineheight") (def ^:icon-id text-lowercase "text-lowercase") (def ^:icon-id text-ltr "text-ltr") (def ^:icon-id text-middle "text-middle") (def ^:icon-id text-mixed "text-mixed") +(def ^:icon-id text-orientation-mixed "text-orientation-mixed") +(def ^:icon-id text-orientation-upright "text-orientation-upright") (def ^:icon-id text-palette "text-palette") (def ^:icon-id text-paragraph "text-paragraph") +(def ^:icon-id text-right "text-right") (def ^:icon-id text-rtl "text-rtl") (def ^:icon-id text-stroked "text-stroked") (def ^:icon-id text-top "text-top") @@ -314,7 +334,11 @@ (def ^:icon-id vertical-align-items-start "vertical-align-items-start") (def ^:icon-id view-as-icons "view-as-icons") (def ^:icon-id view-as-list "view-as-list") +(def ^:icon-id warichu "warichu") +(def ^:icon-id warichu-none "warichu-none") (def ^:icon-id wrap "wrap") +(def ^:icon-id writing-mode-horizontal "writing-mode-horizontal") +(def ^:icon-id writing-mode-vertical "writing-mode-vertical") (def icon-list "A collection of all icons" diff --git a/frontend/src/app/main/ui/icons.cljs b/frontend/src/app/main/ui/icons.cljs index 4dc9408889..9a9e6ac7a9 100644 --- a/frontend/src/app/main/ui/icons.cljs +++ b/frontend/src/app/main/ui/icons.cljs @@ -245,6 +245,9 @@ (def ^:icon text-auto-height (icon-xref :text-auto-height)) (def ^:icon text-auto-width (icon-xref :text-auto-width)) (def ^:icon text-bottom (icon-xref :text-bottom)) +(def ^:icon text-combine-upright-all (icon-xref :text-combine-upright-all)) +(def ^:icon text-combine-upright-digits (icon-xref :text-combine-upright-digits)) +(def ^:icon text-combine-upright-none (icon-xref :text-combine-upright-none)) (def ^:icon text-fixed (icon-xref :text-fixed)) (def ^:icon text-font-family (icon-xref :text-font-family)) (def ^:icon text-font-size (icon-xref :text-font-size)) @@ -256,6 +259,8 @@ (def ^:icon text-ltr (icon-xref :text-ltr)) (def ^:icon text-middle (icon-xref :text-middle)) (def ^:icon text-mixed (icon-xref :text-mixed)) +(def ^:icon text-orientation-mixed (icon-xref :text-orientation-mixed)) +(def ^:icon text-orientation-upright (icon-xref :text-orientation-upright)) (def ^:icon text-palette (icon-xref :text-palette)) (def ^:icon text-paragraph (icon-xref :text-paragraph)) (def ^:icon text-rtl (icon-xref :text-rtl)) @@ -282,7 +287,11 @@ (def ^:icon vertical-align-items-start (icon-xref :vertical-align-items-start)) (def ^:icon view-as-icons (icon-xref :view-as-icons)) (def ^:icon view-as-list (icon-xref :view-as-list)) +(def ^:icon warichu (icon-xref :warichu)) +(def ^:icon warichu-none (icon-xref :warichu-none)) (def ^:icon wrap (icon-xref :wrap)) +(def ^:icon writing-mode-horizontal (icon-xref :writing-mode-horizontal)) +(def ^:icon writing-mode-vertical (icon-xref :writing-mode-vertical)) (def default "A collection of all icons" diff --git a/frontend/src/app/main/ui/settings/options.cljs b/frontend/src/app/main/ui/settings/options.cljs index 00898df09c..8c7bd4f957 100644 --- a/frontend/src/app/main/ui/settings/options.cljs +++ b/frontend/src/app/main/ui/settings/options.cljs @@ -11,10 +11,12 @@ [app.main.data.event :as ev] [app.main.data.notifications :as ntf] [app.main.data.profile :as du] + [app.main.features :as features] [app.main.refs :as refs] [app.main.router :as rt] [app.main.store :as st] [app.main.ui.components.forms :as fm] + [app.main.ui.ds.controls.select :refer [select*]] [app.main.ui.ds.controls.switch :refer [switch*]] [app.main.ui.ds.foundations.assets.icon :refer [icon*]] [app.main.ui.ds.foundations.typography :as t] @@ -23,6 +25,7 @@ [app.render-wasm.api.webgl :as webgl] [app.util.dom :as dom] [app.util.i18n :as i18n :refer [tr]] + [app.util.text.layout :as tl] [app.util.theme :as theme] [rumext.v2 :as mf])) @@ -123,10 +126,48 @@ :on-change handle-render-change}]] [:> text* {:typography t/body-medium :class (stl/css :feedback)} [:a {:href "#" :on-click go-settings-feedback :class (stl/css :link)} (tr "dashboard.webgl-switch.feedback") [:> icon* {:icon-id "arrow-up-right" :size "s"}]]]])) +(def ^:private no-text-language + "Id of the \"None\" choice: the DS select shows \"--\" for a blank id." + "none") + +(mf/defc text-language-settings* + "Language profile the new files of the user start with. The select lists + the profiles in the order of the profile lists, None first." + {::mf/private true} + [{:keys [language]}] + (let [options + (mf/with-memo [] + (into [{:id no-text-language :label (tr "dashboard.text-language.none")}] + (comp (remove :header) + (map (fn [{:keys [id]}] {:id id :label (tl/language-label id)}))) + (tl/language-options nil))) + + handle-change + (mf/use-fn + (fn [id] + (st/emit! (du/update-profile-props + {:default-text-language (when (not= no-text-language id) id)}) + (ntf/success (tr "notifications.profile-saved")))))] + + [:section {:class (stl/css :text-language-container)} + [:> heading* {:class (stl/css :title) :level 2 :typography t/title-large} + (tr "dashboard.text-language.title")] + [:> text* {:class (stl/css :description) :typography t/body-medium} + (tr "dashboard.text-language.description")] + [:div {:class (stl/css :text-language-form)} + [:> heading* {:level 3 :typography t/headline-small :id "text-language-label"} + (tr "dashboard.text-language.label")] + [:> select* {:default-selected (or language no-text-language) + :aria-labelledby "text-language-label" + :options options + :on-change handle-change + :data-testid "setting-text-language"}]]])) + (mf/defc options-page* [] - (let [profile (mf/deref refs/profile) - renderer (or (-> profile :props :renderer) :svg)] + (let [profile (mf/deref refs/profile) + renderer (or (-> profile :props :renderer) :svg) + cjk-feature? (features/use-feature "cjk-support")] (mf/use-effect #(dom/set-html-title (tr "title.settings.options"))) @@ -140,5 +181,7 @@ :id "options-section-title"} (tr "labels.settings")] [:> options-form*]] + (when ^boolean cjk-feature? + [:> text-language-settings* {:language (-> profile :props :default-text-language)}]) (when (contains? cf/flags :render-switch) [:> webgl-settings* {:renderer renderer}])]])) diff --git a/frontend/src/app/main/ui/settings/options.scss b/frontend/src/app/main/ui/settings/options.scss index 91cf8dc3c4..f65155cb11 100644 --- a/frontend/src/app/main/ui/settings/options.scss +++ b/frontend/src/app/main/ui/settings/options.scss @@ -33,7 +33,8 @@ /* Copied from profile.scss .form-container, but without included nested rules, since we want custom styles for it */ -.webgl-container { +.webgl-container, +.text-language-container { display: grid; grid-auto-rows: auto; gap: var(--sp-s); @@ -44,6 +45,12 @@ color: var(--color-foreground-primary); } +.text-language-form { + display: grid; + gap: var(--sp-s); + padding-block-start: var(--sp-l); +} + .webgl-header { display: flex; flex-direction: row; diff --git a/frontend/src/app/main/ui/workspace/main_menu.cljs b/frontend/src/app/main/ui/workspace/main_menu.cljs index b05f7eaab3..f04683205e 100644 --- a/frontend/src/app/main/ui/workspace/main_menu.cljs +++ b/frontend/src/app/main/ui/workspace/main_menu.cljs @@ -46,6 +46,8 @@ [app.util.dom :as dom] [app.util.i18n :as i18n :refer [tr]] [app.util.keyboard :as kbd] + [app.util.text.layout :as tl] + [app.util.text.writing-mode :as wm] [beicon.v2.core :as rx] [potok.v2.core :as ptk] [rumext.v2 :as mf])) @@ -231,15 +233,95 @@ [:span {:class (stl/css :feedback :item-name)} (tr "labels.give-feedback")]])])) +(mf/defc language-profile-menu* + "Menu item showing the language profile `language` (nil for none) and + opening the profile list beside it: None, then the profiles grouped by + language, the current one checked. `on-select` receives the chosen tag, + nil for None. A `disabled` item shows the profile but does not open." + {::mf/private true} + [{:keys [id label language disabled on-select on-close-all]}] + (let [open* (mf/use-state false) + open? (deref open*) + options (mf/with-memo [language] (tl/language-options language)) + + show (mf/use-fn #(reset! open* true)) + hide (mf/use-fn #(reset! open* false)) + + toggle + (mf/use-fn + (fn [event] + (dom/stop-propagation event) + (swap! open* not))) + + handle-select + (mf/use-fn + (mf/deps on-select on-close-all) + (fn [event] + (dom/stop-propagation event) + (let [tag (dom/get-data (dom/get-current-target event) "language")] + (on-select (when (not= "none" tag) tag)) + (on-close-all))))] + + [:> dropdown-menu-item* {:class (stl/css-case :base-menu-item true + :language-menu-item true + :disabled disabled) + :on-click (when-not disabled toggle) + :on-pointer-enter (when-not disabled show) + :on-pointer-leave (when-not disabled hide) + :aria-disabled disabled + :aria-haspopup "menu" + :aria-expanded open? + :id id} + [:span {:class (stl/css :item-name)} label] + [:span {:class (stl/css :item-value)} + (if (some? language) + (tl/language-label language) + (tr "workspace.header.menu.language-none"))] + [:> icon* {:icon-id i/arrow-right + :class (stl/css :item-arrow)}] + + (when ^boolean open? + [:div {:class (stl/css :language-submenu)} + [:ul {:class (stl/css :base-menu :language-submenu-list) + :role "menu"} + [:> dropdown-menu-item* {:class (stl/css :base-menu-item :submenu-item) + :data-language "none" + :on-click handle-select + :id (str id "-none")} + [:span {:class (stl/css :item-name)} + (tr "workspace.header.menu.language-none")] + (when (nil? language) + [:> icon* {:icon-id i/tick :class (stl/css :item-icon)}])] + + (for [{option-id :id :keys [label header selected]} options] + (if ^boolean header + [:li {:key option-id + :class (stl/css :language-separator) + :role "separator"}] + [:> dropdown-menu-item* {:key option-id + :class (stl/css :base-menu-item :submenu-item) + :data-language option-id + :on-click handle-select + :id (str id "-" option-id)} + [:span {:class (stl/css :item-name)} label] + (when ^boolean selected + [:> icon* {:icon-id i/tick :class (stl/css :item-icon)}])]))]])])) + (mf/defc preferences-menu* {::mf/private true ::mf/wrap [mf/memo]} - [{:keys [layout profile toggle-flag on-close toggle-theme toggle-render]}] - (let [renderer (or (-> profile :props :renderer) :svg) + [{:keys [layout profile toggle-flag on-close on-close-all toggle-theme toggle-render]}] + (let [renderer (or (-> profile :props :renderer) :svg) + cjk-feature? (features/use-feature "cjk-support") show-nudge-options (mf/use-fn - #(modal/show! {:type :nudge-option}))] + #(modal/show! {:type :nudge-option})) + + on-select-default-language + (mf/use-fn + (fn [language] + (st/emit! (du/update-profile-props {:default-text-language language}))))] [:> dropdown-menu* {:show true :class (stl/css :base-menu :sub-menu :pos-4) @@ -318,6 +400,13 @@ :id "file-menu-nudge"} [:span {:class (stl/css :item-name)} (tr "modals.nudge-title")]] + (when (and ^boolean cjk-feature? ^boolean (wm/vertical-layout-active?)) + [:> language-profile-menu* {:id "file-menu-default-language" + :label (tr "workspace.header.menu.option.default-language-profile") + :language (-> profile :props :default-text-language) + :on-select on-select-default-language + :on-close-all on-close-all}]) + [:> dropdown-menu-item* {:on-click toggle-theme :class (stl/css :base-menu-item :submenu-item) :on-key-down (fn [event] @@ -568,7 +657,7 @@ (mf/defc file-menu* {::mf/private true} - [{:keys [on-close file]}] + [{:keys [on-close on-close-all file]}] (let [file-id (:id file) shared? (:is-shared file) @@ -583,6 +672,14 @@ perms (mf/use-ctx ctx/permissions) can-edit (:can-edit perms) + cjk-feature? (features/use-feature "cjk-support") + file-language (mf/deref refs/file-text-language) + + on-select-language + (mf/use-fn + (fn [language] + (st/emit! (-> (dw/set-default-text-language language) + (vary-meta assoc ::ev/origin "workspace:menu"))))) on-remove-shared (mf/use-fn @@ -701,6 +798,14 @@ [:span {:class (stl/css :item-name)} (tr "dashboard.add-shared")]])) + (when (and ^boolean cjk-feature? ^boolean (wm/vertical-layout-active?)) + [:> language-profile-menu* {:id "file-menu-language" + :label (tr "workspace.header.menu.option.language") + :language file-language + :disabled (not can-edit) + :on-select on-select-language + :on-close-all on-close-all}]) + (when can-edit [:* [:div {:class (stl/css :separator)}] @@ -1134,7 +1239,8 @@ (case selected-sub-menu :file [:> file-menu* {:file file - :on-close close-sub-menu}] + :on-close close-sub-menu + :on-close-all close-all-menus}] :edit [:> edit-menu* {:on-close close-sub-menu}] @@ -1152,7 +1258,8 @@ :toggle-theme toggle-theme :toggle-render toggle-render :show-shortcuts show-shortcuts - :on-close close-sub-menu}] + :on-close close-sub-menu + :on-close-all close-all-menus}] :plugins [:> plugins-menu* {:open-plugins open-plugins-manager diff --git a/frontend/src/app/main/ui/workspace/main_menu.scss b/frontend/src/app/main/ui/workspace/main_menu.scss index 591e96b9c7..ff52f02492 100644 --- a/frontend/src/app/main/ui/workspace/main_menu.scss +++ b/frontend/src/app/main/ui/workspace/main_menu.scss @@ -194,3 +194,49 @@ background-color: var(--color-background-info); color: var(--color-foreground-primary); } + +// Language profile items (File, Preferences): the current profile shows +// before the arrow, and the profiles open beside the item. The padding +// bridges the gap to the item, so moving the pointer there keeps the list +// open. +.language-menu-item { + position: relative; + grid-template-columns: auto 1fr $sz-16; + grid-template-areas: "name value arrow"; + column-gap: var(--sp-s); + white-space: nowrap; +} + +.item-value { + grid-area: value; + justify-self: end; + color: var(--menu-shortcut-foreground-color); +} + +.language-submenu { + position: absolute; + inset-block-start: calc(-1 * (var(--sp-xs) + $b-2)); + inset-inline-start: 100%; + padding-inline-start: var(--sp-s); +} + +// The list sits inside the Language item, so it would inherit the colors +// that item sets on hover; it takes back the design-token defaults and its +// own items get their hover again. +.language-submenu-list { + --menu-background-color: var(--color-background-tertiary); + --menu-foreground-color: var(--color-foreground-primary); + --menu-icon-foreground-color: var(--color-foreground-secondary); + --menu-shortcut-foreground-color: var(--color-foreground-secondary); + + position: relative; + min-inline-size: $sz-192; + margin: 0; +} + +.language-separator { + block-size: 0; + margin-inline: calc(-1 * var(--sp-xs)); + border-top: $b-1 solid var(--color-background-quaternary); + list-style: none; +} diff --git a/frontend/src/app/main/ui/workspace/shapes/text/selection_menu.cljs b/frontend/src/app/main/ui/workspace/shapes/text/selection_menu.cljs new file mode 100644 index 0000000000..da9dab6dfd --- /dev/null +++ b/frontend/src/app/main/ui/workspace/shapes/text/selection_menu.cljs @@ -0,0 +1,516 @@ +;; 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 app.main.ui.workspace.shapes.text.selection-menu + "Menu of the text selected in the WASM text editor, with the CJK + annotations of the text language. It shows as a small icon at the top + right corner of the selection, like a comment pin, and opens into the + full toolbar on hover or click." + (:require-macros [app.main.style :as stl]) + (:require + [app.common.data.macros :as dm] + [app.common.types.text.cjk-layout :as cjk] + [app.main.data.workspace.texts :as dwt] + [app.main.data.workspace.texts-v3 :as dwt-v3] + [app.main.refs :as refs] + [app.main.store :as st] + [app.main.ui.ds.foundations.assets.icon :refer [icon*] :as i] + [app.main.ui.workspace.sidebar.options.menus.text-cjk-controls :as cjk-controls] + [app.render-wasm.text-editor :as wasm.text-editor] + [app.util.dom :as dom] + [app.util.i18n :refer [tr]] + [app.util.keyboard :as kbd] + [app.util.timers :as ts] + [cuerdas.core :as str] + [rumext.v2 :as mf])) + +;; Languages that show the menu. +(def ^:private menu-language-families + #{:japanese :chinese}) + +;; Contenteditable of the WASM text editor (see the v3 editor). +(def ^:private editor-input-id "text-editor-wasm-input") + +;; Icon of each `:text-emphasis` value. +(def ^:private emphasis-icons + {"none" i/remove + "filled-dot" i/text-emphasis-filled-dot + "open-dot" i/text-emphasis-open-dot + "filled-circle" i/text-emphasis-filled-circle + "open-circle" i/text-emphasis-open-circle + "filled-sesame" i/text-emphasis + "open-sesame" i/text-emphasis-open-sesame}) + +(defn- keep-editor-focus + "Keep the focus where it is when a press lands on the menu, so the editor + keeps its selection. The reading input takes the focus to type in it." + [event] + (when-not (= "INPUT" (.-tagName (dom/get-target event))) + (dom/prevent-default event))) + +(defn- focus-editor + [] + (some-> (dom/get-element editor-input-id) (dom/focus!))) + +(defn- composing-key? + "True for a key-down that an IME composition takes." + [event] + (let [native (dom/event->native-event event)] + (or (.-isComposing ^js native) + (= 229 (.-keyCode ^js native))))) + +(defn- escape-key? + "True for an Escape key-down that does not end an IME composition." + [event] + (and (kbd/esc? event) (not (composing-key? event)))) + +(defn- apply-text-attrs + "Apply span `attrs` to the text selected in the editor of `shape-id`, as + the design sidebar does." + [shape-id attrs] + (st/emit! (dwt-v3/v3-update-text-editor-styles shape-id attrs) + (dwt/update-all-attrs #{shape-id} attrs))) + +(defn- span-value + "Value of a span attr of the selection: nil when its spans differ." + [styles attr] + (let [value (get styles attr)] + (when-not (= :multiple value) value))) + +(defn annotated? + "True when the selection has a reading, emphasis marks, warichu or + combine (see `annotations` in `selection-menu*`)." + [{:keys [ruby emphasis warichu combine]}] + (or (not (str/blank? ruby)) + (and (some? emphasis) (not= "none" emphasis)) + (= "warichu" warichu) + (= "all" combine))) + +;; Time the toolbar stays open after the pointer leaves it. +(def ^:private collapse-delay 300) + +;; Room the compact icon needs above the selection; with less, the menu +;; shows below it. +(def ^:private icon-room 40) + +(mf/defc menu-item* + "Button with `children` as its icon, before the label, and a caret after + it when it opens a dropdown, open when `expanded` is true. `active` shows + that the selected text has the annotation; `pressed` also tells it to + assistive tech on toggles. A `disabled` item explains why in `hint`." + {::mf/private true} + [{:keys [label children on-click hint pressed + ^boolean dropdown ^boolean expanded ^boolean active ^boolean disabled]}] + [:button {:type "button" + :class (stl/css-case :menu-item true + :menu-item-active active) + :aria-haspopup (when dropdown "menu") + :aria-expanded (when dropdown expanded) + :aria-pressed pressed + :aria-disabled disabled + :title (when disabled hint) + :on-click (when-not disabled on-click)} + children + [:span {:class (stl/css :label)} label] + (when dropdown + [:> icon* {:icon-id i/arrow-down + :size "s" + :class (stl/css :caret) + :aria-hidden true}])]) + +(mf/defc glyph* + "A CJK character standing for an annotation that has no icon." + {::mf/private true} + [{:keys [glyph]}] + [:span {:class (stl/css :glyph) :aria-hidden true} glyph]) + +(mf/defc emphasis-list* + "The emphasis marks to choose from, `selected` checked." + {::mf/private true} + [{:keys [selected on-select]}] + (let [on-click + (mf/use-fn + (mf/deps on-select) + (fn [event] + (on-select (dom/get-data (dom/get-current-target event) "value"))))] + [:ul {:class (stl/css :emphasis-list) + :role "menu" + :aria-label (tr "workspace.options.text-options.text-emphasis")} + (for [{:keys [id label]} (cjk-controls/text-emphasis-options)] + (let [selected? (= id selected)] + [:li {:key id :role "none"} + [:button {:type "button" + :role "menuitemradio" + :aria-checked selected? + :class (stl/css-case :emphasis-option true + :emphasis-option-selected selected?) + :data-value id + :on-click on-click} + [:> icon* {:icon-id i/tick + :size "s" + :class (stl/css :emphasis-check) + :aria-hidden true}] + [:> icon* {:icon-id (get emphasis-icons id) + :class (stl/css-case :emphasis-mark true + :emphasis-mark-none (= id "none")) + :aria-hidden true}] + [:span {:class (stl/css :emphasis-label)} label]]]))])) + +(mf/defc toolbar* + "The annotations of the `family` language for the selected text of + `shape-id`. `annotations` holds what the text has (nil when its spans + differ). A reading takes text without annotations or a whole base + (`readable`); a warichu note, the text of one span (`warichu-enabled`)." + {::mf/private true} + [{:keys [shape-id family annotations on-ruby emphasis-open on-emphasis-open + ^boolean readable ^boolean ruby-enabled ^boolean warichu-enabled]}] + (let [{:keys [ruby emphasis warichu combine]} annotations + + warichu? (= "warichu" warichu) + combine? (= "all" combine) + ruby? (not (str/blank? ruby)) + + span-hint (tr "workspace.text-selection-menu.single-span-hint") + + toggle-emphasis + (mf/use-fn + (mf/deps emphasis-open on-emphasis-open) + #(on-emphasis-open (not emphasis-open))) + + open-ruby + (mf/use-fn + (mf/deps on-ruby on-emphasis-open) + (fn [] + (on-emphasis-open false) + (on-ruby))) + + select-emphasis + (mf/use-fn + (mf/deps shape-id on-emphasis-open) + (fn [value] + (on-emphasis-open false) + (apply-text-attrs shape-id {:text-emphasis value}))) + + toggle-warichu + (mf/use-fn + (mf/deps shape-id warichu? on-emphasis-open) + (fn [] + (on-emphasis-open false) + (apply-text-attrs shape-id {:warichu (if warichu? "none" "warichu")}))) + + toggle-combine + (mf/use-fn + (mf/deps shape-id combine? on-emphasis-open) + (fn [] + (on-emphasis-open false) + (apply-text-attrs shape-id {:text-combine-upright (if combine? "none" "all")})))] + + [:* + [:> menu-item* {:label (tr "workspace.text-selection-menu.ruby") + :active ruby? + :disabled (not ruby-enabled) + :hint (if readable + (tr "workspace.options.text-options.zhuyin-selection-hint") + (tr "workspace.text-selection-menu.reading-hint")) + :on-click open-ruby} + [:> glyph* {:glyph "あ"}]] + [:div {:class (stl/css :emphasis-anchor)} + [:> menu-item* {:label (tr "workspace.text-selection-menu.emphasis") + :dropdown true + :expanded emphasis-open + :active (and (some? emphasis) (not= "none" emphasis)) + :on-click toggle-emphasis} + [:> icon* {:icon-id i/text-emphasis :aria-hidden true}]] + (when emphasis-open + [:> emphasis-list* {:selected emphasis + :on-select select-emphasis}])] + ;; Chinese layout renders neither warichu nor tate-chu-yoko. + (when (= family :japanese) + [:* + [:hr {:class (stl/css :separator)}] + [:> menu-item* {:label (tr "workspace.text-selection-menu.warichu") + :active warichu? + :pressed warichu? + :disabled (not warichu-enabled) + :hint span-hint + :on-click toggle-warichu} + [:> icon* {:icon-id i/warichu :aria-hidden true}]] + [:> menu-item* {:label (tr "workspace.text-selection-menu.combine") + :active combine? + :pressed combine? + :on-click toggle-combine} + [:> icon* {:icon-id i/text-combine-upright-all :aria-hidden true}]]])])) + +(mf/defc ruby-input* + "Input for the ruby reading of the selected text, starting from its + current `reading`. Enter applies it, Remove drops the current one, and + Escape and the back button return to the toolbar. A `validate` fn rejects + the readings it returns false for." + {::mf/private true} + [{:keys [reading validate invalid-hint on-apply on-back more-open on-more]}] + (let [reading* (mf/use-state (or reading "")) + value (deref reading*) + valid? (or (str/blank? value) (nil? validate) (validate (str/trim value))) + + ;; True when the pending Escape key-up ends a real Escape press, not + ;; an IME composition. + escape-ref (mf/use-ref false) + + on-change + (mf/use-fn + (fn [event] + (reset! reading* (dom/get-value (dom/get-target event))))) + + on-key-down + (mf/use-fn + (mf/deps value valid? on-apply) + (fn [event] + (cond + (kbd/esc? event) + (mf/set-ref-val! escape-ref (escape-key? event)) + + (and (kbd/enter? event) (not (composing-key? event))) + (do + (dom/prevent-default event) + (when valid? + (on-apply (str/trim value))))))) + + ;; The editor exits on an Escape key-up reaching the document, so the + ;; input keeps every Escape key-up to itself. + on-key-up + (mf/use-fn + (mf/deps on-back) + (fn [event] + (when (kbd/esc? event) + (dom/stop-propagation event) + (when (mf/ref-val escape-ref) + (mf/set-ref-val! escape-ref false) + (on-back))))) + + on-remove + (mf/use-fn + (mf/deps on-apply) + #(on-apply ""))] + + [:* + [:button {:type "button" + :class (stl/css :back-button) + :aria-label (tr "workspace.text-selection-menu.back") + :on-click on-back} + [:> icon* {:icon-id i/arrow-left :aria-hidden true}] + [:span {:class (stl/css :label)} (tr "workspace.text-selection-menu.ruby")]] + [:input {:type "text" + :class (stl/css :reading-input) + :aria-label (tr "workspace.text-selection-menu.reading") + :aria-invalid (not valid?) + :title (when-not valid? invalid-hint) + :auto-focus true + :value value + :on-change on-change + :on-key-down on-key-down + :on-key-up on-key-up}] + (when-not (str/blank? reading) + [:button {:type "button" + :class (stl/css :remove-button) + :on-click on-remove} + [:span {:class (stl/css :label)} (tr "workspace.text-selection-menu.remove")]]) + [:button {:type "button" + :class (stl/css :more-button) + :aria-label (tr "workspace.text-selection-menu.ruby-options") + :aria-expanded more-open + :data-testid "selection-menu-ruby-options" + :on-click on-more} + [:> icon* {:icon-id i/menu + :class (stl/css :more-icon) + :aria-hidden true}]]])) + +(mf/defc ruby-options* + "Size, alignment, overhang and side of the reading of the selection." + {::mf/private true} + [{:keys [shape-id values vertical]}] + (let [on-change (mf/use-fn + (mf/deps shape-id) + (fn [attrs] (apply-text-attrs shape-id attrs)))] + [:div {:class (stl/css :ruby-options)} + [:> cjk-controls/ruby-presentation-fields* {:values values + :vertical vertical + :on-change on-change}]])) + +(mf/defc menu* + "The compact icon at the top right corner of the selection, which opens + into the toolbar on hover or click, anchored at the same corner. Without + room above the selection both show below it. The toolbar collapses once + the pointer leaves it, unless one of its options is open (the reading + input or the emphasis list): those close with Enter, Escape or a new + selection." + {::mf/private true} + [{:keys [shape-id rect vbox zoom family annotations ruby-values zhuyin vertical + ^boolean readable ^boolean ruby-enabled ^boolean warichu-enabled]}] + (let [view* (mf/use-state :toolbar) + view (deref view*) + expanded* (mf/use-state false) + expanded? (deref expanded*) + emphasis-open* (mf/use-state false) + emphasis-open (deref emphasis-open*) + more-open* (mf/use-state false) + more-open (deref more-open*) + timer-ref (mf/use-ref nil) + + engaged? (or (= view :ruby) emphasis-open) + open? (or expanded? engaged?) + + right (* (- (+ (:x rect) (:width rect)) (:x vbox)) zoom) + top (* (- (:y rect) (:y vbox)) zoom) + bottom (* (- (+ (:y rect) (:height rect)) (:y vbox)) zoom) + below? (< top icon-room) + style (mf/with-memo [right top bottom below?] + #js {:left (dm/str right "px") + :top (dm/str (if below? bottom top) "px")}) + + cancel-collapse + (mf/use-fn + (fn [] + (when-let [timer (mf/ref-val timer-ref)] + (ts/dispose! timer) + (mf/set-ref-val! timer-ref nil)))) + + expand + (mf/use-fn + (mf/deps cancel-collapse) + (fn [] + (cancel-collapse) + (reset! expanded* true))) + + schedule-collapse + (mf/use-fn + (mf/deps cancel-collapse) + (fn [] + (cancel-collapse) + (mf/set-ref-val! timer-ref + (ts/schedule collapse-delay #(reset! expanded* false))))) + + open-ruby + (mf/use-fn #(reset! view* :ruby)) + + close-ruby + (mf/use-fn + (fn [] + (reset! view* :toolbar) + (reset! more-open* false) + (focus-editor))) + + toggle-more + (mf/use-fn #(swap! more-open* not)) + + apply-ruby + (mf/use-fn + (mf/deps shape-id close-ruby) + (fn [reading] + (apply-text-attrs shape-id {:ruby reading}) + (close-ruby))) + + selection-key (dm/str (:x rect) ":" (:y rect) ":" (:width rect) ":" (:height rect))] + + ;; A new selection closes what the previous one opened. + (mf/with-effect [selection-key] + (reset! view* :toolbar) + (reset! emphasis-open* false) + (reset! more-open* false) + (reset! expanded* false)) + + (mf/with-effect [cancel-collapse] + cancel-collapse) + + [:div {:class (stl/css-case :selection-anchor true + :selection-anchor-below below?) + :style style + ;; Focus moving into the menu keeps the editor alive (see + ;; `keep-editing-on-blur?` in the v3 editor). + :data-keep-editing-on-blur true + :on-mouse-down keep-editor-focus + :on-pointer-enter expand + :on-pointer-leave schedule-collapse} + (if ^boolean open? + [:div {:class (stl/css-case :selection-menu true + :ruby-view (= view :ruby)) + :role "toolbar" + :aria-label (tr "workspace.text-selection-menu.title")} + (case view + :ruby [:* + [:> ruby-input* {:reading (:ruby annotations) + :validate (when zhuyin cjk/zhuyin-reading?) + :invalid-hint (tr "workspace.options.text-options.zhuyin-invalid") + :more-open more-open + :on-more toggle-more + :on-apply apply-ruby + :on-back close-ruby}] + (when ^boolean more-open + [:> ruby-options* {:shape-id shape-id + :values ruby-values + :vertical vertical}])] + [:> toolbar* {:shape-id shape-id + :family family + :annotations annotations + :readable readable + :ruby-enabled ruby-enabled + :warichu-enabled warichu-enabled + :emphasis-open emphasis-open + :on-emphasis-open #(reset! emphasis-open* %) + :on-ruby open-ruby}])] + [:button {:type "button" + :class (stl/css :compact-trigger) + :aria-label (tr "workspace.text-selection-menu.title") + :aria-haspopup "true" + :aria-expanded false + :data-testid "selection-menu-trigger" + :on-click expand} + [:span {:class (stl/css :compact-glyph) :aria-hidden true} "文"] + (when (annotated? annotations) + [:span {:class (stl/css :compact-dot) + :data-testid "selection-menu-dot"}])])])) + +(mf/defc selection-menu* + "Shows the menu over the text selected in the editor of `shape` when the + text renders with a Japanese or Chinese layout." + [{:keys [shape vbox zoom]}] + (let [shape-id (:id shape) + rects (mf/deref refs/workspace-wasm-editor-selection-rects) + styles-map (mf/deref refs/workspace-wasm-editor-styles) + rect (get rects shape-id) + family (cjk/language-family (cjk/content-language (:content shape)))] + (when (and (some? rect) (contains? menu-language-families family)) + (let [styles (get styles-map shape-id) + emphasis (get styles :text-emphasis) + annotations {:ruby (span-value styles :ruby) + ;; Spans without a mark store none. + :emphasis (cond + (nil? emphasis) "none" + (= :multiple emphasis) nil + :else emphasis) + :warichu (span-value styles :warichu) + :combine (span-value styles :text-combine-upright)} + content (wasm.text-editor/get-cached-content shape-id) + selection (wasm.text-editor/text-editor-get-selection) + ;; A reading joins the spans it covers into one base; a warichu + ;; note stays in one span. + readable (wasm.text-editor/reading-selection? content selection) + ;; Zhuyin annotates one Han character at a time. + zhuyin (and (= family :chinese) (true? (:ruby-zhuyin styles))) + ruby-values (select-keys styles [:ruby-size :ruby-align :ruby-overhang :ruby-side])] + [:> menu* {:shape-id shape-id + :rect rect + :vbox vbox + :zoom zoom + :family family + :annotations annotations + :ruby-values ruby-values + :vertical (cjk/stored-vertical? (:content shape)) + :zhuyin zhuyin + :readable readable + :ruby-enabled (and readable + (or (not zhuyin) + (wasm.text-editor/zhuyin-selection? content selection))) + :warichu-enabled (wasm.text-editor/single-span-selection? content selection)}])))) diff --git a/frontend/src/app/main/ui/workspace/shapes/text/selection_menu.scss b/frontend/src/app/main/ui/workspace/shapes/text/selection_menu.scss new file mode 100644 index 0000000000..7284e779bc --- /dev/null +++ b/frontend/src/app/main/ui/workspace/shapes/text/selection_menu.scss @@ -0,0 +1,285 @@ +// 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 + +@use "ds/_borders.scss" as *; +@use "ds/_sizes.scss" as *; +@use "ds/_utils.scss" as *; +@use "ds/typography.scss" as *; + +%surface { + background-color: var(--color-background-tertiary); + border: $b-1 solid var(--color-background-quaternary); + border-radius: $br-8; + box-shadow: 0 px2rem(2) px2rem(8) 0 var(--color-shadow-light); +} + +%plain-button { + appearance: none; + display: flex; + align-items: center; + block-size: $sz-32; + border: none; + border-radius: $br-6; + background: transparent; + cursor: pointer; + + &:hover { + background-color: var(--color-background-quaternary); + } + + &:focus-visible { + outline: $b-1 solid var(--color-accent-primary); + } +} + +// Its left and top, set inline, are the top right corner of the selection +// (the bottom right one with `.selection-anchor-below`). The icon and the +// toolbar grow from that corner, up or down. +.selection-anchor { + --selection-menu-margin: var(--sp-xs); + + position: absolute; + inline-size: 0; + block-size: 0; + pointer-events: auto; +} + +.compact-trigger { + @extend %surface; + + position: absolute; + inset-block-end: var(--selection-menu-margin); + inset-inline-start: var(--selection-menu-margin); + display: flex; + align-items: center; + justify-content: center; + inline-size: $sz-24; + block-size: $sz-24; + padding: 0; + + // Round but for the corner that points at the selection. + border-radius: $br-12 $br-12 $br-12 px2rem(2); + color: var(--color-foreground-primary); + cursor: pointer; + + &:hover, + &:focus-visible { + border-color: var(--color-accent-primary); + outline: none; + } +} + +.compact-glyph { + @include use-typography("body-small"); + + line-height: 1; +} + +// The selection has annotations. +.compact-dot { + position: absolute; + inset-block-start: calc(-1 * var(--sp-xxs)); + inset-inline-end: calc(-1 * var(--sp-xxs)); + inline-size: px2rem(8); + block-size: px2rem(8); + border: $b-1 solid var(--color-background-tertiary); + border-radius: $br-circle; + background-color: var(--color-accent-primary); +} + +.selection-menu { + @extend %surface; + + position: absolute; + inset-block-end: var(--selection-menu-margin); + inset-inline-start: var(--selection-menu-margin); + display: flex; + align-items: center; + column-gap: var(--sp-xxs); + padding: var(--sp-xs); + white-space: nowrap; + pointer-events: auto; +} + +.ruby-view { + column-gap: var(--sp-s); +} + +.menu-item { + @extend %plain-button; + + column-gap: px2rem(6); + padding: 0 var(--sp-s); + color: var(--color-foreground-primary); + + &[aria-expanded="true"] { + background-color: var(--color-background-quaternary); + } + + &[aria-disabled="true"] { + color: var(--color-foreground-secondary); + cursor: default; + + &:hover { + background: transparent; + } + } +} + +// The selected text has the annotation. +.menu-item-active { + background-color: var(--color-background-quaternary); + color: var(--color-accent-primary); +} + +.label { + @include use-typography("body-small"); +} + +.glyph { + @include use-typography("body-medium"); + + inline-size: $sz-16; + text-align: center; +} + +.caret { + color: var(--color-icon-default); +} + +.separator { + inline-size: $b-1; + block-size: px2rem(20); + margin: 0; + border: none; + background-color: var(--color-background-quaternary); +} + +// The list hangs below the Emphasis button, past the menu padding and border. +.emphasis-anchor { + position: relative; +} + +.emphasis-list { + @extend %surface; + + position: absolute; + inset-block-start: calc(100% + 2 * var(--sp-xs) + #{$b-1}); + inset-inline-start: 0; + display: flex; + flex-direction: column; + inline-size: $sz-200; + margin: 0; + padding: var(--sp-xs); + list-style: none; +} + +.emphasis-option { + @extend %plain-button; + + column-gap: var(--sp-s); + inline-size: 100%; + padding: 0 var(--sp-m); + color: var(--color-foreground-primary); +} + +.emphasis-check { + color: var(--color-accent-primary); + visibility: hidden; +} + +.emphasis-mark-none { + color: var(--color-foreground-secondary); +} + +.emphasis-label { + @include use-typography("body-small"); +} + +.emphasis-option-selected { + .emphasis-check { + visibility: visible; + } + + .emphasis-label { + color: var(--color-accent-primary); + } +} + +.back-button { + @extend %plain-button; + + column-gap: var(--sp-xs); + padding: 0 var(--sp-xs); + color: var(--color-foreground-secondary); +} + +.reading-input { + @include use-typography("body-medium"); + + inline-size: px2rem(180); + block-size: $sz-32; + margin: 0; + padding: 0 var(--sp-s); + border: $b-1 solid transparent; + border-radius: $br-6; + background-color: var(--color-background-primary); + color: var(--color-foreground-primary); + caret-color: var(--color-accent-primary); + + &:focus { + border-color: var(--color-accent-primary); + outline: none; + } + + &[aria-invalid="true"] { + border-color: var(--color-accent-error); + } +} + +.remove-button { + @extend %plain-button; + + padding: 0 var(--sp-s); + color: var(--color-foreground-primary); +} + +.more-button { + @extend %plain-button; + + padding: 0 var(--sp-s); + color: var(--color-foreground-secondary); +} + +// The menu icon is vertical; the design shows it lying down. +.more-icon { + rotate: 90deg; +} + +.selection-anchor-below { + .compact-trigger, + .selection-menu { + inset-block: var(--selection-menu-margin) auto; + } + + .compact-trigger { + border-radius: px2rem(2) $br-12 $br-12; + } +} + +// The presentation options of the reading, below the input. +.ruby-options { + @extend %surface; + + position: absolute; + inset-block-start: calc(100% + var(--sp-xs)); + inset-inline-start: 0; + display: flex; + flex-direction: column; + gap: var(--sp-xs); + inline-size: px2rem(280); + padding: var(--sp-s); +} diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/common.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/common.cljs index ad8bcc2a9c..10b430c7a3 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/common.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/common.cljs @@ -7,6 +7,7 @@ (ns app.main.ui.workspace.sidebar.options.common (:require-macros [app.main.style :as stl]) (:require + [app.common.data :as d] [app.main.data.workspace.tokens.application :as dwta] [app.main.store :as st] [app.util.dom :as dom] @@ -26,6 +27,20 @@ :ref ref} children]))) +(defn radio-selected + "Value to select in a radio group for an attribute value: its name, or + `default` when unset. Mixed (`:multiple`) values select nothing." + ([value] + (radio-selected value "")) + ([value default] + (cond + (= value :multiple) "" + (or (nil? value) + (and (string? value) (empty? value))) default + (keyword? value) (d/name value) + (string? value) value + :else (str value)))) + (defn emit-value-or-token [value emit-value-fn ids attrs] (if (or (string? value) (number? value) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs index 3f56fbbc23..0241b0680c 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.cljs @@ -23,10 +23,9 @@ [app.main.features :as features] [app.main.refs :as refs] [app.main.store :as st] - [app.main.ui.components.dropdown :refer [dropdown]] + [app.main.ui.components.grouped-dropdown :refer [grouped-dropdown*]] [app.main.ui.components.numeric-input :as deprecated-input] [app.main.ui.components.radio-buttons :refer [radio-button radio-buttons]] - [app.main.ui.components.search-bar :refer [search-bar*]] [app.main.ui.ds.buttons.icon-button :refer [icon-button*]] [app.main.ui.ds.foundations.assets.icon :refer [icon*] :as i] [app.main.ui.icons :as deprecated-icon] @@ -136,6 +135,20 @@ acc)))) acc))))) +(defn size-preset-options + "Rows of the size presets dropdown: a preset without `:width` heads the + presets that follow it. A preset is selected when it has the shape size." + [presets width height] + (mapv (fn [preset] + (let [preset-name (:name preset)] + (if (:width preset) + {:id preset-name + :label preset-name + :detail (str (:width preset) " x " (:height preset)) + :selected (and (= width (:width preset)) (= height (:height preset)))} + {:id preset-name :label preset-name :header true}))) + presets)) + (defn check-measures-menu-props [old-props new-props] (let [o-values (unchecked-get old-props "values") @@ -366,16 +379,18 @@ (mf/with-memo [preset-search-term] (filter-size-presets preset-search-term size-presets)) + preset-width (d/parse-integer (:width values) 0) + preset-height (d/parse-integer (:height values) 0) + + preset-options + (mf/with-memo [filtered-size-presets preset-width preset-height] + (size-preset-options filtered-size-presets preset-width preset-height)) + on-preset-selected (mf/use-fn (mf/deps ids) - (fn [event] - (let [width (-> (dom/get-current-target event) - (dom/get-data "width") - (d/read-string)) - height (-> (dom/get-current-target event) - (dom/get-data "height") - (d/read-string))] + (fn [preset-name _event] + (let [{:keys [width height]} (d/seek #(= preset-name (:name %)) size-presets)] (st/emit! (udw/update-dimensions ids :width width) (udw/update-dimensions ids :height height)) (reset! preset-state* false) @@ -498,42 +513,15 @@ [:span {:class (stl/css :select-name)} (tr "workspace.options.size-presets")] [:span {:class (stl/css :collapsed-icon)} deprecated-icon/arrow] - [:& dropdown {:show show-presets-dropdown? - :on-close close-presets - :container preset-container-ref} - [:div {:class (stl/css :custom-select-dropdown) - :on-click dom/stop-propagation} - [:div {:class (stl/css :preset-search)} - [:> search-bar* {:on-change on-preset-search-change - :value preset-search-term - :auto-focus true - :placeholder (tr "workspace.options.search-size-preset")}]] - [:ul {:class (stl/css :preset-list)} - (if (empty? filtered-size-presets) - [:li {:class (stl/css-case :dropdown-element true - :disabled true)} - [:span {:class (stl/css :preset-name)} - (tr "workspace.options.no-size-preset-results")]] - (for [size-preset filtered-size-presets] - (if-not (:width size-preset) - [:li {:key (:name size-preset) - :class (stl/css-case :dropdown-element true - :disabled true)} - [:span {:class (stl/css :preset-name)} (:name size-preset)]] - - (let [preset-match (and (= (:width size-preset) (d/parse-integer (:width values) 0)) - (= (:height size-preset) (d/parse-integer (:height values) 0)))] - [:li {:key (:name size-preset) - :class (stl/css-case :dropdown-element true - :match preset-match) - :data-width (str (:width size-preset)) - :data-height (str (:height size-preset)) - :on-click on-preset-selected} - [:div {:class (stl/css :name-wrapper)} - [:span {:class (stl/css :preset-name)} (:name size-preset)] - [:span {:class (stl/css :preset-size)} (:width size-preset) " x " (:height size-preset)]] - (when preset-match - [:span {:class (stl/css :check-icon)} deprecated-icon/tick])]))))]]]] + [:> grouped-dropdown* {:show show-presets-dropdown? + :on-close close-presets + :container preset-container-ref + :options preset-options + :on-select on-preset-selected + :search-term preset-search-term + :search-placeholder (tr "workspace.options.search-size-preset") + :on-search-change on-preset-search-change + :empty-label (tr "workspace.options.no-size-preset-results")}]] [:& radio-buttons {:selected (or (d/name orientation) "") :on-change on-orientation-change diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.scss b/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.scss index 8fa48d1bb0..d74103971f 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.scss +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/measures.scss @@ -69,90 +69,6 @@ cursor: pointer; } -.custom-select-dropdown { - @extend %dropdown-wrapper; - - margin-top: deprecated.$s-2; - max-height: 70vh; - width: deprecated.$s-252; - display: flex; - flex-direction: column; - overflow: hidden; -} - -.preset-search { - padding: deprecated.$s-4; - border-bottom: deprecated.$s-1 solid var(--menu-border-color-rest, transparent); -} - -.preset-list { - flex: 1 1 auto; - min-height: 0; - overflow-y: auto; - margin: 0; - padding: 0; - list-style: none; - - .dropdown-element { - @extend %dropdown-element-base; - - .name-wrapper { - display: flex; - gap: deprecated.$s-8; - flex-grow: 1; - - .preset-name { - color: var(--menu-foreground-color-rest); - } - - .preset-size { - color: var(--menu-foreground-color-rest); - } - } - - .check-icon { - @include deprecated.flex-center; - - svg { - @extend %button-icon-small; - - stroke: var(--icon-foreground); - } - } - - &.disabled { - pointer-events: none; - cursor: default; - - .preset-name { - color: var(--menu-foreground-color); - } - } - - &.match { - .name-wrapper .preset-name { - color: var(--menu-foreground-color-hover); - } - - .check-icon svg { - stroke: var(--menu-foreground-color-hover); - } - } - - &:hover { - background-color: var(--menu-background-color-hover); - - .name-wrapper .preset-name { - color: var(--menu-foreground-color-hover); - } - - .check-icon svg { - stroke: var(--menu-foreground-color-hover); - } - } - } -} - .size, .position, .rotation-radius { diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/text.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text.cljs index 034d209ac1..dd272ca78d 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/text.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text.cljs @@ -9,6 +9,7 @@ (:require [app.common.data :as d] [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk] [app.common.uuid :as uuid] [app.config :as cf] [app.main.data.workspace.libraries :as dwl] @@ -30,6 +31,9 @@ [app.main.ui.ds.controls.shared.searchable-options-dropdown :refer [searchable-options-dropdown*]] [app.main.ui.ds.foundations.assets.icon :as i] [app.main.ui.hooks :as hooks] + [app.main.ui.workspace.sidebar.options.common :refer [radio-selected]] + [app.main.ui.workspace.sidebar.options.menus.text-cjk-controls :as tcc] + [app.main.ui.workspace.sidebar.options.menus.text-language-layout :as tll] [app.main.ui.workspace.sidebar.options.menus.token-typography-row :refer [token-typography-row*]] [app.main.ui.workspace.sidebar.options.menus.typography :refer [text-options* typography-entry*]] [app.main.ui.workspace.tokens.management.forms.controls.utils :as csu] @@ -38,6 +42,7 @@ [app.util.object :as obj] [app.util.text.content :as content] [app.util.text.ui :as txu] + [app.util.text.writing-mode :as wm] [app.util.timers :as ts] [beicon.v2.core :as rx] [potok.v2.core :as ptk] @@ -55,30 +60,33 @@ (def ^:private text-lists-flag-enabled? (contains? cf/flags :text-editor-wasm-v1-lists)) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Sub-components -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - (mf/defc text-align-options* [{:keys [values on-change on-blur]}] - (let [options - (mf/with-memo [] + (let [vertical? (wm/vertical-values? values) + options + (mf/with-memo [vertical?] [{:value "left" :id "text-align-left" - :label (tr "workspace.options.text-options.text-align-left") - :icon i/text-align-left} + :label (if vertical? + (tr "workspace.options.text-options.align-top") + (tr "workspace.options.text-options.text-align-left")) + :icon (if vertical? i/text-align-top i/text-align-left)} {:value "center" :id "text-align-center" - :label (tr "workspace.options.text-options.text-align-center") - :icon i/text-align-center} + :label (if vertical? + (tr "workspace.options.text-options.align-middle") + (tr "workspace.options.text-options.text-align-center")) + :icon (if vertical? i/text-align-middle i/text-align-center)} {:value "right" :id "text-align-right" - :label (tr "workspace.options.text-options.text-align-right") - :icon i/text-align-right} + :label (if vertical? + (tr "workspace.options.text-options.align-bottom") + (tr "workspace.options.text-options.text-align-right")) + :icon (if vertical? i/text-align-bottom i/text-align-right)} {:value "justify" :id "text-align-justify" :label (tr "workspace.options.text-options.text-align-justify") - :icon i/text-justify}]) + :icon (if vertical? i/text-justify-vertical i/text-justify)}]) handle-change (mf/use-fn @@ -88,14 +96,15 @@ (when (some? on-blur) (on-blur))))] [:div {:class (stl/css :align-options)} - [:> radio-buttons* {:selected (:text-align values) + [:> radio-buttons* {:selected (radio-selected (:text-align values)) :on-change handle-change :name "align-text-options" :options options}]])) (mf/defc text-direction-options* [{:keys [values on-change on-blur]}] - (let [direction (:text-direction values) + (let [vertical? (wm/vertical-values? values) + direction (radio-selected (:text-direction values)) options (mf/with-memo [] [{:value "ltr" @@ -114,29 +123,37 @@ (on-change {:text-direction (if (= value direction) "none" value)}) (when (some? on-blur) (on-blur))))] - [:div {:class (stl/css :text-direction-options)} - [:> radio-buttons* {:selected direction - :on-change handle-change - :name "text-direction-options" - :options options}]])) + (when-not vertical? + [:div {:class (stl/css :text-direction-options)} + [:> radio-buttons* {:selected direction + :on-change handle-change + :name "text-direction-options" + :options options}]]))) (mf/defc vertical-align* [{:keys [values on-change on-blur]}] - (let [vertical-align (or (:vertical-align values) "top") + (let [vertical? (wm/vertical-values? values) + vertical-align (radio-selected (:vertical-align values) "top") options - (mf/with-memo [] + (mf/with-memo [vertical?] [{:value "top" :id "vertical-text-align-top" - :label (tr "workspace.options.text-options.align-top") - :icon i/text-top} + :label (if vertical? + (tr "workspace.options.text-options.text-align-right") + (tr "workspace.options.text-options.align-top")) + :icon (if vertical? i/text-right i/text-top)} {:value "center" :id "vertical-text-align-center" - :label (tr "workspace.options.text-options.align-middle") - :icon i/text-middle} + :label (if vertical? + (tr "workspace.options.text-options.text-align-center") + (tr "workspace.options.text-options.align-middle")) + :icon (if vertical? i/text-horizontal-center i/text-middle)} {:value "bottom" :id "vertical-text-align-bottom" - :label (tr "workspace.options.text-options.align-bottom") - :icon i/text-bottom}]) + :label (if vertical? + (tr "workspace.options.text-options.text-align-left") + (tr "workspace.options.text-options.align-bottom")) + :icon (if vertical? i/text-left i/text-bottom)}]) handle-change (mf/use-fn @@ -153,22 +170,28 @@ (mf/defc grow-options* [{:keys [ids values on-blur]}] - (let [grow-type (:grow-type values) + (let [vertical? (wm/vertical-values? values) + grow-type (:grow-type values) + selected (radio-selected grow-type) editor-instance (mf/deref refs/workspace-editor) options - (mf/with-memo [] + (mf/with-memo [vertical?] [{:value "fixed" :id "text-fixed-grow" :label (tr "workspace.options.text-options.grow-fixed") :icon i/text-fixed} {:value "auto-width" :id "text-auto-width-grow" - :label (tr "workspace.options.text-options.grow-auto-width") - :icon i/text-auto-width} + :label (if vertical? + (tr "workspace.options.text-options.grow-auto-height") + (tr "workspace.options.text-options.grow-auto-width")) + :icon (if vertical? i/text-auto-width-vertical i/text-auto-width)} {:value "auto-height" :id "text-auto-height-grow" - :label (tr "workspace.options.text-options.grow-auto-height") - :icon i/text-auto-height}]) + :label (if vertical? + (tr "workspace.options.text-options.grow-auto-width") + (tr "workspace.options.text-options.grow-auto-height")) + :icon (if vertical? i/text-auto-height-vertical i/text-auto-height)}]) handle-change (mf/use-fn @@ -194,14 +217,14 @@ (when (some? on-blur) (on-blur))))] [:div {:class (stl/css :grow-options)} - [:> radio-buttons* {:selected (d/name grow-type) + [:> radio-buttons* {:selected selected :on-change handle-change :name "grow-text-options" :options options}]])) (mf/defc text-decoration-options* [{:keys [values on-change on-blur token-applied]}] - (let [text-decoration (some-> (:text-decoration values) d/name) + (let [text-decoration (radio-selected (:text-decoration values)) options (mf/with-memo [token-applied] [{:value "underline" @@ -225,8 +248,8 @@ [:div {:class (stl/css :text-decoration-options)} [:> radio-buttons* {:selected (if (= text-decoration "none") - nil - text-decoration) + "" + (radio-selected text-decoration)) :on-change handle-change :name "text-decoration-options" :disabled (and token-typography-row-enabled? (some? token-applied)) @@ -279,9 +302,19 @@ (->> (:typography tokens) (d/seek #(= (:id %) (uuid/uuid id))))) +;; CJK attrs the text options show: language, writing mode, readings and +;; the other span, paragraph and root attrs. +(def ^:private cjk-option-attrs + (d/concat-vec cjk/language-paragraph-attrs + cjk/vertical-writing-attrs + cjk/span-attrs + [:line-adjustment])) + (defn- check-props [n-props o-props] - (let [o-values (unchecked-get o-props "values") - n-values (unchecked-get n-props "values")] + (let [o-values (unchecked-get o-props "values") + n-values (unchecked-get n-props "values") + o-ruby-values (unchecked-get o-props "rubyValues") + n-ruby-values (unchecked-get n-props "rubyValues")] (and (identical? (unchecked-get n-props "ids") (unchecked-get o-props "ids")) (identical? (unchecked-get n-props "type") @@ -292,6 +325,21 @@ (unchecked-get o-props "fileId")) (identical? (unchecked-get n-props "typographies") (unchecked-get o-props "typographies")) + (identical? (unchecked-get n-props "textSelectionActive") + (unchecked-get o-props "textSelectionActive")) + (identical? (unchecked-get n-props "readingSelectionActive") + (unchecked-get o-props "readingSelectionActive")) + (identical? (unchecked-get n-props "languageView") + (unchecked-get o-props "languageView")) + (identical? (unchecked-get n-props "onOpenLanguageView") + (unchecked-get o-props "onOpenLanguageView")) + (identical? (unchecked-get n-props "onCloseLanguageView") + (unchecked-get o-props "onCloseLanguageView")) + (identical? (unchecked-get n-props "hasRuby") + (unchecked-get o-props "hasRuby")) + (= n-ruby-values o-ruby-values) + (= (unchecked-get o-props "zhuyinSelectionActive") + (unchecked-get n-props "zhuyinSelectionActive")) (identical? (get o-values :fills) (get n-values :fills)) (identical? (get o-values :font-family) @@ -327,15 +375,23 @@ (identical? (get o-values :typography-ref-id) (get n-values :typography-ref-id)) (identical? (get o-values :vertical-align) - (get n-values :vertical-align))))) + (get n-values :vertical-align)) + (every? #(identical? (get o-values %) (get n-values %)) cjk-option-attrs)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Main component ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (mf/defc text-menu* + "Text section of the design tab, followed by the Language panel. With + `language-view` it shows only the options of the text language, in their + own view (see `tll/language-view*`): `on-open-language-view` opens it, + `on-close-language-view` leaves it. Without `on-open-language-view` the + Language panel offers no options." {::mf/wrap [#(mf/memo' % check-props)]} - [{:keys [ids type values applied-tokens libraries file-id typographies]}] + [{:keys [ids type values ruby-values text-selection-active reading-selection-active zhuyin-selection-active + applied-tokens libraries file-id typographies + language-view on-open-language-view on-close-language-view has-ruby]}] (let [;; --- UI state menu-state* (mf/use-state {:main-menu true @@ -343,6 +399,7 @@ menu-state (deref menu-state*) main-menu-open? (:main-menu menu-state) more-options-open? (:more-options menu-state) + cjk-feature? (features/use-feature "cjk-support") font-id (or (:font-id values) (:font-id txt/default-typography)) @@ -467,6 +524,14 @@ (fn [attrs] (emit-update! ids attrs))) + on-ruby-presentation-change + (mf/use-fn + (mf/deps ids on-change text-selection-active) + (fn [attrs] + (if text-selection-active + (on-change attrs) + (st/emit! (dwt/update-all-ruby-presentation ids attrs))))) + on-convert-to-typography (mf/use-fn (mf/deps values ids file-id emit-update!) @@ -525,7 +590,19 @@ :values values :on-change on-change :show-recent true - :on-blur on-text-blur})] + :on-blur on-text-blur}) + + language-layout-props + (mf/props + {:ids ids + :values values + :ruby-values ruby-values + :text-selection-active text-selection-active + :reading-selection-active reading-selection-active + :zhuyin-selection-active zhuyin-selection-active + :on-change on-change + :on-ruby-presentation-change on-ruby-presentation-change + :on-blur on-text-blur})] (hooks/use-stream expand-stream @@ -543,100 +620,117 @@ (when token-dropdown-open? (ts/schedule 0 #(some-> (mf/ref-val dropdown-ref) dom/focus!)))) - [:section {:class (stl/css :element-set) - ;; Focusing these controls must not exit the v3 text editor (see `keep-editing-on-blur?`). - :data-keep-editing-on-blur true - :aria-label (tr "workspace.options.text-options.text-section")} - [:div {:class (stl/css :element-title)} - [:> title-bar* {:collapsable true - :collapsed (not main-menu-open?) - :on-collapsed toggle-main-menu - :title label - :class (stl/css :title-spacing-text)} - [:* - (when (and token-typography-row-enabled? (some? (resolve-delay typography-tokens)) (not typography)) - [:> icon-button* {:variant "ghost" - :aria-label (tr "ds.inputs.numeric-input.open-token-list-dropdown") - :on-click toggle-token-dropdown - :tooltip-placement "top-left" - :icon i/tokens}]) - (when (and (not typography) (not multiple?) (not applied-token-name)) - [:> icon-button* {:variant "ghost" - :aria-label (if (not font) - (tr "workspace.options.font-not-available" (:font-family values)) - (tr "workspace.options.convert-to-typography")) - :on-click on-convert-to-typography - :tooltip-placement "top-left" - :disabled (not font) - :icon i/add}])]] - (when (and token-typography-row-enabled? token-dropdown-open?) - [:> searchable-options-dropdown* {:on-click on-option-click - :id listbox-id - :options (resolve-delay dropdown-options) - :selected selected-token-id - :align "right" - :placeholder (tr "workspace.tokens.search-by-token") - :ref set-option-ref}])] + (if ^boolean language-view + [:> tll/language-view* {:ids ids + :options-props language-layout-props + :on-change on-change + :on-close on-close-language-view + :has-ruby has-ruby}] + [:* + [:section {:class (stl/css :element-set) + ;; Focusing these controls must not exit the v3 text editor (see `keep-editing-on-blur?`). + :data-keep-editing-on-blur true + :aria-label (tr "workspace.options.text-options.text-section")} + [:div {:class (stl/css :element-title)} + [:> title-bar* {:collapsable true + :collapsed (not main-menu-open?) + :on-collapsed toggle-main-menu + :title label + :class (stl/css :title-spacing-text)} + [:* + (when (and token-typography-row-enabled? (some? (resolve-delay typography-tokens)) (not typography)) + [:> icon-button* {:variant "ghost" + :aria-label (tr "ds.inputs.numeric-input.open-token-list-dropdown") + :on-click toggle-token-dropdown + :tooltip-placement "top-left" + :icon i/tokens}]) + (when (and (not typography) (not multiple?) (not applied-token-name)) + [:> icon-button* {:variant "ghost" + :aria-label (if (not font) + (tr "workspace.options.font-not-available" (:font-family values)) + (tr "workspace.options.convert-to-typography")) + :on-click on-convert-to-typography + :tooltip-placement "top-left" + :disabled (not font) + :icon i/add}])]] + (when (and token-typography-row-enabled? token-dropdown-open?) + [:> searchable-options-dropdown* {:on-click on-option-click + :id listbox-id + :options (resolve-delay dropdown-options) + :selected selected-token-id + :align "right" + :placeholder (tr "workspace.tokens.search-by-token") + :ref set-option-ref}])] - (when main-menu-open? - [:div {:class (stl/css :element-content)} - (cond - (and token-typography-row-enabled? (= :multiple current-token-name) (= typography-id :multiple)) - [:div {:class (stl/css :multiple-typography)} - [:span {:class (stl/css :multiple-text)} - (tr "workspace.libraries.text.mixed-tokens-and-assets")]] + (when main-menu-open? + [:div {:class (stl/css :element-content)} + (cond + (and token-typography-row-enabled? (= :multiple current-token-name) (= typography-id :multiple)) + [:div {:class (stl/css :multiple-typography)} + [:span {:class (stl/css :multiple-text)} + (tr "workspace.libraries.text.mixed-tokens-and-assets")]] - (and token-typography-row-enabled? (= :multiple current-token-name)) - [:div {:class (stl/css :multiple-typography)} - [:span {:class (stl/css :multiple-text)} - (tr "workspace.libraries.text.mixed-tokens")] - [:> icon-button* {:variant "ghost" - :aria-label (tr "workspace.libraries.text.multiple-token-tooltip") - :tooltip-placement "top-left" - :on-click handle-detach-all-tokens - :icon i/detach}]] + (and token-typography-row-enabled? (= :multiple current-token-name)) + [:div {:class (stl/css :multiple-typography)} + [:span {:class (stl/css :multiple-text)} + (tr "workspace.libraries.text.mixed-tokens")] + [:> icon-button* {:variant "ghost" + :aria-label (tr "workspace.libraries.text.multiple-token-tooltip") + :tooltip-placement "top-left" + :on-click handle-detach-all-tokens + :icon i/detach}]] - (and token-typography-row-enabled? current-token-name) - [:> token-typography-row* {:token-name current-token-name - :detach-token detach-token - :active-tokens (resolve-delay typography-tokens)}] + (and token-typography-row-enabled? current-token-name) + [:> token-typography-row* {:token-name current-token-name + :detach-token detach-token + :active-tokens (resolve-delay typography-tokens)}] - (= typography-id :multiple) - [:div {:class (stl/css :multiple-typography)} - [:span {:class (stl/css :multiple-text)} - (tr "workspace.libraries.text.mixed-typography")] - [:> icon-button* {:variant "ghost" - :aria-label (tr "workspace.libraries.text.multiple-assets-tooltip") - :on-click handle-detach-typography - :tooltip-placement "top-left" - :icon i/detach}]] + (= typography-id :multiple) + [:div {:class (stl/css :multiple-typography)} + [:span {:class (stl/css :multiple-text)} + (tr "workspace.libraries.text.mixed-typography")] + [:> icon-button* {:variant "ghost" + :aria-label (tr "workspace.libraries.text.multiple-assets-tooltip") + :on-click handle-detach-typography + :tooltip-placement "top-left" + :icon i/detach}]] - typography - [:> typography-entry* {:file-id typography-file-id - :typography typography - :is-local (= typography-file-id file-id) - :on-detach handle-detach-typography - :on-change handle-change-typography}] + typography + [:> typography-entry* {:file-id typography-file-id + :typography typography + :is-local (= typography-file-id file-id) + :on-detach handle-detach-typography + :on-change handle-change-typography}] + :else + [:> text-options* common-props]) + [:div {:class (stl/css :text-align-options)} + [:> text-align-options* common-props] + [:> grow-options* common-props] + [:> icon-button* {:variant "ghost" + :aria-label (tr "labels.options") + :data-testid "text-align-options-button" + :on-click toggle-more-options + :icon i/menu}]] - :else - [:> text-options* common-props]) + (when (and ^boolean cjk-feature? + ^boolean (wm/vertical-layout-active?) + ^boolean (tcc/cjk-values? values)) + [:> tcc/writing-mode-row* {:values values + :on-change on-change + :on-blur on-text-blur}]) - [:div {:class (stl/css :text-align-options)} - [:> text-align-options* common-props] - [:> grow-options* common-props] - [:> icon-button* {:variant "ghost" - :aria-label (tr "labels.options") - :data-testid "text-align-options-button" - :on-click toggle-more-options - :icon i/menu}]] + (when more-options-open? + [:div {:class (stl/css :text-decoration-options)} + [:> vertical-align* common-props] + [:> text-decoration-options* (mf/spread-props common-props {:token-applied current-token-name})] + [:> text-direction-options* common-props]]) - (when more-options-open? - [:div {:class (stl/css :text-decoration-options)} - [:> vertical-align* common-props] - [:> text-decoration-options* (mf/spread-props common-props {:token-applied current-token-name})] - [:> text-direction-options* common-props]]) + (when (and more-options-open? text-lists-enabled?) + [:> list-options* common-props])])] - (when (and more-options-open? text-lists-enabled?) - [:> list-options* common-props])])])) + (when (and ^boolean cjk-feature? ^boolean (wm/vertical-layout-active?)) + [:> tll/language-section* {:ids ids + :on-change on-change + :on-open-view on-open-language-view}])]))) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_cjk_controls.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_cjk_controls.cljs new file mode 100644 index 0000000000..e22ae2a733 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_cjk_controls.cljs @@ -0,0 +1,533 @@ +;; 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 Sucursal en España SL + +(ns app.main.ui.workspace.sidebar.options.menus.text-cjk-controls + "Controls the CJK language panels share: writing mode, readings (ruby), + emphasis marks and annotation positions. The panels of each language + are in `text-language-layout`." + (:require-macros [app.main.style :as stl]) + (:require + [app.common.types.text.cjk-layout :as cjk] + [app.main.ui.ds.buttons.icon-button :refer [icon-button*]] + [app.main.ui.ds.controls.input :refer [input*]] + [app.main.ui.ds.controls.radio-buttons :refer [radio-buttons*]] + [app.main.ui.ds.controls.select :refer [select*]] + [app.main.ui.ds.controls.switch :refer [switch*]] + [app.main.ui.ds.foundations.assets.icon :as i :refer [icon*]] + [app.main.ui.ds.tooltip :refer [tooltip*]] + [app.main.ui.workspace.sidebar.options.common :refer [advanced-options* radio-selected]] + [app.util.dom :as dom] + [app.util.i18n :refer [tr]] + [rumext.v2 :as mf])) + +(def ^:private mixed-span-values + #{:mixed :multiple "mixed" "multiple"}) + +(defn mixed-span-value? + [value] + (contains? mixed-span-values value)) + +(defn- span-input-value + [value] + (if (mixed-span-value? value) + "mixed" + (radio-selected value))) + +(defn span-select-value + [value default] + (if (mixed-span-value? value) + "mixed" + (radio-selected value default))) + +(defn with-mixed-span-option + [options value] + (cond-> options + (mixed-span-value? value) + (conj {:id "mixed" + :label (tr "settings.multiple") + :disabled true + :dimmed true}))) + +(defn vertical-writing? + "True when the option `values` are of vertical text. The panels only + show under a CJK language, so the writing mode alone decides." + [values] + (= "vertical-rl" (:writing-mode values))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Generic controls +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(mf/defc option-row* + "Row of a language panel: a `label` and the control in `children`. A + `switch` row puts its control at the end." + [{:keys [label switch children]}] + [:div {:class (stl/css-case :option-row true + :switch-row switch)} + [:span {:class (stl/css :option-label)} label] + children]) + +(mf/defc attr-radio-options* + "Radio group bound to one text attribute." + [{:keys [values on-change on-blur attr default-value name options]}] + (let [selected (radio-selected (get values attr) default-value) + group-id (mf/use-id) + options (mf/with-memo [options group-id] + (mapv #(update % :id (fn [id] (str group-id "-" id))) options)) + name (str group-id "-" name) + handle-change + (mf/use-fn + (mf/deps attr on-change on-blur) + (fn [value] + (on-change {attr value}) + (when (some? on-blur) (on-blur))))] + [:div {:class (stl/css :radio-options)} + [:> radio-buttons* {:selected selected + :on-change handle-change + :name name + :options options}]])) + +(mf/defc span-select-option* + "Select bound to one span attribute, with a disabled entry for mixed values." + [{:keys [values on-change on-blur attr default-value label options]}] + (let [value (get values attr) + selected (span-select-value value default-value) + options (with-mixed-span-option options value) + handle-change + (mf/use-fn + (mf/deps attr on-change on-blur) + (fn [value] + (on-change {attr value}) + (when (some? on-blur) (on-blur))))] + [:> option-row* {:label label} + [:> select* {:default-selected selected + :aria-label label + :options options + :on-change handle-change}]])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Writing mode +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn writing-mode-options + [translate] + [{:value "horizontal-tb" + :id "horizontal-tb-writing-mode" + :label (translate "workspace.options.text-options.writing-mode-horizontal") + :icon i/writing-mode-horizontal} + {:value "vertical-rl" + :id "vertical-rl-writing-mode" + :label (translate "workspace.options.text-options.writing-mode-vertical") + :icon i/writing-mode-vertical}]) + +(defn text-orientation-options + [translate] + [{:value "mixed" + :id "mixed-text-orientation" + :label (translate "workspace.options.text-options.text-orientation-mixed") + :icon i/text-orientation-mixed} + {:value "upright" + :id "upright-text-orientation" + :label (translate "workspace.options.text-options.text-orientation-upright") + :icon i/text-orientation-upright}]) + +(mf/defc writing-mode-controls* + "Writing mode row, with the orientation for vertical text. `children` add + the radio groups of a language to the row." + [{:keys [values on-change on-blur children]}] + (let [props (mf/props {:values values :on-change on-change :on-blur on-blur}) + options (mf/with-memo [] + {:writing-mode (writing-mode-options tr) + :text-orientation (text-orientation-options tr)})] + [:div {:class (stl/css :icon-options)} + [:> attr-radio-options* (mf/spread-props props + {:attr :writing-mode + :default-value (cjk/enum-default :writing-mode) + :name "writing-mode-options" + :options (:writing-mode options)})] + ;; An unset orientation reads as nil, or "" in the v2 editor; both + ;; select "mixed". + (when (vertical-writing? values) + [:> attr-radio-options* (mf/spread-props props + {:attr :text-orientation + :default-value (cjk/enum-default :text-orientation) + :name "text-orientation-options" + :options (:text-orientation options)})]) + children])) + +(defn cjk-values? + "True when the text option `values` are of texts with a CJK language; + mixed languages count as CJK." + [values] + (let [language (:text-language values)] + (or (= :multiple language) + (some? (cjk/language-family language))))) + +(mf/defc distribute-toggle* + "Distributed alignment (均等割り付け, 分散对齐, 배분 정렬): spreads the + characters of every line over the box. Turning it off aligns the text to + the start." + {::mf/private true} + [{:keys [values on-change on-blur]}] + (let [distributed? (= "distribute" (radio-selected (:text-align values))) + title (tr "workspace.options.text-options.text-align-distribute") + tooltip-id (mf/use-id) + + handle-click + (mf/use-fn + (mf/deps distributed? on-change on-blur) + (fn [_event] + (on-change {:text-align (if distributed? "left" "distribute")}) + (when (some? on-blur) (on-blur))))] + + [:> tooltip* {:id tooltip-id + :placement "top-left" + :content #(mf/html + [:div {:class (stl/css :distribute-tooltip)} + [:span {:class (stl/css :distribute-tooltip-title)} title] + [:span (tr "workspace.options.text-options.text-align-distribute-hint")]])} + [:button {:type "button" + :class (stl/css-case :distribute-toggle true + :distribute-toggle-active distributed?) + :aria-pressed distributed? + :aria-label title + :aria-describedby tooltip-id + :data-testid "text-align-distribute" + :on-click handle-click} + [:> icon* {:icon-id i/distribute-horizontally + :size "m" + :aria-hidden true}]]])) + +(mf/defc writing-mode-row* + "Text section row for texts with a CJK language: the writing mode, the + orientation for vertical text, and Distribute at the end." + [{:keys [values on-change on-blur]}] + [:div {:class (stl/css :writing-mode-row)} + [:> writing-mode-controls* {:values values :on-change on-change :on-blur on-blur}] + [:> distribute-toggle* {:values values :on-change on-change :on-blur on-blur}]]) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Positions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Annotation positions store both axes (over or under the line, right or +;; left of the column), but only one applies to a writing mode: above/below +;; in horizontal text, right/left in vertical text. The controls show the +;; one that applies and keep the stored other. + +(defn- position-sides + "Block (`\"over\"`/`\"under\"`) and inline (`\"right\"`/`\"left\"`) side + of a position `value`, nil for the ones it leaves to the renderer." + [attr value] + (case attr + :ruby-side + [value nil] + + :text-emphasis-position + (when (and (string? value) (not= "auto" value)) + (let [[block inline] (.split value "-")] [block inline])) + + :text-underline-position + (case value + "under" ["under" nil] + "left" [nil "left"] + "right" [nil "right"] + "under-left" ["under" "left"] + "under-right" ["under" "right"] + nil))) + +(defn- position-of-sides + [attr [block inline]] + (case attr + :ruby-side block + :text-emphasis-position (if (some? block) (str block "-" inline) "auto") + :text-underline-position (cond + (and block inline) (str block "-" inline) + (or block inline) (or block inline) + :else "auto"))) + +(defn position-options + "Choices of the position `attr` for the writing mode." + [attr vertical] + (let [auto {:id "auto" :label (tr "workspace.options.text-options.position-auto")} + over {:id "over" :label (tr "workspace.options.text-options.position-over")} + under {:id "under" :label (tr "workspace.options.text-options.position-under")} + right {:id "right" :label (tr "workspace.options.text-options.position-right")} + left {:id "left" :label (tr "workspace.options.text-options.position-left")} + ;; Ruby takes the block side in vertical text too: over reads right. + ruby-v [(assoc right :id "over") (assoc left :id "under")]] + (case attr + :ruby-side (if ^boolean vertical ruby-v [over under]) + :text-emphasis-position (if ^boolean vertical [auto right left] [auto over under]) + :text-underline-position (if ^boolean vertical [auto right left] [auto under])))) + +(defn position-choice + "Choice of `position-options` a stored position `value` shows." + [attr vertical value] + (if (mixed-span-value? value) + value + (let [[block inline] (position-sides attr value)] + (cond + (= :ruby-side attr) (or block (cjk/enum-default :ruby-side)) + ^boolean vertical (or inline "auto") + :else (or block "auto"))))) + +(defn position-value + "Stored position for picking `choice` over the stored `value`: the side of + the other writing mode stays as it was." + [attr vertical value choice] + (cond + (= :ruby-side attr) + choice + + ;; Emphasis is auto as a whole, or sets both sides. + (= :text-emphasis-position attr) + (if (= "auto" choice) + "auto" + (let [[block inline] (when-not (mixed-span-value? value) (position-sides attr value))] + (position-of-sides attr (if ^boolean vertical + [(or block "over") choice] + [choice (or inline "right")])))) + + :else + (let [[block inline] (when-not (mixed-span-value? value) (position-sides attr value)) + choice (when (not= "auto" choice) choice)] + (position-of-sides attr (if ^boolean vertical [block choice] [choice inline]))))) + +(mf/defc position-select-option* + "Select of the position `attr` with the sides of the writing mode only." + [{:keys [values on-change on-blur attr vertical label]}] + (let [value (get values attr) + selected (span-select-value (position-choice attr vertical value) "auto") + options (mf/with-memo [attr vertical value] + (with-mixed-span-option (position-options attr vertical) value)) + handle-change + (mf/use-fn + (mf/deps attr vertical value on-change on-blur) + (fn [choice] + (on-change {attr (position-value attr vertical value choice)}) + (when (some? on-blur) (on-blur))))] + [:> option-row* {:label label} + [:> select* {:default-selected selected + :aria-label label + :options options + :on-change handle-change}]])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Emphasis marks and annotation clearance +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn text-emphasis-options + ([] + (text-emphasis-options tr)) + ([translate] + [{:id "none" + :label (translate "workspace.options.text-options.text-emphasis-none")} + {:id "filled-dot" + :label (translate "workspace.options.text-options.text-emphasis-filled-dot")} + {:id "open-dot" + :label (translate "workspace.options.text-options.text-emphasis-open-dot")} + {:id "filled-circle" + :label (translate "workspace.options.text-options.text-emphasis-filled-circle")} + {:id "open-circle" + :label (translate "workspace.options.text-options.text-emphasis-open-circle")} + {:id "filled-sesame" + :label (translate "workspace.options.text-options.text-emphasis-filled-sesame")} + {:id "open-sesame" + :label (translate "workspace.options.text-options.text-emphasis-open-sesame")}])) + +(defn annotation-clearance-options + ([] + (annotation-clearance-options tr)) + ([translate] + [{:id "none" + :label (translate "workspace.options.text-options.annotation-clearance-none")} + {:id "auto" + :label (translate "workspace.options.text-options.annotation-clearance-auto")}])) + +(mf/defc text-emphasis-option* + "Emphasis mark (圏点, 着重号) of the spans." + [{:keys [values on-change on-blur]}] + (let [options (mf/with-memo [] (text-emphasis-options))] + [:> span-select-option* {:values values + :on-change on-change + :on-blur on-blur + :attr :text-emphasis + :default-value (cjk/enum-default :text-emphasis) + :label (tr "workspace.options.text-options.text-emphasis") + :options options}])) + +(mf/defc annotation-clearance-option* + "Whether readings and emphasis marks add to the line height." + [{:keys [values on-change on-blur]}] + (let [options (mf/with-memo [] (annotation-clearance-options))] + [:> span-select-option* {:values values + :on-change on-change + :on-blur on-blur + :attr :annotation-clearance + :default-value (cjk/enum-default :annotation-clearance) + :label (tr "workspace.options.text-options.annotation-clearance") + :options options}])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Readings (ruby) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; A reading is furigana in Japanese and pinyin or zhuyin in Chinese. The +;; panel of each language passes its `labels`. + +(defn ruby-size-options + [translate] + [{:id "half" :label (translate "workspace.options.text-options.ruby-size-half")} + {:id "third" :label (translate "workspace.options.text-options.ruby-size-third")} + {:id "quarter" :label (translate "workspace.options.text-options.ruby-size-quarter")}]) + +(defn ruby-align-options + [translate] + [{:id "space-around" :label (translate "workspace.options.text-options.ruby-align-space-around")} + {:id "center" :label (translate "workspace.options.text-options.ruby-align-center")} + {:id "start" :label (translate "workspace.options.text-options.ruby-align-start")} + {:id "space-between" :label (translate "workspace.options.text-options.ruby-align-space-between")}]) + +(defn ruby-overhang-options + [translate] + [{:id "auto" :label (translate "workspace.options.text-options.ruby-overhang-auto")} + {:id "none" :label (translate "workspace.options.text-options.ruby-overhang-none")}]) + +(mf/defc ruby-options* + "Input of the reading of the selected span." + [{:keys [values on-change on-blur advanced-open on-toggle-advanced labels]}] + (let [ruby (span-input-value (:ruby values)) + ruby* (mf/use-state ruby) + dirty* (mf/use-state false) + value (deref ruby*) + dirty? (deref dirty*) + + valid? (or (empty? value) (nil? (:validate labels)) ((:validate labels) value)) + + commit! + (mf/use-fn + (mf/deps dirty? valid? value on-change on-blur) + (fn [] + (when (and dirty? valid?) + (on-change {:ruby value}) + (reset! dirty* false)) + (when (some? on-blur) (on-blur)))) + + handle-change + (mf/use-fn + (fn [event] + (reset! dirty* true) + (reset! ruby* (dom/get-target-val event)))) + + handle-key-down + (mf/use-fn + (mf/deps commit!) + (fn [event] + (when (= "Enter" (.-key event)) + (dom/blur! (dom/get-target event)))))] + + (mf/with-effect [ruby] + (reset! ruby* ruby) + (reset! dirty* false)) + + [:div {:class (stl/css :ruby-options)} + [:> input* {:class (stl/css :ruby-input) + :label (:input labels) + :placeholder (:placeholder labels) + :value value + :on-change handle-change + :on-blur commit! + :on-key-down handle-key-down}] + (when-not valid? + [:p {:role "alert"} (:invalid labels)]) + (when on-toggle-advanced + [:> icon-button* {:variant "ghost" + :selected advanced-open + :aria-label (:advanced labels) + :aria-expanded advanced-open + :data-testid "ruby-advanced-options-toggle" + :on-click on-toggle-advanced + :icon i/menu}])])) + +(mf/defc ruby-hidden-option* + "Switch hiding the readings, unset when the spans differ." + [{:keys [values on-change on-blur]}] + (let [value (:ruby-hidden values) + enabled? (cond + (mixed-span-value? value) nil + (true? value) true + :else false) + label (tr "workspace.options.text-options.ruby-hidden") + handle-change + (mf/use-fn + (mf/deps on-change on-blur) + (fn [checked?] + (on-change {:ruby-hidden checked?}) + (when (some? on-blur) (on-blur))))] + [:> option-row* {:label label :switch true} + [:> switch* {:default-checked enabled? + :aria-label label + :on-change handle-change}]])) + +(defn- ruby-common-props + [values on-change on-blur labels vertical] + (mf/props + {:values values + :on-change on-change + :on-blur on-blur + :labels labels + :vertical vertical})) + +(mf/defc ruby-presentation-fields* + "Size, alignment, overhang and side of the readings." + [{:keys [values on-change on-blur labels vertical]}] + (let [common-props (ruby-common-props values on-change on-blur labels vertical) + options (mf/with-memo [] + {:ruby-size (ruby-size-options tr) + :ruby-align (ruby-align-options tr) + :ruby-overhang (ruby-overhang-options tr)})] + [:* + [:> span-select-option* (mf/spread-props common-props + {:attr :ruby-size + :default-value (cjk/enum-default :ruby-size) + :label (tr "workspace.options.text-options.ruby-size") + :options (:ruby-size options)})] + [:> span-select-option* (mf/spread-props common-props + {:attr :ruby-align + :default-value (cjk/enum-default :ruby-align) + :label (tr "workspace.options.text-options.ruby-align") + :options (:ruby-align options)})] + [:> span-select-option* (mf/spread-props common-props + {:attr :ruby-overhang + :default-value (cjk/enum-default :ruby-overhang) + :label (tr "workspace.options.text-options.ruby-overhang") + :options (:ruby-overhang options)})] + [:> position-select-option* (mf/spread-props common-props + {:attr :ruby-side + :label (tr "workspace.options.text-options.ruby-side")})]])) + +(mf/defc ruby-customization-options* + [{:keys [values on-change on-blur labels vertical]}] + (let [common-props (ruby-common-props values on-change on-blur labels vertical)] + [:div {:class (stl/css :option-list)} + [:> ruby-presentation-fields* common-props] + [:> ruby-hidden-option* common-props]])) + +(mf/defc ruby-advanced-options* + "Reading input of a text selection, with its presentation options." + [{:keys [values on-change on-blur labels vertical]}] + (let [open* (mf/use-state false) + open? (deref open*) + toggle-open (mf/use-fn #(swap! open* not)) + common-props (ruby-common-props values on-change on-blur labels vertical)] + [:div {:class (stl/css :ruby-advanced-options)} + [:> ruby-options* (mf/spread-props common-props + {:advanced-open open? + :on-toggle-advanced toggle-open})] + [:> advanced-options* {:class (stl/css :ruby-advanced-content) + :is-visible open?} + [:> ruby-customization-options* common-props]]])) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_cjk_controls.scss b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_cjk_controls.scss new file mode 100644 index 0000000000..ca5aee4746 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_cjk_controls.scss @@ -0,0 +1,124 @@ +// 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 Sucursal en España SL + +@use "ds/typography.scss" as t; +@use "ds/_sizes.scss" as *; +@use "ds/_borders.scss" as *; +@use "ds/spacing.scss" as *; + +.radio-options { + block-size: $sz-32; +} + +.icon-options { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: var(--sp-xs); +} + +.writing-mode-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: var(--sp-xs); +} + +.distribute-toggle { + @include t.use-typography("body-small"); + + display: flex; + align-items: center; + justify-content: center; + block-size: $sz-32; + inline-size: $sz-32; + padding: 0; + border: none; + border-radius: $br-8; + color: var(--color-foreground-secondary); + background-color: var(--color-background-tertiary); + cursor: pointer; + + &:hover { + color: var(--color-foreground-primary); + } + + &:focus-visible { + outline: $b-1 solid var(--color-accent-primary); + } +} + +.distribute-toggle-active { + color: var(--color-accent-primary); + background-color: var(--color-background-quaternary); + + &:hover { + color: var(--color-accent-primary); + } +} + +.distribute-tooltip { + display: flex; + flex-direction: column; + gap: var(--sp-xs); + max-inline-size: $sz-252; +} + +.distribute-tooltip-title { + @include t.use-typography("body-medium"); + + color: var(--color-foreground-primary); +} + +.option-list { + display: flex; + flex-direction: column; + gap: var(--sp-xs); +} + +.option-row { + display: grid; + grid-template-columns: minmax(0, 1fr) minmax(0, 1.35fr); + align-items: center; + gap: var(--sp-s); + inline-size: 100%; + min-block-size: $sz-32; +} + +.switch-row { + grid-template-columns: minmax(0, 1fr) auto; +} + +.option-label { + @include t.use-typography("body-small"); + + color: var(--color-foreground-secondary); +} + +.ruby-advanced-options { + display: flex; + flex-direction: column; + overflow: visible; +} + +.ruby-options { + display: flex; + align-items: flex-end; + gap: var(--sp-xs); +} + +.ruby-input { + flex: 1; +} + +.ruby-advanced-content { + display: flex; + flex-direction: column; + gap: var(--sp-xs); + padding: var(--sp-xs); + border: $b-1 solid var(--color-background-quaternary); + border-radius: $br-8; +} diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_language_layout.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_language_layout.cljs new file mode 100644 index 0000000000..604a553c92 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_language_layout.cljs @@ -0,0 +1,526 @@ +;; 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 app.main.ui.workspace.sidebar.options.menus.text-language-layout + "Language panel of the text options, and the options of each CJK + language below it. The controls they share are in `text-cjk-controls`." + (:require-macros [app.main.style :as stl]) + (:require + [app.common.types.text.cjk-layout :as cjk] + [app.main.data.workspace.texts :as dwt] + [app.main.refs :as refs] + [app.main.store :as st] + [app.main.ui.components.grouped-dropdown :refer [grouped-dropdown*]] + [app.main.ui.components.title-bar :refer [title-bar*]] + [app.main.ui.ds.buttons.icon-button :refer [icon-button*]] + [app.main.ui.ds.controls.select :refer [select*]] + [app.main.ui.ds.controls.switch :refer [switch*]] + [app.main.ui.ds.foundations.assets.icon :refer [icon*] :as i] + [app.main.ui.workspace.sidebar.options.common :refer [radio-selected]] + [app.main.ui.workspace.sidebar.options.menus.text-cjk-controls :as tcc] + [app.util.i18n :refer [tr]] + [app.util.text.layout :as tl] + [rumext.v2 :as mf])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Japanese +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn proportional-metrics-feature + "Proportional metrics feature for the writing mode: `vpal` or `palt`." + [writing-mode] + (if (= writing-mode "vertical-rl") "vpal" "palt")) + +;; select* matches options by :id, so each id is the stored value. +(defn text-combine-upright-count-options + [translate] + [{:id "digits2" + :label (translate "workspace.options.text-options.text-combine-upright-digits-2")} + {:id "digits3" + :label (translate "workspace.options.text-options.text-combine-upright-digits-3")} + {:id "digits" + :label (translate "workspace.options.text-options.text-combine-upright-digits-4")}]) + +;; Warichu (割注): renders the selection as two half-size lines in one inline +;; position (top/bottom horizontally, right/left vertically). +(defn warichu-options + [translate] + [{:value "none" + :id "none-warichu" + :label (translate "workspace.options.text-options.warichu-none") + :icon i/warichu-none} + {:value "warichu" + :id "warichu-warichu" + :label (translate "workspace.options.text-options.warichu") + :icon i/warichu}]) + +(defn line-adjustment-options + ([] + (line-adjustment-options tr)) + ([translate] + [{:id "push-in-first" + :label (translate "workspace.options.text-options.line-adjustment-push-in-first")} + {:id "push-out-first" + :label (translate "workspace.options.text-options.line-adjustment-push-out-first")} + {:id "push-out-only" + :label (translate "workspace.options.text-options.line-adjustment-push-out-only")}])) + +(defn- text-combine-upright-value + [values] + (radio-selected (:text-combine-upright values) (cjk/enum-default :text-combine-upright))) + +;; Tate-chu-yoko (縦中横) for a whole text: none, or the digit runs up to a +;; length. `all` combines a text selection (see `combine-options`). +(defn tate-chu-yoko-options + [translate] + [{:value "none" + :id "none-text-combine-upright" + :label (translate "workspace.options.text-options.text-combine-upright-none") + :icon i/text-combine-upright-none} + {:value "digits" + :id "digits-text-combine-upright" + :label (translate "workspace.options.text-options.text-combine-upright-digits") + :icon i/text-combine-upright-digits}]) + +(defn combine-options + [translate] + [{:value "none" + :id "none-combine-upright" + :label (translate "workspace.options.text-options.text-combine-upright-none") + :icon i/text-combine-upright-none} + {:value "all" + :id "all-combine-upright" + :label (translate "workspace.options.text-options.text-combine-upright-all") + :icon i/text-combine-upright-all}]) + +(mf/defc tate-chu-yoko-option* + "Tate-chu-yoko of vertical text: the digit runs to set upright, with the + longest run in the same row once it is on." + [{:keys [values on-change on-blur]}] + (let [value (text-combine-upright-value values) + digits? (cjk/digit-combine? value) + options (mf/with-memo [] + {:mode (tate-chu-yoko-options tr) + :count (text-combine-upright-count-options tr)}) + handle-count-change + (mf/use-fn + (mf/deps on-change on-blur) + (fn [id] + (on-change {:text-combine-upright id}) + (when (some? on-blur) (on-blur))))] + [:div {:class (stl/css :icon-options)} + [:> tcc/attr-radio-options* {:values {:text-combine-upright (if digits? "digits" value)} + :on-change on-change + :on-blur on-blur + :attr :text-combine-upright + :default-value (cjk/enum-default :text-combine-upright) + :name "text-combine-upright-options" + :options (:mode options)}] + (when ^boolean digits? + [:div {:class (stl/css :digit-count)} + [:> select* {:default-selected value + :aria-label (tr "workspace.options.text-options.text-combine-upright-digits-count") + :options (:count options) + :on-change handle-count-change}]])])) + +(mf/defc font-features-option* + "Proportional metric alternates, per span: `palt` for horizontal text, + `vpal` for vertical." + [{:keys [values on-change on-blur]}] + (let [feature (proportional-metrics-feature (:writing-mode values)) + value (:font-features values) + enabled? (cond + (tcc/mixed-span-value? value) nil + (= feature value) true + :else false) + label (tr "workspace.options.text-options.font-features") + + handle-change + (mf/use-fn + (mf/deps feature on-change on-blur) + (fn [checked?] + (on-change {:font-features (if checked? feature "none")}) + (when (some? on-blur) (on-blur))))] + + [:> tcc/option-row* {:label label :switch true} + [:> switch* {:default-checked enabled? + :aria-label label + :on-change handle-change}]])) + +(mf/defc line-adjustment-option* + "Whole-shape setting: how lines slightly too long are fitted." + [{:keys [values on-change on-blur]}] + (let [options (mf/with-memo [] (line-adjustment-options))] + [:> tcc/span-select-option* {:values values + :on-change on-change + :on-blur on-blur + :attr :line-adjustment + :default-value (cjk/enum-default :line-adjustment) + :label (tr "workspace.options.text-options.line-adjustment") + :options options}])) + +(defn- japanese-reading-labels + [] + {:input (tr "workspace.options.text-options.ruby") + :placeholder (tr "workspace.options.text-options.ruby-placeholder") + :advanced (tr "workspace.options.text-options.ruby-advanced-options")}) + +(mf/defc japanese-selection-options* + "Japanese options of a text selection: its reading, emphasis marks, + warichu and, in vertical text, combine." + [{:keys [values on-change on-blur]}] + (let [vertical? (tcc/vertical-writing? values) + props (mf/props {:values values :on-change on-change :on-blur on-blur + :vertical vertical? :labels (japanese-reading-labels)}) + options (mf/with-memo [] + {:warichu (warichu-options tr) + :combine (combine-options tr)})] + [:* + [:> tcc/ruby-advanced-options* props] + ;; Emphasis marks (圏点 / bouten) apply to the selected characters. + [:> tcc/text-emphasis-option* props] + [:div {:class (stl/css :icon-options)} + [:> tcc/attr-radio-options* {:values values + :on-change on-change + :on-blur on-blur + :attr :warichu + :default-value (cjk/enum-default :warichu) + :name "warichu-options" + :options (:warichu options)}] + (when ^boolean vertical? + [:> tcc/attr-radio-options* {:values {:text-combine-upright + (if (= "all" (text-combine-upright-value values)) "all" "none")} + :on-change on-change + :on-blur on-blur + :attr :text-combine-upright + :default-value (cjk/enum-default :text-combine-upright) + :name "combine-upright-options" + :options (:combine options)}])]])) + +(mf/defc japanese-layout-options* + "Japanese options of the whole text." + [{:keys [values on-change on-blur]}] + (let [props (mf/props {:values values :on-change on-change :on-blur on-blur})] + [:* + (when (tcc/vertical-writing? values) + [:> tate-chu-yoko-option* props]) + [:> font-features-option* props] + [:> line-adjustment-option* props]])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Chinese +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn- chinese-reading-labels + [zhuyin?] + (if zhuyin? + {:input (tr "workspace.options.text-options.zhuyin") + :placeholder (tr "workspace.options.text-options.zhuyin-placeholder") + :advanced (tr "workspace.options.text-options.ruby-advanced-options") + :validate cjk/zhuyin-reading? + :invalid (tr "workspace.options.text-options.zhuyin-invalid")} + {:input (tr "workspace.options.text-options.pinyin") + :placeholder (tr "workspace.options.text-options.pinyin-placeholder") + :advanced (tr "workspace.options.text-options.ruby-advanced-options")})) + +(mf/defc chinese-selection-options* + "Chinese options of a text selection: its reading, pinyin or zhuyin, when + it can take one, then its emphasis marks. Zhuyin annotates one Han + character at a time." + [{:keys [values reading-selection-active zhuyin-selection-active on-change on-blur]}] + (let [zhuyin? (true? (:ruby-zhuyin values)) + vertical? (tcc/vertical-writing? values) + format-label (tr "workspace.options.text-options.chinese-reading-format") + handle-format-change + (mf/use-fn + (mf/deps on-change) + (fn [value] (on-change {:ruby-zhuyin (= "zhuyin" value)}))) + props (mf/props {:values values :on-change on-change :on-blur on-blur + :vertical vertical? + :labels (chinese-reading-labels zhuyin?)})] + [:* + (when ^boolean reading-selection-active + [:* + [:> tcc/option-row* {:label format-label} + [:> select* {:default-selected (if zhuyin? "zhuyin" "pinyin") + :aria-label format-label + :options [{:id "pinyin" :label (tr "workspace.options.text-options.pinyin")} + {:id "zhuyin" :label (tr "workspace.options.text-options.zhuyin") + :disabled (not zhuyin-selection-active)}] + :on-change handle-format-change}]] + (cond + (not zhuyin?) + [:> tcc/ruby-advanced-options* props] + + ^boolean zhuyin-selection-active + [:> tcc/ruby-options* props] + + :else + [:p {:class (stl/css :selection-hint)} + (tr "workspace.options.text-options.zhuyin-selection-hint")])]) + [:> tcc/text-emphasis-option* props] + [:> tcc/position-select-option* + (mf/spread-props props {:attr :text-emphasis-position + :label (tr "workspace.options.text-options.emphasis-position")})]])) + +(mf/defc chinese-layout-options* + "Chinese options of the whole text. Emphasis marks show here while no + text is selected, and then apply to the whole text. Clearance shows + here unless the text has readings: then the Ruby group has it." + [{:keys [values text-selection-active has-ruby on-change on-blur]}] + (let [props (mf/props {:values values :on-change on-change :on-blur on-blur + :vertical (tcc/vertical-writing? values)})] + [:* + (when-not ^boolean text-selection-active + [:* + [:> tcc/text-emphasis-option* props] + [:> tcc/position-select-option* + (mf/spread-props props {:attr :text-emphasis-position + :label (tr "workspace.options.text-options.emphasis-position")})]]) + [:> tcc/position-select-option* + (mf/spread-props props {:attr :text-underline-position + :label (tr "workspace.options.text-options.underline-position")})] + (when-not ^boolean has-ruby + [:> tcc/annotation-clearance-option* props])])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Korean +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn korean-wrap-options + [value translate] + (cond-> [{:id "syllable" :label (translate "workspace.options.text-options.korean-wrap-syllable")} + {:id "keep-words" :label (translate "workspace.options.text-options.korean-wrap-keep-words")}] + (= value "spaces-only") + (conj {:id "spaces-only" :label (translate "workspace.options.text-options.korean-wrap-spaces-only")}))) + +(mf/defc korean-layout-options* + [{:keys [values on-change on-blur]}] + [:> tcc/span-select-option* {:values values + :on-change on-change + :on-blur on-blur + :attr :korean-wrap + :default-value (cjk/enum-default :korean-wrap) + :label (tr "workspace.options.text-options.korean-wrap") + :options (korean-wrap-options (:korean-wrap values) tr)}]) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Ruby +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(mf/defc ruby-group-options* + "How the readings of the text look. `values` are the ruby values of the + text (or of the selection), and `on-change` applies to every reading of + the text without a selection. Zhuyin readings can only be hidden." + [{:keys [values on-change on-blur]}] + (let [props (mf/props {:values values :on-change on-change :on-blur on-blur + :vertical (tcc/vertical-writing? values)})] + [:* + [:> tcc/annotation-clearance-option* props] + (when-not (true? (:ruby-zhuyin values)) + [:> tcc/ruby-presentation-fields* props]) + [:> tcc/ruby-hidden-option* props]])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Language panel and view +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(def ^:private no-language "none") + +(mf/defc language-picker* + "The language of the texts, as a select row opening the profile list: + None first, then the profiles grouped by language. `on-select` receives + the chosen tag, nil for None." + {::mf/private true} + [{:keys [language on-select children]}] + (let [dropdown* (mf/use-state false) + dropdown? (deref dropdown*) + container-ref (mf/use-ref nil) + options (mf/with-memo [language] + (into [{:id no-language + :label (tr "workspace.options.text-options.language-none") + :selected (nil? language)}] + (tl/language-options language))) + + toggle-dropdown (mf/use-fn #(swap! dropdown* not)) + close-dropdown (mf/use-fn #(reset! dropdown* false)) + + handle-select + (mf/use-fn + (mf/deps on-select) + (fn [tag _event] + (reset! dropdown* false) + (on-select (when (not= no-language tag) tag))))] + + [:div {:class (stl/css :language-row) :ref container-ref} + [:button {:type "button" + :class (stl/css-case :language-select true + :mixed (= :multiple language)) + :aria-haspopup "listbox" + :aria-expanded dropdown? + :aria-label (tr "workspace.options.text-options.language") + :data-testid "text-language-select" + :on-click toggle-dropdown} + [:span {:class (stl/css :language-name)} (tl/language-label language)] + [:> icon* {:icon-id i/arrow-down + :class (stl/css :language-arrow) + :size "s" + :aria-hidden true}]] + children + [:> grouped-dropdown* {:show dropdown? + :on-close close-dropdown + :container container-ref + :class (stl/css :language-dropdown) + :options options + :on-select handle-select}]])) + +(defn- use-select-language + "Handler giving the texts the language `tag` (nil removes it); new texts + take it too. `on-chosen` runs after a CJK language is chosen." + [on-change on-chosen] + (mf/use-fn + (mf/deps on-change on-chosen) + (fn [tag] + (on-change {:text-language tag}) + (st/emit! (dwt/remember-text-language tag)) + (when (and (some? on-chosen) (some? (cjk/language-family tag))) + (on-chosen))))) + +(mf/defc language-section* + "Language panel of the text options. A text has one language: picking or + removing one changes every paragraph of the texts in `ids`, and new texts + take it. The options of the language open in their own view through + `on-open-view`, offered when the texts share a CJK language, and also + right after one is chosen." + [{:keys [ids on-change on-open-view]}] + (let [language-ref (mf/with-memo [ids] (refs/texts-language ids)) + language (mf/deref language-ref) + family (cjk/language-family language) + + open* (mf/use-state true) + open? (deref open*) + dropdown* (mf/use-state false) + dropdown? (deref dropdown*) + container-ref (mf/use-ref nil) + + title (tr "workspace.options.text-options.language") + options (mf/with-memo [] (tl/language-options nil)) + + toggle-open (mf/use-fn #(swap! open* not)) + toggle-dropdown (mf/use-fn #(swap! dropdown* not)) + close-dropdown (mf/use-fn #(reset! dropdown* false)) + + select-language (use-select-language on-change on-open-view) + + handle-add + (mf/use-fn + (mf/deps select-language) + (fn [tag _event] + (reset! dropdown* false) + (reset! open* true) + (select-language tag)))] + + [:section {:class (stl/css :language-section) + :data-keep-editing-on-blur true + :aria-label title} + [:div {:class (stl/css :language-title) :ref container-ref} + [:> title-bar* {:collapsable (some? language) + :collapsed (not open?) + :on-collapsed toggle-open + :title title} + (when (nil? language) + [:> icon-button* {:variant "ghost" + :aria-label (tr "workspace.options.text-options.add-language") + :aria-haspopup "listbox" + :aria-expanded dropdown? + :data-testid "text-language-add" + :on-click toggle-dropdown + :icon i/add}])] + (when (nil? language) + [:> grouped-dropdown* {:show dropdown? + :on-close close-dropdown + :container container-ref + :class (stl/css :language-dropdown) + :options options + :on-select handle-add}])] + + (when (and (some? language) open?) + [:div {:class (stl/css :language-content)} + [:> language-picker* {:language language :on-select select-language} + (when (some? on-open-view) + [:> icon-button* {:variant "ghost" + :aria-label (tr "workspace.options.text-options.language-options") + :data-testid "text-language-options" + :disabled (nil? family) + :on-click on-open-view + :icon i/arrow-right}])]])])) + +(mf/defc language-group* + {::mf/private true} + [{:keys [title children]}] + [:div {:class (stl/css :language-group)} + [:h3 {:class (stl/css :language-group-title)} title] + children]) + +(mf/defc language-view* + "Options of the CJK language of the texts in `ids`, in a view of their + own that `on-close` leaves. Selected text (with a text selection) comes + first, then Layout, then Ruby when the text has readings (`has-ruby`). + The view closes when the texts no longer share a CJK language." + [{:keys [ids options-props on-change on-close has-ruby]}] + (let [language-ref (mf/with-memo [ids] (refs/texts-language ids)) + language (mf/deref language-ref) + family (cjk/language-family language) + + values (unchecked-get options-props "values") + ruby-values (unchecked-get options-props "rubyValues") + text-selection-active (unchecked-get options-props "textSelectionActive") + on-blur (unchecked-get options-props "onBlur") + on-ruby-presentation-change (unchecked-get options-props "onRubyPresentationChange") + + select-language (use-select-language on-change nil) + + ruby-props + (mf/props {:values (assoc ruby-values :writing-mode (:writing-mode values)) + :on-change on-ruby-presentation-change + :on-blur on-blur})] + + (mf/with-effect [family] + (when (nil? family) + (on-close))) + + [:section {:class (stl/css :language-view) + :data-keep-editing-on-blur true + :aria-label (tr "workspace.options.text-options.language-options")} + [:div {:class (stl/css :language-view-header)} + [:> icon-button* {:variant "ghost" + :aria-label (tr "labels.back") + :data-testid "text-language-back" + :on-click on-close + :icon i/arrow-left}] + [:span {:class (stl/css :language-view-title)} + (tr "workspace.options.text-options.language")]] + + [:> language-picker* {:language language :on-select select-language}] + + (when (and ^boolean text-selection-active (not= :korean family)) + [:> language-group* {:title (tr "workspace.options.text-options.language-group-selected-text")} + (case family + :japanese [:> japanese-selection-options* options-props] + :chinese [:> chinese-selection-options* options-props] + nil)]) + + (when (some? family) + [:> language-group* {:title (tr "workspace.options.text-options.language-group-layout")} + (case family + :japanese [:> japanese-layout-options* options-props] + :chinese [:> chinese-layout-options* + (mf/spread-props options-props {:has-ruby has-ruby})] + :korean [:> korean-layout-options* options-props])]) + + (when (and ^boolean has-ruby (not= :korean family) (some? family)) + [:> language-group* {:title (tr "workspace.options.text-options.language-group-ruby")} + [:> ruby-group-options* ruby-props]])])) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_language_layout.scss b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_language_layout.scss new file mode 100644 index 0000000000..75b9b9b929 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/text_language_layout.scss @@ -0,0 +1,160 @@ +// 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 Sucursal en España SL + +@use "../../../sidebar/common/sidebar.scss" as sidebar; +@use "ds/typography.scss" as t; +@use "ds/_borders.scss" as *; +@use "ds/_sizes.scss" as *; +@use "ds/_utils.scss" as *; + +.language-section { + @include sidebar.option-grid-structure; +} + +.language-title { + position: relative; + grid-column: span 8; +} + +.language-content { + display: flex; + flex-direction: column; + gap: var(--sp-xs); + grid-column: span 8; + margin-block-start: var(--sp-xs); +} + +.language-row { + position: relative; + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + align-items: center; + gap: var(--sp-xs); +} + +// Looks like the DS select it stands for; it opens the grouped dropdown. +.language-select { + --language-select-background: var(--color-background-tertiary); + --language-select-icon-color: var(--color-icon-default); + --language-select-outline-color: none; + + @include t.use-typography("body-small"); + + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + align-items: center; + gap: var(--sp-xs); + block-size: $sz-32; + padding: var(--sp-s); + border: none; + border-radius: $br-8; + outline: $b-1 solid var(--language-select-outline-color); + background: var(--language-select-background); + color: var(--color-foreground-primary); + text-align: start; + cursor: pointer; + appearance: none; + + &:is(:hover, [aria-expanded="true"]) { + --language-select-background: var(--color-background-quaternary); + --language-select-icon-color: var(--color-foreground-primary); + } + + &:focus-visible { + --language-select-background: var(--color-background-quaternary); + --language-select-outline-color: var(--color-accent-primary); + } +} + +.language-name { + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; + padding-inline-start: var(--sp-xxs); +} + +.mixed .language-name { + color: var(--color-foreground-secondary); +} + +.language-arrow { + color: var(--language-select-icon-color); +} + +// The profile list spans the row, with white profiles under small grey +// headers. The parent class wins over the shared list width. +:is(.language-row, .language-title) .language-dropdown { + --menu-foreground-color-rest: var(--color-foreground-primary); + + inline-size: 100%; + padding: var(--sp-xs); + + li[role="presentation"] { + @include t.use-typography("headline-small"); + + font-size: px2rem(11); + block-size: $sz-24; + color: var(--color-foreground-secondary); + } +} + +.icon-options { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: var(--sp-xs); +} + +.digit-count { + flex: 1; + min-inline-size: 0; +} + +// The options of the text language, in a view that replaces the design tab +// (like editing a grid layout). +.language-view { + display: flex; + flex-direction: column; + gap: var(--sp-xs); + padding-block-end: var(--sp-s); +} + +.language-view-header { + display: flex; + align-items: center; + gap: var(--sp-xs); + block-size: $sz-32; +} + +.language-view-title { + @include t.use-typography("body-medium"); + + color: var(--color-foreground-primary); +} + +.language-group { + display: flex; + flex-direction: column; + gap: var(--sp-xs); +} + +.selection-hint { + @include t.use-typography("body-small"); + + margin: 0; + color: var(--color-foreground-secondary); +} + +.language-group-title { + @include t.use-typography("body-medium"); + + display: flex; + align-items: center; + block-size: $sz-32; + margin: 0; + padding: 0; + color: var(--color-foreground-primary); +} diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/shapes/text.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/shapes/text.cljs index dd2c9dc102..50c26ef2dd 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/shapes/text.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/shapes/text.cljs @@ -10,6 +10,7 @@ [app.common.data.macros :as dm] [app.common.types.shape.layout :as ctl] [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk] [app.main.data.workspace.texts :as dwt] [app.main.features :as features] [app.main.refs :as refs] @@ -27,8 +28,31 @@ [app.main.ui.workspace.sidebar.options.menus.shadow :refer [shadow-menu*]] [app.main.ui.workspace.sidebar.options.menus.stroke :refer [stroke-attrs stroke-menu*]] [app.main.ui.workspace.sidebar.options.menus.text :refer [text-menu*]] + [app.render-wasm.api :as wasm.api] + [app.render-wasm.text-editor :as wasm.text-editor] [rumext.v2 :as mf])) +(defn- dom-text-range-selected? + [editor] + (when editor + (let [selection (.getSelection js/window) + element (.-element editor) + anchor (some-> selection .-anchorNode) + focus (some-> selection .-focusNode)] + (and selection + (not (.-isCollapsed selection)) + element + anchor + focus + (.contains element anchor) + (.contains element focus))))) + +(defn- draft-text-range-selected? + [editor-state] + (when editor-state + (let [selection (.getSelection editor-state)] + (and selection (not (.isCollapsed selection)))))) + (mf/defc options* [{:keys [shape libraries file-id page-id]}] (let [id (dm/get-prop shape :id) @@ -47,6 +71,19 @@ ids (mf/with-memo [id] [id]) shapes (mf/with-memo [shape] [shape]) + ;; The options of the text language replace the design tab while + ;; open. A new text range keeps them open; another selection closes + ;; them (see the effect below). + language-view* (mf/use-state false) + language-view? (deref language-view*) + open-language-view (mf/use-fn #(reset! language-view* true)) + close-language-view (mf/use-fn #(reset! language-view* false)) + + has-ruby + (mf/with-memo [shape] + (boolean (some cjk/ruby-span? + (txt/node-seq txt/is-text-node? (:content shape))))) + typographies (mf/deref refs/workspace-file-typography) @@ -128,6 +165,32 @@ (when text-editor-v2? editor) + text-selection-active + (boolean + (cond + text-editor-wasm? + (and (= id (wasm.api/text-editor-get-active-shape-id)) + (wasm.api/text-editor-has-selection?)) + + text-editor-v2? + (dom-text-range-selected? editor-instance) + + :else + (draft-text-range-selected? editor-state))) + + reading-selection-active + (and text-selection-active + text-editor-wasm? + (wasm.text-editor/reading-selection? + (wasm.text-editor/get-cached-content id) + (wasm.text-editor/text-editor-get-selection))) + + zhuyin-selection-active + (and reading-selection-active + (wasm.text-editor/zhuyin-selection? + (wasm.text-editor/get-cached-content id) + (wasm.text-editor/text-editor-get-selection))) + fill-values (dwt/current-text-values {:editor-styles editor-styles @@ -154,87 +217,120 @@ :editor-state editor-state :editor-instance editor-instance :shape shape - :attrs txt/text-node-attrs}))] + :attrs txt/text-node-attrs})) - [:* - [:> layer-menu* {:ids ids - :type type - :applied-tokens applied-tokens - :values layer-values}] - [:> measures-menu* - {:ids ids - :type type - :values measure-values - :applied-tokens applied-tokens - :shapes shapes}] + ruby-attrs + (conj cjk/ruby-presentation-attrs :annotation-clearance) - [:> layout-container-menu* - {:type type - :ids ids - :values layout-container-values - :applied-tokens applied-tokens - :multiple false}] + ruby-values + (if text-selection-active + (select-keys text-values ruby-attrs) + (dwt/current-ruby-values + {:shape shape + :attrs ruby-attrs}))] - (when (and (= (count ids) 1) is-layout-child? is-grid-parent?) - [:> grid-cell/options* - {:shape-id (-> (first parents) - :id) - :cell (ctl/get-cell-by-shape-id (first parents) (first ids))}]) + (mf/with-effect [id] + (reset! language-view* false)) - (when is-layout-child? - [:> layout-item-menu* + (if ^boolean language-view? + [:> text-menu* + {:ids ids + :type type + :applied-tokens applied-tokens + :values text-values + :ruby-values ruby-values + :text-selection-active text-selection-active + :reading-selection-active reading-selection-active + :zhuyin-selection-active zhuyin-selection-active + :libraries libraries + :file-id file-id + :typographies typographies + :language-view true + :on-close-language-view close-language-view + :has-ruby has-ruby}] + [:* + [:> layer-menu* {:ids ids + :type type + :applied-tokens applied-tokens + :values layer-values}] + [:> measures-menu* {:ids ids :type type - :values layout-item-values - :is-layout-child true - :is-flex-parent is-flex-parent? - :is-grid-parent is-grid-parent? + :values measure-values :applied-tokens applied-tokens - :shape shape}]) + :shapes shapes}] - (when (or (not ^boolean is-layout-child?) ^boolean is-layout-child-absolute?) - [:> constraints-menu* - {:ids ids - :values (select-keys shape constraint-attrs)}]) - - [:> text-menu* - {:ids ids - :type type - :applied-tokens applied-tokens - :values text-values - :libraries libraries - :file-id file-id - :typographies typographies}] - - [:> fill/fill-menu* - {:ids ids - :type type - :values fill-values - :applied-tokens applied-tokens}] - - [:> stroke-menu* {:ids ids - :type type - :values stroke-values - :disable-stroke-style true - :applied-tokens applied-tokens}] - - (when (= :multiple (:fills fill-values)) - [:> color-selection-menu* + [:> layout-container-menu* {:type type - :shapes shapes + :ids ids + :values layout-container-values + :applied-tokens applied-tokens + :multiple false}] + + (when (and (= (count ids) 1) is-layout-child? is-grid-parent?) + [:> grid-cell/options* + {:shape-id (-> (first parents) + :id) + :cell (ctl/get-cell-by-shape-id (first parents) (first ids))}]) + + (when is-layout-child? + [:> layout-item-menu* + {:ids ids + :type type + :values layout-item-values + :is-layout-child true + :is-flex-parent is-flex-parent? + :is-grid-parent is-grid-parent? + :applied-tokens applied-tokens + :shape shape}]) + + (when (or (not ^boolean is-layout-child?) ^boolean is-layout-child-absolute?) + [:> constraints-menu* + {:ids ids + :values (select-keys shape constraint-attrs)}]) + + [:> text-menu* + {:ids ids + :type type + :applied-tokens applied-tokens + :values text-values + :ruby-values ruby-values + :text-selection-active text-selection-active + :reading-selection-active reading-selection-active + :zhuyin-selection-active zhuyin-selection-active + :libraries libraries :file-id file-id - :libraries libraries}]) + :typographies typographies + :on-open-language-view open-language-view}] - [:> shadow-menu* {:ids ids :values (get shape :shadow)}] + [:> fill/fill-menu* + {:ids ids + :type type + :values fill-values + :applied-tokens applied-tokens}] - [:> blur-menu* - {:ids ids - :values (select-keys shape [:blur :background-blur])}] + [:> stroke-menu* {:ids ids + :type type + :values stroke-values + :disable-stroke-style true + :applied-tokens applied-tokens}] - [:> exports-menu* {:type type - :ids ids - :shapes shapes - :values (select-keys shape exports-attrs) - :page-id page-id - :file-id file-id}]])) + (when (= :multiple (:fills fill-values)) + [:> color-selection-menu* + {:type type + :shapes shapes + :file-id file-id + :libraries libraries}]) + [:> shadow-menu* {:ids ids :values (get shape :shadow)}] + + [:> blur-menu* + {:ids ids + :values (select-keys shape [:blur :background-blur])}] + + [:> exports-menu* {:type type + :ids ids + :shapes shapes + :values (select-keys shape exports-attrs) + :page-id page-id + :file-id file-id}]]))) diff --git a/frontend/src/app/main/ui/workspace/viewport_wasm.cljs b/frontend/src/app/main/ui/workspace/viewport_wasm.cljs index 1cb2ad6c88..11652247c1 100644 --- a/frontend/src/app/main/ui/workspace/viewport_wasm.cljs +++ b/frontend/src/app/main/ui/workspace/viewport_wasm.cljs @@ -30,6 +30,7 @@ [app.main.ui.measurements :as msr] [app.main.ui.workspace.shapes.path.editor :refer [path-editor*]] [app.main.ui.workspace.shapes.text.editor :as editor-v1] + [app.main.ui.workspace.shapes.text.selection-menu :as text-selection-menu] [app.main.ui.workspace.shapes.text.text-edition-outline :refer [text-edition-outline*]] [app.main.ui.workspace.shapes.text.v2-editor :as editor-v2] [app.main.ui.workspace.shapes.text.v3-editor :as editor-v3] @@ -708,7 +709,13 @@ (when picking-color? [:> pixel-overlay/pixel-overlay-wasm* {:viewport-ref viewport-ref - :canvas-ref canvas-ref}])] + :canvas-ref canvas-ref}]) + + (when (and show-text-editor? + (features/active-feature? @st/state "text-editor-wasm/v1")) + [:> text-selection-menu/selection-menu* {:shape editing-shape + :vbox vbox + :zoom zoom}])] [:canvas {:id "render" :data-testid "canvas-wasm-shapes" diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 8fa06ed9bb..d4c2346145 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -124,6 +124,7 @@ [frontend-tests.render-wasm.text-editor-content-range-test] [frontend-tests.render-wasm.text-editor-history-test] [frontend-tests.render-wasm.text-paste-test] + [frontend-tests.render-wasm.texts-test] [frontend-tests.render-wasm.webgl-test] [frontend-tests.router-test] [frontend-tests.svg-fills-test] @@ -155,6 +156,7 @@ [frontend-tests.ui.comment-input-ime-test] [frontend-tests.ui.comments-clustering-test] [frontend-tests.ui.comments-position-modifier-test] + [frontend-tests.ui.css-cursors-test] [frontend-tests.ui.dashboard-import-linking-test] [frontend-tests.ui.drawarea-test] [frontend-tests.ui.ds-controls-numeric-input-test] @@ -162,11 +164,13 @@ [frontend-tests.ui.export-preview-test] [frontend-tests.ui.file-restored-modal-test] [frontend-tests.ui.gradient-handlers-test] + [frontend-tests.ui.grouped-dropdown-test] [frontend-tests.ui.history-test] [frontend-tests.ui.inspect-geometry-test] [frontend-tests.ui.inspect-stroke-tokens-test] [frontend-tests.ui.inspect-text-list-test] [frontend-tests.ui.jobs-progress-test] + [frontend-tests.ui.language-profile-menu-test] [frontend-tests.ui.layout-container-multiple-test] [frontend-tests.ui.mcp-authorize-test] [frontend-tests.ui.measurements-test] @@ -175,6 +179,7 @@ [frontend-tests.ui.routes-test] [frontend-tests.ui.settings-password-schema-test] [frontend-tests.ui.settings-shortcuts-test] + [frontend-tests.ui.settings-text-language-test] [frontend-tests.ui.shape-filters-test] [frontend-tests.ui.shortcuts-labels-test] [frontend-tests.ui.sidebar-scroll-test] @@ -184,6 +189,8 @@ [frontend-tests.ui.subscription-test] [frontend-tests.ui.team-container-test] [frontend-tests.ui.text-attrs-multiple-test] + [frontend-tests.ui.text-options-test] + [frontend-tests.ui.text-selection-menu-test] [frontend-tests.ui.text-v3-editor-test] [frontend-tests.ui.viewport-top-bar-test] [frontend-tests.ui.workspace-libraries-test] @@ -346,6 +353,7 @@ 'frontend-tests.render-wasm.text-editor-history-test 'frontend-tests.render-wasm.text-paste-test 'frontend-tests.render-wasm.webgl-test + 'frontend-tests.render-wasm.texts-test 'frontend-tests.browser-history-test 'frontend-tests.router-test 'frontend-tests.svg-fills-test @@ -377,17 +385,20 @@ 'frontend-tests.ui.comments-clustering-test 'frontend-tests.ui.comments-position-modifier-test 'frontend-tests.ui.dashboard-import-linking-test + 'frontend-tests.ui.css-cursors-test 'frontend-tests.ui.drawarea-test 'frontend-tests.ui.ds-controls-numeric-input-test 'frontend-tests.ui.error-boundary-test 'frontend-tests.ui.export-preview-test 'frontend-tests.ui.file-restored-modal-test 'frontend-tests.ui.gradient-handlers-test + 'frontend-tests.ui.grouped-dropdown-test 'frontend-tests.ui.history-test 'frontend-tests.ui.inspect-geometry-test 'frontend-tests.ui.inspect-stroke-tokens-test 'frontend-tests.ui.inspect-text-list-test 'frontend-tests.ui.jobs-progress-test + 'frontend-tests.ui.language-profile-menu-test 'frontend-tests.ui.layout-container-multiple-test 'frontend-tests.ui.measurements-test 'frontend-tests.ui.measures-menu-props-test @@ -398,6 +409,7 @@ 'frontend-tests.text-editor-paste-guard-test 'frontend-tests.ui.settings-password-schema-test 'frontend-tests.ui.settings-shortcuts-test + 'frontend-tests.ui.settings-text-language-test 'frontend-tests.ui.shape-filters-test 'frontend-tests.ui.shortcuts-labels-test 'frontend-tests.ui.sidebar-scroll-test @@ -407,6 +419,8 @@ 'frontend-tests.ui.subscription-test 'frontend-tests.ui.team-container-test 'frontend-tests.ui.text-attrs-multiple-test + 'frontend-tests.ui.text-options-test + 'frontend-tests.ui.text-selection-menu-test 'frontend-tests.ui.text-v3-editor-test 'frontend-tests.ui.viewport-top-bar-test 'frontend-tests.ui.workspace-libraries-test diff --git a/frontend/test/frontend_tests/ui/grouped_dropdown_test.cljs b/frontend/test/frontend_tests/ui/grouped_dropdown_test.cljs new file mode 100644 index 0000000000..d300995470 --- /dev/null +++ b/frontend/test/frontend_tests/ui/grouped_dropdown_test.cljs @@ -0,0 +1,66 @@ +;; 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.ui.grouped-dropdown-test + (:require + ["react-dom/server" :as rds] + [app.main.ui.components.grouped-dropdown :refer [grouped-dropdown*]] + [app.main.ui.workspace.sidebar.options.menus.measures :as measures] + [cljs.test :as t :include-macros true] + [cuerdas.core :as str] + [rumext.v2 :as mf])) + +(def ^:private sample-options + [{:id "fruits" :label "FRUITS" :header true} + {:id "apple" :label "Apple" :detail "red"} + {:id "pear" :label "Pear" :selected true}]) + +(defn- render + [{:keys [show options search-term on-search-change empty-label] + :or {show true}}] + (rds/renderToStaticMarkup + (mf/element grouped-dropdown* + (mf/props {:show show + :on-close identity + :on-select identity + :options options + :search-term search-term + :on-search-change on-search-change + :empty-label empty-label})))) + +(t/deftest headers-are-not-options + (let [markup (render {:options sample-options})] + (t/is (= 2 (count (re-seq #"role=\"option\"" markup)))) + (t/is (re-find #"role=\"presentation\"[^>]*>FRUITS" markup)) + (t/is (str/includes? markup "red")) + (t/is (= ["false" "true"] + (mapv second (re-seq #"aria-selected=\"([^\"]+)\"" markup)))))) + +(t/deftest search-bar-shows-only-with-a-search-handler + (t/is (not (str/includes? (render {:options sample-options}) " {:type "paragraph" :children [{:text "文"}]} + (some? language) (assoc :text-language language))) + languages)}]}) + +(defn- render-language-section + "Static markup of the Language panel for texts with `language`." + [language] + (with-redefs [refs/texts-language (fn [_] (l/atom language)) + hooks/use-portal-container (fn ([] nil) ([_] nil))] + (rds/renderToStaticMarkup + (mf/element tll/language-section* + (mf/props {:ids [(random-uuid)] + :on-change identity + :on-open-view identity}))))) + +(defn- render-language-view + "Static markup of the language options view for texts with `language`." + [language {:keys [values ruby-values selection reading has-ruby]}] + (with-redefs [refs/texts-language (fn [_] (l/atom language)) + hooks/use-portal-container (fn ([] nil) ([_] nil))] + (rds/renderToStaticMarkup + (mf/element tll/language-view* + (mf/props {:ids [(random-uuid)] + :options-props (mf/props {:values (merge {:text-language language} values) + :ruby-values ruby-values + :text-selection-active (boolean selection) + :reading-selection-active (boolean reading) + :zhuyin-selection-active false + :on-change identity + :on-ruby-presentation-change identity}) + :on-change identity + :on-close identity + :has-ruby (boolean has-ruby)}))))) + +(t/deftest texts-language-is-mixed-when-any-paragraph-differs + (t/is (= "ja" (tl/get-texts-language [(text-content "ja" "ja") (text-content "ja")]))) + (t/is (nil? (tl/get-texts-language [(text-content nil nil)]))) + (t/is (nil? (tl/get-texts-language []))) + (t/is (= :multiple (tl/get-texts-language [(text-content "ja" nil)])) + "a paragraph without language differs from one with it") + (t/is (= :multiple (tl/get-texts-language [(text-content "ko") (text-content "zh-Hant-TW")]))) + (t/is (= :multiple (tl/get-texts-language [(text-content "ko") (text-content nil)]))) + (t/is (= :multiple (tl/get-texts-language [(text-content "ko") nil])) + "a text that has no content yet has no language")) + +(t/deftest language-dropdown-groups-the-regional-languages + (let [options (tl/language-options "zh-Hant-TW")] + (t/is (= ["japanese" "ja" "chinese" + "zh-Hans-CN" "zh-Hant-CN" "zh-Hant-HK" "zh-Hant-TW" + "korean" "ko"] + (mapv :id options))) + (t/is (= ["japanese" "chinese" "korean"] (mapv :id (filter :header options)))) + (t/is (= ["zh-Hant-TW"] (mapv :id (filter :selected options)))) + (t/is (not-any? :detail options) "the list shows no language tags") + (t/is (not-any? :selected (tl/language-options :multiple))))) + +(t/deftest language-row-names-mixed-and-unlisted-languages + (t/is (= "settings.multiple" (tl/language-label :multiple))) + (t/is (= "workspace.options.text-options.language-japanese" (tl/language-label "ja"))) + (t/is (= "workspace.options.text-options.language-chinese-taiwan" (tl/language-label "zh-Hant-TW")) + "a Chinese profile has one full name everywhere") + (t/is (= "zh-Hans" (tl/language-label "zh-Hans")) + "a tag outside the list shows as it is")) + +(t/deftest language-panel-without-language-only-offers-to-add-one + (let [markup (render-language-section nil)] + (t/is (str/includes? markup "data-testid=\"text-language-add\"")) + (t/is (not (str/includes? markup "data-testid=\"text-language-select\""))) + (t/is (not (str/includes? markup "horizontal-tb-writing-mode")) + "writing mode needs a language"))) + +(t/deftest language-panel-offers-the-options-in-their-own-view + (doseq [language ["ja" "ko" "zh-Hant-HK"]] + (let [markup (render-language-section language)] + (t/is (not (str/includes? markup "data-testid=\"text-language-add\""))) + (t/is (str/includes? markup "data-testid=\"text-language-select\"")) + (t/is (str/includes? markup "data-testid=\"text-language-options\"")) + (t/is (not (str/includes? markup "disabled=\"\""))) + (t/is (not (str/includes? markup "data-testid=\"text-language-remove\"")) + "None in the list removes the language") + (t/is (not (str/includes? markup "workspace.options.text-options.line-adjustment")) + "the options are in the view, not in the main sidebar")))) + +(t/deftest mixed-languages-disable-the-options-view + (let [markup (render-language-section :multiple)] + (t/is (str/includes? markup "settings.multiple")) + (t/is (str/includes? markup "disabled=\"\"")) + (t/is (not (str/includes? markup "data-testid=\"text-language-add\""))))) + +(t/deftest new-texts-take-the-last-language-else-the-file-one + (let [language (fn [remembered file] + (with-redefs [refs/default-font (l/atom nil) + refs/default-text-language (l/atom remembered) + refs/file-text-language (l/atom file)] + (get-in (tc/v2-default-text-content) + [:children 0 :children 0 :text-language])))] + (t/is (= "ja" (language nil "ja")) "the file language seeds a new text") + (t/is (= "ko" (language "ko" "ja")) "the language used last wins") + (t/is (nil? (language nil nil))))) + +(t/deftest korean-word-wrapping-is-distinct-from-the-saved-experiment + (t/is (= ["syllable" "keep-words"] + (mapv :id (tll/korean-wrap-options nil identity)))) + (t/is (= ["syllable" "keep-words" "spaces-only"] + (mapv :id (tll/korean-wrap-options "spaces-only" identity))))) + +(t/deftest language-view-shows-the-layout-options-of-the-language + (doseq [[language label] [["ja" "workspace.options.text-options.line-adjustment"] + ["ko" "workspace.options.text-options.korean-wrap"] + ["zh-Hant-TW" "workspace.options.text-options.underline-position"]]] + (let [markup (render-language-view language {})] + (t/is (str/includes? markup "data-testid=\"text-language-back\"")) + (t/is (str/includes? markup "workspace.options.text-options.language-group-layout")) + (t/is (str/includes? markup label) language)))) + +(t/deftest language-view-shows-selected-text-options-only-for-a-selection + (doseq [language ["ja" "zh-Hant-TW"]] + (let [group "workspace.options.text-options.language-group-selected-text" + emphasis "workspace.options.text-options.text-emphasis"] + (t/is (not (str/includes? (render-language-view language {}) group))) + (t/is (str/includes? (render-language-view language {:selection true}) group)) + (t/is (str/includes? (render-language-view language {:selection true}) emphasis)))) + (t/is (not (str/includes? (render-language-view "ja" {}) + "workspace.options.text-options.text-emphasis")) + "Japanese emphasis marks need a text selection") + (t/is (< (str/index-of (render-language-view "ja" {:selection true}) + "workspace.options.text-options.language-group-selected-text") + (str/index-of (render-language-view "ja" {:selection true}) + "workspace.options.text-options.language-group-layout")) + "Selected text comes first")) + +(t/deftest language-view-shows-ruby-options-only-for-text-with-ruby + (let [group "workspace.options.text-options.language-group-ruby"] + (t/is (not (str/includes? (render-language-view "ja" {}) group))) + (t/is (not (str/includes? (render-language-view "ja" {}) "workspace.options.text-options.annotation-clearance")) + "clearance shows with the readings") + (let [markup (render-language-view "ja" {:has-ruby true})] + (t/is (str/includes? markup group)) + (doseq [label ["annotation-clearance" "ruby-size" "ruby-align" "ruby-overhang" "ruby-side" "ruby-hidden"]] + (t/is (str/includes? markup (str "workspace.options.text-options." label)) label))) + (t/is (not (str/includes? (render-language-view "ko" {:has-ruby true}) group))))) + +(defn- count-of + "Times the translation key `text` shows in `markup`, as a whole key." + [markup text] + (count (re-seq (js/RegExp. (str (str/replace text "." "\\.") "(?![-\\w])") "g") + markup))) + +(t/deftest chinese-layout-keeps-emphasis-and-clearance-of-the-whole-text + (let [emphasis "workspace.options.text-options.text-emphasis" + position "workspace.options.text-options.emphasis-position" + clearance "workspace.options.text-options.annotation-clearance" + whole (render-language-view "zh-Hant-TW" {}) + selection (render-language-view "zh-Hant-TW" {:selection true})] + (t/is (pos? (count-of whole emphasis)) "emphasis marks apply to the whole text") + (t/is (str/includes? whole position)) + (t/is (str/includes? whole clearance) "clearance also spaces emphasis marks") + (t/is (= (count-of whole emphasis) (count-of selection emphasis)) + "with a selection, emphasis marks show once, for the selection") + (t/is (= (count-of (render-language-view "ja" {:has-ruby true}) clearance) + (count-of (render-language-view "zh-Hant-TW" {:has-ruby true}) clearance)) + "with readings, clearance shows once, in the Ruby group"))) + +(t/deftest zhuyin-explains-what-selection-it-needs + (let [hint "workspace.options.text-options.zhuyin-selection-hint"] + (t/is (str/includes? (render-language-view "zh-Hant-TW" {:values {:ruby-zhuyin true} + :selection true + :reading true}) + hint)) + (t/is (not (str/includes? (render-language-view "zh-Hant-TW" {:selection true + :reading true}) + hint))))) + +(t/deftest chinese-reading-input-needs-a-reading-selection + (doseq [reading [false true]] + (let [markup (render-language-view "zh-Hant-TW" {:values {:ruby "nǐ hǎo"} + :selection true + :reading reading})] + (t/is (= reading (str/includes? markup "workspace.options.text-options.pinyin-placeholder"))) + (t/is (not (str/includes? markup "selection-hint")) "no permanent hint")))) + +(t/deftest text-emphasis-select-reports-and-restores-canonical-values + (let [options (tcc/text-emphasis-options identity) + reported-id (:id (select/get-option options "filled-dot"))] + (t/is (= "filled-dot" reported-id)) + (t/is (= "filled-dot" + (:id (select/get-option options reported-id)))))) + +(t/deftest annotation-clearance-select-reports-canonical-values + (let [options (tcc/annotation-clearance-options identity)] + (t/is (= ["none" "auto"] (mapv :id options))) + (t/is (= "auto" (:id (select/get-option options "auto")))))) + +(t/deftest whole-text-tate-chu-yoko-and-selection-combine-are-apart + (t/is (= ["none" "digits"] (mapv :value (tll/tate-chu-yoko-options identity)))) + (t/is (= ["none" "all"] (mapv :value (tll/combine-options identity))))) + +(t/deftest ruby-container-styles-preserve-customization + (let [style (text-styles/generate-ruby-container-styles + {:ruby-align "space-between" + :ruby-overhang "none" + :ruby-side "under"})] + (t/is (= "space-between" (.-rubyAlign style))) + (t/is (= "none" (.-rubyOverhang style))) + (t/is (= "under" (.-rubyPosition style))))) + +(t/deftest advanced-ruby-options-of-a-selection-are-collapsed-by-default + (with-redefs [hooks/use-portal-container (fn ([] nil) ([_] nil))] + (let [markup (rds/renderToStaticMarkup + (mf/element tcc/ruby-advanced-options* + #js {:values {} + :labels {:input "workspace.options.text-options.ruby" + :placeholder "workspace.options.text-options.ruby-placeholder" + :advanced "workspace.options.text-options.ruby-advanced-options"} + :on-change identity + :on-blur identity}))] + (t/is (str/includes? markup "data-testid=\"ruby-advanced-options-toggle\"")) + (t/is (str/includes? markup "aria-expanded=\"false\""))))) + +(t/deftest advanced-furigana-options-include-hide-toggle + (let [markup (rds/renderToStaticMarkup + (mf/element tcc/ruby-hidden-option* + #js {:values {:ruby-hidden true} + :on-change identity + :on-blur identity}))] + (t/is (str/includes? markup "workspace.options.text-options.ruby-hidden")) + (t/is (str/includes? markup "role=\"switch\"")) + (t/is (str/includes? markup "aria-checked=\"true\"")))) + +(t/deftest hidden-furigana-renders-base-text-without-ruby-layout + (let [markup (rds/renderToStaticMarkup + (mf/element html-text/render-text* + #js {:node {:text "日" + :ruby "にち" + :ruby-hidden true} + :parent {} + :shape {}}))] + (t/is (str/includes? markup ">日")) + (t/is (not (str/includes? markup "]*data-testid=\"text-align-distribute\"" %))] + (t/is (< (str/index-of (markup "left") "horizontal-tb-writing-mode") + (str/index-of (markup "left") "data-testid=\"text-align-distribute\""))) + (t/is (= "true" (pressed (markup "distribute")))) + (t/is (= "false" (pressed (markup "left")))) + (t/is (= "false" (pressed (markup :multiple)))))) + +(t/deftest writing-mode-row-shows-orientation-only-for-vertical-text + (t/is (not (str/includes? (alignment-markup tcc/writing-mode-row* "horizontal-tb" true) + "mixed-text-orientation"))) + (t/is (str/includes? (alignment-markup tcc/writing-mode-row* "vertical-rl" true) + "mixed-text-orientation"))) + +(t/deftest writing-mode-row-needs-a-cjk-language + (t/is (true? (tcc/cjk-values? {:text-language "ja"}))) + (t/is (true? (tcc/cjk-values? {:text-language :multiple}))) + (t/is (false? (tcc/cjk-values? {:text-language "en"}))) + (t/is (false? (tcc/cjk-values? {})))) + +(t/deftest vertical-column-block-alignment-shows-right-center-and-left + (let [markup (alignment-markup text-menu/vertical-align* "vertical-rl" true)] + (t/is (= ["text-right" "text-horizontal-center" "text-left"] + (alignment-icons markup))) + (t/is (= ["workspace.options.text-options.text-align-right" + "workspace.options.text-options.text-align-center" + "workspace.options.text-options.text-align-left"] + (alignment-labels markup))) + (t/is (= ["top" "center" "bottom"] + (mapv second (re-seq #"value=\"([^\"]+)\"" markup)))))) + +(t/deftest horizontal-and-mixed-writing-modes-use-horizontal-alignment-controls + (doseq [writing-mode [nil "horizontal-tb" :multiple]] + (let [paragraph (alignment-markup text-menu/text-align-options* writing-mode true) + block (alignment-markup text-menu/vertical-align* writing-mode true)] + (t/is (= ["text-align-left" "text-align-center" "text-align-right" "text-justify"] + (alignment-icons paragraph))) + (t/is (= ["text-top" "text-middle" "text-bottom"] (alignment-icons block)))))) + +(t/deftest svg-renderer-keeps-horizontal-alignment-for-stored-vertical-text + (let [paragraph (alignment-markup text-menu/text-align-options* "vertical-rl" false) + block (alignment-markup text-menu/vertical-align* "vertical-rl" false)] + (t/is (= ["text-align-left" "text-align-center" "text-align-right" "text-justify"] + (alignment-icons paragraph))) + (t/is (= ["text-top" "text-middle" "text-bottom"] (alignment-icons block))) + (t/is (= "workspace.options.text-options.text-align-left" (first (alignment-labels paragraph)))) + (t/is (= "workspace.options.text-options.align-top" (first (alignment-labels block)))))) + + +(t/deftest vertical-text-growth-controls-show-the-physical-growth-direction + (let [markup (alignment-markup text-menu/grow-options* "vertical-rl" true)] + (t/is (= ["text-fixed" "text-auto-width-vertical" "text-auto-height-vertical"] + (alignment-icons markup))) + (t/is (= ["workspace.options.text-options.grow-fixed" + "workspace.options.text-options.grow-auto-height" + "workspace.options.text-options.grow-auto-width"] + (alignment-labels markup))) + (t/is (= ["fixed" "auto-width" "auto-height"] + (mapv second (re-seq #"value=\"([^\"]+)\"" markup)))))) + +(t/deftest horizontal-and-svg-text-growth-controls-keep-horizontal-directions + (doseq [[writing-mode wasm-enabled] [[nil true] + ["horizontal-tb" true] + [:multiple true] + ["vertical-rl" false]]] + (let [markup (alignment-markup text-menu/grow-options* writing-mode wasm-enabled)] + (t/is (= ["text-fixed" "text-auto-width" "text-auto-height"] + (alignment-icons markup))) + (t/is (= ["workspace.options.text-options.grow-fixed" + "workspace.options.text-options.grow-auto-width" + "workspace.options.text-options.grow-auto-height"] + (alignment-labels markup)))))) + + +(t/deftest japanese-layout-controls-include-line-adjustment + (let [markup (alignment-markup tll/japanese-layout-options* "vertical-rl" true)] + (t/is (str/includes? markup "workspace.options.text-options.line-adjustment")))) + + +(t/deftest vertical-text-hides-ltr-and-rtl-controls + (t/is (empty? (alignment-markup text-menu/text-direction-options* "vertical-rl" true)))) + +(t/deftest horizontal-and-svg-text-show-ltr-and-rtl-controls + (doseq [[writing-mode wasm-enabled] [[nil true] + ["horizontal-tb" true] + [:multiple true] + ["vertical-rl" false]]] + (let [markup (alignment-markup text-menu/text-direction-options* writing-mode wasm-enabled)] + (t/is (= ["text-ltr" "text-rtl"] (alignment-icons markup)))))) + +(t/deftest hiding-direction-controls-preserves-the-stored-direction + (let [values {:text-direction "rtl"} + render #(alignment-markup text-menu/text-direction-options* % true values)] + (t/is (str/includes? (re-find #"]*id=\"rtl-text-direction\"[^>]*>" + (render "horizontal-tb")) + "checked=\"\"")) + (t/is (empty? (render "vertical-rl"))) + (t/is (str/includes? (re-find #"]*id=\"rtl-text-direction\"[^>]*>" + (render "horizontal-tb")) + "checked=\"\"")))) + + +(t/deftest decoration-position-changes-refresh-the-text-options + (doseq [attr [:text-emphasis-position :text-underline-position]] + (let [before (mf/props {:values {attr "auto"}}) + after (mf/props {:values {attr "under-left"}})] + (t/is (false? (@#'text-menu/check-props before after)))))) + +(t/deftest zhuyin-handoff-sets-symbols-upright-in-a-right-side-strip + (let [html (rds/renderToStaticMarkup + (mf/element html-text/render-text* + {:node {:text "中" :ruby "ㄋㄧˇ" :ruby-zhuyin true :font-size "40"} + :shape {} :parent {}}))] + (t/is (str/includes? html "width:1.6em")) + (t/is (str/includes? html "writing-mode:horizontal-tb")) + (t/is (str/includes? html "ㄋ")) + (t/is (str/includes? html "ㄧ")) + (t/is (str/includes? html "ˇ")) + (t/is (not (str/includes? html "