Skip to content
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

Disable arrow keys if text area is empty #352

Merged
merged 7 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.31.0",
org.apache.commons.logging;bundle-version="1.2.0",
slf4j.api;bundle-version="2.0.13",
org.apache.commons.lang3;bundle-version="3.14.0"
Bundle-Classpath: target/classes/,
Bundle-Classpath: .,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It automatically updated I think.

target/dependency/annotations-2.28.26.jar,
target/dependency/apache-client-2.28.26.jar,
target/dependency/auth-2.28.26.jar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package software.aws.toolkits.eclipse.amazonq.views;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.util.List;
import java.util.Optional;

Expand All @@ -26,6 +28,8 @@
import software.aws.toolkits.eclipse.amazonq.lsp.model.QuickActionsCommandGroup;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
import software.aws.toolkits.eclipse.amazonq.util.ObjectMapperFactory;
import software.aws.toolkits.eclipse.amazonq.util.PluginPlatform;
import software.aws.toolkits.eclipse.amazonq.util.PluginUtils;
import software.aws.toolkits.eclipse.amazonq.util.ThreadingUtils;
import software.aws.toolkits.eclipse.amazonq.views.actions.AmazonQCommonActions;

Expand Down Expand Up @@ -91,6 +95,7 @@ public void completed(final ProgressEvent event) {
amazonQCommonActions = getAmazonQCommonActions();

chatCommunicationManager.setChatUiRequestListener(this);

new BrowserFunction(browser, "ideCommand") {
@Override
public Object function(final Object[] arguments) {
Expand All @@ -101,6 +106,24 @@ public Object function(final Object[] arguments) {
}
};

new BrowserFunction(browser, "isMacOs") {
@Override
public Object function(final Object[] arguments) {
return Boolean.TRUE.equals(PluginUtils.getPlatform() == PluginPlatform.MAC);
}
};

new BrowserFunction(browser, "copyToClipboard") {
@Override
public Object function(final Object[] arguments) {
if (arguments.length > 0 && arguments[0] instanceof String) {
StringSelection stringSelection = new StringSelection((String) arguments[0]);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
return null;
}
};

// Inject chat theme after mynah-ui has loaded
browser.addProgressListener(new ProgressAdapter() {
@Override
Expand Down Expand Up @@ -210,6 +233,17 @@ private String generateCss() {
mask-position: center;
scale: 60%;
}
.mynah-button.mynah-button-secondary.fill-state-always.code-snippet-close-button
.mynah-ui-clickable-item
.mynah-ui-icon-cancel {
-webkit-mask-size: 187.5% !important;
mask-size: 187.5% !important;
mask-position: center;
aspect-ratio: 1/1;
width: 15px;
height: 15px;
scale: 50%
}
.mynah-ui-icon-tabs {
-webkit-mask-size: 102% !important;
mask-size: 102% !important;
Expand Down Expand Up @@ -245,9 +279,17 @@ private String generateJS(final String jsEntrypoint) {
}

window.addEventListener('load', init);

%s

%s

%s

</script>
""", jsEntrypoint, getWaitFunction(), chatQuickActionConfig,
"true".equals(disclaimerAcknowledged));
"true".equals(disclaimerAcknowledged), getArrowKeyBlockingFunction(),
getSelectAllAndCopySupportFunctions(), getPreventEmptyPopupFunction());
}

/*
Expand All @@ -271,6 +313,118 @@ private String serializeQuickActionCommands(final List<QuickActionsCommandGroup>
}
}

private String getArrowKeyBlockingFunction() {
return """
window.addEventListener('load', () => {
const textarea = document.querySelector('textarea.mynah-chat-prompt-input');
if (textarea) {
textarea.addEventListener('keydown', (event) => {
const cursorPosition = textarea.selectionStart;
const hasText = textarea.value.length > 0;

// block arrow keys on empty text area
switch (event.key) {
case 'ArrowLeft':
if (!hasText || cursorPosition === 0) {
event.preventDefault();
event.stopPropagation();
}
break;

case 'ArrowRight':
if (!hasText || cursorPosition === textarea.value.length) {
event.preventDefault();
event.stopPropagation();
}
break;
}
});
}
});
""";
}

private String getSelectAllAndCopySupportFunctions() {
return """
window.addEventListener('load', () => {
const textarea = document.querySelector('textarea.mynah-chat-prompt-input');
if (textarea) {
textarea.addEventListener("keydown", (event) => {
if (((isMacOs() && event.metaKey) || (!isMacOs() && event.ctrlKey))
&& event.key === 'a') {
textarea.select();
event.preventDefault();
event.stopPropagation();
}
});
}
});

window.addEventListener('load', () => {
const textarea = document.querySelector('textarea.mynah-chat-prompt-input');
if (textarea) {
textarea.addEventListener("keydown", (event) => {
if (((isMacOs() && event.metaKey) || (!isMacOs() && event.ctrlKey))
&& event.key === 'c') {
copyToClipboard(textarea.value);
event.preventDefault();
event.stopPropagation();
}
});
}
});
""";
}

private String getPreventEmptyPopupFunction() {
String selector = ".mynah-button" + ".mynah-button-secondary.mynah-button-border" + ".fill-state-always"
+ ".mynah-chat-item-followup-question-option" + ".mynah-ui-clickable-item";

return """
const observer = new MutationObserver((mutations) => {
try {
const selector = '%s';

mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) { // Check if it's an element node
// Check for direct match
if (node.matches(selector)) {
attachEventListeners(node);
}
// Check for nested matches
const buttons = node.querySelectorAll();
buttons.forEach(attachEventListeners);
}
});
});
} catch (error) {
console.error('Error in mutation observer:', error);
}
});
function attachEventListeners(element) {
if (!element || element.dataset.hasListener) return; // Prevent duplicate listeners
element.addEventListener('mouseover', function(event) {
const textSpan = this.querySelector('span.mynah-button-label');
if (textSpan && textSpan.scrollWidth <= textSpan.offsetWidth) {
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
}
}, true);
element.dataset.hasListener = 'true';
}
try {
observer.observe(document.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error('Error starting observer:', error);
}
""".formatted(selector);
}

@Override
public final void onSendToChatUi(final String message) {
String script = "window.postMessage(" + message + ");";
Expand Down