From ff3a5d19729a18004cb1610fd28ed16ec713449c Mon Sep 17 00:00:00 2001 From: oeyoews Date: Tue, 2 Apr 2024 23:45:32 +0800 Subject: [PATCH] feat: better tip for exist tiddlers --- package.json | 2 +- utils/constant.ts | 1 + utils/random.ts | 2 ++ utils/save2TiddlyWiki.ts | 57 +++++++++++++++++++++++++++++----------- 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 utils/constant.ts create mode 100644 utils/random.ts diff --git a/package.json b/package.json index 4af5109..9e83de9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/utils/constant.ts b/utils/constant.ts new file mode 100644 index 0000000..cb24cca --- /dev/null +++ b/utils/constant.ts @@ -0,0 +1 @@ +export const markdown_type = 'text/markdown'; diff --git a/utils/random.ts b/utils/random.ts new file mode 100644 index 0000000..6cb1fb3 --- /dev/null +++ b/utils/random.ts @@ -0,0 +1,2 @@ +const random = () => Math.random().toString(36).slice(-8); +export default random; diff --git a/utils/save2TiddlyWiki.ts b/utils/save2TiddlyWiki.ts index 2eb0367..d4e8079 100644 --- a/utils/save2TiddlyWiki.ts +++ b/utils/save2TiddlyWiki.ts @@ -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, @@ -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', @@ -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: @@ -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;