Developing an OpenSocial Gadget within Visual Studio - visual-studio

We are about to implement a bunch of OpenSocial gadgets. They are not going to be run on Facebook or any other social network but rather on our own internal Apache Shindig server. (We embed these gadgets using iframes in a normal ASP.NET MVC cshtml view)
According to http://docs.opensocial.org/display/OSREF/OpenSocial+Tutorial, the structure of a gadget is like so:
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Hello World!">
<Require feature="opensocial-0.8" />
</ModulePrefs>
<Content type="html">
<![CDATA[
Hello, world!
]]>
</Content>
</Module>
Whereas the HTML, CSS and JS are embedded within the in the tag. If we want to develop a file like this in XML format in Visual Studio, it renders Syntax Highlighting, IntelliSense, IntelliTrace and JavaScript debugging impossible. That's not very comfortable.
Question 1: Do you know of any usable(!) extension for Visual Studio that re-enables all these features for these OpenSocial XML gadgets?
Question 2: Or if not, have any of you ever tried the same and found a good way to work around these hurdles?
Update: We have done a proof-of-concept of using a post build step that takes a plain HTML file, a plain JS file, and an XML skeleton and merges them into a gadget. In debug mode we might conceivably include the plain files directly while in the deployment process we call Shindig with the merged XML file from the post build step:
if (#Html.IsDebugMode())
{
<iframe src="/Gadgets/HelloWorld.html"/>
}
else
{
<iframe src="http://example.org/shindig?url=http://example.org/Gadgets/Merged/HelloWorld.xml"/>
}
This is how the solution works:
Add a Gadgets\HelloWorld.js file
Add a Gadgets\HelloWorld.html file that includes the js file.
Add a Gadgets\HelloWorld.xml file with the gadget XML but with an empty <Content> tag.
Create a CSHTML page with an iframe that includes the plain HTML file in Debug mode but includes the Gadget in Release mode.
Define a Post Build Event that reads the contents of HelloWorld.html and pastes it into the Content tag of the XML. In addition it reads the JS code from the JS file and replaces the script inclusion <script src="HelloWorld.js" /> in the HTML by an inline JavaScript <script>...</script> with the respective code.
Advantages:
Clean separation of logic (JS file), presentation (HTML file) and metadata (XML file).
Complete Visual Studio HTML authoring support when working on the HTML.
Complete Javascript authoring support when working on the JS.
Tools such as JSLint can be used to check the JavaScript style.
Caveats:
In Debug mode, if we call Shindig to render the gadget, we don't have a way to open the JavaScript in Visual Studio and setting a breakpoint. However, if any error occurs in the JS code, Visual Studio will open a view on the JS code and mark the erroneous line. Once that view is open, we can use it for setting break points, too. Live editing is not possible though because the original JS file only gets merged into the XML in the course of the build process.
On the other hand, if we include the plain HTML file as an iframe instead of having Shindig render it, we gain the possibility of live-editing and debugging directly in the respective files. But of course all of the Shindig features are not effective. In particular, all the scripts automatically added by Shindig are missing or have to be included by hand.
Ideally every change in the HTML or JS file while running the application in debug mode would immediately update the merged Gadget XML file, thus allowing life editing even when using Shindig.

A2: You can develop your javascript in a separated js file and just include it in the gadget xml as you would with html file contain a js include. Further more, you can use frames inside your gadget html, but that will may prevent you from using some of the js API available from Shindig.

Related

Common JavaScript function for Outlook Add-ins

I have an Outlook Add-ins to automatically insert signature.
With desktop client, manifest.xml refers to only 1 javascript file, so I need to put everything in 1 single file.
However, I want to reuse some of the function it contains in the taskpane, which use another set of Javascript files.
Is there a way to create a common.js file that would be included everywhere?
Thanks
You can create a JS file specifically for automatically inserting the signature, and then include that file in the HTML for the taskpane. Just ensure that this javascript file only contains ES6 code, and mainly API calls that you want to be shared between the signature insertion and the taskpane parts of your add-in. Also, include it in the taskpane HTML file, after the taskpane JS file.
Alternatively, it is OK to have 2 separate files here to avoid accidentally breaking one part of your add-in when you make future updates to it.

how to add custom code snippets for Razor pages (cshtml) in VS

Is it possible to add custom code snippets for Razor pages using the Code Snippets Manager in Visual Studio?
I can add snippets for HTML, but I can't find CSHTML anywhere.
I'm using Visual Studio Community 2019 16.4.1
That is because there are no dedicated Cshtml snippets. The Razor code is either C# or VB, so those snippets will be valid in a code block.
To use those snippets you simply have to open a code-block:
Either type # or #( to open an explicit statement
type foreach
type tab twice to activate the snippet
To edit the snippets, press Ctrl+K, Ctrl+B
This worked for me in Visual Studio 2019
Create and save your XML .snippet file. Example template:
<CodeSnippet Format="1.1.0">
<Header>
<Title>NameOfSnippet</Title>
<Author>YourName</Author>
<Shortcut>ShortCutName</Shortcut>
<Description>Description</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Id</ID>
<ToolTip>Please add an ID</ToolTip>
<Default>REPLACE_ID</Default>
</Literal>
</Declarations>
<Code Language="html"><![CDATA[<div id="$Id$">Example HTML Snippet</div>$end$]]></Code>
</Snippet>
Tools > Code Snippet Manager > Language Dropdown: html > Import
Navigation to your .snippet file and open
Select both My HTML Snippets & HTML
Select Finish and OK
Open a cshtml file and type in your ShortCutName > hit tab
You should see your snippet in the cshtml, since language is html, and not CSharp, no need to open a code block
When I was trying to tackle this problem, the thing that was missing in my mental model was that a .cshtml file consists of multiple parts that are in their own language context. For example, you could have:
a <style></style> block that its contents would be css and VS would recognize CSS snippets in there,
a <script></script> block that has javascript content and VS would recognize javascript snippets
a # block where vs could recognize either CSharp or Basic snippets
If you aren't in one of the other spots, html snippets are recognized.
In my case, I wanted to add a javascript snippet but I thought that the available snippets were tied directly with my file type so I was trying to add them as HTML snippets (and then was disappointed when they didn't show up in my script tag).
ALSO, its important to note that at least with VS 2017, you may need to restart VS to get the editor to recognize any new snippets.

How Visual Studio syntax highlight works?

When I create a tag 'style' in an aspx page, Visual Studio highligths code using css style syntax to highlights and formatting. When I create a tag 'script', Visual Studio does the same, but using javascript style syntax.
When I create a .scss file, Visual Studio has scss syntax. I created a new server control that processes SCSS, and I want that code wrote inside this server control use scss style syntax. How do I vinculate a certain tag to a certain syntax processor?
Ps.: I'm using Visual Studio 2017
Editors (and many editor related features) in VS rely on a Content Type which indicates the language they support. Most editors, like C# or VB, only work on a single Content Type.
The HTML and ASPX editors in Visual Studio (which are separate editors) identify certain patterns that indicate a different language is being used. It then creates multiple internal documents in memory (TextBuffers) with separate Content Types for each language identified. The language services for each of those can then be used to handle the language natively instead of HTML having to "know" all about CSS/JavaScript/anything else. For example:
<script> blocks or event handlers create a JavaScript buffer for the contents
<style> blocks or style= attributes create CSS buffers
Any of the <%-style ASP.NET nuggets create appropriate language buffers (C#/VB)
The # in Razor CSHTML files creates a C# buffer
etc
These nested TextBuffers are actually entire documents to make the language look right. For example, if I have:
<span style="color: blue;" />
The CSS buffer is an internal document that looks something like this:
/* BEGIN EXTERNAL SOURCE */
span {
color: blue;
}
/* END EXTERNAL SOURCE */
It's important to note that this is a complete and parseable CSS document, so the language service can work as normal for finding errors, giving IntelliSense, etc.
(And if you think the CSS buffer looks messy, you should see what happens for Razor or ASPX nuggets...)
The ASPX editor then uses the VS editor platform to coordinate projections between these buffers into the single view of what you see - a TextView. So it looks like a single document, but it's being handled internally as several different documents (TextBuffers), one per language.
Now, getting to your question: how can you make the ASPX editor support SCSS? Well, there are two parts to this:
First, the ASPX editor would need to know how to identify the SCSS region of the document. This might be <style type="text/scss"> or another indicator. This could even be done by writing an extension to the ASPX editor if it were extensible (the HTML editor is much more friendly to writing extensions).
Next, the ASPX editor would create the projection span. It would hand that off to the SCSS language service. The SCSS language service needs to know that it's in a projection, and generate all that extra stuff to make the syntax tree work. (Note: the SCSS language service in VS doesn't support this today.)
From then on, everything that happens in the SCSS region needs to be mapped between the TextView (where the cursor is in the document you see) to the SCSS-specific TextBuffer (where the cursor is in the in-memory document used by the language service). Again, today the SCSS LS does not take this scenario into account.
The best way to get this to be supported is to give feedback through the Visual Studio developer community. This feature is already on the backlog, but adding additional votes for it will help the team to prioritize the work.

Transfer javascript code in html files to typescript files and link them to the views

I need some suggestions to transfer javascript code in html files to typescript files in Visual Studio 2015.
Right now, I have 4 HTML views, where I want to take out the javascript code and convert it to the typescript files. The views are:
Travel.cshtml,
Hotel.cshtml,
Tpa.cshtml,
Passengers.cshtml
Every view contains html code + razor and a javascript section.
I'm trying to create 4 typescript files and link them to the view. As the image shows, I have created the files in a folder called "pages". For now they are all just empty :)
How can I link those pages to the representative views, so that the
Travel.ts file is linked to the Travel.cshtml file?
Some may ask, why am I doing this? .. I'm doing this to get clean html files with no javascript in, so its easier to read. Also I need to learn the typescript syntax which is a little difficult for me.
Hope someone can help.
Valid javascript code is valid typescript code. So to get started, you can use your existing javascript code inside your typescript files.
Every time you save your typescript file in visual studio, typescript compiler will be compiling this file and generate the corresponding javascript file with the same name as of your typescript file name, provided you do not have any errors in your typescript file.The generated file will be in the same location of where you have your typescript files are. If you want to see those, you can enable the Show All files option in solution explorer.
You will not include the typescript files in your pages, instead we will use the generated js files. So in your case, in your Hotels view(Hotels.cshtml), you can include it like
#section Scripts
{
<script src="~/Content/Scripts/Pages/Hotels.js"></script>
}
Or if you want to bundle it, Add a new bundle in your RegisterBundles() method.
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/hotels").Include(
"~/Content/Scripts/Pages/Hotels.js"));
// Other bundles goes here
}
And in your view, use the bundle path
#section Scripts
{
#Scripts.Render("~/bundles/hotels")
}

Is there software I can use to view my Markdown files as a wiki, with relative links, on Windows

Our team has installed the Markdown Mode extension in Visual Studio on our Windows PCs, and we're happy with that as an editor for Markdown files, but we need a way to generate a wiki from those files where we can click on links that cross-link the files of the wiki. I've been trying to find something, but haven't had any success getting something running.
I tried creating an empty web application and pasting in the html file from here http://dynalon.github.io/mdwiki/#!index.md and naming it index.html, and adding a couple of md files to the same directory that I set to always copy to the build directory, but I got 404-3 errors when it tried to access the .md file.
I see a couple of tools that look possibly good but need Python or Ruby installed, which isn't ideal: http://markdoc.org/quickstart or http://helloform.com/projects/commonplace/
I see this ASP.NET control for embedding a Markdown file into a page http://wikicontrol.codeplex.com/ but the control is for VS 2010 so clearly is not being actively maintained, plus to use it I'll need to build something to take the relative links and find the related .md files and load them up in MVC - sounds like a hassle to get working, and it will require me to put MVC in my docs project.
Is there something that is just designed so that I can put an html file or similar in a directory with a root .md file and have it just immediately act like a wiki and allow navigation between them?
We have decided to use MarkdownDeep NuGet package and a single MVC controller to handle this. The MVC controller looks at the requested path, uses it to figure out the location of the Markdown file, reads that file and renders it to HTML and returns the HTML.

Resources