dsh-composer-live 0.2.13 → 0.2.16

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/CHANGELOG.md CHANGED
@@ -2,6 +2,37 @@
2
2
 
3
3
  > 更新日志以中文记录。Changelog entries are written in Chinese.
4
4
 
5
+ ## 0.2.16(2026-10-24)
6
+
7
+ **选中一段文字加粗/斜体/行内码后,选区后面的字消失(用户实测反馈「翻转头去选择一部分文字用 Ctrl+B,在它后面的有一些字就消失了」)。**
8
+
9
+ - **根因(行内路径)**:`applyWrapAction` 的光标恢复用 `requestAnimationFrame`、选区替换用 `setTimeout(0)` 的 execCommand——页面有高频渲染活动(caret 闪烁/React 动效)时 rAF 会在被 clamp 的定时器**之前**执行,把 DOM 选区改写成 `[start, start+insert.length)`,随后的 execCommand 就地替换该选区 → **恰好吞掉 insert 标记字符数(bold 为 4 个)的正文**。是否触发取决于 keydown 时刻的帧相位:用户自然操作节奏经常踩中(diag-ctrlb-rate 在活跃页面 100% 复现),单行全选/尾部选区恰好不敏感(旧 E2E 的 Q6 因此漏检)。
10
+ - **根因(跨行路径)**:选区含换行(`act.insert` 含 `\n`)时直接对选区跑 `insertInline` 的 beforeinput 逐行序列——与 Lexical 内部 selection 状态不同步(Tab 块缩进当年同款坑),产生内容丢失/重复(diag-ctrlb 的 C1/B4 实锤)。
11
+ - **修法**:行内分支光标恢复改为排在 execCommand **完成之后**的 setTimeout 链(时序上不可能再先于插入);跨行/代码块分支改走「先删后插」三步链(重建选区 → 合成 deleteContentBackward → commit 后光标落 start → 插入完整内容 → 恢复选区)。
12
+ - **E2E 体系大扩充(62 → 129 场景)**:S 组(12 种用户选择方式——真实鼠标拖选/双击选词/三击选段/Shift+Click/Shift+方向键/行选择,CDP trusted 事件)+ W 组(20 种格式化形态——行首/行中/行尾/跨行 br/字面 \n/行尾边界/光标态/嵌套/链式/含标记/含 emoji/列表引用行内/撤销)+ X 组(10 个 CDP trusted 逐字打字通道场景——**合成 beforeinput 逐字符在 Lexical 下失效(只进 1 字),此前矩阵根本没有真实打字通道**)+ D 组(9 种删除变体——选中删/替换删/Ctrl+Backspace 删词/跨行合并)+ U 组(7 条撤销重做链)+ P 组(5 个刷新多 p/粘贴通道格式化)。
13
+ - **测试基建修三层假阴性**(都由本次「测不出用户 bug」倒逼出来的):① runner 的 `cdp.eval` 缺 `awaitPromise:true`——所有 async op 的断言失败值被丢弃(拿到空对象=恒 PASS);② headless 空闲页面 compositor 降频、rAF 稀疏——时序竞争路径根本不执行,stressWrap 内置「每帧真实重绘」的 rAF 心跳还原真实用户页面节奏;③ 单次断言对帧相位敏感——stressWrap 用「execCommand 时刻选区」确定性断言(插入前选区被改写即报错,与帧相位无关)+ 12 轮 0-80ms 相位扫描 + 文本精确比对双保险。
14
+ - **新增 CDP trusted op**(run-e2e.mjs 拦截执行,合成事件不触发浏览器默认行为的结构性盲区):`cdpType`(Input.insertText 逐字打字)/ `cdpKey`(trusted keydown)/ `mouseSelect`(真实鼠标拖选)/ `dblClick` / `tripleClick` / `shiftClick`;页内新增 op:`typeChars`(execCommand 逐字符)/ `deleteKey`(前向删除)/ `selectWord` / `expectSel`(选区投影区间断言)/ `stressWrap`(时序压力探测器)。
15
+ - 单测 188 → 206 项(`_decideWrapAction` 此前零覆盖,补 18 条形态测试)。
16
+
17
+
18
+ **输入 `/` 命令/技能名时文字无颜色、hover 被色块遮挡(用户 0.2.1 实测反馈)。**
19
+
20
+ - **根因**:0.2.x 官方把 `/anatomy` 这类技能命令识别为「文本引用装饰」`[data-composer-text-ref]`——span 带 `color: var(--dsw-alias-state-business-primary)`(主题引用色)与 `:hover { background: var(--dsw-alias-state-business-tertiary) }`。插件把编辑器文字 `-webkit-text-fill-color: transparent` 后由 overlay 重绘,装饰的 `color` 因此不可见(文字显示为普通正文色);而 hover 背景留在编辑器层(`z-index: 1`,画在 overlay 之上),会整块盖住渲染文字。
21
+ - **修法**:`buildAnchors` 记录 `[data-composer-text-ref]` 的投影区间(挂在 `anchors.refs`),`renderCharOverlay` 按绝对 offset 给对应字符加 `dsh-cl-ref` class,颜色取同一主题变量 `--dsw-alias-state-business-primary`(明暗主题自动跟随,取不到时回退 `#3b82f6`);同时注入 `[data-composer-text-ref]:hover { background: transparent !important }` 禁掉编辑器层 hover 背景,杜绝遮挡。
22
+ - 单测 188 项通过。
23
+
24
+ ## 0.2.14(2026-10-06)
25
+
26
+ **工具栏迁出输入卡:放卡外正上方,不再占输入框内部空间(用户反馈「工具栏阻碍输入框空间」)。**
27
+
28
+ - **DOM 位置**:`.dsh-cl-toolbar` 从「输入卡内、编辑器滚动容器顶部(v0.2.8 sticky)」迁到「card 的父容器内、card 前一格」——输入卡外、正上方,视觉为与卡等宽的独立细条(36px 高、圆角 12px、明暗两套底色阴影、与卡 6px 间距)。编辑器 padding-top 让位(v0.2.8 的 BAR_RESERVE 6px)随 sticky 体系整体退役,输入框恢复官方原始内距。
29
+ - **宽度同步**:父容器(`uV2eYG_root`,flex column + align-items:flex-start)内容宽 ≠ card 宽,CSS `width:100%` 会比卡突出——搬正逻辑每次从 `card.offsetWidth` 同步(有差异才写,避免每次 run 触发样式失效)。
30
+ - **card 重建防双份**:工具栏跨 card 重建存活(hero 切换/会话切换后旧工具栏仍留在父容器),setupCard 先查父容器复用(接管 `__dshClToolbar`/`__dshClBtns` 引用)再谈新建;expand 按钮闭包改 `findCard()` 现查(格式键本就现查)。卸载清理改引用直取 + `document.querySelectorAll` 全局兜底(card 已消失时父容器查找清不到)。
31
+ - **React 重排自愈**:官方重建父容器 children 时不认识外来节点,工具栏可能被挤错位——搬正逻辑(每次 setupCard/run 跑)持续拉回 card 前一格。
32
+ - **代价说明**:工具栏卡外后占文档流 36px,消息区可视高度相应少 36px(消息区自身可滚动,内容无损)。
33
+ - E2E:`scrollToolbar` op 重写为「卡外位置 + 与卡等宽 + 长文滚动时输入区(工具栏+输入框)整体固定」断言(旧 sticky 钉顶语义退役);`toolbarClear` 查找范围从 card 内改 document 级。场景 R8 更名 `R8-toolbar-outside-card`。
34
+ - 单测 188 项通过;8124(0.1.5-rc.2)E2E 全绿后发布。
35
+
5
36
  ## 0.2.13(2026-10-01)
