useDOM.js 4.1 KB

1
  1. "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useScrollOffset=exports.useRefAll=exports.useRef=exports.useRectAll=exports.useRect=exports.useQuery=exports.useComputedStyle=void 0;var _global=require("../internals/global"),_pxToNumber=require("../shared/pxToNumber");function _slicedToArray(e,r){return _arrayWithHoles(e)||_iterableToArrayLimit(e,r)||_unsupportedIterableToArray(e,r)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,r){var t;if(e)return"string"==typeof e?_arrayLikeToArray(e,r):"Map"===(t="Object"===(t=Object.prototype.toString.call(e).slice(8,-1))&&e.constructor?e.constructor.name:t)||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(e,r):void 0}function _arrayLikeToArray(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,o=new Array(r);t<r;t++)o[t]=e[t];return o}function _iterableToArrayLimit(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var o,n,i,u,l=[],s=!0,a=!1;try{if(i=(t=t.call(e)).next,0===r){if(Object(t)!==t)return;s=!1}else for(;!(s=(o=i.call(t)).done)&&(l.push(o.value),l.length!==r);s=!0);}catch(e){a=!0,n=e}finally{try{if(!s&&null!=t.return&&(u=t.return(),Object(u)!==u))return}finally{if(a)throw n}}return l}}function _arrayWithHoles(e){if(Array.isArray(e))return e}var useQuery=exports.useQuery=function(e){return e?_global.miniprogramThis.createSelectorQuery().in(e):_global.miniprogramThis.createSelectorQuery()},makeFields=function(){return{id:!0,dataset:!0,mark:!0,rect:!0,scrollOffset:!0,computedStyle:["width","height","borderTopWidth","borderRightWidth","borderBottomWidth","borderLeftWidth"],node:!0}},makeNodeRef=function(e){var r=(0,_pxToNumber.pxToNumber)(e.borderRightWidth||0),t=(0,_pxToNumber.pxToNumber)(e.borderLeftWidth||0),o=(0,_pxToNumber.pxToNumber)(e.borderTopWidth||0),n=(0,_pxToNumber.pxToNumber)(e.borderBottomWidth||0),i=(0,_pxToNumber.pxToNumber)(e.width),u=(0,_pxToNumber.pxToNumber)(e.height),r=i+r+t,n=u+o+n;return{id:e.id,dataset:e.dataset,mark:e.mark,top:e.top,right:e.right,bottom:e.bottom,left:e.left,width:r,height:n,x:e.left,y:e.top,offsetWidth:r,offsetHeight:n,clientLeft:t,clientTop:o,clientWidth:i,clientHeight:u,scrollHeight:e.scrollHeight,scrollLeft:e.scrollLeft,scrollTop:e.scrollTop,scrollWidth:e.scrollWidth,node:e.node}},useRef=exports.useRef=function(e,n){return new Promise(function(r){var t=useQuery(n),o=Array.isArray(e);(o?e:[e]).forEach(function(e){t.select(e).fields(makeFields())}),t.exec(function(e){r(o?e.map(function(e){return makeNodeRef(e)}):makeNodeRef(e[0]))})})},useRefAll=exports.useRefAll=function(e,n){return new Promise(function(r){var t=useQuery(n),o=Array.isArray(e);(o?e:[e]).forEach(function(e){t.selectAll(e).fields(makeFields())}),t.exec(function(e){r(o?e.map(function(e){return e.map(function(e){return makeNodeRef(e)})}):e[0].map(function(e){return makeNodeRef(e)}))})})},useRect=exports.useRect=function(e,n){return new Promise(function(r){var t=useQuery(n),o=Array.isArray(e);(o?e:[e]).forEach(function(e){t.select(e).boundingClientRect()}),t.exec(function(e){r(o?e:e[0])})})},useRectAll=exports.useRectAll=function(e,n){return new Promise(function(r){var t=useQuery(n),o=Array.isArray(e);(o?e:[e]).forEach(function(e){t.selectAll(e).boundingClientRect()}),t.exec(function(e){r(o?e:e[0])})})},useScrollOffset=exports.useScrollOffset=function(t){return new Promise(function(r){var e=useQuery(t);e.selectViewport().scrollOffset(),e.exec(function(e){e=_slicedToArray(e,1)[0];r(e)})})},useComputedStyle=exports.useComputedStyle=function(t){var o=2==(arguments.length<=1?0:arguments.length-1)?arguments.length<=1?void 0:arguments[1]:["width","height"],n=2==(arguments.length<=1?0:arguments.length-1)?arguments.length<=2?void 0:arguments[2]:arguments.length<=1?void 0:arguments[1];return new Promise(function(r){var e=useQuery(n);e.select(t).fields({computedStyle:o}),e.exec(function(e){e=_slicedToArray(e,1)[0];r(e)})})};