From 86237542bbe335d6069b0b7c6a6fd897a8517879 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 2 Feb 2023 12:59:28 +0100 Subject: fix spoiler unhide on empty reading --- anki-card-template/card.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/anki-card-template/card.js b/anki-card-template/card.js index 38d2cfb..7d1742e 100644 --- a/anki-card-template/card.js +++ b/anki-card-template/card.js @@ -170,22 +170,22 @@ function parseReading(nodes) { var mode = "writing"; // parsing mode ("writing" or "reading") var flush_writings = () => { + if (writings.reduce((current, n) => current + n.length, 0) == 0) return; + out += ``; for(let i = 0; i < writings.length; i++) { if (i == 1) out += ``; if (i > 0) out += `\u3001`; var classes = ["writing"]; if (i == 0) classes.push("first"); out += `${writings[i].trim()}`; - if (writings.length > 1 && i == writings.length - 1) out += `+${writings.length - 1}`; } + if (writings.length > 1) out += `+${writings.length - 1}`; + out += ``; writings = []; writingIndex = 0; }; for (var i = 0; i < input.length; i++) { if (i == 0) { - // start kanji reading - out += ``; - var match = input.match(/\((.+?)\)/); // display "(note)" before kanji if (match) { @@ -214,6 +214,7 @@ function parseReading(nodes) { else out += input[i]; } flush_writings(); // kana only word fix + console.log(out); return out; }); } @@ -316,12 +317,15 @@ HTMLElement.prototype.parse = function() { if (this.classList.contains("parsed")) return; // ignore already parsed elements var nodes = parse(this.innerHTML, Array.from(this.classList)); - - var out = nodes.map(n => n.data).join(""); - this.innerHTML = out; this.classList.add("parsed"); - if (nodes.reduce((current, n) => current + (n.type == "text" ? n.data.length : 0), 0) == 0) - this.classList.add("empty"); // if innerHTML only contains empty html (no 'user' text) + + // if innerHTML only contains empty html (no 'user' text) + if (nodes.reduce((current, n) => current + (n.type == "text" ? n.data.length : 0), 0) == 0) { + this.classList.add("empty"); + this.innerHTML = ""; + return; + } + this.innerHTML = nodes.map(n => n.data).join(""); }; function layout() { @@ -348,8 +352,10 @@ function run() { }; // remove spoiler from sentence translation if word reading field is empty - if(document.getElementById("target-word-reading").classList.contains("empty")) + if(document.getElementById("target-word-reading").classList.contains("empty")) { document.getElementById("sentence-translation").classList.remove("hidden"); + document.getElementById("sentence-translation").classList.add("visible"); + } layout(); } -- cgit v1.2.3