-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add support for named terminal add-ons #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/flowingcode/vaadin/addons/xterm/ClientTerminalAddon.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* <p> | ||
* 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. | ||
* </p> | ||
* | ||
* @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. | ||
* <p> | ||
* 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. | ||
* </p> | ||
* | ||
* @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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalAddon.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* <p> | ||
* 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. | ||
* </p> | ||
* | ||
* @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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.