Tuesday, October 1, 2013

WYSIWYG Editor in Windows Forms Applications - Leveraging TinyMCE

Recently I was tasked with working on creating a version 2.0 application for an internal tool we use at work. We have a team that creates web content for our clients and this application allows them to compose that web content. In the redesign, we decided it would be fantastic if people could use a simplified HTML editor to generate content for the prompts and various information we output on the web pages. Currently, individuals are required to know (read: ask others for help) the HTML and it makes for a frustrating experience in using something that doesn't actually expose the HTML to the end user. Those who are using the app shouldn't be required to understand block elements or how to open a link in a new window. It should be painless and inputting that content should be fast.

Rather than forcing someone to use HTML tags, they should be able to highlight and toggle as if they were using a text editor. Well, I got as far as making the three buttons for bolditalic, and underline and realized I was reinventing the wheel. I had just added TinyMCE to our text areas for custom text our clients entered in our development environment. It was incredibly easy to use and it has a permissive license.

TinyMCE Sample - http://www.tinymce.com/
Naturally, there had to be a way to implement TinyMCE inside a Windows form and save a lot of time creating monotonous string handling code. And of course there was.

I recognize I'm writing this tutorial when there are others that offer help (one I used for help) on getting this set up. The one I used for help is pretty much the information that's here. That was the only other tutorial I found that was worth while. If you get tired of reading my outline of the solution, go read that one. If you want to continue with me, then go get TinyMCE. I actually downloaded it and stored it to a local resource, but you could always just use the CDN if you want. I'd recommend local.

Once you have that done, let's start a visual studio project. I'll use Visual Basic for this example but the C# code is almost identical (add curly braces, stir, and call me in the morning).

There are only 3 real pieces to this entire thing.

  1. Web browser control pointed at your local HTML file with the JavaScript stuff all in it
  2. A method to set the content inside the TinyMCE editor
  3. A method to get the content out of the TinyMCE editor
Best part? The heavy lifting is all JavaScript in the HTML page. For those that found this page via a search engine, the short version is make a method in the JavaScript for getting and setting the content of TinyMCE. Then use your big boy programming language (just kidding, I love you JavaScript) to access that information. The reason you need the get and set methods is because you're probably going to be doing something with the content that's being filled in or displayed. 

In my case, I'm pulling html content out of a database and shoving it in TinyMCE so non-technical folks can easily create styling for text. Once they're done, I invoke the get and then store the result back into the database.

Ok, enough with details. Let's see the code you'll need to produce.

HTML

Replace the bold lettering with what you need.
<script type="text/javascript" src="file:///yourpath/tinymce/tinymce.min.js"></script>


<script type="text/javascript">
    function GetContent() {
        return tinymce.get('tinyMceEditor').getContent();
    }

    function SetContent(htmlContent) {
        tinymce.get('tinyMceEditor').setContent(htmlContent);
    }

    tinymce.init({ whole host of options });
</script>

<body>
  <form method="post">
  <textarea name="tinyMceEditor" cols="1" rows="1" style="width:100%; height: 100%"></textarea>
  </form>
</body>

That's pretty much it. You'd do well to add your usual html tags to it so it is recognized as html. But yeah. That's all the html it took to get working. The cool part is setting up the other set of code for this.

Drop a web browser control on a form, tell it to navigate to your html doc. Awesome, TinyMCE loads right? So that's fantastic. But let's add the two methods it takes to get and set content. Which are crazy simple.

Public Sub SetTinyMceContent(ByVal htmlContent As String)
        Dim contents() As Object = {htmlContent}
        TinyMceBrowser.Document.InvokeScript("SetContent"contents)
End Sub

In case it's not apparent, the TinyMceBrowser object is my browser control. Now the get content method.

Public Sub GetTinyMceContent()
        Dim html As Object = TinyMceBrowser.Document.InvokeScript("GetContent")
        Dim content As String = html.ToString()
End Sub

That's it. Now obviously you probably want the Get as a function that returns a value unless it's just going to run off and perform its procedure. But that's really all it takes. I have more code in these methods to perform stuff related to our database, but these two lines in each method are where the magic is happening.

Ta da. Short. Sweet. Simple tutorial on how to bring some slick JavaScript text editor action to your Windows applications.

Monday, May 6, 2013

Initial Impressions of Light Table IDE

Light Table is a programming integrated development environment (IDE). But it's a little different compared to what is currently out there. The author, +Chris Granger, has focused on what we are creating rather than getting to the creation. If that concept doesn't quite make sense in your head, don't worry. It's visual. Think real-time flow of your program. If you want to watch how a value changes as it moves through your program you can see it in real-time. Bret Victor gave a great talk on the idea called Inventing on Principle. Light Table extends this concept and the roadmap for development has a lot of nifty fun features of programmers.

I love UX/UI. Especially when it makes stuff easier/faster/better. Light Table is basically IDE porn.

I don't think I can really describe it better than that, but I'll try to explain some things I like and where it has some room for improvement. I have been using it as a primary editor for VB.Net, Python (Django), and a little JavaScript for nearly a week. VB.Net is my day job, the others are for projects at home. For the most part I work in Sublime Text 2, which is a fantastic editor. If you haven't checked it out before the go grab it and start playing. It's extensible, built in Python, and has tons of plugins available. And yes, I write VB in it. It supports VB Script which is good enough. I switch back and forth between Visual Studio and Sublime Text 2 depending on what I'm working on.

Welcome Screen
1. Light Table Welcome Screen
So clearly minimalism is heavily at play here. I'm a fan of minimalism, so this works for me. What you see at the top are tabs. They're unobtrusive. You'll notice I'm in full screen mode here, or as I like to call it: Stop messaging me on Google Talk mode. You can hit ctrl + enter and it pops up the command prompt so you can be handsfree. Which I like. Granger is a fan of VIM so there are vim bindings that can be enabled. But I like his theory that the future of editing code can't always just be that the vim, emacs, etc style of only text visualization is the answer forever. 


Command Bar
2. Command Bar via ctrl + enter
The UI is clean and simple. After all, you don't need a ton of features to get your work done. The workspace tab allows you to bring files into the "workspace" which is kind of like open folder for Sublime Text 2. What's different is that you can add files or folders to this workspace from all over the place. Then those files all show up on the quick open stuff under navigation which we'll get to.


Light Table Workspace
3. Light Table Workspace
So any of those files on the left, or if you have multiple folders can be removed from the workspace without affecting your files on disk. Handy feature. But also, you can just add another folder or files to the workspace. And everything is available to open. Double-clicking any of the files on the left will open them for editing in the current tabset on the right. Tabsets are like groups of tabs. You can subdivide so you can have multiple tabsets each with multiple tabs.

From there we can move on to the navigate tab. It basically shows you all files in all folders. Let's look at a django project.


Light Table Navigate
4. Light Table Navigate
One of the great things about more and more programs is the support for fuzzy search. This makes using command bars and file openers incredibly easy but it also makes search in general better regarding programming languages. You may have 10 methods with Get as the beginning like GetMyFoo, GetNewBars, etc. But if you want GetMyFoo, you can just search for Foo. Fuzzy search will find it. That concept can be expanded on drastically in practice and in different concepts (command bar for instance, file names, settings, and your code itself) and you get some pretty awesome results. I've loved that in Sublime Text 2 and I'm loving using it in Light Table.

The default them is also pleasant for code. It's not terribly high contrast but I find it's easy on my eyes in a dark room. And let's be honest. We program in the dark a lot. But my other favorite monokai is on there and switching is a command and a few keystrokes away so I don't mind switching back and forth.

