Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add language select, abstract text transformations #584

Merged
merged 101 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
5bbbe72
Copy functions from JapaneseUtil
toasted-nutbread Jan 21, 2024
52a5f5b
Remove JapaneseUtil
toasted-nutbread Jan 21, 2024
59fd9ed
Update usages of JapaneseUtil functions
toasted-nutbread Jan 21, 2024
188bab5
part1
StefanVukovic99 Jan 22, 2024
ed08427
frotend done?
StefanVukovic99 Jan 22, 2024
004fc0e
fix tests
StefanVukovic99 Jan 22, 2024
f07b236
offscreen and type complications
StefanVukovic99 Jan 22, 2024
2d71abb
add tests
StefanVukovic99 Jan 23, 2024
982323a
Merge branch 'text-transformation-tests' into language-select
StefanVukovic99 Jan 23, 2024
53ed36f
start fixing tests
StefanVukovic99 Jan 23, 2024
b1b4446
keep fixing tests
StefanVukovic99 Jan 23, 2024
c9b9675
fix tests
StefanVukovic99 Jan 23, 2024
e0ff55f
Copy functions from JapaneseUtil
toasted-nutbread Jan 21, 2024
51d1d22
Remove JapaneseUtil
toasted-nutbread Jan 21, 2024
e5c6994
Update usages of JapaneseUtil functions
toasted-nutbread Jan 21, 2024
1185860
Merge branch 'master' into text-transformation-tests
StefanVukovic99 Jan 27, 2024
bea7c56
Merge remote-tracking branch 'toasted-nutbread/japanese-util-refactor…
StefanVukovic99 Jan 27, 2024
beac9ea
Merge branch 'master' into language-select
StefanVukovic99 Jan 27, 2024
d6cb0cc
delete pt
StefanVukovic99 Jan 27, 2024
a262149
Merge branch 'master' into text-transformation-tests
djahandarie Jan 28, 2024
9437888
renames
StefanVukovic99 Jan 28, 2024
197b746
Merge branch 'master' into text-transformation-tests
StefanVukovic99 Jan 28, 2024
ee3c4c2
Merge branch 'text-transformation-tests' into language-select
StefanVukovic99 Jan 28, 2024
4ef38ae
add tests
StefanVukovic99 Jan 28, 2024
2925c3f
Merge branch 'master' into language-select
StefanVukovic99 Jan 28, 2024
42580fa
kebab-case filenames
StefanVukovic99 Jan 28, 2024
8292ec7
lint
StefanVukovic99 Jan 29, 2024
3e95962
Merge branch 'master' into language-select
StefanVukovic99 Jan 29, 2024
39ec942
Merge branch 'master' into language-select
StefanVukovic99 Jan 31, 2024
96a0b0e
Merge branch 'master' into language-select
StefanVukovic99 Jan 31, 2024
44ecc94
minor fixes
StefanVukovic99 Jan 31, 2024
b4eeb66
Merge branch 'master' into language-select
StefanVukovic99 Feb 1, 2024
77e7944
merge
StefanVukovic99 Feb 1, 2024
61e0ff6
Merge branch 'master' into language-select
StefanVukovic99 Feb 1, 2024
97d0564
Merge branch 'master' into language-select
StefanVukovic99 Feb 3, 2024
2580d24
fixes
StefanVukovic99 Feb 3, 2024
16c1e39
fix part of comments
StefanVukovic99 Feb 3, 2024
2865806
fix more comments
StefanVukovic99 Feb 3, 2024
d6b4708
delete unused types
StefanVukovic99 Feb 3, 2024
5abd90c
comment
StefanVukovic99 Feb 3, 2024
9b5bfa0
comment
StefanVukovic99 Feb 3, 2024
c088e23
do backend
StefanVukovic99 Feb 3, 2024
61c0236
other files
StefanVukovic99 Feb 3, 2024
0b59ceb
move fetch utils to own file
StefanVukovic99 Feb 3, 2024
46b782b
remove extra line
StefanVukovic99 Feb 3, 2024
5acad61
add extra line
StefanVukovic99 Feb 3, 2024
c3b594d
remove unnecessary export
StefanVukovic99 Feb 3, 2024
0142a91
simplify folder structure
StefanVukovic99 Feb 3, 2024
cd560a2
Merge branch 'fetch-utilities' into language-select
StefanVukovic99 Feb 3, 2024
1ed8653
remove redundant async
StefanVukovic99 Feb 3, 2024
b204d3e
fix param type in api
StefanVukovic99 Feb 3, 2024
a9b6f4e
fix language index
StefanVukovic99 Feb 3, 2024
986a417
undo changes to cssStyleApplier
StefanVukovic99 Feb 3, 2024
fc6cccf
Merge branch 'fetch-utilities' into language-select
StefanVukovic99 Feb 3, 2024
be400d4
Merge branch 'master' into language-select
StefanVukovic99 Feb 4, 2024
e9dfc45
undo changes to utilities.js
StefanVukovic99 Feb 4, 2024
7079ceb
undo changes to utilities.js
StefanVukovic99 Feb 4, 2024
79a6eb3
simplify language util
StefanVukovic99 Feb 4, 2024
b1314cd
lint
StefanVukovic99 Feb 4, 2024
996c721
undo phantom changes to anki integration
StefanVukovic99 Feb 4, 2024
0ed108a
require textTransformations options
StefanVukovic99 Feb 4, 2024
68e6fd8
explicit locale in localeCompare
StefanVukovic99 Feb 4, 2024
49d02e7
punctuate notes
StefanVukovic99 Feb 4, 2024
2ef75a8
prefer early exit
StefanVukovic99 Feb 4, 2024
17ceb75
rename LanguageOptionsObjectMap
StefanVukovic99 Feb 4, 2024
6557689
rename to textPreprocessor
StefanVukovic99 Feb 4, 2024
0611e06
tuple with names instead of boolean array
StefanVukovic99 Feb 4, 2024
12911af
safe data setting
StefanVukovic99 Feb 4, 2024
2c8a92a
optional chaining
StefanVukovic99 Feb 4, 2024
c1d2197
simplify LanguageOptions
StefanVukovic99 Feb 4, 2024
3b64fc2
encapsulate languages
StefanVukovic99 Feb 4, 2024
4699904
delete language util
StefanVukovic99 Feb 4, 2024
f032d99
nullable language in text preprocessors controller
StefanVukovic99 Feb 4, 2024
ddb9b97
Merge branch 'master' into language-select
StefanVukovic99 Feb 5, 2024
183215a
rename transform to process
StefanVukovic99 Feb 5, 2024
a99442f
Merge branch 'master' into language-select
StefanVukovic99 Feb 5, 2024
edce060
remove settings
StefanVukovic99 Feb 7, 2024
a1d3c8d
Merge branch 'master' into language-select
StefanVukovic99 Feb 7, 2024
ecc08ee
make translation advanced again
StefanVukovic99 Feb 7, 2024
fb249cf
remove unused getTextTransformations api call
StefanVukovic99 Feb 7, 2024
ab8112b
comments
StefanVukovic99 Feb 8, 2024
950979c
change language types
StefanVukovic99 Feb 8, 2024
3a89fae
RIP flags
StefanVukovic99 Feb 8, 2024
eda3b76
comments
StefanVukovic99 Feb 8, 2024
61788d5
Merge branch 'master' into language-select
StefanVukovic99 Feb 8, 2024
f416de0
fix tests
StefanVukovic99 Feb 8, 2024
9486269
Merge branch 'master' into language-select
StefanVukovic99 Feb 11, 2024
dbbdf0a
lint
StefanVukovic99 Feb 11, 2024
a3007d3
Text preprocessor type changes (#10)
toasted-nutbread Feb 11, 2024
4c387d6
Merge remote-tracking branch 'yezichak/language-select' into language…
StefanVukovic99 Feb 11, 2024
53433fa
lint
StefanVukovic99 Feb 11, 2024
e45f39b
update translator benchmark
StefanVukovic99 Feb 11, 2024
56ffd3f
undo markdown changes
StefanVukovic99 Feb 11, 2024
4ab1696
Merge branch 'master' into language-select
djahandarie Feb 12, 2024
f82987e
Merge branch 'master' into language-select
StefanVukovic99 Feb 12, 2024
65d6b31
Merge remote-tracking branch 'yezichak/language-select' into language…
StefanVukovic99 Feb 12, 2024
950e1b3
undo markdown changes
StefanVukovic99 Feb 12, 2024
d639e25
undo markdown changes
StefanVukovic99 Feb 12, 2024
bcd6d1d
Merge branch 'master' into language-select
StefanVukovic99 Feb 15, 2024
1d7a349
more merge
StefanVukovic99 Feb 15, 2024
000bfa7
simplify language controller
StefanVukovic99 Feb 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@
"ext/js/core.js",
"ext/js/core/extension-error.js",
"ext/js/**/sandbox/**/*.js",
"ext/js/language/japanese.js",
"ext/js/language/japanese-wanakana.js"
"ext/js/language/languages/ja/japanese.js",
"ext/js/language/languages/ja/japanese-wanakana.js"
],
"env": {
"webextensions": false
Expand Down
1 change: 1 addition & 0 deletions dev/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"error": ["../types/ext/error"],
"event-listener-collection": ["../types/ext/event-listener-collection"],
"japanese-util": ["../types/ext/japanese-util"],
"language": ["../types/ext/language"],
"ext/json-schema": ["../types/ext/json-schema"],
"log": ["../types/ext/log"],
"settings": ["../types/ext/settings"],
Expand Down
77 changes: 40 additions & 37 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@
"sentenceParsing",
"inputs",
"clipboard",
"accessibility"
"accessibility",
"languages"
],
"properties": {
"general": {
"type": "object",
"required": [
"enable",
"language",
"resultOutputMode",
"debugInfo",
"maxResults",
Expand Down Expand Up @@ -126,6 +128,10 @@
"type": "boolean",
"default": true
},
"language": {
"type": "string",
"default": "ja"
},
"resultOutputMode": {
"type": "string",
"enum": ["group", "merge", "split"],
Expand Down Expand Up @@ -722,12 +728,6 @@
"translation": {
"type": "object",
"required": [
"convertHalfWidthCharacters",
"convertNumericCharacters",
"convertAlphabeticCharacters",
"convertHiraganaToKatakana",
"convertKatakanaToHiragana",
"collapseEmphaticSequences",
"textReplacements",
"searchResolution"
],
Expand All @@ -740,36 +740,6 @@
],
"default": "letter"
},
"convertHalfWidthCharacters": {
"type": "string",
"enum": ["false", "true", "variant"],
"default": "false"
},
"convertNumericCharacters": {
"type": "string",
"enum": ["false", "true", "variant"],
"default": "false"
},
"convertAlphabeticCharacters": {
"type": "string",
"enum": ["false", "true", "variant"],
"default": "false"
},
"convertHiraganaToKatakana": {
"type": "string",
"enum": ["false", "true", "variant"],
"default": "false"
},
"convertKatakanaToHiragana": {
"type": "string",
"enum": ["false", "true", "variant"],
"default": "variant"
},
"collapseEmphaticSequences": {
"type": "string",
"enum": ["false", "true", "full"],
"default": "false"
},
"textReplacements": {
"type": "object",
"required": [
Expand Down Expand Up @@ -813,6 +783,39 @@
}
}
},
"languages": {
"type": "object",
"propertyNames": {
"type": "string"
},
"additionalProperties": {
"type": "object",
"properties": {
"textTransformations": {
"type": "object",
"propertyNames": {
"type": "string"
},
"additionalProperties": {
"type": "string",
"default": "false"
}
}
}
},
"default": {
"ja": {
StefanVukovic99 marked this conversation as resolved.
Show resolved Hide resolved
"textTransformations": {
"convertHalfWidthCharacters": "false",
"convertNumericCharacters": "false",
"convertAlphabeticCharacters": "false",
"convertHiraganaToKatakana": "false",
"convertKatakanaToHiragana": "variant",
"collapseEmphaticSequences": "false"
}
}
}
},
"dictionaries": {
"type": "array",
"items": {
Expand Down
88 changes: 36 additions & 52 deletions ext/js/background/backend.js
StefanVukovic99 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ import {ClipboardReader} from '../comm/clipboard-reader.js';
import {Mecab} from '../comm/mecab.js';
import {createApiMap, invokeApiMapHandler} from '../core/api-map.js';
import {ExtensionError} from '../core/extension-error.js';
import {readResponseJson} from '../core/json.js';
import {log} from '../core/logger.js';
import {clone, deferPromise, isObject, promiseTimeout} from '../core/utilities.js';
import {clone, deferPromise, fetchJson, fetchText, isObject, promiseTimeout} from '../core/utilities.js';
import {AnkiUtil} from '../data/anki-util.js';
import {OptionsUtil} from '../data/options-util.js';
import {PermissionsUtil} from '../data/permissions-util.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
import {DictionaryDatabase} from '../dictionary/dictionary-database.js';
import {Environment} from '../extension/environment.js';
import {ObjectPropertyAccessor} from '../general/object-property-accessor.js';
import {distributeFuriganaInflected, isCodePointJapanese, isStringPartiallyJapanese, convertKatakanaToHiragana as jpConvertKatakanaToHiragana} from '../language/japanese.js';
import {LanguageUtil} from '../language/language-util.js';
import {distributeFuriganaInflected, isCodePointJapanese, isStringPartiallyJapanese, convertKatakanaToHiragana as jpConvertKatakanaToHiragana} from '../language/languages/ja/japanese.js';
import {Translator} from '../language/translator.js';
import {AudioDownloader} from '../media/audio-downloader.js';
import {MediaUtil} from '../media/media-util.js';
import {ClipboardReaderProxy, DictionaryDatabaseProxy, OffscreenProxy, TranslatorProxy} from './offscreen-proxy.js';
import {ClipboardReaderProxy, DictionaryDatabaseProxy, LanguageUtilProxy, OffscreenProxy, TranslatorProxy} from './offscreen-proxy.js';
import {ProfileConditionsUtil} from './profile-conditions-util.js';
import {RequestBuilder} from './request-builder.js';
import {injectStylesheet} from './script-manager.js';
Expand Down Expand Up @@ -65,8 +65,11 @@ export class Backend {
this._offscreen = null;
/** @type {DictionaryDatabase|DictionaryDatabaseProxy} */
this._dictionaryDatabase = new DictionaryDatabase();
/** @type {LanguageUtil|LanguageUtilProxy} */
this._languageUtil = new LanguageUtil();
/** @type {Translator|TranslatorProxy} */
this._translator = new Translator({
languageUtil: this._languageUtil,
database: this._dictionaryDatabase
});
/** @type {ClipboardReader|ClipboardReaderProxy} */
Expand All @@ -81,6 +84,8 @@ export class Backend {
this._offscreen = new OffscreenProxy(webExtension);
/** @type {DictionaryDatabase|DictionaryDatabaseProxy} */
this._dictionaryDatabase = new DictionaryDatabaseProxy(this._offscreen);
/** @type {LanguageUtil|LanguageUtilProxy} */
this._languageUtil = new LanguageUtilProxy(this._offscreen);
/** @type {Translator|TranslatorProxy} */
this._translator = new TranslatorProxy(this._offscreen);
/** @type {ClipboardReader|ClipboardReaderProxy} */
Expand Down Expand Up @@ -187,7 +192,9 @@ export class Backend {
['textHasJapaneseCharacters', this._onApiTextHasJapaneseCharacters.bind(this)],
['getTermFrequencies', this._onApiGetTermFrequencies.bind(this)],
['findAnkiNotes', this._onApiFindAnkiNotes.bind(this)],
['openCrossFramePort', this._onApiOpenCrossFramePort.bind(this)]
['openCrossFramePort', this._onApiOpenCrossFramePort.bind(this)],
['getLanguages', this._onApiGetLanguages.bind(this)],
['getTextTransformations', this._onApiGetTextTransformations.bind(this)]
]);
/* eslint-enable no-multi-spaces */

Expand Down Expand Up @@ -282,14 +289,16 @@ export class Backend {
log.error(e);
}

/** @type {import('deinflector').ReasonsRaw} */
const deinflectionReasons = await this._fetchJson('/data/deinflect.json');
this._translator.prepare(deinflectionReasons);

await this._optionsUtil.prepare();
this._defaultAnkiFieldTemplates = (await this._fetchText('/data/templates/default-anki-field-templates.handlebars')).trim();
this._defaultAnkiFieldTemplates = (await fetchText('/data/templates/default-anki-field-templates.handlebars')).trim();
this._options = await this._optionsUtil.load();

await this._languageUtil.prepare();

/** @type {import('deinflector').ReasonsRaw} */
const deinflectionReasons = await fetchJson('/data/deinflect.json');
this._translator.prepare(deinflectionReasons);

this._applyOptions('background');

const options = this._getProfileOptions({current: true}, false);
Expand Down Expand Up @@ -672,7 +681,7 @@ export class Backend {
if (!url.startsWith('/') || url.startsWith('//') || !url.endsWith('.css')) {
throw new Error('Invalid URL');
}
return await this._fetchText(url);
return await fetchText(url);
}

/** @type {import('api').ApiHandler<'getEnvironmentInfo'>} */
Expand All @@ -687,7 +696,7 @@ export class Backend {

/** @type {import('api').ApiHandler<'getDisplayTemplatesHtml'>} */
async _onApiGetDisplayTemplatesHtml() {
return await this._fetchText('/display-templates.html');
return await fetchText('/display-templates.html');
}

/** @type {import('api').ApiHandler<'getZoom'>} */
Expand Down Expand Up @@ -911,6 +920,16 @@ export class Backend {
return {targetTabId, targetFrameId};
}

/** @type {import('api').ApiHandler<'getLanguages'>} */
_onApiGetLanguages() {
return this._languageUtil.getLanguages();
}

/** @type {import('api').ApiHandler<'getTextTransformations'>} */
_onApiGetTextTransformations({language}) {
return this._languageUtil.getTextTransformations(language);
}

// Command handlers

/**
Expand Down Expand Up @@ -1852,44 +1871,6 @@ export class Backend {
});
}

/**
* @param {string} url
* @returns {Promise<Response>}
*/
async _fetchAsset(url) {
const response = await fetch(chrome.runtime.getURL(url), {
method: 'GET',
mode: 'no-cors',
cache: 'default',
credentials: 'omit',
redirect: 'follow',
referrerPolicy: 'no-referrer'
});
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.status}`);
}
return response;
}

/**
* @param {string} url
* @returns {Promise<string>}
*/
async _fetchText(url) {
const response = await this._fetchAsset(url);
return await response.text();
}

/**
* @template [T=unknown]
* @param {string} url
* @returns {Promise<T>}
*/
async _fetchJson(url) {
const response = await this._fetchAsset(url);
return await readResponseJson(response);
}

/**
* @template {import('application').ApiNames} TName
* @param {import('application').ApiMessage<TName>} message
Expand Down Expand Up @@ -2406,7 +2387,7 @@ export class Backend {
if (typeof deinflect !== 'boolean') { deinflect = true; }
const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options);
const {
general: {mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder},
general: {mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder, language},
scanning: {alphanumeric},
translation: {
convertHalfWidthCharacters,
Expand All @@ -2419,6 +2400,7 @@ export class Backend {
searchResolution
}
} = options;
const textTransformationsOptions = options.languages[language].textTransformations || {};
StefanVukovic99 marked this conversation as resolved.
Show resolved Hide resolved
const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions);
let excludeDictionaryDefinitions = null;
if (mode === 'merge' && !enabledDictionaryMap.has(mainDictionary)) {
Expand Down Expand Up @@ -2448,7 +2430,9 @@ export class Backend {
searchResolution,
textReplacements,
enabledDictionaryMap,
excludeDictionaryDefinitions
excludeDictionaryDefinitions,
language,
textTransformationsOptions
};
}

Expand Down
32 changes: 32 additions & 0 deletions ext/js/background/offscreen-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ export class DictionaryDatabaseProxy {
}
}

export class LanguageUtilProxy {
/**
* @param {OffscreenProxy} offscreen
*/
constructor(offscreen) {
/** @type {OffscreenProxy} */
this._offscreen = offscreen;
}

/**
* @returns {Promise<void>}
*/
async prepare() {
await this._offscreen.sendMessagePromise({action: 'languageUtilPrepareOffscreen'});
}

/**
* @param {string} iso
* @returns {Promise<import('language').TextTransformation[]>}
*/
async getTextTransformations(iso) {
return this._offscreen.sendMessagePromise({action: 'getTextTransformationsOffscreen', params: {iso}});
}

/**
* @returns {Promise<import('language').Language[]>}
*/
async getLanguages() {
return this._offscreen.sendMessagePromise({action: 'getLanguagesOffscreen'});
StefanVukovic99 marked this conversation as resolved.
Show resolved Hide resolved
}
}

export class TranslatorProxy {
/**
* @param {OffscreenProxy} offscreen
Expand Down
Loading
Loading