AIR Example : HTML Editor with live preview
As part of my Flash on the Beach Intro to AIR Session, I built a simple HTML Editor with a live preview. I have added comments to the code, and uploaded it to the on AIR tour google repository.
This example demonstrates:
- Using the File API to write a string to a file
- Using the HTML Control within an application to render a string of HTML
- Using the File.browseForSave API to open a native save dialog.
Here is the code if you just want to glance at it:
HTMLEditor.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
viewSourceURL="srcview/index.html">
<!-- include the code for the main app class -->
<mx:Script source="HTMLEditorClass.as" />
<!-- TextArea used to type in HTML -->
<mx:TextArea height="172" top="10" left="10" right="10"
change="onTextChange()"
id="inputField" />
<!-- HTML Control which we use for live preview of HTML -->
<mx:HTML top="190" left="10" right="10" bottom="41" id="htmlField"/>
<!-- Save button to save the HTML to a file -->
<mx:Button label="Save" right="10" bottom="10" click="onSaveClick()"/>
</mx:WindowedApplication>
HTMLEditorClass.as
import flash.events.Event;
import flash.filesystem.File;
//called when the text / html is changed
private function onTextChange():void
{
htmlField.htmlText = inputField.text;
}
//Called when the user selects a file to save
//html in.
private function onBrowseForSave(e:Event):void
{
//get a file reference to the file the user selected
var f:File = File(e.target);
//create a file stream to write to the file
var fs:FileStream = new FileStream();
//open the file for writing
fs.open(f, FileMode.WRITE);
//write string of html to file
fs.writeUTFBytes(inputField.text);
//close the file
fs.close();
}
//called when user presses save button
private function onSaveClick():void
{
//get a reference to the desktop dir
//this will be used as the default dir that the dialog
//will open at
var f:File = File.desktopDirectory;
//listen for the select event for when the user selects
//a file to save the html in
f.addEventListener(Event.SELECT, onBrowseForSave);
//open the browse for save native dialog, and pass in a title
//for the dialog
f.browseForSave("Save HTML File");
}
You can grab the code from the on AIR Tour code repository.






[...] ??????? (Mike Chambers) ? ????????? ??? ?? ?????????. ? ???? ?????? ?? ??????? HTML ????????, ? ??????? ????? [...]
AIR: HTML ???????? ? ???????????? ?????? | RIA crumbs. RIA development related blog
6 Nov 07 at 9:12 am
Interesting article, thanks for posting it.
Quick side comment, shouldn’t your tag line be:
code == joy ;)
rob
6 Nov 07 at 9:30 pm
[...] AIR + HTML LIVE! Mike Chambers [...]
Simon Shand on Nothing In Particular » Microsoft’s Zune Journey
7 Nov 07 at 5:38 am
[...] – Richard Galvan, et al., Adobe “50 Reasons AS3 Kicks Ass” – Grant Skinner “Intro to AIR” – Mike Chambers “Make Flash Games, Retire Early” – Keith Peters “Dynamic Abstractions” – Joshua Davis “If it ain’t [...]
jonnymac blog » Flash on the Beach 07 / Day 1 / Sessions and Adobe Keynote
8 Nov 07 at 5:05 am
Nice example Mike. thanks for the post. your blog has some good stuff!! :)
Anuj Gakhar
8 Nov 07 at 5:40 am
[...] Mike Chambers » Blog Archive » AIR Example : HTML Editor with live preview As part of my Flash on the Beach Intro to AIR Session, I built a simple HTML Editor with a live preview. I have added comments to the code, and uploaded it to the on AIR tour google repository. (tags: AIR apollo flex actionscript code) [...]
links for 2007-11-12 | The Marketing Technology Blog
12 Nov 07 at 7:25 am
Mike,
Thanks for this. I knew something like could be done, I just hadn’t taken the time myself to mess with it. One thing i love about AIR apps and Flex 3.0 is the HTML pane.
Will we see it in the web version of Flex anytime soon? There are quite a few projects I have that could make good use of it. The text pane just doesn’t have the power that the HTML pane has.
Greg Ferrell
14 Nov 07 at 5:27 am
I just want to try this… :)
Vanda
8 Apr 08 at 3:00 am