Here's an interesting problem. I want the user to be able to select multiple elements and choose a new tag for them by clicking a button. My current code looks like this:
sel = editor.getSelection()
ranges = sel.getRanges()
# Iterate through the selection ranges
for range in ranges
iterator = range.createIterator()
paragraphs = []
# Iterate through the blocks in the range
while (block = iterator.getNextParagraph())
# Undo the style if it's already applied
if (block.getName() == tagName)
tagName = 'p'
paragraphs.push( block )
if paragraphs.length
# Create the new element
bqBlock = editor.document.createElement(tagName)
bqBlock.insertBefore( paragraphs[0] )
# Move each child in the selection into the new element
while ( paragraphs.length > 0 )
block = paragraphs.shift()
block.moveChildren(bqBlock)
block.remove()
This works great, except the selection goes erased, which is horribly user-unfriendly. I'd like to maintain the original selection.
I'm using ckeditor if there's a built-in way to do this.
Edit:
Reinmar found the answer for me: Set cursor to specific position in CKEditor