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

Insert HTML #337

Open
BaptisteVasseur opened this issue Nov 24, 2020 · 5 comments
Open

Insert HTML #337

BaptisteVasseur opened this issue Nov 24, 2020 · 5 comments
Labels

Comments

@BaptisteVasseur
Copy link

Hi !

I switched from jquery-textcomplete to the new version but I can no longer paste html content in my contenteditable div, it is automatically escaped.

How can I solve my problem ?

I guess the problem is in the ContenteditableEditor file (applySearchResult method) with insertText

this.el.ownerDocument.execCommand(
    "insertText",
     false,
     replace[0] + replace[1]
)
@yuku yuku added the question label Nov 25, 2020
@ShaMan123
Copy link

+1

@ShaMan123
Copy link

#217

@ShaMan123
Copy link

ShaMan123 commented May 7, 2021

I found a solution.
This patches SearchResult#replace ContenteditableEditor#applySearchResult in order for the methods to handle html.

import { ContenteditableEditor } from "@textcomplete/contenteditable";
import {
  SearchResult,
  StrategyProps,
  Textcomplete,
  TextcompleteOption
} from "@textcomplete/core";

SearchResult.prototype.replace = function (
  this: SearchResult,
  beforeCursor: string,
  afterCursor: string
): [DocumentFragment, DocumentFragment] | void {
  let result = this.strategy.replace(this.data);
  if (result == null) return;
  if (Array.isArray(result)) {
    afterCursor = result[1] + afterCursor;
    result = result[0];
  }
  const match = this.strategy.match(beforeCursor);
  if (match == null || match.index == null) return;
  const fragment = document.createDocumentFragment();
  fragment.append(
    beforeCursor.slice(0, match.index),
    result,
    beforeCursor.slice(match.index + match[0].length)
  );
  const afterFragment = document.createDocumentFragment();
  afterFragment.append(afterCursor);
  return [fragment, afterFragment];
};

ContenteditableEditor.prototype.applySearchResult = function (
  this: ContenteditableEditor,
  searchResult
) {
  const before = this.getBeforeCursor();
  const after = this.getAfterCursor();
  if (before != null && after != null) {
    const replace = searchResult.replace(before, after) as [
      DocumentFragment,
      DocumentFragment
    ];
    if (Array.isArray(replace)) {
      const range = this.getRange() as Range;
      range.selectNode(range.startContainer);
      range.deleteContents();
      const last = replace[0].lastChild;
      range.insertNode(replace[1]);
      range.insertNode(replace[0]);
      range.detach();
      const newRange = this.getRange();
      newRange.setStartAfter(last);
      newRange.collapse(true);
    }
  }
};

@ShaMan123
Copy link

ShaMan123 commented May 7, 2021

I have noticed that for string values sometimes the dropdown doesn't show.
This is caused by a wrong value of beforeCursor.

@ShaMan123
Copy link

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

No branches or pull requests

3 participants