diff --git a/dist/index.mjs b/dist/index.mjs index 567480ba5ccc30619db2eb8e6da8d618ddb5e891..c49c573893a26897062cda2af9896bc69881db2a 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -3056,14 +3056,16 @@ function useSetEditorValues({ ); } function createTab({ + id, + title, query = null, variables = null, headers = null -} = {}) { +}) { return { - id: guid(), + id: id || guid(), hash: hashFromTabContents({ query, variables, headers }), - title: query && fuzzyExtractOperationName(query) || DEFAULT_TITLE, + title: title || query && fuzzyExtractOperationName(query) || DEFAULT_TITLE, query, variables, headers, @@ -3081,8 +3083,7 @@ function setPropertiesInActiveTab(state, partialTab) { const newTab = { ...tab, ...partialTab }; return { ...newTab, - hash: hashFromTabContents(newTab), - title: newTab.operationName || (newTab.query ? fuzzyExtractOperationName(newTab.query) : void 0) || DEFAULT_TITLE + hash: hashFromTabContents(newTab) }; }) }; @@ -3304,25 +3305,31 @@ function EditorContextProvider(props) { responseEditor, defaultHeaders }); - const addTab = useCallback(() => { - setTabState((current) => { - const updatedValues = synchronizeActiveTabValues(current); - const updated = { - tabs: [...updatedValues.tabs, createTab({ headers: defaultHeaders })], - activeTabIndex: updatedValues.tabs.length - }; - storeTabs(updated); - setEditorValues(updated.tabs[updated.activeTabIndex]); - onTabChange == null ? void 0 : onTabChange(updated); - return updated; - }); - }, [ - defaultHeaders, - onTabChange, - setEditorValues, - storeTabs, - synchronizeActiveTabValues - ]); + const addTab = useCallback( + (_tabState) => { + setTabState((current) => { + const updatedValues = synchronizeActiveTabValues(current); + const updated = { + tabs: [ + ...updatedValues.tabs, + createTab({ ..._tabState, headers: defaultHeaders }) + ], + activeTabIndex: updatedValues.tabs.length + }; + storeTabs(updated); + setEditorValues(updated.tabs[updated.activeTabIndex]); + onTabChange == null ? void 0 : onTabChange(updated); + return updated; + }); + }, + [ + defaultHeaders, + onTabChange, + setEditorValues, + storeTabs, + synchronizeActiveTabValues + ] + ); const changeTab = useCallback( (index) => { setTabState((current) => { @@ -3418,6 +3425,7 @@ function EditorContextProvider(props) { const value = useMemo( () => ({ ...tabState, + setTabState, addTab, changeTab, moveTab, diff --git a/dist/types/editor/context.d.ts b/dist/types/editor/context.d.ts index 199db8a294f8132d46470498870adbdf9fdc83af..d8901fe0d50db17db36a502dcf69d5f69efb84a1 100644 --- a/dist/types/editor/context.d.ts +++ b/dist/types/editor/context.d.ts @@ -1,6 +1,6 @@ import { DocumentNode, FragmentDefinitionNode, OperationDefinitionNode, ValidationRule } from 'graphql'; import { VariableToType } from 'graphql-language-service'; -import { ReactNode } from 'react'; +import { Dispatch, ReactNode, SetStateAction } from 'react'; import { TabDefinition, TabsState, TabState } from './tabs'; import { CodeMirrorEditor } from './types'; export declare type CodeMirrorEditorWithOperationFacts = CodeMirrorEditor & { @@ -10,10 +10,11 @@ export declare type CodeMirrorEditorWithOperationFacts = CodeMirrorEditor & { variableToType: VariableToType | null; }; export declare type EditorContextType = TabsState & { + setTabState: Dispatch>; /** * Add a new tab. */ - addTab(): void; + addTab(tabState?: Pick): void; /** * Switch to a different tab. * @param index The index of the tab that should be switched to. @@ -38,7 +39,7 @@ export declare type EditorContextType = TabsState & { * @param partialTab A partial tab state object that will override the * current values. The properties `id`, `hash` and `title` cannot be changed. */ - updateActiveTabValues(partialTab: Partial>): void; + updateActiveTabValues(partialTab: Partial>): void; /** * The CodeMirror editor instance for the headers editor. */ diff --git a/dist/types/editor/tabs.d.ts b/dist/types/editor/tabs.d.ts index 28704a9c1c6e22fa75986de8591759e13035c8c5..5204d2b25198f89da9bba70804656f02799c7df6 100644 --- a/dist/types/editor/tabs.d.ts +++ b/dist/types/editor/tabs.d.ts @@ -90,7 +90,7 @@ export declare function useSetEditorValues({ queryEditor, variableEditor, header headers?: string | null | undefined; response: string | null; }) => void; -export declare function createTab({ query, variables, headers, }?: Partial): TabState; +export declare function createTab({ id, title, query, variables, headers, }: Partial>): TabState; export declare function setPropertiesInActiveTab(state: TabsState, partialTab: Partial>): TabsState; export declare function fuzzyExtractOperationName(str: string): string | null; export declare function clearHeadersFromTabs(storage: StorageAPI | null): void;