From ea381eac5140e85cedfc0a6d487e67bcdcb28fc1 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 23 Aug 2021 13:22:47 +0200 Subject: [PATCH] Open file updates --- src/commands/Folders.ts | 25 +++++++++++++++++++++---- src/models/PanelSettings.ts | 11 +++++++++++ src/viewpanel/CommandToCode.ts | 1 + src/viewpanel/components/BaseView.tsx | 17 +++++++++++++++++ src/webview/ExplorerView.ts | 17 +++++++++++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index ede94559..c812a119 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -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) { diff --git a/src/models/PanelSettings.ts b/src/models/PanelSettings.ts index 23cf032e..9f4d17e5 100644 --- a/src/models/PanelSettings.ts +++ b/src/models/PanelSettings.ts @@ -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; diff --git a/src/viewpanel/CommandToCode.ts b/src/viewpanel/CommandToCode.ts index 7affddf0..294195f2 100644 --- a/src/viewpanel/CommandToCode.ts +++ b/src/viewpanel/CommandToCode.ts @@ -22,4 +22,5 @@ export enum CommandToCode { toggleWritingSettings = "toggle-writing-settings", openPreview = "open-preview", updatePreviewUrl = "update-preview-url", + openInEditor = "open-in-editor", } \ No newline at end of file diff --git a/src/viewpanel/components/BaseView.tsx b/src/viewpanel/components/BaseView.tsx index 56412990..f89af8db 100644 --- a/src/viewpanel/components/BaseView.tsx +++ b/src/viewpanel/components/BaseView.tsx @@ -19,6 +19,11 @@ export const BaseView: React.FunctionComponent = ({settings}: Re const createContent = () => { MessageHelper.sendMessage(CommandToCode.createContent); }; + + const openFile = (filePath: string) => { + MessageHelper.sendMessage(CommandToCode.openInEditor, filePath); + }; + return (
@@ -40,6 +45,18 @@ export const BaseView: React.FunctionComponent = ({settings}: Re settings.contentInfo.map(folder => (
{folder.title}: {folder.files} file{folder.files > 1 ? 's' : ''} + + { + folder.lastModified && ( +
    + { + folder.lastModified.map(file => ( +
  • openFile(file.filePath)}>{file.fileName}
  • + )) + } +
+ ) + }
)) } diff --git a/src/webview/ExplorerView.ts b/src/webview/ExplorerView.ts index 1ae40e2a..ee1794ac 100644 --- a/src/webview/ExplorerView.ts +++ b/src/webview/ExplorerView.ts @@ -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