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(); this.updateSearchButton();
if (valid) { if (valid) {
const {definitions} = await apiTermsFind(query, this.optionsContext); const {definitions} = await apiTermsFind(query, this.optionsContext);
this.termsShow(definitions, { this.setContentTerms(definitions, {
focus: false, focus: false,
sentence: null, sentence: null,
url: window.location.href url: window.location.href

View File

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

View File

@ -32,25 +32,12 @@ class DisplayFloat extends Display {
onError(error) { onError(error) {
if (window.yomichan_orphaned) { if (window.yomichan_orphaned) {
this.onOrphaned(); this.setContentOrphaned();
} else { } else {
logError(error, true); 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() { onSearchClear() {
window.parent.postMessage('popupClose', '*'); window.parent.postMessage('popupClose', '*');
} }
@ -121,10 +108,8 @@ DisplayFloat.onKeyDownHandlers = {
}; };
DisplayFloat.messageHandlers = { DisplayFloat.messageHandlers = {
termsShow: (self, {definitions, context}) => self.termsShow(definitions, context), setContent: (self, {type, details}) => self.setContent(type, details),
kanjiShow: (self, {definitions, context}) => self.kanjiShow(definitions, context),
clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(), clearAutoPlayTimer: (self) => self.clearAutoPlayTimer(),
orphaned: (self) => self.onOrphaned(),
setCustomCss: (self, {css}) => self.setCustomCss(css), setCustomCss: (self, {css}) => self.setCustomCss(css),
initialize: (self, {options, popupInfo, url, childrenSupported}) => self.initialize(options, popupInfo, url, childrenSupported) initialize: (self, {options, popupInfo, url, childrenSupported}) => self.initialize(options, popupInfo, url, childrenSupported)
}; };

View File

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

View File

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

View File

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

View File

@ -90,6 +90,13 @@ class Popup {
this.updateTheme(); 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) { async show(elementRect, writingMode) {
await this.inject(); await this.inject();
@ -218,12 +225,6 @@ class Popup {
return [position, size, after]; return [position, size, after];
} }
async showOrphaned(elementRect, writingMode) {
if (!this.isInitialized()) { return; }
await this.show(elementRect, writingMode);
this.invokeApi('orphaned');
}
hide(changeFocus) { hide(changeFocus) {
if (!this.isVisible()) { if (!this.isVisible()) {
return; return;
@ -320,18 +321,6 @@ class Popup {
return false; 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) { async setCustomCss(css) {
this.invokeApi('setCustomCss', {css}); this.invokeApi('setCustomCss', {css});
} }

View File

@ -74,8 +74,8 @@ class Display {
context.source.source = this.context.source; context.source.source = this.context.source;
} }
const kanjiDefs = await apiKanjiFind(link.textContent, this.getOptionsContext()); const definitions = await apiKanjiFind(link.textContent, this.getOptionsContext());
this.kanjiShow(kanjiDefs, context); this.setContentKanji(definitions, context);
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);
} }
@ -122,7 +122,7 @@ class Display {
context.source.source = this.context.source; context.source.source = this.context.source;
} }
this.termsShow(definitions, context); this.setContentTerms(definitions, context);
} catch (e) { } catch (e) {
this.onError(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; } if (!this.isInitialized()) { return; }
try { try {
@ -317,7 +330,7 @@ class Display {
} }
} }
async kanjiShow(definitions, context) { async setContentKanji(definitions, context) {
if (!this.isInitialized()) { return; } if (!this.isInitialized()) { return; }
try { 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() { autoPlayAudio() {
this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0); this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0);
} }
@ -441,7 +467,7 @@ class Display {
source: this.context.source.source source: this.context.source.source
}; };
this.termsShow(this.context.source.definitions, context); this.setContentTerms(this.context.source.definitions, context);
} }
} }