// Copyright 2023 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only const QUILL_EMBED_GUARD = '\uFEFF'; export function createEventHandler({ deleteSelection, }: { deleteSelection: boolean; }) { return (event: ClipboardEvent): void => { // If we're attempting to cut, and focus is not in one of our composer elements, we // let the browser do its default behavior. We don't need formatting from them. if (deleteSelection) { const { activeElement } = document; if ( !activeElement || activeElement.matches('input, textarea') || !activeElement.closest('[contenteditable=plaintext-only]') ) { return; } } if (!event.clipboardData) { return; } const selection = window.getSelection(); if (!selection) { return; } // Create synthetic html with the full selection we can put into clipboard const container = document.createElement('div'); for (let i = 0, max = selection.rangeCount; i < max; i += 1) { const range = selection.getRangeAt(i); container.appendChild(range.cloneContents()); } // We fail over to selection.toString() because we can't pull values from the DOM if // the selection is within an or