更改选择标记

时间:2013-06-19 21:38:59

标签: javascript dom coffeescript ckeditor

这是一个有趣的问题。我希望用户能够选择多个元素,并通过单击按钮为它们选择一个新标签。我目前的代码如下:

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()

这很有效,除了选择被删除,这是非常用户不友好的。我想保留最初的选择。

如果有内置的方法,我正在使用ckeditor。

编辑:

Reinmar找到了答案: Set cursor to specific position in CKEditor

0 个答案:

没有答案