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 "bridge/declarative_frontend/engine/functions/js_function.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20 #include "bridge/declarative_frontend/view_stack_processor.h"
21 #include "core/components/web/web_component.h"
22
23 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)24 void JSRichText::Create(const JSCallbackInfo& info)
25 {
26 if (info.Length() < 1) {
27 LOGI("richtext create error, info is non-valid");
28 return;
29 }
30
31 std::string data = "";
32 RefPtr<WebComponent> webComponent;
33 if (ParseJsString(info[0], data)) {
34 webComponent = AceType::MakeRefPtr<OHOS::Ace::WebComponent>("");
35 webComponent->SetData(data);
36 } else {
37 LOGE("richtext component failed to parse data");
38 return;
39 }
40 ViewStackProcessor::GetInstance()->Push(webComponent);
41 }
42
JSBind(BindingTarget globalObj)43 void JSRichText::JSBind(BindingTarget globalObj)
44 {
45 JSClass<JSRichText>::Declare("RichText");
46 JSClass<JSRichText>::StaticMethod("create", &JSRichText::Create);
47 JSClass<JSRichText>::StaticMethod("onStart", &JSRichText::OnStart);
48 JSClass<JSRichText>::StaticMethod("onComplete", &JSRichText::OnComplete);
49 JSClass<JSRichText>::Inherit<JSViewAbstract>();
50 JSClass<JSRichText>::Bind<>(globalObj);
51 }
52
OnStart(const JSCallbackInfo & info)53 void JSRichText::OnStart(const JSCallbackInfo& info)
54 {
55 if (info.Length() > 0 && info[0]->IsFunction()) {
56 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
57 auto eventMarker = EventMarker([execCtx = info.GetExecutionContext(),
58 func = std::move(jsFunc)](const BaseEventInfo* info) {
59 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
60 func->Execute();
61 });
62 auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
63 webComponent->SetPageStartedEventId(eventMarker);
64 }
65 }
66
OnComplete(const JSCallbackInfo & info)67 void JSRichText::OnComplete(const JSCallbackInfo& info)
68 {
69 if (info.Length() > 0 && info[0]->IsFunction()) {
70 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
71 auto eventMarker = EventMarker([execCtx = info.GetExecutionContext(),
72 func = std::move(jsFunc)](const BaseEventInfo* info) {
73 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
74 func->Execute();
75 });
76 auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
77 webComponent->SetPageFinishedEventId(eventMarker);
78 }
79 }
80 } // namespace OHOS::Ace::Framework
81