preload.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { contextBridge, ipcRenderer } from 'electron';
  2. // --------- Expose some API to the Renderer process ---------
  3. contextBridge.exposeInMainWorld('ipcRenderer', {
  4. invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
  5. const [channel, ...omit] = args;
  6. return ipcRenderer.invoke(channel, ...omit);
  7. },
  8. off(...args: Parameters<typeof ipcRenderer.off>) {
  9. const [channel, ...omit] = args;
  10. return ipcRenderer.off(channel, ...omit);
  11. },
  12. on(...args: Parameters<typeof ipcRenderer.on>) {
  13. const [channel, listener] = args;
  14. return ipcRenderer.on(channel, (event, ...args) =>
  15. listener(event, ...args),
  16. );
  17. },
  18. send(...args: Parameters<typeof ipcRenderer.send>) {
  19. const [channel, ...omit] = args;
  20. return ipcRenderer.send(channel, ...omit);
  21. },
  22. // You can expose other APTs you need here.
  23. // ...
  24. });
  25. // --------- Preload scripts loading ---------
  26. // function domReady(
  27. // condition: DocumentReadyState[] = ['complete', 'interactive'],
  28. // ) {
  29. // return new Promise((resolve) => {
  30. // if (condition.includes(document.readyState)) {
  31. // resolve(true);
  32. // } else {
  33. // document.addEventListener('readystatechange', () => {
  34. // if (condition.includes(document.readyState)) {
  35. // resolve(true);
  36. // }
  37. // });
  38. // }
  39. // });
  40. // }
  41. // const safeDOM = {
  42. // append(parent: HTMLElement, child: HTMLElement) {
  43. // if (![...parent.children].includes(child)) {
  44. // return parent.append(child);
  45. // }
  46. // },
  47. // remove(parent: HTMLElement, child: HTMLElement) {
  48. // if ([...parent.children].includes(child)) {
  49. // return child.remove();
  50. // }
  51. // },
  52. // };
  53. // function useLoading() {
  54. // const className = `loaders-css__square-spin`;
  55. // const styleContent = `
  56. // @keyframes square-spin {
  57. // 25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
  58. // 50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
  59. // 75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
  60. // 100% { transform: perspective(100px) rotateX(0) rotateY(0); }
  61. // }
  62. // .${className} > div {
  63. // animation-fill-mode: both;
  64. // width: 50px;
  65. // height: 50px;
  66. // background: #fff;
  67. // animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
  68. // }
  69. // .app-loading-wrap {
  70. // position: fixed;
  71. // top: 0;
  72. // left: 0;
  73. // width: 100vw;
  74. // height: 100vh;
  75. // display: flex;
  76. // align-items: center;
  77. // justify-content: center;
  78. // background: #282c34;
  79. // z-index: 9;
  80. // }
  81. // `;
  82. // const oStyle = document.createElement('style');
  83. // const oDiv = document.createElement('div');
  84. // oStyle.id = 'app-loading-style';
  85. // oStyle.innerHTML = styleContent;
  86. // oDiv.className = 'app-loading-wrap';
  87. // oDiv.innerHTML = `<div class="${className}"><div></div></div>`;
  88. // return {
  89. // appendLoading() {
  90. // safeDOM.append(document.head, oStyle);
  91. // safeDOM.append(document.body, oDiv);
  92. // },
  93. // removeLoading() {
  94. // safeDOM.remove(document.head, oStyle);
  95. // safeDOM.remove(document.body, oDiv);
  96. // },
  97. // };
  98. // }
  99. // const { appendLoading, removeLoading } = useLoading();
  100. // domReady().then(appendLoading);
  101. // window.onmessage = (ev) => {
  102. // ev.data.payload === 'removeLoading' && removeLoading();
  103. // };
  104. // setTimeout(removeLoading, 4999);