@react-native-windows/codegen 0.74.2 → 0.74.4
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.
- package/CHANGELOG.md +21 -5
- package/lib-commonjs/Cli.js +11 -0
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/AliasManaging.d.ts +3 -3
- package/lib-commonjs/generators/AliasManaging.js.map +1 -1
- package/lib-commonjs/generators/GenerateComponentWindows.d.ts +13 -0
- package/lib-commonjs/generators/GenerateComponentWindows.js +388 -0
- package/lib-commonjs/generators/GenerateComponentWindows.js.map +1 -0
- package/lib-commonjs/generators/PropObjectTypes.d.ts +18 -0
- package/lib-commonjs/generators/PropObjectTypes.js +198 -0
- package/lib-commonjs/generators/PropObjectTypes.js.map +1 -0
- package/lib-commonjs/index.d.ts +3 -1
- package/lib-commonjs/index.js +16 -13
- package/lib-commonjs/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Cli.ts +12 -0
- package/src/generators/AliasManaging.ts +12 -12
- package/src/generators/GenerateComponentWindows.ts +522 -0
- package/src/generators/PropObjectTypes.ts +214 -0
- package/src/index.ts +33 -11
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
SchemaType,
|
|
11
|
+
EventTypeAnnotation,
|
|
12
|
+
PropTypeAnnotation,
|
|
13
|
+
ObjectTypeAnnotation,
|
|
14
|
+
CommandParamTypeAnnotation,
|
|
15
|
+
} from '@react-native/codegen/lib/CodegenSchema';
|
|
16
|
+
import {getAliasCppName, setPreferredModuleName} from './AliasManaging';
|
|
17
|
+
import {
|
|
18
|
+
translateComponentPropsFieldType,
|
|
19
|
+
translateComponentEventType,
|
|
20
|
+
translateCommandParamType,
|
|
21
|
+
} from './PropObjectTypes';
|
|
22
|
+
import type {CppStringTypes} from './ObjectTypes';
|
|
23
|
+
import type {AliasMap} from './AliasManaging';
|
|
24
|
+
|
|
25
|
+
export type {CppStringTypes} from './ObjectTypes';
|
|
26
|
+
|
|
27
|
+
type FilesOutput = Map<string, string>;
|
|
28
|
+
|
|
29
|
+
const headerTemplate = `/*
|
|
30
|
+
* This file is auto-generated from ::_COMPONENT_NAME_::NativeComponent spec file in flow / TypeScript.
|
|
31
|
+
*/
|
|
32
|
+
#pragma once
|
|
33
|
+
|
|
34
|
+
#include <JSValueComposition.h>
|
|
35
|
+
#include <NativeModules.h>
|
|
36
|
+
#include <winrt/Microsoft.ReactNative.Composition.h>
|
|
37
|
+
#include <winrt/Microsoft.UI.Composition.h>`;
|
|
38
|
+
|
|
39
|
+
const propsTemplate = `REACT_STRUCT(::_PROPS_NAME_::)
|
|
40
|
+
struct ::_PROPS_NAME_:: : winrt::implements<::_PROPS_NAME_::, winrt::Microsoft::ReactNative::IComponentProps> {
|
|
41
|
+
::_PROPS_NAME_::(winrt::Microsoft::ReactNative::ViewProps props) : ViewProps(props) {}
|
|
42
|
+
|
|
43
|
+
void SetProp(uint32_t hash, winrt::hstring propName, winrt::Microsoft::ReactNative::IJSValueReader value) noexcept {
|
|
44
|
+
winrt::Microsoft::ReactNative::ReadProp(hash, propName, value, *this);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
::_PROPS_FIELDS_::
|
|
48
|
+
const winrt::Microsoft::ReactNative::ViewProps ViewProps;
|
|
49
|
+
};`;
|
|
50
|
+
|
|
51
|
+
const propsObjectTemplate = `REACT_STRUCT(::_OBJECT_NAME_::)
|
|
52
|
+
struct ::_OBJECT_NAME_:: {
|
|
53
|
+
::_OBJECT_FIELDS_::};
|
|
54
|
+
`;
|
|
55
|
+
const eventsObjectTemplate = `REACT_STRUCT(::_OBJECT_NAME_::)
|
|
56
|
+
struct ::_OBJECT_NAME_:: {
|
|
57
|
+
::_OBJECT_FIELDS_::};
|
|
58
|
+
`;
|
|
59
|
+
|
|
60
|
+
const eventEmitterMethodTemplate = ` void ::_EVENT_NAME_::(::_EVENT_OBJECT_TYPE_:: &value) const {
|
|
61
|
+
m_eventEmitter.DispatchEvent(L"::_EVENT_NAME_NO_ON_::", [value](const winrt::Microsoft::ReactNative::IJSValueWriter writer) {
|
|
62
|
+
winrt::Microsoft::ReactNative::WriteValue(writer, value);
|
|
63
|
+
});
|
|
64
|
+
}`;
|
|
65
|
+
|
|
66
|
+
const eventEmitterTemplate = `::_COMPONENT_EVENT_OBJECT_TYPES_::
|
|
67
|
+
|
|
68
|
+
struct ::_EVENT_EMITTER_NAME_:: {
|
|
69
|
+
::_EVENT_EMITTER_NAME_::(const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter)
|
|
70
|
+
: m_eventEmitter(eventEmitter) {}
|
|
71
|
+
|
|
72
|
+
::_EVENT_EMITTER_USINGS_::
|
|
73
|
+
|
|
74
|
+
::_EVENT_EMITTER_METHODS_::
|
|
75
|
+
|
|
76
|
+
private:
|
|
77
|
+
winrt::Microsoft::ReactNative::EventEmitter m_eventEmitter{nullptr};
|
|
78
|
+
};`;
|
|
79
|
+
|
|
80
|
+
const baseStructTemplate = `
|
|
81
|
+
template<typename TUserData>
|
|
82
|
+
struct Base::_COMPONENT_NAME_:: {
|
|
83
|
+
|
|
84
|
+
virtual void UpdateProps(
|
|
85
|
+
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
|
|
86
|
+
const winrt::com_ptr<::_COMPONENT_NAME_::Props> &newProps,
|
|
87
|
+
const winrt::com_ptr<::_COMPONENT_NAME_::Props> &/*oldProps*/) noexcept {
|
|
88
|
+
m_props = newProps;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// UpdateState will only be called if this method is overridden
|
|
92
|
+
virtual void UpdateState(
|
|
93
|
+
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
|
|
94
|
+
const winrt::Microsoft::ReactNative::IComponentState &/*newState*/) noexcept {
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
virtual void UpdateEventEmitter(const std::shared_ptr<::_COMPONENT_NAME_::EventEmitter> &eventEmitter) noexcept {
|
|
98
|
+
m_eventEmitter = eventEmitter;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// MountChildComponentView will only be called if this method is overridden
|
|
102
|
+
virtual void MountChildComponentView(const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
|
|
103
|
+
const winrt::Microsoft::ReactNative::MountChildComponentViewArgs &/*args*/) noexcept {
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// UnmountChildComponentView will only be called if this method is overridden
|
|
107
|
+
virtual void UnmountChildComponentView(const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
|
|
108
|
+
const winrt::Microsoft::ReactNative::UnmountChildComponentViewArgs &/*args*/) noexcept {
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Initialize will only be called if this method is overridden
|
|
112
|
+
virtual void Initialize(const winrt::Microsoft::ReactNative::ComponentView &/*view*/) noexcept {
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// CreateVisual will only be called if this method is overridden
|
|
116
|
+
virtual winrt::Microsoft::UI::Composition::Visual CreateVisual(const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
|
|
117
|
+
return view.as<winrt::Microsoft::ReactNative::Composition::ComponentView>().Compositor().CreateSpriteVisual();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// FinalizeUpdate will only be called if this method is overridden
|
|
121
|
+
virtual void FinalizeUpdate(const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
|
|
122
|
+
winrt::Microsoft::ReactNative::ComponentViewUpdateMask /*mask*/) noexcept {
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
::_COMPONENT_VIEW_COMMAND_HANDLERS_::
|
|
126
|
+
|
|
127
|
+
::_COMPONENT_VIEW_COMMAND_HANDLER_::
|
|
128
|
+
|
|
129
|
+
const std::shared_ptr<::_COMPONENT_NAME_::EventEmitter>& EventEmitter() const { return m_eventEmitter; }
|
|
130
|
+
const winrt::com_ptr<::_COMPONENT_NAME_::Props>& Props() const { return m_props; }
|
|
131
|
+
|
|
132
|
+
private:
|
|
133
|
+
winrt::com_ptr<::_COMPONENT_NAME_::Props> m_props;
|
|
134
|
+
std::shared_ptr<::_COMPONENT_NAME_::EventEmitter> m_eventEmitter;
|
|
135
|
+
};
|
|
136
|
+
`;
|
|
137
|
+
|
|
138
|
+
const registerTemplate = `
|
|
139
|
+
template <typename TUserData>
|
|
140
|
+
void Register::_COMPONENT_NAME_::NativeComponent(
|
|
141
|
+
winrt::Microsoft::ReactNative::IReactPackageBuilder const &packageBuilder,
|
|
142
|
+
std::function<void(const winrt::Microsoft::ReactNative::Composition::IReactCompositionViewComponentBuilder&)> builderCallback) noexcept {
|
|
143
|
+
packageBuilder.as<winrt::Microsoft::ReactNative::IReactPackageBuilderFabric>().AddViewComponent(
|
|
144
|
+
L"::_COMPONENT_NAME_::", [builderCallback](winrt::Microsoft::ReactNative::IReactViewComponentBuilder const &builder) noexcept {
|
|
145
|
+
auto compBuilder = builder.as<winrt::Microsoft::ReactNative::Composition::IReactCompositionViewComponentBuilder>();
|
|
146
|
+
|
|
147
|
+
builder.SetCreateProps(
|
|
148
|
+
[](winrt::Microsoft::ReactNative::ViewProps props) noexcept { return winrt::make<::_COMPONENT_NAME_::Props>(props); });
|
|
149
|
+
|
|
150
|
+
builder.SetUpdatePropsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
151
|
+
const winrt::Microsoft::ReactNative::IComponentProps &newProps,
|
|
152
|
+
const winrt::Microsoft::ReactNative::IComponentProps &oldProps) noexcept {
|
|
153
|
+
auto userData = view.UserData().as<TUserData>();
|
|
154
|
+
userData->UpdateProps(view, newProps ? newProps.as<::_COMPONENT_NAME_::Props>() : nullptr, oldProps ? oldProps.as<::_COMPONENT_NAME_::Props>() : nullptr);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
builder.SetUpdateEventEmitterHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
158
|
+
const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter) noexcept {
|
|
159
|
+
auto userData = view.UserData().as<TUserData>();
|
|
160
|
+
userData->UpdateEventEmitter(std::make_shared<::_COMPONENT_NAME_::EventEmitter>(eventEmitter));
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
if constexpr (&TUserData::FinalizeUpdate != &Base::_COMPONENT_NAME_::<TUserData>::FinalizeUpdate) {
|
|
164
|
+
builder.SetFinalizeUpdateHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
165
|
+
winrt::Microsoft::ReactNative::ComponentViewUpdateMask mask) noexcept {
|
|
166
|
+
auto userData = view.UserData().as<TUserData>();
|
|
167
|
+
userData->FinalizeUpdate(view, mask);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if constexpr (&TUserData::UpdateState != &Base::_COMPONENT_NAME_::<TUserData>::UpdateState) {
|
|
172
|
+
builder.SetUpdateStateHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
173
|
+
const winrt::Microsoft::ReactNative::IComponentState &newState) noexcept {
|
|
174
|
+
auto userData = view.UserData().as<TUserData>();
|
|
175
|
+
userData->member(view, newState);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
::_REGISTER_CUSTOM_COMMAND_HANDLER_::
|
|
180
|
+
|
|
181
|
+
if constexpr (&TUserData::MountChildComponentView != &Base::_COMPONENT_NAME_::<TUserData>::MountChildComponentView) {
|
|
182
|
+
builder.SetMountChildComponentViewHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
183
|
+
const winrt::Microsoft::ReactNative::MountChildComponentViewArgs &args) noexcept {
|
|
184
|
+
auto userData = view.UserData().as<TUserData>();
|
|
185
|
+
return userData->MountChildComponentView(view, args);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if constexpr (&TUserData::UnmountChildComponentView != &Base::_COMPONENT_NAME_::<TUserData>::UnmountChildComponentView) {
|
|
190
|
+
builder.SetUnmountChildComponentViewHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
191
|
+
const winrt::Microsoft::ReactNative::UnmountChildComponentViewArgs &args) noexcept {
|
|
192
|
+
auto userData = view.UserData().as<TUserData>();
|
|
193
|
+
return userData->UnmountChildComponentView(view, args);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
compBuilder.SetViewComponentViewInitializer([](const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
|
|
198
|
+
auto userData = winrt::make_self<TUserData>();
|
|
199
|
+
if constexpr (&TUserData::Initialize != &Base::_COMPONENT_NAME_::<TUserData>::Initialize) {
|
|
200
|
+
userData->Initialize(view);
|
|
201
|
+
}
|
|
202
|
+
view.UserData(*userData);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if constexpr (&TUserData::CreateVisual != &Base::_COMPONENT_NAME_::<TUserData>::CreateVisual) {
|
|
206
|
+
compBuilder.SetCreateVisualHandler([](const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
|
|
207
|
+
auto userData = view.UserData().as<TUserData>();
|
|
208
|
+
return userData->CreateVisual(view);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Allow app to further customize the builder
|
|
213
|
+
if (builderCallback) {
|
|
214
|
+
builderCallback(compBuilder);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
`;
|
|
219
|
+
|
|
220
|
+
const fileTemplate = `
|
|
221
|
+
${headerTemplate}
|
|
222
|
+
|
|
223
|
+
namespace ::_NAMESPACE_:: {
|
|
224
|
+
|
|
225
|
+
::_COMPONENT_PROP_OBJECT_TYPES_::
|
|
226
|
+
::_COMPONENT_PROP_TYPES_::
|
|
227
|
+
|
|
228
|
+
::_COMPONENT_EVENT_EMITTER_::
|
|
229
|
+
|
|
230
|
+
::_BASE_COMPONENT_STRUCT_::
|
|
231
|
+
|
|
232
|
+
::_COMPONENT_REGISTRATION_::
|
|
233
|
+
} // namespace ::_NAMESPACE_::
|
|
234
|
+
`;
|
|
235
|
+
|
|
236
|
+
function capitalizeFirstLetter(s: string) {
|
|
237
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function createComponentGenerator({
|
|
241
|
+
namespace,
|
|
242
|
+
cppStringType,
|
|
243
|
+
}: {
|
|
244
|
+
namespace: string;
|
|
245
|
+
cppStringType: CppStringTypes;
|
|
246
|
+
}) {
|
|
247
|
+
return (
|
|
248
|
+
_libraryName: string,
|
|
249
|
+
schema: SchemaType,
|
|
250
|
+
_moduleSpecName: string,
|
|
251
|
+
): FilesOutput => {
|
|
252
|
+
const files = new Map<string, string>();
|
|
253
|
+
|
|
254
|
+
const cppCodegenOptions = {cppStringType};
|
|
255
|
+
|
|
256
|
+
for (const componentName of Object.keys(schema.modules)) {
|
|
257
|
+
const component = schema.modules[componentName];
|
|
258
|
+
setPreferredModuleName(componentName);
|
|
259
|
+
|
|
260
|
+
if (component.type === 'Component') {
|
|
261
|
+
console.log(`Generating ${componentName}.g.h`);
|
|
262
|
+
|
|
263
|
+
const componentShape = component.components[componentName];
|
|
264
|
+
|
|
265
|
+
componentShape.extendsProps.forEach(propsBaseType => {
|
|
266
|
+
if (
|
|
267
|
+
(propsBaseType.type as any) !== 'ReactNativeBuiltInType' ||
|
|
268
|
+
(propsBaseType.knownTypeName as any) !== 'ReactNativeCoreViewProps'
|
|
269
|
+
) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
'Currently only supports props extending from ViewProps',
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// Props
|
|
277
|
+
const propObjectAliases: AliasMap<
|
|
278
|
+
ObjectTypeAnnotation<PropTypeAnnotation>
|
|
279
|
+
> = {types: {}, jobs: []};
|
|
280
|
+
const propsName = `${componentName}Props`;
|
|
281
|
+
const propsFields = componentShape.props
|
|
282
|
+
.map(prop => {
|
|
283
|
+
const propType = translateComponentPropsFieldType(
|
|
284
|
+
prop.typeAnnotation,
|
|
285
|
+
propObjectAliases,
|
|
286
|
+
`${propsName}_${prop.name}`,
|
|
287
|
+
cppCodegenOptions,
|
|
288
|
+
);
|
|
289
|
+
return ` REACT_FIELD(${prop.name})\n ${
|
|
290
|
+
prop.optional && !propType.alreadySupportsOptionalOrHasDefault
|
|
291
|
+
? `std::optional<${propType.type}>`
|
|
292
|
+
: propType.type
|
|
293
|
+
} ${prop.name}${propType.initializer};\n`;
|
|
294
|
+
})
|
|
295
|
+
.join('\n');
|
|
296
|
+
|
|
297
|
+
const propObjectTypes = propObjectAliases.jobs
|
|
298
|
+
.map(propObjectTypeName => {
|
|
299
|
+
const propObjectType = propObjectAliases.types[propObjectTypeName]!;
|
|
300
|
+
const propsObjectFields = propObjectType.properties
|
|
301
|
+
.map(property => {
|
|
302
|
+
const propType = translateComponentPropsFieldType(
|
|
303
|
+
property.typeAnnotation,
|
|
304
|
+
propObjectAliases,
|
|
305
|
+
`${propsName}_${property.name}`,
|
|
306
|
+
cppCodegenOptions,
|
|
307
|
+
);
|
|
308
|
+
return ` REACT_FIELD(${property.name})\n ${
|
|
309
|
+
property.optional &&
|
|
310
|
+
!propType.alreadySupportsOptionalOrHasDefault
|
|
311
|
+
? `std::optional<${propType.type}>`
|
|
312
|
+
: propType.type
|
|
313
|
+
} ${property.name}${propType.initializer};\n`;
|
|
314
|
+
})
|
|
315
|
+
.join('\n');
|
|
316
|
+
|
|
317
|
+
return propsObjectTemplate
|
|
318
|
+
.replace(
|
|
319
|
+
/::_OBJECT_NAME_::/g,
|
|
320
|
+
getAliasCppName(propObjectTypeName),
|
|
321
|
+
)
|
|
322
|
+
.replace(/::_OBJECT_FIELDS_::/g, propsObjectFields);
|
|
323
|
+
})
|
|
324
|
+
.join('\n');
|
|
325
|
+
|
|
326
|
+
// Events
|
|
327
|
+
const eventObjectAliases: AliasMap<
|
|
328
|
+
ObjectTypeAnnotation<EventTypeAnnotation>
|
|
329
|
+
> = {types: {}, jobs: []};
|
|
330
|
+
const eventEmitterName = `${componentName}EventEmitter`;
|
|
331
|
+
const eventEmitterMethods = componentShape.events
|
|
332
|
+
.filter(event => event.typeAnnotation.argument)
|
|
333
|
+
.map(event => {
|
|
334
|
+
if (event.typeAnnotation.argument?.baseTypes) {
|
|
335
|
+
throw new Error(
|
|
336
|
+
'Events with base type arguments not currently supported',
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Called to collect the eventObjectAliases
|
|
341
|
+
translateComponentEventType(
|
|
342
|
+
event.typeAnnotation.argument!,
|
|
343
|
+
eventObjectAliases,
|
|
344
|
+
`${event.name}`,
|
|
345
|
+
cppCodegenOptions,
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
// onSomething -> something
|
|
349
|
+
let eventNameLower = event.name.replace('on', '');
|
|
350
|
+
eventNameLower =
|
|
351
|
+
eventNameLower[0].toLowerCase() + eventNameLower.slice(1);
|
|
352
|
+
|
|
353
|
+
return eventEmitterMethodTemplate
|
|
354
|
+
.replace(/::_EVENT_NAME_::/g, event.name)
|
|
355
|
+
.replace(/::_EVENT_NAME_NO_ON_::/g, eventNameLower)
|
|
356
|
+
.replace(
|
|
357
|
+
/::_EVENT_OBJECT_TYPE_::/g,
|
|
358
|
+
event.name.replace('on', 'On'),
|
|
359
|
+
);
|
|
360
|
+
})
|
|
361
|
+
.join('\n\n');
|
|
362
|
+
|
|
363
|
+
const eventObjects = eventObjectAliases.jobs
|
|
364
|
+
.map(eventObjectTypeName => {
|
|
365
|
+
const eventObjectType =
|
|
366
|
+
eventObjectAliases.types[eventObjectTypeName]!;
|
|
367
|
+
const eventObjectFields = eventObjectType.properties
|
|
368
|
+
.map(property => {
|
|
369
|
+
const eventPropType = translateComponentEventType(
|
|
370
|
+
property.typeAnnotation,
|
|
371
|
+
eventObjectAliases,
|
|
372
|
+
eventObjectTypeName,
|
|
373
|
+
cppCodegenOptions,
|
|
374
|
+
);
|
|
375
|
+
return ` REACT_FIELD(${property.name})\n ${
|
|
376
|
+
property.optional &&
|
|
377
|
+
!eventPropType.alreadySupportsOptionalOrHasDefault
|
|
378
|
+
? `std::optional<${eventPropType.type}>`
|
|
379
|
+
: eventPropType.type
|
|
380
|
+
} ${property.name}${eventPropType.initializer};\n`;
|
|
381
|
+
})
|
|
382
|
+
.join('\n');
|
|
383
|
+
return eventsObjectTemplate
|
|
384
|
+
.replace(
|
|
385
|
+
/::_OBJECT_NAME_::/g,
|
|
386
|
+
`${componentName}_${eventObjectTypeName.replace('on', 'On')}`,
|
|
387
|
+
)
|
|
388
|
+
.replace(/::_OBJECT_FIELDS_::/g, eventObjectFields);
|
|
389
|
+
})
|
|
390
|
+
.join('\n');
|
|
391
|
+
|
|
392
|
+
const eventObjectUsings = eventObjectAliases.jobs
|
|
393
|
+
.map(eventObjectTypeName => {
|
|
394
|
+
return ` using ${eventObjectTypeName.replace(
|
|
395
|
+
'on',
|
|
396
|
+
'On',
|
|
397
|
+
)} = ${componentName}_${eventObjectTypeName.replace('on', 'On')};`;
|
|
398
|
+
})
|
|
399
|
+
.join('\n');
|
|
400
|
+
|
|
401
|
+
const eventEmitter = eventEmitterTemplate
|
|
402
|
+
.replace(/::_COMPONENT_EVENT_OBJECT_TYPES_::/g, eventObjects)
|
|
403
|
+
.replace(/::_EVENT_EMITTER_METHODS_::/g, eventEmitterMethods)
|
|
404
|
+
.replace(/::_EVENT_EMITTER_USINGS_::/g, eventObjectUsings);
|
|
405
|
+
|
|
406
|
+
// Commands
|
|
407
|
+
const commandAliases: AliasMap<
|
|
408
|
+
ObjectTypeAnnotation<CommandParamTypeAnnotation>
|
|
409
|
+
> = {types: {}, jobs: []};
|
|
410
|
+
const hasAnyCommands = componentShape.commands.length !== 0;
|
|
411
|
+
const commandHandlers = hasAnyCommands
|
|
412
|
+
? componentShape.commands
|
|
413
|
+
.map(command => {
|
|
414
|
+
const commandArgs = command.typeAnnotation.params
|
|
415
|
+
.map(param => {
|
|
416
|
+
const commandArgType = translateCommandParamType(
|
|
417
|
+
param.typeAnnotation,
|
|
418
|
+
commandAliases,
|
|
419
|
+
`${componentName}_${command.name}`,
|
|
420
|
+
cppCodegenOptions,
|
|
421
|
+
);
|
|
422
|
+
return `${
|
|
423
|
+
param.optional &&
|
|
424
|
+
!commandArgType.alreadySupportsOptionalOrHasDefault
|
|
425
|
+
? `std::optional<${commandArgType.type}>`
|
|
426
|
+
: commandArgType.type
|
|
427
|
+
} ${param.name}`;
|
|
428
|
+
})
|
|
429
|
+
.join(', ');
|
|
430
|
+
|
|
431
|
+
return ` // You must provide an implementation of this method to handle the "${
|
|
432
|
+
command.name
|
|
433
|
+
}" command
|
|
434
|
+
virtual void Handle${capitalizeFirstLetter(
|
|
435
|
+
command.name,
|
|
436
|
+
)}Command(${commandArgs}) noexcept = 0;`;
|
|
437
|
+
})
|
|
438
|
+
.join('\n\n')
|
|
439
|
+
: '';
|
|
440
|
+
|
|
441
|
+
const commandHandler = hasAnyCommands
|
|
442
|
+
? `void HandleCommand(const winrt::Microsoft::ReactNative::ComponentView &view, const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
|
|
443
|
+
auto userData = view.UserData().as<TUserData>();
|
|
444
|
+
auto commandName = args.CommandName();
|
|
445
|
+
${componentShape.commands
|
|
446
|
+
.map(command => {
|
|
447
|
+
const commaSeparatedCommandArgs = command.typeAnnotation.params
|
|
448
|
+
.map(param => param.name)
|
|
449
|
+
.join(', ');
|
|
450
|
+
return ` if (commandName == L"${command.name}") {
|
|
451
|
+
${
|
|
452
|
+
command.typeAnnotation.params.length !== 0
|
|
453
|
+
? ` ${command.typeAnnotation.params
|
|
454
|
+
.map(param => {
|
|
455
|
+
const commandArgType = translateCommandParamType(
|
|
456
|
+
param.typeAnnotation,
|
|
457
|
+
commandAliases,
|
|
458
|
+
`${componentName}_${command.name}`,
|
|
459
|
+
cppCodegenOptions,
|
|
460
|
+
);
|
|
461
|
+
return `${
|
|
462
|
+
param.optional &&
|
|
463
|
+
!commandArgType.alreadySupportsOptionalOrHasDefault
|
|
464
|
+
? `std::optional<${commandArgType.type}>`
|
|
465
|
+
: commandArgType.type
|
|
466
|
+
} ${param.name};`;
|
|
467
|
+
})
|
|
468
|
+
.join('\n')}
|
|
469
|
+
winrt::Microsoft::ReactNative::ReadArgs(args.CommandArgs(), ${commaSeparatedCommandArgs});`
|
|
470
|
+
: ''
|
|
471
|
+
}
|
|
472
|
+
userData->Handle${capitalizeFirstLetter(
|
|
473
|
+
command.name,
|
|
474
|
+
)}Command(${commaSeparatedCommandArgs});
|
|
475
|
+
return;
|
|
476
|
+
}`;
|
|
477
|
+
})
|
|
478
|
+
.join('\n\n')}
|
|
479
|
+
}`
|
|
480
|
+
: '';
|
|
481
|
+
|
|
482
|
+
const registerCommandHandler = hasAnyCommands
|
|
483
|
+
? ` builder.SetCustomCommandHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
484
|
+
const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
|
|
485
|
+
auto userData = view.UserData().as<TUserData>();
|
|
486
|
+
userData->HandleCommand(view, args);
|
|
487
|
+
});`
|
|
488
|
+
: '';
|
|
489
|
+
|
|
490
|
+
const baseType = baseStructTemplate
|
|
491
|
+
.replace(/::_COMPONENT_VIEW_COMMAND_HANDLERS_::/g, commandHandlers)
|
|
492
|
+
.replace(/::_COMPONENT_VIEW_COMMAND_HANDLER_::/g, commandHandler);
|
|
493
|
+
|
|
494
|
+
// Registration
|
|
495
|
+
const componentRegistration = registerTemplate.replace(
|
|
496
|
+
/::_REGISTER_CUSTOM_COMMAND_HANDLER_::/g,
|
|
497
|
+
registerCommandHandler,
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
// Final output
|
|
501
|
+
const replaceContent = function (template: string): string {
|
|
502
|
+
return template
|
|
503
|
+
.replace(/::_COMPONENT_PROP_OBJECT_TYPES_::/g, propObjectTypes)
|
|
504
|
+
.replace(/::_COMPONENT_PROP_TYPES_::/g, propsTemplate)
|
|
505
|
+
.replace(/::_COMPONENT_EVENT_EMITTER_::/g, eventEmitter)
|
|
506
|
+
.replace(/::_BASE_COMPONENT_STRUCT_::/g, baseType)
|
|
507
|
+
.replace(/::_COMPONENT_REGISTRATION_::/g, componentRegistration)
|
|
508
|
+
.replace(/::_EVENT_EMITTER_NAME_::/g, eventEmitterName)
|
|
509
|
+
.replace(/::_PROPS_NAME_::/g, propsName)
|
|
510
|
+
.replace(/::_COMPONENT_NAME_::/g, componentName)
|
|
511
|
+
.replace(/::_PROPS_FIELDS_::/g, propsFields)
|
|
512
|
+
.replace(/::_NAMESPACE_::/g, namespace)
|
|
513
|
+
.replace(/\n\n\n+/g, '\n\n');
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
files.set(`${componentName}.g.h`, replaceContent(fileTemplate));
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return files;
|
|
521
|
+
};
|
|
522
|
+
}
|