|
@@ -5,53 +5,36 @@ import test from 'node:test'
|
|
|
/**
|
|
/**
|
|
|
* 「设备类型状态分布」图例排版守卫(维护者 2026-12-02)。
|
|
* 「设备类型状态分布」图例排版守卫(维护者 2026-12-02)。
|
|
|
*
|
|
*
|
|
|
- * 症状:图一(donutGridBlock)的图例标签全被截成「窨井盖···343」「可燃···32」,
|
|
|
|
|
- * 且行与行对不齐;图二(stackedBlock)两列、标签完整、色点+名称+计数对齐。
|
|
|
|
|
|
|
+ * 用户要求:图例的**排版模式**与「监测分布-设备类型构成(按点位数)」1:1 一致,
|
|
|
|
|
+ * 包括「图例间的间距」与「文字与数值之间的距离」。
|
|
|
*
|
|
*
|
|
|
- * 根因:donutGrid 的图例是 flex-wrap,且 `.panel-donut__label` 被固定成 4em
|
|
|
|
|
- * (`--donut-label-ch` 默认 4,而 donutGridBlock 没算这个变量),长标签必被截断。
|
|
|
|
|
- * 修法:让 donut-grid 图例复用 stacked 的 grid 两列 + flex 行排版,
|
|
|
|
|
- * 标签列自然占满列宽,不再写死 4em。
|
|
|
|
|
|
|
+ * 唯一可靠的 1:1 是让两处图例**共用同一套 class**(panel-stacked__*):
|
|
|
|
|
+ * 图上所有间距/gap/字体/颜色/hover 只在一处定义,永远不会漂移。
|
|
|
|
|
+ * 曾试图给 donut-grid 另写一套等价 CSS,但漏掉了数值右对齐(margin-left:auto)
|
|
|
|
|
+ * 与图二「数值紧跟标签」不一致,且两处规则要同步维护 —— 直接复用才是根治。
|
|
|
*/
|
|
*/
|
|
|
-function styleOf(file) {
|
|
|
|
|
- const source = readFileSync(new URL(`../${file}`, import.meta.url), 'utf8')
|
|
|
|
|
- const match = source.match(/<style[^>]*>([\s\S]*?)<\/style>/)
|
|
|
|
|
- assert.ok(match, `${file} 应有样式块`)
|
|
|
|
|
- return match[1]
|
|
|
|
|
-}
|
|
|
|
|
|
|
+const source = readFileSync(new URL('../src/modules/cockpit/components/PanelBlocks.vue', import.meta.url), 'utf8')
|
|
|
|
|
+const template = source.slice(0, source.indexOf('<script setup>'))
|
|
|
|
|
+const legendStart = template.indexOf('data-donut-grid-legend')
|
|
|
|
|
+assert.ok(legendStart >= 0, '应能定位 donut-grid 图例')
|
|
|
|
|
+// 截取图例模板段(从该 ul 到其结束 ul)。
|
|
|
|
|
+const legendTemplate = template.slice(template.lastIndexOf('<ul', legendStart), template.indexOf('</ul>', legendStart) + 5)
|
|
|
|
|
+const style = source.slice(source.indexOf('<style'), source.indexOf('</style>'))
|
|
|
|
|
|
|
|
-/** 取一条选择器的规则体(按声明块花括号配对),并剥离注释避免误判。 */
|
|
|
|
|
-function ruleBody(css, selector) {
|
|
|
|
|
- const index = css.indexOf(selector)
|
|
|
|
|
- assert.ok(index >= 0, `找不到规则:${selector}`)
|
|
|
|
|
- const open = css.indexOf('{', index)
|
|
|
|
|
- let depth = 0
|
|
|
|
|
- for (let i = open; i < css.length; i += 1) {
|
|
|
|
|
- if (css[i] === '{') depth += 1
|
|
|
|
|
- else if (css[i] === '}') {
|
|
|
|
|
- depth -= 1
|
|
|
|
|
- if (depth === 0) return css.slice(open + 1, i).replace(/\/\*[\s\S]*?\*\//g, '')
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- throw new Error(`规则花括号不配对:${selector}`)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-const css = styleOf('src/modules/cockpit/components/PanelBlocks.vue')
|
|
|
|
|
-
|
|
|
|
|
-test('donut-grid 图例用与 stacked 参考一致的 auto-fill 两列栅格,不再 flex-wrap', () => {
|
|
|
|
|
- const body = ruleBody(css, '.panel-donuts__legend {')
|
|
|
|
|
- assert.match(body, /display:\s*grid/, '图例容器应为 grid')
|
|
|
|
|
- // 与图二 stackedBlock 同源:auto-fill + minmax(96u,1fr),列数随面板宽度自适应,
|
|
|
|
|
- // 每列至少 96u 保证标签有足够宽度,不再被压成 4 字。
|
|
|
|
|
- assert.match(body, /grid-template-columns:\s*repeat\(\s*auto-fill\s*,\s*minmax\(calc\(96 \* var\(--u, 1px\)\), 1fr\)\)/)
|
|
|
|
|
- assert.doesNotMatch(body, /flex-wrap/, '不能再回到 flex-wrap')
|
|
|
|
|
|
|
+test('donut-grid 图例直接复用 stacked 的排版 class(1:1 的根本保证)', () => {
|
|
|
|
|
+ assert.match(legendTemplate, /class="panel-stacked__legend"/, '图例容器应与 stacked 同源')
|
|
|
|
|
+ assert.match(legendTemplate, /class="panel-stacked__row"/, '图例行应与 stacked 同源')
|
|
|
|
|
+ assert.match(legendTemplate, /class="panel-stacked__label"/, '标签应与 stacked 同源')
|
|
|
|
|
+ assert.match(legendTemplate, /class="panel-stacked__count"/, '计数应与 stacked 同源')
|
|
|
|
|
+ assert.doesNotMatch(legendTemplate, /panel-donuts__legend|panel-donut__row|panel-donut__label|panel-donut__count/,
|
|
|
|
|
+ '不能再留一套独立的图例 class(两套规则必然漂移)')
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
-test('donut-grid 图例行改为 flex(色点+名称+计数),不再用固定 4em 标签列', () => {
|
|
|
|
|
- const row = ruleBody(css, '.panel-donuts__legend .panel-donut__row {')
|
|
|
|
|
- assert.match(row, /display:\s*flex/, '图例行应为 flex')
|
|
|
|
|
-
|
|
|
|
|
- const label = ruleBody(css, '.panel-donuts__legend .panel-donut__label {')
|
|
|
|
|
- assert.match(label, /width:\s*auto/, '标签列应自动占满剩余宽度')
|
|
|
|
|
- assert.doesNotMatch(label, /--donut-label-ch/, '不能再把标签宽度写死成字符数')
|
|
|
|
|
|
|
+test('数值紧跟标签,不再有把数值推到列尾的右对齐规则', () => {
|
|
|
|
|
+ // 图二 stacked 的数值是 flex:none 紧跟标签;图一曾加 margin-left:auto 造成「文字···343」的疏离。
|
|
|
|
|
+ assert.doesNotMatch(style, /\.panel-donuts__legend\s+\.panel-donut__count/,
|
|
|
|
|
+ '不应再为 donut-grid 单独定义计数右对齐')
|
|
|
|
|
+ // donut-grid 图例现在没有独立 CSS 规则,间距/字体完全交给 stacked 一套。
|
|
|
|
|
+ assert.doesNotMatch(style, /\.panel-donuts__legend\s*\{/,
|
|
|
|
|
+ '不应再有独立的 donut-grid 图例规则')
|
|
|
})
|
|
})
|