textbrowser 0.45.2 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,4 @@
1
1
  /* eslint-env browser */
2
- import {IMF} from 'imf';
3
2
  import {getJSON} from 'simple-get-json';
4
3
 
5
4
  import {replaceHash, getParamsSetter, getSerializeParamsAsURL} from './utils/Params.js';
@@ -32,45 +31,40 @@ export default async function workDisplay ({
32
31
  );
33
32
 
34
33
  async function _displayWork ({
35
- lf, metadataObj, getFieldAliasOrName, schemaObj, schemaItems, fieldInfo,
36
- metadata, pluginsForWork, groupsToWorks
34
+ workI18n, metadataObj, getFieldAliasOrName, schemaObj, schemaItems,
35
+ fieldInfo, metadata, pluginsForWork, groupsToWorks
37
36
  }) {
38
- const il = localizeParamNames
37
+ const lParam = localizeParamNames
39
38
  ? key => l(['params', key])
40
39
  : key => key;
41
- const iil = localizeParamNames
40
+ const lIndexedParam = localizeParamNames
42
41
  ? key => l(['params', 'indexed', key])
43
42
  : key => key;
44
43
 
45
- const localeFromLangData = languages.localeFromLangData.bind(languages);
46
- const imfLang = IMF({
47
- locales: lang.map(localeFromLangData),
48
- fallbackLocales: fallbackLanguages.map(localeFromLangData)
49
- });
50
- const imfl = imfLang.getFormatter();
51
-
52
- // Returns option element with localized option text (as Jamilih), with
44
+ // Returns element with localized option text (as Jamilih), with
53
45
  // optional fallback direction
54
- const le = (key, el, attToLocalize, atts, children = []) => {
55
- atts[attToLocalize] = l({
56
- key,
57
- fallback ({message}) {
58
- atts.dir = fallbackDirection;
59
- return message;
60
- }
61
- });
46
+ const lElement = (key, el, attToLocalize, atts, children = []) => {
47
+ atts[attToLocalize] = l(
48
+ key
49
+ // Restore this if `intl-dom` supports
50
+ // fallback ({message}) {
51
+ // atts.dir = fallbackDirection;
52
+ // return message;
53
+ // }
54
+ );
62
55
  return [el, atts, children];
63
56
  };
64
57
 
65
58
  // Returns plain text node or element (as Jamilih) with fallback direction
66
- const ld = (key, values, formats) =>
67
- l({
59
+ const lDirectional = (key, substitutions) =>
60
+ l(
68
61
  key,
69
- values,
70
- formats,
71
- fallback: ({message}) =>
72
- Templates.workDisplay.bdo({fallbackDirection, message})
73
- });
62
+ substitutions
63
+ // formats
64
+ // Restore if `intl-dom` supports
65
+ // fallback: ({message}) =>
66
+ // Templates.workDisplay.bdo({fallbackDirection, message})
67
+ );
74
68
 
75
69
  const fieldMatchesLocale = metadata.getFieldMatchesLocale({
76
70
  namespace: this.namespace,
@@ -84,18 +78,18 @@ export default async function workDisplay ({
84
78
  callback ({browseFields, i}) {
85
79
  Templates.workDisplay.addBrowseFields({
86
80
  browseFields, fieldInfo,
87
- ld, i, iil, $p, content
81
+ lDirectional, i, lIndexedParam, $p, content
88
82
  });
89
83
  }
90
84
  });
91
85
 
92
86
  /*
93
- Templates.workDisplay.addRandomFormFields({
94
- il, l, ld, le, $p, serializeParamsAsURL, content
95
- });
96
- */
97
- const serializeParamsAsURL = getSerializeParamsAsURL({l, il, $p});
98
- const paramsSetter = getParamsSetter({l, il, $p});
87
+ Templates.workDisplay.addRandomFormFields({
88
+ lParam, l, lDirectional, lElement, $p, serializeParamsAsURL, content
89
+ });
90
+ */
91
+ const serializeParamsAsURL = getSerializeParamsAsURL({l, lParam, $p});
92
+ const paramsSetter = getParamsSetter({l, lParam, $p});
99
93
 
100
94
  const {groups} = await getJSON(this.files);
101
95
 
@@ -127,15 +121,28 @@ export default async function workDisplay ({
127
121
  };
128
122
  })();
129
123
 
124
+ const displayNames = new Intl.DisplayNames(
125
+ [...lang, ...fallbackLanguages],
126
+ {
127
+ type: 'language',
128
+ // Dialect: "American English"
129
+ // Standard: "English (United States)"
130
+ languageDisplay: 'standard' // 'dialect'
131
+ }
132
+ );
133
+ const languageI18n = (code) => {
134
+ return displayNames.of(code);
135
+ };
136
+
130
137
  Templates.workDisplay.main({
131
- languageParam, lang, lf,
138
+ languageParam, lang, workI18n,
132
139
  l, namespace: this.namespace, groups, heading,
133
- imfl, fallbackDirection,
140
+ languageI18n, fallbackDirection,
134
141
  langs, fieldInfo, localizeParamNames,
135
142
  serializeParamsAsURL, paramsSetter, replaceHash,
136
143
  getFieldAliasOrNames,
137
144
  hideFormattingSection, $p,
138
- metadataObj, il, le, ld, iil,
145
+ metadataObj, lParam, lElement, lDirectional, lIndexedParam,
139
146
  fieldMatchesLocale,
140
147
  preferredLocale, schemaItems, content,
141
148
  preferencesPlugin
@@ -143,21 +150,20 @@ export default async function workDisplay ({
143
150
  }
144
151
 
145
152
  try {
146
- const {lf, fileData, metadataObj, ...args} = await this.getWorkData({
153
+ const {workI18n, fileData, metadataObj, ...args} = await this.getWorkData({
147
154
  lang, fallbackLanguages, preferredLocale,
148
155
  languages, work: $p.get('work')
149
156
  });
150
157
 
151
- document.title = lf({
152
- key: 'browserfile-workdisplay',
153
- values: {
158
+ document.title = workI18n(
159
+ 'browserfile-workdisplay',
160
+ {
154
161
  work: fileData
155
162
  ? getMetaProp(lang, metadataObj, 'alias')
156
163
  : ''
157
- },
158
- fallback: true
159
- });
160
- await _displayWork.call(this, {lf, metadataObj, ...args});
164
+ }
165
+ );
166
+ await _displayWork.call(this, {workI18n, metadataObj, ...args});
161
167
  } catch (err) {
162
168
  console.log('err', err);
163
169
  dialogs.alert(err);
@@ -1,6 +1,6 @@
1
1
  /* eslint-env browser */
2
2
  import {getJSON} from 'simple-get-json';
3
- import {IMF} from 'imf';
3
+ import {i18n} from 'intl-dom';
4
4
 
5
5
  import {getMetaProp} from './utils/Metadata.js';
6
6
  import {dialogs} from './utils/dialogs.js';
@@ -13,42 +13,63 @@ export default async function workSelect ({
13
13
  // We use getJSON instead of JsonRefs as we do not necessarily need to
14
14
  // resolve the file contents here
15
15
  try {
16
- const dbs = await getJSON(files);
17
- const localeFromFileData = (lan) =>
18
- dbs['localization-strings'][lan];
16
+ const works = await getJSON(files);
17
+ const localizationStrings = works['localization-strings'];
19
18
 
20
- const metadataObjs = await getJSON(dbs.groups.reduce((arr, fileGroup) => {
21
- const metadataBaseDir = (dbs.metadataBaseDirectory || '') +
19
+ const metadataObjs = await getJSON(works.groups.reduce((arr, fileGroup) => {
20
+ const metadataBaseDir = (works.metadataBaseDirectory || '') +
22
21
  (fileGroup.metadataBaseDirectory || '') + '/';
23
22
  return fileGroup.files.reduce((ar, fileData) =>
24
23
  [...ar, metadataBaseDir + fileData.metadataFile],
25
24
  arr);
26
25
  }, []));
27
- const imfFile = IMF({
28
- locales: lang.map(localeFromFileData),
29
- fallbackLocales: fallbackLanguages.map(localeFromFileData)
26
+
27
+ const workI18n = await i18n({
28
+ messageStyle: 'plainNested',
29
+ locales: lang,
30
+ defaultLocales: fallbackLanguages,
31
+ // Todo: Could at least share this with `index.js`
32
+ localeStringFinder ({
33
+ locales, defaultLocales
34
+ }) {
35
+ const locale = [...locales, ...defaultLocales].find((language) => {
36
+ return language in localizationStrings;
37
+ });
38
+ return {
39
+ locale,
40
+ strings: {
41
+ head: {},
42
+ body: localizationStrings[locale]
43
+ }
44
+ };
45
+ }
30
46
  });
31
- const lf = imfFile.getFormatter();
32
- document.title = lf({key: 'browserfile-workselect', fallback: true});
47
+
48
+ document.title = workI18n('browserfile-workselect');
33
49
  /*
34
- function ld (key, values, formats) {
35
- return l({
36
- key: key, values: values, formats: formats, fallback: ({message}) =>
37
- // Displaying as div with inline display instead of span since
38
- // Firefox puts punctuation at left otherwise
39
- ['div', {
40
- style: 'display: inline;direction: ' + fallbackDirection
41
- }, [message]]
42
- });
43
- }
44
- */
50
+ // Would need adapting now that not using IMF
51
+ function lDirectional (key, substitutions, formats) {
52
+ return workI18n(
53
+ key, substitutions,
54
+ formats,
55
+ fallback: ({message}) =>
56
+ // Displaying as div with inline display instead of span since
57
+ // Firefox puts punctuation at left otherwise
58
+ ['div', {
59
+ style: 'display: inline;direction: ' + fallbackDirection
60
+ }, [message]]
61
+ });
62
+ }
63
+ */
45
64
 
46
65
  const metadataObjsIter = metadataObjs[Symbol.iterator]();
47
66
  const getNextAlias = () => {
48
67
  const metadataObj = metadataObjsIter.next().value;
49
68
  return getMetaProp(lang, metadataObj, 'alias');
50
69
  };
51
- Templates.workSelect({groups: dbs.groups, lf, getNextAlias, $p, followParams});
70
+ Templates.workSelect({
71
+ groups: works.groups, workI18n, getNextAlias, $p, followParams
72
+ });
52
73
  } catch (err) {
53
74
  console.log('Error', err);
54
75
  dialogs.alert(err);
package/server/main.js CHANGED
@@ -12,7 +12,7 @@ import {getJSON} from 'simple-get-json';
12
12
 
13
13
  import IntlURLSearchParams from '../resources/utils/IntlURLSearchParams.js';
14
14
  import {resultsDisplayServer} from '../resources/resultsDisplay.js';
15
- import getIMFFallbackResults from '../resources/utils/getIMFFallbackResults.js';
15
+ import getLocaleFallbackResults from '../resources/utils/getLocaleFallbackResults.js';
16
16
  import {setServiceWorkerDefaults} from '../resources/utils/ServiceWorker.js';
17
17
  // import setGlobalVars from 'indexeddbshim/src/node-UnicodeIdentifiers.js';
18
18
  import {Languages} from '../resources/utils/Languages.js';
@@ -99,10 +99,8 @@ setGlobalVars(null, {
99
99
 
100
100
  if (userParams.nodeActivate) {
101
101
  global.fetch = fetch;
102
- (async () => {
103
- await activateCallback({...userParamsWithDefaults, basePath});
104
- console.log('Activated');
105
- })();
102
+ await activateCallback({...userParamsWithDefaults, basePath});
103
+ console.log('Activated');
106
104
  }
107
105
  console.log('past activate check');
108
106
 
@@ -196,40 +194,50 @@ const srv = http.createServer(async (req, res) => {
196
194
 
197
195
  const {lang, langs, fallbackLanguages} = languagesInstance.getLanguageInfo({$p});
198
196
 
199
- getIMFFallbackResults({
197
+ const l = await getLocaleFallbackResults({
200
198
  $p,
201
- basePath,
202
- lang, langs, fallbackLanguages,
203
- langData,
204
- async resultsDisplay (resultsArgs, ...args) {
205
- const serverOutput = $p.get('serverOutput', true);
206
- let allowPlugins = $p.get('allowPlugins', true);
207
- if (allowPlugins === '0') {
208
- allowPlugins = false;
209
- }
210
- const isHTML = serverOutput === 'html';
211
- res.writeHead(200, {'Content-Type': isHTML
212
- ? 'text/html;charset=utf8'
213
- : 'application/json;charset=utf8'
214
- });
215
- resultsArgs = {
216
- ...resultsArgs,
199
+ lang, langs, langData, fallbackLanguages,
200
+ basePath
201
+ });
202
+
203
+ const resultsDisplay = async function () {
204
+ const serverOutput = $p.get('serverOutput', true);
205
+ let allowPlugins = $p.get('allowPlugins', true);
206
+ if (allowPlugins === '0') {
207
+ allowPlugins = false;
208
+ }
209
+ const isHTML = serverOutput === 'html';
210
+ res.writeHead(200, {'Content-Type': isHTML
211
+ ? 'text/html;charset=utf8'
212
+ : 'application/json;charset=utf8'
213
+ });
214
+ // Todo: Move sw-sample.js to bahaiwritings and test
215
+ const result = await resultsDisplayServer.call(
216
+ // Context
217
+ {
218
+ ...userParamsWithDefaults, lang, langs, fallbackLanguages
219
+ },
220
+ // resultsArgs
221
+ {
222
+ l,
223
+ lang,
224
+ fallbackLanguages,
225
+ locales: l.strings,
226
+ $p,
227
+ basePath,
217
228
  skipIndexedDB: false,
218
229
  allowPlugins,
219
230
  serverOutput,
220
231
  langData,
221
232
  prefI18n: $p.get('prefI18n', true)
222
- };
223
- // Todo: Move sw-sample.js to bahaiwritings and test
224
- const result = await resultsDisplayServer.call(
225
- {
226
- ...userParamsWithDefaults, lang, langs, fallbackLanguages
227
- }, resultsArgs, ...args
228
- );
229
- res.end(isHTML ? result : JSON.stringify(result));
230
- }
231
- });
233
+ }
234
+ );
235
+ res.end(isHTML ? result : JSON.stringify(result));
236
+ };
237
+
238
+ await resultsDisplay();
232
239
  });
240
+
233
241
  if (!userParams.domain) {
234
242
  srv.listen(port);
235
243
  } else {
package/sw-sample.js CHANGED
@@ -77,7 +77,7 @@ function logError (error, ...messages) {
77
77
  async function tryAndRetry (cb, timeout, errMessage, time = 0) {
78
78
  time++;
79
79
  try {
80
- // eslint-disable-next-line node/callback-return
80
+ // eslint-disable-next-line n/callback-return
81
81
  await cb(time);
82
82
  return undefined;
83
83
  } catch (err) {
@@ -1,47 +0,0 @@
1
- import {IMF} from 'imf';
2
-
3
- export default function getIMFFallbackResults ({
4
- $p,
5
- lang, langs, langData, fallbackLanguages,
6
- resultsDisplay,
7
- basePath = '',
8
- localeCallback = false
9
- }) {
10
- return new Promise((resolve, reject) => {
11
- const resultsCallback = (...args) => {
12
- const [l10n] = args;
13
- if (!$p.l10n) {
14
- $p.l10n = l10n;
15
- }
16
- return resultsDisplay({
17
- l: l10n,
18
- lang,
19
- fallbackLanguages,
20
- imfLocales: imf.locales,
21
- $p,
22
- basePath
23
- }, ...args);
24
- };
25
- const imf = IMF({
26
- languages: lang,
27
- fallbackLanguages,
28
- localeFileResolver (code) {
29
- // Todo: For editing of locales, we might instead resolve all
30
- // `$ref` (as with <https://github.com/whitlockjc/json-refs>) and
31
- // replace IMF() loadLocales behavior with our own now resolved
32
- // locales; see https://github.com/jdorn/json-editor/issues/132
33
- return basePath + (langData.localeFileBasePath) + langs.find((l) =>
34
- l.code === code
35
- ).locale.$ref;
36
- },
37
- async callback (...args) {
38
- if (localeCallback && localeCallback(...args)) {
39
- resolve();
40
- return;
41
- }
42
- await resultsCallback(...args);
43
- resolve();
44
- }
45
- });
46
- });
47
- }