Skip to content

Commit

Permalink
feat: 0.3.0 下载时提供可选文件夹
Browse files Browse the repository at this point in the history
  • Loading branch information
whosydd committed Nov 6, 2021
1 parent 4878890 commit 3499203
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [0.3.0]

- 添加功能:工作区如果存在多个文件夹,下载图片时提示可选文件夹

## [0.2.0]

- 菜单选项国际化,仅支持 `zh-cn``en`
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "carbon",
"displayName": "Carbon",
"description": "Create and share beautiful images of your source code",
"version": "0.2.0",
"version": "0.3.0",
"engines": {
"vscode": "^1.61.0"
},
Expand Down Expand Up @@ -87,7 +87,7 @@
"carbon",
"carbon.now.sh"
],
"author": "whosydd <[email protected]>",
"author": "GY <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/whosydd/carbon/issues"
Expand Down
80 changes: 45 additions & 35 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,55 @@ interface ThemeConfig {
watermark: boolean
}

export default (file: Object) => {
// 获取文件名
const filename = file.toString().split('/').pop()
export default (
file: vscode.FileType
): Promise<{
filename: string
domain: string
theme: ThemeConfig
code: string
}> => {
return new Promise(async (resolve, reject) => {
try {
// 获取文件名
const filename = file.toString().split('/').pop()

// 获取配置项
const config = vscode.workspace.getConfiguration('carbon')
const domain: string | undefined = config.get('domain')
const theme: ThemeConfig | undefined = config.get('theme')
// 获取配置项
const config = vscode.workspace.getConfiguration('carbon')
const domain: string | undefined = config.get('domain')
const theme: ThemeConfig | undefined = config.get('theme')

// 获取项目根目录
const workspace = vscode.workspace.workspaceFolders
if (workspace === undefined) throw new Error(`Can't find ${workspace}`)
const rootPath = workspace[0].uri.fsPath
if (!filename || !domain || !theme) throw new Error('')

// 获取选中代码块
const editor = vscode.window.activeTextEditor
if (!editor) throw new Error('hello world!')
const {
document: { lineAt, getText },
selection: { start, end, active },
} = editor
const code = (
start.isEqual(end) ? lineAt(active.line).text : getText(new vscode.Range(start, end))
).trim()
// 获取选中代码块
const editor = vscode.window.activeTextEditor
if (!editor) throw new Error('hello world!')
const {
document: { lineAt, getText },
selection: { start, end, active },
} = editor
const code = (
start.isEqual(end) ? lineAt(active.line).text : getText(new vscode.Range(start, end))
).trim()

if (!code.match(/\w+/)) throw new Error(`Selected code is empty, refusing to send to carbon.`)
if (!code.match(/\w+/)) throw new Error(`Selected code is empty, refusing to send to carbon.`)

const maxCharacterLength = 1000
if (code.length > 1000) {
throw new Error(
`Selected code is longer than ${maxCharacterLength} characters, refusing to send to carbon.`
)
}
const maxCharacterLength = 1000
if (code.length > 1000) {
throw new Error(
`Selected code is longer than ${maxCharacterLength} characters, refusing to send to carbon.`
)
}

return {
filename,
domain,
theme,
rootPath,
code,
}
resolve({
filename,
domain,
theme,
code,
})
} catch (error: any) {
if (error.message === '') return
vscode.window.showErrorMessage(error.message)
}
})
}
31 changes: 24 additions & 7 deletions src/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ import * as path from 'path'
import * as vscode from 'vscode'
import config from './config'

export default async (file: object) => {
// 获取配置项
const { filename, domain, theme, rootPath, code } = config(file)
let isDefault: boolean
export default async (file: vscode.FileType) => {
try {
// 获取配置项
const { filename, domain, theme, code } = await config(file)
let isDefault: boolean

if (theme !== undefined && Object.keys(theme).length !== 0) isDefault = false
else isDefault = true
if (theme !== undefined && Object.keys(theme).length !== 0) isDefault = false
else isDefault = true

// 获取工作区目录
let rootPath = ''
const workspace = vscode.workspace.workspaceFolders
if (workspace === undefined) throw new Error('Please open a workspace')
// 如果工作区中存在的多个文件夹,显示选择框
if (workspace.length > 1) {
const pick = await vscode.window.showWorkspaceFolderPick()
if (!pick) throw new Error('')
rootPath = pick.uri.fsPath
} else {
const pick = workspace[0]
rootPath = pick.uri.fsPath
}

try {
// 发送ajax请求
const params = Object.assign({ code }, isDefault ? null : theme)
const now = Date.now()
Expand All @@ -23,11 +37,14 @@ export default async (file: object) => {
)
const res = await axios.post(`https://${domain}/api/cook`, params, {
responseType: 'arraybuffer',
timeout: 5000,
})
if (!fs.existsSync(rootPath + '/carbon')) fs.mkdirSync(rootPath + '/carbon')
fs.createWriteStream(imgPath).write(res.data)
vscode.window.showInformationMessage('Done')
vscode.env.openExternal(vscode.Uri.file(imgPath))
} catch (error: any) {
if (error.message === '') return
vscode.window.showErrorMessage(error.message)
}
}
4 changes: 2 additions & 2 deletions src/utils/openWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { URL } from 'url'
import * as vscode from 'vscode'
import config from './config'

export default (file: object) => {
export default async (file: vscode.FileType) => {
// 获取配置项
const { theme, code } = config(file)
const { theme, code } = await config(file)
let isDefault: boolean

try {
Expand Down

0 comments on commit 3499203

Please sign in to comment.