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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RICHEDITOR_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RICHEDITOR_H 18 19 #include "core/components_ng/pattern/rich_editor/rich_editor_event_hub.h" 20 #include "core/components_ng/pattern/rich_editor/rich_editor_model.h" 21 #include "core/components_ng/pattern/text_field/text_field_model.h" 22 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h" 23 24 namespace OHOS::Ace::Framework { 25 class JSRichEditor : public JSContainerBase { 26 public: 27 static void Create(const JSCallbackInfo& info); 28 static void JSBind(BindingTarget globalObj); 29 static void SetOnReady(const JSCallbackInfo& args); 30 static void SetOnSelect(const JSCallbackInfo& args); 31 static void SetOnSelectionChange(const JSCallbackInfo& args); 32 static void SetAboutToIMEInput(const JSCallbackInfo& args); 33 static void SetOnIMEInputComplete(const JSCallbackInfo& args); 34 static void SetOnDidIMEInput(const JSCallbackInfo& args); 35 static void SetAboutToDelete(const JSCallbackInfo& args); 36 static void SetOnDeleteComplete(const JSCallbackInfo& args); 37 static void SetCustomKeyboard(const JSCallbackInfo& args); 38 static JSRef<JSVal> CreateJsAboutToIMEInputObj(const NG::RichEditorInsertValue& insertValue); 39 static JSRef<JSVal> CreateJsOnIMEInputComplete(const NG::RichEditorAbstractSpanResult& textSpanResult); 40 static JSRef<JSVal> CreateJsOnDidIMEInput(const TextRange& textRange); 41 static JSRef<JSVal> CreateJsAboutToDelet(const NG::RichEditorDeleteValue& deleteValue); 42 static void SetChangeTextSpans( 43 JSRef<JSArray>& jsArray, const std::vector<NG::RichEditorAbstractSpanResult>& spanList); 44 static JSRef<JSVal> CreateJsOnWillChange(const NG::RichEditorChangeValue& changeValue); 45 static JSRef<JSVal> CreateJsOnDidChange(const std::vector<NG::RichEditorAbstractSpanResult>& spanList); 46 static void JsClip(const JSCallbackInfo& info); 47 static void JsFocusable(const JSCallbackInfo& info); 48 static void SetCopyOptions(const JSCallbackInfo& info); 49 static void BindSelectionMenu(const JSCallbackInfo& info); 50 static void SetOnPaste(const JSCallbackInfo& info); 51 static void SetPlaceholder(const JSCallbackInfo& info); 52 static void JsEnableDataDetector(const JSCallbackInfo& info); 53 static void JsEnablePreviewText(const JSCallbackInfo& info); 54 static void JsDataDetectorConfig(const JSCallbackInfo& info); 55 static void SetOnWillChange(const JSCallbackInfo& info); 56 static void SetOnDidChange(const JSCallbackInfo& info); 57 static void SetOnCut(const JSCallbackInfo& info); 58 static void SetOnCopy(const JSCallbackInfo& info); 59 static JSRef<JSObject> CreateJSSpanResultObject(const ResultObject& resultObject); 60 static JSRef<JSVal> CreateJSSelection(const SelectionInfo& selectInfo); 61 static JSRef<JSVal> CreateJSSelectionRange(const SelectionRangeInfo& selectRange); 62 static JSRef<JSObject> CreateJSTextStyleResult(const TextStyleResult& textStyleResult); 63 static JSRef<JSObject> CreateJSParagraphStyle(const TextStyleResult& textStyleResult); 64 static JSRef<JSObject> CreateJSSymbolSpanStyleResult(const SymbolSpanStyle& symbolSpanStyle); 65 static JSRef<JSObject> CreateJSValueResource(const RefPtr<ResourceObject>& valueResource); 66 static JSRef<JSObject> CreateJSLayoutStyle(const ImageStyleResult& imageStyleResult); 67 static JSRef<JSObject> CreateJSImageStyleResult(const ImageStyleResult& imageStyleResult); 68 static JSRef<JSObject> CreateParagraphStyleResult(const ParagraphInfo& info); 69 static JSRef<JSVal> CreateJSTextCommonEvent(NG::TextCommonEvent& event); 70 static void SetCaretColor(const JSCallbackInfo& info); 71 static void SetOnEditingChange(const JSCallbackInfo& args); 72 static void SetSelectedBackgroundColor(const JSCallbackInfo& info); 73 static void SetEnterKeyType(const JSCallbackInfo& info); 74 static void CreateJsRichEditorCommonEvent(const JSCallbackInfo& info); 75 static void SetOnSubmit(const JSCallbackInfo& info); 76 static Local<JSValueRef> JsKeepEditableState(panda::JsiRuntimeCallInfo* info); 77 static std::optional<NG::BorderRadiusProperty> ParseBorderRadiusAttr(JsiRef<JSVal> args); 78 static std::optional<NG::MarginProperty> ParseMarginAttr(JsiRef<JSVal> marginAttr); 79 static CalcDimension ParseLengthMetrics(const JSRef<JSObject>& obj); 80 static void EditMenuOptions(const JSCallbackInfo& info); 81 static void SetEnableKeyboardOnFocus(const JSCallbackInfo& info); 82 static void SetEnableHapticFeedback(const JSCallbackInfo& info); 83 static JSRef<JSArray> CreateJsTextShadowObjectArray(const TextStyleResult& textStyleResult); 84 static JSRef<JSArray> CreateJsTextShadowObjectArray(const std::vector<Shadow>& textShadows); 85 static void SetBarState(const JSCallbackInfo& info); 86 static void SetKeyboardAppearance(const JSCallbackInfo& info); 87 88 private: 89 static void CreateTextStyleObj(JSRef<JSObject>& textStyleObj, const NG::RichEditorAbstractSpanResult& spanResult); 90 static void CreateImageStyleObj(JSRef<JSObject>& imageStyleObj, JSRef<JSObject>& spanResultObj, 91 const NG::RichEditorAbstractSpanResult& spanResult); 92 static JSRef<JSArray> CreateJSDeleteSpans(const NG::RichEditorDeleteValue& deleteValue); 93 static void ParseUserGesture( 94 const JSCallbackInfo& args, UserGestureOptions& gestureOption, const std::string& spanType); 95 static void ParseJsFont(const JSRef<JSObject>& fontObject, Font& font); 96 static void SetTextChangeSpanResult(JSRef<JSObject>& resultObj, 97 const NG::RichEditorAbstractSpanResult& spanResult); 98 static void SetImageChangeSpanResult(JSRef<JSObject>& resultObj, 99 const NG::RichEditorAbstractSpanResult& spanResult); 100 static void SetSymbolChangeSpanResult(JSRef<JSObject>& resultObj, 101 const NG::RichEditorAbstractSpanResult& spanResult); 102 static void SetJSSpanResultObject(JSRef<JSObject>& resultObj, const ResultObject& resultObject); 103 static void SetJSDeleteSpan(JSRef<JSObject>& spanResultObj, const NG::RichEditorAbstractSpanResult& it); 104 }; 105 106 class JSRichEditorBaseController : public Referenced { 107 public: SetController(const RefPtr<RichEditorBaseControllerBase> & controller)108 void SetController(const RefPtr<RichEditorBaseControllerBase>& controller) 109 { 110 controllerWeak_ = controller; 111 } 112 113 void SetCaretOffset(const JSCallbackInfo& args); 114 void GetCaretOffset(const JSCallbackInfo& args); 115 void SetTypingStyle(const JSCallbackInfo& info); 116 void ParseJsTextStyle( 117 const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle); 118 void ParseJsLineHeightLetterSpacingTextStyle(const JSRef<JSObject>& styleObject, TextStyle& style, 119 struct UpdateSpanStyle& updateSpanStyle, bool isSupportPercent = true); 120 void ParseJsFontFeatureTextStyle(const JSRef<JSObject>& styleObject, TextStyle& style, 121 struct UpdateSpanStyle& updateSpanStyle); 122 void GetTypingStyle(const JSCallbackInfo& info); 123 void CloseSelectionMenu(); 124 void IsEditing(const JSCallbackInfo& args); 125 void StopEditing(); 126 void SetSelection(const JSCallbackInfo& args); 127 bool FontSizeRangeIsNegative(const CalcDimension& size); 128 void GetLayoutManager(const JSCallbackInfo& args); 129 void GetPreviewTextInfo(const JSCallbackInfo& args); 130 SetInstanceId(int32_t id)131 void SetInstanceId(int32_t id) 132 { 133 instanceId_ = id; 134 } 135 SetStyledStringMode(bool isStyledStringMode)136 void SetStyledStringMode(bool isStyledStringMode) 137 { 138 isStyledStringMode_ = isStyledStringMode; 139 } 140 IsStyledStringMode()141 bool IsStyledStringMode() 142 { 143 return isStyledStringMode_; 144 } 145 146 protected: 147 int32_t instanceId_ = INSTANCE_ID_UNDEFINED; 148 WeakPtr<RichEditorBaseControllerBase> controllerWeak_; 149 struct UpdateSpanStyle typingStyle_; 150 bool isStyledStringMode_ = false; 151 static JSRef<JSObject> CreateTypingStyleResult(const struct UpdateSpanStyle& typingStyle); 152 void ParseTextDecoration( 153 const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle); 154 void ParseTextShadow( 155 const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle); 156 JSRef<JSObject> JSObjectCast(JSRef<JSVal> jsValue); 157 void ParseJsSelectionOptions(const JSCallbackInfo& args, std::optional<SelectionOptions>& options); 158 JSRef<JSObject> CreateJSPreviewTextInfo(const PreviewTextInfo& info); 159 }; 160 161 class JSRichEditorController final : public JSRichEditorBaseController { 162 public: 163 JSRichEditorController() = default; 164 ~JSRichEditorController() override = default; 165 166 static void JSBind(BindingTarget globalObj); 167 Constructor(const JSCallbackInfo & args)168 static void Constructor(const JSCallbackInfo& args) 169 { 170 auto controller = Referenced::MakeRefPtr<JSRichEditorController>(); 171 controller->SetStyledStringMode(false); 172 controller->IncRefCount(); 173 args.SetReturnValue(Referenced::RawPtr(controller)); 174 } 175 Destructor(JSRichEditorController * controller)176 static void Destructor(JSRichEditorController* controller) 177 { 178 if (controller != nullptr) { 179 controller->DecRefCount(); 180 } 181 } 182 183 void AddImageSpan(const JSCallbackInfo& args); 184 void AddTextSpan(const JSCallbackInfo& args); 185 void AddSymbolSpan(const JSCallbackInfo& args); 186 void AddPlaceholderSpan(const JSCallbackInfo& args); 187 void ParseOptions(const JSCallbackInfo& args, SpanOptionBase& placeholderSpan); 188 void DeleteSpans(const JSCallbackInfo& args); 189 ImageSpanAttribute ParseJsImageSpanAttribute(JSRef<JSObject> imageAttribute); 190 void ParseJsSymbolSpanStyle( 191 const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle); 192 ImageSpanOptions CreateJsImageOptions(const JSCallbackInfo& args); 193 void UpdateSpanStyle(const JSCallbackInfo& info); 194 void UpdateParagraphStyle(const JSCallbackInfo& info); 195 void GetSpansInfo(const JSCallbackInfo& args); 196 void GetParagraphsInfo(const JSCallbackInfo& args); 197 void GetSelection(const JSCallbackInfo& args); 198 void ToStyledString(const JSCallbackInfo& args); 199 void FromStyledString(const JSCallbackInfo& args); 200 201 private: 202 static void ParseUserGesture( 203 const JSCallbackInfo& args, UserGestureOptions& gestureOption, const std::string& spanType); 204 static void ParseUserClickEvent(const JSCallbackInfo& args, const JSRef<JSObject>& gestureObj, 205 UserGestureOptions& gestureOption, const std::string& spanType); 206 static void ParseUserMouseOption( 207 const JSCallbackInfo& args, UserMouseOptions& mouseOption, const std::string& spanType); 208 bool ParseParagraphStyle(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style); 209 bool IsPixelMap(const JSRef<JSVal>& jsValue); 210 bool IsDrawable(const JSRef<JSVal>& jsValue); 211 static JSRef<JSVal> CreateJSSpansInfo(const SelectionInfo& info); 212 static JSRef<JSVal> CreateJSParagraphsInfo(const std::vector<ParagraphInfo>& info); 213 static void ParseWordBreakParagraphStyle(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style); 214 static void ParseTextAlignParagraphStyle(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style); 215 bool CheckImageSource(std::string assetSrc); 216 217 ACE_DISALLOW_COPY_AND_MOVE(JSRichEditorController); 218 struct UpdateSpanStyle updateSpanStyle_; 219 static void ParseLineBreakStrategyParagraphStyle( 220 const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style); 221 }; 222 223 class JSRichEditorStyledStringController final : public JSRichEditorBaseController { 224 public: 225 JSRichEditorStyledStringController() = default; 226 ~JSRichEditorStyledStringController() override = default; 227 228 static void JSBind(BindingTarget globalObj); 229 Constructor(const JSCallbackInfo & args)230 static void Constructor(const JSCallbackInfo& args) 231 { 232 auto controller = Referenced::MakeRefPtr<JSRichEditorStyledStringController>(); 233 controller->SetStyledStringMode(true); 234 controller->IncRefCount(); 235 args.SetReturnValue(Referenced::RawPtr(controller)); 236 } 237 Destructor(JSRichEditorStyledStringController * controller)238 static void Destructor(JSRichEditorStyledStringController* controller) 239 { 240 if (controller != nullptr) { 241 controller->DecRefCount(); 242 } 243 } 244 245 void GetSelection(const JSCallbackInfo& args); 246 void SetStyledString(const JSCallbackInfo& args); 247 void GetStyledString(const JSCallbackInfo& args); 248 void OnContentChanged(const JSCallbackInfo& args); 249 250 private: 251 void SetOnWillChange(const JSCallbackInfo& args); 252 void SetOnDidChange(const JSCallbackInfo& args); 253 static JSRef<JSVal> CreateJsOnWillChange(const NG::StyledStringChangeValue& changeValue); 254 ACE_DISALLOW_COPY_AND_MOVE(JSRichEditorStyledStringController); 255 }; 256 } // namespace OHOS::Ace::Framework 257 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RICHEDITOR_H 258