diff --git a/src/extension.mts b/src/extension.mts index c5b89009..f6ef8532 100644 --- a/src/extension.mts +++ b/src/extension.mts @@ -66,6 +66,7 @@ export async function activate(context: ExtensionContext): Promise { const settings = Settings.createInstance( context.workspaceState, + context.globalState, context.extension.packageJSON as PackageJSON ); GithubApiCache.createInstance(context); diff --git a/src/settings.mts b/src/settings.mts index 5b656858..06eb2c56 100644 --- a/src/settings.mts +++ b/src/settings.mts @@ -1,4 +1,5 @@ -import type { Memento, WorkspaceConfiguration } from "vscode"; +import { homedir } from "os"; +import { type Memento, Uri, type WorkspaceConfiguration } from "vscode"; import { workspace } from "vscode"; /** @@ -26,14 +27,26 @@ export interface PackageJSON { publisher: string; } +export type GlobalStateType = Memento & { + setKeysForSync(keys: readonly string[]): void; +}; + +const LAST_PROJECT_ROOT_STATE_KEY = "lastProjectRoot"; + export default class Settings { private static instance?: Settings; private config: WorkspaceConfiguration; public context: Memento; + public globalState: GlobalStateType; private pkg: PackageJSON; - private constructor(context: Memento, packageJSON: PackageJSON) { + private constructor( + context: Memento, + globalState: GlobalStateType, + packageJSON: PackageJSON + ) { this.context = context; + this.globalState = globalState; this.pkg = packageJSON; this.config = workspace.getConfiguration(packageJSON.name); @@ -41,9 +54,10 @@ export default class Settings { public static createInstance( context: Memento, + globalState: GlobalStateType, packageJSON: PackageJSON ): Settings { - Settings.instance = new Settings(context, packageJSON); + Settings.instance = new Settings(context, globalState, packageJSON); return Settings.instance; } @@ -109,4 +123,20 @@ export default class Settings { public getExtensionId(): string { return [this.pkg.publisher, this.pkg.name].join("."); } + + public async setLastProjectRoot(root: Uri): Promise { + // saving fsPath not uri project as it would get corrupted and lose its + // fsPath property after loading + await this.globalState.update(LAST_PROJECT_ROOT_STATE_KEY, root.fsPath); + } + + public getLastProjectRoot(): Uri { + const fsPath = this.globalState.get( + LAST_PROJECT_ROOT_STATE_KEY, + // default to home directory as new project root + homedir() + ); + + return Uri.file(fsPath); + } } diff --git a/src/webview/newProjectPanel.mts b/src/webview/newProjectPanel.mts index 2f6c75d8..5dc7eca7 100644 --- a/src/webview/newProjectPanel.mts +++ b/src/webview/newProjectPanel.mts @@ -398,6 +398,9 @@ export class NewProjectPanel { this._extensionUri = extensionUri; this._settings = settings; this._isProjectImport = isProjectImport; + // set local property as it's an indicator for initial projectRoot update + // later during webview initialization + projectUri = projectUri ?? this._settings.getLastProjectRoot(); this._projectRoot = projectUri; this._isCreateFromExampleOnly = createFromExample; @@ -440,6 +443,7 @@ export class NewProjectPanel { if (newLoc && newLoc[0]) { // overwrite preview folderUri this._projectRoot = newLoc[0]; + await this._settings.setLastProjectRoot(newLoc[0]); // update webview await this._panel.webview.postMessage({