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

macOS: some commands no longer work inside iframe inside webview #65452

Closed
bpasero opened this issue Dec 20, 2018 · 29 comments
Closed

macOS: some commands no longer work inside iframe inside webview #65452

bpasero opened this issue Dec 20, 2018 · 29 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities webview Webview issues
Milestone

Comments

@bpasero
Copy link
Member

bpasero commented Dec 20, 2018

With 8a58c3e some commands no longer work when inside a webview, for example:

  • Copy
  • Paste
  • Cut
  • Undo
  • Redo
@bpasero bpasero added bug Issue identified by VS Code Team member as probable bug webview Webview issues electron-3.0.x-update labels Dec 20, 2018
@njzydark
Copy link

njzydark commented Dec 20, 2018

I ran into this problem and thought it was the reason for the plugin at first.
It’s too annoying to perform the undo operation.

@bpasero
Copy link
Member Author

bpasero commented Jan 29, 2019

While this seems to work fine for e.g. markdown preview it does not seem to work for an extension that allows to live preview. Steps:

  • install negokaz.live-server-preview
  • preview a simple HTML file that has <input> as content
  • try to Select All, Cut, Copy, Paste etc
  • 🐛 does not seem to work anymore (works fine in stable)

@bpasero bpasero reopened this Jan 29, 2019
@bpasero bpasero added the verification-found Issue verification failed label Jan 29, 2019
@mjbvz
Copy link
Collaborator

mjbvz commented Jan 29, 2019

Since we've unblocked the github pr extension and most other users of the webview api, I'm not going to try fixing this for iframe extensions in iteration

@mrmeku
Copy link

mrmeku commented Mar 12, 2019

You can try out nrlw.angular-console to reproduce the bug too. It might be helpful for whomever looks into this bug in the future

@ivankravets
Copy link

Do we have any workarounds?

Similar issue with PlatformIO IDE => platformio/platformio-vscode-ide#606

@askhogan
Copy link

@mjbvz Copy Paste for me (VS Code Insiders - latest version) doesn't work on any of the extension pages within the app. Example trying to copy configuration settings inside of a page that is describing the extension. The keybindings never even register.

@arduinotech
Copy link

Fix it please!

@ivankravets
Copy link

@mjbvz could you explain what should be done from the extension side to make it work? I've just downloaded the latest insiders build and it does not work for me:

Version: 1.43.0-insider
Commit: beaf391bc53136912e4f0bbdfb7844ad954247db
Date: 2020-02-07T05:30:08.850Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 19.3.0

I opened PIO Home, selected some library example and pressed Cmd+C and a buffer is empty. What is more, the right-click context menu is empty too.

These source code of how do we initialize WebView => https://github.com/platformio/platformio-vscode-ide/blob/develop/src/home.js#L47

Thanks!

Screen Shot 2020-02-07 at 12 09 01

@vladimiry
Copy link

Keyboard shortcuts for copy, paste, cut, undo, redo work for me in a textarea inside a webview

@mjbvz can you point me to the code change / commit that fixed the issue?

@ivankravets
Copy link

@mjbvz could you reopen this issue?

@mjbvz
Copy link
Collaborator

mjbvz commented Feb 13, 2020

Is it not working?

@ivankravets
Copy link

Yes, it does not work. Re-tested again with

Version: 1.43.0-insider
Commit: 1fbacccbc900bb59ba8a8f26a4128d48a1c97842
Date: 2020-02-13T05:38:41.855Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 19.3.0

@mjbvz
Copy link
Collaborator

mjbvz commented Feb 13, 2020

Please provide repo steps and an example extension that demonstrates this issue

@ivankravets
Copy link

Could you install https://marketplace.visualstudio.com/items?itemName=platformio.platformio-ide ? It will open PIO Home, then try to copy something from it.

@mjbvz mjbvz changed the title macOS: some commands no longer work inside the webview macOS: some commands no longer work inside iframe inside webview Feb 13, 2020
@mjbvz
Copy link
Collaborator

mjbvz commented Feb 13, 2020

Confirmed that platformIO still doesn't work

@ivankravets Your extension uses an iframe inside a webview to display all of its content. This is why the commands still don't work for PlatformIO but work fine in other webview based extensions. Since this scenario is important to you but not terribly common (or recommended) for webview extensions more broadly, please look into contributing a PR

I've update the issue title to make it more clear what is still blocked.

@mjbvz mjbvz reopened this Feb 13, 2020
@mjbvz mjbvz modified the milestones: February 2020, Backlog Feb 13, 2020
@mjbvz mjbvz added help wanted Issues identified as good community contribution opportunities and removed fixed-in-electron-7 labels Feb 13, 2020
@ivankravets
Copy link

