Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy over router-slot code #887

Merged
merged 6 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist-cms
schemas
temp-schema-generator
APP_PLUGINS
/src/external/router-slot
5 changes: 0 additions & 5 deletions devops/publish/cleanse-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ console.log('[Prepublish] Cleansing package.json');
const packageFile = './package.json';
const packageJson = JSON.parse(readFileSync(packageFile, 'utf8'));

/**
* Here we will modify the package.json to remove dependencies that are not needed in the CMS or does not work on npm.
*/
delete packageJson.dependencies['router-slot'];

// Remove all DevDependencies
delete packageJson.devDependencies;

Expand Down
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
},
"files": [
"dist-cms",
"README.md",
"router-slot-*.*.*.tgz"
"README.md"
],
"repository": {
"url": "https://github.com/umbraco/Umbraco.CMS.Backoffice",
Expand Down Expand Up @@ -133,7 +132,6 @@
"lit": "^2.8.0",
"lodash-es": "4.17.21",
"monaco-editor": "^0.41.0",
"router-slot": "file:router-slot-2.3.0.tgz",
"rxjs": "^7.8.1",
"tinymce": "^6.6.1",
"tinymce-i18n": "^23.8.7",
Expand Down
9 changes: 9 additions & 0 deletions src/external/router-slot/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright © 2018 Andreas Mehlsen andmehlsen@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
9 changes: 9 additions & 0 deletions src/external/router-slot/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PathMatch } from "./model.js";

export const CATCH_ALL_WILDCARD: string = "**";
export const TRAVERSE_FLAG: string = "\\.\\.\\/";
export const PARAM_IDENTIFIER: RegExp = /:([^\\/]+)/g;
export const ROUTER_SLOT_TAG_NAME: string = "router-slot";
export const GLOBAL_ROUTER_EVENTS_TARGET = window;
export const HISTORY_PATCH_NATIVE_KEY: string = `native`;
export const DEFAULT_PATH_MATCH: PathMatch = "prefix";
8 changes: 5 additions & 3 deletions src/external/router-slot/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export * from 'router-slot';
export * from './router-slot.js';
export * from './config.js';
export * from './util/index.js';

export type {
Guard,
IRoute,
Expand All @@ -10,5 +13,4 @@ export type {
PageComponent,
Component,
Params,
} from 'router-slot/model';
export * from 'router-slot/util';
} from './model.js';
178 changes: 178 additions & 0 deletions src/external/router-slot/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
export interface IRouterSlot<D = any, P = any> extends HTMLElement {
readonly route: IRoute<D> | null;
readonly isRoot: boolean;
readonly fragments: IPathFragments | null;
readonly params: Params | null;
readonly match: IRouteMatch<D> | null;
routes: IRoute<D>[];
add: ((routes: IRoute<D>[], navigate?: boolean) => void);
clear: (() => void);
render: (() => Promise<void>);
constructAbsolutePath: ((path: PathFragment) => string);
parent: IRouterSlot<P> | null | undefined;
queryParentRouterSlot: (() => IRouterSlot<P> | null);
}

export type IRoutingInfo<D = any, P = any> = {
slot: IRouterSlot<D, P>,
match: IRouteMatch<D>
};

export type CustomResolver<D = any, P = any> = ((info: IRoutingInfo<D>) => boolean | void | Promise<boolean> | Promise<void>);
export type Guard<D = any, P = any> = ((info: IRoutingInfo<D, P>) => boolean | Promise<boolean>);
export type Cancel = (() => boolean);

export type PageComponent = HTMLElement | undefined;
export type ModuleResolver = Promise<{default: any; /*PageComponent*/}>;
export type Class<T extends PageComponent = PageComponent> = {new (...args: any[]): T;};
export type Component = Class | ModuleResolver | PageComponent | (() => Class) | (() => PromiseLike<Class>) | (() => PageComponent) | (() => PromiseLike<PageComponent>) | (() => ModuleResolver) | (() => PromiseLike<ModuleResolver>);
export type Setup<D = any> = ((component: PageComponent, info: IRoutingInfo<D>) => void);

export type RouterTree<D = any, P = any> = {slot: IRouterSlot<D, P>} & {child?: RouterTree} | null | undefined;
export type PathMatch = "prefix" | "suffix" | "full" | "fuzzy";

