benjismith
benjismith

Reputation: 16735

Transient formatting in a Quill document?

I'd like to implement transient highlighting in a Quill document.

For example, imagine a SEARCH button where the user can highlight all instances of a keyword in the current document, by setting the text color for matching text ranges.

I can do that today, with something like this:

var keyword = "hello";
var text = quill.getText();

var matchIndex = text.indexOf(keyword);
while (matchIndex >= 0) {
  quill.formatText(matchIndex, keyword.length, { "color" : "#f00" });
  matchIndex = text.indexOf(keyword, matchIndex + keyword.length);
}

But I don't want the resultant deltas to be incorporated into the official change history of this document. These are just transient highlights, and I'd like to be able to clear them all away with something like this...

quill.clearTransientFormats();

I'd even like to give the user the choice of leaving the transient highlights enabled, while they continue to edit and modify the document with their own destructive changes.

Essentially, I want to have two different kinds of formatting:

What's the best way to implement something like this?

Upvotes: 0

Views: 202

Answers (1)

jhchen
jhchen

Reputation: 14767

I would recommend just post-processing the delta before saving. It can be achieved fairly easily with compose:

var length = aboutToBeStored.length();
var toStore = aboutToBeStored.compose(new Delta().retain(0, length, { color: null }));

Upvotes: 1

Related Questions