zhengyiming
2025-02-12 006e8527857850dfd01371384afa532af5a001f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { VNode } from 'vue';
import Taro from '@tarojs/taro';
 
export type PortalNode = VNode | VNode[];
 
export type State = {
  portals: Array<{
    key: number;
    children: PortalNode;
  }>;
};
export type PortalManagerState = {
  portals: Array<{
    key: number;
    children: PortalNode;
  }>;
};
 
export type PortalMethods = {
  mount: (children: PortalNode) => number;
  update: (key: number, children: PortalNode) => void;
  unmount: (key: number) => void;
};
 
export type Operation =
  | { type: 'mount'; key: number; children: PortalNode }
  | { type: 'update'; key: number; children: PortalNode }
  | { type: 'unmount'; key: number };
 
export const PortalContextKey: InjectionKey<PortalMethods> = Symbol('PortalContextKey');
 
export function usePortalContext() {
  return inject(PortalContextKey);
}
 
export function getUniqueKey(key: string) {
  const router = Taro.getCurrentInstance().router;
  const prefix = router.path;
  return `${prefix}_${key}`;
}
 
// events
export const addType = 'ADD_PORTAL';
export const removeType = 'REMOVE_PORTAL';
 
export const TopViewEventEmitter = new Taro.Events();
 
class PortalGuard {
  private nextKey = 10000;
  add = (render: (_key: number) => PortalNode) => {
    const key = this.nextKey++;
    const node = render(key);
    TopViewEventEmitter.trigger(getUniqueKey(addType), node, key);
    return key;
  };
  remove = (key: number) => {
    TopViewEventEmitter.trigger(getUniqueKey(removeType), key);
  };
}
/**
 * portal
 */
export const portal = new PortalGuard();
 
export type PortalManagerInstance = {
  mount: (key: number, children: PortalNode) => void;
  update: (key: number, children: PortalNode) => void;
  unmount: (key: number) => {
    key: number;
    children: PortalNode;
  }[];
};