Don't flatten filtered objects

This commit is contained in:
Misode
2021-01-16 14:05:00 +01:00
parent c3914d46fb
commit 8e7668b3b9
4 changed files with 28 additions and 3 deletions

View File

@@ -0,0 +1,23 @@
import { Hook } from '@mcschema/core'
import { getFilterKey } from './getFilterKey'
export const canFlatten: Hook<[], boolean> = {
base: () => false,
boolean: () => false,
choice: () => false,
list: () => false,
map: () => false,
number: () => false,
string: () => false,
object({ node, getActiveFields }, path) {
const filterKey = path.modelArr.length === 0 ? null : node.hook(getFilterKey, path, path)
const visibleEntries = Object.entries(getActiveFields(path))
.filter(([k, v]) => filterKey !== k && v.enabled(path))
if (visibleEntries.length !== 1) return false
const nestedPath = path.push(visibleEntries[0][0])
if (visibleEntries[0][1].type(nestedPath) !== 'object') return false
return visibleEntries[0][1].hook(getFilterKey, nestedPath, nestedPath) === null
}
}

View File

@@ -1,5 +1,5 @@
import { Errors, Hook, relativePath } from '@mcschema/core'
import { App, BlockStateRegistry } from '../App'
import { App } from '../App'
import { getFilterKey } from './getFilterKey'
import { walk } from './walk'

View File

@@ -4,8 +4,9 @@ import { Mounter } from '../views/View'
import { hexId, htmlEncode } from '../Utils'
import { suffixInjector } from './suffixInjector'
import { Octicon } from '../components/Octicon'
import { App, BlockStateRegistry } from '../App'
import { App } from '../App'
import { getFilterKey } from './getFilterKey'
import { canFlatten } from './canFlatten'
/**
* Secondary model used to remember the keys of a map
@@ -189,7 +190,7 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
: activeKeys)
.filter(k => filterKey !== k)
.filter(k => activeFields[k].enabled(path))
if (visibleKeys.length === 1 && activeFields[visibleKeys[0]].type(path.push(visibleKeys[0])) === 'object') {
if (node.hook(canFlatten, path)) {
const newValue = value[visibleKeys[0]] ?? {}
body = activeFields[visibleKeys[0]].hook(this, path.push(visibleKeys[0]), newValue, mounter)[2]
} else {

View File

@@ -3,6 +3,7 @@
"target": "es6",
"lib": [
"dom",
"es2017.object",
"es2019"
],
"module": "esnext",