2019-12-01 22:01:41 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2019-2020 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-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-05-30 13:33:13 +00:00
|
|
|
constructor(settingsController, ankiController) {
|
|
|
|
this._settingsController = settingsController;
|
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-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-05-29 23:52:51 +00:00
|
|
|
const markers = new Set([
|
|
|
|
...this._ankiController.getFieldMarkers('terms'),
|
|
|
|
...this._ankiController.getFieldMarkers('kanji')
|
|
|
|
]);
|
|
|
|
const fragment = this._ankiController.getFieldMarkersHtml(markers);
|
|
|
|
|
|
|
|
const list = document.querySelector('#field-templates-list');
|
|
|
|
list.appendChild(fragment);
|
|
|
|
for (const node of list.querySelectorAll('.marker-link')) {
|
|
|
|
node.addEventListener('click', this._onMarkerClicked.bind(this), false);
|
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
|
2020-05-30 20:22:05 +00:00
|
|
|
document.querySelector('#field-templates').addEventListener('change', this._onChanged.bind(this), false);
|
|
|
|
document.querySelector('#field-template-render').addEventListener('click', this._onRender.bind(this), false);
|
|
|
|
document.querySelector('#field-templates-reset').addEventListener('click', this._onReset.bind(this), false);
|
|
|
|
document.querySelector('#field-templates-reset-confirm').addEventListener('click', this._onResetConfirm.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-05-30 20:22:05 +00:00
|
|
|
document.querySelector('#field-templates').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();
|
|
|
|
$('#field-template-reset-modal').modal('show');
|
|
|
|
}
|
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-05-29 23:52:51 +00:00
|
|
|
$('#field-template-reset-modal').modal('hide');
|
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-05-29 23:52:51 +00:00
|
|
|
const element = document.querySelector('#field-templates');
|
|
|
|
element.value = value;
|
|
|
|
element.dispatchEvent(new Event('change'));
|
|
|
|
}
|
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() {
|
|
|
|
const infoNode = document.querySelector('#field-template-compile-result');
|
|
|
|
this._validate(infoNode, '{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();
|
|
|
|
document.querySelector('#field-template-render-text').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-05-29 23:52:51 +00:00
|
|
|
const field = document.querySelector('#field-template-render-text').value;
|
|
|
|
const infoNode = document.querySelector('#field-template-render-result');
|
|
|
|
infoNode.hidden = true;
|
|
|
|
this._validate(infoNode, field, 'term-kanji', true, false);
|
|
|
|
}
|
2019-12-15 05:02:52 +00:00
|
|
|
|
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) {
|
|
|
|
const text = document.querySelector('#field-templates-preview-text').value || '';
|
|
|
|
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({
|
|
|
|
renderTemplate: api.templateRender.bind(api),
|
|
|
|
getClipboardImage: api.clipboardGetImage.bind(api)
|
|
|
|
});
|
2020-09-09 15:54:40 +00:00
|
|
|
const {general: {resultOutputMode, compactGlossaries}} = options;
|
|
|
|
const note = await ankiNoteBuilder.createNote({
|
|
|
|
definition,
|
|
|
|
mode,
|
|
|
|
context,
|
|
|
|
templates,
|
|
|
|
resultOutputMode,
|
|
|
|
compactGlossaries,
|
|
|
|
modeOptions: {
|
|
|
|
fields: {field},
|
|
|
|
deck: '',
|
|
|
|
model: ''
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
const input = document.querySelector('#field-templates');
|
|
|
|
input.classList.toggle('is-invalid', hasException);
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 22:01:41 +00:00
|
|
|
}
|