TIP: How to add ‘paste’ feature.
Posted: Sun Jul 21, 2019 10:39 pm
You would think something as basic as copy/paste would be easy but native Javascript makes it not so easy. However, most browsers, including chrome, safari and firefox, have both the clipboardData API and onPaste/onCopy/onCut event handlers included. So, for a simple paste function one method is this:
Add this after the other scripts in your standalone html:
This adds a listener ('onPaste') to the standalone canvas and passes this event (as 'event') into a function which collects the 'Text' data (which you could swap out for 'text/html' if that is better for your needs). This 'clipboardText' is then sent back to LiveCode (in this case a stack called 'myStandalone') via your handler ('js2LC_paste').
In your LC stack you would have this as your handler:
Initially, because the standalone cannot yet in v9.5.0 handle modifier keys directly as commandKeyDown, etc, a 'v' will appear in your field but this will be quickly replaced by your clipboard text and then place the cursor at the end of your pasted text.
I'll put the 'Copy' and 'Cut' scripts here too shortly.
Pi
Add this after the other scripts in your standalone html:
Code: Select all
<script type="text/javascript">
var canvas = document.getElementById('canvas');
canvas.onpaste = function(event){
var clipboardText = event.clipboardData.getData('Text');
var myStack = document.liveCode.findStackWithName('myStandalone');
myStack.js2LC_paste(clipboardText);
};
</script>
In your LC stack you would have this as your handler:
Code: Select all
on js2lc_Paste pData
put the selectedField into tFld // returns 'field n' where n is the field number on this card.
if tFld is not empty then
put focusedObject() into tFldID // returns the long object ID of the field.
put the selectedchunk into tChunk // returns 'char n1 to n2 of field n3' where n1=startChar, n2=endChar, n3=field number on card
put (word -4 of tChunk + len(pData)) into tLoc
put the text of tFldID into tText
put pData into char (word -4 of tChunk) of tText // replaces the last char placed ('v') with paste text.
set the text of tFldID to tText
select char tLoc to tLoc -1 of tFldID
end if
end js2lc_Paste
I'll put the 'Copy' and 'Cut' scripts here too shortly.
Pi