Serialize inline content node to a clipboard

I’m working on a search interface: you can enter a facet, and it turns into an atom node when you select it from suggestions. Something like this:

image

This AND(...) node is what I’m working on right now. It’s inline node with content, and I want AND(...) to be copied when I’m copying text, so that I can then parse it back. Right now I’m rendering it like that:

    toDOM: node => ['span', {'data-func': node.attrs.name}, 'AND(', ['span', 0], ')'],

but still there is nothing in clipboard. I’ve tried looking into clipboardSerializer – it returns DOM nodes, and, it seems, leafText is called afterwards. I see in Fragment.textBetween that text is taken only from text or leaf nodes, so… not sure how to approach this at all.

Thanks in advance for any ideas!

You mean when copying plain text or when copying HTML? If the former, you’ll need to figure out some textual syntax for these and write clipboardTextParser and clipboardTextSerializer functions to wire it into the editor.

Ah, okay, I don’t know why I did not go that way immediately – I already had clipboardTextParser in a plugin… Still learning the API, I guess. I ended up with function like this:

export function node2human(node) {
  if (node.isText)
    return node.text;

  if (node.isLeaf) {
    return node.type.spec.leafText(node);
  }

  let joiner = node.content.content[0]?.isBlock ? '\n\n' : '';
  let text = node.content.content.map(c => node2human(c)).join(joiner);
  if (!node.type) // just a slice
    return text;
  if (node.type.spec.blockText)
    return node.type.spec.blockText(node, text);
  return text;
}

which is then called in clipboardTextSerializer(slice, view) { return node2human(slice) }. Thanks!