An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
fix: edit tab name bug
ObservedObserver5b965c0f1001
diff --git a/packages/graphic-walker/src/components/tabs/editableTab.tsx b/packages/graphic-walker/src/components/tabs/editableTab.tsxindex ebf6e80e..af004dcc 100644--- a/packages/graphic-walker/src/components/tabs/editableTab.tsx+++ b/packages/graphic-walker/src/components/tabs/editableTab.tsx@@ -1,74 +1,109 @@-import React, { useCallback, useEffect, useState } from "react";+import React, { useState } from "react";import { useTranslation } from "react-i18next";-+import { PencilSquareIcon } from "@heroicons/react/24/outline";+import Modal from "../modal";+import { unstable_batchedUpdates } from "react-dom";+import DefaultButton from "../button/default";+import PrimaryButton from "../button/primary";function classNames(...classes: string[]) {- return classes.filter(Boolean).join(' ')+ return classes.filter(Boolean).join(" ");}export interface ITabOption {label: string;key: string;- options?: Record<string, any>;+ editable?: boolean;}interface EditableTabsProps {tabs: ITabOption[];selectedKey: string;onSelected: (selectedKey: string, index: number) => void;- allowEdit?: boolean;onEditLabel?: (label: string, index: number) => void;}export default function EditableTabs(props: EditableTabsProps) {- const { tabs, selectedKey, onSelected, allowEdit, onEditLabel } = props;- const [editList, setEditList] = useState<boolean[]>([]);+ const { tabs, selectedKey, onSelected, onEditLabel } = props;+ const [editingIndex, setEditingIndex] = useState<number>(-1);+ const [name, setName] = useState<string>("");const { t } = useTranslation();- const clearEditStatus = useCallback(() => {- setEditList(new Array(tabs.length).fill(false))- }, [tabs.length]);-- useEffect(() => {- clearEditStatus();- }, [clearEditStatus]);-return (- <div className="border-b border-gray-200 dark:border-gray-700 overflow-x-auto overflow-y-hidden" onMouseLeave={clearEditStatus}>+ <div className="border-b border-gray-200 dark:border-gray-700 overflow-x-auto overflow-y-hidden">+ <Modal+ show={editingIndex > -1}+ onClose={() => {+ setEditingIndex(-1);+ }}+ >+ <div>+ <span className="block text-sm font-medium leading-6">{t('main.tablist.chart_name')}</span>+ <div className="mt-2">+ <input+ value={name}+ onChange={(e) => {+ setName(e.target.value);+ }}+ type="text"+ name="text"+ className="block w-full rounded-md border-0 px-2 py-1.5 bg-transparent shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"+ />+ </div>+ <div className="mt-4 flex justify-end">+ <DefaultButton+ className="mr-2"+ text={t("actions.cancel")}+ onClick={() => {+ unstable_batchedUpdates(() => {+ setEditingIndex(-1);+ setName("");+ });+ }}+ />+ <PrimaryButton+ text={t("actions.confirm")}+ onClick={() => {+ unstable_batchedUpdates(() => {+ onEditLabel && onEditLabel(name, editingIndex);+ setEditingIndex(-1);+ setName("");+ });+ }}+ />+ </div>+ </div>+ </Modal><nav className="-mb-px flex h-8 border-gray-200 dark:border-gray-700" role="tablist" aria-label="Tabs">{tabs.map((tab, tabIndex) => (<spanrole="tab"tabIndex={0}- dangerouslySetInnerHTML={{- __html: t(tab.label, tab.options)- }}+ // dangerouslySetInnerHTML={{+ // __html: tab.label+ // }}onClick={() => {- onSelected(tab.key, tabIndex)- }}- onDoubleClick={() => {- setEditList(v => {- const nv = [...v];- nv[tabIndex] = true;- return nv- })- }}- contentEditable={editList[tabIndex]}- onInput={(e) => {- onEditLabel && onEditLabel(`${e.currentTarget.textContent}`, tabIndex)- }}- onKeyDown={(e) => {- if (e.key === 'Enter') {- clearEditStatus();- e.preventDefault();- }+ onSelected(tab.key, tabIndex);}}key={tab.key}className={classNames(tab.key === selectedKey? "border rounded-t": "text-gray-500 dark:text-gray-400 hover:text-gray-700 hover:bg-gray-50 dark:hover:text-gray-200 dark:hover:bg-gray-800",- "whitespace-nowrap border-gray-200 dark:border-gray-700 py-1 px-2 pr-6 text-sm cursor-pointer dark:text-white"+ "whitespace-nowrap border-gray-200 dark:border-gray-700 py-1 px-2 pr-6 text-sm cursor-default dark:text-white"+ )}+ >+ {tab.label}{" "}+ {tab.key === selectedKey && tab.editable && (+ <PencilSquareIcon+ className="w-3 inline cursor-pointer"+ onClick={() => {+ unstable_batchedUpdates(() => {+ setEditingIndex(tabIndex);+ setName(tab.label);+ });+ }}+ />)}- />+ </span>))}</nav></div>diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.tsindex e6c1d623..6dc84a53 100644--- a/packages/graphic-walker/src/interfaces.ts+++ b/packages/graphic-walker/src/interfaces.ts@@ -1,6 +1,6 @@import { StatFuncName } from "visual-insights/build/esm/statistics";import { AggFC } from 'cube-core/built/types';-import { IAnalyticType, IMutField as VIMutField, ISemanticType } from 'visual-insights';+import { IAnalyticType, ISemanticType } from 'visual-insights';export type DeepReadonly<T extends Record<keyof any, any>> = {readonly [K in keyof T]: T[K] extends Record<keyof any, any> ? DeepReadonly<T[K]> : T[K];@@ -176,7 +176,7 @@ export interface IVisualConfig {export interface IVisSpec {readonly visId: string;- readonly name?: [string, Record<string, any>?];+ readonly name?: string;readonly encodings: DeepReadonly<DraggableFieldState>;readonly config: DeepReadonly<IVisualConfig>;}diff --git a/packages/graphic-walker/src/locales/en-US.json b/packages/graphic-walker/src/locales/en-US.jsonindex 9dfa2373..e008d293 100644--- a/packages/graphic-walker/src/locales/en-US.json+++ b/packages/graphic-walker/src/locales/en-US.json@@ -117,7 +117,8 @@"main": {"tablist": {"new": "+ New",- "autoTitle": "Chart {{idx}}"+ "auto_title": "Chart {{idx}}",+ "chart_name": "Chart Name"},"tabpanel": {"menubar": {@@ -190,6 +191,7 @@"prev": "Previous","next": "Next","drop_field": "Drop Field Here",- "confirm": "Confirm"+ "confirm": "Confirm",+ "cancel": "Cancel"}}\ No newline at end of filediff --git a/packages/graphic-walker/src/locales/i18n.ts b/packages/graphic-walker/src/locales/i18n.tsindex 968eeb4a..90b99f4a 100644--- a/packages/graphic-walker/src/locales/i18n.ts+++ b/packages/graphic-walker/src/locales/i18n.ts@@ -6,7 +6,6 @@ import localeEnUs from './en-US.json';import localeJaJp from './ja-JP.json';import localeZhCn from './zh-CN.json';-const locales: Resource & { 'en-US': any } = {'en': {translation: localeEnUs,diff --git a/packages/graphic-walker/src/locales/ja-JP.json b/packages/graphic-walker/src/locales/ja-JP.jsonindex d94c9686..0a95d399 100644--- a/packages/graphic-walker/src/locales/ja-JP.json+++ b/packages/graphic-walker/src/locales/ja-JP.json@@ -117,7 +117,8 @@"main": {"tablist": {"new": "+ 新規作成",- "autoTitle": "グラフ {{idx}}"+ "auto_title": "グラフ {{idx}}",+ "chart_name": "グラフ名"},"tabpanel": {"menubar": {@@ -190,6 +191,7 @@"prev": "前へ","next": "次へ","drop_field": "ここにフィールドをドロップ",- "confirm": "確認"+ "confirm": "確認",+ "cancel": "キャンセル"}}\ No newline at end of filediff --git a/packages/graphic-walker/src/locales/zh-CN.json b/packages/graphic-walker/src/locales/zh-CN.jsonindex 0aff3986..96d13bdc 100644--- a/packages/graphic-walker/src/locales/zh-CN.json+++ b/packages/graphic-walker/src/locales/zh-CN.json@@ -117,7 +117,8 @@"main": {"tablist": {"new": "+ 新建",- "autoTitle": "图表 {{idx}}"+ "auto_title": "图表 {{idx}}",+ "chart_name": "图表名称"},"tabpanel": {"menubar": {@@ -190,6 +191,7 @@"prev": "向前","next": "向后","drop_field": "拖拽字段至此",- "confirm": "确认"+ "confirm": "确认",+ "cencel": "取消"}}\ No newline at end of filediff --git a/packages/graphic-walker/src/models/visSpecHistory.ts b/packages/graphic-walker/src/models/visSpecHistory.tsindex 1312170a..f747b631 100644--- a/packages/graphic-walker/src/models/visSpecHistory.ts+++ b/packages/graphic-walker/src/models/visSpecHistory.ts@@ -27,6 +27,14 @@ export class VisSpecWithHistory {private batchFlag = false;+ public updateLatest(snapshot: Partial<Readonly<VisSpecWithHistory['snapshots'][0]>>) {+ this.snapshots[this.cursor] = {+ ...this.snapshots[this.cursor],+ ...snapshot,+ }+ }++private commit(snapshot: Partial<Readonly<VisSpecWithHistory['snapshots'][0]>>): void {if (this.batchFlag) {// batch this commit@@ -120,6 +128,13 @@ export class VisSpecWithHistory {});}+ public clone () {+ console.log(this.snapshots)+ const nextVSWH = new VisSpecWithHistory(this.frame);+ nextVSWH.cursor = this.cursor;+ return nextVSWH;+ }+public exportGW (): IVisSpec {return {...this.framediff --git a/packages/graphic-walker/src/segments/visNav.tsx b/packages/graphic-walker/src/segments/visNav.tsxindex 06322e60..903802a0 100644--- a/packages/graphic-walker/src/segments/visNav.tsx+++ b/packages/graphic-walker/src/segments/visNav.tsx@@ -1,5 +1,6 @@-import React, { useCallback } from "react";+import React, { useCallback, useEffect } from "react";import { observer } from "mobx-react-lite";+import { useTranslation } from "react-i18next";import EditableTabs, { ITabOption } from "../components/tabs/editableTab";import { useGlobalStore } from "../store";@@ -11,25 +12,36 @@ const VisNav: React.FC = (props) => {const { visIndex, visList } = vizStore;const { currentDataset } = commonStore;+ const { t } = useTranslation();+const tabs: ITabOption[] = visList.map((v) => ({key: v.visId,- label: v.name?.[0] || 'vis',- options: v.name?.[1],+ label: v.name ?? 'vis',+ editable: true}));tabs.push({key: ADD_KEY,- label: 'main.tablist.new'+ label: t('main.tablist.new')});+ useEffect(() => {+ if (visList.length === 1) {+ // should set the first vis name when the component is mounted+ console.log(t('main.tablist.auto_title', { idx: 1 }))+ vizStore.setVisName(0, t('main.tablist.auto_title', { idx: 1 }));+ }+ // no need to add deps here+ }, [])+const visSelectionHandler = useCallback((tabKey: string, tabIndex: number) => {if (tabKey === ADD_KEY) {- vizStore.addVisualization();+ vizStore.addVisualization(t('main.tablist.auto_title', { idx: visList.length + 1 }));vizStore.initMetaState(currentDataset)} else {vizStore.selectVisualization(tabIndex);}- }, [currentDataset, vizStore])+ }, [currentDataset, vizStore, visList.length])const editLabelHandler = useCallback((content: string, tabIndex: number) => {vizStore.setVisName(tabIndex, content)diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.tsindex 0a4dc70a..1d942fb8 100644--- a/packages/graphic-walker/src/store/visualSpecStore.ts+++ b/packages/graphic-walker/src/store/visualSpecStore.ts@@ -139,7 +139,7 @@ export class VizSpecStore {this.visualConfig = initVisualConfig();this.visList.push(new VisSpecWithHistory({- name: ["main.tablist.autoTitle", { idx: 1 }],+ name: 'Chart 1',visId: uuidv4(),config: this.visualConfig,encodings: this.draggableFieldState,@@ -275,10 +275,11 @@ export class VizSpecStore {});return fields;}- public addVisualization() {+ public addVisualization(defaultName?: string) {+ const name = defaultName || 'Chart ' + (this.visList.length + 1);this.visList.push(new VisSpecWithHistory({- name: ["main.tablist.autoTitle", { idx: this.visList.length + 1 }],+ name,visId: uuidv4(),config: initVisualConfig(),encodings: initEncoding(),@@ -290,9 +291,10 @@ export class VizSpecStore {this.visIndex = visIndex;}public setVisName(visIndex: number, name: string) {- this.useMutable(() => {- this.visList[visIndex].name = [name];- });+ this.visList[visIndex] = this.visList[visIndex].clone();+ this.visList[visIndex].updateLatest({+ name+ })}public initState() {this.useMutable((tab) => {