kf-score-panel.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * components/kf-score-panel.js —— <kf-score-panel> 多人评分面板。
  3. *
  4. * 显示:平均分(大字)、评分人数、人工平均 / Agent 平均、每一条评分(谁、几分、评语、什么时候)。
  5. * 输入:用户 + 0–100 分 + 评语 → 派发 score-submit { user, score, comment };业务(去重、平均)在 core。
  6. */
  7. import { LitElement, html, css } from 'lit';
  8. import { useTokens, define } from './base.js';
  9. import './kf-viz.js';
  10. import './kf-stat.js';
  11. import { fmtAgo, fmtDate } from '../core/index.js';
  12. import { t } from './cjk.js';
  13. export class KfScorePanel extends LitElement {
  14. static properties = {
  15. stats: { type: Object },
  16. user: { type: String },
  17. busy: { type: Boolean },
  18. _score: { state: true },
  19. _comment: { state: true },
  20. _err: { state: true },
  21. };
  22. constructor() {
  23. super();
  24. this.stats = { n: 0, avg: null, scores: [] };
  25. this.user = '';
  26. this.busy = false;
  27. this._score = '';
  28. this._comment = '';
  29. this._err = '';
  30. }
  31. connectedCallback() {
  32. super.connectedCallback();
  33. useTokens(this);
  34. }
  35. static styles = css`
  36. :host { display: block; }
  37. /* 平均分:大数字(1.1 行盒修剪)+ 进度环(板块二:弧长表达平均在 100 里的位置),环心印数字 */
  38. .head { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
  39. .avgwrap { display: flex; align-items: center; gap: 12px; }
  40. .avg { font-size: 32px; font-weight: 600; color: var(--kf-ink); line-height: 1; font-variant-numeric: tabular-nums; text-box-trim: trim-both; text-box-edge: cap alphabetic;
  41. font-family: var(--kf-font-num); letter-spacing: var(--ls-cn-display); display: block; height: max-content; } /* v11b:flex 容器不修剪 */
  42. .avg small { font: 500 11px var(--kf-font); color: var(--kf-ink-3); margin-left: 0.12em; letter-spacing: 0; }
  43. .meta { display: grid; gap: 2px; font-size: 13px; color: var(--kf-ink-3); }
  44. .meta b { color: var(--kf-ink); font-weight: 600; font-variant-numeric: tabular-nums; }
  45. .none { color: var(--kf-ink-3); font-size: 14px; font-weight: 600; }
  46. ul { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 6px; }
  47. li { display: grid; grid-template-columns: 88px 48px 1fr; gap: 8px; align-items: baseline; padding: 8px 12px; border-radius: 8px; background: rgba(255,255,255,0.4); border: 1px solid var(--kf-line); }
  48. /* Agent 评分:6% 染色 + 1px 同色半透明描边(不是整块紫底) */
  49. li.agent { background: color-mix(in srgb, var(--kf-accent) 6%, rgba(255,255,255,0.4)); border-color: color-mix(in srgb, var(--kf-accent) 18%, transparent); }
  50. .who { color: var(--kf-ink); font-weight: 600; font-size: 13px; }
  51. .s { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--kf-accent-ink); font-size: 14px; }
  52. /* 行内只用 kf-stat 的数字槽(数字滚动 + 单位分层都复用它),不渲染标签 / 注脚 */
  53. .s kf-stat { display: inline-block; }
  54. .c .t { color: var(--kf-ink-3); font-size: 11px; margin-left: 6px; }
  55. form { margin-top: 16px; padding: 12px; border-radius: 8px; border: 1px dashed var(--kf-line-strong); display: grid; gap: 8px; }
  56. .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
  57. input[type="number"], input[type="text"] { height: 32px; border-radius: 8px; border: 1px solid var(--kf-line-strong); background: rgba(255,255,255,0.6); transition: var(--kf-t); }
  58. input:focus { outline: none; border-color: var(--kf-focus); box-shadow: 0 0 0 2px rgba(91, 91, 214, 0.22); background: #fff; }
  59. input[type="number"] { width: 88px; padding: 0 8px; font: 600 14px var(--kf-font-num); font-variant-numeric: tabular-nums; }
  60. input[type="text"] { flex: 1; min-width: 160px; padding: 0 12px; font: 13px var(--kf-font); }
  61. .err { color: var(--kf-danger); font-size: 11px; }
  62. .hint { font-size: 11px; color: var(--kf-ink-3); }
  63. `;
  64. #submit(e) {
  65. e.preventDefault();
  66. this._err = '';
  67. const score = Number(this._score);
  68. if (!this.user) { this._err = '先选「我是谁」(右上角),再评分'; return; }
  69. if (!(score >= 0 && score <= 100) || this._score === '') { this._err = t('分数必须在 0–100 之间'); return; }
  70. this.dispatchEvent(new CustomEvent('score-submit', {
  71. detail: { user: this.user, score, comment: this._comment.trim() },
  72. bubbles: true, composed: true,
  73. }));
  74. this._score = '';
  75. this._comment = '';
  76. }
  77. render() {
  78. const s = this.stats || { n: 0, avg: null, scores: [] };
  79. return html`
  80. <div class="head">
  81. ${s.avg == null
  82. ? html`<div class="none">暂无评分</div>`
  83. : html`<div class="avgwrap"><kf-ring size=${44} .pct=${Math.round(s.avg)} label=${String(s.avg)}></kf-ring><div class="avg kf-num">${s.avg}<small>平均</small></div></div>`}
  84. <div class="meta">
  85. <span>${t(`${s.n || 0} 人评分`)}${s.n > 1 ? '(同一人多次取最后一次)' : ''}</span>
  86. <span>${t(`人工平均 ${s.humanAvg ?? '—'} · Agent 平均 ${s.agentAvg ?? '—'}`)}</span>
  87. ${s.max != null ? html`<span>${t(`最高 ${s.max} · 最低 ${s.min} · 分歧 ${s.max - s.min} 分`)}</span>` : ''}
  88. </div>
  89. </div>
  90. ${s.scores?.length ? html`
  91. <ul aria-label="评分明细">
  92. ${s.scores.map((x) => html`
  93. <li class=${x.source === 'agent' ? 'agent' : ''}>
  94. <span class="who">${x.user}</span>
  95. <span class="s kf-num"><kf-stat inline value=${String(x.score)}></kf-stat></span>
  96. <span class="c">${x.comment || html`<span class="kf-muted">(无评语)</span>`}<span class="t">${x.at ? fmtAgo(x.at) : ''}${x.source === 'agent' ? t(' · Agent') : ''}</span></span>
  97. </li>`)}
  98. </ul>` : ''}
  99. <form @submit=${this.#submit} data-no-activate>
  100. <div class="row">
  101. <strong>我来评分</strong>
  102. <span class="hint">${this.user ? html`评分人:<b>${this.user}</b>` : '未选择「我是谁」'}</span>
  103. </div>
  104. <div class="row">
  105. <input type="number" min="0" max="100" placeholder="0–100" .value=${this._score}
  106. @input=${(e) => { this._score = e.target.value; }} aria-label="分数" />
  107. <input type="text" placeholder="一句评语(可选)" .value=${this._comment}
  108. @input=${(e) => { this._comment = e.target.value; }} aria-label="评语" />
  109. <button class="kf-btn primary" type="submit" ?disabled=${this.busy}>${this.busy ? '提交中…' : '提交评分'}</button>
  110. </div>
  111. ${this._err ? html`<div class="err" role="alert">${this._err}</div>` : ''}
  112. ${s.latest ? html`<div class="hint">${t(`最近一次:${s.latest.user} ${s.latest.score} 分 · ${fmtDate(s.latest.at, true)}`)}</div>` : ''}
  113. </form>
  114. `;
  115. }
  116. }
  117. define('kf-score-panel', KfScorePanel);