Skip to content

✨ feat: support json theme #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 18 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("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;
}
}