【问题标题】:Get html-like string representation of element in ckeditor 5在ckeditor 5中获取元素的类似html的字符串表示
【发布时间】:2019-09-25 18:33:38
【问题描述】:

在 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); 
})

我的编辑器只允许一个段落。

【问题讨论】:

    标签: javascript ckeditor ckeditor5 ckeditor5-react


    【解决方案1】:

    原来数据控制器有stringify 接受模型元素并返回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
      }]);
    })
    

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 2019-01-28
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多