@kanbun-skam/skam-web-component 0.1.0 → 0.3.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.
@@ -1,303 +0,0 @@
1
- // src/xml-extraction.ts
2
- var SKAM_XML_MEDIA_TYPE = "application/vnd.berlysia.skam+xml";
3
- function extractXmlFromScriptElement(host) {
4
- const script = host.querySelector(`script[type="${SKAM_XML_MEDIA_TYPE}"]`);
5
- if (script) {
6
- const text = script.textContent?.trim();
7
- return text || null;
8
- }
9
- return null;
10
- }
11
- function extractXmlFromLightDOM(host) {
12
- const text = host.textContent;
13
- if (text && text.trim()) {
14
- return text;
15
- }
16
- return null;
17
- }
18
- function extractXml(xmlContent, host) {
19
- if (xmlContent !== void 0) {
20
- return xmlContent || null;
21
- }
22
- const fromScript = extractXmlFromScriptElement(host);
23
- if (fromScript) {
24
- return fromScript;
25
- }
26
- return extractXmlFromLightDOM(host);
27
- }
28
-
29
- // src/skam-renderer.ts
30
- import { parse } from "@kanbun-skam/skam-xml-parser";
31
- import { render, calibrateGridBaseline } from "@kanbun-skam/skam-html-renderer";
32
-
33
- // src/attribute-map.ts
34
- import { PROFILES } from "@kanbun-skam/skam-html-renderer";
35
- var VALID_WRITING_MODES = ["vertical", "horizontal"];
36
- var VALID_COPYABLE_ELEMENTS = ["ruby", "okurigana", "soegana", "kaeriten", "okototen"];
37
- function parseWritingMode(value) {
38
- if (value != null && VALID_WRITING_MODES.includes(value)) {
39
- return value;
40
- }
41
- return "vertical";
42
- }
43
- function parseProfile(value) {
44
- if (value == null) return void 0;
45
- const preset = PROFILES[value];
46
- if (preset) return preset;
47
- try {
48
- const parsed = JSON.parse(value);
49
- if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
50
- return parsed;
51
- }
52
- } catch {
53
- }
54
- return void 0;
55
- }
56
- function parseBooleanAttr(value) {
57
- return value !== null;
58
- }
59
- function parseCopyable(value) {
60
- if (value == null) return void 0;
61
- if (value === "all") return "all";
62
- const elements = value.split(/\s+/).filter(
63
- (v) => VALID_COPYABLE_ELEMENTS.includes(v)
64
- );
65
- return elements.length > 0 ? elements : void 0;
66
- }
67
- function buildRenderOptions(attrs) {
68
- const options = {
69
- writingMode: parseWritingMode(attrs["writing-mode"]),
70
- useLayer: false
71
- // Shadow DOM doesn't need @layer
72
- };
73
- const profile = parseProfile(attrs.profile);
74
- if (profile) {
75
- options.profile = profile;
76
- }
77
- if (parseBooleanAttr(attrs.inline)) {
78
- options.inline = true;
79
- }
80
- if (attrs["include-reading-layer"] === null) {
81
- options.includeReadingLayer = true;
82
- } else {
83
- options.includeReadingLayer = attrs["include-reading-layer"] !== "false";
84
- }
85
- const copyable = parseCopyable(attrs.copyable);
86
- if (copyable) {
87
- options.copyable = copyable;
88
- }
89
- if (attrs["class-prefix"] != null) {
90
- options.classPrefix = attrs["class-prefix"];
91
- }
92
- return options;
93
- }
94
-
95
- // src/font-loader.ts
96
- var DEFAULT_FONT_FAMILY = "Noto Serif JP";
97
- var GOOGLE_FONTS_URL = "https://fonts.googleapis.com/css2?family=Noto+Serif+JP:wght@400;700&display=swap";
98
- var LINK_SELECTOR = 'link[href*="fonts.googleapis.com"][href*="Noto+Serif+JP"]';
99
- function injectGoogleFontsLink() {
100
- if (typeof document === "undefined") return;
101
- if (!document.querySelector(LINK_SELECTOR)) {
102
- const link = document.createElement("link");
103
- link.rel = "stylesheet";
104
- link.href = GOOGLE_FONTS_URL;
105
- document.head.appendChild(link);
106
- }
107
- }
108
- function buildFontStyle(variablePrefix) {
109
- const family = `"${DEFAULT_FONT_FAMILY}", serif`;
110
- return `:host {
111
- --${variablePrefix}-font-family: ${family};
112
- --${variablePrefix}-font-family-ruby: ${family};
113
- }`;
114
- }
115
-
116
- // src/skam-renderer.ts
117
- var ERROR_CSS = `
118
- [role="alert"] {
119
- color: #c00;
120
- padding: 0.5em;
121
- font-family: monospace;
122
- white-space: pre-wrap;
123
- }
124
- `;
125
- function generateVarOverrideCss(prefix) {
126
- const vp = prefix;
127
- return `
128
- :host {
129
- --${vp}-color-fg: currentColor;
130
- --${vp}-color-kaeriten: currentColor;
131
- --${vp}-color-ruby: currentColor;
132
- --${vp}-color-emphasis: currentColor;
133
- --${vp}-glyph-size: 1em;
134
- --${vp}-ruby-ratio: 0.5;
135
- --${vp}-line-height: 2;
136
- --${vp}-letter-spacing: 0;
137
- }
138
- .${vp}-document {
139
- --${vp}-color-fg: inherit;
140
- --${vp}-color-kaeriten: inherit;
141
- --${vp}-color-ruby: inherit;
142
- --${vp}-color-emphasis: inherit;
143
- --${vp}-font-family: inherit;
144
- --${vp}-font-family-ruby: inherit;
145
- --${vp}-glyph-size: inherit;
146
- --${vp}-ruby-ratio: inherit;
147
- --${vp}-line-height: inherit;
148
- --${vp}-letter-spacing: inherit;
149
- }`;
150
- }
151
- var SkamRendererElement = class extends HTMLElement {
152
- static observedAttributes = [
153
- "writing-mode",
154
- "profile",
155
- "inline",
156
- "include-reading-layer",
157
- "copyable",
158
- "class-prefix",
159
- "auto-font"
160
- ];
161
- #shadow;
162
- #mainStyle;
163
- #errorStyle;
164
- #fontStyle;
165
- #contentDiv;
166
- #xmlContent;
167
- #observer = null;
168
- #rafId = null;
169
- constructor() {
170
- super();
171
- this.#shadow = this.attachShadow({ mode: "open" });
172
- this.#mainStyle = document.createElement("style");
173
- this.#mainStyle.id = "main-css";
174
- this.#errorStyle = document.createElement("style");
175
- this.#errorStyle.id = "error-css";
176
- this.#errorStyle.textContent = ERROR_CSS;
177
- this.#fontStyle = document.createElement("style");
178
- this.#fontStyle.id = "font-css";
179
- this.#contentDiv = document.createElement("div");
180
- this.#contentDiv.id = "content";
181
- this.#shadow.appendChild(this.#fontStyle);
182
- this.#shadow.appendChild(this.#mainStyle);
183
- this.#shadow.appendChild(this.#errorStyle);
184
- this.#shadow.appendChild(this.#contentDiv);
185
- }
186
- /**
187
- * XML コンテンツをプログラマティックに設定する。
188
- * undefined の場合は <script type="application/vnd.berlysia.skam+xml"> や Light DOM にフォールバック。
189
- * 空文字列 "" も「設定済み」とみなし、フォールバックしない。
190
- */
191
- get xmlContent() {
192
- return this.#xmlContent;
193
- }
194
- set xmlContent(value) {
195
- this.#xmlContent = value;
196
- this.#scheduleRender();
197
- }
198
- connectedCallback() {
199
- this.#ensureFont();
200
- this.#observer = new MutationObserver(() => {
201
- this.#scheduleRender();
202
- });
203
- this.#observer.observe(this, {
204
- childList: true,
205
- subtree: true,
206
- characterData: true
207
- });
208
- this.#scheduleRender();
209
- }
210
- disconnectedCallback() {
211
- this.#observer?.disconnect();
212
- this.#observer = null;
213
- if (this.#rafId !== null) {
214
- cancelAnimationFrame(this.#rafId);
215
- this.#rafId = null;
216
- }
217
- }
218
- attributeChangedCallback(_name, _oldValue, _newValue) {
219
- this.#scheduleRender();
220
- }
221
- #scheduleRender() {
222
- if (this.#rafId !== null) return;
223
- this.#rafId = requestAnimationFrame(() => {
224
- this.#rafId = null;
225
- this.#performRender();
226
- });
227
- }
228
- #performRender() {
229
- const xml = extractXml(this.#xmlContent, this);
230
- if (!xml) {
231
- this.#contentDiv.innerHTML = "";
232
- this.#mainStyle.textContent = "";
233
- return;
234
- }
235
- try {
236
- const doc = parse(xml);
237
- const attrs = this.#getAttributeValues();
238
- const options = buildRenderOptions(attrs);
239
- const { html, css } = render(doc, options);
240
- const prefix = options.classPrefix ?? "skam";
241
- this.#mainStyle.textContent = css + generateVarOverrideCss(prefix);
242
- this.#ensureFont();
243
- this.#updateFontStyle(prefix);
244
- this.#contentDiv.innerHTML = html;
245
- calibrateGridBaseline(this.#contentDiv, {
246
- variablePrefix: prefix
247
- });
248
- this.dispatchEvent(
249
- new CustomEvent("skam-render", {
250
- detail: { document: doc },
251
- bubbles: true,
252
- composed: true
253
- })
254
- );
255
- } catch (error) {
256
- this.#handleError(error);
257
- }
258
- }
259
- #handleError(error) {
260
- const message = error instanceof Error ? error.message : String(error);
261
- this.#contentDiv.innerHTML = "";
262
- const alert = document.createElement("div");
263
- alert.setAttribute("role", "alert");
264
- alert.textContent = message;
265
- this.#contentDiv.appendChild(alert);
266
- this.dispatchEvent(
267
- new CustomEvent("skam-error", {
268
- detail: { error },
269
- bubbles: true,
270
- composed: true
271
- })
272
- );
273
- }
274
- /** auto-font 属性があれば Google Fonts <link> を document.head に注入 */
275
- #ensureFont() {
276
- if (this.hasAttribute("auto-font")) {
277
- injectGoogleFontsLink();
278
- }
279
- }
280
- /** Shadow DOM 内のフォント CSS Variable を更新(auto-font 時のみ設定) */
281
- #updateFontStyle(variablePrefix) {
282
- if (this.hasAttribute("auto-font")) {
283
- this.#fontStyle.textContent = buildFontStyle(variablePrefix);
284
- } else {
285
- this.#fontStyle.textContent = "";
286
- }
287
- }
288
- #getAttributeValues() {
289
- return {
290
- "writing-mode": this.getAttribute("writing-mode"),
291
- profile: this.getAttribute("profile"),
292
- inline: this.getAttribute("inline"),
293
- "include-reading-layer": this.getAttribute("include-reading-layer"),
294
- copyable: this.getAttribute("copyable"),
295
- "class-prefix": this.getAttribute("class-prefix")
296
- };
297
- }
298
- };
299
-
300
- export {
301
- SKAM_XML_MEDIA_TYPE,
302
- SkamRendererElement
303
- };