From 5d8ac70395f548923549bb73d183004c9a71e756 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 18 Dec 2020 12:05:33 -0500 Subject: Display an indication when Anki field order/existence doesn't match (#1119) --- ext/bg/css/settings2.css | 17 ++++++++++++++-- ext/bg/js/settings/anki-controller.js | 38 ++++++++++++++++++++++++++++++++++- ext/bg/settings.html | 2 +- ext/bg/settings2.html | 2 +- 4 files changed, 54 insertions(+), 5 deletions(-) (limited to 'ext') diff --git a/ext/bg/css/settings2.css b/ext/bg/css/settings2.css index e15fa080..d25004bc 100644 --- a/ext/bg/css/settings2.css +++ b/ext/bg/css/settings2.css @@ -2088,9 +2088,22 @@ select.profile-condition-operator { .anki-card-field-input-header { font-weight: bold; } -.anki-card-field-name { - margin-right: 1em; +.anki-card-field-name-container { + display: flex; + flex-flow: row nowrap; + align-self: stretch; + align-items: center; margin-top: 0.25em; + margin-right: 0.25em; + padding: 0 0.25em; +} +.anki-card-field-name-container[data-is-valid=false], +.anki-card-field-name-container[data-order-matches=false][data-index='0'] { + background-color: var(--danger-color-lightest); + border-radius: var(--text-input-border-radius); +} +.anki-card-field-name { + flex: 1 1 auto; } .anki-card-field-value-container { display: flex; diff --git a/ext/bg/js/settings/anki-controller.js b/ext/bg/js/settings/anki-controller.js index 957f86d9..541916b7 100644 --- a/ext/bg/js/settings/anki-controller.js +++ b/ext/bg/js/settings/anki-controller.js @@ -46,6 +46,7 @@ class AnkiController { this._ankiErrorInvalidResponseInfo = null; this._ankiCardPrimary = null; this._ankiCardPrimaryType = null; + this._validateFieldsToken = null; } async prepare() { @@ -464,10 +465,19 @@ class AnkiCardController { const markers = this._ankiController.getFieldMarkers(this._cardType); const totalFragment = document.createDocumentFragment(); + const fieldMap = new Map(); + let index = 0; for (const [fieldName, fieldValue] of Object.entries(this._fields)) { const content = this._settingsController.instantiateTemplateFragment('anki-card-field'); - content.querySelector('.anki-card-field-name').textContent = fieldName; + const fieldNameContainerNode = content.querySelector('.anki-card-field-name-container'); + fieldNameContainerNode.dataset.index = `${index}`; + const fieldNameNode = content.querySelector('.anki-card-field-name'); + fieldNameNode.textContent = fieldName; + fieldMap.set(fieldName, {fieldNameContainerNode}); + + const valueContainer = content.querySelector('.anki-card-field-value-container'); + valueContainer.dataset.index = `${index}`; const inputField = content.querySelector('.anki-card-field-value'); inputField.value = fieldValue; @@ -494,6 +504,8 @@ class AnkiCardController { } totalFragment.appendChild(content); + + ++index; } const ELEMENT_NODE = Node.ELEMENT_NODE; @@ -503,6 +515,30 @@ class AnkiCardController { container.removeChild(node); } container.appendChild(totalFragment); + + this._validateFields(fieldMap); + } + + async _validateFields(fieldMap) { + const token = {}; + this._validateFieldsToken = token; + + let fieldNames; + try { + fieldNames = await this._ankiController.getModelFieldNames(this._model); + } catch (e) { + return; + } + + if (token !== this._validateFieldsToken) { return; } + + const fieldNamesSet = new Set(fieldNames); + let index = 0; + for (const [fieldName, {fieldNameContainerNode}] of fieldMap.entries()) { + fieldNameContainerNode.dataset.isValid = `${fieldNamesSet.has(fieldName)}`; + fieldNameContainerNode.dataset.orderMatches = `${index < fieldNames.length && fieldName === fieldNames[index]}`; + ++index; + } } async _setDeck(value) { diff --git a/ext/bg/settings.html b/ext/bg/settings.html index acb07a60..6876701d 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -1083,7 +1083,7 @@