diff --git a/package.json b/package.json index 7f6b50e..23a728f 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,16 @@ "onStartupFinished" ], "contributes": { + "configuration": { + "title": "antd-design-token", + "properties": { + "antdDesignToken.themePath": { + "type": "string", + "default": "", + "description": "json theme path ,relative to workspace root" + } + } + }, "commands": [ { "command": "antd-design-token.toggle", diff --git a/src/extension.ts b/src/extension.ts index 4d16de5..91240aa 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,7 +4,7 @@ import setupEventListenerAndDecorations, { DisposableAndClear, } from "./listener"; import setupAntdTokenCompletion from "./typing"; -import { checkAntdProject } from "./utils"; +import { checkAntdProject, getThemeConfig } from "./utils"; export function activate(context: vscode.ExtensionContext) { let isActive = true; @@ -38,7 +38,16 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(disposable); function setup() { - const fullToken = getDesignToken(); + let fullToken: any; + + const themeConfig = getThemeConfig(); + console.log({ themeConfig }); + + if (themeConfig) { + fullToken = getDesignToken(themeConfig); + } else { + fullToken = getDesignToken(); + } if (!fullToken) { vscode.window.showErrorMessage("Failed to get antd fullToken."); @@ -53,6 +62,13 @@ export function activate(context: vscode.ExtensionContext) { } } + vscode.workspace.onDidChangeConfiguration((event) => { + if (event.affectsConfiguration("antdDesignToken.themePath")) { + if (isActive) { + setup(); + } + } + }); function setupDecorationsAndCompletion( context: vscode.ExtensionContext, fullToken: any diff --git a/src/utils/index.ts b/src/utils/index.ts index 026613f..0c935c1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -59,3 +59,30 @@ export function getProjectPath(): string | undefined { ?.map((folder) => folder.uri.fsPath) .filter((fsPath) => fileName?.startsWith(fsPath))[0]; } + +export function getThemeConfig() { + try { + const config = vscode.workspace.getConfiguration("antdDesignToken"); + const themePath = config.get("themePath") ?? ""; + const extname = path.extname(themePath); + if (extname !== ".json") { + console.warn(`invalid theme path ${themePath}`); + return null; + } + + let workspaceFolder = + vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath ?? ""; + let themeFullPath; + if (path.isAbsolute(themePath)) { + themeFullPath = themePath; + } else { + themeFullPath = path.join(workspaceFolder, themePath); + } + const jsonStr = fs.readFileSync(themeFullPath, "utf-8"); + const json = JSON.parse(jsonStr); + return json; + } catch (err) { + vscode.window.showInformationMessage("failed to get theme json "); + return null; + } +}