Class AiCodeBlock

java.lang.Object
com.vaadin.flow.component.Component
io.jmix.aichat.component.codeblock.AiCodeBlock
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, Serializable, org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware

@Tag("jmix-ai-code-block") @JsModule("./src/ai-chat/codeblock/jmix-ai-code-block.js") @NpmPackage(value="highlight.js", version="11.12.0") public class AiCodeBlock extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.HasSize, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean
A standalone code-block component: renders one block of code with its own horizontal scroll, copy and wrap actions, an optional language label, and optional syntax highlighting. Highlighting runs client-side only when highlight is on, streaming is off, and a language is set; otherwise the code renders as plain monospace text.

Reusable on its own, and embedded by AiMessageList: the client <jmix-ai-markdown> element renders one <jmix-ai-code-block> per fenced code block of an assistant message, binding its code, language, i18n and streaming properties.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected org.springframework.context.ApplicationContext
     
    protected String
    Mirror of the code the client is known to have, maintained by handleCodeUpdate().
    protected String
     
    protected AiCodeBlockI18n
     
    protected boolean
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty code block.
    AiCodeBlock(@Nullable String code)
    Creates a code block with the given source.
    AiCodeBlock(@Nullable String code, @Nullable String language)
    Creates a code block with the given source and language.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    appendCode(@Nullable String delta)
    Appends a delta to the current source code without resending the whole block: the client's matching appendCode adds the delta to its code property.
     
     
     
    protected void
    Sends the accumulated code change to the client as a single update: an append of the concatenated diff when the new code extends what the client already has, a full property update otherwise.
    protected void
     
    boolean
     
    boolean
     
    boolean
     
    boolean
     
    boolean
     
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    protected void
    Schedules a client sync of the code to be run before the next client response.
    void
    setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
     
    void
    setCode(@Nullable String code)
    Sets the source code.
    void
    setCodeWrapped(boolean codeWrapped)
    Sets the wrap state of the code.
    void
    setCopyActionEnabled(boolean copyActionEnabled)
    Enables or disables the copy action.
    void
    setHighlight(boolean highlight)
    Enables or disables syntax highlighting.
    void
    Sets the i18n labels for the actions.
    void
    setLanguage(@Nullable String language)
    Sets the language id.
    void
    setStreaming(boolean streaming)
    Marks the code as still streaming.
    void
    setWrapActionEnabled(boolean wrapActionEnabled)
    Enables or disables the wrap action.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttached

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasSize

    bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
  • Field Details

    • code

      protected String code
    • clientCode

      protected String clientCode
      Mirror of the code the client is known to have, maintained by handleCodeUpdate(). It cannot diverge because the server originates every change (the element is a read-only view).
    • pendingCodeUpdate

      protected boolean pendingCodeUpdate
    • i18n

      protected AiCodeBlockI18n i18n
    • applicationContext

      protected org.springframework.context.ApplicationContext applicationContext
  • Constructor Details

    • AiCodeBlock

      public AiCodeBlock()
      Creates an empty code block.
    • AiCodeBlock

      public AiCodeBlock(@Nullable String code)
      Creates a code block with the given source.
      Parameters:
      code - the source code
    • AiCodeBlock

      public AiCodeBlock(@Nullable String code, @Nullable String language)
      Creates a code block with the given source and language.
      Parameters:
      code - the source code
      language - the language id (e.g. "java")
  • Method Details

    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • initI18n

      protected void initI18n()
    • setCode

      public void setCode(@Nullable String code)
      Sets the source code.

      Replacing the code (setting a value that does not extend the current one) resets the wrap state: the client element does the same for its user-toggled wrap, and the server mirrors the rule so isCodeWrapped() stays truthful. Extending the current code or populating an empty block keeps the wrap state.

      Parameters:
      code - the source code; null is treated as empty
    • getCode

      public String getCode()
      Returns:
      the current source code (never null)
    • appendCode

      public void appendCode(@Nullable String delta)
      Appends a delta to the current source code without resending the whole block: the client's matching appendCode adds the delta to its code property. Intended for streaming.

      Updates are batched: all changes made within one round trip are flushed before the client response as a single update — one concatenated append when the new code extends what the client has, or one full update otherwise (see scheduleCodeUpdate()).

      Parameters:
      delta - the text to append
    • scheduleCodeUpdate

      protected void scheduleCodeUpdate()
      Schedules a client sync of the code to be run before the next client response. Batching all changes from one round trip into a single flush-time update (instead of one invocation per appendCode call) keeps the wire payload small and removes the ordering hazard of mixing property updates and JS invocations, which the client applies in different phases of the same server message.
    • handleCodeUpdate

      protected void handleCodeUpdate()
      Sends the accumulated code change to the client as a single update: an append of the concatenated diff when the new code extends what the client already has, a full property update otherwise.
    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
    • setLanguage

      public void setLanguage(@Nullable String language)
      Sets the language id. Drives the toolbar/label and the highlighter language.
      Parameters:
      language - the language id; null is treated as empty
    • getLanguage

      public String getLanguage()
      Returns:
      the language id (empty when unset)
    • setHighlight

      public void setHighlight(boolean highlight)
      Enables or disables syntax highlighting.
      Parameters:
      highlight - true to highlight (default), false for plain text
    • isHighlight

      public boolean isHighlight()
      Returns:
      whether syntax highlighting is enabled
    • setStreaming

      public void setStreaming(boolean streaming)
      Marks the code as still streaming. While true, highlighting is deferred; flipping it to false triggers one full highlight.
      Parameters:
      streaming - true while code is still arriving
    • isStreaming

      public boolean isStreaming()
      Returns:
      whether the code is marked as streaming
    • setCodeWrapped

      public void setCodeWrapped(boolean codeWrapped)
      Sets the wrap state of the code. The state is reset when the code is replaced (see setCode(String)).
      Parameters:
      codeWrapped - true to soft-wrap, false for horizontal scroll (default)
    • isCodeWrapped

      public boolean isCodeWrapped()
      Returns:
      whether the code is soft-wrapped
    • setCopyActionEnabled

      public void setCopyActionEnabled(boolean copyActionEnabled)
      Enables or disables the copy action.
      Parameters:
      copyActionEnabled - true to show the copy button (default)
    • isCopyActionEnabled

      public boolean isCopyActionEnabled()
      Returns:
      whether the copy action is enabled
    • setWrapActionEnabled

      public void setWrapActionEnabled(boolean wrapActionEnabled)
      Enables or disables the wrap action.
      Parameters:
      wrapActionEnabled - true to show the wrap button (default)
    • isWrapActionEnabled

      public boolean isWrapActionEnabled()
      Returns:
      whether the wrap action is enabled
    • getI18n

      public AiCodeBlockI18n getI18n()
      Returns:
      the i18n object, resolved from the message bundle when the component was created and replaced by every setI18n(AiCodeBlockI18n) since
    • setI18n

      public void setI18n(AiCodeBlockI18n i18n)
      Sets the i18n labels for the actions. Updating the object after this call has no effect; call setI18n again to apply changes.
      Parameters:
      i18n - the i18n object, not null