Skip to content

Commit

Permalink
feat(runtime): 增加页面 hooks
Browse files Browse the repository at this point in the history
* 增加 onAddToFavorites、onShareTimeline 页面事件 hooks
* 【Breaking】使用 onShareAppMessage,useShareAppMessage 时,必须设置页面的 enableShareAppMessage 属性为 true,#6825
  • Loading branch information
Chen-jj committed Jul 10, 2020
1 parent 74d0f67 commit ec3a3a9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
4 changes: 4 additions & 0 deletions packages/taro-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
useTitleClick,
useOptionMenuClick,
usePullIntercept,
useShareTimeline,
useAddToFavorites,
useReady,
useRouter,
options,
Expand Down Expand Up @@ -57,6 +59,8 @@ const Taro = {
useTitleClick,
useOptionMenuClick,
usePullIntercept,
useShareTimeline,
useAddToFavorites,
useReady,
useRouter,
options,
Expand Down
40 changes: 27 additions & 13 deletions packages/taro-runtime/src/dsl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function getOnHideEventKey (path: string) {
return path + '.' + 'onHide'
}

export function createPageConfig (component: React.ComponentClass, pageName?: string, data?: Record<string, unknown>) {
export function createPageConfig (component: any, pageName?: string, data?: Record<string, unknown>) {
const id = pageName ?? `taro_page_${pageId()}`
// 小程序 Page 构造器是一个傲娇小公主,不能把复杂的对象挂载到参数上
let pageElement: TaroRootElement | null = null
Expand Down Expand Up @@ -176,18 +176,6 @@ export function createPageConfig (component: React.ComponentClass, pageName?: st
const path = getPath(id, this.options)
return safeExecute(path, 'onPageScroll', options)
},
onShareAppMessage (options) {
const target = options.target
if (target != null) {
const id = target.id
const element = document.getElementById(id)
if (element != null) {
options.target!.dataset = element.dataset
}
}
const path = getPath(id, this.options)
return safeExecute(path, 'onShareAppMessage', options)
},
onResize (options) {
const path = getPath(id, this.options)
return safeExecute(path, 'onResize', options)
Expand All @@ -211,6 +199,32 @@ export function createPageConfig (component: React.ComponentClass, pageName?: st
onPullIntercept () {
const path = getPath(id, this.options)
return safeExecute(path, 'onPullIntercept')
},
onAddToFavorites () {
const path = getPath(id, this.options)
return safeExecute(path, 'onAddToFavorites')
}
}

// onShareAppMessage 和 onShareTimeline 一样,会影响小程序右上方按钮的选项,因此不能默认注册。
if (component.onShareAppMessage || component.enableShareAppMessage) {
config.onShareAppMessage = function (options) {
const target = options.target
if (target != null) {
const id = target.id
const element = document.getElementById(id)
if (element != null) {
options.target!.dataset = element.dataset
}
}
const path = getPath(id, this.options)
return safeExecute(path, 'onShareAppMessage', options)
}
}
if (component.onShareTimeline || component.enableShareTimeline) {
config.onShareTimeline = function () {
const path = getPath(id, this.options)
return safeExecute(path, 'onShareTimeline')
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/taro-runtime/src/dsl/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export const useOptionMenuClick = taroHooks('onOptionMenuClick')

export const usePullIntercept = taroHooks('onPullIntercept')

export const useShareTimeline = taroHooks('onShareTimeline')

export const useAddToFavorites = taroHooks('onAddToFavorites')

export const useReady = taroHooks('onReady')

export const useRouter = (dynamic = false) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-runtime/src/dsl/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface PageLifeCycle extends Show {
onPopMenuClick?(): void
onReady?(): void
onPullIntercept?(): void
onShareTimeline?(): void
onAddToFavorites?(): void
eh?(event: MpEvent): void
onLoad(options: Record<string, unknown>): void
onUnload(): void
Expand Down
10 changes: 10 additions & 0 deletions packages/taro/types/taro.hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ declare namespace Taro {
*/
function useTabItemTap(callback: (payload: TabItemTapObject) => any): void

/**
* 用户点击右上角菜单“收藏”按钮时的回调。
*/
function useAddToFavorites(callback: (paload: AddToFavoritesObject) => AddToFavoritesReturnObject): void

/**
* 用户点击右上角菜单“分享到朋友圈”按钮时的回调。
*/
function useShareTimeline(callback: () => any): void

/**
* 页面初次渲染完成的回调。
* 此时页面已经准备妥当,可以和视图层进行交互。
Expand Down
38 changes: 28 additions & 10 deletions packages/taro/types/taro.lifecycle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ declare namespace Taro {
* 转发事件来源
* `button`:页面内转发按钮
* `menu`:右上角转发菜单
*
*
* @since 1.2.4
*/
from?: 'button' | 'menu' | string
/**
* 如果 `from` 值是 `button`,则 `target` 是触发这次转发事件的 `button`,否则为 `undefined`
*
*
* @since 1.2.4
*/
target?: object
/**
* 页面中包含 `<web-view>` 组件时,返回当前 `<web-view>` 的 url
*
*
* @since 1.6.4
*/
webViewUrl?: string
Expand All @@ -62,7 +62,7 @@ declare namespace Taro {
* 支持PNG及JPG
* 显示图片长宽比是 5:4
* 默认使用截图
*
*
* @since 1.5.0
*/
imageUrl?: string
Expand All @@ -71,26 +71,44 @@ declare namespace Taro {
interface TabItemTapObject {
/**
* 被点击tabItem的序号,从 0 开始
*
* @since 1.9.0
*/
index: string

/**
* 被点击tabItem的页面路径
*
* @since 1.9.0
*/
pagePath: string

/**
* 被点击tabItem的按钮文字
*
* @since 1.9.0
*/
text: string
}

interface AddToFavoritesObject {
/**
* 页面中包含web-view组件时,返回当前web-view的url
*/
webviewUrl: string
}

interface AddToFavoritesReturnObject {
/**
* 自定义标题
*/
title?: string

/**
* 自定义图片,显示图片长宽比为 1:1
*/
imageUrl?: string

/**
* 自定义query字段
*/
query?: string
}

type GetDerivedStateFromProps<P, S> =
/**
* Returns an update to a component's state based on its new props and old state.
Expand Down

0 comments on commit ec3a3a9

Please sign in to comment.