Override internal editor behavior for clipboard and drag-and-drop operations
The Override system allows you to customize core editor behaviors like paste, copy, and drag-and-drop operations without modifying the editor’s internal code.
The Override system provides low-level control over editor behavior. Use it carefully as incorrect implementations may break core functionality.
editor.override.paste = (evt?: ClipboardEvent) => { if (!evt) return const text = evt.clipboardData?.getData('text/plain') if (text) { // Custom paste logic console.log('Pasting text:', text) // Transform the text before inserting const transformedText = text.toUpperCase() editor.command.executeInsertElementList([{ value: transformedText }]) // Prevent default paste behavior return { preventDefault: true } }}
editor.override.paste = (evt?: ClipboardEvent) => { if (!evt) return const html = evt.clipboardData?.getData('text/html') const text = evt.clipboardData?.getData('text/plain') // Only allow plain text, strip all formatting if (text) { editor.command.executeInsertElementList([{ value: text }]) return { preventDefault: true } } // Block paste if no plain text available return { preventDefault: true }}
editor.override.paste = async (evt?: ClipboardEvent) => { if (!evt) return const text = evt.clipboardData?.getData('text/plain') if (text) { // Process text asynchronously (e.g., sanitize via API) try { const sanitized = await sanitizeContent(text) editor.command.executeInsertElementList([{ value: sanitized }]) } catch (error) { console.error('Failed to sanitize content:', error) } return { preventDefault: true } }}async function sanitizeContent(text: string): Promise<string> { // API call to sanitize content const response = await fetch('/api/sanitize', { method: 'POST', body: JSON.stringify({ text }), headers: { 'Content-Type': 'application/json' } }) const data = await response.json() return data.sanitized}
editor.override.copy = async () => { // Get selected text const rangeText = editor.command.getRangeText() if (!rangeText) return // Add source attribution const attribution = '\n\nSource: My Document Editor' const text = rangeText + attribution // Write to clipboard const plainText = new Blob([text], { type: 'text/plain' }) const item = new ClipboardItem({ 'text/plain': plainText }) await navigator.clipboard.write([item]) // Prevent default copy behavior return { preventDefault: true }}
editor.override.copy = async () => { const selection = editor.command.getRange() const elements = editor.command.getElementList() // Get selected elements const selectedElements = elements.slice( selection.startIndex, selection.endIndex + 1 ) // Convert to HTML const html = convertElementsToHTML(selectedElements) const text = selectedElements.map(el => el.value).join('') // Copy both HTML and plain text const htmlBlob = new Blob([html], { type: 'text/html' }) const textBlob = new Blob([text], { type: 'text/plain' }) const item = new ClipboardItem({ 'text/html': htmlBlob, 'text/plain': textBlob }) await navigator.clipboard.write([item]) return { preventDefault: true }}function convertElementsToHTML(elements: IElement[]): string { return elements .map(el => { let html = el.value || '' if (el.bold) html = `<strong>${html}</strong>` if (el.italic) html = `<em>${html}</em>` if (el.underline) html = `<u>${html}</u>` return html }) .join('')}