Skip to content

Commit

Permalink
#273 - Allow single value arrays to be set as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Oct 24, 2023
1 parent 7c2a596 commit 96c496c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### 🎨 Enhancements

- [#273](https://github.com/estruyf/vscode-front-matter/issues/273): Allow single value arrays to be set as a string with the `singleValueAsString` field property
- [#686](https://github.com/estruyf/vscode-front-matter/issues/686): Allow script authors to ask questions during script execution
- [#688](https://github.com/estruyf/vscode-front-matter/issues/688): Allow to show the scheduled articles in the content dashboard (filter and group)

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,11 @@
"default": 0,
"description": "%setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.taxonomyLimit.description%"
},
"singleValueAsString": {
"type": "boolean",
"default": false,
"description": "%setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.singleValueAsString.description%"
},
"isPublishDate": {
"type": "boolean",
"default": false,
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.numberOptions.properties.max.description": "The maximum value",
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.numberOptions.properties.step.description": "The step value",
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.taxonomyLimit.description": "Limit the number of taxonomies to select. Set to 0 to allow unlimited.",
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.singleValueAsString.description": "Specify if you want to store a single array value as a string instead of an array.",
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.isPublishDate.description": "Specify if the field is the publish date field",
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.isModifiedDate.description": "Specify if the field is the modified date field",
"setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.dataFileId.description": "Specify the ID of the data file to use for this field",
Expand Down
28 changes: 26 additions & 2 deletions src/listeners/panel/TaxonomyListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class TaxonomyListener extends BaseListener {
msg.payload?.fieldName,
msg.payload?.values || [],
msg.payload?.parents || [],
typeof msg.payload?.renderAsString !== 'undefined' ? msg.payload?.renderAsString : false,
msg.payload?.blockData
);
break;
Expand All @@ -36,6 +37,7 @@ export class TaxonomyListener extends BaseListener {
msg.payload?.fieldName,
msg.payload?.values || [],
msg.payload?.parents || [],
typeof msg.payload?.renderAsString !== 'undefined' ? msg.payload?.renderAsString : false,
msg.payload?.blockData
);
break;
Expand Down Expand Up @@ -134,6 +136,7 @@ export class TaxonomyListener extends BaseListener {
fieldName: string,
values: string[],
parents: string[],
renderAsString: boolean,
blockData?: BlockFieldData
) {
const editor = window.activeTextEditor;
Expand All @@ -145,7 +148,17 @@ export class TaxonomyListener extends BaseListener {
if (article && article.data) {
const parentObj = DataListener.getParentObject(article.data, article, parents, blockData);

parentObj[fieldName] = values || [];
if (renderAsString) {
if (values.length === 0) {
parentObj[fieldName] = '';
} else if (values.length === 1) {
parentObj[fieldName] = values[0];
} else {
parentObj[fieldName] = values || [];
}
} else {
parentObj[fieldName] = values || [];
}
ArticleHelper.update(editor, article);
DataListener.pushMetadata(article!.data);
}
Expand Down Expand Up @@ -174,7 +187,18 @@ export class TaxonomyListener extends BaseListener {
data.blockData
);

parentObj[data.name] = data.options || [];
if (data.renderAsString) {
if (!data.options || data.options.length === 0) {
parentObj[data.name] = '';
} else if (data.options.length === 1) {
parentObj[data.name] = data.options[0];
} else {
parentObj[data.name] = data.options || [] || [];
}
} else {
parentObj[data.name] = data.options || [];
}

ArticleHelper.update(editor, article);
DataListener.pushMetadata(article!.data);
}
Expand Down
1 change: 1 addition & 0 deletions src/models/CustomTaxonomyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export interface CustomTaxonomyData {
options?: string[] | undefined;
option?: string | undefined;
parents?: string[];
renderAsString?: boolean;
blockData?: BlockFieldData;
}
1 change: 1 addition & 0 deletions src/models/PanelSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface Field {
fieldGroup?: string | string[];
dataType?: string | string[];
taxonomyLimit?: number;
singleValueAsString?: boolean;
fileExtensions?: string[];
editable?: boolean;
required?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/panelWebView/components/Fields/WrapperField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
parents={parentFields}
blockData={blockData}
limit={field.taxonomyLimit}
renderAsString={field.singleValueAsString}
required={!!field.required}
/>
</FieldBoundary>
Expand All @@ -335,6 +336,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
parents={parentFields}
blockData={blockData}
limit={field.taxonomyLimit}
renderAsString={field.singleValueAsString}
required={!!field.required}
/>
</FieldBoundary>
Expand All @@ -356,6 +358,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
parents={parentFields}
blockData={blockData}
limit={field.taxonomyLimit}
renderAsString={field.singleValueAsString}
required={!!field.required}
/>
</FieldBoundary>
Expand Down
11 changes: 8 additions & 3 deletions src/panelWebView/components/TagPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ITagPickerProps {
taxonomyId?: string;
limit?: number;
required?: boolean;
renderAsString?: boolean;
}

const TagPicker: React.FunctionComponent<ITagPickerProps> = ({
Expand All @@ -53,7 +54,8 @@ const TagPicker: React.FunctionComponent<ITagPickerProps> = ({
parents,
blockData,
limit,
required
required,
renderAsString
}: React.PropsWithChildren<ITagPickerProps>) => {
const [selected, setSelected] = React.useState<string[]>([]);
const [inputValue, setInputValue] = React.useState<string>('');
Expand Down Expand Up @@ -96,18 +98,20 @@ const TagPicker: React.FunctionComponent<ITagPickerProps> = ({
* Send an update to VSCode
* @param values
*/
const sendUpdate = (values: string[]) => {
const sendUpdate = useCallback((values: string[]) => {
if (type === TagType.tags) {
Messenger.send(CommandToCode.updateTags, {
fieldName,
values,
renderAsString,
parents,
blockData
});
} else if (type === TagType.categories) {
Messenger.send(CommandToCode.updateCategories, {
fieldName,
values,
renderAsString,
parents,
blockData
});
Expand All @@ -121,11 +125,12 @@ const TagPicker: React.FunctionComponent<ITagPickerProps> = ({
id: taxonomyId,
name: fieldName,
options: values,
renderAsString,
parents,
blockData
} as CustomTaxonomyData);
}
};
}, [renderAsString]);

/**
* Triggers the focus to the input when command is executed
Expand Down

0 comments on commit 96c496c

Please sign in to comment.