Skip to content

Commit

Permalink
#590 - Fix for image fields inside a sub-block
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jun 20, 2023
1 parent 91d6d35 commit b799b46
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [#567](https://github.com/estruyf/vscode-front-matter/issues/567): Fix taxonomy filters that are incorrectly positioned
- [#572](https://github.com/estruyf/vscode-front-matter/issues/572): Fix the media snippet placeholder link
- [#577](https://github.com/estruyf/vscode-front-matter/issues/577): Fix in the `dataFile` field where data entries get overwritten
- [#590](https://github.com/estruyf/vscode-front-matter/issues/590): Fix for image fields inside a sub-block

## [8.4.0] - 2023-04-03 - [Release notes](https://beta.frontmatter.codes/updates/v8.4.0)

Expand Down
17 changes: 14 additions & 3 deletions src/listeners/panel/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ export class DataListener extends BaseListener {
let parentObj = data;
let allParents = Object.assign([], parents);
const contentType = ArticleHelper.getContentType(article.data);
const selectedIndexes =
typeof blockData?.selectedIndex === 'string'
? blockData?.selectedIndex.split('.').map((v) => parseInt(v))
: [blockData?.selectedIndex];
let lastSelectedIndex: number | undefined;

// Add support for block fields
if (blockData?.parentFields) {
Expand All @@ -272,13 +277,19 @@ export class DataListener extends BaseListener {

parentObj = parentObj[parent];
crntField = contentType.fields.find((f) => f.name === parent);

// Check if current field is an array
if (parentObj instanceof Array) {
lastSelectedIndex = selectedIndexes.shift();
if (typeof lastSelectedIndex !== 'undefined') {
parentObj = parentObj[lastSelectedIndex];
}
}
}

// Fetches the current block
if (blockData && crntField && crntField.type === 'block') {
if (typeof blockData.selectedIndex !== 'undefined') {
parentObj = parentObj[blockData.selectedIndex];
} else {
if (typeof lastSelectedIndex === 'undefined') {
parentObj.push({
fieldGroup: blockData.blockType
});
Expand Down
2 changes: 1 addition & 1 deletion src/models/BlockFieldData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface BlockFieldData {
parentFields: string[] | undefined;
blockType: string | undefined;
selectedIndex: number | undefined;
selectedIndex: number | string | undefined;
}
22 changes: 20 additions & 2 deletions src/panelWebView/components/DataBlock/DataBlockField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IDataBlockFieldProps {
field: Field;
parentFields: string[];
value: any;
blockData: BlockFieldData | undefined;
filePath: string;
fieldsRenderer: (
ctFields: Field[],
Expand All @@ -37,6 +38,7 @@ export const DataBlockField: React.FunctionComponent<IDataBlockFieldProps> = ({
field,
parentFields = [],
value,
blockData,
fieldsRenderer,
onSubmit,
parentBlock,
Expand All @@ -58,6 +60,7 @@ export const DataBlockField: React.FunctionComponent<IDataBlockFieldProps> = ({

const onFieldUpdate = useCallback(
(crntField: string | undefined, crntValue: any, parents: string[]) => {
debugger;
const dataClone: any[] = Object.assign([], value);

if (!crntField) {
Expand Down Expand Up @@ -91,7 +94,7 @@ export const DataBlockField: React.FunctionComponent<IDataBlockFieldProps> = ({
// Delete the field group to have it added at the end
delete data['fieldGroup'];

if (selectedIndex !== null && selectedIndex !== undefined) {
if (selectedIndex !== null && selectedIndex !== undefined && dataClone.length > 0) {
dataClone[selectedIndex] = {
...data,
fieldGroup: selectedGroup?.id
Expand Down Expand Up @@ -238,6 +241,21 @@ export const DataBlockField: React.FunctionComponent<IDataBlockFieldProps> = ({
[SELECTION_STATE_KEY]
);

const getSelectedIndex = useCallback(() => {
console.log(blockData)
let crntValue = [];

if (blockData?.selectedIndex !== null && blockData?.selectedIndex !== undefined) {
crntValue.push(blockData.selectedIndex);
}

if (selectedIndex !== null && selectedIndex !== undefined) {
crntValue.push(selectedIndex);
}

return crntValue.join('.');
}, [blockData, selectedIndex]);

useEffect(() => {
if (selectedIndex !== null) {
const fieldData = value[selectedIndex];
Expand Down Expand Up @@ -293,7 +311,7 @@ export const DataBlockField: React.FunctionComponent<IDataBlockFieldProps> = ({
{
parentFields: [...parentFields, field.name],
blockType: selectedGroup?.id || undefined,
selectedIndex: selectedIndex === null ? undefined : selectedIndex
selectedIndex: getSelectedIndex()
},
onFieldUpdate,
`${field.name}-${selectedGroup?.id}-${selectedIndex || 0}`
Expand Down
9 changes: 4 additions & 5 deletions src/panelWebView/components/Fields/PreviewImageField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ export const PreviewImageField: React.FunctionComponent<IPreviewImageFieldProps>
<FieldTitle label={label} icon={<PhotographIcon />} required={required} />

<div
className={`metadata_field__preview_image ${
multiple && value && (value as PreviewImageValue[]).length > 0
? `metadata_field__multiple_images`
: ''
} ${showRequiredState ? 'required' : ''}`}
className={`metadata_field__preview_image ${multiple && value && (value as PreviewImageValue[]).length > 0
? `metadata_field__multiple_images`
: ''
} ${showRequiredState ? 'required' : ''}`}
>
{(!value || isFaultyImage || multiple) && (
<button
Expand Down
5 changes: 3 additions & 2 deletions src/panelWebView/components/Fields/WrapperField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
</FieldBoundary>
);
} else if (field.type === 'block') {
const blockData = Object.assign([], parent[field.name]);
const fieldData = Object.assign([], parent[field.name]);

return (
<FieldBoundary key={field.name} fieldName={field.title || field.name}>
Expand All @@ -437,7 +437,8 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
description={field.description}
settings={settings}
field={field}
value={blockData}
value={fieldData}
blockData={blockData}
fieldsRenderer={renderFields}
parentFields={parentFields}
filePath={metadata.filePath as string}
Expand Down

0 comments on commit b799b46

Please sign in to comment.