-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Share chart button #7802
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
Open
emilykl
wants to merge
23
commits into
master
Choose a base branch
from
upload-to-cloud-proto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Share chart button #7802
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
42c9601
add button placeholder which shows chart JSON
emilykl 8ce5512
add icon
emilykl ad9ddfc
use existing sendChartToCloud function
emilykl 100da7b
update plot-schema
emilykl 82f5437
Update approach to use postMessage and wait for ready signal before s…
marthacryan fddd50e
Merge branch 'master' of github.com:plotly/plotly.js into upload-to-c…
marthacryan ba91b11
Create a confirmation dialog
marthacryan 717fc26
Add default URL for plotlyServerURL
marthacryan 8774b9c
Update string to indicate success
marthacryan 13b8f12
Update src/plot_api/plot_config.js
marthacryan a44f6ef
Add localization
marthacryan 836d24c
Address review
marthacryan 1f47ebd
Update default cloud URL
marthacryan 83deac2
Update src/components/modebar/cloud_confirm.js
marthacryan 852dc1f
Set default to false for showSendToCloud
marthacryan 4a71e43
Merge changes
marthacryan ff34976
Update plot-schema in dist
marthacryan e5933bd
Revert "Update plot-schema in dist"
emilykl c1b7122
update test/plot-schema.json
emilykl c28c434
Update default URL for upload to empty string until plotly cloud endp…
marthacryan da868be
Merge branch 'upload-to-cloud-proto' of github.com:plotly/plotly.js i…
marthacryan bf8971a
Update src/components/modebar/cloud_confirm.js
marthacryan ada548c
Update src/components/modebar/buttons.js
marthacryan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| 'use strict'; | ||
|
|
||
| var d3 = require('@plotly/d3'); | ||
|
|
||
| var _ = require('../../lib')._; | ||
|
|
||
| /** | ||
| * Show a styled confirmation dialog before sharing a chart with Plotly Cloud. | ||
| * | ||
| * The dialog is appended to the plot's positioning container (.svg-container) | ||
| * so it is centered over the plot rather than the whole viewport. It can be | ||
| * dismissed by clicking Cancel, clicking the backdrop, or pressing Escape. | ||
| * | ||
| * @param {DOM node} gd - the graph div, used to scope the dialog to the plot | ||
| * @param {string} serverUrl - destination shown in the dialog message | ||
| * @param {function} onConfirm - called when the user confirms the upload | ||
| */ | ||
| module.exports = function confirmCloudDialog(gd, serverUrl, onConfirm) { | ||
| var container = d3.select(gd._fullLayout._paperdiv.node()); | ||
|
|
||
| // Never stack dialogs - drop any that is already open. | ||
| container.selectAll('.plotly-cloud-dialog').remove(); | ||
|
|
||
| var overlay = container | ||
| .append('div') | ||
| .classed('plotly-cloud-dialog', true); | ||
|
|
||
| var dialog = overlay.append('div') | ||
| .classed('plotly-cloud-dialog-box', true); | ||
|
|
||
| dialog.append('div') | ||
| .classed('plotly-cloud-dialog-title', true) | ||
| .text(_(gd, 'Share with Plotly Cloud')); | ||
|
|
||
| dialog.append('div') | ||
| .classed('plotly-cloud-dialog-message', true) | ||
| .text(_(gd, 'This chart and its data will be sent to') + ' ' + serverUrl + '.'); | ||
|
|
||
| var buttons = dialog.append('div') | ||
| .classed('plotly-cloud-dialog-buttons', true); | ||
|
|
||
| function close() { | ||
| overlay.remove(); | ||
| document.removeEventListener('keydown', onKeydown); | ||
| } | ||
|
|
||
| function onKeydown(e) { | ||
| if(e.key === 'Escape' || e.keyCode === 27) close(); | ||
| } | ||
| document.addEventListener('keydown', onKeydown); | ||
|
|
||
| // Clicking the backdrop (but not the dialog box) cancels. | ||
| overlay.on('click', function() { | ||
| if(d3.event.target === overlay.node()) close(); | ||
| }); | ||
|
|
||
| buttons.append('button') | ||
| .classed('plotly-cloud-dialog-btn', true) | ||
| .classed('plotly-cloud-dialog-btn--cancel', true) | ||
| .text(_(gd, 'Cancel')) | ||
| .on('click', close); | ||
|
|
||
| buttons.append('button') | ||
| .classed('plotly-cloud-dialog-btn', true) | ||
| .classed('plotly-cloud-dialog-btn--confirm', true) | ||
| .text(_(gd, 'Share')) | ||
| .on('click', function() { | ||
| close(); | ||
| onConfirm(); | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| .plotly-cloud-dialog { | ||
| font-family: 'Open Sans', verdana, arial, sans-serif; // Because not all contexts have this specified. | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| z-index: 1001; | ||
|
|
||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
|
|
||
| background-color: rgba(0, 0, 0, 0.4); | ||
|
|
||
| .plotly-cloud-dialog-box { | ||
| box-sizing: border-box; | ||
| min-width: 300px; | ||
| max-width: 420px; | ||
| padding: 20px 24px; | ||
|
|
||
| background-color: $color-bg-light; | ||
| border: 1px solid $color-bg-darker; | ||
| border-radius: 4px; | ||
| box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25); | ||
|
|
||
| font-size: 13px; | ||
| color: #2a3f5f; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-title { | ||
| font-size: 16px; | ||
| font-weight: bold; | ||
| margin-bottom: 12px; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-message { | ||
| line-height: 1.5; | ||
| overflow-wrap: break-word; | ||
| word-wrap: break-word; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-buttons { | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| margin-top: 20px; | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-btn { | ||
| font-family: inherit; | ||
| font-size: 13px; | ||
| padding: 7px 16px; | ||
| margin-left: 8px; | ||
|
|
||
| border-radius: 3px; | ||
| border: 1px solid transparent; | ||
| cursor: pointer; | ||
|
|
||
| &:focus-visible { | ||
| outline: 2px solid $color-brand-primary; | ||
| outline-offset: 1px; | ||
| } | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-btn--cancel { | ||
| background-color: $color-bg-light; | ||
| border-color: $color-bg-darker; | ||
| color: $color-muted-text; | ||
|
|
||
| &:hover { | ||
| background-color: $color-bg-base; | ||
| } | ||
| } | ||
|
|
||
| .plotly-cloud-dialog-btn--confirm { | ||
| background-color: $color-brand-primary; | ||
| color: $color-bg-light; | ||
|
|
||
| &:hover { | ||
| background-color: $color-brand-accent; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ var formatLocale = require('d3-format').formatLocale; | |
| var isNumeric = require('fast-isnumeric'); | ||
| var b64encode = require('base64-arraybuffer'); | ||
|
|
||
| var version = require('../version').version; | ||
| var Registry = require('../registry'); | ||
| var PlotSchema = require('../plot_api/plot_schema'); | ||
| var Template = require('../plot_api/plot_template'); | ||
|
|
@@ -202,35 +203,55 @@ function positionPlayWithData(gd, container) { | |
|
|
||
| plots.sendDataToCloud = function(gd) { | ||
| var baseUrl = (window.PLOTLYENV || {}).BASE_URL || gd._context.plotlyServerURL; | ||
|
Comment on lines
204
to
205
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to pass in |
||
| if(!baseUrl) return; | ||
| if(!baseUrl) { | ||
| console.error('No destination URL provided (plotlyServerURL is not set)'); | ||
| return; | ||
| } | ||
|
|
||
| // Plotly Cloud origin, used to validate incoming messages and to target outgoing ones. | ||
| // `baseUrl` (plotlyServerURL) is the upload page that handles login and signals | ||
| // back when authentication succeeds. | ||
| var cloudOrigin; | ||
| try { | ||
| cloudOrigin = new URL(baseUrl).origin; | ||
| } catch(e) { | ||
| console.error('Invalid plotlyServerURL: ' + baseUrl); | ||
| return; | ||
| } | ||
|
|
||
| gd.emit('plotly_beforeexport'); | ||
|
|
||
| var hiddenformDiv = d3.select(gd) | ||
| .append('div') | ||
| .attr('id', 'hiddenform') | ||
| .style('display', 'none'); | ||
| // Build the request body: the chart JSON plus the plotly.js version used to | ||
| // generate it, so Cloud can host the chart with a compatible plotly.js version. | ||
| var chart = plots.graphJson(gd, false, 'keepdata', 'object'); | ||
|
marthacryan marked this conversation as resolved.
|
||
| chart.version = version; | ||
|
|
||
| // Open the Cloud login page in a new tab. We keep a reference so we can post | ||
| // the chart back to it once Cloud reports that authentication succeeded. | ||
| var cloudWindow = window.open(baseUrl, '_blank'); | ||
| if(!cloudWindow) { | ||
| console.error('Unable to open Plotly Cloud (the popup may have been blocked)'); | ||
| gd.emit('plotly_exportfail'); | ||
| return; | ||
| } | ||
|
|
||
| var handleMessage = function(event) { | ||
| // Only trust messages coming from the Cloud origin. | ||
| if(event.origin !== cloudOrigin) return; | ||
|
|
||
| var hiddenform = hiddenformDiv | ||
| .append('form') | ||
| .attr({ | ||
| action: baseUrl + '/external', | ||
| method: 'post', | ||
| target: '_blank' | ||
| }); | ||
| if(event.data && event.data.type === 'CHART_AUTH_SUCCESS') { | ||
| cloudWindow.postMessage({ | ||
| type: 'chart', | ||
| chart: chart | ||
| }, cloudOrigin); | ||
|
|
||
| var hiddenformInput = hiddenform | ||
| .append('input') | ||
| .attr({ | ||
| type: 'text', | ||
| name: 'data' | ||
| }); | ||
| window.removeEventListener('message', handleMessage); | ||
| gd.emit('plotly_afterexport'); | ||
| } | ||
| }; | ||
|
|
||
| hiddenformInput.node().value = plots.graphJson(gd, false, 'keepdata'); | ||
| hiddenform.node().submit(); | ||
| hiddenformDiv.remove(); | ||
| window.addEventListener('message', handleMessage); | ||
|
|
||
| gd.emit('plotly_afterexport'); | ||
| return false; | ||
| }; | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the URL validation steps (truthiness check and
try { new URL(baseUrl).origin;}) should happen inside avalidateURLfunction here, rather than insidePlots.sendDataToCloud(gd);, so that we don't even show the dialog at all if the URL is invalid.