Skip to content

An open source alternative to Tableau. Embeddable visual analytic

fix: export pygwalker code

ObservedObserver039735fd2d18
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index 5d074828..6f23463a 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -23,6 +23,7 @@ import { guardDataKeys } from "./utils/dataPrep";
import SegmentNav from "./segments/segmentNav";
import DatasetConfig from "./dataSource/datasetConfig";
import { useCurrentMediaTheme } from "./utils/media";
+import CodeExport from "./components/codeExport";
export interface IGWProps {
dataSource?: IRow[];
@@ -140,6 +141,7 @@ const App = observer<IGWProps>(function App (props) {
{segmentKey === ISegmentKey.vis && (
<div style={{ marginTop: "0em", borderTop: "none" }} className="m-4 p-4 border border-gray-200 dark:border-gray-700">
<VisualSettings rendererHandler={rendererRef} darkModePreference={dark} />
+ <CodeExport />
<div className="md:grid md:grid-cols-12 xl:grid-cols-6">
<div className="md:col-span-3 xl:col-span-1">
<DatasetFields />
diff --git a/packages/graphic-walker/src/components/codeExport/index.tsx b/packages/graphic-walker/src/components/codeExport/index.tsx
new file mode 100644
index 00000000..c6d25aaf
--- /dev/null
+++ b/packages/graphic-walker/src/components/codeExport/index.tsx
@@ -0,0 +1,114 @@
+import React, { useEffect, useState } from "react";
+import Modal from "../modal";
+import { observer } from "mobx-react-lite";
+import { useGlobalStore } from "../../store";
+import DefaultButton from "../button/default";
+import PrimaryButton from "../button/primary";
+import { useTranslation } from "react-i18next";
+import DefaultTab, { ITabOption } from "../tabs/defaultTab";
+
+const syntaxHighlight = (json: any) => {
+ if (typeof json != "string") {
+ json = JSON.stringify(json, undefined, 4);
+ }
+ json = json
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;")
+ .replace(/\n/g, "<br>")
+ .replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;")
+ .replace(/\s/g, "&nbsp;&nbsp;");
+ return json.replace(
+ /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
+ function (match) {
+ var cls = "text-sky-500"; // number
+ if (/^"/.test(match)) {
+ if (/:$/.test(match)) {
+ cls = "text-purple-500"; // key
+ } else {
+ cls = "text-emerald-500"; // string
+ }
+ } else if (/true|false/.test(match)) {
+ cls = "text-blue-500";
+ } else if (/null/.test(match)) {
+ cls = "text-sky-500";
+ }
+ return '<span class="' + cls + '">' + match + "</span>";
+ }
+ );
+};
+
+const CodeExport: React.FC = observer((props) => {
+ const { commonStore, vizStore } = useGlobalStore();
+ const { showCodeExportPanel } = commonStore;
+ const { t } = useTranslation();
+ const [tabKey, setTabKey] = useState<string>("vega");
+ const [code, setCode] = useState<any>("");
+
+ const specTabs: ITabOption[] = [
+ {
+ key: "pygwalker",
+ label: "PyGWalker",
+ },
+ {
+ key: "vega-lite",
+ label: "Vega-Lite",
+ disabled: true
+ },
+ ];
+
+ useEffect(() => {
+ if (showCodeExportPanel) {
+ if (tabKey === "pygwalker") {
+ const res = vizStore.exportViewSpec();
+ setCode(res);
+ } else {
+ setCode("vega code");
+ }
+ }
+ }, [tabKey, showCodeExportPanel]);
+ return (
+ <Modal
+ show={showCodeExportPanel}
+ onClose={() => {
+ commonStore.setShowCodeExportPanel(false);
+ }}
+ >
+ <div>
+ <h1>Code Export</h1>
+ <DefaultTab
+ tabs={specTabs}
+ selectedKey={tabKey}
+ onSelected={(k) => {
+ setTabKey(k as string);
+ }}
+ />
+ {tabKey === "pygwalker" && (
+ <div className="text-sm px-6 max-h-56 overflow-auto">
+ <div dangerouslySetInnerHTML={{ __html: syntaxHighlight(code) }} />
+ </div>
+ )}
+ <div className="mt-4 flex justify-start">
+ <PrimaryButton
+ // text={t("actions.confirm")}
+ className="mr-2 px-6"
+ text="Copy to Clipboard"
+ onClick={() => {
+ navigator.clipboard.writeText(JSON.stringify(code));
+ commonStore.setShowCodeExportPanel(false);
+ }}
+ />
+ <DefaultButton
+ text={t("actions.cancel")}
+ className="mr-2 px-6"
+ onClick={() => {
+ commonStore.setShowCodeExportPanel(false);
+ }}
+ />
+ </div>
+ </div>
+ </Modal>
+ );
+});
+
+export default CodeExport;
diff --git a/packages/graphic-walker/src/components/tabs/defaultTab.tsx b/packages/graphic-walker/src/components/tabs/defaultTab.tsx
index 30eec76a..66dada51 100644
--- a/packages/graphic-walker/src/components/tabs/defaultTab.tsx
+++ b/packages/graphic-walker/src/components/tabs/defaultTab.tsx
@@ -7,6 +7,7 @@ function classNames(...classes: string[]) {
export interface ITabOption {
label: string | ReactElement;
key: string;
+ disabled?: boolean;
}
interface DefaultProps {
tabs: ITabOption[];
@@ -26,14 +27,15 @@ export default function Default(props: DefaultProps) {
role="tab"
tabIndex={0}
onClick={() => {
- onSelected(tab.key, tabIndex)
+ !tab.disabled && onSelected(tab.key, tabIndex)
}}
key={tab.key}
className={classNames(
tab.key === selectedKey
? 'border-indigo-500 text-indigo-600 dark:border-indigo-400 dark:text-indigo-300'
: 'border-transparent text-gray-500 hover:text-gray-700 dark:hover:text-gray-200 hover:border-gray-300 dark:text-gray-400',
- 'whitespace-nowrap py-2 px-1 border-b-2 font-medium text-sm cursor-pointer'
+ 'whitespace-nowrap py-2 px-1 border-b-2 font-medium text-sm cursor-pointer',
+ tab.disabled ? 'opacity-50 cursor-not-allowed' : ''
)}
>{tab.label}</span>
))}
diff --git a/packages/graphic-walker/src/store/commonStore.ts b/packages/graphic-walker/src/store/commonStore.ts
index 7719e1ab..cc294b5c 100644
--- a/packages/graphic-walker/src/store/commonStore.ts
+++ b/packages/graphic-walker/src/store/commonStore.ts
@@ -14,6 +14,7 @@ export class CommonStore {
public showInsightBoard: boolean = false;
public vizEmbededMenu: { show: boolean; position: [number, number] } = { show: false, position: [0, 0] };
public showDataConfig: boolean = false;
+ public showCodeExportPanel: boolean = false;
public filters: Filters = {};
public segmentKey: ISegmentKey = ISegmentKey.vis;
constructor () {
@@ -61,6 +62,9 @@ export class CommonStore {
this.vizEmbededMenu.show = true;
this.vizEmbededMenu.position = position;
}
+ public setShowCodeExportPanel (show: boolean) {
+ this.showCodeExportPanel = show;
+ }
public closeEmbededMenu () {
this.vizEmbededMenu.show = false;
}
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 481e7c12..ccc19248 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -702,10 +702,7 @@ export class VizSpecStore {
}
public exportViewSpec() {
const pureVisList = dumpsGWPureSpec(this.visList);
- return {
- datasets: toJS(this.commonStore.datasets),
- specList: pureVisList,
- };
+ return pureVisList
}
public importStoInfo (stoInfo: IStoInfo) {
this.commonStore.datasets = stoInfo.datasets;
diff --git a/packages/graphic-walker/src/visualSettings/index.tsx b/packages/graphic-walker/src/visualSettings/index.tsx
index 747cace0..22db7b1a 100644
--- a/packages/graphic-walker/src/visualSettings/index.tsx
+++ b/packages/graphic-walker/src/visualSettings/index.tsx
@@ -21,6 +21,7 @@ import {
ChevronDoubleUpIcon,
ArrowsUpDownIcon,
LightBulbIcon,
+ CodeBracketSquareIcon,
} from '@heroicons/react/24/outline';
import { observer } from 'mobx-react-lite';
import React, { SVGProps, useCallback, useMemo } from 'react';
@@ -66,7 +67,7 @@ interface IVisualSettings {
}
const VisualSettings: React.FC<IVisualSettings> = ({ rendererHandler, darkModePreference }) => {
- const { vizStore } = useGlobalStore();
+ const { vizStore, commonStore } = useGlobalStore();
const { visualConfig, canUndo, canRedo } = vizStore;
const { t: tGlobal } = useTranslation();
const { t } = useTranslation('translation', { keyPrefix: 'main.tabpanel.settings' });
@@ -335,6 +336,14 @@ const VisualSettings: React.FC<IVisualSettings> = ({ rendererHandler, darkModePr
</FormContainer>
),
},
+ {
+ key: 'export_code',
+ label: t('button.export_code'),
+ icon: CodeBracketSquareIcon,
+ onClick: () => {
+ commonStore.setShowCodeExportPanel(true);
+ }
+ }
] as ToolbarItemProps[];
}, [vizStore, canUndo, canRedo, defaultAggregated, markType, stack, interactiveScale, sizeMode, width, height, explorationMode, brushDirection, showActions, downloadPNG, downloadSVG, dark]);
diff --git a/packages/graphic-walker/tailwind.config.js b/packages/graphic-walker/tailwind.config.js
index 967920d7..cfd32308 100644
--- a/packages/graphic-walker/tailwind.config.js
+++ b/packages/graphic-walker/tailwind.config.js
@@ -14,5 +14,14 @@ module.exports = {
'backgroundColor': ['disabled']
},
},
+ safelist: [
+ 'text-orange-500',
+ 'text-green-500',
+ 'text-blue-500',
+ 'text-red-500',
+ {
+ pattern: /text-([a-z]+)-(500)/,
+ }
+ ],
plugins: [],
}