Open file updates

This commit is contained in:
Elio Struyf
2021-08-23 13:22:47 +02:00
parent 6395e471f0
commit ea381eac51
5 changed files with 67 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
import { commands, Uri, workspace, window } from "vscode";
import { CONFIG_KEY, SETTINGS_CONTENT_FOLDERS } from "../constants";
import { basename } from "path";
import { ContentFolder, FolderInfo } from "../models";
import { basename, join } from "path";
import { ContentFolder, FileInfo, FolderInfo } from "../models";
import uniqBy = require("lodash.uniqby");
import { Template } from "./Template";
import { Notifications } from "../helpers/Notifications";
@@ -129,11 +129,28 @@ export class Folders {
for (const folder of folders) {
try {
const files = await workspace.fs.readDirectory(Uri.file(folder.fsPath));
const folderPath = Uri.file(folder.fsPath);
const files = await workspace.fs.readDirectory(folderPath);
if (files) {
let fileStats: FileInfo[] = [];
for (const file of files) {
const fileName = file[0];
const filePath = Uri.file(join(folderPath.fsPath, fileName));
const stats = await workspace.fs.stat(filePath);
fileStats.push({
filePath: filePath.fsPath,
fileName,
...stats
});
}
fileStats = fileStats.sort((a, b) => b.mtime - a.mtime).slice(0, 10);
folderInfo.push({
title: folder.title,
files: files.length
files: files.length,
lastModified: fileStats
});
}
} catch (e) {

View File

@@ -1,3 +1,4 @@
import { FileType } from "vscode";
export interface PanelSettings {
seo: SEO;
@@ -29,8 +30,18 @@ export interface Slug {
export interface FolderInfo {
title: string;
files: number;
lastModified: FileInfo[];
}
export interface FileInfo {
type: FileType;
ctime: number;
mtime: number;
size: number;
filePath: string;
fileName: string;
};
export interface CustomScript {
title: string;
script: string;

View File

@@ -22,4 +22,5 @@ export enum CommandToCode {
toggleWritingSettings = "toggle-writing-settings",
openPreview = "open-preview",
updatePreviewUrl = "update-preview-url",
openInEditor = "open-in-editor",
}

View File

@@ -19,6 +19,11 @@ export const BaseView: React.FunctionComponent<IBaseViewProps> = ({settings}: Re
const createContent = () => {
MessageHelper.sendMessage(CommandToCode.createContent);
};
const openFile = (filePath: string) => {
MessageHelper.sendMessage(CommandToCode.openInEditor, filePath);
};
return (
<div className="frontmatter">
@@ -40,6 +45,18 @@ export const BaseView: React.FunctionComponent<IBaseViewProps> = ({settings}: Re
settings.contentInfo.map(folder => (
<div key={folder.title}>
{folder.title}: {folder.files} file{folder.files > 1 ? 's' : ''}
{
folder.lastModified && (
<ul>
{
folder.lastModified.map(file => (
<li key={file.fileName} onClick={() => openFile(file.filePath)}>{file.fileName}</li>
))
}
</ul>
)
}
</div>
))
}

View File

@@ -168,6 +168,9 @@ export class ExplorerView implements WebviewViewProvider, Disposable {
case CommandToCode.updatePreviewUrl:
this.updatePreviewUrl(msg.data || "");
break;
case CommandToCode.openInEditor:
this.openFileInEditor(msg.data);
break;
}
});
@@ -224,6 +227,20 @@ export class ExplorerView implements WebviewViewProvider, Disposable {
this.postWebviewMessage({ command: Command.closeSections });
}
/**
* Open the file via its path
*/
private async openFileInEditor(filePath: string) {
if (filePath) {
try {
const doc = await workspace.openTextDocument(Uri.file(filePath));
await window.showTextDocument(doc, 1, false);
} catch (e) {
Notifications.error(`Couldn't open the file.`);
}
}
}
/**
* Run a custom script
* @param msg