dsh-hero-mascot 0.0.0-stage → 2.2.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/LICENSE +21 -0
- package/README.md +95 -2
- package/assets/hero.png +0 -0
- package/assets/screenshot.jpg +0 -0
- package/client.js +551 -0
- package/cordis.patch.yml +4 -0
- package/index.js +53 -0
- package/package.json +60 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cyrene-xl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,96 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-hero-mascot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
新会话 Hero 区左侧的吉祥物,外加一个调节面板。
|
|
6
|
+
|
|
7
|
+
- **图片可换**:默认用 `assets/hero.png`,也可以在设置里**上传自己的图**
|
|
8
|
+
- **可选抠白底**:上传时从画布四边泛洪掉与边框连通的纯白背景
|
|
9
|
+
- **可调**:在 **设置 → 通用** 里实时调节**大小 / 水平位置 / 垂直位置**
|
|
10
|
+
- **可锁定**:同一行有「锁定位置与大小」开关,锁上后三根滑杆不可拖动(防误触),状态存 local
|
|
11
|
+
- 改动即时生效并写入 `localStorage`,刷新与重开都在
|
|
12
|
+
|
|
13
|
+
## 图片处理
|
|
14
|
+
|
|
15
|
+
上传的图在浏览器端做三步,产出 PNG data URL 存进 `localStorage`:
|
|
16
|
+
|
|
17
|
+
1. **降采样到长边 384**(显示尺寸最大 160px × dpr 1.875 ≈ 300px,384 有富余);
|
|
18
|
+
2. **可选抠白底**:从画布四边泛洪填充,只吃掉与边框连通的近白像素
|
|
19
|
+
(亮度 ≥ 240 且饱和度 ≤ 12)。**角色内部被描边围住的白色不会被打洞**;
|
|
20
|
+
3. 对紧邻背景的一圈像素做 **3×3 alpha 均值**羽化 —— 1px 宽的软化带,
|
|
21
|
+
把描边锯齿抹平。这会让 1px 细线的最外圈降到约 170/255(刻意软化,不是丢失)。
|
|
22
|
+
|
|
23
|
+
编码后若超过 `MAX_IMAGE_CHARS`(1.2M 字符)就继续降采样,四次仍不行则提示失败。
|
|
24
|
+
`getImageData` / `toDataURL` 失败(极端图片、内存不足)也走同一条失败提示。
|
|
25
|
+
|
|
26
|
+
抠白底开关**在选择图片时生效**;要改就重新选一次 —— 这样只需要存处理后的结果,
|
|
27
|
+
不必在 `localStorage` 里同时留一份原图。
|
|
28
|
+
|
|
29
|
+
## 结构
|
|
30
|
+
|
|
31
|
+
| 文件 | 作用 |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `index.js` | 主机半边:把 `assets/hero.png` 通过 webServer 暴露成 `/dsh-hero-mascot/hero.png` |
|
|
34
|
+
| `client.js` | 浏览器半边:占 `conversation.hero.brand.mark` 槽位 + 注册 `settings.general.item` 设置行 |
|
|
35
|
+
| `assets/hero.png` | 吉祥物图片(512×512 RGBA,已抠白底) |
|
|
36
|
+
|
|
37
|
+
**为什么不把图片 base64 内联进 `client.js`**:图有 ~300 KB,内联会让浏览器半边变成一个
|
|
38
|
+
巨大且改不动的字符串。走路由后源码保持可读,换图只要替换 `assets/hero.png` 再重启。
|
|
39
|
+
|
|
40
|
+
## 图片是怎么处理的
|
|
41
|
+
|
|
42
|
+
原图是 1440×1440 白底 JPEG。处理步骤(Node + `jpeg-js` / `pngjs` 纯 JS 实现):
|
|
43
|
+
|
|
44
|
+
1. **从画布四边泛洪填充**,只抠掉「与边框连通的近白像素」(亮度 ≥ 240 且饱和度 ≤ 12)。
|
|
45
|
+
这一点很关键:角色衬衫、裙摆的白色被深色描边围住、不与边框连通,因此**完整保留**,
|
|
46
|
+
不会被打出空洞;`你好` 字样与叶片描边同样保留。
|
|
47
|
+
2. **边界羽化**:只对紧邻背景的像素做一次 3×3 alpha 均值,软化描边处的锯齿,避免浅色白边。
|
|
48
|
+
3. **预乘 alpha 的 box 降采样**到 512×512 —— 直接用普通平均会让半透明边缘混入白色,
|
|
49
|
+
产生暗边/白边。
|
|
50
|
+
|
|
51
|
+
要换图:把自己的图替换掉 `assets/hero.png`(同样建议先抠成透明底),重启 Web 即可。
|
|
52
|
+
|
|
53
|
+
## 设置行
|
|
54
|
+
|
|
55
|
+
`settings.general.item` 是 `@deepseek-ai/dsh-client-ui-settings-general` 声明的 **list** 槽位,
|
|
56
|
+
用 `id: 'hero-mascot'` 注册即可在「设置 → 通用」里追加一行。
|
|
57
|
+
|
|
58
|
+
行样式照官方 `EnterBehaviorRow` 的 CSS 原值复刻(那套是另一个 CSS 模块的 hash 类名,
|
|
59
|
+
无法跨模块复用):
|
|
60
|
+
|
|
61
|
+
```css
|
|
62
|
+
row { border-bottom: .5px solid var(--dsw-alias-border-l2); padding: 16px 0 }
|
|
63
|
+
title { color: var(--dsw-alias-label-primary); font-size: 14px; line-height: 22px }
|
|
64
|
+
desc { color: var(--dsw-alias-label-tertiary); font-size: 12px; line-height: 18px }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
文案走本插件自己的 locale 命名空间 `dsh-hero-mascot`(中/英),
|
|
68
|
+
**不能**借用 `conversation` —— `locale.register` 对同 namespace + 同语言会直接抛错。
|
|
69
|
+
|
|
70
|
+
取值范围:大小 24–160 px,水平/垂直偏移 −80–80 px;超出范围的值在读盘时会被夹回。
|
|
71
|
+
|
|
72
|
+
## 安装 / 卸载
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
python3 /root/.dsh/plugin-manager.py import <本目录的 zip>
|
|
76
|
+
|
|
77
|
+
# 卸载
|
|
78
|
+
dsh plugin --profile web remove dsh-hero-mascot
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
装好后**重启 Web** 生效。
|
|
82
|
+
|
|
83
|
+
## 注意
|
|
84
|
+
|
|
85
|
+
- 依赖官方槽位契约 `conversation.hero.brand.mark` 与 `settings.general.item`,
|
|
86
|
+
以及 `--dsw-alias-*` 设计令牌;DSH 前端大版本升级若改了这些,需要同步更新。
|
|
87
|
+
- 主机路由注册失败时吉祥物不显示,但设置行仍在(设置行不依赖路由)。
|
|
88
|
+
- 手机上 `.fish` 的悬停游动动画不会触发(`(hover: hover)` 不成立),是静态的。
|
|
89
|
+
|
|
90
|
+
## 兼容性与相关插件
|
|
91
|
+
|
|
92
|
+
- 需要 DSH **0.1.5 ~ 0.2.1**、Node **22+**;
|
|
93
|
+
- 图片与尺寸存浏览器 `localStorage`,按 origin(端口)隔离,换端口/换包名会丢;
|
|
94
|
+
- 默认吉祥物图为非商用同人素材,角色版权归 HoYoverse / 米哈游,详见
|
|
95
|
+
[THIRD_PARTY_NOTICES](../../THIRD_PARTY_NOTICES.md);可上传自己的图片替换;
|
|
96
|
+
- 其它界面类插件见 [根 README 的「关联开源插件」](../../README.md#关联开源插件)。
|
package/assets/hero.png
ADDED
|
Binary file
|
|
Binary file
|
package/client.js
ADDED
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsha-hero-mascot 的浏览器半边。
|
|
3
|
+
*
|
|
4
|
+
* 两件事:
|
|
5
|
+
* 1. 占据新会话 Hero 区左侧的品牌图标槽位 conversation.hero.brand.mark,
|
|
6
|
+
* 显示吉祥物图片。默认用主机半边的 /dsha-hero-mascot/hero.png,
|
|
7
|
+
* 也可以在设置里**上传自己的图**。
|
|
8
|
+
* 2. 在「设置 → 通用」注册一行 settings.general.item,调节:
|
|
9
|
+
* 图片(上传 / 清除)、抠白底、大小、水平位置、垂直位置
|
|
10
|
+
* 改动即时生效并写入 localStorage。
|
|
11
|
+
*
|
|
12
|
+
* 抠白底:上传的图若带纯白背景,直接贴上去在壁纸底上就是一块白方块。
|
|
13
|
+
* 这里用「从画布四边泛洪填充」只吃掉与边框连通的近白像素(亮度 ≥ 240 且
|
|
14
|
+
* 饱和度 ≤ 12),角色内部被描边围住的白色(衬衫、裙摆)不会被打洞;
|
|
15
|
+
* 之后对紧邻背景的一圈像素做 1 次 3×3 alpha 均值,软化锯齿、避免白边。
|
|
16
|
+
*
|
|
17
|
+
* 存储:处理后的图是 PNG data URL,直接进 localStorage。上传时先降采样到
|
|
18
|
+
* 长边 384(160px 显示尺寸 × dpr 1.875 ≈ 300px,384 有富余),若编码后仍
|
|
19
|
+
* 超过配额上限就继续降。抠白底开关在**选择图片时**生效;要改就重新选一次。
|
|
20
|
+
*/
|
|
21
|
+
window.__ModuleLoader__.load({
|
|
22
|
+
id: 'dsh-hero-mascot',
|
|
23
|
+
factory: (require) => {
|
|
24
|
+
var module = { exports: {} };
|
|
25
|
+
var exports = module.exports;
|
|
26
|
+
var react = require('react');
|
|
27
|
+
|
|
28
|
+
/** 本插件自己的 locale 命名空间(不能借用 conversation,同 ns+同语言会抛错)。 */
|
|
29
|
+
var NS = 'dsha-hero-mascot';
|
|
30
|
+
/** 主机半边提供的默认图片地址。 */
|
|
31
|
+
var DEFAULT_IMAGE = '/dsh-hero-mascot/hero.png';
|
|
32
|
+
/** 设置落盘位置(固定 origin,刷新与重开都在)。 */
|
|
33
|
+
var STORAGE_KEY = 'dsha-hero-mascot:view';
|
|
34
|
+
|
|
35
|
+
/** 默认值与取值范围(px)。 */
|
|
36
|
+
var DEFAULTS = { size: 56, offsetX: 0, offsetY: 0, image: '' };
|
|
37
|
+
var LIMITS = {
|
|
38
|
+
size: [24, 160],
|
|
39
|
+
offsetX: [-80, 80],
|
|
40
|
+
offsetY: [-80, 80]
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** 上传图片的降采样长边。 */
|
|
44
|
+
var UPLOAD_MAX_SIDE = 384;
|
|
45
|
+
/** data URL 字符数上限(localStorage 配额留余量)。 */
|
|
46
|
+
var MAX_IMAGE_CHARS = 1200000;
|
|
47
|
+
|
|
48
|
+
//#region 设置状态:localStorage + 订阅
|
|
49
|
+
|
|
50
|
+
function clamp(value, lo, hi) {
|
|
51
|
+
return Math.min(hi, Math.max(lo, value));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function readState() {
|
|
55
|
+
var raw = {};
|
|
56
|
+
try {
|
|
57
|
+
raw = JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}') || {};
|
|
58
|
+
} catch (err) {
|
|
59
|
+
raw = {};
|
|
60
|
+
}
|
|
61
|
+
var out = {};
|
|
62
|
+
out.size = numberOr(raw.size, DEFAULTS.size, LIMITS.size);
|
|
63
|
+
out.offsetX = numberOr(raw.offsetX, DEFAULTS.offsetX, LIMITS.offsetX);
|
|
64
|
+
out.offsetY = numberOr(raw.offsetY, DEFAULTS.offsetY, LIMITS.offsetY);
|
|
65
|
+
out.image = typeof raw.image === 'string' && raw.image.indexOf('data:image/') === 0 ? raw.image : '';
|
|
66
|
+
out.cutout = raw.cutout !== false;
|
|
67
|
+
/* 锁定模式:防止误触把大小 / 位置拖歪(默认关)。 */
|
|
68
|
+
out.locked = raw.locked === true;
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function numberOr(value, fallback, range) {
|
|
73
|
+
var number = Number(value);
|
|
74
|
+
if (!Number.isFinite(number)) return fallback;
|
|
75
|
+
return clamp(number, range[0], range[1]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
var state = readState();
|
|
79
|
+
var listeners = new Set();
|
|
80
|
+
|
|
81
|
+
function subscribe(fn) {
|
|
82
|
+
listeners.add(fn);
|
|
83
|
+
return function () { listeners.delete(fn); };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function update(patch) {
|
|
87
|
+
state = Object.assign({}, state, patch);
|
|
88
|
+
try {
|
|
89
|
+
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
90
|
+
} catch (err) {
|
|
91
|
+
/* 配额满 / 隐私模式:内存里仍生效,只是不落盘。 */
|
|
92
|
+
}
|
|
93
|
+
listeners.forEach(function (fn) { fn(state); });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 订阅当前设置;吉祥物与设置行共用同一个状态源。 */
|
|
97
|
+
function useView() {
|
|
98
|
+
var pair = react.useState(state);
|
|
99
|
+
var value = pair[0];
|
|
100
|
+
var setValue = pair[1];
|
|
101
|
+
react.useEffect(function () { return subscribe(setValue); }, []);
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
|
|
107
|
+
//#region 抠白底(从边框泛洪填充)
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 只吃掉「与画布边框连通的近白像素」,然后羽化边界。
|
|
111
|
+
* @param ctx - 已经画好图、尺寸 w×h 的 2D 上下文(原地修改)。
|
|
112
|
+
* @param w - 宽。
|
|
113
|
+
* @param h - 高。
|
|
114
|
+
*/
|
|
115
|
+
function removeBorderWhite(ctx, w, h) {
|
|
116
|
+
var frame = ctx.getImageData(0, 0, w, h);
|
|
117
|
+
var data = frame.data;
|
|
118
|
+
var count = w * h;
|
|
119
|
+
var background = new Uint8Array(count);
|
|
120
|
+
var stack = new Int32Array(count);
|
|
121
|
+
var top = 0;
|
|
122
|
+
|
|
123
|
+
function isNearWhite(index) {
|
|
124
|
+
var p = index * 4;
|
|
125
|
+
var r = data[p];
|
|
126
|
+
var g = data[p + 1];
|
|
127
|
+
var b = data[p + 2];
|
|
128
|
+
var lum = 0.299 * r + 0.587 * g + 0.114 * b;
|
|
129
|
+
var sat = Math.max(r, g, b) - Math.min(r, g, b);
|
|
130
|
+
return lum >= 240 && sat <= 12;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function seed(index) {
|
|
134
|
+
if (!background[index] && isNearWhite(index)) {
|
|
135
|
+
background[index] = 1;
|
|
136
|
+
stack[top++] = index;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (var x = 0; x < w; x++) {
|
|
141
|
+
seed(x);
|
|
142
|
+
seed((h - 1) * w + x);
|
|
143
|
+
}
|
|
144
|
+
for (var y = 0; y < h; y++) {
|
|
145
|
+
seed(y * w);
|
|
146
|
+
seed(y * w + (w - 1));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
while (top > 0) {
|
|
150
|
+
var index = stack[--top];
|
|
151
|
+
var px = index % w;
|
|
152
|
+
var py = (index - px) / w;
|
|
153
|
+
if (px > 0) seed(index - 1);
|
|
154
|
+
if (px < w - 1) seed(index + 1);
|
|
155
|
+
if (py > 0) seed(index - w);
|
|
156
|
+
if (py < h - 1) seed(index + w);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* 先把 alpha 写成 0/255,并留一份快照给边界均值用。 */
|
|
160
|
+
var alpha = new Uint8Array(count);
|
|
161
|
+
for (var i = 0; i < count; i++) {
|
|
162
|
+
alpha[i] = background[i] ? 0 : 255;
|
|
163
|
+
data[i * 4 + 3] = alpha[i];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/* 只对紧邻背景的一圈做 3×3 均值:软化锯齿,又不整体糊掉。 */
|
|
167
|
+
for (var yy = 1; yy < h - 1; yy++) {
|
|
168
|
+
for (var xx = 1; xx < w - 1; xx++) {
|
|
169
|
+
var at = yy * w + xx;
|
|
170
|
+
if (!background[at]
|
|
171
|
+
&& !background[at - 1] && !background[at + 1]
|
|
172
|
+
&& !background[at - w] && !background[at + w]) continue;
|
|
173
|
+
var sum = 0;
|
|
174
|
+
for (var dy = -1; dy <= 1; dy++) {
|
|
175
|
+
for (var dx = -1; dx <= 1; dx++) sum += alpha[at + dy * w + dx];
|
|
176
|
+
}
|
|
177
|
+
data[at * 4 + 3] = Math.round(sum / 9);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/* 背景像素强制全透明(上一步的均值可能给了它们非零 alpha)。 */
|
|
182
|
+
for (var k = 0; k < count; k++) if (background[k]) data[k * 4 + 3] = 0;
|
|
183
|
+
|
|
184
|
+
ctx.putImageData(frame, 0, 0);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 把已解码的图按长边降采样画到 canvas 上,返回 canvas。 */
|
|
188
|
+
function drawScaled(image, maxSide) {
|
|
189
|
+
var scale = Math.min(1, maxSide / Math.max(image.width, image.height));
|
|
190
|
+
var width = Math.max(1, Math.round(image.width * scale));
|
|
191
|
+
var height = Math.max(1, Math.round(image.height * scale));
|
|
192
|
+
var canvas = document.createElement('canvas');
|
|
193
|
+
canvas.width = width;
|
|
194
|
+
canvas.height = height;
|
|
195
|
+
var ctx = canvas.getContext('2d');
|
|
196
|
+
ctx.drawImage(image, 0, 0, width, height);
|
|
197
|
+
return { canvas: canvas, ctx: ctx, width: width, height: height };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 读一个用户选的文件 → 可选抠白底 → 降采样 → PNG data URL。
|
|
202
|
+
* 超过配额就继续降采样,仍不行则回调 null。
|
|
203
|
+
* @param file - 用户选的文件。
|
|
204
|
+
* @param cutout - 是否抠白底。
|
|
205
|
+
* @param onDone - 回调,参数为 data URL 或 null。
|
|
206
|
+
*/
|
|
207
|
+
function readImageFile(file, cutout, onDone) {
|
|
208
|
+
var reader = new FileReader();
|
|
209
|
+
reader.onerror = function () { onDone(null); };
|
|
210
|
+
reader.onload = function () {
|
|
211
|
+
var image = new Image();
|
|
212
|
+
image.onerror = function () { onDone(null); };
|
|
213
|
+
image.onload = function () {
|
|
214
|
+
var side = UPLOAD_MAX_SIDE;
|
|
215
|
+
try {
|
|
216
|
+
for (var attempt = 0; attempt < 4; attempt++) {
|
|
217
|
+
var drawn = drawScaled(image, side);
|
|
218
|
+
if (cutout) removeBorderWhite(drawn.ctx, drawn.width, drawn.height);
|
|
219
|
+
var url = drawn.canvas.toDataURL('image/png');
|
|
220
|
+
if (url.length <= MAX_IMAGE_CHARS) { onDone(url); return; }
|
|
221
|
+
side = Math.round(side * 0.75);
|
|
222
|
+
}
|
|
223
|
+
onDone(null);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
/* getImageData / toDataURL 失败(极端图片或内存不足) */
|
|
226
|
+
console.warn('[dsha-hero-mascot] 图片处理失败:', err && err.message);
|
|
227
|
+
onDone(null);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
image.src = reader.result;
|
|
231
|
+
};
|
|
232
|
+
reader.readAsDataURL(file);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
//#endregion
|
|
236
|
+
|
|
237
|
+
//#region 吉祥物
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Hero 品牌图标:自定义吉祥物。
|
|
241
|
+
* @param props - 槽位传入的 `className`(Hero 的 .fish,带上就保留官方那套
|
|
242
|
+
* 悬停游动动画;手机上 `(hover:hover)` 不成立,所以天然是静态的)。
|
|
243
|
+
*/
|
|
244
|
+
function HeroMascot(props) {
|
|
245
|
+
var view = useView();
|
|
246
|
+
var passed = props && typeof props.className === 'string' ? props.className : '';
|
|
247
|
+
return react.createElement(
|
|
248
|
+
'span',
|
|
249
|
+
{
|
|
250
|
+
className: (passed ? passed + ' ' : '') + 'dsha-hero-mascot',
|
|
251
|
+
'data-dsha-hero-mascot': '',
|
|
252
|
+
style: {
|
|
253
|
+
display: 'inline-flex',
|
|
254
|
+
alignItems: 'center',
|
|
255
|
+
justifyContent: 'center',
|
|
256
|
+
flex: 'none',
|
|
257
|
+
position: 'relative',
|
|
258
|
+
width: view.size + 'px',
|
|
259
|
+
height: view.size + 'px',
|
|
260
|
+
left: view.offsetX + 'px',
|
|
261
|
+
top: view.offsetY + 'px'
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
react.createElement('img', {
|
|
265
|
+
src: view.image || DEFAULT_IMAGE,
|
|
266
|
+
alt: '',
|
|
267
|
+
draggable: false,
|
|
268
|
+
decoding: 'async',
|
|
269
|
+
style: { display: 'block', width: '100%', height: '100%', objectFit: 'contain' }
|
|
270
|
+
})
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
//#endregion
|
|
275
|
+
|
|
276
|
+
//#region 设置行
|
|
277
|
+
|
|
278
|
+
/* 样式值照官方 EnterBehaviorRow / modalInput 的 CSS 复刻,保证观感一致。 */
|
|
279
|
+
var rowStyle = {
|
|
280
|
+
borderBottom: '0.5px solid var(--dsw-alias-border-l2)',
|
|
281
|
+
padding: '16px 0',
|
|
282
|
+
display: 'flex',
|
|
283
|
+
flexDirection: 'column',
|
|
284
|
+
gap: '10px',
|
|
285
|
+
minWidth: 0
|
|
286
|
+
};
|
|
287
|
+
var rowTextStyle = { display: 'flex', flexDirection: 'column', gap: '4px', minWidth: 0 };
|
|
288
|
+
var titleStyle = {
|
|
289
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
290
|
+
fontSize: '14px',
|
|
291
|
+
fontWeight: 400,
|
|
292
|
+
lineHeight: '22px'
|
|
293
|
+
};
|
|
294
|
+
var descStyle = {
|
|
295
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
296
|
+
fontSize: '12px',
|
|
297
|
+
fontWeight: 400,
|
|
298
|
+
lineHeight: '18px'
|
|
299
|
+
};
|
|
300
|
+
var controlRowStyle = { display: 'flex', alignItems: 'center', gap: '8px', minWidth: 0, flexWrap: 'wrap' };
|
|
301
|
+
var sliderRowStyle = { display: 'flex', alignItems: 'center', gap: '12px', minWidth: 0 };
|
|
302
|
+
var sliderLabelStyle = {
|
|
303
|
+
flex: 'none',
|
|
304
|
+
width: '40px',
|
|
305
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
306
|
+
fontSize: '12px',
|
|
307
|
+
lineHeight: '18px'
|
|
308
|
+
};
|
|
309
|
+
var sliderStyle = {
|
|
310
|
+
flex: '1 1 auto',
|
|
311
|
+
minWidth: 0,
|
|
312
|
+
accentColor: 'var(--dsw-alias-state-business-primary)'
|
|
313
|
+
};
|
|
314
|
+
var sliderValueStyle = {
|
|
315
|
+
flex: 'none',
|
|
316
|
+
width: '52px',
|
|
317
|
+
textAlign: 'right',
|
|
318
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
319
|
+
fontSize: '12px',
|
|
320
|
+
lineHeight: '18px',
|
|
321
|
+
fontVariantNumeric: 'tabular-nums'
|
|
322
|
+
};
|
|
323
|
+
var pillStyle = {
|
|
324
|
+
flex: 'none',
|
|
325
|
+
background: 'var(--dsw-alias-bg-module-platform)',
|
|
326
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
327
|
+
font: 'inherit',
|
|
328
|
+
fontSize: '13px',
|
|
329
|
+
lineHeight: '20px',
|
|
330
|
+
cursor: 'pointer',
|
|
331
|
+
border: 'none',
|
|
332
|
+
borderRadius: '14px',
|
|
333
|
+
padding: '5px 14px',
|
|
334
|
+
whiteSpace: 'nowrap'
|
|
335
|
+
};
|
|
336
|
+
var chipStyle = {
|
|
337
|
+
flex: 'none',
|
|
338
|
+
font: 'inherit',
|
|
339
|
+
fontSize: '12px',
|
|
340
|
+
lineHeight: '18px',
|
|
341
|
+
cursor: 'pointer',
|
|
342
|
+
border: '0.5px solid var(--dsw-alias-border-l4)',
|
|
343
|
+
borderRadius: '12px',
|
|
344
|
+
padding: '3px 10px',
|
|
345
|
+
background: 'transparent',
|
|
346
|
+
color: 'var(--dsw-alias-label-tertiary)'
|
|
347
|
+
};
|
|
348
|
+
var chipOnStyle = Object.assign({}, chipStyle, {
|
|
349
|
+
background: 'var(--dsw-alias-state-business-primary)',
|
|
350
|
+
borderColor: 'transparent',
|
|
351
|
+
color: '#fff'
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
/** 打开系统文件选择器。用临时 input,省得在 React 树里维护 ref。 */
|
|
355
|
+
function pickImageFile(onPick) {
|
|
356
|
+
var input = document.createElement('input');
|
|
357
|
+
input.type = 'file';
|
|
358
|
+
input.accept = 'image/*';
|
|
359
|
+
input.style.display = 'none';
|
|
360
|
+
input.addEventListener('change', function () {
|
|
361
|
+
var file = input.files && input.files[0];
|
|
362
|
+
if (file) onPick(file);
|
|
363
|
+
if (input.parentNode) input.parentNode.removeChild(input);
|
|
364
|
+
});
|
|
365
|
+
document.body.appendChild(input);
|
|
366
|
+
input.click();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 设置行:吉祥物图片 + 大小 + 位置。
|
|
371
|
+
* @param props - 槽位按 `locale: NS` 注入的 `t`。
|
|
372
|
+
*/
|
|
373
|
+
function MascotSettingsRow(props) {
|
|
374
|
+
var t = props && typeof props.t === 'function' ? props.t : function (key) { return key; };
|
|
375
|
+
var view = useView();
|
|
376
|
+
var noticePair = react.useState('');
|
|
377
|
+
var notice = noticePair[0];
|
|
378
|
+
var setNotice = noticePair[1];
|
|
379
|
+
|
|
380
|
+
function slider(key, label) {
|
|
381
|
+
return react.createElement(
|
|
382
|
+
'div',
|
|
383
|
+
{ key: key, style: sliderRowStyle },
|
|
384
|
+
react.createElement('span', { style: sliderLabelStyle }, label),
|
|
385
|
+
react.createElement('input', {
|
|
386
|
+
type: 'range',
|
|
387
|
+
min: LIMITS[key][0],
|
|
388
|
+
max: LIMITS[key][1],
|
|
389
|
+
step: 1,
|
|
390
|
+
value: view[key],
|
|
391
|
+
'aria-label': label,
|
|
392
|
+
disabled: view.locked === true,
|
|
393
|
+
style: view.locked === true
|
|
394
|
+
? Object.assign({}, sliderStyle, { opacity: 0.4, cursor: 'not-allowed' })
|
|
395
|
+
: sliderStyle,
|
|
396
|
+
onChange: function (event) {
|
|
397
|
+
var patch = {};
|
|
398
|
+
patch[key] = Number(event.target.value);
|
|
399
|
+
update(patch);
|
|
400
|
+
}
|
|
401
|
+
}),
|
|
402
|
+
react.createElement('span', { style: sliderValueStyle }, view[key] + ' px')
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return react.createElement(
|
|
407
|
+
'div',
|
|
408
|
+
{ style: rowStyle },
|
|
409
|
+
react.createElement(
|
|
410
|
+
'div',
|
|
411
|
+
{ style: rowTextStyle },
|
|
412
|
+
react.createElement('div', { style: titleStyle }, t('title')),
|
|
413
|
+
react.createElement('div', { style: descStyle }, t('description'))
|
|
414
|
+
),
|
|
415
|
+
|
|
416
|
+
/* 图片行:上传 / 清除 / 抠白底 */
|
|
417
|
+
react.createElement(
|
|
418
|
+
'div',
|
|
419
|
+
{ style: controlRowStyle },
|
|
420
|
+
react.createElement('span', { style: sliderLabelStyle }, t('image')),
|
|
421
|
+
react.createElement('button', {
|
|
422
|
+
type: 'button',
|
|
423
|
+
style: pillStyle,
|
|
424
|
+
onClick: function () {
|
|
425
|
+
setNotice('');
|
|
426
|
+
pickImageFile(function (file) {
|
|
427
|
+
readImageFile(file, view.cutout !== false, function (url) {
|
|
428
|
+
if (url === null) { setNotice(t('imageFailed')); return; }
|
|
429
|
+
update({ image: url });
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}, view.image ? t('imageChange') : t('imagePick')),
|
|
434
|
+
view.image
|
|
435
|
+
? react.createElement('button', {
|
|
436
|
+
type: 'button',
|
|
437
|
+
style: pillStyle,
|
|
438
|
+
onClick: function () { setNotice(''); update({ image: '' }); }
|
|
439
|
+
}, t('imageClear'))
|
|
440
|
+
: null,
|
|
441
|
+
react.createElement('button', {
|
|
442
|
+
type: 'button',
|
|
443
|
+
style: view.cutout !== false ? chipOnStyle : chipStyle,
|
|
444
|
+
'aria-pressed': view.cutout !== false,
|
|
445
|
+
title: t('cutoutHint'),
|
|
446
|
+
onClick: function () { update({ cutout: view.cutout === false }); }
|
|
447
|
+
}, t('cutout') + (view.cutout !== false ? ' ✓' : ''))
|
|
448
|
+
),
|
|
449
|
+
|
|
450
|
+
notice ? react.createElement('div', {
|
|
451
|
+
style: { color: 'var(--dsw-alias-state-error-primary)', fontSize: '12px', lineHeight: '18px' }
|
|
452
|
+
}, notice) : null,
|
|
453
|
+
|
|
454
|
+
/* 锁定模式:一行开关,锁上之后三根滑杆不可拖动。 */
|
|
455
|
+
react.createElement(
|
|
456
|
+
'div',
|
|
457
|
+
{ style: Object.assign({}, controlRowStyle, { gap: '10px' }) },
|
|
458
|
+
react.createElement('button', {
|
|
459
|
+
type: 'button',
|
|
460
|
+
style: view.locked === true ? chipOnStyle : chipStyle,
|
|
461
|
+
'aria-pressed': view.locked === true,
|
|
462
|
+
title: t('lockHint'),
|
|
463
|
+
onClick: function () { setNotice(''); update({ locked: view.locked !== true }); }
|
|
464
|
+
}, t('lock') + (view.locked === true ? ' ✓' : '')),
|
|
465
|
+
react.createElement('span', {
|
|
466
|
+
style: { color: 'var(--dsw-alias-label-tertiary)', fontSize: '12px', lineHeight: '18px' }
|
|
467
|
+
}, view.locked === true ? t('lockOnHint') : t('lockOffHint'))
|
|
468
|
+
),
|
|
469
|
+
|
|
470
|
+
slider('size', t('size')),
|
|
471
|
+
slider('offsetX', t('offsetX')),
|
|
472
|
+
slider('offsetY', t('offsetY')),
|
|
473
|
+
|
|
474
|
+
react.createElement('button', {
|
|
475
|
+
type: 'button',
|
|
476
|
+
style: Object.assign({}, pillStyle, { alignSelf: 'flex-start' }),
|
|
477
|
+
onClick: function () { setNotice(''); update(Object.assign({}, DEFAULTS)); }
|
|
478
|
+
}, t('reset'))
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
//#endregion
|
|
483
|
+
|
|
484
|
+
/** 需要槽位服务与 locale 服务。 */
|
|
485
|
+
var inject = ['slots', 'locale'];
|
|
486
|
+
|
|
487
|
+
function apply(ctx) {
|
|
488
|
+
ctx.effect(function () {
|
|
489
|
+
return ctx.locale.register(NS, {
|
|
490
|
+
zh: {
|
|
491
|
+
title: '吉祥物',
|
|
492
|
+
description: '新会话页左侧吉祥物的图片与位置。可上传自己的图片(自动压缩存储);「抠白底」会从四边泛洪掉与边框连通的纯白背景。',
|
|
493
|
+
image: '图片',
|
|
494
|
+
imagePick: '选择图片',
|
|
495
|
+
imageChange: '更换图片',
|
|
496
|
+
imageClear: '清除',
|
|
497
|
+
imageFailed: '这张图处理失败或太大,换一张试试',
|
|
498
|
+
cutout: '抠白底',
|
|
499
|
+
cutoutHint: '在上传时生效:只吃掉与画布四边连通的纯白背景,角色内部的白色保留。改变后请重新选择图片。',
|
|
500
|
+
size: '大小',
|
|
501
|
+
offsetX: '水平',
|
|
502
|
+
offsetY: '垂直',
|
|
503
|
+
lock: '锁定位置与大小',
|
|
504
|
+
lockHint: '锁定后三根滑杆不可拖动,避免误触;再点一次解锁',
|
|
505
|
+
lockOnHint: '已锁定:滑杆不可拖动(防止误触)',
|
|
506
|
+
lockOffHint: '未锁定:可拖动调整大小与位置',
|
|
507
|
+
reset: '恢复默认'
|
|
508
|
+
},
|
|
509
|
+
en: {
|
|
510
|
+
title: 'Mascot',
|
|
511
|
+
description: 'Image and position of the mascot beside the new-session headline. Upload your own image (stored compressed); "Cut white" flood-fills pure white connected to the edges.',
|
|
512
|
+
image: 'Image',
|
|
513
|
+
imagePick: 'Choose image',
|
|
514
|
+
imageChange: 'Change image',
|
|
515
|
+
imageClear: 'Clear',
|
|
516
|
+
imageFailed: 'That image could not be processed — try another one',
|
|
517
|
+
cutout: 'Cut white',
|
|
518
|
+
cutoutHint: 'Applied when you pick a file: removes pure white connected to the canvas edges, keeping enclosed whites. Re-pick the image after changing this.',
|
|
519
|
+
size: 'Size',
|
|
520
|
+
offsetX: 'X',
|
|
521
|
+
offsetY: 'Y',
|
|
522
|
+
lock: 'Lock size & position',
|
|
523
|
+
lockHint: 'When locked the sliders cannot be dragged; click again to unlock',
|
|
524
|
+
lockOnHint: 'Locked: sliders are read-only',
|
|
525
|
+
lockOffHint: 'Unlocked: drag to adjust size and position',
|
|
526
|
+
reset: 'Reset'
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
}, 'dsha-hero-mascot: locale');
|
|
530
|
+
|
|
531
|
+
/* 1. 吉祥物:占据 Hero 品牌图标槽位。 */
|
|
532
|
+
ctx.slots.inject('conversation.hero.brand.mark', function () {
|
|
533
|
+
return ctx.slots.register({ name: 'conversation.hero.brand.mark', id: 'hero-mascot' }, HeroMascot);
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
/* 2. 设置行:设置 → 通用里追加一项(list 槽位,用 id 保证唯一)。 */
|
|
537
|
+
ctx.slots.inject('settings.general.item', function () {
|
|
538
|
+
return ctx.slots.register({
|
|
539
|
+
name: 'settings.general.item',
|
|
540
|
+
id: 'hero-mascot',
|
|
541
|
+
order: 40,
|
|
542
|
+
locale: NS
|
|
543
|
+
}, MascotSettingsRow);
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
exports.apply = apply;
|
|
548
|
+
exports.inject = inject;
|
|
549
|
+
return module.exports;
|
|
550
|
+
}
|
|
551
|
+
});
|
package/cordis.patch.yml
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsha-hero-mascot 的主机半边。
|
|
3
|
+
*
|
|
4
|
+
* 只做一件事:把 assets/hero.png 通过 webServer 暴露成一个固定 URL,
|
|
5
|
+
* 供浏览器半边当 <img src> 使用。
|
|
6
|
+
*
|
|
7
|
+
* 为什么不把图片 base64 内联进 client.js:图有 ~300 KB,内联会让浏览器半边
|
|
8
|
+
* 变成一个巨大的、改不动也读不了的字符串;走路由则源码保持可读,换成你自己的
|
|
9
|
+
* 图时只要替换 assets/hero.png 再重启即可,不用改一行代码。
|
|
10
|
+
*
|
|
11
|
+
* 图片读一次就缓存住(进程内),避免每个请求都读盘。
|
|
12
|
+
*/
|
|
13
|
+
import { readFileSync } from 'node:fs'
|
|
14
|
+
import { dirname, join } from 'node:path'
|
|
15
|
+
import { fileURLToPath } from 'node:url'
|
|
16
|
+
|
|
17
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
18
|
+
/** 对外路径。与 /plugins/<id>/client.js 不冲突。 */
|
|
19
|
+
const ROUTE = '/dsh-hero-mascot/hero.png'
|
|
20
|
+
/** 磁盘路径。 */
|
|
21
|
+
const HERO_FILE = join(HERE, 'assets', 'hero.png')
|
|
22
|
+
|
|
23
|
+
const name = 'dsh-hero-mascot'
|
|
24
|
+
/** webServer 声明为硬依赖:loader 会等它就绪后再激活本行。 */
|
|
25
|
+
const inject = ['webServer']
|
|
26
|
+
|
|
27
|
+
function apply(ctx) {
|
|
28
|
+
const webServer = ctx.webServer
|
|
29
|
+
/** 进程内缓存;文件不存在时为保持 null,每次请求重试(方便换图后不重启也能生效一次)。 */
|
|
30
|
+
let cached = null
|
|
31
|
+
|
|
32
|
+
ctx.effect(() => webServer.register({
|
|
33
|
+
kind: 'exact',
|
|
34
|
+
path: ROUTE,
|
|
35
|
+
handler: (req, res) => {
|
|
36
|
+
try {
|
|
37
|
+
if (cached === null) cached = readFileSync(HERO_FILE)
|
|
38
|
+
res.writeHead(200, {
|
|
39
|
+
'content-type': 'image/png',
|
|
40
|
+
'content-length': String(cached.length),
|
|
41
|
+
// 换图后重启即可生效;这里不用强缓存,避免浏览器留着旧图。
|
|
42
|
+
'cache-control': 'no-cache'
|
|
43
|
+
})
|
|
44
|
+
res.end(cached)
|
|
45
|
+
} catch (err) {
|
|
46
|
+
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' })
|
|
47
|
+
res.end('dsha-hero-mascot: assets/hero.png 读取失败 — ' + String((err && err.message) || err))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}), 'dsh-hero-mascot: hero image route')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { name, inject, apply }
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-hero-mascot",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.2.1",
|
|
4
|
+
"description": "新会话 Hero 区左侧吉祥物:图片可上传自定义(可选抠白底)、大小与位置可在「设置 → 通用」调节,并可锁定防误触。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"client.js",
|
|
16
|
+
"cordis.patch.yml",
|
|
17
|
+
"assets",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"dsh": {
|
|
21
|
+
"bundle": {
|
|
22
|
+
"patch": "./cordis.patch.yml"
|
|
23
|
+
},
|
|
24
|
+
"client": {
|
|
25
|
+
"platform": "web",
|
|
26
|
+
"inject": [
|
|
27
|
+
"@deepseek-ai/dsh-client-ui-slots",
|
|
28
|
+
"@deepseek-ai/dsh-client-locale",
|
|
29
|
+
"@deepseek-ai/dsh-client-ui-settings-general"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"author": {
|
|
35
|
+
"name": "Cyrene-xl",
|
|
36
|
+
"url": "https://github.com/Cyrene-xl"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/Cyrene-xl/DSH-Cyrene-UI.git",
|
|
41
|
+
"directory": "packages/hero-mascot"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/Cyrene-xl/DSH-Cyrene-UI/tree/main/packages/hero-mascot",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/Cyrene-xl/DSH-Cyrene-UI/issues"
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"dsh",
|
|
49
|
+
"dsh-plugin",
|
|
50
|
+
"theme",
|
|
51
|
+
"ui",
|
|
52
|
+
"mascot",
|
|
53
|
+
"hero"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=22",
|
|
57
|
+
"dsh": ">=0.1.5 <0.3.0"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
62
|
+
}
|