How can I help you?
Clipboard Cleanup in EJ2 TypeScript Rich Text Editor control
9 Dec 20256 minutes to read
The Rich Text Editor supports automatic cleanup of clipboard content during copy (Ctrl + C) and cut (Ctrl + X) operations. When this feature is enabled, unwanted inline styles are automatically removed from the clipboard content while preserving important structural elements such as tables, lists, and images. You can enable this behavior using the enableClipboardCleanup property.
Rich Text Editor features are segregated into individual feature-wise modules. To use clipboard cleanup, inject ClipboardCleanup module using the
RichTextEditor.Inject(ClipboardCleanup).
When enableClipboardCleanup is set to true, copy and cut operations are intercepted to remove unwanted inline styles. When set to false the browser’s default copy and cut behavior applies.
For a cleaner editing experience,
enableClipboardCleanupistrueby default.
import { RichTextEditor, Toolbar, Image, Link, HtmlEditor, QuickToolbar, ClipboardCleanup } from '@syncfusion/ej2-richtexteditor';
RichTextEditor.Inject(Toolbar, Image, Link, HtmlEditor, QuickToolbar, ClipboardCleanup );
let editor: RichTextEditor = new RichTextEditor({});
editor.appendTo('#editor');<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Rich Text Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-base/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-richtexteditor/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-inputs/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-lists/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-navigations/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-popups/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-buttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='editor'></div>
</div>
</body>
</html>Customizing Copied/Cut Content
You can use the beforeClipboardWrite event, which fires before content is written to the clipboard during copy or cut operations. This event lets you to modify the HTML and plain-text representations of the content and also determine whether the action is a copy or a cut.
In this example, the beforeClipboardWrite event is used to customize the selected content only during a copy operation, while the cut operation remains unaffected. To observe this behavior, try copying any text within the Rich Text Editor.
import { RichTextEditor, Toolbar, Image, Link, HtmlEditor, QuickToolbar, ClipboardCleanup, ClipboardWriteEventArgs } from '@syncfusion/ej2-richtexteditor';
RichTextEditor.Inject(Toolbar, Image, Link, HtmlEditor, QuickToolbar, ClipboardCleanup, ClipboardWriteEventArgs );
let editor: RichTextEditor = new RichTextEditor({
beforeClipboardWrite: onBeforeClipboardWrite,
});
editor.appendTo('#editor');
function onBeforeClipboardWrite(args: ClipboardWriteEventArgs) {
if (args.operation === 'copy') {
args.htmlContent = '<h1>This is customized content triggered while copy event</h1>';
args.plainTextContent = 'This is customized content triggered while copy event';
}
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Rich Text Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-base/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-richtexteditor/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-inputs/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-lists/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-navigations/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-popups/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-buttons/styles/tailwind3.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/33.1.44/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='editor'></div>
</div>
</body>
</html>Best Practices & Troubleshooting
Best Practices
- Always include
ClipboardCleanupin production application - it’s the #1 way to prevent style explosion. - Combine with Paste Cleanup module for even stricter incoming paste handling.
- Use
beforeClipboardWritesparingly — heavy processing can slightly delayCtrl+C.
Common Issues
- “Copied content still has inline styles” → Confirm
ClipboardCleanupis in providers. - “Event not firing” → Make sure you’re using the correct event name
beforeClipboardWrite(camelCase in TypeScript & JavaScript).