Class CKEditorConfig

java.lang.Object
com.wontlost.ckeditor.CKEditorConfig

public class CKEditorConfig extends Object
CKEditor configuration class. Used to build JSON configuration passed to CKEditor.
  • Constructor Details

    • CKEditorConfig

      public CKEditorConfig()
  • Method Details

    • setPlaceholder

      public CKEditorConfig setPlaceholder(String placeholder)
      Set placeholder text
    • setLanguage

      public CKEditorConfig setLanguage(String language)
      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

      public CKEditorConfig setToolbar(String... items)
      Set toolbar items
    • setToolbar

      public CKEditorConfig setToolbar(String[] items, boolean shouldNotGroupWhenFull)
      Set toolbar with grouping option
    • setHeading

      public CKEditorConfig setHeading(CKEditorConfig.HeadingOption... options)
      Set heading options
    • setFontSize

      public CKEditorConfig setFontSize(String... sizes)
      Set font size options
    • setFontSize

      public CKEditorConfig setFontSize(boolean supportAllValues, String... sizes)
      Set font size options with support all values flag
    • setFontFamily

      public CKEditorConfig setFontFamily(String... families)
      Set font family options
    • setFontFamily

      public CKEditorConfig setFontFamily(boolean supportAllValues, String... families)
      Set font family options with support all values flag
    • setAlignment

      public CKEditorConfig setAlignment(String... options)
      Set alignment options
    • setLink

      public CKEditorConfig setLink(String defaultProtocol, boolean addTargetToExternalLinks)
      Set link configuration
    • setImage

      public CKEditorConfig setImage(String[] toolbar, String[] styles)
      Set image configuration
    • setTable

      public CKEditorConfig setTable(String[] contentToolbar)
      Set table configuration
    • setCodeBlock

      public CKEditorConfig setCodeBlock(String indentSequence, CKEditorConfig.CodeBlockLanguage... languages)
      Set code block languages
    • setMediaEmbed

      public CKEditorConfig setMediaEmbed(boolean previewsInData)
      Set media embed configuration
    • setMention

      public CKEditorConfig setMention(CKEditorConfig.MentionFeed... feeds)
      Set mention feeds
    • setSimpleUpload

      public CKEditorConfig setSimpleUpload(String uploadUrl)
      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

      public CKEditorConfig setSimpleUpload(String uploadUrl, Map<String,String> headers)
      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 withCredentials to true when:

      • 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-Origin to 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 request
      withCredentials - whether to send cookies and credentials with cross-origin requests
      Returns:
      this config for chaining
    • allowPrivateNetworks

      public CKEditorConfig allowPrivateNetworks(boolean allow)
      Allow private/internal network addresses as upload URLs.

      By default, for security reasons, localhost, 127.0.0.1, 192.168.x.x, 10.x.x.x, 172.16-31.x.x and other internal addresses are blocked. In development environments, you can enable this option:

      config.allowPrivateNetworks(true)
            .setSimpleUpload("/api/upload");
      

      Warning: Enabling this option in production may expose SSRF attack risks.

      Parameters:
      allow - true to allow internal addresses, false to block (default)
      Returns:
      this
    • isAllowPrivateNetworks

      public boolean isAllowPrivateNetworks()
      Check whether private network addresses are allowed.
      Returns:
      true if private networks are allowed
    • setAutosave

      public CKEditorConfig setAutosave(int waitingTime)
      Set autosave configuration
      Parameters:
      waitingTime - waiting time in milliseconds, range 100-60000
      Throws:
      IllegalArgumentException - if waiting time is outside the valid range
    • setLicenseKey

      public CKEditorConfig setLicenseKey(String licenseKey)
      Set license key (for premium features)
    • setStyle

      public CKEditorConfig setStyle(CKEditorConfig.StyleDefinition... definitions)
      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

      public CKEditorConfig setHtmlSupport(boolean allowAll)
      Set general HTML support configuration
    • setToolbarStyle

      public CKEditorConfig setToolbarStyle(CKEditorConfig.ToolbarStyle style)
      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

      public CKEditorConfig setUiViewportOffset(double top, double right, double bottom, double left)
      Set UI viewport offset
    • set

      public CKEditorConfig set(String key, tools.jackson.databind.JsonNode value)
      Set custom configuration value
    • getConfigs

      public Map<String, tools.jackson.databind.JsonNode> getConfigs()
      Get configuration map
    • toJson

      public tools.jackson.databind.node.ObjectNode toJson()
      Convert to JSON object
    • getPlaceholder

      public String getPlaceholder()
      Get the placeholder text.
      Returns:
      the placeholder text, or null if not set
    • getLanguage

      public String getLanguage()
      Get the UI language.
      Returns:
      the language code, or null if not set
    • getToolbar

      public String[] getToolbar()
      Get the toolbar configuration.
      Returns:
      array of toolbar items, or null if not set
    • hasConfig

      public boolean hasConfig(String key)
      Check if a configuration key is set.
      Parameters:
      key - the configuration key
      Returns:
      true if the key exists
    • getString

      public String getString(String key)
      Get a string configuration value.
      Parameters:
      key - the configuration key
      Returns:
      the string value, or null if not set or not a string
    • getBoolean

      public boolean getBoolean(String key, boolean defaultValue)
      Get a boolean configuration value.
      Parameters:
      key - the configuration key
      defaultValue - the default value if not set
      Returns:
      the boolean value, or defaultValue if not set
    • getInt

      public int getInt(String key, int defaultValue)
      Get an integer configuration value.
      Parameters:
      key - the configuration key
      defaultValue - the default value if not set
      Returns:
      the integer value, or defaultValue if not set
    • getSimpleUploadUrl

      public String 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

      public String getLicenseKey()
      Get the license key.
      Returns:
      the license key, or null if not set
    • getFontSizeOptions

      public String[] 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

      public String[] 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

      public String[] getAlignmentOptions()
      Get the alignment options.
      Returns:
      array of alignment options, or null if not configured
    • getLinkDefaultProtocol

      public String 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

      public String[] getImageToolbar()
      Get the image toolbar items.
      Returns:
      array of image toolbar items, or null if not configured
    • getImageStyles

      public String[] getImageStyles()
      Get the image styles.
      Returns:
      array of image styles, or null if not configured
    • getTableContentToolbar

      public String[] getTableContentToolbar()
      Get the table content toolbar items.
      Returns:
      array of table toolbar items, or null if not configured
    • getCodeBlockIndentSequence

      public String 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

      public CKEditorConfig.ToolbarStyle 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

      public tools.jackson.databind.JsonNode getJsonNode(String key)
      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