Skip to content

Commit e4bc5cd

Browse files
committed
✨ Feature(custom): add upload selected file in menu
1 parent 7e1bfe9 commit e4bc5cd

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,22 @@
6161
"command": "piclist.uploadAllImgInFile",
6262
"title": "%command.uploadAllImgInFile.title%",
6363
"category": "PicList"
64+
},
65+
{
66+
"command": "piclist.uploadSelectedImg",
67+
"title": "%command.uploadSelectedImg.title%",
68+
"category": "PicList"
6469
}
6570
],
6671
"menus": {
6772
"editor/context": [
6873
{
6974
"command": "piclist.deleteImage",
7075
"when": "editorTextFocus"
76+
},
77+
{
78+
"command": "piclist.uploadSelectedImg",
79+
"when": "editorTextFocus"
7180
}
7281
]
7382
},

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"command.openImageDB.title": "Open PicList image database",
88
"command.uploadAllImgInFile.title": "Upload all images in the file",
99
"command.delete.title": "Delete Image by PicList",
10+
"command.uploadSelectedImg.title": "Upload Image by PicList",
1011
"config.title": "PicList",
1112
"config.uploadAPIUrl.description": "Upload API URL, default is `http://127.0.0.1:36677/upload`",
1213
"config.deleteAPIUrl.description": "Delete API URL, default is `http://127.0.0.1:36677/delete`",

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export async function activate(context: vscode.ExtensionContext) {
4343
}
4444
}
4545
}),
46+
vscode.commands.registerCommand('piclist.uploadSelectedImg', async () => {
47+
await Commands.commandManager.uploadSelectedImg()
48+
}),
4649
vscode.commands.registerCommand('piclist.uploadAllImgInFile', async () => {
4750
await Commands.commandManager.uploadAllImgInFile()
4851
})

src/vscode/commands.ts

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

15-
async uploadCommand(input?: string[], shouldKeepAfterUploading = true, writeToEditor = true) {
16-
const output = await Uploader.picgoAPI.upload(input)
15+
async uploadCommand(
16+
input?: string[],
17+
shouldKeepAfterUploading = true,
18+
writeToEditor = true,
19+
getFileNameFromRes = false
20+
) {
21+
const output = await Uploader.picgoAPI.upload(input, getFileNameFromRes)
1722
if (!output) return
1823
if (shouldKeepAfterUploading === false && input) {
1924
fs.removeSync(input[0])
@@ -106,11 +111,17 @@ export class Commands {
106111
}
107112
}
108113

109-
async uploadAllImgInFile() {
114+
async uploadAllImgInFile(selected = false) {
110115
const editor = vscode.window.activeTextEditor
111116
if (editor) {
112117
const document = editor.document
113118
let text = document.getText()
119+
if (selected) {
120+
if (editor.selection.isEmpty) {
121+
return
122+
}
123+
text = document.getText(editor.selection)
124+
}
114125
const regex = /(!\[.*?\]\((.*?)\))|(<img[^>]*src="(.*?)"[^>]*>)|(https?:\/\/[^\s]+)|(\[img\](.*?)\[\/img\])/g
115126
let match
116127
const uploadedImages: { [key: string]: string } = {}
@@ -127,11 +138,11 @@ export class Commands {
127138
res = uploadedImages[url]
128139
} else {
129140
if (isURL(url)) {
130-
res = await this.uploadCommand([url], true, false)
141+
res = await this.uploadCommand([url], true, false, true)
131142
} else {
132143
const localPath = path.isAbsolute(url) ? url : path.join(document.uri.fsPath, '../', url)
133144
if (fs.existsSync(localPath)) {
134-
res = await this.uploadCommand([localPath], true, false)
145+
res = await this.uploadCommand([localPath], true, false, true)
135146
}
136147
}
137148
if (res) {
@@ -143,10 +154,20 @@ export class Commands {
143154
}
144155
}
145156
}
146-
const range = new vscode.Range(document.positionAt(0), document.positionAt(text.length))
157+
const range =
158+
selected && !editor.selection.isEmpty
159+
? editor.selection
160+
: new vscode.Range(document.positionAt(0), document.positionAt(text.length))
147161
editor.edit(editBuilder => {
148162
editBuilder.replace(range, text)
149163
})
150164
}
151165
}
166+
167+
async uploadSelectedImg() {
168+
const editor = vscode.window.activeTextEditor
169+
if (editor) {
170+
await this.uploadAllImgInFile(true)
171+
}
172+
}
152173
}

src/vscode/uploader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Uploader {
4747
return false
4848
}
4949

50-
async upload(input?: string[]): Promise<string> {
50+
async upload(input?: string[], getFileNameFromRes = false): Promise<string> {
5151
try {
5252
let res
5353
if (getRemoteServerMode()) {
@@ -79,7 +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(item, getFileName(item, selectedText))
82+
return this.formatOutput(item, getFileName(item, selectedText, getFileNameFromRes))
8383
})
8484
const outputStr = output.join('\n')
8585
DataStore.writeUploadedFileDB(res.data.fullResult)

src/vscode/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export function getRemoteServerMode(): boolean {
6464
return false
6565
}
6666

67-
export function getFileName(item: string, selectedText?: string): string {
67+
export function getFileName(item: string, selectedText?: string, getFileNameFromRes = false): string {
6868
let fileName: string = ''
69-
if (selectedText) {
69+
if (selectedText && !getFileNameFromRes) {
7070
fileName = selectedText
7171
} else {
7272
try {

0 commit comments

Comments
 (0)