1 /*
2  * Copyright (c) 2021 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_DECLARATION_RICHTEXT_RICHTEXT_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_RICHTEXT_RICHTEXT_DECLARATION_H
18 
19 #include "core/components/declaration/common/declaration.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 
22 namespace OHOS::Ace {
23 
24 struct RichTextAttribute : Attribute {
25     std::string data;
26 };
27 
28 struct RichTextEvent : Event {
29     EventMarker pageStartEventId;
30     EventMarker pageFinishEventId;
31     EventMarker pageErrorEventId;
32 };
33 
34 class RichTextDeclaration : public Declaration {
35     DECLARE_ACE_TYPE(RichTextDeclaration, Declaration);
36 
37 public:
38     RichTextDeclaration() = default;
39     ~RichTextDeclaration() override = default;
40 
SetData(const std::string & data)41     void SetData(const std::string& data)
42     {
43         auto& attribute = MaybeResetAttribute<RichTextAttribute>(AttributeTag::SPECIALIZED_ATTR);
44         attribute.data = data;
45     }
46 
GetData()47     const std::string& GetData() const
48     {
49         auto& attribute = static_cast<RichTextAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
50         return attribute.data;
51     }
52 
SetPageStartedEventId(const EventMarker & pageStartedEventId)53     void SetPageStartedEventId(const EventMarker& pageStartedEventId)
54     {
55         auto& event = MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
56         event.pageStartEventId = pageStartedEventId;
57     }
58 
GetPageStartedEventId()59     const EventMarker& GetPageStartedEventId() const
60     {
61         auto& event = static_cast<RichTextEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
62         return event.pageStartEventId;
63     }
64 
SetPageFinishedEventId(const EventMarker & pageFinishedEventId)65     void SetPageFinishedEventId(const EventMarker& pageFinishedEventId)
66     {
67         auto& event = MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
68         event.pageFinishEventId = pageFinishedEventId;
69     }
70 
GetPageFinishedEventId()71     const EventMarker& GetPageFinishedEventId() const
72     {
73         auto& event = static_cast<RichTextEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
74         return event.pageFinishEventId;
75     }
76 
SetPageErrorEventId(const EventMarker & pageErrorEventId)77     void SetPageErrorEventId(const EventMarker& pageErrorEventId)
78     {
79         auto& event = MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
80         event.pageErrorEventId = pageErrorEventId;
81     }
82 
GetPageErrorEventId()83     const EventMarker& GetPageErrorEventId() const
84     {
85         auto& event = static_cast<RichTextEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
86         return event.pageErrorEventId;
87     }
88 
89 protected:
90     void InitSpecialized() override;
91     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
92     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
93 };
94 
95 } // namespace OHOS::Ace
96 
97 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_RICHTEXT_RICHTEXT_DECLARATION_H
98