|
| 1 | +import type * as Schema from "./@types/custom_connected_block_template"; |
1 | 2 |
|
2 | 3 | abstract class AbstractField { |
3 | 4 | private eventName: string; |
@@ -255,6 +256,44 @@ const Data = { |
255 | 256 | showPatternsField: new CheckBoxField("Showing: Face Tags", "Showing: Patterns"), |
256 | 257 | showFloorGridField: new CheckBoxField("Disable Floor Grid", "Enable Floor Grid"), |
257 | 258 | patternIndexField: new NumberField("Pattern Index") |
| 259 | + }, |
| 260 | + updateJson: function() { |
| 261 | + let display = document.getElementById("json_display"); |
| 262 | + let shape = Data.controls.shapesField.getPickedOption(); |
| 263 | + let patternIndex = Data.controls.patternIndexField.getValue(); |
| 264 | + |
| 265 | + if (Data.cachedJson.obj && display) { |
| 266 | + let json = Data.cachedJson.obj as Schema.CustomConnectedBlockTemplateAsset; |
| 267 | + let shapeObj = json.Shapes![shape]; |
| 268 | + |
| 269 | + // Collect patterns in a new array, to avoid parasiting the original |
| 270 | + let patternList: Schema.Pattern[] = []; |
| 271 | + if (shapeObj.PatternsToMatchAnyOf) { |
| 272 | + patternList.push(...shapeObj.PatternsToMatchAnyOf); |
| 273 | + } |
| 274 | + |
| 275 | + // Concatenate all patterns into a HTML string, to be re-appended in place of the unstyled block. |
| 276 | + let patternStr: string = ""; |
| 277 | + for (let i = 0; i < patternList.length; i++) { |
| 278 | + let clazz = "pattern-str"; |
| 279 | + let comma = ","; |
| 280 | + |
| 281 | + if (i == patternIndex) clazz += " selected"; |
| 282 | + if (i == patternList.length - 1) comma = ""; |
| 283 | + |
| 284 | + patternStr += `<i class="${clazz}"> ${JSON.stringify(patternList[i], null, 4)}${comma}</i>`; |
| 285 | + } |
| 286 | + |
| 287 | + // Stringify original and split patterns off |
| 288 | + let shapeStr: string = JSON.stringify(shapeObj, null, 4); |
| 289 | + let split = shapeStr.split("\"PatternsToMatchAnyOf\""); |
| 290 | + |
| 291 | + // Make strings more HTML-friendly, and add back leading spaces for patterns |
| 292 | + let splitStr = split[0].replaceAll("\n", "<br>"); |
| 293 | + patternStr = patternStr.replaceAll("\n", "<br> "); |
| 294 | + |
| 295 | + display.innerHTML = `${splitStr}"PatternsToMatchAnyOf": [<br>${patternStr} ]<br>}`; |
| 296 | + } |
258 | 297 | } |
259 | 298 | } |
260 | 299 |
|
@@ -286,17 +325,8 @@ function initControls() { |
286 | 325 | }) |
287 | 326 |
|
288 | 327 | // Read shapes trigger |
289 | | - Data.controls.shapesField.addChangedListener(() => { |
290 | | - let shape = Data.controls.shapesField.getPickedOption(); |
291 | | - |
292 | | - if (Data.cachedJson.obj) { |
293 | | - let shapeData = Data.cachedJson.obj["Shapes"] as any; |
294 | | - let shapeObj = shapeData[shape]; |
295 | | - let shapeStr = JSON.stringify(shapeObj, null, 4); |
296 | | - |
297 | | - jsonDisplay.textContent = shapeStr; |
298 | | - } |
299 | | - }) |
| 328 | + Data.controls.shapesField.addChangedListener(Data.updateJson); |
| 329 | + Data.controls.patternIndexField.addChangedListener(Data.updateJson); |
300 | 330 |
|
301 | 331 | // Add fields to panel |
302 | 332 | let fields = []; |
|
0 commit comments