diff --git a/dist/vw-studio-removeEmptyElement.min.js b/dist/vw-studio-removeEmptyElement.min.js index b6b3141..cae7083 100644 --- a/dist/vw-studio-removeEmptyElement.min.js +++ b/dist/vw-studio-removeEmptyElement.min.js @@ -1 +1 @@ -!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=2)}({2:function(e,t){r.$inject=["$window","$actions","$amhEditorService"];function r(e,t,r){t.newAction({id:"studio.extension.rel.removeAll",priority:15,icon:"remove_circle",title:"Remove Emplty Elements",description:"Find and remove empty elements from current document",visible:function(){var e=r.getWorkbench();return!!e&&!!e.getContentValue()},action:function(){var t=r.getWorkbench().getEditors();_.forEach(t,(function(t){var r=t.getRootWidget(),n=0;r&&(n=function e(t){var r=[],n=0,o=t.getChildren();return _.forEach(o,(function(t){_.includes(["img","audio","video"],t.getType())||!_.isEmpty(t.getChildren())||t.getModelProperty("html")||t.getModelProperty("text")?n+=e(t):(r.push(t),n++)})),_.forEach(r,(function(e){e.delete()})),n}(r)),e.alert("Number of widgets which are removed: "+n)}))},groups:["amh.workbench.editor.weburger.toolbar#common"]})}mblowfish.addExtension(r)}}); \ No newline at end of file +!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=2)}({2:function(e,t){r.$inject=["$window","$actions","$amhEditorService"];function n(e){return!_.isString(e)&&0===(e=_.trim(e)).length()}function r(e,t,r){t.newAction({id:"studio.extension.rel.removeAll",priority:15,icon:"remove_circle",title:"Remove Emplty Elements",description:"Find and remove empty elements from current document",visible:function(){var e=r.getWorkbench();return!!e&&!!e.getContentValue()},action:function(){var t=r.getWorkbench().getEditors();_.forEach(t,(function(t){var r=t.getRootWidget(),o=0;r&&(o=function e(t){var r=[],o=0,i=t.getChildren();return _.forEach(i,(function(t){o+=e(t),_.includes(["img","audio","video","ObjectCollection","import"],t.getType())||_.isEmpty(t.getChildren())&&n(t.getModelProperty("html"))&&n(t.getModelProperty("text"))&&(r.push(t),o++)})),_.forEach(r,(function(e){e.delete()})),o}(r)),e.alert("Number of widgets which are removed: "+o)}))},groups:["amh.workbench.editor.weburger.toolbar#common"]})}mblowfish.addExtension(r)}}); \ No newline at end of file diff --git a/src/vw-studio-removeEmptyElement.js b/src/vw-studio-removeEmptyElement.js index 93fe043..15f1c7b 100644 --- a/src/vw-studio-removeEmptyElement.js +++ b/src/vw-studio-removeEmptyElement.js @@ -13,20 +13,30 @@ const ANCHOR_TOOLBAR_COMMON = 'amh.workbench.editor.weburger.toolbar#common'; * Finds all empty widgets from the current editor and remove them all. * **************************************************************************/ - +function isEmpmtyString(str) { + if (_.isString(str)) { + return false; + } + str = _.trim(str); + return str.length() === 0; +} function removeEmptyWidget(root) { var toDelete = []; var count = 0; var children = root.getChildren(); _.forEach(children, function(child) { - if (!(_.includes(['img', 'audio', 'video'], child.getType())) && - _.isEmpty(child.getChildren()) && - !child.getModelProperty('html') && - !child.getModelProperty('text')) { + // 1- remove empty childs + count += removeEmptyWidget(child); + + if (_.includes(['img', 'audio', 'video', 'ObjectCollection', 'import'], child.getType())) { + return; + } + + if (_.isEmpty(child.getChildren()) && + isEmpmtyString(child.getModelProperty('html')) && + isEmpmtyString(child.getModelProperty('text'))) { toDelete.push(child); count++; - } else { - count += removeEmptyWidget(child); } }); _.forEach(toDelete, function(item) {