@hxa-rn/react-native-share-menu 0.0.0-stage → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/LICENSE +23 -0
  2. package/README.OpenHarmony_CN.md +306 -0
  3. package/README.md +98 -2
  4. package/harmony/share_menu/NOTICE +35 -0
  5. package/harmony/share_menu/NOTICE_LICENSE +22 -0
  6. package/harmony/share_menu/OAT.xml +38 -0
  7. package/harmony/share_menu/build-profile.json5 +16 -0
  8. package/harmony/share_menu/hvigorfile.ts +2 -0
  9. package/harmony/share_menu/index.ets +4 -0
  10. package/harmony/share_menu/oh-package.json5 +14 -0
  11. package/harmony/share_menu/src/main/cpp/CMakeLists.txt +20 -0
  12. package/harmony/share_menu/src/main/cpp/ShareMenuPackage.h +15 -0
  13. package/harmony/share_menu/src/main/cpp/generated/RNOHGeneratedPackage.h +76 -0
  14. package/harmony/share_menu/src/main/cpp/generated/ShareMenu.cpp +23 -0
  15. package/harmony/share_menu/src/main/cpp/generated/ShareMenu.h +21 -0
  16. package/harmony/share_menu/src/main/ets/ShareMapping.ts +101 -0
  17. package/harmony/share_menu/src/main/ets/ShareMenuPackage.ets +12 -0
  18. package/harmony/share_menu/src/main/ets/ShareMenuStore.ets +219 -0
  19. package/harmony/share_menu/src/main/ets/ShareMenuTurboModule.ets +68 -0
  20. package/harmony/share_menu/src/main/ets/generated/components/ts.ts +5 -0
  21. package/harmony/share_menu/src/main/ets/generated/index.ets +8 -0
  22. package/harmony/share_menu/src/main/ets/generated/ts.ts +9 -0
  23. package/harmony/share_menu/src/main/ets/generated/turboModules/ShareMenu.ts +23 -0
  24. package/harmony/share_menu/src/main/ets/generated/turboModules/ts.ts +8 -0
  25. package/harmony/share_menu/src/main/module.json5 +13 -0
  26. package/harmony/share_menu/src/main/resources/base/element/string.json +8 -0
  27. package/harmony/share_menu/src/main/resources/en_US/element/string.json +8 -0
  28. package/harmony/share_menu/src/main/resources/zh_CN/element/string.json +8 -0
  29. package/harmony/share_menu.har +0 -0
  30. package/package.json +59 -4
  31. package/src/NativeShareMenu.ts +15 -0
  32. package/src/index.harmony.js +60 -0
