Package com.wontlost.ckeditor
Class VaadinCKEditor
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.BlurNotifier<com.vaadin.flow.component.customfield.CustomField<String>>,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.Focusable<com.vaadin.flow.component.customfield.CustomField<String>>,com.vaadin.flow.component.FocusNotifier<com.vaadin.flow.component.customfield.CustomField<String>>,com.vaadin.flow.component.HasAriaLabel,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasEnabled,com.vaadin.flow.component.HasHelper,com.vaadin.flow.component.HasLabel,com.vaadin.flow.component.HasSize,com.vaadin.flow.component.HasStyle,com.vaadin.flow.component.HasTheme,com.vaadin.flow.component.HasValidation,com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,,String>, String> com.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,,String>, String> com.vaadin.flow.component.shared.HasTooltip,com.vaadin.flow.component.shared.HasValidationProperties,com.vaadin.flow.component.shared.InputField<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,,String>, String> Serializable
@Tag("vaadin-ckeditor")
@JsModule("./vaadin-ckeditor/vaadin-ckeditor.ts")
@NpmPackage(value="ckeditor5",version="48.2.0") @NpmPackage(value="lit",version="^3.3.3")
public class VaadinCKEditor
extends com.vaadin.flow.component.customfield.CustomField<String>
implements com.vaadin.flow.component.HasAriaLabel
Vaadin CKEditor 5 component.
Modular CKEditor 5 integration with plugin-based customization.
Usage examples:
// Use preset
VaadinCKEditor editor = VaadinCKEditor.create()
.withPreset(CKEditorPreset.STANDARD)
.build();
// Custom plugins (dependencies auto-resolved)
VaadinCKEditor editor = VaadinCKEditor.create()
.withPlugins(CKEditorPlugin.BOLD, CKEditorPlugin.ITALIC, CKEditorPlugin.IMAGE_CAPTION)
.withToolbar("bold", "italic", "|", "insertImage")
.build();
// IMAGE_CAPTION automatically includes IMAGE plugin as dependency
// Customize preset
VaadinCKEditor editor = VaadinCKEditor.create()
.withPreset(CKEditorPreset.BASIC)
.addPlugin(CKEditorPlugin.TABLE)
.withLanguage("zh-cn")
.build();
Dependency Resolution:
The builder automatically resolves plugin dependencies by default. For example, adding IMAGE_CAPTION will automatically include the IMAGE plugin.
// Auto-resolve with recommended plugins for full feature set
VaadinCKEditor editor = VaadinCKEditor.create()
.withPlugins(CKEditorPlugin.IMAGE)
.withDependencyMode(DependencyMode.AUTO_RESOLVE_WITH_RECOMMENDED)
.build();
// Includes IMAGE plus recommended: IMAGE_TOOLBAR, IMAGE_CAPTION, IMAGE_STYLE, IMAGE_RESIZE
// Strict mode - fail if dependencies missing
VaadinCKEditor editor = VaadinCKEditor.create()
.withPlugins(CKEditorPlugin.IMAGE_CAPTION) // Missing IMAGE dependency
.withDependencyMode(DependencyMode.STRICT)
.build(); // Throws IllegalStateException
// Manual mode - no dependency checking
VaadinCKEditor editor = VaadinCKEditor.create()
.withPlugins(CKEditorPlugin.ESSENTIALS, CKEditorPlugin.PARAGRAPH, CKEditorPlugin.BOLD)
.withDependencyMode(DependencyMode.MANUAL)
.build();
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<C extends com.vaadin.flow.component.Component,V> Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
com.vaadin.flow.component.BlurNotifier.BlurEvent<C extends com.vaadin.flow.component.Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
com.vaadin.flow.component.FocusNotifier.FocusEvent<C extends com.vaadin.flow.component.Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
com.vaadin.flow.component.HasValue.ValueChangeEvent<V>, com.vaadin.flow.component.HasValue.ValueChangeListener<E extends com.vaadin.flow.component.HasValue.ValueChangeEvent<?>> -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddAutosaveListener(com.vaadin.flow.component.ComponentEventListener<AutosaveEvent> listener) Add autosave event listener.com.vaadin.flow.shared.RegistrationaddContentChangeListener(com.vaadin.flow.component.ComponentEventListener<ContentChangeEvent> listener) Add content change event listener.com.vaadin.flow.shared.RegistrationaddEditorErrorListener(com.vaadin.flow.component.ComponentEventListener<EditorErrorEvent> listener) Add editor error event listener.com.vaadin.flow.shared.RegistrationaddEditorReadyListener(com.vaadin.flow.component.ComponentEventListener<EditorReadyEvent> listener) Add editor ready event listener.com.vaadin.flow.shared.RegistrationaddFallbackListener(com.vaadin.flow.component.ComponentEventListener<FallbackEvent> listener) Add fallback event listener.booleancancelUpload(String uploadId) Cancel the specified upload task.voidClean up all event listeners.voidclear()Clear editor contentstatic VaadinCKEditorBuildercreate()Create editor builder.voidProgrammatically focus the editor's editable area.protected StringintGet the number of active uploads.intGet character count of content (excluding HTML tags).Get error handler.Get fallback mode.Get HTML sanitizer.getId()Get statistics of registered listeners.Get plain text content (strip HTML tags)Get sanitized HTML (remove dangerous tags)Get sanitized content.Get upload handler.getValue()static StringGet versionintGet word count of content.booleanCheck if there are uploads in progress.voidinsertText(String text) Insert text at cursor positionbooleanCheck if content is empty.booleanvoidonEnabledStateChanged(boolean enabled) 同步 enabled 状态到前端(issue #46)。sanitizeHtml(String html, org.jsoup.safety.Safelist safelist) Sanitize HTML with specified rulesvoidsetAiSidebarEnabled(boolean enabled) 启用 AI 侧栏容器(仅 DECOUPLED 类型)。voidsetAllowConfigRequiredPlugins(boolean allow) Allow plugins that require special configuration (CloudServices, Minimap, etc.).voidsetAnnotationSidebarEnabled(boolean enabled) Enable annotation sidebar for collaboration features (DECOUPLED type only).voidsetAutosaveCallback(Consumer<String> callback) Set autosave callbackvoidMove the editor caret (collapsed selection) to the very end of the document and focus the editor.voidMove the editor caret (collapsed selection) to the very start of the document and focus the editor.voidsetCommentPermissionEnforcerEnabled(boolean enabled) 启用评论权限强制插件,隐藏非当前用户评论的 Edit/Remove 下拉菜单。voidsetDocumentOutlineEnabled(boolean enabled) Enable Document Outline sidebar (DECOUPLED type only).voidsetErrorHandler(ErrorHandler handler) Set error handler.voidSet fallback mode.voidsetGeneralHtmlSupportEnabled(boolean enabled) Enable general HTML supportvoidvoidsetHideToolbar(boolean hide) Set toolbar visibilityvoidsetHtmlSanitizer(HtmlSanitizer sanitizer) Set HTML sanitizer.voidSet editor ID.voidsetMinimapEnabled(boolean enabled) Enable minimap (DECOUPLED type only)voidsetMinimapSimplePreview(boolean enabled) Enable simple preview mode for minimap (DECOUPLED type only).protected voidsetModelValue(String value, boolean fromClient) voidsetOverrideCssUrl(String url) Set custom CSS URLprotected voidsetPresentationValue(String value) voidsetReadOnly(boolean readOnly) voidsetReadOnlyWithToolbarAction(boolean readOnly) Enable read-only mode and hide toolbarvoidsetSynchronized(boolean sync) Set synchronous update modevoidsetUploadHandler(UploadHandler handler) Set upload handler.voidvoidstatic VaadinCKEditorwithPreset(CKEditorPreset preset) Quick create editor with presetMethods inherited from class com.vaadin.flow.component.customfield.CustomField
add, addThemeVariants, getLabel, remove, removeThemeVariants, setLabel, updateValueMethods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, bindValue, getEmptyValue, isEmpty, valueEqualsMethods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setTestId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, focus, getTabIndex, setTabIndexMethods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListenerMethods inherited from interface com.vaadin.flow.component.HasAriaLabel
getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy, setAriaLabelledByMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
bindEnabled, isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasHelper
bindHelperText, getHelperComponent, getHelperText, setHelperComponent, setHelperTextMethods inherited from interface com.vaadin.flow.component.HasSize
bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, bindThemeName, bindThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipMarkdown, setTooltipTextMethods inherited from interface com.vaadin.flow.component.HasValidation
setManualValidationMethods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setErrorMessage, setInvalidMethods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, bindValue, getEmptyValue, getOptionalValue, isEmptyMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
bindReadOnly, bindRequiredIndicatorVisible, isRequiredIndicatorVisible, setRequiredIndicatorVisible
-
Method Details
-
create
Create editor builder.- Returns:
- a new builder instance
-
withPreset
Quick create editor with preset -
generateModelValue
- Specified by:
generateModelValuein classcom.vaadin.flow.component.customfield.CustomField<String>
-
setPresentationValue
- Specified by:
setPresentationValuein classcom.vaadin.flow.component.customfield.CustomField<String>
-
getValue
- Specified by:
getValuein interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,String>, String> - Overrides:
getValuein classcom.vaadin.flow.component.AbstractField<com.vaadin.flow.component.customfield.CustomField<String>,String>
-
getSanitizedValue
Get sanitized content. If HtmlSanitizer is set, applies sanitization; otherwise returns original content.- Returns:
- sanitized HTML content
-
setValue
- Specified by:
setValuein interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,String>, String> - Overrides:
setValuein classcom.vaadin.flow.component.AbstractField<com.vaadin.flow.component.customfield.CustomField<String>,String>
-
setModelValue
-
setId
Set editor ID. Note: This sets both the Vaadin component ID and the internal editorId property. The editorId is used for frontend component identification.- Overrides:
setIdin classcom.vaadin.flow.component.Component- Parameters:
id- the ID to set, or null to generate a random ID
-
getId
- Overrides:
getIdin classcom.vaadin.flow.component.Component
-
setReadOnly
public void setReadOnly(boolean readOnly) - Specified by:
setReadOnlyin interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,String>, String> - Specified by:
setReadOnlyin interfacecom.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,String>, String>
-
isReadOnly
public boolean isReadOnly()- Specified by:
isReadOnlyin interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,String>, String> - Specified by:
isReadOnlyin interfacecom.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<String>,String>, String>
-
onEnabledStateChanged
public void onEnabledStateChanged(boolean enabled) 同步 enabled 状态到前端(issue #46)。沿用 Vaadin 推荐的
Component.onEnabledStateChanged(boolean)钩子,而非覆写setEnabled——Vaadin 自身负责 disabled 属性的传播,这里只把状态 推给 CKEditor 前端,使禁用时编辑器不可交互(CKEditor 5 通过只读锁实现禁用)。- Overrides:
onEnabledStateChangedin classcom.vaadin.flow.component.Component
-
setWidth
- Specified by:
setWidthin interfacecom.vaadin.flow.component.HasSize
-
setHeight
- Specified by:
setHeightin interfacecom.vaadin.flow.component.HasSize
-
setHideToolbar
public void setHideToolbar(boolean hide) Set toolbar visibility -
setReadOnlyWithToolbarAction
public void setReadOnlyWithToolbarAction(boolean readOnly) Enable read-only mode and hide toolbar -
setMinimapEnabled
public void setMinimapEnabled(boolean enabled) Enable minimap (DECOUPLED type only) -
setMinimapSimplePreview
public void setMinimapSimplePreview(boolean enabled) Enable simple preview mode for minimap (DECOUPLED type only). When true, minimap renders content as simple boxes for better performance. Use this option if the minimap updates too slowly with large documents.- Parameters:
enabled- true to enable simple preview mode
-
setDocumentOutlineEnabled
public void setDocumentOutlineEnabled(boolean enabled) Enable Document Outline sidebar (DECOUPLED type only). Requires DocumentOutline plugin to be loaded (premium feature).- Parameters:
enabled- true to enable document outline
-
setAnnotationSidebarEnabled
public void setAnnotationSidebarEnabled(boolean enabled) Enable annotation sidebar for collaboration features (DECOUPLED type only). Provides container for Comments, TrackChanges and PresenceList UI.- Parameters:
enabled- true to enable annotation sidebar and presence list
-
setAiSidebarEnabled
public void setAiSidebarEnabled(boolean enabled) 启用 AI 侧栏容器(仅 DECOUPLED 类型)。 AI Chat/Assistant 插件在 sidebar 模式下需要 DOM 容器。 启用后会在编辑器右侧创建 AI 侧栏容器,并自动将 config.ai.container.element 绑定到该容器。- Parameters:
enabled- true 启用 AI 侧栏
-
setCommentPermissionEnforcerEnabled
public void setCommentPermissionEnforcerEnabled(boolean enabled) 启用评论权限强制插件,隐藏非当前用户评论的 Edit/Remove 下拉菜单。 需要同时启用 annotationSidebarEnabled 和 CommentsRepository 插件。- Parameters:
enabled- true 启用评论权限 UI 强制
-
setGeneralHtmlSupportEnabled
public void setGeneralHtmlSupportEnabled(boolean enabled) Enable general HTML support -
setAllowConfigRequiredPlugins
public void setAllowConfigRequiredPlugins(boolean allow) Allow plugins that require special configuration (CloudServices, Minimap, etc.). When enabled, these plugins won't be automatically filtered out by the plugin resolver. This is automatically set to true when premium plugins requiring CloudServices are used. -
setSynchronized
public void setSynchronized(boolean sync) Set synchronous update mode -
setOverrideCssUrl
Set custom CSS URL -
setAutosaveCallback
Set autosave callback -
clear
public void clear()Clear editor content -
insertText
Insert text at cursor position -
setCaretToStart
public void setCaretToStart()Move the editor caret (collapsed selection) to the very start of the document and focus the editor.Useful when leading block content (e.g. a table used as a letterhead) would otherwise be auto-selected/highlighted on focus (issue #52). Calling this places the caret before the first content so nothing is highlighted.
-
setCaretToEnd
public void setCaretToEnd()Move the editor caret (collapsed selection) to the very end of the document and focus the editor. -
focusEditor
public void focusEditor()Programmatically focus the editor's editable area. -
getPlainText
Get plain text content (strip HTML tags) -
getSanitizedHtml
Get sanitized HTML (remove dangerous tags) -
sanitizeHtml
Sanitize HTML with specified rules -
getCharacterCount
public int getCharacterCount()Get character count of content (excluding HTML tags).- Returns:
- character count
-
getWordCount
public int getWordCount()Get word count of content.- Returns:
- word count
-
isContentEmpty
public boolean isContentEmpty()Check if content is empty.- Returns:
- true if content is empty or contains only whitespace
-
getVersion
Get version -
addEditorReadyListener
public com.vaadin.flow.shared.Registration addEditorReadyListener(com.vaadin.flow.component.ComponentEventListener<EditorReadyEvent> listener) Add editor ready event listener. Fired when the editor is fully initialized and ready to accept user input.Usage example:
editor.addEditorReadyListener(event -> { logger.info("Editor ready in {} ms", event.getInitializationTimeMs()); event.getSource().focus(); });- Parameters:
listener- the event listener- Returns:
- registration object for removing the listener
-
addEditorErrorListener
public com.vaadin.flow.shared.Registration addEditorErrorListener(com.vaadin.flow.component.ComponentEventListener<EditorErrorEvent> listener) Add editor error event listener. Fired when the editor encounters an error.Usage example:
editor.addEditorErrorListener(event -> { EditorError error = event.getError(); if (error.getSeverity() == ErrorSeverity.FATAL) { Notification.show("Editor error: " + error.getMessage(), Notification.Type.ERROR_MESSAGE); } });- Parameters:
listener- the event listener- Returns:
- registration object for removing the listener
-
addAutosaveListener
public com.vaadin.flow.shared.Registration addAutosaveListener(com.vaadin.flow.component.ComponentEventListener<AutosaveEvent> listener) Add autosave event listener. Fired when editor content is auto-saved.- Parameters:
listener- the event listener- Returns:
- registration object for removing the listener
-
addContentChangeListener
public com.vaadin.flow.shared.Registration addContentChangeListener(com.vaadin.flow.component.ComponentEventListener<ContentChangeEvent> listener) Add content change event listener. Fired when editor content changes.- Parameters:
listener- the event listener- Returns:
- registration object for removing the listener
-
addFallbackListener
public com.vaadin.flow.shared.Registration addFallbackListener(com.vaadin.flow.component.ComponentEventListener<FallbackEvent> listener) Add fallback event listener. Fired when the editor triggers fallback mode due to an error.- Parameters:
listener- the event listener- Returns:
- registration object for removing the listener
-
setErrorHandler
Set error handler.- Parameters:
handler- the error handler
-
getErrorHandler
Get error handler.- Returns:
- the error handler, may be null
-
setHtmlSanitizer
Set HTML sanitizer.- Parameters:
sanitizer- the HTML sanitizer
-
getHtmlSanitizer
Get HTML sanitizer.- Returns:
- the HTML sanitizer, may be null
-
setUploadHandler
Set upload handler.- Parameters:
handler- the upload handler
-
getUploadHandler
Get upload handler.- Returns:
- the upload handler, may be null
-
setFallbackMode
Set fallback mode.- Parameters:
mode- the fallback mode
-
getFallbackMode
Get fallback mode.- Returns:
- the current fallback mode
-
getListenerStats
Get statistics of registered listeners. Used for debugging and monitoring.- Returns:
- listener statistics
-
cleanupListeners
public void cleanupListeners()Clean up all event listeners. Usually called before component destruction. -
hasActiveUploads
public boolean hasActiveUploads()Check if there are uploads in progress.- Returns:
- true if there are active uploads
-
getActiveUploadCount
public int getActiveUploadCount()Get the number of active uploads.- Returns:
- active upload count
-
cancelUpload
Cancel the specified upload task.- Parameters:
uploadId- the upload ID- Returns:
- true if successfully cancelled
-