Class CKEditorConfig
java.lang.Object
com.wontlost.ckeditor.CKEditorConfig
CKEditor configuration class.
Used to build JSON configuration passed to CKEditor.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classIndividual button style configuration.static classCode block language definitionstatic classHeading option definitionstatic classMention feed definitionstatic classStyle definition for the Style plugin.static classToolbar style configuration for customizing CKEditor toolbar appearance. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionallowPrivateNetworks(boolean allow) 允许私有/内网地址作为上传 URL。String[]Get the alignment options.intGet the autosave waiting time.booleangetBoolean(String key, boolean defaultValue) Get a boolean configuration value.Get the code block indent sequence.Get configuration mapString[]Get the font family options.String[]Get the font size options.tools.jackson.databind.JsonNodeGet heading options configuration.String[]Get the image styles.String[]Get the image toolbar items.intGet an integer configuration value.tools.jackson.databind.JsonNodegetJsonNode(String key) Get the raw JSON node for a configuration key.Get the UI language.Get the license key.Get the link default protocol.tools.jackson.databind.JsonNodeGet mention feeds configuration.Get the placeholder text.Get the simple upload URL if configured.Get a string configuration value.tools.jackson.databind.JsonNodeGet style definitions configuration.String[]Get the table content toolbar items.String[]Get the toolbar configuration.Get the toolbar style configuration.double[]Get the UI viewport offset.booleanCheck if autosave is configured.booleanCheck if a configuration key is set.booleanCheck if config-required plugins are allowed.boolean检查是否允许私有网络地址booleanCheck if font family supports all values.booleanCheck if font size supports all values.booleanCheck if HTML support allows all elements.booleanCheck if links should add target to external links.booleanCheck if media embed includes previews in data.booleanCheck if strict plugin loading is enabled.Set custom configuration valuesetAlignment(String... options) Set alignment optionssetAutosave(int waitingTime) Set autosave configurationsetCodeBlock(String indentSequence, CKEditorConfig.CodeBlockLanguage... languages) Set code block languagessetFontFamily(boolean supportAllValues, String... families) Set font family options with support all values flagsetFontFamily(String... families) Set font family optionssetFontSize(boolean supportAllValues, String... sizes) Set font size options with support all values flagsetFontSize(String... sizes) Set font size optionssetHeading(CKEditorConfig.HeadingOption... options) Set heading optionssetHtmlSupport(boolean allowAll) Set general HTML support configurationSet image configurationsetLanguage(String language) Set UI languagesetLanguage(String uiLanguage, String contentLanguage, String[] textPartLanguages) Set complex language configuration (UI language, content language, text part languages)setLicenseKey(String licenseKey) Set license key (for premium features)Set link configurationsetMediaEmbed(boolean previewsInData) Set media embed configurationsetMention(CKEditorConfig.MentionFeed... feeds) Set mention feedssetPlaceholder(String placeholder) Set placeholder textsetSimpleUpload(String uploadUrl) Set simple upload configuration for the SimpleUploadAdapter plugin.setSimpleUpload(String uploadUrl, Map<String, String> headers) Set simple upload configuration with custom headers.setSimpleUpload(String uploadUrl, Map<String, String> headers, boolean withCredentials) Set simple upload configuration with all options.setStyle(CKEditorConfig.StyleDefinition... definitions) Set style definitions for the Style plugin.Set table configurationsetToolbar(String... items) Set toolbar itemssetToolbar(String[] items, boolean shouldNotGroupWhenFull) Set toolbar with grouping optionSet toolbar style configuration.setUiViewportOffset(double top, double right, double bottom, double left) Set UI viewport offsettools.jackson.databind.node.ObjectNodetoJson()Convert to JSON object
-
Constructor Details
-
CKEditorConfig
public CKEditorConfig()
-
-
Method Details
-
setPlaceholder
Set placeholder text -
setLanguage
Set UI language -
setLanguage
public CKEditorConfig setLanguage(String uiLanguage, String contentLanguage, String[] textPartLanguages) Set complex language configuration (UI language, content language, text part languages) -
setToolbar
Set toolbar items -
setToolbar
Set toolbar with grouping option -
setHeading
Set heading options -
setFontSize
Set font size options -
setFontSize
Set font size options with support all values flag -
setFontFamily
Set font family options -
setFontFamily
Set font family options with support all values flag -
setAlignment
Set alignment options -
setLink
Set link configuration -
setImage
Set image configuration -
setTable
Set table configuration -
setCodeBlock
public CKEditorConfig setCodeBlock(String indentSequence, CKEditorConfig.CodeBlockLanguage... languages) Set code block languages -
setMediaEmbed
Set media embed configuration -
setMention
Set mention feeds -
setSimpleUpload
Set simple upload configuration for the SimpleUploadAdapter plugin.SimpleUploadAdapter uses XMLHttpRequest to send files to your server. This is a lightweight solution when you have a simple upload endpoint.
Basic Usage
VaadinCKEditor editor = VaadinCKEditor.create() .withPreset(CKEditorPreset.STANDARD) .removePlugin(CKEditorPlugin.BASE64_UPLOAD_ADAPTER) .addPlugin(CKEditorPlugin.SIMPLE_UPLOAD_ADAPTER) .withConfig(config -> config .setSimpleUpload("https://your-server.com/api/upload") ) .build();With Authentication
.withConfig(config -> config .setSimpleUpload( "https://your-server.com/api/upload", Map.of( "Authorization", "Bearer " + jwtToken, "X-CSRF-TOKEN", csrfToken ) ) )Server Response Format
Your server must return JSON in one of these formats:
Success (single URL):
{ "url": "https://example.com/images/foo.jpg" }Success (responsive images):
{ "urls": { "default": "https://example.com/images/foo.jpg", "800": "https://example.com/images/foo-800.jpg", "1200": "https://example.com/images/foo-1200.jpg" } }Error:
{ "error": { "message": "File too large (max 5MB)" } }- Parameters:
uploadUrl- the URL to upload files to (required)- Returns:
- this config for chaining
- See Also:
-
setSimpleUpload
Set simple upload configuration with custom headers.- Parameters:
uploadUrl- the URL to upload files to (required)headers- custom HTTP headers to send with the upload request (e.g., Authorization)- Returns:
- this config for chaining
- See Also:
-
setSimpleUpload
public CKEditorConfig setSimpleUpload(String uploadUrl, Map<String, String> headers, boolean withCredentials) Set simple upload configuration with all options.Cross-Origin Requests with Credentials
Set
withCredentialstotruewhen:- Your upload endpoint is on a different domain (CORS)
- You need to send cookies or HTTP authentication
- Your server uses session-based authentication
Note: When using
withCredentials, your server must:- Set
Access-Control-Allow-Credentials: true - Set
Access-Control-Allow-Originto a specific origin (not*)
Example with Credentials
.withConfig(config -> config .setSimpleUpload( "https://api.example.com/upload", Map.of("X-CSRF-TOKEN", csrfToken), true // Enable credentials for cross-origin requests ) )Spring Boot Server Example
@PostMapping("/api/upload") @CrossOrigin(origins = "https://your-app.com", allowCredentials = "true") public Map<String, Object> upload(@RequestParam("upload") MultipartFile file) { String url = storageService.store(file); return Map.of("url", url); }- Parameters:
uploadUrl- the URL to upload files to (required)headers- custom HTTP headers to send with the upload requestwithCredentials- whether to send cookies and credentials with cross-origin requests- Returns:
- this config for chaining
-
allowPrivateNetworks
允许私有/内网地址作为上传 URL。默认情况下,出于安全考虑,禁止使用 localhost、127.0.0.1、192.168.x.x、 10.x.x.x、172.16-31.x.x 等内网地址。在开发环境中,可以启用此选项:
config.allowPrivateNetworks(true) .setSimpleUpload("/api/upload");警告:在生产环境中启用此选项可能导致 SSRF 攻击风险。
- Parameters:
allow- true 允许内网地址,false 禁止(默认)- Returns:
- this
-
isAllowPrivateNetworks
public boolean isAllowPrivateNetworks()检查是否允许私有网络地址- Returns:
- 是否允许私有网络
-
setAutosave
Set autosave configuration- Parameters:
waitingTime- 等待时间(毫秒),范围 100-60000- Throws:
IllegalArgumentException- 如果等待时间超出有效范围
-
setLicenseKey
Set license key (for premium features) -
setStyle
Set style definitions for the Style plugin. The Style plugin allows applying CSS classes to elements via a dropdown. Requires: Style plugin and GeneralHtmlSupport plugin.- Parameters:
definitions- style definitions (block and inline styles)- Returns:
- this config for chaining
-
setHtmlSupport
Set general HTML support configuration -
setToolbarStyle
Set toolbar style configuration. This enables custom styling of the CKEditor toolbar via CSS injection.Usage example:
config.setToolbarStyle(ToolbarStyle.builder() .background("#f5f5f5") .borderColor("#ddd") .borderRadius("8px") .buttonHoverBackground("rgba(0, 0, 0, 0.1)") .build());- Parameters:
style- the toolbar style configuration- Returns:
- this config for chaining
-
setUiViewportOffset
Set UI viewport offset -
set
Set custom configuration value -
getConfigs
-
toJson
public tools.jackson.databind.node.ObjectNode toJson()Convert to JSON object -
getPlaceholder
Get the placeholder text.- Returns:
- the placeholder text, or null if not set
-
getLanguage
-
getToolbar
Get the toolbar configuration.- Returns:
- array of toolbar items, or null if not set
-
hasConfig
Check if a configuration key is set.- Parameters:
key- the configuration key- Returns:
- true if the key exists
-
getString
-
getBoolean
Get a boolean configuration value.- Parameters:
key- the configuration keydefaultValue- the default value if not set- Returns:
- the boolean value, or defaultValue if not set
-
getInt
Get an integer configuration value.- Parameters:
key- the configuration keydefaultValue- the default value if not set- Returns:
- the integer value, or defaultValue if not set
-
getSimpleUploadUrl
Get the simple upload URL if configured.- Returns:
- the upload URL, or null if not configured
-
getAutosaveWaitingTime
public int getAutosaveWaitingTime()Get the autosave waiting time.- Returns:
- the autosave waiting time in milliseconds, or -1 if not configured
-
hasAutosave
public boolean hasAutosave()Check if autosave is configured.- Returns:
- true if autosave is configured
-
getLicenseKey
-
getFontSizeOptions
Get the font size options.- Returns:
- array of font size options, or null if not configured
-
isFontSizeSupportAllValues
public boolean isFontSizeSupportAllValues()Check if font size supports all values.- Returns:
- true if supportAllValues is enabled, false otherwise
-
getFontFamilyOptions
Get the font family options.- Returns:
- array of font family options, or null if not configured
-
isFontFamilySupportAllValues
public boolean isFontFamilySupportAllValues()Check if font family supports all values.- Returns:
- true if supportAllValues is enabled, false otherwise
-
getAlignmentOptions
Get the alignment options.- Returns:
- array of alignment options, or null if not configured
-
getLinkDefaultProtocol
Get the link default protocol.- Returns:
- the default protocol, or null if not configured
-
isLinkAddTargetToExternalLinks
public boolean isLinkAddTargetToExternalLinks()Check if links should add target to external links.- Returns:
- true if addTargetToExternalLinks is enabled, false otherwise
-
getImageToolbar
Get the image toolbar items.- Returns:
- array of image toolbar items, or null if not configured
-
getImageStyles
Get the image styles.- Returns:
- array of image styles, or null if not configured
-
getTableContentToolbar
Get the table content toolbar items.- Returns:
- array of table toolbar items, or null if not configured
-
getCodeBlockIndentSequence
Get the code block indent sequence.- Returns:
- the indent sequence, or null if not configured
-
isMediaEmbedPreviewsInData
public boolean isMediaEmbedPreviewsInData()Check if media embed includes previews in data.- Returns:
- true if previewsInData is enabled, false otherwise
-
isHtmlSupportAllowAll
public boolean isHtmlSupportAllowAll()Check if HTML support allows all elements.- Returns:
- true if HTML support is configured with allow all, false otherwise
-
getToolbarStyle
Get the toolbar style configuration.- Returns:
- the toolbar style, or null if not configured
-
getUiViewportOffset
public double[] getUiViewportOffset()Get the UI viewport offset.- Returns:
- array of [top, right, bottom, left] values, or null if not configured
-
getJsonNode
Get the raw JSON node for a configuration key. Use this for complex configurations that don't have dedicated getters.- Parameters:
key- the configuration key- Returns:
- the JSON node, or null if not set
-
getHeadingOptions
public tools.jackson.databind.JsonNode getHeadingOptions()Get heading options configuration.- Returns:
- the heading node for further inspection, or null if not configured
-
getMentionFeeds
public tools.jackson.databind.JsonNode getMentionFeeds()Get mention feeds configuration.- Returns:
- the mention feeds node for further inspection, or null if not configured
-
getStyleDefinitions
public tools.jackson.databind.JsonNode getStyleDefinitions()Get style definitions configuration.- Returns:
- the style definitions node for further inspection, or null if not configured
-
isStrictPluginLoading
public boolean isStrictPluginLoading()Check if strict plugin loading is enabled.- Returns:
- true if strictPluginLoading is set, false otherwise
-
isAllowConfigRequiredPlugins
public boolean isAllowConfigRequiredPlugins()Check if config-required plugins are allowed.- Returns:
- true if allowConfigRequiredPlugins is set, false otherwise
-