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/base/view_stack_processor.h"
24 #include "core/components_ng/pattern/web/richtext_model_ng.h"
25
26 namespace OHOS::Ace {
27 std::unique_ptr<RichTextModel> RichTextModel::instance_ = nullptr;
28 std::mutex RichTextModel::mutex_;
GetInstance()29 RichTextModel* RichTextModel::GetInstance()
30 {
31 if (!instance_) {
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (!instance_) {
34 #ifdef NG_BUILD
35 instance_.reset(new NG::RichTextModelNG());
36 #else
37 if (Container::IsCurrentUseNewPipeline()) {
38 instance_.reset(new NG::RichTextModelNG());
39 } else {
40 instance_.reset(new Framework::RichTextModelImpl());
41 }
42 #endif
43 }
44 }
45 return instance_.get();
46 }
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)50 void JSRichText::Create(const JSCallbackInfo& info)
51 {
52 if (info.Length() < 1) {
53 return;
54 }
55
56 std::string data;
57
58 if (ParseJsString(info[0], data)) {
59 RichTextModel::GetInstance()->Create(data);
60 } else {
61 TAG_LOGW(AceLogTag::ACE_RICH_TEXT, "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("onAttach", &JSInteractableView::JsOnAttach);
72 JSClass<JSRichText>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
73 JSClass<JSRichText>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
74 JSClass<JSRichText>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
75 JSClass<JSRichText>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
76 JSClass<JSRichText>::InheritAndBind<JSViewAbstract>(globalObj);
77 }
78
OnStart(const JSCallbackInfo & info)79 void JSRichText::OnStart(const JSCallbackInfo& info)
80 {
81 if (info.Length() > 0 && info[0]->IsFunction()) {
82 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
83 auto targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
84 auto onStartEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
85 const BaseEventInfo* info) {
86 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
87 PipelineContext::SetCallBackNode(node);
88 func->Execute();
89 };
90
91 RichTextModel::GetInstance()->SetOnPageStart(onStartEvent);
92 }
93 }
94
OnComplete(const JSCallbackInfo & info)95 void JSRichText::OnComplete(const JSCallbackInfo& info)
96 {
97 if (info.Length() > 0 && info[0]->IsFunction()) {
98 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
99 auto targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
100 auto onCompleteEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
101 const BaseEventInfo* info) {
102 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
103 PipelineContext::SetCallBackNode(node);
104 func->Execute();
105 };
106
107 RichTextModel::GetInstance()->SetOnPageFinish(onCompleteEvent);
108 }
109 }
110 } // namespace OHOS::Ace::Framework
111