From c59e2bcc970f783670f9c0c5b3ee846f13f23404 Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Thu, 18 Apr 2019 17:25:10 +0800 Subject: [PATCH] :sparkles: Feature: add file-name for customurl add $fileName arg for customUrl ISSUES CLOSED: #173 --- src/main/index.js | 12 ++++-------- src/main/utils/guiApi.js | 3 +-- src/main/utils/pasteTemplate.js | 22 ++++++++++++++++++++-- src/renderer/pages/Gallery.vue | 8 +++----- src/renderer/pages/TrayPage.vue | 2 +- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 5780392..c027b16 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -170,8 +170,7 @@ function createTray () { const imgs = await new Uploader(files, window.webContents).upload() if (imgs !== false) { for (let i in imgs) { - const url = imgs[i].url || imgs[i].imgUrl - clipboard.writeText(pasteTemplate(pasteStyle, url)) + clipboard.writeText(pasteTemplate(pasteStyle, imgs[i])) const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl, @@ -343,8 +342,7 @@ const uploadClipboardFiles = async () => { if (img !== false) { if (img.length > 0) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' - const url = img[0].url || img[0].imgUrl - clipboard.writeText(pasteTemplate(pasteStyle, url)) + clipboard.writeText(pasteTemplate(pasteStyle, img[0])) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl, @@ -374,8 +372,7 @@ const uploadChoosedFiles = async (webContents, files) => { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' let pasteText = '' for (let i in imgs) { - const url = imgs[i].url || imgs[i].imgUrl - pasteText += pasteTemplate(pasteStyle, url) + '\r\n' + pasteText += pasteTemplate(pasteStyle, imgs[i]) + '\r\n' const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl, @@ -401,8 +398,7 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => { const img = await new Uploader(undefined, window.webContents).upload() if (img !== false) { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' - const url = img[0].url || img[0].imgUrl - clipboard.writeText(pasteTemplate(pasteStyle, url)) + clipboard.writeText(pasteTemplate(pasteStyle, img[0])) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl, diff --git a/src/main/utils/guiApi.js b/src/main/utils/guiApi.js index d280ae2..69615ef 100644 --- a/src/main/utils/guiApi.js +++ b/src/main/utils/guiApi.js @@ -63,8 +63,7 @@ class GuiApi { const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown' let pasteText = '' for (let i in imgs) { - const url = imgs[i].url || imgs[i].imgUrl - pasteText += pasteTemplate(pasteStyle, url) + '\r\n' + pasteText += pasteTemplate(pasteStyle, imgs[i]) + '\r\n' const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl, diff --git a/src/main/utils/pasteTemplate.js b/src/main/utils/pasteTemplate.js index 4b6b3ec..f97af0a 100644 --- a/src/main/utils/pasteTemplate.js +++ b/src/main/utils/pasteTemplate.js @@ -1,13 +1,31 @@ import db from '../../datastore' -export default (style, url) => { +const formatCustomLink = (customLink, item) => { + let fileName = item.fileName.replace(new RegExp(`\\${item.extname}$`), '') + let url = item.url || item.imgUrl + let formatObj = { + url, + fileName + } + let keys = Object.keys(formatObj) + keys.forEach(item => { + if (customLink.indexOf(`$${item}`) !== -1) { + let reg = new RegExp(`\\$${item}`, 'g') + customLink = customLink.replace(reg, formatObj[item]) + } + }) + return customLink +} + +export default (style, item) => { + let url = item.url || item.imgUrl const customLink = db.read().get('settings.customLink').value() || '$url' const tpl = { 'markdown': `![](${url})`, 'HTML': ``, 'URL': url, 'UBB': `[IMG]${url}[/IMG]`, - 'Custom': customLink.replace(/\$url/g, url) + 'Custom': formatCustomLink(customLink, item) } return tpl[style] } diff --git a/src/renderer/pages/Gallery.vue b/src/renderer/pages/Gallery.vue index 7d9ed9f..ac83307 100644 --- a/src/renderer/pages/Gallery.vue +++ b/src/renderer/pages/Gallery.vue @@ -213,13 +213,12 @@ export default { this.changeZIndexForGallery(false) }, copy (item) { - const url = item.url || item.imgUrl const style = this.$db.read().get('settings.pasteStyle').value() || 'markdown' - const copyLink = pasteStyle(style, url) + const copyLink = pasteStyle(style, item) const obj = { title: '复制链接成功', body: copyLink, - icon: url + icon: item.url || item.imgUrl } const myNotification = new window.Notification(obj.title, obj) this.$electron.clipboard.writeText(copyLink) @@ -325,8 +324,7 @@ export default { Object.keys(this.choosedList).forEach(key => { if (this.choosedList[key]) { const item = this.$db.read().get('uploaded').getById(key).value() - const url = item.url || item.imgUrl - copyString += pasteStyle(style, url) + '\n' + copyString += pasteStyle(style, item) + '\n' this.choosedList[key] = false } }) diff --git a/src/renderer/pages/TrayPage.vue b/src/renderer/pages/TrayPage.vue index 189d9b5..bc01ca5 100644 --- a/src/renderer/pages/TrayPage.vue +++ b/src/renderer/pages/TrayPage.vue @@ -84,7 +84,7 @@ this.notification.icon = item.imgUrl const myNotification = new window.Notification(this.notification.title, this.notification) const pasteStyle = this.$db.read().get('settings.pasteStyle').value() || 'markdown' - this.$electron.clipboard.writeText(pasteTemplate(pasteStyle, item.imgUrl)) + this.$electron.clipboard.writeText(pasteTemplate(pasteStyle, item)) myNotification.onclick = () => { return true }