Skip to content

Commit e7552e1

Browse files
committed
so close…
1 parent dbadf99 commit e7552e1

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Operations around keyboard support
3+
*/
4+
5+
import * as cldrClient from "./cldrClient.mjs";
6+
import * as cldrStatus from "./cldrStatus.mjs";
7+
8+
// See https://keyman.com/developer/keymanweb/ for the latest version and scripts to use
9+
const KEYMAN_VERSION = "17.0.333";
10+
const KEYMAN_SCRIPTS = ["keymanweb.js", "kmwuitoggle.js"];
11+
const KEYMAN_BASE = "https://s.keyman.com/kmw/engine/";
12+
const KEYMAN_ATTACH = `(
13+
function(kmw) {
14+
kmw.init({attachType:'auto'});
15+
}
16+
)(keyman);`;
17+
18+
19+
// are the scripts installed?
20+
let installed = false;
21+
let installing = false;
22+
// is the option enabled?
23+
let enabled = false;
24+
// did we call init yet?
25+
let initted = false;
26+
27+
28+
function getRemoteScript(src) {
29+
const e = document.createElement('script');
30+
e.setAttribute('src', src);
31+
return e;
32+
}
33+
34+
function getInlineScript(body) {
35+
const e = document.createElement('script');
36+
e.textContent = body;
37+
return e;
38+
}
39+
40+
/** insert the script tags */
41+
async function installKeyboards() {
42+
if (installing) return;
43+
installing = true;
44+
45+
if (!installed) {
46+
const { body } = document;
47+
for (const script of KEYMAN_SCRIPTS) {
48+
const e = getRemoteScript(`${KEYMAN_BASE}${KEYMAN_VERSION}/${script}`);
49+
body.appendChild(e);
50+
await waitForLoad(e); // block here until loaded
51+
}
52+
const attach = getInlineScript(KEYMAN_ATTACH);
53+
body.appendChild(attach);
54+
installed = true;
55+
installing = false;
56+
updateKeyboardLocale(cldrStatus.getCurrentLocale());
57+
}
58+
59+
function waitForLoad(e) {
60+
return new Promise((resolve, reject) => {
61+
try {
62+
e.addEventListener('load', resolve);
63+
} catch (err) {
64+
reject(err);
65+
}
66+
});
67+
}
68+
}
69+
70+
/**
71+
* Update whether user has requested web keyboards
72+
* @param {boolean} enable true if keyboards are enabled
73+
*/
74+
export function setUseKeyboards(enable) {
75+
if (enable != enabled) {
76+
if (enable) {
77+
installKeyboards().then(() => { enabled = true; });
78+
} else {
79+
enabled = false;
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Update the keyboard locale
86+
* @param {string} curLocale
87+
*/
88+
export function updateKeyboardLocale(curLocale) {
89+
if (installed && enabled) {
90+
// make sure keyman is loaded
91+
for (const { InternalName } of keyman.getKeyboards()) {
92+
keyman.removeKeyboards(InternalName);
93+
console.log(`Removed kbd: ${InternalName}`);
94+
}
95+
console.log(`Adding kbd: @${curLocale}`);
96+
keyman.addKeyboards(`@${curLocale}`);
97+
// end keyboards
98+
}
99+
}
100+
101+
export function isEnabled() {
102+
return enabled;
103+
}
104+
105+
/** Note: may need to call reload() in order to unload keyboard. */
106+
export async function setEnabledPref(enable) {
107+
const client = await cldrClient.getClient();
108+
setUseKeyboards(enable);
109+
if (enable) {
110+
await client.apis.user.setSetting({setting: 'webkeyboard'});
111+
} else {
112+
await client.apis.user.removeSetting({setting: 'webkeyboard'});
113+
}
114+
}

tools/cldr-apps/js/src/esm/cldrMenu.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as cldrCoverage from "./cldrCoverage.mjs";
88
import * as cldrDom from "./cldrDom.mjs";
99
import * as cldrEvent from "./cldrEvent.mjs";
1010
import * as cldrGui from "./cldrGui.mjs";
11+
import * as cldrKeyboard from "./cldrKeyboard.mjs";
1112
import * as cldrLoad from "./cldrLoad.mjs";
1213
import { LocaleMap } from "./cldrLocaleMap.mjs";
1314
import * as cldrStatus from "./cldrStatus.mjs";
@@ -418,6 +419,8 @@ function updateLocaleMenu() {
418419
const curLocale = cldrStatus.getCurrentLocale();
419420
let prefixMessage = "";
420421
if (curLocale != null && curLocale != "" && curLocale != "-") {
422+
// hook to update the keyboard locale
423+
cldrKeyboard.updateKeyboardLocale(curLocale);
421424
const locmap = cldrLoad.getTheLocaleMap();
422425
cldrStatus.setCurrentLocaleName(locmap.getLocaleName(curLocale));
423426
var bund = locmap.getLocaleInfo(curLocale);

tools/cldr-apps/js/src/esm/cldrStatus.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import * as cldrGui from "./cldrGui.mjs";
55
import { ref } from "vue";
6+
import { setUseKeyboards } from "./cldrKeyboard.mjs";
67

78
const refs = {
89
currentLocale: ref(null),
@@ -44,6 +45,7 @@ function getStatusTarget() {
4445
* Events:
4546
* - sessionId: session ID changed
4647
* - surveyUser: survey user changed
48+
* - update: any update changed
4749
*/
4850
function on(type, callback) {
4951
getStatusTarget().addEventListener(type, callback);
@@ -94,6 +96,8 @@ function updateAll(status) {
9496
if (status.user) {
9597
setSurveyUser(status.user);
9698
}
99+
setUseKeyboards(status?.settings?.user?.webkeyboard);
100+
dispatchEvent(new Event('update'));
97101
}
98102

99103
/**

tools/cldr-apps/js/src/views/MainMenu.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,15 @@
134134
<li><a href="#error_subtypes///">Error Subtypes</a></li>
135135
</ul>
136136
</li>
137+
<li v-if="loggedIn">
138+
<a-checkbox v-model:checked="webkeyboard" @change="setKeyboard">Show Web Keyboard</a-checkbox>
139+
</li>
137140
</ul>
138141
</template>
139142

140143
<script>
141144
import * as cldrStatus from "../esm/cldrStatus.mjs";
145+
import * as cldrKeyboard from "../esm/cldrKeyboard.mjs";
142146
143147
export default {
144148
data() {
@@ -158,6 +162,7 @@ export default {
158162
uploadXmlUrl: null,
159163
userId: 0,
160164
showClaMenu: true,
165+
webkeyboard: false,
161166
};
162167
},
163168
@@ -187,6 +192,12 @@ export default {
187192
this.org = cldrStatus.getOrganizationName();
188193
this.recentActivityUrl = this.getSpecialUrl("recent_activity");
189194
this.uploadXmlUrl = this.getSpecialUrl("upload");
195+
cldrStatus.on('update', () => this.webkeyboard = cldrKeyboard.isEnabled());
196+
},
197+
198+
setKeyboard(checked) {
199+
cldrKeyboard.setEnabledPref(this.webkeyboard);
200+
// window.location.reload();
190201
},
191202
192203
getSpecialUrl(special) {

tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/UserAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public Response setSetting(
201201
}
202202
}
203203

204-
@DeleteProvider
204+
@DELETE
205205
@Operation(summary = "Remove Settings", description = "remove a settings field")
206206
@APIResponses(
207207
value = {

0 commit comments

Comments
 (0)