yomitan-core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +485 -0
- package/dist/anki-connect-BQyCGW3O.cjs +513 -0
- package/dist/anki-connect-BQyCGW3O.cjs.map +1 -0
- package/dist/anki-connect-CPPuhyiQ.js +6 -0
- package/dist/anki-connect-DbrQHphS.js +495 -0
- package/dist/anki-connect-DbrQHphS.js.map +1 -0
- package/dist/anki-connect-DcheJrp-.cjs +6 -0
- package/dist/anki.cjs +1758 -0
- package/dist/anki.cjs.map +1 -0
- package/dist/anki.d.cts +751 -0
- package/dist/anki.d.cts.map +1 -0
- package/dist/anki.d.ts +751 -0
- package/dist/anki.d.ts.map +1 -0
- package/dist/anki.js +1751 -0
- package/dist/anki.js.map +1 -0
- package/dist/audio-D9DvYyB7.d.cts +48 -0
- package/dist/audio-D9DvYyB7.d.cts.map +1 -0
- package/dist/audio-DQulUkDM.d.ts +48 -0
- package/dist/audio-DQulUkDM.d.ts.map +1 -0
- package/dist/audio-url-generator-BXvQaqUi.cjs +4 -0
- package/dist/audio-url-generator-Dy2hb2Mm.cjs +414 -0
- package/dist/audio-url-generator-Dy2hb2Mm.cjs.map +1 -0
- package/dist/audio-url-generator-Qi0rfzHz.js +4 -0
- package/dist/audio-url-generator-pFQAB5Nb.js +390 -0
- package/dist/audio-url-generator-pFQAB5Nb.js.map +1 -0
- package/dist/audio.cjs +7 -0
- package/dist/audio.d.cts +86 -0
- package/dist/audio.d.cts.map +1 -0
- package/dist/audio.d.ts +86 -0
- package/dist/audio.d.ts.map +1 -0
- package/dist/audio.js +4 -0
- package/dist/batch-processor-BR-gB3H3.js +84 -0
- package/dist/batch-processor-BR-gB3H3.js.map +1 -0
- package/dist/batch-processor-CSF1acTw.cjs +3 -0
- package/dist/batch-processor-DFqM_L-_.cjs +91 -0
- package/dist/batch-processor-DFqM_L-_.cjs.map +1 -0
- package/dist/batch-processor-Quo9jUyf.js +3 -0
- package/dist/chunk-BCwAaXi7.cjs +31 -0
- package/dist/cjk-util-Dp0ZU0sh.cjs +167 -0
- package/dist/cjk-util-Dp0ZU0sh.cjs.map +1 -0
- package/dist/cjk-util-DubXBGDG.js +94 -0
- package/dist/cjk-util-DubXBGDG.js.map +1 -0
- package/dist/core-BUpclilG.d.cts +102 -0
- package/dist/core-BUpclilG.d.cts.map +1 -0
- package/dist/core-DFUj5GtA.d.ts +102 -0
- package/dist/core-DFUj5GtA.d.ts.map +1 -0
- package/dist/database.cjs +7 -0
- package/dist/database.d.cts +4 -0
- package/dist/database.d.ts +4 -0
- package/dist/database.js +5 -0
- package/dist/dictionary-D7l-qFt1.d.cts +316 -0
- package/dist/dictionary-D7l-qFt1.d.cts.map +1 -0
- package/dist/dictionary-_vzfBLWi.d.ts +316 -0
- package/dist/dictionary-_vzfBLWi.d.ts.map +1 -0
- package/dist/dictionary-data-util-CHnRdYZ9.cjs +378 -0
- package/dist/dictionary-data-util-CHnRdYZ9.cjs.map +1 -0
- package/dist/dictionary-data-util-CfOLfEDE.js +323 -0
- package/dist/dictionary-data-util-CfOLfEDE.js.map +1 -0
- package/dist/dictionary-database-BDC2f9zc.d.ts +58 -0
- package/dist/dictionary-database-BDC2f9zc.d.ts.map +1 -0
- package/dist/dictionary-database-CU4TsvCC.js +393 -0
- package/dist/dictionary-database-CU4TsvCC.js.map +1 -0
- package/dist/dictionary-database-DsOi04Sg.d.cts +58 -0
- package/dist/dictionary-database-DsOi04Sg.d.cts.map +1 -0
- package/dist/dictionary-database-lvFvftnO.cjs +412 -0
- package/dist/dictionary-database-lvFvftnO.cjs.map +1 -0
- package/dist/dictionary-importer-BkQQSBhm.d.ts +237 -0
- package/dist/dictionary-importer-BkQQSBhm.d.ts.map +1 -0
- package/dist/dictionary-importer-Cen1z6co.js +1821 -0
- package/dist/dictionary-importer-Cen1z6co.js.map +1 -0
- package/dist/dictionary-importer-DYmmWmcX.cjs +8 -0
- package/dist/dictionary-importer-Da3AuTZw.d.cts +237 -0
- package/dist/dictionary-importer-Da3AuTZw.d.cts.map +1 -0
- package/dist/dictionary-importer-Dhn75iZ4.cjs +1834 -0
- package/dist/dictionary-importer-Dhn75iZ4.cjs.map +1 -0
- package/dist/dictionary-importer-xWkel0h-.js +8 -0
- package/dist/dictionary-update-checker-BNE4pGTx.js +4 -0
- package/dist/dictionary-update-checker-Byjvifd2.cjs +75 -0
- package/dist/dictionary-update-checker-Byjvifd2.cjs.map +1 -0
- package/dist/dictionary-update-checker-YdpalZ41.cjs +4 -0
- package/dist/dictionary-update-checker-kKukiovj.js +69 -0
- package/dist/dictionary-update-checker-kKukiovj.js.map +1 -0
- package/dist/display-generator-BGVWiI0t.js +746 -0
- package/dist/display-generator-BGVWiI0t.js.map +1 -0
- package/dist/display-generator-BMQmG5Ov.cjs +9 -0
- package/dist/display-generator-BxZ7mBjP.js +9 -0
- package/dist/display-generator-DyP-HNzP.cjs +758 -0
- package/dist/display-generator-DyP-HNzP.cjs.map +1 -0
- package/dist/errors-BSezaJwm.cjs +35 -0
- package/dist/errors-BSezaJwm.cjs.map +1 -0
- package/dist/errors-DuuDSO5N.js +22 -0
- package/dist/errors-DuuDSO5N.js.map +1 -0
- package/dist/frequency-ranking-BXjfhhUQ.js +3 -0
- package/dist/frequency-ranking-Cx1kkIrw.cjs +3 -0
- package/dist/frequency-ranking-DEJMTMdg.js +159 -0
- package/dist/frequency-ranking-DEJMTMdg.js.map +1 -0
- package/dist/frequency-ranking-DVYxTXN-.cjs +166 -0
- package/dist/frequency-ranking-DVYxTXN-.cjs.map +1 -0
- package/dist/furigana-5HK97CY8.js +4 -0
- package/dist/furigana-9bBI9-qe.d.ts +47 -0
- package/dist/furigana-9bBI9-qe.d.ts.map +1 -0
- package/dist/furigana-B3-0y231.js +471 -0
- package/dist/furigana-B3-0y231.js.map +1 -0
- package/dist/furigana-CjOhzvZt.d.cts +47 -0
- package/dist/furigana-CjOhzvZt.d.cts.map +1 -0
- package/dist/furigana-DpZLcues.cjs +609 -0
- package/dist/furigana-DpZLcues.cjs.map +1 -0
- package/dist/furigana-h3v2ub4-.cjs +4 -0
- package/dist/import.cjs +12 -0
- package/dist/import.d.cts +107 -0
- package/dist/import.d.cts.map +1 -0
- package/dist/import.d.ts +107 -0
- package/dist/import.d.ts.map +1 -0
- package/dist/import.js +9 -0
- package/dist/index.cjs +275 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +211 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +211 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +238 -0
- package/dist/index.js.map +1 -0
- package/dist/json-DGd-cunA.js +17 -0
- package/dist/json-DGd-cunA.js.map +1 -0
- package/dist/json-DKWp-B7Y.cjs +30 -0
- package/dist/json-DKWp-B7Y.cjs.map +1 -0
- package/dist/language-KN_u-nTR.d.ts +104 -0
- package/dist/language-KN_u-nTR.d.ts.map +1 -0
- package/dist/language-xAbQxgXc.d.cts +104 -0
- package/dist/language-xAbQxgXc.d.cts.map +1 -0
- package/dist/language.cjs +15626 -0
- package/dist/language.cjs.map +1 -0
- package/dist/language.d.cts +959 -0
- package/dist/language.d.cts.map +1 -0
- package/dist/language.d.ts +959 -0
- package/dist/language.d.ts.map +1 -0
- package/dist/language.js +15522 -0
- package/dist/language.js.map +1 -0
- package/dist/log-D8KtR3aP.cjs +67 -0
- package/dist/log-D8KtR3aP.cjs.map +1 -0
- package/dist/log-hgSll-dS.js +60 -0
- package/dist/log-hgSll-dS.js.map +1 -0
- package/dist/lookup.cjs +13 -0
- package/dist/lookup.d.cts +161 -0
- package/dist/lookup.d.cts.map +1 -0
- package/dist/lookup.d.ts +161 -0
- package/dist/lookup.d.ts.map +1 -0
- package/dist/lookup.js +10 -0
- package/dist/media-loader-BABA_E4W.js +3 -0
- package/dist/media-loader-Ce9cuANS.cjs +21 -0
- package/dist/media-loader-Ce9cuANS.cjs.map +1 -0
- package/dist/media-loader-qRti-Q6h.js +14 -0
- package/dist/media-loader-qRti-Q6h.js.map +1 -0
- package/dist/media-loader-xlUGaJrx.cjs +3 -0
- package/dist/multi-language-transformer-AlxOM6b3.js +637 -0
- package/dist/multi-language-transformer-AlxOM6b3.js.map +1 -0
- package/dist/multi-language-transformer-MdbQBBOt.cjs +685 -0
- package/dist/multi-language-transformer-MdbQBBOt.cjs.map +1 -0
- package/dist/multi-language-transformer-SEhcJXEB.d.ts +63 -0
- package/dist/multi-language-transformer-SEhcJXEB.d.ts.map +1 -0
- package/dist/multi-language-transformer-Ul9mbRce.d.cts +63 -0
- package/dist/multi-language-transformer-Ul9mbRce.d.cts.map +1 -0
- package/dist/pronunciation-generator-BtBc4q_V.js +397 -0
- package/dist/pronunciation-generator-BtBc4q_V.js.map +1 -0
- package/dist/pronunciation-generator-CBYdXYou.js +4 -0
- package/dist/pronunciation-generator-CFbZlf5J.cjs +445 -0
- package/dist/pronunciation-generator-CFbZlf5J.cjs.map +1 -0
- package/dist/pronunciation-generator-DOz9hEuk.cjs +4 -0
- package/dist/render.cjs +2796 -0
- package/dist/render.cjs.map +1 -0
- package/dist/render.d.cts +424 -0
- package/dist/render.d.cts.map +1 -0
- package/dist/render.d.ts +424 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +2777 -0
- package/dist/render.js.map +1 -0
- package/dist/sentence-parser-BPAJNzqW.js +126 -0
- package/dist/sentence-parser-BPAJNzqW.js.map +1 -0
- package/dist/sentence-parser-BVIOI64h.cjs +132 -0
- package/dist/sentence-parser-BVIOI64h.cjs.map +1 -0
- package/dist/sentence-parser-BoHO3cHn.js +5 -0
- package/dist/sentence-parser-DQVLSW0z.cjs +5 -0
- package/dist/structured-content-generator-BtOApkTW.cjs +4 -0
- package/dist/structured-content-generator-Bx62RYa8.js +4 -0
- package/dist/structured-content-generator-CLnybumI.js +276 -0
- package/dist/structured-content-generator-CLnybumI.js.map +1 -0
- package/dist/structured-content-generator-DrwkB0-k.cjs +282 -0
- package/dist/structured-content-generator-DrwkB0-k.cjs.map +1 -0
- package/dist/text-utilities-B7PIythe.js +8 -0
- package/dist/text-utilities-B7PIythe.js.map +1 -0
- package/dist/text-utilities-Del2Ivkg.cjs +15 -0
- package/dist/text-utilities-Del2Ivkg.cjs.map +1 -0
- package/dist/translator-CRPlPzqi.cjs +1545 -0
- package/dist/translator-CRPlPzqi.cjs.map +1 -0
- package/dist/translator-CWgG5drA.js +1539 -0
- package/dist/translator-CWgG5drA.js.map +1 -0
- package/dist/translator-CaGtJvnQ.cjs +6 -0
- package/dist/translator-Cc6OGxrW.d.ts +180 -0
- package/dist/translator-Cc6OGxrW.d.ts.map +1 -0
- package/dist/translator-CcA-s-W4.d.cts +180 -0
- package/dist/translator-CcA-s-W4.d.cts.map +1 -0
- package/dist/translator-CuJOTK6l.js +6 -0
- package/dist/utilities-C-lbZaJE.cjs +52 -0
- package/dist/utilities-C-lbZaJE.cjs.map +1 -0
- package/dist/utilities-bi3EF-q5.js +33 -0
- package/dist/utilities-bi3EF-q5.js.map +1 -0
- package/package.json +102 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { getLanguageFromText } from "./text-utilities-B7PIythe.js";
|
|
2
|
+
|
|
3
|
+
//#region src/render/structured-content-generator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generates DOM elements from structured content definitions found in
|
|
6
|
+
* dictionary glossary entries. Handles images, links, tables, styled
|
|
7
|
+
* elements, and other structured content types.
|
|
8
|
+
*/
|
|
9
|
+
var StructuredContentGenerator = class {
|
|
10
|
+
_contentManager;
|
|
11
|
+
_document;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new StructuredContentGenerator.
|
|
14
|
+
* @param contentManager - The content manager for loading media resources.
|
|
15
|
+
* @param doc - The Document object to use for creating DOM elements.
|
|
16
|
+
*/
|
|
17
|
+
constructor(contentManager, doc) {
|
|
18
|
+
this._contentManager = contentManager;
|
|
19
|
+
this._document = doc;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Appends structured content to an existing node, adding the 'structured-content' class.
|
|
23
|
+
* @param node - The container element to append content to.
|
|
24
|
+
* @param content - The structured content to render.
|
|
25
|
+
* @param dictionary - The dictionary name for resolving media references.
|
|
26
|
+
*/
|
|
27
|
+
appendStructuredContent(node, content, dictionary) {
|
|
28
|
+
node.classList.add("structured-content");
|
|
29
|
+
this._appendStructuredContent(node, content, dictionary, null);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new span element containing the rendered structured content.
|
|
33
|
+
* @param content - The structured content to render.
|
|
34
|
+
* @param dictionary - The dictionary name for resolving media references.
|
|
35
|
+
* @returns A span element with class 'structured-content'.
|
|
36
|
+
*/
|
|
37
|
+
createStructuredContent(content, dictionary) {
|
|
38
|
+
const node = this._createElement("span", "structured-content");
|
|
39
|
+
this._appendStructuredContent(node, content, dictionary, null);
|
|
40
|
+
return node;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates an image element for a definition, wrapped in an anchor tag.
|
|
44
|
+
* @param data - The image data from the dictionary.
|
|
45
|
+
* @param dictionary - The dictionary name for resolving the image path.
|
|
46
|
+
* @returns An anchor element containing the image.
|
|
47
|
+
*/
|
|
48
|
+
createDefinitionImage(data, dictionary) {
|
|
49
|
+
const { path, width = 100, height = 100, preferredWidth, preferredHeight, title, pixelated, imageRendering, appearance, background, collapsed, collapsible } = data;
|
|
50
|
+
const verticalAlign = "verticalAlign" in data ? data.verticalAlign : void 0;
|
|
51
|
+
const border = "border" in data ? data.border : void 0;
|
|
52
|
+
const borderRadius = "borderRadius" in data ? data.borderRadius : void 0;
|
|
53
|
+
const sizeUnits = "sizeUnits" in data ? data.sizeUnits : void 0;
|
|
54
|
+
const hasPreferredWidth = typeof preferredWidth === "number";
|
|
55
|
+
const hasPreferredHeight = typeof preferredHeight === "number";
|
|
56
|
+
const invAspectRatio = hasPreferredWidth && hasPreferredHeight ? preferredHeight / preferredWidth : height / width;
|
|
57
|
+
const usedWidth = hasPreferredWidth ? preferredWidth : hasPreferredHeight ? preferredHeight / invAspectRatio : width;
|
|
58
|
+
const node = this._createElement("a", "gloss-image-link");
|
|
59
|
+
node.target = "_blank";
|
|
60
|
+
node.rel = "noreferrer noopener";
|
|
61
|
+
const imageContainer = this._createElement("span", "gloss-image-container");
|
|
62
|
+
node.appendChild(imageContainer);
|
|
63
|
+
const aspectRatioSizer = this._createElement("span", "gloss-image-sizer");
|
|
64
|
+
imageContainer.appendChild(aspectRatioSizer);
|
|
65
|
+
const imageBackground = this._createElement("span", "gloss-image-background");
|
|
66
|
+
imageContainer.appendChild(imageBackground);
|
|
67
|
+
const overlay = this._createElement("span", "gloss-image-container-overlay");
|
|
68
|
+
imageContainer.appendChild(overlay);
|
|
69
|
+
const linkText = this._createElement("span", "gloss-image-link-text");
|
|
70
|
+
linkText.textContent = "Image";
|
|
71
|
+
node.appendChild(linkText);
|
|
72
|
+
node.dataset.path = path;
|
|
73
|
+
node.dataset.dictionary = dictionary;
|
|
74
|
+
node.dataset.imageLoadState = "not-loaded";
|
|
75
|
+
node.dataset.hasAspectRatio = "true";
|
|
76
|
+
node.dataset.imageRendering = typeof imageRendering === "string" ? imageRendering : pixelated ? "pixelated" : "auto";
|
|
77
|
+
node.dataset.appearance = typeof appearance === "string" ? appearance : "auto";
|
|
78
|
+
node.dataset.background = typeof background === "boolean" ? `${background}` : "true";
|
|
79
|
+
node.dataset.collapsed = typeof collapsed === "boolean" ? `${collapsed}` : "false";
|
|
80
|
+
node.dataset.collapsible = typeof collapsible === "boolean" ? `${collapsible}` : "true";
|
|
81
|
+
if (typeof verticalAlign === "string") node.dataset.verticalAlign = verticalAlign;
|
|
82
|
+
if (typeof sizeUnits === "string" && (hasPreferredWidth || hasPreferredHeight)) node.dataset.sizeUnits = sizeUnits;
|
|
83
|
+
aspectRatioSizer.style.paddingTop = `${invAspectRatio * 100}%`;
|
|
84
|
+
if (typeof border === "string") imageContainer.style.border = border;
|
|
85
|
+
if (typeof borderRadius === "string") imageContainer.style.borderRadius = borderRadius;
|
|
86
|
+
imageContainer.style.width = `${usedWidth}em`;
|
|
87
|
+
if (typeof title === "string") imageContainer.title = title;
|
|
88
|
+
const image = this._createElement("img", "gloss-image");
|
|
89
|
+
if (sizeUnits === "em" && (hasPreferredWidth || hasPreferredHeight)) {
|
|
90
|
+
image.style.width = `${usedWidth}em`;
|
|
91
|
+
image.style.height = `${usedWidth * invAspectRatio}em`;
|
|
92
|
+
}
|
|
93
|
+
image.width = usedWidth;
|
|
94
|
+
image.height = usedWidth * invAspectRatio;
|
|
95
|
+
image.style.width = "100%";
|
|
96
|
+
image.style.height = "100%";
|
|
97
|
+
imageContainer.appendChild(image);
|
|
98
|
+
const url = this._contentManager.loadMedia(path, dictionary, "image");
|
|
99
|
+
if (url) this._setImageData(node, image, imageBackground, url, false);
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
_appendStructuredContent(container, content, dictionary, language) {
|
|
103
|
+
if (typeof content === "string") {
|
|
104
|
+
if (content.length > 0) {
|
|
105
|
+
container.appendChild(this._createTextNode(content));
|
|
106
|
+
if (language === null) {
|
|
107
|
+
const language2 = getLanguageFromText(content, language);
|
|
108
|
+
if (language2 !== null) container.lang = language2;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!(typeof content === "object" && content !== null)) return;
|
|
114
|
+
if (Array.isArray(content)) {
|
|
115
|
+
for (const item of content) this._appendStructuredContent(container, item, dictionary, language);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const node = this._createStructuredContentGenericElement(content, dictionary, language);
|
|
119
|
+
if (node !== null) container.appendChild(node);
|
|
120
|
+
}
|
|
121
|
+
_createElement(tagName, className) {
|
|
122
|
+
const node = this._document.createElement(tagName);
|
|
123
|
+
node.className = className;
|
|
124
|
+
return node;
|
|
125
|
+
}
|
|
126
|
+
_createTextNode(data) {
|
|
127
|
+
return this._document.createTextNode(data);
|
|
128
|
+
}
|
|
129
|
+
_setElementDataset(element, data) {
|
|
130
|
+
for (let [key, value] of Object.entries(data)) {
|
|
131
|
+
if (key.length > 0) key = `${key[0].toUpperCase()}${key.substring(1)}`;
|
|
132
|
+
key = `sc${key}`;
|
|
133
|
+
try {
|
|
134
|
+
element.dataset[key] = value;
|
|
135
|
+
} catch (_e) {}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
_setImageData(node, image, imageBackground, url, unloaded) {
|
|
139
|
+
if (url !== null) {
|
|
140
|
+
image.src = url;
|
|
141
|
+
node.href = url;
|
|
142
|
+
node.dataset.imageLoadState = "loaded";
|
|
143
|
+
imageBackground.style.setProperty("--image", `url("${url}")`);
|
|
144
|
+
} else {
|
|
145
|
+
image.removeAttribute("src");
|
|
146
|
+
node.removeAttribute("href");
|
|
147
|
+
node.dataset.imageLoadState = unloaded ? "unloaded" : "load-error";
|
|
148
|
+
imageBackground.style.removeProperty("--image");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
_createStructuredContentGenericElement(content, dictionary, language) {
|
|
152
|
+
const { tag } = content;
|
|
153
|
+
switch (tag) {
|
|
154
|
+
case "br": return this._createStructuredContentElement(tag, content, dictionary, language, "simple", false, false);
|
|
155
|
+
case "ruby":
|
|
156
|
+
case "rt":
|
|
157
|
+
case "rp": return this._createStructuredContentElement(tag, content, dictionary, language, "simple", true, false);
|
|
158
|
+
case "table": return this._createStructuredContentTableElement(tag, content, dictionary, language);
|
|
159
|
+
case "thead":
|
|
160
|
+
case "tbody":
|
|
161
|
+
case "tfoot":
|
|
162
|
+
case "tr": return this._createStructuredContentElement(tag, content, dictionary, language, "table", true, false);
|
|
163
|
+
case "th":
|
|
164
|
+
case "td": return this._createStructuredContentElement(tag, content, dictionary, language, "table-cell", true, true);
|
|
165
|
+
case "div":
|
|
166
|
+
case "span":
|
|
167
|
+
case "ol":
|
|
168
|
+
case "ul":
|
|
169
|
+
case "li":
|
|
170
|
+
case "details":
|
|
171
|
+
case "summary": return this._createStructuredContentElement(tag, content, dictionary, language, "simple", true, true);
|
|
172
|
+
case "img": return this.createDefinitionImage(content, dictionary);
|
|
173
|
+
case "a": return this._createLinkElement(content, dictionary, language);
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
_createStructuredContentTableElement(tag, content, dictionary, language) {
|
|
178
|
+
const container = this._createElement("div", "gloss-sc-table-container");
|
|
179
|
+
const table = this._createStructuredContentElement(tag, content, dictionary, language, "table", true, false);
|
|
180
|
+
container.appendChild(table);
|
|
181
|
+
return container;
|
|
182
|
+
}
|
|
183
|
+
_createStructuredContentElement(tag, content, dictionary, language, type, hasChildren, hasStyle) {
|
|
184
|
+
const node = this._createElement(tag, `gloss-sc-${tag}`);
|
|
185
|
+
const { data, lang } = content;
|
|
186
|
+
if (typeof data === "object" && data !== null) this._setElementDataset(node, data);
|
|
187
|
+
if (typeof lang === "string") {
|
|
188
|
+
node.lang = lang;
|
|
189
|
+
language = lang;
|
|
190
|
+
}
|
|
191
|
+
switch (type) {
|
|
192
|
+
case "table-cell":
|
|
193
|
+
{
|
|
194
|
+
const cell = node;
|
|
195
|
+
const { colSpan, rowSpan } = content;
|
|
196
|
+
if (typeof colSpan === "number") cell.colSpan = colSpan;
|
|
197
|
+
if (typeof rowSpan === "number") cell.rowSpan = rowSpan;
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
if (hasStyle) {
|
|
202
|
+
const { style, title, open } = content;
|
|
203
|
+
if (typeof style === "object" && style !== null) this._setStructuredContentElementStyle(node, style);
|
|
204
|
+
if (typeof title === "string") node.title = title;
|
|
205
|
+
if (typeof open === "boolean" && open) node.setAttribute("open", "");
|
|
206
|
+
}
|
|
207
|
+
if (hasChildren) this._appendStructuredContent(node, content.content, dictionary, language);
|
|
208
|
+
return node;
|
|
209
|
+
}
|
|
210
|
+
_setStructuredContentElementStyle(node, contentStyle) {
|
|
211
|
+
const { style } = node;
|
|
212
|
+
const { fontStyle, fontWeight, fontSize, color, background, backgroundColor, textDecorationLine, textDecorationStyle, textDecorationColor, borderColor, borderStyle, borderRadius, borderWidth, clipPath, verticalAlign, textAlign, textEmphasis, textShadow, margin, marginTop, marginLeft, marginRight, marginBottom, padding, paddingTop, paddingLeft, paddingRight, paddingBottom, wordBreak, whiteSpace, cursor, listStyleType } = contentStyle;
|
|
213
|
+
if (typeof fontStyle === "string") style.fontStyle = fontStyle;
|
|
214
|
+
if (typeof fontWeight === "string") style.fontWeight = fontWeight;
|
|
215
|
+
if (typeof fontSize === "string") style.fontSize = fontSize;
|
|
216
|
+
if (typeof color === "string") style.color = color;
|
|
217
|
+
if (typeof background === "string") style.background = background;
|
|
218
|
+
if (typeof backgroundColor === "string") style.backgroundColor = backgroundColor;
|
|
219
|
+
if (typeof verticalAlign === "string") style.verticalAlign = verticalAlign;
|
|
220
|
+
if (typeof textAlign === "string") style.textAlign = textAlign;
|
|
221
|
+
if (typeof textEmphasis === "string") style.textEmphasis = textEmphasis;
|
|
222
|
+
if (typeof textShadow === "string") style.textShadow = textShadow;
|
|
223
|
+
if (typeof textDecorationLine === "string") style.textDecoration = textDecorationLine;
|
|
224
|
+
else if (Array.isArray(textDecorationLine)) style.textDecoration = textDecorationLine.join(" ");
|
|
225
|
+
if (typeof textDecorationStyle === "string") style.textDecorationStyle = textDecorationStyle;
|
|
226
|
+
if (typeof textDecorationColor === "string") style.textDecorationColor = textDecorationColor;
|
|
227
|
+
if (typeof borderColor === "string") style.borderColor = borderColor;
|
|
228
|
+
if (typeof borderStyle === "string") style.borderStyle = borderStyle;
|
|
229
|
+
if (typeof borderRadius === "string") style.borderRadius = borderRadius;
|
|
230
|
+
if (typeof borderWidth === "string") style.borderWidth = borderWidth;
|
|
231
|
+
if (typeof clipPath === "string") style.clipPath = clipPath;
|
|
232
|
+
if (typeof margin === "string") style.margin = margin;
|
|
233
|
+
if (typeof marginTop === "number") style.marginTop = `${marginTop}em`;
|
|
234
|
+
if (typeof marginTop === "string") style.marginTop = marginTop;
|
|
235
|
+
if (typeof marginLeft === "number") style.marginLeft = `${marginLeft}em`;
|
|
236
|
+
if (typeof marginLeft === "string") style.marginLeft = marginLeft;
|
|
237
|
+
if (typeof marginRight === "number") style.marginRight = `${marginRight}em`;
|
|
238
|
+
if (typeof marginRight === "string") style.marginRight = marginRight;
|
|
239
|
+
if (typeof marginBottom === "number") style.marginBottom = `${marginBottom}em`;
|
|
240
|
+
if (typeof marginBottom === "string") style.marginBottom = marginBottom;
|
|
241
|
+
if (typeof padding === "string") style.padding = padding;
|
|
242
|
+
if (typeof paddingTop === "string") style.paddingTop = paddingTop;
|
|
243
|
+
if (typeof paddingLeft === "string") style.paddingLeft = paddingLeft;
|
|
244
|
+
if (typeof paddingRight === "string") style.paddingRight = paddingRight;
|
|
245
|
+
if (typeof paddingBottom === "string") style.paddingBottom = paddingBottom;
|
|
246
|
+
if (typeof wordBreak === "string") style.wordBreak = wordBreak;
|
|
247
|
+
if (typeof whiteSpace === "string") style.whiteSpace = whiteSpace;
|
|
248
|
+
if (typeof cursor === "string") style.cursor = cursor;
|
|
249
|
+
if (typeof listStyleType === "string") style.listStyleType = listStyleType;
|
|
250
|
+
}
|
|
251
|
+
_createLinkElement(content, dictionary, language) {
|
|
252
|
+
const { href } = content;
|
|
253
|
+
const internal = href.startsWith("?");
|
|
254
|
+
const node = this._createElement("a", "gloss-link");
|
|
255
|
+
node.dataset.external = `${!internal}`;
|
|
256
|
+
const text = this._createElement("span", "gloss-link-text");
|
|
257
|
+
node.appendChild(text);
|
|
258
|
+
const { lang } = content;
|
|
259
|
+
if (typeof lang === "string") {
|
|
260
|
+
node.lang = lang;
|
|
261
|
+
language = lang;
|
|
262
|
+
}
|
|
263
|
+
this._appendStructuredContent(text, content.content, dictionary, language);
|
|
264
|
+
if (!internal) {
|
|
265
|
+
const icon = this._createElement("span", "gloss-link-external-icon icon");
|
|
266
|
+
icon.dataset.icon = "external-link";
|
|
267
|
+
node.appendChild(icon);
|
|
268
|
+
}
|
|
269
|
+
this._contentManager.prepareLink(node, href, internal);
|
|
270
|
+
return node;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
//#endregion
|
|
275
|
+
export { StructuredContentGenerator };
|
|
276
|
+
//# sourceMappingURL=structured-content-generator-CLnybumI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-content-generator-CLnybumI.js","names":["contentManager: ContentManager","doc: Document","node: HTMLElement","content: StructuredContent.Content","dictionary: string","data: StructuredContent.ImageElement | DictionaryData.TermGlossaryImage","container: HTMLElement","content: StructuredContent.Content | undefined","language: string | null","tagName: string","className: string","data: string","element: HTMLElement","data: StructuredContent.Data","node: HTMLAnchorElement","image: HTMLImageElement","imageBackground: HTMLElement","url: string | null","unloaded: boolean","content: StructuredContent.Element","tag: string","content: StructuredContent.UnstyledElement","content:\n | StructuredContent.StyledElement\n | StructuredContent.UnstyledElement\n | StructuredContent.TableElement\n | StructuredContent.LineBreak","type: 'simple' | 'table' | 'table-cell'","hasChildren: boolean","hasStyle: boolean","contentStyle: StructuredContent.StructuredContentStyle","content: StructuredContent.LinkElement"],"sources":["../src/render/structured-content-generator.ts"],"sourcesContent":["/*\n * Copyright (C) 2023-2025 Yomitan Authors\n * Copyright (C) 2021-2022 Yomichan Authors\n *\n * This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see <https://www.gnu.org/licenses/>.\n */\n\nimport { getLanguageFromText } from '../language/text-utilities.js';\nimport type * as DictionaryData from '../types/dictionary-data.js';\nimport type * as StructuredContent from '../types/structured-content.js';\nimport type { ContentManager } from './content-manager.js';\n\n/**\n * Generates DOM elements from structured content definitions found in\n * dictionary glossary entries. Handles images, links, tables, styled\n * elements, and other structured content types.\n */\nexport class StructuredContentGenerator {\n private _contentManager: ContentManager;\n private _document: Document;\n\n /**\n * Creates a new StructuredContentGenerator.\n * @param contentManager - The content manager for loading media resources.\n * @param doc - The Document object to use for creating DOM elements.\n */\n constructor(contentManager: ContentManager, doc: Document) {\n this._contentManager = contentManager;\n this._document = doc;\n }\n\n /**\n * Appends structured content to an existing node, adding the 'structured-content' class.\n * @param node - The container element to append content to.\n * @param content - The structured content to render.\n * @param dictionary - The dictionary name for resolving media references.\n */\n appendStructuredContent(node: HTMLElement, content: StructuredContent.Content, dictionary: string): void {\n node.classList.add('structured-content');\n this._appendStructuredContent(node, content, dictionary, null);\n }\n\n /**\n * Creates a new span element containing the rendered structured content.\n * @param content - The structured content to render.\n * @param dictionary - The dictionary name for resolving media references.\n * @returns A span element with class 'structured-content'.\n */\n createStructuredContent(content: StructuredContent.Content, dictionary: string): HTMLElement {\n const node = this._createElement('span', 'structured-content');\n this._appendStructuredContent(node, content, dictionary, null);\n return node;\n }\n\n /**\n * Creates an image element for a definition, wrapped in an anchor tag.\n * @param data - The image data from the dictionary.\n * @param dictionary - The dictionary name for resolving the image path.\n * @returns An anchor element containing the image.\n */\n createDefinitionImage(\n data: StructuredContent.ImageElement | DictionaryData.TermGlossaryImage,\n dictionary: string,\n ): HTMLAnchorElement {\n const {\n path,\n width = 100,\n height = 100,\n preferredWidth,\n preferredHeight,\n title,\n pixelated,\n imageRendering,\n appearance,\n background,\n collapsed,\n collapsible,\n } = data;\n\n const verticalAlign = 'verticalAlign' in data ? data.verticalAlign : undefined;\n const border = 'border' in data ? data.border : undefined;\n const borderRadius = 'borderRadius' in data ? data.borderRadius : undefined;\n const sizeUnits = 'sizeUnits' in data ? data.sizeUnits : undefined;\n\n const hasPreferredWidth = typeof preferredWidth === 'number';\n const hasPreferredHeight = typeof preferredHeight === 'number';\n const invAspectRatio =\n hasPreferredWidth && hasPreferredHeight\n ? (preferredHeight as number) / (preferredWidth as number)\n : height / width;\n const usedWidth = hasPreferredWidth\n ? (preferredWidth as number)\n : hasPreferredHeight\n ? (preferredHeight as number) / invAspectRatio\n : width;\n\n const node = this._createElement('a', 'gloss-image-link') as HTMLAnchorElement;\n node.target = '_blank';\n node.rel = 'noreferrer noopener';\n\n const imageContainer = this._createElement('span', 'gloss-image-container');\n node.appendChild(imageContainer);\n\n const aspectRatioSizer = this._createElement('span', 'gloss-image-sizer');\n imageContainer.appendChild(aspectRatioSizer);\n\n const imageBackground = this._createElement('span', 'gloss-image-background');\n imageContainer.appendChild(imageBackground);\n\n const overlay = this._createElement('span', 'gloss-image-container-overlay');\n imageContainer.appendChild(overlay);\n\n const linkText = this._createElement('span', 'gloss-image-link-text');\n linkText.textContent = 'Image';\n node.appendChild(linkText);\n\n node.dataset.path = path;\n node.dataset.dictionary = dictionary;\n node.dataset.imageLoadState = 'not-loaded';\n node.dataset.hasAspectRatio = 'true';\n node.dataset.imageRendering =\n typeof imageRendering === 'string' ? imageRendering : pixelated ? 'pixelated' : 'auto';\n node.dataset.appearance = typeof appearance === 'string' ? appearance : 'auto';\n node.dataset.background = typeof background === 'boolean' ? `${background}` : 'true';\n node.dataset.collapsed = typeof collapsed === 'boolean' ? `${collapsed}` : 'false';\n node.dataset.collapsible = typeof collapsible === 'boolean' ? `${collapsible}` : 'true';\n if (typeof verticalAlign === 'string') {\n node.dataset.verticalAlign = verticalAlign;\n }\n if (typeof sizeUnits === 'string' && (hasPreferredWidth || hasPreferredHeight)) {\n node.dataset.sizeUnits = sizeUnits;\n }\n\n aspectRatioSizer.style.paddingTop = `${invAspectRatio * 100}%`;\n\n if (typeof border === 'string') {\n imageContainer.style.border = border;\n }\n if (typeof borderRadius === 'string') {\n imageContainer.style.borderRadius = borderRadius;\n }\n imageContainer.style.width = `${usedWidth}em`;\n if (typeof title === 'string') {\n (imageContainer as HTMLElement).title = title;\n }\n\n // Load image via content manager\n const image = this._createElement('img', 'gloss-image') as HTMLImageElement;\n if (sizeUnits === 'em' && (hasPreferredWidth || hasPreferredHeight)) {\n image.style.width = `${usedWidth}em`;\n image.style.height = `${usedWidth * invAspectRatio}em`;\n }\n image.width = usedWidth;\n image.height = usedWidth * invAspectRatio;\n\n // Anki will not render images correctly without specifying to use 100% width and height\n image.style.width = '100%';\n image.style.height = '100%';\n\n imageContainer.appendChild(image);\n\n const url = this._contentManager.loadMedia(path, dictionary, 'image');\n if (url) {\n this._setImageData(node, image, imageBackground, url, false);\n }\n\n return node;\n }\n\n // Private\n\n private _appendStructuredContent(\n container: HTMLElement,\n content: StructuredContent.Content | undefined,\n dictionary: string,\n language: string | null,\n ): void {\n if (typeof content === 'string') {\n if (content.length > 0) {\n container.appendChild(this._createTextNode(content));\n if (language === null) {\n const language2 = getLanguageFromText(content, language);\n if (language2 !== null) {\n container.lang = language2;\n }\n }\n }\n return;\n }\n if (!(typeof content === 'object' && content !== null)) {\n return;\n }\n if (Array.isArray(content)) {\n for (const item of content) {\n this._appendStructuredContent(container, item, dictionary, language);\n }\n return;\n }\n const node = this._createStructuredContentGenericElement(content, dictionary, language);\n if (node !== null) {\n container.appendChild(node);\n }\n }\n\n private _createElement(tagName: string, className: string): HTMLElement {\n const node = this._document.createElement(tagName);\n node.className = className;\n return node;\n }\n\n private _createTextNode(data: string): Text {\n return this._document.createTextNode(data);\n }\n\n private _setElementDataset(element: HTMLElement, data: StructuredContent.Data): void {\n for (let [key, value] of Object.entries(data)) {\n if (key.length > 0) {\n key = `${key[0].toUpperCase()}${key.substring(1)}`;\n }\n key = `sc${key}`;\n try {\n element.dataset[key] = value;\n } catch (_e) {\n // DOMException if key is malformed\n }\n }\n }\n\n private _setImageData(\n node: HTMLAnchorElement,\n image: HTMLImageElement,\n imageBackground: HTMLElement,\n url: string | null,\n unloaded: boolean,\n ): void {\n if (url !== null) {\n image.src = url;\n node.href = url;\n node.dataset.imageLoadState = 'loaded';\n imageBackground.style.setProperty('--image', `url(\"${url}\")`);\n } else {\n image.removeAttribute('src');\n node.removeAttribute('href');\n node.dataset.imageLoadState = unloaded ? 'unloaded' : 'load-error';\n imageBackground.style.removeProperty('--image');\n }\n }\n\n private _createStructuredContentGenericElement(\n content: StructuredContent.Element,\n dictionary: string,\n language: string | null,\n ): HTMLElement | null {\n const { tag } = content;\n switch (tag) {\n case 'br':\n return this._createStructuredContentElement(tag, content, dictionary, language, 'simple', false, false);\n case 'ruby':\n case 'rt':\n case 'rp':\n return this._createStructuredContentElement(tag, content, dictionary, language, 'simple', true, false);\n case 'table':\n return this._createStructuredContentTableElement(\n tag,\n content as StructuredContent.UnstyledElement,\n dictionary,\n language,\n );\n case 'thead':\n case 'tbody':\n case 'tfoot':\n case 'tr':\n return this._createStructuredContentElement(tag, content, dictionary, language, 'table', true, false);\n case 'th':\n case 'td':\n return this._createStructuredContentElement(\n tag,\n content,\n dictionary,\n language,\n 'table-cell',\n true,\n true,\n );\n case 'div':\n case 'span':\n case 'ol':\n case 'ul':\n case 'li':\n case 'details':\n case 'summary':\n return this._createStructuredContentElement(tag, content, dictionary, language, 'simple', true, true);\n case 'img':\n return this.createDefinitionImage(content as StructuredContent.ImageElement, dictionary);\n case 'a':\n return this._createLinkElement(content as StructuredContent.LinkElement, dictionary, language);\n }\n return null;\n }\n\n private _createStructuredContentTableElement(\n tag: string,\n content: StructuredContent.UnstyledElement,\n dictionary: string,\n language: string | null,\n ): HTMLElement {\n const container = this._createElement('div', 'gloss-sc-table-container');\n const table = this._createStructuredContentElement(tag, content, dictionary, language, 'table', true, false);\n container.appendChild(table);\n return container;\n }\n\n private _createStructuredContentElement(\n tag: string,\n content:\n | StructuredContent.StyledElement\n | StructuredContent.UnstyledElement\n | StructuredContent.TableElement\n | StructuredContent.LineBreak,\n dictionary: string,\n language: string | null,\n type: 'simple' | 'table' | 'table-cell',\n hasChildren: boolean,\n hasStyle: boolean,\n ): HTMLElement {\n const node = this._createElement(tag, `gloss-sc-${tag}`);\n const { data, lang } = content;\n if (typeof data === 'object' && data !== null) {\n this._setElementDataset(node, data);\n }\n if (typeof lang === 'string') {\n node.lang = lang;\n language = lang;\n }\n switch (type) {\n case 'table-cell':\n {\n const cell = node as HTMLTableCellElement;\n const { colSpan, rowSpan } = content as StructuredContent.TableElement;\n if (typeof colSpan === 'number') {\n cell.colSpan = colSpan;\n }\n if (typeof rowSpan === 'number') {\n cell.rowSpan = rowSpan;\n }\n }\n break;\n }\n if (hasStyle) {\n const { style, title, open } = content as StructuredContent.StyledElement;\n if (typeof style === 'object' && style !== null) {\n this._setStructuredContentElementStyle(node, style);\n }\n if (typeof title === 'string') {\n node.title = title;\n }\n if (typeof open === 'boolean' && open) {\n node.setAttribute('open', '');\n }\n }\n if (hasChildren) {\n this._appendStructuredContent(\n node,\n (content as StructuredContent.StyledElement).content,\n dictionary,\n language,\n );\n }\n return node;\n }\n\n private _setStructuredContentElementStyle(\n node: HTMLElement,\n contentStyle: StructuredContent.StructuredContentStyle,\n ): void {\n const { style } = node;\n const {\n fontStyle,\n fontWeight,\n fontSize,\n color,\n background,\n backgroundColor,\n textDecorationLine,\n textDecorationStyle,\n textDecorationColor,\n borderColor,\n borderStyle,\n borderRadius,\n borderWidth,\n clipPath,\n verticalAlign,\n textAlign,\n textEmphasis,\n textShadow,\n margin,\n marginTop,\n marginLeft,\n marginRight,\n marginBottom,\n padding,\n paddingTop,\n paddingLeft,\n paddingRight,\n paddingBottom,\n wordBreak,\n whiteSpace,\n cursor,\n listStyleType,\n } = contentStyle;\n if (typeof fontStyle === 'string') {\n style.fontStyle = fontStyle;\n }\n if (typeof fontWeight === 'string') {\n style.fontWeight = fontWeight;\n }\n if (typeof fontSize === 'string') {\n style.fontSize = fontSize;\n }\n if (typeof color === 'string') {\n style.color = color;\n }\n if (typeof background === 'string') {\n style.background = background;\n }\n if (typeof backgroundColor === 'string') {\n style.backgroundColor = backgroundColor;\n }\n if (typeof verticalAlign === 'string') {\n style.verticalAlign = verticalAlign;\n }\n if (typeof textAlign === 'string') {\n style.textAlign = textAlign;\n }\n if (typeof textEmphasis === 'string') {\n (style as unknown as Record<string, string>).textEmphasis = textEmphasis;\n }\n if (typeof textShadow === 'string') {\n style.textShadow = textShadow;\n }\n if (typeof textDecorationLine === 'string') {\n style.textDecoration = textDecorationLine;\n } else if (Array.isArray(textDecorationLine)) {\n style.textDecoration = textDecorationLine.join(' ');\n }\n if (typeof textDecorationStyle === 'string') {\n (style as unknown as Record<string, string>).textDecorationStyle = textDecorationStyle;\n }\n if (typeof textDecorationColor === 'string') {\n (style as unknown as Record<string, string>).textDecorationColor = textDecorationColor;\n }\n if (typeof borderColor === 'string') {\n style.borderColor = borderColor;\n }\n if (typeof borderStyle === 'string') {\n style.borderStyle = borderStyle;\n }\n if (typeof borderRadius === 'string') {\n style.borderRadius = borderRadius;\n }\n if (typeof borderWidth === 'string') {\n style.borderWidth = borderWidth;\n }\n if (typeof clipPath === 'string') {\n (style as unknown as Record<string, string>).clipPath = clipPath;\n }\n if (typeof margin === 'string') {\n style.margin = margin;\n }\n if (typeof marginTop === 'number') {\n style.marginTop = `${marginTop}em`;\n }\n if (typeof marginTop === 'string') {\n style.marginTop = marginTop;\n }\n if (typeof marginLeft === 'number') {\n style.marginLeft = `${marginLeft}em`;\n }\n if (typeof marginLeft === 'string') {\n style.marginLeft = marginLeft;\n }\n if (typeof marginRight === 'number') {\n style.marginRight = `${marginRight}em`;\n }\n if (typeof marginRight === 'string') {\n style.marginRight = marginRight;\n }\n if (typeof marginBottom === 'number') {\n style.marginBottom = `${marginBottom}em`;\n }\n if (typeof marginBottom === 'string') {\n style.marginBottom = marginBottom;\n }\n if (typeof padding === 'string') {\n style.padding = padding;\n }\n if (typeof paddingTop === 'string') {\n style.paddingTop = paddingTop;\n }\n if (typeof paddingLeft === 'string') {\n style.paddingLeft = paddingLeft;\n }\n if (typeof paddingRight === 'string') {\n style.paddingRight = paddingRight;\n }\n if (typeof paddingBottom === 'string') {\n style.paddingBottom = paddingBottom;\n }\n if (typeof wordBreak === 'string') {\n (style as unknown as Record<string, string>).wordBreak = wordBreak;\n }\n if (typeof whiteSpace === 'string') {\n style.whiteSpace = whiteSpace;\n }\n if (typeof cursor === 'string') {\n style.cursor = cursor;\n }\n if (typeof listStyleType === 'string') {\n style.listStyleType = listStyleType;\n }\n }\n\n private _createLinkElement(\n content: StructuredContent.LinkElement,\n dictionary: string,\n language: string | null,\n ): HTMLAnchorElement {\n const { href } = content;\n const internal = href.startsWith('?');\n\n const node = this._createElement('a', 'gloss-link') as HTMLAnchorElement;\n node.dataset.external = `${!internal}`;\n\n const text = this._createElement('span', 'gloss-link-text');\n node.appendChild(text);\n\n const { lang } = content;\n if (typeof lang === 'string') {\n node.lang = lang;\n language = lang;\n }\n\n this._appendStructuredContent(text, content.content, dictionary, language);\n\n if (!internal) {\n const icon = this._createElement('span', 'gloss-link-external-icon icon');\n icon.dataset.icon = 'external-link';\n node.appendChild(icon);\n }\n\n this._contentManager.prepareLink(node, href, internal);\n return node;\n }\n}\n"],"mappings":";;;;;;;;AA4BA,IAAa,6BAAb,MAAwC;CACpC,AAAQ;CACR,AAAQ;;;;;;CAOR,YAAYA,gBAAgCC,KAAe;AACvD,OAAK,kBAAkB;AACvB,OAAK,YAAY;CACpB;;;;;;;CAQD,wBAAwBC,MAAmBC,SAAoCC,YAA0B;AACrG,OAAK,UAAU,IAAI,qBAAqB;AACxC,OAAK,yBAAyB,MAAM,SAAS,YAAY,KAAK;CACjE;;;;;;;CAQD,wBAAwBD,SAAoCC,YAAiC;EACzF,MAAM,OAAO,KAAK,eAAe,QAAQ,qBAAqB;AAC9D,OAAK,yBAAyB,MAAM,SAAS,YAAY,KAAK;AAC9D,SAAO;CACV;;;;;;;CAQD,sBACIC,MACAD,YACiB;EACjB,MAAM,EACF,MACA,QAAQ,KACR,SAAS,KACT,gBACA,iBACA,OACA,WACA,gBACA,YACA,YACA,WACA,aACH,GAAG;EAEJ,MAAM,gBAAgB,mBAAmB,OAAO,KAAK;EACrD,MAAM,SAAS,YAAY,OAAO,KAAK;EACvC,MAAM,eAA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-utilities-Del2Ivkg.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/render/structured-content-generator.ts
|
|
5
|
+
/**
|
|
6
|
+
* Generates DOM elements from structured content definitions found in
|
|
7
|
+
* dictionary glossary entries. Handles images, links, tables, styled
|
|
8
|
+
* elements, and other structured content types.
|
|
9
|
+
*/
|
|
10
|
+
var StructuredContentGenerator = class {
|
|
11
|
+
_contentManager;
|
|
12
|
+
_document;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new StructuredContentGenerator.
|
|
15
|
+
* @param contentManager - The content manager for loading media resources.
|
|
16
|
+
* @param doc - The Document object to use for creating DOM elements.
|
|
17
|
+
*/
|
|
18
|
+
constructor(contentManager, doc) {
|
|
19
|
+
this._contentManager = contentManager;
|
|
20
|
+
this._document = doc;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Appends structured content to an existing node, adding the 'structured-content' class.
|
|
24
|
+
* @param node - The container element to append content to.
|
|
25
|
+
* @param content - The structured content to render.
|
|
26
|
+
* @param dictionary - The dictionary name for resolving media references.
|
|
27
|
+
*/
|
|
28
|
+
appendStructuredContent(node, content, dictionary) {
|
|
29
|
+
node.classList.add("structured-content");
|
|
30
|
+
this._appendStructuredContent(node, content, dictionary, null);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new span element containing the rendered structured content.
|
|
34
|
+
* @param content - The structured content to render.
|
|
35
|
+
* @param dictionary - The dictionary name for resolving media references.
|
|
36
|
+
* @returns A span element with class 'structured-content'.
|
|
37
|
+
*/
|
|
38
|
+
createStructuredContent(content, dictionary) {
|
|
39
|
+
const node = this._createElement("span", "structured-content");
|
|
40
|
+
this._appendStructuredContent(node, content, dictionary, null);
|
|
41
|
+
return node;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates an image element for a definition, wrapped in an anchor tag.
|
|
45
|
+
* @param data - The image data from the dictionary.
|
|
46
|
+
* @param dictionary - The dictionary name for resolving the image path.
|
|
47
|
+
* @returns An anchor element containing the image.
|
|
48
|
+
*/
|
|
49
|
+
createDefinitionImage(data, dictionary) {
|
|
50
|
+
const { path, width = 100, height = 100, preferredWidth, preferredHeight, title, pixelated, imageRendering, appearance, background, collapsed, collapsible } = data;
|
|
51
|
+
const verticalAlign = "verticalAlign" in data ? data.verticalAlign : void 0;
|
|
52
|
+
const border = "border" in data ? data.border : void 0;
|
|
53
|
+
const borderRadius = "borderRadius" in data ? data.borderRadius : void 0;
|
|
54
|
+
const sizeUnits = "sizeUnits" in data ? data.sizeUnits : void 0;
|
|
55
|
+
const hasPreferredWidth = typeof preferredWidth === "number";
|
|
56
|
+
const hasPreferredHeight = typeof preferredHeight === "number";
|
|
57
|
+
const invAspectRatio = hasPreferredWidth && hasPreferredHeight ? preferredHeight / preferredWidth : height / width;
|
|
58
|
+
const usedWidth = hasPreferredWidth ? preferredWidth : hasPreferredHeight ? preferredHeight / invAspectRatio : width;
|
|
59
|
+
const node = this._createElement("a", "gloss-image-link");
|
|
60
|
+
node.target = "_blank";
|
|
61
|
+
node.rel = "noreferrer noopener";
|
|
62
|
+
const imageContainer = this._createElement("span", "gloss-image-container");
|
|
63
|
+
node.appendChild(imageContainer);
|
|
64
|
+
const aspectRatioSizer = this._createElement("span", "gloss-image-sizer");
|
|
65
|
+
imageContainer.appendChild(aspectRatioSizer);
|
|
66
|
+
const imageBackground = this._createElement("span", "gloss-image-background");
|
|
67
|
+
imageContainer.appendChild(imageBackground);
|
|
68
|
+
const overlay = this._createElement("span", "gloss-image-container-overlay");
|
|
69
|
+
imageContainer.appendChild(overlay);
|
|
70
|
+
const linkText = this._createElement("span", "gloss-image-link-text");
|
|
71
|
+
linkText.textContent = "Image";
|
|
72
|
+
node.appendChild(linkText);
|
|
73
|
+
node.dataset.path = path;
|
|
74
|
+
node.dataset.dictionary = dictionary;
|
|
75
|
+
node.dataset.imageLoadState = "not-loaded";
|
|
76
|
+
node.dataset.hasAspectRatio = "true";
|
|
77
|
+
node.dataset.imageRendering = typeof imageRendering === "string" ? imageRendering : pixelated ? "pixelated" : "auto";
|
|
78
|
+
node.dataset.appearance = typeof appearance === "string" ? appearance : "auto";
|
|
79
|
+
node.dataset.background = typeof background === "boolean" ? `${background}` : "true";
|
|
80
|
+
node.dataset.collapsed = typeof collapsed === "boolean" ? `${collapsed}` : "false";
|
|
81
|
+
node.dataset.collapsible = typeof collapsible === "boolean" ? `${collapsible}` : "true";
|
|
82
|
+
if (typeof verticalAlign === "string") node.dataset.verticalAlign = verticalAlign;
|
|
83
|
+
if (typeof sizeUnits === "string" && (hasPreferredWidth || hasPreferredHeight)) node.dataset.sizeUnits = sizeUnits;
|
|
84
|
+
aspectRatioSizer.style.paddingTop = `${invAspectRatio * 100}%`;
|
|
85
|
+
if (typeof border === "string") imageContainer.style.border = border;
|
|
86
|
+
if (typeof borderRadius === "string") imageContainer.style.borderRadius = borderRadius;
|
|
87
|
+
imageContainer.style.width = `${usedWidth}em`;
|
|
88
|
+
if (typeof title === "string") imageContainer.title = title;
|
|
89
|
+
const image = this._createElement("img", "gloss-image");
|
|
90
|
+
if (sizeUnits === "em" && (hasPreferredWidth || hasPreferredHeight)) {
|
|
91
|
+
image.style.width = `${usedWidth}em`;
|
|
92
|
+
image.style.height = `${usedWidth * invAspectRatio}em`;
|
|
93
|
+
}
|
|
94
|
+
image.width = usedWidth;
|
|
95
|
+
image.height = usedWidth * invAspectRatio;
|
|
96
|
+
image.style.width = "100%";
|
|
97
|
+
image.style.height = "100%";
|
|
98
|
+
imageContainer.appendChild(image);
|
|
99
|
+
const url = this._contentManager.loadMedia(path, dictionary, "image");
|
|
100
|
+
if (url) this._setImageData(node, image, imageBackground, url, false);
|
|
101
|
+
return node;
|
|
102
|
+
}
|
|
103
|
+
_appendStructuredContent(container, content, dictionary, language) {
|
|
104
|
+
if (typeof content === "string") {
|
|
105
|
+
if (content.length > 0) {
|
|
106
|
+
container.appendChild(this._createTextNode(content));
|
|
107
|
+
if (language === null) {
|
|
108
|
+
const language2 = require_text_utilities.getLanguageFromText(content, language);
|
|
109
|
+
if (language2 !== null) container.lang = language2;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!(typeof content === "object" && content !== null)) return;
|
|
115
|
+
if (Array.isArray(content)) {
|
|
116
|
+
for (const item of content) this._appendStructuredContent(container, item, dictionary, language);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const node = this._createStructuredContentGenericElement(content, dictionary, language);
|
|
120
|
+
if (node !== null) container.appendChild(node);
|
|
121
|
+
}
|
|
122
|
+
_createElement(tagName, className) {
|
|
123
|
+
const node = this._document.createElement(tagName);
|
|
124
|
+
node.className = className;
|
|
125
|
+
return node;
|
|
126
|
+
}
|
|
127
|
+
_createTextNode(data) {
|
|
128
|
+
return this._document.createTextNode(data);
|
|
129
|
+
}
|
|
130
|
+
_setElementDataset(element, data) {
|
|
131
|
+
for (let [key, value] of Object.entries(data)) {
|
|
132
|
+
if (key.length > 0) key = `${key[0].toUpperCase()}${key.substring(1)}`;
|
|
133
|
+
key = `sc${key}`;
|
|
134
|
+
try {
|
|
135
|
+
element.dataset[key] = value;
|
|
136
|
+
} catch (_e) {}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
_setImageData(node, image, imageBackground, url, unloaded) {
|
|
140
|
+
if (url !== null) {
|
|
141
|
+
image.src = url;
|
|
142
|
+
node.href = url;
|
|
143
|
+
node.dataset.imageLoadState = "loaded";
|
|
144
|
+
imageBackground.style.setProperty("--image", `url("${url}")`);
|
|
145
|
+
} else {
|
|
146
|
+
image.removeAttribute("src");
|
|
147
|
+
node.removeAttribute("href");
|
|
148
|
+
node.dataset.imageLoadState = unloaded ? "unloaded" : "load-error";
|
|
149
|
+
imageBackground.style.removeProperty("--image");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
_createStructuredContentGenericElement(content, dictionary, language) {
|
|
153
|
+
const { tag } = content;
|
|
154
|
+
switch (tag) {
|
|
155
|
+
case "br": return this._createStructuredContentElement(tag, content, dictionary, language, "simple", false, false);
|
|
156
|
+
case "ruby":
|
|
157
|
+
case "rt":
|
|
158
|
+
case "rp": return this._createStructuredContentElement(tag, content, dictionary, language, "simple", true, false);
|
|
159
|
+
case "table": return this._createStructuredContentTableElement(tag, content, dictionary, language);
|
|
160
|
+
case "thead":
|
|
161
|
+
case "tbody":
|
|
162
|
+
case "tfoot":
|
|
163
|
+
case "tr": return this._createStructuredContentElement(tag, content, dictionary, language, "table", true, false);
|
|
164
|
+
case "th":
|
|
165
|
+
case "td": return this._createStructuredContentElement(tag, content, dictionary, language, "table-cell", true, true);
|
|
166
|
+
case "div":
|
|
167
|
+
case "span":
|
|
168
|
+
case "ol":
|
|
169
|
+
case "ul":
|
|
170
|
+
case "li":
|
|
171
|
+
case "details":
|
|
172
|
+
case "summary": return this._createStructuredContentElement(tag, content, dictionary, language, "simple", true, true);
|
|
173
|
+
case "img": return this.createDefinitionImage(content, dictionary);
|
|
174
|
+
case "a": return this._createLinkElement(content, dictionary, language);
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
_createStructuredContentTableElement(tag, content, dictionary, language) {
|
|
179
|
+
const container = this._createElement("div", "gloss-sc-table-container");
|
|
180
|
+
const table = this._createStructuredContentElement(tag, content, dictionary, language, "table", true, false);
|
|
181
|
+
container.appendChild(table);
|
|
182
|
+
return container;
|
|
183
|
+
}
|
|
184
|
+
_createStructuredContentElement(tag, content, dictionary, language, type, hasChildren, hasStyle) {
|
|
185
|
+
const node = this._createElement(tag, `gloss-sc-${tag}`);
|
|
186
|
+
const { data, lang } = content;
|
|
187
|
+
if (typeof data === "object" && data !== null) this._setElementDataset(node, data);
|
|
188
|
+
if (typeof lang === "string") {
|
|
189
|
+
node.lang = lang;
|
|
190
|
+
language = lang;
|
|
191
|
+
}
|
|
192
|
+
switch (type) {
|
|
193
|
+
case "table-cell":
|
|
194
|
+
{
|
|
195
|
+
const cell = node;
|
|
196
|
+
const { colSpan, rowSpan } = content;
|
|
197
|
+
if (typeof colSpan === "number") cell.colSpan = colSpan;
|
|
198
|
+
if (typeof rowSpan === "number") cell.rowSpan = rowSpan;
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
if (hasStyle) {
|
|
203
|
+
const { style, title, open } = content;
|
|
204
|
+
if (typeof style === "object" && style !== null) this._setStructuredContentElementStyle(node, style);
|
|
205
|
+
if (typeof title === "string") node.title = title;
|
|
206
|
+
if (typeof open === "boolean" && open) node.setAttribute("open", "");
|
|
207
|
+
}
|
|
208
|
+
if (hasChildren) this._appendStructuredContent(node, content.content, dictionary, language);
|
|
209
|
+
return node;
|
|
210
|
+
}
|
|
211
|
+
_setStructuredContentElementStyle(node, contentStyle) {
|
|
212
|
+
const { style } = node;
|
|
213
|
+
const { fontStyle, fontWeight, fontSize, color, background, backgroundColor, textDecorationLine, textDecorationStyle, textDecorationColor, borderColor, borderStyle, borderRadius, borderWidth, clipPath, verticalAlign, textAlign, textEmphasis, textShadow, margin, marginTop, marginLeft, marginRight, marginBottom, padding, paddingTop, paddingLeft, paddingRight, paddingBottom, wordBreak, whiteSpace, cursor, listStyleType } = contentStyle;
|
|
214
|
+
if (typeof fontStyle === "string") style.fontStyle = fontStyle;
|
|
215
|
+
if (typeof fontWeight === "string") style.fontWeight = fontWeight;
|
|
216
|
+
if (typeof fontSize === "string") style.fontSize = fontSize;
|
|
217
|
+
if (typeof color === "string") style.color = color;
|
|
218
|
+
if (typeof background === "string") style.background = background;
|
|
219
|
+
if (typeof backgroundColor === "string") style.backgroundColor = backgroundColor;
|
|
220
|
+
if (typeof verticalAlign === "string") style.verticalAlign = verticalAlign;
|
|
221
|
+
if (typeof textAlign === "string") style.textAlign = textAlign;
|
|
222
|
+
if (typeof textEmphasis === "string") style.textEmphasis = textEmphasis;
|
|
223
|
+
if (typeof textShadow === "string") style.textShadow = textShadow;
|
|
224
|
+
if (typeof textDecorationLine === "string") style.textDecoration = textDecorationLine;
|
|
225
|
+
else if (Array.isArray(textDecorationLine)) style.textDecoration = textDecorationLine.join(" ");
|
|
226
|
+
if (typeof textDecorationStyle === "string") style.textDecorationStyle = textDecorationStyle;
|
|
227
|
+
if (typeof textDecorationColor === "string") style.textDecorationColor = textDecorationColor;
|
|
228
|
+
if (typeof borderColor === "string") style.borderColor = borderColor;
|
|
229
|
+
if (typeof borderStyle === "string") style.borderStyle = borderStyle;
|
|
230
|
+
if (typeof borderRadius === "string") style.borderRadius = borderRadius;
|
|
231
|
+
if (typeof borderWidth === "string") style.borderWidth = borderWidth;
|
|
232
|
+
if (typeof clipPath === "string") style.clipPath = clipPath;
|
|
233
|
+
if (typeof margin === "string") style.margin = margin;
|
|
234
|
+
if (typeof marginTop === "number") style.marginTop = `${marginTop}em`;
|
|
235
|
+
if (typeof marginTop === "string") style.marginTop = marginTop;
|
|
236
|
+
if (typeof marginLeft === "number") style.marginLeft = `${marginLeft}em`;
|
|
237
|
+
if (typeof marginLeft === "string") style.marginLeft = marginLeft;
|
|
238
|
+
if (typeof marginRight === "number") style.marginRight = `${marginRight}em`;
|
|
239
|
+
if (typeof marginRight === "string") style.marginRight = marginRight;
|
|
240
|
+
if (typeof marginBottom === "number") style.marginBottom = `${marginBottom}em`;
|
|
241
|
+
if (typeof marginBottom === "string") style.marginBottom = marginBottom;
|
|
242
|
+
if (typeof padding === "string") style.padding = padding;
|
|
243
|
+
if (typeof paddingTop === "string") style.paddingTop = paddingTop;
|
|
244
|
+
if (typeof paddingLeft === "string") style.paddingLeft = paddingLeft;
|
|
245
|
+
if (typeof paddingRight === "string") style.paddingRight = paddingRight;
|
|
246
|
+
if (typeof paddingBottom === "string") style.paddingBottom = paddingBottom;
|
|
247
|
+
if (typeof wordBreak === "string") style.wordBreak = wordBreak;
|
|
248
|
+
if (typeof whiteSpace === "string") style.whiteSpace = whiteSpace;
|
|
249
|
+
if (typeof cursor === "string") style.cursor = cursor;
|
|
250
|
+
if (typeof listStyleType === "string") style.listStyleType = listStyleType;
|
|
251
|
+
}
|
|
252
|
+
_createLinkElement(content, dictionary, language) {
|
|
253
|
+
const { href } = content;
|
|
254
|
+
const internal = href.startsWith("?");
|
|
255
|
+
const node = this._createElement("a", "gloss-link");
|
|
256
|
+
node.dataset.external = `${!internal}`;
|
|
257
|
+
const text = this._createElement("span", "gloss-link-text");
|
|
258
|
+
node.appendChild(text);
|
|
259
|
+
const { lang } = content;
|
|
260
|
+
if (typeof lang === "string") {
|
|
261
|
+
node.lang = lang;
|
|
262
|
+
language = lang;
|
|
263
|
+
}
|
|
264
|
+
this._appendStructuredContent(text, content.content, dictionary, language);
|
|
265
|
+
if (!internal) {
|
|
266
|
+
const icon = this._createElement("span", "gloss-link-external-icon icon");
|
|
267
|
+
icon.dataset.icon = "external-link";
|
|
268
|
+
node.appendChild(icon);
|
|
269
|
+
}
|
|
270
|
+
this._contentManager.prepareLink(node, href, internal);
|
|
271
|
+
return node;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
//#endregion
|
|
276
|
+
Object.defineProperty(exports, 'StructuredContentGenerator', {
|
|
277
|
+
enumerable: true,
|
|
278
|
+
get: function () {
|
|
279
|
+
return StructuredContentGenerator;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
//# sourceMappingURL=structured-content-generator-DrwkB0-k.cjs.map
|