|
1 | 1 | <script lang="ts" context="module"> |
| 2 | + import { validateItem } from 'src/util/minecraftUtil' |
2 | 3 | import { ITEM_DISPLAY_ITEM_DISPLAY_SELECT } from '../interface/panel/vanillaItemDisplayElement' |
3 | 4 | import { VanillaItemDisplay } from '../outliner/vanillaItemDisplay' |
4 | | - import EVENTS from '../util/events' |
5 | | - import { Valuable } from '../util/stores' |
6 | 5 | import { translate } from '../util/translation' |
7 | 6 | </script> |
8 | 7 |
|
9 | 8 | <script lang="ts"> |
10 | | - let selectedDisplay = VanillaItemDisplay.selected.at(0) |
11 | | - let lastSelected = selectedDisplay |
12 | | -
|
13 | | - let item = new Valuable<string>('') |
14 | | - let error = new Valuable<string>('') |
15 | | - let itemDisplaySlot: HTMLDivElement |
16 | | - let visible = false |
17 | | -
|
18 | | - let unsub: (() => void) | undefined |
19 | | -
|
20 | | - EVENTS.UPDATE_SELECTION.subscribe(() => { |
21 | | - selectedDisplay = VanillaItemDisplay.selected.at(0) |
22 | | - if (!selectedDisplay || selected.length > 1) { |
23 | | - item = new Valuable('') |
24 | | - error = new Valuable('') |
25 | | - visible = false |
26 | | - return |
27 | | - } |
28 | | - $item = selectedDisplay.item |
29 | | - error = selectedDisplay.error |
30 | | - visible = true |
31 | | - }) |
32 | | -
|
33 | | - EVENTS.UPDATE_SELECTION.subscribe(() => { |
34 | | - unsub?.() |
35 | | -
|
36 | | - lastSelected = selectedDisplay |
37 | | - selectedDisplay = VanillaItemDisplay.selected.at(0) |
38 | | -
|
39 | | - if (!selectedDisplay) { |
40 | | - visible = false |
41 | | - return |
| 9 | + export let selected: VanillaItemDisplay |
| 10 | +
|
| 11 | + let item = selected.item |
| 12 | + let error = selected.error |
| 13 | +
|
| 14 | + $: { |
| 15 | + $error = '' |
| 16 | + if (selected.item !== item) { |
| 17 | + void validateItem(item) |
| 18 | + .then(err => { |
| 19 | + if (err) { |
| 20 | + $error = err |
| 21 | + console.log('Item validation error:', err) |
| 22 | + return |
| 23 | + } |
| 24 | + console.log('Changing item to', item) |
| 25 | + Undo.initEdit({ elements: [selected] }) |
| 26 | +
|
| 27 | + selected.item = item |
| 28 | + Project!.saved = false |
| 29 | +
|
| 30 | + Undo.finishEdit(`Change Item Display Item to "${item}"`, { |
| 31 | + elements: [selected], |
| 32 | + }) |
| 33 | + }) |
| 34 | + .catch(err => { |
| 35 | + $error = err.message |
| 36 | + }) |
42 | 37 | } |
| 38 | + } |
43 | 39 |
|
44 | | - $item = selectedDisplay.item |
45 | | - error = selectedDisplay.error |
46 | | - ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set(selectedDisplay.itemDisplay) |
47 | | - visible = true |
48 | | -
|
49 | | - unsub = item.subscribe(value => { |
50 | | - if (selectedDisplay == undefined || selectedDisplay !== lastSelected) { |
51 | | - lastSelected = selectedDisplay |
52 | | - return |
53 | | - } |
54 | | - if (value === selectedDisplay.item) return |
55 | | -
|
56 | | - Undo.initEdit({ elements: VanillaItemDisplay.selected }) |
57 | | -
|
58 | | - if (VanillaItemDisplay.selected.length > 1) { |
59 | | - for (const display of VanillaItemDisplay.selected) { |
60 | | - display.item = value |
61 | | - } |
62 | | - } else { |
63 | | - selectedDisplay.item = value |
64 | | - } |
65 | | - Project!.saved = false |
66 | | -
|
67 | | - Undo.finishEdit(`Change Item Display's Item to "${$item}"`, { |
68 | | - elements: VanillaItemDisplay.selected, |
69 | | - }) |
70 | | - }) |
71 | | - }) |
72 | | -
|
73 | | - requestAnimationFrame(() => { |
74 | | - itemDisplaySlot.appendChild(ITEM_DISPLAY_ITEM_DISPLAY_SELECT.node) |
75 | | - }) |
| 40 | + const mountItemDisplaySelect = (node: HTMLDivElement) => { |
| 41 | + node.appendChild(ITEM_DISPLAY_ITEM_DISPLAY_SELECT.node) |
| 42 | + } |
76 | 43 | </script> |
77 | 44 |
|
78 | | -<p class="panel_toolbar_label label" style={!!visible ? '' : 'visibility:hidden; height: 0px;'}> |
| 45 | +<p class="panel_toolbar_label label"> |
79 | 46 | {translate('panel.vanilla_item_display.title')} |
80 | 47 | </p> |
81 | 48 |
|
82 | | -<div |
83 | | - class="toolbar custom-toolbar" |
84 | | - style={!!visible ? '' : 'visibility:hidden; height: 0px;'} |
85 | | - title={translate('panel.vanilla_item_display.description')} |
86 | | -> |
| 49 | +<div class="toolbar custom-toolbar" title={translate('panel.vanilla_item_display.description')}> |
87 | 50 | <div class="content" style="width: 95%;"> |
88 | | - <input type="text" bind:value={$item} /> |
| 51 | + <input type="text" bind:value={item} /> |
89 | 52 | </div> |
90 | | - <div class="content" bind:this={itemDisplaySlot}></div> |
| 53 | + <div class="content" use:mountItemDisplaySelect></div> |
91 | 54 | </div> |
92 | 55 |
|
93 | | -<div |
94 | | - class="error" |
95 | | - style={!!$error ? '' : 'visibility:hidden; height: 0px; color: var(--color-error);'} |
96 | | -> |
97 | | - {$error} |
98 | | -</div> |
| 56 | +{#if $error} |
| 57 | + <div class="error"> |
| 58 | + {$error} |
| 59 | + </div> |
| 60 | +{/if} |
99 | 61 |
|
100 | 62 | <style> |
101 | 63 | input { |
|
0 commit comments