Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
add profile switching between matching profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
siikamiika committed Mar 5, 2020
1 parent f41326a commit c7d0df4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
12 changes: 8 additions & 4 deletions ext/bg/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,21 @@ class DisplaySearch extends Display {
}
}

async updateOptions() {
await super.updateOptions();
this.queryParser.setOptions(this.profileSwitcher);

onProfileChanged(profileIndex) {
super.onProfileChanged(profileIndex);
const query = this.query.value;
if (query) {
this.setQuery(query);
this.onSearchQueryUpdated(query, false);
}
}

async updateOptions() {
await super.updateOptions();
this.queryParser.setOptions(this.profileSwitcher);
this.onProfileChanged(0);
}

isWanakanaEnabled() {
return this.wanakanaEnable !== null && this.wanakanaEnable.checked;
}
Expand Down
2 changes: 2 additions & 0 deletions ext/bg/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ <h1>Yomichan Search</h1>
<button class="action-button action-next"><img src="/mixed/img/source-term.svg" class="icon-image" title="Next term (Alt + F)" alt></button>
</div></div><div class="navigation-header-spacer"></div>

<div id="profile-select"></div>

<div id="content"></div>

<div id="no-results" hidden>
Expand Down
2 changes: 2 additions & 0 deletions ext/fg/float.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<button class="action-button action-next"><img src="/mixed/img/source-term.svg" class="icon-image" title="Next term (Alt + F)" alt></button>
</div></div><div class="navigation-header-spacer"></div>

<div id="profile-select"></div>

<div id="definitions"></div>

<div id="no-results" hidden>
Expand Down
9 changes: 9 additions & 0 deletions ext/fg/js/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ class DisplayFloat extends Display {
}
}

onProfileChanged(profileIndex) {
super.onProfileChanged(profileIndex);
apiForward('profileChanged', {profileIndex, targetPopupId: this._popupId});
}

async getMessageToken() {
// this._messageTokenPromise is used to ensure that only one call to apiGetMessageToken is made.
if (this._messageTokenPromise === null) {
Expand All @@ -142,6 +147,10 @@ class DisplayFloat extends Display {
handler(params);
}

async updateOptions() {
await super.updateOptions();
}

autoPlayAudio() {
this.clearAutoPlayTimer();
this.autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400);
Expand Down
10 changes: 8 additions & 2 deletions ext/fg/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class Frontend extends TextScanner {
]);

this._runtimeMessageHandlers = new Map([
['popupSetVisibleOverride', ({visible}) => { this.popup.setVisibleOverride(visible); }]
['popupSetVisibleOverride', ({visible}) => { this.popup.setVisibleOverride(visible); }],
['profileChanged', ({profileIndex}) => { this.onProfileChanged(profileIndex); }]
]);
}

Expand Down Expand Up @@ -127,6 +128,11 @@ class Frontend extends TextScanner {
this.optionsContext.url = this.popup.url;
const profileSwitcher = new ProfileSwitcher(await apiProfilesGetMatching(this.optionsContext));
this.setOptions(profileSwitcher);
await this.onProfileChanged(0);
}

async onProfileChanged(profileIndex) {
this.profileSwitcher.setIndex(profileIndex);
await this.popup.setOptions(this.profileSwitcher.options);
this._updateContentScale();
if (this.textSourceCurrent !== null && this.causeCurrent !== null) {
Expand Down Expand Up @@ -213,7 +219,7 @@ class Frontend extends TextScanner {

getOptionsContext() {
return {
id: this.profileSwitcher.globalProfileIndex
index: this.profileSwitcher.globalProfileIndex
};
}

Expand Down
17 changes: 17 additions & 0 deletions ext/mixed/js/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ class DisplayGenerator {
return node;
}

createProfileSelect(profiles, currentProfileIndex) {
const select = document.createElement('select');

let profileIndex = 0;
for (const profile of profiles) {
const option = document.createElement('option');
option.textContent = profile.name;
option.value = profileIndex;
select.appendChild(option);
++profileIndex;
}

select.selectedIndex = currentProfileIndex;

return select;
}

_appendKanjiLinks(container, text) {
let part = '';
for (const c of text) {
Expand Down
38 changes: 29 additions & 9 deletions ext/mixed/js/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ class Display {

async prepare() {
await yomichan.prepare();
const displayGeneratorPromise = this.displayGenerator.prepare();
const updateOptionsPromise = this.updateOptions();
await Promise.all([displayGeneratorPromise, updateOptionsPromise]);
await this.displayGenerator.prepare();
await this.updateOptions();
yomichan.on('optionsUpdated', () => this.updateOptions());
}

Expand Down Expand Up @@ -356,21 +355,33 @@ class Display {
}
}

getOptionsContext() {
return {
id: this.profileSwitcher.globalProfileIndex
};
onProfileSelect(e) {
const select = e.target;
const profileIndex = select.value;
this.onProfileChanged(profileIndex);
}

async updateOptions() {
this.profileSwitcher = new ProfileSwitcher(await apiProfilesGetMatching(this.optionsContext));
onProfileChanged(profileIndex) {
this.profileSwitcher.setIndex(profileIndex);
const options = this.profileSwitcher.options;
this.updateDocumentOptions(options);
this.updateTheme(options.general.popupTheme);
this.setCustomCss(options.general.customPopupCss);
audioPrepareTextToSpeech(options);
}

getOptionsContext() {
return {
index: this.profileSwitcher.globalProfileIndex
};
}

async updateOptions() {
this.profileSwitcher = new ProfileSwitcher(await apiProfilesGetMatching(this.optionsContext));
this.onProfileChanged(0);
this.renderProfileSelect(this.profileSwitcher.getProfiles());
}

updateDocumentOptions(options) {
const data = document.documentElement.dataset;
data.ankiEnabled = `${options.anki.enable}`;
Expand Down Expand Up @@ -404,6 +415,15 @@ class Display {
}
}

renderProfileSelect(profiles) {
const profileSelectContainer = document.querySelector('#profile-select');
profileSelectContainer.textContent = '';
if (profiles.length <= 1) { return; }
const profileSelect = this.displayGenerator.createProfileSelect(profiles, this.profileIndex);
profileSelect.addEventListener('change', this.onProfileSelect.bind(this));
profileSelectContainer.appendChild(profileSelect);
}

setInteractive(interactive) {
interactive = !!interactive;
if (this.interactive === interactive) { return; }
Expand Down

0 comments on commit c7d0df4

Please sign in to comment.