get copied data in code and do paste operation web author

Questions about XML that are not covered by the other forums should go here.
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

get copied data in code and do paste operation web author

Post by shikhar_472 »

Hi Team,

I wanted to get the copied data which holds by copy event CTRL+C and want to paste by button action whatever position I want i web author, could you please help me on this.

Thanks,
Shikhar.
cristi_talau
Posts: 496
Joined: Thu Sep 04, 2014 4:22 pm

Re: get copied data in code and do paste operation web author

Post by cristi_talau »

Hello,

In Web Author users can paste content in two ways:
  • Using the Ctrl+V (or Cmd+V) shortcut.
  • Using the "Paste special" action from the contextual menu which shows a dialog where the users need to paste the content.
In Desktop applications, there is usually a "Paste" button that pastes the content directly. In Web applications the browser API that allows you to read the clipboard (https://developer.mozilla.org/en-US/doc ... pboard_API) is not very well supported across modern web browsers. So, you can implement a "Paste" button in the context menu only for some users (e.g. using Chrome). Is that what you want?

Regarding the location of the paste, do you want to create some actions like "Paste after" or "Paste before"? Or just want to tweak the paste position in some cases where the default paste strategy does not meet user expectations? The second use-case can be solved by using the

Code: Select all

ro.sync.ecss.extensions.api.AuthorSchemaAwareEditingHandler.handlePasteFragment()
API.

Best,
Cristian
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

Re: get copied data in code and do paste operation web author

Post by shikhar_472 »

Hey Cristian,

Thanks for the reply.
I have one more question i have created the new Fragment using seriliazed xml(createNewDocumentFragmentInContext using this method) and now i want access all the nodes of that fragment inside AuthorDocumentFragment there is getContentNodes method is there which is only returning the first level node but i want to access all the nodes which are present inside the fragment created. Could you please suggest the way to achieve that

Thanks,
Shikhar.
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

Re: get copied data in code and do paste operation web author

Post by shikhar_472 »

test
Bogdan Dumitru
Site Admin
Posts: 142
Joined: Tue Mar 20, 2018 5:28 pm

Re: get copied data in code and do paste operation web author

Post by Bogdan Dumitru »

Hello,

To access all the nodes which are present inside the created fragment you have to cast from AuthorNode to AuthorElement [0].
For a safe cast you can first check if the node type is NODE_TYPE_ELEMENT:

Code: Select all

if (node.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
  AuthorElement element = (AuthorElement) node;
  element.getContentNodes();
}
[0] https://www.oxygenxml.com/InstData/Edit ... ement.html
Bogdan Dumitru
http://www.oxygenxml.com
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

Re: get copied data in code and do paste operation web author

Post by shikhar_472 »

how to get nodes from createdFragment because inside fragment getContentNodes method is present which is returning only first node.

AuthorDocumentFragment docFragment = model.getAuthorAccess().getDocumentController()
.createNewDocumentFragmentInContext(cotentToPaste,
model.getAuthorAccess().getEditorAccess().getCaretOffset());


AuthorNode authorNode = (AuthorElement)docFragment.getContentNodes(); //here it can not cast because list is there

ArrayList<AuthorNode> listNodes =(ArrayList<AuthorNode>) docFragment.getContentNodes();
i tried looping listNodes still not able to access all the nodes
My task is from text xml i created a fragment eg. <a><b><c id ="2"> </c></b><a>
now inside this fragment i want to increment the id for that i want to access c node to setAttribute but using getContentNodes inside list i am getting only <a> node.
Bogdan Dumitru
Site Admin
Posts: 142
Joined: Tue Mar 20, 2018 5:28 pm

Re: get copied data in code and do paste operation web author

Post by Bogdan Dumitru »

Hi,

Yes, you have to iterate on all nodes returned by the "getContentNodes()" method and cast them to AuthorElement, then you have to recursively iterate in order to find all nodes, not just the ones from the first level.

The code should look something like this:

Code: Select all

  public void findElementsToUpdate(AuthorDocumentFragment fragment) {
    List<AuthorElement> elementsWithId = new ArrayList<>();

    for (AuthorNode firstLevelNode : fragment.getContentNodes()) {
      if (firstLevelNode.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
        AuthorElement firstLevelElement = (AuthorElement) firstLevelNode;
        elementsWithId.addAll(recursivelyFindNodesWithId(firstLevelElement));
      }
    }

    System.out.println("Elements to updated: " + elementsWithId);
  }
  
  private static List<AuthorElement> recursivelyFindNodesWithId(AuthorElement element) {
    List<AuthorElement> toReturn = new ArrayList<>();
    if (element.getAttribute("id") != null) {
      toReturn.add(element);
    }
    for (AuthorNode childNode : element.getContentNodes()) {
      if (childNode.getType() == AuthorNode.NODE_TYPE_ELEMENT) {
        AuthorElement childElement = (AuthorElement) childNode;
        toReturn.addAll(recursivelyFindNodesWithId(childElement));
      }
    }
    return toReturn;
  }
Bogdan Dumitru
http://www.oxygenxml.com
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

Re: get copied data in code and do paste operation web author

Post by shikhar_472 »

is there any optimize way to create new document fragment from string xml because method which is present inisde document controller is taking more time which is affecting the perofromance. Could you please suggest.
Bogdan Dumitru
Site Admin
Posts: 142
Joined: Tue Mar 20, 2018 5:28 pm

Re: get copied data in code and do paste operation web author

Post by Bogdan Dumitru »

Hello,

The AuthorDocumentController.createNewDocumentFragmentInContext() shouldn't take much time unless you have a DOCTYPE that refers to a remote DTD that leads to a remote connection being made.
Your performance problem might be caused actually to the fact that the fragment is recursively iterated or the "createNewDocumentFragmentInContext()" is called multiple times. It should be faster to insert the fragment and then find the elements with the "id" attribute using AuthorDocumentController.evaluateXPath(). Another solution would be to process the fragment using a RegExp like " id=\"[^"]+\"".
Bogdan Dumitru
http://www.oxygenxml.com
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

