ultrawidify/src/common/components/JsonEditor/JsonElement.vue

92 lines
2.2 KiB
Vue
Raw Normal View History

2020-03-11 00:15:53 +01:00
<template>
2020-03-11 23:12:18 +01:00
<div class="flex flex-row json-level-indent">
<div>
<b>
<span v-if="label" class="item-key"
:class="{'item-key-boolean-false': typeof value_internal === 'boolean' && !value_internal}"
>
"{{label}}"
</span>
:&nbsp;
2020-03-11 23:12:18 +01:00
</b>
2020-03-11 00:15:53 +01:00
</div>
2020-03-11 23:12:18 +01:00
<div v-if="typeof value_internal === 'boolean'"
:class="{'json-value-boolean-true': value_internal, 'json-value-boolean-false': !value_internal}"
2020-03-11 00:15:53 +01:00
@click="toggleBoolean"
>
2020-03-11 23:12:18 +01:00
<template v-if="value_internal">true</template>
2020-03-11 00:15:53 +01:00
<template v-else>false</template>
</div>
<div v-else
class="flex flex-row inline-edit"
:class="{
2020-03-11 23:12:18 +01:00
'json-value-number': typeof value_internal === 'number',
'json-value-string': typeof value_internal === 'string'
2020-03-11 00:15:53 +01:00
}"
>
<template v-if="typeof value_internal === 'string'">
"<div ref="element-edit-area"
contenteditable="true"
@keydown.enter="changeValue"
2020-03-11 00:15:53 +01:00
>
2020-03-11 23:12:18 +01:00
{{value_internal}}
</div>"
2020-03-11 00:15:53 +01:00
</template>
<template v-else>
<div ref="element-edit-area"
contenteditable="true"
@keydown.enter="changeValue"
>
2020-03-11 23:12:18 +01:00
{{value_internal}}
</div>
</template>,
2020-03-11 00:15:53 +01:00
</div>
</div>
</template>
<script>
export default {
name: 'json-element',
props: [
'value',
2020-03-11 23:12:18 +01:00
'label',
2020-03-11 00:15:53 +01:00
],
data() {
return {
2020-03-11 23:12:18 +01:00
value_internal: undefined,
2020-03-11 00:15:53 +01:00
editing: false,
}
},
2020-03-11 23:12:18 +01:00
created() {
this.value_internal = this.value;
},
2020-03-11 00:15:53 +01:00
watch: {
value: function(val) {
2020-03-11 23:12:18 +01:00
this.value_internal = val;
2020-03-11 00:15:53 +01:00
}
},
methods: {
toggleBoolean() {
2020-03-11 23:12:18 +01:00
this.value_internal = !this.value_internal;
this.$emit('change', this.value_internal);
2020-03-11 00:15:53 +01:00
},
changeValue() {
this.$refs['element-edit-area'].blur();
const newValue = this.$refs['element-edit-area'].textContent.replace(/\n/g, '');
2020-03-11 00:15:53 +01:00
if (isNaN(newValue)) {
2020-03-11 23:12:18 +01:00
this.value_internal = newValue;
2020-03-11 00:15:53 +01:00
this.$emit('change', newValue);
} else {
this.value_internal = +newValue;
2020-03-11 00:15:53 +01:00
this.$emit('change', +newValue);
}
this.editing = false;
}
}
}
2020-03-11 23:12:18 +01:00
</script>
<style lang="scss" scoped>
@import url('./json.scss');
</style>