Skip to content

Commit 3b71f6b

Browse files
committed
✨ feat: support json theme
1 parent d2961c3 commit 3b71f6b

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
"onStartupFinished"
1818
],
1919
"contributes": {
20+
"configuration": {
21+
"title": "antd-design-token",
22+
"properties": {
23+
"antdDesignToken.themePath": {
24+
"type": "string",
25+
"default": "",
26+
"description": "json theme path ,relative to workspace root"
27+
}
28+
}
29+
},
2030
"commands": [
2131
{
2232
"command": "antd-design-token.toggle",

src/extension.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import setupEventListenerAndDecorations, {
44
DisposableAndClear,
55
} from "./listener";
66
import setupAntdTokenCompletion from "./typing";
7-
import { checkAntdProject } from "./utils";
7+
import { checkAntdProject, getThemeConfig } from "./utils";
88

99
export function activate(context: vscode.ExtensionContext) {
1010
let isActive = true;
@@ -38,7 +38,16 @@ export function activate(context: vscode.ExtensionContext) {
3838
context.subscriptions.push(disposable);
3939

4040
function setup() {
41-
const fullToken = getDesignToken();
41+
let fullToken: any;
42+
43+
const themeConfig = getThemeConfig();
44+
console.log({ themeConfig });
45+
46+
if (themeConfig) {
47+
fullToken = getDesignToken(themeConfig);
48+
} else {
49+
fullToken = getDesignToken();
50+
}
4251

4352
if (!fullToken) {
4453
vscode.window.showErrorMessage("Failed to get antd fullToken.");
@@ -53,6 +62,13 @@ export function activate(context: vscode.ExtensionContext) {
5362
}
5463
}
5564

65+
vscode.workspace.onDidChangeConfiguration((event) => {
66+
if (event.affectsConfiguration("antdDesignToken.themePath")) {
67+
if (isActive) {
68+
setup();
69+
}
70+
}
71+
});
5672
function setupDecorationsAndCompletion(
5773
context: vscode.ExtensionContext,
5874
fullToken: any

src/utils/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,30 @@ export function getProjectPath(): string | undefined {
5959
?.map((folder) => folder.uri.fsPath)
6060
.filter((fsPath) => fileName?.startsWith(fsPath))[0];
6161
}
62+
63+
export function getThemeConfig() {
64+
try {
65+
const config = vscode.workspace.getConfiguration("antdDesignToken");
66+
const themePath = config.get<string>("themePath") ?? "";
67+
const extname = path.extname(themePath);
68+
if (extname !== ".json") {
69+
console.warn(`invalid theme path ${themePath}`);
70+
return null;
71+
}
72+
73+
let workspaceFolder =
74+
vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath ?? "";
75+
let themeFullPath;
76+
if (path.isAbsolute(themePath)) {
77+
themeFullPath = themePath;
78+
} else {
79+
themeFullPath = path.join(workspaceFolder, themePath);
80+
}
81+
const jsonStr = fs.readFileSync(themeFullPath, "utf-8");
82+
const json = JSON.parse(jsonStr);
83+
return json;
84+
} catch (err) {
85+
vscode.window.showInformationMessage("failed to get theme json ");
86+
return null;
87+
}
88+
}

0 commit comments

Comments
 (0)