我已经成功地为 ckeditor 5 创建了一个插件,它允许用户从一个页面中选择一个或多个以前的帖子,然后单击“应用引号”按钮,它将选定的帖子作为块引用一个接一个地插入到编辑器视图中。
这很好用,但是,我想让光标在最后一个块引用之后的新行上,以便用户可以添加自己的评论。
我尝试在引用列表中附加一个段落标签,但这在最后一个引用中显示为一个新段落,而不是在它之后。
有人对此有解决方案吗?
我写了一个简单的函数,添加到编辑器引用的文本和用户回复的段落。
/**
* @param {String} author An author of the message.
* @param {String} message A message that will be quoted.
*/
function replyTo( author, message ) {
// window.editor must be an instance of the editor.
editor.model.change( writer => {
const root = editor.model.document.getRoot();
const blockQuote = writer.createElement( 'blockQuote' );
const authorParagraph = writer.createElement( 'paragraph' );
const messageParagraph = writer.createElement( 'paragraph' );
// Inserts: "Author wrote:" as bold and italic text.
writer.insertText( `${ author } wrote:`, { bold: true, italic: true }, authorParagraph );
// Inserts the message.
writer.insertText( message, messageParagraph );
// Appends "Author wrote" and the message to block quote.
writer.append( authorParagraph, blockQuote );
writer.append( messageParagraph, blockQuote );
// A paragraph that allows typing below the block quote.
const replyParagraph = writer.createElement( 'paragraph' );
// Appends the block quote to editor.
writer.append( blockQuote, root );
// Appends the reply paragraph below the block quote.
writer.append( replyParagraph, root );
// Removes the initial paragraph from the editor.
writer.remove( root.getChild( 0 ) );
// And place selection inside the paragraph.
writer.setSelection( replyParagraph, 'in' );
} );
editor.editing.view.focus();
}
如果您对如何将其调整为您的代码有一些疑问,我想看看您编写的代码。它会让你理解你做了什么。
当然,您可以将建议的代码视为在线演示 - https://jsfiddle.net/pomek/s2yjug64/