From 2196bc17775419d177e9c1d337368ab3adfe3163 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sun, 6 Oct 2024 01:04:41 +0800 Subject: refactor: patch 8d1b346, set order correctly --- src/editor.mjs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/editor.mjs b/src/editor.mjs index c121eb0..dfafaa9 100644 --- a/src/editor.mjs +++ b/src/editor.mjs @@ -833,25 +833,31 @@ new window.MutationObserver(mutaions => { // }}} /** - * addMapRandomlyByPreset. insert text of valid mapclay yaml into editor + * addMapRandomlyByPreset. insert random text of valid mapclay yaml into editor */ const addMapRandomlyByPreset = () => { + const yamlText = [ + 'apply: dist/default.yml', + 'width: 85%', + 'height: 200px', + ] const order = [ + 'id', + 'apply', 'use', + 'width', + 'height', 'center', 'XYZ', 'zoom' ] const aliasesEntries = Object.entries(aliasesForMapOptions) - .sort((a, b) => order.indexOf(a) < order.indexOf(b)) - .filter(([key, _]) => order.includes(key)) + .filter(([key, _]) => + order.includes(key) + && !yamlText.find(text => text.startsWith(key)) + ) if (aliasesEntries.length === 0) return - const yamlText = [ - 'apply: dist/default.yml', - 'width: 100%', - 'height: 200px', - ] aliasesEntries.forEach(([option, aliases]) => { const entries = Object.entries(aliases) const validEntries = entries @@ -869,9 +875,15 @@ const addMapRandomlyByPreset = () => { const randomValue = validEntries .at((Math.random() * validEntries.length) | 0) .at(0) + yamlText.push(`${option}: ${typeof randomValue === 'object' ? randomValue.value : randomValue}`) + + if (option === 'center') yamlText.push(`id: ${randomValue}`) }) + yamlText.sort((a, b) => + order.indexOf(a.split(':')[0]) > order.indexOf(b.split(':')[0]) + ) cm.replaceRange( '\n```map\n' + yamlText.join('\n') + '\n```\n', cm.getCursor() -- cgit v1.2.3-70-g09d2