Merge branch 'issue/81'

This commit is contained in:
Elio Struyf
2021-09-03 10:42:12 +02:00
11 changed files with 84 additions and 41 deletions
+2 -2
View File
@@ -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",
+9 -8
View File
@@ -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() || "";
+19 -13
View File
@@ -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) })));
}
}
+4 -4
View File
@@ -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;
}
}
+4 -4
View File
@@ -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);
+7 -2
View File
@@ -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";
export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart";
/**
* @deprecated
*/
export const SETTINGS_CONTENT_FOLDERS = "content.folders";
+1
View File
@@ -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');
+23
View File
@@ -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<void> {
const config = SettingsHelper.getConfig();
const folders = config.get<any>(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);
}
}
}
+1 -2
View File
@@ -1,5 +1,4 @@
export interface ContentFolder {
title: string;
fsPath: string;
paths: string[];
path: string;
}
@@ -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<IWelcomeScreenProps> = ({settings}: React.PropsWithChildren<IWelcomeScreenProps>) => {
React.useEffect(() => {
return () => {
MessageHelper.sendMessage(DashboardMessage.Reload)
};
}, ['']);
return (
<div className={`h-full overflow-auto py-24`}>
+6 -6
View File
@@ -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") {