Files
JayuCloud/WeChatProgram/utils/keyboard-safe.js

300 lines
9.3 KiB
JavaScript

/**
* Purpose: DEFAULT_CURSOR_SPACING supports keyboard-safe.js by holding or executing the default cursor spacing page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DEFAULT_CURSOR_SPACING = 96
/**
* Purpose: DEFAULT_SPACER_EXTRA_RPX supports keyboard-safe.js by holding or executing the default spacer extra rpx page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DEFAULT_SPACER_EXTRA_RPX = 48
/**
* Purpose: DEFAULT_SCROLL_DELAY supports keyboard-safe.js by holding or executing the default scroll delay page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DEFAULT_SCROLL_DELAY = 32
/**
* Purpose: MAX_CURSOR_SPACING supports keyboard-safe.js by holding or executing the max cursor spacing page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const MAX_CURSOR_SPACING = 180
/**
* Purpose: getWindowWidth supports keyboard-safe.js by holding or executing the get window width page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getWindowWidth() {
try {
if (typeof wx.getWindowInfo === 'function') {
const info = wx.getWindowInfo()
if (info && info.windowWidth) {
return info.windowWidth
}
}
const systemInfo = wx.getSystemInfoSync()
return (systemInfo && systemInfo.windowWidth) || 375
} catch (error) {
return 375
}
}
/**
* Purpose: pxToRpx supports keyboard-safe.js by holding or executing the px to rpx page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function pxToRpx(px) {
return Math.round((Number(px) || 0) * 750 / getWindowWidth())
}
/**
* Purpose: createKeyboardSafeData supports keyboard-safe.js by holding or executing the create keyboard safe data page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function createKeyboardSafeData(options = {}) {
const cursorSpacing = Number(options.cursorSpacing) > 0
? Math.round(Number(options.cursorSpacing))
: DEFAULT_CURSOR_SPACING
return {
keyboardVisible: false,
keyboardHeightPx: 0,
keyboardInsetRpx: 0,
keyboardAnchorId: '',
keyboardCursorSpacing: cursorSpacing
}
}
/**
* Purpose: clearKeyboardTimers supports keyboard-safe.js by holding or executing the clear keyboard timers page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function clearKeyboardTimers(state) {
if (!state) {
return
}
if (state.anchorTimer) {
clearTimeout(state.anchorTimer)
state.anchorTimer = null
}
if (state.blurTimer) {
clearTimeout(state.blurTimer)
state.blurTimer = null
}
}
/**
* Purpose: queueAnchorScroll supports keyboard-safe.js by holding or executing the queue anchor scroll page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function queueAnchorScroll(page, anchorId) {
const state = page && page.__keyboardSafe
if (!page || !state || !anchorId) {
return
}
if (state.anchorTimer) {
clearTimeout(state.anchorTimer)
}
const nextDelay = Number(state.options.scrollDelay) || DEFAULT_SCROLL_DELAY
const updateAnchor = () => {
if (!page.__keyboardSafe) {
return
}
page.setData({
keyboardAnchorId: anchorId
})
}
if (page.data && page.data.keyboardAnchorId === anchorId) {
page.setData({
keyboardAnchorId: ''
})
state.anchorTimer = setTimeout(updateAnchor, nextDelay)
return
}
state.anchorTimer = setTimeout(updateAnchor, 0)
}
/**
* Purpose: attachKeyboardSafe supports keyboard-safe.js by holding or executing the attach keyboard safe page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function attachKeyboardSafe(page, options = {}) {
if (!page) {
return
}
detachKeyboardSafe(page)
const state = {
activeAnchorId: '',
anchorTimer: null,
blurTimer: null,
options: {
cursorSpacing: Number(options.cursorSpacing) > 0
? Math.round(Number(options.cursorSpacing))
: DEFAULT_CURSOR_SPACING,
spacerExtraRpx: Number(options.spacerExtraRpx) >= 0
? Number(options.spacerExtraRpx)
: DEFAULT_SPACER_EXTRA_RPX,
scrollDelay: Number(options.scrollDelay) > 0
? Number(options.scrollDelay)
: DEFAULT_SCROLL_DELAY
},
listener: null
}
state.listener = result => {
const nextHeight = Math.max(0, Number(result && result.height) || 0)
const keyboardVisible = nextHeight > 0
const keyboardInsetRpx = keyboardVisible
? pxToRpx(nextHeight) + state.options.spacerExtraRpx
: 0
const keyboardCursorSpacing = keyboardVisible
? Math.max(
state.options.cursorSpacing,
Math.min(MAX_CURSOR_SPACING, Math.round(nextHeight * 0.38))
)
: state.options.cursorSpacing
page.setData({
keyboardVisible,
keyboardHeightPx: nextHeight,
keyboardInsetRpx,
keyboardCursorSpacing,
keyboardAnchorId: keyboardVisible ? page.data.keyboardAnchorId : (state.activeAnchorId ? page.data.keyboardAnchorId : '')
})
if (keyboardVisible && state.activeAnchorId) {
queueAnchorScroll(page, state.activeAnchorId)
}
}
page.__keyboardSafe = state
if (typeof wx.onKeyboardHeightChange === 'function') {
wx.onKeyboardHeightChange(state.listener)
}
}
/**
* Purpose: detachKeyboardSafe supports keyboard-safe.js by holding or executing the detach keyboard safe page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function detachKeyboardSafe(page) {
if (!page || !page.__keyboardSafe) {
return
}
const state = page.__keyboardSafe
clearKeyboardTimers(state)
if (typeof wx.offKeyboardHeightChange === 'function' && state.listener) {
try {
wx.offKeyboardHeightChange(state.listener)
} catch (error) {}
}
delete page.__keyboardSafe
}
/**
* Purpose: handleKeyboardFocus supports keyboard-safe.js by holding or executing the handle keyboard focus page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function handleKeyboardFocus(page, event) {
const state = page && page.__keyboardSafe
if (!page || !state) {
return
}
if (state.blurTimer) {
clearTimeout(state.blurTimer)
state.blurTimer = null
}
const anchorId = String(
event
&& event.currentTarget
&& event.currentTarget.dataset
&& event.currentTarget.dataset.keyboardAnchor
? event.currentTarget.dataset.keyboardAnchor
: ''
)
state.activeAnchorId = anchorId
if (anchorId) {
queueAnchorScroll(page, anchorId)
}
if (!page.data.keyboardCursorSpacing || page.data.keyboardCursorSpacing < state.options.cursorSpacing) {
page.setData({
keyboardCursorSpacing: state.options.cursorSpacing
})
}
}
/**
* Purpose: resetKeyboardSafe supports keyboard-safe.js by holding or executing the reset keyboard safe page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function resetKeyboardSafe(page, options = {}) {
if (!page) {
return
}
const state = page.__keyboardSafe
if (state) {
clearKeyboardTimers(state)
if (options.clearActiveAnchor !== false) {
state.activeAnchorId = ''
}
}
const fallbackCursorSpacing = state ? state.options.cursorSpacing : DEFAULT_CURSOR_SPACING
page.setData({
keyboardVisible: false,
keyboardHeightPx: 0,
keyboardInsetRpx: 0,
keyboardAnchorId: options.keepAnchor ? (page.data.keyboardAnchorId || '') : '',
keyboardCursorSpacing: fallbackCursorSpacing
})
}
/**
* Purpose: handleKeyboardBlur supports keyboard-safe.js by holding or executing the handle keyboard blur page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function handleKeyboardBlur(page) {
const state = page && page.__keyboardSafe
if (!page || !state) {
return
}
if (state.blurTimer) {
clearTimeout(state.blurTimer)
}
state.blurTimer = setTimeout(() => {
if (!page.__keyboardSafe) {
return
}
resetKeyboardSafe(page)
}, 120)
}
module.exports = {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
}