@equinor/fusion-framework-module-navigation 6.0.1 → 7.0.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +83 -5
- package/README.md +694 -0
- package/dist/esm/NavigationConfigurator.interface.js +2 -0
- package/dist/esm/NavigationConfigurator.interface.js.map +1 -0
- package/dist/esm/NavigationConfigurator.js +144 -0
- package/dist/esm/NavigationConfigurator.js.map +1 -0
- package/dist/esm/NavigationProvider.interface.js +2 -0
- package/dist/esm/NavigationProvider.interface.js.map +1 -0
- package/dist/esm/NavigationProvider.js +256 -0
- package/dist/esm/NavigationProvider.js.map +1 -0
- package/dist/esm/enable-navigation.js +52 -0
- package/dist/esm/enable-navigation.js.map +1 -0
- package/dist/esm/events.js +23 -0
- package/dist/esm/events.js.map +1 -0
- package/dist/esm/index.js +6 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/BaseHistory.js +189 -0
- package/dist/esm/lib/BaseHistory.js.map +1 -0
- package/dist/esm/lib/BrowserHistory.js +91 -0
- package/dist/esm/lib/BrowserHistory.js.map +1 -0
- package/dist/esm/lib/BrowserHistoryHashStack.js +53 -0
- package/dist/esm/lib/BrowserHistoryHashStack.js.map +1 -0
- package/dist/esm/lib/BrowserHistoryStack.js +77 -0
- package/dist/esm/lib/BrowserHistoryStack.js.map +1 -0
- package/dist/esm/lib/MemoryHistory.js +47 -0
- package/dist/esm/lib/MemoryHistory.js.map +1 -0
- package/dist/esm/lib/MemoryStack.js +98 -0
- package/dist/esm/lib/MemoryStack.js.map +1 -0
- package/dist/esm/lib/create-history.js +48 -0
- package/dist/esm/lib/create-history.js.map +1 -0
- package/dist/esm/lib/index.js +8 -1
- package/dist/esm/lib/index.js.map +1 -1
- package/dist/esm/lib/state/history.actions.js +71 -0
- package/dist/esm/lib/state/history.actions.js.map +1 -0
- package/dist/esm/lib/state/history.flows.js +212 -0
- package/dist/esm/lib/state/history.flows.js.map +1 -0
- package/dist/esm/lib/state/history.reducer.js +59 -0
- package/dist/esm/lib/state/history.reducer.js.map +1 -0
- package/dist/esm/lib/state/history.state.js +37 -0
- package/dist/esm/lib/state/history.state.js.map +1 -0
- package/dist/esm/lib/state/index.js +4 -0
- package/dist/esm/lib/state/index.js.map +1 -0
- package/dist/esm/lib/types.js +25 -0
- package/dist/esm/lib/types.js.map +1 -0
- package/dist/esm/lib/utils/encode-trailing-whitespace.js +19 -0
- package/dist/esm/lib/utils/encode-trailing-whitespace.js.map +1 -0
- package/dist/esm/lib/utils/has-protocol.js +22 -0
- package/dist/esm/lib/utils/has-protocol.js.map +1 -0
- package/dist/esm/lib/utils/index.js +6 -0
- package/dist/esm/lib/utils/index.js.map +1 -0
- package/dist/esm/lib/utils/path-to-string.js +23 -0
- package/dist/esm/lib/utils/path-to-string.js.map +1 -0
- package/dist/esm/lib/utils/path-to-url.js +47 -0
- package/dist/esm/lib/utils/path-to-url.js.map +1 -0
- package/dist/esm/lib/utils/resolve-browser-location.js +53 -0
- package/dist/esm/lib/utils/resolve-browser-location.js.map +1 -0
- package/dist/esm/lib/utils/resolve-path.js +21 -0
- package/dist/esm/lib/utils/resolve-path.js.map +1 -0
- package/dist/esm/module.js +34 -25
- package/dist/esm/module.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/NavigationConfigurator.d.ts +60 -0
- package/dist/types/NavigationConfigurator.interface.d.ts +17 -0
- package/dist/types/NavigationProvider.d.ts +112 -0
- package/dist/types/NavigationProvider.interface.d.ts +75 -0
- package/dist/types/enable-navigation.d.ts +40 -0
- package/dist/types/events.d.ts +48 -0
- package/dist/types/index.d.ts +13 -6
- package/dist/types/lib/BaseHistory.d.ts +122 -0
- package/dist/types/lib/BrowserHistory.d.ts +53 -0
- package/dist/types/lib/BrowserHistoryHashStack.d.ts +41 -0
- package/dist/types/lib/BrowserHistoryStack.d.ts +51 -0
- package/dist/types/lib/MemoryHistory.d.ts +27 -0
- package/dist/types/lib/MemoryStack.d.ts +57 -0
- package/dist/types/lib/create-history.d.ts +35 -0
- package/dist/types/lib/index.d.ts +7 -1
- package/dist/types/lib/state/history.actions.d.ts +61 -0
- package/dist/types/lib/state/history.flows.d.ts +102 -0
- package/dist/types/lib/state/history.reducer.d.ts +85 -0
- package/dist/types/lib/state/history.state.d.ts +27 -0
- package/dist/types/lib/state/index.d.ts +3 -0
- package/dist/types/lib/types.d.ts +145 -0
- package/dist/types/lib/utils/encode-trailing-whitespace.d.ts +16 -0
- package/dist/types/lib/utils/has-protocol.d.ts +19 -0
- package/dist/types/lib/utils/index.d.ts +5 -0
- package/dist/types/lib/utils/path-to-string.d.ts +17 -0
- package/dist/types/lib/utils/path-to-url.d.ts +32 -0
- package/dist/types/lib/utils/resolve-browser-location.d.ts +42 -0
- package/dist/types/lib/utils/resolve-path.d.ts +17 -0
- package/dist/types/module.d.ts +33 -5
- package/dist/types/version.d.ts +1 -1
- package/package.json +25 -7
- package/src/NavigationConfigurator.interface.ts +18 -0
- package/src/NavigationConfigurator.ts +184 -0
- package/src/NavigationProvider.interface.ts +86 -0
- package/src/NavigationProvider.ts +313 -0
- package/src/__tests__/BrowserHistory.test.ts +151 -0
- package/src/__tests__/HashHistory.test.ts +121 -0
- package/src/__tests__/MemoryHistory.test.ts +185 -0
- package/src/__tests__/NavigationProvider.test.ts +60 -0
- package/src/__tests__/setup.ts +8 -0
- package/src/enable-navigation.ts +59 -0
- package/src/events.ts +65 -0
- package/src/index.ts +27 -6
- package/src/lib/BaseHistory.ts +229 -0
- package/src/lib/BrowserHistory.ts +123 -0
- package/src/lib/BrowserHistoryHashStack.ts +56 -0
- package/src/lib/BrowserHistoryStack.ts +82 -0
- package/src/lib/MemoryHistory.ts +66 -0
- package/src/lib/MemoryStack.ts +106 -0
- package/src/lib/create-history.ts +63 -0
- package/src/lib/index.ts +22 -1
- package/src/lib/state/history.actions.ts +108 -0
- package/src/lib/state/history.flows.ts +279 -0
- package/src/lib/state/history.reducer.ts +70 -0
- package/src/lib/state/history.state.ts +62 -0
- package/src/lib/state/index.ts +3 -0
- package/src/lib/types.ts +159 -0
- package/src/lib/utils/encode-trailing-whitespace.ts +18 -0
- package/src/lib/utils/has-protocol.ts +21 -0
- package/src/lib/utils/index.ts +5 -0
- package/src/lib/utils/path-to-string.ts +24 -0
- package/src/lib/utils/path-to-url.ts +52 -0
- package/src/lib/utils/resolve-browser-location.ts +60 -0
- package/src/lib/utils/resolve-path.ts +22 -0
- package/src/module.ts +53 -37
- package/src/version.ts +1 -1
- package/tsconfig.json +10 -1
- package/vitest.config.ts +14 -0
- package/dist/esm/configurator.js +0 -5
- package/dist/esm/configurator.js.map +0 -1
- package/dist/esm/createHistory.js +0 -18
- package/dist/esm/createHistory.js.map +0 -1
- package/dist/esm/lib/provider/INavigationProvider.js +0 -2
- package/dist/esm/lib/provider/INavigationProvider.js.map +0 -1
- package/dist/esm/lib/provider/NavigationProvider.js +0 -82
- package/dist/esm/lib/provider/NavigationProvider.js.map +0 -1
- package/dist/esm/lib/provider/index.js +0 -2
- package/dist/esm/lib/provider/index.js.map +0 -1
- package/dist/esm/navigator.js +0 -158
- package/dist/esm/navigator.js.map +0 -1
- package/dist/esm/types.js +0 -2
- package/dist/esm/types.js.map +0 -1
- package/dist/types/configurator.d.ts +0 -9
- package/dist/types/createHistory.d.ts +0 -4
- package/dist/types/lib/provider/INavigationProvider.d.ts +0 -38
- package/dist/types/lib/provider/NavigationProvider.d.ts +0 -22
- package/dist/types/lib/provider/index.d.ts +0 -2
- package/dist/types/navigator.d.ts +0 -105
- package/dist/types/types.d.ts +0 -3
- package/src/configurator.ts +0 -11
- package/src/createHistory.ts +0 -21
- package/src/lib/provider/INavigationProvider.ts +0 -46
- package/src/lib/provider/NavigationProvider.ts +0 -117
- package/src/lib/provider/index.ts +0 -2
- package/src/navigator.ts +0 -204
- package/src/types.ts +0 -4
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { type AgnosticRouteObject, type Path, type To } from '@remix-run/router';
|
|
2
|
-
import { BaseModuleProvider, type BaseModuleProviderCtorArgs } from '@equinor/fusion-framework-module/provider';
|
|
3
|
-
import type { INavigationProvider } from './INavigationProvider';
|
|
4
|
-
import type { INavigationConfigurator } from '../../configurator';
|
|
5
|
-
import { type INavigator } from '../../navigator';
|
|
6
|
-
export declare class NavigationProvider extends BaseModuleProvider<INavigationConfigurator> implements INavigationProvider {
|
|
7
|
-
#private;
|
|
8
|
-
get state$(): import("rxjs").Observable<{
|
|
9
|
-
action: import("@remix-run/router").Action;
|
|
10
|
-
location: Path;
|
|
11
|
-
}>;
|
|
12
|
-
get navigator(): INavigator;
|
|
13
|
-
get path(): Path;
|
|
14
|
-
constructor(args: BaseModuleProviderCtorArgs<INavigationConfigurator>);
|
|
15
|
-
createRouter(routes: AgnosticRouteObject[]): import("@remix-run/router").Router;
|
|
16
|
-
createHref(to?: To): string;
|
|
17
|
-
createURL(to?: To): URL;
|
|
18
|
-
push(to: To, state?: unknown): void;
|
|
19
|
-
replace(to: To, state?: unknown): void;
|
|
20
|
-
protected _localizePath(location: Path): Path;
|
|
21
|
-
protected _createToPath(to: To): Partial<Path>;
|
|
22
|
-
}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import type { History, To, Path, Location } from '@remix-run/router';
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
3
|
-
import type { NavigationUpdate, NavigationListener } from './types';
|
|
4
|
-
/**
|
|
5
|
-
* Interface representing a navigator that extends both an observable of navigation updates and the History interface.
|
|
6
|
-
*/
|
|
7
|
-
export interface INavigator<T extends NavigationUpdate = NavigationUpdate> extends Observable<T>, History {
|
|
8
|
-
readonly basename?: string;
|
|
9
|
-
readonly origin?: string;
|
|
10
|
-
readonly value: NavigationUpdate;
|
|
11
|
-
readonly location: NavigationUpdate['location'];
|
|
12
|
-
dispose: VoidFunction;
|
|
13
|
-
listen(listener: NavigationListener): VoidFunction;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Class representing a navigator that manages navigation and history state.
|
|
17
|
-
*/
|
|
18
|
-
export declare class Navigator<T extends NavigationUpdate = NavigationUpdate> extends Observable<T> implements INavigator<T> {
|
|
19
|
-
#private;
|
|
20
|
-
/**
|
|
21
|
-
* Gets the basename of the navigator.
|
|
22
|
-
*/
|
|
23
|
-
get basename(): string | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* Gets the origin of the navigator.
|
|
26
|
-
*/
|
|
27
|
-
get origin(): string | undefined;
|
|
28
|
-
/**
|
|
29
|
-
* Gets the current navigation state as an observable.
|
|
30
|
-
*/
|
|
31
|
-
get state(): Observable<T>;
|
|
32
|
-
/**
|
|
33
|
-
* Gets the current navigation state value.
|
|
34
|
-
*/
|
|
35
|
-
get value(): T;
|
|
36
|
-
/**
|
|
37
|
-
* Gets the current location from the navigation state.
|
|
38
|
-
*/
|
|
39
|
-
get location(): T['location'];
|
|
40
|
-
/**
|
|
41
|
-
* Gets the current action from the navigation state.
|
|
42
|
-
*/
|
|
43
|
-
get action(): T['action'];
|
|
44
|
-
/**
|
|
45
|
-
* Navigator constructor.
|
|
46
|
-
* @param args - Configuration options for the navigator.
|
|
47
|
-
*/
|
|
48
|
-
constructor(args: {
|
|
49
|
-
basename?: string;
|
|
50
|
-
history: History;
|
|
51
|
-
logger?: any;
|
|
52
|
-
mode?: 'MASTER' | 'SLAVE';
|
|
53
|
-
});
|
|
54
|
-
/**
|
|
55
|
-
* Encodes a location into a path string.
|
|
56
|
-
* @param to - The location to encode.
|
|
57
|
-
* @returns The encoded path string.
|
|
58
|
-
*/
|
|
59
|
-
encodeLocation(to: To): Path;
|
|
60
|
-
/**
|
|
61
|
-
* Subscribes to navigation updates.
|
|
62
|
-
* @param listener - The listener function to receive updates.
|
|
63
|
-
* @returns A function to unsubscribe the listener.
|
|
64
|
-
*/
|
|
65
|
-
listen(listener: NavigationListener): VoidFunction;
|
|
66
|
-
/**
|
|
67
|
-
* Creates a URL string for a given location.
|
|
68
|
-
* @param to - The location to create a URL for.
|
|
69
|
-
* @returns The created URL string.
|
|
70
|
-
*/
|
|
71
|
-
createHref(to: To): string;
|
|
72
|
-
/**
|
|
73
|
-
* Navigates to a specific history entry by its relative position to the current entry.
|
|
74
|
-
* @param delta - The relative position to move in the history stack.
|
|
75
|
-
*/
|
|
76
|
-
go(delta: number): void;
|
|
77
|
-
/**
|
|
78
|
-
* Pushes a new entry onto the history stack.
|
|
79
|
-
* @param to - The location or URL to navigate to.
|
|
80
|
-
* @param state - Optional state to associate with the navigation.
|
|
81
|
-
*/
|
|
82
|
-
push(to: To, state?: unknown): void;
|
|
83
|
-
/**
|
|
84
|
-
* Replaces the current history entry with a new one.
|
|
85
|
-
* @param to - The location or URL to navigate to.
|
|
86
|
-
* @param state - Optional state to associate with the navigation.
|
|
87
|
-
*/
|
|
88
|
-
replace(to: To, state?: unknown): void;
|
|
89
|
-
/**
|
|
90
|
-
* Creates a full URL for a given location.
|
|
91
|
-
* @param to - The location to create a URL for.
|
|
92
|
-
* @returns The created URL object.
|
|
93
|
-
*/
|
|
94
|
-
createURL(to: To): URL;
|
|
95
|
-
/**
|
|
96
|
-
* Checks if a given location is a duplicate of the current location.
|
|
97
|
-
* @param location - The location to check.
|
|
98
|
-
* @returns True if the location is a duplicate, false otherwise.
|
|
99
|
-
*/
|
|
100
|
-
protected _isDuplicateLocation(location: Location): boolean;
|
|
101
|
-
/**
|
|
102
|
-
* Disposes of the navigator by unsubscribing from all subscriptions.
|
|
103
|
-
*/
|
|
104
|
-
dispose(): void;
|
|
105
|
-
}
|
package/dist/types/types.d.ts
DELETED
package/src/configurator.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { History } from '@remix-run/router';
|
|
2
|
-
|
|
3
|
-
export interface INavigationConfigurator {
|
|
4
|
-
basename?: string;
|
|
5
|
-
history?: History;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class NavigationConfigurator implements INavigationConfigurator {
|
|
9
|
-
public history?: History;
|
|
10
|
-
public basename?: string;
|
|
11
|
-
}
|
package/src/createHistory.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { createBrowserHistory, createHashHistory, createMemoryHistory } from '@remix-run/router';
|
|
2
|
-
import type { History } from '@remix-run/router';
|
|
3
|
-
|
|
4
|
-
type HistoryType = 'browser' | 'hash' | 'memory';
|
|
5
|
-
|
|
6
|
-
export const createHistory = (type?: HistoryType): History => {
|
|
7
|
-
switch (type) {
|
|
8
|
-
case 'hash':
|
|
9
|
-
return createHashHistory({ v5Compat: true });
|
|
10
|
-
case 'memory':
|
|
11
|
-
return createMemoryHistory({ v5Compat: true });
|
|
12
|
-
case 'browser':
|
|
13
|
-
return createBrowserHistory({
|
|
14
|
-
v5Compat: true,
|
|
15
|
-
});
|
|
16
|
-
default: {
|
|
17
|
-
console.warn(`Unknown history type: ${type}. Using browser history as default.`);
|
|
18
|
-
return createHistory('browser');
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { Action, AgnosticRouteObject, Path, Router, To } from '@remix-run/router';
|
|
2
|
-
|
|
3
|
-
import type { INavigator } from '../../navigator';
|
|
4
|
-
|
|
5
|
-
import type { Observable } from 'rxjs';
|
|
6
|
-
import type { IModuleProvider } from '@equinor/fusion-framework-module';
|
|
7
|
-
|
|
8
|
-
export interface INavigationProvider extends IModuleProvider {
|
|
9
|
-
/**
|
|
10
|
-
* Observer navigation changes, will return localized paths
|
|
11
|
-
*/
|
|
12
|
-
readonly state$: Observable<{ action: Action; location: Path }>;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Localized current path
|
|
16
|
-
*/
|
|
17
|
-
readonly path: Path;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* History controller
|
|
21
|
-
*/
|
|
22
|
-
readonly navigator: INavigator;
|
|
23
|
-
|
|
24
|
-
// TODO - create own router object type, too many mismatch on type
|
|
25
|
-
createRouter(routes: AgnosticRouteObject[]): Router;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Create a localized pathname
|
|
29
|
-
*/
|
|
30
|
-
createHref(to?: To): string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Create a localized URL
|
|
34
|
-
*/
|
|
35
|
-
createURL(to?: To): URL;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Execute a relative `push` navigation
|
|
39
|
-
*/
|
|
40
|
-
push(to: To, state?: unknown): void;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Execute a relative `replace` navigation
|
|
44
|
-
*/
|
|
45
|
-
replace(to: To, state?: unknown): void;
|
|
46
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { type AgnosticRouteObject, createRouter, type Path, type To } from '@remix-run/router';
|
|
2
|
-
|
|
3
|
-
import { filter, map } from 'rxjs/operators';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
BaseModuleProvider,
|
|
7
|
-
type BaseModuleProviderCtorArgs,
|
|
8
|
-
} from '@equinor/fusion-framework-module/provider';
|
|
9
|
-
|
|
10
|
-
import type { INavigationProvider } from './INavigationProvider';
|
|
11
|
-
|
|
12
|
-
import type { INavigationConfigurator } from '../../configurator';
|
|
13
|
-
import { Navigator, type INavigator } from '../../navigator';
|
|
14
|
-
|
|
15
|
-
const normalizePathname = (path: string) => path.replace(/\/+/g, '/');
|
|
16
|
-
|
|
17
|
-
export class NavigationProvider
|
|
18
|
-
extends BaseModuleProvider<INavigationConfigurator>
|
|
19
|
-
implements INavigationProvider
|
|
20
|
-
{
|
|
21
|
-
#navigator: INavigator;
|
|
22
|
-
#basePathname?: string;
|
|
23
|
-
|
|
24
|
-
public get state$() {
|
|
25
|
-
return this.#navigator.pipe(
|
|
26
|
-
/** only push navigation state if the path is within the basename scope */
|
|
27
|
-
filter((event) =>
|
|
28
|
-
this.#basePathname ? event.location.pathname.startsWith(this.#basePathname) : true,
|
|
29
|
-
),
|
|
30
|
-
/** map path to localized path */
|
|
31
|
-
map(({ action, location }) => ({
|
|
32
|
-
action,
|
|
33
|
-
location: this._localizePath(location),
|
|
34
|
-
})),
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public get navigator(): INavigator {
|
|
39
|
-
return this.#navigator;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public get path(): Path {
|
|
43
|
-
return this._localizePath(this.navigator.location);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
constructor(args: BaseModuleProviderCtorArgs<INavigationConfigurator>) {
|
|
47
|
-
super(args);
|
|
48
|
-
|
|
49
|
-
const { basename, history } = args.config;
|
|
50
|
-
|
|
51
|
-
this.#basePathname = basename;
|
|
52
|
-
|
|
53
|
-
if (!history) {
|
|
54
|
-
throw Error('no history provided!');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
this.#navigator = new Navigator({
|
|
58
|
-
basename,
|
|
59
|
-
history,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
this._addTeardown(() => this.#navigator.dispose());
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public createRouter(routes: AgnosticRouteObject[]) {
|
|
66
|
-
const history = this.#navigator;
|
|
67
|
-
console.debug('NavigationProvider::createRouter', routes);
|
|
68
|
-
const router = createRouter({
|
|
69
|
-
basename: history.basename,
|
|
70
|
-
history,
|
|
71
|
-
routes,
|
|
72
|
-
future: {
|
|
73
|
-
v7_prependBasename: true,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
router.initialize();
|
|
77
|
-
return router;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public createHref(to?: To): string {
|
|
81
|
-
return this.#navigator.createHref(this._createToPath(to ?? this.path));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public createURL(to?: To): URL {
|
|
85
|
-
return this.#navigator.createURL(this._createToPath(to ?? this.path));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public push(to: To, state?: unknown): void {
|
|
89
|
-
this.#navigator.push(this._createToPath(to), state);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
public replace(to: To, state?: unknown): void {
|
|
93
|
-
this.#navigator.replace(this._createToPath(to), state);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
protected _localizePath(location: Path): Path {
|
|
97
|
-
const { pathname, search, hash } = location;
|
|
98
|
-
return {
|
|
99
|
-
// TODO make better check
|
|
100
|
-
pathname: normalizePathname(pathname.replace(this.#basePathname ?? '', '')),
|
|
101
|
-
search,
|
|
102
|
-
hash,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
protected _createToPath(to: To): Partial<Path> {
|
|
107
|
-
const { pathname, search, hash } =
|
|
108
|
-
typeof to === 'string' ? { pathname: to, search: undefined, hash: undefined } : to;
|
|
109
|
-
return {
|
|
110
|
-
pathname: normalizePathname(
|
|
111
|
-
[this.#basePathname, pathname === '/' ? undefined : pathname].filter((x) => !!x).join('/'),
|
|
112
|
-
),
|
|
113
|
-
search,
|
|
114
|
-
hash,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
package/src/navigator.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import type { History, To, Path, Location } from '@remix-run/router';
|
|
2
|
-
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
|
|
3
|
-
|
|
4
|
-
import type { NavigationUpdate, NavigationListener } from './types';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Interface representing a navigator that extends both an observable of navigation updates and the History interface.
|
|
8
|
-
*/
|
|
9
|
-
export interface INavigator<T extends NavigationUpdate = NavigationUpdate>
|
|
10
|
-
extends Observable<T>,
|
|
11
|
-
History {
|
|
12
|
-
readonly basename?: string;
|
|
13
|
-
readonly origin?: string;
|
|
14
|
-
readonly value: NavigationUpdate;
|
|
15
|
-
readonly location: NavigationUpdate['location'];
|
|
16
|
-
dispose: VoidFunction;
|
|
17
|
-
listen(listener: NavigationListener): VoidFunction;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Type guard to check if a given 'to' parameter is a Location object.
|
|
22
|
-
* @param to - The object to check.
|
|
23
|
-
* @returns True if 'to' is a Location object, false otherwise.
|
|
24
|
-
*/
|
|
25
|
-
const isLocation = (to: To): to is Location => typeof to === 'object' && 'key' in to;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Class representing a navigator that manages navigation and history state.
|
|
29
|
-
*/
|
|
30
|
-
export class Navigator<T extends NavigationUpdate = NavigationUpdate>
|
|
31
|
-
extends Observable<T>
|
|
32
|
-
implements INavigator<T>
|
|
33
|
-
{
|
|
34
|
-
#history: History;
|
|
35
|
-
#baseName?: string;
|
|
36
|
-
#origin?: string = window.location.origin;
|
|
37
|
-
#subscriptions = new Subscription();
|
|
38
|
-
#state: BehaviorSubject<T>;
|
|
39
|
-
|
|
40
|
-
#logger: typeof console;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Gets the basename of the navigator.
|
|
44
|
-
*/
|
|
45
|
-
get basename(): string | undefined {
|
|
46
|
-
return this.#baseName;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Gets the origin of the navigator.
|
|
51
|
-
*/
|
|
52
|
-
get origin(): string | undefined {
|
|
53
|
-
return this.#origin;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Gets the current navigation state as an observable.
|
|
58
|
-
*/
|
|
59
|
-
get state(): Observable<T> {
|
|
60
|
-
return this.#state.asObservable();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Gets the current navigation state value.
|
|
65
|
-
*/
|
|
66
|
-
get value(): T {
|
|
67
|
-
return this.#state.value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Gets the current location from the navigation state.
|
|
72
|
-
*/
|
|
73
|
-
get location(): T['location'] {
|
|
74
|
-
return this.value.location;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Gets the current action from the navigation state.
|
|
79
|
-
*/
|
|
80
|
-
get action(): T['action'] {
|
|
81
|
-
return this.value.action;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Navigator constructor.
|
|
86
|
-
* @param args - Configuration options for the navigator.
|
|
87
|
-
*/
|
|
88
|
-
constructor(args: {
|
|
89
|
-
basename?: string;
|
|
90
|
-
history: History;
|
|
91
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
-
logger?: any;
|
|
93
|
-
mode?: 'MASTER' | 'SLAVE';
|
|
94
|
-
}) {
|
|
95
|
-
super((subscriber) => this.#state.subscribe(subscriber));
|
|
96
|
-
const { basename, history } = args;
|
|
97
|
-
this.#logger = args.logger || console;
|
|
98
|
-
this.#baseName = basename;
|
|
99
|
-
this.#history = history;
|
|
100
|
-
this.#state = new BehaviorSubject<T>({
|
|
101
|
-
action: this.#history.action,
|
|
102
|
-
location: this.#history.location,
|
|
103
|
-
delta: null,
|
|
104
|
-
} as T);
|
|
105
|
-
this.#subscriptions.add(
|
|
106
|
-
this.#history.listen((update) => {
|
|
107
|
-
// Prevent duplicate state updates.
|
|
108
|
-
if (update.location.key !== this.#state.value.location.key) {
|
|
109
|
-
this.#state.next(update as T);
|
|
110
|
-
}
|
|
111
|
-
}),
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Encodes a location into a path string.
|
|
117
|
-
* @param to - The location to encode.
|
|
118
|
-
* @returns The encoded path string.
|
|
119
|
-
*/
|
|
120
|
-
public encodeLocation(to: To): Path {
|
|
121
|
-
return this.#history.encodeLocation(to);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Subscribes to navigation updates.
|
|
126
|
-
* @param listener - The listener function to receive updates.
|
|
127
|
-
* @returns A function to unsubscribe the listener.
|
|
128
|
-
*/
|
|
129
|
-
public listen(listener: NavigationListener): VoidFunction {
|
|
130
|
-
const subscription = this.#state.subscribe(listener);
|
|
131
|
-
this.#logger.debug('Navigator::listen', listener);
|
|
132
|
-
return () => {
|
|
133
|
-
this.#logger.debug('Navigator::listen[unsubscribe]', listener);
|
|
134
|
-
return subscription.unsubscribe();
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Creates a URL string for a given location.
|
|
140
|
-
* @param to - The location to create a URL for.
|
|
141
|
-
* @returns The created URL string.
|
|
142
|
-
*/
|
|
143
|
-
public createHref(to: To): string {
|
|
144
|
-
return this.#history.createHref(to);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Navigates to a specific history entry by its relative position to the current entry.
|
|
149
|
-
* @param delta - The relative position to move in the history stack.
|
|
150
|
-
*/
|
|
151
|
-
public go(delta: number): void {
|
|
152
|
-
this.#history.go(delta);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Pushes a new entry onto the history stack.
|
|
157
|
-
* @param to - The location or URL to navigate to.
|
|
158
|
-
* @param state - Optional state to associate with the navigation.
|
|
159
|
-
*/
|
|
160
|
-
public push(to: To, state?: unknown): void {
|
|
161
|
-
const skip = isLocation(to) && this._isDuplicateLocation(to);
|
|
162
|
-
if (!skip) {
|
|
163
|
-
this.#history.push(to, state);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Replaces the current history entry with a new one.
|
|
169
|
-
* @param to - The location or URL to navigate to.
|
|
170
|
-
* @param state - Optional state to associate with the navigation.
|
|
171
|
-
*/
|
|
172
|
-
public replace(to: To, state?: unknown): void {
|
|
173
|
-
const skip = isLocation(to) && this._isDuplicateLocation(to);
|
|
174
|
-
if (!skip) {
|
|
175
|
-
this.#history.replace(to, state);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Creates a full URL for a given location.
|
|
181
|
-
* @param to - The location to create a URL for.
|
|
182
|
-
* @returns The created URL object.
|
|
183
|
-
*/
|
|
184
|
-
public createURL(to: To): URL {
|
|
185
|
-
return this.#history.createURL(to);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Checks if a given location is a duplicate of the current location.
|
|
190
|
-
* @param location - The location to check.
|
|
191
|
-
* @returns True if the location is a duplicate, false otherwise.
|
|
192
|
-
*/
|
|
193
|
-
protected _isDuplicateLocation(location: Location) {
|
|
194
|
-
// TODO: might check if there are changes in the URL compared to the current location.
|
|
195
|
-
return location.key === this.#state.value.location.key;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Disposes of the navigator by unsubscribing from all subscriptions.
|
|
200
|
-
*/
|
|
201
|
-
dispose() {
|
|
202
|
-
this.#subscriptions.unsubscribe();
|
|
203
|
-
}
|
|
204
|
-
}
|
package/src/types.ts
DELETED