2021-06-26 18:40:37 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2021-2022 Yomichan Authors
|
2021-06-26 18:40:37 +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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class StructuredContentGenerator {
|
2022-05-14 22:12:57 +00:00
|
|
|
constructor(contentManager, japaneseUtil, document) {
|
2022-03-15 01:42:33 +00:00
|
|
|
this._contentManager = contentManager;
|
2022-05-14 22:12:57 +00:00
|
|
|
this._japaneseUtil = japaneseUtil;
|
2021-06-26 18:40:37 +00:00
|
|
|
this._document = document;
|
|
|
|
}
|
|
|
|
|
2022-05-14 22:12:57 +00:00
|
|
|
appendStructuredContent(node, content, dictionary) {
|
|
|
|
node.classList.add('structured-content');
|
|
|
|
this._appendStructuredContent(node, content, dictionary, null);
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:40:37 +00:00
|
|
|
createStructuredContent(content, dictionary) {
|
2022-05-14 22:12:57 +00:00
|
|
|
const node = this._createElement('span', 'structured-content');
|
|
|
|
this._appendStructuredContent(node, content, dictionary, null);
|
|
|
|
return node;
|
2021-06-26 18:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createDefinitionImage(data, dictionary) {
|
|
|
|
const {
|
|
|
|
path,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
preferredWidth,
|
|
|
|
preferredHeight,
|
|
|
|
title,
|
|
|
|
pixelated,
|
|
|
|
imageRendering,
|
|
|
|
appearance,
|
|
|
|
background,
|
|
|
|
collapsed,
|
|
|
|
collapsible,
|
|
|
|
verticalAlign,
|
|
|
|
sizeUnits
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
const hasPreferredWidth = (typeof preferredWidth === 'number');
|
|
|
|
const hasPreferredHeight = (typeof preferredHeight === 'number');
|
2021-06-29 01:46:54 +00:00
|
|
|
const invAspectRatio = (
|
2021-06-26 18:40:37 +00:00
|
|
|
hasPreferredWidth && hasPreferredHeight ?
|
2021-06-29 01:46:54 +00:00
|
|
|
preferredHeight / preferredWidth :
|
|
|
|
height / width
|
2021-06-26 18:40:37 +00:00
|
|
|
);
|
|
|
|
const usedWidth = (
|
|
|
|
hasPreferredWidth ?
|
|
|
|
preferredWidth :
|
2021-07-22 22:11:46 +00:00
|
|
|
(hasPreferredHeight ? preferredHeight / invAspectRatio : width)
|
2021-06-26 18:40:37 +00:00
|
|
|
);
|
|
|
|
|
2021-06-26 19:49:23 +00:00
|
|
|
const node = this._createElement('a', 'gloss-image-link');
|
|
|
|
node.target = '_blank';
|
|
|
|
node.rel = 'noreferrer noopener';
|
|
|
|
|
|
|
|
const imageContainer = this._createElement('span', 'gloss-image-container');
|
|
|
|
node.appendChild(imageContainer);
|
|
|
|
|
2021-06-29 02:12:17 +00:00
|
|
|
const aspectRatioSizer = this._createElement('span', 'gloss-image-sizer');
|
2021-06-26 19:49:23 +00:00
|
|
|
imageContainer.appendChild(aspectRatioSizer);
|
|
|
|
|
2021-06-27 19:01:35 +00:00
|
|
|
const imageBackground = this._createElement('span', 'gloss-image-background');
|
2021-06-26 19:49:23 +00:00
|
|
|
imageContainer.appendChild(imageBackground);
|
|
|
|
|
|
|
|
const image = this._createElement('img', 'gloss-image');
|
|
|
|
image.alt = '';
|
|
|
|
imageContainer.appendChild(image);
|
|
|
|
|
|
|
|
const overlay = this._createElement('span', 'gloss-image-container-overlay');
|
|
|
|
imageContainer.appendChild(overlay);
|
|
|
|
|
|
|
|
const linkText = this._createElement('span', 'gloss-image-link-text');
|
|
|
|
linkText.textContent = 'Image';
|
|
|
|
node.appendChild(linkText);
|
2021-06-26 18:40:37 +00:00
|
|
|
|
|
|
|
node.dataset.path = path;
|
|
|
|
node.dataset.dictionary = dictionary;
|
|
|
|
node.dataset.imageLoadState = 'not-loaded';
|
|
|
|
node.dataset.hasAspectRatio = 'true';
|
|
|
|
node.dataset.imageRendering = typeof imageRendering === 'string' ? imageRendering : (pixelated ? 'pixelated' : 'auto');
|
|
|
|
node.dataset.appearance = typeof appearance === 'string' ? appearance : 'auto';
|
|
|
|
node.dataset.background = typeof background === 'boolean' ? `${background}` : 'true';
|
|
|
|
node.dataset.collapsed = typeof collapsed === 'boolean' ? `${collapsed}` : 'false';
|
|
|
|
node.dataset.collapsible = typeof collapsible === 'boolean' ? `${collapsible}` : 'true';
|
|
|
|
if (typeof verticalAlign === 'string') {
|
|
|
|
node.dataset.verticalAlign = verticalAlign;
|
|
|
|
}
|
|
|
|
if (typeof sizeUnits === 'string' && (hasPreferredWidth || hasPreferredHeight)) {
|
|
|
|
node.dataset.sizeUnits = sizeUnits;
|
|
|
|
}
|
|
|
|
|
|
|
|
imageContainer.style.width = `${usedWidth}em`;
|
|
|
|
if (typeof title === 'string') {
|
|
|
|
imageContainer.title = title;
|
|
|
|
}
|
|
|
|
|
2021-06-29 01:46:54 +00:00
|
|
|
aspectRatioSizer.style.paddingTop = `${invAspectRatio * 100.0}%`;
|
2021-06-26 18:40:37 +00:00
|
|
|
|
2022-03-15 01:42:33 +00:00
|
|
|
if (this._contentManager !== null) {
|
|
|
|
this._contentManager.loadMedia(
|
2021-06-26 18:40:37 +00:00
|
|
|
path,
|
|
|
|
dictionary,
|
|
|
|
(url) => this._setImageData(node, image, imageBackground, url, false),
|
|
|
|
() => this._setImageData(node, image, imageBackground, null, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2022-05-14 22:12:57 +00:00
|
|
|
_appendStructuredContent(container, content, dictionary, language) {
|
|
|
|
if (typeof content === 'string') {
|
|
|
|
if (content.length > 0) {
|
|
|
|
container.appendChild(this._createTextNode(content));
|
|
|
|
if (language === null && this._japaneseUtil.isStringPartiallyJapanese(content)) {
|
|
|
|
container.lang = 'ja';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(typeof content === 'object' && content !== null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Array.isArray(content)) {
|
|
|
|
for (const item of content) {
|
|
|
|
this._appendStructuredContent(container, item, dictionary, language);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const node = this._createStructuredContentGenericElement(content, dictionary, language);
|
|
|
|
if (node !== null) {
|
|
|
|
container.appendChild(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 19:49:23 +00:00
|
|
|
_createElement(tagName, className) {
|
|
|
|
const node = this._document.createElement(tagName);
|
|
|
|
node.className = className;
|
|
|
|
return node;
|
2021-06-26 18:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_createTextNode(data) {
|
|
|
|
return this._document.createTextNode(data);
|
|
|
|
}
|
|
|
|
|
2021-09-04 16:43:56 +00:00
|
|
|
_setElementDataset(element, data) {
|
|
|
|
for (let [key, value] of Object.entries(data)) {
|
|
|
|
if (key.length > 0) {
|
|
|
|
key = `${key[0].toUpperCase()}${key.substring(1)}`;
|
|
|
|
}
|
|
|
|
key = `sc${key}`;
|
2022-05-14 22:16:13 +00:00
|
|
|
try {
|
|
|
|
element.dataset[key] = value;
|
|
|
|
} catch (e) {
|
|
|
|
// DOMException if key is malformed
|
|
|
|
}
|
2021-09-04 16:43:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:40:37 +00:00
|
|
|
_setImageData(node, image, imageBackground, url, unloaded) {
|
|
|
|
if (url !== null) {
|
|
|
|
image.src = url;
|
|
|
|
node.href = url;
|
|
|
|
node.dataset.imageLoadState = 'loaded';
|
|
|
|
imageBackground.style.setProperty('--image', `url("${url}")`);
|
|
|
|
} else {
|
|
|
|
image.removeAttribute('src');
|
|
|
|
node.removeAttribute('href');
|
|
|
|
node.dataset.imageLoadState = unloaded ? 'unloaded' : 'load-error';
|
|
|
|
imageBackground.style.removeProperty('--image');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 22:12:57 +00:00
|
|
|
_createStructuredContentGenericElement(content, dictionary, language) {
|
|
|
|
const {tag} = content;
|
|
|
|
switch (tag) {
|
|
|
|
case 'br':
|
|
|
|
return this._createStructuredContentElement(tag, content, dictionary, language, 'simple', false, false);
|
|
|
|
case 'ruby':
|
|
|
|
case 'rt':
|
|
|
|
case 'rp':
|
|
|
|
return this._createStructuredContentElement(tag, content, dictionary, language, 'simple', true, false);
|
|
|
|
case 'table':
|
|
|
|
return this._createStructuredContentTableElement(tag, content, dictionary, language);
|
|
|
|
case 'thead':
|
|
|
|
case 'tbody':
|
|
|
|
case 'tfoot':
|
|
|
|
case 'tr':
|
|
|
|
return this._createStructuredContentElement(tag, content, dictionary, language, 'table', true, false);
|
|
|
|
case 'th':
|
|
|
|
case 'td':
|
|
|
|
return this._createStructuredContentElement(tag, content, dictionary, language, 'table-cell', true, true);
|
|
|
|
case 'div':
|
|
|
|
case 'span':
|
|
|
|
case 'ol':
|
|
|
|
case 'ul':
|
|
|
|
case 'li':
|
|
|
|
return this._createStructuredContentElement(tag, content, dictionary, language, 'simple', true, true);
|
|
|
|
case 'img':
|
|
|
|
return this.createDefinitionImage(content, dictionary);
|
|
|
|
case 'a':
|
|
|
|
return this._createLinkElement(content, dictionary, language);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createStructuredContentTableElement(tag, content, dictionary, language) {
|
2021-06-26 19:49:23 +00:00
|
|
|
const container = this._createElement('div', 'gloss-sc-table-container');
|
2022-05-14 22:12:57 +00:00
|
|
|
const table = this._createStructuredContentElement(tag, content, dictionary, language, 'table', true, false);
|
2021-06-26 18:40:37 +00:00
|
|
|
container.appendChild(table);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2022-05-14 22:12:57 +00:00
|
|
|
_createStructuredContentElement(tag, content, dictionary, language, type, hasChildren, hasStyle) {
|
2021-06-26 19:49:23 +00:00
|
|
|
const node = this._createElement(tag, `gloss-sc-${tag}`);
|
Add new structured content features: lists and the HTML `lang` attribute (#2129)
* Add support for structured content lists and `list-style-type` style
A full list of supported style types is documented here:
https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type
There's nothing in this code preventing a term bank from assigning,
for example, a `list-style-type` style to a `div` element, but it
doesn't seem like browsers will complain about things like that.
* Add support for `lang` attribute in structured content
Support added for the following node types:
"ruby", "rt", "rp", "table", "thead", "tbody", "tfoot", "tr", "td",
"th", "span", "div", "ol", "ul", "li", "a"
I couldn't get it to work for the alt-hover text on "img" tags.
Tests are included in the file
"test/data/dictionaries/valid-dictionary/term_bank_1.json"
* Add styles for structured content lists
* Add override rules for new structured-content list styles
see: https://github.com/FooSoft/yomichan/pull/2129
Co-authored-by: stephenmk <stephenmk@users.noreply.github.com>
2022-05-14 13:59:38 +00:00
|
|
|
const {data, lang} = content;
|
2021-09-04 16:43:56 +00:00
|
|
|
if (typeof data === 'object' && data !== null) { this._setElementDataset(node, data); }
|
2022-05-14 22:12:57 +00:00
|
|
|
if (typeof lang === 'string') {
|
|
|
|
node.lang = lang;
|
|
|
|
language = lang;
|
|
|
|
}
|
2021-06-26 18:40:37 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'table-cell':
|
|
|
|
{
|
|
|
|
const {colSpan, rowSpan} = content;
|
|
|
|
if (typeof colSpan === 'number') { node.colSpan = colSpan; }
|
|
|
|
if (typeof rowSpan === 'number') { node.rowSpan = rowSpan; }
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (hasStyle) {
|
|
|
|
const {style} = content;
|
|
|
|
if (typeof style === 'object' && style !== null) {
|
|
|
|
this._setStructuredContentElementStyle(node, style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasChildren) {
|
2022-05-14 22:12:57 +00:00
|
|
|
this._appendStructuredContent(node, content.content, dictionary, language);
|
2021-06-26 18:40:37 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setStructuredContentElementStyle(node, contentStyle) {
|
|
|
|
const {style} = node;
|
2021-09-01 01:08:30 +00:00
|
|
|
const {
|
|
|
|
fontStyle,
|
|
|
|
fontWeight,
|
|
|
|
fontSize,
|
|
|
|
textDecorationLine,
|
|
|
|
verticalAlign,
|
|
|
|
marginTop,
|
|
|
|
marginLeft,
|
|
|
|
marginRight,
|
Add new structured content features: lists and the HTML `lang` attribute (#2129)
* Add support for structured content lists and `list-style-type` style
A full list of supported style types is documented here:
https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type
There's nothing in this code preventing a term bank from assigning,
for example, a `list-style-type` style to a `div` element, but it
doesn't seem like browsers will complain about things like that.
* Add support for `lang` attribute in structured content
Support added for the following node types:
"ruby", "rt", "rp", "table", "thead", "tbody", "tfoot", "tr", "td",
"th", "span", "div", "ol", "ul", "li", "a"
I couldn't get it to work for the alt-hover text on "img" tags.
Tests are included in the file
"test/data/dictionaries/valid-dictionary/term_bank_1.json"
* Add styles for structured content lists
* Add override rules for new structured-content list styles
see: https://github.com/FooSoft/yomichan/pull/2129
Co-authored-by: stephenmk <stephenmk@users.noreply.github.com>
2022-05-14 13:59:38 +00:00
|
|
|
marginBottom,
|
|
|
|
listStyleType
|
2021-09-01 01:08:30 +00:00
|
|
|
} = contentStyle;
|
2021-06-26 18:40:37 +00:00
|
|
|
if (typeof fontStyle === 'string') { style.fontStyle = fontStyle; }
|
|
|
|
if (typeof fontWeight === 'string') { style.fontWeight = fontWeight; }
|
|
|
|
if (typeof fontSize === 'string') { style.fontSize = fontSize; }
|
|
|
|
if (typeof verticalAlign === 'string') { style.verticalAlign = verticalAlign; }
|
|
|
|
if (typeof textDecorationLine === 'string') {
|
|
|
|
style.textDecoration = textDecorationLine;
|
|
|
|
} else if (Array.isArray(textDecorationLine)) {
|
|
|
|
style.textDecoration = textDecorationLine.join(' ');
|
|
|
|
}
|
2021-09-01 01:08:30 +00:00
|
|
|
if (typeof marginTop === 'number') { style.marginTop = `${marginTop}em`; }
|
|
|
|
if (typeof marginLeft === 'number') { style.marginLeft = `${marginLeft}em`; }
|
|
|
|
if (typeof marginRight === 'number') { style.marginRight = `${marginRight}em`; }
|
|
|
|
if (typeof marginBottom === 'number') { style.marginBottom = `${marginBottom}em`; }
|
Add new structured content features: lists and the HTML `lang` attribute (#2129)
* Add support for structured content lists and `list-style-type` style
A full list of supported style types is documented here:
https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type
There's nothing in this code preventing a term bank from assigning,
for example, a `list-style-type` style to a `div` element, but it
doesn't seem like browsers will complain about things like that.
* Add support for `lang` attribute in structured content
Support added for the following node types:
"ruby", "rt", "rp", "table", "thead", "tbody", "tfoot", "tr", "td",
"th", "span", "div", "ol", "ul", "li", "a"
I couldn't get it to work for the alt-hover text on "img" tags.
Tests are included in the file
"test/data/dictionaries/valid-dictionary/term_bank_1.json"
* Add styles for structured content lists
* Add override rules for new structured-content list styles
see: https://github.com/FooSoft/yomichan/pull/2129
Co-authored-by: stephenmk <stephenmk@users.noreply.github.com>
2022-05-14 13:59:38 +00:00
|
|
|
if (typeof listStyleType === 'string') { style.listStyleType = listStyleType; }
|
2021-06-26 18:40:37 +00:00
|
|
|
}
|
2022-03-17 23:01:59 +00:00
|
|
|
|
2022-05-14 22:12:57 +00:00
|
|
|
_createLinkElement(content, dictionary, language) {
|
2022-03-17 23:01:59 +00:00
|
|
|
let {href} = content;
|
|
|
|
const internal = href.startsWith('?');
|
|
|
|
if (internal) {
|
|
|
|
href = `${location.protocol}//${location.host}/search.html${href.length > 1 ? href : ''}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const node = this._createElement('a', 'gloss-link');
|
|
|
|
node.dataset.external = `${!internal}`;
|
|
|
|
|
|
|
|
const text = this._createElement('span', 'gloss-link-text');
|
|
|
|
node.appendChild(text);
|
|
|
|
|
Add new structured content features: lists and the HTML `lang` attribute (#2129)
* Add support for structured content lists and `list-style-type` style
A full list of supported style types is documented here:
https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type
There's nothing in this code preventing a term bank from assigning,
for example, a `list-style-type` style to a `div` element, but it
doesn't seem like browsers will complain about things like that.
* Add support for `lang` attribute in structured content
Support added for the following node types:
"ruby", "rt", "rp", "table", "thead", "tbody", "tfoot", "tr", "td",
"th", "span", "div", "ol", "ul", "li", "a"
I couldn't get it to work for the alt-hover text on "img" tags.
Tests are included in the file
"test/data/dictionaries/valid-dictionary/term_bank_1.json"
* Add styles for structured content lists
* Add override rules for new structured-content list styles
see: https://github.com/FooSoft/yomichan/pull/2129
Co-authored-by: stephenmk <stephenmk@users.noreply.github.com>
2022-05-14 13:59:38 +00:00
|
|
|
const {lang} = content;
|
2022-05-14 22:12:57 +00:00
|
|
|
if (typeof lang === 'string') {
|
|
|
|
node.lang = lang;
|
|
|
|
language = lang;
|
|
|
|
}
|
Add new structured content features: lists and the HTML `lang` attribute (#2129)
* Add support for structured content lists and `list-style-type` style
A full list of supported style types is documented here:
https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type
There's nothing in this code preventing a term bank from assigning,
for example, a `list-style-type` style to a `div` element, but it
doesn't seem like browsers will complain about things like that.
* Add support for `lang` attribute in structured content
Support added for the following node types:
"ruby", "rt", "rp", "table", "thead", "tbody", "tfoot", "tr", "td",
"th", "span", "div", "ol", "ul", "li", "a"
I couldn't get it to work for the alt-hover text on "img" tags.
Tests are included in the file
"test/data/dictionaries/valid-dictionary/term_bank_1.json"
* Add styles for structured content lists
* Add override rules for new structured-content list styles
see: https://github.com/FooSoft/yomichan/pull/2129
Co-authored-by: stephenmk <stephenmk@users.noreply.github.com>
2022-05-14 13:59:38 +00:00
|
|
|
|
2022-05-14 22:12:57 +00:00
|
|
|
this._appendStructuredContent(text, content.content, dictionary, language);
|
2022-03-17 23:01:59 +00:00
|
|
|
|
|
|
|
if (!internal) {
|
|
|
|
const icon = this._createElement('span', 'gloss-link-external-icon icon');
|
|
|
|
icon.dataset.icon = 'external-link';
|
|
|
|
node.appendChild(icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._contentManager.prepareLink(node, href, internal);
|
|
|
|
return node;
|
|
|
|
}
|
2021-06-26 18:40:37 +00:00
|
|
|
}
|