mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-02-26 13:00:09 +08:00
refactor: optimize code
This commit is contained in:
parent
b6a24b71eb
commit
6f18c6fbfb
@ -1,19 +0,0 @@
|
|||||||
import { useLoading } from './loading'
|
|
||||||
|
|
||||||
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
if (condition.includes(document.readyState)) {
|
|
||||||
resolve(true)
|
|
||||||
} else {
|
|
||||||
document.addEventListener('readystatechange', () => {
|
|
||||||
if (condition.includes(document.readyState)) {
|
|
||||||
resolve(true)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const { appendLoading, removeLoading } = useLoading()
|
|
||||||
window.removeLoading = removeLoading
|
|
||||||
domReady().then(appendLoading)
|
|
@ -1,10 +1,38 @@
|
|||||||
|
|
||||||
|
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
if (condition.includes(document.readyState)) {
|
||||||
|
resolve(true)
|
||||||
|
} else {
|
||||||
|
document.addEventListener('readystatechange', () => {
|
||||||
|
if (condition.includes(document.readyState)) {
|
||||||
|
resolve(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const safeDOM = {
|
||||||
|
append(parent: HTMLElement, child: HTMLElement) {
|
||||||
|
if (!Array.from(parent.children).find(e => e === child)) {
|
||||||
|
return parent.appendChild(child)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
remove(parent: HTMLElement, child: HTMLElement) {
|
||||||
|
if (Array.from(parent.children).find(e => e === child)) {
|
||||||
|
return parent.removeChild(child)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://tobiasahlin.com/spinkit
|
* https://tobiasahlin.com/spinkit
|
||||||
* https://connoratherton.com/loaders
|
* https://connoratherton.com/loaders
|
||||||
* https://projects.lukehaas.me/css-loaders
|
* https://projects.lukehaas.me/css-loaders
|
||||||
* https://matejkustec.github.io/SpinThatShit
|
* https://matejkustec.github.io/SpinThatShit
|
||||||
*/
|
*/
|
||||||
export function useLoading() {
|
function useLoading() {
|
||||||
const className = `loaders-css__square-spin`
|
const className = `loaders-css__square-spin`
|
||||||
const styleContent = `
|
const styleContent = `
|
||||||
@keyframes square-spin {
|
@keyframes square-spin {
|
||||||
@ -43,25 +71,18 @@
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
appendLoading() {
|
appendLoading() {
|
||||||
safe.append(document.head, oStyle)
|
safeDOM.append(document.head, oStyle)
|
||||||
safe.append(document.body, oDiv)
|
safeDOM.append(document.body, oDiv)
|
||||||
},
|
},
|
||||||
removeLoading() {
|
removeLoading() {
|
||||||
safe.remove(document.head, oStyle)
|
safeDOM.remove(document.head, oStyle)
|
||||||
safe.remove(document.body, oDiv)
|
safeDOM.remove(document.body, oDiv)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const safe = {
|
// ----------------------------------------------------------------------
|
||||||
append(parent: HTMLElement, child: HTMLElement) {
|
|
||||||
if (!Array.from(parent.children).find(e => e === child)) {
|
const { appendLoading, removeLoading } = useLoading()
|
||||||
return parent.appendChild(child)
|
window.removeLoading = removeLoading
|
||||||
}
|
domReady().then(appendLoading)
|
||||||
},
|
|
||||||
remove(parent: HTMLElement, child: HTMLElement) {
|
|
||||||
if (Array.from(parent.children).find(e => e === child)) {
|
|
||||||
return parent.removeChild(child)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user