Comments (8)
Yes,you are right.There are some convenient API of Electron to handle clipboard,but vscode only provide the node environment to plugin.A plugin can't access Electron API throught vscode for now.
You can browse:
- access electron API from vscode extension · Issue #3011 · Microsoft/vscode
- Clipboard access through Extensions API · Issue #4972 · Microsoft/vscode
from vscode-paste-image.
Ah! That makes sense!
What about creating a small electron app, and then invoking it from your extension?
For a proof of concept I did this:
Add Electron dependency to the extension (sadly a big download, but it works!)
npm install --save electron
in your extension you launch a custom standalone Electron app to save the png:
var child_process = require('child_process')
var pathToElectron = require('electron'); //when run from node this returns the path to the executable
var output = [];
function collect(data) { output.push(""+data); }
var proc = child_process.spawn(
pathToElectron,
[
"/path/to/extension/custom-electron-app",
"/path/to/destination.png"
]
)
proc.stdout.on('data',collect);
proc.stderr.on('data',collect);
proc.on( 'close', function(){ console.log(output.join("")) } )
and then in /path/to/extension/custom-electron-app/index.js
const {app, clipboard, BrowserWindow} = require('electron')
const fs = require("fs")
app.on('ready', captureClipboard)
function captureClipboard(){
var destination = process.argv[2];
console.log(destination)
var clipboardContents = clipboard.availableFormats() // probably ["image/png"] for a screenshot on the clipboard
var clipboardImage = clipboard.readImage();
var uriImage = clipboardImage.toDataURL(); //if you wanted to embed the entire image in the markdown
var pngBuffer = clipboardImage.toPng() //returns a node Buffer that contains a png that you
//could write to disk: https://nodejs.org/api/buffer.html#buffer_class_buffer
fs.writeFile(destination,pngBuffer, function(err){
if(err) {
console.error("Error",err);
} else {
console.log("done")
}
});
app.quit()
}
You'd obviously need to add in proper error handling and communication from the sub process to the extension, but it should all work in theory.
Thanks!
from vscode-paste-image.
+1 for Linux support! Cheers!
from vscode-paste-image.
@danshryock very very thank you for your research!
Issue #3011 had mentioned the method that use child process to start up a electron and call electron api from this child process. It sounds feasible,but I feel it is not a good or a elegance way.Because a electron environment is very big to a plugin and start a electron cost a lot of time.
As vscode is build on electron, so the best way is that vscode expose the lower api to plugin environment. We have to wait.
But I really holp every can use this plugin, so I plan use this method when user is using win/linux.But When I finish code,I found a new problem....
If not use electron, the size of packaged plugin is 1.6M, after add electron, it become 64MB....
And it cause I can't publish plugin to vscode marketplace....(marketplace only accept plugin that limit to 20MB,see cannot publish no request found. · Issue #92 · Microsoft/vscode-vsce)
So....
from vscode-paste-image.
Ah, I suppose that makes sense. Thank you for trying! I really appreciate it!
Hopefully examples like this will help the VSCode team to realize that they need to provide some sort of support to the extension community for things like this.
Maybe via enhanced APIs provided by them, or maybe even a simple way to run a sandboxed electron instance for extensions. It could be a shell exec of the electron provided with VSCode, or maybe a custom background electron that they provide for extension authors to use.
microsoft/vscode#3011
microsoft/vscode#4972
from vscode-paste-image.
@danshryock @viniciusarcanjo plugin support windows now! @kivle add the support of window by powershell.
from vscode-paste-image.
Great news! Thanks!
from vscode-paste-image.
@danshryock @viniciusarcanjo support Linux now! I use xclip to implement this feature. And I add featrue that use selected text as the image file name.
from vscode-paste-image.
Related Issues (20)
- Quick custom name image paste HOT 1
- I don't get how to use this...
- Export ipynb with the pictures
- customize ${imageSyntaxPrefix}
- How to get the default name to be YYYY-MM-DD_HH-mm-ss_<selected text>
- New line character support for pasteImage.insertPattern
- Could not find any matching file with following pattern: puppet/shared.pp
- encoding image file name HOT 6
- Unable to change the default name (using a format)
- Adjusting image size?
- Use on VSCode for the Web
- Windows paste image is not working on vscode insider, cannot generate image file, but work correctly on normal vscode
- Suggestion: When pasting non-image content, fall back to normal paste command
- Add 'Paste Image' to Context Menu to pick up the base path more easily HOT 1
- [Suggestion]: Add file summary md5 to the file name
- Settings do not apply. HOT 4
- There is not a image in clipboard & can't capture screenshots in markdown
- alt text feature
- If the image has spaces on it, the preview does not works
- Suggestion - file name encoding use dash instead of URL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vscode-paste-image.