Как добавить нередактируемый тег к содержимому в редакторе Quill

Я хочу добавить пару готовых ярлыков, таких как

<div class="label"> Label Text <span>x</span><div>

к содержимому html в редакторе quill. Добавить такой тег не должно быть проблемой само по себе. Однако я не хочу, чтобы пользователь мог редактировать текст внутри метки. Курсор нельзя даже помещать внутрь метки. При удалении все div должно быть удалено. Вся этикетка должна действовать как изображение в каком-то смысле. Является ли это возможным ?


person pravin    schedule 20.09.2016    source источник
comment
ты решил свою проблему?   -  person antonyboom    schedule 06.02.2018
comment
@antonyboom Нет.. :( Не уверен, что перо устарело или вопрос!   -  person pravin    schedule 06.02.2018
comment
stackoverflow.com/questions/40159839/   -  person 2can    schedule 12.01.2019


Ответы (1)


Вы должны быть в состоянии добиться этого, написав свой собственный Blot:

class Label extends Parchment.Embed {
  static create(value) {
    const node = super.create(value);
    node.innerText = value;
    // Remember to set this so users can't edit
    // the label's content
    node.contentEditable = 'false';
    this._addRemovalButton(node);
    return node;
  }

  static value(node) {
    // Only return the text of the first child
    // node (ie the text node), otherwise the
    // value includes the contents of the button
    return node.childNodes[0].textContent;
  }

  static _addRemovalButton(node) {
    const button = document.createElement('button');
    button.innerText = 'x';
    button.onclick = () => node.remove();
    button.contentEditable = 'false';
    node.appendChild(button);

    // Extra span forces the cursor to the end of
    // the label, otherwise it appears inside the
    // removal button
    const span = document.createElement('span');
    span.innerText = ' ';
    node.appendChild(span);
  }
}
Label.blotName = 'label';
Label.tagName = 'SPAN';
Label.className = 'ql-label';

Вы регистрируете его в Quill:

Quill.register(Label);

И, наконец, вы можете использовать его аналогично image или другим встраиваниям:

quill.updateContents(
  new Delta().insert({ label: 'foo' })
);

NB: любой стиль, который вам нужен, может быть применен с классом .ql-label:

.ql-label {
  background-color: lightgrey;
  padding: 0.3em;
  margin: 0 0.2em;
}

.ql-label button {
  margin-left: 0.3em;
}

Наконец, наконец: рабочий пример.

person Alec    schedule 10.04.2019
comment
Спасибо! У меня есть аналогичный вариант использования, но для этого требуется добавить кляксы для каждого контента. Для этого я создал новый вопрос -- stackoverflow.com/questions/56048010/ - person amitaibu; 08.05.2019
comment
Спасибо за Ваш ответ! но где именно я должен определить метку класса - person Xsmael; 08.04.2021