Copy text between fields preserving formatting

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Copy text between fields preserving formatting

Post by Traxgeek » Sat Dec 14, 2013 8:28 pm

Hi,

Whilst I continue to struggle with Android I have been trying to come up with a generic (cross platform - hence I'm not using the 'clipboard') method / script to copy and paste text between two html fields.

In my scenario a user may have entered some quite detailed 'standard' text in one record and want to have it duplicated to another (generally, new) record.
I currently have buttons to make a users selected text bold, italic, underlined & coloured etc and other buttons enabling certain standard data to be inserted at the cursor position (such as the date etc.). All this works great. :)

My problem comes when I try to copy html formatted text from one field to another html formatted field. I always end up with either no formatting for my inserted / copied text (more precisely, inserted text that inherits the text formatting around its insert position) or the verbose html (< b>My < i>copied text< /i>< /b> type of thing).

I've tried playing with [1] just inserting the variable contents (gsMyCopiedText) into the new location, [2] putting my copied text into another field (as htmltext - which displays the formatting correctly in this other, temporary field) BUT I cannot for the life of me put formatted text into a specific (cursor) location in an existing formatted field without (like I say) it either losing its formatting or it displaying the html format tags...

I've have tried using 'selectedchunk' to get me the start / stop positions of the highlighted position within the new field that the copied text is to go into and also 'put 'gs'MyCopiedText'...into the selection' to try and sort this out but whatever I do I make no progress... and I'm now going round in circles ! :( I've also googled and searched the forums for an answer without success...

Is this normal behaviour (I can copy and paste into a specific location - but lose any html formatting) or is there something I'm missing please ?

Thanks.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Copy text between fields preserving formatting

Post by dunbarx » Sat Dec 14, 2013 8:54 pm

Hi.

Do I understand what you want? If I have two fields with a small amount of text, and a blinking cursor in fid 2, say, and this in a button:

Code: Select all

on mouseUp
put fld 1 into the selectedChunk
end mouseUp
I can insert text at the cursor.

But you want the htmlText of fld 1 to be inserted, correct? I am no expert in this, but it seems at first glance that the "setting" of htmlText is at odds with the "putting" of text at all, in that you cannot put the htmlText, it being a property.

I would wait for others to comment, but If there is no simple solution, can you live with this kluge:

Code: Select all

on mouseUp
   get word 2 of the selectedChunk
   set the htmlText of fld 2 to char 1 to it of fld 2 & the htmlText of fld 1 & char it to (the length of fld 2) of fld 2
end mouseUp
Ugly, but works.

Craig Newman

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Copy text between fields preserving formatting

Post by bn » Sat Dec 14, 2013 8:57 pm

Hi Traxgeek,

do you want to copy formatted text from one field into formatted text of another field manually or by script?

by script this works for me assuming the insertion cursor is anywhere in field 2

Code: Select all

on mouseUp
   put the htmltext of field 1 into tData
   put the selectedChunk of field 2 into tSelect
   set the htmltext tSelect to tData
end mouseUp
you could elaborate on that using only a chunk of field 1 etc. You could also define the insertion in field 2 by giving a position reference

Kind regards

Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Copy text between fields preserving formatting

Post by dunbarx » Sat Dec 14, 2013 9:41 pm

Bernd.

Same as mine, sort of, but yours so much cleaner.

Is there no other, more direct way than to assemble like Legos?

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Copy text between fields preserving formatting

Post by bn » Sat Dec 14, 2013 10:10 pm

Hi Craig,

I don't know of an easier way than to set the htmlText of a chunk expression.

What you could also do is

Code: Select all

on mouseUp
   select text of field 1
   copy
   select after word 3 of field 2
   paste
end mouseUp
this will keep the formatting intact. But I think traxgeek did not want to use the clipboard.

Even a little more Lego is the effective HTMLtext...

Kind regards
Bernd

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Copy text between fields preserving formatting

Post by Traxgeek » Sun Dec 15, 2013 10:03 am

Hi both,

Hmmm... I have struggled with this seemingly simple little thing....