Re: get copied data in code and do paste operation web author

Post by shikhar_472 »

Hi Dumitru,

Instant start = Instant.now();
AuthorDocumentFragment docFragment = docControl.createNewDocumentFragmentInContext(cotentToPaste,
authorAccess.getEditorAccess().getCaretOffset(););

Instant end = Instant.now();
Duration timeElapsed1 = Duration.between(start, end);
logger.info("after created doc fragment " + timeElapsed1");

This single line itself is taking more than 2 seconds and recursive iteration is taking only 0.026 Second could you please help me on this.
Bogdan Dumitru
Site Admin
Posts: 142
Joined: Tue Mar 20, 2018 5:28 pm

Re: get copied data in code and do paste operation web author

Post by Bogdan Dumitru »

Hello,

Curious that the "createNewDocumentFragmentInContext(cotentToPaste, [...])" call takes that long.

If all you want is id attribute auto-generation, see the "Configuring the Automatic ID Generation and Unique Attributes Recognizer" topic [0], it might be exactly what you need.

If that topic isn't exactly what you need, please consider offering more details about your use case so that we can guide you to a more efficient solution that may not require manually creating an AuthorDocumentFragment.

If you want to troubleshoot why it takes soo long to create the fragment, please do and answer the following:
1. check the length of the "cotentToPaste" string. If it's a huge string, it might justify a long time computing it.
2. enable debug logging [1] and check if server makes requests when calling "createNewDocumentFragmentInContext()". This way you'll check that no external DTD is fetched.
3. does it take 2s just the first time when the method is called or always?
4. does the problem reproduces in a fresh Web Author kit from the last released version, v24.1?
5. help us to reproduce the problem. Send us to "support@oxygenxml.com" all the necessary so we can reproduce the problem: the "cotentToPaste" string, your framework, and relevant custom plugins if present.

[0] https://www.oxygenxml.com/doc/versions/ ... nizer.html
[1] https://www.oxygenxml.com/doc/versions/ ... tgc_1wq_2v
Bogdan Dumitru
http://www.oxygenxml.com
shikhar_472
Posts: 99
Joined: Fri Jul 01, 2022 12:08 pm

Re: get copied data in code and do paste operation web author

Post by shikhar_472 »

Hi Team,

For me navigator.clipboard is coming as undefined is there any way to read data from clipboard without using navigator.clipboard
mihai_coanda
Posts: 78
Joined: Wed Jul 20, 2016 8:22 am

Re: get copied data in code and do paste operation web author

Post by mihai_coanda »

Hello,
This API has limitations as described in this StackOverflow post [1] and in the MDN documentation [2].

Best Regards,
Michael

1. https://stackoverflow.com/questions/518 ... -undefined.
2. https://developer.mozilla.org/en-US/doc ... /Clipboard
Michael

https://www.oxygenxml.com
Post Reply