Skip to content

Commit 11f6248

Browse files
committed
feat: 更改 FullScreen 为服务
1 parent 4a53e3b commit 11f6248

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

src/BootstrapBlazor/Components/FullScreen/FullScreenService.cs renamed to src/BootstrapBlazor/Services/FullScreenService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ namespace BootstrapBlazor.Components;
88
/// <summary>
99
/// FullScreen 服务
1010
/// </summary>
11-
public class FullScreenService : BootstrapServiceBase<FullScreenOption>
11+
public class FullScreenService(IJSRuntime jSRuntime)
1212
{
13+
[NotNull]
14+
private JSModule? _module = null;
15+
1316
/// <summary>
1417
/// 全屏方法,已经全屏时再次调用后退出全屏
1518
/// </summary>
1619
/// <param name="option"></param>
20+
/// <param name="token"></param>
1721
/// <returns></returns>
18-
public Task Toggle(FullScreenOption? option = null) => Invoke(option ?? new());
22+
public async Task Toggle(FullScreenOption? option = null, CancellationToken token = default)
23+
{
24+
_module ??= await jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/fullscreen.js");
25+
await _module.InvokeAsync<string?>("toggle", token, option);
26+
}
1927
}

src/BootstrapBlazor/wwwroot/modules/fullscreen.js

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
11
import { isElement } from "./utility.js"
2-
import Data from "./data.js"
32

4-
export function init(id) {
5-
const fs = { toggleElement: null };
6-
Data.set(id, fs)
7-
8-
fs.toggle = options => {
9-
if (options.id) {
10-
fs.toggleElement = document.getElementById(options.id)
11-
}
12-
else if (options.element && isElement(options.element)) {
13-
fs.toggleElement = el
14-
}
15-
else {
16-
fs.toggleElement = document.documentElement
17-
}
3+
export function toggle(options) {
4+
let el = null;
5+
options = options || {};
6+
if (options.id) {
7+
el = document.getElementById(options.id);
8+
}
9+
else if (options.element && isElement(options.element)) {
10+
el = options.element;
11+
}
12+
else {
13+
el = document.documentElement
14+
}
1815

16+
if (el !== null) {
1917
if (isFullscreen()) {
20-
exit()
18+
exit();
2119
}
2220
else {
23-
fs.enter()
21+
enterFullscreen(el);
2422
}
2523
}
24+
}
2625

27-
fs.enter = () => {
28-
fs.toggleElement.requestFullscreen() ||
29-
fs.toggleElement.webkitRequestFullscreen ||
30-
fs.toggleElement.mozRequestFullScreen ||
31-
fs.toggleElement.msRequestFullscreen
26+
const enterFullscreen = el => {
27+
el.requestFullscreen() || el.webkitRequestFullscreen || el.mozRequestFullScreen || el.msRequestFullscreen
3228

33-
// 处理 ESC 按键退出全屏
34-
var handler = setTimeout(() => {
35-
clearTimeout(handler);
29+
// 处理 ESC 按键退出全屏
30+
var handler = setTimeout(() => {
31+
clearTimeout(handler);
3632

37-
const fullscreenCheck = () => {
38-
if (!isFullscreen()) {
39-
fs.toggleElement.classList.remove('bb-fs-open');
40-
document.documentElement.classList.remove('bb-fs-open');
41-
}
42-
else {
43-
fs.toggleElement.classList.add('bb-fs-open')
44-
requestAnimationFrame(fullscreenCheck);
45-
}
33+
const fullscreenCheck = () => {
34+
if (!isFullscreen()) {
35+
el.classList.remove('bb-fs-open');
36+
document.documentElement.classList.remove('bb-fs-open');
4637
}
47-
requestAnimationFrame(fullscreenCheck);
48-
}, 200);
49-
}
50-
}
51-
52-
export function execute(id, options) {
53-
const fs = Data.get(id)
54-
if (fs) {
55-
fs.toggle(options);
56-
}
57-
}
58-
59-
export function dispose(id) {
60-
Data.remove(id)
38+
else {
39+
el.classList.add('bb-fs-open')
40+
requestAnimationFrame(fullscreenCheck);
41+
}
42+
}
43+
requestAnimationFrame(fullscreenCheck);
44+
}, 200);
6145
}
6246

6347
const isFullscreen = () => {

0 commit comments

Comments
 (0)