What I'm trying to explain (poorly):
I need my app to offer the same functionality across platforms. The app is designed to 'rove' on a tablet (away from the office/whilst on site) but 'return' to a desktop (office) when desired. So, clipboard actions will (as I understand it) not help me. (Oops ! Forgot, I'm coding on a Mac (mavericks) with LC6.5.0 commercial). So...

I have a card with a text input field on it.
In the field is a 'load of' html formatted text the user has entered.

I have a second field (generally on a different card (easier to explain like this) - but (in reality) on a new instance of the same card (with a different client data).
In this second field is also (potentially) a 'load of' html formatted text that the user has already entered (but it may too be empty as of yet).

I would like to copy a user selected, html formatted, portion of the first field (on the first card) to a user selected position in a another html formatted text field on the second (instance) of a card - whilst maintaining the html formatting of the copied text.

So, what I currently do is along the lines of :
COPY Script - this just selects the user-hilighted characters and dumps them (in this case) into a global (this could also be a Custom Property or...)

Code: Select all

-- assuming my vars are declared...
--...and after checking the selected (target / focussedobject) field is indeed a copy-able field... 
put the selectedchunk into sText
put word 2 of sText into nSa
if nSa > 0 then --Just check the cursor is in the field
    put the htmltext of the selection into gsCopyText
else
    beep --Warn the user of invalid cursor location...
end if
Now then, if I simply 'set the text of field "FieldToPasteInto" to gsCopyText', then I get html formatted text in the field (but, of course, no other text, if there were any) - so , in theory, I'm half way there ! Now all I have to do is set the insertion point. Well, I could do this with 'selected chunk' of the newly highlighted field and, after all the usual checks, 'put gsCopyText into the selection' or split the text in the destination field and insert the copytext into it... or... in all other cases I've tried, when I try to ADD or INSERT my copytext into an existing text I simply end up with either text that takes the formatting of the destination point OR text that displays the html tags...

Code: Select all

put the text of field "lblCopyText" into the selection --no formatting, no tags
put the htmltext of field "lblCopyText" into the selection --no formatting, tags are displayed
put gsCopyText into the selection --no formatting, tags are displayed
BUT, your way (both of you) works like a charm :

Code: Select all

put the selectedChunk of field "DestinationField" into sDestination
set the htmltext sDestination to gsCopyText
I'm not sure why... but I'm absolutely delighted it does - THANKS A MILLION GUYS ! Just goes to prove 'there's more than one way to skin a cat' and LC is very flexible even if one doesn't always receive the result one expects...
Is there, in fact, any specific reason I'm missing why Bernd's/Craig's script works where as mine doesn't (except that you two are obviously far more adept than I !)

I can see :

Code: Select all

put gsCopyText into the selection --Fails miserably
but

Code: Select all

set the htmltext sDestination to gsCopyText --works brilliantly
any obvious reason as to why - apart from that's the way the engine translates the two commands...

Either way, thanks again chaps - MUCH appreciated...
Have a great weekend !
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Copy text between fields preserving formatting

Post by bn » Sun Dec 15, 2013 12:06 pm

Hi Traxgeek,

Maybe it helps to understand if you break the representation of text down into different levels of the appearence of text.

First there is the text which for now we assume to be ASCII text (unicode is a bit more involved but similar)

Each character is represented by a number between 0 and 255. Nothing more. If I type an uppercase A the number is 65, a lowercase a is 97. There is no information how to represent this in a field. No size no font no nothing.

In livecode the text of a field is the sequence of those numbers. The fact that you can see them is that the field has certain text properties like fontSize, font, right or left align etc.
The ASCII text is then displayed using these properties of the field. And those field properties apply to all the text in the field.

Now you want to change some of the displayed properties of the text, let's say you want word 5 of the text to be bold.
This information has to go somewhere to be applied to word 5. That is what the htmlText of the field is for. It is a representation of both the text plus the style of the text, in this case bold.
Up until recently the only way to get at the styled representation of the text was the htmlText of the field or a chunk of that field. (as of Livecode 5.5 onwards there is also an array form called the "styledText" which is similar to htmlText)

For Livecode a field is a container and the command you use to address a container is put something into aContainer. When this issued for text Livecode takes the text (ASCII) and puts it into the field. Hence you end up with the text(ASCII) of the htmlText of some text in a field if you put the htmlText of word 5 of a given text. This is what happens with

Code: Select all

put the htmltext of field "lblCopyText" into the selection --no formatting, tags are displayed
put gsCopyText into the selection --no formatting, tags are displayed
If you set word 5 to bold then you set a property of word 5 to "bold". That is on top of the ASCII "numbers". Livecode treats properties differently. You have to SET a property.

Code: Select all

set the textstyle of word 1 of field 1 to "bold"
What you want to do is to carry over the text and the properties of the text into another field. That is why you use the htmlText BUT you have to SET the properties of the insertion char(s) to the htmlText of the source.
This will replace the properties (bold) (and the text) of the recipient chunk(s) text. This is what happens here

Code: Select all

put the selectedChunk of field "DestinationField" into sDestination
set the htmltext sDestination to gsCopyText
which more generically could be

Code: Select all

set the htmlText of last char of field "DestinationField" to the htmlText of word 5 of field "origin"
So far so good.
But htmlText is not a complete description of the display of the text. It only stores differences from the fields own properties like font and size. If you want to take everything over to the destination field, including font and size of the original field you would use the effective htmlText of the source. What is it good for? Assume your field "origin" has a textSize of 12 but your field "destination" has a textSize of 18. Using the htmlText your bold word will be displayed in fontSize 18. Which is what is wanted in most case. But if you want to force the exact size and font etc of the original chunk in the destination field you would use "effectve htmlText"

I hope this explains it a bit what is going on.

And tomorrow we have fun using the extremly powerful and granular multi-dimensional array "styledText" (just kidding) :)

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Copy text between fields preserving formatting

Post by bn » Sun Dec 15, 2013 12:14 pm

Just to add a bit:

if you say

Code: Select all

put the text of field 1 into field 1
you effectively wipe out all formatting like bold etc of field 1

the same as above goes for

Code: Select all

put field 1 into field 1
but

Code: Select all

set the htmlText of field 1 to the htmlText of field 1
you retain all formatting like bold.

Just thought that would help

Kind regards

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Copy text between fields preserving formatting

Post by Traxgeek » Sun Dec 15, 2013 4:29 pm

Hi Bernd,

Just brilliant ! Couldn't get my head around what was happening but with your explanation and quite a bit of thought, reading and re-reading... I get it !

Fantastic - just fantastic !

Thanks a million Bernd - much appreciated.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Copy text between fields preserving formatting

Post by bn » Sun Dec 15, 2013 6:39 pm

Traxgeek,

thank you.

here is a little stack that hopefully shows what we are talking about:
How styled text is copied between fields.
I like sample stacks because often they make it easier to understand to see the concept in action.
click the buttons, see the scripts.

Kind regards
Bernd
Attachments
copyStyle.livecode.zip
(1.79 KiB) Downloaded 368 times

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Copy text between fields preserving formatting

Post by LC4iOS » Tue Jan 07, 2014 5:48 am

Is there a way we can do this with a DataGrid Cell?
Thanks to RunRev.
Thanks to LiveCode forum members.

LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Copy text between fields preserving formatting

Post by bn » Tue Jan 07, 2014 11:20 am

Hi LC4iOS,
Is there a way we can do this with a DataGrid Cell?
I would not try this. The datagrid has its own formatting that you control via datagrid.

I don't know if it is possible at all but DataGridHelper lets you format a lot and consistently.

Kind regards
Bernd

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Copy text between fields preserving formatting

Post by Klaus » Tue Jan 07, 2014 2:19 pm

This MIGHT work, if you ever find out how to address single "cells" in the
nested, nested, nested groups of a datagrid! :D

Did I mention "nested"? 8)

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Copy text between fields preserving formatting

Post by LC4iOS » Tue Jan 07, 2014 10:01 pm

Ok ta.
Sorted using custom column scripting.
Forgot every thing is easy in livecode if I just follow the samples and previous dg threads.
And doing it as shown instead of trying to do it my way.
Thanks to RunRev.
Thanks to LiveCode forum members.

LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member

starfirescully
Posts: 23
Joined: Wed Jul 09, 2014 2:35 pm

Re: Copy text between fields preserving formatting

Post by starfirescully » Fri Jun 10, 2016 6:29 pm

I am having a similar problem. This code works to completely copy from one fld to the other.

Code: Select all

set the htmlText of field 1 to the htmlText of field 1
But what if I want the text of field 2 to come after other information already in field 1?

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”