Update *show* APIs to unified showContent and setContent

This commit is contained in:
toasted-nutbread 2019-10-16 21:36:10 -04:00
parent 779b4af590
commit 598cd32946
8 changed files with 58 additions and 85 deletions

View File

@ -101,7 +101,7 @@ class DisplaySearch extends Display {
this.updateSearchButton();
if (valid) {
const {definitions} = await apiTermsFind(query, this.optionsContext);
this.termsShow(definitions, {
this.setContentTerms(definitions, {
focus: false,
sentence: null,
url: window.location.href

View File

@ -100,8 +100,7 @@ class SettingsPopupPreview {
const elementRect = textSource.getRect();
const writingMode = textSource.getWritingMode();
const options = this.frontend.options;
this.frontend.popup.show(elementRect, writingMode, options);
this.frontend.popup.showContent(elementRect, writingMode);
}
onMessage(e) {

View File

@ -32,25 +32,12 @@ class DisplayFloat extends Display {
onError(error) {
if (window.yomichan_orphaned) {
this.onOrphaned();
this.setContentOrphaned();
} else {
logError(error, true);
}
}
onOrphaned() {
const definitions = document.querySelector('#definitions');
const errorOrphaned = document.querySelector('#error-orphaned');
if (definitions !== null) {
definitions.style.setProperty('display', 'none', 'important');
}
if (errorOrphaned !== null) {
errorOrphaned.style.setProperty('display', 'block', 'important');
}
}
onSearchClear() {
window.parent.postMessage('popupClose', '*');
}
@ -121,10 +108,8 @@ DisplayFloat.onKeyDownHandlers = {
};
DisplayFloat.messageHandlers = {
termsShow: (self, {definitions, context}) => self.termsShow(definitions, context),
kanjiShow: (self, {definitions, context}) => self.kanjiShow(definitions, context),
setContent: (self, {type, details}) => self.setContent(type, details),
clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(),
orphaned: (self) => self.onOrphaned(),
setCustomCss: (self, {css}) => self.setCustomCss(css),
initialize: (self, {options, popupInfo, url, childrenSupported}) => self.initialize(options, popupInfo, url, childrenSupported)
};

View File

@ -332,9 +332,10 @@ class Frontend {
} catch (e) {
if (window.yomichan_orphaned) {
if (textSource && this.options.scanning.modifier !== 'none') {
this.lastShowPromise = this.popup.showOrphaned(
this.lastShowPromise = this.popup.showContent(
textSource.getRect(),
textSource.getWritingMode()
textSource.getWritingMode(),
'orphaned'
);
}
} else {
@ -370,11 +371,11 @@ class Frontend {
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const url = window.location.href;
this.lastShowPromise = this.popup.termsShow(
this.lastShowPromise = this.popup.showContent(
textSource.getRect(),
textSource.getWritingMode(),
definitions,
{sentence, url, focus}
'terms',
{definitions, context: {sentence, url, focus}}
);
this.textSourceLast = textSource;
@ -400,11 +401,11 @@ class Frontend {
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const url = window.location.href;
this.lastShowPromise = this.popup.kanjiShow(
this.lastShowPromise = this.popup.showContent(
textSource.getRect(),
textSource.getWritingMode(),
definitions,
{sentence, url, focus}
'kanji',
{definitions, context: {sentence, url, focus}}
);
this.textSourceLast = textSource;

View File

@ -39,12 +39,10 @@ class PopupProxyHost {
this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, {
createNestedPopup: ({parentId}) => this.createNestedPopup(parentId),
setOptions: ({id, options}) => this.setOptions(id, options),
showOrphaned: ({id, elementRect}) => this.showOrphaned(id, elementRect),
hide: ({id, changeFocus}) => this.hide(id, changeFocus),
setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible),
containsPoint: ({id, x, y}) => this.containsPoint(id, x, y),
termsShow: ({id, elementRect, writingMode, definitions, context}) => this.termsShow(id, elementRect, writingMode, definitions, context),
kanjiShow: ({id, elementRect, writingMode, definitions, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, context),
showContent: ({id, elementRect, writingMode, type, details}) => this.showContent(id, elementRect, writingMode, type, details),
setCustomCss: ({id, css}) => this.setCustomCss(id, css),
clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id)
});
@ -94,12 +92,6 @@ class PopupProxyHost {
return await popup.setOptions(options);
}
async showOrphaned(id, elementRect) {
const popup = this.getPopup(id);
elementRect = this.jsonRectToDOMRect(popup, elementRect);
return await popup.showOrphaned(elementRect);
}
async hide(id, changeFocus) {
const popup = this.getPopup(id);
return popup.hide(changeFocus);
@ -115,18 +107,11 @@ class PopupProxyHost {
return await popup.containsPoint(x, y);
}
async termsShow(id, elementRect, writingMode, definitions, context) {
async showContent(id, elementRect, writingMode, type, details) {
const popup = this.getPopup(id);
elementRect = this.jsonRectToDOMRect(popup, elementRect);
if (!PopupProxyHost.popupCanShow(popup)) { return false; }
return await popup.termsShow(elementRect, writingMode, definitions, context);
}
async kanjiShow(id, elementRect, writingMode, definitions, context) {
const popup = this.getPopup(id);
elementRect = this.jsonRectToDOMRect(popup, elementRect);
if (!PopupProxyHost.popupCanShow(popup)) { return false; }
return await popup.kanjiShow(elementRect, writingMode, definitions, context);
if (!PopupProxyHost.popupCanShow(popup)) { return Promise.resolve(false); }
return await popup.showContent(elementRect, writingMode, type, details);
}
async setCustomCss(id, css) {

View File

@ -51,12 +51,6 @@ class PopupProxy {
return await this.invokeHostApi('setOptions', {id, options});
}
async showOrphaned(elementRect) {
const id = await this.getPopupId();
elementRect = PopupProxy.DOMRectToJson(elementRect);
return await this.invokeHostApi('showOrphaned', {id, elementRect});
}
async hide(changeFocus) {
if (this.id === null) {
return;
@ -76,16 +70,10 @@ class PopupProxy {
return await this.invokeHostApi('containsPoint', {id: this.id, x, y});
}
async termsShow(elementRect, writingMode, definitions, context) {
async showContent(elementRect, writingMode, type=null, details=null) {
const id = await this.getPopupId();
elementRect = PopupProxy.DOMRectToJson(elementRect);
return await this.invokeHostApi('termsShow', {id, elementRect, writingMode, definitions, context});
}
async kanjiShow(elementRect, writingMode, definitions, context) {
const id = await this.getPopupId();
elementRect = PopupProxy.DOMRectToJson(elementRect);
return await this.invokeHostApi('kanjiShow', {id, elementRect, writingMode, definitions, context});
return await this.invokeHostApi('showContent', {id, elementRect, writingMode, type, details});
}
async setCustomCss(css) {

View File

@ -90,6 +90,13 @@ class Popup {
this.updateTheme();
}
async showContent(elementRect, writingMode, type=null, details=null) {
if (!this.isInitialized()) { return; }
await this.show(elementRect, writingMode);
if (type === null) { return; }
this.invokeApi('setContent', {type, details});
}
async show(elementRect, writingMode) {
await this.inject();
@ -218,12 +225,6 @@ class Popup {
return [position, size, after];
}
async showOrphaned(elementRect, writingMode) {
if (!this.isInitialized()) { return; }
await this.show(elementRect, writingMode);
this.invokeApi('orphaned');
}
hide(changeFocus) {
if (!this.isVisible()) {
return;
@ -320,18 +321,6 @@ class Popup {
return false;
}
async termsShow(elementRect, writingMode, definitions, context) {
if (!this.isInitialized()) { return; }
await this.show(elementRect, writingMode);
this.invokeApi('termsShow', {definitions, context});
}
async kanjiShow(elementRect, writingMode, definitions, context) {
if (!this.isInitialized()) { return; }
await this.show(elementRect, writingMode);
this.invokeApi('kanjiShow', {definitions, context});
}
async setCustomCss(css) {
this.invokeApi('setCustomCss', {css});
}

View File

@ -74,8 +74,8 @@ class Display {
context.source.source = this.context.source;
}
const kanjiDefs = await apiKanjiFind(link.textContent, this.getOptionsContext());
this.kanjiShow(kanjiDefs, context);
const definitions = await apiKanjiFind(link.textContent, this.getOptionsContext());
this.setContentKanji(definitions, context);
} catch (e) {
this.onError(e);
}
@ -122,7 +122,7 @@ class Display {
context.source.source = this.context.source;
}
this.termsShow(definitions, context);
this.setContentTerms(definitions, context);
} catch (e) {
this.onError(e);
}
@ -263,7 +263,20 @@ class Display {
});
}
async termsShow(definitions, context) {
setContent(type, details) {
switch (type) {
case 'terms':
return this.setContentTerms(details.definitions, details.context);
case 'kanji':
return this.setContentKanji(details.definitions, details.context);
case 'orphaned':
return this.setContentOrphaned();
default:
return Promise.resolve();
}
}
async setContentTerms(definitions, context) {
if (!this.isInitialized()) { return; }
try {
@ -317,7 +330,7 @@ class Display {
}
}
async kanjiShow(definitions, context) {
async setContentKanji(definitions, context) {
if (!this.isInitialized()) { return; }
try {
@ -363,6 +376,19 @@ class Display {
}
}
async setContentOrphaned() {
const definitions = document.querySelector('#definitions');
const errorOrphaned = document.querySelector('#error-orphaned');
if (definitions !== null) {
definitions.style.setProperty('display', 'none', 'important');
}
if (errorOrphaned !== null) {
errorOrphaned.style.setProperty('display', 'block', 'important');
}
}
autoPlayAudio() {
this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0);
}
@ -441,7 +467,7 @@ class Display {
source: this.context.source.source
};
this.termsShow(this.context.source.definitions, context);
this.setContentTerms(this.context.source.definitions, context);
}
}