Compare commits

..

No commits in common. "e4768b63603c50238dacd19e3d5c43ef6da6610b" and "351b2e336624b05e1b2c9f8b67d9a45cda7ebf9d" have entirely different histories.

10 changed files with 182 additions and 308 deletions

View File

@ -1,28 +1,24 @@
// console.log('index.js'); // console.log('index.js');
// //@ts-ignore // //@ts-ignore
import { Page } from '@kevisual/store/page'; // import { Page } from '@kevisual/store/page';
const page = new Page({ // const page = new Page({
isListen: true, // isListen: true,
}); // });
page.basename = ''; // page.addPage('/', 'home');
page.addPage('/', 'home'); // page.addPage('/:id', 'user');
page.addPage('/:id', 'user'); // page.subscribe(
page.subscribe( // 'home',
'home', // (params, state) => {
(params, state) => { // console.log('home', params, 'state', state);
console.log('home', params, 'state', state); // return;
return; // },
}, // );
); // page.subscribe('user', (params, state) => {
page.subscribe('user', (params, state) => { // console.log('user', params, 'state', state);
console.log('user', params, 'state', state); // return;
return; // });
});
// page.navigate('/c', { id: 3 }); // // page.navigate('/c', { id: 3 });
// page.navigate('/c', { id: 2 }); // // page.navigate('/c', { id: 2 });
// page.refresh(); // // page.refresh();
// @ts-ignore
window.page = page;

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/store", "name": "@kevisual/store",
"version": "0.0.7", "version": "0.0.4",
"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",
@ -26,8 +26,6 @@
"license": "ISC", "license": "ISC",
"description": "", "description": "",
"devDependencies": { "devDependencies": {
"@kevisual/load": "workspace:*",
"@kevisual/types": "link:../types",
"@rollup/plugin-commonjs": "^28.0.3", "@rollup/plugin-commonjs": "^28.0.3",
"@rollup/plugin-node-resolve": "^16.0.1", "@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.2", "@rollup/plugin-typescript": "^12.1.2",
@ -36,16 +34,14 @@
"immer": "^10.1.1", "immer": "^10.1.1",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"nanoid": "^5.1.5", "nanoid": "^5.1.5",
"rollup": "^4.41.1", "rollup": "^4.37.0",
"rollup-plugin-dts": "^6.2.1", "rollup-plugin-dts": "^6.2.1",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"tslib": "^2.8.1", "tslib": "^2.8.1",
"typescript": "^5.8.3", "tsup": "^8.4.0",
"zustand": "^5.0.5", "typescript": "^5.8.2",
"@kevisual/router": "^0.0.21", "vite-plugin-dts": "^4.5.3",
"@rollup/plugin-terser": "^0.4.4", "zustand": "^5.0.3"
"eventemitter3": "^5.0.1",
"path-to-regexp": "^8.2.0"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
@ -64,13 +60,24 @@
"require": "./dist/web-config.js" "require": "./dist/web-config.js"
}, },
"./context": { "./context": {
"import": "./dist/web-config.js", "import": "./dist/web-context.js",
"require": "./dist/web-config.js" "require": "./dist/web-context.js"
}, },
"./web-page.js": { "./web-page.js": {
"import": "./dist/web-page.js", "import": "./dist/web-page.js",
"require": "./dist/web-page.js" "require": "./dist/web-page.js"
},
"./web": {
"import": "./dist/web.js",
"require": "./dist/web.js"
},
"./react": {
"import": "./dist-react/store-react.js",
"types": "./dist-react/index.d.ts"
} }
}, },
"dependencies": {} "dependencies": {
"eventemitter3": "^5.0.1",
"path-to-regexp": "^8.2.0"
}
} }

View File

