diff --git a/package.json b/package.json index 1f7e3dca..72cd6407 100644 --- a/package.json +++ b/package.json @@ -95,10 +95,10 @@ "default": true, "description": "Specify if you want to highlight the Front Matter in the Markdown file." }, - "frontMatter.content.folders": { + "frontMatter.content.pageFolders": { "type": "array", "default": [], - "markdownDescription": "This array of folders defines where the extension can easily create new content by running the create article command." + "markdownDescription": "This array of folders defines where the extension can retrieve or create new pages." }, "frontMatter.content.publicFolder": { "type": "string", diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index 3e3e85d2..3fa1abed 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -118,10 +118,12 @@ export class Dashboard { await commands.executeCommand(COMMAND_NAME.init, Dashboard.getSettings); break; case DashboardMessage.Reload: - Dashboard.webview?.dispose(); - setTimeout(() => { - Dashboard.open(); - }, 100); + if (!Dashboard.isDisposed) { + Dashboard.webview?.dispose(); + setTimeout(() => { + Dashboard.open(); + }, 100); + } break; } }); @@ -157,8 +159,7 @@ export class Dashboard { */ private static async getPages() { const config = SettingsHelper.getConfig(); - const wsFolders = workspace.workspaceFolders; - const crntWsFolder = wsFolders && wsFolders.length > 0 ? wsFolders[0] : null; + const wsFolder = Folders.getWorkspaceFolder(); const descriptionField = config.get(SETTING_SEO_DESCRIPTION_FIELD) as string || "description"; const dateField = config.get(SETTING_DATE_FIELD) as string || "date"; @@ -192,8 +193,8 @@ export class Dashboard { description: article?.data[descriptionField] || "", }; - if (article?.data.preview && crntWsFolder) { - const previewPath = join(crntWsFolder.uri.fsPath, staticFolder || "", article?.data.preview); + if (article?.data.preview && wsFolder) { + const previewPath = join(wsFolder.fsPath, staticFolder || "", article?.data.preview); const previewUri = Uri.file(previewPath); const preview = Dashboard.webview?.webview.asWebviewUri(previewUri); page.preview = preview?.toString() || ""; diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index 09ecca4c..cbab4457 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -1,5 +1,5 @@ +import { SETTINGS_CONTENT_PAGE_FOLDERS } from './../constants/settings'; import { commands, Uri, workspace, window } from "vscode"; -import { SETTINGS_CONTENT_FOLDERS } from "../constants"; import { basename, join } from "path"; import { ContentFolder, FileInfo, FolderInfo } from "../models"; import uniqBy = require("lodash.uniqby"); @@ -8,6 +8,8 @@ import { Notifications } from "../helpers/Notifications"; import { CONTEXT } from "../constants/context"; import { SettingsHelper } from "../helpers"; +export const WORKSPACE_PLACEHOLDER = `[[workspace]]`; + export class Folders { /** @@ -38,7 +40,7 @@ export class Folders { const location = folders.find(f => f.title === selectedFolder); if (location) { - const folderPath = Folders.getFolderPath(Uri.file(location.fsPath)); + const folderPath = Folders.getFolderPath(Uri.file(location.path)); if (folderPath) { Template.create(folderPath); } @@ -55,7 +57,7 @@ export class Folders { let folders = Folders.get(); - const exists = folders.find(f => f.paths.includes(folder.fsPath) || f.paths.includes(wslPath)); + const exists = folders.find(f => f.path.includes(folder.fsPath) || f.path.includes(wslPath)); if (exists) { Notifications.warning(`Folder is already registered`); @@ -70,11 +72,10 @@ export class Folders { folders.push({ title: folderName, - fsPath: folder.fsPath, - paths: folder.fsPath === wslPath ? [folder.fsPath] : [folder.fsPath, wslPath] + path: folder.fsPath } as ContentFolder); - folders = uniqBy(folders, f => f.fsPath); + folders = uniqBy(folders, f => f.path); await Folders.update(folders); Notifications.info(`Folder registered`); @@ -90,7 +91,7 @@ export class Folders { public static async unregister(folder: Uri) { if (folder && folder.path) { let folders = Folders.get(); - folders = folders.filter(f => f.fsPath !== folder.fsPath); + folders = folders.filter(f => f.path !== folder.fsPath); await Folders.update(folders); } @@ -104,7 +105,7 @@ export class Folders { const folders = Folders.get(); let allFolders: string[] = []; for (const folder of folders) { - allFolders = [...allFolders, ...folder.paths] + allFolders = [...allFolders, folder.path] } commands.executeCommand('setContext', CONTEXT.registeredFolders, allFolders); } @@ -159,7 +160,7 @@ export class Folders { for (const folder of folders) { try { const projectName = Folders.getProjectFolderName(); - let projectStart = folder.fsPath.split(projectName).pop(); + let projectStart = folder.path.split(projectName).pop(); if (projectStart) { projectStart = projectStart.replace(/\\/g, '/'); projectStart = projectStart.startsWith('/') ? projectStart.substr(1) : projectStart; @@ -211,10 +212,14 @@ export class Folders { * Get the folder settings * @returns */ - public static get() { + public static get(): ContentFolder[] { const config = SettingsHelper.getConfig(); - const folders: ContentFolder[] = config.get(SETTINGS_CONTENT_FOLDERS) as ContentFolder[]; - return folders; + const wsPath = Folders.getWorkspaceFolder(); + const folders: ContentFolder[] = config.get(SETTINGS_CONTENT_PAGE_FOLDERS) as ContentFolder[]; + return folders.map(folder => ({ + title: folder.title, + path: folder.path.replace(WORKSPACE_PLACEHOLDER, wsPath?.fsPath || "") + })); } /** @@ -223,6 +228,7 @@ export class Folders { */ private static async update(folders: ContentFolder[]) { const config = SettingsHelper.getConfig(); - await config.update(SETTINGS_CONTENT_FOLDERS, folders); + const wsFolder = Folders.getWorkspaceFolder(); + await config.update(SETTINGS_CONTENT_PAGE_FOLDERS, folders.map(folder => ({ title: folder.title, path: folder.path.replace(wsFolder?.fsPath || "", WORKSPACE_PLACEHOLDER) }))); } } \ No newline at end of file diff --git a/src/commands/Project.ts b/src/commands/Project.ts index 85896815..eebc6265 100644 --- a/src/commands/Project.ts +++ b/src/commands/Project.ts @@ -4,6 +4,7 @@ import { join } from "path"; import * as fs from "fs"; import { Notifications } from "../helpers/Notifications"; import { Template } from "./Template"; +import { Folders } from "./Folders"; export class Project { @@ -52,14 +53,13 @@ categories: [] */ public static templatePath() { const folder = Template.getSettings(); - const workspaceFolders = workspace.workspaceFolders; + const wsFolder = Folders.getWorkspaceFolder(); - if (!folder || !workspaceFolders || workspaceFolders.length === 0) { + if (!folder || !wsFolder) { return null; } - const workspaceFolder = workspaceFolders[0]; - const templatePath = Uri.file(join(workspaceFolder.uri.fsPath, folder)); + const templatePath = Uri.file(join(wsFolder.fsPath, folder)); return templatePath; } } \ No newline at end of file diff --git a/src/commands/Template.ts b/src/commands/Template.ts index ed966746..5a3baa75 100644 --- a/src/commands/Template.ts +++ b/src/commands/Template.ts @@ -9,6 +9,7 @@ import { Article } from '.'; import { Notifications } from '../helpers/Notifications'; import { CONTEXT } from '../constants/context'; import { Project } from './Project'; +import { Folders } from './Folders'; export class Template { @@ -24,15 +25,14 @@ export class Template { * Check if the project is already initialized */ public static async isInitialized() { - const workspaceFolders = vscode.workspace.workspaceFolders; + const wsFolder = Folders.getWorkspaceFolder(); const folder = Template.getSettings(); - if (!folder || !workspaceFolders || workspaceFolders.length === 0) { + if (!folder || !wsFolder) { return false; } - const workspaceFolder = workspaceFolders[0]; - const templatePath = vscode.Uri.file(path.join(workspaceFolder.uri.fsPath, folder)); + const templatePath = vscode.Uri.file(path.join(wsFolder.fsPath, folder)); try { await vscode.workspace.fs.stat(templatePath); diff --git a/src/constants/settings.ts b/src/constants/settings.ts index 2b5a0164..86e2acc2 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -33,8 +33,13 @@ export const SETTING_PREVIEW_PATHNAME = "preview.pathName"; export const SETTING_CUSTOM_SCRIPTS = "custom.scripts"; export const SETTING_AUTO_UPDATE_DATE = "content.autoUpdateDate"; -export const SETTINGS_CONTENT_FOLDERS = "content.folders"; +export const SETTINGS_CONTENT_PAGE_FOLDERS = "content.pageFolders"; export const SETTINGS_CONTENT_STATIC_FOLDERS = "content.publicFolder"; export const SETTINGS_CONTENT_FRONTMATTER_HIGHLIGHT = "content.fmHighlight"; -export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart"; \ No newline at end of file +export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart"; + +/** + * @deprecated + */ +export const SETTINGS_CONTENT_FOLDERS = "content.folders"; \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index ce479374..8a96076d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -21,6 +21,7 @@ const mdSelector: vscode.DocumentSelector = { language: 'markdown', scheme: 'fil export async function activate({ subscriptions, extensionUri, extensionPath, globalState }: vscode.ExtensionContext) { const extension = Extension.getInstance(globalState, extensionUri); + extension.migrateSettings() collection = vscode.languages.createDiagnosticCollection('frontMatter'); diff --git a/src/helpers/Extension.ts b/src/helpers/Extension.ts index e35b4421..9c8a922b 100644 --- a/src/helpers/Extension.ts +++ b/src/helpers/Extension.ts @@ -1,5 +1,9 @@ +import { basename } from "path"; import { Memento, extensions, Uri } from "vscode"; +import { Folders, WORKSPACE_PLACEHOLDER } from "../commands/Folders"; +import { SETTINGS_CONTENT_FOLDERS, SETTINGS_CONTENT_PAGE_FOLDERS } from "../constants"; import { EXTENSION_ID, EXTENSION_STATE_VERSION } from "../constants/Extension"; +import { SettingsHelper } from "./SettingsHelper"; export class Extension { @@ -50,4 +54,23 @@ export class Extension { public get extensionPath(): Uri { return this.extPath; } + + /** + * Migrate old settings to new settings + */ + public async migrateSettings(): Promise { + const config = SettingsHelper.getConfig(); + const folders = config.get(SETTINGS_CONTENT_FOLDERS); + if (folders && folders.length > 0) { + const workspace = Folders.getWorkspaceFolder(); + const projectFolder = basename(workspace?.fsPath || ""); + + const paths = folders.map((folder: any) => ({ + title: folder.title, + path: `${WORKSPACE_PLACEHOLDER}${folder.fsPath.split(projectFolder).slice(1).join('')}` + })); + + await config.update(`${SETTINGS_CONTENT_PAGE_FOLDERS}`, paths); + } + } } \ No newline at end of file diff --git a/src/models/ContentFolder.ts b/src/models/ContentFolder.ts index 806a8e59..1a5e6cb2 100644 --- a/src/models/ContentFolder.ts +++ b/src/models/ContentFolder.ts @@ -1,5 +1,4 @@ export interface ContentFolder { title: string; - fsPath: string; - paths: string[]; + path: string; } \ No newline at end of file diff --git a/src/pagesView/components/WelcomeScreen.tsx b/src/pagesView/components/WelcomeScreen.tsx index daead105..44167ce5 100644 --- a/src/pagesView/components/WelcomeScreen.tsx +++ b/src/pagesView/components/WelcomeScreen.tsx @@ -1,8 +1,10 @@ import { HeartIcon, StarIcon } from '@heroicons/react/outline'; import * as React from 'react'; import { GITHUB_LINK, REVIEW_LINK, SPONSOR_LINK } from '../../constants/Links'; +import { MessageHelper } from '../../helpers/MessageHelper'; import { FrontMatterIcon } from '../../viewpanel/components/Icons/FrontMatterIcon'; import { GitHubIcon } from '../../viewpanel/components/Icons/GitHubIcon'; +import { DashboardMessage } from '../DashboardMessage'; import { Settings } from '../models/Settings'; import { StepsToGetStarted } from './Steps/StepsToGetStarted'; @@ -11,6 +13,12 @@ export interface IWelcomeScreenProps { } export const WelcomeScreen: React.FunctionComponent = ({settings}: React.PropsWithChildren) => { + + React.useEffect(() => { + return () => { + MessageHelper.sendMessage(DashboardMessage.Reload) + }; + }, ['']); return (
diff --git a/src/webview/ExplorerView.ts b/src/webview/ExplorerView.ts index 869cc8fd..f127124e 100644 --- a/src/webview/ExplorerView.ts +++ b/src/webview/ExplorerView.ts @@ -130,9 +130,9 @@ export class ExplorerView implements WebviewViewProvider, Disposable { this.runCustomScript(msg); break; case CommandToCode.openProject: - const wsFolders = workspace.workspaceFolders; - if (wsFolders && wsFolders.length > 0) { - const wsPath = wsFolders[0].uri.fsPath; + const wsFolder = Folders.getWorkspaceFolder(); + if (wsFolder) { + const wsPath = wsFolder.fsPath; if (os.type() === "Darwin") { exec(`open ${wsPath}`); } else if (os.type() === "Windows_NT") { @@ -275,9 +275,9 @@ export class ExplorerView implements WebviewViewProvider, Disposable { const article = ArticleHelper.getFrontMatter(editor); - const wsFolders = workspace.workspaceFolders; - if (wsFolders && wsFolders.length > 0) { - const wsPath = wsFolders[0].uri.fsPath; + const wsFolder = Folders.getWorkspaceFolder(); + if (wsFolder) { + const wsPath = wsFolder.fsPath; let articleData = `'${JSON.stringify(article?.data)}'`; if (os.type() === "Windows_NT") {