Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support quill.format("mention") better for manual mentioning #152

Open
Plonq opened this issue Jun 21, 2020 · 1 comment
Open

Support quill.format("mention") better for manual mentioning #152

Plonq opened this issue Jun 21, 2020 · 1 comment

Comments

@Plonq
Copy link

Plonq commented Jun 21, 2020

There are two problems when using quill.format("mention", ...):

  1. It doesn't replace the selected text, but instead inserts the mention after the selection
  2. I get the console error TypeError: wrapper.appendChild is not a function

Other than that, it actually seems to work fine. The first problem can be be worked around by manually deleting text and changing cursor position, and I'm not sure what is breaking due to the error (other than the function not returning anything). But it seems this use case wasn't considered.

Tested with:

quill.format("mention", {
  index: 0,
  id: 1,
  value: "George",
  denotationChar: "@",
});

Versions (using in Angular 9)

"ngx-quill": "^11.0.0",
"quill": "^1.3.7",
"quill-mention": "^2.2.5",
@Plonq
Copy link
Author

Plonq commented Jun 21, 2020

In case it helps, this is what I've had to do to workaround the issues:

  newCharacterFromSelection() {
    const selection = this.editor.quillEditor.getSelection();
    const selectedText = this.editor.quillEditor.getText(
      selection.index,
      selection.length,
    );
    // This could be any function that returns an observable
    this.charService.newCharacter(selectedText).subscribe((character) => {
      //  Must focus manually, probably due to focus changing due to modal
      this.editor.quillEditor.focus();
      // Delete selection, because format("mention") doesn't
      this.editor.quillEditor.deleteText(selection.index, selection.length);
      this._insertMention({
        type: "character",
        id: character.id,
        value: character.name,
        denotationChar: DENOTATIONS.CHARACTER,
      });
      // Set cursor to after the mention (should be index + 1, but only + 2 worked)
      this.editor.quillEditor.setSelection(selection.index + 2, 0);
    });
  }

  _insertMention(data: {
    type: string;
    id: number;
    value: string;
    denotationChar: string;
  }) {
    try {
      this.editor.quillEditor.format("mention", {
        index: 0,
        ...data,
      });
    } catch (e) {
      // Have to wrap in try/catch otherwise error can prevent focus/selection/delete working properly
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant