Vscode API: read clipboard text content - node.js

I'm currently trying to write an extension for Visual Studio Code, and I can't manage to understand how to read the clipboard content.
The VSCode API specifies this method:
readText ():Thenable<String>
Following what I read about Thenable, I should be able to get the clipboard's text like that:
var clipboard_content = vscode.env.clipboard.readText().then((text)=>text);
But all I manage to get is a Promise { pending } object.
What I would like to get is the clipboard content as a string

Basics mistake.
Because you use promises (async) and want async/await (linear) functionality.
It should be (promises, async code):
vscode.env.clipboard.readText().then((text)=>{
clipboard_content = text;
/* code */
});
or (sequential code)
let clipboard_content = await vscode.env.clipboard.readText();
/* code */
PS.: In JS, you should use camelCase instead of snake_case when naming variables and functions. This is one of the recommendations of JavaScript Standard Style

Related

How to load txt file in haxe in js project?

I've started a haxe js project in FlashDevelop, I need to load a local file, is this possible? how to to so?
The simple answer is use "resources". You add a path and an identifier to your hxml:
-resource hello_message.txt#welcome
And you use it in your code like this:
var welcome = haxe.Resource.getString("welcome");
Note that the operation is performed at compile time so there is no runtime overhead. It is essentially equivalent to embed the file content in a quoted string.
The complex answer is to use a macro. With them you can load, parse, process and do all the manipulation you might need. Pretty commonly, you can see macros to load a config file (say JSON or YAML) and use it as part of your application (again at compile time and not at runtime).
You could grab files with an XMLHttpRequest as long as you keep them somewhere public (if you're putting it online) and accessible to the script.
Here's a quick example of grabbing a text file from the location assets/test.txt
This is the sort of thing I usually do in the JS games I make, I find it a bit more flexible than just embedding them with -resource.
If it's not exactly what you're looking for then Franco's answer should see you through.
package ;
import js.html.XMLHttpRequest;
import js.html.Event;
class Start {
static function main() {
var request = new XMLHttpRequest();
// using the GET method, get the file at this location, asynchronously
request.open("GET", "assets/test.txt", true);
// when loaded, get the response and trace it out
request.onload = function(e:Event){
trace(request.response);
};
// if there's an error, handle it
request.onerror = function(e:Event) {
trace("error :(");
};
// send the actual request to the server
request.send();
}
}

Visual Studio 2013 NodeJS Tools, TypeScript, Keep Comments

That's my setup. VS 2013, with the Node JS Tools, and Typescript. Adding a .ts file is handled without a hiccup. I am having some issues with the npm integration, but I've been able to work around them.
I've also added EdgeJS. It doesn't yet support TypeScript but I just write my EdgeJS calls with regular JS in my TS files. The problem is that EdgeJS allos you to write your CS functions a few different ways.
One way is like the following, where the entire body is enclosed in a comment block:
var hello = edge.func(function () {/*
async(input) => {
return ".NET welcomes " + input.ToString();
}
*/});
Unfortunately, the TS compiler, by default, removes these comments and I can't find a way in this project type to change that behavior.
Am I just out of luck (for now)?
To preserve comments for TypeScript, you'll need to start them on a new line. In the example you provided, the multi-line comment is not preserved as it starts on the end of a line with code.
Simply move the block comment start:
var edge = edge.func(() => {
/*
async(input) => {
}
*/
});

JavaScript Library XPages

I currently have JavaScript dotted around my XPages - I would like to create a JavaScript library containing all this code and then just call the individual functions e.g. the press of a button. Here is an example of the code I would like in the JavaScript library:
// get the user document for that person
var myView:NotesView = database.getView("xpBenutzerInnen");
var query = new java.util.Vector();
query.addElement(sessionScope.notesName);
var myDoc:NotesDocument = myView.getDocumentByKey(query, true);
When I place this code in the library I get the error:
Syntax error on token ":", ; expected
I assume that the "var name:type" declaration is specific to XPages (this is what I found on creating JavaScript vars: http://www.w3schools.com/js/js_variables.asp) - I could just remove the type declaration and the code seems to run without any problems - I would like to better define the variable type though.
Is there any way that I can move the code out of the XPage but still keeping my typing?
Thanking you in advance
Ursus
You need to distinguish between client side JavaScript and Server side JavaScript. Your code is server side JS and should work in a Script library just as it does inside an XPage. Have you accidentally created a client side JS lib?
A few pointers: I try to make functions in a script library independent from global objects. Mostly the database object. This function works in a library just fine:
function getUserDocument(username:string, db:database) {
var myView:NotesView = db.getView("xpBenutzerInnen");
var query = new java.util.Vector();
query.addElement(username);
var myDoc:NotesDocument = myView.getDocumentByKey(query, true);
myView.recycle();
return myDoc;
}
Let me know how it goes

Config DustJS (ServerSide) do not escape new line?

I'm using DustJS with ExpressJS with NodeJS.
When ExpressJS renders, it auto escape the new line and white space in my dustJS; while since I'm putting JavaScript into my DustJS templates, I want to leave the white spaces as it is.
How to achieve that?
Thanks a lot.
Update: here's some further explanation:
In my template.dust, I have something like:
<script type='template' id="tempVO">{.vo|s}</script>
<script> {! Block to compile the dustJS into cache !}
$(function($){
dust.compile($("#tempVO").html(),"vo");
// register helper
dust.helpers.justToString = function(chunk, ctx, body, param){
console.log(param);
}
})();
The default behaviour of DustJS, when used with expressJS by doing:
app.get("/",function(req,res){
res.render("ajaxTemplate",xmlService.templates);
});
would escape all the white spaces so that it turns out something like:
$(function($){dust.compile($("#tempVO").html(),"vo");// register helperdust.helpers.justToString = function(chunk, ctx, body, param){console.log(param);}})();// AJAX Callback</script><script>$(function(){$(".tipNeeded").tooltip();$(".tabsNeeded").tabs();});
Which is certainly not what I want.]
Although I can just use some external JavaScript Import, like:
<script src="my/own/beautiful.js"></script>
Still I'd like to know how to put in-HTML script by not-escaping the white space when render my dustJS files.
From the Dust Tutorial on controlling whitespace suppression, the exact method would vary based upon the version you are using. If you are on v1.2.6 (the latest as of this writing) you would add this line to your code:
dust.optimizers.format = function(ctx, node) { return node };
To quickly test it, add that line immediately after your require for the dustjs-linkedin module:
var dust = require('dustjs-linkedin');
Edit:
From version 1.2.0 to 1.2.5, the compile function took three arguments:
dust.compile = function(source, name, strip)
If strip was set to false then whitespace would be preserved. However, it looks like this argument was removed in 1.2.6. Even with a version that has the strip argument, when using express, you aren't calling dust.compile directly. You might be able to use something like consolidate.js as a basis to write two render calls, one with whitespace and one without...but I don't see a plainly simple way.

Passing raw Markdown text to Jade

I'm playing around with my first Node.js Express application, and as every programmer knows, the first thing you should build when testing out a new framework is a blog! Anyway, I'd like to write the articles in Markdown and then render it in the view. I saw that Jade allows for this to be done inside the view itself, using filters, but I can't get that working.
To simplify the situation, here's an example of what I'm talking about.
//app.js
res.render("article", {
md : "Hello World!\n\n*Woo*"
});
//article.jade
section
:markdown
#{md}
But, that outputs this: <section><h1>{md}</h1></section>... it isn't substituting in the variables I've passed to it.
Then I tried this:
//article.jade
section
:markdown
!{md}
And the output is this:
<section><p>Hello World!
*Woo*</p></section>
So, now it's not parsing the markdown!
I have been able to get this to work by parsing the markdown in the app.js file and then passing the HTML to the view to display, but I don't know, that seems a bit messier.
Is there a way to pass variables into Jade filters?
You can do this with a function passed in to jade from node:
var md = require("node-markdown").Markdown;
Then pass it into the view as a local:
res.render('view', { md:md, markdownContent:data });
Then render it in the jade view by calling the function:
!= md(markdownContent)
The node module node-markdown is deprecated. The marked is advanced new version. You can try like this
var md = require('marked');
Inside your router
res.render('template', { md: md });
Inside your jade template
div!= md(note.string)
I don't think jade can do this out of the box. One way to accomplish it that might feel slightly cleaner than pre-rendering the markdown is to create a helper function called markdown that takes a markdown string and returns HTML. Then you could do something like
section
!= markdown(md)
The markdown function should be included in the locals data when you render the jade template and can directly use a markdown library to convert the markdown syntax to HTML.
If you are using Scalate's Jade support you can enter:
section
:&markdown
#{md}
You can also import external files with:
section
:&markdown
#{include("MyFile.md")}

Resources