@kanbun-skam/skam-web-component 0.1.0 → 0.2.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 +101 -19
- package/dist/auto.js +2 -2
- package/dist/{chunk-NXVDX5VV.js → chunk-HPIH7MRS.js} +178 -27
- package/dist/index.d.ts +21 -2
- package/dist/index.js +5 -3
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**`<skam-renderer>`** カスタムエレメント。
|
|
4
4
|
|
|
5
|
-
SKAM-ML/XML を Shadow DOM 内で HTML レンダリングする Web Component
|
|
5
|
+
SKAM-ML/XML を Shadow DOM 内で HTML レンダリングする Web Component。
|
|
6
6
|
|
|
7
7
|
## インストール
|
|
8
8
|
|
|
@@ -14,11 +14,13 @@ pnpm add @kanbun-skam/skam-web-component
|
|
|
14
14
|
|
|
15
15
|
## 使い方
|
|
16
16
|
|
|
17
|
-
###
|
|
17
|
+
### 自動登録(かんたん)
|
|
18
|
+
|
|
19
|
+
`/auto` エンドポイントをインポートすると `<skam-renderer>` が自動登録される。
|
|
18
20
|
|
|
19
21
|
```html
|
|
20
22
|
<script type="module">
|
|
21
|
-
import '@kanbun-skam/skam-web-component';
|
|
23
|
+
import '@kanbun-skam/skam-web-component/auto';
|
|
22
24
|
</script>
|
|
23
25
|
|
|
24
26
|
<skam-renderer>
|
|
@@ -35,38 +37,99 @@ pnpm add @kanbun-skam/skam-web-component
|
|
|
35
37
|
</skam-renderer>
|
|
36
38
|
```
|
|
37
39
|
|
|
40
|
+
### 手動登録
|
|
41
|
+
|
|
42
|
+
メインエントリからは自動登録されない。利用側で `customElements.define()` を呼ぶ。
|
|
43
|
+
|
|
44
|
+
<!-- doctest:typecheck -->
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { SkamRendererElement } from '@kanbun-skam/skam-web-component';
|
|
48
|
+
|
|
49
|
+
customElements.define('skam-renderer', SkamRendererElement);
|
|
50
|
+
```
|
|
51
|
+
|
|
38
52
|
### プログラマティックに XML を設定
|
|
39
53
|
|
|
54
|
+
<!-- doctest:typecheck -->
|
|
55
|
+
|
|
40
56
|
```typescript
|
|
41
57
|
import { SkamRendererElement } from '@kanbun-skam/skam-web-component';
|
|
42
58
|
|
|
59
|
+
declare const xmlString: string;
|
|
60
|
+
|
|
43
61
|
const el = document.querySelector('skam-renderer') as SkamRendererElement;
|
|
44
62
|
el.xmlContent = xmlString;
|
|
45
63
|
```
|
|
46
64
|
|
|
47
65
|
### 属性
|
|
48
66
|
|
|
49
|
-
| 属性 | 値 | デフォルト | 説明
|
|
50
|
-
| ----------------------- | --------------------------------------------------- | ---------- |
|
|
51
|
-
| `writing-mode` | `vertical` \| `horizontal` | `vertical` | 書字方向
|
|
52
|
-
| `profile` | `full` \| `learningBasic` \| `learningHint` \| JSON | `full` | 表示プロファイル
|
|
53
|
-
| `inline` | (boolean) | false | インラインモード
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
56
|
-
| `copyable` | `all` \| スペース区切りの要素名 | (なし) | コピー可能な注記要素
|
|
57
|
-
| `class-prefix` | 文字列 | `skam` | CSS クラス名の接頭辞
|
|
58
|
-
| `
|
|
67
|
+
| 属性 | 値 | デフォルト | 説明 |
|
|
68
|
+
| ----------------------- | --------------------------------------------------- | ---------- | ------------------------------------------------- |
|
|
69
|
+
| `writing-mode` | `vertical` \| `horizontal` | `vertical` | 書字方向 |
|
|
70
|
+
| `profile` | `full` \| `learningBasic` \| `learningHint` \| JSON | `full` | 表示プロファイル |
|
|
71
|
+
| `inline` | (boolean) | false | インラインモード |
|
|
72
|
+
| `include-reading-layer` | `true` \| `false` | `true` | 読み層の表示 |
|
|
73
|
+
| `reading-layer-source` | `readings` \| `display` | `readings` | 読み層の出どころ (`@experimental`) |
|
|
74
|
+
| `copyable` | `all` \| スペース区切りの要素名 | (なし) | コピー可能な注記要素 |
|
|
75
|
+
| `class-prefix` | 文字列 | `skam` | CSS クラス名の接頭辞 |
|
|
76
|
+
| `display-lang` | BCP 47 言語タグ | (なし) | 表示層 `.skam-display` の `lang` |
|
|
77
|
+
| `auto-font` | (boolean) | false | Google Fonts 自動読み込み(Noto Serif JP, serif) |
|
|
78
|
+
|
|
79
|
+
`auto-font` を有効にすると、Google Fonts から **Noto Serif JP** を読み込み、`--{class-prefix}-font-family` と `--{class-prefix}-font-family-ruby` を自動設定する。無効時はフォントを継承する。
|
|
80
|
+
|
|
81
|
+
`profile` は、プリセット名か JSON で書く。JSON には 2 つの形がある。
|
|
82
|
+
|
|
83
|
+
- 部分指定(例: `{"okurigana":false}`): **書かなかったキーは full(表示)になる**。
|
|
84
|
+
- `{ "base": "learningBasic", "overrides": { "okurigana": true } }`: プリセットから出発して一部を変える。注記を隠す用途ではこちらを使う。
|
|
85
|
+
|
|
86
|
+
未知のキー、真偽値でない値、空文字、JSON の `null` / 配列 / 数値は不正で、**描画しない**(下の `skam-error`、`recoverable: false`)。`include-reading-layer` も `true` / `false` / 空文字以外は描画しない。`profile` 属性を外すと、full で描画する。
|
|
87
|
+
|
|
88
|
+
`reading-layer-source` (`@experimental`) は、読み層の出どころを選ぶ。`readings`(既定)は作者が書いた読みを、`display` は `profile` で画面に見えている注記だけを言葉にする。`readings` と白文は参照しない。`readings` / `display` 以外は不正で、描画しない。**`reading-layer-source=""` は不正(描画しない)だが、`include-reading-layer=""` は真偽値の属性として true になる。** 読み上げの言葉を変える `accessibleLabels` は関数なので、属性からは渡せない。`render()` を直接呼ぶ。
|
|
89
|
+
|
|
90
|
+
### 出題に使うとき
|
|
91
|
+
|
|
92
|
+
教材の出題画面のように注記の一部を隠すときは、次の 4 点に従う。
|
|
93
|
+
|
|
94
|
+
1. `profile` は `{ "base": ..., "overrides": ... }` の形で書き、**必ず** `reading-layer-source="display"` と併用する。部分指定はキーを書き忘れると full 側に倒れる。
|
|
95
|
+
2. 既定の `readings` は、隠した注記を読み下しとして読み上げる。
|
|
96
|
+
3. `profile` 属性を外すと、full で描かれる。属性を動的に付け外しする画面では、外した瞬間に答えが見える。
|
|
97
|
+
4. 保証するのは画面と読み層だけである。HTML ソースや `<script>` 内の SKAM-ML からは元の注記を取れる。答えを本当に隠したいなら、配る前に SKAM-ML から注記を除く。
|
|
98
|
+
|
|
99
|
+
```html
|
|
100
|
+
<skam-renderer
|
|
101
|
+
profile='{"base":"learningHint","overrides":{"okurigana":false}}'
|
|
102
|
+
reading-layer-source="display"
|
|
103
|
+
>
|
|
104
|
+
<script type="application/vnd.berlysia.skam+xml">
|
|
105
|
+
...
|
|
106
|
+
</script>
|
|
107
|
+
</skam-renderer>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
各属性の詳細(`copyable` の指定可能な値など)は [@kanbun-skam/skam-html-renderer の README](https://github.com/berlysia/kanbun-annotation/tree/master/packages/skam-html-renderer#readme) を参照。
|
|
59
111
|
|
|
60
112
|
### イベント
|
|
61
113
|
|
|
62
|
-
| イベント | detail
|
|
63
|
-
| ------------- |
|
|
64
|
-
| `skam-render` | `{ document: SKAMDocument }`
|
|
65
|
-
| `skam-error` | `{ error: Error }`
|
|
114
|
+
| イベント | detail | 説明 |
|
|
115
|
+
| ------------- | ------------------------------------------------------------ | ------------------ |
|
|
116
|
+
| `skam-render` | `{ document: SKAMDocument }` | レンダリング成功時 |
|
|
117
|
+
| `skam-error` | `{ error: Error, recoverable: boolean, attribute?: string }` | エラー発生時 |
|
|
118
|
+
|
|
119
|
+
`skam-error` には 2 種類ある。
|
|
120
|
+
|
|
121
|
+
- `recoverable: false`: 描画に失敗した。内容を消して `role="alert"` を表示する。`profile` / `include-reading-layer` / `reading-layer-source` が不正なときも、この形で発火する(`attribute` に属性名が入り、`detail` に属性の生の値は入らない。値は 64 文字に切り詰めて、メッセージに含まれる)。描画の失敗は再描画のたびに発火する。属性が不正な間は、`<script>` の内容が変わっても空のままで、同じ属性の同じ値では再び発火しない。
|
|
122
|
+
- `recoverable: true`: `writing-mode` / `copyable` / `class-prefix` / `display-lang` の値が不正だったため、その属性だけ既定値に戻して描画を続けた(`copyable` は有効な要素名だけを使う)。`attribute` に属性名が入る。`skam-render` より前に、`writing-mode` → `copyable` → `class-prefix` → `display-lang` の順で発火する。`class-prefix` / `display-lang` では、`error.message` に属性の値は含まれない。
|
|
123
|
+
|
|
124
|
+
回復可能なエラーは、同じ属性に同じ不正値が残っている間は再描画しても再び発火しない。値が変わったときだけ発火する。値が有効になった場合と、要素を DOM から外した場合は、記録が消える。ハンドラが属性を毎回違う不正値に書き換えると、エラー → 再描画 → エラーの連鎖は止まらない。これはハンドラ側の誤りとして扱う。
|
|
125
|
+
|
|
126
|
+
### SSR
|
|
127
|
+
|
|
128
|
+
`@kanbun-skam/skam-web-component` と `/auto` は、`HTMLElement` や `customElements` が無い環境でも import できる。`/auto` はその環境では何もしない。ただし `SkamRendererElement` をサーバー側で `new` することはできない (constructor が `document` を使うため)。
|
|
66
129
|
|
|
67
130
|
### CSS Variables によるカスタマイズ
|
|
68
131
|
|
|
69
|
-
Shadow DOM 内のスタイルはホスト要素の CSS Variables
|
|
132
|
+
Shadow DOM 内のスタイルはホスト要素の CSS Variables で制御可能。変数一覧は [skam-html-renderer の CSS Variables セクション](https://github.com/berlysia/kanbun-annotation/tree/master/packages/skam-html-renderer#css-variables-によるカスタマイズ)を参照。
|
|
70
133
|
|
|
71
134
|
```css
|
|
72
135
|
skam-renderer {
|
|
@@ -83,6 +146,25 @@ skam-renderer {
|
|
|
83
146
|
2. `<script type="application/vnd.berlysia.skam+xml">` 子要素
|
|
84
147
|
3. Light DOM のテキストコンテンツ
|
|
85
148
|
|
|
149
|
+
## 0.1 からの移行
|
|
150
|
+
|
|
151
|
+
0.1.x からの変更のうち、このパッケージに関わるものです。ID は全体の移行表と共通です。
|
|
152
|
+
|
|
153
|
+
| ID | 旧挙動 | 新挙動 | 回避策 |
|
|
154
|
+
| --- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
155
|
+
| M01 | npm には 0.1.0 から `@kanbun-skam/*` の名前で公開している (`@kanbun/*` は npm に公開していない)。リポジトリの CHANGELOG には、未公開の 0.1.1 として `@kanbun` からの改名が書かれていた。 | 名前は変わらない。CHANGELOG から未公開の 0.1.1 の節を消し、0.1.0 の次を 0.2.0 とした。 | 不要。 |
|
|
156
|
+
| M15 | readings のない文書では本文が支援技術から見えなかった。読み層に `aria-label` を付けた。 | readings がない文書は、読み層に白文 (本文のテキスト) を出力する。読み層に `aria-label` は付けない。 | スナップショットや VRT を取り直す。`aria-label` に依存した検索や CSS があれば、読み層の要素の内容に切り替える。 |
|
|
157
|
+
| M20 | `detail` は従来の項目だけだった。 | `detail` に `recoverable` と `attribute` が加わる。回復可能なエラーは、同じ不正な値で再描画しても発火しない。 | `skam-error` のハンドラで `recoverable` を見て扱いを分ける。 |
|
|
158
|
+
| M21 | DOM のない環境で import すると失敗した。 | SSR 中に import できる。要素の登録はブラウザで行う。 | 対応は要らない (非破壊の修正)。 |
|
|
159
|
+
| M22 | 表示言語を指定する手段がなかった。 | html-renderer に `displayLang` オプション、web-component に `display-lang` 属性を足す。 | 対応は要らない (非破壊の追加)。 |
|
|
160
|
+
| M24 | 条件オブジェクトで `import` が `types` より前にあった。`skam-web-component` の `./auto` には `types` がなかった。 | `types` を先頭にする。`./auto` に `types` を足す。サブパスと実体は変わらない。 | 対応は要らない (非破壊の変更)。 |
|
|
161
|
+
| M27 | なし。 | `skam` / `skam-html-renderer` / `skam-canvas-renderer`: `RENDER_PROFILES` (html と canvas では `PROFILES` の別名のまま)、`resolveRenderProfile`、`RenderProfileInput`、`RenderProfileName` (`RenderProfile` の型も skam から再 export する)。`skam-web-component`: `extractXmlFromScriptElement` (`@experimental`)。 | 対応は要らない (非破壊の追加)。`@experimental` の API は、0.x の間はマイナーリリースで値が変わりうる。 |
|
|
162
|
+
| M31 | `profile` 属性が不正な値 (JSON として読めない値、未知のプリセット名、JSON の `null` / 配列 / 数値、空文字) なら、黙って full で描いた。`include-reading-layer` は `"false"` 以外をすべて true として扱った。 | `profile` が不正な値 (未知のキー、真偽値でない値、`undefined` の値、`base` と個別キーの混在、own property の `__proto__`、プリセット名でも JSON でもない値、JSON の `null` / 配列 / 数値、空文字) なら、描画しない。内容を空にして `role="alert"` の要素にメッセージを出し、`skam-error` を `recoverable: false` で発火する。`include-reading-layer` が `"true"` / `"false"` / 空文字以外のときも同じ。不正な間は `<script>` の内容が変わっても空のまま。`detail` には属性の生の値を入れない。同じ属性の同じ値では再発火しない。属性を外すと full で描く。 | `profile` はプリセット名か、検証を通る JSON で書く。`include-reading-layer` は `true` / `false` / 空文字にする。`skam-error` を購読して `recoverable: false` を扱う。 |
|
|
163
|
+
| M32 | `writing-mode` が `vertical` / `horizontal` 以外、`copyable` が未知の要素名を含むとき、黙って既定値 (縦書き、有効な要素名だけ) で描いた。 | 描画は変わらない。`skam-error` を `recoverable: true`、`attribute` 付きで発火する (同じ値では再発火しない)。`skam-render` より前に、`writing-mode` → `copyable` → `class-prefix` → `display-lang` の順で発火する。 | 対応は要らない。`skam-error` を購読している場合は、`recoverable` と `attribute` で扱いを分ける。 |
|
|
164
|
+
| M37 | なし。 | `skam`: `describeAnnotationIR`、`formatAccessibleText`、`DEFAULT_ACCESSIBLE_LABELS`、型 `AccessibleNote` / `AccessibleUnit` / `AccessibleBlock` / `AccessibleLabels` (すべて `@experimental`)。`skam-html-renderer`: `readingLayerSource` / `accessibleLabels` オプションと `AccessibleLabels` 型 (`@experimental`)。`skam-web-component`: `reading-layer-source` 属性 (`@experimental`)。 | 対応は要らない (追加。既定の挙動は変わらない)。`@experimental` の API は、0.x の間はマイナーリリースで値が変わりうる。読み上げの言葉も 0.x のマイナーで変わりうる。 |
|
|
165
|
+
|
|
166
|
+
全体の移行表 (対象パッケージ、依存の更新を含む): https://github.com/berlysia/kanbun-annotation/blob/master/docs/migration-0.1-to-0.2.md
|
|
167
|
+
|
|
86
168
|
## 関連パッケージ
|
|
87
169
|
|
|
88
170
|
- [@kanbun-skam/skam](https://github.com/berlysia/kanbun-annotation/tree/master/packages/skam) - 型定義・バリデーター
|
|
@@ -91,4 +173,4 @@ skam-renderer {
|
|
|
91
173
|
|
|
92
174
|
## ライセンス
|
|
93
175
|
|
|
94
|
-
|
|
176
|
+
MIT
|
package/dist/auto.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SkamRendererElement
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-HPIH7MRS.js";
|
|
4
4
|
|
|
5
5
|
// src/auto.ts
|
|
6
|
-
if (!customElements.get("skam-renderer")) {
|
|
6
|
+
if (typeof customElements !== "undefined" && !customElements.get("skam-renderer")) {
|
|
7
7
|
customElements.define("skam-renderer", SkamRendererElement);
|
|
8
8
|
}
|
|
@@ -31,7 +31,11 @@ import { parse } from "@kanbun-skam/skam-xml-parser";
|
|
|
31
31
|
import { render, calibrateGridBaseline } from "@kanbun-skam/skam-html-renderer";
|
|
32
32
|
|
|
33
33
|
// src/attribute-map.ts
|
|
34
|
-
import {
|
|
34
|
+
import {
|
|
35
|
+
PROFILES,
|
|
36
|
+
resolveRenderProfile,
|
|
37
|
+
validateRenderOptions
|
|
38
|
+
} from "@kanbun-skam/skam-html-renderer";
|
|
35
39
|
var VALID_WRITING_MODES = ["vertical", "horizontal"];
|
|
36
40
|
var VALID_COPYABLE_ELEMENTS = ["ruby", "okurigana", "soegana", "kaeriten", "okototen"];
|
|
37
41
|
function parseWritingMode(value) {
|
|
@@ -40,18 +44,20 @@ function parseWritingMode(value) {
|
|
|
40
44
|
}
|
|
41
45
|
return "vertical";
|
|
42
46
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
var MAX_ECHO = 64;
|
|
48
|
+
function resolveProfileAttribute(value) {
|
|
49
|
+
if (Object.hasOwn(PROFILES, value)) {
|
|
50
|
+
return resolveRenderProfile({ base: value });
|
|
51
|
+
}
|
|
52
|
+
let parsed;
|
|
47
53
|
try {
|
|
48
|
-
|
|
49
|
-
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
50
|
-
return parsed;
|
|
51
|
-
}
|
|
54
|
+
parsed = JSON.parse(value);
|
|
52
55
|
} catch {
|
|
56
|
+
throw new TypeError(
|
|
57
|
+
`profile must be a preset name or a JSON object (got ${JSON.stringify(value.slice(0, MAX_ECHO))})`
|
|
58
|
+
);
|
|
53
59
|
}
|
|
54
|
-
return
|
|
60
|
+
return resolveRenderProfile(parsed);
|
|
55
61
|
}
|
|
56
62
|
function parseBooleanAttr(value) {
|
|
57
63
|
return value !== null;
|
|
@@ -64,32 +70,107 @@ function parseCopyable(value) {
|
|
|
64
70
|
);
|
|
65
71
|
return elements.length > 0 ? elements : void 0;
|
|
66
72
|
}
|
|
73
|
+
function findUnknownCopyable(value) {
|
|
74
|
+
if (value == null || value === "all") return void 0;
|
|
75
|
+
return value.split(/\s+/).find((v) => v !== "" && !VALID_COPYABLE_ELEMENTS.includes(v));
|
|
76
|
+
}
|
|
77
|
+
function checkAttribute(attribute, probe) {
|
|
78
|
+
try {
|
|
79
|
+
validateRenderOptions(probe);
|
|
80
|
+
return void 0;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return { attribute, error: toTypeError(error) };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function toTypeError(error) {
|
|
86
|
+
return error instanceof TypeError ? error : new TypeError(String(error));
|
|
87
|
+
}
|
|
88
|
+
function parseReadingLayerFlag(value) {
|
|
89
|
+
if (value === "" || value === "true") return true;
|
|
90
|
+
if (value === "false") return false;
|
|
91
|
+
throw new TypeError(
|
|
92
|
+
`include-reading-layer must be "true", "false" or empty (got ${JSON.stringify(value.slice(0, MAX_ECHO))})`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
function parseReadingLayerSource(value) {
|
|
96
|
+
if (value === "readings" || value === "display") return value;
|
|
97
|
+
throw new TypeError('reading-layer-source must be "readings" or "display"');
|
|
98
|
+
}
|
|
99
|
+
function applyHiddenAnnotationAttributes(attrs, options, blocked) {
|
|
100
|
+
if (attrs.profile !== null) {
|
|
101
|
+
try {
|
|
102
|
+
options.profile = resolveProfileAttribute(attrs.profile);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
blocked.push({ attribute: "profile", error: toTypeError(error) });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const readingLayer = attrs["include-reading-layer"];
|
|
108
|
+
if (readingLayer === null) {
|
|
109
|
+
options.includeReadingLayer = true;
|
|
110
|
+
} else {
|
|
111
|
+
try {
|
|
112
|
+
options.includeReadingLayer = parseReadingLayerFlag(readingLayer);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
blocked.push({ attribute: "include-reading-layer", error: toTypeError(error) });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const readingLayerSource = attrs["reading-layer-source"] ?? null;
|
|
118
|
+
if (readingLayerSource !== null) {
|
|
119
|
+
try {
|
|
120
|
+
options.readingLayerSource = parseReadingLayerSource(readingLayerSource);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
blocked.push({ attribute: "reading-layer-source", error: toTypeError(error) });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function checkWritingModeAndCopyable(attrs) {
|
|
127
|
+
const invalid = [];
|
|
128
|
+
const writingMode = attrs["writing-mode"];
|
|
129
|
+
if (writingMode !== null && !VALID_WRITING_MODES.includes(writingMode)) {
|
|
130
|
+
invalid.push({
|
|
131
|
+
attribute: "writing-mode",
|
|
132
|
+
error: new TypeError('writing-mode must be "vertical" or "horizontal"')
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (findUnknownCopyable(attrs.copyable) !== void 0) {
|
|
136
|
+
invalid.push({
|
|
137
|
+
attribute: "copyable",
|
|
138
|
+
error: new TypeError(
|
|
139
|
+
'copyable must be "all" or a space-separated list of known element names'
|
|
140
|
+
)
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return invalid;
|
|
144
|
+
}
|
|
67
145
|
function buildRenderOptions(attrs) {
|
|
68
146
|
const options = {
|
|
69
147
|
writingMode: parseWritingMode(attrs["writing-mode"]),
|
|
70
148
|
useLayer: false
|
|
71
149
|
// Shadow DOM doesn't need @layer
|
|
72
150
|
};
|
|
73
|
-
const profile = parseProfile(attrs.profile);
|
|
74
|
-
if (profile) {
|
|
75
|
-
options.profile = profile;
|
|
76
|
-
}
|
|
77
151
|
if (parseBooleanAttr(attrs.inline)) {
|
|
78
152
|
options.inline = true;
|
|
79
153
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
} else {
|
|
83
|
-
options.includeReadingLayer = attrs["include-reading-layer"] !== "false";
|
|
84
|
-
}
|
|
154
|
+
const blocked = [];
|
|
155
|
+
applyHiddenAnnotationAttributes(attrs, options, blocked);
|
|
85
156
|
const copyable = parseCopyable(attrs.copyable);
|
|
86
157
|
if (copyable) {
|
|
87
158
|
options.copyable = copyable;
|
|
88
159
|
}
|
|
89
|
-
|
|
90
|
-
|
|
160
|
+
const invalid = checkWritingModeAndCopyable(attrs);
|
|
161
|
+
const classPrefix = attrs["class-prefix"];
|
|
162
|
+
if (classPrefix != null) {
|
|
163
|
+
const problem = checkAttribute("class-prefix", { classPrefix });
|
|
164
|
+
if (problem) invalid.push(problem);
|
|
165
|
+
else options.classPrefix = classPrefix;
|
|
91
166
|
}
|
|
92
|
-
|
|
167
|
+
const displayLang = attrs["display-lang"];
|
|
168
|
+
if (displayLang != null) {
|
|
169
|
+
const problem = checkAttribute("display-lang", { displayLang });
|
|
170
|
+
if (problem) invalid.push(problem);
|
|
171
|
+
else options.displayLang = displayLang;
|
|
172
|
+
}
|
|
173
|
+
return { options, invalid, blocked };
|
|
93
174
|
}
|
|
94
175
|
|
|
95
176
|
// src/font-loader.ts
|
|
@@ -148,14 +229,18 @@ function generateVarOverrideCss(prefix) {
|
|
|
148
229
|
--${vp}-letter-spacing: inherit;
|
|
149
230
|
}`;
|
|
150
231
|
}
|
|
151
|
-
var
|
|
232
|
+
var BaseElement = typeof HTMLElement === "undefined" ? class {
|
|
233
|
+
} : HTMLElement;
|
|
234
|
+
var SkamRendererElement = class extends BaseElement {
|
|
152
235
|
static observedAttributes = [
|
|
153
236
|
"writing-mode",
|
|
154
237
|
"profile",
|
|
155
238
|
"inline",
|
|
156
239
|
"include-reading-layer",
|
|
240
|
+
"reading-layer-source",
|
|
157
241
|
"copyable",
|
|
158
242
|
"class-prefix",
|
|
243
|
+
"display-lang",
|
|
159
244
|
"auto-font"
|
|
160
245
|
];
|
|
161
246
|
#shadow;
|
|
@@ -166,6 +251,10 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
166
251
|
#xmlContent;
|
|
167
252
|
#observer = null;
|
|
168
253
|
#rafId = null;
|
|
254
|
+
/** 最後に skam-error で通知した不正値 (属性名 → 値)。同じ値では再通知しない */
|
|
255
|
+
#reportedInvalid = /* @__PURE__ */ new Map();
|
|
256
|
+
/** fail closed で通知済みの属性 → 値。同じ値では再通知しない */
|
|
257
|
+
#reportedBlocked = /* @__PURE__ */ new Map();
|
|
169
258
|
constructor() {
|
|
170
259
|
super();
|
|
171
260
|
this.#shadow = this.attachShadow({ mode: "open" });
|
|
@@ -214,6 +303,8 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
214
303
|
cancelAnimationFrame(this.#rafId);
|
|
215
304
|
this.#rafId = null;
|
|
216
305
|
}
|
|
306
|
+
this.#reportedInvalid.clear();
|
|
307
|
+
this.#reportedBlocked.clear();
|
|
217
308
|
}
|
|
218
309
|
attributeChangedCallback(_name, _oldValue, _newValue) {
|
|
219
310
|
this.#scheduleRender();
|
|
@@ -226,6 +317,13 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
226
317
|
});
|
|
227
318
|
}
|
|
228
319
|
#performRender() {
|
|
320
|
+
const attrs = this.#getAttributeValues();
|
|
321
|
+
const { options, invalid, blocked } = buildRenderOptions(attrs);
|
|
322
|
+
if (blocked.length > 0) {
|
|
323
|
+
this.#renderBlocked(blocked, attrs);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
this.#reportedBlocked.clear();
|
|
229
327
|
const xml = extractXml(this.#xmlContent, this);
|
|
230
328
|
if (!xml) {
|
|
231
329
|
this.#contentDiv.innerHTML = "";
|
|
@@ -234,8 +332,6 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
234
332
|
}
|
|
235
333
|
try {
|
|
236
334
|
const doc = parse(xml);
|
|
237
|
-
const attrs = this.#getAttributeValues();
|
|
238
|
-
const options = buildRenderOptions(attrs);
|
|
239
335
|
const { html, css } = render(doc, options);
|
|
240
336
|
const prefix = options.classPrefix ?? "skam";
|
|
241
337
|
this.#mainStyle.textContent = css + generateVarOverrideCss(prefix);
|
|
@@ -245,6 +341,7 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
245
341
|
calibrateGridBaseline(this.#contentDiv, {
|
|
246
342
|
variablePrefix: prefix
|
|
247
343
|
});
|
|
344
|
+
this.#reportInvalid(invalid, attrs);
|
|
248
345
|
this.dispatchEvent(
|
|
249
346
|
new CustomEvent("skam-render", {
|
|
250
347
|
detail: { document: doc },
|
|
@@ -256,6 +353,57 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
256
353
|
this.#handleError(error);
|
|
257
354
|
}
|
|
258
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* 既定値に戻した属性を回復可能なエラーとして通知する (内容は消さない)。
|
|
358
|
+
* 同じ不正値が残っている間は再描画しても再通知しない。ハンドラが属性や内容を
|
|
359
|
+
* 書き換えたときに、エラー → 再描画 → エラーの連鎖が起きるのを避けるため。
|
|
360
|
+
*/
|
|
361
|
+
#reportInvalid(invalid, attrs) {
|
|
362
|
+
const invalidNames = new Set(invalid.map((i) => i.attribute));
|
|
363
|
+
for (const name of this.#reportedInvalid.keys()) {
|
|
364
|
+
if (!invalidNames.has(name)) this.#reportedInvalid.delete(name);
|
|
365
|
+
}
|
|
366
|
+
for (const { attribute, error } of invalid) {
|
|
367
|
+
const value = attrs[attribute] ?? "";
|
|
368
|
+
if (this.#reportedInvalid.get(attribute) === value) continue;
|
|
369
|
+
this.#reportedInvalid.set(attribute, value);
|
|
370
|
+
this.dispatchEvent(
|
|
371
|
+
new CustomEvent("skam-error", {
|
|
372
|
+
detail: { error, recoverable: true, attribute },
|
|
373
|
+
bubbles: true,
|
|
374
|
+
composed: true
|
|
375
|
+
})
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* 隠す属性が不正なとき、描いた内容を消してメッセージだけを出す。
|
|
381
|
+
* 同じ属性の同じ値では再通知しない (#reportInvalid と同じ理由)。
|
|
382
|
+
* detail には属性の生の値を入れない。値は 64 文字に切り詰めてメッセージに含まれる。
|
|
383
|
+
*/
|
|
384
|
+
#renderBlocked(blocked, attrs) {
|
|
385
|
+
const names = new Set(blocked.map((b) => b.attribute));
|
|
386
|
+
for (const name of this.#reportedBlocked.keys()) {
|
|
387
|
+
if (!names.has(name)) this.#reportedBlocked.delete(name);
|
|
388
|
+
}
|
|
389
|
+
this.#contentDiv.innerHTML = "";
|
|
390
|
+
const alert = document.createElement("div");
|
|
391
|
+
alert.setAttribute("role", "alert");
|
|
392
|
+
alert.textContent = blocked.map((b) => b.error.message).join("\n");
|
|
393
|
+
this.#contentDiv.appendChild(alert);
|
|
394
|
+
for (const { attribute, error } of blocked) {
|
|
395
|
+
const value = attrs[attribute] ?? "";
|
|
396
|
+
if (this.#reportedBlocked.get(attribute) === value) continue;
|
|
397
|
+
this.#reportedBlocked.set(attribute, value);
|
|
398
|
+
this.dispatchEvent(
|
|
399
|
+
new CustomEvent("skam-error", {
|
|
400
|
+
detail: { error, recoverable: false, attribute },
|
|
401
|
+
bubbles: true,
|
|
402
|
+
composed: true
|
|
403
|
+
})
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
259
407
|
#handleError(error) {
|
|
260
408
|
const message = error instanceof Error ? error.message : String(error);
|
|
261
409
|
this.#contentDiv.innerHTML = "";
|
|
@@ -265,7 +413,7 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
265
413
|
this.#contentDiv.appendChild(alert);
|
|
266
414
|
this.dispatchEvent(
|
|
267
415
|
new CustomEvent("skam-error", {
|
|
268
|
-
detail: { error },
|
|
416
|
+
detail: { error, recoverable: false },
|
|
269
417
|
bubbles: true,
|
|
270
418
|
composed: true
|
|
271
419
|
})
|
|
@@ -292,12 +440,15 @@ var SkamRendererElement = class extends HTMLElement {
|
|
|
292
440
|
inline: this.getAttribute("inline"),
|
|
293
441
|
"include-reading-layer": this.getAttribute("include-reading-layer"),
|
|
294
442
|
copyable: this.getAttribute("copyable"),
|
|
295
|
-
"class-prefix": this.getAttribute("class-prefix")
|
|
443
|
+
"class-prefix": this.getAttribute("class-prefix"),
|
|
444
|
+
"display-lang": this.getAttribute("display-lang"),
|
|
445
|
+
"reading-layer-source": this.getAttribute("reading-layer-source")
|
|
296
446
|
};
|
|
297
447
|
}
|
|
298
448
|
};
|
|
299
449
|
|
|
300
450
|
export {
|
|
301
451
|
SKAM_XML_MEDIA_TYPE,
|
|
452
|
+
extractXmlFromScriptElement,
|
|
302
453
|
SkamRendererElement
|
|
303
454
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* SKAM の HTML レンダリングパイプラインを Web Component として提供する。
|
|
5
5
|
* Shadow DOM でスタイルをカプセル化し、CSS Variables による外部カスタマイズを可能にする。
|
|
6
6
|
*/
|
|
7
|
-
declare
|
|
7
|
+
declare const BaseElement: typeof HTMLElement;
|
|
8
|
+
declare class SkamRendererElement extends BaseElement {
|
|
8
9
|
#private;
|
|
9
10
|
static observedAttributes: string[];
|
|
10
11
|
constructor();
|
|
@@ -37,6 +38,14 @@ interface AttributeValues {
|
|
|
37
38
|
'include-reading-layer': string | null;
|
|
38
39
|
copyable: string | null;
|
|
39
40
|
'class-prefix': string | null;
|
|
41
|
+
/** 任意。省略は null と同じ (export 済みの型なので既存の利用者を壊さない) */
|
|
42
|
+
'display-lang'?: string | null;
|
|
43
|
+
/**
|
|
44
|
+
* 任意。省略は null と同じ (readings)。`readings` / `display` 以外は不正で、描画しない。
|
|
45
|
+
*
|
|
46
|
+
* @experimental
|
|
47
|
+
*/
|
|
48
|
+
'reading-layer-source'?: string | null;
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
/**
|
|
@@ -47,6 +56,16 @@ interface AttributeValues {
|
|
|
47
56
|
* 2. <script type="application/vnd.berlysia.skam+xml"> 子要素の textContent
|
|
48
57
|
* 3. Light DOM の textContent(フォールバック)
|
|
49
58
|
*/
|
|
59
|
+
/**
|
|
60
|
+
* SKAM-ML を HTML に埋め込むときのメディアタイプ。仕様の約束であり、実装の都合では変えない。
|
|
61
|
+
* 根拠と範囲は SKAM-ML-draft §3.3 を参照。
|
|
62
|
+
*/
|
|
50
63
|
declare const SKAM_XML_MEDIA_TYPE = "application/vnd.berlysia.skam+xml";
|
|
64
|
+
/**
|
|
65
|
+
* <script type="application/vnd.berlysia.skam+xml"> 要素からXMLを取得する
|
|
66
|
+
*
|
|
67
|
+
* @experimental 0.x の minor で変わりうる。
|
|
68
|
+
*/
|
|
69
|
+
declare function extractXmlFromScriptElement(host: HTMLElement): string | null;
|
|
51
70
|
|
|
52
|
-
export { type AttributeValues, SKAM_XML_MEDIA_TYPE, SkamRendererElement };
|
|
71
|
+
export { type AttributeValues, SKAM_XML_MEDIA_TYPE, SkamRendererElement, extractXmlFromScriptElement };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SKAM_XML_MEDIA_TYPE,
|
|
3
|
-
SkamRendererElement
|
|
4
|
-
|
|
3
|
+
SkamRendererElement,
|
|
4
|
+
extractXmlFromScriptElement
|
|
5
|
+
} from "./chunk-HPIH7MRS.js";
|
|
5
6
|
export {
|
|
6
7
|
SKAM_XML_MEDIA_TYPE,
|
|
7
|
-
SkamRendererElement
|
|
8
|
+
SkamRendererElement,
|
|
9
|
+
extractXmlFromScriptElement
|
|
8
10
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kanbun-skam/skam-web-component",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "SKAM Web Component - <skam-renderer> カスタムエレメント",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": [
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
],
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
14
|
},
|
|
15
15
|
"./auto": {
|
|
16
|
+
"types": "./dist/auto.d.ts",
|
|
16
17
|
"import": "./dist/auto.js"
|
|
17
18
|
}
|
|
18
19
|
},
|
|
@@ -45,8 +46,8 @@
|
|
|
45
46
|
"node": ">=18.0.0"
|
|
46
47
|
},
|
|
47
48
|
"dependencies": {
|
|
48
|
-
"@kanbun-skam/skam-
|
|
49
|
-
"@kanbun-skam/skam-
|
|
49
|
+
"@kanbun-skam/skam-xml-parser": "0.2.0",
|
|
50
|
+
"@kanbun-skam/skam-html-renderer": "0.2.0"
|
|
50
51
|
},
|
|
51
52
|
"scripts": {
|
|
52
53
|
"build": "tsup",
|