@240xu/dsh-message-ops 0.24.0 → 0.47.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 +653 -0
- package/package.json +8 -4
- package/src/auth.js +274 -0
- package/src/client/00-core.js +284 -0
- package/src/client/10-i18n.js +260 -0
- package/src/client/20-host-state.js +311 -0
- package/src/client/30-panel.js +1478 -0
- package/src/client/40-edit.js +189 -0
- package/src/client/50-inline-edit.js +348 -0
- package/src/client/60-session.js +353 -0
- package/src/client/70-revert.js +679 -0
- package/src/client/80-inject.js +864 -0
- package/src/client/85-sidebar.js +368 -0
- package/src/client/90-apply.js +126 -0
- package/src/client.js +1906 -1277
- package/src/derived-incremental.js +787 -0
- package/src/host-services.js +84 -0
- package/src/index.js +1100 -280
- package/src/ops-core.js +624 -362
- package/src/ops-write.js +301 -0
- package/src/session-file.js +145 -22
- package/src/session-trash.js +495 -42
- package/src/write-lock.js +48 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// ══ 40-edit · 编辑并重发:官方发送面 + 唯一编排 ══
|
|
2
|
+
const EDIT_SOURCE = 'messageEdit'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 官方发送面:`sessions.using(target, { source }, ref => ref.binding.session.prompt(...))`。
|
|
6
|
+
*
|
|
7
|
+
* 为什么手写 using 而不是 retain/release:官方 `using` 的实现就是
|
|
8
|
+
* `retain()` → `await reference.ready` → `operation()` → **`finally release()`**
|
|
9
|
+
* (已读 dsh-api-session-controller/lib/client.js)。自己 retain 就得多写一遍
|
|
10
|
+
* release,失败路径更容易漏 —— 漏了就是 scope 永远不回收。
|
|
11
|
+
*
|
|
12
|
+
* 为什么 `prompt(..., 'queue')`:queue = 追加一轮,steer = 打断当前轮。回撤之后
|
|
13
|
+
* 会话本来就在等新输入,queue 是官方的「接着跑一轮」语义(与宿主 composer 回车
|
|
14
|
+
* 同一条 API)。运行中会话的回撤会被服务端 409 拦住(见 ①),所以不需要 steer 抢跑。
|
|
15
|
+
*
|
|
16
|
+
* @param text - 改后的文本
|
|
17
|
+
* @param options - { rawSessionId, sessions }
|
|
18
|
+
* @returns { ok, accepted?, unavailable?, reason? }
|
|
19
|
+
*/
|
|
20
|
+
async function promptEditedText(text, options) {
|
|
21
|
+
const opts = options || {}
|
|
22
|
+
const rawSessionId = String(opts.rawSessionId || '').trim()
|
|
23
|
+
const sessions = opts.sessions || __sessionsSvc
|
|
24
|
+
if (!sessions || typeof sessions.using !== 'function') {
|
|
25
|
+
return { ok: false, unavailable: true, reason: 'sessions service unavailable' }
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
return await sessions.using(rawSessionId, { source: EDIT_SOURCE }, async reference => {
|
|
29
|
+
if (reference && reference.ready) await reference.ready
|
|
30
|
+
const face = reference && reference.binding ? reference.binding.session : null
|
|
31
|
+
if (!face || typeof face.prompt !== 'function') {
|
|
32
|
+
return { ok: false, unavailable: true, reason: 'session.prompt unavailable' }
|
|
33
|
+
}
|
|
34
|
+
let result = null
|
|
35
|
+
try {
|
|
36
|
+
result = await face.prompt([{ type: 'text', text: text }], 'queue')
|
|
37
|
+
} catch (e) {
|
|
38
|
+
return { ok: false, reason: e && e.message ? e.message : String(e) }
|
|
39
|
+
}
|
|
40
|
+
if (!result || result.ok !== true) {
|
|
41
|
+
const err = result && result.error
|
|
42
|
+
return { ok: false, reason: (err && (err.code || err.message)) || 'prompt rejected' }
|
|
43
|
+
}
|
|
44
|
+
return { ok: true, accepted: true }
|
|
45
|
+
})
|
|
46
|
+
} catch (e) {
|
|
47
|
+
// `using` 的 acquisition 失败(未知会话 / generation 已 dispose)也走这里。
|
|
48
|
+
return { ok: false, reason: e && e.message ? e.message : String(e) }
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 「撤销并发送」的唯一编排实现。
|
|
54
|
+
*
|
|
55
|
+
* 抽成具名函数(而不是埋在组件里)有两个理由:
|
|
56
|
+
* 1. 时序是本功能的核心契约,必须能被测试**真执行**断言
|
|
57
|
+
* (revert 失败 → 零 prompt、零草稿、零视图重建)。
|
|
58
|
+
* 2. 组件只负责 UI 状态;失败时「保留文本」由调用方按返回值决定。
|
|
59
|
+
*
|
|
60
|
+
* @param options - { sessionId, rawSessionId, seq, text, deps, reverted }
|
|
61
|
+
* sessionId 裸 uuid(给 HTTP)
|
|
62
|
+
* rawSessionId `session-` 前缀(给 sessions.using 与草稿键);缺省从 sessionId 补
|
|
63
|
+
* reverted 已经成功回撤过一次(「重新发送」):跳过 ①~③,直接走 ④。
|
|
64
|
+
* 同一个 seq 再回撤必然 409,所以重试只能重发。
|
|
65
|
+
* deps 可注入的副作用集合(测试用);缺省取真实实现
|
|
66
|
+
* @returns Promise<{ok, stage, submitted?, mode?, manual?, persisted?, reason?, status?}>
|
|
67
|
+
* stage: 'validate' | 'revert' | 'send' | 'done'
|
|
68
|
+
*/
|
|
69
|
+
async function sendEditAndRevert(options) {
|
|
70
|
+
const opts = options || {}
|
|
71
|
+
const sessionId = String(opts.sessionId || '').trim()
|
|
72
|
+
// sessions.using 与草稿键都必须用带 `session-` 前缀的写法:裸 uuid 会让宿主
|
|
73
|
+
// `sessions.retain` 抛 `unknown session`(0.9.6 事故,见 afterWrite 注释)。
|
|
74
|
+
const rawSessionId = String(opts.rawSessionId || '').trim() || (sessionId ? 'session-' + sessionId : '')
|
|
75
|
+
const seq = opts.seq
|
|
76
|
+
const text = typeof opts.text === 'string' ? opts.text : ''
|
|
77
|
+
const deps = opts.deps || {}
|
|
78
|
+
const reverted = opts.reverted === true
|
|
79
|
+
// ⚠️ 先只解析 fetch:revert 失败时**不得**触碰任何草稿/视图副作用,
|
|
80
|
+
// 连「解析实现」都不该发生在失败之前(解析顺序 = 语义顺序)。
|
|
81
|
+
const doFetch = deps.fetch || fetch
|
|
82
|
+
|
|
83
|
+
if (!sessionId) return { ok: false, stage: 'validate', reason: 'invalid target' }
|
|
84
|
+
if (!reverted && !(Number.isSafeInteger(seq) && seq >= 0)) return { ok: false, stage: 'validate', reason: 'invalid target' }
|
|
85
|
+
if (text.trim() === '') return { ok: false, stage: 'validate', reason: 'empty' }
|
|
86
|
+
|
|
87
|
+
if (!reverted) {
|
|
88
|
+
// ① 回撤:失败就**什么都不做**(不写草稿、不重建视图、不动编辑器)
|
|
89
|
+
let res = null
|
|
90
|
+
let data = null
|
|
91
|
+
try {
|
|
92
|
+
res = await doFetch('/api/message-ops/revert', {
|
|
93
|
+
method: 'POST',
|
|
94
|
+
headers: { 'content-type': 'application/json' },
|
|
95
|
+
body: JSON.stringify({ sessionId, seq }),
|
|
96
|
+
})
|
|
97
|
+
// ⚠️ 本函数是**可被单独抽取执行**的编排层(contract-hardening 用 new Function
|
|
98
|
+
// 抽它来断言时序),因此不许依赖模块级 helper —— 这里保留内联读法;
|
|
99
|
+
// 语义与 readOpsJson 一致:解析失败 = {},绝不当成「服务端说了什么」。
|
|
100
|
+
data = await res.json().catch(() => null)
|
|
101
|
+
} catch (e) {
|
|
102
|
+
return { ok: false, stage: 'revert', reason: e && e.message ? e.message : String(e) }
|
|
103
|
+
}
|
|
104
|
+
if (!res || !res.ok || !data || data.ok !== true) {
|
|
105
|
+
return {
|
|
106
|
+
ok: false,
|
|
107
|
+
stage: 'revert',
|
|
108
|
+
status: res ? res.status : 0,
|
|
109
|
+
reason: (data && data.error) || 'HTTP ' + (res ? res.status : '?'),
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── 回撤已成功 ── 从这里开始才解析并使用其余副作用 ──────────────────────
|
|
114
|
+
const markShadowed = deps.markRowsShadowedFrom || markRowsShadowedFrom
|
|
115
|
+
const doEmitChanged = deps.emitChanged || emitChanged
|
|
116
|
+
const doOpenSession = deps.openSessionById || openSessionById
|
|
117
|
+
|
|
118
|
+
// ② 防 409 护栏:本地缓存里 >= seq 的行立刻标成已遮蔽(与 runUserRevert/runUndo 同一函数)
|
|
119
|
+
try {
|
|
120
|
+
markShadowed(seq)
|
|
121
|
+
} catch (e) {
|
|
122
|
+
console.warn('[message-ops] markRowsShadowedFrom failed:', e)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ③ 通知 + 重建视图(rawSessionId 带前缀;裸 uuid 会抛 unknown session)
|
|
126
|
+
try {
|
|
127
|
+
doEmitChanged()
|
|
128
|
+
} catch (e) {
|
|
129
|
+
/* 广播失败不阻断写结果 */
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
doOpenSession(rawSessionId)
|
|
133
|
+
} catch (e) {
|
|
134
|
+
console.warn('[message-ops] openSession failed:', e)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ④ 官方 prompt —— **恰好一次**。注入位是 deps.prompt(text, 'queue'):
|
|
139
|
+
// 真执行测试与 contract-hardening 都从这个面观测,不靠源码形状。
|
|
140
|
+
const doPrompt = deps.prompt || (t => promptEditedText(t, { rawSessionId: rawSessionId, sessions: deps.sessions }))
|
|
141
|
+
let sent = null
|
|
142
|
+
try {
|
|
143
|
+
sent = await doPrompt(text, 'queue')
|
|
144
|
+
} catch (e) {
|
|
145
|
+
sent = { ok: false, reason: e && e.message ? e.message : String(e) }
|
|
146
|
+
}
|
|
147
|
+
if (sent && sent.ok === true) {
|
|
148
|
+
return { ok: true, stage: 'done', submitted: true, mode: 'official' }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 老宿主(官方发送面缺席)→ **唯一**降级:写草稿 + 回读校验 + 提示按回车。
|
|
152
|
+
// 这一条真落盘才算成功;回读到的值不同就如实报 persisted=false。
|
|
153
|
+
if (sent && sent.unavailable === true) {
|
|
154
|
+
const doWrite = deps.writeSessionDraft || writeSessionDraft
|
|
155
|
+
const doRead = deps.readSessionDraft || readSessionDraft
|
|
156
|
+
let persisted = false
|
|
157
|
+
try {
|
|
158
|
+
persisted = doWrite(rawSessionId, text) === true
|
|
159
|
+
} catch (e) {
|
|
160
|
+
console.warn('[message-ops] writeSessionDraft failed:', e)
|
|
161
|
+
}
|
|
162
|
+
// 回读校验:只有读到**不同的非空值**才算落盘失败(读不到时保持原判断,不误判)
|
|
163
|
+
if (persisted && typeof doRead === 'function') {
|
|
164
|
+
try {
|
|
165
|
+
const stored = doRead(rawSessionId)
|
|
166
|
+
if (stored != null && stored !== text) persisted = false
|
|
167
|
+
} catch (e) {
|
|
168
|
+
/* 读回失败不推翻写入结果 */
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return { ok: true, stage: 'done', submitted: false, mode: 'manual', manual: true, persisted: persisted }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// 官方面在、但拒收/抛错:**回撤已经成功**,绝不能返回 ok:false 让 UI 报「回撤失败」。
|
|
175
|
+
// 单列 stage:'send',组件据此给「回撤成功但发送被拒」的文案。
|
|
176
|
+
const reason = sent && sent.reason ? String(sent.reason) : 'prompt rejected'
|
|
177
|
+
return { ok: false, stage: 'send', submitted: false, mode: 'official', reason: reason }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// --- 0.28.0 就地编辑(替代 0.26 的浮动编辑层)-------------------------------
|
|
181
|
+
//
|
|
182
|
+
// 为什么重写:官方 `useAnchoredPosition` 在当前宿主(0.2.2-rc.5 构建产物)里
|
|
183
|
+
// **已不存在**(只剩 `useAnchoredMaxHeight`),旧实现的
|
|
184
|
+
// `useAnchoredPosition = primitives.useAnchoredPosition || (() => null)` 静默降级
|
|
185
|
+
// → 面板永远停在 `visibility:hidden` 的测量态、坐标 (0,0)(真机复现)。
|
|
186
|
+
//
|
|
187
|
+
// 就地编辑不依赖任何定位 API:编辑器直接挂进这条消息自己的行里(气泡下方、
|
|
188
|
+
// 动作行上方),宿主滚动/重排都带着它走。保存 = 回撤 + 官方 prompt
|
|
189
|
+
// (仍由 `sendEditAndRevert` 单一编排,时序契约不变)。
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// ══ 50-inline-edit · 就地编辑(气泡下、动作行上) ══
|
|
2
|
+
const IE_STYLE_ID = 'dsh-message-ops-inline-edit-style'
|
|
3
|
+
const IE_CLASS = 'mopsIeWrap'
|
|
4
|
+
/** 当前打开的编辑器。宿主重建块时会换掉 DOM,靠它按 seq 重挂(文本不丢)。 */
|
|
5
|
+
let inlineEdit = null
|
|
6
|
+
|
|
7
|
+
/** 就地编辑样式:只用已确认存在的 token;不做 absolute/fixed 定位。 */
|
|
8
|
+
function ensureIeStyle() {
|
|
9
|
+
try {
|
|
10
|
+
if (typeof document === 'undefined' || !document.head) return
|
|
11
|
+
if (document.getElementById(IE_STYLE_ID)) return
|
|
12
|
+
const tag = document.createElement('style')
|
|
13
|
+
tag.id = IE_STYLE_ID
|
|
14
|
+
tag.textContent = [
|
|
15
|
+
'.mopsIeWrap{display:flex;flex-direction:column;gap:8px;align-self:flex-end;width:min(calc(var(--dsh-chat-content-width,748px)*.702),82%);margin-top:2px;text-align:left}',
|
|
16
|
+
'.mopsIeWrap *{box-sizing:border-box}',
|
|
17
|
+
'.mopsIeText{width:100%;min-height:72px;max-height:32vh;resize:vertical;padding:8px 10px;border:1px solid var(--dsw-alias-border-l2,#e6e6e6);border-radius:var(--dsw-radius-sm,8px);background:var(--dsw-alias-bg-layer-1,#fff);color:var(--dsw-alias-label-primary,#0f1115);font:inherit;font-size:14px;line-height:1.6}',
|
|
18
|
+
'.mopsIeText:focus{border-color:var(--dsw-alias-state-business-primary,#4d6bfe);box-shadow:0 0 0 2px color-mix(in srgb,var(--dsw-alias-state-business-primary,#4d6bfe) 24%,transparent)}',
|
|
19
|
+
'.mopsIeText:disabled{opacity:.7}',
|
|
20
|
+
'.mopsIeHint{font-size:12px;line-height:1.5;color:var(--dsw-alias-label-tertiary,#8a8a8e)}',
|
|
21
|
+
'.mopsIeStatus{font-size:12px;line-height:1.5;color:var(--dsw-alias-state-error-primary,#d33)}',
|
|
22
|
+
'.mopsIeActions{display:flex;align-items:center;justify-content:flex-end;gap:8px}',
|
|
23
|
+
'.mopsIeBtn{height:28px;padding:0 12px;border-radius:var(--dsw-radius-sm,8px);border:1px solid var(--dsw-alias-border-l2,#e6e6e6);background:var(--dsw-alias-bg-layer-1,#fff);color:var(--dsw-alias-label-primary,#0f1115);font:inherit;font-size:13px;cursor:pointer}',
|
|
24
|
+
'.mopsIeBtn:disabled{opacity:.55;cursor:default}',
|
|
25
|
+
'.mopsIePrimary{background:var(--dsw-alias-label-primary,#0f1115);color:var(--dsw-alias-bg-layer-1,#fff);border-color:transparent}',
|
|
26
|
+
'.mopsIeGhost{background:transparent}',
|
|
27
|
+
].join('')
|
|
28
|
+
document.head.appendChild(tag)
|
|
29
|
+
} catch (e) {
|
|
30
|
+
/* 样式注入失败不阻断编辑(宿主禁用 style 时会退化) */
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 关闭编辑区。
|
|
36
|
+
*
|
|
37
|
+
* `restoreFocus` 默认 true:Esc / 取消后把焦点还给铅笔按钮 —— 否则焦点掉到
|
|
38
|
+
* body,键盘用户会丢失位置(真机实测:Esc 后 document.activeElement 是 BODY)。
|
|
39
|
+
* 发送成功后不还原:那一行会被回撤重建,旧按钮已不在 DOM 里。
|
|
40
|
+
*
|
|
41
|
+
* @param {'close'|'sent'} reason 关闭原因(sent = 发送成功,行会被重建)
|
|
42
|
+
*/
|
|
43
|
+
function closeInlineEdit(reason) {
|
|
44
|
+
const prev = inlineEdit
|
|
45
|
+
if (prev && prev.el && prev.el.isConnected) {
|
|
46
|
+
try {
|
|
47
|
+
prev.el.remove()
|
|
48
|
+
} catch (e) {
|
|
49
|
+
console.warn('[message-ops] inline editor remove failed:', e)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
inlineEdit = null
|
|
53
|
+
if (prev && prev.btn && prev.btn.isConnected) {
|
|
54
|
+
try {
|
|
55
|
+
prev.btn.setAttribute('aria-expanded', 'false')
|
|
56
|
+
prev.btn.removeAttribute('aria-controls')
|
|
57
|
+
} catch (e) {
|
|
58
|
+
/* aria 复位失败不影响编辑 */
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if ((reason || 'close') === 'close' && prev && prev.btn && prev.btn.isConnected) {
|
|
62
|
+
try {
|
|
63
|
+
prev.btn.focus()
|
|
64
|
+
} catch (e) {
|
|
65
|
+
/* 焦点归还失败不影响编辑 */
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 周期刷新编辑区的运行态(scan 每帧调用;编辑器没开时是空操作)。 */
|
|
71
|
+
function paintInlineEdit() {
|
|
72
|
+
if (inlineEdit && typeof inlineEdit.paint === 'function') {
|
|
73
|
+
try {
|
|
74
|
+
inlineEdit.paint()
|
|
75
|
+
} catch (e) {
|
|
76
|
+
console.warn('[message-ops] inline editor paint failed:', e)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 回撤已成功但发送被拒:文本放回该会话草稿(并更新活着的编辑器)。 */
|
|
82
|
+
function stageDraft(rawSid, text) {
|
|
83
|
+
try {
|
|
84
|
+
writeSessionDraft(rawSid, text)
|
|
85
|
+
} catch (e) {
|
|
86
|
+
console.warn('[message-ops] writeSessionDraft failed:', e)
|
|
87
|
+
}
|
|
88
|
+
const live = inputActionsFor(rawSid)
|
|
89
|
+
if (live && typeof live.setDraft === 'function') {
|
|
90
|
+
try {
|
|
91
|
+
live.setDraft(text)
|
|
92
|
+
} catch (e) {
|
|
93
|
+
console.warn('[message-ops] setDraft failed:', e)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 行内挂载点:气泡栈之后、宿主动作行之前(动作行始终在最下面)。 */
|
|
99
|
+
function mountInlineEdit(block, el, rowHint) {
|
|
100
|
+
const rowEl = rowHint || block.querySelector('[class*="userRow"]') || block
|
|
101
|
+
const acts = rowEl.querySelector('[class*="' + HOST_CLS.actions + '"]')
|
|
102
|
+
if (acts && acts.parentElement === rowEl) rowEl.insertBefore(el, acts)
|
|
103
|
+
else rowEl.appendChild(el)
|
|
104
|
+
return rowEl
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 就地编辑入口(铅笔点击后调用)。
|
|
109
|
+
*
|
|
110
|
+
* @param btn - 铅笔按钮(读失败时据此复位 disabled)
|
|
111
|
+
* @param block - 该条用户消息的流块(`data-chat-node-key` / `data-mops-seq`)
|
|
112
|
+
* @param row - `/messages` 里的该行(`fullText` 是全文,**不是** snippet)
|
|
113
|
+
* @param rawSidArg - 带 `session-` 前缀的会话 id(缺省取 `__currentSessionId`)
|
|
114
|
+
* @param deps - 可注入副作用(测试用:send / notify / running / stageDraft)
|
|
115
|
+
*/
|
|
116
|
+
function openInlineEdit(btn, block, row, rawSidArg, deps) {
|
|
117
|
+
const rawSid = rawSidArg || __currentSessionId
|
|
118
|
+
if (!rawSid || !row) return
|
|
119
|
+
const sid = bareSessionId(rawSid)
|
|
120
|
+
const seq = row.seq
|
|
121
|
+
if (!Number.isSafeInteger(seq)) return
|
|
122
|
+
// 同一条再点铅笔 = 收起(幂等;不会叠出两个编辑区)
|
|
123
|
+
if (inlineEdit && inlineEdit.seq === seq && inlineEdit.block === block) {
|
|
124
|
+
closeInlineEdit()
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
closeInlineEdit()
|
|
128
|
+
ensureIeStyle()
|
|
129
|
+
// 批量 steering 的一个 flow 块里可能有多个 userRow(pending/echo 各一条),
|
|
130
|
+
// 从 block 里 querySelector 会挂到上一条消息下面 —— 用铅笔自己的祖先定位。
|
|
131
|
+
const rowHint = btn && typeof btn.closest === 'function' ? btn.closest('[class*="userRow"]') : null
|
|
132
|
+
|
|
133
|
+
const wrap = document.createElement('div')
|
|
134
|
+
wrap.className = IE_CLASS
|
|
135
|
+
wrap.id = 'mops-ie-' + seq
|
|
136
|
+
wrap.setAttribute('data-mops-ie', '1')
|
|
137
|
+
if (btn && typeof btn.setAttribute === 'function') {
|
|
138
|
+
btn.setAttribute('aria-expanded', 'true')
|
|
139
|
+
btn.setAttribute('aria-controls', wrap.id)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const ta = document.createElement('textarea')
|
|
143
|
+
ta.className = 'mopsIeText'
|
|
144
|
+
ta.rows = 3
|
|
145
|
+
ta.value = typeof row.fullText === 'string' ? row.fullText : ''
|
|
146
|
+
ta.placeholder = __t('edit.placeholder')
|
|
147
|
+
ta.setAttribute('aria-label', __t('edit.title'))
|
|
148
|
+
|
|
149
|
+
const hint = document.createElement('div')
|
|
150
|
+
hint.className = 'mopsIeHint'
|
|
151
|
+
// D-3:回撤不是删除,原文仍在本地会话文件里。这句不能删。
|
|
152
|
+
hint.textContent = __t('edit.replaceHint')
|
|
153
|
+
|
|
154
|
+
const status = document.createElement('div')
|
|
155
|
+
status.className = 'mopsIeStatus'
|
|
156
|
+
status.setAttribute('role', 'alert')
|
|
157
|
+
status.hidden = true
|
|
158
|
+
|
|
159
|
+
const actions = document.createElement('div')
|
|
160
|
+
actions.className = 'mopsIeActions'
|
|
161
|
+
const cancelBtn = document.createElement('button')
|
|
162
|
+
cancelBtn.type = 'button'
|
|
163
|
+
cancelBtn.className = 'mopsIeBtn mopsIeGhost'
|
|
164
|
+
cancelBtn.textContent = __t('edit.cancel')
|
|
165
|
+
const sendBtn = document.createElement('button')
|
|
166
|
+
sendBtn.type = 'button'
|
|
167
|
+
sendBtn.className = 'mopsIeBtn mopsIePrimary'
|
|
168
|
+
sendBtn.textContent = __t('edit.send')
|
|
169
|
+
actions.appendChild(cancelBtn)
|
|
170
|
+
actions.appendChild(sendBtn)
|
|
171
|
+
|
|
172
|
+
wrap.appendChild(ta)
|
|
173
|
+
wrap.appendChild(hint)
|
|
174
|
+
// 错误贴着输入框与按钮,而不是被按钮行隔开(IE-12)
|
|
175
|
+
wrap.appendChild(status)
|
|
176
|
+
wrap.appendChild(actions)
|
|
177
|
+
mountInlineEdit(block, wrap, rowHint)
|
|
178
|
+
|
|
179
|
+
let saving = false
|
|
180
|
+
const send = (deps && deps.send) || sendEditAndRevert
|
|
181
|
+
const notify = (deps && deps.notify) || notifyDone
|
|
182
|
+
const stash = (deps && deps.stageDraft) || stageDraft
|
|
183
|
+
const runningNow = () =>
|
|
184
|
+
deps && typeof deps.running === 'function'
|
|
185
|
+
? deps.running() === true
|
|
186
|
+
: (() => {
|
|
187
|
+
const cache = __peekRowsCache ? __peekRowsCache() : null
|
|
188
|
+
return !!(cache && cache.running === true)
|
|
189
|
+
})()
|
|
190
|
+
const paint = () => {
|
|
191
|
+
const running = runningNow()
|
|
192
|
+
ta.readOnly = saving
|
|
193
|
+
// 保存中不要 disabled:Chrome 对 disabled 元素会立刻失焦,
|
|
194
|
+
// 焦点掉到 BODY 后 Esc(挂在 wrap 上)也收不到。用 aria-disabled 表达。
|
|
195
|
+
cancelBtn.disabled = false
|
|
196
|
+
cancelBtn.setAttribute('aria-disabled', saving ? 'true' : 'false')
|
|
197
|
+
sendBtn.disabled = false
|
|
198
|
+
sendBtn.setAttribute('aria-disabled', saving || running || ta.value.trim() === '' ? 'true' : 'false')
|
|
199
|
+
sendBtn.textContent = saving ? __t('edit.sending') : __t('edit.send')
|
|
200
|
+
sendBtn.title = running ? __t('edit.running') : __t('edit.shortcut')
|
|
201
|
+
// 运行中不能只靠 tooltip:把原因写进可见的说明行(D-3 文案保留)
|
|
202
|
+
hint.textContent = running ? __t('edit.replaceHint') + ' · ' + __t('edit.running') : __t('edit.replaceHint')
|
|
203
|
+
}
|
|
204
|
+
const fail = msg => {
|
|
205
|
+
status.textContent = msg
|
|
206
|
+
status.hidden = false
|
|
207
|
+
}
|
|
208
|
+
const clearFail = () => {
|
|
209
|
+
status.textContent = ''
|
|
210
|
+
status.hidden = true
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const submit = async () => {
|
|
214
|
+
if (saving) return
|
|
215
|
+
const text = ta.value
|
|
216
|
+
if (text.trim() === '') {
|
|
217
|
+
fail(__t('edit.empty'))
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
if (runningNow()) {
|
|
221
|
+
fail(__t('edit.running'))
|
|
222
|
+
paint()
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
clearFail()
|
|
226
|
+
saving = true
|
|
227
|
+
paint()
|
|
228
|
+
// 0.32.2:回撤步必须有超时。sendEditAndRevert 明令不许依赖模块级 helper
|
|
229
|
+
// (它能被单独抽取执行),所以超时通过 deps.fetch 注入;saving 才不会
|
|
230
|
+
// 因为后端挂起而永久为 true(按钮 aria-disabled + textarea readOnly)。
|
|
231
|
+
const result = await send({
|
|
232
|
+
sessionId: sid,
|
|
233
|
+
rawSessionId: rawSid,
|
|
234
|
+
seq: seq,
|
|
235
|
+
text: text,
|
|
236
|
+
deps: { fetch: (url, init) => fetchWithTimeout(url, init, 15000) },
|
|
237
|
+
})
|
|
238
|
+
saving = false
|
|
239
|
+
if (result.ok) {
|
|
240
|
+
notify(result.submitted ? __t('edit.doneSubmitted') : __t('edit.manual'))
|
|
241
|
+
closeInlineEdit('sent')
|
|
242
|
+
focusComposer()
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
if (result.stage === 'send') {
|
|
246
|
+
// 回撤已生效、视图已重建 —— 编辑区所在的节点很可能已经没了。
|
|
247
|
+
// 不赌「浮层活过重建」:文本直接放回输入框,用户按回车即可重发。
|
|
248
|
+
stash(rawSid, text)
|
|
249
|
+
notify(__t('edit.sendFailed') + (result.reason || ''), 'warn')
|
|
250
|
+
closeInlineEdit('sent')
|
|
251
|
+
focusComposer()
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
// 回撤失败(或参数非法):编辑区与文本都必须保留。
|
|
255
|
+
// 0.33.3(ui-live P2-3):15s 超时的 AbortError message 是英文原文,
|
|
256
|
+
// 走本地化超时文案(与面板同一口径),不把 "The operation was aborted." 摊给用户。
|
|
257
|
+
const reasonText = /abort/i.test(String(result.reason || '')) ? __t('panel.timeout') : result.reason || ''
|
|
258
|
+
fail((result.stage === 'revert' ? __t('edit.failed') : '') + reasonText)
|
|
259
|
+
paint()
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
cancelBtn.addEventListener('click', e => {
|
|
263
|
+
e.preventDefault()
|
|
264
|
+
if (!saving) closeInlineEdit()
|
|
265
|
+
})
|
|
266
|
+
sendBtn.addEventListener('click', e => {
|
|
267
|
+
e.preventDefault()
|
|
268
|
+
if (saving || sendBtn.getAttribute('aria-disabled') === 'true') return
|
|
269
|
+
submit()
|
|
270
|
+
})
|
|
271
|
+
ta.addEventListener('input', () => {
|
|
272
|
+
clearFail()
|
|
273
|
+
paint()
|
|
274
|
+
})
|
|
275
|
+
// IE-2:Esc 挂在整块上 —— 焦点 Tab 到按钮后仍然有效;Enter 只在输入框里提交
|
|
276
|
+
wrap.addEventListener('keydown', e => {
|
|
277
|
+
if (e.key === 'Escape') {
|
|
278
|
+
e.preventDefault()
|
|
279
|
+
if (!saving) closeInlineEdit()
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
if (e.target !== ta) return
|
|
283
|
+
if (e.key !== 'Enter' || e.shiftKey) return
|
|
284
|
+
// IME 组词中的 Enter 是选词,不是提交(keyCode 229 是旧引擎的组词信号)
|
|
285
|
+
if (e.isComposing || e.keyCode === 229) return
|
|
286
|
+
e.preventDefault()
|
|
287
|
+
submit()
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
inlineEdit = { seq: seq, rawSid: rawSid, el: wrap, block: block, ta: ta, btn: btn || null, focused: false, sel: null, paint: paint, original: ta.value }
|
|
291
|
+
ta.addEventListener('focus', () => {
|
|
292
|
+
if (inlineEdit && inlineEdit.ta === ta) inlineEdit.focused = true
|
|
293
|
+
})
|
|
294
|
+
ta.addEventListener('blur', () => {
|
|
295
|
+
if (inlineEdit && inlineEdit.ta === ta) {
|
|
296
|
+
inlineEdit.focused = false
|
|
297
|
+
inlineEdit.sel = [ta.selectionStart, ta.selectionEnd]
|
|
298
|
+
}
|
|
299
|
+
})
|
|
300
|
+
ta.addEventListener('input', () => {
|
|
301
|
+
if (inlineEdit && inlineEdit.ta === ta) inlineEdit.sel = [ta.selectionStart, ta.selectionEnd]
|
|
302
|
+
})
|
|
303
|
+
paint()
|
|
304
|
+
try {
|
|
305
|
+
// 编辑区比气泡高约 150px:靠近底部时要保证按钮与说明也在视口内
|
|
306
|
+
if (typeof wrap.scrollIntoView === 'function') wrap.scrollIntoView({ block: 'nearest' })
|
|
307
|
+
ta.focus({ preventScroll: true })
|
|
308
|
+
ta.setSelectionRange(ta.value.length, ta.value.length)
|
|
309
|
+
} catch (e) {
|
|
310
|
+
/* 聚焦失败不影响编辑 */
|
|
311
|
+
}
|
|
312
|
+
if (btn) btn.disabled = false
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 宿主重建消息块后把编辑区挂回去(同一 seq 的新块),文本随元素保留。
|
|
317
|
+
* 会话切换或该行已不存在 → 关闭。
|
|
318
|
+
*/
|
|
319
|
+
function rehydrateInlineEdit() {
|
|
320
|
+
if (!inlineEdit) return
|
|
321
|
+
// 只有「确实切到了另一个会话」才关;当前会话未知(测试/宿主尚未挂载)时不猜。
|
|
322
|
+
if (__currentSessionId && inlineEdit.rawSid !== __currentSessionId) {
|
|
323
|
+
// IE-13:切会话时编辑区会被摘掉。改过的文本必须落回**原会话**草稿,
|
|
324
|
+
// 否则用户打了一半的字凭空消失(原先 detach 分支不保存)。
|
|
325
|
+
const text = inlineEdit.ta ? inlineEdit.ta.value : ''
|
|
326
|
+
if (text && text !== inlineEdit.original) stageDraft(inlineEdit.rawSid, text)
|
|
327
|
+
closeInlineEdit('detach')
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
if (inlineEdit.el.isConnected) return
|
|
331
|
+
const target = Array.from(document.querySelectorAll('[data-chat-flow-kind="user"]')).find(
|
|
332
|
+
b => b.getAttribute('data-mops-seq') === String(inlineEdit.seq),
|
|
333
|
+
)
|
|
334
|
+
if (!target) return
|
|
335
|
+
mountInlineEdit(target, inlineEdit.el)
|
|
336
|
+
inlineEdit.block = target
|
|
337
|
+
// 宿主重建块会摘掉元素 → 焦点掉到 body。用户正在打字时必须把焦点与光标还回去。
|
|
338
|
+
if (inlineEdit.focused && document.activeElement === document.body) {
|
|
339
|
+
try {
|
|
340
|
+
inlineEdit.ta.focus({ preventScroll: true })
|
|
341
|
+
if (inlineEdit.sel) inlineEdit.ta.setSelectionRange(inlineEdit.sel[0], inlineEdit.sel[1])
|
|
342
|
+
} catch (e) {
|
|
343
|
+
/* 恢复焦点失败不影响编辑 */
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// --- 头部按钮 ---------------------------------------------------------------
|