@ -4,7 +4,6 @@ import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve'; import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs'; import commonjs from '@rollup/plugin-commonjs';
import { dts } from 'rollup-plugin-dts'; import { dts } from 'rollup-plugin-dts';
import terser from '@rollup/plugin-terser';
/** /**
* @type {import('rollup').RollupOptions} * @type {import('rollup').RollupOptions}
@ -31,7 +30,7 @@ export default [
plugins: [dts()], plugins: [dts()],
}, },
{ {
input: 'src/web-env.ts', input: 'src/web-config.ts',
output: { output: {
file: 'dist/web-config.js', file: 'dist/web-config.js',
format: 'es', format: 'es',
@ -39,13 +38,29 @@ export default [
plugins: [resolve({ browser: true }), commonjs(), typescript()], plugins: [resolve({ browser: true }), commonjs(), typescript()],
}, },
{ {
input: 'src/web-env.ts', input: 'src/web-config.ts',
output: { output: {
file: 'dist/web-config.d.ts', file: 'dist/web-config.d.ts',
format: 'es', format: 'es',
}, },
plugins: [dts()], plugins: [dts()],
}, },
{
input: 'src/web-context.ts',
output: {
file: 'dist/web-context.js',
format: 'es',
},
plugins: [resolve({ browser: true }), commonjs(), typescript()],
},
{
input: 'src/web-context.ts',
output: {
file: 'dist/web-context.d.ts',
format: 'es',
},
plugins: [dts()],
},
{ {
input: 'src/page.ts', input: 'src/page.ts',
output: { output: {
@ -62,4 +77,20 @@ export default [
}, },
plugins: [dts()], plugins: [dts()],
}, },
{
input: 'src/web.ts',
output: {
file: 'dist/web.js',
format: 'es',
},
plugins: [resolve({ browser: true }), commonjs(), typescript()],
},
{
input: 'src/web.ts',
output: {
file: 'dist/web.d.ts',
format: 'es',
},
plugins: [dts()],
},
]; ];

View File

@ -1,23 +1,10 @@
import { getPathKey } from '@/utils/path-key.ts'; import { getPathKey } from '@/utils/path-key.ts';
import * as pathToRegexp from 'path-to-regexp'; import { match } from 'path-to-regexp';
import deepEqual from 'fast-deep-equal'; import deepEqual from 'fast-deep-equal';
const generateRandom = () => {
// return Math.random().toString(36).substring(8);
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;
}; };
@ -32,9 +19,6 @@ type CallbackInfo = {
fn: CallFn; fn: CallFn;
id: string; id: string;
} & PageModule; } & PageModule;
/**
* basename,path和key
*/
export class Page { export class Page {
pageModule = new Map<string, PageModule>(); pageModule = new Map<string, PageModule>();
// pathname的第一个路径 // pathname的第一个路径
@ -44,51 +28,18 @@ export class Page {
basename: string; basename: string;
isListen: boolean; isListen: boolean;
callbacks = [] as CallbackInfo[]; callbacks = [] as CallbackInfo[];
ok = false;
constructor(opts?: PageOptions) { constructor(opts?: PageOptions) {
const pathKey = getPathKey(); const pathKey = getPathKey();
this.path = opts?.path ?? pathKey.path; this.path = opts?.path ?? pathKey.path;
this.key = opts?.key ?? pathKey.key; this.key = opts?.key ?? pathKey.key;
if (opts?.basename) { this.basename = opts?.basename ?? `/${this.path}/${this.key}`;
this.basename = opts?.basename;
} else {
if (this.key) {
this.basename = `/${this.path}/${this.key}`;
} else {
const location = window.location;
this.basename = location.pathname;
}
}
this.clearEndSlash();
const isListen = opts?.isListen ?? true; const isListen = opts?.isListen ?? true;
if (isListen) { if (isListen) {
this.listen(); this.listen();
} }
this.ok = !!this.key;
} }
/** popstate(event?: PopStateEvent, manualOpts?: { id?: string; type: 'singal' | 'all' }) {
* const pathname = window.location.pathname;
*/
clearEndSlash() {
this.basename = this.basename.replace(/\/+$/, '');
return this;
}
/**
*
*/
checkPath() {
const pathKey = getPathKey();
const { path, key } = pathKey;
this.path = path || '';
this.key = key || '';
this.ok = !!this.key;
this.basename = `/${this.path}/${this.key}`;
this.clearEndSlash();
return this;
}
popstate(event?: PopStateEvent, manualOpts?: { id?: string; type: 'singal' | 'all'; pathname?: string }) {
const pathname = manualOpts?.pathname ?? window.location.pathname;
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);
@ -105,17 +56,7 @@ 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优先级高于pathnamelocation.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);
} }
@ -142,7 +83,7 @@ export class Page {
}); });
} }
/** /**
* , pathnamepathname *
* @param key * @param key
* @param pathname * @param pathname
* @returns * @returns
@ -159,12 +100,8 @@ export class Page {
} }
const location = window.location; const location = window.location;
const _pathname = pathname || location.pathname; const _pathname = pathname || location.pathname;
if (!_pathname.includes(this.basename)) {
// console.error(`PageModule ${key} not found`);
return false;
}
const cur = _pathname.replace(this.basename, ''); const cur = _pathname.replace(this.basename, '');
const routeMatch = pathToRegexp.match(pageModule.path, { decode: decodeURIComponent }); const routeMatch = match(pageModule.path, { decode: decodeURIComponent });
const result = routeMatch(cur); const result = routeMatch(cur);
let params = {}; let params = {};
if (result) { if (result) {
@ -181,22 +118,20 @@ export class Page {
* @param opts * @param opts
* @returns * @returns
*/ */
async subscribe(key: string, fn?: CallFn, opts?: { pathname?: string; runImmediately?: boolean; id?: string }) { subscribe(key: string, fn?: CallFn, opts?: { pathname?: string; runImmediately?: boolean; id?: string }) {
const runImmediately = opts?.runImmediately ?? true; // 默认立即执行 const runImmediately = opts?.runImmediately ?? true; // 默认立即执行
const id = opts?.id ?? generateRandom(); const id = opts?.id ?? Math.random().toString(36).slice(2);
const pageModule = this.pageModule.get(key); const path = this.pageModule.get(key)?.path;
if (!pageModule) { if (!path) {
console.error(`PageModule ${key} not found`); console.error(`PageModule ${key} not found`);
return () => {}; return () => {};
} }
const path = pageModule?.path || '';
this.callbacks.push({ key, fn, id: id, path }); this.callbacks.push({ key, fn, id: id, path });
if (runImmediately) { if (runImmediately) {
const location = window.location; const location = window.location;
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' });
} }
} }
@ -204,9 +139,6 @@ export class Page {
this.callbacks = this.callbacks.filter((item) => item.id !== id); this.callbacks = this.callbacks.filter((item) => item.id !== id);
}; };
} }
getPathKey() {
return getPathKey();
}
/** /**
* basename * basename
* @returns * @returns
@ -219,26 +151,12 @@ export class Page {
getAppPath() { getAppPath() {
return this.path; return this.path;
} }
/**
* key
* @returns
*/
getAppKey() { getAppKey() {
return this.key; return this.key;
} }
/**
*
* @param path
* @returns
*/
decodePath(path: string) { decodePath(path: string) {
return decodeURIComponent(path); return decodeURIComponent(path);
} }
/**
*
* @param path
* @returns
*/
encodePath(path: string) { encodePath(path: string) {
return encodeURIComponent(path); return encodeURIComponent(path);
} }
@ -246,7 +164,7 @@ export class Page {
* state pathname都相等popstate * state pathname都相等popstate
* @param path * @param path
* @param state * @param state
* @param check , * @param check
* @returns * @returns
*/ */
navigate(path: string | number, state?: any, check?: boolean) { navigate(path: string | number, state?: any, check?: boolean) {
@ -269,45 +187,17 @@ export class Page {
this.popstate({ state } as any, { type: 'all' }); this.popstate({ state } as any, { type: 'all' });
} }
} }
/**
*
* @param path
* @param state
* @param check
*/
replace(path: string, state?: any, check?: boolean) { replace(path: string, state?: any, check?: boolean) {
let _check = check ?? true; let _check = check ?? true;
let newPath = this.basename + path; window.history.replaceState(state, '', this.basename + path);
if (path.startsWith('http')) {
const url = new URL(path);
const origin = url.origin;
newPath = url.toString().replace(origin, '');
}
window.history.replaceState(state, '', newPath);
if (_check) { if (_check) {
this.popstate({ state } as any, { type: 'all' }); this.popstate({ state } as any, { type: 'all' });
} }
} }
/**
*
*/
refresh() { refresh() {
const state = window.history.state; const state = window.history.state;
this.popstate({ state } as any, { type: 'all' }); this.popstate({ state } as any, { type: 'all' });
} }
/**
*
* @param path
* @param checkPath
* @returns
*/
pathMatch(regexpPath: string, checkPath: string) {
return pathToRegexp.match(regexpPath, { decode: decodeURIComponent })(checkPath);
}
pathToRegexp = pathToRegexp;
static match(regexpPath: string, checkPath: string) {
return pathToRegexp.match(regexpPath, { decode: decodeURIComponent })(checkPath);
}
} }
/** /**
* history state * history state
@ -323,16 +213,17 @@ export const getHistoryState = <T = Record<string, any>>() => {
* history state * history state
* @param state * @param state
*/ */
export const setHistoryState = (state: any, url?: string) => { export const setHistoryState = (state: any) => {
const history = window.history; const history = window.history;
const oldState = getHistoryState(); const oldState = getHistoryState();
history.replaceState({ ...oldState, ...state }, '', url || window.location.href); // 只更新 state 而不改变 URL
history.replaceState({ ...oldState, ...state }, '', window.location.href);
}; };
/** /**
* history state * history state
*/ */
export const clearHistoryState = (url?: string) => { export const clearHistoryState = () => {
const history = window.history; const history = window.history;
history.replaceState({}, '', url || window.location.href); history.replaceState({}, '', window.location.href);
}; };

