Skip to content

Commit 160da0c

Browse files
committed
♻️ refactor(表现): 替换 key, 可以完成简单的 back 操作
1 parent ca3d1db commit 160da0c

15 files changed

+1278
-1171
lines changed
File renamed without changes.
File renamed without changes.

dist/vue-page-stack.cjs.js

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-page-stack.cjs.js.map

-1
This file was deleted.

dist/vue-page-stack.es.js

+122-111
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,61 @@
1-
import { defineComponent as S, getCurrentInstance as b, queuePostFlushCb as R, onMounted as K, onUpdated as g, onBeforeUnmount as v, isVNode as w, cloneVNode as q, setTransitionHooks as B, callWithAsyncErrorHandling as W } from "vue";
2-
import { useRoute as G } from "vue-router";
3-
const E = {
1+
import { defineComponent as M, getCurrentInstance as S, queuePostFlushCb as y, onMounted as w, onUpdated as K, onBeforeUnmount as v, isVNode as x, cloneVNode as W, callWithAsyncErrorHandling as B, setTransitionHooks as q } from "vue";
2+
import "vue-router";
3+
const s = {
44
componentName: "VuePageStack",
55
keyName: "stack-key",
66
pushName: "push",
77
goName: "go",
88
replaceName: "replace",
99
backName: "back",
1010
forwardName: "forward"
11+
}, p = {
12+
action: s.pushName
1113
};
12-
var p;
13-
(function(e) {
14-
e[e.ELEMENT = 1] = "ELEMENT", e[e.FUNCTIONAL_COMPONENT = 2] = "FUNCTIONAL_COMPONENT", e[e.STATEFUL_COMPONENT = 4] = "STATEFUL_COMPONENT", e[e.TEXT_CHILDREN = 8] = "TEXT_CHILDREN", e[e.ARRAY_CHILDREN = 16] = "ARRAY_CHILDREN", e[e.SLOTS_CHILDREN = 32] = "SLOTS_CHILDREN", e[e.TELEPORT = 64] = "TELEPORT", e[e.SUSPENSE = 128] = "SUSPENSE", e[e.COMPONENT_SHOULD_KEEP_ALIVE = 256] = "COMPONENT_SHOULD_KEEP_ALIVE", e[e.COMPONENT_KEPT_ALIVE = 512] = "COMPONENT_KEPT_ALIVE", e[e.COMPONENT = 6] = "COMPONENT";
15-
})(p || (p = {}));
16-
const V = (e, n) => {
17-
for (let r = 0; r < e.length; r++)
18-
e[r](n);
14+
let C;
15+
const G = new Uint8Array(16);
16+
function X() {
17+
if (!C && (C = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !C))
18+
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
19+
return C(G);
20+
}
21+
const i = [];
22+
for (let n = 0; n < 256; ++n)
23+
i.push((n + 256).toString(16).slice(1));
24+
function Y(n, e = 0) {
25+
return i[n[e + 0]] + i[n[e + 1]] + i[n[e + 2]] + i[n[e + 3]] + "-" + i[n[e + 4]] + i[n[e + 5]] + "-" + i[n[e + 6]] + i[n[e + 7]] + "-" + i[n[e + 8]] + i[n[e + 9]] + "-" + i[n[e + 10]] + i[n[e + 11]] + i[n[e + 12]] + i[n[e + 13]] + i[n[e + 14]] + i[n[e + 15]];
26+
}
27+
const j = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), b = {
28+
randomUUID: j
29+
};
30+
function z(n, e, c) {
31+
if (b.randomUUID && !e && !n)
32+
return b.randomUUID();
33+
n = n || {};
34+
const o = n.random || (n.rng || X)();
35+
if (o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, e) {
36+
c = c || 0;
37+
for (let E = 0; E < 16; ++E)
38+
e[c + E] = o[E];
39+
return e;
40+
}
41+
return Y(o);
42+
}
43+
var N;
44+
(function(n) {
45+
n[n.ELEMENT = 1] = "ELEMENT", n[n.FUNCTIONAL_COMPONENT = 2] = "FUNCTIONAL_COMPONENT", n[n.STATEFUL_COMPONENT = 4] = "STATEFUL_COMPONENT", n[n.TEXT_CHILDREN = 8] = "TEXT_CHILDREN", n[n.ARRAY_CHILDREN = 16] = "ARRAY_CHILDREN", n[n.SLOTS_CHILDREN = 32] = "SLOTS_CHILDREN", n[n.TELEPORT = 64] = "TELEPORT", n[n.SUSPENSE = 128] = "SUSPENSE", n[n.COMPONENT_SHOULD_KEEP_ALIVE = 256] = "COMPONENT_SHOULD_KEEP_ALIVE", n[n.COMPONENT_KEPT_ALIVE = 512] = "COMPONENT_KEPT_ALIVE", n[n.COMPONENT = 6] = "COMPONENT";
46+
})(N || (N = {}));
47+
const V = (n, e) => {
48+
for (let c = 0; c < n.length; c++)
49+
n[c](e);
1950
};
20-
function h(e, n, r, o) {
21-
W(e, n, Y.VNODE_HOOK, [r, o]);
51+
function R(n, e, c, o) {
52+
B(n, e, Q.VNODE_HOOK, [c, o]);
2253
}
23-
const X = (e) => e.__isSuspense, I = {
54+
const J = (n) => n.__isSuspense, I = {
2455
ENTER: 0,
2556
LEAVE: 1,
2657
REORDER: 2
27-
}, Y = {
58+
}, Q = {
2859
SETUP_FUNCTION: 0,
2960
RENDER_FUNCTION: 1,
3061
WATCH_GETTER: 2,
@@ -34,125 +65,105 @@ const X = (e) => e.__isSuspense, I = {
3465
COMPONENT_EVENT_HANDLER: 6,
3566
VNODE_HOOK: 7
3667
};
37-
function H(e) {
38-
e.shapeFlag &= ~p.COMPONENT_SHOULD_KEEP_ALIVE, e.shapeFlag &= ~p.COMPONENT_KEPT_ALIVE;
68+
function g(n) {
69+
n.shapeFlag &= ~N.COMPONENT_SHOULD_KEEP_ALIVE, n.shapeFlag &= ~N.COMPONENT_KEPT_ALIVE;
3970
}
40-
function P(e) {
41-
return e.shapeFlag & p.SUSPENSE ? e.ssContent : e;
71+
function k(n) {
72+
return n.shapeFlag & N.SUSPENSE ? n.ssContent : n;
4273
}
43-
const N = [], U = (e) => {
44-
for (let n = 0; n < N.length; n++)
45-
if (N[n].key === e)
46-
return n;
47-
return -1;
48-
}, j = (e) => S({
49-
name: E.componentName,
74+
const T = [], Z = (n) => M({
75+
name: s.componentName,
5076
__isKeepAlive: !0,
51-
setup(n, { slots: r }) {
52-
const o = b(), c = o.ctx, u = o.suspense, {
77+
setup(e, { slots: c }) {
78+
const o = S(), E = o.ctx, u = o.suspense, {
5379
renderer: {
54-
p: T,
55-
m: L,
56-
um: x,
57-
o: { createElement: k }
80+
p: d,
81+
m,
82+
um: D,
83+
o: { createElement: H }
5884
}
59-
} = c, y = k("div");
60-
c.activate = (t, O, l, a, s) => {
61-
const i = t.component;
62-
L(t, O, l, I.ENTER, u), T(i.vnode, t, O, l, i, u, a, t.slotScopeIds, s), R(() => {
63-
i.isDeactivated = !1, i.a && V(i.a);
64-
const _ = t.props && t.props.onVnodeMounted;
65-
_ && h(_, i.parent, t);
85+
} = E, f = H("div");
86+
E.activate = (t, r, a, P, O) => {
87+
const l = t.component;
88+
m(t, r, a, I.ENTER, u), d(l.vnode, t, r, a, l, u, P, t.slotScopeIds, O), y(() => {
89+
l.isDeactivated = !1, l.a && V(l.a);
90+
const U = t.props && t.props.onVnodeMounted;
91+
U && R(U, l.parent, t);
6692
}, u);
67-
}, c.deactivate = (t) => {
68-
const O = t.component;
69-
L(t, y, null, I.LEAVE, u), R(() => {
70-
O.da && V(O.da);
71-
const l = t.props && t.props.onVnodeUnmounted;
72-
l && h(l, O.parent, t), O.isDeactivated = !0;
93+
}, E.deactivate = (t) => {
94+
const r = t.component;
95+
m(t, f, null, I.LEAVE, u), y(() => {
96+
r.da && V(r.da);
97+
const a = t.props && t.props.onVnodeUnmounted;
98+
a && R(a, r.parent, t), r.isDeactivated = !0;
7399
}, u);
74100
};
75-
function D(t) {
76-
H(t), x(t, o, u, !0);
101+
function h(t) {
102+
g(t), D(t, o, u, !0);
77103
}
78-
let f = null, C = !1;
104+
let L = null, _ = !1;
79105
const A = () => {
80-
f != null && (C ? N[N.length - 1].vnode = P(o.subTree) : N.push({ key: f, vnode: P(o.subTree) }));
106+
L && (_ ? T[T.length - 1].vnode = k(o.subTree) : (console.log("cacheSubtree and push"), T.push({ vnode: k(o.subTree) }), console.log(T)));
81107
};
82-
return K(A), g(A), v(() => {
83-
for (const t of N)
84-
D(t.vnode);
108+
return w(A), K(A), v(() => {
109+
for (const t of T)
110+
h(t.vnode);
85111
}), () => {
86-
f = null, C = !1;
87-
const O = G().query[e];
88-
if (!r.default)
112+
if (_ = !1, !c.default)
89113
return null;
90-
const l = r.default(), a = l[0];
91-
if (l.length > 1)
92-
return l;
93-
if (!w(a) || !(a.shapeFlag & p.STATEFUL_COMPONENT) && !(a.shapeFlag & p.SUSPENSE))
94-
return a;
95-
let s = P(a);
96-
s.el && (s = q(s), a.shapeFlag & p.SUSPENSE && (a.ssContent = s)), f = O;
97-
let i = U(O);
98-
if (i !== -1) {
99-
const _ = N[i].vnode;
100-
s.el = _.el, s.component = _.component, s.transition && B(s, s.transition), s.shapeFlag |= p.COMPONENT_KEPT_ALIVE;
101-
for (let m = i + 1; m < N.length; m++)
102-
H(N[m].vnode), N[m] = null;
103-
N.splice(i + 1), C = !0;
114+
const t = c.default(), r = t[0];
115+
if (t.length > 1)
116+
return t;
117+
if (!x(r) || !(r.shapeFlag & N.STATEFUL_COMPONENT) && !(r.shapeFlag & N.SUSPENSE))
118+
return r;
119+
let a = k(r);
120+
console.log("vnode.key", a.key);
121+
const P = z();
122+
if (a.el && (a = W(a), r.shapeFlag & N.SUSPENSE && (r.ssContent = a)), L = P, p.action === s.backName) {
123+
console.log("back");
124+
let O = T.pop();
125+
const l = T[T.length - 1].vnode;
126+
a.el = l.el, a.component = l.component, a.transition && q(a, a.transition), a.shapeFlag |= N.COMPONENT_KEPT_ALIVE, g(O.vnode), O = null, _ = !0;
104127
}
105-
return s.shapeFlag |= p.COMPONENT_SHOULD_KEEP_ALIVE, X(a.type) ? a : s;
128+
return a.shapeFlag |= N.COMPONENT_SHOULD_KEEP_ALIVE, J(r.type) ? r : a;
106129
};
107130
}
108-
}), d = {
109-
action: E.pushName
110-
}, z = (e) => {
111-
const n = e.push.bind(e), r = e.go.bind(e), o = e.replace.bind(e), c = e.back.bind(e), u = e.forward.bind(e);
112-
e.push = (T) => (d.action = E.pushName, n(T)), e.go = (T) => {
113-
d.action = E.goName, r(T);
114-
}, e.replace = (T) => (d.action = E.replaceName, o(T)), e.back = () => {
115-
d.action = E.backName, c();
116-
}, e.forward = () => {
117-
d.action = E.forwardName, u();
131+
}), $ = (n) => {
132+
const e = n.push.bind(n), c = n.go.bind(n), o = n.replace.bind(n), E = n.back.bind(n), u = n.forward.bind(n);
133+
n.push = (d) => (p.action = s.pushName, e(d)), n.go = (d) => {
134+
p.action = s.goName, c(d);
135+
}, n.replace = (d) => (p.action = s.replaceName, o(d)), n.back = () => {
136+
p.action = s.backName, E();
137+
}, n.forward = () => {
138+
p.action = s.forwardName, u();
118139
};
119-
};
120-
function M(e, n) {
121-
return !!e[n];
122-
}
123-
function J(e) {
124-
return e.replace(/[xy]/g, (n) => {
125-
const r = Math.random() * 16 | 0;
126-
return (n === "x" ? r : r & 3 | 8).toString(16);
127-
});
128-
}
129-
const $ = {
130-
install(e, { router: n, name: r = E.componentName, keyName: o = E.keyName }) {
131-
if (!n)
140+
}, F = {
141+
install(n, { router: e, backCallback: c, forwardCallback: o } = {
142+
router: null,
143+
backCallback: null,
144+
forwardCallback: null
145+
}) {
146+
if (!e)
147+
throw Error("router is required");
148+
let E = null;
149+
e.options.history.listen((u, d, m) => {
150+
E = m;
151+
}), e.beforeEach(() => {
152+
E && (E.direction === "back" && c ? c() : E.direction === "forward" && o && o(), E = null);
153+
});
154+
}
155+
}, nn = () => {
156+
p.action = s.backName;
157+
}, on = {
158+
install(n, { router: e, name: c = s.componentName, keyName: o = s.keyName }) {
159+
if (!e)
132160
throw Error(`
133161
vue-router is necessary.
134162
135163
`);
136-
e.component(r, j(o)), z(n), n.beforeEach((c, u) => {
137-
if (M(c.query, o))
138-
U(c.query[o]) === -1 ? c.params[o + "-dir"] = E.forwardName : c.params[o + "-dir"] = E.backName;
139-
else {
140-
c.query[o] = J("xxxxxxxx");
141-
const T = d.action === E.replaceName || !M(u.query, o);
142-
return {
143-
hash: c.hash,
144-
path: c.path,
145-
name: c.name,
146-
params: c.params,
147-
query: c.query,
148-
meta: c.meta,
149-
replace: T
150-
};
151-
}
152-
});
164+
n.component(c, Z()), n.use(F, { router: e, backCallback: nn }), $(e);
153165
}
154166
};
155167
export {
156-
$ as default
168+
on as default
157169
};
158-
//# sourceMappingURL=vue-page-stack.es.js.map

dist/vue-page-stack.es.js.map

-1
This file was deleted.

0 commit comments

Comments
 (0)