From 67e32b30e64da495603e3ae4bcb96ad4f327f38e Mon Sep 17 00:00:00 2001 From: Misode Date: Wed, 25 Sep 2019 22:04:21 +0200 Subject: [PATCH] Add map component type --- index.html | 11 +++++++++++ model.js | 35 +++++++++++++++++++++++++++++++++++ schemas/1.14.json | 27 +++++++++++++++++++++++++++ view.js | 17 +++++++++++++++++ 4 files changed, 90 insertions(+) diff --git a/index.html b/index.html index 810d6812..1bfb8682 100644 --- a/index.html +++ b/index.html @@ -183,6 +183,17 @@ +
+
+
+ +
+ +
+ +
+
+
diff --git a/model.js b/model.js index 93fe990c..15d2d60e 100644 --- a/model.js +++ b/model.js @@ -226,6 +226,41 @@ function removeFromSet(el, array) { } } +function addToMap(el) { + let node = getParent(el); + let $field = $(el).closest('[data-index]'); + let key = $field.find('input').val(); + let map = $field.attr('data-index'); + let type = $field.attr('data-item-type'); + if (key.length === 0) { + return; + } + if (!node[map]) { + node[map] = {}; + } + if (type === 'int' || type === 'float' || type === 'random' || type === 'range' || type === 'boundary') { + node[map][key] = 0; + } else if (type === 'boolean') { + node[map][key] = false; + } else { + node[map][key] = ""; + } + invalidated(); +} + +function removeFromMap(el) { + let path = getPath(el); + let key = path.pop(); + let node = getNode(path); + delete node[key]; + if (Object.keys(node).length === 0) { + let field = path.pop(); + let parent = getNode(path); + delete parent[field]; + } + invalidated(); +} + function toggleCollapseObject(el) { let path = getPath(el); let index = path.pop(); diff --git a/schemas/1.14.json b/schemas/1.14.json index 03f9a192..10f4bc06 100644 --- a/schemas/1.14.json +++ b/schemas/1.14.json @@ -442,6 +442,33 @@ "minecraft:weather_check" ] }, + { + "id": "block", + "type": "string", + "require": [ + "minecraft:block_state_propery" + ] + }, + { + "id": "properties", + "type": "map", + "values": { + "type": "string" + }, + "require": [ + "minecraft:block_state_propery" + ] + }, + { + "id": "scores", + "type": "map", + "values": { + "type": "range" + }, + "require": [ + "minecraft:entity_scores" + ] + }, { "id": "predicate", "type": "object", diff --git a/view.js b/view.js index b5ff6cfe..ea9ac43b 100644 --- a/view.js +++ b/view.js @@ -65,6 +65,7 @@ function generateComponent(data, struct) { case 'boundary': return generateBoundary(data, struct); case 'enum': return generateEnum(data, struct); case 'set': return generateSet(data, struct); + case 'map': return generateMap(data, struct); case 'json': return generateJson(data, struct); case 'json-list': return generateJsonList(data, struct); case 'nbt': return generateNbt(data, struct); @@ -192,6 +193,22 @@ function generateSet(data, struct) { return $el; } +function generateMap(data, struct) { + let $el = $('#components').find('[data-type="map"]').clone(); + $el.attr('data-index', struct.id).attr('data-item-type', struct.values.type); + $el.find('[data-name="1"]').attr('data-i18n', struct.id); + $el.find('[data-name="2"]').attr('data-i18n', 'add_' + struct.id); + $el.find('input').keypress((e) => {if (e.which == 13) addToMap(e.target);}); + if (data) { + for (let key of Object.keys(data)) { + let $item = generateComponent(data[key], {id: key, type: struct.values.type}); + $item.append('
'); + $el.append($item); + } + } + return $el; +} + function setValueAndName($el, value, source) { let option = value.split(':').slice(-1); let name = (source) ? source + '.' + option : option;