FreeDATA/gui_vue/electron/preload/index.ts

111 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-09-02 11:37:03 +00:00
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
2023-10-04 17:54:50 +00:00
return new Promise((resolve) => {
2023-09-02 11:37:03 +00:00
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)) {
2023-10-04 17:54:50 +00:00
return parent.appendChild(child)
2023-09-02 11:37:03 +00:00
}
},
remove(parent: HTMLElement, child: HTMLElement) {
if (Array.from(parent.children).find(e => e === child)) {
2023-10-04 17:54:50 +00:00
return parent.removeChild(child)
2023-09-02 11:37:03 +00:00
}
},
}
/**
* https://tobiasahlin.com/spinkit
* https://connoratherton.com/loaders
* https://projects.lukehaas.me/css-loaders
* https://matejkustec.github.io/SpinThatShit
*/
function useLoading() {
const className = `loaders-css__square-spin`
const styleContent = `
@keyframes square-spin {
0% {
transform: rotate(0deg);
2023-10-08 08:37:14 +00:00
background-image: url('icon_cube_border.png'); /* Replace with the URL of your image */
background-size: cover; /* Scale the image to cover the entire container */
}
25% { transform: perspective(100px) rotateX(180deg) rotateY(0);
2023-10-08 08:37:14 +00:00
background-image: url('icon_cube_border.png'); /* Replace with the URL of your image */
background-size: cover; /* Scale the image to cover the entire container */
}
50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg);
2023-10-08 08:37:14 +00:00
background-image: url('icon_cube_border.png'); /* Replace with the URL of your image */
background-size: cover; /* Scale the image to cover the entire container */
}
75% { transform: perspective(100px) rotateX(0) rotateY(180deg);
2023-10-08 08:37:14 +00:00
background-image: url('icon_cube_border.png'); /* Replace with the URL of your image */
background-size: cover; /* Scale the image to cover the entire container */
}
100% { transform: perspective(100px) rotateX(0) rotateY(0);
2023-10-08 08:37:14 +00:00
background-image: url('icon_cube_border.png'); /* Replace with the URL of your image */
background-size: cover; /* Scale the image to cover the entire container */
}
2023-09-02 11:37:03 +00:00
}
.${className} > div {
animation-fill-mode: both;
width: 50px;
height: 50px;
background: #fff;
animation: square-spin 6s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
2023-09-02 11:37:03 +00:00
}
.app-loading-wrap {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #282c34;
z-index: 99999;
2023-09-02 11:37:03 +00:00
}
`
const oStyle = document.createElement('style')
const oDiv = document.createElement('div')
oStyle.id = 'app-loading-style'
oStyle.innerHTML = styleContent
oDiv.className = 'app-loading-wrap'
oDiv.innerHTML = `<div class="${className}"><div></div></div>`
return {
appendLoading() {
safeDOM.append(document.head, oStyle)
safeDOM.append(document.body, oDiv)
},
removeLoading() {
safeDOM.remove(document.head, oStyle)
safeDOM.remove(document.body, oDiv)
},
}
}
// ----------------------------------------------------------------------
const { appendLoading, removeLoading } = useLoading()
domReady().then(appendLoading)
2023-10-04 17:54:50 +00:00
window.onmessage = (ev) => {
2023-09-02 11:37:03 +00:00
ev.data.payload === 'removeLoading' && removeLoading()
}
2023-10-04 17:54:50 +00:00
setTimeout(removeLoading, 4999)