/**
* The base route interface.
* D = the data type of the data
*/
export interface IRouteBase<D = any> {

// The path for the route fragment
path: PathFragment;

// Optional metadata
data?: D;

// If guard returns false, the navigation is not allowed
guards?: Guard[];

// The type of match.
// - If "prefix" router-slot will try to match the first part of the path.
// - If "suffix" router-slot will try to match the last part of the path.
// - If "full" router-slot will try to match the entire path.
// - If "fuzzy" router-slot will try to match an arbitrary part of the path.
pathMatch?: PathMatch;
}

/**
* Route type used for redirection.
*/
export interface IRedirectRoute<D = any> extends IRouteBase<D> {

// The paths the route should redirect to. Can either be relative or absolute.
redirectTo: string;

// Whether the query should be preserved when redirecting.
preserveQuery?: boolean;
}

/**
* Route type used to resolve and stamp components.
*/
export interface IComponentRoute<D = any> extends IRouteBase<D> {

// The component loader (should return a module with a default export)
component: Component | PromiseLike<Component>;

// A custom setup function for the instance of the component.
setup?: Setup;
}

/**
* Route type used to take control of how the route should resolve.
*/
export interface IResolverRoute<D = any> extends IRouteBase<D> {

// A custom resolver that handles the route change
resolve: CustomResolver;
}

export type IRoute<D = any> = IRedirectRoute<D> | IComponentRoute<D> | IResolverRoute<D>;
export type PathFragment = string;
export type IPathFragments = {
consumed: PathFragment,
rest: PathFragment
}

export interface IRouteMatch<D = any> {
route: IRoute<D>;
params: Params,
fragments: IPathFragments;
match: RegExpMatchArray;
}

export type PushStateEvent = CustomEvent<null>;
export type ReplaceStateEvent = CustomEvent<null>;
export type ChangeStateEvent = CustomEvent<null>;
export type WillChangeStateEvent = CustomEvent<{ url?: string | null, eventName: GlobalRouterEvent}>;
export type NavigationStartEvent<D = any> = CustomEvent<IRoutingInfo<D>>;
export type NavigationSuccessEvent<D = any> = CustomEvent<IRoutingInfo<D>>;
export type NavigationCancelEvent<D = any> = CustomEvent<IRoutingInfo<D>>;
export type NavigationErrorEvent<D = any> = CustomEvent<IRoutingInfo<D>>;
export type NavigationEndEvent<D = any> = CustomEvent<IRoutingInfo<D>>;

export type Params = {[key: string]: string};
export type Query = {[key: string]: string};

export type EventListenerSubscription = (() => void);

/**
* RouterSlot related events.
*/
export type RouterSlotEvent = "changestate";

/**
* History related events.
*/
export type GlobalRouterEvent =

// An event triggered when a new state is added to the history.
"pushstate"

// An event triggered when the current state is replaced in the history.
| "replacestate"

// An event triggered when a state in the history is popped from the history.
| "popstate"

// An event triggered when the state changes (eg. pop, push and replace)
| "changestate"

// A cancellable event triggered before the history state changes.
| "willchangestate"

// An event triggered when navigation starts.
| "navigationstart"

// An event triggered when navigation is canceled. This is due to a route guard returning false during navigation.
| "navigationcancel"

// An event triggered when navigation fails due to an unexpected error.
| "navigationerror"

// An event triggered when navigation successfully completes.
| "navigationsuccess"

// An event triggered when navigation ends.
| "navigationend";

export interface ISlashOptions {
start: boolean;
end: boolean;
}

/* Extend the global event handlers map with the router related events */
declare global {
interface GlobalEventHandlersEventMap {
"pushstate": PushStateEvent,
"replacestate": ReplaceStateEvent,
"popstate": PopStateEvent,
"changestate": ChangeStateEvent,
"navigationstart": NavigationStartEvent,
"navigationend": NavigationEndEvent,
"navigationsuccess": NavigationSuccessEvent,
"navigationcancel": NavigationCancelEvent,
"navigationerror": NavigationErrorEvent,
"willchangestate": WillChangeStateEvent
}
}
Loading
Loading