View File

@ -2,8 +2,6 @@ export const getPathKey = () => {
// 从localtion.href的路径中/a/b 中 a为pathb为key // 从localtion.href的路径中/a/b 中 a为pathb为key
const pathname = location.pathname; const pathname = location.pathname;
const paths = pathname.split('/'); const paths = pathname.split('/');
let [path, key] = paths.slice(1); const [path, key] = paths.slice(1);
path = path || '';
key = key || '';
return { path, key, id: path + '---' + key, prefix: `/${path}/${key}` }; return { path, key, id: path + '---' + key, prefix: `/${path}/${key}` };
}; };

41
src/web-config.ts Normal file
View File

@ -0,0 +1,41 @@
import { getPathKey } from './utils/path-key.ts';
type GlobalConfig = {
name?: string;
[key: string]: any;
};
export const useConfig = (initConfig?: GlobalConfig) => {
const config: GlobalConfig = (window as any).config;
const _config = config || initConfig;
!config && ((window as any)['config'] = _config);
return _config;
};
export const useConfigKey = <T>(key: string, init?: () => T): T => {
const _config = useConfig({});
if (key && init) {
_config[key] = init();
return _config[key] as any;
}
if (key) {
return _config[key];
}
return _config as any;
};
export const useConfigKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
const _config = useConfig({});
if (key && init) {
_config[key] = await init();
return _config[key] as any;
}
if (key) {
return _config[key];
}
return _config as any;
};
export const usePageConfig = (init?: () => {}) => {
const { id } = getPathKey();
return useConfigKey(id, init);
};

