feat: add page
This commit is contained in:
parent
7cb42edc5f
commit
c855c7d3d5
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/store",
|
"name": "@kevisual/store",
|
||||||
"version": "0.0.1-alpha.8",
|
"version": "0.0.1-alpha.9",
|
||||||
"main": "dist/store.js",
|
"main": "dist/store.js",
|
||||||
"module": "dist/store.js",
|
"module": "dist/store.js",
|
||||||
"types": "dist/store.d.ts",
|
"types": "dist/store.d.ts",
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
// 当前的功能,把所有的模块注入到windows对象当中
|
// 当前的功能,把所有的模块注入到windows对象当中
|
||||||
import { useConfigKey, useContextKey } from './web-env.ts';
|
import { useConfigKey, useConfigKeySync, useContextKeySync, useContextKey } from './web-env.ts';
|
||||||
import { QueryRouterServer } from '@kevisual/router/browser';
|
import { QueryRouterServer } from '@kevisual/router/browser';
|
||||||
import { Page } from './page.ts';
|
import { Page } from './page.ts';
|
||||||
// bind to window, 必须要的获取全局的环境变量
|
// bind to window, 必须要的获取全局的环境变量
|
||||||
window.useConfigKey = useConfigKey;
|
window.useConfigKey = useConfigKey;
|
||||||
|
window.useConfigKeySync = useConfigKeySync;
|
||||||
window.useContextKey = useContextKey;
|
window.useContextKey = useContextKey;
|
||||||
|
window.useContextKeySync = useContextKeySync;
|
||||||
window.Page = Page;
|
window.Page = Page;
|
||||||
window.QueryRouterServer = QueryRouterServer;
|
window.QueryRouterServer = QueryRouterServer;
|
||||||
|
|
||||||
// bind to window, 获取路由对象
|
// bind to window, 获取路由对象
|
||||||
useContextKey('app', () => new QueryRouterServer());
|
useContextKey('app', () => new QueryRouterServer());
|
||||||
|
|
||||||
|
31
src/page.ts
31
src/page.ts
@ -7,8 +7,17 @@ const generateRandom = () => {
|
|||||||
return crypto.randomUUID();
|
return crypto.randomUUID();
|
||||||
};
|
};
|
||||||
type PageOptions = {
|
type PageOptions = {
|
||||||
|
/**
|
||||||
|
* 路径
|
||||||
|
*/
|
||||||
path?: string;
|
path?: string;
|
||||||
|
/**
|
||||||
|
* key
|
||||||
|
*/
|
||||||
key?: string;
|
key?: string;
|
||||||
|
/**
|
||||||
|
* basename
|
||||||
|
*/
|
||||||
basename?: string;
|
basename?: string;
|
||||||
isListen?: boolean;
|
isListen?: boolean;
|
||||||
};
|
};
|
||||||
@ -23,7 +32,9 @@ type CallbackInfo = {
|
|||||||
fn: CallFn;
|
fn: CallFn;
|
||||||
id: string;
|
id: string;
|
||||||
} & PageModule;
|
} & PageModule;
|
||||||
let currentUrl = location.href;
|
/**
|
||||||
|
* 根据basename,其中的path和key,来判断一个应用的模块。
|
||||||
|
*/
|
||||||
export class Page {
|
export class Page {
|
||||||
pageModule = new Map<string, PageModule>();
|
pageModule = new Map<string, PageModule>();
|
||||||
// pathname的第一个路径
|
// pathname的第一个路径
|
||||||
@ -43,9 +54,8 @@ export class Page {
|
|||||||
this.listen();
|
this.listen();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
popstate(event?: PopStateEvent, manualOpts?: { id?: string; type: 'singal' | 'all' }) {
|
popstate(event?: PopStateEvent, manualOpts?: { id?: string; type: 'singal' | 'all'; pathname?: string }) {
|
||||||
const pathname = window.location.pathname;
|
const pathname = manualOpts?.pathname ?? window.location.pathname;
|
||||||
console.log('popstate', event);
|
|
||||||
if (manualOpts) {
|
if (manualOpts) {
|
||||||
if (manualOpts.type === 'singal') {
|
if (manualOpts.type === 'singal') {
|
||||||
const item = this.callbacks.find((item) => item.id === manualOpts.id);
|
const item = this.callbacks.find((item) => item.id === manualOpts.id);
|
||||||
@ -62,7 +72,17 @@ export class Page {
|
|||||||
result && item.fn?.(result.params, event.state, { event, manualOpts });
|
result && item.fn?.(result.params, event.state, { event, manualOpts });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 调用callback中id或者pathname的函数, 其中id优先级高于pathname,若都没有,则从location.pathname中获取
|
||||||
|
* @param opts
|
||||||
|
*/
|
||||||
|
call(opts?: { id?: string; pathname?: string }) {
|
||||||
|
this.popstate({ state: window.history.state } as any, { ...opts, type: 'all' });
|
||||||
|
}
|
||||||
listen() {
|
listen() {
|
||||||
|
if (this.isListen) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.isListen = true;
|
this.isListen = true;
|
||||||
window.addEventListener('popstate', this.popstate.bind(this), false);
|
window.addEventListener('popstate', this.popstate.bind(this), false);
|
||||||
}
|
}
|
||||||
@ -89,7 +109,7 @@ export class Page {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 检查当前路径是否匹配
|
* 检查当前路径是否匹配, 如何提交pathname,则检查pathname
|
||||||
* @param key
|
* @param key
|
||||||
* @param pathname
|
* @param pathname
|
||||||
* @returns
|
* @returns
|
||||||
@ -138,6 +158,7 @@ export class Page {
|
|||||||
const pathname = opts?.pathname ?? location.pathname;
|
const pathname = opts?.pathname ?? location.pathname;
|
||||||
const result = this.check(key, pathname);
|
const result = this.check(key, pathname);
|
||||||
if (result) {
|
if (result) {
|
||||||
|
// 如果是手动调用,则不需要检查是否相等,直接执行,而且是执行当前的subscribe的函数
|
||||||
this.popstate({ state: window.history.state } as any, { id, type: 'singal' });
|
this.popstate({ state: window.history.state } as any, { id, type: 'singal' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user