|
2 | 2 | import { BlockDisplay } from '@aj/blockbench-additions/outliner-elements/blockDisplay'
|
3 | 3 | import { ItemDisplay } from '@aj/blockbench-additions/outliner-elements/itemDisplay'
|
4 | 4 | import { TextDisplay } from '@aj/blockbench-additions/outliner-elements/textDisplay'
|
| 5 | + import { CommonDisplayConfig } from '@aj/systems/node-configs' |
5 | 6 | import EVENTS from '@aj/util/events'
|
| 7 | + import { translate } from '@aj/util/translation' |
| 8 | + import { slide } from 'svelte/transition' |
| 9 | + import { MODE_ICONS, type CommonOptionMode } from '.' |
6 | 10 | import TextDisplayPage from './textDisplayPage.svelte'
|
7 | 11 |
|
8 |
| - // Fix for svelte not recognizing global Blockbench types |
9 |
| - // eslint-disable-next-line @typescript-eslint/naming-convention |
10 |
| - const Group = Blockbench.Group |
11 |
| -
|
12 | 12 | let selectedThing: TextDisplay | BlockDisplay | ItemDisplay | Group | undefined
|
| 13 | + let commonConfig: CommonDisplayConfig | undefined |
| 14 | + let commonOptionModes: Map<string, CommonOptionMode> |
| 15 | + let commonTabSelected: boolean |
13 | 16 |
|
14 | 17 | EVENTS.UPDATE_SELECTION.subscribe(() => {
|
| 18 | + selectedThing = undefined |
| 19 | + commonConfig = undefined |
| 20 | + commonOptionModes = new Map() |
| 21 | +
|
15 | 22 | if (Group.first_selected) {
|
16 | 23 | selectedThing = Group.first_selected
|
17 | 24 | } else if (TextDisplay.selected.length > 0) {
|
|
20 | 27 | selectedThing = BlockDisplay.selected.at(0)
|
21 | 28 | } else if (ItemDisplay.selected.length > 0) {
|
22 | 29 | selectedThing = ItemDisplay.selected.at(0)
|
23 |
| - } else { |
24 |
| - selectedThing = undefined |
| 30 | + } |
| 31 | +
|
| 32 | + if (selectedThing) { |
| 33 | + commonConfig = new CommonDisplayConfig().fromJSON(selectedThing?.commonConfig) |
| 34 | + commonOptionModes = new Map<string, CommonOptionMode>( |
| 35 | + commonConfig.keys().map(key => { |
| 36 | + if (commonConfig!.getKeyInheritance(key)) { |
| 37 | + return [key, 'inherit'] |
| 38 | + } else if (commonConfig!.get(key, 'local') != undefined) { |
| 39 | + return [key, 'custom'] |
| 40 | + } |
| 41 | + return [key, 'default'] |
| 42 | + }) |
| 43 | + ) |
25 | 44 | }
|
26 | 45 | })
|
| 46 | +
|
| 47 | + // Key is a string, but I need it to be any to make TypeScript happy when indexing into the config object. |
| 48 | + function cycleCommonMode(key: any) { |
| 49 | + if (!selectedThing) { |
| 50 | + console.error('Attempted to cycle common mode without a selected thing') |
| 51 | + return |
| 52 | + } |
| 53 | + if (!commonConfig || !commonOptionModes) { |
| 54 | + console.error('Attempted to cycle common mode without a common config') |
| 55 | + return |
| 56 | + } |
| 57 | +
|
| 58 | + const mode = commonOptionModes.get(key) |
| 59 | + if (mode === 'default') { |
| 60 | + commonOptionModes.set(key, 'custom') |
| 61 | + commonConfig.makeDefault(key) |
| 62 | + } else if (mode === 'custom') { |
| 63 | + commonOptionModes.set(key, 'inherit') |
| 64 | + commonConfig.set(key, undefined) |
| 65 | + commonConfig.setKeyInheritance(key, true) |
| 66 | + } else { |
| 67 | + commonOptionModes.set(key, 'default') |
| 68 | + commonConfig.set(key, undefined) |
| 69 | + commonConfig.setKeyInheritance(key, false) |
| 70 | + } |
| 71 | +
|
| 72 | + console.log('Set', key, 'inheritance mode to', mode?.toUpperCase()) |
| 73 | + Undo.initEdit({ elements: [selectedThing] }) |
| 74 | + selectedThing.commonConfig = commonConfig.toJSON() |
| 75 | + Undo.finishEdit(`Set ${key} inheritance mode to ${mode?.toUpperCase()}`, { |
| 76 | + elements: [selectedThing], |
| 77 | + }) |
| 78 | +
|
| 79 | + commonConfig = commonConfig |
| 80 | + } |
27 | 81 | </script>
|
28 | 82 |
|
29 |
| -{#if selectedThing instanceof Group} |
30 |
| - <div>{selectedThing.name}</div> |
31 |
| -{:else if selectedThing instanceof TextDisplay} |
32 |
| - <TextDisplayPage textDisplay={selectedThing} /> |
33 |
| -{:else if selectedThing instanceof BlockDisplay} |
34 |
| - <div>{selectedThing.name}</div> |
35 |
| -{:else if selectedThing instanceof ItemDisplay} |
36 |
| - <div>{selectedThing.name}</div> |
37 |
| -{:else} |
38 |
| - <div>Unknown type</div> |
| 83 | +<!-- Make sure that we update the panel every time selected thing is assigned, even if it's the same value. --> |
| 84 | +<!-- This makes certain that we have the most up-to-date properties of the text display from undo / redo events --> |
| 85 | +{#if selectedThing} |
| 86 | + <div class="tab-buttons" in:slide={{ duration: 200 }}> |
| 87 | + <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 88 | + <p |
| 89 | + class={'tab-button' + (!commonTabSelected ? ' tab-button-selected' : '')} |
| 90 | + on:click={() => { |
| 91 | + commonTabSelected = false |
| 92 | + }} |
| 93 | + > |
| 94 | + {translate( |
| 95 | + `panel.display.${selectedThing && Object.getPrototypeOf(selectedThing).constructor.type}.label` |
| 96 | + )} |
| 97 | + </p> |
| 98 | + <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 99 | + <p |
| 100 | + class={'tab-button' + (commonTabSelected ? ' tab-button-selected' : '')} |
| 101 | + on:click={() => { |
| 102 | + commonTabSelected = true |
| 103 | + }} |
| 104 | + > |
| 105 | + {translate('panel.display.common.label')} |
| 106 | + </p> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div class="option-list-container"> |
| 110 | + <div> |
| 111 | + {#if commonTabSelected && commonConfig} |
| 112 | + <ul class="option-list"> |
| 113 | + {#each commonConfig.keys(true) as key} |
| 114 | + <li> |
| 115 | + <div> |
| 116 | + <div class="option-title"> |
| 117 | + {translate(`panel.display.common.options.${key}`)} |
| 118 | + </div> |
| 119 | + <div class="option-mode">{commonOptionModes.get(key)}</div> |
| 120 | + <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 121 | + <i |
| 122 | + on:click={() => cycleCommonMode(key)} |
| 123 | + class="material-icons notranslate icon option-mode-toggle" |
| 124 | + > |
| 125 | + {MODE_ICONS[commonOptionModes.get(key) ?? 'default']} |
| 126 | + </i> |
| 127 | + </div> |
| 128 | + {#if commonOptionModes.get(key) === 'custom'} |
| 129 | + <div class="option-value"> |
| 130 | + <input type="text" value={commonConfig[key]} /> |
| 131 | + </div> |
| 132 | + {/if} |
| 133 | + </li> |
| 134 | + {/each} |
| 135 | + </ul> |
| 136 | + {:else} |
| 137 | + {#key selectedThing} |
| 138 | + <!-- svelte-ignore missing-declaration --> |
| 139 | + {#if selectedThing instanceof Group} |
| 140 | + <!-- <div>{selectedThing.name}</div> --> |
| 141 | + {:else if selectedThing instanceof TextDisplay} |
| 142 | + <TextDisplayPage textDisplay={selectedThing} /> |
| 143 | + {:else if selectedThing instanceof BlockDisplay} |
| 144 | + <!-- <div>{selectedThing.name}</div> --> |
| 145 | + {:else if selectedThing instanceof ItemDisplay} |
| 146 | + <!-- <div>{selectedThing.name}</div> --> |
| 147 | + {:else} |
| 148 | + <div>Selection has no Display Options</div> |
| 149 | + {/if} |
| 150 | + {/key} |
| 151 | + {/if} |
| 152 | + </div> |
| 153 | + </div> |
39 | 154 | {/if}
|
40 | 155 |
|
41 | 156 | <style>
|
| 157 | + :global .panel[id='panel_animated_java:display_panel'] { |
| 158 | + & .toolbar { |
| 159 | + & .bar_select { |
| 160 | + height: 28px; |
| 161 | + margin: 1px 0px; |
| 162 | + } |
| 163 | + & bb-select { |
| 164 | + height: 28px; |
| 165 | + display: flex; |
| 166 | + align-items: center; |
| 167 | + padding-top: 0; |
| 168 | + } |
| 169 | + & .sp-replacer { |
| 170 | + padding: 4px 18px; |
| 171 | + height: 28px; |
| 172 | + margin: 1px 0px; |
| 173 | + } |
| 174 | + } |
| 175 | +
|
| 176 | + & .option-list-container { |
| 177 | + max-height: 20rem; |
| 178 | + overflow-y: auto; |
| 179 | + background-color: var(--color-back); |
| 180 | + } |
| 181 | + & .option-list { |
| 182 | + display: flex; |
| 183 | + flex-direction: column; |
| 184 | + gap: 8px; |
| 185 | + padding: 0 8px; |
| 186 | + margin-bottom: 8px; |
| 187 | + } |
| 188 | + & .option-list > li > div { |
| 189 | + display: flex; |
| 190 | + flex-direction: row; |
| 191 | + align-items: center; |
| 192 | + color: var(--color-subtle_text); |
| 193 | + justify-content: space-between; |
| 194 | + } |
| 195 | + & .option-title { |
| 196 | + text-align: center; |
| 197 | + } |
| 198 | + & .option-mode { |
| 199 | + flex-grow: 1; |
| 200 | + text-align: right; |
| 201 | + margin-right: 4px; |
| 202 | + text-transform: uppercase; |
| 203 | + font-size: 12px; |
| 204 | + } |
| 205 | + & .option-mode-toggle { |
| 206 | + text-align: center; |
| 207 | + color: var(--color-text); |
| 208 | + } |
| 209 | + & .option-mode-toggle:hover { |
| 210 | + color: var(--color-light); |
| 211 | + } |
| 212 | + & .option-value { |
| 213 | + border-left: 2px solid var(--color-accent); |
| 214 | + padding-left: 8px; |
| 215 | + } |
| 216 | +
|
| 217 | + & .tab-buttons { |
| 218 | + display: grid; |
| 219 | + gap: 8px; |
| 220 | + padding: 0 8px; |
| 221 | + grid-template-columns: 1fr 1fr; |
| 222 | + margin-bottom: 8px; |
| 223 | + } |
| 224 | + & .tab-button { |
| 225 | + padding: 4px 8px; |
| 226 | + margin: unset; |
| 227 | + cursor: pointer; |
| 228 | + background-color: var(--color-button); |
| 229 | + border-bottom: none; |
| 230 | + text-align: center; |
| 231 | + } |
| 232 | + & .tab-button-selected { |
| 233 | + border-bottom: 2px solid var(--color-accent); |
| 234 | + } |
| 235 | + } |
42 | 236 | </style>
|
0 commit comments