|
1 | 1 | <script lang="ts" module> |
| 2 | + import { onDestroy } from 'svelte' |
2 | 3 | import { VanillaItemDisplay } from '../../outliner/vanillaItemDisplay' |
| 4 | + import EVENTS from '../../util/events' |
3 | 5 | import { localize as translate } from '../../util/lang' |
4 | | - import { validateItem } from '../../util/minecraftUtil' |
5 | | - import { ITEM_DISPLAY_ITEM_DISPLAY_SELECT } from './vanillaItemDisplayElement' |
| 6 | + import { |
| 7 | + ITEM_DISPLAY_ITEM_DISPLAY_SELECT, |
| 8 | + updateItemDisplaySelect, |
| 9 | + } from './vanillaItemDisplayElement' |
6 | 10 | </script> |
7 | 11 |
|
8 | 12 | <script lang="ts"> |
9 | | - export let selected: VanillaItemDisplay |
| 13 | + import { validateItem } from '../../util/minecraftUtil' |
10 | 14 |
|
11 | | - let item = selected.item |
12 | | - let error = selected.error |
| 15 | + let selected = $state(VanillaItemDisplay.selected.at(0)) |
| 16 | + let item = $derived(selected?.item) |
| 17 | + let error = $derived(selected?.error) |
| 18 | +
|
| 19 | + const onSelectionChanged = () => { |
| 20 | + selected = VanillaItemDisplay.selected.at(0) |
| 21 | + item = selected?.item |
| 22 | + error = selected?.error |
| 23 | + updateItemDisplaySelect() |
| 24 | + } |
13 | 25 |
|
14 | | - ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set(selected.itemDisplay) |
| 26 | + const unsubs = [ |
| 27 | + EVENTS.UNDO.subscribe(onSelectionChanged), |
| 28 | + EVENTS.REDO.subscribe(onSelectionChanged), |
| 29 | + EVENTS.UPDATE_SELECTION.subscribe(onSelectionChanged), |
| 30 | + ] |
15 | 31 |
|
16 | | - $: { |
17 | | - $error = '' |
18 | | - if (selected.item !== item) { |
19 | | - void validateItem(item) |
| 32 | + $effect(() => { |
| 33 | + const thisSelected = selected |
| 34 | + const thisItem = item |
| 35 | + error?.set('') |
| 36 | + if (thisSelected && thisItem && thisSelected.item !== thisItem) { |
| 37 | + void validateItem(thisItem) |
20 | 38 | .then(err => { |
21 | 39 | if (err) { |
22 | | - $error = err |
| 40 | + error?.set(err) |
23 | 41 | console.log('Item validation error:', err) |
24 | 42 | return |
25 | 43 | } |
26 | | - console.log('Changing item to', item) |
27 | | - Undo.initEdit({ elements: [selected] }) |
| 44 | + console.log('Changing item to', thisItem) |
| 45 | + Undo.initEdit({ elements: [thisSelected] }) |
28 | 46 |
|
29 | | - selected.item = item |
| 47 | + thisSelected.item = thisItem |
30 | 48 | Project!.saved = false |
31 | 49 |
|
32 | | - Undo.finishEdit(`Change Item Display Item to "${item}"`, { |
33 | | - elements: [selected], |
| 50 | + Undo.finishEdit(`Change Item Display Item to "${thisItem}"`, { |
| 51 | + elements: [thisSelected], |
34 | 52 | }) |
35 | 53 | }) |
36 | 54 | .catch(err => { |
37 | | - $error = err.message |
| 55 | + error?.set(err.message) |
38 | 56 | }) |
39 | 57 | } |
40 | | - } |
| 58 | + }) |
41 | 59 |
|
42 | 60 | const mountItemDisplaySelect = (node: HTMLDivElement) => { |
43 | 61 | node.appendChild(ITEM_DISPLAY_ITEM_DISPLAY_SELECT.node) |
44 | 62 | } |
| 63 | +
|
| 64 | + onDestroy(() => { |
| 65 | + unsubs.forEach(u => u()) |
| 66 | + }) |
45 | 67 | </script> |
46 | 68 |
|
47 | | -<p class="panel_toolbar_label label"> |
48 | | - {translate('panel.vanilla_item_display.title')} |
49 | | -</p> |
| 69 | +{#if selected} |
| 70 | + <p class="panel_toolbar_label label"> |
| 71 | + {translate('panel.vanilla_item_display.title')} |
| 72 | + </p> |
50 | 73 |
|
51 | | -<div class="toolbar custom-toolbar" title={translate('panel.vanilla_item_display.description')}> |
52 | | - <div class="content" style="width: 95%;"> |
53 | | - <input type="text" bind:value={item} /> |
| 74 | + <div class="toolbar custom-toolbar" title={translate('panel.vanilla_item_display.description')}> |
| 75 | + <div class="content" style="width: 95%;"> |
| 76 | + <input type="text" bind:value={item} /> |
| 77 | + </div> |
| 78 | + <div class="content" use:mountItemDisplaySelect></div> |
54 | 79 | </div> |
55 | | - <div class="content" use:mountItemDisplaySelect></div> |
56 | | -</div> |
57 | 80 |
|
58 | | -{#if $error} |
59 | | - <div class="error"> |
60 | | - {$error} |
61 | | - </div> |
| 81 | + {#if $error} |
| 82 | + <div class="error"> |
| 83 | + {$error} |
| 84 | + </div> |
| 85 | + {/if} |
62 | 86 | {/if} |
63 | 87 |
|
64 | 88 | <style> |
|
0 commit comments