2019-12-01 22:01:41 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2019-2021 Yomichan Authors
|
2019-12-01 22:01:41 +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 22:01:41 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* AnkiNoteBuilder
|
2020-09-19 01:16:39 +00:00
|
|
|
* TemplateRendererProxy
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
class AnkiTemplatesController {
|
2020-10-11 00:58:38 +00:00
|
|
|
constructor(settingsController, modalController, ankiController) {
|
2020-05-30 13:33:13 +00:00
|
|
|
this._settingsController = settingsController;
|
2020-10-11 00:58:38 +00:00
|
|
|
this._modalController = modalController;
|
2020-05-29 23:52:51 +00:00
|
|
|
this._ankiController = ankiController;
|
|
|
|
this._cachedDefinitionValue = null;
|
|
|
|
this._cachedDefinitionText = null;
|
2020-05-30 13:33:13 +00:00
|
|
|
this._defaultFieldTemplates = null;
|
2020-10-30 21:41:52 +00:00
|
|
|
this._fieldTemplatesTextarea = null;
|
|
|
|
this._compileResultInfo = null;
|
|
|
|
this._renderFieldInput = null;
|
|
|
|
this._renderResult = null;
|
2020-09-19 21:14:51 +00:00
|
|
|
this._fieldTemplateResetModal = null;
|
2020-09-19 01:16:39 +00:00
|
|
|
this._templateRenderer = new TemplateRendererProxy();
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-30 13:33:13 +00:00
|
|
|
async prepare() {
|
|
|
|
this._defaultFieldTemplates = await api.getDefaultAnkiFieldTemplates();
|
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
this._fieldTemplatesTextarea = document.querySelector('#anki-card-templates-textarea');
|
|
|
|
this._compileResultInfo = document.querySelector('#anki-card-templates-compile-result');
|
|
|
|
this._renderFieldInput = document.querySelector('#anki-card-templates-test-field-input');
|
|
|
|
this._renderTextInput = document.querySelector('#anki-card-templates-test-text-input');
|
|
|
|
this._renderResult = document.querySelector('#anki-card-templates-render-result');
|
|
|
|
const menuButton = document.querySelector('#anki-card-templates-test-field-menu-button');
|
|
|
|
const testRenderButton = document.querySelector('#anki-card-templates-test-render-button');
|
|
|
|
const resetButton = document.querySelector('#anki-card-templates-reset-button');
|
|
|
|
const resetConfirmButton = document.querySelector('#anki-card-templates-reset-button-confirm');
|
|
|
|
const fieldList = document.querySelector('#anki-card-templates-field-list');
|
|
|
|
this._fieldTemplateResetModal = this._modalController.getModal('anki-card-templates-reset');
|
2020-09-19 21:14:51 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
const markers = new Set([
|
|
|
|
...this._ankiController.getFieldMarkers('terms'),
|
|
|
|
...this._ankiController.getFieldMarkers('kanji')
|
|
|
|
]);
|
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
if (fieldList !== null) {
|
|
|
|
const fragment = this._ankiController.getFieldMarkersHtml(markers);
|
|
|
|
fieldList.appendChild(fragment);
|
|
|
|
for (const node of fieldList.querySelectorAll('.marker-link')) {
|
|
|
|
node.addEventListener('click', this._onMarkerClicked.bind(this), false);
|
|
|
|
}
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
this._fieldTemplatesTextarea.addEventListener('change', this._onChanged.bind(this), false);
|
|
|
|
testRenderButton.addEventListener('click', this._onRender.bind(this), false);
|
|
|
|
resetButton.addEventListener('click', this._onReset.bind(this), false);
|
|
|
|
resetConfirmButton.addEventListener('click', this._onResetConfirm.bind(this), false);
|
|
|
|
if (menuButton !== null) {
|
|
|
|
menuButton.addEventListener('menuClosed', this._onFieldMenuClosed.bind(this), false);
|
|
|
|
}
|
2020-02-28 01:33:13 +00:00
|
|
|
|
2020-05-30 13:33:13 +00:00
|
|
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
|
|
|
|
|
|
|
const options = await this._settingsController.getOptions();
|
|
|
|
this._onOptionsChanged({options});
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-30 13:33:13 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_onOptionsChanged({options}) {
|
2020-05-29 23:52:51 +00:00
|
|
|
let templates = options.anki.fieldTemplates;
|
2020-05-30 13:33:13 +00:00
|
|
|
if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; }
|
2020-10-30 21:41:52 +00:00
|
|
|
this._fieldTemplatesTextarea.value = templates;
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
this._onValidateCompile();
|
2019-12-01 22:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
_onReset(e) {
|
|
|
|
e.preventDefault();
|
2020-09-19 21:14:51 +00:00
|
|
|
this._fieldTemplateResetModal.setVisible(true);
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-15 05:02:52 +00:00
|
|
|
|
2020-05-30 20:22:05 +00:00
|
|
|
_onResetConfirm(e) {
|
2020-05-29 23:52:51 +00:00
|
|
|
e.preventDefault();
|
2019-12-15 05:02:52 +00:00
|
|
|
|
2020-09-19 21:14:51 +00:00
|
|
|
this._fieldTemplateResetModal.setVisible(false);
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-30 20:22:05 +00:00
|
|
|
const value = this._defaultFieldTemplates;
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
this._fieldTemplatesTextarea.value = value;
|
|
|
|
this._fieldTemplatesTextarea.dispatchEvent(new Event('change'));
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
async _onChanged(e) {
|
|
|
|
// Get value
|
|
|
|
let templates = e.currentTarget.value;
|
2020-05-30 20:22:05 +00:00
|
|
|
if (templates === this._defaultFieldTemplates) {
|
2020-05-29 23:52:51 +00:00
|
|
|
// Default
|
|
|
|
templates = null;
|
2019-12-01 22:01:41 +00:00
|
|
|
}
|
2020-05-29 23:52:51 +00:00
|
|
|
|
|
|
|
// Overwrite
|
2020-05-30 20:22:05 +00:00
|
|
|
await this._settingsController.setProfileSetting('anki.fieldTemplates', templates);
|
2020-05-29 23:52:51 +00:00
|
|
|
|
|
|
|
// Compile
|
|
|
|
this._onValidateCompile();
|
2019-12-01 22:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
_onValidateCompile() {
|
2020-10-30 21:41:52 +00:00
|
|
|
this._validate(this._compileResultInfo, '{expression}', 'term-kanji', false, true);
|
2019-12-01 22:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
_onMarkerClicked(e) {
|
|
|
|
e.preventDefault();
|
2020-10-30 21:41:52 +00:00
|
|
|
this._renderFieldInput.value = `{${e.target.textContent}}`;
|
2019-12-15 05:02:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
_onRender(e) {
|
|
|
|
e.preventDefault();
|
2019-12-15 05:02:52 +00:00
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
const field = this._renderFieldInput.value;
|
|
|
|
const infoNode = this._renderResult;
|
2020-05-29 23:52:51 +00:00
|
|
|
infoNode.hidden = true;
|
2020-11-13 01:35:11 +00:00
|
|
|
this._cachedDefinitionText = null;
|
2020-05-29 23:52:51 +00:00
|
|
|
this._validate(infoNode, field, 'term-kanji', true, false);
|
|
|
|
}
|
2019-12-15 05:02:52 +00:00
|
|
|
|
2020-10-30 21:41:52 +00:00
|
|
|
_onFieldMenuClosed({currentTarget: node, detail: {action, item}}) {
|
|
|
|
switch (action) {
|
|
|
|
case 'setFieldMarker':
|
|
|
|
this._setFieldMarker(node, item.dataset.marker);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setFieldMarker(element, marker) {
|
|
|
|
const input = this._renderFieldInput;
|
|
|
|
input.value = `{${marker}}`;
|
|
|
|
input.dispatchEvent(new Event('change'));
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
async _getDefinition(text, optionsContext) {
|
|
|
|
if (this._cachedDefinitionText !== text) {
|
|
|
|
const {definitions} = await api.termsFind(text, {}, optionsContext);
|
|
|
|
if (definitions.length === 0) { return null; }
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
this._cachedDefinitionValue = definitions[0];
|
|
|
|
this._cachedDefinitionText = text;
|
|
|
|
}
|
|
|
|
return this._cachedDefinitionValue;
|
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
async _validate(infoNode, field, mode, showSuccessResult, invalidateInput) {
|
2020-10-30 21:41:52 +00:00
|
|
|
const text = this._renderTextInput.value || '';
|
2020-05-29 23:52:51 +00:00
|
|
|
const exceptions = [];
|
|
|
|
let result = `No definition found for ${text}`;
|
|
|
|
try {
|
2020-05-30 13:33:13 +00:00
|
|
|
const optionsContext = this._settingsController.getOptionsContext();
|
2020-05-29 23:52:51 +00:00
|
|
|
const definition = await this._getDefinition(text, optionsContext);
|
|
|
|
if (definition !== null) {
|
2020-05-30 13:33:13 +00:00
|
|
|
const options = await this._settingsController.getOptions();
|
2020-05-29 23:52:51 +00:00
|
|
|
const context = {
|
|
|
|
document: {
|
|
|
|
title: document.title
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let templates = options.anki.fieldTemplates;
|
2020-05-30 20:22:05 +00:00
|
|
|
if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; }
|
2020-09-08 15:01:08 +00:00
|
|
|
const ankiNoteBuilder = new AnkiNoteBuilder({
|
2020-09-10 22:03:46 +00:00
|
|
|
renderTemplate: this._renderTemplate.bind(this)
|
2020-09-08 15:01:08 +00:00
|
|
|
});
|
2020-11-26 04:22:05 +00:00
|
|
|
const {general: {resultOutputMode, glossaryLayoutMode, compactTags}} = options;
|
2020-09-09 15:54:40 +00:00
|
|
|
const note = await ankiNoteBuilder.createNote({
|
|
|
|
definition,
|
|
|
|
mode,
|
|
|
|
context,
|
|
|
|
templates,
|
2021-01-04 00:40:12 +00:00
|
|
|
deckName: '',
|
|
|
|
modelName: '',
|
|
|
|
fields: [
|
|
|
|
['field', field]
|
|
|
|
],
|
2020-09-09 15:54:40 +00:00
|
|
|
resultOutputMode,
|
2020-11-26 04:22:05 +00:00
|
|
|
glossaryLayoutMode,
|
2020-11-13 01:34:11 +00:00
|
|
|
compactTags,
|
2020-09-09 15:54:40 +00:00
|
|
|
errors: exceptions
|
|
|
|
});
|
|
|
|
result = note.fields.field;
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
exceptions.push(e);
|
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-29 23:52:51 +00:00
|
|
|
const hasException = exceptions.length > 0;
|
|
|
|
infoNode.hidden = !(showSuccessResult || hasException);
|
|
|
|
infoNode.textContent = hasException ? exceptions.map((e) => `${e}`).join('\n') : (showSuccessResult ? result : '');
|
|
|
|
infoNode.classList.toggle('text-danger', hasException);
|
|
|
|
if (invalidateInput) {
|
2020-12-20 16:27:05 +00:00
|
|
|
this._fieldTemplatesTextarea.dataset.invalid = `${hasException}`;
|
2020-05-29 23:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 22:03:46 +00:00
|
|
|
|
|
|
|
async _renderTemplate(template, data, marker) {
|
|
|
|
return await this._templateRenderer.render(template, data, marker);
|
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
}
|