Skip to content

Commit 7e1bfe9

Browse files
committed
✨ Feature(custom): add upload all images in file
ISSUES CLOSED: #3
1 parent 6866576 commit 7e1bfe9

File tree

10 files changed

+114
-46
lines changed

10 files changed

+114
-46
lines changed

.vscode/settings.json

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// Place your settings in this file to overwrite default and user settings.
2-
{
3-
"files.exclude": {
4-
"out": false, // set this to true to hide the "out" folder with the compiled JS files
5-
"dist": false // set this to true to hide the "dist" folder with the compiled JS files
6-
},
7-
"search.exclude": {
8-
"out": true, // set this to false to include "out" folder in search results
9-
"dist": true // set this to false to include "dist" folder in search results
10-
},
11-
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
12-
"typescript.tsc.autoDetect": "off"
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"files.exclude": {
4+
"out": false, // set this to true to hide the "out" folder with the compiled JS files
5+
"dist": false // set this to true to hide the "dist" folder with the compiled JS files
6+
},
7+
"editor.formatOnSave": false,
8+
"editor.codeActionsOnSave": {
9+
"source.fixAll.eslint": "explicit"
10+
},
11+
"search.exclude": {
12+
"out": true, // set this to false to include "out" folder in search results
13+
"dist": true // set this to false to include "dist" folder in search results
14+
},
15+
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
16+
"typescript.tsc.autoDetect": "off"
1317
}

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
"command": "piclist.openImageDB",
5757
"title": "%command.openImageDB.title%",
5858
"category": "PicList"
59+
},
60+
{
61+
"command": "piclist.uploadAllImgInFile",
62+
"title": "%command.uploadAllImgInFile.title%",
63+
"category": "PicList"
5964
}
6065
],
6166
"menus": {
@@ -162,7 +167,7 @@
162167
"@types/glob": "^8.1.0",
163168
"@types/mocha": "^10.0.6",
164169
"@types/node": "20.14.2",
165-
"@types/vscode": "^1.90.0",
170+
"@types/vscode": "^1.81.0",
166171
"@typescript-eslint/eslint-plugin": "^7.13.0",
167172
"@typescript-eslint/parser": "^7.13.0",
168173
"@vscode/test-electron": "^2.3.8",

package.nls.json

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
{
2-
"ext.displayName": "PicList",
3-
"ext.description": "A powerful image uploading and managing plugin for VSCode based on PicList desktop software. Compatible with PicGo.",
4-
"command.upload.clipboard.title": "Upload from clipboard",
5-
"command.upload.explorer.title": "Upload from explorer",
6-
"command.upload.inputBox.title": "Upload from input path or url",
7-
"command.openImageDB.title": "Open PicList image database",
8-
"command.delete.title": "Delete Image by PicList",
9-
"config.title": "PicList",
10-
"config.uploadAPIUrl.description": "Upload API URL, default is `http://127.0.0.1:36677/upload`",
11-
"config.deleteAPIUrl.description": "Delete API URL, default is `http://127.0.0.1:36677/delete`",
12-
"config.copyType.markdown": "Markdown: ![alt](url)",
13-
"config.copyType.html": "HTML: <img src=\"url\" alt=\"alt\" />",
14-
"config.copyType.url": "URL: url",
15-
"config.copyType.ubb": "UBB: [img]url[/img]",
16-
"config.copyType.custom": "Custom",
17-
"config.copyType.description": "Copy type, default is `Markdown: ![alt](url)`, support `Markdown`, `HTML`, `URL`, `UBB`, `Custom`",
18-
"config.customType.description": "Custom copy type, default is `Markdown: ![$fileName]($url)`, support `$fileName`, `$url`",
19-
"config.encodeUrl.description": "Whether to encode the URL, default is `false`",
20-
"config.remoteServerMode.description": "Whether to use remote server mode, default is `false`",
21-
"config.enableDragUpload.description": "Whether to enable drag upload, default is `true`"
1+
{
2+
"ext.displayName": "PicList",
3+
"ext.description": "A powerful image uploading and managing plugin for VSCode based on PicList desktop software. Compatible with PicGo.",
4+
"command.upload.clipboard.title": "Upload from clipboard",
5+
"command.upload.explorer.title": "Upload from explorer",
6+
"command.upload.inputBox.title": "Upload from input path or url",
7+
"command.openImageDB.title": "Open PicList image database",
8+
"command.uploadAllImgInFile.title": "Upload all images in the file",
9+
"command.delete.title": "Delete Image by PicList",
10+
"config.title": "PicList",
11+
"config.uploadAPIUrl.description": "Upload API URL, default is `http://127.0.0.1:36677/upload`",
12+
"config.deleteAPIUrl.description": "Delete API URL, default is `http://127.0.0.1:36677/delete`",
13+
"config.copyType.markdown": "Markdown: ![alt](url)",
14+
"config.copyType.html": "HTML: <img src=\"url\" alt=\"alt\" />",
15+
"config.copyType.url": "URL: url",
16+
"config.copyType.ubb": "UBB: [img]url[/img]",
17+
"config.copyType.custom": "Custom",
18+
"config.copyType.description": "Copy type, default is `Markdown: ![alt](url)`, support `Markdown`, `HTML`, `URL`, `UBB`, `Custom`",
19+
"config.customType.description": "Custom copy type, default is `Markdown: ![$fileName]($url)`, support `$fileName`, `$url`",
20+
"config.encodeUrl.description": "Whether to encode the URL, default is `false`",
21+
"config.remoteServerMode.description": "Whether to use remote server mode, default is `false`",
22+
"config.enableDragUpload.description": "Whether to enable drag upload, default is `true`"
2223
}

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export async function activate(context: vscode.ExtensionContext) {
4242
}
4343
}
4444
}
45+
}),
46+
vscode.commands.registerCommand('piclist.uploadAllImgInFile', async () => {
47+
await Commands.commandManager.uploadAllImgInFile()
4548
})
4649
]
4750
context.subscriptions.push(...disposable)

