diff options
Diffstat (limited to 'scripts/park-editor.mjs')
| -rw-r--r-- | scripts/park-editor.mjs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/scripts/park-editor.mjs b/scripts/park-editor.mjs index e3bae45..48b9f17 100644 --- a/scripts/park-editor.mjs +++ b/scripts/park-editor.mjs @@ -83,6 +83,42 @@ function findCorruptions(value, path, out) { return out; } +function recordKey(obj) { + const src = obj && obj.properties ? obj.properties : obj; + if (!src) return null; + for (const key of ["公園名", "name", "名称", "our_name"]) { + const value = src[key]; + if (typeof value === "string" && value) return value; + } + return null; +} + +// ブラウザが古いデータを持っていても、後から足した項目やレコードを消さないようにする。 +// 画面になくて編集できないキーは、ディスク側の値を残す。 +function mergeMissing(postedList, currentList) { + const map = new Map(); + for (const rec of currentList) { + const key = recordKey(rec); + if (key) map.set(key, rec); + } + const postedKeys = new Set(); + for (const rec of postedList) { + const key = recordKey(rec); + if (!key) continue; + postedKeys.add(key); + const current = map.get(key); + if (!current) continue; + const dst = rec.properties || rec; + const src = current.properties || current; + for (const [field, value] of Object.entries(src)) { + if (!(field in dst)) dst[field] = value; + } + } + for (const [key, rec] of map) { + if (!postedKeys.has(key)) postedList.push(rec); + } +} + const server = createServer(async (req, res) => { try { const url = (req.url || "").split("?")[0]; @@ -132,6 +168,14 @@ const server = createServer(async (req, res) => { throw new Error("データが配列ではありません"); } + // 古い画面からの保存で、新しい項目・レコードが消えないようにマージする + const current = JSON.parse(await readFile(target.dataset.path, "utf-8")); + if (target.id === "nearbyParks") { + mergeMissing(data.features, current.features || []); + } else { + mergeMissing(data, current); + } + const text = target.dataset.pretty ? `${JSON.stringify(data, null, 2)}\n` : `${JSON.stringify(data)}\n`; |
