mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
Don't flatten filtered objects
This commit is contained in:
23
src/app/hooks/canFlatten.ts
Normal file
23
src/app/hooks/canFlatten.ts
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
"target": "es6",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es2017.object",
|
||||
"es2019"
|
||||
],
|
||||
"module": "esnext",
|
||||
|
||||
Reference in New Issue
Block a user