@@ -0,0 +1,76 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ *
7
+ * @generatorVersion: 1
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include "RNOH/Package.h"
13
+ #include "RNOH/ArkTSTurboModule.h"
14
+ #include "generated/ShareMenu.h"
15
+
16
+ namespace rnoh {
17
+
18
+ class RNOHGeneratedPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
19
+ public:
20
+ SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
21
+ if (name == "ShareMenu") {
22
+ return std::make_shared<ShareMenu>(ctx, name);
23
+ }
24
+ return nullptr;
25
+ };
26
+ };
27
+
28
+ class GeneratedEventEmitRequestHandler : public EventEmitRequestHandler {
29
+ public:
30
+ void handleEvent(Context const &ctx) override {
31
+ auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
32
+ auto componentName = ctx.shadowViewRegistry->getComponentName(ctx.tag);
33
+
34
+ if (eventEmitter == nullptr) {
35
+ return;
36
+ }
37
+
38
+ std::vector<std::string> supportedComponentNames = {
39
+ };
40
+
41
+ std::vector<std::string> supportedEventNames = {
42
+ };
43
+
44
+ if (std::find(supportedComponentNames.begin(), supportedComponentNames.end(), componentName) != supportedComponentNames.end() &&
45
+ std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
46
+ eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
47
+ }
48
+ }
49
+ };
50
+
51
+ class RNOHGeneratedPackage : public Package {
52
+ public:
53
+ RNOHGeneratedPackage(Package::Context ctx) : Package(ctx){};
54
+
55
+ std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
56
+ return std::make_unique<RNOHGeneratedPackageTurboModuleFactoryDelegate>();
57
+ }
58
+
59
+ std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
60
+ return {
61
+ };
62
+ }
63
+
64
+ ComponentJSIBinderByString createComponentJSIBinderByName() override {
65
+ return {
66
+ };
67
+ };
68
+
69
+ EventEmitRequestHandlers createEventEmitRequestHandlers() override {
70
+ return {
71
+ std::make_shared<GeneratedEventEmitRequestHandler>(),
72
+ };
73
+ }
74
+ };
75
+
76
+ } // namespace rnoh
@@ -0,0 +1,23 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ *
7
+ * @generatorVersion: 1
8
+ */
9
+
10
+ #include "ShareMenu.h"
11
+
12
+ namespace rnoh {
13
+ using namespace facebook;
14
+
15
+ ShareMenu::ShareMenu(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) {
16
+ methodMap_ = {
17
+ ARK_METHOD_METADATA(getSharedText, 1),
18
+ ARK_METHOD_METADATA(addListener, 1),
19
+ ARK_METHOD_METADATA(removeListeners, 1),
20
+ };
21
+ }
22
+
23
+ } // namespace rnoh
@@ -0,0 +1,21 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ *
7
+ * @generatorVersion: 1
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include "RNOH/ArkTSTurboModule.h"
13
+
14
+ namespace rnoh {
15
+
16
+ class JSI_EXPORT ShareMenu : public ArkTSTurboModule {
17
+ public:
18
+ ShareMenu(const ArkTSTurboModule::Context ctx, const std::string name);
19
+ };
20
+
21
+ } // namespace rnoh
@@ -0,0 +1,101 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Ported to HarmonyOS/ArkTS from react-native-share-menu (MIT).
3
+ /**
4
+ * 纯映射逻辑(不引任何 @ohos.*)—— 可 tsc + 离线数值对拍。
5
+ *
6
+ * 上游 payload 形态(android `ShareMenuModule.extractShared`,逐字对照):
7
+ * 单条:{ mimeType: string, data: string | null }
8
+ * 多条:{ mimeType: string, data: string[] }
9
+ * 非分享启动 / 无法解析:**null**(上游 `extractShared` 返回 null,callback 收到 null)
10
+ *
11
+ * 鸿蒙侧来源(`@ohos.app.ability.Want`,字段名 `.d.ts` 实核):
12
+ * want.action 'ohos.want.action.sendData' / 'ohos.want.action.sendMultipleData'
13
+ * (字面量取自 @ohos.ability.wantConstant 的 ACTION_SEND_DATA /
14
+ * ACTION_SEND_MULTIPLE_DATA;枚举自 API9 deprecated,值本身仍是系统约定)
15
+ * want.type MIME 类型(对应 Android `intent.getType()`)
16
+ * want.uri 单条数据的 URI
17
+ * want.parameters['ability.params.stream'] 多条数据的 URI 数组
18
+ * (键名 = @ohos.app.ability.wantConstant Params.PARAMS_STREAM 实核值)
19
+ */
20
+
21
+ export const ACTION_SEND_DATA: string = 'ohos.want.action.sendData';
22
+ export const ACTION_SEND_MULTIPLE_DATA: string = 'ohos.want.action.sendMultipleData';
23
+ /** = wantConstant.Params.PARAMS_STREAM(`.d.ts` 实核值) */
24
+ export const PARAMS_STREAM: string = 'ability.params.stream';
25
+
26
+ /** 上游事件名(android `ShareMenuModule.NEW_SHARE_EVENT` 与 JS `NEW_SHARE_EVENT_NAME` 同值) */
27
+ export const NEW_SHARE_EVENT_NAME: string = 'NewShareEvent';
28
+
29
+ /** 上游 payload 的两个键名(android 侧常量 MIME_TYPE_KEY / DATA_KEY) */
30
+ export interface SharePayload {
31
+ mimeType: string;
32
+ data: string | string[] | null;
33
+ }
34
+
35
+ /** 只描述本映射用到的 want 字段,避免依赖 @ohos.app.ability.Want 类型 */
36
+ export interface WantLike {
37
+ action?: string;
38
+ type?: string;
39
+ uri?: string;
40
+ parameters?: Record<string, Object>;
41
+ // 仅库内 ShareKit 归一化快照;不是 Want parameters 键或新增 JS 参数。
42
+ text?: string | null;
43
+ }
44
+
45
+ function asStringArray(v: Object | undefined): string[] | null {
46
+ if (v === undefined || v === null) {
47
+ return null;
48
+ }
49
+ // SDK PARAMS_STREAM 是 URI 字符串数组;按上游逐项 toString,不过滤空串/空数组。
50
+ const arr = v as Object[];
51
+ return arr.map((uri: Object) => uri.toString());
52
+ }
53
+
54
+ /** want/ShareKit 内部快照 → Android extractShared 的原始结果;不增加校验或错误协议。 */
55
+ export function extractShared(want: WantLike | undefined | null): SharePayload | null {
56
+ if (want === undefined || want === null) {
57
+ return null;
58
+ }
59
+ const type = want.type;
60
+ if (type === undefined || type === null) {
61
+ return null;
62
+ }
63
+ const action = want.action;
64
+
65
+ if (action === ACTION_SEND_DATA) {
66
+ if (type === 'text/plain') {
67
+ if (want.text !== undefined && want.text !== '') {
68
+ return {mimeType: type, data: want.text};
69
+ }
70
+ if (want.uri !== undefined && want.uri !== null && want.uri !== '') {
71
+ return {mimeType: type, data: want.uri};
72
+ }
73
+ return {mimeType: type, data: want.text === undefined ? null : want.text};
74
+ }
75
+ if (want.uri !== undefined && want.uri !== null) {
76
+ return {mimeType: type, data: want.uri};
77
+ }
78
+ const params = want.parameters;
79
+ if (params !== undefined && params !== null) {
80
+ const streams = asStringArray(params[PARAMS_STREAM]);
81
+ if (streams !== null && streams[0] !== undefined) {
82
+ return {mimeType: type, data: streams[0]};
83
+ }
84
+ }
85
+ return null;
86
+ }
87
+
88
+ if (action === ACTION_SEND_MULTIPLE_DATA) {
89
+ const params = want.parameters;
90
+ if (params === undefined || params === null) {
91
+ return null;
92
+ }
93
+ const list = asStringArray(params[PARAMS_STREAM]);
94
+ if (list !== null) {
95
+ return {mimeType: type, data: list};
96
+ }
97
+ return null;
98
+ }
99
+
100
+ return null;
101
+ }
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: MIT
2
+ /** RNOHPackage 注册入口 —— 本 HAR 只提供 TurboModule 'ShareMenu'(无 Fabric 组件)。 */
3
+ import { RNOHPackage, UITurboModule, UITurboModuleContext } from '@rnoh/react-native-openharmony';
4
+
5
+ import { ShareMenuTurboModule } from './ShareMenuTurboModule';
6
+
7
+ export class ShareMenuPackage extends RNOHPackage {
8
+ override getUITurboModuleFactoryByNameMap(): Map<string, (ctx: UITurboModuleContext) => UITurboModule | null> {
9
+ return new Map<string, (ctx: UITurboModuleContext) => UITurboModule | null>()
10
+ .set(ShareMenuTurboModule.NAME, (ctx: UITurboModuleContext) => new ShareMenuTurboModule(ctx));
11
+ }
12
+ }
@@ -0,0 +1,219 @@
1
+ // SPDX-License-Identifier: MIT
2
+ /**
3
+ * 分享启动参数暂存 + 新分享事件转发。
4
+ *
5
+ * 范式与 `react-native-quick-actions` 的 QuickActionsStore 同源(rn 线「want 交接」通用做法):
6
+ * 鸿蒙的启动参数只在 UIAbility 的 `onCreate(want)` / `onNewWant(want)` 里可得,
7
+ * 而 TurboModule 实例化在其后,故用静态 store 交接。ShareKit 解析完成前读取会等待,
8
+ * 后续 Want 串行处理;无宿主与无分享分开,实例销毁时解除 emitter。
9
+ */
10
+ import Want from '@ohos.app.ability.Want';
11
+ import { systemShare } from '@kit.ShareKit';
12
+ import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor';
13
+ import { StandardRNOHLogger } from '@rnoh/react-native-openharmony';
14
+ import {
15
+ ACTION_SEND_DATA, ACTION_SEND_MULTIPLE_DATA, PARAMS_STREAM,
16
+ SharePayload, WantLike, extractShared
17
+ } from './ShareMapping';
18
+
19
+ type Emitter = (payload: SharePayload | null) => void;
20
+
21
+ const INITIAL_GENERATION: number = 0;
22
+ const GENERATION_INCREMENT: number = 1;
23
+ const EMPTY_RECORD_COUNT: number = 0;
24
+ const SINGLE_RECORD_COUNT: number = 1;
25
+ const FIRST_RECORD_INDEX: number = 0;
26
+ const MIME_TEXT_PLAIN: string = 'text/plain';
27
+ const MIME_DIRECTORY: string = 'inode/directory';
28
+ const MIME_BINARY: string = 'application/octet-stream';
29
+ const UTD_TEXT: string = 'general.text';
30
+ const UTD_PLAIN_TEXT: string = 'general.plain-text';
31
+ const UTD_HYPERLINK: string = 'general.hyperlink';
32
+ const UTD_DIRECTORY: string = 'general.directory';
33
+ const UTD_FOLDER: string = 'general.folder';
34
+
35
+ export class ShareMenuStore {
36
+ private static initialShare: WantLike | undefined = undefined;
37
+ private static emitters: Set<Emitter> = new Set<Emitter>();
38
+ private static pending: Promise<void> = Promise.resolve();
39
+ private static hostAvailable: boolean = false;
40
+ private static generation: number = INITIAL_GENERATION;
41
+
42
+ /** 宿主 EntryAbility.onCreate(want) 调用 */
43
+ static setInitialFromWant(want: Want | undefined): Promise<void> {
44
+ ShareMenuStore.generation += GENERATION_INCREMENT;
45
+ const generation = ShareMenuStore.generation;
46
+ ShareMenuStore.hostAvailable = want !== undefined;
47
+ ShareMenuStore.initialShare = undefined;
48
+ ShareMenuStore.pending = want === undefined ? Promise.resolve() :
49
+ ShareMenuStore.normalizeWant(want).then((snapshot: WantLike) => {
50
+ if (generation === ShareMenuStore.generation) {
51
+ ShareMenuStore.initialShare = snapshot;
52
+ }
53
+ });
54
+ ShareMenuStore.observePendingFailure();
55
+ return ShareMenuStore.pending;
56
+ }
57
+
58
+ /** 宿主 EntryAbility.onNewWant(want) 调用 → 转 NewShareEvent */
59
+ static onNewWant(want: Want | undefined): Promise<void> {
60
+ if (!ShareMenuStore.hostAvailable || want === undefined) {
61
+ return Promise.resolve();
62
+ }
63
+ const generation = ShareMenuStore.generation;
64
+ const emitters = Array.from(ShareMenuStore.emitters);
65
+ // 当次没有桥实例时不补发;仍更新后续 getInitialShare 可读的当前 Want。
66
+ const processWant = async (): Promise<void> => {
67
+ const snapshot = await ShareMenuStore.normalizeWant(want);
68
+ if (generation !== ShareMenuStore.generation) {
69
+ return;
70
+ }
71
+ const p = extractShared(snapshot);
72
+ let hasDeliveryError: boolean = false;
73
+ let deliveryError: Error | undefined = undefined;
74
+ for (const emitter of emitters) {
75
+ if (ShareMenuStore.emitters.has(emitter)) {
76
+ try {
77
+ emitter(p);
78
+ } catch (error) {
79
+ // 隔离订阅者;当次任务仍保留首个失败,不补发或伪造成功。
80
+ if (!hasDeliveryError) {
81
+ hasDeliveryError = true;
82
+ deliveryError = error as Error;
83
+ }
84
+ }
85
+ }
86
+ }
87
+ if (hasDeliveryError) {
88
+ throw deliveryError as Error;
89
+ }
90
+ ShareMenuStore.initialShare = snapshot;
91
+ };
92
+ // 仅恢复后续独立 Want;不重跑失败任务,当前 pending 仍可被读取端观察到拒绝。
93
+ ShareMenuStore.pending = ShareMenuStore.pending.then(processWant, processWant);
94
+ ShareMenuStore.observePendingFailure();
95
+ return ShareMenuStore.pending;
96
+ }
97
+
98
+ private static observePendingFailure(): void {
99
+ ShareMenuStore.pending.catch(() => {
100
+ // 只记失败位置,不记录 Want、URI、文本或错误对象中的分享内容。
101
+ new StandardRNOHLogger().error('ShareMenuStore: share task failed');
102
+ });
103
+ }
104
+
105
+ private static inferRecordMime(record: systemShare.SharedRecord): string | undefined {
106
+ try {
107
+ const mimeTypes = uniformTypeDescriptor.getTypeDescriptor(record.utd).mimeTypes;
108
+ const mimeType = mimeTypes[FIRST_RECORD_INDEX];
109
+ if (mimeType !== undefined && mimeType !== '') {
110
+ return mimeType;
111
+ }
112
+ } catch (error) {
113
+ // 部分系统/应用自定义 UTD 无法取得描述;继续按已知 UTD 和真实载荷归一化。
114
+ }
115
+ if (record.utd === UTD_TEXT || record.utd === UTD_PLAIN_TEXT || record.utd === UTD_HYPERLINK ||
116
+ record.content !== undefined && record.content !== null) {
117
+ return MIME_TEXT_PLAIN;
118
+ }
119
+ if (record.uri !== undefined && record.uri !== null) {
120
+ if (record.utd === UTD_DIRECTORY || record.utd === UTD_FOLDER) {
121
+ return MIME_DIRECTORY;
122
+ }
123
+ return MIME_BINARY;
124
+ }
125
+ return undefined;
126
+ }
127
+
128
+ static setEmitter(emitter: Emitter): () => void {
129
+ ShareMenuStore.emitters.add(emitter);
130
+ return () => { ShareMenuStore.emitters.delete(emitter); };
131
+ }
132
+
133
+ /** 仅清 Android clearSharedText 所清的载荷,保留 action/type。undefined 表示无宿主。 */
134
+ static async popInitialShare(isCurrentReader: () => boolean = (): boolean => true):
135
+ Promise<SharePayload | null | undefined> {
136
+ const generation = ShareMenuStore.generation;
137
+ await ShareMenuStore.pending;
138
+ // A destroyed TurboModule must not consume data on behalf of its replacement.
139
+ // Check ownership after asynchronous parsing, before mutating the payload.
140
+ if (!isCurrentReader() || generation !== ShareMenuStore.generation || !ShareMenuStore.hostAvailable) {
141
+ return undefined;
142
+ }
143
+ const want = ShareMenuStore.initialShare;
144
+ const v = extractShared(want);
145
+ if (want !== undefined && want.type !== undefined) {
146
+ if (want.type === 'text/plain') {
147
+ want.text = undefined;
148
+ } else {
149
+ want.uri = undefined;
150
+ const params: Record<string, Object> = {};
151
+ if (want.parameters !== undefined) {
152
+ for (const key of Object.keys(want.parameters)) {
153
+ if (key !== PARAMS_STREAM) {
154
+ params[key] = want.parameters[key];
155
+ }
156
+ }
157
+ }
158
+ want.parameters = params;
159
+ }
160
+ }
161
+ return v;
162
+ }
163
+
164
+ private static async normalizeWant(want: Want): Promise<WantLike> {
165
+ const snapshot: WantLike = {
166
+ action: want.action, type: want.type, uri: want.uri, parameters: want.parameters
167
+ };
168
+ if (want.action !== ACTION_SEND_DATA && want.action !== ACTION_SEND_MULTIPLE_DATA) {
169
+ return snapshot;
170
+ }
171
+ // PARAMS_STREAM 只用于文件;纯文本来自官方 SharedRecord.content。
172
+ let records: systemShare.SharedRecord[];
173
+ try {
174
+ const data = await systemShare.getSharedData(want);
175
+ records = data.getRecords();
176
+ } catch (error) {
177
+ // 普通直接 Want 不含 ShareKit 数据时保留既有 URI 通路,不新增 JS reject。
178
+ return snapshot;
179
+ }
180
+ if (records.length === EMPTY_RECORD_COUNT) {
181
+ return snapshot;
182
+ }
183
+ // Ability IPC supplies an empty MIME for ShareKit payloads on API22.
184
+ // Only a real SharedRecord authorizes inference; direct Want empty MIME
185
+ // remains untouched when ShareKit has no records.
186
+ if (snapshot.type === undefined || snapshot.type === '') {
187
+ snapshot.type = ShareMenuStore.inferRecordMime(records[FIRST_RECORD_INDEX]);
188
+ }
189
+ if (records.length > SINGLE_RECORD_COUNT || want.action === ACTION_SEND_MULTIPLE_DATA) {
190
+ snapshot.action = ACTION_SEND_MULTIPLE_DATA;
191
+ const params: Record<string, Object> = {};
192
+ const uris: string[] = [];
193
+ for (const record of records) {
194
+ if (record.uri === undefined || record.uri === null) {
195
+ // Multiple items follow the upstream URI-array contract. A content-only
196
+ // record cannot supply a URI; retain the original Want fallback rather
197
+ // than throwing, dropping an item, or inventing a content-array result.
198
+ return snapshot;
199
+ }
200
+ uris.push(record.uri.toString());
201
+ }
202
+ params[PARAMS_STREAM] = uris;
203
+ snapshot.parameters = params;
204
+ } else if (snapshot.type === MIME_TEXT_PLAIN) {
205
+ const record = records[FIRST_RECORD_INDEX];
206
+ if (record.content !== undefined && record.content !== null && record.content !== '') {
207
+ snapshot.text = record.content;
208
+ } else if (record.uri !== undefined && record.uri !== null) {
209
+ // URL/.txt 可能同时携带空 content 与有效 URI;有效 URI 不应被空字符串覆盖。
210
+ snapshot.text = record.uri.toString();
211
+ } else {
212
+ snapshot.text = record.content;
213
+ }
214
+ } else {
215
+ snapshot.uri = records[FIRST_RECORD_INDEX].uri;
216
+ }
217
+ return snapshot;
218
+ }
219
+ }
@@ -0,0 +1,68 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Ported to HarmonyOS/ArkTS from react-native-share-menu (MIT).
3
+ /**
4
+ * TurboModule 'ShareMenu' 的鸿蒙实现(注册名与上游 Android `getName()` 逐字一致)。
5
+ *
6
+ * 方法面 = 上游 JS 实际调用的 `getSharedText(callback)`(index.js:33)+ NativeEventEmitter 所需两方法。
7
+ * 事件 `NewShareEvent` 名称取自映射模块常量(与上游 JS 的 `NEW_SHARE_EVENT_NAME` 同值)。
8
+ *
9
+ * 数据来源 = 宿主 EntryAbility 注入的 want(见 demo/README.md §宿主接线):
10
+ * 冷启动(被系统分享面板拉起)→ onCreate(want) → getSharedText 取到
11
+ * 热启动(应用在后台时再次分享)→ onNewWant(want) → 'NewShareEvent' 事件
12
+ */
13
+ import { UITurboModule, UITurboModuleContext, LifecycleState } from '@rnoh/react-native-openharmony';
14
+
15
+ import { SharePayload } from './ShareMapping';
16
+ import { ShareMenuStore } from './ShareMenuStore';
17
+ import { NEW_SHARE_EVENT_NAME } from './ShareMapping';
18
+
19
+ export class ShareMenuTurboModule extends UITurboModule {
20
+ static readonly NAME: string = 'ShareMenu';
21
+ private unsubscribe: (() => void) | undefined;
22
+ private listenerCount: number = 0;
23
+ private isDestroyed: boolean = false;
24
+
25
+ constructor(ctx: UITurboModuleContext) {
26
+ super(ctx);
27
+ this.unsubscribe = ShareMenuStore.setEmitter((payload: SharePayload | null) => {
28
+ if (this.ctx.rnInstance.getLifecycleState() !== LifecycleState.BEFORE_CREATE) {
29
+ this.ctx.rnInstance.emitDeviceEvent(NEW_SHARE_EVENT_NAME, payload);
30
+ }
31
+ });
32
+ }
33
+
34
+ /**
35
+ * 上游签名 `getSharedText(successCallback)`;**非分享启动时回调收到 null**
36
+ * (上游 android `extractShared` 返回 null 时也是把 null 交给 callback)。
37
+ */
38
+ getSharedText(callback: (shared: Object | null) => void): void {
39
+ if (this.isDestroyed) {
40
+ return;
41
+ }
42
+ ShareMenuStore.popInitialShare((): boolean => !this.isDestroyed).then((shared: SharePayload | null | undefined) => {
43
+ if (!this.isDestroyed && shared !== undefined) {
44
+ callback(shared);
45
+ }
46
+ }).catch(() => {
47
+ // 观察内部失败;不向 JS 增加错误回调或伪成功载荷。
48
+ this.ctx.logger.error('ShareMenu: getSharedText failed');
49
+ });
50
+ }
51
+
52
+ // NativeEventEmitter 内部记账;事件仍按 Android 活跃桥规则交付(包括 null)。
53
+ addListener(eventName: string): void {
54
+ this.listenerCount += 1;
55
+ }
56
+
57
+ removeListeners(count: number): void {
58
+ this.listenerCount -= count;
59
+ }
60
+
61
+ override __onDestroy__(): void {
62
+ this.isDestroyed = true;
63
+ this.unsubscribe?.();
64
+ this.unsubscribe = undefined;
65
+ this.listenerCount = 0;
66
+ super.__onDestroy__();
67
+ }
68
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ */
7
+
8
+ export * from './ts'
@@ -0,0 +1,9 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ */
7
+
8
+ export * as RNC from "./components/ts"
9
+ export * as TM from "./turboModules/ts"
@@ -0,0 +1,23 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ *
7
+ * @generatorVersion: 1
8
+ */
9
+
10
+ import { Tag } from "@rnoh/react-native-openharmony/ts"
11
+
12
+ export namespace ShareMenu {
13
+ export const NAME = 'ShareMenu' as const
14
+
15
+ export interface Spec {
16
+ getSharedText(callback: (shared: null | Object) => void): void;
17
+
18
+ addListener(eventName: string): void;
19
+
20
+ removeListeners(count: number): void;
21
+
22
+ }
23
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-harmony"
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be
5
+ * lost once the code is regenerated.
6
+ */
7
+
8
+ export * from "./ShareMenu"
@@ -0,0 +1,13 @@
1
+ {
2
+ "module": {
3
+ "name": "share_menu",
4
+ "type": "har",
5
+ "deviceTypes": [
6
+ "default",
7
+ "tablet",
8
+ "2in1"
9
+ ],
10
+ "requestPermissions": [
11
+ ]
12
+ }
13
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "string": [
3
+ {
4
+ "name": "page_show",
5
+ "value": "page from npm package"
6
+ }
7
+ ]
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "string": [
3
+ {
4
+ "name": "page_show",
5
+ "value": "page from npm package"
6
+ }
7
+ ]
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "string": [
3
+ {
4
+ "name": "page_show",
5
+ "value": "page from npm package"
6
+ }
7
+ ]
8
+ }
Binary file