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

Convert popup-preview.js to a class #571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/bg/js/settings/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
* AnkiTemplatesController
* AudioController
* DictionaryController
* PopupPreviewController
* ProfileController
* SettingsBackup
* SettingsController
* StorageController
* api
* appearanceInitialize
* utilBackend
* utilBackgroundIsolate
*/
Expand Down Expand Up @@ -330,7 +330,7 @@ async function onReady() {

await settingsPopulateModifierKeys();
formSetupEventListeners();
appearanceInitialize();
new PopupPreviewController().prepare();
new AudioController().prepare();
await (new ProfileController()).prepare();
dictionaryController = new DictionaryController(storageController);
Expand Down
138 changes: 78 additions & 60 deletions ext/bg/js/settings/popup-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,83 @@
* wanakana
*/

function appearanceInitialize() {
let previewVisible = false;
$('#settings-popup-preview-button').on('click', () => {
if (previewVisible) { return; }
showAppearancePreview();
previewVisible = true;
});
}
class PopupPreviewController {
constructor() {
this._previewVisible = false;
this._targetOrigin = chrome.runtime.getURL('/').replace(/\/$/, '');
this._frame = null;
this._previewTextInput = null;
}

prepare() {
document.querySelector('#settings-popup-preview-button').addEventListener('click', this._onShowPopupPreviewButtonClick.bind(this), false);
}

// Private

_onShowPopupPreviewButtonClick() {
if (this._previewVisible) { return; }
this._showAppearancePreview();
this._previewVisible = true;
}

_showAppearancePreview() {
const container = document.querySelector('#settings-popup-preview-container');
const buttonContainer = document.querySelector('#settings-popup-preview-button-container');
const settings = document.querySelector('#settings-popup-preview-settings');
const text = document.querySelector('#settings-popup-preview-text');
const customCss = document.querySelector('#custom-popup-css');
const customOuterCss = document.querySelector('#custom-popup-outer-css');
const frame = document.createElement('iframe');

this._previewTextInput = text;
this._frame = frame;

wanakana.bind(text);

frame.addEventListener('load', this._onFrameLoad.bind(this), false);
text.addEventListener('input', this._onTextChange.bind(this), false);
customCss.addEventListener('input', this._onCustomCssChange.bind(this), false);
customOuterCss.addEventListener('input', this._onCustomOuterCssChange.bind(this), false);
yomichan.on('modifyingProfileChange', this._onOptionsContextChange.bind(this));

frame.src = '/bg/settings-popup-preview.html';
frame.id = 'settings-popup-preview-frame';

container.appendChild(frame);
if (buttonContainer.parentNode !== null) {
buttonContainer.parentNode.removeChild(buttonContainer);
}
settings.style.display = '';
}

_onFrameLoad() {
this._onOptionsContextChange();
this._setText(this._previewTextInput.value);
}

_onTextChange(e) {
this._setText(e.currentTarget.value);
}

_onCustomCssChange(e) {
this._invoke('setCustomCss', {css: e.currentTarget.value});
}

_onCustomOuterCssChange(e) {
this._invoke('setCustomOuterCss', {css: e.currentTarget.value});
}

_onOptionsContextChange() {
this._invoke('updateOptionsContext', {optionsContext: getOptionsContext()});
}

_setText(text) {
this._invoke('setText', {text});
}

function showAppearancePreview() {
const container = $('#settings-popup-preview-container');
const buttonContainer = $('#settings-popup-preview-button-container');
const settings = $('#settings-popup-preview-settings');
const text = $('#settings-popup-preview-text');
const customCss = $('#custom-popup-css');
const customOuterCss = $('#custom-popup-outer-css');

const frame = document.createElement('iframe');
frame.src = '/bg/settings-popup-preview.html';
frame.id = 'settings-popup-preview-frame';

wanakana.bind(text[0]);

const targetOrigin = chrome.runtime.getURL('/').replace(/\/$/, '');

text.on('input', () => {
const action = 'setText';
const params = {text: text.val()};
frame.contentWindow.postMessage({action, params}, targetOrigin);
});
customCss.on('input', () => {
const action = 'setCustomCss';
const params = {css: customCss.val()};
frame.contentWindow.postMessage({action, params}, targetOrigin);
});
customOuterCss.on('input', () => {
const action = 'setCustomOuterCss';
const params = {css: customOuterCss.val()};
frame.contentWindow.postMessage({action, params}, targetOrigin);
});

const updateOptionsContext = () => {
const action = 'updateOptionsContext';
const params = {
optionsContext: getOptionsContext()
};
frame.contentWindow.postMessage({action, params}, targetOrigin);
};
yomichan.on('modifyingProfileChange', updateOptionsContext);

frame.addEventListener('load', () => {
const action = 'prepare';
const params = {
optionsContext: getOptionsContext()
};
frame.contentWindow.postMessage({action, params}, targetOrigin);
});

container.append(frame);
buttonContainer.remove();
settings.css('display', '');
_invoke(action, params) {
if (this._frame === null || this._frame.contentWindow === null) { return; }
this._frame.contentWindow.postMessage({action, params}, this._targetOrigin);
}
}