Skip to content

Commit 8463dfc

Browse files
committed
feat: 添加虚拟滚动配置功能,移除冗余的 executing 状态设置,修复 sql cell 在虚拟列表下高度计算
1 parent 6d3feee commit 8463dfc

9 files changed

Lines changed: 116 additions & 57 deletions

File tree

apps/docs/src/lab/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { LibroLabModule, AppExtention } from '@difizen/libro-lab';
22

33
import { LibroApp } from './app.js';
4+
import { VirtualizationConfigContribution } from './virtualize-config-contribution.js';
45
import './index.less';
56

67
const { ManaAppPreset, ManaComponents, ManaModule } = AppExtention;
78

8-
const BaseModule = ManaModule.create().register(LibroApp);
9+
const BaseModule = ManaModule.create().register(
10+
LibroApp,
11+
VirtualizationConfigContribution,
12+
);
913

1014
const App = (): JSX.Element => {
1115
return (
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
ApplicationContribution,
3+
ConfigurationService,
4+
} from '@difizen/libro-common/app';
5+
import { inject, singleton } from '@difizen/libro-common/app';
6+
import {
7+
VirtualizationEnabled,
8+
VirtualizationThresholdCellCount,
9+
VirtualizationThresholdFileSize,
10+
} from '@difizen/libro-core';
11+
import { LibroViewTracker } from '@difizen/libro-core';
12+
13+
@singleton({ contrib: ApplicationContribution })
14+
export class VirtualizationConfigContribution implements ApplicationContribution {
15+
@inject(ConfigurationService) configurationService: ConfigurationService;
16+
@inject(LibroViewTracker) protected libroViewTracker: LibroViewTracker;
17+
18+
async onStart() {
19+
this.configurationService.set(VirtualizationEnabled, false);
20+
this.configurationService.set(VirtualizationThresholdCellCount, 50);
21+
this.configurationService.set(VirtualizationThresholdFileSize, 100 * 1024);
22+
}
23+
}

packages/libro-core/src/components/dnd-component/dnd-list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const DndCellsRender = forwardRef<
174174
// TODO: 类型处理
175175
const model = libroView.model as any;
176176
if (model.currentFileContents && model.currentFileContents.size) {
177-
size = parseFloat((model.currentFileContents.size / 1048576).toFixed(3)); // 单位MB
177+
size = model.currentFileContents.size; // 单位 byte
178178
path = model.currentFileContents.path || '';
179179
}
180180

packages/libro-core/src/components/dnd-component/virtualized-manager.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/libro-core/src/libro-setting-contribution.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
LargeOutputDisplay,
1111
MultiSelectionWhenShiftClick,
1212
RightContentFixed,
13+
VirtualizationEnabled,
14+
VirtualizationThresholdCellCount,
15+
VirtualizationThresholdFileSize,
1316
} from './libro-setting.js';
1417

1518
@singleton({ contrib: ConfigurationContribution })
@@ -25,6 +28,9 @@ export class LibroSettingContribution implements ConfigurationContribution {
2528
MultiSelectionWhenShiftClick,
2629
RightContentFixed,
2730
LargeOutputDisplay,
31+
VirtualizationEnabled,
32+
VirtualizationThresholdCellCount,
33+
VirtualizationThresholdFileSize,
2834
];
2935
}
3036
}

packages/libro-core/src/libro-setting.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,40 @@ export const LargeOutputDisplay: ConfigurationNode<boolean> = {
130130
type: 'boolean',
131131
},
132132
};
133+
134+
export const VirtualizationEnabled: ConfigurationNode<boolean> = {
135+
id: 'libro.virtualization.enabled',
136+
description: l10n.t('是否启用虚拟滚动功能'),
137+
title: l10n.t('虚拟滚动'),
138+
type: 'checkbox',
139+
defaultValue: false,
140+
schema: {
141+
type: 'boolean',
142+
},
143+
};
144+
145+
export const VirtualizationThresholdCellCount: ConfigurationNode<number> = {
146+
id: 'libro.virtualization.threshold.cellcount',
147+
description: l10n.t('启用虚拟滚动所需的cell数量阈值'),
148+
title: l10n.t('虚拟滚动 cell 数量阈值'),
149+
type: 'input',
150+
defaultValue: 50,
151+
schema: {
152+
type: 'number',
153+
minimum: 1,
154+
maximum: 1000,
155+
},
156+
};
157+
158+
export const VirtualizationThresholdFileSize: ConfigurationNode<number> = {
159+
id: 'libro.virtualization.threshold.filesize',
160+
description: l10n.t('启用虚拟滚动所需的文件大小阈值,单位 byte'),
161+
title: l10n.t('虚拟滚动文件大小阈值'),
162+
type: 'input',
163+
defaultValue: 100 * 1024, // 100KB
164+
schema: {
165+
type: 'number',
166+
minimum: 1,
167+
maximum: 1024 * 1024 * 1024, // 1GB
168+
},
169+
};

