1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_MODEL_H 18 19 #include <functional> 20 #include <mutex> 21 #include <optional> 22 23 #include "base/image/pixel_map.h" 24 #include "base/memory/ace_type.h" 25 #include "core/components/common/layout/constants.h" 26 #include "core/components/common/properties/text_style.h" 27 #include "core/components_ng/base/view_abstract_model.h" 28 #include "core/components_ng/pattern/rich_editor/rich_editor_event_hub.h" 29 #include "core/components_ng/pattern/rich_editor/selection_info.h" 30 #include "core/components_ng/pattern/text_field/text_selector.h" 31 #include "core/components_ng/property/border_property.h" 32 #include "core/components_ng/render/paragraph.h" 33 #include "core/common/resource/resource_object.h" 34 35 namespace OHOS::Ace { 36 struct UserGestureOptions { 37 GestureEventFunc onClick; 38 GestureEventFunc onLongPress; 39 }; 40 41 struct ImageSpanSize { 42 CalcDimension width; 43 CalcDimension height; 44 }; 45 46 struct ImageSpanAttribute { 47 std::optional<ImageSpanSize> size; 48 std::optional<VerticalAlign> verticalAlign; 49 std::optional<ImageFit> objectFit; 50 std::optional<OHOS::Ace::NG::MarginProperty> marginProp; 51 std::optional<OHOS::Ace::NG::BorderRadiusProperty> borderRadius; 52 }; 53 54 struct SpanOptionBase { 55 std::optional<int32_t> offset; 56 UserGestureOptions userGestureOption; 57 }; 58 59 struct ImageSpanOptions : SpanOptionBase { 60 std::optional<int32_t> offset; 61 std::optional<std::string> image; 62 std::optional<std::string> bundleName; 63 std::optional<std::string> moduleName; 64 std::optional<RefPtr<PixelMap>> imagePixelMap; 65 std::optional<ImageSpanAttribute> imageAttribute; 66 }; 67 68 struct SpanPositionInfo { SpanPositionInfoSpanPositionInfo69 SpanPositionInfo(int32_t index, int32_t start, int32_t end, int32_t offset) 70 : spanIndex_(index), spanStart_(start), spanEnd_(end), spanOffset_(offset) 71 {} 72 SpanPositionInfoSpanPositionInfo73 SpanPositionInfo() 74 { 75 spanIndex_ = 0; 76 spanStart_ = 0; 77 spanEnd_ = 0; 78 spanOffset_ = 0; 79 } 80 81 int32_t spanIndex_ = 0; 82 int32_t spanStart_ = 0; 83 int32_t spanEnd_ = 0; 84 int32_t spanOffset_ = 0; 85 ToStringSpanPositionInfo86 std::string ToString() 87 { 88 return "spanIndex: " + std::to_string(spanIndex_) + ", spanStart: " + std::to_string(spanStart_) + ", spanEnd" + 89 std::to_string(spanEnd_) + ", spanOffset: " + std::to_string(spanOffset_); 90 } 91 }; 92 93 struct UpdateSpanStyle { ResetStyleUpdateSpanStyle94 void ResetStyle() 95 { 96 updateTextColor.reset(); 97 updateFontSize.reset(); 98 updateItalicFontStyle.reset(); 99 updateFontWeight.reset(); 100 updateFontFamily.reset(); 101 updateTextDecoration.reset(); 102 updateTextDecorationColor.reset(); 103 updateTextShadows.reset(); 104 105 updateSymbolColor.reset(); 106 updateSymbolRenderingStrategy.reset(); 107 updateSymbolEffectStrategy.reset(); 108 109 updateImageWidth.reset(); 110 updateImageHeight.reset(); 111 updateImageVerticalAlign.reset(); 112 updateImageFit.reset(); 113 marginProp.reset(); 114 borderRadius.reset(); 115 isSymbolStyle = false; 116 } 117 118 std::optional<Color> updateTextColor = std::nullopt; 119 std::optional<CalcDimension> updateFontSize = std::nullopt; 120 std::optional<FontStyle> updateItalicFontStyle = std::nullopt; 121 std::optional<FontWeight> updateFontWeight = std::nullopt; 122 std::optional<std::vector<std::string>> updateFontFamily = std::nullopt; 123 std::optional<TextDecoration> updateTextDecoration = std::nullopt; 124 std::optional<Color> updateTextDecorationColor = std::nullopt; 125 std::optional<std::vector<Shadow>> updateTextShadows = std::nullopt; 126 127 std::optional<std::vector<Color>> updateSymbolColor = std::nullopt; 128 std::optional<uint32_t> updateSymbolRenderingStrategy = std::nullopt; 129 std::optional<uint32_t> updateSymbolEffectStrategy = std::nullopt; 130 131 std::optional<CalcDimension> updateImageWidth = std::nullopt; 132 std::optional<CalcDimension> updateImageHeight = std::nullopt; 133 std::optional<VerticalAlign> updateImageVerticalAlign = std::nullopt; 134 std::optional<ImageFit> updateImageFit = std::nullopt; 135 std::optional<OHOS::Ace::NG::MarginProperty> marginProp = std::nullopt; 136 std::optional<OHOS::Ace::NG::BorderRadiusProperty> borderRadius = std::nullopt; 137 bool hasResourceFontColor = false; 138 bool hasResourceDecorationColor = false; 139 bool isSymbolStyle = false; 140 }; 141 142 struct UpdateParagraphStyle { ResetUpdateParagraphStyle143 void Reset() 144 { 145 textAlign.reset(); 146 leadingMargin.reset(); 147 } 148 std::optional<TextAlign> textAlign; 149 std::optional<NG::LeadingMargin> leadingMargin; 150 }; 151 152 struct RangeOptions { 153 std::optional<int32_t> start; 154 std::optional<int32_t> end; 155 }; 156 157 struct TextSpanOptions : SpanOptionBase { 158 std::optional<int32_t> offset; 159 std::string value; 160 std::optional<TextStyle> style; 161 std::optional<UpdateParagraphStyle> paraStyle; 162 UserGestureOptions userGestureOption; 163 bool hasResourceFontColor = false; 164 bool hasResourceDecorationColor = false; 165 }; 166 167 struct SymbolSpanOptions : SpanOptionBase { 168 std::optional<int32_t> offset; 169 uint32_t symbolId; 170 std::optional<TextStyle> style; 171 RefPtr<ResourceObject> resourceObject; 172 }; 173 174 class ACE_EXPORT RichEditorControllerBase : public AceType { 175 DECLARE_ACE_TYPE(RichEditorControllerBase, AceType); 176 177 public: 178 virtual int32_t AddImageSpan(const ImageSpanOptions& options) = 0; 179 virtual int32_t AddTextSpan(const TextSpanOptions& options) = 0; 180 virtual int32_t AddSymbolSpan(const SymbolSpanOptions& options) = 0; 181 virtual int32_t AddPlaceholderSpan(const RefPtr<NG::UINode>& customNode, const SpanOptionBase& options) = 0; 182 virtual int32_t GetCaretOffset() = 0; 183 virtual bool SetCaretOffset(int32_t caretPosition) = 0; 184 virtual void UpdateParagraphStyle(int32_t start, int32_t end, const UpdateParagraphStyle& style) = 0; 185 virtual void UpdateSpanStyle(int32_t start, int32_t end, TextStyle textStyle, ImageSpanAttribute imageStyle) = 0; 186 virtual void SetTypingStyle(struct UpdateSpanStyle& typingStyle, TextStyle textStyle) = 0; 187 virtual void SetUpdateSpanStyle(struct UpdateSpanStyle updateSpanStyle) = 0; 188 virtual SelectionInfo GetSpansInfo(int32_t start, int32_t end) = 0; 189 virtual std::vector<ParagraphInfo> GetParagraphsInfo(int32_t start, int32_t end) = 0; 190 virtual void DeleteSpans(const RangeOptions& options) = 0; 191 virtual void CloseSelectionMenu() = 0; 192 virtual SelectionInfo GetSelectionSpansInfo() = 0; 193 virtual void SetSelection(int32_t selectionStart, int32_t selectionEnd) = 0; 194 }; 195 196 class ACE_EXPORT RichEditorModel { 197 public: 198 static RichEditorModel* GetInstance(); 199 virtual ~RichEditorModel() = default; 200 virtual void Create() = 0; 201 virtual RefPtr<RichEditorControllerBase> GetRichEditorController() = 0; 202 virtual void SetOnReady(std::function<void()>&& func) = 0; 203 virtual void SetOnSelect(std::function<void(const BaseEventInfo*)>&& func) = 0; 204 virtual void SetOnSelectionChange(std::function<void(const BaseEventInfo*)>&& func) = 0; 205 virtual void SetAboutToIMEInput(std::function<bool(const NG::RichEditorInsertValue&)>&& func) = 0; 206 virtual void SetOnIMEInputComplete(std::function<void(const NG::RichEditorAbstractSpanResult&)>&& func) = 0; 207 virtual void SetAboutToDelete(std::function<bool(const NG::RichEditorDeleteValue&)>&& func) = 0; 208 virtual void SetOnDeleteComplete(std::function<void()>&& func) = 0; 209 virtual void SetCustomKeyboard(std::function<void()>&& func) = 0; 210 virtual void SetCopyOption(CopyOptions& copyOptions) = 0; 211 virtual void BindSelectionMenu(NG::TextSpanType& editorType, NG::TextResponseType& responseType, 212 std::function<void()>& buildFunc, NG::SelectMenuParam& menuParam) = 0; 213 virtual void SetOnPaste(std::function<void(NG::TextCommonEvent&)>&& func) = 0; 214 virtual void SetTextDetectEnable(bool value) = 0; 215 virtual void SetTextDetectConfig(const std::string& value, std::function<void(const std::string&)>&& onResult) = 0; 216 private: 217 static std::unique_ptr<RichEditorModel> instance_; 218 static std::mutex mutex_; 219 }; 220 } // namespace OHOS::Ace 221 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_MODEL_H 222