diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-05 20:56:41 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-05 20:57:39 +0800 |
| commit | 8d1b346c4dd7fa2bea554796ff61fd3d706d8680 (patch) | |
| tree | 731e3ce88e1480a67b19ac7f4f28e14d260b53fd /src | |
| parent | 0f856d7816a1fec6f1fdddf4fffe1b2021276115 (diff) | |
feat: add "Roll for map" feature
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.mjs | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index db5d54d..90c446e 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -63,7 +63,7 @@ const editor = new EasyMDE({ | |||
| 63 | name: 'roll', | 63 | name: 'roll', |
| 64 | title: 'Roll a dice', | 64 | title: 'Roll a dice', |
| 65 | text: '\u{2684}', | 65 | text: '\u{2684}', |
| 66 | action: () => toggleEditing() | 66 | action: () => addMapRandomlyByPreset() |
| 67 | }, | 67 | }, |
| 68 | { | 68 | { |
| 69 | name: 'debug', | 69 | name: 'debug', |
| @@ -350,9 +350,9 @@ cm.on('focus', () => { | |||
| 350 | }) | 350 | }) |
| 351 | 351 | ||
| 352 | cm.on('beforeChange', (_, change) => { | 352 | cm.on('beforeChange', (_, change) => { |
| 353 | const line = change.to.line | ||
| 354 | // Don't allow more content after YAML doc separator | 353 | // Don't allow more content after YAML doc separator |
| 355 | if (change.origin.match(/^(\+input|paste)$/)) { | 354 | if (change.origin && change.origin.match(/^(\+input|paste)$/)) { |
| 355 | const line = change.to.line | ||
| 356 | if (cm.getLine(line) === '---' && change.text[0] !== '') { | 356 | if (cm.getLine(line) === '---' && change.text[0] !== '') { |
| 357 | change.cancel() | 357 | change.cancel() |
| 358 | } | 358 | } |
| @@ -805,11 +805,48 @@ new window.MutationObserver(mutaions => { | |||
| 805 | }) | 805 | }) |
| 806 | // }}} | 806 | // }}} |
| 807 | 807 | ||
| 808 | /** | ||
| 809 | * addMapRandomlyByPreset. insert text of valid mapclay yaml into editor | ||
| 810 | */ | ||
| 808 | const addMapRandomlyByPreset = () => { | 811 | const addMapRandomlyByPreset = () => { |
| 809 | if (Object.keys(aliasesForMapOptions).length === 0) return | 812 | const order = [ |
| 810 | cm.replaceRange('\n```map\n```\n', cm.getCursor()); // adds a new line | 813 | 'use', |
| 811 | 814 | 'width', | |
| 815 | 'height', | ||
| 816 | 'center', | ||
| 817 | 'XYZ', | ||
| 818 | 'zoom' | ||
| 819 | ] | ||
| 820 | const aliasesEntries = Object.entries(aliasesForMapOptions) | ||
| 821 | .sort((a, b) => order.indexOf(a) < order.indexOf(b)) | ||
| 822 | .filter(([key, _]) => order.includes(key)) | ||
| 823 | if (aliasesEntries.length === 0) return | ||
| 824 | |||
| 825 | const yamlText = ['apply: dist/default.yml'] | ||
| 826 | aliasesEntries.forEach(([option, aliases]) => { | ||
| 827 | const entries = Object.entries(aliases) | ||
| 828 | const validEntries = entries | ||
| 829 | .filter(([alias, value]) => { | ||
| 830 | // FIXME logic about picking XYZ data | ||
| 831 | if (option === 'XYZ') { | ||
| 832 | const inTaiwan = yamlText.find(text => text.match(/center: TAIWAN/)) | ||
| 833 | if (!inTaiwan) return !alias.includes('TAIWAN') | ||
| 834 | } | ||
| 835 | if (option === 'zoom') { | ||
| 836 | return value > 6 && value < 15 | ||
| 837 | } | ||
| 838 | return true | ||
| 839 | }) | ||
| 840 | const randomValue = validEntries | ||
| 841 | .at((Math.random() * validEntries.length) | 0) | ||
| 842 | .at(0) | ||
| 843 | yamlText.push(`${option}: ${typeof randomValue === 'object' ? randomValue.value : randomValue}`) | ||
| 844 | }) | ||
| 812 | 845 | ||
| 846 | cm.replaceRange( | ||
| 847 | '\n```map\n' + yamlText.join('\n') + '\n```\n', | ||
| 848 | cm.getCursor() | ||
| 849 | ) | ||
| 813 | } | 850 | } |
| 814 | 851 | ||
| 815 | // vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} | 852 | // vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} |