gpt4 book ai didi

ckeditor - 以编程方式在 CKEditor 5 中插入 block 引号和新行

转载 作者:行者123 更新时间:2023-12-01 04:36:21 30 4
gpt4 key购买 nike

我已经成功地为 ckeditor 5 创建了一个插件,它允许用户从一个页面中选择一个或多个以前的帖子,并在单击“应用引号”按钮后,它将选定的帖子一个接一个地插入到编辑器 View 中 block 引用。

这工作正常,但是,我想让光标在最后一个 block 引用之后的新行上,以便用户可以添加他们自己的评论。

screenshot

我曾尝试在引用列表中添加一个段落标记,但这在最后一个引用中显示为一个新段落,而不是在它之后。

有人对此有解决方案吗?

最佳答案

我写了一个简单的函数,它向编辑器添加了引用的文本和用户回复的段落。

/**
* @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/

关于ckeditor - 以编程方式在 CKEditor 5 中插入 block 引号和新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51423801/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com