Paste Cleanup in Vue Block Editor
The Block Editor component provides robust paste clean-up functionalities to ensure that pasted content integrates seamlessly and maintains styling and structural consistency. This feature helps remove unwanted formatting, scripts, and elements copied from external sources like web pages or word processors.
You can configure the paste behavior using the pasteCleanupSettings property, which allows you to define how content is handled when pasted into the editor.
Configuring allowed styles
The allowedStyles property lets you define which CSS styles are permitted in pasted content. Any style not in this list is stripped out, ensuring that only desired visual attributes are preserved.
By default, the following styles are allowed: font-weight, font-style, text-decoration, and text-transform.
The example below shows how only font-weight and font-style styles will be retained from the pasted content. All other inline styles will be removed.
<template>
<div id='container'>
<ejs-blockeditor :blocks="blocksData" :pasteCleanupSettings="pasteCleanupSettings" :afterPasteCleanup="afterPasteCleanup"></ejs-blockeditor>
<div id="controls">
<h4>Test Content to Copy and Paste:</h4>
<div class="test-content">
<div id="sampleContent" contenteditable="true">
<h2 style="color: red; font-weight: bold; font-size: 24px;">Formatted Heading</h2>
<p style="background-color: yellow; font-style: italic;">
This is a <span style="font-weight: bold;">bold paragraph</span> with
<span style="color: blue; font-style: italic;">italic text</span> and
<span style="text-decoration: underline;">underlined content</span>.
</p>
<script> console.log('This script should be removed'); </script>
<iframe src="about:blank" width="100" height="50"></iframe>
<div style="border: 1px solid black; padding: 10px;">
<span style="font-weight: 600;">Heavy text</span> and
<span style="color: green; font-size: 18px;">colored text</span>
</div>
</div>
</div>
<div id="output"></div>
</div>
</div>
</template>
<script setup>
import { BlockEditorComponent as EjsBlockeditor, AfterPasteCleanupEventArgs } from "@syncfusion/ej2-vue-blockeditor";
const blocksData = [
{
id: 'demo-block',
blockType: 'Paragraph'
}
];
const pasteCleanupSettings= {
allowedStyles: ['text-decoration'],
deniedTags: ['script', 'iframe']
};
const afterPasteCleanup= (args: AfterPasteCleanupEventArgs) => {
displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
};
const displayOutput=(message) => {
const outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = message;
}
};
onMounted(() => {
displayOutput(`Paste Cleanup Settings Active:
- Keep Format: ${pasteSettings.keepFormat}
- Plain Text: ${pasteSettings.plainText}
Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
});
</script>
<style>
@import '../node_modules/@syncfusion/ej2-tailwind3-theme/styles/blockeditor/index.css';
</style><template>
<div id='container'>
<ejs-blockeditor :blocks="blocksData" :pasteCleanupSettings="pasteCleanupSettings" :afterPasteCleanup="afterPasteCleanup"></ejs-blockeditor>
<div id="controls">
<h4>Test Content to Copy and Paste:</h4>
<div class="test-content">
<div id="sampleContent" contenteditable="true">
<h2 style="color: red; font-weight: bold; font-size: 24px;">Formatted Heading</h2>
<p style="background-color: yellow; font-style: italic;">
This is a <span style="font-weight: bold;">bold paragraph</span> with
<span style="color: blue; font-style: italic;">italic text</span> and
<span style="text-decoration: underline;">underlined content</span>.
</p>
<script> console.log('This script should be removed'); </script>
<iframe src="about:blank" width="100" height="50"></iframe>
<div style="border: 1px solid black; padding: 10px;">
<span style="font-weight: 600;">Heavy text</span> and
<span style="color: green; font-size: 18px;">colored text</span>
</div>
</div>
</div>
<div id="output"></div>
</div>
</div>
</template>
<script>
import { BlockEditorComponent } from "@syncfusion/ej2-vue-blockeditor";
export default {
components: {
'ejs-blockeditor': BlockEditorComponent,
},
data() {
return {
blocksData : [
{
id: 'demo-block',
blockType: 'Paragraph'
}
],
pasteCleanupSettings: {
allowedStyles: ['text-decoration'],
deniedTags: ['script', 'iframe']
},
afterPasteCleanup: (args) => {
this.displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
}
};
},
methods: {
displayOutput :function(message) {
var outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = message;
}
}
},
mounted() {
this.displayOutput(`Paste Cleanup Settings Active:
- Allowed Styles: ['text-decoration']
- Denied Tags: ['script', 'iframe']
Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
}
};
</script>
<style>
@import '../node_modules/@syncfusion/ej2-tailwind3-theme/styles/blockeditor/index.css';
</style>Setting denied tags
The deniedTags property specifies a list of HTML tags to be removed from pasted content. This is useful for stripping potentially problematic elements like <script> or <iframe> tags. By default, this property is an empty array, so no tags are removed.
The example below removes any <script> or <iframe> tags found in the pasted content, preventing unwanted behavior or styling issues.
pasteCleanupSettings: {
deniedTags: ['script', 'iframe']
}Disable Keep format
By default, the editor retains the formatting of pasted content (e.g., bold, italics, links). You can disable this by setting the keepFormat property to false. When disabled, the editor primarily pastes content as plain text, regardless of the allowedStyles configuration.
Allowing plain text
To paste content as plain text, stripping all HTML tags and inline styles, set the plainText property to true in pasteCleanupSettings. This ensures that only raw text is inserted, which is ideal for maintaining strict content consistency. By default, this property is false.
The example below demonstrates the usage of paste settings that disables the keep format and allows plain text.
<template>
<div id='container'>
<ejs-blockeditor :blocks="blocksData" :pasteCleanupSettings="pasteCleanupSettings" :afterPasteCleanup="afterPasteCleanup"></ejs-blockeditor>
<div id="controls">
<h4>Test Content to Copy and Paste:</h4>
<div class="test-content">
<div id="sampleContent" contenteditable="true">
<h2 style="color: red; font-weight: bold; font-size: 24px;">Formatted Heading</h2>
<p style="background-color: yellow; font-style: italic;">
This is a <span style="font-weight: bold;">bold paragraph</span> with
<span style="color: blue; font-style: italic;">italic text</span> and
<span style="text-decoration: underline;">underlined content</span>.
</p>
<div style="border: 1px solid black; padding: 10px;">
<span style="font-weight: 600;">Heavy text</span> and
<span style="color: green; font-size: 18px;">colored text</span>
</div>
</div>
</div>
<div id="output"></div>
</div>
</div>
</template>
<script setup>
import { BlockEditorComponent as EjsBlockeditor } from "@syncfusion/ej2-vue-blockeditor";
const blocksData = [
{
blockType: 'Paragraph'
}
];
const pasteCleanupSettings= {
keepFormat: false,
plainText: true,
};
const afterPasteCleanup= (args) => {
displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
};
const displayOutput=(message) => {
const outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = message;
}
}
onMounted(() => {
displayOutput(`Paste Cleanup Settings Active:
- Keep Format: ${pasteSettings.keepFormat}
- Plain Text: ${pasteSettings.plainText}
Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
});
</script>
<style>
@import '../node_modules/@syncfusion/ej2-tailwind3-theme/styles/blockeditor/index.css';
</style><template>
<div id='container'>
<ejs-blockeditor :blocks="blocksData" :pasteCleanupSettings="pasteCleanupSettings" :afterPasteCleanup="afterPasteCleanup"></ejs-blockeditor>
<div id="controls">
<h4>Test Content to Copy and Paste:</h4>
<div class="test-content">
<div id="sampleContent" contenteditable="true">
<h2 style="color: red; font-weight: bold; font-size: 24px;">Formatted Heading</h2>
<p style="background-color: yellow; font-style: italic;">
This is a <span style="font-weight: bold;">bold paragraph</span> with
<span style="color: blue; font-style: italic;">italic text</span> and
<span style="text-decoration: underline;">underlined content</span>.
</p>
<div style="border: 1px solid black; padding: 10px;">
<span style="font-weight: 600;">Heavy text</span> and
<span style="color: green; font-size: 18px;">colored text</span>
</div>
</div>
</div>
<div id="output"></div>
</div>
</div>
</template>
<script>
import { BlockEditorComponent, ContentType, AfterPasteEventArgs } from "@syncfusion/ej2-vue-blockeditor";
export default {
components: {
'ejs-blockeditor': BlockEditorComponent,
},
data() {
return {
blocksData : [
{
blockType: 'Paragraph'
}
],
pasteCleanupSettings: {
keepFormat: false,
plainText: true,
},
afterPasteCleanup: (args) => {
this.displayOutput(`After Paste Event: Processed content length: ${args.content.length} characters`);
}
};
},
methods: {
displayOutput :function(message) {
var outputDiv = document.getElementById('output');
if (outputDiv) {
outputDiv.textContent = message;
}
}
},
mounted() {
this.displayOutput(`Paste Cleanup Settings Active:
- Keep Format: ${pasteSettings.keepFormat}
- Plain Text: ${pasteSettings.plainText}
Copy content from the test area above and paste it into the editor to see the cleanup in action.`);
}
};
</script>
<style>
@import '../node_modules/@syncfusion/ej2-tailwind3-theme/styles/blockeditor/index.css';
</style>Events
The Block Editor provides events to monitor and interact with the paste action.
| Name | Args | Description |
|---|---|---|
| beforePasteCleanup | BeforePasteCleanupEventArgs | Triggers before the content is pasted into the editor. |
| afterPasteCleanup | AfterPasteCleanupEventArgs | Triggers after the content is pasted into the editor. |
The snippet below demonstrates how to configure the above events in the editor.
<template>
<ejs-blockeditor @beforePasteCleanup="onBeforePasteCleanup" @afterPasteCleanup="onAfterPasteCleanup"></ejs-blockeditor>
</template>
<script>
import { BlockEditorComponent } from '@syncfusion/ej2-vue-blockeditor';
export default {
components: { 'ejs-blockeditor': BlockEditorComponent },
methods: {
onBeforePasteCleanup(args) {
// Inspect or modify args; return args to apply changes
},
onAfterPasteCleanup(args) {
// Run post-processing after the paste is applied
}
}
};
</script>