Skip to content
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

> 在 issue 或者 QQ 群中提问 wiki 中有的内容,极有可能被无视或者攻击。


## 完成功能

- 查看题目
Expand Down
23 changes: 19 additions & 4 deletions src/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const vscode = {
showQuickPick: () => Promise.resolve(undefined),
showOpenDialog: () => Promise.resolve(undefined),
createWebviewPanel: () => ({
webview: { html: '', onDidReceiveMessage: () => ({ dispose: () => {} }), postMessage: () => {} },
webview: {
html: '',
onDidReceiveMessage: () => ({ dispose: () => {} }),
postMessage: () => {}
},
onDidDispose: () => {},
dispose: () => {}
}),
Expand All @@ -31,8 +35,16 @@ const vscode = {
workspaceFolders: undefined
},
Uri: {
file: (path: string) => ({ fsPath: path, scheme: 'file', toString: () => path }),
parse: (uri: string) => ({ fsPath: uri, scheme: 'file', toString: () => uri })
file: (path: string) => ({
fsPath: path,
scheme: 'file',
toString: () => path
}),
parse: (uri: string) => ({
fsPath: uri,
scheme: 'file',
toString: () => uri
})
},
ViewColumn: { One: 1, Two: 2, Three: 3, Active: -1 },
env: {
Expand All @@ -41,7 +53,10 @@ const vscode = {
},
EventEmitter: class {
listeners: unknown[] = [];
event = (listener: unknown) => { this.listeners.push(listener); return { dispose: () => {} }; };
event = (listener: unknown) => {
this.listeners.push(listener);
return { dispose: () => {} };
};
fire() {}
dispose() {}
},
Expand Down
6 changes: 5 additions & 1 deletion src/commands/traininglist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import * as vscode from 'vscode';
import { searchTrainingdetail, searchTraininglist } from '@/utils/api';
import { getResourceFilePath } from '@/utils/html';
import { showTrainDetails } from '@/utils/showTrainDetails';
import { getUsernameColor, getUserSvg, getWebviewViewColumn } from '@/utils/workspaceUtils';
import {
getUsernameColor,
getUserSvg,
getWebviewViewColumn
} from '@/utils/workspaceUtils';

export default new SuperCommand({
onCommand: 'traininglist',
Expand Down
6 changes: 5 additions & 1 deletion src/features/login/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export default class LuoguAuthProvider
clientID: await genClientID(),
name: ''
});
vscode.commands.executeCommand('setContext', 'luoguLoginStatus', false);
vscode.commands.executeCommand(
'setContext',
'luoguLoginStatus',
false
);
finishlock();
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/features/myArticle/fsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ export default class myArticleFsProvider
try {
const articleData = (await getArticle(uri.query)).data;
const res = articleData.article;
if (!articleData.canEdit)
throw vscode.FileSystemError.NoPermissions(uri);
if (!articleData.canEdit) throw vscode.FileSystemError.NoPermissions(uri);
if (res.promoteStatus === 2)
if (
(await vscode.window.showWarningMessage(
Expand Down
5 changes: 4 additions & 1 deletion src/features/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import * as vscode from 'vscode';
import { fetchRecords, fetchResult } from '@/utils/api';
import { getDistFilePath } from '@/utils/html';
import { createWebsocket, WebsocketSchema } from '@/utils/websocket';
import { processAxiosError, getWebviewViewColumn } from '@/utils/workspaceUtils';
import {
processAxiosError,
getWebviewViewColumn
} from '@/utils/workspaceUtils';
import { RecordData } from 'luogu-api';
import { MessageTypes } from '@w/views/record/data';

Expand Down
5 changes: 4 additions & 1 deletion src/features/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import * as vscode from 'vscode';
import type { ArticleDetails, List } from 'luogu-api';
import { getSolution, voteArticle } from '@/utils/api';
import { getDistFilePath } from '@/utils/html';
import { processAxiosError, getWebviewViewColumn } from '@/utils/workspaceUtils';
import {
processAxiosError,
getWebviewViewColumn
} from '@/utils/workspaceUtils';
import useWebviewResponseHandle from '@/utils/webviewResponse';
import ArticleData from '@/model/article';

Expand Down
11 changes: 7 additions & 4 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,13 @@ interface TagsResponse {

export const fetchLuoguTags = async (): Promise<TagsResponse> => {
try {
const res = await axios.get<TagsResponse>('https://www.luogu.com.cn/_lfe/tags/zh-CN', {
myInterceptors_notCheckCookie: true,
myInterceptors_cookie: null
});
const res = await axios.get<TagsResponse>(
'https://www.luogu.com.cn/_lfe/tags/zh-CN',
{
myInterceptors_notCheckCookie: true,
myInterceptors_cookie: null
}
);
return res.data;
} catch (err) {
throw new Error('获取标签数据失败', { cause: err });
Expand Down
5 changes: 4 additions & 1 deletion src/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ export const formatTime = (
const lens = matched.length === 1 ? 3 : matched.length;
fmt = fmt.replace(
matched,
('00' + o[k]).slice(('' + o[k]).length - 1, ('' + o[k]).length - 1 + lens)
('00' + o[k]).slice(
('' + o[k]).length - 1,
('' + o[k]).length - 1 + lens
)
);
} else {
fmt = fmt.replace(
Expand Down
14 changes: 8 additions & 6 deletions src/utils/showTrainDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ const getDifficultyStatus = (difficulty: number) => {
};
const getTagsStatus = async (tagIds: number[]) => {
const resolved = await tagManager.getTags(tagIds);
return resolved.map((tag, i) => {
const id = tagIds[i];
const name = tag?.name ?? `未知标签(${id})`;
const color = tag?.color ?? '#000000';
return `<span class="lfe-caption" style="color: rgb(255, 255, 255); background-color: ${color}">${name}</span>&nbsp;`;
}).join('');
return resolved
.map((tag, i) => {
const id = tagIds[i];
const name = tag?.name ?? `未知标签(${id})`;
const color = tag?.color ?? '#000000';
return `<span class="lfe-caption" style="color: rgb(255, 255, 255); background-color: ${color}">${name}</span>&nbsp;`;
})
.join('');
};
export class TrainDetals {
public title: string;
Expand Down
12 changes: 9 additions & 3 deletions src/utils/tagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class TagManager {
const cached = globalThis.__luoguTagsCache;
const cacheTime = globalThis.__luoguTagsCacheTime;
if (cached && cacheTime && Date.now() - cacheTime < CACHE_DURATION) {
this.tags = new Map(Object.entries(cached).map(([k, v]) => [Number(k), v as Tag]));
this.tags = new Map(
Object.entries(cached).map(([k, v]) => [Number(k), v as Tag])
);
return true;
}
return false;
Expand All @@ -36,7 +38,10 @@ class TagManager {
try {
const res = await fetchLuoguTags();
const typeColors = new Map(
res.types.map(t => [t.id, isColorKey(t.color) ? ColorPalette[t.color] : '#000000'])
res.types.map(t => [
t.id,
isColorKey(t.color) ? ColorPalette[t.color] : '#000000'
])
);
this.tags.clear();
this.types = typeColors;
Expand All @@ -57,7 +62,8 @@ class TagManager {
}

private async ensureLoaded(): Promise<void> {
if (this.tags.size > 0 && Date.now() - this.lastFetch < CACHE_DURATION) return;
if (this.tags.size > 0 && Date.now() - this.lastFetch < CACHE_DURATION)
return;
if (!this.loadFromCache()) await this.fetchTags();
}

Expand Down
18 changes: 11 additions & 7 deletions src/utils/uiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,15 @@ let needLoginShown = false;
export const needLogin = () => {
if (needLoginShown) return;
needLoginShown = true;
vscode.window.showErrorMessage('未登录', '登录').then(async c => {
if (c) vscode.commands.executeCommand('luogu.signin');
}).then(() => {
setTimeout(() => { needLoginShown = false; }, 5000);
}, () => {
setTimeout(() => { needLoginShown = false; }, 5000);
});
// showErrorMessage 返回 Thenable 而非 Promise(无 finally),需包装为真正的 Promise
Promise.resolve(vscode.window.showErrorMessage('未登录', '登录'))
.then(async c => {
if (c) vscode.commands.executeCommand('luogu.signin');
})
// 无论用户是否点击,5 秒后都重置标记以允许再次提示
.finally(() => {
setTimeout(() => {
needLoginShown = false;
}, 5000);
});
};
10 changes: 8 additions & 2 deletions webview/utils/tags.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const tagsMap: Record<number, { name: string; color: string }> = {};
const tagsJson = document.getElementById('luogu-tags')?.innerText;
if (tagsJson) {
for (const t of JSON.parse(tagsJson) as { id: number; name: string; color: string }[]) {
for (const t of JSON.parse(tagsJson) as {
id: number;
name: string;
color: string;
}[]) {
tagsMap[t.id] = { name: t.name, color: t.color };
}
}

export function getTag(id: number): { name: string; color: string } | undefined {
export function getTag(
id: number
): { name: string; color: string } | undefined {
return tagsMap[id];
}