@elementor/editor-documents
Advanced tools
Comparing version 0.8.1 to 0.8.2
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.8.2](https://github.com/elementor/elementor-packages/compare/@elementor/editor-documents@0.8.1...@elementor/editor-documents@0.8.2) (2023-06-25) | ||
### Bug Fixes | ||
* **editor:** document query parameters are not synced properly [ED-11024] ([#54](https://github.com/elementor/elementor-packages/issues/54)) ([4c17080](https://github.com/elementor/elementor-packages/commit/4c17080a7cecec7c807954e9381fb7c840e51ea4)) | ||
## [0.8.1](https://github.com/elementor/elementor-packages/compare/@elementor/editor-documents@0.8.0...@elementor/editor-documents@0.8.1) (2023-06-11) | ||
@@ -8,0 +19,0 @@ |
@@ -11,3 +11,3 @@ declare function useActiveDocument(): Document | null; | ||
declare function useNavigateToDocument(): (id: number) => Promise<any>; | ||
declare function useNavigateToDocument(): (id: number) => Promise<void>; | ||
@@ -14,0 +14,0 @@ type Document = { |
@@ -308,7 +308,11 @@ "use strict"; | ||
function useNavigateToDocument() { | ||
return (0, import_react2.useCallback)((id) => { | ||
return (0, import_editor_v1_adapters3.runCommand)("editor/documents/switch", { | ||
return (0, import_react2.useCallback)(async (id) => { | ||
await (0, import_editor_v1_adapters3.runCommand)("editor/documents/switch", { | ||
id, | ||
setAsInitial: true | ||
}); | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("post", id.toString()); | ||
url.searchParams.delete("active-document"); | ||
history.replaceState({}, "", url); | ||
}, []); | ||
@@ -315,0 +319,0 @@ } |
{ | ||
"name": "@elementor/editor-documents", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"private": false, | ||
@@ -44,3 +44,3 @@ "author": "Elementor Team", | ||
}, | ||
"gitHead": "70a2c6139730b7afa69d13eb65023b0e6612f23b" | ||
"gitHead": "ad9032e095915dbef0f8543d83f73b9288adc8ad" | ||
} |
@@ -8,4 +8,33 @@ import { renderHook } from '@testing-library/react'; | ||
describe( '@elementor/editor-documents - useNavigateToDocument', () => { | ||
it( 'should navigate to document', () => { | ||
const originalReplaceState = history.replaceState; | ||
const originalLocation = window.location; | ||
beforeEach( () => { | ||
jest.resetAllMocks(); | ||
history.replaceState = jest.fn(); | ||
/** | ||
* @see https://gist.github.com/the0neWhoKnocks/bdac1d09b93b8418d948558f7ab233d7#setting-props-on-windowlocation | ||
*/ | ||
Object.defineProperty( window, 'location', { | ||
writable: true, | ||
value: new URL( 'https://localhost/' ), | ||
} ); | ||
} ); | ||
afterAll( () => { | ||
history.replaceState = originalReplaceState; | ||
Object.defineProperty( window, 'location', { | ||
writable: false, | ||
value: originalLocation, | ||
} ); | ||
} ); | ||
it( 'should navigate to document and change query params', async () => { | ||
// Arrange. | ||
// TS doesn't allow modifying the location object. | ||
( window as unknown as { location: URL } ).location = new URL( 'https://localhost/?post=1&active-document=3' ); | ||
const { result } = renderHook( useNavigateToDocument ); | ||
@@ -16,3 +45,3 @@ | ||
// Act. | ||
navigateToDocument( 123 ); | ||
await navigateToDocument( 123 ); | ||
@@ -25,3 +54,10 @@ // Assert. | ||
} ); | ||
expect( history.replaceState ).toHaveBeenCalledTimes( 1 ); | ||
expect( history.replaceState ).toHaveBeenCalledWith( | ||
expect.anything(), | ||
expect.anything(), | ||
expect.objectContaining( { href: 'https://localhost/?post=123' } ) | ||
); | ||
} ); | ||
} ); |
@@ -5,8 +5,15 @@ import { useCallback } from 'react'; | ||
export default function useNavigateToDocument() { | ||
return useCallback( ( id: number ) => { | ||
return runCommand( 'editor/documents/switch', { | ||
return useCallback( async ( id: number ) => { | ||
await runCommand( 'editor/documents/switch', { | ||
id, | ||
setAsInitial: true, | ||
} ); | ||
const url = new URL( window.location.href ); | ||
url.searchParams.set( 'post', id.toString() ); | ||
url.searchParams.delete( 'active-document' ); | ||
history.replaceState( {}, '', url ); | ||
}, [] ); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
120450
1642