How to copy text with line breaks in the clipboard?

54 views
Skip to first unread message

Nicolas Micoud

unread,
Jun 6, 2024, 7:53:31 AMJun 6
to iDempiere
Hi,

https://idempiere.atlassian.net/browse/IDEMPIERE-5218 allows to copy some contents in clipboard.

Now, I would like to add an item to the right click > popup menu of a Description field to copy the content to the clipboard.
I've duplicated the code and it's working well when there is no line break.

String textToCopy = gridTab.get_ValueAsString("Description");
Clients.evalJavaScript(new StringBuffer("navigator.clipboard.writeText(\"").append(textToCopy).append("\");").toString());

But it throws this error when the content of the field has line breaks:

13:51:50.454===========> DefaultScriptErrorListener.onEvent: Clients.evalJavascript error! message: SyntaxError: "" string literal contains an unescaped line break, stack: b@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:9:838
globalEval@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:9:2864
script@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:13864
g@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:974
_doCmds/<@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:5590
_doCmds@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:6189
afterResponse@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:12016
_onResponseReady@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:9931
v/</<@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:29:1924
 [623]


and the content is not copied.

Any idea how to handle that?

Thanks,

Nicolas

Heng Sin Low

unread,
Jun 6, 2024, 8:12:42 AMJun 6
to idem...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/idempiere/1a73cd01-477b-4c73-b846-b524663db5bfn%40googlegroups.com.

Carlos Antonio Ruiz Gomez

unread,
Jun 6, 2024, 8:17:31 AMJun 6
to idem...@googlegroups.com
Found some tip in stackoverflow

Try replacing the double quote with backtick, something like

Clients.evalJavaScript(new StringBuffer("navigator.clipboard.writeText(`).append(textToCopy).append(`);").toString());

I don't know if it works, just following the tip

Regards,

Carlos Ruiz


Am 06.06.24 um 13:53 schrieb Nicolas Micoud:
Nicolas --

Nicolas Micoud

unread,
Jun 6, 2024, 8:17:53 AMJun 6
to iDempiere
Thanks, I found several other similar solutions, but I can't find a way to implement them properly.
From what I understand, that must be done directly in the javascript, but I can't find the correct syntax :(

Nicolas Micoud

unread,
Jun 6, 2024, 8:21:47 AMJun 6
to iDempiere
Hi Carlos,

What is copied with your suggestion is
).append(textToCopy).append(

Thanks anyway

hengsin

unread,
Jun 6, 2024, 8:32:12 AMJun 6
to iDempiere
textToCopy = textToCopy.replace("\\n", "\\\\n");

Nicolas Micoud

unread,
Jun 6, 2024, 8:51:38 AMJun 6
to iDempiere
It still throws the error :

14:50:19.706===========> DefaultScriptErrorListener.onEvent: Clients.evalJavascript error! message: SyntaxError: "" string literal contains an unescaped line break, stack: b@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:9:838

[625]


Code is

else if (evt.getContextEvent().equals(COPY)) {

String textToCopy = gridTab.get_ValueAsString("Description");

textToCopy = textToCopy.replace("\\n", "\\\\n");

Clients.evalJavaScript(new StringBuffer("navigator.clipboard.writeText(\"").append(textToCopy).append("\");").toString());

}


:-/

hengsin

unread,
Jun 6, 2024, 9:19:24 AMJun 6
to iDempiere
Perhaps your string has "\r" too, try
textToCopy = textToCopy.replace("\\n", "\\\\n");
textToCopy = textToCopy.replace("\\r", "\\\\r");

Nicolas Micoud

unread,
Jun 6, 2024, 10:17:52 AMJun 6
to iDempiere
Still not :(

Here's the field on iDempiere (field with a 2 lines content):

2024-06-06 16_16_17-Expert Light local — Mozilla Firefox.png

click on Copy trigger this code

String textToCopy = gridTab.get_ValueAsString("Description");

System.out.println("Original:\n" + textToCopy);

textToCopy = textToCopy.replace("\\n", "\\\\n");

textToCopy = textToCopy.replace("\\r", "\\\\r");

System.out.println("Final:\n" + textToCopy);

Clients.evalJavaScript(new StringBuffer("navigator.clipboard.writeText(\"").append(textToCopy).append("\");").toString());


And log:

Original:

Comunicazione creata per i documenti iniziali

bla bla bla

Final:

Comunicazione creata per i documenti iniziali

bla bla bla

16:15:22.511===========> DefaultScriptErrorListener.onEvent: Clients.evalJavascript error! message: SyntaxError: "" string literal contains an unescaped line break, stack: b@https://localhost:8443/webui/zkau/web/b8d3d1b8/js/zk.wpd:9:838

[679]




Am I missing something?

hengsin

unread,
Jun 6, 2024, 10:30:57 AMJun 6
to iDempiere
My mistake, should be the following instead:

textToCopy = textToCopy.replace("\n", "\\\\n");
textToCopy = textToCopy.replace("\r", "\\\\r");

Nicolas Micoud

unread,
Jun 6, 2024, 10:40:46 AMJun 6
to iDempiere
Almost there :)

There is error message anymore, but the 'output' is :

Comunicazione creata per i documenti iniziali\nbla bla bla

Logs:

Original:

Comunicazione creata per i documenti iniziali

bla bla bla

Final:

Comunicazione creata per i documenti iniziali\\nbla bla bla

hengsin

unread,
Jun 6, 2024, 11:00:52 AMJun 6
to iDempiere
Well, probably just a case of reducing the "\":

textToCopy = textToCopy.replace("\n", "\\n");
textToCopy = textToCopy.replace("\r", "\\r");

Nicolas Micoud

unread,
Jun 6, 2024, 11:12:04 AMJun 6
to iDempiere
Yes !!!!!

Is working now, thanks a lot !!

Final code is

String textToCopy = gridTab.get_ValueAsString("Description");

textToCopy = textToCopy.replace("\n", "\\n");

textToCopy = textToCopy.replace("\r", "\\r");

Clients.evalJavaScript(new StringBuffer("navigator.clipboard.writeText(\"").append(textToCopy).append("\");").toString());


Regards,

Nicolas

Carlos Antonio Ruiz Gómez

unread,
Jun 6, 2024, 12:59:18 PMJun 6
to iDempiere
:-)  My fault, as said, just writing untested code, forgot to close the quotes, it must be something like this:

Clients.evalJavaScript(new StringBuffer("navigator.clipboard.writeText(`").append(textToCopy).append("`);").toString());


Nicolas Micoud

unread,
Jun 7, 2024, 1:13:30 AMJun 7
to iDempiere
Hi Carlos,
Yes, is working
Thanks !

ps: now I have to chose between those 2 solutions :D

Heng Sin Low

unread,
Jun 7, 2024, 1:15:06 AMJun 7
to idem...@googlegroups.com
The text block approach suggested by Carlos should be the cleaner and better approach.

--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.

Nicolas Micoud

unread,
Jun 7, 2024, 1:19:00 AMJun 7
to iDempiere
Ok, I'll do it this way
Thanks both for you guidance
Reply all
Reply to author
Forward
0 new messages