packages/libro-core/src/libro-view.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,14 +1045,12 @@ export class LibroView extends BaseView implements NotebookView {
10451045
ExecutableCellModel.is(selectedCell.model)
10461046
) {
10471047
selectedCell.clearExecution();
1048-
selectedCell.model.executing = false;
10491048
selectedCell.model.hasOutputHidden = false;
10501049
}
10511050
}
10521051
} else {
10531052
if (ExecutableCellView.is(cell) && ExecutableCellModel.is(cell.model)) {
10541053
cell.clearExecution();
1055-
cell.model.executing = false;
10561054
cell.model.hasOutputHidden = false;
10571055
}
10581056
}
@@ -1064,7 +1062,6 @@ export class LibroView extends BaseView implements NotebookView {
10641062
for (const cell of this.model.cells) {
10651063
if (ExecutableCellView.is(cell) && ExecutableCellModel.is(cell.model)) {
10661064
cell.clearExecution();
1067-
cell.model.executing = false;
10681065
cell.model.hasOutputHidden = false;
10691066
}
10701067
}
Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { transient, prop, inject } from '@difizen/libro-common/app';
2+
import { ConfigurationService } from '@difizen/libro-common/app';
23

34
import type { NotebookModel } from './libro-protocol.js';
45
import { VirtualizedManagerOption } from './libro-protocol.js';
6+
import {
7+
VirtualizationEnabled,
8+
VirtualizationThresholdFileSize,
9+
VirtualizationThresholdCellCount,
10+
} from './libro-setting.js';
511

612
export interface IVirtualizedManager {
713
openVirtualized: (length: number, size?: number, path?: string) => Promise<boolean>;
@@ -18,32 +24,49 @@ export class VirtualizedManager implements IVirtualizedManager {
1824
isVirtualized = false;
1925

2026
libroModel: NotebookModel;
27+
protected configurationService: ConfigurationService;
2128

2229
constructor(
2330
@inject(VirtualizedManagerOption)
2431
virtualizedManagerOption: VirtualizedManagerOption,
32+
@inject(ConfigurationService) configurationService: ConfigurationService,
2533
) {
2634
this.libroModel = virtualizedManagerOption.libroModel;
35+
this.configurationService = configurationService;
2736
}
2837

2938
/**
3039
*
3140
* @param length cell个数
32-
* @param size undefined 或者 单位 为B
41+
* @param size undefined 或者 number,单位为 byte
3342
* @returns 是否使用虚拟滚动
3443
*/
35-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3644
openVirtualized = async (length: number, size?: number, path?: string) => {
45+
// 获取配置项
46+
const virtualizationEnabled =
47+
await this.configurationService.get(VirtualizationEnabled);
48+
const virtualizationThresholdFileSize = await this.configurationService.get(
49+
VirtualizationThresholdFileSize,
50+
);
51+
const virtualizationThresholdCellCount = await this.configurationService.get(
52+
VirtualizationThresholdCellCount,
53+
);
54+
55+
if (!virtualizationEnabled) {
56+
this.isVirtualized = false;
57+
return false;
58+
}
59+
60+
// 如果启用虚拟化,根据阈值判断是否使用虚拟滚动
61+
if (
62+
length >= virtualizationThresholdCellCount ||
63+
(size && size >= virtualizationThresholdFileSize)
64+
) {
65+
this.isVirtualized = true;
66+
return true;
67+
}
68+
3769
this.isVirtualized = false;
3870
return false;
39-
// this.isVirtualized = true;
40-
// return true;
41-
// if (length > 100 || (size && size > 4)) {
42-
// this.isVirtualized = true;
43-
// return true;
44-
// } else {
45-
// this.isVirtualized = false;
46-
// return false;
47-
// }
4871
};
4972
}

packages/libro-sql-cell/src/libro-sql-cell-view.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ export class LibroSqlCellView extends LibroEditableExecutableCellView {
306306
this.codeEditorManager.getUserEditorConfig(this.model);
307307
const codeHeight = countLines(this.model.value) * lineHeight!;
308308
const editorPadding = paddingTop + paddingBottom;
309-
310309
const scrollbarHeight = 12;
311310

312311
// TODO: 滚动条有条件显示
@@ -391,10 +390,17 @@ export class LibroSqlCellView extends LibroEditableExecutableCellView {
391390
}
392391

393392
override onViewResize = (size: ViewSize): void => {
394-
// 把 header 部分高度也放在这部分,用来撑开高度
395-
if (size.height) {
396-
this.editorAreaHeight = size.height + 36;
393+
// 只有在编辑器未加载或加载中时才更新 editorAreaHeight
394+
if (
395+
this.editorStatus === EditorStatus.NOTLOADED ||
396+
this.editorStatus === EditorStatus.LOADING
397+
) {
398+
// 把 header 部分高度也放在这部分,用来撑开高度
399+
if (size.height) {
400+
this.editorAreaHeight = size.height + 36;
401+
}
397402
}
403+
// 编辑器已加载后,高度由 ResizeObserver 等机制自动管理
398404
};
399405

400406
handleDbChange(value: string) {

0 commit comments

Comments
 (0)