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("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 WeakPtr<NG::FrameNode> targetNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
82 auto onStartEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
83 const BaseEventInfo* info) {
84 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
85 PipelineContext::SetCallBackNode(node);
86 func->Execute();
87 };
88
89 RichTextModel::GetInstance()->SetOnPageStart(onStartEvent);
90 }
91 }
92
OnComplete(const JSCallbackInfo & info)93 void JSRichText::OnComplete(const JSCallbackInfo& info)
94 {
95 if (info.Length() > 0 && info[0]->IsFunction()) {
96 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
97 WeakPtr<NG::FrameNode> targetNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
98 auto onCompleteEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
99 const BaseEventInfo* info) {
100 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
101 PipelineContext::SetCallBackNode(node);
102 func->Execute();
103 };
104
105 RichTextModel::GetInstance()->SetOnPageFinish(onCompleteEvent);
106 }
107 }
108 } // namespace OHOS::Ace::Framework
109