• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SetStopBackPress(const JSCallbackInfo& info);
94     static void SetKeyboardAppearance(const JSCallbackInfo& info);
95 
96 private:
97     static void CreateTextStyleObj(JSRef<JSObject>& textStyleObj, const NG::RichEditorAbstractSpanResult& spanResult);
98     static void CreateImageStyleObj(JSRef<JSObject>& imageStyleObj, JSRef<JSObject>& spanResultObj,
99         const NG::RichEditorAbstractSpanResult& spanResult);
100     static JSRef<JSArray> CreateJSDeleteSpans(const NG::RichEditorDeleteValue& deleteValue);
101     static void ParseUserGesture(
102         const JSCallbackInfo& args, UserGestureOptions& gestureOption, const std::string& spanType);
103     static void ParseJsFont(const JSRef<JSObject>& fontObject, Font& font);
104     static void SetTextChangeSpanResult(JSRef<JSObject>& resultObj,
105         const NG::RichEditorAbstractSpanResult& spanResult);
106     static void SetImageChangeSpanResult(JSRef<JSObject>& resultObj,
107         const NG::RichEditorAbstractSpanResult& spanResult);
108     static void SetSymbolChangeSpanResult(JSRef<JSObject>& resultObj,
109         const NG::RichEditorAbstractSpanResult& spanResult);
110     static void SetJSSpanResultObject(JSRef<JSObject>& resultObj, const ResultObject& resultObject);
111     static void SetJSDeleteSpan(JSRef<JSObject>& spanResultObj, const NG::RichEditorAbstractSpanResult& it);
112     static void SetJSUrlStyle(const std::u16string& urlAddress, JSRef<JSObject>& resultObj);
113 };
114 
115 class JSRichEditorBaseController : public Referenced {
116 public:
SetController(const RefPtr<RichEditorBaseControllerBase> & controller)117     virtual void SetController(const RefPtr<RichEditorBaseControllerBase>& controller)
118     {
119         controllerWeak_ = controller;
120     }
121 
122     void SetCaretOffset(const JSCallbackInfo& args);
123     void GetCaretOffset(const JSCallbackInfo& args);
124     void GetCaretRect(const JSCallbackInfo& args);
125     void SetTypingStyle(const JSCallbackInfo& info);
126     void ParseJsTextStyle(
127         const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle);
128     void ParseJsLineHeightLetterSpacingTextStyle(const JSRef<JSObject>& styleObject, TextStyle& style,
129         struct UpdateSpanStyle& updateSpanStyle, bool isSupportPercent = true);
130     void ParseJsFontFeatureTextStyle(const JSRef<JSObject>& styleObject, TextStyle& style,
131         struct UpdateSpanStyle& updateSpanStyle);
132     void ParseJsHalfLeadingTextStyle(const JSRef<JSObject>& styleObject, TextStyle& style,
133         struct UpdateSpanStyle& updateSpanStyle);
134     void GetTypingStyle(const JSCallbackInfo& info);
135     void CloseSelectionMenu();
136     void IsEditing(const JSCallbackInfo& args);
137     void StopEditing();
138     void SetSelection(const JSCallbackInfo& args);
139     bool FontSizeRangeIsNegative(const CalcDimension& size);
140     void GetLayoutManager(const JSCallbackInfo& args);
141     void GetPreviewTextInfo(const JSCallbackInfo& args);
142 
SetInstanceId(int32_t id)143     void SetInstanceId(int32_t id)
144     {
145         instanceId_ = id;
146     }
147 
SetStyledStringMode(bool isStyledStringMode)148     void SetStyledStringMode(bool isStyledStringMode)
149     {
150         isStyledStringMode_ = isStyledStringMode;
151     }
152 
IsStyledStringMode()153     bool IsStyledStringMode()
154     {
155         return isStyledStringMode_;
156     }
157 
GetColorMode()158     ColorMode GetColorMode()
159     {
160         auto controller = controllerWeak_.Upgrade();
161         CHECK_NULL_RETURN(controller, ColorMode::COLOR_MODE_UNDEFINED);
162         return controller->GetColorMode();
163     }
164 
165 protected:
166     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
167     WeakPtr<RichEditorBaseControllerBase> controllerWeak_;
168     struct UpdateSpanStyle typingStyle_;
169     bool isStyledStringMode_ = false;
170     static JSRef<JSObject> CreateTypingStyleResult(const struct UpdateSpanStyle& typingStyle);
171     static JSRef<JSObject> CreateJsDecorationObj(const struct UpdateSpanStyle& typingStyle);
172     void ParseTextDecoration(
173         const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle);
174     void ParseTextShadow(
175         const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle);
176     void ParseTextBackgroundStyle(
177         const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle);
178     void ParseTextUrlStyle(const JSRef<JSObject>& styleObject, std::optional<std::u16string>& urlAddressOpt);
179     JSRef<JSObject> JSObjectCast(JSRef<JSVal> jsValue);
180     void ParseJsSelectionOptions(const JSCallbackInfo& args, std::optional<SelectionOptions>& options);
181     JSRef<JSObject> CreateJSPreviewTextInfo(const PreviewTextInfo& info);
182 };
183 
184 class JSRichEditorController final : public JSRichEditorBaseController {
185 public:
186     JSRichEditorController() = default;
187     ~JSRichEditorController() override = default;
188 
189     static void JSBind(BindingTarget globalObj);
190 
Constructor(const JSCallbackInfo & args)191     static void Constructor(const JSCallbackInfo& args)
192     {
193         auto controller = Referenced::MakeRefPtr<JSRichEditorController>();
194         controller->SetStyledStringMode(false);
195         controller->IncRefCount();
196         args.SetReturnValue(Referenced::RawPtr(controller));
197     }
198 
Destructor(JSRichEditorController * controller)199     static void Destructor(JSRichEditorController* controller)
200     {
201         if (controller != nullptr) {
202             controller->DecRefCount();
203         }
204     }
205 
206     void AddImageSpan(const JSCallbackInfo& args);
207     void AddTextSpan(const JSCallbackInfo& args);
208     void AddSymbolSpan(const JSCallbackInfo& args);
209     void AddPlaceholderSpan(const JSCallbackInfo& args);
210     void ParseOptions(const JSCallbackInfo& args, SpanOptionBase& placeholderSpan);
211     void DeleteSpans(const JSCallbackInfo& args);
212     ImageSpanAttribute ParseJsImageSpanAttribute(JSRef<JSObject> imageAttribute);
213     void ParseJsCustomSymbolStyle(const JSRef<JSVal>& jsValue, TextStyle& style, uint32_t& symbolId);
214     void ParseJsSymbolSpanStyle(
215         const JSRef<JSObject>& styleObject, TextStyle& style, struct UpdateSpanStyle& updateSpanStyle);
216     ImageSpanOptions CreateJsImageOptions(const JSCallbackInfo& args);
217     void UpdateSpanStyle(const JSCallbackInfo& info);
218     void UpdateParagraphStyle(const JSCallbackInfo& info);
219     void GetSpansInfo(const JSCallbackInfo& args);
220     void GetParagraphsInfo(const JSCallbackInfo& args);
221     void GetSelection(const JSCallbackInfo& args);
222     void ToStyledString(const JSCallbackInfo& args);
223     void FromStyledString(const JSCallbackInfo& args);
224 
225 private:
226     static void ParseUserGesture(
227         const JSCallbackInfo& args, UserGestureOptions& gestureOption, const std::string& spanType);
228     static void ParseUserClickEvent(const JSCallbackInfo& args, const JSRef<JSObject>& gestureObj,
229         UserGestureOptions& gestureOption, const std::string& spanType);
230     static void ParseUserMouseOption(
231         const JSCallbackInfo& args, UserMouseOptions& mouseOption, const std::string& spanType);
232     bool ParseParagraphStyle(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style);
233     bool IsPixelMap(const JSRef<JSVal>& jsValue);
234     bool IsDrawable(const JSRef<JSVal>& jsValue);
235     static JSRef<JSVal> CreateJSSpansInfo(const SelectionInfo& info);
236     static JSRef<JSVal> CreateJSParagraphsInfo(const std::vector<ParagraphInfo>& info);
237     static void ParseWordBreakParagraphStyle(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style);
238     static void ParseTextAlignParagraphStyle(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style);
239     static void ParseParagraphSpacing(const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style);
240     bool CheckImageSource(std::string assetSrc);
241 
242     ACE_DISALLOW_COPY_AND_MOVE(JSRichEditorController);
243     struct UpdateSpanStyle updateSpanStyle_;
244     static void ParseLineBreakStrategyParagraphStyle(
245         const JSRef<JSObject>& styleObject, struct UpdateParagraphStyle& style);
246 };
247 
248 class JSRichEditorStyledStringController final : public JSRichEditorBaseController {
249 public:
250     JSRichEditorStyledStringController() = default;
251     ~JSRichEditorStyledStringController() override = default;
252 
253     static void JSBind(BindingTarget globalObj);
254 
Constructor(const JSCallbackInfo & args)255     static void Constructor(const JSCallbackInfo& args)
256     {
257         auto controller = Referenced::MakeRefPtr<JSRichEditorStyledStringController>();
258         controller->SetStyledStringMode(true);
259         controller->IncRefCount();
260         args.SetReturnValue(Referenced::RawPtr(controller));
261     }
262 
Destructor(JSRichEditorStyledStringController * controller)263     static void Destructor(JSRichEditorStyledStringController* controller)
264     {
265         if (controller != nullptr) {
266             controller->DecRefCount();
267         }
268     }
269 
270     void SetController(const RefPtr<RichEditorBaseControllerBase>& controller) override;
271 
272     void GetSelection(const JSCallbackInfo& args);
273     void SetStyledString(const JSCallbackInfo& args);
274     void GetStyledString(const JSCallbackInfo& args);
275     void OnContentChanged(const JSCallbackInfo& args);
276 
SetStyledStringCache(RefPtr<SpanString> styledString)277     void SetStyledStringCache(RefPtr<SpanString> styledString)
278     {
279         styledStringCache_ = styledString;
280     }
281 
GetStyledStringCache()282     RefPtr<SpanString> GetStyledStringCache() const
283     {
284         return styledStringCache_;
285     }
286 
287 private:
288     void SetOnWillChange(const JSCallbackInfo& args);
289     void SetOnDidChange(const JSCallbackInfo& args);
290     static JSRef<JSVal> CreateJsOnWillChange(const NG::StyledStringChangeValue& changeValue);
291     ACE_DISALLOW_COPY_AND_MOVE(JSRichEditorStyledStringController);
292     RefPtr<SpanString> styledStringCache_;
293 };
294 } // namespace OHOS::Ace::Framework
295 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_RICHEDITOR_H
296