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 #define LOG_TAG "UndoManager"
16
17 #include "napi_undo_manager.h"
18
19 #include "napi_error_utils.h"
20
21 namespace OHOS::CollaborationEdit {
22
UndoManager(std::string tableName,int64_t captureTimeout)23 UndoManager::UndoManager(std::string tableName, int64_t captureTimeout) : captureTimeout_(captureTimeout)
24 {
25 this->adapter_ = std::make_shared<RdAdapter>();
26 this->adapter_->SetTableName(tableName);
27 }
28
~UndoManager()29 UndoManager::~UndoManager()
30 {}
31
Initialize(napi_env env,napi_callback_info info)32 napi_value UndoManager::Initialize(napi_env env, napi_callback_info info)
33 {
34 size_t argc = 2;
35 napi_value argv[2] = {nullptr};
36 napi_value self = nullptr;
37 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
38 std::string tableName;
39 napi_status status = NapiUtils::GetValue(env, argv[0], tableName);
40 ASSERT_THROW_BASE(env, status == napi_ok, Status::INVALID_ARGUMENT, "read tableName go wrong", self);
41 int64_t captureTimeout = 0;
42 status = NapiUtils::GetNamedProperty(env, argv[1], "captureTimeout", captureTimeout);
43 if (status != napi_ok) {
44 ThrowNapiError(env, Status::INVALID_ARGUMENT, "read captureTimeout param go wrong");
45 return self;
46 }
47 ASSERT_THROW_BASE(env, status == napi_ok, Status::INVALID_ARGUMENT, "read captureTimeout param go wrong", self);
48 ASSERT_THROW_BASE(env, captureTimeout >= 0, Status::INVALID_ARGUMENT,
49 "Param Error: captureTimeout cannot be negative.", self);
50 tableName = std::to_string(LABEL_FRAGMENT) + "_" + tableName;
51 UndoManager *undoManager = new (std::nothrow) UndoManager(tableName, captureTimeout);
52 auto finalize = [](napi_env env, void *data, void *hint) {
53 UndoManager *undoManager = reinterpret_cast<UndoManager *>(data);
54 delete undoManager;
55 };
56 status = napi_wrap(env, self, undoManager, finalize, nullptr, nullptr);
57 if (status != napi_ok) {
58 LOG_ERROR("napi_wrap failed. code:%{public}d", status);
59 delete undoManager;
60 return nullptr;
61 }
62 return self;
63 }
64
Constructor(napi_env env)65 napi_value UndoManager::Constructor(napi_env env)
66 {
67 auto lambda = []() -> std::vector<napi_property_descriptor> {
68 std::vector<napi_property_descriptor> properties = {
69 DECLARE_NAPI_FUNCTION("undo", Undo),
70 DECLARE_NAPI_FUNCTION("redo", Redo),
71 };
72 return properties;
73 };
74 return NapiUtils::DefineClass(
75 env, "ohos.data.collaborationEditObject", "UndoManager", lambda, Initialize);
76 }
77
NewInstance(napi_env env,napi_callback_info info)78 napi_value UndoManager::NewInstance(napi_env env, napi_callback_info info)
79 {
80 size_t argc = 2;
81 napi_value argv[2] = {nullptr};
82 napi_value undoManager = nullptr;
83 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
84
85 napi_status status = napi_new_instance(env, Constructor(env), argc, argv, &undoManager);
86 if (undoManager == nullptr || status != napi_ok) {
87 LOG_ERROR("[NewInstance] new instance go wrong");
88 }
89 return undoManager;
90 }
91
Undo(napi_env env,napi_callback_info info)92 napi_value UndoManager::Undo(napi_env env, napi_callback_info info)
93 {
94 napi_value self = nullptr;
95 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &self, nullptr));
96 UndoManager *manager = nullptr;
97 napi_status status = napi_unwrap(env, self, reinterpret_cast<void **>(&manager));
98 if (status != napi_ok || manager == nullptr) {
99 ThrowNapiError(env, Status::INVALID_ARGUMENT, "unwrap node go wrong");
100 return nullptr;
101 }
102 int32_t retCode = manager->GetAdapter()->Undo();
103 if (retCode != 0) {
104 ThrowNapiError(env, Status::DB_ERROR, "undo go wrong.");
105 }
106 return nullptr;
107 }
108
Redo(napi_env env,napi_callback_info info)109 napi_value UndoManager::Redo(napi_env env, napi_callback_info info)
110 {
111 napi_value self = nullptr;
112 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &self, nullptr));
113 UndoManager *manager = nullptr;
114 napi_status status = napi_unwrap(env, self, reinterpret_cast<void **>(&manager));
115 if (status != napi_ok || manager == nullptr) {
116 ThrowNapiError(env, Status::INVALID_ARGUMENT, "unwrap self go wrong");
117 return nullptr;
118 }
119 int32_t retCode = manager->GetAdapter()->Redo();
120 if (retCode != 0) {
121 ThrowNapiError(env, Status::DB_ERROR, "redo go wrong.");
122 }
123 return nullptr;
124 }
125
GetTableName()126 std::string UndoManager::GetTableName()
127 {
128 return this->adapter_->GetTableName();
129 }
130
GetCaptureTimeout()131 int64_t UndoManager::GetCaptureTimeout()
132 {
133 return this->captureTimeout_;
134 }
135
GetAdapter()136 std::shared_ptr<RdAdapter> UndoManager::GetAdapter()
137 {
138 return this->adapter_;
139 }
140
SetDBStore(std::shared_ptr<DBStore> dbStore)141 void UndoManager::SetDBStore(std::shared_ptr<DBStore> dbStore)
142 {
143 this->adapter_->SetDBStore(dbStore);
144 }
145
146 } // namespace OHOS::CollaborationEdit
147