From 4a19917274eb2758ea64c3260c8676910429bf12 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Wed, 14 May 2025 16:14:56 -0300 Subject: [PATCH 1/2] build: set version to 3.2.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5dbf841..a9cffd2 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.flowingcode.addons xterm-console - 3.1.1-SNAPSHOT + 3.2.0-SNAPSHOT XTerm Console Addon Integration of xterm.js for Vaadin Flow From d6ad8cb8cdd13a15933eba533817e7b061c9ce9d Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Wed, 14 May 2025 16:16:18 -0300 Subject: [PATCH 2/2] feat: add support for named terminal add-ons --- .../addons/xterm/ClientTerminalAddon.java | 106 ++++++++++++++++++ .../addons/xterm/PreserveStateAddon.java | 7 +- .../vaadin/addons/xterm/TerminalAddon.java | 51 +++++++++ .../vaadin/addons/xterm/XTermBase.java | 47 +++++++- .../frontend/fc-xterm/xterm-element.ts | 2 + 5 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/flowingcode/vaadin/addons/xterm/ClientTerminalAddon.java create mode 100644 src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalAddon.java diff --git a/src/main/java/com/flowingcode/vaadin/addons/xterm/ClientTerminalAddon.java b/src/main/java/com/flowingcode/vaadin/addons/xterm/ClientTerminalAddon.java new file mode 100644 index 0000000..51b1f37 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/xterm/ClientTerminalAddon.java @@ -0,0 +1,106 @@ +/*- + * #%L + * XTerm Console Addon + * %% + * Copyright (C) 2020 - 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.xterm; + +import com.vaadin.flow.dom.Element; +import com.vaadin.flow.internal.JsonCodec; +import elemental.json.Json; +import elemental.json.JsonArray; +import java.io.Serializable; + +/** + * Represents an abstract base class for server-side terminal add-ons that have a corresponding + * client-side (JavaScript) component or require interaction with the client-side terminal + * environment. It extends {@link TerminalAddon} and specializes its use for client-aware + * operations. + * + * @author Javier Godoy / Flowing Code S.A. + */ +@SuppressWarnings("serial") +public abstract class ClientTerminalAddon extends TerminalAddon { + + private final XTermBase xterm; + + /** + * Constructs a new {@code ClientTerminalAddon} and associates it with the specified + * {@link XTermBase} instance. + *

+ * This constructor ensures the add-on is registered with the terminal and verifies that the + * add-on's name, as returned by {@link #getName()}, is not {@code null}. A non-null name is + * required for client-side add-ons to be uniquely identified and targeted for JavaScript + * execution. + *

+ * + * @param xterm the {@link XTermBase} instance this add-on will be attached to. Must not be + * {@code null}. + * @throws NullPointerException if {@code xterm} is {@code null} + * @throws IllegalStateException if {@link #getName()} returns {@code null} immediately after + * superclass construction. This check relies on {@code getName()} being a static value. + */ + protected ClientTerminalAddon(XTermBase xterm) { + super(xterm); + this.xterm = xterm; + if (getName() == null) { + throw new IllegalStateException("getName() must return a non-null value"); + } + } + + /** + * The xterm instance that this add-on is associated with. + */ + protected XTermBase getXterm() { + return xterm; + } + + /** + * Retrieves the unique name of this client-side add-on. + *

+ * This name is used by {@link #executeJs(String, Serializable...)} to target the corresponding + * JavaScript object on the client (i.e., {@code this.addons[name]} within the client-side + * terminal's scope). The name effectively acts as a key in a client-side add-ons collection + * managed by the terminal. + *

+ * + * @return the unique, non-null string identifier for the client-side counterpart of this add-on. + * Subclasses must implement this to provide a name for add-on-specific JavaScript + * execution. + */ + protected abstract String getName(); + + /** + * Executes a JavaScript {@code expression} in the context of this add-on, with the specified + * {@code parameters}. + * + * @see #getName() + * @see Element#executeJs(String, Serializable...) + */ + protected final void executeJs(String expression, Serializable... parameters) { + String name = getName(); + + JsonArray args = Json.createArray(); + for (int i = 0; i < parameters.length; i++) { + args.set(i, JsonCodec.encodeWithTypeInfo(parameters[i])); + } + + expression = expression.replaceAll("\\$(\\d+)", "\\$1[$1]"); + xterm.executeJs("(function(){" + expression + "}).apply(this.addons[$0],$1);", name, args); + } + +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/xterm/PreserveStateAddon.java b/src/main/java/com/flowingcode/vaadin/addons/xterm/PreserveStateAddon.java index 753e5fa..a2d0b72 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/xterm/PreserveStateAddon.java +++ b/src/main/java/com/flowingcode/vaadin/addons/xterm/PreserveStateAddon.java @@ -2,7 +2,7 @@ * #%L * XTerm Console Addon * %% - * Copyright (C) 2020 - 2023 Flowing Code + * Copyright (C) 2020 - 2025 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,9 @@ * addon.writePrompt(); * */ -public class PreserveStateAddon implements ITerminal, ITerminalOptions { +public class PreserveStateAddon extends TerminalAddon + implements ITerminal, ITerminalOptions { + /** * The xterm to delegate all calls to. */ @@ -80,6 +82,7 @@ public class PreserveStateAddon implements ITerminal, ITerminalOptions { private final ITerminalOptions optionsDelegate; public PreserveStateAddon(XTerm xterm) { + super(xterm); this.xterm = Objects.requireNonNull(xterm); optionsMemoizer = new StateMemoizer(xterm, ITerminalOptions.class); optionsDelegate = (ITerminalOptions) optionsMemoizer.getProxy(); diff --git a/src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalAddon.java b/src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalAddon.java new file mode 100644 index 0000000..01e608d --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalAddon.java @@ -0,0 +1,51 @@ +/*- + * #%L + * XTerm Console Addon + * %% + * Copyright (C) 2020 - 2025 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.xterm; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Represents an abstract base class for server-side add-ons designed to extend or modify the + * functionality of an {@link XTermBase} terminal instance. + *

+ * Concrete add-on implementations should subclass this class to provide specific features. Each + * add-on is tightly coupled with a specific {@code XTermBase} instance, allowing it to interact + * with and enhance that terminal. + *

+ * + * @author Javier Godoy / Flowing Code S.A. + */ +@SuppressWarnings("serial") +public abstract class TerminalAddon implements Serializable { + + /** + * Constructs a new {@code TerminalAddon} and associates it with the provided {@link XTermBase} + * instance. + * + * @param xterm the {@code XTermBase} instance to which this add-on will be attached + * @throws NullPointerException if the provided {@code xterm} is {@code null} + */ + protected TerminalAddon(XTermBase xterm) { + Objects.requireNonNull(xterm); + xterm.registerServerSideAddon(this); + } + +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/xterm/XTermBase.java b/src/main/java/com/flowingcode/vaadin/addons/xterm/XTermBase.java index 92542ca..de8ec8e 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/xterm/XTermBase.java +++ b/src/main/java/com/flowingcode/vaadin/addons/xterm/XTermBase.java @@ -2,7 +2,7 @@ * #%L * XTerm Console Addon * %% - * Copyright (C) 2020 - 2023 Flowing Code + * Copyright (C) 2020 - 2025 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,7 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; @@ -69,6 +70,8 @@ public abstract class XTermBase extends Component private List deferredCommands; + private final List addons = new ArrayList<>(); + private class ProxyInvocationHandler implements InvocationHandler, Serializable { @Override @@ -281,4 +284,46 @@ private Registration addCustomKeyListener( public void setEnabled(boolean enabled) { HasEnabled.super.setEnabled(enabled); } + + /** + * Retrieves a registered server-side add-on instance of a specific type. + *

+ * Example usage: + *

+ * + *
{@code
+   * MySpecificAddon addon = terminal.getAddon(MySpecificAddon.class);
+   * if (addon != null) {
+   *   addon.doSomethingSpecific();
+   * }
+   * }
+ * + * @param the type of the add-on to retrieve. This is inferred from the {@code clazz} + * parameter. + * @param clazz the {@code Class} object representing the type of the add-on to retrieve. Must not + * be {@code null}. + * @return the registered add-on instance that is of the specified {@code Class}, or + * {@code null} if no such add-on is found + * @throws NullPointerException if {@code clazz} is {@code null} + */ + public T getAddon(Class clazz) { + return addons.stream().filter(clazz::isInstance).map(clazz::cast).findFirst().orElse(null); + } + + /** + * Registers a server-side add-on with this terminal instance. This method is called by the add-on + * itself during its construction. + * + * @param addon the add-on to register. Must not be {@code null}. + * @throws NullPointerException if {@code addon} is {@code null} + * @throws IllegalStateException if an add-on of the same class as the provided {@code addon} is + * already registered with this terminal instance + */ + final void registerServerSideAddon(T addon) { + if (getAddon(addon.getClass()) != null) { + throw new IllegalStateException("Addon already registered: " + addon.getClass().getName()); + } + addons.add(addon); + } + } diff --git a/src/main/resources/META-INF/frontend/fc-xterm/xterm-element.ts b/src/main/resources/META-INF/frontend/fc-xterm/xterm-element.ts index 95e786c..a9d5d16 100644 --- a/src/main/resources/META-INF/frontend/fc-xterm/xterm-element.ts +++ b/src/main/resources/META-INF/frontend/fc-xterm/xterm-element.ts @@ -161,6 +161,8 @@ export class XTermElement extends LitElement implements TerminalMixin { bellStyle: 'none' | 'sound' customKeyEventHandlers: CustomKeyEventHandlerRegistry; + + addons : Object = {}; render(): TemplateResult { return html`