Получить html-подобное строковое представление элемента в ckeditor 5 - PullRequest
0 голосов
/ 25 сентября 2019

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

Мой редактор допускает только один абзац.

1 Ответ

0 голосов
/ 26 сентября 2019

Оказывается, у контроллера данных есть метод stringify, который принимает элемент модели и возвращает строку html.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
  }]);
})
...