Wednesday, November 7, 2012

AS3 TextConverter.importToFlow Explained

Working on a WYSIWYG type editor for a project, I found myself needing to import and export raw TextLayoutFormat in string format (vs XML) for use in transport. When obtaining the string data and using it within an import, I found myself not quite sure how to use the returned TextFlow object.

This is how I solved the problem. First I used the importToFlow passing along my string, and specifying that it was in the TEXT_LAYOUT_FORMAT, and the configuration file I wanted the TextFlow to have (since it is only read-only following object construction).

var flow:String = TextConverter.export(textFlow, TextConverter.TEXT_LAYOUT_FORMAT, ConversionType.STRING_TYPE) as String;
var _textFlow:TextFlow = TextConverter.importToFlow(flow, TextConverter.TEXT_LAYOUT_FORMAT, configuration);

Now I found myself with two TextFlow objects, one which was the original connected to several controllers, and implemented with many event listeners, and so on. I actually had two problems.

Problem 1: The data string which I assumed had textflow structure within it, was empty. An empty string will fire an error, so be sure the string data is properly formatted and not empty.

Problem 2: Simply replacing my orginal TextFlow object with my new one meant that all my existing attribute would be lost.

I ended up creating a method called setupTextFlow which would take a TextFlow instance and apply all the listeners I wanted, as well as would loop through a dictionary of existing containers and reconnect them, and then finally set it to update all controllers.

Side Note: I wish I could just set the TextFlow object to contain my new data, but I couldn't find any methods that would allow me to just replace contents. replaceChildren almost seems to work, but I couldn't find a way to access all the children elements of the TextFlow for replacement.

No comments: