diff --git a/demo/src/index.ts b/demo/src/index.ts index 20a8b1f..fbd141d 100644 --- a/demo/src/index.ts +++ b/demo/src/index.ts @@ -4,6 +4,7 @@ import { Page } from '@kevisual/store/page'; const page = new Page({ isListen: true, }); +page.basename = ''; page.addPage('/', 'home'); page.addPage('/:id', 'user'); page.subscribe( @@ -21,3 +22,6 @@ page.subscribe('user', (params, state) => { // page.navigate('/c', { id: 3 }); // page.navigate('/c', { id: 2 }); // page.refresh(); + +// @ts-ignore +window.page = page; \ No newline at end of file diff --git a/package.json b/package.json index 858095a..864b6d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/store", - "version": "0.0.1-alpha.5", + "version": "0.0.1-alpha.7", "main": "dist/store.js", "module": "dist/store.js", "types": "dist/store.d.ts", @@ -23,6 +23,7 @@ "license": "ISC", "description": "", "devDependencies": { + "@kevisual/types": "link:../types", "@rollup/plugin-commonjs": "^28.0.1", "@rollup/plugin-node-resolve": "^15.3.0", "@rollup/plugin-typescript": "^12.1.1", diff --git a/rollup.config.js b/rollup.config.js index 02eb0c3..7443441 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -30,7 +30,7 @@ export default [ plugins: [dts()], }, { - input: 'src/web-config.ts', + input: 'src/web-env.ts', output: { file: 'dist/web-config.js', format: 'es', @@ -38,29 +38,13 @@ export default [ plugins: [resolve({ browser: true }), commonjs(), typescript()], }, { - input: 'src/web-config.ts', + input: 'src/web-env.ts', output: { file: 'dist/web-config.d.ts', format: 'es', }, 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', output: { diff --git a/src/page.ts b/src/page.ts index 70c4fe8..4559a84 100644 --- a/src/page.ts +++ b/src/page.ts @@ -2,6 +2,10 @@ import { getPathKey } from '@/utils/path-key.ts'; import { match } from 'path-to-regexp'; import deepEqual from 'fast-deep-equal'; +const generateRandom = () => { + // return Math.random().toString(36).substring(8); + return crypto.randomUUID(); +}; type PageOptions = { path?: string; key?: string; @@ -19,6 +23,7 @@ type CallbackInfo = { fn: CallFn; id: string; } & PageModule; +let currentUrl = location.href; export class Page { pageModule = new Map(); // pathname的第一个路径 @@ -40,6 +45,7 @@ export class Page { } popstate(event?: PopStateEvent, manualOpts?: { id?: string; type: 'singal' | 'all' }) { const pathname = window.location.pathname; + console.log('popstate', event); if (manualOpts) { if (manualOpts.type === 'singal') { const item = this.callbacks.find((item) => item.id === manualOpts.id); @@ -120,7 +126,7 @@ export class Page { */ subscribe(key: string, fn?: CallFn, opts?: { pathname?: string; runImmediately?: boolean; id?: string }) { const runImmediately = opts?.runImmediately ?? true; // 默认立即执行 - const id = opts?.id ?? Math.random().toString(36).slice(2); + const id = opts?.id ?? generateRandom(); const path = this.pageModule.get(key)?.path; if (!path) { console.error(`PageModule ${key} not found`); diff --git a/src/web-config.ts b/src/web-config.ts deleted file mode 100644 index e8bd5d6..0000000 --- a/src/web-config.ts +++ /dev/null @@ -1,41 +0,0 @@ -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 = (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 (key: string, init?: () => Promise): Promise => { - 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); -}; diff --git a/src/web-context.ts b/src/web-context.ts deleted file mode 100644 index 5e30543..0000000 --- a/src/web-context.ts +++ /dev/null @@ -1,41 +0,0 @@ -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 = (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 (key: string, init?: () => Promise): Promise => { - 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); -}; diff --git a/src/web-env.ts b/src/web-env.ts new file mode 100644 index 0000000..fcb5c0a --- /dev/null +++ b/src/web-env.ts @@ -0,0 +1,90 @@ +import { getPathKey } from './utils/path-key.ts'; + +type GlobalEnv = { + name?: string; + [key: string]: any; +}; +export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => { + const env: GlobalEnv = (window as any)[initKey]; + const _env = env || initEnv; + if (!env) { + if (_env) { + (window as any)[initKey] = _env; + } else { + (window as any)[initKey] = {}; + } + } + return window[initKey] as GlobalEnv; +}; + +export const useEnvKey = (key: string, init?: () => T | null, initKey = 'config'): T => { + const _env = useEnv({}, initKey); + if (key && _env[key]) { + return _env[key]; + } + if (key && init) { + _env[key] = init(); + return _env[key]; + } + + return _env as any; +}; + +export const useEnvKeySync = async (key: string, init?: () => Promise | null, initKey = 'conifg'): Promise => { + const _env = useEnv({}, initKey); + if (key && init) { + _env[key] = await init(); + return _env[key]; + } + if (key) { + return _env[key]; + } + return _env as any; +}; + +export const usePageEnv = (init?: () => {}, initKey = 'conifg') => { + const { id } = getPathKey(); + return useEnvKey(id, init, initKey); +}; + +type GlobalContext = { + name?: string; + [key: string]: any; +}; +export const useContext = (initContext?: GlobalContext) => { + return useEnv(initContext, 'context'); +}; + +export const useContextKey = (key: string, init?: () => T): T => { + return useEnvKey(key, init, 'context'); +}; + +export const useContextKeySync = async (key: string, init?: () => Promise): Promise => { + return useEnvKeySync(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 = (key: string, init?: () => T): T => { + return useEnvKey(key, init, 'config'); +}; + +export const useConfigKeySync = async (key: string, init?: () => Promise): Promise => { + return useEnvKeySync(key, init, 'config'); +}; + +export const usePageConfig = (init?: () => {}) => { + const { id } = getPathKey(); + return useConfigKey(id, init); +}; diff --git a/src/web.ts b/src/web.ts index 807e402..b17642b 100644 --- a/src/web.ts +++ b/src/web.ts @@ -1,6 +1,5 @@ export * from './page.ts'; -export * from './web-context.ts'; -export * from './web-config.ts'; +export * from './web-env.ts'; export * from 'nanoid'; export * from 'path-to-regexp'; diff --git a/tsconfig.json b/tsconfig.json index e8e7f97..4591ee9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "baseUrl": "./", "typeRoots": [ "node_modules/@types", + "node_modules/@kevisual/types", ], "declaration": false, "noEmit": true,