Skip to content

Commit

Permalink
fix: 优化软件启动加载
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 23, 2022
1 parent 48fb520 commit be866d1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
38 changes: 28 additions & 10 deletions packages/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,41 @@
</template>

<script setup lang="ts">
import Index from './pages/index.vue';
import { nextTick, onMounted } from 'vue';
import Title from './components/Title.vue';
import { notify } from './utils/notify';
import Index from './pages/index.vue';
import { handleFile, initOpenFiles, setAutoLaunch, setZoomFactor } from './store';
import { fetchRemoteNotify } from './utils';
import { notify } from './utils/notify';
import { remote } from './utils/remote';
const { ipcRenderer } = require('electron');
/** 获取最新远程通知 */
fetchRemoteNotify(false);
onMounted(() => {
nextTick(() => {
/** 获取最新远程通知 */
fetchRemoteNotify(false);
/** 如果正在更新的话,获取更新进度 */
const { ipcRenderer } = require('electron');
ipcRenderer.on('update', (e, tag, rate, totalLength, chunkLength) => {
notify(
'OCS更新程序',
/** 如果正在更新的话,获取更新进度 */
ipcRenderer.on('update', (e, tag, rate, totalLength, chunkLength) => {
notify(
'OCS更新程序',
`更新中: ${(chunkLength / 1024 / 1024).toFixed(2)}MB/${(totalLength / 1024 / 1024).toFixed(2)}MB`,
'updater',
{ type: 'info', duration: 5, close: false }
);
);
});
/** 初始化 store */
remote.logger.call('info', 'render store init');
setZoomFactor();
setAutoLaunch();
initOpenFiles();
ipcRenderer.on('open-file', (e, file) => {
handleFile(file);
});
});
});
</script>

Expand Down
14 changes: 7 additions & 7 deletions packages/web/src/components/Title.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<a-menu class="title-menu">
<TitleLink
title="教程"
url="https://github.com/enncy/online-course-script#readme"
url="https://enncy.github.io/online-course-script/"
/>
<a-menu-item @click="about">
关于
Expand All @@ -85,7 +85,7 @@

<TitleLink
title="版本更新"
url="https://github.com/enncy/online-course-script/releases"
url="https://enncy.github.io/online-course-script/app-version"
/>
<TitleLink
title="脚本更新日志"
Expand Down Expand Up @@ -119,15 +119,15 @@
</template>

<script setup lang="ts">
import { Modal } from 'ant-design-vue';
import favicon from 'root/public/favicon.ico';
import { h, ref, watch } from 'vue';
import { config } from '../config';
import { router, routes } from '../route';
import { remote } from '../utils/remote';
import TitleLink from './TitleLink.vue';
import { fetchRemoteNotify, formatDate } from '../utils';
import { remote } from '../utils/remote';
import { path } from './file/File';
import { h, ref, watch } from 'vue';
import favicon from 'root/public/favicon.ico';
import { Modal } from 'ant-design-vue';
import TitleLink from './TitleLink.vue';

const { shell } = require('electron');

Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/file/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ import { ITerminal } from '../terminal';
import { Process } from '../terminal/process';
import Terminal from '../terminal/Terminal.vue';
import { FileNode, fs, validFileContent } from './File';

const { scriptNames } = require('@ocsjs/scripts');
const childProcess = require('child_process') as typeof import('child_process');

Expand All @@ -217,7 +218,6 @@ if (typeof result === 'string') {
} else {
error = result.error;
}
console.log('data.options', options);

const data = reactive<{
activeKey: 'setting' | 'terminal' | 'content'
Expand Down
29 changes: 8 additions & 21 deletions packages/web/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fs, showFile } from '../components/file/File';
import { Project } from '../components/project';
import { remote } from '../utils/remote';
import { FileNode } from './../components/file/File';
const { ipcRenderer } = require('electron');

const Store = require('electron-store');
const { getValidBrowserPaths } = require('@ocsjs/common') as typeof import('@ocsjs/common');

Expand All @@ -13,7 +13,8 @@ const s = new Store();
/** 工作区数据 */
export const workspace = reactive<{
projects: Project[],
opened: FileNode[]
opened: FileNode[],

}>({
projects: [],
/** 打开的文件 */
Expand Down Expand Up @@ -63,25 +64,11 @@ export const store = reactive({
validBrowserPaths: (s.get('validBrowserPaths') as ReturnType<typeof getValidBrowserPaths>) || getValidBrowserPaths()
});

window.addEventListener('load', () => {
setTimeout(() => {
remote.logger.call('info', 'render store init');

setZoomFactor();
autoLaunch();
initOpenFiles();
}, 1000);
});

ipcRenderer.on('open-file', (e, file) => {
handleFile(file);
});

watch(store, (newStore) => {
s.store = JSON.parse(JSON.stringify(newStore));
});

watch(() => store['auto-launch'], autoLaunch);
watch(() => store['auto-launch'], setAutoLaunch);
watch(() => store.win.size, setZoomFactor);

/** 监听打开的文件,保留工作区打开的文件 */
Expand All @@ -92,15 +79,15 @@ watch(
}
);

function autoLaunch() {
export function setAutoLaunch() {
remote.methods.call('autoLaunch');
}

function setZoomFactor() {
export function setZoomFactor() {
remote.webContents.call('setZoomFactor', store.win.size);
}

function handleFile(file: string) {
export function handleFile(file: string) {
if (fs.existsSync(String(file))) {
workspace.opened.push(Project.createFileNode(String(file)));

Expand All @@ -115,7 +102,7 @@ function handleFile(file: string) {
/**
* 处理打开的文件
*/
function initOpenFiles() {
export function initOpenFiles() {
store.files = Array.from(new Set(store.files));
for (const file of store.files) {
handleFile(String(file));
Expand Down

0 comments on commit be866d1

Please sign in to comment.