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

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

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