• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RICH_EDITOR_ONE_STEP_DRAG_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_ONE_STEP_DRAG_CONTROLLER_H
18 
19 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
20 
21 namespace OHOS::Ace::NG {
22 namespace {
23 using TagFilter = std::function<bool(const std::string&)>;
24 using Builder = std::function<void()>;
25 const TagFilter DEFAULT_FILTER = [](const std::string&) { return false; };
26 const TagFilter IMAGE_TAG_FILTER = [](const std::string& tag) { return tag == V2::IMAGE_ETS_TAG; };
27 const TagFilter PLACEHOLDER_TAG_FILTER = [](const std::string& tag) { return tag == V2::PLACEHOLDER_SPAN_ETS_TAG; };
28 }
29 
30 class OneStepDragParam {
31 public:
32     OneStepDragParam(const Builder& builder, const SelectMenuParam& selectMenuParam,
33         TextSpanType spanType, TagFilter tagFilter);
34     virtual ~OneStepDragParam() = default;
35     virtual MenuParam GetMenuParam(const RefPtr<FrameNode>& frameNode) const = 0;
36     virtual void EnableOneStepDrag(const RefPtr<FrameNode>& frameNode) = 0;
37     virtual void EnableDrag(const RefPtr<FrameNode>& frameNode) const;
38     void BindContextMenu(const RefPtr<FrameNode>& frameNode);
39     void FillJsonValue(const std::unique_ptr<JsonValue>& jsonValue) const;
40     void HandleDirtyNodes();
41     inline void MarkDirtyNode(const WeakPtr<FrameNode>& dirtyFrameNode);
42 
43     const TextSpanType spanType_;
44     const std::function<bool(const std::string&)> tagFilter_;
45 
46 protected:
47     Builder menuBuilder = nullptr;
48     Builder previewBuilder = nullptr;
49     std::function<void(int32_t, int32_t)> onAppear = nullptr;
50     std::function<void(int32_t, int32_t)> onDisappear = nullptr;
51     MenuParam menuParam;
52     std::queue<WeakPtr<FrameNode>> dirtyFrameNodes;
53 };
54 
55 class ImageOneStepDragParam : public OneStepDragParam {
56 public:
ImageOneStepDragParam(const Builder & builder,const SelectMenuParam & selectMenuParam)57     ImageOneStepDragParam(const Builder& builder, const SelectMenuParam& selectMenuParam)
58         : OneStepDragParam(builder, selectMenuParam, TextSpanType::IMAGE, IMAGE_TAG_FILTER) {}
59     MenuParam GetMenuParam(const RefPtr<FrameNode>& frameNode) const override;
60     void EnableOneStepDrag(const RefPtr<FrameNode>& frameNode) override;
61 
62 private:
63     float CalcImageScale(const RefPtr<ImageSpanNode>& imageNode) const;
64 };
65 
66 class PlaceholderOneStepDragParam : public OneStepDragParam {
67 public:
PlaceholderOneStepDragParam(const Builder & builder,const SelectMenuParam & selectMenuParam)68     PlaceholderOneStepDragParam(const Builder& builder, const SelectMenuParam& selectMenuParam)
69         : OneStepDragParam(builder, selectMenuParam, TextSpanType::BUILDER, PLACEHOLDER_TAG_FILTER) {}
70     MenuParam GetMenuParam(const RefPtr<FrameNode>& frameNode) const override;
71     void EnableDrag(const RefPtr<FrameNode>& frameNode) const override;
72     void EnableOneStepDrag(const RefPtr<FrameNode>& frameNode) override;
73 };
74 
75 class OneStepDragController {
76 public:
OneStepDragController(const WeakPtr<RichEditorPattern> & pattern)77     OneStepDragController(const WeakPtr<RichEditorPattern>& pattern) : pattern_(pattern) {}
78     static std::string GetJsonRange(TextSpanType spanType, const RefPtr<FrameNode>& frameNode);
79     bool SetMenuParam(TextSpanType spanType, const Builder& builder, const SelectMenuParam& menuParam);
80     void EnableOneStepDrag(TextSpanType spanType, const RefPtr<FrameNode>& frameNode);
81     void CopyDragCallback(TextSpanType spanType, const RefPtr<FrameNode>& frameNode);
82     void SetEnableEventResponse(bool isEnable);
83     void SetEnableEventResponse(const TextSelector& selector,
84         std::list<WeakPtr<ImageSpanNode>>& imageNodes,
85         std::list<WeakPtr<PlaceholderSpanNode>>& builderNodes);
86     template<typename T>
87     void SetEnableEventResponse(int32_t start, int32_t end, std::list<WeakPtr<T>>& builderNodes);
88     void FillJsonValue(const std::unique_ptr<JsonValue>& jsonValue);
89     void MarkDirtyNode(const WeakPtr<ImageSpanNode>& dirtyFrameNode);
90     void HandleDirtyNodes();
91 
92 private:
93     const std::unique_ptr<OneStepDragParam>& GetDragParam(TextSpanType spanType) const;
94     const std::unique_ptr<OneStepDragParam>& CreateDragParam(TextSpanType spanType, const Builder& builder,
95         const SelectMenuParam& menuParam);
96 
97 private:
98     const std::unique_ptr<OneStepDragParam> invalidParam = nullptr;
99     WeakPtr<RichEditorPattern> pattern_;
100     std::unique_ptr<OneStepDragParam> imageDragParam_ = nullptr;
101     std::unique_ptr<OneStepDragParam> placeholderDragParam_ = nullptr;
102     bool isEnableEventResponse_ = true;
103 };
104 
105 } // namespace OHOS::Ace::NG
106 
107 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_ONE_STEP_DRAG_CONTROLLER_H