Files

198 lines
7.6 KiB
JavaScript

const fs = require('fs')
const p = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/renderer/src/editor/Editor.tsx'
let c = fs.readFileSync(p, 'utf8')
function rep(from, to) {
if (!c.includes(from)) {
console.log('MISS ::', from.slice(0, 70).replace(/\n/g, '\\n'))
return
}
c = c.replace(from, to)
}
// 导入周期表相关
rep(
`import {
ELEMENT_CATEGORY_LABEL,
CATEGORY_COLORS,
DEFAULT_PERIODIC_STYLE,
elementName,
MAX_LEVEL,
type PeriodicStyle
} from '../../../shared/elements'`,
`import {
CATEGORY_COLORS,
DEFAULT_PERIODIC_STYLE,
ELEMENT_CATEGORY_LABEL,
ELEMENT_GRID_POS,
ELEMENTS,
elementName,
MAX_LEVEL,
periodicCellColor,
PERIODIC_PRESETS,
type PeriodicStyle
} from '../../../shared/elements'`
)
// 兼容旧导入名
c = c.replace(/ELEMENT_CATEGORY_LABEL,\n CATEGORY_COLORS,/, 'CATEGORY_COLORS,\n ELEMENT_CATEGORY_LABEL,')
// 颜色是否被改过(自定义状态)
rep(
" const [presetName, setPresetName] = useState('')",
" const [presetName, setPresetName] = useState('')\n const [customDirty, setCustomDirty] = useState(false)"
)
// 周期表配色块替换:迷你周期表 + 可点图例 + 自定义状态
const startMark = ' <div className="section-title">周期表配色</div>'
const endMark = ` <button
className="mini wide"
onClick={() => {
void api()?.saveCatalog({ items, jobs, levels: levelsMeta, periodic }).then((r) => {`
const s = c.indexOf(startMark)
const e = c.indexOf(endMark)
if (s < 0 || e < 0) {
console.log('MISS periodic block bounds', s, e)
} else {
const block = ` <div className="section-title">周期表配色</div>
<div className="field">
<span>配色</span>
<select
className="text-input"
value={customDirty ? '__custom' : periodic.scheme + (periodic.colors ? ':' + JSON.stringify(periodic.colors) : '')}
onChange={(ev) => {
const v = ev.target.value
if (v === '__custom') return
const preset = PERIODIC_PRESETS.find((x) => x.id === v)
if (preset) {
setPeriodic({ ...periodic, scheme: preset.scheme, colors: { ...preset.colors } })
setCustomDirty(false)
}
}}
>
{customDirty && <option value="__custom">自定义(未保存)</option>}
{PERIODIC_PRESETS.map((ps) => (
<option key={ps.id} value={ps.id}>
{ps.name}
</option>
))}
</select>
</div>
<p className="tips">点下面的格子或图例就能改颜色;改完会变成「自定义」,另存命名后才会保留。</p>
<div className="periodic-editor" style={{ background: periodic.bg ?? '#0e1420' }}>
{ELEMENTS.map((el) => {
const [row, col] = ELEMENT_GRID_POS[el.z - 1] ?? [1, 1]
const category =
periodic.scheme === 'category' ? undefined : el.block
const key = periodic.scheme === 'category' ? undefined : el.block
const colorFor = periodicCellColor(el, periodic)
const colorKey =
periodic.scheme === 'category'
? Object.entries(CATEGORY_COLORS).find(([, v]) => v === colorFor)?.[0] ?? ''
: String(key ?? '')
return (
<label
key={el.z}
className={'ped-cell' + (lvKind === 'chemistry' && lvKey === el.z ? ' on' : '')}
style={{ gridRow: row, gridColumn: col, background: colorFor }}
title={el.z + ' ' + elementName(el.z) + ' ' + el.symbol}
>
<input
type="color"
className="ped-color"
value={colorFor}
onChange={(ev) => {
const nextColors = { ...(periodic.colors ?? {}) }
if (periodic.scheme === 'category' && colorKey) {
nextColors[colorKey] = ev.target.value
} else {
nextColors[String(key ?? '')] = ev.target.value
}
setPeriodic({ ...periodic, colors: nextColors })
setCustomDirty(true)
}}
/>
<span className="ped-z">{el.z}</span>
<span className="ped-sym">{el.symbol}</span>
<span
className="ped-pick"
onClick={(ev) => {
ev.preventDefault()
setLvKind('chemistry')
setLvKey(el.z)
}}
/>
</label>
)
})}
</div>
<div className="periodic-colors">
{(periodic.scheme === 'category'
? (Object.keys(ELEMENT_CATEGORY_LABEL) as Array<keyof typeof ELEMENT_CATEGORY_LABEL>).map(
(k) => ({
key: k as string,
label: ELEMENT_CATEGORY_LABEL[k],
fallback: CATEGORY_COLORS[k]
})
)
: (['s', 'p', 'd', 'f'] as const).map((b) => ({
key: b as string,
label: b + ' 区',
fallback: ({ s: '#ffd54a', p: '#6fc8ff', d: '#8ef0b0', f: '#cf9bff' } as Record<
string,
string
>)[b]
}))
).map((row) => (
<label key={row.key} className="color-row">
<input
type="color"
value={periodic.colors?.[row.key] ?? row.fallback}
onChange={(e) => {
setPeriodic({
...periodic,
colors: { ...(periodic.colors ?? {}), [row.key]: e.target.value }
})
setCustomDirty(true)
}}
/>
<span>{row.label}</span>
</label>
))}
</div>
<div className="field">
<span>底图背景</span>
<input
type="color"
value={periodic.bg ?? '#0e1420'}
onChange={(e) => {
setPeriodic({ ...periodic, bg: e.target.value })
setCustomDirty(true)
}}
/>
</div>
`
c = c.slice(0, s) + block + c.slice(e)
}
// 另存自定义后:命名成功即视为已保存,清掉「自定义」标记
rep(
` setPresetName('')
void api()?.saveCatalog({ items, jobs, levels: levelsMeta, periodic: next }).then((r) => {`,
` setPresetName('')
setCustomDirty(false)
void api()?.saveCatalog({ items, jobs, levels: levelsMeta, periodic: next }).then((r) => {`
)
// 保存整套配色后也清标记
rep(
` 保存周期表配色`,
` 保存当前配色`
)
fs.writeFileSync(p, c, 'utf8')
console.log('editor periodic grid patched')