5. Light Table Default Theme
If you watch the videos you'll see some of the wonderful things Light Table can do, but I think the best thing going for it right now is the incredibly responsive development. I'm not sure if +Chris Granger is working solo or if he has assembled a team, but I watched the repo on GitHub for issue tracking and I'm getting a lot of mail which is Q&A and feature suggestions and bug fixes. I love that part of it. I'm actually learning more about the editor just by watching the "issues" emails come across.


I think that's all I'll go into for now. I'm using it as the IDE of choice for a Django project at the moment and it'll be interesting to see what ultimately I like and dislike at the end of that. Code Mirror's underlying editor has a few bugs with the syntax in VB, but I have a feeling some of the features the community requests will be easy to address once the plugin system is live, which is forthcoming in 0.5. I'm already wondering if I can build out some Django tools for the editor, even though I'm not sure what that would look like. It supports virtualenvs for python, although I haven't tried it. Django work means I'm not really doing eval much unless I'm just quickly testing pieces of a function.

I'll post again once I have had the opportunity to really spend a lot of hours in it. For now, I really enjoy it. I miss some of the conveniences of Sublime Text 2, but they're not anything that can't be resolved. And for an alpha...this product has definitely caught my attention. I look at it and all I can think is how amazing this is going to be.

Thursday, January 31, 2013

Windows 8: Good, Bad, and the In Between

I'm just going to admit it up front: I am a complete sucker for a good UI. UX is important as well but if you give me a really ugly UI and say "But the UX is amazing!" I'm less likely to be interested. Love it, hate it, whatever, the new Windows style of interface is attractive. Live tiles are a clever method for content consumption that is far more interesting than a straight feed reader. Even though that's basically the purpose.

Sure you can put whatever you want there, but essentially it's a feed. The ease of programming for the Windows 8 style of application is also welcome. Granted, I really don't know anything about XAML. I can mostly read what's going on but I'm currently having to read up on what it is I want to do if I'm not going to be dropping things in from the toolbox. But it's fun. And you get Visual Studio.

I like Eclipse a lot. My Java class and C class both had me using Eclipse (IDE was optional but Eclipse was a natural choice) and I found myself productive in it and all that. But when it came to playing with Android, the experience felt extremely clunky. I know there are many that will disagree, and that's fine. Preference is subjective. Visual Studio feels streamlined. When I drive a car, gadgets are fun but I really only care if they enhance my driving experience. I don't need a million buttons to do things. I don't have to have volume control on my steering wheel, but it's handy. I don't need extensive configuration. My car's purpose is a point A to point B type of thing for me and I like to feel the drive. I feel the same about the tools I use to fulfill my role of code monkey.

Visual Studio deploys easily between tests for Desktop (windows 8 store), Tablet, and Phone. The simulators are clean for tablet and phone. And you don't have to wait for what feels like an eternity for them to boot. That's my favorite part.

As for the operating system itself, I don't understand all the complaints. Granted, I'm an individual that hits the windows key and then immediately begins typing to get my desired program. I don't like having to run through context menus to find it. So for me to use Windows 8, I had almost no learning curve. Yes to get to my wireless settings I have to go through the charm bar. So what? Hide the stuff I don't need to see all the time. I'm fine with that. I spend most of my time on the desktop. I really don't see the Start menu dash unless I'm doing a search to find the program I want.

This isn't some sort of colossal failure or a war on users. Why is it we idolize the screens we see on Star Trek and such and then when we're presented with something that moves more toward that direction we reject it?

I've never been a fan of the ribbon for Office, but I actually like it for Windows Explorer. New folder? Boom. Dedicated icon for that. Search filtering. Dedicated icons. I feel like it's an improvement. It adds a little bulk to the top of the window but it isn't an unwelcome feature.

Windows 8 really shines with touch interaction though. I like the paradigm shift. The choice of blending tablet with desktop is nice because you can grab a tablet PC (that is, one running Windows 8, not WindowsRT) and have it on the couch while you're watching television, but then snap it in to whatever docking method your particular hardware supports and you're back to your usual mouse and keyboard. Love that.