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 22 #include "base/image/pixel_map.h" 23 #include "base/memory/ace_type.h" 24 #include "core/components/common/layout/constants.h" 25 #include "core/components/common/properties/text_style.h" 26 #include "core/components_ng/base/view_abstract_model.h" 27 #include "core/components_ng/pattern/rich_editor/rich_editor_event_hub.h" 28 #include "core/components_ng/pattern/rich_editor/rich_editor_selection.h" 29 30 namespace OHOS::Ace { 31 struct ImageSpanSize { 32 CalcDimension width; 33 CalcDimension height; 34 }; 35 36 struct ImageSpanAttribute { 37 std::optional<ImageSpanSize> size; 38 std::optional<VerticalAlign> verticalAlign; 39 std::optional<ImageFit> objectFit; 40 }; 41 struct ImageSpanOptions { 42 std::optional<int32_t> offset; 43 std::optional<std::string> image; 44 std::optional<std::string> bundleName; 45 std::optional<std::string> moduleName; 46 std::optional<RefPtr<PixelMap>> imagePixelMap; 47 std::optional<ImageSpanAttribute> imageAttribute; 48 }; 49 50 struct SpanPositionInfo { SpanPositionInfoSpanPositionInfo51 SpanPositionInfo(int32_t index, int32_t start, int32_t end, int32_t offset) 52 : spanIndex_(index), spanStart_(start), spanEnd_(end), spanOffset_(offset) 53 {} 54 SpanPositionInfoSpanPositionInfo55 SpanPositionInfo() 56 { 57 spanIndex_ = 0; 58 spanStart_ = 0; 59 spanEnd_ = 0; 60 spanOffset_ = 0; 61 } 62 63 int32_t spanIndex_ = 0; 64 int32_t spanStart_ = 0; 65 int32_t spanEnd_ = 0; 66 int32_t spanOffset_ = 0; 67 }; 68 69 struct TextSpanOptions { 70 std::optional<int32_t> offset; 71 std::string value; 72 std::optional<TextStyle> style; 73 }; 74 75 struct UpdateSpanStyle { ResetStyleUpdateSpanStyle76 void ResetStyle() 77 { 78 updateTextColor.reset(); 79 updateFontSize.reset(); 80 updateItalicFontStyle.reset(); 81 updateFontWeight.reset(); 82 updateFontFamily.reset(); 83 updateTextDecoration.reset(); 84 updateTextDecorationColor.reset(); 85 86 updateImageWidth.reset(); 87 updateImageHeight.reset(); 88 updateImageVerticalAlign.reset(); 89 updateImageFit.reset(); 90 } 91 92 std::optional<Color> updateTextColor = std::nullopt; 93 std::optional<CalcDimension> updateFontSize = std::nullopt; 94 std::optional<FontStyle> updateItalicFontStyle = std::nullopt; 95 std::optional<FontWeight> updateFontWeight = std::nullopt; 96 std::optional<std::vector<std::string>> updateFontFamily = std::nullopt; 97 std::optional<TextDecoration> updateTextDecoration = std::nullopt; 98 std::optional<Color> updateTextDecorationColor = std::nullopt; 99 100 std::optional<CalcDimension> updateImageWidth = std::nullopt; 101 std::optional<CalcDimension> updateImageHeight = std::nullopt; 102 std::optional<VerticalAlign> updateImageVerticalAlign = std::nullopt; 103 std::optional<ImageFit> updateImageFit = std::nullopt; 104 }; 105 106 struct RangeOptions { 107 std::optional<int32_t> start; 108 std::optional<int32_t> end; 109 }; 110 111 struct SelectMenuParam { 112 std::function<void(int32_t, int32_t)> onAppear; 113 std::function<void()> onDisappear; 114 }; 115 116 class ACE_EXPORT RichEditorControllerBase : public AceType { 117 DECLARE_ACE_TYPE(RichEditorControllerBase, AceType); 118 119 public: 120 virtual int32_t AddImageSpan(const ImageSpanOptions& options) = 0; 121 virtual int32_t AddTextSpan(const TextSpanOptions& options) = 0; 122 virtual int32_t GetCaretOffset() = 0; 123 virtual bool SetCaretOffset(int32_t caretPosition) = 0; 124 virtual void UpdateSpanStyle(int32_t start, int32_t end, TextStyle textStyle, ImageSpanAttribute imageStyle) = 0; 125 virtual void SetTypingStyle(struct UpdateSpanStyle& typingStyle, TextStyle textStyle) = 0; 126 virtual void SetUpdateSpanStyle(struct UpdateSpanStyle updateSpanStyle) = 0; 127 virtual RichEditorSelection GetSpansInfo(int32_t start, int32_t end) = 0; 128 virtual void DeleteSpans(const RangeOptions& options) = 0; 129 virtual void CloseSelectionMenu() = 0; 130 }; 131 132 class ACE_EXPORT RichEditorModel { 133 public: 134 static RichEditorModel* GetInstance(); 135 virtual ~RichEditorModel() = default; 136 virtual void Create() = 0; 137 virtual RefPtr<RichEditorControllerBase> GetRichEditorController() = 0; 138 virtual void SetOnReady(std::function<void()>&& func) = 0; 139 virtual void SetOnSelect(std::function<void(const BaseEventInfo*)>&& func) = 0; 140 virtual void SetAboutToIMEInput(std::function<bool(const NG::RichEditorInsertValue&)>&& func) = 0; 141 virtual void SetOnIMEInputComplete(std::function<void(const NG::RichEditorAbstractSpanResult&)>&& func) = 0; 142 virtual void SetAboutToDelete(std::function<bool(const NG::RichEditorDeleteValue&)>&& func) = 0; 143 virtual void SetOnDeleteComplete(std::function<void()>&& func) = 0; 144 virtual void SetCustomKeyboard(std::function<void()>&& func) = 0; 145 virtual void SetCopyOption(CopyOptions& copyOptions) = 0; 146 virtual void BindSelectionMenu(RichEditorType& editorType, ResponseType& responseType, 147 std::function<void()>& buildFunc, SelectMenuParam& menuParam) = 0; 148 149 private: 150 static std::unique_ptr<RichEditorModel> instance_; 151 static std::mutex mutex_; 152 }; 153 } // namespace OHOS::Ace 154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_MODEL_H 155