提交 daf34f35 编写于 作者: D DCloud_LXH

feat: getSelectedTextRange

上级 8b3d8a85
......@@ -10,6 +10,8 @@ export * from './service/ui/createIntersectionObserver'
export * from './service/ui/createSelectorQuery'
export * from './service/ui/tabBar'
export * from './service/keyboard/getSelectedTextRange'
// protocols
export * from './protocols/base/canIUse'
......@@ -28,6 +30,7 @@ export * from './protocols/file/getFileInfo'
export * from './protocols/file/openDocument'
export * from './protocols/keyboard/keyboard'
export * from './protocols/keyboard/getSelectedTextRange'
export * from './protocols/location/chooseLocation'
export * from './protocols/location/getLocation'
......
export const API_GET_SELECTED_TEXT_RANGE = 'getSelectedTextRange'
export type API_TYPE_GET_SELECTED_TEXT_RANGE = typeof uni.getSelectedTextRange
import {
createCallbacks,
getCurrentPageId,
ServiceJSBridge,
} from '@dcloudio/uni-core'
import {
API_GET_SELECTED_TEXT_RANGE,
API_TYPE_GET_SELECTED_TEXT_RANGE,
} from '../../protocols/keyboard/getSelectedTextRange'
import { defineAsyncApi } from '../../helpers/api'
const getSelectedTextRangeEventCallbacks = createCallbacks(
'getSelectedTextRangeEvent'
)
ServiceJSBridge.subscribe &&
ServiceJSBridge.subscribe(
'onGetSelectedTextRange',
({ callbackId, data }: { callbackId: number; data: Data }) => {
const callback = getSelectedTextRangeEventCallbacks.pop(callbackId)
if (callback) {
callback(data)
}
}
)
export const getSelectedTextRange =
defineAsyncApi<API_TYPE_GET_SELECTED_TEXT_RANGE>(
API_GET_SELECTED_TEXT_RANGE,
(_, { resolve, reject }) => {
const pageId = getCurrentPageId()
ServiceJSBridge.publishHandler &&
ServiceJSBridge.publishHandler(
'getSelectedTextRange',
{
pageId,
callbackId: getSelectedTextRangeEventCallbacks.push(function (
res: UniApp.GetSelectedTextRangeSuccessCallbackResult
) {
if (
typeof res.end === 'undefined' &&
typeof res.start === 'undefined'
) {
reject('no focused')
} else {
resolve(res)
}
}),
},
pageId
)
}
)
......@@ -683,6 +683,7 @@ function useMovableViewState(
'px) translateZ(0px) scale(' +
scale +
')'
// TODO 使用 uni.previewImage 点击关闭时,由于组件销毁此处会报错
rootRef.value!.style.transform = transform
rootRef.value!.style.webkitTransform = transform
_translateX = x
......
......@@ -19,6 +19,41 @@ import {
} from './useKeyboard'
import { useScopedAttrs } from './useScopedAttrs'
import { useFormField } from './useFormField'
import { getCurrentPageId } from '@dcloudio/uni-core'
const pageIds: number[] = []
const UniViewJSBridgeSubscribe = function () {
const pageId = getCurrentPageId()
if (pageIds.includes(pageId)) return
pageIds.push(pageId)
UniViewJSBridge.subscribe(
pageId + '.getSelectedTextRange',
function ({ pageId, callbackId }: { pageId: number; callbackId: number }) {
const activeElement = document.activeElement
if (!activeElement) return
const tagName = activeElement.tagName.toLowerCase()
const tagNames = ['input', 'textarea']
const data: {
errMsg?: string
start?: number | null
end?: number | null
} = {}
if (tagNames.includes(tagName)) {
data.start = (activeElement as HTMLInputElement).selectionStart
data.end = (activeElement as HTMLInputElement).selectionEnd
}
UniViewJSBridge.publishHandler(
'onGetSelectedTextRange',
{
callbackId,
data,
},
pageId
)
}
)
}
// App 延迟获取焦点
const FOCUS_DELAY = 200
......@@ -356,6 +391,7 @@ export function useField(
emit: SetupContext['emit'],
beforeInput?: (event: Event, state: State) => any
) {
UniViewJSBridgeSubscribe()
const { fieldRef, state, trigger } = useBase(props, rootRef, emit)
const { triggerInput } = useValueSync(props, state, emit, trigger)
useAutoFocus(props, fieldRef)
......
......@@ -914,6 +914,13 @@ ServiceJSBridge.subscribe("onCanvasMethodCallback", ({callbackId, data}) => {
}
});
const API_ON_TAB_BAR_MID_BUTTON_TAP = "onTabBarMidButtonTap";
const getSelectedTextRangeEventCallbacks = createCallbacks("getSelectedTextRangeEvent");
ServiceJSBridge.subscribe && ServiceJSBridge.subscribe("onGetSelectedTextRange", ({callbackId, data}) => {
const callback = getSelectedTextRangeEventCallbacks.pop(callbackId);
if (callback) {
callback(data);
}
});
const API_GET_STORAGE = "getStorage";
const GetStorageProtocol = {
key: {
......@@ -2984,6 +2991,29 @@ function useFormField(nameKey, value) {
};
uniForm.addField(ctx);
}
const pageIds = [];
const UniViewJSBridgeSubscribe = function() {
const pageId = getCurrentPageId();
if (pageIds.includes(pageId))
return;
pageIds.push(pageId);
UniViewJSBridge.subscribe(pageId + ".getSelectedTextRange", function({pageId: pageId2, callbackId}) {
const activeElement = document.activeElement;
if (!activeElement)
return;
const tagName = activeElement.tagName.toLowerCase();
const tagNames = ["input", "textarea"];
const data = {};
if (tagNames.includes(tagName)) {
data.start = activeElement.selectionStart;
data.end = activeElement.selectionEnd;
}
UniViewJSBridge.publishHandler("onGetSelectedTextRange", {
callbackId,
data
}, pageId2);
});
};
function getValueString(value) {
return value === null ? "" : String(value);
}
......@@ -3208,6 +3238,7 @@ function useEvent(fieldRef, state, trigger, triggerInput, beforeInput) {
vue.watch(() => fieldRef.value, initField);
}
function useField(props2, rootRef, emit2, beforeInput) {
UniViewJSBridgeSubscribe();
const {fieldRef, state, trigger} = useBase(props2, rootRef, emit2);
const {triggerInput} = useValueSync(props2, state, emit2, trigger);
useAutoFocus(props2, fieldRef);
......
......@@ -485,7 +485,7 @@ var safeAreaInsets = {
onChange,
offChange
};
var out = safeAreaInsets;
var D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out = safeAreaInsets;
const onEventPrevent = /* @__PURE__ */ withModifiers(() => {
}, ["prevent"]);
const onEventStop = /* @__PURE__ */ withModifiers(() => {
......@@ -497,10 +497,10 @@ function getWindowOffset() {
const left = parseInt(style.getPropertyValue("--window-left"));
const right = parseInt(style.getPropertyValue("--window-right"));
return {
top: top ? top + out.top : 0,
bottom: bottom ? bottom + out.bottom : 0,
left: left ? left + out.left : 0,
right: right ? right + out.right : 0
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
};
}
function updateCssVar(cssVars) {
......@@ -1185,7 +1185,7 @@ function normalizePageMeta(pageMeta) {
let offset = rpx2px(refreshOptions.offset);
const {type} = navigationBar;
if (type !== "transparent" && type !== "none") {
offset += NAVBAR_HEIGHT + out.top;
offset += NAVBAR_HEIGHT + D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top;
}
refreshOptions.offset = offset;
refreshOptions.height = rpx2px(refreshOptions.height);
......@@ -3069,6 +3069,27 @@ const createSelectorQuery = () => {
const API_ON_TAB_BAR_MID_BUTTON_TAP = "onTabBarMidButtonTap";
const onTabBarMidButtonTap = /* @__PURE__ */ defineOnApi(API_ON_TAB_BAR_MID_BUTTON_TAP, () => {
});
const API_GET_SELECTED_TEXT_RANGE = "getSelectedTextRange";
const getSelectedTextRangeEventCallbacks = createCallbacks("getSelectedTextRangeEvent");
ServiceJSBridge.subscribe && ServiceJSBridge.subscribe("onGetSelectedTextRange", ({callbackId, data}) => {
const callback2 = getSelectedTextRangeEventCallbacks.pop(callbackId);
if (callback2) {
callback2(data);
}
});
const getSelectedTextRange = /* @__PURE__ */ defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, {resolve, reject}) => {
const pageId = getCurrentPageId();
ServiceJSBridge.publishHandler && ServiceJSBridge.publishHandler("getSelectedTextRange", {
pageId,
callbackId: getSelectedTextRangeEventCallbacks.push(function(res) {
if (typeof res.end === "undefined" && typeof res.start === "undefined") {
reject("no focused");
} else {
resolve(res);
}
})
}, pageId);
});
const API_CAN_I_USE = "canIUse";
const CanIUseProtocol = [
{
......@@ -7472,6 +7493,29 @@ function useFormField(nameKey, value) {
uniForm.removeField(ctx);
});
}
const pageIds = [];
const UniViewJSBridgeSubscribe = function() {
const pageId = getCurrentPageId();
if (pageIds.includes(pageId))
return;
pageIds.push(pageId);
UniViewJSBridge.subscribe(pageId + ".getSelectedTextRange", function({pageId: pageId2, callbackId}) {
const activeElement = document.activeElement;
if (!activeElement)
return;
const tagName = activeElement.tagName.toLowerCase();
const tagNames = ["input", "textarea"];
const data = {};
if (tagNames.includes(tagName)) {
data.start = activeElement.selectionStart;
data.end = activeElement.selectionEnd;
}
UniViewJSBridge.publishHandler("onGetSelectedTextRange", {
callbackId,
data
}, pageId2);
});
};
function getValueString(value) {
return value === null ? "" : String(value);
}
......@@ -7705,6 +7749,7 @@ function useEvent(fieldRef, state2, trigger, triggerInput, beforeInput) {
watch(() => fieldRef.value, initField);
}
function useField(props2, rootRef, emit2, beforeInput) {
UniViewJSBridgeSubscribe();
const {fieldRef, state: state2, trigger} = useBase(props2, rootRef, emit2);
const {triggerInput} = useValueSync(props2, state2, emit2, trigger);
useAutoFocus(props2, fieldRef);
......@@ -14919,7 +14964,7 @@ const getSystemInfoSync = /* @__PURE__ */ defineSyncApi("getSystemInfoSync", ()
const windowWidth = getWindowWidth(screenWidth);
let windowHeight = window.innerHeight;
const language = navigator.language;
const statusBarHeight = out.top;
const statusBarHeight = D__DCloud_local_git_uniAppNext_node_modules_safeAreaInsets_out.top;
let osname;
let osversion;
let model;
......@@ -15032,12 +15077,12 @@ const getSystemInfoSync = /* @__PURE__ */ defineSyncApi("getSystemInfoSync", ()
const system = `${osname} ${osversion}`;
const platform = osname.toLocaleLowerCase();
const safeArea = {
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
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
};
const {top: windowTop, bottom: windowBottom} = getWindowOffset();
windowHeight -= windowTop;
......@@ -15057,10 +15102,10 @@ const getSystemInfoSync = /* @__PURE__ */ defineSyncApi("getSystemInfoSync", ()
model,
safeArea,
safeAreaInsets: {
top: out.top,
right: out.right,
bottom: out.bottom,
left: out.left
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
}
};
});
......@@ -17403,6 +17448,7 @@ var api = /* @__PURE__ */ Object.freeze({
canvasGetImageData,
canvasPutImageData,
canvasToTempFilePath,
getSelectedTextRange,
cssVar,
cssEnv,
cssConstant,
......@@ -19814,4 +19860,4 @@ var index = /* @__PURE__ */ defineComponent({
return openBlock(), createBlock("div", clazz, [loadingVNode]);
}
});
export {index$1 as AsyncErrorComponent, index as AsyncLoadingComponent, _sfc_main$8 as Audio, index$k as Button, _sfc_main$7 as Canvas, index$h as Checkbox, index$j as CheckboxGroup, _sfc_main$2 as CoverImage, _sfc_main$3 as CoverView, index$g as Editor, Form, Friction$1 as Friction, index$f as Icon, index$e as Image, Input, index$i as Label, LayoutComponent, index$3 as Map, MovableArea, MovableView, _sfc_main$6 as Navigator, index$2 as PageComponent, _sfc_main$1 as Picker, PickerView, PickerViewColumn, index$d as Progress, index$b as Radio, index$c as RadioGroup, ResizeSensor, _sfc_main$5 as RichText, _sfc_main$4 as ScrollView, Scroller$1 as Scroller, index$a as Slider, Spring$1 as Spring, Swiper, SwiperItem, index$9 as Switch, index$8 as Text, index$7 as Textarea, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, index$5 as Video, index$6 as View, index$4 as WebView, addInterceptor, arrayBufferToBase64, base64ToArrayBuffer, canIUse, canvasGetImageData, canvasPutImageData, canvasToTempFilePath, chooseFile, chooseImage, chooseVideo, clearStorage, clearStorageSync, closeSocket, connectSocket, createCanvasContext, createInnerAudioContext, createIntersectionObserver, createMapContext, createSelectorQuery, createVideoContext, cssBackdropFilter, cssConstant, cssEnv, cssVar, disableScrollBounce, downloadFile, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getFileInfo, getImageInfo, getLocation, getNetworkType, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, getSystemInfo, getSystemInfoSync, getVideoInfo, hideKeyboard, hideLoading, hideNavigationBarLoading, hideTabBar, hideTabBarRedDot, hideToast, initScrollBounce, loadFontFace, makePhoneCall, navigateBack, navigateTo, offAccelerometerChange, offCompassChange, offNetworkStatusChange, onAccelerometerChange, onCompassChange, onNetworkStatusChange, onSocketClose, onSocketError, onSocketMessage, onSocketOpen, onTabBarMidButtonTap, openDocument, openLocation, pageScrollTo, index$l as plugin, previewImage, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, removeStorage, removeStorageSync, removeTabBarBadge, request, sendSocketMessage, setNavigationBarColor, setNavigationBarTitle, setStorage, setStorageSync, setTabBarBadge, setTabBarItem, setTabBarStyle, setupApp, setupPage, showActionSheet, showLoading, showModal, showNavigationBarLoading, showTabBar, showTabBarRedDot, showToast, startAccelerometer, startCompass, startPullDownRefresh, stopAccelerometer, stopCompass, stopPullDownRefresh, switchTab, uni$1 as uni, uploadFile, upx2px, useAttrs, useBooleanAttr, useCustomEvent, useNativeEvent, useOn, useScroller, useSubscribe, useTouchtrack, useUserAction, vibrateLong, vibrateShort, withWebEvent};
export {index$1 as AsyncErrorComponent, index as AsyncLoadingComponent, _sfc_main$8 as Audio, index$k as Button, _sfc_main$7 as Canvas, index$h as Checkbox, index$j as CheckboxGroup, _sfc_main$2 as CoverImage, _sfc_main$3 as CoverView, index$g as Editor, Form, Friction$1 as Friction, index$f as Icon, index$e as Image, Input, index$i as Label, LayoutComponent, index$3 as Map, MovableArea, MovableView, _sfc_main$6 as Navigator, index$2 as PageComponent, _sfc_main$1 as Picker, PickerView, PickerViewColumn, index$d as Progress, index$b as Radio, index$c as RadioGroup, ResizeSensor, _sfc_main$5 as RichText, _sfc_main$4 as ScrollView, Scroller$1 as Scroller, index$a as Slider, Spring$1 as Spring, Swiper, SwiperItem, index$9 as Switch, index$8 as Text, index$7 as Textarea, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, index$5 as Video, index$6 as View, index$4 as WebView, addInterceptor, arrayBufferToBase64, base64ToArrayBuffer, canIUse, canvasGetImageData, canvasPutImageData, canvasToTempFilePath, chooseFile, chooseImage, chooseVideo, clearStorage, clearStorageSync, closeSocket, connectSocket, createCanvasContext, createInnerAudioContext, createIntersectionObserver, createMapContext, createSelectorQuery, createVideoContext, cssBackdropFilter, cssConstant, cssEnv, cssVar, disableScrollBounce, downloadFile, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getFileInfo, getImageInfo, getLocation, getNetworkType, getSelectedTextRange, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, getSystemInfo, getSystemInfoSync, getVideoInfo, hideKeyboard, hideLoading, hideNavigationBarLoading, hideTabBar, hideTabBarRedDot, hideToast, initScrollBounce, loadFontFace, makePhoneCall, navigateBack, navigateTo, offAccelerometerChange, offCompassChange, offNetworkStatusChange, onAccelerometerChange, onCompassChange, onNetworkStatusChange, onSocketClose, onSocketError, onSocketMessage, onSocketOpen, onTabBarMidButtonTap, openDocument, openLocation, pageScrollTo, index$l as plugin, previewImage, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, removeStorage, removeStorageSync, removeTabBarBadge, request, sendSocketMessage, setNavigationBarColor, setNavigationBarTitle, setStorage, setStorageSync, setTabBarBadge, setTabBarItem, setTabBarStyle, setupApp, setupPage, showActionSheet, showLoading, showModal, showNavigationBarLoading, showTabBar, showTabBarRedDot, showToast, startAccelerometer, startCompass, startPullDownRefresh, stopAccelerometer, stopCompass, stopPullDownRefresh, switchTab, uni$1 as uni, uploadFile, upx2px, useAttrs, useBooleanAttr, useCustomEvent, useNativeEvent, useOn, useScroller, useSubscribe, useTouchtrack, useUserAction, vibrateLong, vibrateShort, withWebEvent};
......@@ -74,5 +74,6 @@ export {
canvasGetImageData,
canvasPutImageData,
canvasToTempFilePath,
getSelectedTextRange,
} from '@dcloudio/uni-api'
//#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册