1 /*
2  * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_MUTABLE_SPAN_STRING_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_MUTABLE_SPAN_STRING_H
18 
19 #include <list>
20 #include <string>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "core/components_ng/pattern/text/span/span_string.h"
25 
26 namespace OHOS::Ace {
27 enum class AroundSpecialNode { AFTER = 0, BEFORE, BETWEEN, NONE };
28 
29 class ACE_EXPORT MutableSpanString : public SpanString {
30     DECLARE_ACE_TYPE(MutableSpanString, SpanString);
31 
32 public:
MutableSpanString(const std::string & text)33     explicit MutableSpanString(const std::string& text) : SpanString(text) {}
MutableSpanString(const ImageSpanOptions & options)34     explicit MutableSpanString(const ImageSpanOptions& options) : SpanString(options) {}
MutableSpanString(RefPtr<CustomSpan> & span)35     explicit MutableSpanString(RefPtr<CustomSpan>& span) : SpanString(span) {}
36     void ReplaceString(int32_t start, int32_t length, const std::string& other);
37     void InsertString(int32_t start, const std::string& other);
38     void RemoveString(int32_t start, int32_t length);
39     void ReplaceSpan(int32_t start, int32_t length, const RefPtr<SpanBase>& span);
40     void RemoveSpans(int32_t start, int32_t length, bool removeSpecialSpan = true);
41     void ClearAllSpans();
42     void ReplaceSpanString(int32_t start, int32_t length, const RefPtr<SpanString>& spanString);
43     void InsertSpanString(int32_t start, const RefPtr<SpanString>& spanString);
44     void AppendSpanString(const RefPtr<SpanString>& spanString);
45     bool IsSpeicalNode(int32_t location, SpanType speicalType);
46     void SetSpanWatcher(const WeakPtr<SpanWatcher>& watcher);
47     void NotifySpanWatcher();
48 
49 private:
50     WeakPtr<SpanWatcher> watcher_;
51     void KeepSpansOrder();
52     std::list<RefPtr<NG::SpanItem>>::iterator HandleSpanOperation(
53         std::list<RefPtr<NG::SpanItem>>::iterator it, int32_t start, int32_t length,
54         const std::string& other, SpanStringOperation op, const std::pair<int32_t, int32_t>& intersection);
55     void ApplyReplaceStringToSpans(int32_t start, int32_t length, const std::string& other, SpanStringOperation op);
56     void ApplyInsertStringToSpans(int32_t start, const std::string& other);
57     void ApplyReplaceStringToSpanBase(int32_t start, int32_t length, const std::string& other, SpanStringOperation op);
58     void ApplyInsertSpanStringToSpans(int32_t start, const RefPtr<SpanString>& spanString);
59     void ApplyInsertSpanStringToSpanBase(int32_t start, const RefPtr<SpanString>& spanString);
60     void SplitSpansByNewLine();
61     bool InsertUseFrontStyle(int32_t start);
62     void UpdateSpanAndSpanMapAfterInsertSpanString(int32_t start, int32_t offset);
63     void UpdateSpansAndSpanMapWithOffsetAfterInsert(int32_t start, int32_t offset, bool useFrontStyle);
64     AroundSpecialNode IsInsertAroundSpecialNode(int32_t start);
65     void InsertStringAroundSpecialNode(int32_t start, const std::string& str, AroundSpecialNode aroundMode);
66     void RemoveSpecialpanText();
67     void RemoveSpecialSpans(int32_t start, int32_t length);
68     void ProcessSpanBaseList(std::list<RefPtr<SpanBase>>& spans, int32_t start,
69         int32_t end, const std::string& other, SpanStringOperation op, SpanType spanType);
70 };
71 } // namespace OHOS::Ace
72 
73 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_MUTABLE_SPAN_STRING_H
74