diff --git a/.eslintrc.js b/.eslintrc.js index 2d63962..2f3f6d8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,5 +2,6 @@ module.exports = { extends: 'guo/vue', rules: { 'valid-jsdoc': 'off', + 'array-bracket-spacing': 'off' } }; diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..88fb4c8 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,6 @@ +// .prettierrc.js +module.exports = { + printWidth: 120, + singleQuote: true, + trailingComma: "es5", +}; diff --git a/example/App.vue b/example/App.vue index 8607d4e..7d4c9ee 100644 --- a/example/App.vue +++ b/example/App.vue @@ -1,4 +1,3 @@ - diff --git a/src/JsonEditor.vue b/src/JsonEditor.vue index 014003f..4569272 100644 --- a/src/JsonEditor.vue +++ b/src/JsonEditor.vue @@ -1,385 +1,416 @@ diff --git a/src/parser.js b/src/parser.js index eba95f5..bc0993c 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,13 +1,11 @@ 'use strict'; const { initChild, getChild } = require('./utils'); -const ARRAY_KEYWORDS = [ 'anyOf', 'oneOf', 'enum' ]; +const ARRAY_KEYWORDS = ['anyOf', 'oneOf', 'enum']; const setCommonFields = (schema, field, schemaName) => { // eslint-disable-next-line no-nested-ternary - field.value = schema.hasOwnProperty('default') - ? schema.default - : field.hasOwnProperty('value') ? field.value : ''; + field.value = schema.hasOwnProperty('default') ? schema.default : field.hasOwnProperty('value') ? field.value : ''; field.component = schema.component; field.schemaType = schema.type; @@ -23,12 +21,11 @@ const setFormValue = (vm, field) => { const vmValue = getChild(vm.value, ns); if (vm.value && !vmValue) { const n = ns.pop(); - const ret = (ns.length > 0 ? initChild(vm.value, ns) : vm.value); + const ret = ns.length > 0 ? initChild(vm.value, ns) : vm.value; vm.$set(ret, n, field.value); } }; - export const parseBoolean = (vm, schema, schemaName) => { const field = schema.attrs || {}; @@ -104,8 +101,8 @@ export const parseString = (vm, schema, schemaName) => { return field; }; -export const parseItems = (items) => { - return items.map((item) => { +export const parseItems = items => { + return items.map(item => { if (typeof item !== 'object') { return { value: item, label: item }; } @@ -144,11 +141,10 @@ export const parseArray = (vm, schema, schemaName) => { field.value = field.value || []; field.items = parseItems(schema[keyword]); break; - } } } - if(!field.type) { + if (!field.type) { field.type = schema.type; field.value = field.value || []; field.items = []; @@ -180,14 +176,19 @@ export const loadFields = (vm, schema, fields = vm.fields, sub) => { } } } - if(schema.name && !fields[schemaName]) { + if (schema.name && !fields[schemaName]) { fields[schemaName] = { $sub: true, $title: schema.title, $description: schema.description, }; } - loadFields(vm, schema.properties[key], schema.name ? fields[schemaName] : undefined, sub ? [ ...sub, key ] : [ key ]); + loadFields( + vm, + schema.properties[key], + schema.name ? fields[schemaName] : undefined, + sub ? [...sub, key] : [key] + ); } break; @@ -217,5 +218,4 @@ export const loadFields = (vm, schema, fields = vm.fields, sub) => { default: fields[schemaName] = parseString(vm, schema, schemaName); } - }; diff --git a/src/utils.js b/src/utils.js index fcea363..fa97fd4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -42,7 +42,7 @@ export function initChild(data, ns) { const n = ns[i]; const ret = getExtendibleLeaf(obj, n, true); if (ret === undefined) { - throw new TypeError('fail to init because namespace ' + ns.join('.') + ' = ' + obj + '(' + (typeof obj) + ')'); + throw new TypeError('fail to init because namespace ' + ns.join('.') + ' = ' + obj + '(' + typeof obj + ')'); } parent = obj; obj = ret; @@ -57,7 +57,7 @@ export function setVal(data, n, v) { const ns = Array.isArray(n) ? n : n.split('.'); // eslint-disable-next-line n = ns.pop(); - const ret = (ns.length > 0 ? initChild(data, ns) : data); + const ret = ns.length > 0 ? initChild(data, ns) : data; ret[n] = v; return v; }