Class 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
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
FieldsModifier and TypeFieldDescriptionprotected org.springframework.context.ApplicationContextprotected StringMirror of thecodethe client is known to have, maintained byhandleCodeUpdate().protected Stringprotected AiCodeBlockI18nprotected boolean -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 TypeMethodDescriptionvoidvoidappendCode(@Nullable String delta) Appends a delta to the current source code without resending the whole block: the client's matchingappendCodeadds the delta to itscodeproperty.getCode()getI18n()protected voidSends 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 voidinitI18n()booleanbooleanbooleanbooleanbooleanprotected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) protected voidSchedules a client sync of the code to be run before the next client response.voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext) voidSets the source code.voidsetCodeWrapped(boolean codeWrapped) Sets the wrap state of the code.voidsetCopyActionEnabled(boolean copyActionEnabled) Enables or disables the copy action.voidsetHighlight(boolean highlight) Enables or disables syntax highlighting.voidsetI18n(AiCodeBlockI18n i18n) Sets the i18n labels for the actions.voidsetLanguage(@Nullable String language) Sets the language id.voidsetStreaming(boolean streaming) Marks the code as still streaming.voidsetWrapActionEnabled(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, whenAttachedMethods 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.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods 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, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Field Details
-
code
-
clientCode
Mirror of thecodethe client is known to have, maintained byhandleCodeUpdate(). It cannot diverge because the server originates every change (the element is a read-only view). -
pendingCodeUpdate
protected boolean pendingCodeUpdate -
i18n
-
applicationContext
protected org.springframework.context.ApplicationContext applicationContext
-
-
Constructor Details
-
AiCodeBlock
public AiCodeBlock()Creates an empty code block. -
AiCodeBlock
Creates a code block with the given source.- Parameters:
code- the source code
-
AiCodeBlock
Creates a code block with the given source and language.- Parameters:
code- the source codelanguage- the language id (e.g."java")
-
-
Method Details
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException - Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Throws:
org.springframework.beans.BeansException
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
initI18n
protected void initI18n() -
setCode
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;nullis treated as empty
-
getCode
- Returns:
- the current source code (never
null)
-
appendCode
Appends a delta to the current source code without resending the whole block: the client's matchingappendCodeadds the delta to itscodeproperty. 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 perappendCodecall) 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:
onAttachin classcom.vaadin.flow.component.Component
-
setLanguage
Sets the language id. Drives the toolbar/label and the highlighter language.- Parameters:
language- the language id;nullis treated as empty
-
getLanguage
- Returns:
- the language id (empty when unset)
-
setHighlight
public void setHighlight(boolean highlight) Enables or disables syntax highlighting.- Parameters:
highlight-trueto highlight (default),falsefor plain text
-
isHighlight
public boolean isHighlight()- Returns:
- whether syntax highlighting is enabled
-
setStreaming
public void setStreaming(boolean streaming) Marks the code as still streaming. Whiletrue, highlighting is deferred; flipping it tofalsetriggers one full highlight.- Parameters:
streaming-truewhile 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 (seesetCode(String)).- Parameters:
codeWrapped-trueto soft-wrap,falsefor 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-trueto 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-trueto show the wrap button (default)
-
isWrapActionEnabled
public boolean isWrapActionEnabled()- Returns:
- whether the wrap action is enabled
-
getI18n
- Returns:
- the i18n object, resolved from the message bundle when the component was
created and replaced by every
setI18n(AiCodeBlockI18n)since
-
setI18n
Sets the i18n labels for the actions. Updating the object after this call has no effect; callsetI18nagain to apply changes.- Parameters:
i18n- the i18n object, notnull
-