6
37
 
7
38
  **发布仓库隐私整改 + E2E 工具通用化(插件本体零改动)。**
package/README.md CHANGED
@@ -55,7 +55,7 @@ It is a **pure browser-side plugin**: zero runtime dependencies, no service inje
55
55
 
56
56
  ### Toolbars and shortcuts
57
57
 
58
- - **Sticky top toolbar**: B / italic / code / link / list / code block / expand. Wraps the selection, or inserts empty markers at the caret; B lights up when the caret sits inside `**…**`. The bar is pinned to the top of the scroll area, so it never overlaps content or image attachments.
58
+ - **Format toolbar (outside the composer card since v0.2.14)**: B / italic / code / link / list / code block / expand. Wraps the selection, or inserts empty markers at the caret; B lights up when the caret sits inside `**…**`. The bar is a standalone strip directly above the composer card, card-aligned width, taking no space inside the input area — it never overlaps content or image attachments.
59
59
  - **Selection floating format bar** (Open WebUI style): select text and B/italic/code/link appear above the selection.
60
60
  - **Ctrl+B / Ctrl+I / Ctrl+E** for bold / italic / inline code — which also blocks the browser's native contenteditable bold that would silently insert real `<b>` tags into the draft.
61
61
 
@@ -68,7 +68,7 @@ It is a **pure browser-side plugin**: zero runtime dependencies, no service inje
68
68
  - **Esc** does the right thing in context: open candidate menus → official handler; expanded composer → collapse; generation in progress → stop generating.
69
69
  - One-key **70vh expand** for long-form writing.
70
70
  - Light/dark theme aware; IME-safe (composition input never flashes); rendering is rAF-batched and incremental.
