dsh-share-html 0.0.0-stage → 0.1.1

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/lib/client.js ADDED
@@ -0,0 +1,614 @@
1
+ /**
2
+ * M9 / M10 / M11 — 浏览器半。
3
+ *
4
+ * 结构(照 dsh-plugins/packages/plantuml/lib/client.js 的既有写法):
5
+ * - `window.__ModuleLoader__.load({ id, factory })` 挂载,factory 内 `require("react")`
6
+ * - `exports.name` / `exports.inject` / `exports.apply(ctx)`
7
+ * - 按钮注册到 `conversation.session.header.actions`(M9)
8
+ * - 弹窗自己 portal 到 body:该插槽文档写明「overflow is clipped and faded before the
9
+ * utilities, so overlays must portal」
10
+ * - 下载用 Blob + `<a download>`(M11),不经过任何服务端
11
+ *
12
+ * 与 Host 的通信走 `ctx.remote` 不可用时的退化路径:直接用 `ctx.get('connection')`
13
+ * 拿到的 RPC 若不存在,则回退到插件自己的 `host.call`(动态半侧)或抛可读错误。
14
+ * 这里刻意把传输层收在 `callHost()` 一个函数里,换传输只改这一处。
15
+ */
16
+ window.__ModuleLoader__.load({
17
+ id: 'dsh-share-html',
18
+ factory: (require) => {
19
+ var module = { exports: {} }
20
+ var exports = module.exports
21
+ var React = require('react')
22
+ var h = React.createElement
23
+ var useState = React.useState
24
+ var useEffect = React.useEffect
25
+ var useMemo = React.useMemo
26
+ var useRef = React.useRef
27
+
28
+ var SLOT_ACTIONS = 'conversation.session.header.actions'
29
+
30
+ // ---- 令牌:与导出产物同源(DSH 宿主内它们本来就有效,可直接引用变量) ------
31
+ var RADIUS_SM = 'var(--dsw-radius-sm, 8px)'
32
+ var RADIUS_MD = 'var(--dsw-radius-md, 12px)'
33
+ var RADIUS_LG = 'var(--dsw-radius-lg, 16px)'
34
+ var RADIUS_XL = 'var(--dsw-radius-xl, 20px)'
35
+ var LABEL = 'var(--dsw-alias-label-primary, #0f1115)'
36
+ var LABEL_2 = 'var(--dsw-alias-label-secondary, #61666b)'
37
+ var LABEL_3 = 'var(--dsw-alias-label-tertiary, #81858c)'
38
+ var BORDER_2 = 'var(--dsw-alias-border-l2, #0000001a)'
39
+ var BORDER_3 = 'var(--dsw-alias-border-l3, #0000001f)'
40
+ var BG_LAYER_2 = 'var(--dsw-alias-bg-layer-2, #fff)'
41
+ var BG_LAYER_3 = 'var(--dsw-alias-bg-layer-3, #fff)'
42
+ var MASK_2 = 'var(--dsw-alias-bg-mask-2, #0000001f)'
43
+ var LINK = 'var(--dsw-alias-link, #4176e6)'
44
+ var DANGER = 'var(--dsw-alias-state-error-primary, #ec1313)'
45
+ var WARN = 'var(--dsw-alias-state-warn-primary, #f59e0b)'
46
+ var FONT_TEXT = 'var(--dsw-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif)'
47
+ var FONT_CODE = 'var(--ds-font-family-code, "SF Mono", Menlo, monospace)'
48
+
49
+ // ---- 传输层(唯一入口) ---------------------------------------------------
50
+ // 包形式插件经 Connection 的通用 RPC 与 Host 通信(走共享 /api 通道):
51
+ // ctx.connection.rpc.call('/api', 'share-html/<method>', payload)
52
+ // 返回值是 { ok: true, value } | { ok: false, error }。
53
+ // 业务错误在 value 里以 { __error } 形状返回,这里统一转成抛异常,上层只管 catch。
54
+ // Host 半用 connection.rpc.intercept 认领 'share-html/' 前缀(见 lib/index.js)。
55
+ var RPC_CHANNEL = '/api'
56
+ var RPC_PREFIX = 'share-html/'
57
+
58
+ function getRpc(ctx) {
59
+ var connection = ctx && typeof ctx.get === 'function' ? ctx.get('connection') : undefined
60
+ var rpc = connection && connection.rpc
61
+ if (!rpc || typeof rpc.call !== 'function') {
62
+ var err = new Error(
63
+ 'share-html: Connection RPC 不可用(ctx.connection.rpc.call 缺失)。'
64
+ + '请确认 dsh-client-connection 已装载且本插件的 Host 半已注入 connection。',
65
+ )
66
+ err.code = 'transport-unavailable'
67
+ throw err
68
+ }
69
+ return rpc
70
+ }
71
+
72
+ // 新 core:Host 用 connection.fetch 在 /api/share-html/<method> 注册 exact Fetch route,
73
+ // 客户端直接 POST 同一路径即可(cookie 认证,不走共享 /api 通道)。
74
+ // 老 core:退回 connection.rpc.call("/api", "share-html/<method>")。
75
+ function parseEnvelope(result) {
76
+ if (!result || result.ok !== true) {
77
+ var detail = result && result.error ? (result.error.message || result.error.code) : "传输失败"
78
+ var transportErr = new Error(detail)
79
+ transportErr.code = result && result.error ? result.error.code : "transport"
80
+ throw transportErr
81
+ }
82
+ var value = result.value
83
+ if (value && value.__error) {
84
+ var err = new Error(value.message || "share-html 调用失败")
85
+ err.code = value.code
86
+ throw err
87
+ }
88
+ return value
89
+ }
90
+
91
+ function legacyInvoke(ctx, endpoint, payload) {
92
+ var rpc = getRpc(ctx)
93
+ return Promise.resolve(rpc.call(RPC_CHANNEL, endpoint, payload)).then(parseEnvelope)
94
+ }
95
+
96
+ function invoke(ctx, method, args) {
97
+ var endpoint = String(method).indexOf(RPC_PREFIX) === 0 ? String(method) : RPC_PREFIX + String(method)
98
+ var payload = args === undefined ? null : args
99
+ var url = "/api/" + endpoint
100
+ return fetch(url, {
101
+ method: "POST",
102
+ headers: { "content-type": "application/json" },
103
+ body: JSON.stringify(payload),
104
+ credentials: "same-origin",
105
+ }).then(function (response) {
106
+ if (response.status === 404) return legacyInvoke(ctx, endpoint, payload)
107
+ return response.json().then(parseEnvelope)
108
+ }, function () {
109
+ return legacyInvoke(ctx, endpoint, payload)
110
+ })
111
+ }
112
+
113
+ // ---- 小部件 ---------------------------------------------------------------
114
+
115
+ function Button(props) {
116
+ var style = {
117
+ display: 'inline-flex',
118
+ alignItems: 'center',
119
+ gap: 6,
120
+ height: 28,
121
+ padding: '0 10px',
122
+ border: '1px solid ' + BORDER_2,
123
+ borderRadius: RADIUS_SM,
124
+ background: props.primary ? LABEL : 'transparent',
125
+ color: props.primary ? 'var(--dsw-alias-label-primary-inverted, #fff)' : LABEL,
126
+ fontFamily: FONT_TEXT,
127
+ fontSize: 13,
128
+ lineHeight: '20px',
129
+ cursor: props.disabled ? 'not-allowed' : 'pointer',
130
+ opacity: props.disabled ? 0.5 : 1,
131
+ whiteSpace: 'nowrap',
132
+ }
133
+ return h('button', {
134
+ type: 'button',
135
+ onClick: props.disabled ? undefined : props.onClick,
136
+ disabled: props.disabled,
137
+ style: style,
138
+ title: props.title,
139
+ }, props.children)
140
+ }
141
+
142
+ function Segmented(props) {
143
+ return h('div', {
144
+ style: {
145
+ display: 'inline-flex',
146
+ padding: 2,
147
+ gap: 2,
148
+ borderRadius: RADIUS_SM,
149
+ background: 'var(--dsw-alias-interactive-bg-hover, rgba(38,49,72,.06))',
150
+ },
151
+ }, props.options.map(function (option) {
152
+ var active = option.value === props.value
153
+ return h('button', {
154
+ key: option.value,
155
+ type: 'button',
156
+ onClick: function () { props.onChange(option.value) },
157
+ title: option.hint,
158
+ style: {
159
+ border: 'none',
160
+ borderRadius: 6,
161
+ padding: '4px 10px',
162
+ fontFamily: FONT_TEXT,
163
+ fontSize: 13,
164
+ lineHeight: '18px',
165
+ cursor: 'pointer',
166
+ background: active ? BG_LAYER_2 : 'transparent',
167
+ color: active ? LABEL : LABEL_2,
168
+ boxShadow: active ? '0 1px 2px rgba(0,0,0,.06)' : 'none',
169
+ },
170
+ }, option.label)
171
+ }))
172
+ }
173
+
174
+ function Switch(props) {
175
+ var on = Boolean(props.checked)
176
+ return h('label', {
177
+ style: {
178
+ display: 'flex',
179
+ alignItems: 'center',
180
+ justifyContent: 'space-between',
181
+ gap: 12,
182
+ padding: '6px 0',
183
+ cursor: props.disabled ? 'not-allowed' : 'pointer',
184
+ opacity: props.disabled ? 0.55 : 1,
185
+ fontSize: 13,
186
+ lineHeight: '20px',
187
+ color: LABEL,
188
+ fontFamily: FONT_TEXT,
189
+ },
190
+ }, [
191
+ h('span', { key: 'label', style: { display: 'flex', flexDirection: 'column', gap: 1 } }, [
192
+ h('span', { key: 't' }, props.label),
193
+ props.hint ? h('span', { key: 'h', style: { fontSize: 12, color: LABEL_3, lineHeight: '16px' } }, props.hint) : null,
194
+ ]),
195
+ h('span', {
196
+ key: 'box',
197
+ style: {
198
+ flex: 'none',
199
+ width: 32,
200
+ height: 18,
201
+ borderRadius: 9,
202
+ background: on ? LABEL : 'var(--dsw-alias-border-l4, rgba(0,0,0,.16))',
203
+ position: 'relative',
204
+ transition: 'background 120ms',
205
+ },
206
+ }, h('span', {
207
+ style: {
208
+ position: 'absolute',
209
+ top: 2,
210
+ left: on ? 16 : 2,
211
+ width: 14,
212
+ height: 14,
213
+ borderRadius: 7,
214
+ background: 'var(--dsw-alias-switch-thumb, #fff)',
215
+ transition: 'left 120ms',
216
+ },
217
+ })),
218
+ ])
219
+ }
220
+
221
+ // ---- 弹窗(M10) ----------------------------------------------------------
222
+
223
+ var PRESETS = [
224
+ { value: 'conversation', label: '对话', hint: '提问 + 最终回复' },
225
+ { value: 'process', label: '过程', hint: '加上思考、工具调用与检索' },
226
+ { value: 'full', label: '完整', hint: '加上工具结果全文、文件与模型信息' },
227
+ ]
228
+
229
+ var INCLUDE_LABELS = {
230
+ reasoning: { label: '思考过程', hint: '模型的推理内容' },
231
+ retrieval: { label: '检索结果', hint: '搜索结果条目' },
232
+ toolCalls: { label: '工具调用', hint: '工具名与参数' },
233
+ toolResults: { label: '工具结果', hint: '结果正文(关掉只留一行摘要)' },
234
+ files: { label: '附件', hint: '只显示文件名与类型,不含内容' },
235
+ deepResearch: { label: '深度研究', hint: '深研过程折叠展示' },
236
+ modelMeta: { label: '模型信息', hint: '模型名与耗时' },
237
+ }
238
+
239
+ function presetInclude(preset) {
240
+ if (preset === 'conversation') {
241
+ return { reasoning: false, retrieval: false, toolCalls: false, toolResults: false, files: false, deepResearch: false, modelMeta: false }
242
+ }
243
+ if (preset === 'process') {
244
+ return { reasoning: true, retrieval: true, toolCalls: true, toolResults: false, files: true, deepResearch: true, modelMeta: false }
245
+ }
246
+ return { reasoning: true, retrieval: true, toolCalls: true, toolResults: true, files: true, deepResearch: true, modelMeta: true }
247
+ }
248
+
249
+ function formatBytes(n) {
250
+ if (!n && n !== 0) return '—'
251
+ if (n < 1024) return n + ' B'
252
+ if (n < 1024 * 1024) return (n / 1024).toFixed(1) + ' KB'
253
+ return (n / 1024 / 1024).toFixed(2) + ' MB'
254
+ }
255
+
256
+ /** 预览栏左侧状态文字:把三种互斥状态收敛成一处,避免嵌套三元。 */
257
+ function statusText(busy, error, result) {
258
+ if (busy) return '渲染中…'
259
+ if (error) return '渲染失败'
260
+ if (!result) return '—'
261
+ return result.stats.turns + ' 轮 · ' + result.stats.messages + ' 条消息 · ' + formatBytes(result.stats.bytes)
262
+ }
263
+
264
+ function Dialog(props) {
265
+ var ctx = props.ctx
266
+ var sessionId = props.sessionId
267
+ var onClose = props.onClose
268
+
269
+ var presetState = useState('conversation')
270
+ var preset = presetState[0]
271
+ var setPreset = presetState[1]
272
+
273
+ var includeState = useState(presetInclude('conversation'))
274
+ var include = includeState[0]
275
+ var setInclude = includeState[1]
276
+
277
+ var redactState = useState(true)
278
+ var redact = redactState[0]
279
+ var setRedact = redactState[1]
280
+
281
+ var modeState = useState('mask')
282
+ var redactMode = modeState[0]
283
+ var setRedactMode = modeState[1]
284
+
285
+ var confirmState = useState(false)
286
+ var needConfirm = confirmState[0]
287
+ var setNeedConfirm = confirmState[1]
288
+
289
+ var resultState = useState(null)
290
+ var result = resultState[0]
291
+ var setResult = resultState[1]
292
+
293
+ var errorState = useState(null)
294
+ var error = errorState[0]
295
+ var setError = errorState[1]
296
+
297
+ var busyState = useState(false)
298
+ var busy = busyState[0]
299
+ var setBusy = busyState[1]
300
+
301
+ var seq = useRef(0)
302
+
303
+ // 档位切换:把开关重置成该档默认(逐项开关仍可再改)
304
+ useEffect(function () {
305
+ setInclude(presetInclude(preset))
306
+ }, [preset])
307
+
308
+ // 预览实时更新:任一选项变化就重渲染,用序号丢弃过期结果
309
+ useEffect(function () {
310
+ if (!sessionId) return
311
+ if (!redact && !needConfirm) {
312
+ setNeedConfirm(true)
313
+ return
314
+ }
315
+ if (!redact && needConfirm) return
316
+ var mine = ++seq.current
317
+ setBusy(true)
318
+ var request = {
319
+ sessionId: sessionId,
320
+ preset: preset,
321
+ include: include,
322
+ redact: redact,
323
+ redactMode: redactMode,
324
+ }
325
+ var timer = setTimeout(function () {
326
+ invoke(ctx, 'share-html/render', request).then(function (value) {
327
+ if (mine !== seq.current) return
328
+ setResult(value)
329
+ setError(null)
330
+ }).catch(function (err) {
331
+ if (mine !== seq.current) return
332
+ setResult(null)
333
+ setError(err)
334
+ }).then(function () {
335
+ if (mine !== seq.current) return
336
+ setBusy(false)
337
+ })
338
+ }, 180)
339
+ return function () { clearTimeout(timer) }
340
+ }, [sessionId, preset, JSON.stringify(include), redact, redactMode])
341
+
342
+ var download = function () {
343
+ if (!result || !result.html) return
344
+ var blob = new Blob([result.html], { type: 'text/html;charset=utf-8' })
345
+ var url = URL.createObjectURL(blob)
346
+ var a = document.createElement('a')
347
+ a.href = url
348
+ a.download = result.filename
349
+ document.body.appendChild(a)
350
+ a.click()
351
+ document.body.removeChild(a)
352
+ setTimeout(function () { URL.revokeObjectURL(url) }, 1000)
353
+ }
354
+
355
+ var overlay = h('div', {
356
+ onClick: function (e) { if (e.target === e.currentTarget) onClose() },
357
+ style: {
358
+ position: 'fixed',
359
+ inset: 0,
360
+ zIndex: 2147483000,
361
+ background: MASK_2,
362
+ display: 'flex',
363
+ alignItems: 'center',
364
+ justifyContent: 'center',
365
+ padding: 24,
366
+ fontFamily: FONT_TEXT,
367
+ },
368
+ }, h('div', {
369
+ role: 'dialog',
370
+ 'aria-modal': 'true',
371
+ 'aria-label': '分享会话',
372
+ style: {
373
+ display: 'flex',
374
+ flexDirection: 'column',
375
+ width: 'min(980px, 100%)',
376
+ height: 'min(720px, 100%)',
377
+ background: BG_LAYER_2,
378
+ borderRadius: RADIUS_XL,
379
+ boxShadow: '0 12px 32px rgba(0,0,0,.16)',
380
+ overflow: 'hidden',
381
+ },
382
+ }, [
383
+ // 头部
384
+ h('div', {
385
+ key: 'head',
386
+ style: {
387
+ display: 'flex', alignItems: 'center', justifyContent: 'space-between',
388
+ gap: 12, padding: '14px 18px', borderBottom: '1px solid ' + BORDER_2,
389
+ },
390
+ }, [
391
+ h('div', { key: 't', style: { display: 'flex', flexDirection: 'column' } }, [
392
+ h('div', { key: 'a', style: { fontSize: 15, fontWeight: 600, color: LABEL } }, '分享会话'),
393
+ h('div', { key: 'b', style: { fontSize: 12, color: LABEL_3, lineHeight: '16px' } },
394
+ '导出为单个离线 HTML 文件;不含脚本、不加载任何外部资源'),
395
+ ]),
396
+ h('button', {
397
+ key: 'x', type: 'button', onClick: onClose, 'aria-label': '关闭',
398
+ style: {
399
+ border: 'none', background: 'transparent', cursor: 'pointer',
400
+ fontSize: 18, lineHeight: '20px', color: LABEL_3, padding: 4,
401
+ },
402
+ }, '×'),
403
+ ]),
404
+
405
+ // 主体:左选项 / 右预览
406
+ h('div', {
407
+ key: 'body',
408
+ style: { display: 'flex', flex: 1, minHeight: 0 },
409
+ }, [
410
+ h('div', {
411
+ key: 'left',
412
+ style: {
413
+ width: 300, flex: 'none', padding: '14px 18px', overflowY: 'auto',
414
+ borderRight: '1px solid ' + BORDER_2, display: 'flex', flexDirection: 'column', gap: 14,
415
+ },
416
+ }, [
417
+ h('div', { key: 'p', style: { display: 'flex', flexDirection: 'column', gap: 8 } }, [
418
+ h('div', { key: 'l', style: { fontSize: 12, color: LABEL_3 } }, '分享粒度'),
419
+ h(Segmented, {
420
+ key: 's', value: preset, options: PRESETS,
421
+ onChange: function (v) { setPreset(v) },
422
+ }),
423
+ h('div', { key: 'h', style: { fontSize: 12, color: LABEL_3, lineHeight: '16px' } },
424
+ (PRESETS.filter(function (p) { return p.value === preset })[0] || {}).hint || ''),
425
+ ]),
426
+
427
+ h('div', { key: 'i', style: { display: 'flex', flexDirection: 'column', gap: 2 } },
428
+ Object.keys(INCLUDE_LABELS).map(function (key) {
429
+ return h(Switch, {
430
+ key: key,
431
+ label: INCLUDE_LABELS[key].label,
432
+ hint: INCLUDE_LABELS[key].hint,
433
+ checked: Boolean(include[key]),
434
+ onChange: function () {
435
+ var next = Object.assign({}, include)
436
+ next[key] = !next[key]
437
+ setInclude(next)
438
+ },
439
+ })
440
+ })),
441
+
442
+ h('div', { key: 'r', style: { borderTop: '1px solid ' + BORDER_2, paddingTop: 10, display: 'flex', flexDirection: 'column', gap: 6 } }, [
443
+ h(Switch, {
444
+ key: 'sw', label: '脱敏', hint: 'API Key / 令牌 / 邮箱 / 内网 IP / 绝对路径',
445
+ checked: redact,
446
+ onChange: function () {
447
+ setRedact(!redact)
448
+ if (redact) setNeedConfirm(false)
449
+ },
450
+ }),
451
+ redact ? h(Segmented, {
452
+ key: 'm', value: redactMode,
453
+ options: [
454
+ { value: 'mask', label: '遮蔽', hint: '替换为 [REDACTED]' },
455
+ { value: 'mask-hash', label: '哈希', hint: '同值同标记,保留相等性' },
456
+ ],
457
+ onChange: function (v) { setRedactMode(v) },
458
+ }) : null,
459
+ ]),
460
+
461
+ !redact ? h('div', {
462
+ key: 'warn',
463
+ style: {
464
+ padding: '10px 12px', borderRadius: RADIUS_SM,
465
+ background: 'color-mix(in srgb, ' + DANGER + ' 10%, transparent)',
466
+ border: '1px solid color-mix(in srgb, ' + DANGER + ' 40%, transparent)',
467
+ color: LABEL, fontSize: 12, lineHeight: '18px',
468
+ display: 'flex', flexDirection: 'column', gap: 8,
469
+ },
470
+ }, [
471
+ h('div', { key: 'm' }, '关闭脱敏会把原始文本写进文件:API Key、邮箱、内网地址都不会被遮蔽。'),
472
+ needConfirm
473
+ ? h(Button, { key: 'c', onClick: function () { setNeedConfirm(false) } }, '我已了解,继续导出')
474
+ : h('div', { key: 'ok', style: { color: LABEL_3 } }, '已确认'),
475
+ ]) : null,
476
+ ]),
477
+
478
+ h('div', {
479
+ key: 'right',
480
+ style: { flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', background: BG_LAYER_3 },
481
+ }, [
482
+ h('div', {
483
+ key: 'bar',
484
+ style: {
485
+ display: 'flex', alignItems: 'center', gap: 10, padding: '8px 14px',
486
+ borderBottom: '1px solid ' + BORDER_2, fontSize: 12, color: LABEL_3,
487
+ },
488
+ }, [
489
+ h('span', { key: 's' }, statusText(busy, error, result)),
490
+ h('span', { key: 'f', style: { marginLeft: 'auto', fontFamily: FONT_CODE } }, result ? result.filename : ''),
491
+ ]),
492
+ h('div', { key: 'preview', style: { flex: 1, minHeight: 0, position: 'relative' } }, [
493
+ error
494
+ ? h('div', {
495
+ key: 'e',
496
+ style: { padding: 20, color: DANGER, fontSize: 13, lineHeight: '20px' },
497
+ }, '导出失败:' + error.message)
498
+ : result
499
+ ? h('iframe', {
500
+ key: 'i',
501
+ title: '预览',
502
+ sandbox: '',
503
+ srcDoc: result.html,
504
+ style: { width: '100%', height: '100%', border: 'none', background: '#fff' },
505
+ })
506
+ : h('div', {
507
+ key: 'empty',
508
+ style: { display: 'grid', placeItems: 'center', height: '100%', color: LABEL_3, fontSize: 13 },
509
+ }, busy ? '正在生成预览…' : '选择粒度后自动生成预览'),
510
+ ]),
511
+ result && result.warnings && result.warnings.length
512
+ ? h('div', {
513
+ key: 'wr',
514
+ style: {
515
+ padding: '6px 14px', fontSize: 12, color: LABEL_3,
516
+ borderTop: '1px solid ' + BORDER_2, display: 'flex', gap: 8, flexWrap: 'wrap',
517
+ },
518
+ }, result.warnings.map(function (w, i) { return h('span', { key: i }, '· ' + w) }))
519
+ : null,
520
+ ]),
521
+ ]),
522
+
523
+ // 底部操作
524
+ h('div', {
525
+ key: 'foot',
526
+ style: {
527
+ display: 'flex', alignItems: 'center', justifyContent: 'flex-end',
528
+ gap: 8, padding: '12px 18px', borderTop: '1px solid ' + BORDER_2,
529
+ },
530
+ }, [
531
+ h(Button, { key: 'c', onClick: onClose }, '取消'),
532
+ h(Button, {
533
+ key: 'd', primary: true, disabled: !result || busy, onClick: download,
534
+ title: result ? '下载 ' + result.filename : '尚无可用产物',
535
+ }, '下载 .html'),
536
+ ]),
537
+ ]))
538
+ return overlay
539
+ }
540
+
541
+ // ---- 入口按钮(M9) -------------------------------------------------------
542
+
543
+ /** 插槽组件:宿主下发 sessionId,inject 下发 ctx。 */
544
+ function ShareHeaderAction(props) {
545
+ var sessionId = props.sessionId
546
+ || (props.session && (props.session.id || props.session.sessionId))
547
+ || props.session_id
548
+ var ctx = props.ctx
549
+ var openState = useState(false)
550
+ var open = openState[0]
551
+ var setOpen = openState[1]
552
+
553
+ // 弹窗要 portal 到 body:header 插槽会裁切溢出内容(插槽文档明确要求)
554
+ var Portal = function (child) {
555
+ if (typeof document === 'undefined') return null
556
+ var host = document.getElementById('dsh-share-html-root')
557
+ if (!host) {
558
+ host = document.createElement('div')
559
+ host.id = 'dsh-share-html-root'
560
+ document.body.appendChild(host)
561
+ }
562
+ return require('react-dom').createPortal(child, host)
563
+ }
564
+
565
+ return h(React.Fragment, null, [
566
+ h(Button, {
567
+ key: 'btn',
568
+ onClick: function () { setOpen(true) },
569
+ disabled: !sessionId,
570
+ title: sessionId ? '导出当前会话为离线 HTML' : '当前没有可导出的会话',
571
+ }, '分享'),
572
+ open && sessionId
573
+ ? Portal(h(Dialog, {
574
+ ctx: ctx,
575
+ sessionId: sessionId,
576
+ onClose: function () { setOpen(false) },
577
+ }))
578
+ : null,
579
+ ])
580
+ }
581
+
582
+ // ---- 注册 ---------------------------------------------------------------
583
+ //
584
+ // 插槽 API(对照 dsh-client-ui-jobs 的真实用法):
585
+ // ctx.slots.register({ name, id, order, locale?, inject? }, Component)
586
+ // 组件 props 里带 sessionId 等宿主下发字段;inject() 的返回值也会注入 props。
587
+ // 这里用 inject 把 ctx 与调用封装送进组件,避免组件闭包依赖外部变量。
588
+
589
+ exports.name = 'dsh-share-html/client'
590
+ exports.inject = ['slots', 'connection']
591
+
592
+ exports.apply = function apply(ctx) {
593
+ ctx.slots.inject(SLOT_ACTIONS, function () {
594
+ return ctx.slots.register({
595
+ name: SLOT_ACTIONS,
596
+ id: 'share-html',
597
+ order: 20,
598
+ locale: null,
599
+ inject: function () {
600
+ return {
601
+ ctx: ctx,
602
+ shareInvoke: function (method, args) {
603
+ return invoke(ctx, method, args)
604
+ },
605
+ }
606
+ },
607
+ }, ShareHeaderAction)
608
+ })
609
+ console.log('[dsh-share-html] registered share button on ' + SLOT_ACTIONS)
610
+ }
611
+
612
+ return module.exports
613
+ },
614
+ })