From 0c2a7644f3f4982f496e06f5e8da890c049464db Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 20 Apr 2021 17:02:37 +0800 Subject: [PATCH] feat: normalizeNativeEvent --- .../uni-components/src/helpers/useEvent.ts | 20 +- .../uni-core/src/view/plugin/appConfig.ts | 10 +- .../src/view/plugin/componentEvents.ts | 129 --------- .../src/view/plugin/componentInstance.ts | 99 +++++-- .../uni-core/src/view/plugin/componentWxs.ts | 52 ++-- packages/uni-h5-vue/dist/vue.runtime.esm.js | 5 +- packages/uni-h5-vue/lib/vue.runtime.esm.js | 5 +- packages/uni-h5/dist/uni-h5.esm.js | 271 +++++++----------- packages/uni-shared/dist/uni-shared.cjs.js | 10 + packages/uni-shared/dist/uni-shared.d.ts | 7 + packages/uni-shared/dist/uni-shared.esm.js | 11 +- packages/uni-shared/src/dom.ts | 10 + 12 files changed, 268 insertions(+), 361 deletions(-) delete mode 100644 packages/uni-core/src/view/plugin/componentEvents.ts diff --git a/packages/uni-components/src/helpers/useEvent.ts b/packages/uni-components/src/helpers/useEvent.ts index 668700dbf..bf4c5d00e 100644 --- a/packages/uni-components/src/helpers/useEvent.ts +++ b/packages/uni-components/src/helpers/useEvent.ts @@ -1,4 +1,5 @@ import { Ref, SetupContext } from 'vue' +import { normalizeTarget } from '@dcloudio/uni-shared' type EventDetail = Record export type CustomEventTrigger = ReturnType @@ -15,33 +16,18 @@ export function useCustomEvent( } } -function normalizeDataset(el: HTMLElement) { - return el.dataset -} - -function normalizeTarget(el: HTMLElement): WechatMiniprogram.Target { - const { id, offsetTop, offsetLeft } = el - return { - id, - dataset: normalizeDataset(el), - offsetTop, - offsetLeft, - } -} - function normalizeCustomEvent( name: string, domEvt: Event, el: HTMLElement, detail: EventDetail -) { +): WechatMiniprogram.CustomEvent { const target = normalizeTarget(el) - const evt: WechatMiniprogram.CustomEvent = { + return { type: detail.type || name, timeStamp: domEvt.timeStamp || 0, target, currentTarget: target, detail, } - return evt } diff --git a/packages/uni-core/src/view/plugin/appConfig.ts b/packages/uni-core/src/view/plugin/appConfig.ts index 4dac15a06..05ceaf9b3 100644 --- a/packages/uni-core/src/view/plugin/appConfig.ts +++ b/packages/uni-core/src/view/plugin/appConfig.ts @@ -1,13 +1,13 @@ import { AppConfig } from 'vue' -// import { extend } from '@vue/shared' +import { extend } from '@vue/shared' -// import * as instance from './componentInstance' +import * as instance from './componentInstance' -import { getComponentDescriptor, handleWxsEvent } from './componentWxs' +import { getComponentDescriptor } from './componentWxs' export function initAppConfig(appConfig: AppConfig) { const globalProperties = appConfig.globalProperties - // extend(globalProperties, instance) + extend(globalProperties, instance) if (__UNI_FEATURE_WXS__) { globalProperties.getComponentDescriptor = getComponentDescriptor Object.defineProperty(globalProperties, '$ownerInstance', { @@ -15,6 +15,6 @@ export function initAppConfig(appConfig: AppConfig) { return this.$getComponentDescriptor(this) }, }) - globalProperties.$handleWxsEvent = handleWxsEvent + // globalProperties.$handleWxsEvent = handleWxsEvent } } diff --git a/packages/uni-core/src/view/plugin/componentEvents.ts b/packages/uni-core/src/view/plugin/componentEvents.ts deleted file mode 100644 index 4664a5ae9..000000000 --- a/packages/uni-core/src/view/plugin/componentEvents.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { extend } from '@vue/shared' - -import { getWindowOffset } from '../../helpers/getWindowOffset' - -export function findUniTarget($event: Event, $el: HTMLElement): HTMLElement { - let target = $event.target as HTMLElement - for (; target && target !== $el; target = target.parentNode as HTMLElement) { - if (target.tagName && target.tagName.indexOf('UNI-') === 0) { - break - } - } - return target -} - -export function normalizeDataset(dataset: DOMStringMap = {}) { - // ios8.x,9.x Object.assign({},dataset) 始终返回 {} - // http://ask.dcloud.net.cn/question/70246 - const result = JSON.parse(JSON.stringify(dataset)) - // 暂不处理该逻辑了,理论上应该没什么影响 - // if (__PLATFORM__ === 'h5') { - // removeScoped(result) - // } - return result -} - -export function normalizeEvent( - name: string, - $event: Event, - detail: Record = {}, - target: HTMLElement | null, - currentTarget: HTMLElement | null -) { - if (($event as any)._processed) { - ;($event as any).type = detail.type || name - return $event - } - - // fixed 针对小程序 click(tap)事件,补充事件详情 - if (isClickEvent($event, name)) { - const { top } = getWindowOffset() - detail = { - x: $event.x, - y: $event.y - top, - } - normalizeClickEvent($event) - } - - const ret = { - _processed: true, - type: detail.type || name, - timeStamp: $event.timeStamp || 0, - detail: detail, - target: normalizeTarget(target, detail), - currentTarget: normalizeTarget(currentTarget), - touches: normalizeTouchList(($event as any).touches), - changedTouches: normalizeTouchList(($event as any).changedTouches), - preventDefault() {}, - stopPropagation() {}, - } - - if (__PLATFORM__ === 'app' && currentTarget) { - const nid = currentTarget.getAttribute('_i') - ;(ret as any).options = { - nid, - } - // 保留原始 currentTarget 方便后续对比 - ;(ret as any).$origCurrentTarget = currentTarget - } - - return ret -} - -function normalizeClickEvent($event: MouseEvent) { - ;($event as any).touches = ($event as any).changedTouches = [ - { - force: 1, - identifier: 0, - clientX: $event.clientX, - clientY: $event.clientY, - pageX: $event.pageX, - pageY: $event.pageY, - }, - ] -} - -function isClickEvent(val: unknown, name: string): val is MouseEvent { - return name === 'click' -} - -function normalizeTarget( - target: HTMLElement | null, - detail?: Record | undefined -) { - if (!target) { - target = {} as HTMLElement - } - const res = { - id: target.id, - offsetLeft: target.offsetLeft, - offsetTop: target.offsetTop, - dataset: normalizeDataset(target.dataset), - } - if (detail) { - extend(res, detail) - } - return res -} - -function normalizeTouchList(touches: unknown) { - if (touches && touches instanceof TouchList) { - const res = [] - - const { top } = getWindowOffset() - - for (let i = 0; i < touches.length; i++) { - const touch = touches[i] - res.push({ - identifier: touch.identifier, - pageX: touch.pageX, - pageY: touch.pageY - top, - clientX: touch.clientX, - clientY: touch.clientY - top, - force: touch.force || 0, - }) - } - return res - } - return [] -} diff --git a/packages/uni-core/src/view/plugin/componentInstance.ts b/packages/uni-core/src/view/plugin/componentInstance.ts index 96d024ea7..e7b81bad9 100644 --- a/packages/uni-core/src/view/plugin/componentInstance.ts +++ b/packages/uni-core/src/view/plugin/componentInstance.ts @@ -1,28 +1,87 @@ import { ComponentPublicInstance } from 'vue' -import { normalizeEvent, findUniTarget } from './componentEvents' +import { normalizeTarget } from '@dcloudio/uni-shared' +import { getWindowOffset } from '../../helpers/getWindowOffset' -export function $trigger( +const isClickEvent = (val: Event): val is MouseEvent => val.type === 'click' +const isMouseEvent = (val: Event): val is MouseEvent => + val.type.indexOf('mouse') === 0 + +export function $normalizeNativeEvent( this: ComponentPublicInstance, - name: string, - $event: Event, - detail?: Record + evt: Event ) { - const target = this.$el - this.$emit(name, normalizeEvent(name, $event, detail, target, target)) + const { currentTarget } = evt + if (!(evt instanceof Event) || !(currentTarget instanceof HTMLElement)) { + return evt + } + if (currentTarget.tagName.indexOf('UNI-') !== 0) { + return evt + } + + const res = createNativeEvent(evt) + + if (isClickEvent(evt)) { + normalizeClickEvent((res as unknown) as WechatMiniprogram.Touch, evt) + } else if (__PLATFORM__ === 'h5' && isMouseEvent(evt)) { + normalizeMouseEvent((res as unknown) as WechatMiniprogram.Touch, evt) + } + + return res +} + +function createNativeEvent(evt: Event) { + const { type, timeStamp, currentTarget } = evt + const target = normalizeTarget(currentTarget as HTMLElement) + return { + type, + timeStamp, + target, + detail: {}, + currentTarget: target, + preventDefault() { + if (__DEV__) { + console.warn( + 'preventDefault is only supported in h5, use `.prevent` instead.' + ) + } + return evt.preventDefault() + }, + stopPropagation() { + if (__DEV__) { + console.warn( + 'stopPropagation is only supported in h5, use `.stop` instead.' + ) + } + return evt.stopPropagation() + }, + } +} + +function normalizeClickEvent( + evt: WechatMiniprogram.Touch, + mouseEvt: MouseEvent +) { + const { x, y } = mouseEvt + const { top } = getWindowOffset() + evt.detail = { x, y: y - top } + evt.touches = evt.changedTouches = [createTouchEvent(mouseEvt)] +} + +function normalizeMouseEvent(evt: Record, mouseEvt: MouseEvent) { + const { top } = getWindowOffset() + evt.pageX = mouseEvt.pageX + evt.pageY = mouseEvt.pageY - top + evt.clientX = mouseEvt.clientX + evt.clientY = mouseEvt.clientY - top } -export function $handleEvent(this: ComponentPublicInstance, $event: Event) { - // 未处理的 event 对象 需要对 target 校正及包装 - // 查找 uniTarget - if ($event instanceof Event) { - const target = findUniTarget($event, this.$el) - return normalizeEvent( - $event.type, - $event, - {}, - target || $event.target, - $event.currentTarget as HTMLElement - ) +function createTouchEvent(evt: MouseEvent) { + return { + force: 1, + identifier: 0, + clientX: evt.clientX, + clientY: evt.clientY, + pageX: evt.pageX, + pageY: evt.pageY, } - return $event } diff --git a/packages/uni-core/src/view/plugin/componentWxs.ts b/packages/uni-core/src/view/plugin/componentWxs.ts index 4a03c0d4a..40d48d359 100644 --- a/packages/uni-core/src/view/plugin/componentWxs.ts +++ b/packages/uni-core/src/view/plugin/componentWxs.ts @@ -1,6 +1,6 @@ import { ComponentPublicInstance } from 'vue' import { isFunction, isPlainObject } from '@vue/shared' -import { normalizeEvent, findUniTarget } from './componentEvents' +// import { normalizeEvent, findUniTarget } from './componentEvents' interface WxsElement extends HTMLElement { __wxsStyle: Record @@ -213,28 +213,28 @@ export function getComponentDescriptor( return createComponentDescriptor(instance || this, isOwnerInstance) } -export function handleWxsEvent(this: ComponentPublicInstance, $event: Event) { - if (!($event instanceof Event)) { - return $event - } - const currentTarget = $event.currentTarget as WxsElement - const instance = - currentTarget && - currentTarget.__vue__ && - getComponentDescriptor.call(this, currentTarget.__vue__, false) - const $origEvent = $event - $event = normalizeEvent( - $origEvent.type, - $origEvent, - {}, - findUniTarget($origEvent, this.$el) || $origEvent.target, - $origEvent.currentTarget as HTMLElement - ) as Event - ;($event as any).instance = instance - $event.preventDefault = function () { - return $origEvent.preventDefault() - } - $event.stopPropagation = function () { - return $origEvent.stopPropagation() - } -} +// export function handleWxsEvent(this: ComponentPublicInstance, $event: Event) { +// if (!($event instanceof Event)) { +// return $event +// } +// const currentTarget = $event.currentTarget as WxsElement +// const instance = +// currentTarget && +// currentTarget.__vue__ && +// getComponentDescriptor.call(this, currentTarget.__vue__, false) +// const $origEvent = $event +// $event = normalizeEvent( +// $origEvent.type, +// $origEvent, +// {}, +// findUniTarget($origEvent, this.$el) || $origEvent.target, +// $origEvent.currentTarget as HTMLElement +// ) as Event +// ;($event as any).instance = instance +// $event.preventDefault = function () { +// return $origEvent.preventDefault() +// } +// $event.stopPropagation = function () { +// return $origEvent.stopPropagation() +// } +// } diff --git a/packages/uni-h5-vue/dist/vue.runtime.esm.js b/packages/uni-h5-vue/dist/vue.runtime.esm.js index c46aadd82..fca38b3eb 100644 --- a/packages/uni-h5-vue/dist/vue.runtime.esm.js +++ b/packages/uni-h5-vue/dist/vue.runtime.esm.js @@ -8396,7 +8396,10 @@ function createInvoker(initialValue, instance) { // AFTER it was attached. const timeStamp = e.timeStamp || _getNow(); if (timeStamp >= invoker.attached - 1) { - callWithAsyncErrorHandling(patchStopImmediatePropagation(e, invoker.value), instance, 5 /* NATIVE_EVENT_HANDLER */, [e]); + // fixed by xxxxxx + const proxy = instance && instance.proxy; + const normalizeNativeEvent = proxy && proxy.$normalizeNativeEvent; + callWithAsyncErrorHandling(patchStopImmediatePropagation(e, invoker.value), instance, 5 /* NATIVE_EVENT_HANDLER */, [normalizeNativeEvent ? normalizeNativeEvent(e) : e]); } }; invoker.value = initialValue; diff --git a/packages/uni-h5-vue/lib/vue.runtime.esm.js b/packages/uni-h5-vue/lib/vue.runtime.esm.js index 2afff7026..3706494d2 100644 --- a/packages/uni-h5-vue/lib/vue.runtime.esm.js +++ b/packages/uni-h5-vue/lib/vue.runtime.esm.js @@ -8396,7 +8396,10 @@ function createInvoker(initialValue, instance) { // AFTER it was attached. const timeStamp = e.timeStamp || _getNow(); if (timeStamp >= invoker.attached - 1) { - callWithAsyncErrorHandling(patchStopImmediatePropagation(e, invoker.value), instance, 5 /* NATIVE_EVENT_HANDLER */, [e]); + // fixed by xxxxxx + const proxy = instance && instance.proxy; + const normalizeNativeEvent = proxy && proxy.$normalizeNativeEvent; + callWithAsyncErrorHandling(patchStopImmediatePropagation(e, invoker.value), instance, 5 /* NATIVE_EVENT_HANDLER */, [normalizeNativeEvent ? normalizeNativeEvent(e) : e]); } }; invoker.value = initialValue; diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index 86c078de1..86f94bb5f 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -1,13 +1,13 @@ import {isFunction, extend, isPlainObject, isString, invokeArrayFns as invokeArrayFns$1, hyphenate, isArray, hasOwn as hasOwn$1, isObject as isObject$1, capitalize, toRawType, makeMap as makeMap$1, isPromise} from "@vue/shared"; import {injectHook, createVNode, inject, provide, reactive, computed, nextTick, getCurrentInstance, onBeforeMount, onMounted, onBeforeActivate, onBeforeDeactivate, openBlock, createBlock, mergeProps, toDisplayString, ref, defineComponent, resolveComponent, toHandlers, renderSlot, watch, onActivated, onBeforeUnmount, withModifiers, withDirectives, vShow, vModelDynamic, createCommentVNode, createTextVNode, Fragment, renderList, vModelText, watchEffect, withCtx, KeepAlive, resolveDynamicComponent} from "vue"; -import {once, passive, invokeArrayFns, NAVBAR_HEIGHT, parseQuery, decodedQuery, plusReady, debounce, PRIMARY_COLOR as PRIMARY_COLOR$1, removeLeadingSlash, getLen, updateElementStyle} from "@dcloudio/uni-shared"; +import {once, passive, normalizeTarget, invokeArrayFns, NAVBAR_HEIGHT, parseQuery, decodedQuery, plusReady, debounce, PRIMARY_COLOR as PRIMARY_COLOR$1, removeLeadingSlash, getLen, updateElementStyle} from "@dcloudio/uni-shared"; import {useRoute, createRouter, createWebHistory, createWebHashHistory, isNavigationFailure, RouterView} from "vue-router"; -function applyOptions(options, instance, publicThis) { +function applyOptions(options, instance2, publicThis) { Object.keys(options).forEach((name) => { if (name.indexOf("on") === 0) { const hook = options[name]; if (isFunction(hook)) { - injectHook(name, hook.bind(publicThis), instance); + injectHook(name, hook.bind(publicThis), instance2); } } }); @@ -604,7 +604,7 @@ var safeAreaInsets = { onChange, offChange }; -var D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out = safeAreaInsets; +var out = safeAreaInsets; function getWindowOffset() { const style = document.documentElement.style; const top = parseInt(style.getPropertyValue("--window-top")); @@ -612,103 +612,81 @@ function getWindowOffset() { const left = parseInt(style.getPropertyValue("--window-left")); const right = parseInt(style.getPropertyValue("--window-right")); return { - top: top ? top + D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top : 0, - bottom: bottom ? bottom + D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.bottom : 0, - left: left ? left + D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.left : 0, - right: right ? right + D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.right : 0 + top: top ? top + out.top : 0, + bottom: bottom ? bottom + out.bottom : 0, + left: left ? left + out.left : 0, + right: right ? right + out.right : 0 }; } -function findUniTarget($event, $el) { - let target = $event.target; - for (; target && target !== $el; target = target.parentNode) { - if (target.tagName && target.tagName.indexOf("UNI-") === 0) { - break; - } +const isClickEvent = (val) => val.type === "click"; +const isMouseEvent = (val) => val.type.indexOf("mouse") === 0; +function $normalizeNativeEvent(evt) { + const {currentTarget} = evt; + if (!(evt instanceof Event) || !(currentTarget instanceof HTMLElement)) { + return evt; } - return target; -} -function normalizeDataset$1(dataset = {}) { - const result = JSON.parse(JSON.stringify(dataset)); - return result; -} -function normalizeEvent$1(name, $event, detail = {}, target, currentTarget) { - if ($event._processed) { - $event.type = detail.type || name; - return $event; + if (currentTarget.tagName.indexOf("UNI-") !== 0) { + return evt; } - if (isClickEvent($event, name)) { - const {top} = getWindowOffset(); - detail = { - x: $event.x, - y: $event.y - top - }; - normalizeClickEvent($event); + const res = createNativeEvent(evt); + if (isClickEvent(evt)) { + normalizeClickEvent(res, evt); + } else if (isMouseEvent(evt)) { + normalizeMouseEvent(res, evt); } - const ret = { - _processed: true, - type: detail.type || name, - timeStamp: $event.timeStamp || 0, - detail, - target: normalizeTarget$1(target, detail), - currentTarget: normalizeTarget$1(currentTarget), - touches: normalizeTouchList($event.touches), - changedTouches: normalizeTouchList($event.changedTouches), + return res; +} +function createNativeEvent(evt) { + const {type, timeStamp, currentTarget} = evt; + const target = normalizeTarget(currentTarget); + return { + type, + timeStamp, + target, + detail: {}, + currentTarget: target, preventDefault() { + if (process.env.NODE_ENV !== "production") { + console.warn("preventDefault is only supported in h5, use `.prevent` instead."); + } + return evt.preventDefault(); }, stopPropagation() { + if (process.env.NODE_ENV !== "production") { + console.warn("stopPropagation is only supported in h5, use `.stop` instead."); + } + return evt.stopPropagation(); } }; - return ret; } -function normalizeClickEvent($event) { - $event.touches = $event.changedTouches = [ - { - force: 1, - identifier: 0, - clientX: $event.clientX, - clientY: $event.clientY, - pageX: $event.pageX, - pageY: $event.pageY - } - ]; +function normalizeClickEvent(evt, mouseEvt) { + const {x, y} = mouseEvt; + const {top} = getWindowOffset(); + evt.detail = {x, y: y - top}; + evt.touches = evt.changedTouches = [createTouchEvent(mouseEvt)]; } -function isClickEvent(val, name) { - return name === "click"; +function normalizeMouseEvent(evt, mouseEvt) { + const {top} = getWindowOffset(); + evt.pageX = mouseEvt.pageX; + evt.pageY = mouseEvt.pageY - top; + evt.clientX = mouseEvt.clientX; + evt.clientY = mouseEvt.clientY - top; } -function normalizeTarget$1(target, detail) { - if (!target) { - target = {}; - } - const res = { - id: target.id, - offsetLeft: target.offsetLeft, - offsetTop: target.offsetTop, - dataset: normalizeDataset$1(target.dataset) +function createTouchEvent(evt) { + return { + force: 1, + identifier: 0, + clientX: evt.clientX, + clientY: evt.clientY, + pageX: evt.pageX, + pageY: evt.pageY }; - if (detail) { - extend(res, detail); - } - return res; -} -function normalizeTouchList(touches) { - if (touches && touches instanceof TouchList) { - const res = []; - const {top} = getWindowOffset(); - for (let i2 = 0; i2 < touches.length; i2++) { - const touch = touches[i2]; - res.push({ - identifier: touch.identifier, - pageX: touch.pageX, - pageY: touch.pageY - top, - clientX: touch.clientX, - clientY: touch.clientY - top, - force: touch.force || 0 - }); - } - return res; - } - return []; } +var instance = /* @__PURE__ */ Object.freeze({ + __proto__: null, + [Symbol.toStringTag]: "Module", + $normalizeNativeEvent +}); const CLASS_RE = /^\s+|\s+$/g; const WXS_CLASS_RE = /\s+/; function getWxsClsArr(clsArr, classList, isAdd) { @@ -859,27 +837,12 @@ function createComponentDescriptor(vm, isOwnerInstance = true) { return vm.$el.__wxsComponentDescriptor; } } -function getComponentDescriptor(instance, isOwnerInstance) { - return createComponentDescriptor(instance || this, isOwnerInstance); -} -function handleWxsEvent($event) { - if (!($event instanceof Event)) { - return $event; - } - const currentTarget = $event.currentTarget; - const instance = currentTarget && currentTarget.__vue__ && getComponentDescriptor.call(this, currentTarget.__vue__, false); - const $origEvent = $event; - $event = normalizeEvent$1($origEvent.type, $origEvent, {}, findUniTarget($origEvent, this.$el) || $origEvent.target, $origEvent.currentTarget); - $event.instance = instance; - $event.preventDefault = function() { - return $origEvent.preventDefault(); - }; - $event.stopPropagation = function() { - return $origEvent.stopPropagation(); - }; +function getComponentDescriptor(instance2, isOwnerInstance) { + return createComponentDescriptor(instance2 || this, isOwnerInstance); } function initAppConfig$1(appConfig) { const globalProperties = appConfig.globalProperties; + extend(globalProperties, instance); if (__UNI_FEATURE_WXS__) { globalProperties.getComponentDescriptor = getComponentDescriptor; Object.defineProperty(globalProperties, "$ownerInstance", { @@ -887,7 +850,6 @@ function initAppConfig$1(appConfig) { return this.$getComponentDescriptor(this); } }); - globalProperties.$handleWxsEvent = handleWxsEvent; } } function initView(app) { @@ -1037,8 +999,8 @@ function getRealRoute(fromRoute, toRoute) { fromRouteArray.splice(fromRouteArray.length - i2 - 1, i2 + 1); return "/" + fromRouteArray.concat(toRouteArray).join("/"); } -function errorHandler(err, instance, info) { - if (!instance) { +function errorHandler(err, instance2, info) { + if (!instance2) { throw err; } const app = getApp(); @@ -1101,7 +1063,7 @@ function normalizePageMeta(pageMeta) { let offset = rpx2px(refreshOptions.offset); const {type} = navigationBar; if (type !== "transparent" && type !== "none") { - offset += NAVBAR_HEIGHT + D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top; + offset += NAVBAR_HEIGHT + out.top; } refreshOptions.height = rpx2px(refreshOptions.height); refreshOptions.range = rpx2px(refreshOptions.range); @@ -1356,9 +1318,9 @@ function usePageRoute() { function wrapperComponentSetup(comp, {init: init2, setup, after}) { const oldSetup = comp.setup; comp.setup = (props2, ctx) => { - const instance = getCurrentInstance(); - init2(instance.proxy); - setup(instance); + const instance2 = getCurrentInstance(); + init2(instance2.proxy); + setup(instance2); if (oldSetup) { return oldSetup(props2, ctx); } @@ -1376,32 +1338,32 @@ function setupComponent(comp, options) { function setupPage(comp) { return setupComponent(comp, { init: initPage, - setup(instance) { + setup(instance2) { const route = usePageRoute(); if (route.meta.isTabBar) { - instance.__isActive = true; + instance2.__isActive = true; } onBeforeMount(() => { - const {onLoad, onShow} = instance; + const {onLoad, onShow} = instance2; onLoad && invokeArrayFns$1(onLoad, decodedQuery(route.query)); - instance.__isVisible = true; + instance2.__isVisible = true; onShow && invokeArrayFns$1(onShow); }); onMounted(() => { - const {onReady} = instance; + const {onReady} = instance2; onReady && invokeArrayFns$1(onReady); }); onBeforeActivate(() => { - if (!instance.__isVisible) { - instance.__isVisible = true; - const {onShow} = instance; + if (!instance2.__isVisible) { + instance2.__isVisible = true; + const {onShow} = instance2; onShow && invokeArrayFns$1(onShow); } }); onBeforeDeactivate(() => { - if (instance.__isVisible && !instance.__isUnload) { - instance.__isVisible = false; - const {onHide} = instance; + if (instance2.__isVisible && !instance2.__isUnload) { + instance2.__isVisible = false; + const {onHide} = instance2; onHide && invokeArrayFns$1(onHide); } }); @@ -1411,10 +1373,10 @@ function setupPage(comp) { function setupApp(comp) { return setupComponent(comp, { init: initApp, - setup(instance) { + setup(instance2) { const route = usePageRoute(); onBeforeMount(() => { - const {onLaunch, onShow} = instance; + const {onLaunch, onShow} = instance2; onLaunch && invokeArrayFns$1(onLaunch, { path: route.meta.route, query: decodedQuery(route.query), @@ -5766,28 +5728,15 @@ function useCustomEvent(ref2, emit) { emit(name, normalizeCustomEvent(name, evt, ref2.value, detail || {})); }; } -function normalizeDataset(el) { - return el.dataset; -} -function normalizeTarget(el) { - const {id: id2, offsetTop, offsetLeft} = el; - return { - id: id2, - dataset: normalizeDataset(el), - offsetTop, - offsetLeft - }; -} function normalizeCustomEvent(name, domEvt, el, detail) { const target = normalizeTarget(el); - const evt = { + return { type: detail.type || name, timeStamp: domEvt.timeStamp || 0, target, currentTarget: target, detail }; - return evt; } var ResizeSensor = /* @__PURE__ */ defineComponent({ name: "ResizeSensor", @@ -6087,14 +6036,14 @@ function useFormField(nameKey, valueKey) { if (!uniForm) { return; } - const instance = getCurrentInstance(); + const instance2 = getCurrentInstance(); const ctx = { submit() { - const proxy = instance.proxy; + const proxy = instance2.proxy; return [proxy[nameKey], proxy[valueKey]]; }, reset() { - instance.proxy[valueKey] = ""; + instance2.proxy[valueKey] = ""; } }; uniForm.addField(ctx); @@ -6202,9 +6151,9 @@ const _sfc_main$g = { formElem.appendChild(this.$refs.input); this.$refs.wrapper.appendChild(formElem); } - const instance = getCurrentInstance(); - if (instance && instance.vnode.scopeId) { - this.$refs.placeholder.setAttribute(instance.vnode.scopeId, ""); + const instance2 = getCurrentInstance(); + if (instance2 && instance2.vnode.scopeId) { + this.$refs.placeholder.setAttribute(instance2.vnode.scopeId, ""); } this.initKeyboard(this.$refs.input); }, @@ -9884,16 +9833,16 @@ function removeSubscribe(componentId, vm) { UniViewJSBridge.unsubscribe(normalizeEvent(componentId, vm)); } function useSubscribe(callback) { - const instance = getCurrentInstance().proxy; + const instance2 = getCurrentInstance().proxy; onMounted(() => { - addSubscribe(instance.id, instance, callback); - watch(() => instance.id, (value, oldValue) => { - addSubscribe(value, instance, callback); - removeSubscribe(oldValue, instance); + addSubscribe(instance2.id, instance2, callback); + watch(() => instance2.id, (value, oldValue) => { + addSubscribe(value, instance2, callback); + removeSubscribe(oldValue, instance2); }); }); onBeforeMount(() => { - removeSubscribe(instance.id, instance); + removeSubscribe(instance2.id, instance2); }); } const passiveOptions = passive(false); @@ -10828,7 +10777,7 @@ const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { const windowWidth = getWindowWidth(screenWidth); let windowHeight = window.innerHeight; const language = navigator.language; - const statusBarHeight = D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top; + const statusBarHeight = out.top; let osname; let osversion; let model; @@ -10941,12 +10890,12 @@ const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { const system = `${osname} ${osversion}`; const platform = osname.toLocaleLowerCase(); const safeArea = { - left: D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.left, - right: windowWidth - D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.right, - top: D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top, - bottom: windowHeight - D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.bottom, - width: windowWidth - D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.left - D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.right, - height: windowHeight - D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top - D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.bottom + left: out.left, + right: windowWidth - out.right, + top: out.top, + bottom: windowHeight - out.bottom, + width: windowWidth - out.left - out.right, + height: windowHeight - out.top - out.bottom }; const {top: windowTop, bottom: windowBottom} = getWindowOffset(); windowHeight -= windowTop; @@ -10966,10 +10915,10 @@ const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { model, safeArea, safeAreaInsets: { - top: D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top, - right: D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.right, - bottom: D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.bottom, - left: D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.left + top: out.top, + right: out.right, + bottom: out.bottom, + left: out.left } }; }); diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index 92f722a47..8702ef383 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -10,6 +10,15 @@ function passive(passive) { function normalizeDataset(el) { // TODO return JSON.parse(JSON.stringify(el.dataset || {})); +} +function normalizeTarget(el) { + const { id, offsetTop, offsetLeft } = el; + return { + id, + dataset: normalizeDataset(el), + offsetTop, + offsetLeft, + }; } function plusReady(callback) { @@ -247,6 +256,7 @@ exports.isBuiltInComponent = isBuiltInComponent; exports.isCustomElement = isCustomElement; exports.isNativeTag = isNativeTag; exports.normalizeDataset = normalizeDataset; +exports.normalizeTarget = normalizeTarget; exports.once = once; exports.parseQuery = parseQuery; exports.passive = passive; diff --git a/packages/uni-shared/dist/uni-shared.d.ts b/packages/uni-shared/dist/uni-shared.d.ts index 1568f42e6..1d68f1320 100644 --- a/packages/uni-shared/dist/uni-shared.d.ts +++ b/packages/uni-shared/dist/uni-shared.d.ts @@ -37,6 +37,13 @@ export declare const NAVBAR_HEIGHT = 44; export declare function normalizeDataset(el: Element): any; +export declare function normalizeTarget(el: HTMLElement): { + id: string; + dataset: any; + offsetTop: number; + offsetLeft: number; +}; + export declare function once(fn: (...args: any[]) => any, ctx?: unknown): (...args: any[]) => any; /** diff --git a/packages/uni-shared/dist/uni-shared.esm.js b/packages/uni-shared/dist/uni-shared.esm.js index 0525ecbaa..f9070617f 100644 --- a/packages/uni-shared/dist/uni-shared.esm.js +++ b/packages/uni-shared/dist/uni-shared.esm.js @@ -6,6 +6,15 @@ function passive(passive) { function normalizeDataset(el) { // TODO return JSON.parse(JSON.stringify(el.dataset || {})); +} +function normalizeTarget(el) { + const { id, offsetTop, offsetLeft } = el; + return { + id, + dataset: normalizeDataset(el), + offsetTop, + offsetLeft, + }; } function plusReady(callback) { @@ -224,4 +233,4 @@ const RESPONSIVE_MIN_WIDTH = 768; const COMPONENT_NAME_PREFIX = 'VUni'; const PRIMARY_COLOR = '#007aff'; -export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, debounce, decode, decodedQuery, getLen, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, once, parseQuery, passive, plusReady, removeLeadingSlash, stringifyQuery, updateElementStyle }; +export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, debounce, decode, decodedQuery, getLen, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, stringifyQuery, updateElementStyle }; diff --git a/packages/uni-shared/src/dom.ts b/packages/uni-shared/src/dom.ts index 21e842633..3570d7175 100644 --- a/packages/uni-shared/src/dom.ts +++ b/packages/uni-shared/src/dom.ts @@ -6,3 +6,13 @@ export function normalizeDataset(el: Element) { // TODO return JSON.parse(JSON.stringify((el as HTMLElement).dataset || {})) } + +export function normalizeTarget(el: HTMLElement) { + const { id, offsetTop, offsetLeft } = el + return { + id, + dataset: normalizeDataset(el), + offsetTop, + offsetLeft, + } +} -- GitLab