@chaoswise/portal-theme 0.0.0-stage → 0.2.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 +243 -2
- package/lib/apply.js +122 -0
- package/lib/color.js +363 -0
- package/lib/defaults.js +100 -0
- package/lib/index.js +214 -0
- package/lib/resolve.js +151 -0
- package/lib/runtime.js +385 -0
- package/package.json +44 -4
- package/types/index.d.ts +179 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 DOCP
|
|
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,244 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @chaoswise/portal-theme
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
把门户(AOPC Portal「主题配置」)里的**品牌主色 / 外壳背景 / 侧栏配色**下发给各前端工程。
|
|
4
|
+
只配置一次,所有产品读同一份值:首屏不闪、接口异常不白屏、未配置走门户默认值、切租户不串色。
|
|
5
|
+
|
|
6
|
+
分工:`@chaoswise/branding` 管 logo / favicon / 页签标题(按语言),本包管颜色(按租户)。
|
|
7
|
+
|
|
8
|
+
不做的事:不带 UI 组件、不实现配置页(在 `aops_aopc_portal_web`)、
|
|
9
|
+
不封装请求(注入 `getTheme`)、不做 `@chaoswise/ui` / antd 的 token 映射(见文末)。
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @chaoswise/portal-theme
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 接口契约
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
GET /{gateWay}/api/v1/theme/get // portal 网关,如 /docp/gateway/portal
|
|
21
|
+
resp(实测):
|
|
22
|
+
{ error_code: "000000", result: true,
|
|
23
|
+
data: { id, accountId, createTime, updateTime,
|
|
24
|
+
data: { version, shared, old, new } } } // ← 主题本体在 data.data
|
|
25
|
+
data 为 null / "" 表示「该租户没配置」→ 用包内门户默认值
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
服务端返回的是**主题记录**(一行配置:id / accountId / 时间戳 / creator),主题本体挂在记录的 `data` 上:
|
|
29
|
+
信封里是**两层 `data`**,`body.data` 是记录、`body.data.data` 才是主题本体。
|
|
30
|
+
(旧版本文档写成「`body.data` 就是主题本体」,照它接出来的现象是「门户里配了颜色、页面一点没变、也不报错」。)
|
|
31
|
+
|
|
32
|
+
本包不关心信封,`getTheme` 只负责返回主题本体。**按形状识别,不要按层数硬解** `body?.data?.data`——
|
|
33
|
+
后端或网关改一次包装就会挂:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
const THEME_KEYS = ['shared', 'old', 'new', 'version'];
|
|
37
|
+
const looksLikeTheme = (v) =>
|
|
38
|
+
!!v && typeof v === 'object' && !Array.isArray(v) && THEME_KEYS.some((k) => k in v);
|
|
39
|
+
|
|
40
|
+
const unwrapTheme = (body) => {
|
|
41
|
+
const data = body?.data ?? null;
|
|
42
|
+
if (looksLikeTheme(data)) return data; // 封装已剥掉一层:直接是主题本体
|
|
43
|
+
const nested = data?.data ?? null;
|
|
44
|
+
return looksLikeTheme(nested) ? nested : null; // 主题记录包一层(当前真实返回)
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
getTheme: async () => unwrapTheme(await request.get(`${baseUrl}${gateWay}/api/v1/theme/get`)),
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
服务端只存**覆盖值**,缺失字段由本包合并默认值:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
{
|
|
54
|
+
version: 1,
|
|
55
|
+
shared: { primaryColor: '#1890ff' },
|
|
56
|
+
old: { // 经典菜单
|
|
57
|
+
headerBackground, headerText,
|
|
58
|
+
sider: { background, text, hoverBackground, selectedBackground, selectedText }
|
|
59
|
+
},
|
|
60
|
+
new: {
|
|
61
|
+
light: { // 新版菜单
|
|
62
|
+
shellBackground, contentSurface, headerText,
|
|
63
|
+
sider: { background, text, hoverBackground, selectedBackground, selectedText }
|
|
64
|
+
},
|
|
65
|
+
dark: { /* 同 light */ }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
两条必须守住的约定:
|
|
71
|
+
|
|
72
|
+
- **默认值只有一份**,就是本包 `src/defaults.js`。业务工程不要再抄一份,也不要让后端算「最终值」。
|
|
73
|
+
- **非法值就地回退**:颜色过 `sanitizeColor` 校验,渐变不接受用户输入,
|
|
74
|
+
`transparent` 只允许 `new.*.sider.background`。写坏一个字段不影响其它字段。
|
|
75
|
+
|
|
76
|
+
`old.sider.selectedText` 默认 `null` = 跟随品牌主色(antd 经典菜单的选中文字色就是 colorPrimary)。
|
|
77
|
+
|
|
78
|
+
## CSS 变量
|
|
79
|
+
|
|
80
|
+
`initPortalTheme()` 默认把当前值写到 `document.documentElement`,未配置时也会写包内门户默认值。
|
|
81
|
+
样式里使用 `var(--portal-theme-xxx, 兜底值)`;兜底值填该工程接入前的实际颜色,用于 JS 未运行、销毁或回滚时保持原视觉。
|
|
82
|
+
|
|
83
|
+
| 变量 | 取自 | 默认(new / light) |
|
|
84
|
+
| --- | --- | --- |
|
|
85
|
+
| `--portal-theme-primary` | `shared.primaryColor` | `#1890ff` |
|
|
86
|
+
| `--portal-theme-shell-bg` | `new.*.shellBackground` | `linear-gradient( 270deg, #DEF0EE 0%, #DEE6F6 57%, #E2E9F9 100%)` |
|
|
87
|
+
| `--portal-theme-content-surface` | `new.*.contentSurface` | `#FFFFFF` |
|
|
88
|
+
| `--portal-theme-header-text` | `*.headerText` | `#1D2129` |
|
|
89
|
+
| `--portal-theme-sider-bg` | `sider.background` | `transparent` |
|
|
90
|
+
| `--portal-theme-sider-text` | `sider.text` | `#333A48` |
|
|
91
|
+
| `--portal-theme-sider-hover-bg` | `sider.hoverBackground` | `#F5F6F8` |
|
|
92
|
+
| `--portal-theme-sider-selected-bg` | `sider.selectedBackground` | `#FFFFFF` |
|
|
93
|
+
| `--portal-theme-sider-selected-text` | `sider.selectedText` | `#008DFF` |
|
|
94
|
+
| `--portal-theme-sider-muted-text` | 派生 | 仅经典布局改过侧栏配色时才写入 |
|
|
95
|
+
| `--portal-theme-sider-secondary-text` | `deriveMutedText(sider.text, 实际底色)` | 仅新版布局写入;默认 `#333A48` 对默认外壳渐变派生为 `rgba(51, 58, 72, 0.57)` |
|
|
96
|
+
|
|
97
|
+
- 变量名见 `PORTAL_THEME_CSS_VARS`,按 key 批量取值时用它,不要手写字符串。
|
|
98
|
+
- 切布局(new ↔ old)会清掉不属于当前布局的变量,所以 `var()` 的兜底值一定要写。
|
|
99
|
+
|
|
100
|
+
### 弱化文字(次级账号 / 菜单分组标题)
|
|
101
|
+
|
|
102
|
+
`--portal-theme-sider-secondary-text` 是**新版布局**专用的弱化文字色,由 `deriveMutedText` 在写入变量时按
|
|
103
|
+
`sider.text` 与实际底色反解。产品侧直接消费,**不要**自己再写一套"正文色 × 固定不透明度"的派生:
|
|
104
|
+
|
|
105
|
+
```css
|
|
106
|
+
.sidebar-user-account { color: var(--portal-theme-sider-secondary-text, #00000073); }
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
兜底值填该工程接入前的实际弱化色(如 `#00000073`)。**不要**在 `var()` 取不到时再调本地函数补算一版 ——
|
|
110
|
+
同样的算法换底色会算出不同档位,两套值并存正是这个问题本来的样子。
|
|
111
|
+
|
|
112
|
+
两个变量语义不同,别混用:
|
|
113
|
+
|
|
114
|
+
| 变量 | 布局 | 用途 | 是否弱化 |
|
|
115
|
+
| --- | --- | --- | --- |
|
|
116
|
+
| `--portal-theme-sider-muted-text` | 仅经典 | 侧栏底部工具条图标(设置 / 版本 / 折叠) | 否(= 正文色) |
|
|
117
|
+
| `--portal-theme-sider-secondary-text` | 仅新版 | 次级账号、菜单分组标题等弱化文字 | 是(按对比度反解) |
|
|
118
|
+
|
|
119
|
+
底色怎么取:`sider.background` 非透明时用侧栏实色;为 `transparent`(新版默认)时用 `shellBackground`,
|
|
120
|
+
否则量的是"透明色",等于没做对比度约束。包内默认外壳渐变按**更不利的一端**判定,渐变整段都达标。
|
|
121
|
+
|
|
122
|
+
正文色本身达不到目标对比度时,变量写入正文色本身(不弱化):继续压 alpha 只会更不可读。
|
|
123
|
+
这种配色应该在**配置页**保存时被 `isContrastEnough(sider.text, 实际底色)` 拦住(见下文),运行时不再救。
|
|
124
|
+
|
|
125
|
+
### 配置页:保存前校验正文色对比度
|
|
126
|
+
|
|
127
|
+
派生只能解决"弱化文字不要比正文更不可读",救不了"正文色本身就不可读"。例如 `#E62604` 对浅蓝外壳只有
|
|
128
|
+
3.71:1,14px 正常字重达不到 WCAG AA 的 4.5:1。配置页(`aops_aopc_portal_web`)在保存主题前应对
|
|
129
|
+
`sider.text` 与 `selectedText` 各跑一次:
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
import { isContrastEnough } from '@chaoswise/portal-theme';
|
|
133
|
+
|
|
134
|
+
const bg = sider.background && sider.background !== 'transparent'
|
|
135
|
+
? sider.background
|
|
136
|
+
: shellBackground; // 新版侧栏常为 transparent,透出外壳背景
|
|
137
|
+
|
|
138
|
+
// min 默认 MIN_TEXT_CONTRAST = 4.5;fallbackBackground 仅在底色本身透明时生效
|
|
139
|
+
isContrastEnough(sider.text, bg);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
注意 `isContrastEnough` 的 `fallbackBackground` 默认是 `#FFFFFF`:新版侧栏底色为 `transparent` 时必须
|
|
143
|
+
显式传 `shellBackground`,否则按白底算会有偏差。`shellBackground` 为包内默认渐变时,对渐变起止色各校验一次。
|
|
144
|
+
- 切布局(new ↔ old)会清掉不属于当前布局的变量,所以 `var()` 的兜底值一定要写。
|
|
145
|
+
|
|
146
|
+
## 接入
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
// 1. 启动早期初始化一次(登录后、拿到 accountId 之后)
|
|
150
|
+
import { initPortalTheme } from '@chaoswise/portal-theme';
|
|
151
|
+
|
|
152
|
+
initPortalTheme({
|
|
153
|
+
getTheme: () => request.get('/gateway/portal/api/v1/theme/get').then(unwrapTheme), // 见「接口契约」
|
|
154
|
+
menuTheme: 'new', // 'new' | 'old'
|
|
155
|
+
mode: 'light', // 'light' | 'dark'
|
|
156
|
+
cacheKey: `portal-theme:${accountId}`, // 缓存默认开启,此时必填且必须带 accountId
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// 2. 组件里订阅(React 18)
|
|
160
|
+
const snapshot = useSyncExternalStore(subscribePortalTheme, getPortalThemeSnapshot);
|
|
161
|
+
const active = snapshot.active; // 已按 menuTheme / mode 选好并合并默认值
|
|
162
|
+
|
|
163
|
+
// 3. 样式用 CSS 变量,或读 snapshot.active.* 生成自己的 token
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
需要「首屏就是正确颜色」时不阻塞启动:读缓存是同步的,首帧已经对了,只给网络加个上限:
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
await Promise.race([initPortalTheme({ ... }), new Promise((r) => setTimeout(r, 300))]);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**非 React / 非 `@chaoswise/ui` 的工程**(Vite + tailwind、shadcn 之类)把本包当输入源,自己映射:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
initPortalTheme({ getTheme, menuTheme: 'new', mode: 'light', cacheKey, applyCssVars: false });
|
|
176
|
+
|
|
177
|
+
const syncVars = ({ active }) => {
|
|
178
|
+
const root = document.documentElement.style;
|
|
179
|
+
root.setProperty('--app-nav-active-color', active.primaryColor);
|
|
180
|
+
root.setProperty('--app-shell-background', active.shellBackground);
|
|
181
|
+
root.setProperty('--app-panel-background', active.contentSurface);
|
|
182
|
+
root.setProperty('--app-nav-color', active.sider.text);
|
|
183
|
+
root.setProperty('--app-nav-hover-background', active.sider.hoverBackground);
|
|
184
|
+
root.setProperty('--app-nav-active-background', active.sider.selectedBackground);
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
subscribePortalTheme(syncVars);
|
|
188
|
+
syncVars(getPortalThemeSnapshot()); // 订阅只推后续变化,首屏要自己同步一次
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
注意 shadcn 的 `--primary` 是近黑(`#171717`),**不要**把品牌主色往 `--primary` 上盖,
|
|
192
|
+
否则按钮、边框、focus ring 全变蓝。主色挂到自己的语义变量上。
|
|
193
|
+
|
|
194
|
+
## 缓存与租户
|
|
195
|
+
|
|
196
|
+
- 缓存条目 `{ v, ts, raw }` 存在 `localStorage`。
|
|
197
|
+
- `cache` 默认 `true`,此时必须显式传带 `accountId` 的 `cacheKey`:`portal-theme:${accountId}`;
|
|
198
|
+
不需要缓存时传 `cache: false`,此时可以不传 `cacheKey`。
|
|
199
|
+
- `initPortalTheme` 检测到 `cacheKey` 变了会立刻清掉旧值和旧变量,且在飞的旧请求结果直接丢弃。
|
|
200
|
+
- 缓存条目结构版本独立于 `PORTAL_THEME_VERSION`,只有缓存结构变化时才升版本,旧缓存自动作废。
|
|
201
|
+
- 登出 / 切租户 / 配置页「恢复默认」后调 `clearPortalThemeCache()`;
|
|
202
|
+
清掉所有 `portal-theme` 前缀的 key 调 `clearPortalThemeCache(undefined, true)`。
|
|
203
|
+
- 隐私模式下 `localStorage` 存在但抛错,读写都包了 try/catch,失败只影响首帧,不影响功能。
|
|
204
|
+
|
|
205
|
+
## API
|
|
206
|
+
|
|
207
|
+
| 方法 | 说明 |
|
|
208
|
+
| --- | --- |
|
|
209
|
+
| `initPortalTheme(options)` | 启动时调一次;先落地缓存/默认值再拉远端,返回 `Promise<snapshot>` |
|
|
210
|
+
| `refreshPortalTheme()` | 配置页保存后、切回前台时重拉;不读缓存但会写 |
|
|
211
|
+
| `setPortalThemeContext({ menuTheme, mode })` | 只切换取哪一套值并重算,不发请求 |
|
|
212
|
+
| `getPortalThemeSnapshot()` | 同步读快照,状态不变时引用稳定,适合 `useSyncExternalStore` |
|
|
213
|
+
| `getResolvedPortalTheme()` | 已合并默认值的完整主题(新旧两套都在) |
|
|
214
|
+
| `subscribePortalTheme(fn)` | 订阅变化,返回取消函数 |
|
|
215
|
+
| `applyPortalThemeCssVars(active)` | 手动写变量(`applyCssVars: false` 时用) |
|
|
216
|
+
| `removePortalThemeCssVars()` | 移除全部协议变量 |
|
|
217
|
+
| `clearPortalThemeCache(key?, all?)` | 清缓存 |
|
|
218
|
+
| `destroyPortalTheme()` | 移除监听与变量、状态复位(微前端卸载、单测隔离) |
|
|
219
|
+
| `resolvePortalTheme(raw)` / `resolveActiveTheme(resolved, { menuTheme, mode })` | 纯函数,服务端/测试可直接用 |
|
|
220
|
+
| `parseColor` / `isValidColor` / `contrastRatio` / `isContrastEnough` | 颜色工具,做自己映射时判断可读性 |
|
|
221
|
+
| `deriveMutedText(text, background, { targetRatio?, maxAlpha? })` | 派生弱化文字色:按对比度反解 alpha;正文色本身不达标时返回原文本色。`MIN_MUTED_TEXT_CONTRAST = 3` 为默认目标 |
|
|
222
|
+
|
|
223
|
+
`snapshot` 结构:`{ raw, resolved, active, fromCache, loading, error }`。
|
|
224
|
+
失败只写 `error`,不抛、不阻塞渲染;`onError` 回调自身抛错也会被隔离。并发请求只有最新一次的响应算数。
|
|
225
|
+
|
|
226
|
+
## 与 `@chaoswise/ui` 的关系
|
|
227
|
+
|
|
228
|
+
本包只给「值」和「CSS 变量」,`ConfigProvider` token 映射留在各工程
|
|
229
|
+
(组织内 `@chaoswise/ui` 版本分裂,同一段映射代码在不同工程行为不一致)。
|
|
230
|
+
|
|
231
|
+
## 开发
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
cd packages/portal-theme
|
|
235
|
+
npm i # 首次
|
|
236
|
+
npm run build # gulp + babel → lib/(CJS)
|
|
237
|
+
npm test # node --test,__tests__/**/*.test.js
|
|
238
|
+
npm pack # 出 tgz,本地工程可用 file: 依赖联调
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
- 产物只有 CJS `lib/`,`src/` 不进包;`types/` 手写,改导出必须同步改 `types/index.d.ts`。
|
|
242
|
+
- 包内 `import` 必须带 `.js` 扩展名(Node 直接跑 ESM 测试时按路径解析)。
|
|
243
|
+
- `PORTAL_THEME_VERSION` 只表示后端主题协议版本;缓存结构变化时单独递增 `runtime.js` 的 `CACHE_SCHEMA_VERSION`。
|
|
244
|
+
- `npm test` 需要支持 ESM 语法探测的 Node(≥ 22.7 默认开启,20.19+ 亦可):测试文件是 ESM,而 `package.json` 没有 `"type": "module"`,低版本会按 CJS 解析报错。
|
package/lib/apply.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.removePortalThemeCssVars = exports.applyPortalThemeCssVars = exports.PORTAL_THEME_CSS_VARS = void 0;
|
|
8
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
10
|
+
var _color = require("./color.js");
|
|
11
|
+
var _defaults = require("./defaults.js");
|
|
12
|
+
/**
|
|
13
|
+
* 把生效主题写到 :root 的 CSS 变量上
|
|
14
|
+
*
|
|
15
|
+
* 为什么不是 props 往下传:外壳/侧栏样式分散在多个样式文件里,有些规则要穿透到组件库内部结构,
|
|
16
|
+
* 用 CSS 变量能一次写入、处处生效,且未配置时 var() 的兜底值就是当前视觉,回退零成本。
|
|
17
|
+
*
|
|
18
|
+
* 变量名是协议的一部分:样式文件统一写 var(--portal-theme-xxx, 原默认值),
|
|
19
|
+
* 新增变量必须同时更新这里、README 的变量表和消费方样式。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** 运行时 CSS 变量名,样式文件通过 var(--portal-theme-xxx, 原默认值) 消费 */
|
|
23
|
+
var PORTAL_THEME_CSS_VARS = exports.PORTAL_THEME_CSS_VARS = {
|
|
24
|
+
primary: '--portal-theme-primary',
|
|
25
|
+
shellBackground: '--portal-theme-shell-bg',
|
|
26
|
+
contentSurface: '--portal-theme-content-surface',
|
|
27
|
+
headerText: '--portal-theme-header-text',
|
|
28
|
+
siderBackground: '--portal-theme-sider-bg',
|
|
29
|
+
siderText: '--portal-theme-sider-text',
|
|
30
|
+
siderHoverBackground: '--portal-theme-sider-hover-bg',
|
|
31
|
+
siderSelectedBackground: '--portal-theme-sider-selected-bg',
|
|
32
|
+
siderSelectedText: '--portal-theme-sider-selected-text',
|
|
33
|
+
// 只给经典菜单用:侧栏底部工具条(设置/版本/折叠)的图标色,见下方写入逻辑
|
|
34
|
+
siderMutedText: '--portal-theme-sider-muted-text',
|
|
35
|
+
// 只给新版菜单用:弱化文字(次级账号 / 菜单分组标题),由 deriveMutedText 按对比度派生。
|
|
36
|
+
// 与 siderMutedText 分开:后者是工具条图标色(跟随正文,不能弱化),这个是次级文字色。
|
|
37
|
+
siderSecondaryText: '--portal-theme-sider-secondary-text'
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 经典菜单侧栏底部工具条(设置/版本/折叠)的颜色
|
|
42
|
+
*
|
|
43
|
+
* 库里这几个图标写死了 colorTextTertiary / colorTextSecondary,深色侧栏下会几乎看不见;
|
|
44
|
+
* 但也只有用户真的改过经典侧栏配色时才接管,未配置时返回 undefined(不写变量),
|
|
45
|
+
* 保留库默认值,保证「什么都不配」的视觉零变化。
|
|
46
|
+
*/
|
|
47
|
+
var resolveSiderMutedText = function resolveSiderMutedText(activeTheme, sider) {
|
|
48
|
+
if (activeTheme.target !== _defaults.THEME_TARGET_OLD) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
var fallbackSider = _defaults.DEFAULT_PORTAL_THEME.old.sider;
|
|
52
|
+
var customized = sider.background !== fallbackSider.background || sider.text !== fallbackSider.text;
|
|
53
|
+
return customized ? sider.text : undefined;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 新版布局的弱化文字色(次级账号 / 菜单分组标题)
|
|
58
|
+
*
|
|
59
|
+
* 只在新版写入:经典菜单没有这层消费点,写了也是空挂;
|
|
60
|
+
* 保持与 siderMutedText 一样的「切布局清掉不属于当前布局的变量」的约定。
|
|
61
|
+
*
|
|
62
|
+
* 底色取侧栏实际背景:新版 sider.background 常为 transparent(透出外壳渐变),
|
|
63
|
+
* 这时必须用 shellBackground 派生,否则量的是「透明色」,等于没做对比度约束。
|
|
64
|
+
* deriveMutedText 对包内默认渐变取更不利的一端,保证渐变整段都达标。
|
|
65
|
+
*
|
|
66
|
+
* 正文色本身不达标时返回正文色(不弱化):继续压 alpha 只会更不可读,
|
|
67
|
+
* 这种配色应该在配置页保存时被 isContrastEnough 拦住,而不是在运行时救。
|
|
68
|
+
*/
|
|
69
|
+
var resolveSiderSecondaryText = function resolveSiderSecondaryText(activeTheme, sider) {
|
|
70
|
+
if (activeTheme.target !== _defaults.THEME_TARGET_NEW) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
var background = sider.background && sider.background !== 'transparent' ? sider.background : activeTheme.shellBackground;
|
|
74
|
+
return (0, _color.deriveMutedText)(sider.text, background);
|
|
75
|
+
};
|
|
76
|
+
var getRootStyle = function getRootStyle() {
|
|
77
|
+
if (typeof document === 'undefined' || !document.documentElement) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return document.documentElement.style;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 写入 :root CSS 变量;值为空/非字符串时移除该变量,回到样式文件里的兜底值
|
|
85
|
+
*
|
|
86
|
+
* @param {object} activeTheme resolveActiveTheme 的结果
|
|
87
|
+
*/
|
|
88
|
+
var applyPortalThemeCssVars = exports.applyPortalThemeCssVars = function applyPortalThemeCssVars(activeTheme) {
|
|
89
|
+
var style = getRootStyle();
|
|
90
|
+
if (!style || !activeTheme) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
var sider = activeTheme.sider || {};
|
|
94
|
+
var valueMap = (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, PORTAL_THEME_CSS_VARS.primary, activeTheme.primaryColor), PORTAL_THEME_CSS_VARS.shellBackground, activeTheme.shellBackground), PORTAL_THEME_CSS_VARS.contentSurface, activeTheme.contentSurface), PORTAL_THEME_CSS_VARS.headerText, activeTheme.headerText), PORTAL_THEME_CSS_VARS.siderBackground, sider.background), PORTAL_THEME_CSS_VARS.siderText, sider.text), PORTAL_THEME_CSS_VARS.siderHoverBackground, sider.hoverBackground), PORTAL_THEME_CSS_VARS.siderSelectedText, sider.selectedText), PORTAL_THEME_CSS_VARS.siderMutedText, resolveSiderMutedText(activeTheme, sider)), PORTAL_THEME_CSS_VARS.siderSecondaryText, resolveSiderSecondaryText(activeTheme, sider));
|
|
95
|
+
Object.entries(valueMap).forEach(function (_ref) {
|
|
96
|
+
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
|
|
97
|
+
name = _ref2[0],
|
|
98
|
+
value = _ref2[1];
|
|
99
|
+
if (typeof value === 'string' && value.trim()) {
|
|
100
|
+
// 纯色与默认渐变都直接写入;渐变值 parseColor 解析不了但仍是合法 CSS
|
|
101
|
+
style.setProperty(name, value);
|
|
102
|
+
} else {
|
|
103
|
+
style.removeProperty(name);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 移除全部主题变量,回到样式文件里的兜底值
|
|
110
|
+
*
|
|
111
|
+
* 用于「销毁 / 恢复默认」:删掉变量后 var(--x, 兜底值) 会取兜底值,
|
|
112
|
+
* 不会残留上一次租户的颜色。
|
|
113
|
+
*/
|
|
114
|
+
var removePortalThemeCssVars = exports.removePortalThemeCssVars = function removePortalThemeCssVars() {
|
|
115
|
+
var style = getRootStyle();
|
|
116
|
+
if (!style) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
Object.values(PORTAL_THEME_CSS_VARS).forEach(function (name) {
|
|
120
|
+
style.removeProperty(name);
|
|
121
|
+
});
|
|
122
|
+
};
|