Thanks that checked it. So, is this issue on our side? Do you have any hints on how to resolve this issue?

@mjbvz
Copy link
Collaborator

mjbvz commented Feb 14, 2020

@ivankravets The more I think the problem, the more it seems like something that individual extensions need to workaround.

To explain, here's how events such as copy are handled in webviews:

  • When a webview is focused, all keyboard events go to this webview. This means that the main VS Code window does not see these keypresses, so actions such as copy or open command palette would normally not work when the user is focused on a webview

  • To workaround this, we capture keyboard events inside the webview and rebroadcast them to the main VS Code process:

    const handleInnerKeydown = (e) => {

  • Then the main renderer process can handle these commands if needed

However, once you add an iframe inside of a webview, we run into the same problem all over again:

  • If the user is focused on the iframe in the webview, now all keypresses go to the iframe. The webview containing the iframe does not get any keyboard events

  • Which means we never can rebroadcast the keypresses back to the main VS Code renderer

To make matters worse, since the iframes used by most extension (including PlatformIO) are served up in a different origin from the webview itself, there is no way that I'm aware of for VS Code webview's to automatically add keypress listeners into these iframe. This is a dom security limitation and I'm not sure if there is any reasonable workaround.


Given that I tried and failed to fix keyboard events for PlatformIO with changes in VS Code itself, I think you should try adding a workaround in your extension itself.

Specifically, try adding code into your iframe that rebroadcasts keyboard events up to the webview so it can send then back to VS Code:

document.addEventListener('keydown', e => {
     window.parent.dispatchEvent(new KeyboardEvent('keydown', e));
})

I tested this and it works in a simple extension

@arduinotech
Copy link

arduinotech commented Feb 14, 2020

But for some reason, menu items work (edit -> paste). And since they work, it’s probably possible to somehow use the hotkey.
Or not and I don’t understand something?

@mjbvz
Copy link
Collaborator

mjbvz commented Feb 14, 2020

@arduinotech I don't see undo and redo working, even using the menus. And remember that we also want keyboard shortcuts for VS Code specific action to work when you are in a webview, such as opening the command palette. These work fine for normal webviews, but not when you are focused on an iframe inside a webview

If someone has any ideas of how to workaround this, please let me know (ideally the workaround should also work in browsers but that may be a stretch)

@tamuratak
Copy link
Contributor

I have tried rebroadcasting keyboard events on the pdf viewer of LaTeX Workshop. It works well.

Because the source of the iframe is http://localhost/... in LaTeX Workshop,
window.parent.dispatchEvent causes an error due to the same-origin policy. We have to use postMessage instead of dispatchEvent. The following are the snippets.

In the frame, we call:

document.addEventListener('keydown', e => {
    const obj = {
        altKey: e.altKey,
        code: e.code,
        ctrlKey: e.ctrlKey,
        isComposing: e.isComposing,
        key: e.key,
        location: e.location,
        metaKey: e.metaKey,
        repeat: e.repeat,
        shiftKey: e.shiftKey
    }
    window.parent.postMessage( JSON.stringify(obj), '*')
})

In the parent webview, we call:

window.addEventListener("message", (e) => {
    window.dispatchEvent(new KeyboardEvent('keydown', JSON.parse(e.data)));
}, false);

@ivankravets
Copy link

@mjbvz is it possible to pass HTTP link to WebView via src instead of using IFRAME?

Will this resolve the issue without any hooks?

tamuratak added a commit to tamuratak/LaTeX-Workshop that referenced this issue Feb 14, 2020
we have to dispatch keyboard events in the parent window.
See microsoft/vscode#65452
Close James-Yu#1406.
@mjbvz
Copy link
Collaborator

mjbvz commented Feb 15, 2020

@ivankravets Not currently. One reason is that we need to control aspects of the webview's content, such as setting the vscode css variables and the post message scripts

The ideal fix would be not to use an iframe inside the webview at all. Then other webview features, such as link handling, should also work as expected

tamuratak added a commit to tamuratak/LaTeX-Workshop that referenced this issue Feb 15, 2020
we have to dispatch keyboard events in the parent window.
See microsoft/vscode#65452
Close James-Yu#1406.
@mjbvz
Copy link
Collaborator

mjbvz commented Apr 15, 2020

I'm closing this issue because, as I explained, I don't think we can workaround it on the VS Code side.

Using iframes in webviews is not ideal, but if your extension absolutely need to use them, make sure to rebroadcast the events as described above

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities webview Webview issues
Projects
None yet
Development

No branches or pull requests