簡體   English   中英

獲取 ckeditor 5 中元素的類似 html 的字符串表示

[英]Get html-like string representation of element in ckeditor 5

在 Ckeditor 5 中,我無法找到從元素中提取字符串的方法。 當用戶在段落中間點擊回車時,我想添加一個功能:

  • 拆分段落
  • 獲取最后一段的內容
  • 從編輯器中刪除最后一段
editor.model.change( writer => {
  const paragraph = editor.model.document.selection.getFirstPosition();
  writer.split(paragraph);

  const model = editor.model;
  const doc = model.document;
  const root = doc.getRoot();
  // HTML-like string representation of lastParagraph below?
  const lastParagraph = root.getChild(root.childCount - 1); 
})

我的編輯只允許一個段落。

事實證明,數據 controller 具有stringify方法,該方法接受 model 元素並返回 html-string。 https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_controller_datacontroller-DataController.html#function-stringify

editor.model.change( writer => {
  const paragraph = editor.model.document.selection.getFirstPosition();
  writer.split(paragraph);

  const model = editor.model;
  const root = model.document.getRoot();
  const lastParagraph = root.getChild(root.childCount - 1);
  const splittedMarkup = `<p>${editor.data.stringify(lastParagraph)}</p>`;

  writer.remove(lastParagraph);
  // dispatch an action 
  addMoreMarkup([position, {
    markup: splittedMarkup
  }]);
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM