Fix not being able to change feature type

This commit is contained in:
Misode
2021-09-29 00:30:47 +02:00
parent e5d2c02fb3
commit a0b7cb1c81
+21 -13
View File
@@ -67,7 +67,10 @@ const renderHtml: RenderHook = {
const choiceContextPath = config?.choiceContext ? new Path([], [config.choiceContext]) : config?.context ? new Path([], [config.context]) : path const choiceContextPath = config?.choiceContext ? new Path([], [config.choiceContext]) : config?.context ? new Path([], [config.context]) : path
const set = (value: string) => { const set = (value: string) => {
const c = choices.find(c => c.type === value) ?? choice const c = choices.find(c => c.type === value) ?? choice
path.model.set(path, c.change ? c.change(value, { wrapLists: true }) : DataModel.wrapLists(c.node.default())) const newValue = c.change
? c.change(value, { wrapLists: true })
: DataModel.wrapLists(config.choiceContext === 'feature' ? c.node.default()?.config?.feature : c.node.default())
path.model.set(path, newValue)
} }
const inject = <select value={choice.type} onChange={(e) => set((e.target as HTMLSelectElement).value)}> const inject = <select value={choice.type} onChange={(e) => set((e.target as HTMLSelectElement).value)}>
{choices.map(c => <option value={c.type}> {choices.map(c => <option value={c.type}>
@@ -514,11 +517,12 @@ function createDecoratorsWrapper(originalFields: NodeChildren, path: ModelPath,
} }
const schema = ObjectNode(fields, { context: 'feature' }) const schema = ObjectNode(fields, { context: 'feature' })
const featurePath = new Path(['config', 'feature']) const featurePath = new Path(['config', 'feature'])
const decoratorsPath = new Path(['config', 'decorators'])
const model = path.getModel() const model = path.getModel()
const wrapper: ModelWrapper = new ModelWrapper(schema, path => { const wrapper: ModelWrapper = new ModelWrapper(schema, path => {
if (path.startsWith(featurePath)) { if (path.startsWith(featurePath)) {
return new Path([...[...Array(decorators.length - 1)].flatMap(() => ['config', 'feature']), ...path.modelArr]) return new Path([...[...Array(decorators.length - 1)].flatMap(() => ['config', 'feature']), ...path.modelArr])
} else if (path.startsWith(new Path(['config', 'decorators']))) { } else if (path.startsWith(decoratorsPath)) {
if (path.modelArr.length === 2) { if (path.modelArr.length === 2) {
return new Path([]) return new Path([])
} }
@@ -529,34 +533,38 @@ function createDecoratorsWrapper(originalFields: NodeChildren, path: ModelPath,
} }
return path return path
}, path => { }, path => {
if (path.equals(new Path(['config', 'decorators']))) { if (path.equals(decoratorsPath)) {
const decorators: any[] = [] const newDecorators: any[] = []
iterateNestedDecorators(model.data, decorators) iterateNestedDecorators(model.data, newDecorators)
return decorators return newDecorators
} }
return model.get(wrapper.map(path)) return model.get(wrapper.map(path))
}, (path, value, silent) => { }, (path, value, silent) => {
if (path.startsWith(featurePath)) { if (path.startsWith(featurePath)) {
model.set(new Path([...[...Array(decorators.length - 1)].flatMap(() => ['config', 'feature']), ...path.modelArr]), value, silent) const newDecorators: any[] = []
} else if (path.startsWith(new Path(['config', 'decorators']))) { iterateNestedDecorators(model.data, newDecorators)
const newPath =new Path([...[...Array(newDecorators.length - 1)].flatMap(() => ['config', 'feature']), ...path.modelArr])
return model.set(newPath, value, silent)
} else if (path.startsWith(decoratorsPath)) {
const index = path.modelArr[2] const index = path.modelArr[2]
if (path.modelArr.length === 2) { if (path.modelArr.length === 2) {
const feature = wrapper.get(featurePath) const feature = wrapper.get(featurePath)
model.set(new Path(), produceNestedDecorators(feature, value)) return model.set(new Path(), produceNestedDecorators(feature, value), silent)
} else if (typeof index === 'number') { } else if (typeof index === 'number') {
if (path.modelArr.length === 3 && value === undefined) { if (path.modelArr.length === 3 && value === undefined) {
const feature = wrapper.get(featurePath) const feature = wrapper.get(featurePath)
const newDecorators = [...decorators] const newDecorators: any[] = []
iterateNestedDecorators(model.data, newDecorators)
newDecorators.splice(index, 1) newDecorators.splice(index, 1)
const newValue = produceNestedDecorators(feature, newDecorators) const newValue = produceNestedDecorators(feature, newDecorators)
model.set(new Path(), newValue) return model.set(new Path(), newValue, silent)
} else { } else {
const newPath = new Path([...[...Array(index)].flatMap(() => ['config', 'feature']), 'config', 'decorator', ...path.modelArr.slice(3)]) const newPath = new Path([...[...Array(index)].flatMap(() => ['config', 'feature']), 'config', 'decorator', ...path.modelArr.slice(3)])
model.set(newPath, value, silent) return model.set(newPath, value, silent)
} }
} }
} }
return path model.set(path, value, silent)
}) })
wrapper.data = { wrapper.data = {
type: model.data.type, type: model.data.type,