Skip to content

Add command to post a message to an html preview document - #18422

Closed
Matt Bierner (mjbvz) wants to merge 2 commits into
microsoft:masterfrom
mjbvz:add-send-message-html-preview-command
Closed

Matt Bierner (mjbvz) wants to merge 2 commits into
microsoft:masterfrom
mjbvz:add-send-message-html-preview-command

Conversation

@mjbvz

@mjbvz Matt Bierner (mjbvz) commented Jan 11, 2017 •

Copy link
Copy Markdown
Contributor

Bug
There is currently no easy way to communicate from the editor to an html preview document after the preview has been created.

Fix
Adds a command called vscode.htmlPreview.postMessage to post a message to a visible html preview. This message will only be posted if the target preview is visible (meaning this is mainly for side by side editor use cases).

Inside the preview, the event is recieved using the standard dom event:

window.addEventListener('message', event => {
	console.log(event.data);
	...
}, false);

Matt Bierner added 2 commits January 11, 2017 14:05
**Bug**
There is currently no easy way to communicate with an html preview document after the preview has been created.

**Fix**
Adds a command called `vscode.htmlPreview.postMessage` to post a message to a visible html preview. This message will only be posted if the target preview is visible.

Inside the preview, the event is recieved using the standard dom event:
.filter(c => c instanceof HtmlPreviewPart)
.map(e => e as HtmlPreviewPart)
.filter(e => e.model.uri.scheme == uri.scheme && e.model.uri.path === uri.path);
if (activePreview.length) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You may want to highlight in the description or doc that only the first visible matched htmlPreview document can get the message.

@jrieken

Copy link
Copy Markdown
Contributor

There is currently no easy way to communicate from the editor to an html preview document after the preview has been created.

TBH that's the idea of the preview command. It's a fire and forget command to preview a string as html, not a PreviewPart that you can interact with. I am against adding more commands around this.

It seems to me we are trying improve Markdown but need new API for each feature. So we can either add more APIs for this (special) case or we simply move Markdown back into the core.

@siegebell

Copy link
Copy Markdown

I've implemented some generic code to provide a similar feature here. It uses websockets to send & recieve messages. Maybe it should be spun off into a standalone library via npm or as an exported extension API?

Supported functions:

  • eval: run arbitrary javascript in the hosted html
  • setSourceHTML: load the provided html
  • setSourceUri: navigate to the provided url
  • goto: scroll to the given anchor (and optionally highlight the anchor)
  • send: post a message to the document
  • onData (event): called when the hosted document posts a message
  • provideSource: return an html document or uri in case vscode wants to reload the document

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants