1 /*
2  * Copyright (c) 2023 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_ng/pattern/rich_editor/rich_editor_controller.h"
17 
18 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
19 namespace OHOS::Ace::NG {
AddImageSpan(const ImageSpanOptions & options)20 int32_t RichEditorController::AddImageSpan(const ImageSpanOptions& options)
21 {
22     auto richEditorPattern = pattern_.Upgrade();
23     CHECK_NULL_RETURN(richEditorPattern, 0);
24     return richEditorPattern->AddImageSpan(options);
25 }
26 
AddTextSpan(const TextSpanOptions & options)27 int32_t RichEditorController::AddTextSpan(const TextSpanOptions& options)
28 {
29     auto richEditorPattern = pattern_.Upgrade();
30     CHECK_NULL_RETURN(richEditorPattern, 0);
31     TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "textLength=%{public}d, offset=%{public}d",
32         static_cast<int32_t>(options.value.length()), options.offset.value_or(-1));
33     return richEditorPattern->AddTextSpan(options);
34 }
35 
AddSymbolSpan(const SymbolSpanOptions & options)36 int32_t RichEditorController::AddSymbolSpan(const SymbolSpanOptions& options)
37 {
38     auto richEditorPattern = pattern_.Upgrade();
39     CHECK_NULL_RETURN(richEditorPattern, 0);
40     return richEditorPattern->AddSymbolSpan(options);
41 }
42 
AddPlaceholderSpan(const RefPtr<UINode> & customNode,const SpanOptionBase & options)43 int32_t RichEditorController::AddPlaceholderSpan(const RefPtr<UINode>& customNode, const SpanOptionBase& options)
44 {
45     auto richEditorPattern = pattern_.Upgrade();
46     CHECK_NULL_RETURN(richEditorPattern, 0);
47     return richEditorPattern->AddPlaceholderSpan(customNode, options);
48 }
49 
UpdateSpanStyle(int32_t start,int32_t end,TextStyle textStyle,ImageSpanAttribute imageStyle)50 void RichEditorController::UpdateSpanStyle(
51     int32_t start, int32_t end, TextStyle textStyle, ImageSpanAttribute imageStyle)
52 {
53     auto richEditorPattern = pattern_.Upgrade();
54     CHECK_NULL_VOID(richEditorPattern);
55     auto length = richEditorPattern->GetTextContentLength();
56     start = std::max(0, start);
57     if (end < 0 || end > length) {
58         end = length;
59     }
60     if (start > end) {
61         std::swap(start, end);
62     }
63     if (start > length || end < 0 || start == end) {
64         return;
65     }
66     richEditorPattern->SetUpdateSpanStyle(updateSpanStyle_);
67     richEditorPattern->UpdateSpanStyle(start, end, textStyle, imageStyle);
68     richEditorPattern->ForceTriggerAvoidOnCaretChange();
69 }
70 
SetUpdateSpanStyle(struct UpdateSpanStyle updateSpanStyle)71 void RichEditorController::SetUpdateSpanStyle(struct UpdateSpanStyle updateSpanStyle)
72 {
73     updateSpanStyle_ = updateSpanStyle;
74 }
75 
GetSpansInfo(int32_t start,int32_t end)76 SelectionInfo RichEditorController::GetSpansInfo(int32_t start, int32_t end)
77 {
78     auto richEditorPattern = pattern_.Upgrade();
79     CHECK_NULL_RETURN(richEditorPattern, {});
80     ACE_SCOPED_TRACE("RichEditorControllerGetSpansInfo");
81     return richEditorPattern->GetSpansInfo(start, end, GetSpansMethod::GETSPANS);
82 }
83 
GetSelectionSpansInfo()84 SelectionInfo RichEditorController::GetSelectionSpansInfo()
85 {
86     auto richEditorPattern = pattern_.Upgrade();
87     CHECK_NULL_RETURN(richEditorPattern, {});
88 
89     auto start = std::max(richEditorPattern->GetTextSelector().GetTextStart(), 0);
90     auto end = std::max(richEditorPattern->GetTextSelector().GetTextEnd(), 0);
91     if (start == end) {
92         start = richEditorPattern->GetCaretPosition();
93         end = richEditorPattern->GetCaretPosition();
94     }
95     TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "GetSelection, start, range=[%{public}d,%{public}d]", start, end);
96     SelectionInfo value = richEditorPattern->GetSpansInfo(start, end, GetSpansMethod::GETSPANS);
97     TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "GetSelection end");
98     return value;
99 }
100 
DeleteSpans(const RangeOptions & options)101 void RichEditorController::DeleteSpans(const RangeOptions& options)
102 {
103     auto richEditorPattern = pattern_.Upgrade();
104     CHECK_NULL_VOID(richEditorPattern);
105     richEditorPattern->DeleteSpans(options);
106 }
107 
UpdateParagraphStyle(int32_t start,int32_t end,const struct UpdateParagraphStyle & style)108 void RichEditorController::UpdateParagraphStyle(int32_t start, int32_t end, const struct UpdateParagraphStyle& style)
109 {
110     auto richEditorPattern = pattern_.Upgrade();
111     CHECK_NULL_VOID(richEditorPattern);
112     richEditorPattern->UpdateParagraphStyle(start, end, style);
113     richEditorPattern->ForceTriggerAvoidOnCaretChange();
114 }
GetParagraphsInfo(int32_t start,int32_t end)115 std::vector<ParagraphInfo> RichEditorController::GetParagraphsInfo(int32_t start, int32_t end)
116 {
117     auto pattern = pattern_.Upgrade();
118     CHECK_NULL_RETURN(pattern, {});
119     TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "GetParagraphsInfo, start, range=[%{public}d,%{public}d]", start, end);
120     auto paragraphInfo = pattern->GetParagraphInfo(start, end);
121     TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "GetParagraphsInfo, end");
122     return paragraphInfo;
123 }
124 
ToStyledString(int32_t start,int32_t end)125 RefPtr<SpanStringBase> RichEditorController::ToStyledString(int32_t start, int32_t end)
126 {
127     auto richEditorPattern = pattern_.Upgrade();
128     CHECK_NULL_RETURN(richEditorPattern, nullptr);
129     return richEditorPattern->ToStyledString(start, end);
130 }
131 
FromStyledString(RefPtr<SpanStringBase> spanStringBase)132 SelectionInfo RichEditorController::FromStyledString(RefPtr<SpanStringBase> spanStringBase)
133 {
134     auto richEditorPattern = pattern_.Upgrade();
135     CHECK_NULL_RETURN(richEditorPattern, SelectionInfo());
136     auto spanString = AceType::DynamicCast<SpanString>(spanStringBase);
137     CHECK_NULL_RETURN(spanString, SelectionInfo());
138     return richEditorPattern->FromStyledString(spanString);
139 }
140 
141 } // namespace OHOS::Ace::NG
142