Skip to content

Commit f5cb636

Browse files
authored
Merge pull request #318 from ccagml/main
fix(打开webview的相关界面): 修改加载css样式的写法
2 parents 214ad98 + 65d82d4 commit f5cb636

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# version 3.1.22
2+
3+
- 修改预览题目、题解的样式加载方式,原本的方式在高版本的 VS Code 会加载失败 401 错误,导致界面很乱
4+
15
# version 3.1.21
26

37
- 修改国际站修改登录方式 cRUL 方式登录,解决国际站不能提交使用的问题

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode",
44
"description": "%main.description%",
5-
"version": "3.1.21",
5+
"version": "3.1.22",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",

src/commitResult/CommitResultModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class SubmissionService extends BaseWebViewService {
8989
}
9090

9191
protected getWebviewContent(): string {
92-
const styles: string = markdownService.getStyles();
92+
const styles: string = markdownService.getStyles(this.panel);
9393
const title: string = `## ${this.result.messages[0]}`;
9494
if (this.result?.costTime && this.result?.costTime.length > 0) {
9595
this.result.messages.push(this.result?.costTime[0]);

src/preView/PreviewModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class PreviewService extends BaseWebViewService {
110110
<html>
111111
<head>
112112
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource: 'unsafe-inline'; style-src vscode-resource: 'unsafe-inline';"/>
113-
${markdownService.getStyles()}
113+
${markdownService.getStyles(this.panel)}
114114
${!this.sideMode ? button.style : ""}
115115
<style>
116116
code { white-space: pre-wrap; }

src/service/MarkdownService.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,29 @@ class MarkdownService implements vscode.Disposable {
5050
return this.engine.render(md, env);
5151
}
5252

53-
public getStyles(): string {
54-
return [this.getBuiltinStyles(), this.getSettingsStyles()].join(os.EOL);
53+
public getStyles(panel: vscode.WebviewPanel | undefined): string {
54+
return [this.getBuiltinStyles(panel), this.getDefaultStyle()].join(os.EOL);
5555
}
5656

57-
private getBuiltinStyles(): string {
57+
private getBuiltinStyles(panel: vscode.WebviewPanel | undefined): string {
5858
let styles: vscode.Uri[] = [];
5959
try {
6060
const stylePaths: string[] = require(path.join(this.config.extRoot, "package.json"))["contributes"][
6161
"markdown.previewStyles"
6262
];
6363
styles = stylePaths.map((p: string) =>
64-
vscode.Uri.file(path.join(this.config.extRoot, p)).with({
65-
scheme: "vscode-resource",
66-
})
64+
vscode.Uri.file(path.join(this.config.extRoot, p))
6765
);
6866
} catch (error) {
6967
BABA.getProxy(BabaStr.LogOutputProxy).get_log().appendLine("[Error] Fail to load built-in markdown style file.");
7068
}
71-
return styles
72-
.map((style: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${style.toString()}">`)
69+
let bbb = styles
70+
.map((style: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${panel?.webview.asWebviewUri(style)}">`)
7371
.join(os.EOL);
72+
return bbb
7473
}
7574

76-
private getSettingsStyles(): string {
75+
private getDefaultStyle(): string {
7776
return [
7877
`<style>`,
7978
`body {`,

src/solution/SolutionModule.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 2022 ccagml . All rights reserved.
88
*/
99

10-
import { ViewColumn } from "vscode";
10+
import { ViewColumn, Uri } from "vscode";
1111
import { BaseWebViewService } from "../service/BaseWebviewService";
1212
import { markdownService } from "../service/MarkdownService";
1313
import { IWebViewOption } from "../model/ConstDefind";
@@ -59,7 +59,7 @@ class SolutionService extends BaseWebViewService {
5959
}
6060

6161
private getHintsContent(): string {
62-
const styles: string = markdownService.getStyles();
62+
const styles: string = markdownService.getStyles(this.panel);
6363
let h = this.hints;
6464
let body: Array<any> = [];
6565
if (h.length == 0) {
@@ -71,21 +71,25 @@ class SolutionService extends BaseWebViewService {
7171
body.push(hint_body);
7272
}
7373
}
74+
75+
let kates_css_path = path.join(
76+
__dirname,
77+
"..",
78+
"..",
79+
"..",
80+
"resources",
81+
"katexcss",
82+
"kates.min.css"
83+
)
84+
const catGifSrc = this.panel?.webview.asWebviewUri(Uri.file(kates_css_path));
85+
7486
return `
7587
<!DOCTYPE html>
7688
<html>
7789
<head>
7890
<meta http-equiv="Content-Security-Policy" content="default-src self; img-src vscode-resource:; script-src vscode-resource: 'self' 'unsafe-inline'; style-src vscode-resource: 'self' 'unsafe-inline'; "/>
7991
${styles}
80-
<link rel="stylesheet" type="text/css" href= "vscode-resource:${path.join(
81-
__dirname,
82-
"..",
83-
"..",
84-
"..",
85-
"resources",
86-
"katexcss",
87-
"kates.min.css"
88-
)}">
92+
<link rel="stylesheet" type="text/css" href= "${catGifSrc}">
8993
</head>
9094
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
9195
${body.join("\n")}
@@ -95,7 +99,7 @@ class SolutionService extends BaseWebViewService {
9599
}
96100

97101
private getSolutionContent(): string {
98-
const styles: string = markdownService.getStyles();
102+
const styles: string = markdownService.getStyles(this.panel);
99103
const { title, url, lang, author, votes } = this.solution;
100104
const head: string = markdownService.render(`# [${title}](${url})`);
101105
const auth: string = this.solution.is_cn
@@ -120,22 +124,16 @@ class SolutionService extends BaseWebViewService {
120124
});
121125
// "<link rel=\"stylesheet\" type=\"text/css\" href=\"vscode-resource:/home/cc/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/extensions/markdown-language-features/media/markdown.css\">\n<link rel=\"stylesheet\" type=\"text/css\" href=\"vscode-resource:/home/cc/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/extensions/markdown-language-features/media/highlight.css\">\n<style>\nbody {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', system-ui, 'Ubuntu', 'Droid Sans', sans-serif;\n font-size: 14px;\n line-height: 1.6;\n}\n</style>"
122126
// <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
127+
let kates_css_path = path.join(__dirname, "..", "..", "..", "resources", "katexcss", "kates.min.css")
128+
const catGifSrc = this.panel?.webview.asWebviewUri(Uri.file(kates_css_path));
123129

124130
return `
125131
<!DOCTYPE html>
126132
<html>
127133
<head>
128134
<meta http-equiv="Content-Security-Policy" content="default-src self; img-src vscode-resource:; script-src vscode-resource: 'self' 'unsafe-inline'; style-src vscode-resource: 'self' 'unsafe-inline'; "/>
129135
${styles}
130-
<link rel="stylesheet" type="text/css" href= "vscode-resource:${path.join(
131-
__dirname,
132-
"..",
133-
"..",
134-
"..",
135-
"resources",
136-
"katexcss",
137-
"kates.min.css"
138-
)}">
136+
<link rel="stylesheet" type="text/css" href= "${catGifSrc}">
139137
</head>
140138
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
141139
${head}

0 commit comments

Comments
 (0)