Fix multiline copying (#1493)

* Change order

* Update multiline text assignment
This commit is contained in:
toasted-nutbread 2021-03-06 13:41:38 -05:00 committed by GitHub
parent 4bc53d2348
commit 6e00b5d765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -302,7 +302,7 @@ class DisplayGenerator {
_createTermGlossaryItemText(glossary) { _createTermGlossaryItemText(glossary) {
const node = this._templates.instantiate('glossary-item'); const node = this._templates.instantiate('glossary-item');
const container = node.querySelector('.glossary'); const container = node.querySelector('.glossary');
this._setTextContent(container, glossary); this._setMultilineTextContent(container, glossary);
return node; return node;
} }
@ -350,7 +350,7 @@ class DisplayGenerator {
if (typeof description === 'string') { if (typeof description === 'string') {
const container = node.querySelector('.glossary-image-description'); const container = node.querySelector('.glossary-image-description');
this._setTextContent(container, description); this._setMultilineTextContent(container, description);
} }
return node; return node;
@ -385,7 +385,7 @@ class DisplayGenerator {
_createKanjiGlossaryItem(glossary) { _createKanjiGlossaryItem(glossary) {
const node = this._templates.instantiate('kanji-glossary-item'); const node = this._templates.instantiate('kanji-glossary-item');
const container = node.querySelector('.kanji-glossary'); const container = node.querySelector('.kanji-glossary');
this._setTextContent(container, glossary); this._setMultilineTextContent(container, glossary);
return node; return node;
} }
@ -721,12 +721,36 @@ class DisplayGenerator {
} }
_setTextContent(node, value, language) { _setTextContent(node, value, language) {
node.textContent = value;
if (typeof language === 'string') { if (typeof language === 'string') {
node.lang = language; node.lang = language;
} else if (this._japaneseUtil.isStringPartiallyJapanese(value)) { } else if (this._japaneseUtil.isStringPartiallyJapanese(value)) {
node.lang = 'ja'; node.lang = 'ja';
} }
node.textContent = value;
}
_setMultilineTextContent(node, value, language) {
// This can't just call _setTextContent because the lack of <br> elements will
// cause the text to not copy correctly.
if (typeof language === 'string') {
node.lang = language;
} else if (this._japaneseUtil.isStringPartiallyJapanese(value)) {
node.lang = 'ja';
}
let start = 0;
while (true) {
const end = value.indexOf('\n', start);
if (end < 0) { break; }
node.appendChild(document.createTextNode(value.substring(start, end)));
node.appendChild(document.createElement('br'));
start = end + 1;
}
if (start < value.length) {
node.appendChild(document.createTextNode(start === 0 ? value : value.substring(start)));
}
} }
_getPitchAccentCategories(pitches) { _getPitchAccentCategories(pitches) {