@yunkersek/dsh-prtsui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +82 -2
- package/SKILL.md +72 -0
- package/client.js +750 -0
- package/cordis.patch.yml +6 -0
- package/docs/harness.html +785 -0
- package/docs/prtsui-preview.png +0 -0
- package/icon.svg +9 -0
- package/index.js +198 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +59 -4
package/client.js
ADDED
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yunkersek/dsh-prtsui — client half (web).
|
|
3
|
+
*
|
|
4
|
+
* Finds ```dsh-ui fenced JSON in the rendered transcript and mounts it as a
|
|
5
|
+
* PRTS-styled DOM tree (prts-design visual language: terminal surfaces, cyan
|
|
6
|
+
* rails, square corners, hairline strokes, uppercase overline labels).
|
|
7
|
+
*
|
|
8
|
+
* Pure DOM on purpose: no react-dom, no runtime assets, no CDN.
|
|
9
|
+
*/
|
|
10
|
+
window.__ModuleLoader__.load({
|
|
11
|
+
id: '@yunkersek/dsh-prtsui',
|
|
12
|
+
factory(require) {
|
|
13
|
+
const TYPES = new Set([
|
|
14
|
+
'text', 'hero', 'callout', 'list', 'table', 'stat', 'stats', 'keyvalue',
|
|
15
|
+
'steps', 'choices', 'tabs', 'card', 'chart', 'code', 'divider', 'kbd',
|
|
16
|
+
'badge', 'progress', 'quote', 'compare', 'image', 'accordion',
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
const CSS = `
|
|
20
|
+
.prtsui{--p-bg:var(--dsw-alias-bg-layer-1,#1a1b1c);--p-bg2:var(--dsw-alias-bg-layer-2,#232425);--p-line:var(--dsw-alias-border-l2,#2f3133);--p-soft:var(--dsw-alias-border-l1,#262729);--p-fg:var(--dsw-alias-label-primary,#f0f0f0);--p-fg2:var(--dsw-alias-label-secondary,#c9c9c9);--p-acc:var(--dsw-alias-brand-primary,#18d1ff);--p-acc2:var(--dsw-alias-state-warn-primary,#ffb340);--p-ok:var(--dsw-alias-state-success-primary,#4fd18f);--p-warn:var(--dsw-alias-state-warn-primary,#ffb340);--p-err:var(--dsw-alias-state-error-primary,#ff5c5c);--p-on:var(--dsw-alias-label-on-color,#04161c);--p-mono:ui-monospace,SFMono-Regular,Consolas,monospace;position:relative;margin:10px 0;border:1px solid var(--p-line);background:var(--p-bg);color:var(--p-fg);font-size:13px;line-height:1.66;border-radius:2px;overflow:hidden}
|
|
21
|
+
.prtsui::before{content:'';position:absolute;left:0;top:0;width:38px;height:2px;background:var(--p-acc);z-index:2}
|
|
22
|
+
.prtsui-banner{display:flex;align-items:center;justify-content:space-between;gap:10px;padding:7px 12px;border-bottom:1px solid var(--p-soft);background:linear-gradient(180deg,color-mix(in srgb,var(--p-acc) 8%,transparent),transparent)}
|
|
23
|
+
.prtsui-banner span{font-size:10px;letter-spacing:.16em;text-transform:uppercase;color:var(--p-fg2)}
|
|
24
|
+
.prtsui-banner i{font:600 10px/1 var(--p-mono);font-style:normal;letter-spacing:.1em;color:var(--p-acc)}
|
|
25
|
+
.prtsui-body{display:flex;flex-direction:column;gap:var(--prtsui-gap,10px);padding:12px}
|
|
26
|
+
.prtsui p{margin:0}
|
|
27
|
+
.prtsui-eyebrow{font-size:10px;letter-spacing:.16em;text-transform:uppercase;color:color-mix(in srgb,var(--p-fg2) 72%,transparent)}
|
|
28
|
+
.prtsui-hero{display:flex;flex-direction:column;gap:4px;padding-bottom:10px;border-bottom:1px solid var(--p-soft)}
|
|
29
|
+
.prtsui-hero h3{margin:0;font-size:20px;line-height:1.24;font-weight:700;letter-spacing:.02em}
|
|
30
|
+
.prtsui-hero small{color:var(--p-fg2);font-size:12px}
|
|
31
|
+
.prtsui-callout{position:relative;padding:9px 12px 9px 14px;background:var(--p-bg2);border-left:3px solid var(--p-info)}
|
|
32
|
+
.prtsui-callout[data-tone=success]{border-left-color:var(--p-ok)}
|
|
33
|
+
.prtsui-callout[data-tone=warning]{border-left-color:var(--p-warn)}
|
|
34
|
+
.prtsui-callout[data-tone=error]{border-left-color:var(--p-err)}
|
|
35
|
+
.prtsui-callout[data-tone=brand]{border-left-color:var(--p-acc)}
|
|
36
|
+
.prtsui-callout b{display:block;margin-bottom:2px;font-size:11px;letter-spacing:.12em;text-transform:uppercase;color:var(--p-fg2);font-weight:600}
|
|
37
|
+
.prtsui-list{margin:0;padding:0;list-style:none;display:flex;flex-direction:column}
|
|
38
|
+
.prtsui-list li{display:flex;gap:9px;padding:4px 0;border-bottom:1px solid var(--p-soft)}
|
|
39
|
+
.prtsui-list li:last-child{border-bottom:0}
|
|
40
|
+
.prtsui-list .idx{flex:none;min-width:18px;font:600 10px/1.9 var(--p-mono);color:color-mix(in srgb,var(--p-acc) 82%,transparent);letter-spacing:.06em}
|
|
41
|
+
.prtsui-list .txt{flex:1}
|
|
42
|
+
.prtsui-list .badge{flex:none}
|
|
43
|
+
.prtsui table{width:100%;border-collapse:collapse;font-size:12px}
|
|
44
|
+
.prtsui caption{caption-side:top;text-align:left;padding-bottom:5px;font-size:10px;letter-spacing:.14em;text-transform:uppercase;color:color-mix(in srgb,var(--p-fg2) 70%,transparent)}
|
|
45
|
+
.prtsui th{text-align:left;font:600 10px/1.6 var(--p-mono);letter-spacing:.12em;text-transform:uppercase;color:var(--p-fg2);padding:0 10px 6px;border-bottom:1px solid var(--p-line);white-space:nowrap}
|
|
46
|
+
.prtsui td{padding:6px 10px;border-bottom:1px solid var(--p-soft);vertical-align:top}
|
|
47
|
+
.prtsui tr:last-child td{border-bottom:0}
|
|
48
|
+
.prtsui td[data-align=right],.prtsui th[data-align=right]{text-align:right;font-variant-numeric:tabular-nums}
|
|
49
|
+
.prtsui td[data-align=center],.prtsui th[data-align=center]{text-align:center}
|
|
50
|
+
.prtsui-stats{display:grid;grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:1px;background:var(--p-soft)}
|
|
51
|
+
.prtsui-stat{background:var(--p-bg);padding:9px 11px;display:flex;flex-direction:column;gap:2px}
|
|
52
|
+
.prtsui-stat span{font-size:10px;letter-spacing:.12em;text-transform:uppercase;color:color-mix(in srgb,var(--p-fg2) 76%,transparent)}
|
|
53
|
+
.prtsui-stat strong{font:700 18px/1.2 var(--p-mono);font-variant-numeric:tabular-nums;letter-spacing:.01em}
|
|
54
|
+
.prtsui-stat em{font-style:normal;font-size:11px;color:var(--p-fg2)}
|
|
55
|
+
.prtsui-stat i{font-style:normal;font:600 10px/1 var(--p-mono);letter-spacing:.06em;color:var(--p-ok)}
|
|
56
|
+
.prtsui-stat i[data-neg=true]{color:var(--p-err)}
|
|
57
|
+
.prtsui-kv{display:grid;grid-template-columns:auto 1fr;gap:1px;background:var(--p-soft);border:1px solid var(--p-soft)}
|
|
58
|
+
.prtsui-kv>div{background:var(--p-bg);padding:6px 10px;font-size:12px}
|
|
59
|
+
.prtsui-kv>div:nth-child(odd){color:var(--p-fg2);white-space:nowrap;font-size:11px;letter-spacing:.04em}
|
|
60
|
+
.prtsui-steps{display:flex;flex-direction:column;gap:0}
|
|
61
|
+
.prtsui-step{position:relative;display:flex;gap:10px;padding:0 0 12px 0}
|
|
62
|
+
.prtsui-step:last-child{padding-bottom:0}
|
|
63
|
+
.prtsui-step .dot{flex:none;width:9px;height:9px;margin-top:5px;border:1px solid var(--p-line);background:var(--p-bg);transform:rotate(45deg)}
|
|
64
|
+
.prtsui-step[data-state=done] .dot{border-color:var(--p-ok);background:var(--p-ok)}
|
|
65
|
+
.prtsui-step[data-state=active] .dot{border-color:var(--p-acc);background:color-mix(in srgb,var(--p-acc) 26%,transparent);box-shadow:0 0 0 3px color-mix(in srgb,var(--p-acc) 16%,transparent)}
|
|
66
|
+
.prtsui-step .rail{position:absolute;left:4px;top:16px;bottom:0;width:1px;background:var(--p-soft)}
|
|
67
|
+
.prtsui-step:last-child .rail{display:none}
|
|
68
|
+
.prtsui-step h4{margin:0;font-size:13px;font-weight:600}
|
|
69
|
+
.prtsui-step[data-state=active] h4{color:var(--p-acc)}
|
|
70
|
+
.prtsui-step[data-state=todo] h4{color:var(--p-fg2);font-weight:500}
|
|
71
|
+
.prtsui-step p{margin:2px 0 0;font-size:12px;color:var(--p-fg2)}
|
|
72
|
+
.prtsui-choices{display:flex;flex-direction:column;gap:6px}
|
|
73
|
+
.prtsui-choice{display:flex;align-items:flex-start;gap:9px;width:100%;text-align:left;padding:8px 11px;border:1px solid var(--p-line);border-left:2px solid transparent;background:var(--p-bg2);color:var(--p-fg);font:inherit;font-size:12px;cursor:pointer;border-radius:0;transition:border-color .12s,background .12s}
|
|
74
|
+
.prtsui-choice:hover{border-left-color:var(--p-acc);background:color-mix(in srgb,var(--p-acc) 12%,transparent)}
|
|
75
|
+
.prtsui-choice[data-picked=true]{border-left-color:var(--p-acc);color:var(--p-acc);background:color-mix(in srgb,var(--p-acc) 10%,transparent)}
|
|
76
|
+
.prtsui-choice .mark{flex:none;font:600 10px/1.7 var(--p-mono);color:color-mix(in srgb,var(--p-acc) 84%,transparent)}
|
|
77
|
+
.prtsui-choice small{display:block;color:var(--p-fg2);font-size:11px;margin-top:2px}
|
|
78
|
+
.prtsui-tabs{display:flex;flex-direction:column;gap:10px}
|
|
79
|
+
.prtsui-tabbar{display:flex;gap:2px;border-bottom:1px solid var(--p-soft)}
|
|
80
|
+
.prtsui-tab{appearance:none;border:0;background:none;color:var(--p-fg2);font:inherit;font-size:12px;padding:6px 12px 7px;cursor:pointer;border-bottom:2px solid transparent;margin-bottom:-1px}
|
|
81
|
+
.prtsui-tab:hover{color:var(--p-fg)}
|
|
82
|
+
.prtsui-tab[data-on=true]{color:var(--p-acc);border-bottom-color:var(--p-acc);font-weight:600}
|
|
83
|
+
.prtsui-panel{display:flex;flex-direction:column;gap:10px}
|
|
84
|
+
.prtsui-panel[hidden]{display:none}
|
|
85
|
+
.prtsui-card{border:1px solid var(--p-line);background:var(--p-bg2)}
|
|
86
|
+
.prtsui-card>.head{display:flex;align-items:baseline;gap:8px;padding:7px 11px;border-bottom:1px solid var(--p-soft);flex-wrap:wrap}
|
|
87
|
+
.prtsui-card>.head b{font-size:11px;letter-spacing:.12em;text-transform:uppercase;font-weight:600;color:var(--p-fg2)}
|
|
88
|
+
.prtsui-card>.head strong{font-size:13px;font-weight:600}
|
|
89
|
+
.prtsui-card>.body{padding:10px 11px;display:flex;flex-direction:column;gap:10px}
|
|
90
|
+
.prtsui-chart{display:flex;flex-direction:column;gap:6px}
|
|
91
|
+
.prtsui-chart svg{width:100%;display:block;overflow:visible}
|
|
92
|
+
.prtsui-bars{display:flex;flex-direction:column;gap:5px}
|
|
93
|
+
.prtsui-bar{display:grid;grid-template-columns:minmax(58px,auto) 1fr auto;gap:9px;align-items:center;font-size:11px}
|
|
94
|
+
.prtsui-bar span{color:var(--p-fg2);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
95
|
+
.prtsui-bar .track{height:8px;background:var(--p-bg2);border:1px solid var(--p-soft);position:relative}
|
|
96
|
+
.prtsui-bar .fill{height:100%;background:var(--p-acc)}
|
|
97
|
+
.prtsui-bar em{font:600 11px/1 var(--p-mono);font-style:normal;font-variant-numeric:tabular-nums;color:var(--p-fg2)}
|
|
98
|
+
.prtsui-legend{display:flex;flex-wrap:wrap;gap:10px;font-size:11px;color:var(--p-fg2)}
|
|
99
|
+
.prtsui-legend i{display:inline-block;width:8px;height:8px;margin-right:5px;vertical-align:middle}
|
|
100
|
+
.prtsui pre.code{margin:0;padding:10px 12px;background:var(--p-bg2);border:1px solid var(--p-soft);overflow:auto;font:12px/1.6 var(--p-mono);color:var(--p-fg)}
|
|
101
|
+
.prtsui-divider{display:flex;align-items:center;gap:10px;color:color-mix(in srgb,var(--p-fg2) 74%,transparent);font-size:10px;letter-spacing:.16em;text-transform:uppercase}
|
|
102
|
+
.prtsui-divider::before,.prtsui-divider::after{content:'';flex:1;height:1px;background:var(--p-soft)}
|
|
103
|
+
.prtsui-kbd{display:inline-flex;gap:4px}
|
|
104
|
+
.prtsui-kbd kbd{font:600 10px/1 var(--p-mono);padding:3px 5px;border:1px solid var(--p-line);border-bottom-width:2px;background:var(--p-bg2);color:var(--p-fg2);border-radius:2px}
|
|
105
|
+
.prtsui-badge{display:inline-flex;align-items:center;padding:1px 6px;border:1px solid var(--p-line);font:600 10px/1.6 var(--p-mono);letter-spacing:.06em;text-transform:uppercase;color:var(--p-fg2);border-radius:0}
|
|
106
|
+
.prtsui-badge[data-tone=brand]{border-color:color-mix(in srgb,var(--p-acc) 54%,transparent);color:var(--p-acc)}
|
|
107
|
+
.prtsui-badge[data-tone=success]{border-color:color-mix(in srgb,var(--p-ok) 54%,transparent);color:var(--p-ok)}
|
|
108
|
+
.prtsui-badge[data-tone=warning]{border-color:color-mix(in srgb,var(--p-warn) 54%,transparent);color:var(--p-warn)}
|
|
109
|
+
.prtsui-badge[data-tone=error]{border-color:color-mix(in srgb,var(--p-err) 54%,transparent);color:var(--p-err)}
|
|
110
|
+
.prtsui-progress{display:flex;flex-direction:column;gap:4px}
|
|
111
|
+
.prtsui-progress .lbl{display:flex;justify-content:space-between;font-size:10px;letter-spacing:.12em;text-transform:uppercase;color:var(--p-fg2)}
|
|
112
|
+
.prtsui-progress .lbl b{font-family:var(--p-mono);letter-spacing:.06em;font-variant-numeric:tabular-nums}
|
|
113
|
+
.prtsui-progress .track{height:8px;background:var(--p-bg2);border:1px solid var(--p-soft);display:flex}
|
|
114
|
+
.prtsui-progress .seg{height:100%}
|
|
115
|
+
.prtsui-progress .seg[data-tone=ok]{background:var(--p-ok)}
|
|
116
|
+
.prtsui-progress .seg[data-tone=brand]{background:var(--p-acc)}
|
|
117
|
+
.prtsui-progress .seg[data-tone=warn]{background:var(--p-warn)}
|
|
118
|
+
.prtsui-progress .seg[data-tone=err]{background:var(--p-err)}
|
|
119
|
+
.prtsui-progress .seg[data-tone=pending]{background:repeating-linear-gradient(-45deg,transparent 0 4px,color-mix(in srgb,var(--p-fg2) 22%,transparent) 4px 8px)}
|
|
120
|
+
.prtsui-quote{margin:0;padding:6px 0 6px 12px;border-left:2px solid var(--p-line);color:var(--p-fg2);font-style:italic}
|
|
121
|
+
.prtsui-quote cite{display:block;margin-top:4px;font-style:normal;font-size:11px;letter-spacing:.06em}
|
|
122
|
+
.prtsui-compare{display:grid;grid-template-columns:1fr 1fr;gap:1px;background:var(--p-soft)}
|
|
123
|
+
.prtsui-compare>div{background:var(--p-bg2);padding:9px 11px;display:flex;flex-direction:column;gap:6px}
|
|
124
|
+
.prtsui-compare b{font-size:10px;letter-spacing:.14em;text-transform:uppercase;color:var(--p-fg2)}
|
|
125
|
+
.prtsui-compare>div:last-child b{color:var(--p-acc)}
|
|
126
|
+
.prtsui figure{margin:0;display:flex;flex-direction:column;gap:5px}
|
|
127
|
+
.prtsui figure img{width:100%;display:block;border:1px solid var(--p-soft)}
|
|
128
|
+
.prtsui figcaption{font-size:11px;color:var(--p-fg2)}
|
|
129
|
+
.prtsui-acc{display:flex;flex-direction:column}
|
|
130
|
+
.prtsui-acc details{border-bottom:1px solid var(--p-soft);padding:6px 0}
|
|
131
|
+
.prtsui-acc summary{cursor:pointer;font-size:12px;font-weight:600;list-style:none;display:flex;gap:8px;align-items:center}
|
|
132
|
+
.prtsui-acc summary::before{content:'+';font:600 11px/1 var(--p-mono);color:var(--p-acc)}
|
|
133
|
+
.prtsui-acc details[open] summary::before{content:'−'}
|
|
134
|
+
.prtsui-acc .inner{padding:8px 0 4px 18px;display:flex;flex-direction:column;gap:10px}
|
|
135
|
+
.prtsui-raw-note{font-size:10px;letter-spacing:.1em;text-transform:uppercase;color:color-mix(in srgb,var(--p-fg2) 60%,transparent)}
|
|
136
|
+
@media (max-width:640px){.prtsui-compare{grid-template-columns:1fr}}
|
|
137
|
+
`
|
|
138
|
+
|
|
139
|
+
// ---------------- helpers ----------------
|
|
140
|
+
function h(tag, cls, text) {
|
|
141
|
+
const el = document.createElement(tag)
|
|
142
|
+
if (cls) el.className = cls
|
|
143
|
+
if (text !== undefined && text !== null) el.textContent = String(text)
|
|
144
|
+
return el
|
|
145
|
+
}
|
|
146
|
+
const s = (v) => (v === undefined || v === null ? '' : String(v))
|
|
147
|
+
const arr = (v) => (Array.isArray(v) ? v : [])
|
|
148
|
+
|
|
149
|
+
function tone(node) {
|
|
150
|
+
const t = s(node.tone).toLowerCase()
|
|
151
|
+
return ['info', 'success', 'warning', 'error', 'brand'].includes(t) ? t : 'info'
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---------------- node renderers ----------------
|
|
155
|
+
const R = {}
|
|
156
|
+
|
|
157
|
+
R.text = (n) => h('p', null, s(n.content ?? n.text))
|
|
158
|
+
|
|
159
|
+
R.hero = (n) => {
|
|
160
|
+
const box = h('div', 'prtsui-hero')
|
|
161
|
+
if (n.eyebrow) box.append(h('div', 'prtsui-eyebrow', n.eyebrow))
|
|
162
|
+
if (n.title) box.append(h('h3', null, n.title))
|
|
163
|
+
if (n.subtitle) box.append(h('small', null, n.subtitle))
|
|
164
|
+
if (n.content) box.append(h('p', null, n.content))
|
|
165
|
+
return box
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
R.callout = (n) => {
|
|
169
|
+
const box = h('div', 'prtsui-callout')
|
|
170
|
+
box.dataset.tone = tone(n)
|
|
171
|
+
if (n.title) box.append(h('b', null, n.title))
|
|
172
|
+
box.append(h('div', null, s(n.content)))
|
|
173
|
+
return box
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
R.list = (n) => {
|
|
177
|
+
const ol = h(n.ordered ? 'ol' : 'ul', 'prtsui-list')
|
|
178
|
+
arr(n.items).forEach((it, i) => {
|
|
179
|
+
const li = h('li')
|
|
180
|
+
li.append(h('span', 'idx', n.ordered ? String(i + 1).padStart(2, '0') : '·'))
|
|
181
|
+
const obj = it && typeof it === 'object' ? it : { text: it }
|
|
182
|
+
const txt = h('span', 'txt', s(obj.text ?? obj.content ?? obj.label))
|
|
183
|
+
li.append(txt)
|
|
184
|
+
if (obj.badge) {
|
|
185
|
+
const b = h('span', 'prtsui-badge', obj.badge)
|
|
186
|
+
if (obj.tone) b.dataset.tone = s(obj.tone).toLowerCase()
|
|
187
|
+
li.append(b)
|
|
188
|
+
}
|
|
189
|
+
ol.append(li)
|
|
190
|
+
})
|
|
191
|
+
return ol
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
R.table = (n) => {
|
|
195
|
+
const table = h('table')
|
|
196
|
+
if (n.caption) table.append(h('caption', null, n.caption))
|
|
197
|
+
const align = arr(n.align)
|
|
198
|
+
const thead = h('thead')
|
|
199
|
+
const hr = h('tr')
|
|
200
|
+
arr(n.columns).forEach((c, i) => {
|
|
201
|
+
const th = h('th', null, c)
|
|
202
|
+
if (align[i]) th.dataset.align = s(align[i])
|
|
203
|
+
hr.append(th)
|
|
204
|
+
})
|
|
205
|
+
thead.append(hr)
|
|
206
|
+
table.append(thead)
|
|
207
|
+
const tbody = h('tbody')
|
|
208
|
+
arr(n.rows).forEach((row) => {
|
|
209
|
+
const tr = h('tr')
|
|
210
|
+
arr(row).forEach((cell, i) => {
|
|
211
|
+
const td = h('td', null, s(cell))
|
|
212
|
+
if (align[i]) td.dataset.align = s(align[i])
|
|
213
|
+
tr.append(td)
|
|
214
|
+
})
|
|
215
|
+
tbody.append(tr)
|
|
216
|
+
})
|
|
217
|
+
table.append(tbody)
|
|
218
|
+
return table
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function statCard(n) {
|
|
222
|
+
const box = h('div', 'prtsui-stat')
|
|
223
|
+
box.append(h('span', null, s(n.label)))
|
|
224
|
+
box.append(h('strong', null, s(n.value)))
|
|
225
|
+
if (n.hint) box.append(h('em', null, s(n.hint)))
|
|
226
|
+
if (n.delta !== undefined && n.delta !== null) {
|
|
227
|
+
const d = h('i', null, s(n.delta))
|
|
228
|
+
d.dataset.neg = String(n.delta).trim().startsWith('-') ? 'true' : 'false'
|
|
229
|
+
box.append(d)
|
|
230
|
+
}
|
|
231
|
+
return box
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
R.stat = (n) => {
|
|
235
|
+
const wrap = h('div', 'prtsui-stats')
|
|
236
|
+
wrap.append(statCard(n))
|
|
237
|
+
return wrap
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
R.stats = (n) => {
|
|
241
|
+
const wrap = h('div', 'prtsui-stats')
|
|
242
|
+
arr(n.items).forEach((it) => wrap.append(statCard(it || {})))
|
|
243
|
+
return wrap
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
R.keyvalue = (n) => {
|
|
247
|
+
const grid = h('div', 'prtsui-kv')
|
|
248
|
+
arr(n.pairs).forEach((p) => {
|
|
249
|
+
grid.append(h('div', null, s(p.key ?? p.k)))
|
|
250
|
+
grid.append(h('div', null, s(p.value ?? p.v)))
|
|
251
|
+
})
|
|
252
|
+
return grid
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
R.steps = (n) => {
|
|
256
|
+
const wrap = h('div', 'prtsui-steps')
|
|
257
|
+
const current = typeof n.current === 'number' ? n.current : -1
|
|
258
|
+
arr(n.steps).forEach((st, i) => {
|
|
259
|
+
const obj = st && typeof st === 'object' ? st : { title: st }
|
|
260
|
+
const state = s(obj.state) || (current >= 0 ? (i < current ? 'done' : i === current ? 'active' : 'todo') : 'todo')
|
|
261
|
+
const row = h('div', 'prtsui-step')
|
|
262
|
+
row.dataset.state = ['done', 'active', 'todo'].includes(state) ? state : 'todo'
|
|
263
|
+
row.append(h('span', 'dot'))
|
|
264
|
+
row.append(h('span', 'rail'))
|
|
265
|
+
const body = h('div')
|
|
266
|
+
body.append(h('h4', null, s(obj.title)))
|
|
267
|
+
if (obj.content) body.append(h('p', null, s(obj.content)))
|
|
268
|
+
row.append(body)
|
|
269
|
+
wrap.append(row)
|
|
270
|
+
})
|
|
271
|
+
return wrap
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function dispatch(ctx, action, payload) {
|
|
275
|
+
const text = '[prtsui-action] ' + action + ' ' + JSON.stringify(payload || {})
|
|
276
|
+
try {
|
|
277
|
+
const sessions = ctx && ctx.sessions
|
|
278
|
+
if (!sessions) return
|
|
279
|
+
const ids = (sessions.list && sessions.list.getSnapshot && sessions.list.getSnapshot().ids) || []
|
|
280
|
+
let target = null
|
|
281
|
+
for (const id of ids) {
|
|
282
|
+
try {
|
|
283
|
+
const info = sessions.retainInfo && sessions.retainInfo(id)
|
|
284
|
+
const snap = info && info.getSnapshot && info.getSnapshot()
|
|
285
|
+
if (snap && snap.retainedBy && snap.retainedBy.mainView > 0) { target = id; break }
|
|
286
|
+
} catch { /* ignore */ }
|
|
287
|
+
}
|
|
288
|
+
if (!target) return
|
|
289
|
+
const conv = sessions.scope(target).get('conversation')
|
|
290
|
+
if (conv && typeof conv.send === 'function') conv.send(text)
|
|
291
|
+
} catch (err) {
|
|
292
|
+
console.warn('[prtsui] action failed', err)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
R.choices = (n, ctx) => {
|
|
297
|
+
const wrap = h('div', 'prtsui-choices')
|
|
298
|
+
const action = n.action ? s(n.action) : ''
|
|
299
|
+
arr(n.options).forEach((opt, i) => {
|
|
300
|
+
const obj = opt && typeof opt === 'object' ? opt : { label: opt }
|
|
301
|
+
const btn = h('button', 'prtsui-choice')
|
|
302
|
+
btn.type = 'button'
|
|
303
|
+
btn.append(h('span', 'mark', action ? '›' : String(i + 1).padStart(2, '0')))
|
|
304
|
+
const body = h('span')
|
|
305
|
+
body.append(h('span', null, s(obj.label)))
|
|
306
|
+
if (obj.description) body.append(h('small', null, s(obj.description)))
|
|
307
|
+
btn.append(body)
|
|
308
|
+
btn.addEventListener('click', () => {
|
|
309
|
+
wrap.querySelectorAll('.prtsui-choice').forEach((el) => { el.dataset.picked = 'false' })
|
|
310
|
+
btn.dataset.picked = 'true'
|
|
311
|
+
if (action) dispatch(ctx, action, { value: obj.value ?? obj.label ?? i, label: obj.label ?? null })
|
|
312
|
+
})
|
|
313
|
+
wrap.append(btn)
|
|
314
|
+
})
|
|
315
|
+
return wrap
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
R.tabs = (n, ctx) => {
|
|
319
|
+
const wrap = h('div', 'prtsui-tabs')
|
|
320
|
+
const bar = h('div', 'prtsui-tabbar')
|
|
321
|
+
const panes = h('div', 'prtsui-panel')
|
|
322
|
+
const panels = []
|
|
323
|
+
arr(n.tabs).forEach((tab, i) => {
|
|
324
|
+
const obj = tab && typeof tab === 'object' ? tab : { label: tab, items: [] }
|
|
325
|
+
const btn = h('button', 'prtsui-tab', s(obj.label))
|
|
326
|
+
btn.type = 'button'
|
|
327
|
+
btn.dataset.on = i === 0 ? 'true' : 'false'
|
|
328
|
+
const pane = h('div', 'prtsui-panel')
|
|
329
|
+
pane.hidden = i !== 0
|
|
330
|
+
arr(obj.items).forEach((child) => {
|
|
331
|
+
const el = renderNode(child, ctx)
|
|
332
|
+
if (el) pane.append(el)
|
|
333
|
+
})
|
|
334
|
+
panels.push({ btn, pane })
|
|
335
|
+
btn.addEventListener('click', () => {
|
|
336
|
+
panels.forEach((p, j) => {
|
|
337
|
+
p.btn.dataset.on = j === i ? 'true' : 'false'
|
|
338
|
+
p.pane.hidden = j !== i
|
|
339
|
+
})
|
|
340
|
+
})
|
|
341
|
+
bar.append(btn)
|
|
342
|
+
})
|
|
343
|
+
wrap.append(bar)
|
|
344
|
+
panels.forEach((p) => wrap.append(p.pane))
|
|
345
|
+
void panes
|
|
346
|
+
return wrap
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
R.card = (n, ctx) => {
|
|
350
|
+
const card = h('div', 'prtsui-card')
|
|
351
|
+
const head = h('div', 'head')
|
|
352
|
+
if (n.eyebrow) head.append(h('b', null, n.eyebrow))
|
|
353
|
+
if (n.title) head.append(h('strong', null, n.title))
|
|
354
|
+
if (head.childNodes.length) card.append(head)
|
|
355
|
+
const body = h('div', 'body')
|
|
356
|
+
arr(n.items).forEach((child) => {
|
|
357
|
+
const el = renderNode(child, ctx)
|
|
358
|
+
if (el) body.append(el)
|
|
359
|
+
})
|
|
360
|
+
card.append(body)
|
|
361
|
+
return card
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
R.chart = (n) => {
|
|
365
|
+
const kind = s(n.kind || 'bars').toLowerCase()
|
|
366
|
+
const data = arr(n.data).map((d) => ({ label: s(d && d.label), value: Number(d && d.value) || 0 }))
|
|
367
|
+
const wrap = h('div', 'prtsui-chart')
|
|
368
|
+
const H = Number(n.height) || 0
|
|
369
|
+
if (kind === 'donut') {
|
|
370
|
+
const total = data.reduce((a, b) => a + b.value, 0) || 1
|
|
371
|
+
const NS = 'http://www.w3.org/2000/svg'
|
|
372
|
+
const svg = document.createElementNS(NS, 'svg')
|
|
373
|
+
svg.setAttribute('viewBox', '0 0 120 120')
|
|
374
|
+
svg.setAttribute('height', String(H || 132))
|
|
375
|
+
const colors = ['var(--p-acc)', 'var(--p-ok)', 'var(--p-warn)', 'var(--p-err)', 'var(--p-info, #5ddcff)', 'color-mix(in srgb,var(--p-fg2) 48%,transparent)']
|
|
376
|
+
let offset = 0
|
|
377
|
+
const toRad = (deg) => (deg - 90) * Math.PI / 180
|
|
378
|
+
data.forEach((d, i) => {
|
|
379
|
+
const frac = d.value / total
|
|
380
|
+
const a0 = offset, a1 = offset + frac * Math.PI * 2
|
|
381
|
+
offset = a1
|
|
382
|
+
const r0 = 34, r1 = 50
|
|
383
|
+
const p = (r, a) => `${(60 + r * Math.cos(a)).toFixed(2)} ${(60 + r * Math.sin(a)).toFixed(2)}`
|
|
384
|
+
if (frac <= 0) return
|
|
385
|
+
const path = document.createElementNS(NS, 'path')
|
|
386
|
+
path.setAttribute('d', `M ${p(r1, toRad(((a0 * 180) / Math.PI)))} A ${r1} ${r1} 0 ${frac > 0.5 ? 1 : 0} 1 ${p(r1, toRad(((a1 * 180) / Math.PI)))} L ${p(r0, toRad(((a1 * 180) / Math.PI)))} A ${r0} ${r0} 0 ${frac > 0.5 ? 1 : 0} 0 ${p(r0, toRad(((a0 * 180) / Math.PI)))} Z`)
|
|
387
|
+
path.setAttribute('fill', colors[i % colors.length])
|
|
388
|
+
path.setAttribute('stroke', 'var(--p-bg)')
|
|
389
|
+
path.setAttribute('stroke-width', '1.5')
|
|
390
|
+
svg.append(path)
|
|
391
|
+
})
|
|
392
|
+
wrap.append(svg)
|
|
393
|
+
const legend = h('div', 'prtsui-legend')
|
|
394
|
+
data.forEach((d, i) => {
|
|
395
|
+
const item = h('span')
|
|
396
|
+
const chip = h('i')
|
|
397
|
+
chip.style.background = colors[i % colors.length]
|
|
398
|
+
item.append(chip, document.createTextNode(`${d.label} ${((d.value / total) * 100).toFixed(1)}%`))
|
|
399
|
+
legend.append(item)
|
|
400
|
+
})
|
|
401
|
+
wrap.append(legend)
|
|
402
|
+
return wrap
|
|
403
|
+
}
|
|
404
|
+
if (kind === 'line') {
|
|
405
|
+
const NS = 'http://www.w3.org/2000/svg'
|
|
406
|
+
const W = 320
|
|
407
|
+
const HH = H || 120
|
|
408
|
+
const max = Math.max(1, ...data.map((d) => d.value))
|
|
409
|
+
const step = data.length > 1 ? W / (data.length - 1) : W
|
|
410
|
+
const pts = data.map((d, i) => `${(i * step).toFixed(2)},${(HH - (d.value / max) * (HH - 12) - 4).toFixed(2)}`)
|
|
411
|
+
const svg = document.createElementNS(NS, 'svg')
|
|
412
|
+
svg.setAttribute('viewBox', `0 0 ${W} ${HH}`)
|
|
413
|
+
svg.setAttribute('height', String(HH))
|
|
414
|
+
const grid = document.createElementNS(NS, 'line')
|
|
415
|
+
grid.setAttribute('x1', '0'); grid.setAttribute('x2', String(W))
|
|
416
|
+
grid.setAttribute('y1', String(HH - 4)); grid.setAttribute('y2', String(HH - 4))
|
|
417
|
+
grid.setAttribute('stroke', 'var(--p-soft)'); grid.setAttribute('stroke-width', '1')
|
|
418
|
+
svg.append(grid)
|
|
419
|
+
const poly = document.createElementNS(NS, 'polyline')
|
|
420
|
+
poly.setAttribute('points', pts.join(' '))
|
|
421
|
+
poly.setAttribute('fill', 'none')
|
|
422
|
+
poly.setAttribute('stroke', 'var(--p-acc)')
|
|
423
|
+
poly.setAttribute('stroke-width', '1.5')
|
|
424
|
+
svg.append(poly)
|
|
425
|
+
data.forEach((d, i) => {
|
|
426
|
+
const c = document.createElementNS(NS, 'circle')
|
|
427
|
+
c.setAttribute('cx', String(i * step)); c.setAttribute('cy', String(HH - (d.value / max) * (HH - 12) - 4))
|
|
428
|
+
c.setAttribute('r', '2.2'); c.setAttribute('fill', 'var(--p-acc)')
|
|
429
|
+
svg.append(c)
|
|
430
|
+
})
|
|
431
|
+
wrap.append(svg)
|
|
432
|
+
const legend = h('div', 'prtsui-legend')
|
|
433
|
+
legend.append(document.createTextNode(`${data[0] ? data[0].label : ''} → ${data[data.length - 1] ? data[data.length - 1].label : ''}${n.unit ? ' · ' + n.unit : ''}`))
|
|
434
|
+
wrap.append(legend)
|
|
435
|
+
return wrap
|
|
436
|
+
}
|
|
437
|
+
const max = Math.max(1, ...data.map((d) => d.value))
|
|
438
|
+
const bars = h('div', 'prtsui-bars')
|
|
439
|
+
data.forEach((d) => {
|
|
440
|
+
const row = h('div', 'prtsui-bar')
|
|
441
|
+
row.append(h('span', null, d.label))
|
|
442
|
+
const track = h('div', 'track')
|
|
443
|
+
const fill = h('div', 'fill')
|
|
444
|
+
fill.style.width = `${Math.max(1.5, (d.value / max) * 100).toFixed(2)}%`
|
|
445
|
+
track.append(fill)
|
|
446
|
+
row.append(track)
|
|
447
|
+
row.append(h('em', null, `${d.value}${n.unit ? ' ' + n.unit : ''}`))
|
|
448
|
+
bars.append(row)
|
|
449
|
+
})
|
|
450
|
+
wrap.append(bars)
|
|
451
|
+
return wrap
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
R.code = (n) => {
|
|
455
|
+
const pre = h('pre', 'code', s(n.content ?? n.code))
|
|
456
|
+
return pre
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
R.divider = (n) => h('div', 'prtsui-divider', s(n.label || ''))
|
|
460
|
+
|
|
461
|
+
R.kbd = (n) => {
|
|
462
|
+
const wrap = h('span', 'prtsui-kbd')
|
|
463
|
+
arr(n.keys).forEach((k) => wrap.append(h('kbd', null, s(k))))
|
|
464
|
+
return wrap
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
R.badge = (n) => {
|
|
468
|
+
const b = h('span', 'prtsui-badge', s(n.content))
|
|
469
|
+
if (n.tone) b.dataset.tone = s(n.tone).toLowerCase()
|
|
470
|
+
return b
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
R.progress = (n) => {
|
|
474
|
+
const wrap = h('div', 'prtsui-progress')
|
|
475
|
+
const label = h('div', 'lbl')
|
|
476
|
+
label.append(h('span', null, s(n.label || '进度')))
|
|
477
|
+
const rawValue = Number(n.value)
|
|
478
|
+
const pct = Math.max(0, Math.min(100, rawValue <= 1 ? rawValue * 100 : rawValue))
|
|
479
|
+
label.append(h('b', null, `${pct.toFixed(1)}%`))
|
|
480
|
+
wrap.append(label)
|
|
481
|
+
const track = h('div', 'track')
|
|
482
|
+
const segs = arr(n.segments)
|
|
483
|
+
if (segs.length) {
|
|
484
|
+
segs.forEach((sg) => {
|
|
485
|
+
const seg = h('div', 'seg')
|
|
486
|
+
seg.dataset.tone = s(sg && sg.tone || 'brand').toLowerCase()
|
|
487
|
+
seg.style.width = `${Number(sg && sg.value) || 0}%`
|
|
488
|
+
track.append(seg)
|
|
489
|
+
})
|
|
490
|
+
} else {
|
|
491
|
+
const seg = h('div', 'seg')
|
|
492
|
+
seg.dataset.tone = s(n.tone || 'brand').toLowerCase()
|
|
493
|
+
seg.style.width = `${pct}%`
|
|
494
|
+
track.append(seg)
|
|
495
|
+
if (pct < 100) {
|
|
496
|
+
const rest = h('div', 'seg')
|
|
497
|
+
rest.dataset.tone = 'pending'
|
|
498
|
+
rest.style.width = `${100 - pct}%`
|
|
499
|
+
track.append(rest)
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
wrap.append(track)
|
|
503
|
+
return wrap
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
R.quote = (n) => {
|
|
507
|
+
const q = h('blockquote', 'prtsui-quote')
|
|
508
|
+
q.append(document.createTextNode(s(n.content)))
|
|
509
|
+
if (n.cite) q.append(h('cite', null, `— ${s(n.cite)}`))
|
|
510
|
+
return q
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
R.compare = (n) => {
|
|
514
|
+
const wrap = h('div', 'prtsui-compare')
|
|
515
|
+
for (const key of ['before', 'after']) {
|
|
516
|
+
const side = n[key]
|
|
517
|
+
if (!side) continue
|
|
518
|
+
const col = h('div')
|
|
519
|
+
col.append(h('b', null, s(side.title || (key === 'before' ? 'Before' : 'After'))))
|
|
520
|
+
arr(side.items).forEach((it) => {
|
|
521
|
+
const obj = it && typeof it === 'object' ? it : { text: it }
|
|
522
|
+
col.append(h('span', null, s(obj.text ?? obj.content ?? obj)))
|
|
523
|
+
})
|
|
524
|
+
wrap.append(col)
|
|
525
|
+
}
|
|
526
|
+
return wrap
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
R.image = (n) => {
|
|
530
|
+
const fig = h('figure')
|
|
531
|
+
const img = document.createElement('img')
|
|
532
|
+
img.src = s(n.src)
|
|
533
|
+
img.alt = s(n.alt || '')
|
|
534
|
+
img.loading = 'lazy'
|
|
535
|
+
fig.append(img)
|
|
536
|
+
if (n.caption) fig.append(h('figcaption', null, n.caption))
|
|
537
|
+
return fig
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
R.accordion = (n, ctx) => {
|
|
541
|
+
const wrap = h('div', 'prtsui-acc')
|
|
542
|
+
arr(n.items).forEach((it) => {
|
|
543
|
+
const details = document.createElement('details')
|
|
544
|
+
const summary = document.createElement('summary')
|
|
545
|
+
summary.textContent = s(it && it.title)
|
|
546
|
+
details.append(summary)
|
|
547
|
+
const inner = h('div', 'inner')
|
|
548
|
+
arr(it && it.items).forEach((child) => {
|
|
549
|
+
const el = renderNode(child, ctx)
|
|
550
|
+
if (el) inner.append(el)
|
|
551
|
+
})
|
|
552
|
+
details.append(inner)
|
|
553
|
+
wrap.append(details)
|
|
554
|
+
})
|
|
555
|
+
return wrap
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function renderNode(node, ctx) {
|
|
559
|
+
if (node === null || node === undefined) return null
|
|
560
|
+
if (typeof node === 'string') return h('p', null, node)
|
|
561
|
+
if (typeof node !== 'object') return h('p', null, s(node))
|
|
562
|
+
const type = s(node.type)
|
|
563
|
+
const fn = R[type]
|
|
564
|
+
if (!fn) return null
|
|
565
|
+
try {
|
|
566
|
+
return fn(node, ctx)
|
|
567
|
+
} catch (err) {
|
|
568
|
+
console.warn('[prtsui] node render failed', type, err)
|
|
569
|
+
return null
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function parseSpec(raw) {
|
|
574
|
+
const text = String(raw || '').trim()
|
|
575
|
+
if (!text || (text[0] !== '{' && text[0] !== '[')) return null
|
|
576
|
+
let data
|
|
577
|
+
try { data = JSON.parse(text) } catch { return null }
|
|
578
|
+
let items
|
|
579
|
+
if (Array.isArray(data)) items = data
|
|
580
|
+
else if (data && typeof data === 'object' && Array.isArray(data.items)) items = data.items
|
|
581
|
+
else return null
|
|
582
|
+
const known = items.filter((it) => it && typeof it === 'object' && TYPES.has(s(it.type)))
|
|
583
|
+
if (known.length === 0) return null
|
|
584
|
+
if (known.length < items.length && items.length > 1) {
|
|
585
|
+
// a stray non-PRTS key means this is probably ordinary JSON, not a spec
|
|
586
|
+
const unknownRatio = (items.length - known.length) / items.length
|
|
587
|
+
if (unknownRatio > 0.5) return null
|
|
588
|
+
}
|
|
589
|
+
return { root: data, spec: { title: data && data.title ? s(data.title) : '', gap: data && Number(data.gap) || 0, items } }
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function build(spec, ctx) {
|
|
593
|
+
const root = h('div', 'prtsui')
|
|
594
|
+
if (spec.gap) root.style.setProperty('--prtsui-gap', `${spec.gap}px`)
|
|
595
|
+
if (spec.title) {
|
|
596
|
+
const banner = h('div', 'prtsui-banner')
|
|
597
|
+
banner.append(h('span', null, spec.title))
|
|
598
|
+
banner.append(h('i', null, 'PRTS'))
|
|
599
|
+
root.append(banner)
|
|
600
|
+
}
|
|
601
|
+
const body = h('div', 'prtsui-body')
|
|
602
|
+
spec.items.forEach((node) => {
|
|
603
|
+
const el = renderNode(node, ctx)
|
|
604
|
+
if (el) body.append(el)
|
|
605
|
+
})
|
|
606
|
+
root.append(body)
|
|
607
|
+
return root
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// ---------------- mounting ----------------
|
|
611
|
+
function textOf(pre) {
|
|
612
|
+
const code = pre.querySelector('code')
|
|
613
|
+
return code ? code.textContent : pre.textContent
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function isHidden(pre) {
|
|
617
|
+
return pre.dataset.prtsuiMounted === '1'
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// The chat shell wraps every fenced block in a card that also paints the
|
|
621
|
+
// "code block / copy" toolbar. Mounting before <pre> would leave that
|
|
622
|
+
// chrome visible above the PRTS card, so walk up and hide the whole wrap.
|
|
623
|
+
function blockWrap(pre) {
|
|
624
|
+
let el = pre
|
|
625
|
+
for (let depth = 0; depth < 4; depth += 1) {
|
|
626
|
+
const parent = el.parentElement
|
|
627
|
+
if (!parent || parent.classList.contains('prtsui-host')) break
|
|
628
|
+
if (parent.querySelectorAll('pre').length > 1) break
|
|
629
|
+
if (parent.querySelectorAll('.prtsui-host').length > 0) break
|
|
630
|
+
if (parent.childElementCount > 8) break
|
|
631
|
+
if (/代码块|Code block|Copy code|复制/.test(String(parent.textContent || ''))) return parent
|
|
632
|
+
el = parent
|
|
633
|
+
}
|
|
634
|
+
return pre
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function hideWrap(wrap) {
|
|
638
|
+
if (!wrap) return
|
|
639
|
+
wrap.dataset.prtsuiWrap = '1'
|
|
640
|
+
wrap.style.display = 'none'
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function showWrap(wrap) {
|
|
644
|
+
if (!wrap || wrap.dataset.prtsuiWrap !== '1') return
|
|
645
|
+
wrap.style.display = ''
|
|
646
|
+
delete wrap.dataset.prtsuiWrap
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// Mounts made by an older build left the code-block card visible with the
|
|
650
|
+
// host nested inside it; hoist the host out and hide the leftover card.
|
|
651
|
+
function repairExisting() {
|
|
652
|
+
for (const host of document.querySelectorAll('.prtsui-host')) {
|
|
653
|
+
const wrap = host.parentElement
|
|
654
|
+
if (!wrap || wrap.classList.contains('prtsui-host')) continue
|
|
655
|
+
if (wrap.dataset.prtsuiWrap === '1') continue
|
|
656
|
+
if (!wrap.querySelector('pre')) continue
|
|
657
|
+
if (!/代码块|Code block|Copy code|复制/.test(String(wrap.textContent || ''))) continue
|
|
658
|
+
if (!wrap.parentNode) continue
|
|
659
|
+
wrap.parentNode.insertBefore(host, wrap)
|
|
660
|
+
hideWrap(wrap)
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function apply(ctx) {
|
|
665
|
+
const style = document.createElement('style')
|
|
666
|
+
style.dataset.plugin = '@yunkersek/dsh-prtsui'
|
|
667
|
+
style.textContent = CSS
|
|
668
|
+
document.head.append(style)
|
|
669
|
+
repairExisting()
|
|
670
|
+
|
|
671
|
+
const state = new Map() // pre -> { host, root, sig }
|
|
672
|
+
let sweeping = false
|
|
673
|
+
let timer = null
|
|
674
|
+
|
|
675
|
+
function mount(pre, parsed) {
|
|
676
|
+
const entry = state.get(pre)
|
|
677
|
+
const sig = JSON.stringify(parsed.spec)
|
|
678
|
+
if (entry && entry.sig === sig) return
|
|
679
|
+
const host = entry ? entry.host : h('div', 'prtsui-host')
|
|
680
|
+
host.replaceChildren(build(parsed.spec, ctx))
|
|
681
|
+
if (!entry) {
|
|
682
|
+
const wrap = blockWrap(pre)
|
|
683
|
+
if (wrap.parentNode) wrap.parentNode.insertBefore(host, wrap)
|
|
684
|
+
pre.dataset.prtsuiMounted = '1'
|
|
685
|
+
hideWrap(wrap)
|
|
686
|
+
state.set(pre, { host, sig, wrap })
|
|
687
|
+
return
|
|
688
|
+
}
|
|
689
|
+
state.set(pre, { host, sig, wrap: entry.wrap })
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function sweep() {
|
|
693
|
+
if (sweeping) return
|
|
694
|
+
sweeping = true
|
|
695
|
+
try {
|
|
696
|
+
for (const pre of document.querySelectorAll('pre')) {
|
|
697
|
+
if (pre.closest('.prtsui-host')) continue
|
|
698
|
+
if (pre.dataset.prtsuiSkip === '1') continue
|
|
699
|
+
const raw = textOf(pre)
|
|
700
|
+
const parsed = parseSpec(raw)
|
|
701
|
+
if (!parsed) {
|
|
702
|
+
const entry = state.get(pre)
|
|
703
|
+
if (entry && !String(raw).includes('"items"')) {
|
|
704
|
+
// the fence was edited away: restore the plain code block
|
|
705
|
+
entry.host.remove()
|
|
706
|
+
showWrap(entry.wrap)
|
|
707
|
+
delete pre.dataset.prtsuiMounted
|
|
708
|
+
state.delete(pre)
|
|
709
|
+
}
|
|
710
|
+
continue
|
|
711
|
+
}
|
|
712
|
+
mount(pre, parsed)
|
|
713
|
+
}
|
|
714
|
+
} finally {
|
|
715
|
+
sweeping = false
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function schedule() {
|
|
720
|
+
if (timer) return
|
|
721
|
+
timer = window.setTimeout(() => { timer = null; sweep() }, 80)
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
const observer = new MutationObserver(() => schedule())
|
|
725
|
+
observer.observe(document.body, { childList: true, subtree: true, characterData: true })
|
|
726
|
+
sweep()
|
|
727
|
+
const interval = window.setInterval(sweep, 2000)
|
|
728
|
+
|
|
729
|
+
return () => {
|
|
730
|
+
observer.disconnect()
|
|
731
|
+
window.clearInterval(interval)
|
|
732
|
+
if (timer) window.clearTimeout(timer)
|
|
733
|
+
for (const [pre, entry] of state.entries()) {
|
|
734
|
+
entry.host.remove()
|
|
735
|
+
showWrap(entry.wrap)
|
|
736
|
+
delete pre.dataset.prtsuiMounted
|
|
737
|
+
}
|
|
738
|
+
state.clear()
|
|
739
|
+
style.remove()
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
return {
|
|
744
|
+
inject: ['sessions'],
|
|
745
|
+
apply(ctx) {
|
|
746
|
+
return ctx.effect(() => apply(ctx), '@yunkersek/dsh-prtsui: fence renderer')
|
|
747
|
+
},
|
|
748
|
+
}
|
|
749
|
+
},
|
|
750
|
+
})
|