Skip to content

Commit

Permalink
Composition: Only try to convert html from text/signal
Browse files Browse the repository at this point in the history
  • Loading branch information
sidke authored and josh-signal committed Nov 23, 2020
1 parent f5574cb commit d76e6d3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ts/quill/signal-clipboard/index.ts
Expand Up @@ -22,6 +22,19 @@ const getSelectionHTML = () => {
return div.innerHTML;
};

const replaceAngleBrackets = (text: string) => {
const entities: Array<[RegExp, string]> = [
[/&/g, '&amp;'],
[/</g, '&lt;'],
[/>/g, '&gt;'],
];

return entities.reduce(
(acc, [re, replaceValue]) => acc.replace(re, replaceValue),
text
);
};

export class SignalClipboard {
quill: Quill;

Expand Down Expand Up @@ -86,6 +99,10 @@ export class SignalClipboard {
const text = event.clipboardData.getData('text/plain');
const html = event.clipboardData.getData('text/signal');

const clipboardDelta = html
? clipboard.convert(html)
: clipboard.convert(replaceAngleBrackets(text));

const { scrollTop } = this.quill.scrollingContainer;

this.quill.selection.update('silent');
Expand All @@ -94,7 +111,7 @@ export class SignalClipboard {
setTimeout(() => {
const delta = new Delta()
.retain(selection.index)
.concat(clipboard.convert(html || text));
.concat(clipboardDelta);
this.quill.updateContents(delta, 'user');
this.quill.setSelection(delta.length(), 0, 'silent');
this.quill.scrollingContainer.scrollTop = scrollTop;
Expand Down

0 comments on commit d76e6d3

Please sign in to comment.