diff --git a/packages/taro-api/src/index.js b/packages/taro-api/src/index.js index 94648687299f..6a8ede3d2a52 100644 --- a/packages/taro-api/src/index.js +++ b/packages/taro-api/src/index.js @@ -24,6 +24,8 @@ import { useTitleClick, useOptionMenuClick, usePullIntercept, + useShareTimeline, + useAddToFavorites, useReady, useRouter, options, @@ -57,6 +59,8 @@ const Taro = { useTitleClick, useOptionMenuClick, usePullIntercept, + useShareTimeline, + useAddToFavorites, useReady, useRouter, options, diff --git a/packages/taro-runtime/src/dsl/common.ts b/packages/taro-runtime/src/dsl/common.ts index 8f942b240f98..0eb3077e2424 100644 --- a/packages/taro-runtime/src/dsl/common.ts +++ b/packages/taro-runtime/src/dsl/common.ts @@ -87,7 +87,7 @@ export function getOnHideEventKey (path: string) { return path + '.' + 'onHide' } -export function createPageConfig (component: React.ComponentClass, pageName?: string, data?: Record) { +export function createPageConfig (component: any, pageName?: string, data?: Record) { const id = pageName ?? `taro_page_${pageId()}` // 小程序 Page 构造器是一个傲娇小公主,不能把复杂的对象挂载到参数上 let pageElement: TaroRootElement | null = null @@ -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) @@ -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') } } diff --git a/packages/taro-runtime/src/dsl/hooks.ts b/packages/taro-runtime/src/dsl/hooks.ts index ac80054829ee..fb38e4c30857 100644 --- a/packages/taro-runtime/src/dsl/hooks.ts +++ b/packages/taro-runtime/src/dsl/hooks.ts @@ -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) => { diff --git a/packages/taro-runtime/src/dsl/instance.ts b/packages/taro-runtime/src/dsl/instance.ts index 40649f7b999c..5b28f1186a7d 100644 --- a/packages/taro-runtime/src/dsl/instance.ts +++ b/packages/taro-runtime/src/dsl/instance.ts @@ -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): void onUnload(): void diff --git a/packages/taro/types/taro.hooks.d.ts b/packages/taro/types/taro.hooks.d.ts index 54794085c9c4..1e45d1b215d3 100644 --- a/packages/taro/types/taro.hooks.d.ts +++ b/packages/taro/types/taro.hooks.d.ts @@ -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 + /** * 页面初次渲染完成的回调。 * 此时页面已经准备妥当,可以和视图层进行交互。 diff --git a/packages/taro/types/taro.lifecycle.d.ts b/packages/taro/types/taro.lifecycle.d.ts index 269b0fe85403..21a018dd0562 100644 --- a/packages/taro/types/taro.lifecycle.d.ts +++ b/packages/taro/types/taro.lifecycle.d.ts @@ -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 /** * 页面中包含 `` 组件时,返回当前 `` 的 url - * + * * @since 1.6.4 */ webViewUrl?: string @@ -62,7 +62,7 @@ declare namespace Taro { * 支持PNG及JPG * 显示图片长宽比是 5:4 * 默认使用截图 - * + * * @since 1.5.0 */ imageUrl?: string @@ -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 = /** * Returns an update to a component's state based on its new props and old state.