-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
523 lines (485 loc) · 20.4 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!DOCTYPE html>
<html lang="en">
<head>
<title>NoteToCard</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<style type="text/css">
html {
width: 100%;
height: 100%;
}
body {
width: calc(100% - 24px);
height: calc(100% - 16px);
}
#container {
width: 100%;
height: 100%;
display: flex;
flex-flow: column;
}
#block-markdown {
width: 100%;
flex: 1;
}
input {
width: 95%;
}
</style>
<body>
<div id="container">
<table id="table">
<tr>
<td><label title="NoteToCard 笔记制卡服务 URL">服务</label></td>
<td><input id="ntc-server" type="text" placeholder="http(s)://host:port/path" value="https://notetocard.com/obsidian"></input></td>
<td><label title="NoteToCard 制卡类型">类型</label></td>
<td>
<select id="ntc-handler">
<option selected>NoteToCard</option>
<option selected>批量卡</option>
<option selected>Sm材料导入</option>
<option selected>Md⇄Opml</option>
</select>
</td>
</tr>
<tr>
<td><label title="NoteToCard 用户邮箱">邮箱</label></td>
<td><input id="ntc-email" type="text" placeholder=""></input></td>
<td>
<labell title="NoteToCard 用户密码">密码</label>
</td>
<td><input id="ntc-password" type="password"></input></td>
</tr>
<tr>
<td><label title="块 ID / 块超链接">块ID</label></td>
<td><input id="block-id" type="text" placeholder="00000000000000-xxxxxxx"></input></td>
<td><label title="卡片标题">标题</label></td>
<td><input id="block-title" type="text" placeholder="卡片标题"></input></td>
</tr>
<tr>
<td><button id="button-open" title="在新窗口打开">打开</button></td>
<td>
<button id="button-preview" title="预览 markdown 源代码">预览</button>
<button id="button-convert" title="提交 markdown 源代码至 NoteToCard 服务器以制卡" disabled>制卡</button>
</td>
</tr>
</table>
<textarea id="block-markdown" placeholder="<markdown 源代码>"></textarea>
</div>
</body>
<script type="text/javascript">
(async () => {
const ntc_server = document.getElementById('ntc-server')
const ntc_handler = document.getElementById('ntc-handler')
const ntc_email = document.getElementById('ntc-email')
const ntc_password = document.getElementById('ntc-password')
const block_id = document.getElementById('block-id')
const block_title = document.getElementById('block-title')
const block_markdown = document.getElementById('block-markdown')
const button_open = document.getElementById('button-open')
const button_preview = document.getElementById('button-preview')
const button_convert = document.getElementById('button-convert')
const url = new URL(window.location.href);
let local_config = JSON.parse(window.localStorage.getItem('NoteToCard'));
const config = {
server: (local_config && local_config.server) || ntc_server.value,
handler: (local_config && local_config.handler) || ntc_handler.value,
email: (local_config && local_config.email) || ntc_email.value,
password: (local_config && local_config.password) || ntc_password.value,
blockId: block_id.value,
title: block_title.value,
markdown: block_markdown.value
};
// console.log(config);
/* 👇思源 API 请求方法👇 */
/* siyuan 请求 */
async function siyuanRequest(url, data, token) {
let response = await fetch(
url,
{
method: "POST",
headers: {
Authorization: `Token ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
},
);
if (response.status === 200) response = await response.json();
else return null;
if (response.code === 0) return response.data;
else return null;
}
/* 获取块属性 */
async function getBlockAttrs(id, token = null) {
return siyuanRequest('/api/attr/getBlockAttrs', { id: id }, token);
}
/* 设置块属性 */
async function setBlockAttrs(id, attrs, token = null) {
return siyuanRequest('/api/attr/setBlockAttrs', { id: id, attrs: attrs }, token);
}
/* 查询块 */
async function queryBlock(id, token = null) {
return siyuanRequest(
'/api/query/sql',
{ stmt: `SELECT * FROM blocks WHERE id = '${id}'` },
token,
);
}
/* 导出文档 Markdown */
async function exportMdContent(id, token = null) {
return siyuanRequest('/api/export/exportMdContent', { id: id }, token);
}
/* 推送消息 */
async function pushMsg(msg, timeout = 7000, token = null) {
return siyuanRequest('/api/notification/pushMsg', {
msg: msg,
timeout: timeout,
}, token);
}
/* 推送错误消息 */
async function pushErrMsg(msg, timeout = 7000, token = null) {
return siyuanRequest('/api/notification/pushErrMsg', {
msg: msg,
timeout: timeout,
}, token);
}
/* 👆思源 API 请求方法👆 */
/* 👇NoteToCard API 请求方法👇 */
async function ntcRequest(url, filename, md, email, password, handler) {
let response = await fetch(
url,
{
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
filename: filename,
md: md,
email: email,
password: password,
handler: handler,
}),
},
);
return await response.json();
}
/* 👆NoteToCard API 请求方法👆 */
/* 👇在新窗口打开👇 */
/**
* 新窗口打开
* REF https://github.com/Zuoqiu-Yingyi/siyuan-theme-dark-plus/blob/54a6342d711cd5ac31457a830af94d1d39edbdc5/script/module/html.js#L62-L187
* @url (string): URL
* @urlParams (object): URL 参数
* @windowParams (object): 窗体参数
* @menuTemplate (object): 窗口菜单栏模板
* @return (BrowserWindow): 窗口对象
*/
function openNewWindow(
url = window.location.href,
urlParams = {},
windowParams = {
width: 720,
height: 480,
frame: true, // 是否显示边缘框
fullscreen: false, // 是否全屏显示
alwaysOnTop: true, // 是否置顶显示
autoHideMenuBar: false, // 是否隐藏菜单栏(使用 Alt 显示)
},
menuTemplate = [
// 新窗口菜单模板
// REF [菜单项 | Electron](https://www.electronjs.org/zh/docs/latest/api/menu-item)
{
label: 'SiYuan',
submenu: [
{
label: 'About SiYuan',
role: 'about',
},
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{
label: 'Hide SiYuan',
role: 'hide',
},
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
{
label: 'Quit SiYuan',
role: 'quit',
},
],
},
{
role: 'editMenu',
submenu: [
{ role: 'selectAll' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteAndMatchStyle', accelerator: 'CmdOrCtrl+Shift+V' },
{ type: 'separator' },
{ role: 'toggleSpellChecker' },
],
},
{
role: 'viewMenu',
submenu: [
{ role: 'resetZoom' },
{ role: 'zoomIn', accelerator: 'CmdOrCtrl+=' },
{ role: 'zoomOut' },
],
},
{
role: 'windowMenu',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
{ role: 'togglefullscreen' },
{ type: 'separator' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'reload', accelerator: 'F5' },
{ role: 'forcereload', accelerator: 'CmdOrCtrl+F5' },
{ role: 'close' },
{ type: 'separator' },
{
label: 'Pinned',
click: (menuItem, browserWindow, event) => {
if (browserWindow) browserWindow.setAlwaysOnTop(!browserWindow.isAlwaysOnTop());
},
type: 'checkbox',
checked: true,
// REF [快捷键 | Electron](https://www.electronjs.org/zh/docs/latest/api/accelerator)
accelerator: 'Alt+Shift+P',
},
],
},
],
) {
try {
url = new URL(url);
// 设置 URL 参数
for (const param in urlParams) {
url.searchParams.set(param, urlParams[param]);
}
// 打开新窗口
try {
const {
BrowserWindow,
Menu,
} = window.top.require('@electron/remote');
// 新建窗口(Electron 环境)
var newWin = new BrowserWindow(windowParams);
const menu = Menu.buildFromTemplate(menuTemplate);
// console.log(menu);
console.log(url.href);
// if (url.protocol === 'file:') newWin.loadFile(url.href.substr(8));
// else newWin.loadURL(url.href);
newWin.setMenu(menu);
newWin.loadURL(url.href);
// REF [Event: 'console-message'](https://www.electronjs.org/docs/latest/api/web-contents#event-console-message)
newWin.on('closed', () => {
newWin = null;
})
return newWin;
}
catch (err) {
console.warn(err);
// 新建标签页(Web 环境)
// window.open(url.href, "_blank");
window.open(url.href, "popup");
return null;
}
}
catch (err) {
console.error(err);
return null;
}
}
/* 👆在新窗口打开👆 */
/* 👇事件处理方法👇 */
/* 保存配置 */
async function saveConfig() {
config.server = ntc_server.value;
config.handler = ntc_handler.value;
config.email = ntc_email.value;
config.password = ntc_password.value;
const attrs = {
'custom-ntc-server': config.server,
'custom-ntc-handler': config.handler,
'custom-ntc-email': config.email,
'custom-ntc-password': config.password,
};
window.localStorage.setItem(
'NoteToCard',
JSON.stringify({
server: config.server,
handler: config.handler,
email: config.email,
password: config.password,
}),
);
await setBlockAttrs(widget_id, attrs);
}
/* 👆事件处理方法👆 */
/* 获取保存在挂件块属性中的配置 */
var widget_id = url.searchParams.get('id');
if (widget_id == null) {
let node = window.frameElement != null ? window.frameElement.parentElement.parentElement : null;
if (node != null) widget_id = node.getAttribute('data-node-id');
}
if (widget_id) { // 加载保存在挂件块属性中的配置
const attrs = await getBlockAttrs(widget_id);
/* 使用挂件块属性中的内容覆盖默认配置 */
if (attrs) {
config.server = attrs['custom-ntc-server'] || config.server;
config.handler = attrs['custom-ntc-handler'] || config.handler;
config.email = attrs['custom-ntc-email'] || config.email;
config.password = attrs['custom-ntc-password'] || config.password;
ntc_server.value = config.server;
ntc_handler.value = config.handler;
ntc_email.value = config.email;
ntc_password.value = config.password;
}
/* 监听 input 元素, 在失去焦点时保存 */
ntc_server.onblur = saveConfig;
ntc_handler.onblur = saveConfig;
ntc_email.onblur = saveConfig;
ntc_password.onblur = saveConfig;
}
/* 监听打开按钮单击事件 */
button_open.onclick = () => openNewWindow(url.href, { id: widget_id });
/* 监听预览按钮单击事件 */
button_preview.onclick = async () => {
let id = block_id.value;
/* 判断 ID 是否合法(不合法消息提示) */
switch (true) {
case /^\d{14}\-[0-9a-z]{7}$/.test(id):
break;
case /^siyuan:\/\/blocks\/(\d{14}\-[0-9a-z]{7})\/*(?:(?:\?)(\w+=\w+)(?:(?:\&)(\w+=\w+))*)?$/.test(id):
id = id.match(/^siyuan:\/\/blocks\/(\d{14}\-[0-9a-z]{7})\/*(?:(?:\?)(\w+=\w+)(?:(?:\&)(\w+=\w+))*)?$/)[1];
break;
default:
id = null;
}
if (id) {
let block = await queryBlock(id);
if (block && block.length == 1) {
block = block[0];
/* 判断 ID 是文档 ID 还是块 ID */
switch (block.type) {
case 'd': // 文档块
const doc = await exportMdContent(id);
if (doc) {
config.title = block.content;
config.markdown = doc.content;
}
else {
await pushErrMsg(`NoteToCard: ID=${id} 的文档导出 markdown 失败`);
return null;
}
break;
default:
config.title = block.name
|| block.alias
|| block.memo
|| block.fcontent
|| block.content;
config.markdown = block.markdown;
break;
}
}
else {
await pushErrMsg(`NoteToCard: ID=${id} 的块不存在`);
return null;
}
/* 获得对应块标题并显示 */
block_title.value = config.title;
/* 获得对应块 Markdown 源代码并写入 textarea */
block_markdown.value = config.markdown;
}
else {
await pushErrMsg(`NoteToCard: 块 ID 格式错误`);
return null;
}
button_convert.disabled = false;
};
/* 监听转换按钮单击事件 */
button_convert.onclick = async () => {
config.server = ntc_server.value;
config.title = block_title.value;
config.markdown = block_markdown.value;
config.email = ntc_email.value;
config.password = ntc_password.value;
config.handler = ntc_handler.value;
/* 判断字段是否填写完整 */
if (config.server
&& config.title
&& config.markdown
&& config.email
&& config.password
&& config.handler
) {
/* 发送请求 */
const response = await ntcRequest(
config.server,
config.title,
config.markdown,
config.email,
config.password,
config.handler,
);
/* 判断是否发送成功, 推送消息提示 */
if (response.status === true || response.status >= 200 && response.status < 300) {
await pushMsg(`NoteToCard: ${response.msg}`);
return null;
}
else {
await pushErrMsg(`NoteToCard: ${response.error || response.msg}`);
return null;
}
}
else {
switch ('') {
case config.server:
await pushErrMsg(`NoteToCard: 服务 字段为空`);
break;
case config.title:
await pushErrMsg(`NoteToCard: 标题 字段为空`);
break;
case config.markdown:
await pushErrMsg(`NoteToCard: markdown 字段为空`);
break;
case config.email:
await pushErrMsg(`NoteToCard: 邮箱 字段为空`);
break;
case config.password:
await pushErrMsg(`NoteToCard: 密码 字段为空`);
break;
case config.handler:
await pushErrMsg(`NoteToCard: 方法 字段为空`);
break;
}
return;
}
}
/* 监听 markdown 预览文本框内容更改 */
block_markdown.onchange = () => {
if (block_markdown.value) {
button_convert.disabled = false;
}
else {
button_convert.disabled = true;
}
}
})();
</script>
</html>