提交 35b26f8f 编写于 作者: fxy060608's avatar fxy060608

fix(app): do not trigger onRemoveChild when insertBefore (question/154836)

上级 1fe9adeb
......@@ -4,6 +4,21 @@
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Tools = {}));
})(this, (function (exports) { 'use strict';
/**
* Make a map and return a function for checking if a key
* is in that map.
* IMPORTANT: all calls of this function must be prefixed with
* \/\*#\_\_PURE\_\_\*\/
* So that rollup can tree-shake them if necessary.
*/
(process.env.NODE_ENV !== 'production')
? Object.freeze({})
: {};
(process.env.NODE_ENV !== 'production') ? Object.freeze([]) : [];
const hasOwnProperty = Object.prototype.hasOwnProperty;
const hasOwn = (val, key) => hasOwnProperty.call(val, key);
const ACTION_TYPE_PAGE_CREATE = 1;
const ACTION_TYPE_PAGE_CREATED = 2;
const ACTION_TYPE_CREATE = 3;
......@@ -56,7 +71,7 @@
case ACTION_TYPE_CREATE:
return decodeCreateAction(action, getDict);
case ACTION_TYPE_INSERT:
return decodeInsertAction(action);
return decodeInsertAction(action, getDict);
case ACTION_TYPE_REMOVE:
return decodeRemoveAction(action);
case ACTION_TYPE_SET_ATTRIBUTE:
......@@ -84,19 +99,19 @@
if (!nodeJson) {
return;
}
if (nodeJson.a) {
if (hasOwn(nodeJson, 'a')) {
nodeJson.a = getDict(nodeJson.a);
}
if (nodeJson.e) {
if (hasOwn(nodeJson, 'e')) {
nodeJson.e = getDict(nodeJson.e, false);
}
if (nodeJson.w) {
if (hasOwn(nodeJson, 'w')) {
nodeJson.w = getWxsEventDict(nodeJson.w, getDict);
}
if (nodeJson.s) {
if (hasOwn(nodeJson, 's')) {
nodeJson.s = getDict(nodeJson.s);
}
if (nodeJson.t) {
if (hasOwn(nodeJson, 't')) {
nodeJson.t = getDict(nodeJson.t);
}
return nodeJson;
......@@ -118,8 +133,14 @@
decodeNodeJson(getDict, nodeJson),
];
}
function decodeInsertAction([, ...action]) {
return ['insert', ...action];
function decodeInsertAction([, ...action], getDict) {
return [
'insert',
action[0],
action[1],
action[2],
action[3] ? decodeNodeJson(getDict, action[3]) : {},
];
}
function decodeRemoveAction([, ...action]) {
return ['remove', ...action];
......
import { hasOwn } from '@vue/shared'
import {
ACTION_TYPE_ADD_EVENT,
ACTION_TYPE_ADD_WXS_EVENT,
......@@ -77,7 +78,7 @@ export function decodeActions(actions: (PageAction | DictAction)[]) {
case ACTION_TYPE_CREATE:
return decodeCreateAction(action, getDict)
case ACTION_TYPE_INSERT:
return decodeInsertAction(action)
return decodeInsertAction(action, getDict)
case ACTION_TYPE_REMOVE:
return decodeRemoveAction(action)
case ACTION_TYPE_SET_ATTRIBUTE:
......@@ -111,31 +112,31 @@ export function decodeNodeJson(
if (!nodeJson) {
return
}
if (nodeJson.a) {
;(nodeJson as unknown as UniNodeJSON).a = getDict(nodeJson.a) as Record<
if (hasOwn(nodeJson, 'a')) {
;(nodeJson as unknown as UniNodeJSON).a = getDict(nodeJson.a!) as Record<
string,
unknown
>
}
if (nodeJson.e) {
if (hasOwn(nodeJson, 'e')) {
;(nodeJson as unknown as UniNodeJSON).e = getDict(
nodeJson.e,
nodeJson.e!,
false
) as Record<string, number>
}
if (nodeJson.w) {
if (hasOwn(nodeJson, 'w')) {
;(nodeJson as unknown as UniNodeJSON).w = getWxsEventDict(
nodeJson.w,
nodeJson.w!,
getDict
)
}
if (nodeJson.s) {
if (hasOwn(nodeJson, 's')) {
;(nodeJson as unknown as UniNodeJSON).s = getDict(
nodeJson.s as [number, number][]
) as UniCSSStyleDeclarationJSON
}
if (nodeJson.t) {
;(nodeJson as unknown as UniNodeJSON).t = getDict(nodeJson.t) as string
if (hasOwn(nodeJson, 't')) {
;(nodeJson as unknown as UniNodeJSON).t = getDict(nodeJson.t!) as string
}
return nodeJson as unknown as UniNodeJSON
}
......@@ -162,8 +163,14 @@ function decodeCreateAction(
]
}
function decodeInsertAction([, ...action]: InsertAction) {
return ['insert', ...action]
function decodeInsertAction([, ...action]: InsertAction, getDict: GetDict) {
return [
'insert',
action[0],
action[1],
action[2],
action[3] ? decodeNodeJson(getDict, action[3] as UniNodeJSONMinify) : {},
]
}
function decodeRemoveAction([, ...action]: RemoveAction) {
......
......@@ -818,7 +818,12 @@ function sibling(node, type) {
function removeNode(node) {
const { parentNode } = node;
if (parentNode) {
parentNode.removeChild(node);
const { childNodes } = parentNode;
const index = childNodes.indexOf(node);
if (index > -1) {
node.parentNode = null;
childNodes.splice(index, 1);
}
}
}
function checkNodeId(node) {
......@@ -894,6 +899,7 @@ class UniNode extends UniEventTarget {
return cloned;
}
insertBefore(newChild, refChild) {
// 先从现在的父节点移除(注意:不能触发onRemoveChild,否则会生成先remove该 id,再 insert)
removeNode(newChild);
newChild.pageNode = this.pageNode;
newChild.parentNode = this;
......
......@@ -814,7 +814,12 @@ function sibling(node, type) {
function removeNode(node) {
const { parentNode } = node;
if (parentNode) {
parentNode.removeChild(node);
const { childNodes } = parentNode;
const index = childNodes.indexOf(node);
if (index > -1) {
node.parentNode = null;
childNodes.splice(index, 1);
}
}
}
function checkNodeId(node) {
......@@ -890,6 +895,7 @@ class UniNode extends UniEventTarget {
return cloned;
}
insertBefore(newChild, refChild) {
// 先从现在的父节点移除(注意:不能触发onRemoveChild,否则会生成先remove该 id,再 insert)
removeNode(newChild);
newChild.pageNode = this.pageNode;
newChild.parentNode = this;
......
......@@ -30,7 +30,12 @@ function sibling(node: UniNode, type: 'n' | 'p') {
function removeNode(node: UniNode) {
const { parentNode } = node
if (parentNode) {
parentNode.removeChild(node)
const { childNodes } = parentNode
const index = childNodes.indexOf(node)
if (index > -1) {
node.parentNode = null
childNodes.splice(index, 1)
}
}
}
......@@ -169,6 +174,7 @@ export class UniNode extends UniEventTarget {
}
insertBefore(newChild: UniNode, refChild: UniNode | null): UniNode {
// 先从现在的父节点移除(注意:不能触发onRemoveChild,否则会生成先remove该 id,再 insert)
removeNode(newChild)
newChild.pageNode = this.pageNode
newChild.parentNode = this
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册