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