Skip to content

Commit 14a1b19

Browse files
authored
perf(ContextMenu): improve performance (#5766)
* refactor: 精简代码 * perf(ContextMenu): 提高性能 * test: 更新单元测试
1 parent 3334599 commit 14a1b19

File tree

5 files changed

+31
-41
lines changed

5 files changed

+31
-41
lines changed

src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
if (context is ContextMenuDivider)
1313
{
1414
<Divider></Divider>
15-
continue;
1615
}
1716
else if (context is ContextMenuItem item)
1817
{

src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.js

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
import Data from "../../modules/data.js"
22
import EventHandler from "../../modules/event-handler.js"
33
import { createPopper, computePosition } from '../../modules/floating-ui.js'
4+
import { registerBootstrapBlazorModule } from "../../modules/utility.js"
45

56
export function init(id) {
67
const el = document.getElementById(id)
78

89
if (el) {
9-
window.bb = window.bb || {};
10-
if (bb.cancelContextMenuHandler === void 0) {
11-
bb.contextMenus = []
12-
bb.cancelContextMenuHandler = e => {
13-
const menu = document.querySelector('.bb-cm.show')
14-
if (menu) {
15-
const menuId = menu.getAttribute('id')
16-
const cm = Data.get(menuId)
17-
if (cm.popper) {
18-
cm.popper()
19-
}
20-
21-
menu.classList.remove('show')
22-
const zone = getZone(menu)
23-
if (zone) {
24-
zone.appendChild(menu)
10+
const cm = { el, zone: getZone(el) };
11+
Data.set(id, cm);
12+
13+
registerBootstrapBlazorModule("ContextMenu", id, context => {
14+
if (context.cancelContextMenuHandler === void 0) {
15+
context.cancelContextMenuHandler = e => {
16+
const menu = document.querySelector('.bb-cm.show')
17+
if (menu) {
18+
const menuId = menu.getAttribute('id')
19+
const cm = Data.get(menuId)
20+
if (cm.popper) {
21+
cm.popper()
22+
}
23+
24+
menu.classList.remove('show')
25+
const zone = getZone(menu)
26+
if (zone) {
27+
zone.appendChild(menu)
28+
}
2529
}
2630
}
2731
}
28-
EventHandler.on(document, 'click', bb.cancelContextMenuHandler)
29-
EventHandler.on(document, 'contextmenu', bb.cancelContextMenuHandler)
30-
}
31-
bb.contextMenus.push(el)
32-
33-
const cm = { el, zone: getZone(el) }
34-
Data.set(id, cm)
32+
EventHandler.on(document, 'click', context.cancelContextMenuHandler)
33+
EventHandler.on(document, 'contextmenu', context.cancelContextMenuHandler)
34+
})
3535
}
3636
}
3737

@@ -64,19 +64,11 @@ export function dispose(id) {
6464
cm.popper()
6565
}
6666

67-
window.bb = window.bb || { contextMenus: [] }
68-
const index = bb.contextMenus.indexOf(el)
69-
if (index > -1) {
70-
bb.contextMenus.splice(index, 1)
71-
}
72-
73-
if (bb.contextMenus.length === 0) {
74-
if (bb.cancelContextMenuHandler) {
75-
EventHandler.off(document, 'click', bb.cancelContextMenuHandler)
76-
EventHandler.off(document, 'contextmenu', bb.cancelContextMenuHandler)
77-
}
78-
delete bb.cancelContextMenuHandler;
79-
}
67+
const { ContextMenu } = window.BootstrapBlazor;
68+
ContextMenu.dispose(id, context => {
69+
EventHandler.off(document, 'click', context.cancelContextMenuHandler)
70+
EventHandler.off(document, 'contextmenu', context.cancelContextMenuHandler)
71+
});
8072
}
8173
}
8274

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) {
822822
}
823823
if (this._init === false) {
824824
this._init = true;
825-
cb();
825+
cb(this);
826826
}
827827
return this;
828828
},
@@ -832,7 +832,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) {
832832
}
833833
if (this._items.length === 0 && cb) {
834834
this._init = false;
835-
cb();
835+
cb(this);
836836
}
837837
}
838838
};

test/UnitTest/Components/LayoutTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public void Authorized_Ok()
581581
pb.Add(a => a.AdditionalAssemblies, new Assembly[] { GetType().Assembly });
582582
pb.Add(a => a.OnAuthorizing, url => Task.FromResult(true));
583583
});
584-
cut.Contains("<section class=\"layout\" style=\"--bb-layout-header-height: 0px; --bb-layout-footer-height: 0px;\"><main class=\"layout-main\"></main></section>");
584+
cut.MarkupMatches("<section id:ignore class=\"layout\" style=\"--bb-layout-header-height: 0px; --bb-layout-footer-height: 0px;\"><main class=\"layout-main\"></main></section>");
585585
Context.DisposeComponents();
586586
}
587587
}

test/UnitTest/Services/ThrottleTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ await Assert.ThrowsAnyAsync<InvalidOperationException>(() => dispatcher.Throttle
9595
{
9696
count++;
9797
});
98-
Assert.Equal(2, count);
9998
}
10099

101100
[Fact]

0 commit comments

Comments
 (0)