Now before y'all lynch me for parsing XML with regex like Stackoverflow is want
to do, hear me out:
I'm trying to insert a link in TLF with a button like in a rich text editor.
Simple, right? Normally you would simply simply use
var linkElement:LinkElement = textArea.textFlow.interactionManager.applyLink( ... );
But no, that alone doesn't work: if your selection crosses format boundaries it
completely fails to assert the link formatting so you have no idea where the
link is that you've just created. Simply adding a LinkElement via addChild()
doesn't work either.
I've got a rudimentary version working for now which rips out the textFlow for
the selection with interactionManager.cutTextScrap(...), wraps that text in a
LinkElement and then "pastes" it back in with interactionManager.applyLink( ...
)...
But that only works so long as the link doesn't span across paragraphs and list
items. If it does, it completely and utterly crashes and burns: paragraph and
list structures collapse.
...so I've had to implement validation which disallows the user from creating
links spanning across different FlowElements (paragraphs or listitems). Which is
kludgy, and not very good UX.
So I have to create my own link insertion routine.
What I've resolved to do is to:
1) convert the textflow of the entire document to a string
2) find the start and end indexes of the selection within the textflow string
3) insert the following string at the start index:
</span><a href="[hrefVar]" target="[targetVar]"><span>
4) insert the following string at the end index:
</span></a><span>
5) reconvert the textflow string into a textflow object for the TextArea
And voila! Instant RTF link!
The only problem is... I have no idea how to write a regex parsing equation
which can find the start and ending indexes for a string match inside XML markup
where the result may be spread across several tags.
For instance, if the TextFlow is (abbreviated):
<TextFlow><p><span>Lorem Ip</span><span fontWeight="bold">sum do</span><span>
lor sit am</span><span fontStyle="italic">et, consectetur adipiscing elit.
</span></p></TextFlow>
And if, for instance, the user has selected "Ipsum dolor sit amet" to be
converted into a link. I need to find the first and last indexes of "Ipsum dolor
sit amet" within that RTF markup, and then insert the strings indicated in 3)
4) above, so that the end result looks like this:
<TextFlow><p><span>Lorem </span><a href="http://www.google.ca" target="_blank">
<span>Ip</span><span fontWeight="bold">sum do</span><span>lor sit am</span>
<span fontStyle="italic">et</span></a><span>, consectetur adipiscing elit.
</span></p></TextFlow>
What I need is the regex to do step 2).
I know the regex to ignore tags and strip out the text between tags, and how to
find a string match of the selected text in the stripped textflow text... but
not how to find the match indexes within the original (unstripped) textflow string.
Anyone?
to do, hear me out:
I'm trying to insert a link in TLF with a button like in a rich text editor.
Simple, right? Normally you would simply simply use
var linkElement:LinkElement = textArea.textFlow.interactionManager.applyLink( ... );
But no, that alone doesn't work: if your selection crosses format boundaries it
completely fails to assert the link formatting so you have no idea where the
link is that you've just created. Simply adding a LinkElement via addChild()
doesn't work either.
I've got a rudimentary version working for now which rips out the textFlow for
the selection with interactionManager.cutTextScrap(...), wraps that text in a
LinkElement and then "pastes" it back in with interactionManager.applyLink( ...
)...
But that only works so long as the link doesn't span across paragraphs and list
items. If it does, it completely and utterly crashes and burns: paragraph and
list structures collapse.
...so I've had to implement validation which disallows the user from creating
links spanning across different FlowElements (paragraphs or listitems). Which is
kludgy, and not very good UX.
So I have to create my own link insertion routine.
What I've resolved to do is to:
1) convert the textflow of the entire document to a string
2) find the start and end indexes of the selection within the textflow string
3) insert the following string at the start index:
</span><a href="[hrefVar]" target="[targetVar]"><span>
4) insert the following string at the end index:
</span></a><span>
5) reconvert the textflow string into a textflow object for the TextArea
And voila! Instant RTF link!
The only problem is... I have no idea how to write a regex parsing equation
which can find the start and ending indexes for a string match inside XML markup
where the result may be spread across several tags.
For instance, if the TextFlow is (abbreviated):
<TextFlow><p><span>Lorem Ip</span><span fontWeight="bold">sum do</span><span>
lor sit am</span><span fontStyle="italic">et, consectetur adipiscing elit.
</span></p></TextFlow>
And if, for instance, the user has selected "Ipsum dolor sit amet" to be
converted into a link. I need to find the first and last indexes of "Ipsum dolor
sit amet" within that RTF markup, and then insert the strings indicated in 3)
4) above, so that the end result looks like this:
<TextFlow><p><span>Lorem </span><a href="http://www.google.ca" target="_blank">
<span>Ip</span><span fontWeight="bold">sum do</span><span>lor sit am</span>
<span fontStyle="italic">et</span></a><span>, consectetur adipiscing elit.
</span></p></TextFlow>
What I need is the regex to do step 2).
I know the regex to ignore tags and strip out the text between tags, and how to
find a string match of the selected text in the stripped textflow text... but
not how to find the match indexes within the original (unstripped) textflow string.
Anyone?