Proper way of creating new doc from selection

Imagine user selected some text in document. I want to be able to create new document from this selection and want this document to be valid against schema. What api should I use, if there is any? Is this task solvable for generic schema at all? Task looks not easy, as user may have cubersome rules in schema. Should I create empty nodes to satisfy schema constraints, or what?

My schema is close to “prosemirror-schema-basic”, plus lists and plus some custom nodes.

I’ve tried state.doc.cut and state.doc.slice, but both have issues and can produce invalid doc. Also I’ve looked into clipboard.ts module of prosemirror-view package, and took some code from there as an inspiration. But still afraid that I’m missing correct approach.

You could take a slice of the selection, and then create an empty document and insert that slice into it, letting Transform.replace handle the fitting and implicit-node-insertion for you.

1 Like