Skip to content

Commit 75eb619

Browse files
authored
feat(Utility): update getTheme return value (#7874)
* feat(Utility): update getTheme return value * doc: 代码格式化 * chore: bump version 10.5.1-beta08
1 parent a06b36f commit 75eb619

File tree

2 files changed

+26
-77
lines changed

2 files changed

+26
-77
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.5.1-beta07</Version>
4+
<Version>10.5.1-beta08</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 25 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import EventHandler from "./event-handler.js"
33
const vibrate = () => {
44
if ('vibrate' in window.navigator) {
55
window.navigator.vibrate([200, 100, 200])
6-
const handler = window.setTimeout(function () {
6+
const handler = window.setTimeout(function() {
77
window.clearTimeout(handler)
88
window.navigator.vibrate([])
99
}, 1000)
@@ -175,16 +175,10 @@ const normalizeLink = link => {
175175
return url
176176
}
177177

178-
/**
179-
* 添加 script 标签到 head
180-
* @param {string} content
181-
* @returns
182-
*/
183178
const addScript = content => {
184-
// content 文件名
185179
const scripts = [...document.getElementsByTagName('script')]
186180
const url = normalizeLink(content)
187-
let link = scripts.filter(function (link) {
181+
let link = scripts.filter(function(link) {
188182
return link.src.indexOf(url) > -1
189183
})
190184
if (link.length === 0) {
@@ -207,72 +201,41 @@ const addScript = content => {
207201
})
208202
}
209203

210-
/**
211-
* 从 head 移除 script 标签
212-
* @param {string} content
213-
*/
214204
const removeScript = content => {
215205
const links = [...document.getElementsByTagName('script')]
216206
const url = normalizeLink(content)
217-
const nodes = links.filter(function (link) {
207+
const nodes = links.filter(function(link) {
218208
return link.src.indexOf(url) > -1
219209
})
220210
for (let index = 0; index < nodes.length; index++) {
221211
document.body.removeChild(nodes[index])
222212
}
223213
}
224214

225-
/**
226-
* 批量添加 script 标签到 head
227-
* @param {string[]} content
228-
* @returns
229-
*/
230215
const addScriptBatch = content => {
231216
const promises = content.map(item => addScript(item));
232217
return Promise.all(promises);
233218
}
234219

235-
/**
236-
* 从 head 批量移除 script 标签
237-
* @param {string[]} content
238-
* @returns
239-
*/
240220
const removeScriptBatch = (content) => {
241221
const promises = content.map(item => removeScript(item));
242222
return Promise.all(promises);
243223
}
244224

245-
/**
246-
* 批量添加 link 标签到 head
247-
* @param {string[]} href
248-
* @param {string} rel
249-
* @returns
250-
*/
251225
const addLinkBatch = (href, rel = "stylesheet") => {
252226
const promises = href.map(item => addLink(item, rel));
253227
return Promise.all(promises);
254228
}
255229

256-
/**
257-
* 从 head 批量移除 link 标签
258-
* @param {string[]} href
259-
* @returns
260-
*/
261230
const removeLinkBatch = (href) => {
262231
const promises = href.map(item => removeLink(item));
263232
return Promise.all(promises);
264233
}
265234

266-
/**
267-
* 添加 link 标签到 head
268-
* @param {string} href
269-
* @param {string} rel
270-
* @returns
271-
*/
272235
const addLink = (href, rel = "stylesheet") => {
273236
const links = [...document.getElementsByTagName('link')]
274237
const url = normalizeLink(href)
275-
let link = links.filter(function (link) {
238+
let link = links.filter(function(link) {
276239
return link.href.indexOf(url) > -1
277240
})
278241
if (link.length === 0) {
@@ -296,25 +259,17 @@ const addLink = (href, rel = "stylesheet") => {
296259
})
297260
}
298261

299-
/**
300-
* 从 head 移除 link 标签
301-
* @param {string} href
302-
*/
303262
const removeLink = href => {
304263
const links = [...document.getElementsByTagName('link')]
305264
const url = normalizeLink(href)
306-
const nodes = links.filter(function (link) {
265+
const nodes = links.filter(function(link) {
307266
return link.href.indexOf(url) > -1
308267
})
309268
for (let index = 0; index < nodes.length; index++) {
310269
document.getElementsByTagName("head")[0].removeChild(nodes[index])
311270
}
312271
}
313272

314-
/**
315-
* 自动识别 css 或者 js 链接并添加到 head
316-
* @param {string[]} fileList
317-
*/
318273
const autoAdd = (fileList) => {
319274
const promises = fileList.map(async (item) => {
320275
const extension = item.match(/\.(\w+)(\?|$)/)[1];
@@ -329,10 +284,6 @@ const autoAdd = (fileList) => {
329284
return Promise.all(promises);
330285
}
331286

332-
/**
333-
* 自动识别 css 或者 js 链接并从 head 中移除
334-
* @param {string[]} fileList
335-
*/
336287
const autoRemove = (fileList) => {
337288
const promises = fileList.map(async (item) => {
338289
const extension = item.match(/\.(\w+)(\?|$)/)[1];
@@ -456,7 +407,6 @@ const isElement = object => {
456407
}
457408

458409
const getElement = object => {
459-
// it's a jQuery object or a node element
460410
if (isElement(object)) {
461411
return object.jquery ? object[0] : object
462412
}
@@ -495,18 +445,15 @@ const getTransitionDurationFromElement = (element) => {
495445
return 0
496446
}
497447

498-
// Get transition-duration of the element
499448
let { transitionDuration, transitionDelay } = window.getComputedStyle(element)
500449

501450
const floatTransitionDuration = Number.parseFloat(transitionDuration)
502451
const floatTransitionDelay = Number.parseFloat(transitionDelay)
503452

504-
// Return 0 if element or transition duration is not found
505453
if (!floatTransitionDuration && !floatTransitionDelay) {
506454
return 0
507455
}
508456

509-
// If multiple durations are defined, take the first
510457
transitionDuration = transitionDuration.split(',')[0]
511458
transitionDelay = transitionDelay.split(',')[0]
512459

@@ -519,7 +466,6 @@ const isVisible = element => {
519466
}
520467

521468
const elementIsVisible = getComputedStyle(element).getPropertyValue('visibility') === 'visible'
522-
// Handle `details` element as its content may falsie appear visible when it is closed
523469
const closedDetails = element.closest('details:not([open])')
524470

525471
if (!closedDetails) {
@@ -572,10 +518,10 @@ const hackPopover = (popover, css) => {
572518
}
573519
}
574520

575-
const hackTooltip = function () {
521+
const hackTooltip = function() {
576522
const mock = () => {
577523
const originalDispose = bootstrap.Tooltip.prototype.dispose;
578-
bootstrap.Tooltip.prototype.dispose = function () {
524+
bootstrap.Tooltip.prototype.dispose = function() {
579525
originalDispose.call(this);
580526
// fix https://github.com/twbs/bootstrap/issues/37474
581527
this._activeTrigger = {};
@@ -608,14 +554,9 @@ const getOverflowParent = element => {
608554
return parent
609555
}
610556

611-
/*
612-
* @param {function} fn - 原函数
613-
* @param {number} duration - 防抖时长
614-
* @return {function} - 条件回调返回真时立即执行
615-
*/
616-
const debounce = function (fn, duration = 200, callback = null) {
557+
const debounce = function(fn, duration = 200, callback = null) {
617558
let handler = null
618-
return function () {
559+
return function() {
619560
if (handler) {
620561
clearTimeout(handler)
621562
}
@@ -716,8 +657,20 @@ export function getHtml(options) {
716657
return html;
717658
}
718659

719-
export function getTheme() {
720-
return localStorage.getItem('theme') || document.documentElement.getAttribute('data-bs-theme') || getAutoThemeValue();
660+
export function getTheme(useLocalstorage = true) {
661+
useLocalstorage = useLocalstorage ?? true;
662+
let theme = null;
663+
if (useLocalstorage) {
664+
theme = localStorage.getItem('theme');
665+
}
666+
else {
667+
theme = document.documentElement.getAttribute('data-bs-theme');
668+
}
669+
670+
if (theme === null || theme === 'auto') {
671+
theme = getAutoThemeValue();
672+
}
673+
return theme;
721674
}
722675

723676
export function saveTheme(theme) {
@@ -806,7 +759,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) {
806759
window.BootstrapBlazor[name] = window.BootstrapBlazor[name] || {
807760
_init: false,
808761
_items: [],
809-
register: function (id, cb) {
762+
register: function(id, cb) {
810763
if (id) {
811764
this._items.push(id);
812765
}
@@ -818,7 +771,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) {
818771
}
819772
return this;
820773
},
821-
dispose: function (id, cb) {
774+
dispose: function(id, cb) {
822775
if (id) {
823776
this._items = this._items.filter(item => item !== id);
824777
}
@@ -876,10 +829,6 @@ export function drawImage(canvas, image, offsetWidth, offsetHeight) {
876829
context.drawImage(image, 0, 0, offsetWidth, offsetHeight);
877830
}
878831

879-
/**
880-
* @param {File} file
881-
* @returns {Blob}
882-
*/
883832
export function readFileAsync(file) {
884833
return new Promise((resolve, reject) => {
885834
const reader = new FileReader();

0 commit comments

Comments
 (0)