Image Processor Plugin
- Features
- imageProcessor
- Basic Usage
- Disable Blob Conversion
- Working with Data URIs
- Programmatic Image Insertion
- Image Selection Behavior
- Handling Image Load Events
- Plugin Initialization
- Image Processing on Change
- Data URI to Blob Conversion
- Blob to Data URI Restoration
- Image Selection
- Image Load Handling
- Memory Management
- JODIT_IMAGE_PROCESSOR_BINDED
- JODIT_IMAGE_BLOB_ID
- resize
- internalUpdate
- Internal Helper: dataURItoBlob()
- Internal Helper: replaceDataURIToBlobUUID()
- Edge Cases
- Notes
- Classes
Handles image processing in the Jodit editor. This plugin automatically manages images in the document, including click handling, selection, load event handling, and conversion of data URIs to blob URLs for better performance.
Features
- Automatic click handler attachment to all images
- Image selection on click/touch
- Automatic editor resize on image load
- Data URI to Blob URL conversion for performance optimization
- Blob URL lifecycle management
- Bidirectional conversion (view ↔ source)
- Memory cleanup on editor destruction
- Automatic image tracking and binding
imageProcessor
Type: { replaceDataURIToBlobIdInView: boolean }
Default: { replaceDataURIToBlobIdInView: true }
Configuration object for the image processor plugin.
imageProcessor.replaceDataURIToBlobIdInView
Type: boolean
Default: true
When enabled, the plugin converts image src
attributes that contain data URIs (base64 encoded images) to blob URLs in the editor view. This provides significant performance benefits for documents with large base64 images.
How it works:
- In Editor View: Images display with
blob:
URLs (e.g.,blob:http://localhost:2000/03377cf0-6260-4351-82ad-8a8901ea104f
) - In Source/Value: Images retain original
data:
URIs (e.g.,data:image/png;base64,iVBORw0KG...
) - The conversion is transparent - when you get
editor.value
, you receive the original data URIs
Example:
const editor = Jodit.make('#editor', {
imageProcessor: {
replaceDataURIToBlobIdInView: true // Default value
}
});
Basic Usage
The plugin is enabled by default and works automatically:
const editor = Jodit.make('#editor');
// Insert image with data URI
editor.value = '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII="/></p>';
// In view: src="blob:http://localhost:2000/..."
// In value: src="data:image/png;base64,..."
Disable Blob Conversion
Disable data URI to blob conversion to use original base64 images:
const editor = Jodit.make('#editor', {
imageProcessor: {
replaceDataURIToBlobIdInView: false
}
});
Working with Data URIs
const editor = Jodit.make('#editor', {
imageProcessor: {
replaceDataURIToBlobIdInView: true
}
});
// Set content with base64 image
editor.value = '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII="/></p>';
// Get value - returns original data URI
console.log(editor.value);
// Output: <p><img src="data:image/png;base64,iVBORw0KG...="/></p>
// Get native editor value - shows blob URL
console.log(editor.getNativeEditorValue());
// Output: <p><img src="blob:http://localhost:2000/03377cf0-6260-4351-82ad-8a8901ea104f"></p>
// Get element value - returns original data URI
console.log(editor.getElementValue());
// Output: <p><img src="data:image/png;base64,iVBORw0KG...="/></p>
Programmatic Image Insertion
const editor = Jodit.make('#editor');
// Insert image with data URI
const dataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=';
editor.s.insertHTML(`<img src="${dataUri}" alt="Test image">`);
// Plugin automatically converts to blob URL in the view
// But preserves data URI in the actual value
Image Selection Behavior
All images are automatically selectable:
const editor = Jodit.make('#editor');
editor.value = '<p><img src="image.jpg"></p>';
// Clicking/touching any image automatically selects it
// No additional code needed
Handling Image Load Events
The plugin automatically handles image loading:
const editor = Jodit.make('#editor');
// When images load, editor automatically resizes
editor.value = '<p><img src="large-image.jpg"></p>';
// Editor will fire 'resize' event after the image loads
editor.e.on('resize', () => {
console.log('Editor resized after image load');
});
Plugin Initialization
- Lifecycle Hooks: Sets up watchers for editor events
- Change Detection: Monitors
:change
,:afterInit
, and:changePlace
events - Value Hooks: Watches
:afterGetValueFromEditor
and:beforeSetElementValue
events
Image Processing on Change
When the editor content changes:
- Image Discovery: Finds all
<img>
elements in the editor - Binding Check: Checks if each image is already processed using data binding
- First-Time Processing: For new images:
- Marks as processed with
JODIT_IMAGE_PROCESSOR_BINDED
flag - Attaches
load
event listener if image not fully loaded - Converts data URI to blob if image has
data:
protocol - Attaches click/touch handlers for selection
- Marks as processed with
Data URI to Blob Conversion
When an image with data URI is detected:
- Feature Detection: Checks for
ArrayBuffer
andURL
support - Blob Creation: Converts base64 data to binary
Blob
:- Extracts MIME type from data URI
- Decodes base64 string using
atob()
- Creates
ArrayBuffer
with binary data - Constructs
Blob
object with correct MIME type
- Object URL: Creates blob URL using
URL.createObjectURL()
- Image Update: Replaces image
src
with blob URL - Mapping Storage: Stores mapping in
editor.buffer
for later restoration:{ 'blob:http://localhost:2000/uuid': 'data:image/png;base64,...' }
Blob to Data URI Restoration
When getting editor value:
- Event Trigger: Catches
:beforeSetElementValue
event - Mapping Lookup: Retrieves blob-to-data URI mapping from buffer
- Replacement: Replaces all blob URLs with original data URIs in the HTML string
- Loop Protection: Uses
while
loop to handle multiple occurrences
Image Selection
When an image is clicked or touched:
- Event Handler: Attached
mousedown
andtouchstart
events - Selection: Calls
editor.s.select(elm)
to select the image - Consistent Behavior: Works for all images, regardless of data URI conversion
Image Load Handling
For images that haven't finished loading:
- Completion Check: Tests
img.complete
property - Event Attachment: Attaches
load
event listener - Resize Trigger: Fires
resize
event when image loads - Cleanup: Removes event listener after first load
- Destruct Check: Verifies editor is not in destruction before firing events
Memory Management
On editor destruction:
- Buffer Access: Retrieves blob URL mappings from buffer
- URL Revocation: Calls
URL.revokeObjectURL()
for each blob URL - Buffer Cleanup: Deletes mapping from buffer
- Memory Release: Ensures browser can free memory for blob objects
JODIT_IMAGE_PROCESSOR_BINDED
Private constant used to mark images as processed. Prevents duplicate event handler attachment.
JODIT_IMAGE_BLOB_ID
Buffer key for storing blob URL to data URI mappings.
resize
Fired when an image finishes loading.
Example:
editor.e.on('resize', () => {
console.log('Editor resized (possibly due to image load)');
});
internalUpdate
Fired after converting data URI to blob URL.
Example:
editor.e.on('internalUpdate', () => {
console.log('Internal update (blob conversion occurred)');
});
dataURItoBlob()
Internal Helper: Converts a data URI string to a Blob object.
Syntax:
function dataURItoBlob(dataURI: string): Blob
Process:
- Splits data URI to extract base64 data and MIME type
- Decodes base64 using
atob()
- Creates
ArrayBuffer
with binary data - Returns
Blob
with correct MIME type
replaceDataURIToBlobUUID()
Internal Helper: Replaces image's data URI with blob URL.
Process:
- Checks
replaceDataURIToBlobIdInView
option - Verifies browser support for
ArrayBuffer
andURL
- Converts data URI to blob
- Creates object URL
- Updates image
src
- Stores mapping in buffer
Edge Cases
-
Browser Support: Conversion only occurs if
ArrayBuffer
andURL
are supported. Falls back gracefully in older browsers. -
Duplicate Processing: Uses data binding to track processed images and avoid duplicate event handlers.
-
Multiple Data URIs: The plugin correctly handles multiple images with data URIs in a single document.
-
Value Retrieval: Different methods return different formats:
editor.value
andeditor.getElementValue()
: Original data URIseditor.getNativeEditorValue()
: Blob URLs
-
Editor Destruction: All blob URLs are properly revoked to prevent memory leaks.
-
Incomplete Images: Load event handlers are only attached to images that haven't finished loading (
!img.complete
). -
Multiple Replacements: Uses
while
loop during restoration to handle cases where the same blob URL appears multiple times. -
Event Cleanup: Load event listeners are removed after first execution to prevent memory leaks.
-
Source Consumer: Only converts blob to data when the consumer is not
SOURCE_CONSUMER
(i.e., when getting value for external use, not internal source mode). -
Empty Buffer: Gracefully handles cases where no blob mappings exist.
Notes
- The plugin uses debouncing on the change handler to optimize performance
- Blob URLs are significantly more efficient for rendering large base64 images
- The conversion is completely transparent to the user
- Original data URIs are always preserved in the editor's value
- Memory is automatically managed - blob URLs are revoked on editor destruction
- The plugin uses the
@watch
decorator for automatic event binding - Image selection works consistently across touch and mouse devices
- The
load
event handler includes a destruct check to prevent errors during editor cleanup - Blob URL mappings are stored in
editor.buffer
for persistence across value get/set operations - The plugin properly handles the bidirectional conversion without data loss
- Works seamlessly with other image-related plugins like
image
andimage-properties