2020-11-06 20:11:18 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type Quill from 'quill';
|
2020-11-12 01:01:45 +00:00
|
|
|
import Delta from 'quill-delta';
|
|
|
|
|
2023-08-04 16:29:47 +00:00
|
|
|
const prepareText = (text: string) => {
|
2020-11-19 17:38:25 +00:00
|
|
|
const entities: Array<[RegExp, string]> = [
|
|
|
|
[/&/g, '&'],
|
|
|
|
[/</g, '<'],
|
|
|
|
[/>/g, '>'],
|
|
|
|
];
|
|
|
|
|
2023-08-04 16:29:47 +00:00
|
|
|
const escapedEntities = entities.reduce(
|
2020-11-19 17:38:25 +00:00
|
|
|
(acc, [re, replaceValue]) => acc.replace(re, replaceValue),
|
|
|
|
text
|
|
|
|
);
|
2023-08-04 16:29:47 +00:00
|
|
|
|
|
|
|
return `<span>${escapedEntities}</span>`;
|
2020-11-19 17:38:25 +00:00
|
|
|
};
|
|
|
|
|
2020-11-06 20:11:18 +00:00
|
|
|
export class SignalClipboard {
|
|
|
|
quill: Quill;
|
|
|
|
|
|
|
|
constructor(quill: Quill) {
|
|
|
|
this.quill = quill;
|
|
|
|
|
2020-11-12 01:01:45 +00:00
|
|
|
this.quill.root.addEventListener('paste', e => this.onCapturePaste(e));
|
2020-11-06 20:11:18 +00:00
|
|
|
}
|
2020-11-12 01:01:45 +00:00
|
|
|
|
|
|
|
onCapturePaste(event: ClipboardEvent): void {
|
2022-09-14 21:40:44 +00:00
|
|
|
if (event.clipboardData == null) {
|
2020-11-12 01:01:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clipboard = this.quill.getModule('clipboard');
|
|
|
|
const selection = this.quill.getSelection();
|
|
|
|
|
2022-09-14 21:40:44 +00:00
|
|
|
if (selection == null) {
|
2020-11-12 01:01:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const text = event.clipboardData.getData('text/plain');
|
2023-05-23 22:00:05 +00:00
|
|
|
const signal = event.clipboardData.getData('text/signal');
|
2020-11-12 01:01:45 +00:00
|
|
|
|
2023-05-23 22:00:05 +00:00
|
|
|
if (!text && !signal) {
|
2023-05-18 19:53:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-26 22:07:32 +00:00
|
|
|
const clipboardContainsFiles = event.clipboardData.files?.length > 0;
|
|
|
|
if (!clipboardContainsFiles) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2023-05-18 19:53:36 +00:00
|
|
|
|
2023-05-23 22:00:05 +00:00
|
|
|
const clipboardDelta = signal
|
|
|
|
? clipboard.convert(signal)
|
2023-08-04 16:29:47 +00:00
|
|
|
: clipboard.convert(prepareText(text));
|
2020-11-19 17:38:25 +00:00
|
|
|
|
2020-11-12 01:01:45 +00:00
|
|
|
const { scrollTop } = this.quill.scrollingContainer;
|
|
|
|
|
|
|
|
this.quill.selection.update('silent');
|
|
|
|
|
|
|
|
if (selection) {
|
|
|
|
setTimeout(() => {
|
|
|
|
const delta = new Delta()
|
|
|
|
.retain(selection.index)
|
2023-05-17 16:58:32 +00:00
|
|
|
.delete(selection.length)
|
2020-11-19 17:38:25 +00:00
|
|
|
.concat(clipboardDelta);
|
2020-11-12 01:01:45 +00:00
|
|
|
this.quill.updateContents(delta, 'user');
|
2023-05-22 20:31:36 +00:00
|
|
|
this.quill.setSelection(delta.length() - selection.length, 0, 'silent');
|
2020-11-12 01:01:45 +00:00
|
|
|
this.quill.scrollingContainer.scrollTop = scrollTop;
|
2023-05-22 20:31:36 +00:00
|
|
|
|
|
|
|
this.quill.focus();
|
2020-11-12 01:01:45 +00:00
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 20:11:18 +00:00
|
|
|
}
|