HTMLとDOM
要素にアダプターを適用するとフォントとコンテナ幅を測定します。要素削除時はdestroy、設定変更時はupdateを使用します。
バンドラーなしの静的HTML
バージョンを固定したESM配布URLをインポートし、測定可能な要素にマウントします。
html
<!doctype html>
<html lang="ja">
<meta charset="utf-8">
<title>Juliya の例</title>
<p id="title" style="max-width: 220px; font-size: 24px">チームの資料をひとつの場所にまとめましょう。</p>
<script type="module">
import { mountJuliya } from 'https://cdn.jsdelivr.net/npm/@oddmash/juliya@0.6.0/dist/dom/index.js/+esm';
const title = document.getElementById('title');
mountJuliya(title, { text: title.textContent, locale: 'ja' });
</script>
</html>npmとビルドツール
パッケージからDOMアダプターをインポートし、textまたはinline: trueを渡します。
ts
import { mountJuliya } from '@oddmash/juliya/dom';
const controller = mountJuliya(element, { text: "文章を自然に読みやすく。", locale: 'ja' });
controller.destroy();言語選択
localeはko、en、ja、zh、zh-Hansに対応し、省略時は韓国語です。
インラインマークアップ
inline: trueでリンクや強調を保ちます。textと同時指定しないでください。
ts
import { mountJuliya } from '@oddmash/juliya/dom';
const controller = mountJuliya(element, { inline: true, locale: 'ja' });オプション
locale、strategy、breakLongWords、forcedBreaksを指定できます。幅とフォントはDOMから測定します。
| オプション | 必須 | 既定値 | 役割 |
|---|---|---|---|
text | 条件付き必須 | - | 文字列モードの原文、inlineでは省略 |
inline | 条件付き必須 | false | 既存インライン要素ではtrue |
locale | 任意 | ko | 言語規則、Reactは最も近いProviderも参照 |
strategy | 任意 | balanced | balancedまたはgreedy |
breakLongWords | 任意 | true | 長い語を書記素境界で分割 |
forcedBreaks | 任意 | - | 原文のUTF-16境界位置 |
更新と解除
updateで設定を更新、refreshで再測定、destroyで解除します。
ts
controller.update({ text: "文章を自然に読みやすく。", locale: 'ja' });
controller.refresh();
controller.destroy();幅や文章の変更
画面幅やフォントの変化は再計測されます。文字列が変わる場合はコントローラーを更新します。
DOMを所有しない観測API
observeJuliyaでコールバックにlinesとmeasuredを受け取り、独自に描画できます。
ts
const controller = observeJuliya(element, { text, locale: 'ja' }, ({ lines, measured }) => render(lines, measured));Canvas測定
createCanvasMeasureでフォントと間隔に一致する測定関数を作り、wrapに渡します。
ts
import { createCanvasMeasure } from '@oddmash/juliya/dom';
const measure = createCanvasMeasure({ font: '400 24px sans-serif' });