-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #753 from contentful/next
chore: next to main branch
- Loading branch information
Showing
29 changed files
with
430 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
ComponentPropertyValue, | ||
ExperienceComponentSettings, | ||
} from '@contentful/experiences-validators'; | ||
import { deserializePatternVariables } from './patternUtils'; | ||
|
||
const nodeVariables = { | ||
cfBackgroundColor: { | ||
type: 'ComponentValue', | ||
key: 'cfBackgroundColor_random_key_test_1', | ||
}, | ||
cfGap: { | ||
type: 'ComponentValue', | ||
key: 'cfGap_random_key_test_1', | ||
}, | ||
cfBorder: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: '27px', | ||
}, | ||
}, | ||
} as Record<string, ComponentPropertyValue>; | ||
|
||
const componentInstanceProps = { | ||
cfBackgroundColor_random_key_test_1: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: 'red', | ||
}, | ||
}, | ||
} as Record<string, ComponentPropertyValue>; | ||
|
||
const patternVariableDefinitions = { | ||
cfBackgroundColor_random_key_test_1: { | ||
type: 'Text', | ||
description: 'The background color of the section', | ||
group: 'style', | ||
displayName: 'Background color', | ||
defaultValue: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: 'blue', | ||
}, | ||
}, | ||
}, | ||
cfGap_random_key_test_1: { | ||
type: 'Text', | ||
description: 'The gap between the elements', | ||
group: 'style', | ||
displayName: 'Gap', | ||
defaultValue: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: '27px', | ||
}, | ||
}, | ||
}, | ||
} as ExperienceComponentSettings['variableDefinitions']; | ||
|
||
describe('deserializePatternVariables', () => { | ||
it('should return the correct values', () => { | ||
const result = deserializePatternVariables({ | ||
nodeVariables, | ||
componentInstanceProps, | ||
componentInstanceUnboundValues: {}, | ||
componentInstanceDataSource: {}, | ||
patternVariableDefinitions, | ||
}); | ||
|
||
expect(result).toEqual({ | ||
childNodeVariable: { | ||
cfBackgroundColor: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: 'red', // got the value from componentInstanceProps | ||
}, | ||
}, | ||
cfGap: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: '27px', // copied over default value | ||
}, | ||
}, | ||
cfBorder: { | ||
type: 'DesignValue', | ||
valuesByBreakpoint: { | ||
desktop: '27px', | ||
}, | ||
}, | ||
}, | ||
dataSource: {}, | ||
unboundValues: {}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { | ||
ComponentPropertyValue, | ||
ExperienceDataSource, | ||
ExperienceUnboundValues, | ||
ExperienceComponentSettings, | ||
DesignValue, | ||
} from '../types'; | ||
|
||
export const deserializePatternVariables = ({ | ||
nodeVariables, | ||
componentInstanceProps, | ||
componentInstanceUnboundValues, | ||
componentInstanceDataSource, | ||
patternVariableDefinitions, | ||
}: { | ||
nodeVariables: Record<string, ComponentPropertyValue>; | ||
componentInstanceProps: Record<string, ComponentPropertyValue>; | ||
componentInstanceUnboundValues: ExperienceUnboundValues; | ||
componentInstanceDataSource: ExperienceDataSource; | ||
patternVariableDefinitions?: ExperienceComponentSettings['variableDefinitions']; | ||
}): { | ||
childNodeVariable: Record<string, ComponentPropertyValue>; | ||
dataSource: ExperienceDataSource; | ||
unboundValues: ExperienceUnboundValues; | ||
} => { | ||
const childNodeVariable: Record<string, ComponentPropertyValue> = {}; | ||
const dataSource: ExperienceDataSource = {}; | ||
const unboundValues: ExperienceUnboundValues = {}; | ||
|
||
for (const [variableName, variable] of Object.entries(nodeVariables)) { | ||
childNodeVariable[variableName] = variable; | ||
if (variable.type === 'ComponentValue') { | ||
const componentValueKey = variable.key; | ||
const instanceProperty = componentInstanceProps[componentValueKey]; | ||
const variableDefinition = patternVariableDefinitions?.[componentValueKey]; | ||
const defaultValue = variableDefinition?.defaultValue; | ||
|
||
// For pattern, we look up the value in the pattern instance and | ||
// replace the componentValue with that one. | ||
if (instanceProperty?.type === 'UnboundValue') { | ||
const componentInstanceValue = componentInstanceUnboundValues[instanceProperty.key]; | ||
unboundValues[instanceProperty.key] = componentInstanceValue; | ||
childNodeVariable[variableName] = instanceProperty; | ||
} else if (instanceProperty?.type === 'BoundValue') { | ||
const [, dataSourceKey] = instanceProperty.path.split('/'); | ||
const componentInstanceValue = componentInstanceDataSource[dataSourceKey]; | ||
dataSource[dataSourceKey] = componentInstanceValue; | ||
childNodeVariable[variableName] = instanceProperty; | ||
} else if (instanceProperty?.type === 'HyperlinkValue') { | ||
const componentInstanceValue = componentInstanceDataSource[instanceProperty.linkTargetKey]; | ||
dataSource[instanceProperty.linkTargetKey] == componentInstanceValue; | ||
childNodeVariable[variableName] = instanceProperty; | ||
} else if (instanceProperty?.type === 'DesignValue') { | ||
childNodeVariable[variableName] = instanceProperty; | ||
} else if (!instanceProperty && defaultValue) { | ||
// So far, we only automatically fallback to the defaultValue for design properties | ||
if (variableDefinition.group === 'style') { | ||
childNodeVariable[variableName] = defaultValue as DesignValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { childNodeVariable, dataSource, unboundValues }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.