亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取ckeditor 5中選定文本的HTML代碼?

如何獲取ckeditor 5中選定文本的HTML代碼?

皈依舞 2023-07-20 15:34:08
如何在 ckeditor 5 中獲取所選文本的 HTML?在 ckeditor 4 中有 getHTML() 方法。第五版中的模擬是什么?
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

您可以使用DataController#stringify使用getSelectedContent將文檔片段中的 HTML 字符串化

import getSelectedContent from "@ckeditor/ckeditor5-engine/src/model/utils/getselectedcontent";


export default class CK5Utils{

? ? ? ? static getSelectedHtml(ed) {

? ? ? ? ? ? const {model, data, model:{document:{selection}}} = ed;

? ? ? ? ? ? const range = selection.getFirstRange();

? ? ? ? ? ? if (range.end.isEqual(range.start)) {

? ? ? ? ? ? ? return '';

? ? ? ? ? ? }

? ? ? ? ? ? const selected = getSelectedContent(model, selection);

? ? ? ? ? ? return data.stringify(selected);

? ? ? ? }

}


查看完整回答
反對 回復 2023-07-20
?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

按照 Alexandros Kyriakos 的建議,使用DataController#stringify和getSelectedContent,也可以這樣做:

let?sHtmlSelection?=?this.editor.data.stringify(this.editor.model.getSelectedContent(this.editor.model.document.selection));



查看完整回答
反對 回復 2023-07-20
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您可以抓取某個范圍內的選定文本,但要獲取 HTML,就有點棘手了。

我能夠抓住 thenameparent通過switch語句運行該值。

注意:下面的代碼已從此處改編:“在 CKEDITOR 5 中獲取突出顯示/選定的文本”

const resultArea = document.querySelector('.selected');


let globalEditor;


ClassicEditor

? .create(document.querySelector('#editor'))

? .then(editor => globalEditor = editor)

? .catch(error => console.error(error));


const formatNode = (node) => {

? switch (node.parent.name) {

? ? case 'heading1'? : return `<h1>${node.data}</h1>`;

? ? case 'heading2'? : return `<h2>${node.data}</h2>`;

? ? case 'heading3'? : return `<h3>${node.data}</h3>`;

? ? case 'paragraph' : return `<p>${node.data}</p>`;

? ? default? ? ? ? ? : return '';

? }

};


const getSelectedHTML = (editor) =>

? [...editor.model.document.selection.getFirstRange().getItems()]

? ? .filter(node => node.data)

? ? .map(formatNode)

? ? .join('\n');


document.querySelector('.btn').addEventListener('click', e => {

? resultArea.value = getSelectedHTML(globalEditor);

});

<!-- https://cdn.ckeditor.com/#ckeditor5 -->

<script src="https://cdn.ckeditor.com/ckeditor5/23.1.0/classic/ckeditor.js"></script>

<div id="editor">

? <h1>This is a heading</h1>

? <p>This is some sample content.</p>

</div>

<p>

? <button class="btn">Get Selected HTML</button>

</p>

<textarea class="selected" rows="8" cols="72"></textarea>


查看完整回答
反對 回復 2023-07-20
  • 3 回答
  • 0 關注
  • 384 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號