71
- - **@ mention chips** stay official and editable. Paragraphs containing chips gracefully degrade to official rendering (chip widths cannot be replicated; zero misalignment wins). Text decorations are unaffected.
71
+ - **@ mention chips** stay official and editable. Paragraphs containing chips gracefully degrade to official rendering (chip widths cannot be replicated; zero misalignment wins). Text decorations are unaffected — `/` commands and skill-name references are painted in the official reference colour, and the official hover background is disabled (it paints above the render layer and would cover the text).
72
72
  - Also fixes two official positioning glitches when image attachments are present: the trigger menu floating far above the composer, and toolbars overlapping thumbnails.
73
73
 
74
74
  ### Not duplicated from DSH 0.1.5
package/README.zh-CN.md CHANGED
@@ -55,7 +55,7 @@ dsh-composer-live 把 DSH Web 的输入框变成 Open WebUI 风格的实时编
55
55
 
56
56
  ### 工具栏与快捷键
57
57
 
58
- - **常驻顶部工具栏**:加粗 / 斜体 / 行内代码 / 链接 / 列表 / 代码块 / 展开。选中文字时包裹、未选中插入空标记;光标落在 `**…**` 内时加粗键自动点亮。工具栏钉在滚动区顶部,永不与正文或图片附件重叠。
58
+ - **常驻格式工具栏(v0.2.14 起在输入卡外正上方)**:加粗 / 斜体 / 行内代码 / 链接 / 列表 / 代码块 / 展开。选中文字时包裹、未选中插入空标记;光标落在 `**…**` 内时加粗键自动点亮。工具栏是与输入卡等宽的独立细条,不占输入框内部高度,也永不与正文或图片附件重叠。
59
59
  - **选区浮动格式条**(Open WebUI 形态):选中一段文字,加粗/斜体/代码/链接四键浮现在选区上方。
60
60
  - **Ctrl+B / Ctrl+I / Ctrl+E** 快捷加粗 / 斜体 / 行内代码——同时拦掉浏览器对 contenteditable 的原生加粗(那会悄悄往草稿里插入真正的 `<b>` 标签)。
61
61
 
@@ -68,7 +68,7 @@ dsh-composer-live 把 DSH Web 的输入框变成 Open WebUI 风格的实时编
68
68
  - **Esc** 按语境做对的事:官方候选菜单开着 → 归官方处理;展开态 → 收起;AI 生成中 → 停止生成。
69
69
  - 一键**展开 70vh** 长文写作模式。
70
70
  - 明暗主题自适应;输入法安全(拼音组字不闪烁);渲染按 rAF 合帧、增量进行。
71
- - **@ 引用胶囊**保持官方原样、可编辑。含胶囊的段落优雅降级为官方显示(胶囊宽度无法复刻,零错位优先)。热词文本装饰不受影响。
71
+ - **@ 引用胶囊**保持官方原样、可编辑。含胶囊的段落优雅降级为官方显示(胶囊宽度无法复刻,零错位优先)。热词文本装饰不受影响——`/` 命令与技能名的引用装饰在渲染层沿用官方引用色,官方 hover 背景已禁用(它画在渲染层之上,会盖住文字)。
72
72
  - 顺带修复官方在图片附件在场时的两个定位问题:候选菜单悬空浮出输入框、工具栏盖住缩略图。
73
73
 
74
74
  ### 不与 DSH 0.1.5 重复
