236 lines
10 KiB
JavaScript
236 lines
10 KiB
JavaScript
const fs = require('fs')
|
||
const p = 'E:/SodiumPet/WorkPlace/v0.1.1/src/renderer/src/editor/Editor.tsx'
|
||
let c = fs.readFileSync(p, 'utf8')
|
||
|
||
// 1) imports
|
||
c = c.replace(
|
||
" periodicCellColor,\n PERIODIC_PRESETS,",
|
||
" periodicCellColor,\n periodicKeyOf,\n periodicZoneList,\n defaultZoneOf,\n ZONE_LABEL,\n ZONE_COLORS,\n PERIODIC_PRESETS,"
|
||
)
|
||
|
||
// 2) state
|
||
c = c.replace(
|
||
" const [customDirty, setCustomDirty] = useState(false)",
|
||
" const [customDirty, setCustomDirty] = useState(false)\n const [pedMode, setPedMode] = useState<'zone' | 'cell'>('zone')\n const [pedKey, setPedKey] = useState('s')"
|
||
)
|
||
|
||
// 3) helpers —— 插在 return 之前
|
||
const helperAnchor = " return (\n <div className=\"editor\""
|
||
const helpers = ` function newZone(): void {
|
||
const used = new Set(Object.keys(periodic.colors ?? {}))
|
||
for (const k of Object.keys(periodic.zoneNames ?? {})) used.add(k)
|
||
let n = 1
|
||
while (used.has('z' + n)) n++
|
||
const id = 'z' + n
|
||
const next: PeriodicStyle = {
|
||
...periodic,
|
||
colors: { ...(periodic.colors ?? {}), [id]: '#9aa7bd' },
|
||
zoneNames: { ...(periodic.zoneNames ?? {}), [id]: '自定义 ' + n }
|
||
}
|
||
setPeriodic(next)
|
||
setPedMode('zone')
|
||
setPedKey(id)
|
||
setCustomDirty(true)
|
||
}
|
||
|
||
const pedZoneRows = periodicZoneList(periodic)
|
||
const pedSelZ = pedMode === 'cell' ? Number(pedKey) : 0
|
||
const pedZoneNow =
|
||
periodic.zoneNames?.[pedKey] ??
|
||
(periodic.scheme === 'category'
|
||
? ELEMENT_CATEGORY_LABEL[pedKey as keyof typeof ELEMENT_CATEGORY_LABEL] ?? pedKey
|
||
: ZONE_LABEL[pedKey as keyof typeof ZONE_LABEL] ?? pedKey)
|
||
|
||
return (
|
||
<div className="editor"`
|
||
c = c.replace(helperAnchor, helpers)
|
||
|
||
// 4) 周期表编辑区整体替换
|
||
const startMark = ' <div className="periodic-editor" style={{ background: periodic.bg ?? \'#0e1420\' }}>'
|
||
const start = c.indexOf(startMark)
|
||
if (start < 0) {
|
||
console.log('periodic editor block not found')
|
||
process.exit(1)
|
||
}
|
||
const endMark = ' <div className="field">\n <span>底图背景</span>'
|
||
const end = c.indexOf(endMark, start)
|
||
if (end < 0) {
|
||
console.log('end marker not found')
|
||
process.exit(1)
|
||
}
|
||
|
||
const block = ` <div className="ped-mode">
|
||
<button
|
||
className={'chip' + (pedMode === 'zone' ? ' on' : '')}
|
||
onClick={() => setPedMode('zone')}
|
||
>
|
||
按分区改色
|
||
</button>
|
||
<button
|
||
className={'chip' + (pedMode === 'cell' ? ' on' : '')}
|
||
onClick={() => setPedMode('cell')}
|
||
>
|
||
单格改色
|
||
</button>
|
||
<span className="tips">按分区:整区同色;单格:只改一个格子。</span>
|
||
</div>
|
||
|
||
<div className="periodic-editor" style={{ background: periodic.bg ?? '#0e1420' }}>
|
||
{ELEMENTS.map((el) => {
|
||
const [row, col] = ELEMENT_GRID_POS[el.z - 1] ?? [1, 1]
|
||
const key = periodicKeyOf(el, periodic)
|
||
const colorFor = periodicCellColor(el, periodic)
|
||
const active = pedMode === 'zone' ? key === pedKey : String(el.z) === pedKey
|
||
return (
|
||
<label
|
||
key={el.z}
|
||
className={'ped-cell' + (active ? ' on' : '')}
|
||
style={{ gridRow: row, gridColumn: col, background: colorFor }}
|
||
title={el.z + ' ' + elementName(el.z) + ' · ' + key}
|
||
onClick={() => {
|
||
setPedKey(pedMode === 'zone' ? key : String(el.z))
|
||
setLvKind('chemistry')
|
||
setLvKey(el.z)
|
||
}}
|
||
>
|
||
<input
|
||
type="color"
|
||
className="ped-color"
|
||
value={colorFor}
|
||
onClick={(ev) => ev.stopPropagation()}
|
||
onChange={(ev) => {
|
||
setCustomDirty(true)
|
||
if (pedMode === 'zone') {
|
||
setPeriodic({
|
||
...periodic,
|
||
colors: { ...(periodic.colors ?? {}), [key]: ev.target.value }
|
||
})
|
||
} else {
|
||
setPeriodic({
|
||
...periodic,
|
||
cellColors: { ...(periodic.cellColors ?? {}), [el.z]: ev.target.value }
|
||
})
|
||
}
|
||
}}
|
||
/>
|
||
<span className="ped-z">{el.z}</span>
|
||
<span className="ped-sym">{el.symbol}</span>
|
||
</label>
|
||
)
|
||
})}
|
||
</div>
|
||
|
||
<div className="ped-current">
|
||
<span>
|
||
当前{pedMode === 'zone' ? '分区' : '格子'}:
|
||
<b>{pedMode === 'zone' ? pedZoneNow : pedKey + ' ' + elementName(Number(pedKey))}</b>
|
||
</span>
|
||
{pedMode === 'cell' && (
|
||
<>
|
||
<span className="muted">所属分区</span>
|
||
<select
|
||
className="text-input ped-small"
|
||
value={periodic.zoneOverrides?.[pedSelZ] ?? ''}
|
||
onChange={(ev) => {
|
||
const v = ev.target.value
|
||
const nextOverrides = { ...(periodic.zoneOverrides ?? {}) }
|
||
if (!v) delete nextOverrides[pedSelZ]
|
||
else nextOverrides[pedSelZ] = v
|
||
setPeriodic({ ...periodic, zoneOverrides: nextOverrides })
|
||
setCustomDirty(true)
|
||
}}
|
||
>
|
||
<option value="">
|
||
默认({ZONE_LABEL[defaultZoneOf(pedSelZ)] ?? defaultZoneOf(pedSelZ)})
|
||
</option>
|
||
{pedZoneRows.map((z) => (
|
||
<option key={z.id} value={z.id}>
|
||
{z.label}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<button
|
||
className="mini"
|
||
onClick={() => {
|
||
const nextCells = { ...(periodic.cellColors ?? {}) }
|
||
delete nextCells[pedSelZ]
|
||
setPeriodic({ ...periodic, cellColors: nextCells })
|
||
setCustomDirty(true)
|
||
}}
|
||
>
|
||
清除本格颜色
|
||
</button>
|
||
</>
|
||
)}
|
||
<button className="mini" onClick={newZone}>
|
||
新建分区
|
||
</button>
|
||
<button
|
||
className="mini"
|
||
onClick={() => {
|
||
setPeriodic({ ...periodic, cellColors: {} })
|
||
setCustomDirty(true)
|
||
}}
|
||
>
|
||
清除全部单格
|
||
</button>
|
||
</div>
|
||
|
||
<div className="periodic-colors">
|
||
{pedZoneRows.map((row) => (
|
||
<label key={row.id} className="color-row">
|
||
<input
|
||
type="color"
|
||
value={row.color}
|
||
onChange={(e) => {
|
||
setPeriodic({
|
||
...periodic,
|
||
colors: { ...(periodic.colors ?? {}), [row.id]: e.target.value }
|
||
})
|
||
setCustomDirty(true)
|
||
}}
|
||
/>
|
||
<span>
|
||
{row.label}({row.count})
|
||
</span>
|
||
{!['s', 'p', 'd', 'ds', 'f'].includes(row.id) &&
|
||
!Object.keys(ELEMENT_CATEGORY_LABEL).includes(row.id) && (
|
||
<input
|
||
className="text-input ped-name"
|
||
value={periodic.zoneNames?.[row.id] ?? ''}
|
||
placeholder="分区名"
|
||
onChange={(e) => {
|
||
setPeriodic({
|
||
...periodic,
|
||
zoneNames: { ...(periodic.zoneNames ?? {}), [row.id]: e.target.value }
|
||
})
|
||
setCustomDirty(true)
|
||
}}
|
||
/>
|
||
)}
|
||
</label>
|
||
))}
|
||
</div>
|
||
|
||
`
|
||
c = c.slice(0, start) + block + c.slice(end)
|
||
|
||
// 5) 预设应用时清掉单格/归属覆盖;匹配时也比对覆盖是否为空
|
||
c = c.replace(
|
||
" setPeriodic({ ...periodic, scheme: preset.scheme, colors: { ...preset.colors } })",
|
||
" setPeriodic({\n scheme: preset.scheme,\n colors: { ...preset.colors },\n cellColors: {},\n zoneOverrides: {},\n zoneNames: {},\n bg: periodic.bg\n })"
|
||
)
|
||
c = c.replace(
|
||
" ps.scheme === periodic.scheme &&\n JSON.stringify(ps.colors) === JSON.stringify(periodic.colors ?? {})",
|
||
" ps.scheme === periodic.scheme &&\n JSON.stringify(ps.colors) === JSON.stringify(periodic.colors ?? {}) &&\n Object.keys(periodic.cellColors ?? {}).length === 0 &&\n Object.keys(periodic.zoneOverrides ?? {}).length === 0"
|
||
)
|
||
|
||
// 6) 提示文案
|
||
c = c.replace(
|
||
'<p className="tips">点下面的格子或图例就能改颜色;改完会变成「自定义」,另存命名后才会保留。</p>',
|
||
'<p className="tips">按分区改色会同时改掉整个区;单格改色只影响一个元素。改完会变成「自定义」,另存命名后才会保留。</p>'
|
||
)
|
||
|
||
fs.writeFileSync(p, c, 'utf8')
|
||
console.log('editor patched')
|