2019-12-01 20:41:09 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2019-2021 Yomichan Authors
|
2019-12-01 20:41:09 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-01 20:41:09 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
2020-09-09 21:46:34 +00:00
|
|
|
* AnkiConnect
|
2021-02-25 02:54:58 +00:00
|
|
|
* AnkiUtil
|
2020-10-25 17:34:42 +00:00
|
|
|
* ObjectPropertyAccessor
|
|
|
|
* SelectorObserver
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
class AnkiController {
|
2020-05-30 13:33:13 +00:00
|
|
|
constructor(settingsController) {
|
|
|
|
this._settingsController = settingsController;
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiConnect = new AnkiConnect();
|
|
|
|
this._selectorObserver = new SelectorObserver({
|
|
|
|
selector: '.anki-card',
|
|
|
|
ignoreSelector: null,
|
|
|
|
onAdded: this._createCardController.bind(this),
|
|
|
|
onRemoved: this._removeCardController.bind(this),
|
|
|
|
isStale: this._isCardControllerStale.bind(this)
|
|
|
|
});
|
2020-10-25 23:04:59 +00:00
|
|
|
this._stringComparer = new Intl.Collator(); // Locale does not matter
|
2020-10-25 17:34:42 +00:00
|
|
|
this._getAnkiDataPromise = null;
|
|
|
|
this._ankiErrorContainer = null;
|
2020-12-30 17:39:33 +00:00
|
|
|
this._ankiErrorMessageNode = null;
|
|
|
|
this._ankiErrorMessageNodeDefaultContent = '';
|
|
|
|
this._ankiErrorMessageDetailsNode = null;
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorMessageDetailsContainer = null;
|
|
|
|
this._ankiErrorMessageDetailsToggle = null;
|
|
|
|
this._ankiErrorInvalidResponseInfo = null;
|
2020-10-26 02:51:28 +00:00
|
|
|
this._ankiCardPrimary = null;
|
2020-12-18 17:05:33 +00:00
|
|
|
this._validateFieldsToken = null;
|
2020-05-30 13:33:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 22:53:07 +00:00
|
|
|
get settingsController() {
|
|
|
|
return this._settingsController;
|
|
|
|
}
|
|
|
|
|
2020-05-30 13:33:13 +00:00
|
|
|
async prepare() {
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorContainer = document.querySelector('#anki-error');
|
2020-12-30 17:39:33 +00:00
|
|
|
this._ankiErrorMessageNode = document.querySelector('#anki-error-message');
|
|
|
|
this._ankiErrorMessageNodeDefaultContent = this._ankiErrorMessageNode.textContent;
|
|
|
|
this._ankiErrorMessageDetailsNode = document.querySelector('#anki-error-message-details');
|
|
|
|
this._ankiErrorMessageDetailsContainer = document.querySelector('#anki-error-message-details-container');
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorMessageDetailsToggle = document.querySelector('#anki-error-message-details-toggle');
|
|
|
|
this._ankiErrorInvalidResponseInfo = document.querySelector('#anki-error-invalid-response-info');
|
|
|
|
this._ankiEnableCheckbox = document.querySelector('[data-setting="anki.enable"]');
|
2020-10-26 02:51:28 +00:00
|
|
|
this._ankiCardPrimary = document.querySelector('#anki-card-primary');
|
2021-02-26 23:15:04 +00:00
|
|
|
const ankiCardPrimaryTypeRadios = document.querySelectorAll('input[type=radio][name=anki-card-primary-type]');
|
2020-10-26 02:51:28 +00:00
|
|
|
|
|
|
|
this._setupFieldMenus();
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorMessageDetailsToggle.addEventListener('click', this._onAnkiErrorMessageDetailsToggleClick.bind(this), false);
|
|
|
|
if (this._ankiEnableCheckbox !== null) { this._ankiEnableCheckbox.addEventListener('settingChanged', this._onAnkiEnableChanged.bind(this), false); }
|
2021-02-26 23:15:04 +00:00
|
|
|
for (const input of ankiCardPrimaryTypeRadios) {
|
|
|
|
input.addEventListener('change', this._onAnkiCardPrimaryTypeRadioChange.bind(this), false);
|
|
|
|
}
|
2020-05-29 23:52:51 +00:00
|
|
|
|
2020-05-30 13:33:13 +00:00
|
|
|
const options = await this._settingsController.getOptions();
|
2020-10-25 17:34:42 +00:00
|
|
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
2020-05-30 13:33:13 +00:00
|
|
|
this._onOptionsChanged({options});
|
2020-01-22 00:08:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
getFieldMarkers(type) {
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
return [
|
|
|
|
'audio',
|
2020-09-08 15:01:08 +00:00
|
|
|
'clipboard-image',
|
2020-09-26 17:45:48 +00:00
|
|
|
'clipboard-text',
|
2020-05-29 23:52:51 +00:00
|
|
|
'cloze-body',
|
|
|
|
'cloze-prefix',
|
|
|
|
'cloze-suffix',
|
2020-11-05 01:39:23 +00:00
|
|
|
'conjugation',
|
2020-05-29 23:52:51 +00:00
|
|
|
'dictionary',
|
|
|
|
'document-title',
|
|
|
|
'expression',
|
2020-11-28 19:30:50 +00:00
|
|
|
'frequencies',
|
2020-05-29 23:52:51 +00:00
|
|
|
'furigana',
|
|
|
|
'furigana-plain',
|
|
|
|
'glossary',
|
|
|
|
'glossary-brief',
|
2021-01-29 02:33:30 +00:00
|
|
|
'glossary-no-dictionary',
|
2021-03-26 23:50:54 +00:00
|
|
|
'part-of-speech',
|
2020-08-01 20:23:33 +00:00
|
|
|
'pitch-accents',
|
|
|
|
'pitch-accent-graphs',
|
|
|
|
'pitch-accent-positions',
|
2020-05-29 23:52:51 +00:00
|
|
|
'reading',
|
|
|
|
'screenshot',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
case 'kanji':
|
|
|
|
return [
|
|
|
|
'character',
|
2020-09-08 15:01:08 +00:00
|
|
|
'clipboard-image',
|
2020-09-26 17:45:48 +00:00
|
|
|
'clipboard-text',
|
2020-08-01 20:23:33 +00:00
|
|
|
'cloze-body',
|
|
|
|
'cloze-prefix',
|
|
|
|
'cloze-suffix',
|
2020-05-29 23:52:51 +00:00
|
|
|
'dictionary',
|
|
|
|
'document-title',
|
|
|
|
'glossary',
|
|
|
|
'kunyomi',
|
|
|
|
'onyomi',
|
|
|
|
'screenshot',
|
|
|
|
'sentence',
|
2021-01-16 20:29:42 +00:00
|
|
|
'stroke-count',
|
2020-05-29 23:52:51 +00:00
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 00:08:56 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
getFieldMarkersHtml(markers) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const marker of markers) {
|
2020-10-25 17:34:42 +00:00
|
|
|
const markerNode = this._settingsController.instantiateTemplate('anki-card-field-marker');
|
2020-05-29 23:52:51 +00:00
|
|
|
markerNode.querySelector('.marker-link').textContent = marker;
|
|
|
|
fragment.appendChild(markerNode);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
2020-01-22 00:08:56 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
async getAnkiData() {
|
|
|
|
let promise = this._getAnkiDataPromise;
|
|
|
|
if (promise === null) {
|
|
|
|
promise = this._getAnkiData();
|
|
|
|
this._getAnkiDataPromise = promise;
|
|
|
|
promise.finally(() => { this._getAnkiDataPromise = null; });
|
|
|
|
}
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getModelFieldNames(model) {
|
|
|
|
return await this._ankiConnect.getModelFieldNames(model);
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:55:11 +00:00
|
|
|
getRequiredPermissions(fieldValue) {
|
2021-02-11 23:55:09 +00:00
|
|
|
return this._settingsController.permissionsUtil.getRequiredPermissionsForAnkiFieldValue(fieldValue);
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
// Private
|
2020-01-22 00:08:56 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
async _onOptionsChanged({options: {anki}}) {
|
|
|
|
this._ankiConnect.server = anki.server;
|
|
|
|
this._ankiConnect.enabled = anki.enable;
|
2020-09-09 21:46:34 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
this._selectorObserver.disconnect();
|
|
|
|
this._selectorObserver.observe(document.documentElement, true);
|
|
|
|
}
|
2020-05-30 13:33:13 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_onAnkiErrorMessageDetailsToggleClick() {
|
|
|
|
const node = this._ankiErrorMessageDetailsContainer;
|
|
|
|
node.hidden = !node.hidden;
|
2020-05-30 13:33:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_onAnkiEnableChanged({detail: {value}}) {
|
2021-01-06 23:16:51 +00:00
|
|
|
if (this._ankiConnect.server === null) { return; }
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiConnect.enabled = value;
|
|
|
|
|
|
|
|
for (const cardController of this._selectorObserver.datas()) {
|
|
|
|
cardController.updateAnkiState();
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2021-02-26 23:15:04 +00:00
|
|
|
_onAnkiCardPrimaryTypeRadioChange(e) {
|
|
|
|
const node = e.currentTarget;
|
|
|
|
if (!node.checked) { return; }
|
|
|
|
|
|
|
|
this._setAnkiCardPrimaryType(node.dataset.value, node.dataset.ankiCardMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setAnkiCardPrimaryType(ankiCardType, ankiCardMenu) {
|
|
|
|
if (this._ankiCardPrimary === null) { return; }
|
|
|
|
this._ankiCardPrimary.dataset.ankiCardType = ankiCardType;
|
2020-10-26 02:51:28 +00:00
|
|
|
if (typeof ankiCardMenu !== 'undefined') {
|
|
|
|
this._ankiCardPrimary.dataset.ankiCardMenu = ankiCardMenu;
|
|
|
|
} else {
|
|
|
|
delete this._ankiCardPrimary.dataset.ankiCardMenu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_createCardController(node) {
|
|
|
|
const cardController = new AnkiCardController(this._settingsController, this, node);
|
2021-01-06 23:16:51 +00:00
|
|
|
cardController.prepare();
|
2020-10-25 17:34:42 +00:00
|
|
|
return cardController;
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_removeCardController(node, cardController) {
|
|
|
|
cardController.cleanup();
|
|
|
|
}
|
2020-05-29 23:52:51 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_isCardControllerStale(node, cardController) {
|
|
|
|
return cardController.isStale();
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:51:28 +00:00
|
|
|
_setupFieldMenus() {
|
|
|
|
const fieldMenuTargets = [
|
2020-10-30 21:41:52 +00:00
|
|
|
[['terms'], '#anki-card-terms-field-menu-template'],
|
|
|
|
[['kanji'], '#anki-card-kanji-field-menu-template'],
|
|
|
|
[['terms', 'kanji'], '#anki-card-all-field-menu-template']
|
2020-10-26 02:51:28 +00:00
|
|
|
];
|
2020-10-30 21:41:52 +00:00
|
|
|
for (const [types, selector] of fieldMenuTargets) {
|
2020-10-26 02:51:28 +00:00
|
|
|
const element = document.querySelector(selector);
|
|
|
|
if (element === null) { continue; }
|
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
let markers = [];
|
|
|
|
for (const type of types) {
|
|
|
|
markers.push(...this.getFieldMarkers(type));
|
|
|
|
}
|
|
|
|
markers = [...new Set(markers)];
|
|
|
|
|
2021-01-24 02:07:45 +00:00
|
|
|
const container = element.content.querySelector('.popup-menu-body');
|
2020-10-26 02:51:28 +00:00
|
|
|
if (container === null) { return; }
|
|
|
|
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const marker of markers) {
|
|
|
|
const option = document.createElement('button');
|
|
|
|
option.textContent = marker;
|
|
|
|
option.className = 'popup-menu-item';
|
|
|
|
option.dataset.menuAction = 'setFieldMarker';
|
|
|
|
option.dataset.marker = marker;
|
|
|
|
fragment.appendChild(option);
|
|
|
|
}
|
|
|
|
container.appendChild(fragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
async _getAnkiData() {
|
2020-12-30 17:39:33 +00:00
|
|
|
this._setAnkiStatusChanging();
|
2020-10-25 17:34:42 +00:00
|
|
|
const [
|
|
|
|
[deckNames, error1],
|
|
|
|
[modelNames, error2]
|
|
|
|
] = await Promise.all([
|
|
|
|
this._getDeckNames(),
|
|
|
|
this._getModelNames()
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (error1 !== null) {
|
|
|
|
this._showAnkiError(error1);
|
|
|
|
} else if (error2 !== null) {
|
|
|
|
this._showAnkiError(error2);
|
2020-05-29 23:52:51 +00:00
|
|
|
} else {
|
2020-10-25 17:34:42 +00:00
|
|
|
this._hideAnkiError();
|
|
|
|
}
|
2020-05-29 23:52:51 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
return {deckNames, modelNames};
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getDeckNames() {
|
|
|
|
try {
|
|
|
|
const result = await this._ankiConnect.getDeckNames();
|
2020-10-25 23:04:59 +00:00
|
|
|
this._sortStringArray(result);
|
2020-10-25 17:34:42 +00:00
|
|
|
return [result, null];
|
|
|
|
} catch (e) {
|
|
|
|
return [[], e];
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
async _getModelNames() {
|
|
|
|
try {
|
|
|
|
const result = await this._ankiConnect.getModelNames();
|
2020-10-25 23:04:59 +00:00
|
|
|
this._sortStringArray(result);
|
2020-10-25 17:34:42 +00:00
|
|
|
return [result, null];
|
|
|
|
} catch (e) {
|
|
|
|
return [[], e];
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2020-12-30 17:39:33 +00:00
|
|
|
_setAnkiStatusChanging() {
|
|
|
|
this._ankiErrorMessageNode.textContent = this._ankiErrorMessageNodeDefaultContent;
|
|
|
|
this._ankiErrorMessageNode.classList.remove('danger-text');
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_hideAnkiError() {
|
2020-12-30 17:39:33 +00:00
|
|
|
if (this._ankiErrorContainer !== null) {
|
|
|
|
this._ankiErrorContainer.hidden = true;
|
|
|
|
}
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorMessageDetailsContainer.hidden = true;
|
2020-12-30 17:39:33 +00:00
|
|
|
this._ankiErrorMessageDetailsToggle.hidden = true;
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorInvalidResponseInfo.hidden = true;
|
2020-12-30 17:39:33 +00:00
|
|
|
this._ankiErrorMessageNode.textContent = (this._ankiConnect.enabled ? 'Connected' : 'Not enabled');
|
|
|
|
this._ankiErrorMessageNode.classList.remove('danger-text');
|
|
|
|
this._ankiErrorMessageDetailsNode.textContent = '';
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_showAnkiError(error) {
|
2020-10-27 01:54:18 +00:00
|
|
|
let errorString = typeof error === 'object' && error !== null ? error.message : null;
|
|
|
|
if (!errorString) { errorString = `${error}`; }
|
2020-12-30 17:39:33 +00:00
|
|
|
if (!/[.!?]$/.test(errorString)) { errorString += '.'; }
|
|
|
|
this._ankiErrorMessageNode.textContent = errorString;
|
|
|
|
this._ankiErrorMessageNode.classList.add('danger-text');
|
2019-12-02 03:21:10 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
const data = error.data;
|
|
|
|
let details = '';
|
|
|
|
if (typeof data !== 'undefined') {
|
|
|
|
details += `${JSON.stringify(data, null, 4)}\n\n`;
|
|
|
|
}
|
|
|
|
details += `${error.stack}`.trimRight();
|
2020-12-30 17:39:33 +00:00
|
|
|
this._ankiErrorMessageDetailsNode.textContent = details;
|
2019-12-03 03:17:45 +00:00
|
|
|
|
2020-12-30 17:39:33 +00:00
|
|
|
if (this._ankiErrorContainer !== null) {
|
|
|
|
this._ankiErrorContainer.hidden = false;
|
|
|
|
}
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiErrorMessageDetailsContainer.hidden = true;
|
|
|
|
this._ankiErrorInvalidResponseInfo.hidden = (errorString.indexOf('Invalid response') < 0);
|
2020-12-30 17:39:33 +00:00
|
|
|
this._ankiErrorMessageDetailsToggle.hidden = false;
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
|
2020-10-25 23:04:59 +00:00
|
|
|
_sortStringArray(array) {
|
|
|
|
const stringComparer = this._stringComparer;
|
|
|
|
array.sort((a, b) => stringComparer.compare(a, b));
|
|
|
|
}
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
2019-12-03 03:17:45 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
class AnkiCardController {
|
|
|
|
constructor(settingsController, ankiController, node) {
|
|
|
|
this._settingsController = settingsController;
|
|
|
|
this._ankiController = ankiController;
|
|
|
|
this._node = node;
|
|
|
|
this._cardType = node.dataset.ankiCardType;
|
2020-10-26 02:51:28 +00:00
|
|
|
this._cardMenu = node.dataset.ankiCardMenu;
|
2020-10-25 17:34:42 +00:00
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
this._fieldEventListeners = new EventListenerCollection();
|
|
|
|
this._fields = null;
|
|
|
|
this._modelChangingTo = null;
|
|
|
|
this._ankiCardFieldsContainer = null;
|
2021-01-06 23:16:51 +00:00
|
|
|
this._cleaned = false;
|
2021-01-31 16:55:11 +00:00
|
|
|
this._fieldEntries = [];
|
2021-04-13 00:20:14 +00:00
|
|
|
this._deckController = new AnkiCardSelectController();
|
|
|
|
this._modelController = new AnkiCardSelectController();
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 23:16:51 +00:00
|
|
|
async prepare() {
|
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
const ankiOptions = options.anki;
|
|
|
|
if (this._cleaned) { return; }
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
const cardOptions = this._getCardOptions(ankiOptions, this._cardType);
|
|
|
|
if (cardOptions === null) { return; }
|
|
|
|
const {deck, model, fields} = cardOptions;
|
2021-04-13 00:20:14 +00:00
|
|
|
this._deckController.prepare(this._node.querySelector('.anki-card-deck'), deck);
|
|
|
|
this._modelController.prepare(this._node.querySelector('.anki-card-model'), model);
|
2020-10-25 17:34:42 +00:00
|
|
|
this._fields = fields;
|
2020-05-24 17:38:48 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
this._ankiCardFieldsContainer = this._node.querySelector('.anki-card-fields');
|
2020-05-24 17:38:48 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
this._setupFields();
|
2020-05-24 17:38:48 +00:00
|
|
|
|
2021-04-13 00:20:14 +00:00
|
|
|
this._eventListeners.addEventListener(this._deckController.select, 'change', this._onCardDeckChange.bind(this), false);
|
|
|
|
this._eventListeners.addEventListener(this._modelController.select, 'change', this._onCardModelChange.bind(this), false);
|
2021-01-31 16:55:11 +00:00
|
|
|
this._eventListeners.on(this._settingsController, 'permissionsChanged', this._onPermissionsChanged.bind(this));
|
2020-05-24 17:38:48 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
await this.updateAnkiState();
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
cleanup() {
|
2021-01-06 23:16:51 +00:00
|
|
|
this._cleaned = true;
|
2021-01-31 16:55:11 +00:00
|
|
|
this._fieldEntries = [];
|
2020-10-25 17:34:42 +00:00
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
}
|
2020-05-29 23:52:51 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
async updateAnkiState() {
|
|
|
|
if (this._fields === null) { return; }
|
|
|
|
const {deckNames, modelNames} = await this._ankiController.getAnkiData();
|
2021-01-06 23:16:51 +00:00
|
|
|
if (this._cleaned) { return; }
|
2021-04-13 00:20:14 +00:00
|
|
|
this._deckController.setOptionValues(deckNames);
|
|
|
|
this._modelController.setOptionValues(modelNames);
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
isStale() {
|
|
|
|
return (this._cardType !== this._node.dataset.ankiCardType);
|
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_onCardDeckChange(e) {
|
|
|
|
this._setDeck(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onCardModelChange(e) {
|
|
|
|
this._setModel(e.currentTarget.value);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:20:29 +00:00
|
|
|
_onFieldChange(index, e) {
|
|
|
|
const node = e.currentTarget;
|
2021-01-31 16:55:11 +00:00
|
|
|
this._validateFieldPermissions(node, index, true);
|
2020-12-20 17:20:29 +00:00
|
|
|
this._validateField(node, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFieldInput(index, e) {
|
|
|
|
const node = e.currentTarget;
|
|
|
|
this._validateField(node, index);
|
2019-12-03 03:17:45 +00:00
|
|
|
}
|
2019-12-02 03:21:10 +00:00
|
|
|
|
2021-01-31 16:55:11 +00:00
|
|
|
_onFieldSettingChanged(index, e) {
|
|
|
|
const node = e.currentTarget;
|
|
|
|
this._validateFieldPermissions(node, index, false);
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:52:57 +00:00
|
|
|
_onFieldMenuClose({currentTarget: button, detail: {action, item}}) {
|
2020-10-26 02:51:28 +00:00
|
|
|
switch (action) {
|
|
|
|
case 'setFieldMarker':
|
2021-01-20 01:52:57 +00:00
|
|
|
this._setFieldMarker(button, item.dataset.marker);
|
2020-10-26 02:51:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_onFieldMarkerLinkClick(e) {
|
2020-05-29 23:52:51 +00:00
|
|
|
e.preventDefault();
|
|
|
|
const link = e.currentTarget;
|
2020-10-26 02:51:28 +00:00
|
|
|
this._setFieldMarker(link, link.textContent);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:20:29 +00:00
|
|
|
_validateField(node, index) {
|
2021-01-31 16:55:11 +00:00
|
|
|
let valid = (node.dataset.hasPermissions !== 'false');
|
2021-02-25 02:54:58 +00:00
|
|
|
if (valid && index === 0 && !AnkiUtil.stringContainsAnyFieldMarker(node.value)) {
|
2021-01-31 16:55:11 +00:00
|
|
|
valid = false;
|
2020-12-20 17:20:29 +00:00
|
|
|
}
|
2021-01-31 16:55:11 +00:00
|
|
|
node.dataset.invalid = `${!valid}`;
|
2020-12-20 17:20:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 02:51:28 +00:00
|
|
|
_setFieldMarker(element, marker) {
|
|
|
|
const input = element.closest('.anki-card-field-value-container').querySelector('.anki-card-field-value');
|
|
|
|
input.value = `{${marker}}`;
|
2020-05-29 23:52:51 +00:00
|
|
|
input.dispatchEvent(new Event('change'));
|
2019-12-01 20:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
_getCardOptions(ankiOptions, cardType) {
|
|
|
|
switch (cardType) {
|
|
|
|
case 'terms': return ankiOptions.terms;
|
|
|
|
case 'kanji': return ankiOptions.kanji;
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setupFields() {
|
|
|
|
this._fieldEventListeners.removeAllEventListeners();
|
|
|
|
|
|
|
|
const markers = this._ankiController.getFieldMarkers(this._cardType);
|
|
|
|
const totalFragment = document.createDocumentFragment();
|
2021-01-31 16:55:11 +00:00
|
|
|
this._fieldEntries = [];
|
2020-12-18 17:05:33 +00:00
|
|
|
let index = 0;
|
2020-10-25 17:34:42 +00:00
|
|
|
for (const [fieldName, fieldValue] of Object.entries(this._fields)) {
|
|
|
|
const content = this._settingsController.instantiateTemplateFragment('anki-card-field');
|
|
|
|
|
2020-12-18 17:05:33 +00:00
|
|
|
const fieldNameContainerNode = content.querySelector('.anki-card-field-name-container');
|
|
|
|
fieldNameContainerNode.dataset.index = `${index}`;
|
|
|
|
const fieldNameNode = content.querySelector('.anki-card-field-name');
|
|
|
|
fieldNameNode.textContent = fieldName;
|
|
|
|
|
|
|
|
const valueContainer = content.querySelector('.anki-card-field-value-container');
|
|
|
|
valueContainer.dataset.index = `${index}`;
|
2020-10-25 17:34:42 +00:00
|
|
|
|
|
|
|
const inputField = content.querySelector('.anki-card-field-value');
|
|
|
|
inputField.value = fieldValue;
|
|
|
|
inputField.dataset.setting = ObjectPropertyAccessor.getPathString(['anki', this._cardType, 'fields', fieldName]);
|
2021-01-31 16:55:11 +00:00
|
|
|
this._validateFieldPermissions(inputField, index, false);
|
|
|
|
|
2020-12-20 17:20:29 +00:00
|
|
|
this._fieldEventListeners.addEventListener(inputField, 'change', this._onFieldChange.bind(this, index), false);
|
|
|
|
this._fieldEventListeners.addEventListener(inputField, 'input', this._onFieldInput.bind(this, index), false);
|
2021-01-31 16:55:11 +00:00
|
|
|
this._fieldEventListeners.addEventListener(inputField, 'settingChanged', this._onFieldSettingChanged.bind(this, index), false);
|
2020-12-20 17:20:29 +00:00
|
|
|
this._validateField(inputField, index);
|
2020-10-25 17:34:42 +00:00
|
|
|
|
|
|
|
const markerList = content.querySelector('.anki-card-field-marker-list');
|
|
|
|
if (markerList !== null) {
|
|
|
|
const markersFragment = this._ankiController.getFieldMarkersHtml(markers);
|
|
|
|
for (const element of markersFragment.querySelectorAll('.marker-link')) {
|
|
|
|
this._fieldEventListeners.addEventListener(element, 'click', this._onFieldMarkerLinkClick.bind(this), false);
|
|
|
|
}
|
|
|
|
markerList.appendChild(markersFragment);
|
|
|
|
}
|
|
|
|
|
2020-10-26 02:51:28 +00:00
|
|
|
const menuButton = content.querySelector('.anki-card-field-value-menu-button');
|
|
|
|
if (menuButton !== null) {
|
|
|
|
if (typeof this._cardMenu !== 'undefined') {
|
|
|
|
menuButton.dataset.menu = this._cardMenu;
|
|
|
|
} else {
|
|
|
|
delete menuButton.dataset.menu;
|
|
|
|
}
|
2021-01-20 01:52:57 +00:00
|
|
|
this._fieldEventListeners.addEventListener(menuButton, 'menuClose', this._onFieldMenuClose.bind(this), false);
|
2020-10-26 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:34:42 +00:00
|
|
|
totalFragment.appendChild(content);
|
2021-01-31 16:55:11 +00:00
|
|
|
this._fieldEntries.push({fieldName, inputField, fieldNameContainerNode});
|
2020-12-18 17:05:33 +00:00
|
|
|
|
|
|
|
++index;
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
2020-10-26 02:51:28 +00:00
|
|
|
|
|
|
|
const ELEMENT_NODE = Node.ELEMENT_NODE;
|
|
|
|
const container = this._ankiCardFieldsContainer;
|
|
|
|
for (const node of [...container.childNodes]) {
|
|
|
|
if (node.nodeType === ELEMENT_NODE && node.dataset.persistent === 'true') { continue; }
|
|
|
|
container.removeChild(node);
|
|
|
|
}
|
|
|
|
container.appendChild(totalFragment);
|
2020-12-18 17:05:33 +00:00
|
|
|
|
2021-01-31 16:55:11 +00:00
|
|
|
this._validateFields();
|
2020-12-18 17:05:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 16:55:11 +00:00
|
|
|
async _validateFields() {
|
2020-12-18 17:05:33 +00:00
|
|
|
const token = {};
|
|
|
|
this._validateFieldsToken = token;
|
|
|
|
|
|
|
|
let fieldNames;
|
|
|
|
try {
|
2021-04-13 00:20:14 +00:00
|
|
|
fieldNames = await this._ankiController.getModelFieldNames(this._modelController.value);
|
2020-12-18 17:05:33 +00:00
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token !== this._validateFieldsToken) { return; }
|
|
|
|
|
|
|
|
const fieldNamesSet = new Set(fieldNames);
|
|
|
|
let index = 0;
|
2021-01-31 16:55:11 +00:00
|
|
|
for (const {fieldName, fieldNameContainerNode} of this._fieldEntries) {
|
2020-12-20 16:27:05 +00:00
|
|
|
fieldNameContainerNode.dataset.invalid = `${!fieldNamesSet.has(fieldName)}`;
|
2020-12-18 17:05:33 +00:00
|
|
|
fieldNameContainerNode.dataset.orderMatches = `${index < fieldNames.length && fieldName === fieldNames[index]}`;
|
|
|
|
++index;
|
|
|
|
}
|
2020-10-25 17:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _setDeck(value) {
|
2021-04-13 00:20:14 +00:00
|
|
|
if (this._deckController.value === value) { return; }
|
|
|
|
this._deckController.value = value;
|
2020-10-25 17:34:42 +00:00
|
|
|
|
|
|
|
await this._settingsController.modifyProfileSettings([{
|
|
|
|
action: 'set',
|
|
|
|
path: ObjectPropertyAccessor.getPathString(['anki', this._cardType, 'deck']),
|
|
|
|
value
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setModel(value) {
|
2021-04-13 00:20:14 +00:00
|
|
|
const select = this._modelController.select;
|
2020-10-25 17:34:42 +00:00
|
|
|
if (this._modelChangingTo !== null) {
|
|
|
|
// Revert
|
2021-04-13 00:20:14 +00:00
|
|
|
select.value = this._modelChangingTo;
|
2020-10-25 17:34:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-04-13 00:20:14 +00:00
|
|
|
if (this._modelController.value === value) { return; }
|
2020-10-25 17:34:42 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
let fieldNames;
|
2021-02-08 22:53:07 +00:00
|
|
|
let options;
|
2020-05-29 23:52:51 +00:00
|
|
|
try {
|
2020-10-25 17:34:42 +00:00
|
|
|
this._modelChangingTo = value;
|
|
|
|
fieldNames = await this._ankiController.getModelFieldNames(value);
|
2021-02-08 22:53:07 +00:00
|
|
|
options = await this._ankiController.settingsController.getOptions();
|
2020-10-25 17:34:42 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Revert
|
2021-04-13 00:20:14 +00:00
|
|
|
select.value = this._modelController.value;
|
2020-05-29 23:52:51 +00:00
|
|
|
return;
|
|
|
|
} finally {
|
2020-10-25 17:34:42 +00:00
|
|
|
this._modelChangingTo = null;
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-01 20:41:09 +00:00
|
|
|
|
2021-02-08 22:53:07 +00:00
|
|
|
const cardType = this._cardType;
|
|
|
|
const cardOptions = this._getCardOptions(options.anki, cardType);
|
|
|
|
const oldFields = cardOptions !== null ? cardOptions.fields : null;
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
const fields = {};
|
2021-02-08 22:53:07 +00:00
|
|
|
for (let i = 0, ii = fieldNames.length; i < ii; ++i) {
|
|
|
|
const fieldName = fieldNames[i];
|
|
|
|
fields[fieldName] = this._getDefaultFieldValue(fieldName, i, cardType, oldFields);
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2020-05-24 17:38:48 +00:00
|
|
|
|
2020-05-30 20:22:51 +00:00
|
|
|
const targets = [
|
2020-10-25 17:34:42 +00:00
|
|
|
{
|
|
|
|
action: 'set',
|
|
|
|
path: ObjectPropertyAccessor.getPathString(['anki', this._cardType, 'model']),
|
|
|
|
value
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'set',
|
|
|
|
path: ObjectPropertyAccessor.getPathString(['anki', this._cardType, 'fields']),
|
|
|
|
value: fields
|
|
|
|
}
|
2020-05-30 20:22:51 +00:00
|
|
|
];
|
|
|
|
|
2021-04-13 00:20:14 +00:00
|
|
|
this._modelController.value = value;
|
2020-10-25 17:34:42 +00:00
|
|
|
this._fields = fields;
|
|
|
|
|
2020-05-30 20:22:51 +00:00
|
|
|
await this._settingsController.modifyProfileSettings(targets);
|
2020-10-25 17:34:42 +00:00
|
|
|
|
|
|
|
this._setupFields();
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2021-01-31 16:55:11 +00:00
|
|
|
|
|
|
|
async _requestPermissions(permissions) {
|
|
|
|
try {
|
2021-02-11 23:55:09 +00:00
|
|
|
await this._settingsController.permissionsUtil.setPermissionsGranted({permissions}, true);
|
2021-01-31 16:55:11 +00:00
|
|
|
} catch (e) {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(e);
|
2021-01-31 16:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _validateFieldPermissions(node, index, request) {
|
|
|
|
const fieldValue = node.value;
|
|
|
|
const permissions = this._ankiController.getRequiredPermissions(fieldValue);
|
|
|
|
if (permissions.length > 0) {
|
|
|
|
node.dataset.requiredPermission = permissions.join(' ');
|
|
|
|
const hasPermissions = await (
|
|
|
|
request ?
|
2021-02-11 23:55:09 +00:00
|
|
|
this._settingsController.permissionsUtil.setPermissionsGranted({permissions}, true) :
|
|
|
|
this._settingsController.permissionsUtil.hasPermissions({permissions})
|
2021-01-31 16:55:11 +00:00
|
|
|
);
|
|
|
|
node.dataset.hasPermissions = `${hasPermissions}`;
|
|
|
|
} else {
|
|
|
|
delete node.dataset.requiredPermission;
|
|
|
|
delete node.dataset.hasPermissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._validateField(node, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPermissionsChanged({permissions: {permissions}}) {
|
|
|
|
const permissionsSet = new Set(permissions);
|
|
|
|
for (let i = 0, ii = this._fieldEntries.length; i < ii; ++i) {
|
|
|
|
const {inputField} = this._fieldEntries[i];
|
|
|
|
let {requiredPermission} = inputField.dataset;
|
|
|
|
if (typeof requiredPermission !== 'string') { continue; }
|
|
|
|
requiredPermission = (requiredPermission.length === 0 ? [] : requiredPermission.split(' '));
|
|
|
|
|
|
|
|
let hasPermissions = true;
|
|
|
|
for (const permission of requiredPermission) {
|
|
|
|
if (!permissionsSet.has(permission)) {
|
|
|
|
hasPermissions = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inputField.dataset.hasPermissions = `${hasPermissions}`;
|
|
|
|
this._validateField(inputField, i);
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 22:53:07 +00:00
|
|
|
|
|
|
|
_getDefaultFieldValue(fieldName, index, cardType, oldFields) {
|
|
|
|
if (
|
|
|
|
typeof oldFields === 'object' &&
|
|
|
|
oldFields !== null &&
|
|
|
|
Object.prototype.hasOwnProperty.call(oldFields, fieldName)
|
|
|
|
) {
|
|
|
|
return oldFields[fieldName];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
return (cardType === 'kanji' ? '{character}' : '{expression}');
|
|
|
|
}
|
|
|
|
|
|
|
|
const markers = this._ankiController.getFieldMarkers(cardType);
|
|
|
|
const markerAliases = new Map([
|
|
|
|
['glossary', ['definition', 'meaning']],
|
|
|
|
['audio', ['sound']],
|
|
|
|
['dictionary', ['dict']]
|
|
|
|
]);
|
|
|
|
|
|
|
|
const hyphenPattern = /-/g;
|
|
|
|
for (const marker of markers) {
|
|
|
|
const names = [marker];
|
|
|
|
const aliases = markerAliases.get(marker);
|
|
|
|
if (typeof aliases !== 'undefined') {
|
|
|
|
names.push(...aliases);
|
|
|
|
}
|
|
|
|
|
|
|
|
let pattern = '^(?:';
|
|
|
|
for (let i = 0, ii = names.length; i < ii; ++i) {
|
|
|
|
const name = names[i];
|
|
|
|
if (i > 0) { pattern += '|'; }
|
|
|
|
pattern += name.replace(hyphenPattern, '[-_ ]*');
|
|
|
|
}
|
|
|
|
pattern += ')$';
|
|
|
|
pattern = new RegExp(pattern, 'i');
|
|
|
|
|
|
|
|
if (pattern.test(fieldName)) {
|
|
|
|
return `{${marker}}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2019-12-02 03:16:58 +00:00
|
|
|
}
|
2021-04-13 00:20:14 +00:00
|
|
|
|
|
|
|
class AnkiCardSelectController {
|
|
|
|
constructor() {
|
|
|
|
this._value = null;
|
|
|
|
this._select = null;
|
|
|
|
this._optionValues = null;
|
|
|
|
this._hasExtraOption = false;
|
|
|
|
this._selectNeedsUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(value) {
|
|
|
|
this._value = value;
|
|
|
|
this._updateSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
get select() {
|
|
|
|
return this._select;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare(select, value) {
|
|
|
|
this._select = select;
|
|
|
|
this._value = value;
|
|
|
|
this._updateSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
setOptionValues(optionValues) {
|
|
|
|
this._optionValues = optionValues;
|
|
|
|
this._selectNeedsUpdate = true;
|
|
|
|
this._updateSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_updateSelect() {
|
|
|
|
const value = this._value;
|
|
|
|
let optionValues = this._optionValues;
|
|
|
|
const hasOptionValues = Array.isArray(optionValues) && optionValues.length > 0;
|
|
|
|
|
|
|
|
if (!hasOptionValues) {
|
|
|
|
optionValues = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasExtraOption = !optionValues.includes(value);
|
|
|
|
if (hasExtraOption) {
|
|
|
|
optionValues = [...optionValues, value];
|
|
|
|
}
|
|
|
|
|
|
|
|
const select = this._select;
|
|
|
|
if (this._selectNeedsUpdate || hasExtraOption !== this._hasExtraOption) {
|
|
|
|
this._setSelectOptions(select, optionValues);
|
|
|
|
select.value = value;
|
|
|
|
this._hasExtraOption = hasExtraOption;
|
|
|
|
this._selectNeedsUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasOptionValues) {
|
|
|
|
select.dataset.invalid = `${hasExtraOption}`;
|
|
|
|
} else {
|
|
|
|
delete select.dataset.invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSelectOptions(select, optionValues) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const optionValue of optionValues) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = optionValue;
|
|
|
|
option.textContent = optionValue;
|
|
|
|
fragment.appendChild(option);
|
|
|
|
}
|
|
|
|
select.textContent = '';
|
|
|
|
select.appendChild(fragment);
|
|
|
|
}
|
|
|
|
}
|