From f027ac1051dd75169ead064ff424f729083268d9 Mon Sep 17 00:00:00 2001 From: guy Date: Fri, 20 Aug 2021 18:29:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/dev.html | 124 ++++++++------------------------ examples/插件设计.html | 142 +++++++++++++++++++++++++++++++++++++ src/core/wrapper/layout.js | 10 +-- template/index.html | 2 + 4 files changed, 178 insertions(+), 100 deletions(-) create mode 100644 examples/插件设计.html diff --git a/examples/dev.html b/examples/dev.html index beaaff545..6afcc3589 100644 --- a/examples/dev.html +++ b/examples/dev.html @@ -9,85 +9,6 @@
- + + +
+ + + + diff --git a/src/core/wrapper/layout.js b/src/core/wrapper/layout.js index 6ef83324f..b8a2615f1 100644 --- a/src/core/wrapper/layout.js +++ b/src/core/wrapper/layout.js @@ -441,10 +441,10 @@ BI.Layout = BI.inherit(BI.Widget, { } else { var sameOldVnode = findOldVnode(oldCh, newStartVnode, oldStartIdx, oldEndIdx); if (BI.isNull(sameOldVnode)) { // 不存在就把新的放到左边 + delete self._children[self._getChildName(newStartIdx)]; var node = addNode(newStartVnode, newStartIdx); insertBefore(node, oldStartVnode); - newStartVnode = newCh[++newStartIdx]; - } else { // 如果新节点在就旧节点区间中存在就复用一下 + } else { // 如果新节点在旧节点区间中存在就复用一下 BI.each(oldCh, function (index, child) { if (child && sameVnode(child, newStartVnode)) { updated = self.patchItem(sameOldVnode, newStartVnode, index, index) || updated; @@ -453,8 +453,8 @@ BI.Layout = BI.inherit(BI.Widget, { insertBefore(sameOldVnode, oldStartVnode); } }); - newStartVnode = newCh[++newStartIdx]; } + newStartVnode = newCh[++newStartIdx]; } } if (oldStartIdx > oldEndIdx) { @@ -486,7 +486,7 @@ BI.Layout = BI.inherit(BI.Widget, { function addNode (vnode, index) { var opt = self._getOptions(vnode); var key = opt.key == null ? index : opt.key; - return children[key] = self._addElement(key, vnode); + return children[key] = self._addElement(index, vnode); } function addVnodes (before, vnodes, startIdx, endIdx) { @@ -502,7 +502,7 @@ BI.Layout = BI.inherit(BI.Widget, { if (BI.isNotNull(ch)) { var node = self._getOptions(ch); var key = node.key == null ? startIdx : node.key; - delete self._children[self._getChildName(key)]; + delete self._children[self._getChildName(startIdx)]; children[key]._destroy(); } } diff --git a/template/index.html b/template/index.html index 3cd341242..213e71282 100644 --- a/template/index.html +++ b/template/index.html @@ -10,6 +10,8 @@ +
+ From 94eaec6ec320bef2044426b1b17d527e5c2576d9 Mon Sep 17 00:00:00 2001 From: guy Date: Fri, 20 Aug 2021 18:38:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/wrapper/layout.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/core/wrapper/layout.js b/src/core/wrapper/layout.js index b8a2615f1..6f1824b5d 100644 --- a/src/core/wrapper/layout.js +++ b/src/core/wrapper/layout.js @@ -440,19 +440,19 @@ BI.Layout = BI.inherit(BI.Widget, { newStartVnode = newCh[++newStartIdx]; } else { var sameOldVnode = findOldVnode(oldCh, newStartVnode, oldStartIdx, oldEndIdx); - if (BI.isNull(sameOldVnode)) { // 不存在就把新的放到左边 + if (BI.isNull(sameOldVnode[0])) { // 不存在就把新的放到左边 delete self._children[self._getChildName(newStartIdx)]; var node = addNode(newStartVnode, newStartIdx); insertBefore(node, oldStartVnode); } else { // 如果新节点在旧节点区间中存在就复用一下 - BI.each(oldCh, function (index, child) { - if (child && sameVnode(child, newStartVnode)) { - updated = self.patchItem(sameOldVnode, newStartVnode, index, index) || updated; - children[sameOldVnode.key == null ? index : sameOldVnode.key] = self._children[self._getChildName(index)]; - oldCh[index] = undefined; - insertBefore(sameOldVnode, oldStartVnode); - } - }); + var sameOldIndex = sameOldVnode[1]; + updated = self.patchItem(sameOldVnode[0], newStartVnode, sameOldIndex, newStartIdx) || updated; + children[sameOldVnode[0].key == null ? newStartIdx : sameOldVnode[0].key] = self._children[self._getChildName(newStartIdx)] = self._children[self._getChildName(sameOldIndex)]; + if (newStartIdx !== sameOldIndex) { + delete self._children[self._getChildName(sameOldIndex)]; + } + oldCh[sameOldIndex] = undefined; + insertBefore(sameOldVnode[0], oldStartVnode); } newStartVnode = newCh[++newStartIdx]; } @@ -531,13 +531,14 @@ BI.Layout = BI.inherit(BI.Widget, { } function findOldVnode (vnodes, vNode, beginIdx, endIdx) { - var i, found; + var i, found, findIndex; for (i = beginIdx; i <= endIdx; ++i) { if (vnodes[i] && sameVnode(vnodes[i], vNode)) { found = vnodes[i]; + findIndex = i; } } - return found; + return [found, findIndex]; } return updated; From a3414cfba9c817e19c840aba7ad0ca25b7636cbf Mon Sep 17 00:00:00 2001 From: guy Date: Fri, 20 Aug 2021 18:39:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/dev.html | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/dev.html b/examples/dev.html index 6afcc3589..f4a855b3d 100644 --- a/examples/dev.html +++ b/examples/dev.html @@ -28,7 +28,6 @@ type: "bi.button", text: "点击", handler: function () { - debugger; layout.populate([{ type: "bi.label", key: "1", @@ -45,11 +44,7 @@ items: [{ type: "bi.label", key: "123", - text: 1 - }, { - type: "bi.label", - key: "245", - text: 2 + text: "start" }], ref: function (_ref) { layout = _ref;