|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "BaseParagraphProps.h" |
| 9 | + |
| 10 | +#include <react/featureflags/ReactNativeFeatureFlags.h> |
| 11 | +#include <react/renderer/attributedstring/conversions.h> |
| 12 | +#include <react/renderer/attributedstring/primitives.h> |
| 13 | +#include <react/renderer/core/propsConversions.h> |
| 14 | +#include <react/renderer/debug/debugStringConvertibleUtils.h> |
| 15 | + |
| 16 | +#include <glog/logging.h> |
| 17 | + |
| 18 | +namespace facebook::react { |
| 19 | + |
| 20 | +BaseParagraphProps::BaseParagraphProps( |
| 21 | + const PropsParserContext& context, |
| 22 | + const BaseParagraphProps& sourceProps, |
| 23 | + const RawProps& rawProps) |
| 24 | + : ViewProps(context, sourceProps, rawProps), |
| 25 | + BaseTextProps(context, sourceProps, rawProps), |
| 26 | + paragraphAttributes( |
| 27 | + ReactNativeFeatureFlags::enableCppPropsIteratorSetter() |
| 28 | + ? sourceProps.paragraphAttributes |
| 29 | + : convertRawProp( |
| 30 | + context, |
| 31 | + rawProps, |
| 32 | + sourceProps.paragraphAttributes, |
| 33 | + {})), |
| 34 | + isSelectable( |
| 35 | + ReactNativeFeatureFlags::enableCppPropsIteratorSetter() |
| 36 | + ? sourceProps.isSelectable |
| 37 | + : convertRawProp( |
| 38 | + context, |
| 39 | + rawProps, |
| 40 | + "selectable", |
| 41 | + sourceProps.isSelectable, |
| 42 | + false)), |
| 43 | + onTextLayout( |
| 44 | + ReactNativeFeatureFlags::enableCppPropsIteratorSetter() |
| 45 | + ? sourceProps.onTextLayout |
| 46 | + : convertRawProp( |
| 47 | + context, |
| 48 | + rawProps, |
| 49 | + "onTextLayout", |
| 50 | + sourceProps.onTextLayout, |
| 51 | + {})), |
| 52 | + // [Windows] |
| 53 | + selectionColor( |
| 54 | + ReactNativeFeatureFlags::enableCppPropsIteratorSetter() |
| 55 | + ? sourceProps.selectionColor |
| 56 | + : convertRawProp( |
| 57 | + context, |
| 58 | + rawProps, |
| 59 | + "selectionColor", |
| 60 | + sourceProps.selectionColor, |
| 61 | + {})) { |
| 62 | + /* |
| 63 | + * These props are applied to `View`, therefore they must not be a part of |
| 64 | + * base text attributes. |
| 65 | + */ |
| 66 | + textAttributes.opacity = std::numeric_limits<Float>::quiet_NaN(); |
| 67 | + textAttributes.backgroundColor = {}; |
| 68 | +}; |
| 69 | + |
| 70 | +void BaseParagraphProps::setProp( |
| 71 | + const PropsParserContext& context, |
| 72 | + RawPropsPropNameHash hash, |
| 73 | + const char* propName, |
| 74 | + const RawValue& value) { |
| 75 | + // All Props structs setProp methods must always, unconditionally, |
| 76 | + // call all super::setProp methods, since multiple structs may |
| 77 | + // reuse the same values. |
| 78 | + ViewProps::setProp(context, hash, propName, value); |
| 79 | + BaseTextProps::setProp(context, hash, propName, value); |
| 80 | + |
| 81 | + static auto defaults = BaseParagraphProps{}; |
| 82 | + |
| 83 | + // ParagraphAttributes has its own switch statement - to keep all |
| 84 | + // of these fields together, and because there are some collisions between |
| 85 | + // propnames parsed here and outside of ParagraphAttributes. |
| 86 | + // This code is also duplicated in AndroidTextInput. |
| 87 | + static auto paDefaults = ParagraphAttributes{}; |
| 88 | + switch (hash) { |
| 89 | + REBUILD_FIELD_SWITCH_CASE( |
| 90 | + paDefaults, |
| 91 | + value, |
| 92 | + paragraphAttributes, |
| 93 | + maximumNumberOfLines, |
| 94 | + "numberOfLines"); |
| 95 | + REBUILD_FIELD_SWITCH_CASE( |
| 96 | + paDefaults, value, paragraphAttributes, ellipsizeMode, "ellipsizeMode"); |
| 97 | + REBUILD_FIELD_SWITCH_CASE( |
| 98 | + paDefaults, |
| 99 | + value, |
| 100 | + paragraphAttributes, |
| 101 | + textBreakStrategy, |
| 102 | + "textBreakStrategy"); |
| 103 | + REBUILD_FIELD_SWITCH_CASE( |
| 104 | + paDefaults, |
| 105 | + value, |
| 106 | + paragraphAttributes, |
| 107 | + adjustsFontSizeToFit, |
| 108 | + "adjustsFontSizeToFit"); |
| 109 | + REBUILD_FIELD_SWITCH_CASE( |
| 110 | + paDefaults, |
| 111 | + value, |
| 112 | + paragraphAttributes, |
| 113 | + minimumFontScale, |
| 114 | + "minimumFontScale"); |
| 115 | + REBUILD_FIELD_SWITCH_CASE( |
| 116 | + paDefaults, |
| 117 | + value, |
| 118 | + paragraphAttributes, |
| 119 | + minimumFontSize, |
| 120 | + "minimumFontSize"); |
| 121 | + REBUILD_FIELD_SWITCH_CASE( |
| 122 | + paDefaults, |
| 123 | + value, |
| 124 | + paragraphAttributes, |
| 125 | + maximumFontSize, |
| 126 | + "maximumFontSize"); |
| 127 | + REBUILD_FIELD_SWITCH_CASE( |
| 128 | + paDefaults, |
| 129 | + value, |
| 130 | + paragraphAttributes, |
| 131 | + includeFontPadding, |
| 132 | + "includeFontPadding"); |
| 133 | + REBUILD_FIELD_SWITCH_CASE( |
| 134 | + paDefaults, |
| 135 | + value, |
| 136 | + paragraphAttributes, |
| 137 | + android_hyphenationFrequency, |
| 138 | + "android_hyphenationFrequency"); |
| 139 | + REBUILD_FIELD_SWITCH_CASE( |
| 140 | + paDefaults, |
| 141 | + value, |
| 142 | + paragraphAttributes, |
| 143 | + textAlignVertical, |
| 144 | + "textAlignVertical"); |
| 145 | + } |
| 146 | + |
| 147 | + switch (hash) { |
| 148 | + RAW_SET_PROP_SWITCH_CASE(isSelectable, "selectable"); |
| 149 | + RAW_SET_PROP_SWITCH_CASE_BASIC(onTextLayout); |
| 150 | + // [Windows] |
| 151 | + RAW_SET_PROP_SWITCH_CASE_BASIC(selectionColor); |
| 152 | + } |
| 153 | + |
| 154 | + /* |
| 155 | + * These props are applied to `View`, therefore they must not be a part of |
| 156 | + * base text attributes. |
| 157 | + */ |
| 158 | + textAttributes.opacity = std::numeric_limits<Float>::quiet_NaN(); |
| 159 | + textAttributes.backgroundColor = {}; |
| 160 | +} |
| 161 | + |
| 162 | +#pragma mark - DebugStringConvertible |
| 163 | + |
| 164 | +#if RN_DEBUG_STRING_CONVERTIBLE |
| 165 | +SharedDebugStringConvertibleList BaseParagraphProps::getDebugProps() const { |
| 166 | + return ViewProps::getDebugProps() + BaseTextProps::getDebugProps() + |
| 167 | + paragraphAttributes.getDebugProps() + |
| 168 | + SharedDebugStringConvertibleList{ |
| 169 | + debugStringConvertibleItem("selectable", isSelectable), |
| 170 | + // [Windows] |
| 171 | + debugStringConvertibleItem("selectionColor", selectionColor)}; |
| 172 | +} |
| 173 | +#endif |
| 174 | +} // namespace facebook::react |
0 commit comments