From c3defc4054d32bf91e2cfb7ae11a0b2251c68caa Mon Sep 17 00:00:00 2001 From: commilitia Date: Fri, 22 May 2026 22:18:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=EF=BC=9Acjk-autospace=20?= =?UTF-8?q?=E5=B7=A5=E5=BB=A0=E5=87=BD=E5=BC=8F=20+=20TS=20=E5=AE=A3?= =?UTF-8?q?=E5=91=8A=20+=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 雙 pass 架構: - Pass A — 對每個 BLOCK 走 DFS 收集邏輯文本(樣式標籤透明、PILL 與子 BLOCK 中斷 run),套 panguSpace,把 MARK 反映射回 textNode / offset 並插入 .cjk-autospace marker span - Pass B — 對每個 PILL(pillSelector)透過樣式鏈遞迴找邏輯鄰字, 按 0 / 1 / 多 + 特殊規則處理空白 工廠 createCjkAutospace(options) 接受 pillSelector / styleInlines / blockTags / skipTags / autospaceClass / skipAttribute / isSkipped 等 配置。UMD-ish export 同時支援 ESM、CommonJS、browser global 與 inline script 形式。 規則表 port 自 Pangu.js v7.2.1(MIT),加上跨樣式邊界透明與行內塊 邊界兩 pass 為原創設計。 --- .gitignore | 3 + README.md | 62 ++++++ cjk-autospace.d.ts | 50 +++++ cjk-autospace.js | 478 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 593 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cjk-autospace.d.ts create mode 100644 cjk-autospace.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..879df49 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules/ +*.log diff --git a/README.md b/README.md new file mode 100644 index 0000000..d22d687 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# cjk-autospace + +CJK ↔ Latin / 數字 / 標點 / 引號 / 括號 / 運算子等邊界之自動間距 shim。Pangu.js v7.2.1 規則之 ES5 端口,加上「跨樣式標籤透明 + 行內塊邊界」兩 pass 架構。工廠模式,由消費端注入配置。 + +## API + +```js +const autospace = createCjkAutospace({ + pillSelector: "code, kbd, .your-pill-class", +}); +autospace.apply(document.body); +``` + +回傳 `{ apply(root?) }`。冪等:在已注入 marker 之 DOM 上重跑會跳過既有 `.cjk-autospace`。完整選項見 `cjk-autospace.d.ts`。 + +## CSS 需求 + +```css +.cjk-autospace { + -webkit-user-select: none; + user-select: none; + /* 視覺寬度由消費端決定,例如 0.25em via inline-block + width */ +} +``` + +`user-select: none` 讓選取與剪貼板跳過 marker;marker 內含真實 ASCII space(layout 視為 whitespace:行末 collapse、容許換行、justify 可拉伸)。 + +## 標籤三分類 + +- **STYLE_INLINES**(`` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `` / `