41
src/web-context.ts Normal file
View File

@ -0,0 +1,41 @@
import { getPathKey } from './utils/path-key.ts';
type GlobalContext = {
name?: string;
[key: string]: any;
};
export const useContext = (initContext?: GlobalContext) => {
const context: GlobalContext = (window as any).context;
const _context = context || initContext;
!context && ((window as any)['context'] = _context);
return _context;
};
export const useContextKey = <T>(key: string, init?: () => T): T => {
const _context = useContext({});
if (key && init) {
_context[key] = init();
return _context[key] as any;
}
if (key) {
return _context[key];
}
return _context as any;
};
export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
const _context = useContext({});
if (key && init) {
_context[key] = await init();
return _context[key] as any;
}
if (key) {
return _context[key];
}
return _context as any;
};
export const usePageContext = (init?: () => {}) => {
const { id } = getPathKey();
return useContextKey(id, init);
};

View File

@ -1,138 +0,0 @@
import { getPathKey } from './utils/path-key.ts';
import { BaseLoad } from '@kevisual/load';
const gt = (globalThis as any) || window || self;
type GlobalEnv = {
name?: string;
[key: string]: any;
};
// 从window对象中获取全局的环境变量如果没有则初始化一个
export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
const env: GlobalEnv = gt[initKey];
const _env = env || initEnv;
if (!env) {
if (_env) {
gt[initKey] = _env;
} else {
gt[initKey] = {};
}
}
return gt[initKey] as GlobalEnv;
};
// 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象
export const useEnvKey = <T = any>(key: string, init?: () => T | null, initKey = 'config'): T => {
const _env = useEnv({}, initKey);
// 已经存在,直接返回
if (key && typeof _env[key] !== 'undefined') {
return _env[key];
}
// 不存在,但是有初始化函数,初始化的返回,同步函数,删除了重新加载?
if (key && init) {
_env[key] = init();
return _env[key];
}
if (key) {
// 加载
const baseLoad = new BaseLoad();
const voidFn = async () => {
return _env[key];
};
const checkFn = async () => {
const loadRes = await baseLoad.load(voidFn, {
key,
isReRun: true,
checkSuccess: () => _env[key],
timeout: 5 * 60 * 1000,
interval: 1000,
//
});
if (loadRes.code !== 200) {
console.error('load key error');
return null;
}
return _env[key];
};
return checkFn() as T;
}
// 不存在,没有初始化函数
console.error('key is empty ');
return null;
};
export const usePageEnv = (init?: () => {}, initKey = 'conifg') => {
const { id } = getPathKey();
return useEnvKey(id, init, initKey);
};
export const useEnvKeyNew = (key: string, initKey = 'conifg', opts?: { getNew?: boolean; init?: () => {} }) => {
const _env = useEnv({}, initKey);
if (key) {
delete _env[key];
}
if (opts?.getNew && opts.init) {
return useEnvKey(key, opts.init, initKey);
} else if (opts?.getNew) {
return useEnvKey(key, null, initKey);
}
};
type GlobalContext = {
name?: string;
[key: string]: any;
};
export const useContext = (initContext?: GlobalContext) => {
return useEnv(initContext, 'context');
};
export const useContextKey = <T = any>(key: string, init?: () => T, isNew?: boolean): T => {
if (isNew) {
return useEnvKeyNew(key, 'context', { getNew: true, init });
}
return useEnvKey(key, init, 'context');
};
export const usePageContext = (init?: () => {}) => {
const { id } = getPathKey();
return useContextKey(id, init);
};
type GlobalConfig = {
name?: string;
[key: string]: any;
};
export const useConfig = (initConfig?: GlobalConfig) => {
return useEnv(initConfig, 'config');
};
export const useConfigKey = <T = any>(key: string, init?: () => T, isNew?: boolean): T => {
if (isNew) {
return useEnvKeyNew(key, 'config', { getNew: true, init });
}
return useEnvKey(key, init, 'config');
};
export const usePageConfig = (init?: () => {}) => {
const { id } = getPathKey();
return useConfigKey(id, init);
};
class InitEnv {
static isInit = false;
static init(opts?: { load?: boolean; page?: boolean }) {
if (InitEnv.isInit) {
return;
}
const { load = true, page = false } = opts || {};
InitEnv.isInit = true;
// bind to window, 必须要的获取全局的环境变量
// @ts-ignore
gt.useConfigKey = useConfigKey;
// @ts-ignore
gt.useContextKey = useContextKey;
// @ts-ignore
gt.webEnv = { useConfigKey, useContextKey };
// @ts-ignore
load && (gt.Load = BaseLoad);
}
}
InitEnv.init();

8
src/web.ts Normal file
View File

@ -0,0 +1,8 @@
export * from './page.ts';
export * from './web-context.ts';
export * from './web-config.ts';
export * from 'nanoid';
export * from 'path-to-regexp';
export * from 'eventemitter3';

View File

@ -10,7 +10,6 @@
"baseUrl": "./", "baseUrl": "./",
"typeRoots": [ "typeRoots": [
"node_modules/@types", "node_modules/@types",
"node_modules/@kevisual/types",
], ],
"declaration": false, "declaration": false,
"noEmit": true, "noEmit": true,