vscode/src/vs/workbench/contrib/example/browser/views/exampleView.ts

183 lines
7.1 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IViewPaneOptions, ViewPane } from '../../../../browser/parts/views/viewPane.js';
import { IKeybindingService } from '../../../../../platform/keybinding/common/keybinding.js';
import { IContextMenuService } from '../../../../../platform/contextview/browser/contextView.js';
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';
import { IContextKeyService } from '../../../../../platform/contextkey/common/contextkey.js';
import { IViewDescriptorService } from '../../../../common/views.js';
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
import { IOpenerService } from '../../../../../platform/opener/common/opener.js';
import { IThemeService } from '../../../../../platform/theme/common/themeService.js';
import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.js';
import { IAccessibleViewService } from '../../../../../platform/accessibility/browser/accessibleView.js';
import { IHoverService } from '../../../../../platform/hover/browser/hover.js';
import * as DOM from '../../../../../base/browser/dom.js';
import { URI } from '../../../../../base/common/uri.js';
import './media/exampleViewContainer.css';
import { IEditorService } from '../../../../services/editor/common/editorService.js';
import { StockDetailsInput } from '../editors/StockDetailsInput.js';
import { IWorkbenchLayoutService, Parts } from '../../../../services/layout/browser/layoutService.js';
import { IStockDetailsService, StockDetail } from '../../common/stockDetailsService.js';
import { httpService } from '../../common/stockHttpService.js';
export class ExampleView extends ViewPane {
constructor(
options: IViewPaneOptions,
@IKeybindingService keybindingService: IKeybindingService,
@IContextMenuService contextMenuService: IContextMenuService,
@IConfigurationService configurationService: IConfigurationService,
@IContextKeyService contextKeyService: IContextKeyService,
@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
@IInstantiationService instantiationService: IInstantiationService,
@IOpenerService openerService: IOpenerService,
@IThemeService themeService: IThemeService,
@ITelemetryService telemetryService: ITelemetryService,
@IHoverService hoverService: IHoverService,
@IAccessibleViewService accessibleViewService: IAccessibleViewService,
@IEditorService private readonly editorService: IEditorService,
@IStockDetailsService private readonly stockDetailsService: IStockDetailsService
) {
super(
options,
keybindingService,
contextMenuService,
configurationService,
contextKeyService,
viewDescriptorService,
instantiationService,
openerService,
themeService,
telemetryService,
hoverService,
);
// 监听活动编辑器的变化
this.editorService.onDidActiveEditorChange(() => this.onActiveEditorChange());
}
// override renderBody(container: HTMLElement): void {
// super.renderBody(container);
// const ul = DOM.$('ul'); // 创建一个无序列表
// ul.classList.add('stock-list'); // 为列表添加样式类
// const data = [
// { name: 'Stock A', code: '000001', price: 100 },
// { name: 'Stock B', code: '000002', price: 200 },
// { name: 'Stock C', code: '000003', price: 300 },
// { name: 'Stock D', code: '000004', price: 400 },
// { name: 'Stock E', code: '000005', price: 500 },
// ]; // 假数据
// for (const stock of data) {
// const li = DOM.$('li');
// li.textContent = `${stock.name} (${stock.code}) ¥${stock.price.toFixed(2)}`;
// li.classList.add('stock-item');
// li.addEventListener('click', () => {
// this.openStockDetails(stock); // 点击时打开股票详情
// });
// ul.appendChild(li);
// }
// container.appendChild(ul); // 将列表附加到容器中
// }
override async renderBody(container: HTMLElement): Promise<void> {
super.renderBody(container);
// 保持现有的静态数据和渲染逻辑
const ul = DOM.$('ul');
ul.classList.add('stock-list');
const data = [
{ name: 'Stock A', code: '000001', price: 100 },
{ name: 'Stock B', code: '000002', price: 200 },
{ name: 'Stock C', code: '000003', price: 300 },
{ name: 'Stock D', code: '000004', price: 400 },
{ name: 'Stock E', code: '000005', price: 500 },
];
for (const stock of data) {
const li = DOM.$('li');
li.textContent = `${stock.name} (${stock.code}) ¥${stock.price.toFixed(2)}`;
li.classList.add('stock-item');
li.addEventListener('click', () => {
this.openStockDetails(stock);
});
ul.appendChild(li);
}
container.appendChild(ul);
// 发起 POST 请求
const requestBody = {
pageNo: 1,
pageSize: 10,
year: '1',
indexType: 'hs300'
};
console.log('即将进入请求调用');
try {
const response = await httpService.fetchPostJson('https://www.shidaotec.com/api/strategy/selectStockPageList', requestBody);
console.log('API Response:', response); // 请求完成后再打印
} catch (err) {
console.error('Error fetching stock data:', err); // 捕获异常
}
console.log('已步过');
}
private openStockDetails(stock: { name: string; code: string; price: number }): void {
if (!stock.code || stock.code.trim() === '') {
console.error('无法打开股票详情:股票代码为空');
return;
}
console.log(`准备打开股票详情: ${stock.name} (${stock.code})`);
const uri = URI.parse(`stock-details://${stock.code}`); // 自定义 URI
const editorInput = new StockDetailsInput(uri); // 创建新的输入实例
const editorService = this.instantiationService.invokeFunction(accessor => accessor.get(IEditorService)); // 获取编辑器服务
editorService.openEditor(editorInput, { pinned: true }).then(() => {
console.log(`成功打开股票详情: ${stock.name} (${stock.code})`);
}).catch(err => {
console.error(`打开股票详情失败: ${err.message}`);
});
// 通知服务选中股票
const stockDetailsService = this.instantiationService.invokeFunction(accessor => accessor.get(IStockDetailsService));
stockDetailsService.selectStock(stock);
// 打开副侧边栏
const layoutService = this.instantiationService.invokeFunction(accessor => accessor.get(IWorkbenchLayoutService));
layoutService.setPartHidden(false, Parts.AUXILIARYBAR_PART); // 打开副侧边栏
}
private onActiveEditorChange(): void {
const activeEditor = this.editorService.activeEditor;
console.log('活动编辑器已更改:', activeEditor);
if (activeEditor instanceof StockDetailsInput) {
const stockDetail: StockDetail = {
code: activeEditor.getCode() // 创建包含 code 的对象
};
this.stockDetailsService.selectStock(stockDetail); // 传递对象
}
}
override layoutBody(height: number, width: number): void {
// 布局逻辑,如果需要调整高度和宽度
}
}