package/client.js CHANGED
@@ -85,7 +85,7 @@ window.__ModuleLoader__.load({
85
85
  /** 「停止生成」按钮 aria-label(i18n 双语;实测校正清单见 README)。 */
86
86
  const STOP_BTN_HINTS = ["停止生成", "Stop generating", "停止"];
87
87
  /** 工具栏高度 + 呼吸(占位同步值)。 */
88
- const BAR_RESERVE = "6px"; // v0.2.8:sticky 工具栏在流内占 38px,编辑器 padding 只留呼吸(原 absolute 方案的 44px 占位退役)
88
+ const TB_HEIGHT = "36px"; // v0.2.14:卡外独立条高度(原卡内 sticky 的 38px + BAR_RESERVE 让位整体退役)
89
89
  //#endregion
90
90
 
91
91
  //#region dsh-composer-live: 安全工具
@@ -434,7 +434,7 @@ window.__ModuleLoader__.load({
434
434
  }
435
435
 
436
436
  /** 字符锚定渲染主入口:清空 overlay,按测量坐标输出「整块背景 + 定位字符段」。 */
437
- function renderCharOverlay(ov, input, anchors, lines, states) {
437
+ function renderCharOverlay(ov, input, anchors, lines, states, refs) {
438
438
  const ovRect = ov.getBoundingClientRect();
439
439
  const ox = ovRect.left, oy = ovRect.top;
440
440
  const segLineHeight = getComputedStyle(ov).lineHeight; // 段定位的 half-leading 修正用
@@ -446,6 +446,10 @@ window.__ModuleLoader__.load({
446
446
  const contentLeft = inpRect.left + pl - 12;
447
447
  const contentW = inpRect.width - pl - pr + 24;
448
448
  let out = "";
449
+ // 每行在投影文本中的起始 offset(refs 区间按绝对 offset 匹配字符)
450
+ const lineStarts = [];
451
+ let lineAcc = 0;
452
+ for (const l of lines) { lineStarts.push(lineAcc); lineAcc += l.length + 1; }
449
453
  // ===== 块级背景(代码块整块一个矩形:垂直无缝;引用段同理) =====
450
454
  // 逐行画背景会按行内容宽度锯齿 + 行间露缝——聚合到块级:从块首行首字符顶
451
455
  // 到块末行末字符底 ±2.5px,水平取容器内容宽。块内空行天然被矩形覆盖。
@@ -503,6 +507,20 @@ window.__ModuleLoader__.load({
503
507
  const tl = tk.text.length;
504
508
  for (let k = 0; k < tl && ci < chars.length; k++, ci++) charCls[ci] = tk.cls;
505
509
  }
510
+ // 官方文本引用装饰(/ 命令、技能名)→ 引用色。编辑器文字透明,官方给
511
+ // 装饰 span 的 color 看不见;按投影区间在渲染层补上同一主题色。
512
+ if (refs.length) {
513
+ const base = lineStarts[li] || 0;
514
+ for (let k = 0; k < chars.length; k++) {
515
+ const abs = base + chars[k].off;
516
+ for (const r of refs) {
517
+ if (abs >= r.start && abs < r.end) {
518
+ charCls[k] = (charCls[k] ? charCls[k] + " " : "") + "dsh-cl-ref";
519
+ break;
520
+ }
521
+ }
522
+ }
523
+ }
506
524
  // 合并「同样式 + 同视觉行 + 横向连续」的字符成段(段内浏览器排版同字体零差)。
507
525
  // 等宽整框语义:文档含代码块时全部段用等宽合成栈(与编辑器 MONO_ALL 同源变量)。
508
526
  const anyCode = states.some((st) => st.inCode);
@@ -822,18 +840,13 @@ window.__ModuleLoader__.load({
822
840
  function styleText() {
823
841
  return (
824
842
  "#" + STYLE_ID + "{}" +
825
- // 常驻工具栏(absolute 横贯输入卡顶部):占位由 input/placeholder 的
826
- // padding-top 同步让出(overlay 的 padding 每次渲染从 input 复制)。
827
- "." + TOOLBAR + "{position:sticky;top:0;left:0;right:0;z-index:60;display:flex;align-items:center;box-sizing:border-box;height:38px;padding:0 10px;gap:2px;background:rgba(28,30,36,.95);border-bottom:1px solid rgba(127,127,127,.16);}" +
828
- // sticky 工具栏钉滚动视口顶(v0.2.8):内容从其下方滚过永不重叠;附件栏在
829
- // 滚动容器外,scroll 顶=附件栏底,天然让位(v0.2.7 的让位变量退役)。背景色
830
- // 必须不透明——滚动内容从下方经过(实测反馈「工具栏透明」问题的根治)。
831
- "body.dsh-cl-light ." + TOOLBAR + "{background:rgba(250,251,253,.97);border-bottom-color:rgba(0,0,0,.08);}" +
843
+ // 常驻工具栏(v0.2.14:输入卡外、卡正上方的独立条):不再占输入框内部
844
+ // 高度——编辑器 padding 让位(v0.2.8 的 BAR_RESERVE)随 sticky 体系整体
845
+ // 退役;宽度由搬正逻辑从 card 同步(父容器内容宽 ≠ card 宽,width:100%
846
+ // 会比卡突出)。背景不透明(沿用 v0.2.8 教训:透底会与后方内容混色)。
847
+ "." + TOOLBAR + "{display:flex;align-items:center;box-sizing:border-box;height:" + TB_HEIGHT + ";padding:0 12px;gap:2px;margin:0 0 6px;border-radius:12px;background:rgba(28,30,36,.95);box-shadow:rgba(0,0,0,.25) 0px 1px 8px 0px;}" +
848
+ "body.dsh-cl-light ." + TOOLBAR + "{background:rgba(250,251,253,.97);box-shadow:rgba(61,55,48,.125) 0px 0px 0px 0.5px,rgba(0,0,0,.04) 0px 1px 8px 0px;}" +
832
849
  "." + TOOLBAR + " [data-cl=\"expand\"]{margin-left:auto;}" +
833
-
834
- // 编辑层与占位层让出工具栏一行(38px 工具栏 + 6px 呼吸)
835
- CARD_SEL + " " + INPUT_SEL + "{padding-top:" + BAR_RESERVE + " !important;}" +
836
- CARD_SEL + " " + PLACEHOLDER_SEL + "{padding-top:" + BAR_RESERVE + " !important;box-sizing:border-box;}" +
837
850
  // 透明化:只隐藏文字「绘制」(-webkit-text-fill-color),color 属性保留
838
851
  // ——caret 颜色、官方 hint ::after、chip 恢复全靠 color 不动。
839
852
  // 提层:编辑器自身 z-index:1 画在 overlay(z:0)之上——caret/选区是编辑器
@@ -905,6 +918,12 @@ window.__ModuleLoader__.load({
905
918
  // 字符段:绝对定位画在编辑器字符 rect 上;字体继承 overlay(syncMetrics
906
919
  // 复制官方度量);样式类只改 paint(伪粗/染色/胶囊背景外扩),零 advance 变化。
907
920
  ".dsh-cl-c{position:absolute;white-space:pre;pointer-events:none;color:inherit;font-family:inherit;}" +
921
+ // 官方文本引用装饰(/ 命令、技能名):渲染层用同一主题引用色。
922
+ // 编辑器层文字已透明,官方给装饰 span 的 color 看不见(此前显示为
923
+ // 普通正文色);hover 背景同样在编辑器层(z-index:1,画在 overlay
924
+ // 之上),会整块盖住渲染文字——禁掉,常驻引用色已足够辨识。
925
+ ".dsh-cl-c.dsh-cl-ref{color:var(--dsw-alias-state-business-primary,#3b82f6);}" +
926
+ INPUT_SEL + "." + HOST + " " + TEXT_REF_SEL + ":hover{background:transparent !important;}" +
908
927
  ".dsh-cl-c.dsh-cl-codefont{font-family:var(--dsh-cl-code-font,var(--ds-font-family-code,monospace));}" +
909
928
  ".dsh-cl-c.dsh-cl-strong,.dsh-cl-c.dsh-cl-heading{font-weight:inherit;-webkit-text-stroke:.45px currentColor;}" +
910
929
  ".dsh-cl-c.dsh-cl-em{font-style:italic;}" +
@@ -1025,6 +1044,9 @@ window.__ModuleLoader__.load({
1025
1044
  */
1026
1045
  function buildAnchors(input) {
1027
1046
  const anchors = [];
1047
+ // 官方文本引用装饰([data-composer-text-ref],0.2.x 用于 / 命令与技能名)的
1048
+ // 投影区间:渲染层据此给字符上引用色(编辑器文字已透明,官方 color 不可见)。
1049
+ const refs = [];
1028
1050
  if (!input) return anchors;
1029
1051
  let firstPara = true;
1030
1052
  const visit = (node) => {
@@ -1046,6 +1068,15 @@ window.__ModuleLoader__.load({
1046
1068
  continue;
1047
1069
  }
1048
1070
  if (c.matches && c.matches(CHIP_HOST_SEL)) { anchors.push({ kind: "chip", el: c, len: 1 }); continue; }
1071
+ if (c.matches && c.matches(TEXT_REF_SEL)) {
1072
+ // 引用/命令装饰:内容仍是普通文本(非原子 chip),记录投影区间
1073
+ // 供渲染层染色;文本照常下钻进锚表。
1074
+ const start = anchorsText(anchors).length;
1075
+ visit(c);
1076
+ const end = anchorsText(anchors).length;
1077
+ if (end > start) refs.push({ start, end });
1078
+ continue;
1079
+ }
1049
1080
  // 其余元素(B/I/STRONG/DIV 等富文本粘贴产物、装饰壳)一律继续
1050
1081
  // 下钻收集文字——「忽略内容」会把粘贴的富文本结构整个漏掉:锚表
1051
1082
  // 空转 → 渲染层空白 + 透明化已生效 = 大段粘贴「一片空白」(文字
@@ -1055,6 +1086,7 @@ window.__ModuleLoader__.load({
1055
1086
  }
1056
1087
  };
1057
1088
  visit(input);
1089
+ anchors.refs = refs;
1058
1090
  return anchors;
1059
1091
  }
1060
1092
 
@@ -1253,7 +1285,7 @@ window.__ModuleLoader__.load({
1253
1285
  fallbackToOfficial();
1254
1286
  } else {
1255
1287
  try {
1256
- renderCharOverlay(ov, input, anchors, lines, states);
1288
+ renderCharOverlay(ov, input, anchors, lines, states, anchors.refs || []);
1257
1289
  if (hasContent && ov.querySelectorAll(".dsh-cl-c").length === 0) fallbackToOfficial();
1258
1290
  } catch (err) {
1259
1291
  fallbackToOfficial();
@@ -1333,13 +1365,60 @@ window.__ModuleLoader__.load({
1333
1365
  setSelectionByOffset(input, act.at, act.at);
1334
1366
  insertInline(input, act.insert);
1335
1367
  } else if (act.insert.indexOf("\n") === -1) {
1336
- // 行内格式(bold/italic/code/link):替换选区(单行)
1368
+ // 行内格式(bold/italic/code/link):替换选区(单行)。
1369
+ // 光标恢复必须排在插入完成【之后】(setTimeout 链)——原 rAF 实现在
1370
+ // 「帧先于定时器」的相位下先执行,把选区改写成 [start, start+insert.length),
1371
+ // 随后 execCommand 就地替换 → 选区后恰好标记字符数的正文被吞(用户报
1372
+ // 「选中加粗后后面的字消失」的根因,2026-10-24;E2E stressWrap 的
1373
+ // 「execCommand 时刻选区」断言第 1 轮即抓,diag-ctrlb-rate 100% 复现)。
1374
+ // 恢复形态=collapsed 落段尾(cursorEnd):旧版恢复成「选中整段」,用户
1375
+ // 加粗后继续打字会整个替换掉刚加粗的内容(误删风险,W6 场景实测)。
1337
1376
  setSelectionByOffset(input, start, end);
1377
+ setTimeout(() => {
1378
+ try {
1379
+ document.execCommand("insertText", false, act.insert);
1380
+ } catch (err) { /* 编辑区失焦等场景静默放弃 */ }
1381
+ const cs = act.cursorEnd == null ? act.cursorStart : act.cursorEnd;
1382
+ setTimeout(() => setSelectionByOffset(input, cs, cs), 0);
1383
+ }, 0);
1384
+ } else if (start === end) {
1385
+ // 光标态多行(block 光标态插空代码块框架):collapsed 处 insertInline
1386
+ //(beforeinput 序列在 collapsed 光标处可靠——问题只出在有选区时)
1387
+ setSelectionByOffset(input, start, start);
1338
1388
  insertInline(input, act.insert);
1339
- requestAnimationFrame(() => setSelectionByOffset(input, act.cursorStart, act.cursorEnd == null ? act.cursorStart : act.cursorEnd));
1389
+ const cs2 = act.cursorEnd == null ? act.cursorStart : act.cursorEnd;
1390
+ setTimeout(() => setSelectionByOffset(input, cs2, cs2), 120);
1340
1391
  } else {
1341
- // 代码块(多行):beforeinput 逐行序列
1342
- insertInline(input, act.insert);
1392
+ // 跨行选区(insert 含 \n):「两端补标记」——先在 end 处插尾标记(不影响
1393
+ // start 侧偏移),再在 start 处插头标记,最后恢复选区为插入后的整段。
1394
+ // 每步都是 collapsed 光标处的单点插入(execCommand / beforeinput 序列在
1395
+ // collapsed 处均可靠),全程零删除——「删+插」三步链实测在行中跨行选区
1396
+ // 上删除范围漂移、beforeinput 序列部分丢弃(diag-ctrlb 跨行矩阵),直接对
1397
+ // 选区跑序列则与 Lexical 内部 selection 不同步(Tab 块缩进同款坑)。
1398
+ let head, tail;
1399
+ if (kind === "block") {
1400
+ const after = src.slice(end);
1401
+ head = "```\n";
1402
+ tail = "\n```" + (after && !after.startsWith("\n") ? "\n" : "");
1403
+ } else {
1404
+ const pairsX = { bold: ["**", "**"], italic: ["*", "*"], code: ["`", "`"], link: ["[", "](url)"] };
1405
+ const px = pairsX[kind] || ["**", "**"];
1406
+ head = px[0]; tail = px[1];
1407
+ }
1408
+ const cursorEnd2 = act.cursorEnd == null ? act.cursorStart : act.cursorEnd;
1409
+ setTimeout(() => {
1410
+ setSelectionByOffset(input, end, end);
1411
+ setTimeout(() => {
1412
+ insertInline(input, tail);
1413
+ setTimeout(() => {
1414
+ setSelectionByOffset(input, start, start);
1415
+ setTimeout(() => {
1416
+ insertInline(input, head);
1417
+ setTimeout(() => setSelectionByOffset(input, cursorEnd2, cursorEnd2), 120);
1418
+ }, head.indexOf("\n") === -1 ? 30 : 120);
1419
+ }, 0);
1420
+ }, 0);
1421
+ }, 0);
1343
1422
  }
1344
1423
  }
1345
1424
  //#endregion
@@ -1429,13 +1508,25 @@ window.__ModuleLoader__.load({
1429
1508
  const input = card.querySelector(INPUT_SEL);
1430
1509
  if (!input) return;
1431
1510
 
1432
- if (card.getAttribute(READY_KEY) !== "1" && !card.querySelector("." + TOOLBAR)) {
1511
+ if (card.getAttribute(READY_KEY) !== "1") {
1433
1512
  card.setAttribute(READY_KEY, "1");
1434
1513
  const scroll = card.querySelector(SCROLL_SEL);
1435
1514
  if (scroll) scroll.classList.add("dsh-cl-anchor");
1436
1515
 
1516
+ // 卡外工具栏复用(v0.2.14):card 重建(hero 切换/会话切换/官方重挂)后,
1517
+ // 旧工具栏仍留在 card 的父容器里——不能建第二份,找到即接管,
1518
+ // __dshClToolbar/__dshClBtns 指向既有节点(按钮闭包一律 findCard() 现查,
1519
+ // 无需重绑监听)。
1520
+ let toolbar = card.parentElement ? card.parentElement.querySelector("." + TOOLBAR) : null;
1521
+ if (toolbar) {
1522
+ card.__dshClToolbar = toolbar;
1523
+ card.__dshClBtns = {};
1524
+ for (const b of toolbar.querySelectorAll(".dsh-cl-btn")) {
1525
+ if (b.dataset.cl) card.__dshClBtns[b.dataset.cl] = b;
1526
+ }
1527
+ } else {
1437
1528
  // 常驻格式工具栏(6 格式键 + 展开键)
1438
- const toolbar = document.createElement("div");
1529
+ toolbar = document.createElement("div");
1439
1530
  toolbar.className = TOOLBAR;
1440
1531
  toolbar.setAttribute("role", "toolbar");
1441
1532
  toolbar.setAttribute("aria-label", "格式工具栏");
@@ -1474,7 +1565,9 @@ window.__ModuleLoader__.load({
1474
1565
  expandBtn.innerHTML = iconSvg("expand");
1475
1566
  expandBtn.addEventListener("mousedown", (ev) => ev.preventDefault());
1476
1567
  expandBtn.addEventListener("click", () => {
1477
- const s = card.querySelector(SCROLL_SEL);
1568
+ // 现查 card:工具栏跨 card 重建存活(复用),闭包里的 card 可能已换实例
1569
+ const c = findCard();
1570
+ const s = c && c.querySelector(SCROLL_SEL);
1478
1571
  if (!s) return;
1479
1572
  const on = s.classList.toggle(EXPANDED);
1480
1573
  expandBtn.classList.toggle("dsh-cl-active", on);
@@ -1482,8 +1575,8 @@ window.__ModuleLoader__.load({
1482
1575
  expandBtn.innerHTML = iconSvg(on ? "collapse" : "expand");
1483
1576
  });
1484
1577
  toolbar.appendChild(expandBtn);
1485
- card.__dshClToolbar = toolbar; // 只创建不挂载——sticky 需挂进滚动容器,见下方 grow 块
1486
- // 挂载延后到 grow(sticky 需在滚动容器内,与 overlay 同一安全位置)
1578
+ card.__dshClToolbar = toolbar; // 只创建不挂载——卡外挂载见下方搬正逻辑
1579
+ } // else(新建工具栏)结束
1487
1580
 
1488
1581
  // 选区浮动格式条(body 级 fixed)
1489
1582
  const floatBar = document.createElement("div");
@@ -1684,6 +1777,19 @@ window.__ModuleLoader__.load({
1684
1777
  }, true);
1685
1778
  }
1686
1779
 
1780
+ // 卡外工具栏搬正(v0.2.14):挂 card 的父容器、card 前一格——输入卡外正上方。
1781
+ // 未挂载/被 React 重排挤错位(官方重建 children 时不认识外来节点)都拉回;
1782
+ // 宽度从 card 同步(父容器内容宽 ≠ card 宽,CSS width:100% 会比卡突出),
1783
+ // 有差异才写 style,避免每次 run 都触发样式失效。
1784
+ const tb = card.__dshClToolbar || (card.parentElement && card.parentElement.querySelector("." + TOOLBAR));
1785
+ if (tb && card.parentElement && (tb.parentElement !== card.parentElement || tb.nextElementSibling !== card)) {
1786
+ card.parentElement.insertBefore(tb, card);
1787
+ }
1788
+ if (tb) {
1789
+ const w = card.offsetWidth + "px";
1790
+ if (tb.style.width !== w) tb.style.width = w;
1791
+ }
1792
+
1687
1793
  // 确保 overlay 与哨兵在位(插在编辑层之前,画在其下不挡光标)
1688
1794
  const grow = input.parentElement;
1689
1795
  if (grow) {
@@ -1694,9 +1800,6 @@ window.__ModuleLoader__.load({
1694
1800
  ov.setAttribute("aria-hidden", "true");
1695
1801
  grow.insertBefore(ov, input);
1696
1802
  }
1697
- // sticky 工具栏进滚动容器(在 overlay 前):未挂载或挂错位置都搬正
1698
- const tb = card.__dshClToolbar || card.querySelector("." + TOOLBAR);
1699
- if (tb && (tb.parentElement !== grow || tb.nextElementSibling !== ov)) grow.insertBefore(tb, ov);
1700
1803
  if (!card.__dshClSentinel || !card.__dshClSentinel.isConnected) {
1701
1804
  const s = document.createElement("div");
1702
1805
  s.className = SENTINEL;
@@ -1802,8 +1905,10 @@ window.__ModuleLoader__.load({
1802
1905
  }
1803
1906
  if (card.__dshClFloat) { card.__dshClFloat.remove(); card.__dshClFloat = null; }
1804
1907
  card.removeAttribute(READY_KEY);
1805
- const tb = card.querySelector("." + TOOLBAR);
1908
+ // v0.2.14:工具栏在 card 外(父容器内),先引用/父容器找,找不到再全局兜底
1909
+ const tb = card.__dshClToolbar || (card.parentElement && card.parentElement.querySelector("." + TOOLBAR));
1806
1910
  if (tb) tb.remove();
1911
+ card.__dshClToolbar = null;
1807
1912
  const input = card.querySelector(INPUT_SEL);
1808
1913
  if (input) {
1809
1914
  input.classList.remove(HOST, MONO_ALL);
@@ -1820,6 +1925,8 @@ window.__ModuleLoader__.load({
1820
1925
  }
1821
1926
  const s = document.getElementById(STYLE_ID);
1822
1927
  if (s) s.remove();
1928
+ // 卡外工具栏全局兜底:card 已随会话切换消失时上面的引用清不到
1929
+ document.querySelectorAll("." + TOOLBAR).forEach((el) => el.remove());
1823
1930
  };
1824
1931
  });
1825
1932
  }
@@ -27,13 +27,15 @@
27
27
  浏览器半的组成:
28
28
 
29
29
  ```
30
+ ┌─ 格式工具栏 .dsh-cl-toolbar(v0.2.14 起挂在输入卡外、卡正上方——
31
+ │ card 父容器内 card 前一格,宽度从 card 同步,不占输入框内部高度)
32
+ │
30
33
  ┌─ 官方输入卡 [data-composer-card]
31
34
  │ ┌─ 官方 Lexical 编辑器 [data-composer-input](contenteditable)
32
35
  │ │ 真实文字:-webkit-text-fill-color:transparent(透明但仍在排版)
33
36
  │ │ position:relative; z-index:1(caret/选区盖在渲染层之上)
34
37
  │ ├─ 渲染层 .dsh-cl-live(自建,pointer-events:none,z-index:0)
35
38
  │ │ 逐字符定位在编辑器文字的真实坐标上
36
- │ └─ 格式工具栏(sticky,挂在滚动容器内)
37
39
  │
38
40
  ├─ 键盘捕获层:[data-composer-card] capture 阶段,先于官方 Lexical keymap
39
41
  └─ 数据源:直接读编辑器 DOM → 投影文本(零服务注入)
@@ -161,7 +163,7 @@ dsh 0.1.5 前端是 CSS Modules——类名全带随机 hash(每次构建变
161
163
 
162
164
  同理,对官方**布局**的适配也用几何法而非类名:图片附件在场时的工具栏让位靠「检测编辑器外的 img → 向上找全宽祖先取底边」,官方 DOM 怎么改都不失效。
163
165
 
164
- 顺带兜底的两个官方定位问题(均为几何法):图片附件在场时候选菜单悬空(官方菜单 absolute + `bottom:4px` 锚定不感知附件栏——bottom 锚定下 margin 零位移、top 与 bottom 同设会拉伸,只有 `transform: translateY` 与官方零冲突且天然幂等收敛);工具栏盖附件缩略图(根治 = 工具栏挂进滚动容器 + `position:sticky; top:0`,附件栏在滚动容器外,sticky 天然让位)。
166
+ 顺带兜底的两个官方定位问题(均为几何法):图片附件在场时候选菜单悬空(官方菜单 absolute + `bottom:4px` 锚定不感知附件栏——bottom 锚定下 margin 零位移、top 与 bottom 同设会拉伸,只有 `transform: translateY` 与官方零冲突且天然幂等收敛);工具栏盖附件缩略图(v0.2.8 曾用「挂进滚动容器 + sticky」根治,**v0.2.14 起工具栏整体迁到输入卡外正上方,与卡内附件栏物理分层,天然无重叠**;宽度从 `card.offsetWidth` 同步而非 CSS `width:100%`——父容器内容宽 ≠ card 宽)。
165
167
 
166
168
  ## 8. 等宽字体体系
167
169
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-composer-live",
3
- "version": "0.2.13",
3
+ "version": "0.2.16",
4
4
  "description": "DSH Web 0.1.5+ 输入框增强(Lexical contenteditable 架构版):markdown 实时渲染 + 代码块语法高亮/段落级等宽 + 格式工具栏(常驻顶条 + 选区浮动条)+ 70vh 展开 + 大段粘贴包代码块 + Esc 分发。纯浏览器端插件,零依赖,不改官方源码。仿 Open WebUI 输入体验。",
5
5
  "type": "module",
6
6
  "main": "index.js",