To start, I expect you want:
editor.cursor
Defined here to get there current cursor. Cursor is a class defined here.
Cursor is a proxy that can return the data about the current cursor. You’ll want to call a method on the cursor to find out what the current state of the DOM is at the time when you want that information to be captured (don’t cache the cursor object itself and try to access the state from when you cached it).
editor.cursor.offsets
Offsets will return a Range object which represents a head-tail on the post AST. head and tail are Position objects.
So, to get the current marker under the cursor:
let { cursor } = editor;
if (!cursor.hasCursor()) {
return; /* cursor isn’t in the editor */
}
if (cursor.hasSelection()) {
return; /* cursor is a selection, and Dan you said you only wanted non-selections */
}
cursor.head; /* Your cursor is at this position. */
Several APIs on the post editor accept positions as arguments, for example insertMarkers and insertTextWithMarkup. Use the builder to create a markup for a
with an href
attribute and insert text at the position as it appropriate.
A lot of links there, but to sum up:
let { cursor } = editor;
if (cursor.hasCursor() && !cursor.hasSelection()) {
let {head: position} = cursor;
editor.run(postEditor => {
let linkMarkup = postEditor.builder.createMarkup('a', {href: ‘http://someurl.com'});
postEditor.insertTextWithMarkups(position, ‘Your link!’, [linkMarkup]);
});
}
And that should do it. I expect you should read off the editor.head.position
eagerly, since if there is another place that the cursor might go (like if you popup a modal with a form) you want to already have that data stashed somewhere you can use it.