Skip to content

Commit

Permalink
feat: better tip for exist tiddlers
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Apr 2, 2024
1 parent b14a6be commit ff3a5d1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "usewiki2",
"description": "Convert HTML to Markdown, and save to your computer, support nodejs tiddlywiki",
"private": true,
"version": "1.5.7",
"version": "1.6.0",
"type": "module",
"scripts": {
"dev": "wxt",
Expand Down
1 change: 1 addition & 0 deletions utils/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const markdown_type = 'text/markdown';
2 changes: 2 additions & 0 deletions utils/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const random = () => Math.random().toString(36).slice(-8);
export default random;
57 changes: 42 additions & 15 deletions utils/save2TiddlyWiki.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { formattime } from './formattime';
import { ElMessage as notify } from 'element-plus';
import { ElMessageBox, ElMessage as notify } from 'element-plus';
import { ofetch } from 'ofetch';
import * as constant from './constant';

const save2TiddlyWiki = async (
title: string,
Expand Down Expand Up @@ -32,6 +33,16 @@ const save2TiddlyWiki = async (

const currentTime = formattime(new Date());

const tiddler = {
text,
creator: status.value.username,
type: constant.markdown_type,
url,
created: currentTime,
modified: currentTime,
tags,
};

const savetwFetch = ofetch.create({
baseURL,
method: 'PUT',
Expand Down Expand Up @@ -64,25 +75,16 @@ const save2TiddlyWiki = async (
'Content-Type': 'application/json',
'x-requested-with': 'TiddlyWiki',
},
});

const oldTiddler = await getTwFetch(`/${title}`, {
async onResponse({ request, response, options }) {
switch (response.status) {
case 200:
notify({
message: `${title} 已存在`,
type: 'error',
});
break;
case 404:
await savetwFetch(`/${title}`, {
body: {
text,
creator: status.value.username,
type: 'text/markdown',
url,
created: currentTime,
modified: currentTime,
tags,
},
body: tiddler,
});
break;
default:
Expand All @@ -91,7 +93,32 @@ const save2TiddlyWiki = async (
},
});

await getTwFetch(`/${title}`);
if (oldTiddler?.text === text) {
notify({
message: h('div', [
h('span', { style: { fontWeight: 'bold' } }, title),
h('span', null, ' 没有新的变化,无需重复保存!'),
]),
type: 'warning',
});
} else {
ElMessageBox.confirm(`确定要覆盖 ${title} 吗`, '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
savetwFetch(`/${title}`, {
body: tiddler,
});
})
.catch(() => {
notify({
message: '已取消保存',
type: 'info',
});
});
}
};

export default save2TiddlyWiki;

0 comments on commit ff3a5d1

Please sign in to comment.