Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 98994

VSCode API - 'textInputFocus' for webview or custom undo handling

$
0
0

I am working on VSCode extension implementation. I am using webview for my extension - in result there is something similar to visual editor, when you can select items and edit. In result I want to implement custom undo/redo handling for webview. I have following code to handle VSCode's 'undo'/'redo' commands:

let undoCommand: Disposable;
let redoCommand: Disposable;
const registerCommands = () => {
    undoCommand = commands.registerCommand('undo', async (args) => {
        // Call custom undo handler
        triggerCustomUndo(appJsonFilePath, extensionWebView.webview);
        // Execute default undo handler
        return commands.executeCommand('default:undo', args);
    });
    redoCommand = commands.registerCommand('redo', async (args) => {
        // Call custom redo handler
        triggerCustomRedo(appJsonFilePath, extensionWebView.webview);
        // Execute default redo handler
        return commands.executeCommand('default:redo', args);
    });
};

extensionWebView.onDidChangeViewState((action: WebviewPanelOnDidChangeViewStateEvent) => {
    if (!action.webviewPanel.visible || !action.webviewPanel.active) {
        undoCommand.dispose();
        redoCommand.dispose();
    } else {
        registerCommands();
    }
});
registerCommands();

It works for me. In result my custom undo/redo handler is called when I select 'undo'/'redo' from VSCode's menu:

Menu image

Problem is that when I am using 'Ctrl+Z'|'Command+Z' shortcut, then 'undo' command is not called for webview.

This happens because of following 'when' clause in default keybindings:

When clause from default settings

If I remove 'textInputFocus' statement from 'when' clause, then 'undo' command is called for webview when keyboard shortcut is used. Some information regarding 'when' contexts - https://github.com/microsoft/vscode-docs/blob/master/docs/getstarted/keybindings.md#contexts

Question: Is there a way how I can set 'textInputFocus' to 'true', when I am using webview?

Alternative way could be:

  • I can read keybinding attachments
    1. Default - 'vscode://defaultsettings/keybindings.json'
    2. Custom/Overwritten: Windows - %APPDATA%\Code\User\keybindings.json; MacOS - $HOME/Library/Application Support/Code/User/keybindings.json; Linux - $HOME/.config/Code/User/keybindings.json;
  • Attach to keyboard key down event manually and handle event to detect 'undo'/'redo' key combination; That should work, but if I could somehow set 'textInputFocus' to 'true' for webview then it would be much easier.

Or maybe there is other simpler solution available?


Viewing all articles
Browse latest Browse all 98994

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>