VSCode API - 'textInputFocus' 用于 webview 或自定义撤消处理
VSCode API - 'textInputFocus' for webview or custom undo handling
我正在研究 VSCode 扩展实现。
我正在为我的扩展使用 webview - 结果有类似于可视化编辑器的东西,当你可以 select 项目和编辑时。
结果我想为 webview 实现自定义 undo/redo 处理。
我有以下代码来处理 VSCode 的 'undo'/'redo' 命令:
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();
对我有用。
结果,当我从 VSCode 的菜单 select 'undo'/'redo' 时调用我的自定义 undo/redo 处理程序:
问题是当我使用 'Ctrl+Z'|'Command+Z' 快捷方式时,没有为 webview 调用 'undo' 命令。
发生这种情况是因为在默认键绑定中遵循 'when' 子句:
如果我从 'when' 子句中删除 'textInputFocus' 语句,则在使用键盘快捷键时会为 webview 调用 'undo' 命令。
关于 'when' 上下文的一些信息 - https://github.com/microsoft/vscode-docs/blob/master/docs/getstarted/keybindings.md#contexts
问题:
当我使用 webview 时,有什么方法可以将 'textInputFocus' 设置为 'true'?
替代方法可以是:
- 我可以阅读快捷键附件
- 默认 - 'vscode://defaultsettings/keybindings.json'
- Custom/Overwritten:
Windows - %APPDATA%\Code\User\keybindings.json;
MacOS - $HOME/Library/Application Support/Code/User/keybindings.json;
Linux - $HOME/.config/Code/User/keybindings.json;
- 手动附加键盘按下事件并处理事件以检测'undo'/'redo'组合键;
那应该可行,但如果我能以某种方式将 Webview 的 'textInputFocus' 设置为 'true',那么它会容易得多。
或者也许还有其他更简单的解决方案?
我找到了手动设置 'textInputFocus' 的方法:
vscode.commands.executeCommand('setContext', 'textInputFocus', true);
我正在研究 VSCode 扩展实现。 我正在为我的扩展使用 webview - 结果有类似于可视化编辑器的东西,当你可以 select 项目和编辑时。 结果我想为 webview 实现自定义 undo/redo 处理。 我有以下代码来处理 VSCode 的 'undo'/'redo' 命令:
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();
对我有用。 结果,当我从 VSCode 的菜单 select 'undo'/'redo' 时调用我的自定义 undo/redo 处理程序:
问题是当我使用 'Ctrl+Z'|'Command+Z' 快捷方式时,没有为 webview 调用 'undo' 命令。
发生这种情况是因为在默认键绑定中遵循 'when' 子句:
如果我从 'when' 子句中删除 'textInputFocus' 语句,则在使用键盘快捷键时会为 webview 调用 'undo' 命令。 关于 'when' 上下文的一些信息 - https://github.com/microsoft/vscode-docs/blob/master/docs/getstarted/keybindings.md#contexts
问题: 当我使用 webview 时,有什么方法可以将 'textInputFocus' 设置为 'true'?
替代方法可以是:
- 我可以阅读快捷键附件
- 默认 - 'vscode://defaultsettings/keybindings.json'
- Custom/Overwritten: Windows - %APPDATA%\Code\User\keybindings.json; MacOS - $HOME/Library/Application Support/Code/User/keybindings.json; Linux - $HOME/.config/Code/User/keybindings.json;
- 手动附加键盘按下事件并处理事件以检测'undo'/'redo'组合键; 那应该可行,但如果我能以某种方式将 Webview 的 'textInputFocus' 设置为 'true',那么它会容易得多。
或者也许还有其他更简单的解决方案?
我找到了手动设置 'textInputFocus' 的方法:
vscode.commands.executeCommand('setContext', 'textInputFocus', true);