Skip to content

Commit

Permalink
#360 - Define which content types can be used on your page folders
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 9, 2022
1 parent f1a8e0d commit e3bd7ee
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### 🎨 Enhancements

- [#360](https://github.com/estruyf/vscode-front-matter/issues/360): Define which content types can be used on your page folders
- [#406](https://github.com/estruyf/vscode-front-matter/issues/406): Added support for single data entries in the data dashboard
- [#428](https://github.com/estruyf/vscode-front-matter/issues/428): Improved UX for inserting images to your content
- [#430](https://github.com/estruyf/vscode-front-matter/issues/430): Support for HEXO its `post_asset_folder` setting (image location)
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@
"filePrefix": {
"type": [ "null", "string" ],
"description": "Defines a prefix for the file name."
},
"contentTypes": {
"type": "array",
"description": "Defines which content types can be used for the current location. If not defined, all content types will be available.",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Folders {
* Create content in a registered folder
* @returns
*/
public static async create() {
public static async create() {
const selectedFolder = await Questions.SelectContentFolder();
if (!selectedFolder) {
return;
Expand Down
20 changes: 12 additions & 8 deletions src/helpers/ContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,26 @@ export class ContentType {
* @returns
*/
public static async createContent() {
const selectedContentType = await Questions.SelectContentType();
if (!selectedContentType) {
return;
}

const selectedFolder = await Questions.SelectContentFolder();
if (!selectedFolder) {
return;
}

const contentTypes = ContentType.getAll();
const folders = Folders.get();
const folder = folders.find(f => f.title === selectedFolder);

const location = folders.find(f => f.title === selectedFolder);
if (contentTypes && location) {
const folderPath = Folders.getFolderPath(Uri.file(location.path));
if (!folder) {
return;
}

const selectedContentType = await Questions.SelectContentType(folder.contentTypes || []);
if (!selectedContentType) {
return;
}

if (contentTypes && folder) {
const folderPath = Folders.getFolderPath(Uri.file(folder.path));
const contentType = contentTypes.find(ct => ct.name === selectedContentType);
if (folderPath && contentType) {
ContentType.create(contentType, folderPath);
Expand Down
12 changes: 9 additions & 3 deletions src/helpers/Questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Questions {
* @returns
*/
public static async SelectContentFolder(showWarning: boolean = true): Promise<string | undefined> {
const folders = Folders.get();
let folders = Folders.get();

let selectedFolder: string | undefined;
if (folders.length > 1) {
Expand All @@ -72,16 +72,22 @@ export class Questions {

/**
* Select the content type to create new content
* @param allowedCts Allowed content types for the folder
* @param showWarning
* @returns
*/
public static async SelectContentType(showWarning: boolean = true): Promise<string | undefined> {
const contentTypes = ContentType.getAll();
public static async SelectContentType(allowedCts: string[], showWarning: boolean = true): Promise<string | undefined> {
let contentTypes = ContentType.getAll();
if (!contentTypes || contentTypes.length === 0) {
Notifications.warning("No content types found. Please create a content type first.");
return;
}

// Only allow content types that are allowed for the folder
if (allowedCts && allowedCts.length > 0) {
contentTypes = contentTypes.filter(ct => allowedCts.find(allowedCt => allowedCt === ct.name));
}

if (contentTypes.length === 1) {
return contentTypes[0].name;
}
Expand Down
1 change: 1 addition & 0 deletions src/models/ContentFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface ContentFolder {
excludeSubdir?: boolean;
previewPath?: string;
filePrefix?: string;
contentTypes?: string[];
}

0 comments on commit e3bd7ee

Please sign in to comment.