-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
937 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ out | |
node_modules | ||
.vscode-test/ | ||
*.vsix | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
// The module 'vscode' contains the VS Code extensibility API | ||
// Import the module and reference it with the alias vscode in your code below | ||
import * as vscode from 'vscode'; | ||
import vscode from 'vscode' | ||
import uploadFromClipboard from './commands/uploadFromClipboard' | ||
import uploadFromExplorer from './commands/uploadFromExplorer' | ||
import Logger from './utils/log' | ||
|
||
// this method is called when your extension is activated | ||
// your extension is activated the very first time the command is executed | ||
export function activate(context: vscode.ExtensionContext) { | ||
|
||
// Use the console to output diagnostic information (console.log) and errors (console.error) | ||
// This line of code will only be executed once when your extension is activated | ||
console.log('Congratulations, your extension "helloworld" is now active!'); | ||
export function activate(context: vscode.ExtensionContext): void { | ||
Logger.channel = vscode.window.createOutputChannel('Elan') | ||
|
||
// The command has been defined in the package.json file | ||
// Now provide the implementation of the command with registerCommand | ||
// The commandId parameter must match the command field in package.json | ||
let disposable = vscode.commands.registerCommand('helloworld.helloWorld', () => { | ||
// The code you place here will be executed every time your command is executed | ||
|
||
// Display a message box to the user | ||
vscode.window.showInformationMessage('Hello World from helloworld!'); | ||
}); | ||
|
||
context.subscriptions.push(disposable); | ||
const disposable = [ | ||
vscode.commands.registerCommand( | ||
'elan.uploadFromClipboard', | ||
uploadFromClipboard | ||
), | ||
vscode.commands.registerCommand( | ||
'elan.uploadFromExplorer', | ||
uploadFromExplorer | ||
) | ||
] | ||
context.subscriptions.push(...disposable) | ||
} | ||
|
||
// this method is called when your extension is deactivated | ||
export function deactivate() {} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
export function deactivate(): void {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import vscode from 'vscode' | ||
import OSS from 'ali-oss' | ||
import path from 'path' | ||
import Logger from '../log' | ||
import { TemplateStore } from './templateStore' | ||
|
||
let uploader: Uploader | null = null | ||
|
||
declare global { | ||
interface PromiseConstructor { | ||
allSettled( | ||
promises: Array<Promise<unknown>> | ||
): Promise< | ||
Array<{ | ||
status: 'fulfilled' | 'rejected' | ||
value?: unknown | ||
reason?: unknown | ||
}> | ||
> | ||
} | ||
} | ||
|
||
interface WrapError { | ||
raw: unknown | ||
imageName: string | ||
} | ||
|
||
interface UploadingProgress { | ||
progress: vscode.Progress<{ message?: string; increment?: number }> | ||
progressResolve: (value?: unknown) => void | ||
progressReject: (value?: unknown) => void | ||
} | ||
|
||
function getUploadingProgress(title = 'Uploading image'): UploadingProgress { | ||
let progressResolve, progressReject, progress | ||
vscode.window.withProgress( | ||
{ | ||
location: vscode.ProgressLocation.Notification, | ||
title | ||
}, | ||
(p) => { | ||
return new Promise((resolve, reject) => { | ||
progressResolve = resolve | ||
progressReject = reject | ||
progress = p | ||
}) | ||
} | ||
) | ||
if (!progress || !progressResolve || !progressReject) | ||
throw new Error('Failed to init vscode progress') | ||
return { | ||
progress, | ||
progressResolve, | ||
progressReject | ||
} | ||
} | ||
|
||
export async function uploadUris(uris: vscode.Uri[]): Promise<void> { | ||
const uploader = getUploader() | ||
const { progress, progressResolve } = getUploadingProgress( | ||
`Uploading ${uris.length} image(s)` | ||
) | ||
const clipboard: string[] = [] | ||
|
||
let finished = 0 | ||
const urisPut = uris.map((uri) => { | ||
const templateStore = new TemplateStore() | ||
const extName = path.extname(uri.fsPath) | ||
const name = path.basename(uri.fsPath, extName) | ||
|
||
templateStore.set('fileName', name) | ||
templateStore.set('extName', extName) | ||
const uploadName = templateStore.transform('uploadName') | ||
|
||
const u = uploader.put(uploadName, uri.fsPath) | ||
u.then((putObjectResult) => { | ||
progress.report({ | ||
message: `(${++finished} / ${uris.length})`, | ||
increment: Math.ceil(100 / uris.length) | ||
}) | ||
|
||
templateStore.set('url', putObjectResult.url) | ||
clipboard.push(templateStore.transform('outputFormat')) | ||
|
||
return putObjectResult | ||
}).catch((err) => { | ||
const wrapError = { | ||
raw: err, | ||
imageName: name | ||
} | ||
return wrapError | ||
}) | ||
return u | ||
}) | ||
|
||
const settled = await Promise.allSettled(urisPut) | ||
const rejects = settled.filter((r) => { | ||
return r.status === 'rejected' | ||
}) | ||
|
||
if (!rejects.length) { | ||
progress.report({ | ||
message: 'Finish.' | ||
}) | ||
|
||
setTimeout(() => { | ||
progressResolve() | ||
}, 1000) | ||
} else { | ||
progress.report({ | ||
message: `${uris.length - rejects.length} images uploaded.` | ||
}) | ||
setTimeout(() => { | ||
progressResolve() | ||
Logger.showErrorMessage( | ||
`Failed to upload these images: ${rejects | ||
.map((r) => { | ||
return (r.reason as WrapError).imageName | ||
}) | ||
.join(',')}` | ||
) | ||
}, 1000) | ||
} | ||
|
||
afterUpload(clipboard) | ||
} | ||
|
||
function afterUpload(clipboard: string[]): void { | ||
if (!clipboard.length) return | ||
const GFM = clipboard.join('\n\n') | ||
vscode.env.clipboard.writeText(GFM) | ||
const activeTextEditor = vscode.window.activeTextEditor | ||
if (!activeTextEditor) return | ||
|
||
activeTextEditor.edit((textEditorEdit) => { | ||
textEditorEdit.insert(activeTextEditor.selection.active, GFM) | ||
}) | ||
} | ||
|
||
export function getUploader(): Uploader { | ||
return uploader || (uploader = new Uploader()) | ||
} | ||
|
||
function initOSSOptions(): OSS.Options { | ||
const config = vscode.workspace.getConfiguration('elan') | ||
const aliyunConfig = config.get<OSS.Options>('aliyun', { | ||
accessKeyId: '', | ||
accessKeySecret: '' | ||
}) | ||
return { | ||
accessKeyId: aliyunConfig.accessKeyId.trim(), | ||
accessKeySecret: aliyunConfig.accessKeySecret.trim(), | ||
bucket: aliyunConfig.bucket?.trim(), | ||
region: aliyunConfig.region?.trim() | ||
} | ||
} | ||
|
||
class Uploader { | ||
client: OSS | ||
constructor() { | ||
this.client = new OSS(initOSSOptions()) | ||
vscode.workspace.onDidChangeConfiguration(() => { | ||
this.client = new OSS(initOSSOptions()) | ||
}) | ||
} | ||
async put(name: string, fsPath: string): Promise<OSS.PutObjectResult> { | ||
return this.client.put(name, fsPath) | ||
} | ||
} |
Oops, something went wrong.