1 /*
2 * Copyright (c) 2022 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 #include "bridge/declarative_frontend/jsview/js_richtext.h"
17
18 #include <string>
19
20 #include "bridge/declarative_frontend/engine/functions/js_function.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "bridge/declarative_frontend/jsview/models/richtext_model_impl.h"
23 #include "core/components_ng/pattern/web/richtext_model_ng.h"
24
25 namespace OHOS::Ace {
26 std::unique_ptr<RichTextModel> RichTextModel::instance_ = nullptr;
27 std::mutex RichTextModel::mutex_;
GetInstance()28 RichTextModel* RichTextModel::GetInstance()
29 {
30 if (!instance_) {
31 std::lock_guard<std::mutex> lock(mutex_);
32 if (!instance_) {
33 #ifdef NG_BUILD
34 instance_.reset(new NG::RichTextModelNG());
35 #else
36 if (Container::IsCurrentUseNewPipeline()) {
37 instance_.reset(new NG::RichTextModelNG());
38 } else {
39 instance_.reset(new Framework::RichTextModelImpl());
40 }
41 #endif
42 }
43 }
44 return instance_.get();
45 }
46 } // namespace OHOS::Ace
47
48 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)49 void JSRichText::Create(const JSCallbackInfo& info)
50 {
51 if (info.Length() < 1) {
52 LOGI("richtext create error, info is non-valid");
53 return;
54 }
55
56 std::string data;
57
58 if (ParseJsString(info[0], data)) {
59 RichTextModel::GetInstance()->Create(data);
60 } else {
61 LOGE("richtext component failed to parse data");
62 }
63 }
64
JSBind(BindingTarget globalObj)65 void JSRichText::JSBind(BindingTarget globalObj)
66 {
67 JSClass<JSRichText>::Declare("RichText");
68 JSClass<JSRichText>::StaticMethod("create", &JSRichText::Create);
69 JSClass<JSRichText>::StaticMethod("onStart", &JSRichText::OnStart);
70 JSClass<JSRichText>::StaticMethod("onComplete", &JSRichText::OnComplete);
71 JSClass<JSRichText>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
72 JSClass<JSRichText>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
73 JSClass<JSRichText>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
74 JSClass<JSRichText>::InheritAndBind<JSViewAbstract>(globalObj);
75 }
76
OnStart(const JSCallbackInfo & info)77 void JSRichText::OnStart(const JSCallbackInfo& info)
78 {
79 if (info.Length() > 0 && info[0]->IsFunction()) {
80 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
81
82 auto onStartEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
83 const BaseEventInfo* info) {
84 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
85 func->Execute();
86 };
87
88 RichTextModel::GetInstance()->SetOnPageStart(onStartEvent);
89 }
90 }
91
OnComplete(const JSCallbackInfo & info)92 void JSRichText::OnComplete(const JSCallbackInfo& info)
93 {
94 if (info.Length() > 0 && info[0]->IsFunction()) {
95 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
96
97 auto onCompleteEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
98 const BaseEventInfo* info) {
99 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
100 func->Execute();
101 };
102
103 RichTextModel::GetInstance()->SetOnPageFinish(onCompleteEvent);
104 }
105 }
106 } // namespace OHOS::Ace::Framework
107