HTML 和 DOM
在元素上挂载适配器以测量字体和容器宽度。移除元素时调用 destroy,更改设置时调用 update。
无需打包工具的静态 HTML
使用固定版本的 ESM 模块 URL,找到可测量的元素后进行挂载。
html
<!doctype html>
<html lang="zh-Hans">
<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: 'zh-Hans' });
</script>
</html>npm 和构建工具
导入 DOM 适配器,根据字符串模式传入 text,已有标记则传入 inline: true。
ts
import { mountJuliya } from '@oddmash/juliya/dom';
const controller = mountJuliya(element, { text: "让每一行自然易读。", locale: 'zh-Hans' });
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: 'zh-Hans' });选项
可指定 locale、strategy、breakLongWords、forcedBreaks;DOM 适配器测量字体与内容宽度。
| 选项 | 必填 | 默认值 | 用途 |
|---|---|---|---|
text | 条件必填 | - | 字符串模式原文;内联模式省略 |
inline | 条件必填 | false | 已有内联标记时设为 true |
locale | 可选 | ko | 语言规则;React 还读取最近的 Provider |
strategy | 可选 | balanced | balanced 或 greedy 布局 |
breakLongWords | 可选 | true | 允许在字素边界拆分长词 |
forcedBreaks | 可选 | - | 原文 UTF-16 边界偏移量 |
更新与清理
使用 update 更新选项、refresh 重新测量,移除元素时调用 destroy。
ts
controller.update({ text: "让每一行自然易读。", locale: 'zh-Hans' });
controller.refresh();
controller.destroy();宽度和文本变化
观察字体与容器宽度变化,文本改变时更新控制器。
不拥有 DOM 的观察 API
通过 observeJuliya 回调获取 lines 与 measured,由调用者自行渲染。
ts
const controller = observeJuliya(element, { text, locale: 'zh-Hans' }, ({ lines, measured }) => render(lines, measured));Canvas 测量
使用 createCanvasMeasure 创建与字体和间距一致的测量函数,再传给 wrap。
ts
import { createCanvasMeasure } from '@oddmash/juliya/dom';
const measure = createCanvasMeasure({ font: '400 24px sans-serif' });