Skip to content
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

vscode theme adaption #4

Merged
merged 1 commit into from
Sep 7, 2023
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dev": "nodemon",
"dev:ext": "npm -C packages/vscode run dev",
"build": "tsup",
"build:ext": "npm -C packages/vscode run build",
"play": "npm -C packages/playground run dev",
"lint": "eslint .",
"test": "vitest",
Expand Down
13 changes: 12 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
}
}
],
"configuration": {
"title": "Vue Hook Optimizer",
"properties": {
"vho.theme": {
"type": "string",
"default": "auto",
"enum": ["auto", "light", "dark"],
"description": "Choose settings that are suitable for the current theme"
}
}
},
"menus": {
"commandPalette": [
{
Expand Down Expand Up @@ -80,7 +91,7 @@
},
"scripts": {
"build": "tsc ./script/index.ts && tsup",
"dev": "tsc ./script/index.ts --watch & tsup --watch",
"dev": "tsc ./script/index.ts --watch & tsup --watch --env.NODE_ENV development",
"lint": "eslint .",
"vscode:prepublish": "pnpm build",
"publish": "vsce publish --no-dependencies",
Expand Down
32 changes: 5 additions & 27 deletions packages/vscode/script/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
let network: any;

function init(dataString: string) {
function init(dataString: string, theme: string) {
// 获取容器
var container = document.getElementById('mynetwork');
const container = document.getElementById('mynetwork');

// 将数据赋值给vis 数据格式化器
var data = JSON.parse(dataString);
var options = {
const data = JSON.parse(dataString);
const options = Object.assign({
physics: {
solver: 'forceAtlas2Based',
forceAtlas2Based: {
gravitationalConstant: -100,
},
},
groups: {
used: {
color: {
border: '#3d7de4',
background: '#9dc2f9',
highlight: {
border: '#3d7de4',
background: '#9dc2f9',
},
},
},
normal: {
color: {
border: '#ccc',
background: '#ddd',
highlight: {
border: '#ccc',
background: '#ddd',
},
},
},
},
};
}, JSON.parse(theme));

// 初始化关系图
// @ts-ignore
Expand Down
45 changes: 38 additions & 7 deletions packages/vscode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import { window } from 'vscode';
import { analyze } from './analyze';
import { template } from 'lodash-es';
import { light, dark } from './themes';
import path from 'path';

const visTemplate = template(`<html>
Expand Down Expand Up @@ -47,7 +48,9 @@ const visTemplate = template(`<html>
w-[200px]
px-4 py-2
border-[#ddd] border-[1px] border-solid rounded-md
bg-transparent
shadow
backdrop-blur
"
>
</div>
Expand All @@ -65,7 +68,7 @@ const visTemplate = template(`<html>
<div
class="
inline-block mr-1
bg-[#9dc2f9]
bg-[<%= legend_used %>]
border border-solid border-[#3d7de4]
w-[10px] h-[10px]
"
Expand All @@ -76,7 +79,7 @@ const visTemplate = template(`<html>
<div
class="
inline-block mr-1
bg-[#eee]
bg-[<%= legend_normal %>]
border border-solid border-[#ddd]
w-[10px] h-[10px]
"
Expand All @@ -87,7 +90,7 @@ const visTemplate = template(`<html>
<div
class="
inline-block mr-1
border border-solid border-[#333]
border border-solid border-[<%= legend_variant %>]
rounded-full
w-[10px] h-[10px]
"
Expand All @@ -98,7 +101,7 @@ const visTemplate = template(`<html>
<div
class="
inline-block mr-1
border border-solid border-[#333]
border border-solid border-[<%= legend_func %>]
rotate-45 transform scale-80
w-[10px] h-[10px]
"
Expand All @@ -109,7 +112,7 @@ const visTemplate = template(`<html>
</div>

<script type="text/javascript">
init('<%= data %>')
init('<%= data %>', '<%= config %>')
const inputEle = findSearchInput();
if(inputEle) {
inputEle.addEventListener('input', (e) => {
Expand All @@ -124,10 +127,10 @@ if(inputEle) {
</body>
</html>`);

function getWebviewUri(webview: vscode.Webview, extensionPath: string, filename: string) {
function getWebviewUri(webview: vscode.Webview, extensionPath: string, filename: string, dir: string = 'script') {
const jsFilePath = vscode.Uri.file(path.resolve(
extensionPath,
`./script/${filename}`
`./${dir}/${filename}`
));
return webview.asWebviewUri(jsFilePath);
}
Expand All @@ -139,6 +142,8 @@ let alerted = false;

export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('vho.action.analyze', async () => {
// 根据主题获取vis config
const config = getVisConfigByTheme();
// 获取当前vue文件的内容
const editor = window.activeTextEditor;
if (!editor) {
Expand Down Expand Up @@ -173,6 +178,11 @@ export function activate(context: vscode.ExtensionContext) {
libTailwind: getWebviewUri(panel.webview, context.extensionPath, 'tailwindcss.min.js'),
libIndex: getWebviewUri(panel.webview, context.extensionPath, 'index.js'),
data: JSON.stringify(res.data.vis),
config: JSON.stringify(config?.vis),
legend_used: config?.legend.used,
legend_normal: config?.legend.normal,
legend_variant: config?.legend.variant,
legend_func: config?.legend.func,
});

outputChannel.append(`${fileName}: \n`);
Expand Down Expand Up @@ -201,6 +211,27 @@ export function activate(context: vscode.ExtensionContext) {
}));
}

function getVisConfigByTheme () {
const config = vscode.workspace.getConfiguration('vho');
const theme = config.get('theme');

if (theme === 'auto') {
const themeKind = vscode.window.activeColorTheme.kind;
if (themeKind === 2 || themeKind === 3) {
return dark;
} else if (themeKind === 1 || themeKind === 4) {
return light;
} else {
outputChannel.append('The current theme is not supported at the moment, so it will be set as light theme.');
return light;
}
} else if (theme === 'light') {
return light;
} else if (theme === 'dark') {
return dark;
}
}

export function deactivate() {

}
53 changes: 53 additions & 0 deletions packages/vscode/src/themes/dark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const used_bg = '#9dc2f9';
const used_border = '#3d7de4';
const used_highlight_bg = '#9dc2f9';
const used_highlight_border = '#3d7de4';
const used_font_color = '#fff';

const normal_bg = '#ddd';
const normal_border = '#ccc';
const normal_highlight_bg = '#ddd';
const normal_highlight_border = '#ccc';
const normal_font_color = '#c8c9cc';


const options = {
vis: {
groups: {
used: {
color: {
border: used_border,
background: used_bg,
highlight: {
border: used_highlight_border,
background: used_highlight_bg,
},
},
font: {
color: used_font_color,
},
},
normal: {
color: {
border: normal_border,
background: normal_bg,
highlight: {
border: normal_highlight_border,
background: normal_highlight_bg,
},
},
font: {
color: normal_font_color,
},
},
},
},
legend: {
used: used_bg,
normal: normal_bg,
variant: '#fff',
func: '#fff',
},
};

export default options;
2 changes: 2 additions & 0 deletions packages/vscode/src/themes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as light } from './light';
export { default as dark } from './dark';
52 changes: 52 additions & 0 deletions packages/vscode/src/themes/light.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const used_bg = '#9dc2f9';
const used_border = '#3d7de4';
const used_highlight_bg = '#9dc2f9';
const used_highlight_border = '#3d7de4';
const used_font_color = '#000';

const normal_bg = '#ddd';
const normal_border = '#ccc';
const normal_highlight_bg = '#ddd';
const normal_highlight_border = '#ccc';
const normal_font_color = '#000';

const options = {
vis: {
groups: {
used: {
color: {
border: used_border,
background: used_bg,
highlight: {
border: used_highlight_border,
background: used_highlight_bg,
},
},
font: {
color: used_font_color,
},
},
normal: {
color: {
border: normal_border,
background: normal_bg,
highlight: {
border: normal_highlight_border,
background: normal_highlight_bg,
},
},
font: {
color: normal_font_color,
},
},
},
},
legend: {
used: used_bg,
normal: normal_bg,
variant: '#000',
func: '#000',
},
};

export default options;
23 changes: 13 additions & 10 deletions packages/vscode/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: [
'src/index.ts',
],
format: ['cjs'],
shims: false,
dts: false,
external: [
'vscode',
],
export default defineConfig((options) => {
return {
entry: [
'src/index.ts',
],
sourcemap: options.env?.NODE_ENV === 'development',
format: ['cjs'],
shims: false,
dts: false,
external: [
'vscode',
],
};
});