src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@ export function extractUrl(str: string): string[] {
4040
urls.push(match[1])
4141
}
4242
}
43-
console.log(`urls: ${urls}`)
4443
return Array.from(new Set(urls))
4544
}

src/vscode/clipboard/getClipboardImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function appDataPath() {
6262

6363
function ImageSaveDir() {
6464
const imageSavePath = path.join(appDataPath(), 'piclist-clipboard-image')
65-
fs.ensureDirSync(imageSavePath)
65+
fs.emptyDirSync(imageSavePath)
6666
return imageSavePath
6767
}
6868

src/vscode/commands.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import getClipboardImage from './clipboard/getClipboardImage'
1212
export class Commands {
1313
static commandManager: Commands = new Commands()
1414

15-
async uploadCommand(input?: string[], shouldKeepAfterUploading = true) {
15+
async uploadCommand(input?: string[], shouldKeepAfterUploading = true, writeToEditor = true) {
1616
const output = await Uploader.picgoAPI.upload(input)
17-
1817
if (!output) return
1918
if (shouldKeepAfterUploading === false && input) {
2019
fs.removeSync(input[0])
2120
}
22-
vscode.env.clipboard.writeText(output)
23-
await Editor.writeToEditor(output)
21+
if (writeToEditor) {
22+
vscode.env.clipboard.writeText(output)
23+
await Editor.writeToEditor(output)
24+
}
2425
return output
2526
}
2627

@@ -104,4 +105,48 @@ export class Commands {
104105
return false
105106
}
106107
}
108+
109+
async uploadAllImgInFile() {
110+
const editor = vscode.window.activeTextEditor
111+
if (editor) {
112+
const document = editor.document
113+
let text = document.getText()
114+
const regex = /(!\[.*?\]\((.*?)\))|(<img[^>]*src="(.*?)"[^>]*>)|(https?:\/\/[^\s]+)|(\[img\](.*?)\[\/img\])/g
115+
let match
116+
const uploadedImages: { [key: string]: string } = {}
117+
const matches = []
118+
while ((match = regex.exec(text)) !== null) {
119+
matches.push(match)
120+
}
121+
for (const match of matches) {
122+
const imgSyntax = match[0]
123+
const url = match[2] || match[4] || match[5] || match[7]
124+
if (url) {
125+
let res: string | undefined
126+
if (uploadedImages[url]) {
127+
res = uploadedImages[url]
128+
} else {
129+
if (isURL(url)) {
130+
res = await this.uploadCommand([url], true, false)
131+
} else {
132+
const localPath = path.isAbsolute(url) ? url : path.join(document.uri.fsPath, '../', url)
133+
if (fs.existsSync(localPath)) {
134+
res = await this.uploadCommand([localPath], true, false)
135+
}
136+
}
137+
if (res) {
138+
uploadedImages[url] = res
139+
}
140+
}
141+
if (res) {
142+
text = text.replace(imgSyntax, res)
143+
}
144+
}
145+
}
146+
const range = new vscode.Range(document.positionAt(0), document.positionAt(text.length))
147+
editor.edit(editBuilder => {
148+
editBuilder.replace(range, text)
149+
})
150+
}
151+
}
107152
}

src/vscode/uploader.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode'
2-
import { showError, getRemoteServerMode } from './utils'
2+
import { showError, getRemoteServerMode, getFileName } from './utils'
33
import axios from 'axios'
44
import { Editor } from './Editor'
55
import { handleUrlEncode } from '../utils'
@@ -79,10 +79,7 @@ export class Uploader {
7979
if (res.status === 200 && res.data.success) {
8080
const selectedText = Editor.editor?.document.getText(Editor.editor.selection)
8181
const output = res.data.result.map((item: string) => {
82-
return this.formatOutput(
83-
item,
84-
selectedText || decodeURIComponent(new URL(item).pathname.split('/').pop() || '') || ''
85-
)
82+
return this.formatOutput(item, getFileName(item, selectedText))
8683
})
8784
const outputStr = output.join('\n')
8885
DataStore.writeUploadedFileDB(res.data.fullResult)

src/vscode/utils/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ export function getRemoteServerMode(): boolean {
6363
}
6464
return false
6565
}
66+
67+
export function getFileName(item: string, selectedText?: string): string {
68+
let fileName: string = ''
69+
if (selectedText) {
70+
fileName = selectedText
71+
} else {
72+
try {
73+
const url = new URL(item)
74+
const pathSep = url.pathname.includes('/') ? '/' : '\\'
75+
fileName = decodeURIComponent(url.pathname.split(pathSep).pop() || '') || ''
76+
} catch (error) {}
77+
}
78+
return fileName || ''
79+
}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@
640640
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
641641
integrity sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==
642642

643-
"@types/vscode@^1.90.0":
643+
"@types/vscode@^1.81.0":
644644
version "1.90.0"
645645
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.90.0.tgz#c122384d51bd774cec4aa86ca443858adc9edef2"
646646
integrity sha512-oT+ZJL7qHS9Z8bs0+WKf/kQ27qWYR3trsXpq46YDjFqBsMLG4ygGGjPaJ2tyrH0wJzjOEmDyg9PDJBBhWg9pkQ==

0 commit comments

Comments
 (0)