From c1136098c0e952d51656167bd5ab8d1a17882d2b Mon Sep 17 00:00:00 2001 From: rxliuli Date: Sun, 2 Oct 2022 21:37:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=20clipboard=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=BD=8D=E7=BD=AE=E4=BB=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=89=AA=E5=88=87=E6=9D=BF=E5=9B=BE=E7=89=87=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util => resources}/clipboard/linux.sh | 36 +++---- .../clipboard/mac.applescript | Bin .../util => resources}/clipboard/windows.ps1 | 52 +++++----- .../clipboard/windows10.ps1 | 92 +++++++++--------- apps/joplin-vscode-plugin/src/RootPath.ts | 3 + .../src/util/UploadResourceUtil.ts | 12 +-- 6 files changed, 99 insertions(+), 96 deletions(-) rename apps/joplin-vscode-plugin/{src/util => resources}/clipboard/linux.sh (97%) rename apps/joplin-vscode-plugin/{src/util => resources}/clipboard/mac.applescript (100%) rename apps/joplin-vscode-plugin/{src/util => resources}/clipboard/windows.ps1 (96%) rename apps/joplin-vscode-plugin/{src/util => resources}/clipboard/windows10.ps1 (96%) create mode 100644 apps/joplin-vscode-plugin/src/RootPath.ts diff --git a/apps/joplin-vscode-plugin/src/util/clipboard/linux.sh b/apps/joplin-vscode-plugin/resources/clipboard/linux.sh similarity index 97% rename from apps/joplin-vscode-plugin/src/util/clipboard/linux.sh rename to apps/joplin-vscode-plugin/resources/clipboard/linux.sh index 191dc73e..7d3b44ac 100644 --- a/apps/joplin-vscode-plugin/src/util/clipboard/linux.sh +++ b/apps/joplin-vscode-plugin/resources/clipboard/linux.sh @@ -1,18 +1,18 @@ -#!/bin/sh -# require xclip(see http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script/677212#677212) -command -v xclip >/dev/null 2>&1 || { echo >&1 "no xclip"; exit 1; } - -# write image in clipboard to file (see http://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file) -filePath=`xclip -selection clipboard -o 2>/dev/null | grep ^file:// | cut -c8-` -if [ ! -n "$filePath" ] ;then - if - xclip -selection clipboard -target image/png -o >/dev/null 2>&1 - then - xclip -selection clipboard -target image/png -o >$1 2>/dev/null - echo $1 - else - echo "no image" - fi -else - echo $filePath -fi +#!/bin/sh +# require xclip(see http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script/677212#677212) +command -v xclip >/dev/null 2>&1 || { echo >&1 "no xclip"; exit 1; } + +# write image in clipboard to file (see http://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file) +filePath=`xclip -selection clipboard -o 2>/dev/null | grep ^file:// | cut -c8-` +if [ ! -n "$filePath" ] ;then + if + xclip -selection clipboard -target image/png -o >/dev/null 2>&1 + then + xclip -selection clipboard -target image/png -o >$1 2>/dev/null + echo $1 + else + echo "no image" + fi +else + echo $filePath +fi diff --git a/apps/joplin-vscode-plugin/src/util/clipboard/mac.applescript b/apps/joplin-vscode-plugin/resources/clipboard/mac.applescript similarity index 100% rename from apps/joplin-vscode-plugin/src/util/clipboard/mac.applescript rename to apps/joplin-vscode-plugin/resources/clipboard/mac.applescript diff --git a/apps/joplin-vscode-plugin/src/util/clipboard/windows.ps1 b/apps/joplin-vscode-plugin/resources/clipboard/windows.ps1 similarity index 96% rename from apps/joplin-vscode-plugin/src/util/clipboard/windows.ps1 rename to apps/joplin-vscode-plugin/resources/clipboard/windows.ps1 index 480b58fd..952990d8 100644 --- a/apps/joplin-vscode-plugin/src/util/clipboard/windows.ps1 +++ b/apps/joplin-vscode-plugin/resources/clipboard/windows.ps1 @@ -1,26 +1,26 @@ - -param($imagePath) - -# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 - -Add-Type -Assembly PresentationCore -$img = [Windows.Clipboard]::GetImage() - -if ($img -eq $null) { - "no image" - Exit 1 -} - -if (-not $imagePath) { - "no image" - Exit 1 -} - -$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) -$stream = [IO.File]::Open($imagePath, "OpenOrCreate") -$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder -$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null -$encoder.Save($stream) | out-null -$stream.Dispose() | out-null - -$imagePath + +param($imagePath) + +# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 + +Add-Type -Assembly PresentationCore +$img = [Windows.Clipboard]::GetImage() + +if ($img -eq $null) { + "no image" + Exit 1 +} + +if (-not $imagePath) { + "no image" + Exit 1 +} + +$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) +$stream = [IO.File]::Open($imagePath, "OpenOrCreate") +$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder +$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null +$encoder.Save($stream) | out-null +$stream.Dispose() | out-null + +$imagePath diff --git a/apps/joplin-vscode-plugin/src/util/clipboard/windows10.ps1 b/apps/joplin-vscode-plugin/resources/clipboard/windows10.ps1 similarity index 96% rename from apps/joplin-vscode-plugin/src/util/clipboard/windows10.ps1 rename to apps/joplin-vscode-plugin/resources/clipboard/windows10.ps1 index cadeb721..3fc4b744 100644 --- a/apps/joplin-vscode-plugin/src/util/clipboard/windows10.ps1 +++ b/apps/joplin-vscode-plugin/resources/clipboard/windows10.ps1 @@ -1,47 +1,47 @@ -# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 -param($imagePath) - -# https://github.com/PowerShell/PowerShell/issues/7233 -# fix the output encoding bug -[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding - -Add-Type -Assembly PresentationCore -function main { - $img = [Windows.Clipboard]::GetImage() - - if ($img -eq $null) { - "no image" - Exit 1 - } - - if (-not $imagePath) { - "no image" - Exit 1 - } - - $fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) - $stream = [IO.File]::Open($imagePath, "OpenOrCreate") - $encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder - $encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null - $encoder.Save($stream) | out-null - $stream.Dispose() | out-null - - $imagePath - # fix windows 10 native cmd crash bug when "picgo upload" - # https://github.com/PicGo/PicGo-Core/issues/32 - Exit 1 -} - -try { - # For WIN10 - $file = Get-Clipboard -Format FileDropList - if ($file -ne $null) { - Convert-Path $file - Exit 1 - } -} catch { - # For WIN7 WIN8 WIN10 - main -} - +# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 +param($imagePath) + +# https://github.com/PowerShell/PowerShell/issues/7233 +# fix the output encoding bug +[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding + +Add-Type -Assembly PresentationCore +function main { + $img = [Windows.Clipboard]::GetImage() + + if ($img -eq $null) { + "no image" + Exit 1 + } + + if (-not $imagePath) { + "no image" + Exit 1 + } + + $fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) + $stream = [IO.File]::Open($imagePath, "OpenOrCreate") + $encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder + $encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null + $encoder.Save($stream) | out-null + $stream.Dispose() | out-null + + $imagePath + # fix windows 10 native cmd crash bug when "picgo upload" + # https://github.com/PicGo/PicGo-Core/issues/32 + Exit 1 +} + +try { + # For WIN10 + $file = Get-Clipboard -Format FileDropList + if ($file -ne $null) { + Convert-Path $file + Exit 1 + } +} catch { + # For WIN7 WIN8 WIN10 + main +} + main \ No newline at end of file diff --git a/apps/joplin-vscode-plugin/src/RootPath.ts b/apps/joplin-vscode-plugin/src/RootPath.ts new file mode 100644 index 00000000..96e01eb3 --- /dev/null +++ b/apps/joplin-vscode-plugin/src/RootPath.ts @@ -0,0 +1,3 @@ +import path from 'path' + +export const RootPath = path.resolve(__dirname, '..') diff --git a/apps/joplin-vscode-plugin/src/util/UploadResourceUtil.ts b/apps/joplin-vscode-plugin/src/util/UploadResourceUtil.ts index 9d39f8ee..5c8d5dd8 100644 --- a/apps/joplin-vscode-plugin/src/util/UploadResourceUtil.ts +++ b/apps/joplin-vscode-plugin/src/util/UploadResourceUtil.ts @@ -1,10 +1,10 @@ -import { fileURLToPath } from 'url' import * as os from 'os' import * as path from 'path' import { createReadStream, mkdirpSync } from '@liuli-util/fs-extra' import { spawn } from 'child_process' import * as fs from '@liuli-util/fs-extra' import { resourceApi } from 'joplin-api' +import { RootPath } from '../RootPath' /** * for clipboard image @@ -65,12 +65,12 @@ export class UploadResourceUtil { const platformPaths: { [index: string]: string } = { - darwin: './clipboard/mac.applescript', - win32: './clipboard/windows.ps1', - win10: './clipboard/windows10.ps1', - linux: './clipboard/linux.sh', + darwin: 'mac.applescript', + win32: 'windows.ps1', + win10: 'windows10.ps1', + linux: 'linux.sh', } - const scriptPath = path.join(__dirname, platformPaths[platform]) + const scriptPath = path.join(RootPath, 'resources/clipboard', platformPaths[platform]) try { if (platform === 'darwin') { execution = spawn('osascript', [scriptPath, imagePath])