-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
338 lines (294 loc) · 10.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<html>
<head>
<link rel="icon" href="./asserts/img/icons8-markdown-96.png" type="image/x-icon" />
<!--Import Google Icon Font-->
<!-- <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> -->
<!-- Compiled and minified CSS -->
<!-- <link rel="stylesheet" href="asserts/css/materialize.min.css">-->
<link rel="stylesheet" href="asserts/css/macos.css">
<!-- <link rel="stylesheet" href="asserts/css/github-markdown.css"> -->
<style>
body,
html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
.head-bar {
background: #eee;
height: 38px;
-webkit-app-region: drag;
text-align: center;
line-height: 38px;
border-bottom: 1px #ccc solid;
}
.right-menu {
position: fixed;
top: 0;
right: 10px;
height: 38px;
line-height: 38px;
/* background: red; */
}
.main {
display: flex;
flex-flow: column nowrap;
height: 100%;
}
.title {
color: grey;
/* overflow: hidden; */
margin-left: 78px;
margin-right: 78px;
user-select: none;
font-size: 13px;
}
.content {
/* background: #C7EDCC; */
flex: 1;
padding: 10px 50px;
overflow-y: auto;
word-break: break-all !important;
}
.center {
display: block;
margin: auto;
/* width: 100px; */
margin-top: 20px;
}
.tips {
position: fixed;
top: calc(50% - 30px);
left: 0;
text-align: center;
width: 100%;
z-index: 99;
user-select: none;
color: #aaa;
}
.tips:hover {
cursor: pointer;
color: #228B22;
}
button {
cursor: pointer;
}
.btn-lg {
font-size: 14px;
}
.btn-menu {
position: fixed;
right: 10px;
top: 5px;
z-index: 99;
border-radius: 5px;
padding: 5px;
}
.tool-bar {
display: none;
position: fixed;
z-index: 99;
padding: 5px;
left: 0;
top: 0;
height: 100%;
/* background: #ccc; */
flex-direction: column;
/* 设置为竖直排列 */
}
.tool-bar button {
margin: 5px;
/* 每个按钮之间增加一点间距(可选) */
min-height: 30px;
}
textarea {
resize: none;
}
/* .content-edit textarea {} */
</style>
</head>
<body>
<div class="main">
<!-- <div class="head-bar">
<span class="title" id="title"></span>
</div> -->
<div class="content markdown-body" id="marked"> </div>
<textarea class="content markdown-body" style="display:none;" id="edit"></textarea>
<span class="tips" id="tips">click to open a markdown file </span>
<div class="tool-bar" id="tool-bar">
<button id="btn-close">↖</button>
<button id="btn-save">✓</button>
</div>
</div>
<script src="./asserts/js/marked.min.js"></script>
<script>
var fileData = null;
var src = null;
function setMarked() {
if (!fileData) {
return false
}
document.getElementById('marked').innerHTML = marked.parse(fileData['data']);
document.getElementById("tips").style.display = 'none';
//show
document.getElementById('edit').value = '';
document.getElementById('edit').style.display = 'none';
document.getElementById('marked').style.display = '';
document.getElementById('tool-bar').style.display = 'none';
}
// function setTile(title) {
// document.getElementById('title').innerHTML = title;
// }
function loadCSS(href) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = href;
document.getElementsByTagName("head")[0].appendChild(link);
}
// 如果你想移除它,需要遍历head中的link标签并移除
function removeCSS(href) {
var links = document.getElementsByTagName('link');
for (var i = 0; i < links.length; i++) {
if (links[i].href.startsWith(href)) {
links[i].parentNode.removeChild(links[i]);
break;
}
}
}
function setTheme(theme = null) {
const THEME_GREEN = 'Eye Protection Theme'
const THEME_DEFAULT = 'Default Theme'
const THEME_DARK = 'Dark Theme'
if (!theme) {
theme = localStorage.getItem('theme');
}
removeCSS('asserts/css/github-markdown')
if (theme == THEME_DARK) {
loadCSS('asserts/css/github-markdown-dark.css')
setBg('#333')
} else if (theme == THEME_GREEN) {
loadCSS('asserts/css/github-markdown.css')
setBg('#C7EDCC')
} else if (theme == THEME_DEFAULT) {
loadCSS('asserts/css/github-markdown.css')
setBg('#fff')
}
console.log('->', 'theme:', theme);
localStorage.setItem('theme', theme)
window.electronAPI.themeChanged(theme);
}
function setBg(rgb = null) {
if (!rgb) {
if (!(rgb = localStorage.getItem('bg-rgb'))) {
rgb = '#fff'
}
}
// 使用getElementById方法根据ID获取div元素
let divNames = ['marked', 'edit']
for (i in divNames) {
let div = document.getElementById(divNames[i]);
// 检查是否成功获取到div元素
if (div) {
// 如果成功获取到,则修改其背景颜色
div.style.backgroundColor = rgb;
localStorage.setItem('bg-rgb', rgb)
console.log('->', 'rgb:', rgb);
} else {
// 如果没有找到对应的div,可以打印一条错误消息到控制台
console.error('未找到ID为' + 'marked' + '的div元素');
}
}
}
// setMarked('# Marked in browser\n\nRendered by **marked**.')
window.electronAPI.onOpenFileData((fileData) => {
// console.log(data)
window.fileData = fileData
setMarked();
// setTile(fileData['path'].split('/').pop());
})
window.electronAPI.onBgChange((rgb) => {
// console.log(data)
setBg(rgb)
})
window.electronAPI.onThemeChange((theme) => {
// console.log(data)
setTheme(theme)
})
function setFont(add = 0) {
let nowFontSize = localStorage.getItem('font-size') ? parseInt(localStorage.getItem('font-size')) : 16; // 默认字体大小为16px
if (nowFontSize + add <= 24 && nowFontSize + add >= 8) {
let divNames = ['marked', 'edit']
for (i in divNames) {
document.getElementById(divNames[i]).style.fontSize = nowFontSize + add + 'px';
}
localStorage.setItem('font-size', nowFontSize + add);
console.log('->', 'font', nowFontSize + add)
}
}
window.electronAPI.onFontSizeChange((add) => {
setFont(add);
})
function setEdit() {
if (fileData) {
console.log('->', 'edit');
document.getElementById('marked').style.display = 'none';
document.getElementById('edit').value = fileData['data'];
document.getElementById('edit').style.display = '';
document.getElementById('tool-bar').style.display = 'flex';
if (!src) {
src = fileData['data'];
}
}
}
window.electronAPI.onEdit(() => {
setEdit()
})
document.getElementById("tips").onclick = () => {
if (document.getElementById('marked').innerText.replace(/^s+|s+$/g, "") == '')
window.electronAPI.openFile();
}
document.getElementById("btn-close").onclick = () => {
saveTmp();
setMarked();
}
function saveTmp() {
window.fileData['data'] = document.getElementById('edit').value;
}
function saveEdit() {
console.log('->', 'save edit')
saveTmp();
window.electronAPI.saveEdit(window.fileData);
}
document.getElementById("btn-save").onclick = () => {
saveEdit()
}
document.getElementById("edit").oninput = () => {
// console.log('->', 'changed')
window.electronAPI.editChanged(document.getElementById('edit').value != window.src);
}
let drag = document.getElementById("marked")
//此方法删除不行
drag.addEventListener("dragover", (e) => {
// 用来hack某些兼容性问题
e.dataTransfer.dropEffect = "copy";
e.stopPropagation();
e.preventDefault();
}, false);
drag.addEventListener("drop", (e) => {
e.preventDefault();
e.stopPropagation();
console.log(e.dataTransfer.files[0].name)
// if (e.dataTransfer.files[0].path) {
// let path = e.dataTransfer.files[0].path
// console.log(path);
// }
}, false);
window.addEventListener("load", (event) => {
setTheme()
setFont()
});
</script>
</body>
</html>