From 11e1647b7f57c8205488a70f9735f5a32aa4a705 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 24 Nov 2026 23:01:18 +0100 Subject: [PATCH] :sparkles: Apply the language view review tweaks The language view follows the review of the demo build: - A divider sets each group apart. Clearance is always a Layout option. - The reading field takes the format's name as placeholder, an eye button hides the readings, and its options open in place, two to a row, until the button is pressed again or the selection changes. - The readings group of the whole text takes the same style and shows only while no text is selected. Japanese calls readings furigana, and side reads Over / right or Under / left in both writing modes. - Japanese tate-chu-yoko joins the writing mode row, with the digit count in its own row; warichu and combine get labelled rows. - Chinese emphasis marks are selection options only. Readings default to zhuyin in Taiwan and Hong Kong while the selection can take it. - Korean texts given the language break by word, now "By word". Texts that already have a wrapping, or none, keep it. AI-assisted-by: claude-opus-5-5 --- .../src/app/common/types/text/cjk_layout.cljc | 18 ++ .../workspace/sidebar/options/menus/text.cljs | 7 +- .../options/menus/text_cjk_controls.cljs | 164 +++++++---- .../options/menus/text_cjk_controls.scss | 31 +- .../options/menus/text_language_layout.cljs | 268 +++++++++++------- .../options/menus/text_language_layout.scss | 24 +- .../sidebar/options/shapes/text.cljs | 12 + frontend/src/app/util/text/content.cljs | 3 +- .../frontend_tests/ui/text_options_test.cljs | 176 +++++++++--- frontend/translations/en.po | 46 ++- frontend/translations/es.po | 44 ++- frontend/translations/jpn_JP.po | 4 - 12 files changed, 553 insertions(+), 244 deletions(-) diff --git a/common/src/app/common/types/text/cjk_layout.cljc b/common/src/app/common/types/text/cjk_layout.cljc index dee58de95e..65956d1f21 100644 --- a/common/src/app/common/types/text/cjk_layout.cljc +++ b/common/src/app/common/types/text/cjk_layout.cljc @@ -312,6 +312,24 @@ "ko" :korean nil))) +(defn zhuyin-default? + "True when new readings of a Chinese text with the language `tag` are + zhuyin rather than pinyin: in Taiwan and Hong Kong." + [tag] + (boolean + (and (= :chinese (language-family tag)) + (some #{"TW" "HK"} (map str/upper (rest (str/split tag "-"))))))) + +(defn chosen-language-attrs + "Paragraph attrs a text takes when `tag` is chosen as its language. A + Korean text breaks between words unless it already has a wrapping in + `values`; texts without one keep breaking between syllables." + [tag values] + (cond-> {:text-language tag} + (and (= :korean (language-family tag)) + (nil? (:korean-wrap values))) + (assoc :korean-wrap "keep-words"))) + (def ^:private ruby-attrs (into #{:ruby :ruby-group} ruby-presentation-attrs)) 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 6aaadf5008..20cd1c41b2 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 @@ -297,6 +297,8 @@ (unchecked-get o-props "typographies")) (identical? (unchecked-get n-props "textSelectionActive") (unchecked-get o-props "textSelectionActive")) + (identical? (unchecked-get n-props "textSelectionKey") + (unchecked-get o-props "textSelectionKey")) (identical? (unchecked-get n-props "readingSelectionActive") (unchecked-get o-props "readingSelectionActive")) (identical? (unchecked-get n-props "languageView") @@ -357,7 +359,8 @@ `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 ruby-values text-selection-active reading-selection-active zhuyin-selection-active + [{:keys [ids type values ruby-values text-selection-active text-selection-key + reading-selection-active zhuyin-selection-active applied-tokens libraries file-id typographies language-view on-open-language-view on-close-language-view has-ruby]}] @@ -562,6 +565,7 @@ :values values :ruby-values ruby-values :text-selection-active text-selection-active + :selection-key text-selection-key :reading-selection-active reading-selection-active :zhuyin-selection-active zhuyin-selection-active :on-change on-change @@ -693,5 +697,6 @@ (when (and ^boolean cjk-feature? ^boolean (wm/vertical-layout-active?)) [:> tll/language-section* {:ids ids + :values values :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 index fd80b42f41..b0926fad89 100644 --- 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 @@ -15,10 +15,9 @@ [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.main.ui.workspace.sidebar.options.common :refer [radio-selected]] [app.util.dom :as dom] [app.util.i18n :refer [tr]] [rumext.v2 :as mf])) @@ -203,10 +202,12 @@ (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]}] + orientation for vertical text, the radio groups in `children`, and + Distribute at the end." + [{:keys [values on-change on-blur children]}] [:div {:class (stl/css :writing-mode-row)} - [:> writing-mode-controls* {:values values :on-change on-change :on-blur on-blur}] + [:> writing-mode-controls* {:values values :on-change on-change :on-blur on-blur} + children] [:> distribute-toggle* {:values values :on-change on-change :on-blur on-blur}]]) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -249,6 +250,14 @@ (or block inline) (or block inline) :else "auto"))) +(defn ruby-side-options + "Sides of a reading. Ruby takes the block side in both writing modes: + over the line in horizontal text is right of the column in vertical + text, so each choice names both." + [translate] + [{:id "over" :label (translate "workspace.options.text-options.ruby-side-over")} + {:id "under" :label (translate "workspace.options.text-options.ruby-side-under")}]) + (defn position-options "Choices of the position `attr` for the writing mode." [attr vertical] @@ -256,11 +265,9 @@ 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")]] + left {:id "left" :label (tr "workspace.options.text-options.position-left")}] (case attr - :ruby-side (if ^boolean vertical ruby-v [over under]) + :ruby-side (ruby-side-options tr) :text-emphasis-position (if ^boolean vertical [auto right left] [auto over under]) :text-underline-position (if ^boolean vertical [auto right left] [auto under])))) @@ -397,8 +404,31 @@ [{:id "auto" :label (translate "workspace.options.text-options.ruby-overhang-auto")} {:id "none" :label (translate "workspace.options.text-options.ruby-overhang-none")}]) +;; The labels of a reading row: `:placeholder` names the reading format +;; (Furigana, Pinyin, Zhuyin) and also names the input; `:hidden`/`:shown` +;; name the eye button; `:advanced` the button opening its options. + +(mf/defc ruby-visibility-button* + "Eye button hiding or showing the readings of `values`. Mixed values show + as shown, and the click hides them all." + [{:keys [values labels on-change on-blur]}] + (let [hidden? (true? (:ruby-hidden values)) + handle-click + (mf/use-fn + (mf/deps hidden? on-change on-blur) + (fn [_event] + (on-change {:ruby-hidden (not hidden?)}) + (when (some? on-blur) (on-blur))))] + [:> icon-button* {:variant "ghost" + :aria-label (if hidden? (:shown labels) (:hidden labels)) + :aria-pressed hidden? + :data-testid "ruby-visibility-toggle" + :on-click handle-click + :icon (if hidden? i/hide i/shown)}])) + (mf/defc ruby-options* - "Input of the reading of the selected span." + "Input of the reading of the selected span, with the eye button and, + given `on-toggle-advanced`, the button opening the reading options." [{:keys [values on-change on-blur advanced-open on-toggle-advanced labels]}] (let [ruby (span-input-value (:ruby values)) ruby* (mf/use-state ruby) @@ -436,14 +466,14 @@ [:div {:class (stl/css :ruby-options)} [:> input* {:class (stl/css :ruby-input) - :label (:input labels) + :aria-label (:placeholder 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)]) + [:> ruby-visibility-button* {:values values :labels labels + :on-change on-change :on-blur on-blur}] (when on-toggle-advanced [:> icon-button* {:variant "ghost" :selected advanced-open @@ -451,28 +481,9 @@ :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. It reads - `(:hidden labels)` when given." - [{:keys [values labels on-change on-blur]}] - (let [value (:ruby-hidden values) - enabled? (cond - (mixed-span-value? value) nil - (true? value) true - :else false) - label (or (:hidden labels) (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}]])) + :icon i/menu}]) + (when-not valid? + [:p {:class (stl/css :ruby-invalid) :role "alert"} (:invalid labels)])])) (defn- ruby-common-props [values on-change on-blur labels vertical] @@ -484,7 +495,7 @@ :vertical vertical})) (mf/defc ruby-presentation-fields* - "Size, alignment, overhang and side of the readings." + "Size, alignment, overhang and side of the readings, one row each." [{: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 [] @@ -511,24 +522,73 @@ {: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-presentation-cell* + "One reading option of `ruby-presentation-grid*`: its label over its + select, with a disabled entry for mixed values." + {::mf/private true} + [{:keys [values on-change on-blur attr label options]}] + (let [value (get values attr) + selected (span-select-value value (cjk/enum-default attr)) + 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))))] + [:div {:class (stl/css :ruby-presentation-cell)} + [:span {:class (stl/css :option-label)} label] + [:> select* {:default-selected selected + :aria-label label + :options options + :on-change handle-change}]])) + +(mf/defc ruby-presentation-grid* + "Size and side, then alignment and overhang of the readings, two to a + row in a light box. Side names both writing modes, so it reads the same + in horizontal and vertical text." + [{:keys [values on-change on-blur class]}] + (let [props (mf/props {:values values :on-change on-change :on-blur on-blur}) + options (mf/with-memo [] + {:ruby-size (ruby-size-options tr) + :ruby-side (ruby-side-options tr) + :ruby-align (ruby-align-options tr) + :ruby-overhang (ruby-overhang-options tr)})] + [:div {:class [class (stl/css :ruby-presentation-grid)] + :data-testid "ruby-presentation-options"} + [:> ruby-presentation-cell* (mf/spread-props props {:attr :ruby-size + :label (tr "workspace.options.text-options.ruby-size") + :options (:ruby-size options)})] + [:> ruby-presentation-cell* (mf/spread-props props {:attr :ruby-side + :label (tr "workspace.options.text-options.ruby-side") + :options (:ruby-side options)})] + [:> ruby-presentation-cell* (mf/spread-props props {:attr :ruby-align + :label (tr "workspace.options.text-options.ruby-align") + :options (:ruby-align options)})] + [:> ruby-presentation-cell* (mf/spread-props props {:attr :ruby-overhang + :label (tr "workspace.options.text-options.ruby-overhang") + :options (:ruby-overhang options)})]])) (mf/defc ruby-advanced-options* - "Reading input of a text selection, with its presentation options." - [{:keys [values on-change on-blur labels vertical]}] + "Reading input of a text selection. Its ⋮ button opens the reading + options in place, below it; they close on the button again or when + `selection-key` changes: another range was selected." + [{:keys [values on-change on-blur labels selection-key]}] (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)] + toggle-open (mf/use-fn #(swap! open* not))] + + (mf/with-effect [selection-key] + (reset! open* false)) + [: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]]])) + [:> ruby-options* {:values values + :on-change on-change + :on-blur on-blur + :labels labels + :advanced-open open? + :on-toggle-advanced toggle-open}] + (when ^boolean open? + [:> ruby-presentation-grid* {:values values + :on-change on-change + :on-blur on-blur}])])) 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 index ca5aee4746..aad451fb4e 100644 --- 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 @@ -101,12 +101,13 @@ .ruby-advanced-options { display: flex; flex-direction: column; - overflow: visible; + gap: var(--sp-xs); } .ruby-options { display: flex; - align-items: flex-end; + flex-wrap: wrap; + align-items: center; gap: var(--sp-xs); } @@ -114,11 +115,27 @@ flex: 1; } -.ruby-advanced-content { - display: flex; - flex-direction: column; - gap: var(--sp-xs); - padding: var(--sp-xs); +.ruby-invalid { + @include t.use-typography("body-small"); + + flex-basis: 100%; + margin: 0; + color: var(--color-foreground-error); +} + +// The reading options, two to a row in a light box. +.ruby-presentation-grid { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: var(--sp-s) var(--sp-xs); + padding: var(--sp-s); border: $b-1 solid var(--color-background-quaternary); border-radius: $br-8; } + +.ruby-presentation-cell { + display: flex; + flex-direction: column; + gap: var(--sp-xs); + min-inline-size: 0; +} 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 index eb015f8d6d..bf6232bf30 100644 --- 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 @@ -97,35 +97,40 @@ :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." +(mf/defc tate-chu-yoko-buttons* + "Tate-chu-yoko of vertical text, none or the digit runs, as a radio group + of the writing mode row." [{: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 + options (mf/with-memo [] (tate-chu-yoko-options tr))] + [:> tcc/attr-radio-options* {:values {:text-combine-upright + (if (cjk/digit-combine? value) "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 options}])) + +(mf/defc combine-digits-option* + "Longest digit run tate-chu-yoko sets upright, shown while it combines + digits." + [{:keys [values on-change on-blur]}] + (let [value (text-combine-upright-value values) + label (tr "workspace.options.text-options.text-combine-upright-digits-count") + options (mf/with-memo [] (text-combine-upright-count-options tr)) + handle-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}]])])) + (when (cjk/digit-combine? value) + [:> tcc/option-row* {:label label} + [:> select* {:default-selected value + :aria-label label + :options options + :on-change handle-change}]]))) (mf/defc font-features-option* "Proportional metric alternates, per span: `palt` for horizontal text, @@ -165,33 +170,41 @@ (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")}) + {:placeholder (tr "workspace.options.text-options.ruby-placeholder") + :advanced (tr "workspace.options.text-options.ruby-advanced-options") + :hidden (tr "workspace.options.text-options.ruby-hidden") + :shown (tr "workspace.options.text-options.ruby-shown")}) (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]}] + warichu and, in vertical text, combine. `selection-key` names the + selected range." + [{:keys [values on-change on-blur selection-key]}] (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)}) + :vertical vertical?}) + labels (mf/with-memo [] (japanese-reading-labels)) options (mf/with-memo [] {:warichu (warichu-options tr) :combine (combine-options tr)})] [:* - [:> tcc/ruby-advanced-options* props] + [:> tcc/ruby-advanced-options* {:values values + :on-change on-change + :on-blur on-blur + :labels labels + :selection-key selection-key}] ;; Emphasis marks (圏点 / bouten) apply to the selected characters. [:> tcc/text-emphasis-option* props] - [:div {:class (stl/css :icon-options)} + [:> tcc/option-row* {:label (tr "workspace.options.text-options.warichu") :switch true} [:> 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? + :options (:warichu options)}]] + (when ^boolean vertical? + [:> tcc/option-row* {:label (tr "workspace.options.text-options.combine-characters") :switch true} [:> tcc/attr-radio-options* {:values {:text-combine-upright (if (= "all" (text-combine-upright-value values)) "all" "none")} :on-change on-change @@ -199,17 +212,19 @@ :attr :text-combine-upright :default-value (cjk/enum-default :text-combine-upright) :name "combine-upright-options" - :options (:combine options)}])]])) + :options (:combine options)}]])])) (mf/defc japanese-layout-options* - "Japanese options of the whole text." + "Japanese options of the whole text. Tate-chu-yoko itself is in the + writing mode row; the digit count follows it." [{: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]) + [:> combine-digits-option* props]) [:> font-features-option* props] - [:> line-adjustment-option* props]])) + [:> line-adjustment-option* props] + [:> tcc/annotation-clearance-option* props]])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Chinese @@ -218,30 +233,69 @@ (defn- chinese-reading-labels [zhuyin?] (if zhuyin? - {:input (tr "workspace.options.text-options.zhuyin") - :placeholder (tr "workspace.options.text-options.zhuyin-placeholder") + {:placeholder (tr "workspace.options.text-options.zhuyin-placeholder") :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") + :invalid (tr "workspace.options.text-options.zhuyin-invalid") + :hidden (tr "workspace.options.text-options.zhuyin-hidden") + :shown (tr "workspace.options.text-options.zhuyin-shown")} + {:placeholder (tr "workspace.options.text-options.pinyin-placeholder") :advanced (tr "workspace.options.text-options.pinyin-advanced-options") - :hidden (tr "workspace.options.text-options.pinyin-hidden")})) + :hidden (tr "workspace.options.text-options.pinyin-hidden") + :shown (tr "workspace.options.text-options.pinyin-shown")})) + +(defn reading-zhuyin? + "Whether the reading of a Chinese text selection with `values` is + zhuyin: a reading keeps its format; a selection without one, which the + editor reports as not zhuyin, takes zhuyin when it can + (`zhuyin-selection-active`) and the language defaults to it." + [values zhuyin-selection-active] + (let [stored (:ruby-zhuyin values) + ruby (:ruby values)] + (cond + (true? stored) true + (tcc/mixed-span-value? stored) false + (not (or (nil? ruby) (= "" ruby))) false + :else (and (true? zhuyin-selection-active) + (cjk/zhuyin-default? (:text-language values)))))) (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)) + character at a time. `selection-key` names the selected range." + [{:keys [values reading-selection-active zhuyin-selection-active selection-key on-change on-blur]}] + (let [;; The format picked for this range, before it has a reading. + chosen* (mf/use-state nil) + chosen (deref chosen*) + zhuyin? (if (some? chosen) + (and (= "zhuyin" chosen) (true? zhuyin-selection-active)) + (reading-zhuyin? values zhuyin-selection-active)) + stored? (true? (:ruby-zhuyin values)) vertical? (tcc/vertical-writing? values) format-label (tr "workspace.options.text-options.chinese-reading-format") + labels (mf/with-memo [zhuyin?] (chinese-reading-labels zhuyin?)) handle-format-change (mf/use-fn (mf/deps on-change) - (fn [value] (on-change {:ruby-zhuyin (= "zhuyin" value)}))) + (fn [value] + (reset! chosen* value) + (on-change {:ruby-zhuyin (= "zhuyin" value)}))) + ;; A reading typed under the zhuyin default stores the format too: + ;; a reading without one is pinyin. + handle-reading-change + (mf/use-fn + (mf/deps zhuyin? stored? on-change) + (fn [attrs] + (on-change (cond-> attrs + (and zhuyin? (not stored?) (contains? attrs :ruby)) + (assoc :ruby-zhuyin true))))) + reading-props (mf/props {:values values :on-change handle-reading-change :on-blur on-blur + :labels labels :selection-key selection-key}) props (mf/props {:values values :on-change on-change :on-blur on-blur - :vertical vertical? - :labels (chinese-reading-labels zhuyin?)})] + :vertical vertical?})] + + (mf/with-effect [selection-key] + (reset! chosen* nil)) + [:* (when ^boolean reading-selection-active [:* @@ -254,10 +308,10 @@ :on-change handle-format-change}]] (cond (not zhuyin?) - [:> tcc/ruby-advanced-options* props] + [:> tcc/ruby-advanced-options* reading-props] ^boolean zhuyin-selection-active - [:> tcc/ruby-options* props] + [:> tcc/ruby-options* reading-props] :else [:p {:class (stl/css :selection-hint)} @@ -268,24 +322,16 @@ :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]}] + "Chinese options of the whole text. Emphasis marks apply to a text + selection, so they are not here." + [{: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)})] [:* - (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])])) + [:> tcc/annotation-clearance-option* props]])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Korean @@ -313,38 +359,40 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn ruby-group-labels - "Title and hide switch label of the readings group of a `family` text: - ruby for Japanese; pinyin, zhuyin or, when the readings mix both + "Title and eye button labels of the readings group of a `family` text: + furigana for Japanese; pinyin, zhuyin or, when the readings mix both (`zhuyin` is `:multiple`), readings for Chinese." [family zhuyin] (cond (not= family :chinese) - {:title (tr "workspace.options.text-options.language-group-ruby")} + {:title (tr "workspace.options.text-options.language-group-ruby") + :hidden (tr "workspace.options.text-options.ruby-hidden") + :shown (tr "workspace.options.text-options.ruby-shown")} (true? zhuyin) {:title (tr "workspace.options.text-options.zhuyin") - :hidden (tr "workspace.options.text-options.zhuyin-hidden")} + :hidden (tr "workspace.options.text-options.zhuyin-hidden") + :shown (tr "workspace.options.text-options.zhuyin-shown")} (= :multiple zhuyin) {:title (tr "workspace.options.text-options.language-group-readings") - :hidden (tr "workspace.options.text-options.readings-hidden")} + :hidden (tr "workspace.options.text-options.readings-hidden") + :shown (tr "workspace.options.text-options.readings-shown")} :else {:title (tr "workspace.options.text-options.pinyin") - :hidden (tr "workspace.options.text-options.pinyin-hidden")})) + :hidden (tr "workspace.options.text-options.pinyin-hidden") + :shown (tr "workspace.options.text-options.pinyin-shown")})) (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 labels on-change on-blur]}] - (let [props (mf/props {:values values :labels labels :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]])) + whole text, and `on-change` applies to every reading of the text. Zhuyin + readings can only be hidden." + [{:keys [values on-change on-blur]}] + (when-not (true? (:ruby-zhuyin values)) + [:> tcc/ruby-presentation-grid* {:values values + :on-change on-change + :on-blur on-blur}])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Language panel and view @@ -400,13 +448,14 @@ :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] + "Handler giving the texts with the option `values` the language `tag` + (nil removes it); new texts take it too. `on-chosen` runs after a CJK + language is chosen." + [on-change on-chosen values] (mf/use-fn - (mf/deps on-change on-chosen) + (mf/deps on-change on-chosen values) (fn [tag] - (on-change {:text-language tag}) + (on-change (cjk/chosen-language-attrs tag values)) (st/emit! (dwt/remember-text-language tag)) (when (and (some? on-chosen) (some? (cjk/language-family tag))) (on-chosen))))) @@ -417,7 +466,7 @@ 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]}] + [{:keys [ids values 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) @@ -435,7 +484,7 @@ 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) + select-language (use-select-language on-change on-open-view values) handle-add (mf/use-fn @@ -481,18 +530,23 @@ :icon i/arrow-right}])]])])) (mf/defc language-group* + "Group of the language view under a divider: a `title`, the buttons in + `actions` at its end, and the rows in `children`." {::mf/private true} - [{:keys [title children]}] + [{:keys [title actions children]}] [:div {:class (stl/css :language-group)} - [:h3 {:class (stl/css :language-group-title)} title] + [:div {:class (stl/css :language-group-header)} + [:h3 {:class (stl/css :language-group-title)} title] + actions] 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`). - Layout starts with the writing mode row of the Text section, so the - view holds every option of the language. + first, then Layout, then the readings of the whole text when the text + has some (`has-ruby`) and no text is selected. Layout starts with the + writing mode row of the Text section, with tate-chu-yoko for Japanese, + so the view holds every option of the language. 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)) @@ -505,15 +559,9 @@ on-blur (unchecked-get options-props "onBlur") on-ruby-presentation-change (unchecked-get options-props "onRubyPresentationChange") - select-language (use-select-language on-change nil) + select-language (use-select-language on-change nil values) - ruby-labels (ruby-group-labels family (:ruby-zhuyin ruby-values)) - - ruby-props - (mf/props {:values (assoc ruby-values :writing-mode (:writing-mode values)) - :labels ruby-labels - :on-change on-ruby-presentation-change - :on-blur on-blur})] + ruby-labels (ruby-group-labels family (:ruby-zhuyin ruby-values))] (mf/with-effect [family] (when (nil? family) @@ -543,13 +591,25 @@ (when (some? family) [:> language-group* {:title (tr "workspace.options.text-options.language-group-layout")} (when ^boolean (wm/vertical-layout-active?) - [:> tcc/writing-mode-row* {:values values :on-change on-change :on-blur on-blur}]) + [:> tcc/writing-mode-row* {:values values :on-change on-change :on-blur on-blur} + (when (and (= :japanese family) (tcc/vertical-writing? values)) + [:> tate-chu-yoko-buttons* {:values values :on-change on-change :on-blur on-blur}])]) (case family :japanese [:> japanese-layout-options* options-props] - :chinese [:> chinese-layout-options* - (mf/spread-props options-props {:has-ruby has-ruby})] + :chinese [:> chinese-layout-options* options-props] :korean [:> korean-layout-options* options-props])]) - (when (and ^boolean has-ruby (not= :korean family) (some? family)) - [:> language-group* {:title (:title ruby-labels)} - [:> ruby-group-options* ruby-props]])])) + (when (and ^boolean has-ruby + (not ^boolean text-selection-active) + (not= :korean family) + (some? family)) + [:> language-group* + {:title (:title ruby-labels) + :actions (mf/html + [:> tcc/ruby-visibility-button* {:values ruby-values + :labels ruby-labels + :on-change on-ruby-presentation-change + :on-blur on-blur}])} + [:> ruby-group-options* {:values ruby-values + :on-change on-ruby-presentation-change + :on-blur on-blur}]])])) 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 index 75b9b9b929..2bf574b6db 100644 --- 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 @@ -101,18 +101,6 @@ } } -.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 { @@ -135,10 +123,22 @@ color: var(--color-foreground-primary); } +// Each group sits under a divider, apart from the language and the other +// groups. .language-group { display: flex; flex-direction: column; gap: var(--sp-xs); + margin-block-start: var(--sp-xs); + padding-block-start: var(--sp-s); + border-block-start: $b-1 solid var(--color-background-quaternary); +} + +.language-group-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: var(--sp-xs); } .selection-hint { 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 463115531d..8e713edead 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 @@ -151,6 +151,9 @@ ;; stays stable when feature flags change at runtime (e.g. switching ;; renderer while editing a text). The selection happens afterwards. wasm-editor-styles-map (mf/deref refs/workspace-wasm-editor-styles) + ;; Changes with the WASM editor selection: renders the options + ;; again for a new range, even one with the same styles. + _selection-rects (mf/deref refs/workspace-wasm-editor-selection-rects) v2-editor-state-map (mf/deref refs/workspace-v2-editor-state) v1-editor-state-map (mf/deref refs/workspace-editor-state) editor (mf/deref refs/workspace-editor) @@ -188,6 +191,14 @@ :else (draft-text-range-selected? editor-state))) + ;; Names the selected range, so options opened for one range + ;; close on the next. Only the WASM editor tells. + text-selection-key + (when (and text-selection-active text-editor-wasm?) + (let [{:keys [anchor-para anchor-offset focus-para focus-offset]} + (wasm.text-editor/text-editor-get-selection)] + (str anchor-para ":" anchor-offset "-" focus-para ":" focus-offset))) + reading-selection-active (and text-selection-active text-editor-wasm? @@ -250,6 +261,7 @@ :values text-values :ruby-values ruby-values :text-selection-active text-selection-active + :text-selection-key text-selection-key :reading-selection-active reading-selection-active :zhuyin-selection-active zhuyin-selection-active :libraries libraries diff --git a/frontend/src/app/util/text/content.cljs b/frontend/src/app/util/text/content.cljs index d927639ca8..93064a17b4 100644 --- a/frontend/src/app/util/text/content.cljs +++ b/frontend/src/app/util/text/content.cljs @@ -7,6 +7,7 @@ (ns app.util.text.content (:require [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk] [app.main.refs :as refs] [app.util.text.content.from-dom :as fd] [app.util.text.content.to-dom :as td])) @@ -32,7 +33,7 @@ language (or (deref refs/default-text-language) (deref refs/file-text-language)) text-defaults (cond-> (merge (txt/get-default-text-attrs) default-font) - (some? language) (assoc :text-language language)) + (some? language) (merge (cjk/chosen-language-attrs language nil))) default-span (merge {:text ""} (select-keys text-defaults txt/text-node-attrs)) default-paragraph (merge {:type "paragraph" diff --git a/frontend/test/frontend_tests/ui/text_options_test.cljs b/frontend/test/frontend_tests/ui/text_options_test.cljs index a0e287f441..e47b58430d 100644 --- a/frontend/test/frontend_tests/ui/text_options_test.cljs +++ b/frontend/test/frontend_tests/ui/text_options_test.cljs @@ -68,7 +68,7 @@ (defn- render-language-view "Static markup of the language options view for texts with `language`." - [language {:keys [values ruby-values selection reading has-ruby]}] + [language {:keys [values ruby-values selection reading zhuyin has-ruby]}] (with-redefs [refs/texts-language (fn [_] (l/atom language)) hooks/use-portal-container (fn ([] nil) ([_] nil))] (rds/renderToStaticMarkup @@ -78,7 +78,7 @@ :ruby-values ruby-values :text-selection-active (boolean selection) :reading-selection-active (boolean reading) - :zhuyin-selection-active false + :zhuyin-selection-active (boolean zhuyin) :on-change identity :on-ruby-presentation-change identity}) :on-change identity @@ -151,6 +151,23 @@ (t/is (= "ko" (language "ko" "ja")) "the language used last wins") (t/is (nil? (language nil nil))))) +(t/deftest new-korean-texts-break-between-words + (let [wrap (fn [remembered] + (with-redefs [refs/default-font (l/atom nil) + refs/default-text-language (l/atom remembered) + refs/file-text-language (l/atom nil)] + (get-in (tc/v2-default-text-content) + [:children 0 :children 0 :korean-wrap])))] + (t/is (= "keep-words" (wrap "ko"))) + (t/is (nil? (wrap "ja"))) + (t/is (nil? (wrap nil)))) + (t/is (= {:text-language "ko" :korean-wrap "keep-words"} + (cjk/chosen-language-attrs "ko" {}))) + (t/is (= {:text-language "ko"} (cjk/chosen-language-attrs "ko" {:korean-wrap "syllable"})) + "a text keeps the wrapping it has") + (t/is (= {:text-language "ja"} (cjk/chosen-language-attrs "ja" {}))) + (t/is (= {:text-language nil} (cjk/chosen-language-attrs 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)))) @@ -191,37 +208,44 @@ "Escape clears the edition before the editor is disposed") (t/is (false? (text-shape-options/wasm-text-range-selected? id (random-uuid))))))) -(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 +(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))) + (let [markup (render-language-view "ja" {:has-ruby true})] + (t/is (str/includes? markup group)) + (t/is (str/includes? markup "data-testid=\"ruby-presentation-options\"")) + (t/is (str/includes? markup "data-testid=\"ruby-visibility-toggle\"")) + (doseq [label ["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 "ja" {:has-ruby true :selection true}) group)) + "with a selection, its reading options are the ones shown") + (t/is (not (str/includes? (render-language-view "ko" {:has-ruby true}) group))))) + +(t/deftest clearance-is-a-layout-option-with-or-without-readings + (let [clearance "workspace.options.text-options.annotation-clearance"] + (doseq [language ["ja" "zh-Hant-TW"] + has-ruby [false true] + :let [markup (render-language-view language {:has-ruby has-ruby})]] + (t/is (= 1 (count-of markup clearance)) (str language " " has-ruby)) + (t/is (< (str/index-of markup "workspace.options.text-options.language-group-layout") + (str/index-of markup clearance)))))) + +(t/deftest chinese-emphasis-marks-are-options-of-a-selection (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/is (zero? (count-of whole emphasis))) + (t/is (not (str/includes? whole position))) + (t/is (= 1 (count-of selection emphasis))) + (t/is (str/includes? selection position)) + (t/is (str/includes? whole "workspace.options.text-options.underline-position")))) (t/deftest chinese-readings-group-names-pinyin-or-zhuyin (doseq [[zhuyin title hidden] @@ -288,28 +312,50 @@ (t/is (= "none" (.-rubyOverhang style))) (t/is (= "under" (.-rubyPosition style))))) +(def ^:private furigana-labels + {:placeholder "workspace.options.text-options.ruby-placeholder" + :advanced "workspace.options.text-options.ruby-advanced-options" + :hidden "workspace.options.text-options.ruby-hidden" + :shown "workspace.options.text-options.ruby-shown"}) + (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"} + :labels furigana-labels :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/is (str/includes? markup "aria-expanded=\"false\"")) + (t/is (not (str/includes? markup "ruby-presentation-options")))))) -(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 reading-input-is-named-by-its-format + (let [markup (with-redefs [hooks/use-portal-container (fn ([] nil) ([_] nil))] + (rds/renderToStaticMarkup + (mf/element tcc/ruby-options* + #js {:values {} :labels furigana-labels + :on-change identity :on-blur identity})))] + (t/is (str/includes? markup "placeholder=\"workspace.options.text-options.ruby-placeholder\"")) + (t/is (str/includes? markup "aria-label=\"workspace.options.text-options.ruby-placeholder\"")) + (t/is (not (str/includes? markup "