/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "frameworks/bridge/declarative_frontend/jsview/js_span.h" #include #include #include #include #include "base/geometry/dimension.h" #include "base/log/ace_scoring_log.h" #include "base/log/ace_trace.h" #include "base/utils/utils.h" #include "bridge/common/utils/utils.h" #include "bridge/declarative_frontend/engine/functions/js_click_function.h" #include "bridge/declarative_frontend/jsview/js_interactable_view.h" #include "bridge/declarative_frontend/jsview/js_view_abstract.h" #include "bridge/declarative_frontend/jsview/models/span_model_impl.h" #include "bridge/declarative_frontend/jsview/models/text_model_impl.h" #ifndef NG_BUILD #include "bridge/declarative_frontend/view_stack_processor.h" #endif #include "core/common/container.h" #include "core/components_ng/pattern/text/span_model.h" #include "core/components_ng/pattern/text/span_model_ng.h" #include "core/components_ng/pattern/text/text_model.h" namespace OHOS::Ace { std::unique_ptr SpanModel::instance_ = nullptr; SpanModel* SpanModel::GetInstance() { if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::SpanModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::SpanModelNG()); } else { instance_.reset(new Framework::SpanModelImpl()); } #endif } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { namespace { const std::vector FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC }; const std::vector TEXT_CASES = { TextCase::NORMAL, TextCase::LOWERCASE, TextCase::UPPERCASE }; } // namespace void JSSpan::SetFontSize(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The argv is wrong, it is supposed to have at least 1 argument"); return; } Dimension fontSize; if (!ParseJsDimensionFp(info[0], fontSize)) { return; } SpanModel::GetInstance()->SetFontSize(fontSize); } void JSSpan::SetFontWeight(const std::string& value) { SpanModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(value)); } void JSSpan::SetTextColor(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The argv is wrong, it is supposed to have at least 1 argument"); return; } Color textColor; if (!ParseJsColor(info[0], textColor)) { return; } SpanModel::GetInstance()->SetTextColor(textColor); } void JSSpan::SetFontStyle(int32_t value) { if (value >= 0 && value < static_cast(FONT_STYLES.size())) { auto style = FONT_STYLES[value]; SpanModel::GetInstance()->SetItalicFontStyle(style); } else { LOGE("Text fontStyle(%{public}d) illegal value", value); } } void JSSpan::SetFontFamily(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The argv is wrong, it is supposed to have at least 1 argument"); return; } std::vector fontFamilies; if (!ParseJsFontFamilies(info[0], fontFamilies)) { LOGE("Parse FontFamilies failed"); return; } SpanModel::GetInstance()->SetFontFamily(fontFamilies); } void JSSpan::SetLetterSpacing(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The argv is wrong, it is supposed to have at least 1 argument"); return; } Dimension value; if (!ParseJsDimensionFp(info[0], value)) { return; } SpanModel::GetInstance()->SetLetterSpacing(value); } void JSSpan::SetTextCase(int32_t value) { if (value >= 0 && value < static_cast(TEXT_CASES.size())) { auto textCase = TEXT_CASES[value]; SpanModel::GetInstance()->SetTextCase(textCase); } else { LOGE("Text textCase(%{public}d) illegal value", value); } } void JSSpan::SetDecoration(const JSCallbackInfo& info) { if (!info[0]->IsObject()) { LOGE("info[0] not is Object"); return; } JSRef obj = JSRef::Cast(info[0]); JSRef typeValue = obj->GetProperty("type"); JSRef colorValue = obj->GetProperty("color"); std::optional textDecoration; if (typeValue->IsNumber()) { textDecoration = static_cast(typeValue->ToNumber()); } std::optional colorVal; Color result; if (ParseJsColor(colorValue, result)) { colorVal = result; } else { // default color colorVal = Color::BLACK; } if (textDecoration) { SpanModel::GetInstance()->SetTextDecoration(textDecoration.value()); } if (colorVal) { SpanModel::GetInstance()->SetTextDecorationColor(colorVal.value()); } } void JSSpan::JsOnClick(const JSCallbackInfo& info) { if (Container::IsCurrentUseNewPipeline()) { if (!info[0]->IsFunction()) { LOGW("the info is not click function"); return; } auto jsOnClickFunc = AceType::MakeRefPtr(JSRef::Cast(info[0])); auto onClick = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc](const BaseEventInfo* info) { const auto* clickInfo = TypeInfoHelper::DynamicCast(info); JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); ACE_SCORING_EVENT("onClick"); func->Execute(*clickInfo); }; SpanModel::GetInstance()->SetOnClick(std::move(onClick)); return; } #ifndef NG_BUILD if (info[0]->IsFunction()) { auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent(); CHECK_NULL_VOID(inspector); auto impl = inspector->GetInspectorFunctionImpl(); RefPtr jsOnClickFunc = AceType::MakeRefPtr(JSRef::Cast(info[0])); auto clickFunc = [execCtx = info.GetExecutionContext(), func = std::move(jsOnClickFunc), impl]( const BaseEventInfo* info) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); LOGD("About to call onclick method on js"); const auto* clickInfo = TypeInfoHelper::DynamicCast(info); auto newInfo = *clickInfo; if (impl) { impl->UpdateEventInfo(newInfo); } ACE_SCORING_EVENT("Span.onClick"); func->Execute(newInfo); }; SpanModel::GetInstance()->SetOnClick(std::move(clickFunc)); } #endif } void JSSpan::JsRemoteMessage(const JSCallbackInfo& info) { #ifndef NG_BUILD RemoteCallback remoteCallback; JSInteractableView::JsRemoteMessage(info, remoteCallback); EventMarker remoteMessageEventId(std::move(remoteCallback)); auto* stack = ViewStackProcessor::GetInstance(); auto textSpanComponent = AceType::DynamicCast(stack->GetMainComponent()); textSpanComponent->SetRemoteMessageEventId(remoteMessageEventId); #endif } void JSSpan::JSBind(BindingTarget globalObj) { JSClass::Declare("Span"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSSpan::Create, opt); JSClass::StaticMethod("fontColor", &JSSpan::SetTextColor, opt); JSClass::StaticMethod("fontSize", &JSSpan::SetFontSize, opt); JSClass::StaticMethod("fontWeight", &JSSpan::SetFontWeight, opt); JSClass::StaticMethod("fontStyle", &JSSpan::SetFontStyle, opt); JSClass::StaticMethod("fontFamily", &JSSpan::SetFontFamily, opt); JSClass::StaticMethod("letterSpacing", &JSSpan::SetLetterSpacing, opt); JSClass::StaticMethod("textCase", &JSSpan::SetTextCase, opt); JSClass::StaticMethod("decoration", &JSSpan::SetDecoration); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey); JSClass::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete); JSClass::StaticMethod("remoteMessage", &JSSpan::JsRemoteMessage); JSClass::StaticMethod("onClick", &JSSpan::JsOnClick); JSClass::Inherit(); JSClass::Inherit(); JSClass::Bind<>(globalObj); } void JSSpan::Create(const JSCallbackInfo& info) { std::string label; if (info.Length() > 0) { ParseJsString(info[0], label); } SpanModel::GetInstance()->Create(label); } } // namespace OHOS::Ace::Framework