Class VaadinCKEditorBuilder
This builder provides a fluent API for configuring CKEditor 5 instances with support for presets, custom plugins, dependency resolution, and more.
Quick Start
// Simplest usage - use a preset
VaadinCKEditor editor = VaadinCKEditor.withPreset(CKEditorPreset.STANDARD);
// Or use the builder directly
VaadinCKEditor editor = VaadinCKEditorBuilder.create()
.withPreset(CKEditorPreset.STANDARD)
.build();
Custom Plugins
Plugin dependencies are automatically resolved by default:
// IMAGE_CAPTION requires IMAGE - it will be added automatically
VaadinCKEditor editor = VaadinCKEditorBuilder.create()
.withPlugins(CKEditorPlugin.BOLD, CKEditorPlugin.IMAGE_CAPTION)
.withToolbar("bold", "|", "insertImage")
.build();
Customizing Presets
VaadinCKEditor editor = VaadinCKEditorBuilder.create()
.withPreset(CKEditorPreset.BASIC)
.addPlugin(CKEditorPlugin.TABLE) // Add table support
.removePlugin(CKEditorPlugin.MEDIA_EMBED) // Remove media embed
.withLanguage("zh-cn") // Chinese UI
.withTheme(CKEditorTheme.DARK) // Dark theme
.build();
View-Only Mode
// For displaying content without editing
VaadinCKEditor viewer = VaadinCKEditorBuilder.create()
.withPreset(CKEditorPreset.BASIC)
.withValue("<p>Content to display</p>")
.withViewOnly() // Read-only + hidden toolbar
.build();
Enterprise Features
VaadinCKEditor editor = VaadinCKEditorBuilder.create()
.withPreset(CKEditorPreset.FULL)
.withErrorHandler(error -> {
// Custom error handling
logger.error("Editor error: " + error.getMessage());
return false; // Let event propagate
})
.withHtmlSanitizer(HtmlSanitizer.withPolicy(SanitizationPolicy.STRICT))
.withUploadHandler((context, stream) -> {
// Handle file uploads
String url = saveFile(context.getFileName(), stream);
return CompletableFuture.completedFuture(new UploadResult(url));
})
.withFallbackMode(FallbackMode.TEXTAREA)
.build();
Thread Safety
Builder instances are not thread-safe. Create a new builder for each editor.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDependency resolution mode for plugins -
Method Summary
Modifier and TypeMethodDescriptionaddCustomPlugin(CustomPlugin plugin) Add custom pluginaddPlugin(CKEditorPlugin plugin) Add pluginaddPlugins(CKEditorPlugin... plugins) Add multiple pluginsbuild()Build and initialize the configured VaadinCKEditor instance.static VaadinCKEditorBuildercreate()Create a new builder instance.Get missing dependencies for current plugin configuration.Get missing dependencies for premium plugins in current configuration.Get the resolved plugins that would be used with current configuration.readOnly(boolean readOnly) Set read-only mode.removePlugin(CKEditorPlugin plugin) Remove pluginwithAutosave(Consumer<String> callback, int waitingTime) Enable autosave with callback and waiting time.withConfig(CKEditorConfig config) Set configurationSet dependency resolution mode.withErrorHandler(ErrorHandler handler) Set error handler for custom error handling.Set fallback mode for graceful degradation.withHeight(String height) Set heightwithHideToolbar(boolean hide) Set whether to hide the toolbar.withHtmlSanitizer(HtmlSanitizer sanitizer) Set HTML sanitizer for content cleaning.withLanguage(String language) Set language for editor UI and spell checking.withLicenseKey(String licenseKey) Set CKEditor license key for premium features.withPlugins(CKEditorPlugin... plugins) Set plugin list (override preset)withPreset(CKEditorPreset preset) Use preset configurationwithReadOnly(boolean readOnly) Convenience method for setting read-only mode.withTheme(CKEditorTheme theme) Set theme mode.withToolbar(String... items) Set toolbar items.withType(CKEditorType type) Set editor typeSet upload configuration for file uploads.withUploadHandler(UploadHandler handler) Set upload handler for file uploads.Set initial contentConfigure editor for view-only mode.Set width
-
Method Details
-
create
-
withPreset
Use preset configuration- Parameters:
preset- the preset to use- Returns:
- this builder for chaining
-
withPlugins
Set plugin list (override preset)- Parameters:
plugins- the plugins to use- Returns:
- this builder for chaining
-
addPlugin
Add plugin- Parameters:
plugin- the plugin to add- Returns:
- this builder for chaining
-
addPlugins
Add multiple plugins- Parameters:
plugins- the plugins to add- Returns:
- this builder for chaining
-
removePlugin
Remove plugin- Parameters:
plugin- the plugin to remove- Returns:
- this builder for chaining
-
addCustomPlugin
Add custom plugin- Parameters:
plugin- the custom plugin to add- Returns:
- this builder for chaining
-
withToolbar
Set toolbar items.Toolbar items can include:
- Plugin command names (e.g., "bold", "italic", "insertTable")
- Separator "|" to create visual groupings
- Line break "-" to wrap to next line
- Parameters:
items- the toolbar items (must not be null or empty)- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if items is null or empty
-
withType
Set editor type- Parameters:
type- the editor type- Returns:
- this builder for chaining
-
withTheme
Set theme mode.Available modes:
CKEditorTheme.AUTO(default) - Automatically syncs with Vaadin's Lumo theme. When Vaadin switches between light/dark mode, CKEditor follows automatically. Also respects OS-level dark mode preference if Vaadin doesn't have an explicit theme.CKEditorTheme.LIGHT- Forces light theme regardless of Vaadin/OS settings.CKEditorTheme.DARK- Forces dark theme regardless of Vaadin/OS settings.
- Parameters:
theme- the theme mode to use- Returns:
- this builder for chaining
-
withLanguage
Set language for editor UI and spell checking.Supported formats:
- ISO 639-1 two-letter code: "en", "zh", "ja", "ko"
- With region: "en-us", "zh-cn", "pt-br"
- Parameters:
language- the language code (e.g., "en", "zh-cn")- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if language format is invalid
-
withFallbackMode
Set fallback mode for graceful degradation.- Parameters:
mode- the fallback mode- Returns:
- this builder for chaining
-
withErrorHandler
Set error handler for custom error handling.- Parameters:
handler- the error handler- Returns:
- this builder for chaining
-
withHtmlSanitizer
Set HTML sanitizer for content cleaning.- Parameters:
sanitizer- the HTML sanitizer- Returns:
- this builder for chaining
-
withUploadHandler
Set upload handler for file uploads.- Parameters:
handler- the upload handler- Returns:
- this builder for chaining
-
withUploadConfig
Set upload configuration for file uploads. Configures file size limits and allowed MIME types.Example:
VaadinCKEditor editor = VaadinCKEditor.create() .withUploadHandler(myHandler) .withUploadConfig(new UploadHandler.UploadConfig() .setMaxFileSize(5 * 1024 * 1024) // 5MB .setAllowedMimeTypes("image/jpeg", "image/png", "image/gif")) .build();- Parameters:
config- the upload configuration- Returns:
- this builder for chaining
-
withValue
Set initial content- Parameters:
value- the initial HTML content- Returns:
- this builder for chaining
-
readOnly
Set read-only mode.- Parameters:
readOnly- whether the editor should be read-only- Returns:
- this builder for chaining
-
withReadOnly
Convenience method for setting read-only mode. Alias forreadOnly(boolean).- Parameters:
readOnly- whether the editor should be read-only- Returns:
- this builder for chaining
-
withViewOnly
Configure editor for view-only mode.This is a convenience method that sets:
- Read-only mode enabled
- Toolbar hidden
- Fallback mode to READ_ONLY
Use this for displaying content without editing capabilities.
- Returns:
- this builder for chaining
-
withHideToolbar
Set whether to hide the toolbar.- Parameters:
hide- true to hide toolbar, false to show- Returns:
- this builder for chaining
-
withAutosave
Enable autosave with callback and waiting time.- Parameters:
callback- the callback to invoke when autosave triggerswaitingTime- waiting time in milliseconds (100-60000)- Returns:
- this builder
- Throws:
IllegalArgumentException- if waitingTime is out of range
-
withConfig
Set configuration- Parameters:
config- the editor configuration- Returns:
- this builder for chaining
-
withWidth
Set width- Parameters:
width- the width (e.g., "100%", "500px")- Returns:
- this builder for chaining
-
withHeight
Set height- Parameters:
height- the height (e.g., "400px", "100%")- Returns:
- this builder for chaining
-
withDependencyMode
Set dependency resolution mode.Available modes:
VaadinCKEditorBuilder.DependencyMode.AUTO_RESOLVE(default) - Automatically add missing dependenciesVaadinCKEditorBuilder.DependencyMode.AUTO_RESOLVE_WITH_RECOMMENDED- Add dependencies and recommended pluginsVaadinCKEditorBuilder.DependencyMode.STRICT- Fail if dependencies are missingVaadinCKEditorBuilder.DependencyMode.MANUAL- No dependency checking
- Parameters:
mode- the dependency resolution mode- Returns:
- this builder
-
withLicenseKey
Set CKEditor license key for premium features.Premium features require a valid license key from CKEditor. Get your license key at: https://ckeditor.com/pricing
Usage example:
VaadinCKEditor editor = VaadinCKEditorBuilder.create() .withPreset(CKEditorPreset.STANDARD) .withLicenseKey("your-license-key-here") .addCustomPlugin(CustomPlugin.fromPremium("ExportPdf")) .build();Note: You must also install the premium features package in your frontend:
npm install ckeditor5-premium-features
- Parameters:
licenseKey- the CKEditor license key- Returns:
- this builder for chaining
- See Also:
-
build
Build and initialize the configured VaadinCKEditor instance.This method performs the following steps:
- Collects plugins from preset and additional plugins
- Removes any plugins marked for removal
- Resolves plugin dependencies based on
VaadinCKEditorBuilder.DependencyMode - Configures toolbar (custom or from preset)
- Applies configuration settings
- Initializes the editor component
Note: The builder can only be used once. After calling
build(), create a new builder for additional editors.- Returns:
- the fully configured and initialized VaadinCKEditor instance
- Throws:
IllegalStateException- ifVaadinCKEditorBuilder.DependencyMode.STRICTis used and plugin dependencies are not satisfied
-
getResolvedPlugins
Get the resolved plugins that would be used with current configuration. Useful for debugging or inspection before building.- Returns:
- set of plugins that would be included
-
getMissingDependencies
Get missing dependencies for current plugin configuration. Useful for understanding what dependencies would be auto-resolved.- Returns:
- map of plugins to their missing dependencies
-
getMissingPremiumDependencies
Get missing dependencies for premium plugins in current configuration. Useful for understanding what premium plugin dependencies would be auto-resolved.- Returns:
- map of premium plugin names to their missing dependencies
-