Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ process.env['PUBLIC'] = app.isPackaged
? process.env['DIST']
: join(process.env['DIST_ELECTRON'], '../public');

import { app, BrowserWindow, shell, ipcMain, Tray, Menu } from 'electron';
import { app, BrowserWindow, shell, ipcMain, Tray, Menu, nativeTheme } from 'electron';
import { release } from 'os';
import { join, dirname } from 'path';
import { setMenu } from './menu';
Expand Down Expand Up @@ -110,6 +110,12 @@ ipcMain.on('drop-file', (_event, filePath: string) => {
win?.setRepresentedFilename(filePath);
});

ipcMain.on('changeSystemTheme', (_event, value: any) => {
console.log(value, 9666);
// 记录最近打开的文件
nativeTheme.themeSource = value;
});

// 渲染线程请求关闭窗口
ipcMain.on('close-window', () => {
win?.close();
Expand Down
26 changes: 25 additions & 1 deletion electron/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Menu, dialog, BrowserWindow, MenuItem } from 'electron';
import { Menu, dialog, BrowserWindow, MenuItem, nativeTheme } from 'electron';
import { readMDFile } from '../utils/file';

export function setMenu(getWin: () => BrowserWindow | null) {
Expand Down Expand Up @@ -47,6 +47,30 @@ export function setMenu(getWin: () => BrowserWindow | null) {
}),
];

// 主题切换菜单
oldMenu.insert(
5,
new MenuItem({
label: 'Theme',
submenu: [
{
label: 'light',
click() {
nativeTheme.themeSource = 'light';
getWin()?.webContents.send('changeTheme', 'light');
},
},
{
label: 'dark',
click() {
nativeTheme.themeSource = 'dark';
getWin()?.webContents.send('changeTheme', 'dark');
},
},
],
}),
);

const fileMenuSub = fileMenu.submenu as Menu;
for (let i = template.length - 1; i >= 0; i--) {
const item = template[i as keyof typeof template] as MenuItem;
Expand Down
17 changes: 15 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const isShowClass = ref<string>('');
const editorRef = ref();
const activeTitleIndex = ref(0);
const directory = ref<MDDirectory[]>([]);
// const theme = ref('');

// // 暂定
// ipcRenderer.on('changeTheme', (event, value) => {
// console.log(event, value);
// theme.value = value;
// });

watch(
mdStore,
Expand All @@ -26,7 +33,7 @@ watch(
);

watch(
() => store.getIsShowCatalogue,
() => store.isShowCatalogue,
(newVal: boolean) => {
if (newVal) {
isShowClass.value = '';
Expand All @@ -38,7 +45,13 @@ watch(
</script>

<template>
<div class="app-main" draggable="true" @drop.stop.prevent="mdStore.onDrop" @dragover.stop.prevent>
<div
class="app-main"
:class="store.theme"
draggable="true"
@drop.stop.prevent="mdStore.onDrop"
@dragover.stop.prevent
>
<section class="directory" :class="isShowClass">
<md-directory
:active-title-index="activeTitleIndex"
Expand Down
23 changes: 23 additions & 0 deletions src/assets/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 暂定
.dark {
background: #1a2229;
color: #fff;

.md-directory li {
color: #fff;
&.active {
color: #fff;
}
&:hover {
background: #626262;
}
}
.md-editor {
position: relative;
box-sizing: border-box;
.toastui-editor-defaultUI {
border-radius: 0;
border: #1a2229;
}
}
}
25 changes: 22 additions & 3 deletions src/components/MdEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import tableMergedCell from '@toast-ui/editor-plugin-table-merged-cell';
import '@toast-ui/editor/dist/toastui-editor.css';
import '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight.css';
import '@toast-ui/editor-plugin-color-syntax/dist/toastui-editor-plugin-color-syntax.css';
import '@toast-ui/editor/dist/theme/toastui-editor-dark.css';
import 'prismjs/themes/prism.min.css';
import '@toast-ui/editor/dist/i18n/zh-cn';
import { debounce } from '@mxssfd/ts-utils';
Expand Down Expand Up @@ -35,7 +36,7 @@ function saveFile() {

type EditorMode = 'wysiwyg' | 'markdown';

let editor: Editor;
let editor: any;
let lastHeadingList: HTMLElement[];
let lastGetHeadingListTime = Date.now();

Expand Down Expand Up @@ -116,6 +117,19 @@ onUnmounted(() => {
window.removeEventListener('resize', listener);
});

watch(
() => store.theme,
(newVal) => {
const editorDom: any = editorDomRef.value;
console.log(newVal);
if (newVal === 'dark') {
editorDom.classList.add('toastui-editor-dark');
} else {
editorDom.classList.remove('toastui-editor-dark');
}
},
);

onMounted(() => {
editor = new Editor({
el: editorDomRef.value as HTMLElement,
Expand All @@ -128,7 +142,12 @@ onMounted(() => {
plugins: [[codeSyntaxHighlight, { highlighter: Prism }], colorSyntax, tableMergedCell],
});

// editor.changeMode('wysiwyg');
const editorDom: any = editorDomRef.value;
if (localStorage.getItem('theme') === 'dark') {
editorDom.classList.add('toastui-editor-dark');
} else {
editorDom.classList.remove('toastui-editor-dark');
}

editor.on('change', () => {
mdStore.content = editor.getMarkdown();
Expand Down Expand Up @@ -176,7 +195,7 @@ onMounted(() => {
});

const isShowClick = () => {
store.setIsShowCatalogue(!store.getIsShowCatalogue);
store.setIsShowCatalogue(!store.isShowCatalogue);
};
</script>
<template>
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createApp } from 'vue';
import App from './App.vue';
import './samples/node-api';
import pinia from './store/init';
import './assets/index.scss';
createApp(App)
.use(pinia)
.mount('#app')
Expand Down
38 changes: 24 additions & 14 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { defineStore } from 'pinia';
import { reactive, toRefs } from 'vue';
import { ipcRenderer } from 'electron';

export const useStore = defineStore('store', {
state: () => {
return {
isShowCatalogue: true,
};
},
getters: {
getIsShowCatalogue: (state) => {
return state.isShowCatalogue;
export const useStore = defineStore('store', () => {
const state = reactive({ isShowCatalogue: true, theme: localStorage.getItem('theme') });

const actions = reactive({
setIsShowCatalogue(isShowCatalogue: boolean): void {
state.isShowCatalogue = isShowCatalogue;
},
},
actions: {
setIsShowCatalogue(isShowCatalogue: boolean) {
this.isShowCatalogue = isShowCatalogue;
setTheme(theme: any): void {
localStorage.setItem('theme', theme);
state.theme = theme;
},
},
});

function addListener(): void {
ipcRenderer.send('changeSystemTheme', localStorage.getItem('theme'));

ipcRenderer.on('changeTheme', (event, value) => {
// state.theme = value;
console.log(63311);
actions.setTheme(value);
});
}
addListener();
return { ...toRefs(state), ...actions };
});