Skip to content

Commit a9fe9d3

Browse files
committed
Fix collapsed white space in certain labels
Previously I used a `display_hotkey` function which was similar to `AccessKeys.toHTML` but it included a `<span style="white-space: pre">` wrapper. The wrapper span (although perhaps not the style attribute) is important to avoid white space collapsing. For example, in the Flip/Rotate dialog, the "Flip &vertical" label was converted to 'Flip <span class="menu-hotkey">v</span>ertical', and since the space between the words was next to an element, it was ignored, within the context of the `<label>` element parent.
1 parent e6ad042 commit a9fe9d3

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

src/edit-colors.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { $DialogWindow } from "./$ToolWindow.js";
66
// import { localize } from "./app-localization.js";
77
import { basic_colors, custom_colors } from "./color-data.js";
88
import { detect_monochrome, make_monochrome_palette, show_error_message, undoable } from "./functions.js";
9-
import { $G, get_help_folder_icon, get_rgba_from_color, make_canvas, rgb_to_hsl } from "./helpers.js";
9+
import { $G, get_help_folder_icon, get_rgba_from_color, make_canvas, render_access_key, rgb_to_hsl } from "./helpers.js";
1010
import { replace_color_globally } from "./image-manipulation.js";
1111

1212
// @TODO:
@@ -265,9 +265,9 @@ function choose_color(initial_color, callback) {
265265
const $left_right_split = $(`<div class="left-right-split">`).appendTo($w.$main);
266266
const $left = $(`<div class="left-side">`).appendTo($left_right_split);
267267
const $right = $(`<div class="right-side">`).appendTo($left_right_split).hide();
268-
$left.append(`<label for="basic-colors">${AccessKeys.toHTML("&Basic colors:")}</label>`);
268+
$left.append(`<label for="basic-colors">${render_access_key("&Basic colors:")}</label>`);
269269
const $basic_colors_grid = make_color_grid(basic_colors, "basic-colors").appendTo($left);
270-
$left.append(`<label for="custom-colors">${AccessKeys.toHTML("&Custom colors:")}</label>`);
270+
$left.append(`<label for="custom-colors">${render_access_key("&Custom colors:")}</label>`);
271271
const custom_colors_dom_order = []; // (wanting) horizontal top to bottom
272272
for (let list_index = 0; list_index < custom_colors.length; list_index++) {
273273
const row = list_index % 2;
@@ -287,7 +287,7 @@ function choose_color(initial_color, callback) {
287287
}
288288

289289
const $define_custom_colors_button = $(`<button class="define-custom-colors-button" type="button">`)
290-
.html(AccessKeys.toHTML("&Define Custom Colors >>"))
290+
.html(render_access_key("&Define Custom Colors >>"))
291291
.appendTo($left)
292292
.on("click", (e) => {
293293
// prevent the form from submitting
@@ -314,7 +314,7 @@ function choose_color(initial_color, callback) {
314314
};
315315
$(window).on("resize", maybe_reenable_button_for_mobile_navigation);
316316

317-
const $color_solid_label = $(`<label for="color-solid-canvas">${AccessKeys.toHTML("Color|S&olid")}</label>`);
317+
const $color_solid_label = $(`<label for="color-solid-canvas">${render_access_key("Color|S&olid")}</label>`);
318318
$color_solid_label.css({
319319
position: "absolute",
320320
left: 10,
@@ -446,7 +446,7 @@ function choose_color(initial_color, callback) {
446446
b: 255,
447447
}[component_letter];
448448
const label = document.createElement("label");
449-
label.innerHTML = AccessKeys.toHTML(text_with_hotkey);
449+
label.innerHTML = render_access_key(text_with_hotkey);
450450
const input_y_spacing = 22;
451451
$(label).css({
452452
position: "absolute",
@@ -614,7 +614,7 @@ function choose_color(initial_color, callback) {
614614
$right.append(rainbow_canvas, luminosity_canvas, result_canvas, $color_solid_label, lum_arrow_canvas);
615615

616616
const $add_to_custom_colors_button = $(`<button class="add-to-custom-colors-button" type="button">`)
617-
.html(AccessKeys.toHTML("&Add To Custom Colors"))
617+
.html(render_access_key("&Add To Custom Colors"))
618618
.appendTo($right)
619619
.on("click", (event) => {
620620
// prevent the form from submitting

src/functions.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { OnCanvasTextBox } from "./OnCanvasTextBox.js";
1010
// import { localize } from "./app-localization.js";
1111
import { default_palette } from "./color-data.js";
1212
import { image_formats } from "./file-format-data.js";
13-
import { $G, E, TAU, debounce, from_canvas_coords, get_help_folder_icon, get_icon_for_tool, get_rgba_from_color, is_discord_embed, is_pride_month, make_canvas, to_canvas_coords } from "./helpers.js";
13+
import { $G, E, TAU, debounce, from_canvas_coords, get_help_folder_icon, get_icon_for_tool, get_rgba_from_color, is_discord_embed, is_pride_month, make_canvas, render_access_key, to_canvas_coords } from "./helpers.js";
1414
import { apply_image_transformation, draw_grid, draw_selection_box, flip_horizontal, flip_vertical, invert_monochrome, invert_rgb, rotate, stretch_and_skew, threshold_black_and_white } from "./image-manipulation.js";
1515
import { show_imgur_uploader } from "./imgur.js";
1616
import { showMessageBox } from "./msgbox.js";
@@ -450,11 +450,11 @@ function show_custom_zoom_window() {
450450
$fieldset.append(`
451451
<legend>${localize("Zoom to")}</legend>
452452
<div class="fieldset-body">
453-
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-1" aria-keyshortcuts="Alt+1 1" value="1"/><label for="zoom-option-1">${AccessKeys.toHTML("&100%")}</label></div>
454-
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-2" aria-keyshortcuts="Alt+2 2" value="2"/><label for="zoom-option-2">${AccessKeys.toHTML("&200%")}</label></div>
455-
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-4" aria-keyshortcuts="Alt+4 4" value="4"/><label for="zoom-option-4">${AccessKeys.toHTML("&400%")}</label></div>
456-
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-6" aria-keyshortcuts="Alt+6 6" value="6"/><label for="zoom-option-6">${AccessKeys.toHTML("&600%")}</label></div>
457-
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-8" aria-keyshortcuts="Alt+8 8" value="8"/><label for="zoom-option-8">${AccessKeys.toHTML("&800%")}</label></div>
453+
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-1" aria-keyshortcuts="Alt+1 1" value="1"/><label for="zoom-option-1">${render_access_key("&100%")}</label></div>
454+
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-2" aria-keyshortcuts="Alt+2 2" value="2"/><label for="zoom-option-2">${render_access_key("&200%")}</label></div>
455+
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-4" aria-keyshortcuts="Alt+4 4" value="4"/><label for="zoom-option-4">${render_access_key("&400%")}</label></div>
456+
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-6" aria-keyshortcuts="Alt+6 6" value="6"/><label for="zoom-option-6">${render_access_key("&600%")}</label></div>
457+
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-8" aria-keyshortcuts="Alt+8 8" value="8"/><label for="zoom-option-8">${render_access_key("&800%")}</label></div>
458458
<div class="radio-field"><input type="radio" name="custom-zoom-radio" id="zoom-option-really-custom" value="really-custom"/><label for="zoom-option-really-custom"><input type="number" min="10" max="1000" name="really-custom-zoom-input" class="inset-deep no-spinner" value=""/>%</label></div>
459459
</div>
460460
`);
@@ -3175,8 +3175,8 @@ function image_attributes() {
31753175
let width_in_px = main_canvas.width;
31763176
let height_in_px = main_canvas.height;
31773177

3178-
const $width_label = $(E("label")).appendTo($main).html(AccessKeys.toHTML(localize("&Width:")));
3179-
const $height_label = $(E("label")).appendTo($main).html(AccessKeys.toHTML(localize("&Height:")));
3178+
const $width_label = $(E("label")).appendTo($main).html(render_access_key(localize("&Width:")));
3179+
const $height_label = $(E("label")).appendTo($main).html(render_access_key(localize("&Height:")));
31803180
const $width = $(E("input")).attr({ type: "number", min: 1, "aria-keyshortcuts": "Alt+W W W" }).addClass("no-spinner inset-deep").appendTo($width_label);
31813181
const $height = $(E("input")).attr({ type: "number", min: 1, "aria-keyshortcuts": "Alt+H H H" }).addClass("no-spinner inset-deep").appendTo($height_label);
31823182

@@ -3192,9 +3192,9 @@ function image_attributes() {
31923192
const $units = $(E("fieldset")).appendTo($main).append(`
31933193
<legend>${localize("Units")}</legend>
31943194
<div class="fieldset-body">
3195-
<div class="radio-field"><input type="radio" name="units" id="unit-in" value="in" aria-keyshortcuts="Alt+I I"><label for="unit-in">${AccessKeys.toHTML(localize("&Inches"))}</label></div>
3196-
<div class="radio-field"><input type="radio" name="units" id="unit-cm" value="cm" aria-keyshortcuts="Alt+M M"><label for="unit-cm">${AccessKeys.toHTML(localize("C&m"))}</label></div>
3197-
<div class="radio-field"><input type="radio" name="units" id="unit-px" value="px" aria-keyshortcuts="Alt+P P"><label for="unit-px">${AccessKeys.toHTML(localize("&Pixels"))}</label></div>
3195+
<div class="radio-field"><input type="radio" name="units" id="unit-in" value="in" aria-keyshortcuts="Alt+I I"><label for="unit-in">${render_access_key(localize("&Inches"))}</label></div>
3196+
<div class="radio-field"><input type="radio" name="units" id="unit-cm" value="cm" aria-keyshortcuts="Alt+M M"><label for="unit-cm">${render_access_key(localize("C&m"))}</label></div>
3197+
<div class="radio-field"><input type="radio" name="units" id="unit-px" value="px" aria-keyshortcuts="Alt+P P"><label for="unit-px">${render_access_key(localize("&Pixels"))}</label></div>
31983198
</div>
31993199
`);
32003200
$units.find(`[value=${current_unit}]`).attr({ checked: true });
@@ -3208,8 +3208,8 @@ function image_attributes() {
32083208
const $colors = $(E("fieldset")).appendTo($main).append(`
32093209
<legend>${localize("Colors")}</legend>
32103210
<div class="fieldset-body">
3211-
<div class="radio-field"><input type="radio" name="colors" id="attribute-monochrome" value="monochrome" aria-keyshortcuts="Alt+B B"><label for="attribute-monochrome">${AccessKeys.toHTML(localize("&Black and white"))}</label></div>
3212-
<div class="radio-field"><input type="radio" name="colors" id="attribute-polychrome" value="polychrome" aria-keyshortcuts="Alt+L L"><label for="attribute-polychrome">${AccessKeys.toHTML(localize("Co&lors"))}</label></div>
3211+
<div class="radio-field"><input type="radio" name="colors" id="attribute-monochrome" value="monochrome" aria-keyshortcuts="Alt+B B"><label for="attribute-monochrome">${render_access_key(localize("&Black and white"))}</label></div>
3212+
<div class="radio-field"><input type="radio" name="colors" id="attribute-polychrome" value="polychrome" aria-keyshortcuts="Alt+L L"><label for="attribute-polychrome">${render_access_key(localize("Co&lors"))}</label></div>
32133213
</div>
32143214
`);
32153215
$colors.find(`[value=${monochrome ? "monochrome" : "polychrome"}]`).attr({ checked: true });
@@ -3290,7 +3290,7 @@ function image_attributes() {
32903290
});
32913291

32923292
// Parsing HTML with jQuery; $Button takes text (not HTML) or Node/DocumentFragment
3293-
$w.$Button($.parseHTML(AccessKeys.toHTML(localize("&Default")))[0], () => {
3293+
$w.$Button($.parseHTML(render_access_key(localize("&Default")))[0], () => {
32943294
width_in_px = default_canvas_width;
32953295
height_in_px = default_canvas_height;
32963296
$width.val(width_in_px / unit_sizes_in_px[current_unit]);
@@ -3376,7 +3376,7 @@ function image_flip_and_rotate() {
33763376
value="flip-horizontal"
33773377
aria-keyshortcuts="Alt+F"
33783378
checked
3379-
/><label for="flip-horizontal">${AccessKeys.toHTML(localize("&Flip horizontal"))}</label>
3379+
/><label for="flip-horizontal">${render_access_key(localize("&Flip horizontal"))}</label>
33803380
</div>
33813381
<div class="radio-wrapper">
33823382
<input
@@ -3385,7 +3385,7 @@ function image_flip_and_rotate() {
33853385
id="flip-vertical"
33863386
value="flip-vertical"
33873387
aria-keyshortcuts="Alt+V"
3388-
/><label for="flip-vertical">${AccessKeys.toHTML(localize("Flip &vertical"))}</label>
3388+
/><label for="flip-vertical">${render_access_key(localize("Flip &vertical"))}</label>
33893389
</div>
33903390
<div class="radio-wrapper">
33913391
<input
@@ -3394,7 +3394,7 @@ function image_flip_and_rotate() {
33943394
id="rotate-by-angle"
33953395
value="rotate-by-angle"
33963396
aria-keyshortcuts="Alt+R"
3397-
/><label for="rotate-by-angle">${AccessKeys.toHTML(localize("&Rotate by angle"))}</label>
3397+
/><label for="rotate-by-angle">${render_access_key(localize("&Rotate by angle"))}</label>
33983398
</div>
33993399
`);
34003400

@@ -3416,7 +3416,7 @@ function image_flip_and_rotate() {
34163416
aria-keyshortcuts="Alt+${AccessKeys.get(label_with_hotkey).toUpperCase()}"
34173417
/><label
34183418
for="rotate-${degrees}"
3419-
>${AccessKeys.toHTML(label_with_hotkey)}</label>
3419+
>${render_access_key(label_with_hotkey)}</label>
34203420
</div>
34213421
`);
34223422
}
@@ -3538,7 +3538,7 @@ function image_stretch_and_skew() {
35383538
width: "40px"
35393539
}).addClass("no-spinner inset-deep");
35403540
$(E("td")).appendTo($tr).append($img);
3541-
$(E("td")).appendTo($tr).append($(E("label")).html(AccessKeys.toHTML(label_with_hotkey)).attr("for", input_id));
3541+
$(E("td")).appendTo($tr).append($(E("label")).html(render_access_key(label_with_hotkey)).attr("for", input_id));
35423542
$(E("td")).appendTo($tr).append($input);
35433543
$(E("td")).appendTo($tr).text(label_unit);
35443544

src/helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ export const is_discord_embed = query_params.get('frame_id') != null;
3232

3333
const $G = $(window);
3434

35+
/**
36+
* Wrapper for AccessKeys.toHTML that ensures whitespace isn't collapsed in cases like "Fox &Trot" or "Fo&x Trot" where the access key abuts a space.
37+
*
38+
* (Actually a simple `<span>` may be enough (since it's an inline element?), but `white-space: pre` is more explicit.)
39+
*
40+
* @param {string} label text with an access key denoted by an ampersand (can escape with double ampersand)
41+
* @returns {string} HTML for label with access key underlined
42+
*/
43+
function render_access_key(label) {
44+
return `<span style="white-space: pre">${AccessKeys.toHTML(label)}</span>`;
45+
}
46+
3547
/**
3648
* @param {string} name filename without extension
3749
* @param {[number, number]} coords hotspot coordinates
@@ -422,6 +434,7 @@ export {
422434
make_canvas,
423435
make_css_cursor,
424436
memoize_synchronous_function,
437+
render_access_key,
425438
rgb_to_hsl,
426439
to_canvas_coords
427440
};

0 commit comments

Comments
 (0)