-
-
Notifications
You must be signed in to change notification settings - Fork 382
feat(Utility): update getTheme return value #7874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import EventHandler from "./event-handler.js" | |
| const vibrate = () => { | ||
| if ('vibrate' in window.navigator) { | ||
| window.navigator.vibrate([200, 100, 200]) | ||
| const handler = window.setTimeout(function () { | ||
| const handler = window.setTimeout(function() { | ||
| window.clearTimeout(handler) | ||
| window.navigator.vibrate([]) | ||
| }, 1000) | ||
|
|
@@ -175,16 +175,10 @@ const normalizeLink = link => { | |
| return url | ||
| } | ||
|
|
||
| /** | ||
| * 添加 script 标签到 head | ||
| * @param {string} content | ||
| * @returns | ||
| */ | ||
| const addScript = content => { | ||
| // content 文件名 | ||
| const scripts = [...document.getElementsByTagName('script')] | ||
| const url = normalizeLink(content) | ||
| let link = scripts.filter(function (link) { | ||
| let link = scripts.filter(function(link) { | ||
| return link.src.indexOf(url) > -1 | ||
| }) | ||
| if (link.length === 0) { | ||
|
|
@@ -207,72 +201,41 @@ const addScript = content => { | |
| }) | ||
| } | ||
|
|
||
| /** | ||
| * 从 head 移除 script 标签 | ||
| * @param {string} content | ||
| */ | ||
| const removeScript = content => { | ||
| const links = [...document.getElementsByTagName('script')] | ||
| const url = normalizeLink(content) | ||
| const nodes = links.filter(function (link) { | ||
| const nodes = links.filter(function(link) { | ||
| return link.src.indexOf(url) > -1 | ||
| }) | ||
| for (let index = 0; index < nodes.length; index++) { | ||
| document.body.removeChild(nodes[index]) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 批量添加 script 标签到 head | ||
| * @param {string[]} content | ||
| * @returns | ||
| */ | ||
| const addScriptBatch = content => { | ||
| const promises = content.map(item => addScript(item)); | ||
| return Promise.all(promises); | ||
| } | ||
|
|
||
| /** | ||
| * 从 head 批量移除 script 标签 | ||
| * @param {string[]} content | ||
| * @returns | ||
| */ | ||
| const removeScriptBatch = (content) => { | ||
| const promises = content.map(item => removeScript(item)); | ||
| return Promise.all(promises); | ||
| } | ||
|
|
||
| /** | ||
| * 批量添加 link 标签到 head | ||
| * @param {string[]} href | ||
| * @param {string} rel | ||
| * @returns | ||
| */ | ||
| const addLinkBatch = (href, rel = "stylesheet") => { | ||
| const promises = href.map(item => addLink(item, rel)); | ||
| return Promise.all(promises); | ||
| } | ||
|
|
||
| /** | ||
| * 从 head 批量移除 link 标签 | ||
| * @param {string[]} href | ||
| * @returns | ||
| */ | ||
| const removeLinkBatch = (href) => { | ||
| const promises = href.map(item => removeLink(item)); | ||
| return Promise.all(promises); | ||
| } | ||
|
|
||
| /** | ||
| * 添加 link 标签到 head | ||
| * @param {string} href | ||
| * @param {string} rel | ||
| * @returns | ||
| */ | ||
| const addLink = (href, rel = "stylesheet") => { | ||
| const links = [...document.getElementsByTagName('link')] | ||
| const url = normalizeLink(href) | ||
| let link = links.filter(function (link) { | ||
| let link = links.filter(function(link) { | ||
| return link.href.indexOf(url) > -1 | ||
| }) | ||
| if (link.length === 0) { | ||
|
|
@@ -296,25 +259,17 @@ const addLink = (href, rel = "stylesheet") => { | |
| }) | ||
| } | ||
|
|
||
| /** | ||
| * 从 head 移除 link 标签 | ||
| * @param {string} href | ||
| */ | ||
| const removeLink = href => { | ||
| const links = [...document.getElementsByTagName('link')] | ||
| const url = normalizeLink(href) | ||
| const nodes = links.filter(function (link) { | ||
| const nodes = links.filter(function(link) { | ||
| return link.href.indexOf(url) > -1 | ||
| }) | ||
| for (let index = 0; index < nodes.length; index++) { | ||
| document.getElementsByTagName("head")[0].removeChild(nodes[index]) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 自动识别 css 或者 js 链接并添加到 head | ||
| * @param {string[]} fileList | ||
| */ | ||
| const autoAdd = (fileList) => { | ||
| const promises = fileList.map(async (item) => { | ||
| const extension = item.match(/\.(\w+)(\?|$)/)[1]; | ||
|
|
@@ -329,10 +284,6 @@ const autoAdd = (fileList) => { | |
| return Promise.all(promises); | ||
| } | ||
|
|
||
| /** | ||
| * 自动识别 css 或者 js 链接并从 head 中移除 | ||
| * @param {string[]} fileList | ||
| */ | ||
| const autoRemove = (fileList) => { | ||
| const promises = fileList.map(async (item) => { | ||
| const extension = item.match(/\.(\w+)(\?|$)/)[1]; | ||
|
|
@@ -456,7 +407,6 @@ const isElement = object => { | |
| } | ||
|
|
||
| const getElement = object => { | ||
| // it's a jQuery object or a node element | ||
| if (isElement(object)) { | ||
| return object.jquery ? object[0] : object | ||
| } | ||
|
|
@@ -495,18 +445,15 @@ const getTransitionDurationFromElement = (element) => { | |
| return 0 | ||
| } | ||
|
|
||
| // Get transition-duration of the element | ||
| let { transitionDuration, transitionDelay } = window.getComputedStyle(element) | ||
|
|
||
| const floatTransitionDuration = Number.parseFloat(transitionDuration) | ||
| const floatTransitionDelay = Number.parseFloat(transitionDelay) | ||
|
|
||
| // Return 0 if element or transition duration is not found | ||
| if (!floatTransitionDuration && !floatTransitionDelay) { | ||
| return 0 | ||
| } | ||
|
|
||
| // If multiple durations are defined, take the first | ||
| transitionDuration = transitionDuration.split(',')[0] | ||
| transitionDelay = transitionDelay.split(',')[0] | ||
|
|
||
|
|
@@ -519,7 +466,6 @@ const isVisible = element => { | |
| } | ||
|
|
||
| const elementIsVisible = getComputedStyle(element).getPropertyValue('visibility') === 'visible' | ||
| // Handle `details` element as its content may falsie appear visible when it is closed | ||
| const closedDetails = element.closest('details:not([open])') | ||
|
|
||
| if (!closedDetails) { | ||
|
|
@@ -572,10 +518,10 @@ const hackPopover = (popover, css) => { | |
| } | ||
| } | ||
|
|
||
| const hackTooltip = function () { | ||
| const hackTooltip = function() { | ||
| const mock = () => { | ||
| const originalDispose = bootstrap.Tooltip.prototype.dispose; | ||
| bootstrap.Tooltip.prototype.dispose = function () { | ||
| bootstrap.Tooltip.prototype.dispose = function() { | ||
| originalDispose.call(this); | ||
| // fix https://github.com/twbs/bootstrap/issues/37474 | ||
| this._activeTrigger = {}; | ||
|
|
@@ -608,14 +554,9 @@ const getOverflowParent = element => { | |
| return parent | ||
| } | ||
|
|
||
| /* | ||
| * @param {function} fn - 原函数 | ||
| * @param {number} duration - 防抖时长 | ||
| * @return {function} - 条件回调返回真时立即执行 | ||
| */ | ||
| const debounce = function (fn, duration = 200, callback = null) { | ||
| const debounce = function(fn, duration = 200, callback = null) { | ||
| let handler = null | ||
| return function () { | ||
| return function() { | ||
| if (handler) { | ||
| clearTimeout(handler) | ||
| } | ||
|
|
@@ -716,8 +657,20 @@ export function getHtml(options) { | |
| return html; | ||
| } | ||
|
|
||
| export function getTheme() { | ||
| return localStorage.getItem('theme') || document.documentElement.getAttribute('data-bs-theme') || getAutoThemeValue(); | ||
| export function getTheme(useLocalstorage = true) { | ||
| useLocalstorage = useLocalstorage ?? true; | ||
| let theme = null; | ||
| if (useLocalstorage) { | ||
| theme = localStorage.getItem('theme'); | ||
| } | ||
| else { | ||
| theme = document.documentElement.getAttribute('data-bs-theme'); | ||
| } | ||
|
|
||
| if (theme === null || theme === 'auto') { | ||
| theme = getAutoThemeValue(); | ||
| } | ||
| return theme; | ||
|
Comment on lines
+660
to
+673
|
||
| } | ||
|
|
||
| export function saveTheme(theme) { | ||
|
|
@@ -806,7 +759,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) { | |
| window.BootstrapBlazor[name] = window.BootstrapBlazor[name] || { | ||
| _init: false, | ||
| _items: [], | ||
| register: function (id, cb) { | ||
| register: function(id, cb) { | ||
| if (id) { | ||
| this._items.push(id); | ||
| } | ||
|
|
@@ -818,7 +771,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) { | |
| } | ||
| return this; | ||
| }, | ||
| dispose: function (id, cb) { | ||
| dispose: function(id, cb) { | ||
| if (id) { | ||
| this._items = this._items.filter(item => item !== id); | ||
| } | ||
|
|
@@ -876,10 +829,6 @@ export function drawImage(canvas, image, offsetWidth, offsetHeight) { | |
| context.drawImage(image, 0, 0, offsetWidth, offsetHeight); | ||
| } | ||
|
|
||
| /** | ||
| * @param {File} file | ||
| * @returns {Blob} | ||
| */ | ||
| export function readFileAsync(file) { | ||
| return new Promise((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter name
useLocalstorageis inconsistent with the establishedlocalStoragecasing (and with the existinguseLocalStoragestring used byThemeValue.UseLocalStorage). Renaming it touseLocalStoragewould improve readability and reduce confusion when this exported API is used directly from JS.