From 1d4869c225d46cd913a48aac34864a659331d50e Mon Sep 17 00:00:00 2001 From: akira-cn Date: Tue, 11 Dec 2018 15:00:59 +0800 Subject: [PATCH] bug fixed vnode update bug & event update bug --- packages/omi-sprite/dist/omi-sprite.js | 10 +++++++--- packages/omi-sprite/dist/omi-sprite.min.js | 2 +- packages/omi-sprite/examples/simple/bundler.js | 12 ++++++++---- packages/omi-sprite/examples/simple/main.js | 2 +- packages/omi-sprite/package.json | 3 ++- packages/omi-sprite/src/index.js | 10 +++++++--- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/omi-sprite/dist/omi-sprite.js b/packages/omi-sprite/dist/omi-sprite.js index 9f0b2488d..452c91c80 100644 --- a/packages/omi-sprite/dist/omi-sprite.js +++ b/packages/omi-sprite/dist/omi-sprite.js @@ -235,6 +235,7 @@ function updateEvents(oldVnode, vnode) { oldVnode.elm.removeEventListener(type, f); }); + oldVnode.events = []; } if (vnode.elm && attrs) { Object.entries(attrs).forEach(function (_ref3) { @@ -243,9 +244,10 @@ function updateEvents(oldVnode, vnode) { val = _ref4[1]; if (key.indexOf('on') === 0 && typeof val === 'function') { - vnode.elm.addEventListener(key.slice(2).toLowerCase(), val); + var eventType = key.slice(2).toLowerCase(); + vnode.elm.addEventListener(eventType, val); vnode.events = vnode.events || []; - vnode.events.push([key, val]); + vnode.events.push([eventType, val]); delete attrs[key]; } }); @@ -336,7 +338,9 @@ function render(scene, children) { children.forEach(function (layer) { if (!layer.attributes.id) layer.attributes.id = 'layer_' + Math.random().toString(36).slice(2); var layerEl = scene.layer(layer.attributes.id); - patch(layerEl, parseVNode(layer)); + var node = layerEl.vnode_ || layerEl; + layerEl.vnode_ = parseVNode(layer); + patch(node, layerEl.vnode_); }); } diff --git a/packages/omi-sprite/dist/omi-sprite.min.js b/packages/omi-sprite/dist/omi-sprite.min.js index 219b46312..cee66cffa 100644 --- a/packages/omi-sprite/dist/omi-sprite.min.js +++ b/packages/omi-sprite/dist/omi-sprite.min.js @@ -3,4 +3,4 @@ * Github: https://github.com/AlloyTeam/omi * MIT Licensed. */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("spritejs"),require("omi")):"function"==typeof define&&define.amd?define(["spritejs","omi"],t):"object"==typeof exports?exports.OmiSpritejs=t(require("spritejs"),require("omi")):e.OmiSpritejs=t(e.spritejs,e.omi)}("undefined"!=typeof self?self:this,function(e,t){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=4)}([function(t,n){t.exports=e},function(e,t,n){"use strict";function r(e,t,n,r,o){return{sel:e,data:t,children:n,text:r,elm:o,key:void 0===t?void 0:t.key}}t.b=r,t.a=r},function(e,t,n){"use strict";function r(e){return"string"==typeof e||"number"==typeof e}n.d(t,"a",function(){return o}),t.b=r;var o=Array.isArray},function(e,t,n){"use strict";function r(e,t,n){if(e.ns="http://www.w3.org/2000/svg","foreignObject"!==n&&void 0!==t)for(var o=0;o0?f:u.length,v=l>0?l:u.length,h=-1!==f||-1!==l?u.slice(0,Math.min(s,v)):u,m=e.elm=o(i)&&o(n=i.ns)?j.createElementNS(n,h):j.createElement(h);for(s0&&m.setAttribute("class",u.slice(v+1).replace(/\./g," ")),n=0;nh?(c=null==n[x+1]?null:n[x+1].elm,v(e,c,n,p,x,o)):y(e,t,s,h))}function g(e,t,n){var i,a;o(i=t.data)&&o(a=i.hook)&&o(i=a.prepatch)&&i(e,t);var u=t.elm=e.elm,f=e.children,l=t.children;if(e!==t){if(void 0!==t.data){for(i=0;i0?f:u.length,v=l>0?l:u.length,h=-1!==f||-1!==l?u.slice(0,Math.min(s,v)):u,m=e.elm=o(i)&&o(n=i.ns)?j.createElementNS(n,h):j.createElement(h);for(s0&&m.setAttribute("class",u.slice(v+1).replace(/\./g," ")),n=0;nh?(c=null==n[x+1]?null:n[x+1].elm,v(e,c,n,p,x,o)):y(e,t,s,h))}function g(e,t,n){var i,a;o(i=t.data)&&o(a=i.hook)&&o(i=a.prepatch)&&i(e,t);var u=t.elm=e.elm,f=e.children,l=t.children;if(e!==t){if(void 0!==t.data){for(i=0;i { - // console.log(evt) + console.log(evt) this.data.scale = 0.5 + Math.random() * 0.1 } diff --git a/packages/omi-sprite/package.json b/packages/omi-sprite/package.json index d9e6492de..c30e76360 100644 --- a/packages/omi-sprite/package.json +++ b/packages/omi-sprite/package.json @@ -8,7 +8,8 @@ "lazy": "webpack -w", "simple": "webpack -w", "build": "webpack", - "build-min": "webpack" + "build-min": "webpack", + "prepublishOnly": "npm run build && npm run build-min" }, "repository": { "type": "git", diff --git a/packages/omi-sprite/src/index.js b/packages/omi-sprite/src/index.js index 32b44ee3e..ac87a8dce 100644 --- a/packages/omi-sprite/src/index.js +++ b/packages/omi-sprite/src/index.js @@ -18,13 +18,15 @@ function updateEvents(oldVnode, vnode) { oldEvents.forEach(([type, f]) => { oldVnode.elm.removeEventListener(type, f) }) + oldVnode.events = [] } if(vnode.elm && attrs) { Object.entries(attrs).forEach(([key, val]) => { if(key.indexOf('on') === 0 && typeof val === 'function') { - vnode.elm.addEventListener(key.slice(2).toLowerCase(), val) + const eventType = key.slice(2).toLowerCase() + vnode.elm.addEventListener(eventType, val) vnode.events = vnode.events || [] - vnode.events.push([key, val]) + vnode.events.push([eventType, val]) delete attrs[key] } }) @@ -98,6 +100,8 @@ function render(scene, children) { children.forEach((layer) => { if(!layer.attributes.id) layer.attributes.id = `layer_${Math.random().toString(36).slice(2)}` const layerEl = scene.layer(layer.attributes.id) - patch(layerEl, parseVNode(layer)) + const node = layerEl.vnode_ || layerEl + layerEl.vnode_ = parseVNode(layer) + patch(node, layerEl.vnode_) }) }