1 /*
2 * Copyright (c) 2021-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 "core/components/rich_text/rich_text_component.h"
17
18 #include "base/geometry/offset.h"
19 #include "base/geometry/size.h"
20 #include "base/log/log.h"
21 #include "core/components/rich_text/render_rich_text.h"
22 #include "core/components/rich_text/resource/rich_text_delegate.h"
23 #include "core/components/rich_text/rich_text_element.h"
24
25 #include <iomanip>
26 #include <sstream>
27
28 namespace OHOS::Ace {
29
RichTextComponent(const std::string & type)30 RichTextComponent::RichTextComponent(const std::string& type) : type_(type)
31 {
32 ACE_DCHECK(!type_.empty());
33 if (!declaration_) {
34 declaration_ = AceType::MakeRefPtr<RichTextDeclaration>();
35 declaration_->Init();
36 }
37 }
38
CreateRenderNode()39 RefPtr<RenderNode> RichTextComponent::CreateRenderNode()
40 {
41 RefPtr<RenderNode> renderNode = RenderRichText::Create();
42 if (!delegate_) {
43 delegate_ = AceType::MakeRefPtr<RichTextDelegate>(AceType::WeakClaim<RichTextComponent>(this),
44 renderNode->GetContext(),
45 std::move(errorCallback_), type_);
46 }
47 auto renderRichText = AceType::DynamicCast<RenderRichText>(renderNode);
48
49 delegate_->AddWebLayoutChangeCallback(
50 [renderRichText, weak = WeakClaim(this)](int32_t width, int32_t height, int32_t contentHeight) {
51 if (!renderRichText) {
52 LOGE("renderRichText is null");
53 return;
54 }
55 auto pipelineContext = renderRichText->GetContext().Upgrade();
56 if (!pipelineContext) {
57 LOGE("fail to create Update due to context is null");
58 return;
59 }
60 auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(),
61 TaskExecutor::TaskType::UI);
62 auto weakRender = AceType::WeakClaim(AceType::RawPtr(renderRichText));
63 uiTaskExecutor.PostTask(
64 [weakRender, width, height, contentHeight] {
65 auto renderRichText = weakRender.Upgrade();
66 if (renderRichText) {
67 renderRichText->UpdateLayoutParams(width, height, contentHeight);
68 }
69 },
70 "ArkUIRichTextUpdateLayoutParams");
71 });
72
73 renderRichText->SetDelegate(delegate_);
74 return renderNode;
75 }
76
CreateElement()77 RefPtr<Element> RichTextComponent::CreateElement()
78 {
79 RefPtr<RichTextElement> elementNode = AceType::MakeRefPtr<RichTextElement>();
80 if (!delegate_) {
81 delegate_ = AceType::MakeRefPtr<RichTextDelegate>(AceType::WeakClaim<RichTextComponent>(this),
82 elementNode->GetContext(),
83 std::move(errorCallback_), type_);
84 }
85 elementNode->SetDelegate(delegate_);
86 return elementNode;
87 }
88 } // namespace OHOS::Ace
89