Bidi: Allow switching text direction & add control for bidi pref

This commit is contained in:
Abe Jellinek 2024-01-11 19:42:17 -05:00 committed by Dan Stillman
parent aa10c71dba
commit 2963b86176
9 changed files with 71 additions and 9 deletions

View file

@ -45,7 +45,9 @@
xmlns:html="http://www.w3.org/1999/xhtml"
style="display: flex;">
<html:link rel="localization" href="browser/menubar.ftl"/>
<linkset>
<html:link rel="localization" href="browser/menubar.ftl"/>
</linkset>
<script>
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

View file

@ -96,6 +96,7 @@ function goBuildEditContextMenu() {
let popup = document.getElementById("textbox-contextmenu");
if (!popup) {
MozXULElement.insertFTLIfNeeded("toolkit/global/textActions.ftl");
MozXULElement.insertFTLIfNeeded("browser/menubar.ftl");
document.documentElement.appendChild(
MozXULElement.parseXULToFragment(`
<popupset>
@ -108,12 +109,25 @@ function goBuildEditContextMenu() {
<menuitem data-l10n-id="text-action-paste" command="cmd_paste"></menuitem>
<menuitem data-l10n-id="text-action-delete" command="cmd_delete"></menuitem>
<menuitem data-l10n-id="text-action-select-all" command="cmd_selectAll"></menuitem>
<menuseparator></menuseparator>
<menuitem data-l10n-id="menu-edit-bidi-switch-text-direction" command="cmd_switchTextDirection"></menuitem>
</menupopup>
</popupset>
`)
);
popup = document.documentElement.lastElementChild.firstElementChild;
}
try {
let menuitemSwitchTextDirection = popup.querySelector("[command='cmd_switchTextDirection']");
let showSwitchTextDirection = Services.prefs.getBoolPref("bidi.browser.ui", false);
menuitemSwitchTextDirection.hidden = !showSwitchTextDirection;
menuitemSwitchTextDirection.previousElementSibling.hidden = !showSwitchTextDirection;
}
catch (e) {
// Not worth failing over Services.jsm not being imported in a utility window
}
return popup;
}

View file

@ -29,6 +29,8 @@
class EditableText extends XULElementBase {
_input;
_textDirection = null;
static observedAttributes = [
'multiline',
'readonly',
@ -101,6 +103,7 @@
set value(value) {
this.setAttribute('value', value || '');
this.resetTextDirection();
}
get initialValue() {
@ -138,6 +141,13 @@
return this._input;
}
resetTextDirection() {
this._textDirection = null;
if (this._input) {
this._input.dir = null;
}
}
sizeToContent = () => {
// Add a temp span, fetch its width with current paddings and set max-width based on that
let span = document.createElement("span");
@ -174,10 +184,10 @@
if (!this.multiline) {
this._input.value = this._input.value.replace(/\n/g, ' ');
}
this.value = this._input.value;
this.setAttribute('value', this._input.value);
};
let handleChange = () => {
this.value = this._input.value;
this.setAttribute('value', this._input.value);
};
input.addEventListener('mousedown', () => {
this.setAttribute("mousedown", true);
@ -211,7 +221,9 @@
}
else if (event.key === 'Escape') {
this.dispatchEvent(new CustomEvent('escape_enter'));
this._input.value = this.value = this._input.dataset.initialValue;
let initialValue = this._input.dataset.initialValue;
this.setAttribute('value', initialValue);
this._input.value = initialValue;
this._input.blur();
}
});
@ -262,6 +274,16 @@
delete autocompleteParams.search;
Object.assign(this._input, autocompleteParams);
}
// Set text direction automatically if user has enabled bidi utilities
if ((!this._input.dir || this._input.dir === 'auto') && Zotero.Prefs.get('bidi.browser.ui', true)) {
if (!this._textDirection) {
this._textDirection = window.windowUtils.getDirectionFromText(this._input.value) === Ci.nsIDOMWindowUtils.DIRECTION_RTL
? 'rtl'
: 'ltr';
}
this._input.dir = this._textDirection;
}
}
focus(options) {

View file

@ -36,6 +36,16 @@
<checkbox label="&zotero.preferences.reportTranslationFailure;" preference="extensions.zotero.reportTranslationFailure" native="true"/>
</groupbox>
<groupbox>
<label><html:h2 data-l10n-id="preferences-advanced-language-and-region-title"/></label>
<checkbox
data-l10n-id="preferences-advanced-enable-bidi-ui"
preference="bidi.browser.ui"
native="true"
/>
</groupbox>
</vbox>
<vbox class="main-section">

View file

@ -113,6 +113,11 @@ Zotero_Preferences.General = {
Services.locale.requestedLocales = [menu.value];
}
// https://searchfox.org/mozilla-central/rev/961a9e56a0b5fa96ceef22c61c5e75fb6ba53395/browser/base/content/utilityOverlay.js#383-387
if (Services.locale.isAppLocaleRTL) {
Zotero.Prefs.set("bidi.browser.ui", true, true);
}
if (!changed) {
return;
}

View file

@ -22,7 +22,9 @@
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml"
>
<html:link rel="localization" href="browser/menubar.ftl"/>
<linkset>
<html:link rel="localization" href="browser/menubar.ftl"/>
</linkset>
<script type="application/javascript">
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

View file

@ -48,8 +48,10 @@
width="1000" height="700"
persist="screenX screenY width height sizemode">
<html:link rel="localization" href="browser/menubar.ftl"/>
<html:link rel="localization" href="toolkit/global/textActions.ftl"/>
<linkset>
<html:link rel="localization" href="browser/menubar.ftl"/>
<html:link rel="localization" href="toolkit/global/textActions.ftl"/>
</linkset>
<script>
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

View file

@ -54,8 +54,9 @@
style="display: flex;"
tooltip="html-tooltip">
<html:link rel="localization" href="browser/menubar.ftl"/>
<linkset>
<html:link rel="localization" href="browser/menubar.ftl"/>
</linkset>
<script>
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

View file

@ -32,3 +32,7 @@ preferences-color-scheme-light =
.label = Light
preferences-color-scheme-dark =
.label = Dark
preferences-advanced-language-and-region-title = Language and Region
preferences-advanced-enable-bidi-ui =
.label = Enable bidirectional text editing utilities