From 82462edce0215e4de14ce7d9348304b2c2ca6a4d Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 7 Apr 2020 19:41:02 -0400 Subject: Add support for API handlers to be optionally asynchronous --- ext/bg/js/backend.js | 77 ++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 35 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1fa7ede1..ed4a24ed 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -77,33 +77,33 @@ class Backend { this.messageToken = yomichan.generateId(16); this._messageHandlers = new Map([ - ['yomichanCoreReady', this._onApiYomichanCoreReady.bind(this)], - ['optionsSchemaGet', this._onApiOptionsSchemaGet.bind(this)], - ['optionsGet', this._onApiOptionsGet.bind(this)], - ['optionsGetFull', this._onApiOptionsGetFull.bind(this)], - ['optionsSet', this._onApiOptionsSet.bind(this)], - ['optionsSave', this._onApiOptionsSave.bind(this)], - ['kanjiFind', this._onApiKanjiFind.bind(this)], - ['termsFind', this._onApiTermsFind.bind(this)], - ['textParse', this._onApiTextParse.bind(this)], - ['textParseMecab', this._onApiTextParseMecab.bind(this)], - ['definitionAdd', this._onApiDefinitionAdd.bind(this)], - ['definitionsAddable', this._onApiDefinitionsAddable.bind(this)], - ['noteView', this._onApiNoteView.bind(this)], - ['templateRender', this._onApiTemplateRender.bind(this)], - ['commandExec', this._onApiCommandExec.bind(this)], - ['audioGetUri', this._onApiAudioGetUri.bind(this)], - ['screenshotGet', this._onApiScreenshotGet.bind(this)], - ['forward', this._onApiForward.bind(this)], - ['frameInformationGet', this._onApiFrameInformationGet.bind(this)], - ['injectStylesheet', this._onApiInjectStylesheet.bind(this)], - ['getEnvironmentInfo', this._onApiGetEnvironmentInfo.bind(this)], - ['clipboardGet', this._onApiClipboardGet.bind(this)], - ['getDisplayTemplatesHtml', this._onApiGetDisplayTemplatesHtml.bind(this)], - ['getQueryParserTemplatesHtml', this._onApiGetQueryParserTemplatesHtml.bind(this)], - ['getZoom', this._onApiGetZoom.bind(this)], - ['getMessageToken', this._onApiGetMessageToken.bind(this)], - ['getDefaultAnkiFieldTemplates', this._onApiGetDefaultAnkiFieldTemplates.bind(this)] + ['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: true}], + ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: true}], + ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: true}], + ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: true}], + ['optionsSet', {handler: this._onApiOptionsSet.bind(this), async: true}], + ['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}], + ['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}], + ['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}], + ['textParse', {handler: this._onApiTextParse.bind(this), async: true}], + ['textParseMecab', {handler: this._onApiTextParseMecab.bind(this), async: true}], + ['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}], + ['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}], + ['noteView', {handler: this._onApiNoteView.bind(this), async: true}], + ['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}], + ['commandExec', {handler: this._onApiCommandExec.bind(this), async: true}], + ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}], + ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}], + ['forward', {handler: this._onApiForward.bind(this), async: true}], + ['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}], + ['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}], + ['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}], + ['clipboardGet', {handler: this._onApiClipboardGet.bind(this), async: true}], + ['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}], + ['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}], + ['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}], + ['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: true}], + ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: true}] ]); this._commandHandlers = new Map([ @@ -167,16 +167,23 @@ class Backend { } onMessage({action, params}, sender, callback) { - const handler = this._messageHandlers.get(action); - if (typeof handler !== 'function') { return false; } + const messageHandler = this._messageHandlers.get(action); + if (typeof messageHandler === 'undefined') { return false; } + + const {handler, async} = messageHandler; try { - const promise = handler(params, sender); - promise.then( - (result) => callback({result}), - (error) => callback({error: errorToJson(error)}) - ); - return true; + const promiseOrResult = handler(params, sender); + if (async) { + promiseOrResult.then( + (result) => callback({result}), + (error) => callback({error: errorToJson(error)}) + ); + return true; + } else { + callback({result: promiseOrResult}); + return false; + } } catch (error) { callback({error: errorToJson(error)}); return false; -- cgit v1.2.3 From 038e43d19dff736a818125db695699ad63437704 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 7 Apr 2020 19:47:46 -0400 Subject: Update some API handlers to be synchronous --- ext/bg/js/backend.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index ed4a24ed..8f494166 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -78,9 +78,9 @@ class Backend { this._messageHandlers = new Map([ ['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: true}], - ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: true}], - ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: true}], - ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: true}], + ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: false}], + ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: false}], + ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: false}], ['optionsSet', {handler: this._onApiOptionsSet.bind(this), async: true}], ['optionsSave', {handler: this._onApiOptionsSave.bind(this), async: true}], ['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}], @@ -91,7 +91,7 @@ class Backend { ['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}], ['noteView', {handler: this._onApiNoteView.bind(this), async: true}], ['templateRender', {handler: this._onApiTemplateRender.bind(this), async: true}], - ['commandExec', {handler: this._onApiCommandExec.bind(this), async: true}], + ['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}], ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}], ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}], ['forward', {handler: this._onApiForward.bind(this), async: true}], @@ -102,8 +102,8 @@ class Backend { ['getDisplayTemplatesHtml', {handler: this._onApiGetDisplayTemplatesHtml.bind(this), async: true}], ['getQueryParserTemplatesHtml', {handler: this._onApiGetQueryParserTemplatesHtml.bind(this), async: true}], ['getZoom', {handler: this._onApiGetZoom.bind(this), async: true}], - ['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: true}], - ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: true}] + ['getMessageToken', {handler: this._onApiGetMessageToken.bind(this), async: false}], + ['getDefaultAnkiFieldTemplates', {handler: this._onApiGetDefaultAnkiFieldTemplates.bind(this), async: false}] ]); this._commandHandlers = new Map([ @@ -332,15 +332,15 @@ class Backend { }); } - async _onApiOptionsSchemaGet() { + _onApiOptionsSchemaGet() { return this.getOptionsSchema(); } - async _onApiOptionsGet({optionsContext}) { + _onApiOptionsGet({optionsContext}) { return this.getOptions(optionsContext); } - async _onApiOptionsGetFull() { + _onApiOptionsGetFull() { return this.getFullOptions(); } @@ -547,7 +547,7 @@ class Backend { return this._renderTemplate(template, data); } - async _onApiCommandExec({command, params}) { + _onApiCommandExec({command, params}) { return this._runCommand(command, params); } @@ -698,11 +698,11 @@ class Backend { }); } - async _onApiGetMessageToken() { + _onApiGetMessageToken() { return this.messageToken; } - async _onApiGetDefaultAnkiFieldTemplates() { + _onApiGetDefaultAnkiFieldTemplates() { return this.defaultAnkiFieldTemplates; } -- cgit v1.2.3 From 96566b8581549adba7cd24f37537fd7910eb8d37 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 7 Apr 2020 19:49:54 -0400 Subject: Update forward to be synchronous and ignore the reply --- ext/bg/js/backend.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 8f494166..31d29a20 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -94,7 +94,7 @@ class Backend { ['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}], ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}], ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}], - ['forward', {handler: this._onApiForward.bind(this), async: true}], + ['forward', {handler: this._onApiForward.bind(this), async: false}], ['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}], ['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}], ['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}], @@ -569,13 +569,13 @@ class Backend { _onApiForward({action, params}, sender) { if (!(sender && sender.tab)) { - return Promise.resolve(); + return false; } const tabId = sender.tab.id; - return new Promise((resolve) => { - chrome.tabs.sendMessage(tabId, {action, params}, (response) => resolve(response)); - }); + const callback = () => this.checkLastError(chrome.runtime.lastError); + chrome.tabs.sendMessage(tabId, {action, params}, callback); + return true; } _onApiFrameInformationGet(params, sender) { -- cgit v1.2.3 From a2b66dc6cc3bd0d037c050eb49e270189a6617fb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 7 Apr 2020 19:51:39 -0400 Subject: Rename apiForward to apiBroadcast --- ext/bg/js/backend.js | 4 ++-- ext/fg/js/float.js | 6 +++--- ext/fg/js/frame-offset-forwarder.js | 4 ++-- ext/fg/js/frontend-initialize.js | 4 ++-- ext/fg/js/frontend.js | 6 +++--- ext/mixed/js/api.js | 4 ++-- ext/mixed/js/display.js | 4 ++-- 7 files changed, 16 insertions(+), 16 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 31d29a20..c471c594 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -94,7 +94,7 @@ class Backend { ['commandExec', {handler: this._onApiCommandExec.bind(this), async: false}], ['audioGetUri', {handler: this._onApiAudioGetUri.bind(this), async: true}], ['screenshotGet', {handler: this._onApiScreenshotGet.bind(this), async: true}], - ['forward', {handler: this._onApiForward.bind(this), async: false}], + ['broadcast', {handler: this._onApiBroadcast.bind(this), async: false}], ['frameInformationGet', {handler: this._onApiFrameInformationGet.bind(this), async: true}], ['injectStylesheet', {handler: this._onApiInjectStylesheet.bind(this), async: true}], ['getEnvironmentInfo', {handler: this._onApiGetEnvironmentInfo.bind(this), async: true}], @@ -567,7 +567,7 @@ class Backend { }); } - _onApiForward({action, params}, sender) { + _onApiBroadcast({action, params}, sender) { if (!(sender && sender.tab)) { return false; } diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 01055ca6..8c00d0a5 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -18,7 +18,7 @@ /* global * Display - * apiForward + * apiBroadcast * apiGetMessageToken * popupNestedInitialize */ @@ -80,7 +80,7 @@ class DisplayFloat extends Display { this.setContentScale(scale); - apiForward('popupPrepareCompleted', {targetPopupId: this._popupId}); + apiBroadcast('popupPrepareCompleted', {targetPopupId: this._popupId}); } onError(error) { @@ -181,7 +181,7 @@ class DisplayFloat extends Display { }, 2000 ); - apiForward('requestDocumentInformationBroadcast', {uniqueId}); + apiBroadcast('requestDocumentInformationBroadcast', {uniqueId}); const {title} = await promise; return title; diff --git a/ext/fg/js/frame-offset-forwarder.js b/ext/fg/js/frame-offset-forwarder.js index 7b417b6e..88c1b72c 100644 --- a/ext/fg/js/frame-offset-forwarder.js +++ b/ext/fg/js/frame-offset-forwarder.js @@ -17,7 +17,7 @@ */ /* global - * apiForward + * apiBroadcast */ class FrameOffsetForwarder { @@ -97,6 +97,6 @@ class FrameOffsetForwarder { } _forwardFrameOffsetOrigin(offset, uniqueId) { - apiForward('frameOffset', {offset, uniqueId}); + apiBroadcast('frameOffset', {offset, uniqueId}); } } diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js index 4a1409db..d2572746 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/frontend-initialize.js @@ -21,7 +21,7 @@ * Frontend * PopupProxy * PopupProxyHost - * apiForward + * apiBroadcast * apiOptionsGet */ @@ -44,7 +44,7 @@ async function main() { } } ); - apiForward('rootPopupRequestInformationBroadcast'); + apiBroadcast('rootPopupRequestInformationBroadcast'); const {popupId, frameId} = await rootPopupInformationPromise; const frameOffsetForwarder = new FrameOffsetForwarder(); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 31843212..bcb0b84a 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -18,7 +18,7 @@ /* global * TextScanner - * apiForward + * apiBroadcast * apiGetZoom * apiKanjiFind * apiOptionsGet @@ -261,12 +261,12 @@ class Frontend extends TextScanner { _broadcastRootPopupInformation() { if (!this.popup.isProxy() && this.popup.depth === 0) { - apiForward('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId}); + apiBroadcast('rootPopupInformation', {popupId: this.popup.id, frameId: this.popup.frameId}); } } _broadcastDocumentInformation(uniqueId) { - apiForward('documentInformationBroadcast', { + apiBroadcast('documentInformationBroadcast', { uniqueId, frameId: this.popup.frameId, title: document.title diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index feec94df..d28e3ab6 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -81,8 +81,8 @@ function apiScreenshotGet(options) { return _apiInvoke('screenshotGet', {options}); } -function apiForward(action, params) { - return _apiInvoke('forward', {action, params}); +function apiBroadcast(action, params) { + return _apiInvoke('broadcast', {action, params}); } function apiFrameInformationGet() { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 2f456c3e..710674b2 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -23,9 +23,9 @@ * DisplayGenerator * WindowScroll * apiAudioGetUri + * apiBroadcast * apiDefinitionAdd * apiDefinitionsAddable - * apiForward * apiKanjiFind * apiNoteView * apiOptionsGet @@ -855,7 +855,7 @@ class Display { } setPopupVisibleOverride(visible) { - return apiForward('popupSetVisibleOverride', {visible}); + return apiBroadcast('popupSetVisibleOverride', {visible}); } setSpinnerVisible(visible) { -- cgit v1.2.3 From c88ec43ad1523fefaa47b6e46dd0fa95f342559c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 7 Apr 2020 19:59:10 -0400 Subject: Fix yomichanCoreReady waiting for a response --- ext/bg/js/backend.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index c471c594..35e75bbe 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -77,7 +77,7 @@ class Backend { this.messageToken = yomichan.generateId(16); this._messageHandlers = new Map([ - ['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: true}], + ['yomichanCoreReady', {handler: this._onApiYomichanCoreReady.bind(this), async: false}], ['optionsSchemaGet', {handler: this._onApiOptionsSchemaGet.bind(this), async: false}], ['optionsGet', {handler: this._onApiOptionsGet.bind(this), async: false}], ['optionsGetFull', {handler: this._onApiOptionsGetFull.bind(this), async: false}], @@ -320,16 +320,15 @@ class Backend { _onApiYomichanCoreReady(_params, sender) { // tab ID isn't set in background (e.g. browser_action) + const callback = () => this.checkLastError(chrome.runtime.lastError); + const data = {action: 'backendPrepared'}; if (typeof sender.tab === 'undefined') { - const callback = () => this.checkLastError(chrome.runtime.lastError); - chrome.runtime.sendMessage({action: 'backendPrepared'}, callback); - return Promise.resolve(); + chrome.runtime.sendMessage(data, callback); + return false; + } else { + chrome.tabs.sendMessage(sender.tab.id, data, callback); + return true; } - - const tabId = sender.tab.id; - return new Promise((resolve) => { - chrome.tabs.sendMessage(tabId, {action: 'backendPrepared'}, resolve); - }); } _onApiOptionsSchemaGet() { -- cgit v1.2.3 From 99f58b49f6324441c1abd010a92cd0ceb1f889ea Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 21:56:00 -0400 Subject: Use data-expression-multi instead of data-multi --- ext/mixed/css/display.css | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'ext') diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index a4432016..ac17e2ec 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -298,13 +298,13 @@ button.action-button { content: "\3001"; } -.term-expression-list[data-multi=true]>.term-expression:last-of-type:after { +.entry[data-expression-multi=true] .term-expression-list>.term-expression:last-of-type:after { font-size: 2em; content: "\3000"; visibility: hidden; } -.term-expression-list[data-multi=true] .term-expression-details { +.entry[data-expression-multi=true] .term-expression-list .term-expression-details { display: inline-block; position: relative; width: 0; @@ -313,21 +313,21 @@ button.action-button { z-index: 1; } -.term-expression-list[data-multi=true] .term-expression:hover .term-expression-details { +.entry[data-expression-multi=true] .term-expression:hover .term-expression-details { visibility: visible; } -.term-expression-list[data-multi=true] .term-expression-details>.action-play-audio { +.entry[data-expression-multi=true] .term-expression-list .term-expression-details>.action-play-audio { position: absolute; left: 0; bottom: 0.5em; } -.term-expression-list:not([data-multi=true]) .term-expression-details>.action-play-audio { +.entry:not([data-expression-multi=true]) .term-expression-list .term-expression-details>.action-play-audio { display: none; } -.term-expression-list[data-multi=true] .term-expression-details>.tags { +.entry[data-expression-multi=true] .term-expression-list .term-expression-details>.tags { display: block; position: absolute; left: 0; @@ -335,7 +335,7 @@ button.action-button { white-space: nowrap; } -.term-expression-list[data-multi=true] .term-expression-details>.frequencies { +.entry[data-expression-multi=true] .term-expression-list .term-expression-details>.frequencies { display: block; position: absolute; left: 0; -- cgit v1.2.3 From 75c5c7aabc88379b9ba03ea7f0ccdb96bc000af0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 21:57:07 -0400 Subject: Remove data-multi assignments --- ext/mixed/js/display-generator.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index f1122e3d..cd76000a 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -361,7 +361,6 @@ class DisplayGenerator { container.appendChild(node); } - container.dataset.multi = 'true'; container.dataset.count = `${exclusiveExpressions.length + exclusiveReadings.length}`; container.dataset.expressionCount = `${exclusiveExpressions.length}`; container.dataset.readingCount = `${exclusiveReadings.length}`; @@ -464,7 +463,6 @@ class DisplayGenerator { ++count; } - container.dataset.multi = `${multi}`; container.dataset.count = `${count}`; return count; -- cgit v1.2.3 From d7e4ff067e8a2c39963b93829cdfea867bde6bf9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 21:59:02 -0400 Subject: Remove statics --- ext/mixed/js/display-generator.js | 62 +++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index cd76000a..2f1154eb 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -44,7 +44,7 @@ class DisplayGenerator { const debugInfoContainer = node.querySelector('.debug-info'); const bodyContainer = node.querySelector('.term-entry-body'); - const pitches = DisplayGenerator._getPitchInfos(details); + const pitches = this._getPitchInfos(details); const pitchCount = pitches.reduce((i, v) => i + v[1].length, 0); const expressionMulti = Array.isArray(details.expressions); @@ -70,11 +70,11 @@ class DisplayGenerator { let expressions = details.expressions; expressions = Array.isArray(expressions) ? expressions.map((e) => [e, termTags]) : null; - DisplayGenerator._appendMultiple(expressionsContainer, this.createTermExpression.bind(this), expressions, [[details, termTags]]); - DisplayGenerator._appendMultiple(reasonsContainer, this.createTermReason.bind(this), details.reasons); - DisplayGenerator._appendMultiple(frequenciesContainer, this.createFrequencyTag.bind(this), details.frequencies); - DisplayGenerator._appendMultiple(pitchesContainer, this.createPitches.bind(this), pitches); - DisplayGenerator._appendMultiple(definitionsContainer, this.createTermDefinitionItem.bind(this), details.definitions, [details]); + this._appendMultiple(expressionsContainer, this.createTermExpression.bind(this), expressions, [[details, termTags]]); + this._appendMultiple(reasonsContainer, this.createTermReason.bind(this), details.reasons); + this._appendMultiple(frequenciesContainer, this.createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(pitchesContainer, this.createPitches.bind(this), pitches); + this._appendMultiple(definitionsContainer, this.createTermDefinitionItem.bind(this), details.definitions, [details]); if (debugInfoContainer !== null) { debugInfoContainer.textContent = JSON.stringify(details, null, 4); @@ -100,7 +100,7 @@ class DisplayGenerator { // This case should not occur furiganaSegments = [{text: details.expression, furigana: details.reading}]; } - DisplayGenerator._appendFurigana(expressionContainer, furiganaSegments, this._appendKanjiLinks.bind(this)); + this._appendFurigana(expressionContainer, furiganaSegments, this._appendKanjiLinks.bind(this)); } if (!Array.isArray(termTags)) { @@ -110,9 +110,9 @@ class DisplayGenerator { const searchQueries = [details.expression, details.reading] .filter((x) => !!x) .map((x) => ({query: x})); - DisplayGenerator._appendMultiple(tagContainer, this.createTag.bind(this), termTags); - DisplayGenerator._appendMultiple(tagContainer, this.createSearchTag.bind(this), searchQueries); - DisplayGenerator._appendMultiple(frequencyContainer, this.createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(tagContainer, this.createTag.bind(this), termTags); + this._appendMultiple(tagContainer, this.createSearchTag.bind(this), searchQueries); + this._appendMultiple(frequencyContainer, this.createFrequencyTag.bind(this), details.frequencies); return node; } @@ -134,9 +134,9 @@ class DisplayGenerator { node.dataset.dictionary = details.dictionary; - DisplayGenerator._appendMultiple(tagListContainer, this.createTag.bind(this), details.definitionTags); - DisplayGenerator._appendMultiple(onlyListContainer, this.createTermOnly.bind(this), details.only); - DisplayGenerator._appendMultiple(glossaryContainer, this.createTermGlossaryItem.bind(this), details.glossary); + this._appendMultiple(tagListContainer, this.createTag.bind(this), details.definitionTags); + this._appendMultiple(onlyListContainer, this.createTermOnly.bind(this), details.only); + this._appendMultiple(glossaryContainer, this.createTermGlossaryItem.bind(this), details.glossary); return node; } @@ -145,7 +145,7 @@ class DisplayGenerator { const node = this._templateHandler.instantiate('term-glossary-item'); const container = node.querySelector('.term-glossary'); if (container !== null) { - DisplayGenerator._appendMultilineText(container, glossary); + this._appendMultilineText(container, glossary); } return node; } @@ -184,11 +184,11 @@ class DisplayGenerator { glyphContainer.textContent = details.character; } - DisplayGenerator._appendMultiple(frequenciesContainer, this.createFrequencyTag.bind(this), details.frequencies); - DisplayGenerator._appendMultiple(tagContainer, this.createTag.bind(this), details.tags); - DisplayGenerator._appendMultiple(glossaryContainer, this.createKanjiGlossaryItem.bind(this), details.glossary); - DisplayGenerator._appendMultiple(chineseReadingsContainer, this.createKanjiReading.bind(this), details.onyomi); - DisplayGenerator._appendMultiple(japaneseReadingsContainer, this.createKanjiReading.bind(this), details.kunyomi); + this._appendMultiple(frequenciesContainer, this.createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(tagContainer, this.createTag.bind(this), details.tags); + this._appendMultiple(glossaryContainer, this.createKanjiGlossaryItem.bind(this), details.glossary); + this._appendMultiple(chineseReadingsContainer, this.createKanjiReading.bind(this), details.onyomi); + this._appendMultiple(japaneseReadingsContainer, this.createKanjiReading.bind(this), details.kunyomi); if (statisticsContainer !== null) { statisticsContainer.appendChild(this.createKanjiInfoTable(details.stats.misc)); @@ -214,7 +214,7 @@ class DisplayGenerator { const node = this._templateHandler.instantiate('kanji-glossary-item'); const container = node.querySelector('.kanji-glossary'); if (container !== null) { - DisplayGenerator._appendMultilineText(container, glossary); + this._appendMultilineText(container, glossary); } return node; } @@ -231,7 +231,7 @@ class DisplayGenerator { const container = node.querySelector('.kanji-info-table-body'); if (container !== null) { - const count = DisplayGenerator._appendMultiple(container, this.createKanjiInfoTableItem.bind(this), details); + const count = this._appendMultiple(container, this.createKanjiInfoTableItem.bind(this), details); if (count === 0) { const n = this.createKanjiInfoTableItemEmpty(); container.appendChild(n); @@ -298,7 +298,7 @@ class DisplayGenerator { node.querySelector('.term-pitch-accent-group-tag-list').appendChild(tag); const n = node.querySelector('.term-pitch-accent-list'); - DisplayGenerator._appendMultiple(n, this.createPitch.bind(this), dictionaryPitches); + this._appendMultiple(n, this.createPitch.bind(this), dictionaryPitches); return node; } @@ -316,7 +316,7 @@ class DisplayGenerator { n.textContent = `${position}`; n = node.querySelector('.term-pitch-accent-tag-list'); - DisplayGenerator._appendMultiple(n, this.createTag.bind(this), tags); + this._appendMultiple(n, this.createTag.bind(this), tags); n = node.querySelector('.term-pitch-accent-disambiguation-list'); this.createPitchAccentDisambiguations(n, exclusiveExpressions, exclusiveReadings); @@ -445,7 +445,7 @@ class DisplayGenerator { } } - static _appendMultiple(container, createItem, detailsIterable, fallback=[]) { + _appendMultiple(container, createItem, detailsIterable, fallback=[]) { if (container === null) { return 0; } const multi = ( @@ -468,7 +468,7 @@ class DisplayGenerator { return count; } - static _appendFurigana(container, segments, addText) { + _appendFurigana(container, segments, addText) { for (const {text, furigana} of segments) { if (furigana) { const ruby = document.createElement('ruby'); @@ -483,7 +483,7 @@ class DisplayGenerator { } } - static _appendMultilineText(container, text) { + _appendMultilineText(container, text) { const parts = text.split('\n'); container.appendChild(document.createTextNode(parts[0])); for (let i = 1, ii = parts.length; i < ii; ++i) { @@ -492,7 +492,7 @@ class DisplayGenerator { } } - static _getPitchInfos(definition) { + _getPitchInfos(definition) { const results = new Map(); const allExpressions = new Set(); @@ -510,7 +510,7 @@ class DisplayGenerator { } for (const {position, tags} of pitches) { - let pitchInfo = DisplayGenerator._findExistingPitchInfo(reading, position, tags, dictionaryResults); + let pitchInfo = this._findExistingPitchInfo(reading, position, tags, dictionaryResults); if (pitchInfo === null) { pitchInfo = {expressions: new Set(), reading, position, tags}; dictionaryResults.push(pitchInfo); @@ -539,12 +539,12 @@ class DisplayGenerator { return [...results.entries()]; } - static _findExistingPitchInfo(reading, position, tags, pitchInfoList) { + _findExistingPitchInfo(reading, position, tags, pitchInfoList) { for (const pitchInfo of pitchInfoList) { if ( pitchInfo.reading === reading && pitchInfo.position === position && - DisplayGenerator._areTagListsEqual(pitchInfo.tags, tags) + this._areTagListsEqual(pitchInfo.tags, tags) ) { return pitchInfo; } @@ -552,7 +552,7 @@ class DisplayGenerator { return null; } - static _areTagListsEqual(tagList1, tagList2) { + _areTagListsEqual(tagList1, tagList2) { const ii = tagList1.length; if (tagList2.length !== ii) { return false; } -- cgit v1.2.3 From 9bbe35da68b9cdbc49c8f223544bdefad16d66d4 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 22:02:17 -0400 Subject: Mark functions as private --- ext/mixed/js/display-generator.js | 92 +++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 46 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index 2f1154eb..c3817ab2 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -70,11 +70,11 @@ class DisplayGenerator { let expressions = details.expressions; expressions = Array.isArray(expressions) ? expressions.map((e) => [e, termTags]) : null; - this._appendMultiple(expressionsContainer, this.createTermExpression.bind(this), expressions, [[details, termTags]]); - this._appendMultiple(reasonsContainer, this.createTermReason.bind(this), details.reasons); - this._appendMultiple(frequenciesContainer, this.createFrequencyTag.bind(this), details.frequencies); - this._appendMultiple(pitchesContainer, this.createPitches.bind(this), pitches); - this._appendMultiple(definitionsContainer, this.createTermDefinitionItem.bind(this), details.definitions, [details]); + this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressions, [[details, termTags]]); + this._appendMultiple(reasonsContainer, this._createTermReason.bind(this), details.reasons); + this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(pitchesContainer, this._createPitches.bind(this), pitches); + this._appendMultiple(definitionsContainer, this._createTermDefinitionItem.bind(this), details.definitions, [details]); if (debugInfoContainer !== null) { debugInfoContainer.textContent = JSON.stringify(details, null, 4); @@ -83,7 +83,7 @@ class DisplayGenerator { return node; } - createTermExpression([details, termTags]) { + _createTermExpression([details, termTags]) { const node = this._templateHandler.instantiate('term-expression'); const expressionContainer = node.querySelector('.term-expression-text'); @@ -110,14 +110,14 @@ class DisplayGenerator { const searchQueries = [details.expression, details.reading] .filter((x) => !!x) .map((x) => ({query: x})); - this._appendMultiple(tagContainer, this.createTag.bind(this), termTags); - this._appendMultiple(tagContainer, this.createSearchTag.bind(this), searchQueries); - this._appendMultiple(frequencyContainer, this.createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(tagContainer, this._createTag.bind(this), termTags); + this._appendMultiple(tagContainer, this._createSearchTag.bind(this), searchQueries); + this._appendMultiple(frequencyContainer, this._createFrequencyTag.bind(this), details.frequencies); return node; } - createTermReason(reason) { + _createTermReason(reason) { const fragment = this._templateHandler.instantiateFragment('term-reason'); const node = fragment.querySelector('.term-reason'); node.textContent = reason; @@ -125,7 +125,7 @@ class DisplayGenerator { return fragment; } - createTermDefinitionItem(details) { + _createTermDefinitionItem(details) { const node = this._templateHandler.instantiate('term-definition-item'); const tagListContainer = node.querySelector('.term-definition-tag-list'); @@ -134,14 +134,14 @@ class DisplayGenerator { node.dataset.dictionary = details.dictionary; - this._appendMultiple(tagListContainer, this.createTag.bind(this), details.definitionTags); - this._appendMultiple(onlyListContainer, this.createTermOnly.bind(this), details.only); - this._appendMultiple(glossaryContainer, this.createTermGlossaryItem.bind(this), details.glossary); + this._appendMultiple(tagListContainer, this._createTag.bind(this), details.definitionTags); + this._appendMultiple(onlyListContainer, this._createTermOnly.bind(this), details.only); + this._appendMultiple(glossaryContainer, this._createTermGlossaryItem.bind(this), details.glossary); return node; } - createTermGlossaryItem(glossary) { + _createTermGlossaryItem(glossary) { const node = this._templateHandler.instantiate('term-glossary-item'); const container = node.querySelector('.term-glossary'); if (container !== null) { @@ -150,14 +150,14 @@ class DisplayGenerator { return node; } - createTermOnly(only) { + _createTermOnly(only) { const node = this._templateHandler.instantiate('term-definition-only'); node.dataset.only = only; node.textContent = only; return node; } - createKanjiLink(character) { + _createKanjiLink(character) { const node = document.createElement('a'); node.href = '#'; node.className = 'kanji-link'; @@ -184,23 +184,23 @@ class DisplayGenerator { glyphContainer.textContent = details.character; } - this._appendMultiple(frequenciesContainer, this.createFrequencyTag.bind(this), details.frequencies); - this._appendMultiple(tagContainer, this.createTag.bind(this), details.tags); - this._appendMultiple(glossaryContainer, this.createKanjiGlossaryItem.bind(this), details.glossary); - this._appendMultiple(chineseReadingsContainer, this.createKanjiReading.bind(this), details.onyomi); - this._appendMultiple(japaneseReadingsContainer, this.createKanjiReading.bind(this), details.kunyomi); + this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(tagContainer, this._createTag.bind(this), details.tags); + this._appendMultiple(glossaryContainer, this._createKanjiGlossaryItem.bind(this), details.glossary); + this._appendMultiple(chineseReadingsContainer, this._createKanjiReading.bind(this), details.onyomi); + this._appendMultiple(japaneseReadingsContainer, this._createKanjiReading.bind(this), details.kunyomi); if (statisticsContainer !== null) { - statisticsContainer.appendChild(this.createKanjiInfoTable(details.stats.misc)); + statisticsContainer.appendChild(this._createKanjiInfoTable(details.stats.misc)); } if (classificationsContainer !== null) { - classificationsContainer.appendChild(this.createKanjiInfoTable(details.stats.class)); + classificationsContainer.appendChild(this._createKanjiInfoTable(details.stats.class)); } if (codepointsContainer !== null) { - codepointsContainer.appendChild(this.createKanjiInfoTable(details.stats.code)); + codepointsContainer.appendChild(this._createKanjiInfoTable(details.stats.code)); } if (dictionaryIndicesContainer !== null) { - dictionaryIndicesContainer.appendChild(this.createKanjiInfoTable(details.stats.index)); + dictionaryIndicesContainer.appendChild(this._createKanjiInfoTable(details.stats.index)); } if (debugInfoContainer !== null) { @@ -210,7 +210,7 @@ class DisplayGenerator { return node; } - createKanjiGlossaryItem(glossary) { + _createKanjiGlossaryItem(glossary) { const node = this._templateHandler.instantiate('kanji-glossary-item'); const container = node.querySelector('.kanji-glossary'); if (container !== null) { @@ -219,21 +219,21 @@ class DisplayGenerator { return node; } - createKanjiReading(reading) { + _createKanjiReading(reading) { const node = this._templateHandler.instantiate('kanji-reading'); node.textContent = reading; return node; } - createKanjiInfoTable(details) { + _createKanjiInfoTable(details) { const node = this._templateHandler.instantiate('kanji-info-table'); const container = node.querySelector('.kanji-info-table-body'); if (container !== null) { - const count = this._appendMultiple(container, this.createKanjiInfoTableItem.bind(this), details); + const count = this._appendMultiple(container, this._createKanjiInfoTableItem.bind(this), details); if (count === 0) { - const n = this.createKanjiInfoTableItemEmpty(); + const n = this._createKanjiInfoTableItemEmpty(); container.appendChild(n); } } @@ -241,7 +241,7 @@ class DisplayGenerator { return node; } - createKanjiInfoTableItem(details) { + _createKanjiInfoTableItem(details) { const node = this._templateHandler.instantiate('kanji-info-table-item'); const nameNode = node.querySelector('.kanji-info-table-item-header'); const valueNode = node.querySelector('.kanji-info-table-item-value'); @@ -254,11 +254,11 @@ class DisplayGenerator { return node; } - createKanjiInfoTableItemEmpty() { + _createKanjiInfoTableItemEmpty() { return this._templateHandler.instantiate('kanji-info-table-empty'); } - createTag(details) { + _createTag(details) { const node = this._templateHandler.instantiate('tag'); const inner = node.querySelector('.tag-inner'); @@ -270,7 +270,7 @@ class DisplayGenerator { return node; } - createSearchTag(details) { + _createSearchTag(details) { const node = this._templateHandler.instantiate('tag-search'); node.textContent = details.query; @@ -280,7 +280,7 @@ class DisplayGenerator { return node; } - createPitches(details) { + _createPitches(details) { if (!this._termPitchAccentStaticTemplateIsSetup) { this._termPitchAccentStaticTemplateIsSetup = true; const t = this._templateHandler.instantiate('term-pitch-accent-static'); @@ -294,16 +294,16 @@ class DisplayGenerator { node.dataset.pitchesMulti = 'true'; node.dataset.pitchesCount = `${dictionaryPitches.length}`; - const tag = this.createTag({notes: '', name: dictionary, category: 'pitch-accent-dictionary'}); + const tag = this._createTag({notes: '', name: dictionary, category: 'pitch-accent-dictionary'}); node.querySelector('.term-pitch-accent-group-tag-list').appendChild(tag); const n = node.querySelector('.term-pitch-accent-list'); - this._appendMultiple(n, this.createPitch.bind(this), dictionaryPitches); + this._appendMultiple(n, this._createPitch.bind(this), dictionaryPitches); return node; } - createPitch(details) { + _createPitch(details) { const {reading, position, tags, exclusiveExpressions, exclusiveReadings} = details; const morae = jp.getKanaMorae(reading); @@ -316,10 +316,10 @@ class DisplayGenerator { n.textContent = `${position}`; n = node.querySelector('.term-pitch-accent-tag-list'); - this._appendMultiple(n, this.createTag.bind(this), tags); + this._appendMultiple(n, this._createTag.bind(this), tags); n = node.querySelector('.term-pitch-accent-disambiguation-list'); - this.createPitchAccentDisambiguations(n, exclusiveExpressions, exclusiveReadings); + this._createPitchAccentDisambiguations(n, exclusiveExpressions, exclusiveReadings); n = node.querySelector('.term-pitch-accent-characters'); for (let i = 0, ii = morae.length; i < ii; ++i) { @@ -339,13 +339,13 @@ class DisplayGenerator { } if (morae.length > 0) { - this.populatePitchGraph(node.querySelector('.term-pitch-accent-graph'), position, morae); + this._populatePitchGraph(node.querySelector('.term-pitch-accent-graph'), position, morae); } return node; } - createPitchAccentDisambiguations(container, exclusiveExpressions, exclusiveReadings) { + _createPitchAccentDisambiguations(container, exclusiveExpressions, exclusiveReadings) { const templateName = 'term-pitch-accent-disambiguation'; for (const exclusiveExpression of exclusiveExpressions) { const node = this._templateHandler.instantiate(templateName); @@ -366,7 +366,7 @@ class DisplayGenerator { container.dataset.readingCount = `${exclusiveReadings.length}`; } - populatePitchGraph(svg, position, morae) { + _populatePitchGraph(svg, position, morae) { const svgns = svg.getAttribute('xmlns'); const ii = morae.length; svg.setAttribute('viewBox', `0 0 ${50 * (ii + 1)} 100`); @@ -406,7 +406,7 @@ class DisplayGenerator { path.setAttribute('d', `M${pathPoints.join(' L')}`); } - createFrequencyTag(details) { + _createFrequencyTag(details) { const node = this._templateHandler.instantiate('tag-frequency'); let n = node.querySelector('.term-frequency-dictionary-name'); @@ -434,7 +434,7 @@ class DisplayGenerator { part = ''; } - const link = this.createKanjiLink(c); + const link = this._createKanjiLink(c); container.appendChild(link); } else { part += c; -- cgit v1.2.3 From f9bdf2c66f5eca0ecb1c21f231492be47e40e690 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 22:03:16 -0400 Subject: Organize --- ext/mixed/js/display-generator.js | 92 ++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 45 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index c3817ab2..951f16ec 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -83,6 +83,53 @@ class DisplayGenerator { return node; } + createKanjiEntry(details) { + const node = this._templateHandler.instantiate('kanji-entry'); + + const glyphContainer = node.querySelector('.kanji-glyph'); + const frequenciesContainer = node.querySelector('.frequencies'); + const tagContainer = node.querySelector('.tags'); + const glossaryContainer = node.querySelector('.kanji-glossary-list'); + const chineseReadingsContainer = node.querySelector('.kanji-readings-chinese'); + const japaneseReadingsContainer = node.querySelector('.kanji-readings-japanese'); + const statisticsContainer = node.querySelector('.kanji-statistics'); + const classificationsContainer = node.querySelector('.kanji-classifications'); + const codepointsContainer = node.querySelector('.kanji-codepoints'); + const dictionaryIndicesContainer = node.querySelector('.kanji-dictionary-indices'); + const debugInfoContainer = node.querySelector('.debug-info'); + + if (glyphContainer !== null) { + glyphContainer.textContent = details.character; + } + + this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); + this._appendMultiple(tagContainer, this._createTag.bind(this), details.tags); + this._appendMultiple(glossaryContainer, this._createKanjiGlossaryItem.bind(this), details.glossary); + this._appendMultiple(chineseReadingsContainer, this._createKanjiReading.bind(this), details.onyomi); + this._appendMultiple(japaneseReadingsContainer, this._createKanjiReading.bind(this), details.kunyomi); + + if (statisticsContainer !== null) { + statisticsContainer.appendChild(this._createKanjiInfoTable(details.stats.misc)); + } + if (classificationsContainer !== null) { + classificationsContainer.appendChild(this._createKanjiInfoTable(details.stats.class)); + } + if (codepointsContainer !== null) { + codepointsContainer.appendChild(this._createKanjiInfoTable(details.stats.code)); + } + if (dictionaryIndicesContainer !== null) { + dictionaryIndicesContainer.appendChild(this._createKanjiInfoTable(details.stats.index)); + } + + if (debugInfoContainer !== null) { + debugInfoContainer.textContent = JSON.stringify(details, null, 4); + } + + return node; + } + + // Private + _createTermExpression([details, termTags]) { const node = this._templateHandler.instantiate('term-expression'); @@ -165,51 +212,6 @@ class DisplayGenerator { return node; } - createKanjiEntry(details) { - const node = this._templateHandler.instantiate('kanji-entry'); - - const glyphContainer = node.querySelector('.kanji-glyph'); - const frequenciesContainer = node.querySelector('.frequencies'); - const tagContainer = node.querySelector('.tags'); - const glossaryContainer = node.querySelector('.kanji-glossary-list'); - const chineseReadingsContainer = node.querySelector('.kanji-readings-chinese'); - const japaneseReadingsContainer = node.querySelector('.kanji-readings-japanese'); - const statisticsContainer = node.querySelector('.kanji-statistics'); - const classificationsContainer = node.querySelector('.kanji-classifications'); - const codepointsContainer = node.querySelector('.kanji-codepoints'); - const dictionaryIndicesContainer = node.querySelector('.kanji-dictionary-indices'); - const debugInfoContainer = node.querySelector('.debug-info'); - - if (glyphContainer !== null) { - glyphContainer.textContent = details.character; - } - - this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); - this._appendMultiple(tagContainer, this._createTag.bind(this), details.tags); - this._appendMultiple(glossaryContainer, this._createKanjiGlossaryItem.bind(this), details.glossary); - this._appendMultiple(chineseReadingsContainer, this._createKanjiReading.bind(this), details.onyomi); - this._appendMultiple(japaneseReadingsContainer, this._createKanjiReading.bind(this), details.kunyomi); - - if (statisticsContainer !== null) { - statisticsContainer.appendChild(this._createKanjiInfoTable(details.stats.misc)); - } - if (classificationsContainer !== null) { - classificationsContainer.appendChild(this._createKanjiInfoTable(details.stats.class)); - } - if (codepointsContainer !== null) { - codepointsContainer.appendChild(this._createKanjiInfoTable(details.stats.code)); - } - if (dictionaryIndicesContainer !== null) { - dictionaryIndicesContainer.appendChild(this._createKanjiInfoTable(details.stats.index)); - } - - if (debugInfoContainer !== null) { - debugInfoContainer.textContent = JSON.stringify(details, null, 4); - } - - return node; - } - _createKanjiGlossaryItem(glossary) { const node = this._templateHandler.instantiate('kanji-glossary-item'); const container = node.querySelector('.kanji-glossary'); -- cgit v1.2.3 From 6788bb31d241e1b77a6630df3d617de4a9470f26 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 22:16:55 -0400 Subject: Update _appendMultiple to not use a fallback --- ext/mixed/js/display-generator.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index 951f16ec..5c4b330d 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -68,13 +68,13 @@ class DisplayGenerator { const termTags = details.termTags; let expressions = details.expressions; - expressions = Array.isArray(expressions) ? expressions.map((e) => [e, termTags]) : null; + expressions = Array.isArray(expressions) ? expressions.map((e) => [e, termTags]) : [[details, termTags]]; - this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressions, [[details, termTags]]); + this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressions); this._appendMultiple(reasonsContainer, this._createTermReason.bind(this), details.reasons); this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); this._appendMultiple(pitchesContainer, this._createPitches.bind(this), pitches); - this._appendMultiple(definitionsContainer, this._createTermDefinitionItem.bind(this), details.definitions, [details]); + this._appendMultiple(definitionsContainer, this._createTermDefinitionItem.bind(this), definitionMulti ? details.definitions : [details]); if (debugInfoContainer !== null) { debugInfoContainer.textContent = JSON.stringify(details, null, 4); @@ -447,22 +447,23 @@ class DisplayGenerator { } } - _appendMultiple(container, createItem, detailsIterable, fallback=[]) { - if (container === null) { return 0; } - - const multi = ( - detailsIterable !== null && - typeof detailsIterable === 'object' && - typeof detailsIterable[Symbol.iterator] !== 'undefined' + _isIterable(value) { + return ( + value !== null && + typeof value === 'object' && + typeof value[Symbol.iterator] !== 'undefined' ); - if (!multi) { detailsIterable = fallback; } + } + _appendMultiple(container, createItem, detailsIterable) { let count = 0; - for (const details of detailsIterable) { - const item = createItem(details); - if (item === null) { continue; } - container.appendChild(item); - ++count; + if (container !== null && this._isIterable(detailsIterable)) { + for (const details of detailsIterable) { + const item = createItem(details); + if (item === null) { continue; } + container.appendChild(item); + ++count; + } } container.dataset.count = `${count}`; -- cgit v1.2.3 From bb37b74288cf0653f42bbb149e3c563adcf2ead5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 22:19:58 -0400 Subject: Rename "only" to "disambiguation" to match naming convention used for pitch accents --- ext/mixed/css/display.css | 10 +++++----- ext/mixed/display-templates.html | 4 ++-- ext/mixed/js/display-generator.js | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'ext') diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index ac17e2ec..e10c9379 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -365,19 +365,19 @@ button.action-button { list-style-type: circle; } -.term-definition-only-list[data-count="0"] { +.term-definition-disambiguation-list[data-count="0"] { display: none; } -.term-definition-only-list:before { +.term-definition-disambiguation-list:before { content: "("; } -.term-definition-only-list:after { +.term-definition-disambiguation-list:after { content: " only)"; } -.term-definition-only+.term-definition-only:before { +.term-definition-disambiguation+.term-definition-disambiguation:before { content: ", "; } @@ -399,7 +399,7 @@ button.action-button { } :root[data-compact-glossaries=true] .term-definition-tag-list, -:root[data-compact-glossaries=true] .term-definition-only-list:not([data-count="0"]) { +:root[data-compact-glossaries=true] .term-definition-disambiguation-list:not([data-count="0"]) { display: inline; } diff --git a/ext/mixed/display-templates.html b/ext/mixed/display-templates.html index b8d52d15..3baa8293 100644 --- a/ext/mixed/display-templates.html +++ b/ext/mixed/display-templates.html @@ -30,10 +30,10 @@ - + diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index 5c4b330d..6ae727e0 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -176,13 +176,13 @@ class DisplayGenerator { const node = this._templateHandler.instantiate('term-definition-item'); const tagListContainer = node.querySelector('.term-definition-tag-list'); - const onlyListContainer = node.querySelector('.term-definition-only-list'); + const onlyListContainer = node.querySelector('.term-definition-disambiguation-list'); const glossaryContainer = node.querySelector('.term-glossary-list'); node.dataset.dictionary = details.dictionary; this._appendMultiple(tagListContainer, this._createTag.bind(this), details.definitionTags); - this._appendMultiple(onlyListContainer, this._createTermOnly.bind(this), details.only); + this._appendMultiple(onlyListContainer, this._createTermDisambiguation.bind(this), details.only); this._appendMultiple(glossaryContainer, this._createTermGlossaryItem.bind(this), details.glossary); return node; @@ -197,10 +197,10 @@ class DisplayGenerator { return node; } - _createTermOnly(only) { - const node = this._templateHandler.instantiate('term-definition-only'); - node.dataset.only = only; - node.textContent = only; + _createTermDisambiguation(disambiguation) { + const node = this._templateHandler.instantiate('term-definition-disambiguation'); + node.dataset.term = disambiguation; + node.textContent = disambiguation; return node; } -- cgit v1.2.3 From 06183ec12442fc127a7f723225bcf1782eb15ebc Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 22:35:26 -0400 Subject: Update how termTags is passed to _createTermExpression --- ext/mixed/js/display-generator.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index 6ae727e0..b2d34cfc 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -68,9 +68,9 @@ class DisplayGenerator { const termTags = details.termTags; let expressions = details.expressions; - expressions = Array.isArray(expressions) ? expressions.map((e) => [e, termTags]) : [[details, termTags]]; + expressions = Array.isArray(expressions) ? expressions : [details]; - this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressions); + this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressions, termTags); this._appendMultiple(reasonsContainer, this._createTermReason.bind(this), details.reasons); this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); this._appendMultiple(pitchesContainer, this._createPitches.bind(this), pitches); @@ -130,7 +130,7 @@ class DisplayGenerator { // Private - _createTermExpression([details, termTags]) { + _createTermExpression(details, termTags) { const node = this._templateHandler.instantiate('term-expression'); const expressionContainer = node.querySelector('.term-expression-text'); @@ -455,11 +455,11 @@ class DisplayGenerator { ); } - _appendMultiple(container, createItem, detailsIterable) { + _appendMultiple(container, createItem, detailsIterable, ...args) { let count = 0; if (container !== null && this._isIterable(detailsIterable)) { for (const details of detailsIterable) { - const item = createItem(details); + const item = createItem(details, ...args); if (item === null) { continue; } container.appendChild(item); ++count; -- cgit v1.2.3 From 025b1f398b7e498a0a61ebee095498caccb46819 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 9 Apr 2020 22:38:08 -0400 Subject: Simplify access to details fields in createTermEntry --- ext/mixed/js/display-generator.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'ext') diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js index b2d34cfc..19f6a56e 100644 --- a/ext/mixed/js/display-generator.js +++ b/ext/mixed/js/display-generator.js @@ -44,13 +44,15 @@ class DisplayGenerator { const debugInfoContainer = node.querySelector('.debug-info'); const bodyContainer = node.querySelector('.term-entry-body'); + const {termTags, expressions, definitions} = details; + const pitches = this._getPitchInfos(details); const pitchCount = pitches.reduce((i, v) => i + v[1].length, 0); - const expressionMulti = Array.isArray(details.expressions); - const definitionMulti = Array.isArray(details.definitions); - const expressionCount = expressionMulti ? details.expressions.length : 1; - const definitionCount = definitionMulti ? details.definitions.length : 1; + const expressionMulti = Array.isArray(expressions); + const definitionMulti = Array.isArray(definitions); + const expressionCount = expressionMulti ? expressions.length : 1; + const definitionCount = definitionMulti ? definitions.length : 1; const uniqueExpressionCount = Array.isArray(details.expression) ? new Set(details.expression).size : 1; node.dataset.expressionMulti = `${expressionMulti}`; @@ -66,15 +68,11 @@ class DisplayGenerator { (pitches.length > 0 ? 1 : 0) }`; - const termTags = details.termTags; - let expressions = details.expressions; - expressions = Array.isArray(expressions) ? expressions : [details]; - - this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressions, termTags); + this._appendMultiple(expressionsContainer, this._createTermExpression.bind(this), expressionMulti ? expressions : [details], termTags); this._appendMultiple(reasonsContainer, this._createTermReason.bind(this), details.reasons); this._appendMultiple(frequenciesContainer, this._createFrequencyTag.bind(this), details.frequencies); this._appendMultiple(pitchesContainer, this._createPitches.bind(this), pitches); - this._appendMultiple(definitionsContainer, this._createTermDefinitionItem.bind(this), definitionMulti ? details.definitions : [details]); + this._appendMultiple(definitionsContainer, this._createTermDefinitionItem.bind(this), definitionMulti ? definitions : [details]); if (debugInfoContainer !== null) { debugInfoContainer.textContent = JSON.stringify(details, null, 4); -- cgit v1.2.3 From 42a2917bf7aa3ab424ada2fc3acf224b74020a7f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 10 Apr 2020 11:56:18 -0400 Subject: Add support for collapsing emphatic character sequences --- ext/bg/data/options-schema.json | 8 +++++++- ext/bg/js/japanese.js | 38 +++++++++++++++++++++++++++++++++++++- ext/bg/js/options.js | 3 ++- ext/bg/js/settings/main.js | 2 ++ ext/bg/js/translator.js | 17 +++++++++++++++-- ext/bg/settings.html | 11 ++++++++++- 6 files changed, 73 insertions(+), 6 deletions(-) (limited to 'ext') diff --git a/ext/bg/data/options-schema.json b/ext/bg/data/options-schema.json index da1f1ce0..4f9e694d 100644 --- a/ext/bg/data/options-schema.json +++ b/ext/bg/data/options-schema.json @@ -388,7 +388,8 @@ "convertNumericCharacters", "convertAlphabeticCharacters", "convertHiraganaToKatakana", - "convertKatakanaToHiragana" + "convertKatakanaToHiragana", + "collapseEmphaticSequences" ], "properties": { "convertHalfWidthCharacters": { @@ -415,6 +416,11 @@ "type": "string", "enum": ["false", "true", "variant"], "default": "variant" + }, + "collapseEmphaticSequences": { + "type": "string", + "enum": ["false", "true", "full"], + "default": "false" } } }, diff --git a/ext/bg/js/japanese.js b/ext/bg/js/japanese.js index 2a2b39fd..e8b258cb 100644 --- a/ext/bg/js/japanese.js +++ b/ext/bg/js/japanese.js @@ -83,6 +83,8 @@ const ITERATION_MARK_CODE_POINT = 0x3005; + const HIRAGANA_SMALL_TSU_CODE_POINT = 0x3063; + const KATAKANA_SMALL_TSU_CODE_POINT = 0x30c3; // Existing functions @@ -373,6 +375,39 @@ } + // Miscellaneous + + function collapseEmphaticSequences(sourceText, fullCollapse, sourceMap=null) { + let result = ''; + let collapseCodePoint = -1; + const hasSourceMap = (sourceMap !== null); + for (const char of sourceText) { + const c = char.codePointAt(0); + if (c === HIRAGANA_SMALL_TSU_CODE_POINT || c === KATAKANA_SMALL_TSU_CODE_POINT) { + if (collapseCodePoint !== c) { + collapseCodePoint = c; + if (!fullCollapse) { + result += char; + continue; + } + } + } else { + collapseCodePoint = -1; + result += char; + continue; + } + + if (hasSourceMap) { + const index = result.length; + if (index > 0) { + sourceMap.combine(index - 1, 1); + } + } + } + return result; + } + + // Exports Object.assign(jp, { @@ -384,6 +419,7 @@ convertHalfWidthKanaToFullWidth, convertAlphabeticToKana, distributeFurigana, - distributeFuriganaInflected + distributeFuriganaInflected, + collapseEmphaticSequences }); })(); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index abb054d4..fa96c96c 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -171,7 +171,8 @@ function profileOptionsCreateDefaults() { convertNumericCharacters: 'false', convertAlphabeticCharacters: 'false', convertHiraganaToKatakana: 'false', - convertKatakanaToHiragana: 'variant' + convertKatakanaToHiragana: 'variant', + collapseEmphaticSequences: 'false' }, dictionaries: {}, diff --git a/ext/bg/js/settings/main.js b/ext/bg/js/settings/main.js index 1653ee35..18c2da73 100644 --- a/ext/bg/js/settings/main.js +++ b/ext/bg/js/settings/main.js @@ -119,6 +119,7 @@ async function formRead(options) { options.translation.convertAlphabeticCharacters = $('#translation-convert-alphabetic-characters').val(); options.translation.convertHiraganaToKatakana = $('#translation-convert-hiragana-to-katakana').val(); options.translation.convertKatakanaToHiragana = $('#translation-convert-katakana-to-hiragana').val(); + options.translation.collapseEmphaticSequences = $('#translation-collapse-emphatic-sequences').val(); options.parsing.enableScanningParser = $('#parsing-scan-enable').prop('checked'); options.parsing.enableMecabParser = $('#parsing-mecab-enable').prop('checked'); @@ -200,6 +201,7 @@ async function formWrite(options) { $('#translation-convert-alphabetic-characters').val(options.translation.convertAlphabeticCharacters); $('#translation-convert-hiragana-to-katakana').val(options.translation.convertHiraganaToKatakana); $('#translation-convert-katakana-to-hiragana').val(options.translation.convertKatakanaToHiragana); + $('#translation-collapse-emphatic-sequences').val(options.translation.collapseEmphaticSequences); $('#parsing-scan-enable').prop('checked', options.parsing.enableScanningParser); $('#parsing-mecab-enable').prop('checked', options.parsing.enableMecabParser); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 27f91c05..402ac6bd 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -348,17 +348,27 @@ class Translator { getAllDeinflections(text, options) { const translationOptions = options.translation; + const collapseEmphaticOptions = [[false, false]]; + switch (translationOptions.collapseEmphaticSequences) { + case 'true': + collapseEmphaticOptions.push([true, false]); + break; + case 'full': + collapseEmphaticOptions.push([true, true]); + break; + } const textOptionVariantArray = [ Translator.getTextOptionEntryVariants(translationOptions.convertHalfWidthCharacters), Translator.getTextOptionEntryVariants(translationOptions.convertNumericCharacters), Translator.getTextOptionEntryVariants(translationOptions.convertAlphabeticCharacters), Translator.getTextOptionEntryVariants(translationOptions.convertHiraganaToKatakana), - Translator.getTextOptionEntryVariants(translationOptions.convertKatakanaToHiragana) + Translator.getTextOptionEntryVariants(translationOptions.convertKatakanaToHiragana), + collapseEmphaticOptions ]; const deinflections = []; const used = new Set(); - for (const [halfWidth, numeric, alphabetic, katakana, hiragana] of Translator.getArrayVariants(textOptionVariantArray)) { + for (const [halfWidth, numeric, alphabetic, katakana, hiragana, [collapseEmphatic, collapseEmphaticFull]] of Translator.getArrayVariants(textOptionVariantArray)) { let text2 = text; const sourceMap = new TextSourceMap(text2); if (halfWidth) { @@ -376,6 +386,9 @@ class Translator { if (hiragana) { text2 = jp.convertKatakanaToHiragana(text2); } + if (collapseEmphatic) { + text2 = jp.collapseEmphaticSequences(text2, collapseEmphaticFull, sourceMap); + } for (let i = text2.length; i > 0; --i) { const text2Substring = text2.substring(0, i); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 1297a9cc..91051f3e 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -427,7 +427,7 @@

The conversion options below are listed in the order that the conversions are applied to the input text. - Each conversion has three possible values: + Conversions commonly have three possible values:

    @@ -490,6 +490,15 @@ + +
    + + +