How to get selected text

In previous version following code was doing what expected. Anyhow since pm.doc.sliceBetween doesn’t seem to exist, what is official way to grab selected text to i.e. wrap it?

Twitterable.register('command', 'insert', {
    label: 'Wrap the selection in a block quote',
    run(pm) {
        let content = pm.doc.sliceBetween(pm.selection.from, pm.selection.to),
            node = this.create(null, content.content);
        return pm.tr.replaceSelection(node).apply();
    },
    menuGroup: 'block(46)',
})

You can call pm.doc.cut(pm.selection.from, pm.selection.to) to get a fragment of the document.

(Note that your approach to wrapping something in a blockquote has the disadvantage of completely replacing the whole range, which would work poorly when doing collaborative editing, since it’ll nullify all concurrent changes in that range. Some combination of Transform.split and Transform.wrap would prevent this.)

2 Likes

Thanks. That should be enough. We don’t pick collab editing yet.