From 6dce5b0da289ab148c85ed534f4df18b856933a4 Mon Sep 17 00:00:00 2001 From: Callum McIntyre Date: Tue, 20 Jul 2021 23:08:35 +0100 Subject: [PATCH] Add embed option exportDataSet If set, this is used to populate the data when opening in Vega editor or viewing Vega source --- src/embed.ts | 21 ++++++++++++-- test-changeset.html | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 test-changeset.html diff --git a/src/embed.ts b/src/embed.ts index 1f95fdb9..6f243bc7 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -88,6 +88,7 @@ export interface EmbedOptions { timeFormatLocale?: Record; ast?: boolean; viewClass?: typeof View; + exportDataSet?: string; } const NAMES: {[key in Mode]: string} = { @@ -468,7 +469,15 @@ async function _embed( viewSourceLink.text = i18n.SOURCE_ACTION; viewSourceLink.href = '#'; viewSourceLink.addEventListener('click', function (this, e) { - viewSource(stringify(spec), opts.sourceHeader ?? '', opts.sourceFooter ?? '', mode); + const exportSpec = spec; + if (opts.exportDataSet !== undefined) { + exportSpec.data = { + // We convert from JSON and back to remove the un-serialisable Symbol from view.data + values: view.data(opts.exportDataSet).map((d) => JSON.parse(JSON.stringify(d))), + }; + } + + viewSource(stringify(exportSpec), opts.sourceHeader ?? '', opts.sourceFooter ?? '', mode); e.preventDefault(); }); @@ -497,11 +506,19 @@ async function _embed( editorLink.text = i18n.EDITOR_ACTION; editorLink.href = '#'; editorLink.addEventListener('click', function (this, e) { + const exportSpec = spec; + if (opts.exportDataSet !== undefined) { + exportSpec.data = { + // We convert from JSON and back to remove the un-serialisable Symbol from view.data + values: view.data(opts.exportDataSet).map((d) => JSON.parse(JSON.stringify(d))), + }; + } + post(window, editorUrl, { config: config as Config, mode, renderer, - spec: stringify(spec), + spec: stringify(exportSpec), }); e.preventDefault(); }); diff --git a/test-changeset.html b/test-changeset.html new file mode 100644 index 00000000..d8ce68c3 --- /dev/null +++ b/test-changeset.html @@ -0,0 +1,71 @@ + + + + + + Vega-Embed for Vega-Lite + + + + + + +

Template for Embedding Vega-Lite Visualization

+
+ + + +