Skip to content

Commit

Permalink
Merge pull request #1 from hemengke1997/context
Browse files Browse the repository at this point in the history
add context-menu for image copy
  • Loading branch information
hemengke1997 committed Jan 4, 2024
2 parents d961e0e + e937a74 commit 65ad208
Show file tree
Hide file tree
Showing 43 changed files with 527 additions and 180 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# CHANGELOG

## [0.0.9](https://github.com/hemengke1997/vscode-image-manager/compare/v0.0.8...v0.0.9) (2024-01-03)

### Break Change

* rename extension to `Image Manager`

### feat

* error boundary ([9a214d5](https://github.com/hemengke1997/vscode-image-manager/commit/9a214d53ac129e2b9b5a6933f0a08b0c189bdacf))

If any internal error occurs, the extension will display a friendly error message instead of crashing. Furthermore, user could report the error to the author or choose to restart.

## [0.0.8](https://github.com/hemengke1997/vscode-image-manager/compare/v0.0.7...v0.0.8) (2024-01-02)

### fix
Expand Down
Binary file added bin/clipboard/linux/bin/cb
Binary file not shown.
Binary file added bin/clipboard/linux/lib/libcbwayland.so
Binary file not shown.
Binary file added bin/clipboard/linux/lib/libcbx11.so
Binary file not shown.
Binary file added bin/clipboard/linux/lib/libgui.a
Binary file not shown.
Binary file added bin/clipboard/macos/bin/cb
Binary file not shown.
Binary file added bin/clipboard/macos/lib/libgui.a
Binary file not shown.
Binary file added bin/clipboard/windows/bin/cb.exe
Binary file not shown.
Binary file added bin/clipboard/windows/lib/gui.lib
Binary file not shown.
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

<head>
<meta charset=" UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport"
content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<meta name="renderer" content="webkit" />
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"icon": "assets/logo.png",
"keywords": [
"image",
"manager",
"viewer",
"manager",
"base64",
"svg",
"img",
Expand Down Expand Up @@ -99,7 +99,7 @@
"dev": "simple-git-hooks && tsup --watch",
"ui:dev": "vite -c vite.config.ts -m development",
"ui:build": "vite build -c vite.config.ts -m production",
"build": "tsup",
"build": "rm -rf dist && tsup",
"vscode:prepublish": "run-s ui:build build",
"pack": "vsce package --no-dependencies",
"lint": "eslint .",
Expand All @@ -111,7 +111,6 @@
},
"dependencies": {
"@ctrl/tinycolor": "^4.0.2",
"@minko-fe/lodash-pro": "^0.1.16",
"@minko-fe/react-hook": "^0.1.16",
"@minko-fe/vite-config": "^0.1.16",
"@vscode/webview-ui-toolkit": "^1.2.2",
Expand All @@ -128,20 +127,22 @@
"react-hotkeys-hook": "^4.4.1",
"react-i18next": "^13.5.0",
"react-icons": "^4.12.0",
"ts-key-enum": "^2.0.12",
"un-detector": "^0.0.6"
},
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@minko-fe/commitlint-config": "^2.0.7",
"@minko-fe/eslint-config": "^2.0.7",
"@minko-fe/html-transform": "^0.1.16",
"@minko-fe/lodash-pro": "^0.1.16",
"@minko-fe/tsconfig": "^2.0.7",
"@types/eslint": "^8.56.0",
"@types/node": "^20.10.5",
"@types/react": "^18.2.45",
"@types/react-copy-to-clipboard": "^5.0.7",
"@types/react-dom": "^18.2.18",
"@types/vscode": "^1.60.0",
"@types/vscode": "^1.80.0",
"@types/vscode-webview": "^1.57.4",
"@vscode/vsce": "^2.22.0",
"bumpp": "^9.2.1",
Expand All @@ -151,6 +152,7 @@
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"image-size": "^1.0.2",
"is-wsl": "^3.1.0",
"npm-run-all": "^4.1.5",
"simple-git-hooks": "^2.9.0",
"tsup": "6.7.0",
Expand All @@ -162,7 +164,7 @@
"vite-plugin-i18n-detector": "^2.2.0"
},
"engines": {
"vscode": "^1.60.0"
"vscode": "^1.80.0"
},
"simple-git-hooks": {
"commit-msg": "pnpm exec commitlint -e",
Expand Down
23 changes: 14 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type ExtensionContext, ExtensionMode, window } from 'vscode'

export class Context {
private static instance: Context
static _inited = false
theme: 'light' | 'dark' = 'dark'

constructor(public ext: ExtensionContext) {
Expand All @@ -19,11 +18,13 @@ export class Context {
break
}
}
}

if (!Context.instance && !Context._inited && ext) {
Context._inited = true
public static getInstance(ext?: ExtensionContext) {
if (!Context.instance && ext) {
Context.instance = new Context(ext)
}

return Context.instance
}

Expand Down
31 changes: 31 additions & 0 deletions src/clipboard/UnClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { castArray } from '@minko-fe/lodash-pro'
import { type CommonOptions, type Options, type SyncOptions, execa, execaSync } from 'execa'

class UnClipboard {
constructor(
private _binarayPath: string,
private _options?: CommonOptions,
) {
this._options = {
..._options,
stdio: 'inherit',
}
}
async copy(args: string | string[], options?: Options) {
return execa(this._binarayPath, ['copy', ...castArray(args)], { ...options, ...this._options })
}
async paste(options: Options, args?: string | string[]) {
return await execa(this._binarayPath, ['paste', ...castArray(args)], {
...options,
...this._options,
})
}
copySycn(args: string | string[], options?: SyncOptions) {
return execaSync(this._binarayPath, ['copy', ...castArray(args)], { ...options, ...this._options })
}
pasteSync(options: SyncOptions, args?: string | string[]) {
return execaSync(this._binarayPath, ['paste', ...castArray(args)], { ...options, ...this._options })
}
}

export default UnClipboard
26 changes: 26 additions & 0 deletions src/clipboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import isWSL from 'is-wsl'
import type UnClipboard from './UnClipboard'

async function getClipboard(): Promise<UnClipboard> {
switch (process.platform) {
case 'darwin': {
return (await import('./macos')).default
}
case 'win32': {
return (await import('./windows')).default
}

case 'linux': {
return (await import('./linux')).default
}
default: {
if (isWSL) {
return (await import('./windows')).default
}

return (await import('./linux')).default
}
}
}

export { getClipboard }
9 changes: 9 additions & 0 deletions src/clipboard/linux/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { $ } from 'execa'
import path from 'node:path'
import UnClipboard from '../UnClipboard'

const bin = path.join(__dirname, './clipboard/linux/bin/cb')

$`chmod +x ${bin}`

export default new UnClipboard(bin)
9 changes: 9 additions & 0 deletions src/clipboard/macos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { $ } from 'execa'
import path from 'node:path'
import UnClipboard from '../UnClipboard'

const bin = path.join(__dirname, './clipboard/macos/bin/cb')

$`chmod +x ${bin}`

export default new UnClipboard(bin)
6 changes: 6 additions & 0 deletions src/clipboard/windows/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'node:path'
import UnClipboard from '../UnClipboard'

const bin = path.join(__dirname, './clipboard/windows/bin/cb.exe')

export default new UnClipboard(bin)
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ImageManagerPanel } from './panel/ImageManagerPanel'

export function activate(context: ExtensionContext) {
console.log('"Image Manager" is now active')
const ctx = new Context(context)
const ctx = Context.getInstance(context)

const showImageManagerCmd = commands.registerCommand('image-manager.open-image-manager', () => {
ImageManagerPanel.render(ctx)
Expand Down
19 changes: 10 additions & 9 deletions src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ export const getProjectPath = () => {

let lastLogTimestamp = 0

/**
* log to file
*/
const log = (text: any) => {
const logMsg = (text: any) => {
const now = new Date()
const gap = now.getTime() - lastLogTimestamp
// add some blank rows
Expand All @@ -34,16 +31,20 @@ const log = (text: any) => {
lastLogTimestamp = now.getTime()
console.log(`${new Date().toISOString()} ${text}`)
}
export const logInfo = (text: any, ...args: any[]) => log(`[Info] ${text} ${args.join('')}`)
export const logWarn = (text: any) => log(`[Warn] ${text}`)
export const logError = (text: any) => log(`[Error] ${text}`)

export const Log = {
info: (text: any, ...args: any[]) => logMsg(`[Info] ${text} ${args.join('')}`),
warn: (text: any) => logMsg(`[Warn] ${text}`),
error: (text: any) => logMsg(`[Error] ${text}`),
}

export const showInfo = (text: any) => {
window.showInformationMessage(text)
logInfo(text)
Log.info(text)
}
export const showError = (text: any) => {
window.showErrorMessage(text)
logError(text)
Log.error(text)
}

export function getUri(webview: Webview, extensionUri: Uri, pathList: string[]) {
Expand Down
33 changes: 24 additions & 9 deletions src/message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@ import { type Webview } from 'vscode'
import { messageHandler } from './messageHandler'
import { CmdToVscode } from './shared'

export type MessageType = {
export type MessageType<T = any> = {
msgId: string
cmd: string
postTime: string
callbackId: string
data: any
data: T
}

export const vscodeMessageCenter: Record<
string,
(params: { message: MessageType; webview: Webview }) => Promise<any> | any
> = {
[CmdToVscode.GET_ALL_IMAGES]: async ({ webview }) => {
export type MessageParams<T = any> = { message: MessageType<T>; webview: Webview }

export type KeyofMessage = keyof typeof VscodeMessageCenter

export type ReturnOfMessageCenter<K extends KeyofMessage> = RmPromise<ReturnType<(typeof VscodeMessageCenter)[K]>>

export const VscodeMessageCenter = {
[CmdToVscode.RELOAD_WEBVIEW]: async () => {
const data = messageHandler.reloadWebview()
return data
},
[CmdToVscode.GET_ALL_IMAGES]: async ({ webview }: MessageParams) => {
const data = await messageHandler.getAllImgs(webview)
return data
},
[CmdToVscode.GET_IMAGE_DIMENSIONS]: ({ message }) => {
[CmdToVscode.GET_IMAGE_DIMENSIONS]: async ({ message }: MessageParams<{ filePath: string }>) => {
const data = messageHandler.getImageDimensions(message.data.filePath)
return data
},
[CmdToVscode.GET_EXT_CONFIG]: () => {
[CmdToVscode.GET_EXT_CONFIG]: async () => {
const data = messageHandler.getExtConfig()
return data
},
[CmdToVscode.COPY_IMAGE]: async ({ message }: MessageParams<{ filePath: string }>) => {
const data = await messageHandler.copyImage(message.data.filePath)
return data
},
[CmdToVscode.PASTE_IMAGE]: async ({ message }: MessageParams<{ dest: string }>) => {
const data = await messageHandler.pasteImage(message.data.dest)
return data
},
}
Loading

0 comments on commit 65ad208

Please sign in to comment.