@powerhousedao/reactor-browser 6.0.0-dev.3 → 6.0.0-dev.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/actions/dispatch.d.ts.map +1 -1
- package/dist/src/actions/dispatch.js +6 -2
- package/dist/src/actions/dispatch.js.map +1 -1
- package/dist/src/actions/document.d.ts +17 -4
- package/dist/src/actions/document.d.ts.map +1 -1
- package/dist/src/actions/document.js +162 -324
- package/dist/src/actions/document.js.map +1 -1
- package/dist/src/actions/drive.d.ts +1 -1
- package/dist/src/actions/drive.d.ts.map +1 -1
- package/dist/src/actions/drive.js +62 -209
- package/dist/src/actions/drive.js.map +1 -1
- package/dist/src/actions/index.d.ts +1 -1
- package/dist/src/actions/index.d.ts.map +1 -1
- package/dist/src/actions/index.js +1 -1
- package/dist/src/actions/index.js.map +1 -1
- package/dist/src/actions/queue.d.ts +2 -2
- package/dist/src/actions/queue.d.ts.map +1 -1
- package/dist/src/actions/queue.js +9 -38
- package/dist/src/actions/queue.js.map +1 -1
- package/dist/src/actions/sign.d.ts.map +1 -1
- package/dist/src/actions/sign.js +2 -7
- package/dist/src/actions/sign.js.map +1 -1
- package/dist/src/hooks/add-ph-event-handlers.d.ts.map +1 -1
- package/dist/src/hooks/add-ph-event-handlers.js +1 -7
- package/dist/src/hooks/add-ph-event-handlers.js.map +1 -1
- package/dist/src/hooks/config/connect.d.ts +2 -2
- package/dist/src/hooks/connect.d.ts +2 -2
- package/dist/src/hooks/connect.d.ts.map +1 -1
- package/dist/src/hooks/connect.js +2 -2
- package/dist/src/hooks/connect.js.map +1 -1
- package/dist/src/hooks/document-operations.d.ts +20 -0
- package/dist/src/hooks/document-operations.d.ts.map +1 -0
- package/dist/src/hooks/document-operations.js +92 -0
- package/dist/src/hooks/document-operations.js.map +1 -0
- package/dist/src/hooks/index.d.ts +2 -1
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +2 -1
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/node-actions.d.ts +1 -0
- package/dist/src/hooks/node-actions.d.ts.map +1 -1
- package/dist/src/hooks/node-actions.js +12 -1
- package/dist/src/hooks/node-actions.js.map +1 -1
- package/dist/src/hooks/reactor.d.ts +7 -27
- package/dist/src/hooks/reactor.d.ts.map +1 -1
- package/dist/src/hooks/reactor.js +19 -28
- package/dist/src/hooks/reactor.js.map +1 -1
- package/dist/src/hooks/use-feature-flags.d.ts +0 -27
- package/dist/src/hooks/use-feature-flags.d.ts.map +1 -1
- package/dist/src/hooks/use-feature-flags.js +0 -54
- package/dist/src/hooks/use-feature-flags.js.map +1 -1
- package/dist/src/hooks/use-get-switchboard-link.d.ts.map +1 -1
- package/dist/src/hooks/use-get-switchboard-link.js +18 -7
- package/dist/src/hooks/use-get-switchboard-link.js.map +1 -1
- package/dist/src/hooks/vetra-packages.d.ts +1 -1
- package/dist/src/hooks/vetra-packages.d.ts.map +1 -1
- package/dist/src/hooks/vetra-packages.js +14 -4
- package/dist/src/hooks/vetra-packages.js.map +1 -1
- package/dist/src/pglite/drop.d.ts +1 -0
- package/dist/src/pglite/drop.d.ts.map +1 -1
- package/dist/src/pglite/drop.js +9 -1
- package/dist/src/pglite/drop.js.map +1 -1
- package/dist/src/pglite/index.d.ts +1 -1
- package/dist/src/pglite/index.d.ts.map +1 -1
- package/dist/src/pglite/index.js +1 -1
- package/dist/src/pglite/index.js.map +1 -1
- package/dist/src/reactor-client-document-cache.d.ts.map +1 -1
- package/dist/src/reactor-client-document-cache.js +1 -3
- package/dist/src/reactor-client-document-cache.js.map +1 -1
- package/dist/src/reactor.d.ts +16 -7
- package/dist/src/reactor.d.ts.map +1 -1
- package/dist/src/reactor.js +24 -41
- package/dist/src/reactor.js.map +1 -1
- package/dist/src/renown/utils.d.ts +2 -3
- package/dist/src/renown/utils.d.ts.map +1 -1
- package/dist/src/renown/utils.js +13 -23
- package/dist/src/renown/utils.js.map +1 -1
- package/dist/src/types/global.d.ts +2 -8
- package/dist/src/types/global.d.ts.map +1 -1
- package/dist/src/utils/drive-url.d.ts +2 -0
- package/dist/src/utils/drive-url.d.ts.map +1 -0
- package/dist/src/utils/drive-url.js +3 -0
- package/dist/src/utils/drive-url.js.map +1 -0
- package/dist/src/utils/drives.d.ts +3 -33
- package/dist/src/utils/drives.d.ts.map +1 -1
- package/dist/src/utils/drives.js +10 -131
- package/dist/src/utils/drives.js.map +1 -1
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/test/drop.test.js +3 -6
- package/dist/test/drop.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/dist/src/hooks/processor-manager.d.ts +0 -9
- package/dist/src/hooks/processor-manager.d.ts.map +0 -1
- package/dist/src/hooks/processor-manager.js +0 -9
- package/dist/src/hooks/processor-manager.js.map +0 -1
|
@@ -1,79 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { addFolder as baseAddFolder, copyNode as baseCopyNode, deleteNode as baseDeleteNode, updateFile as baseUpdateFile, generateNodesCopy, handleTargetNameCollisions, isFileNode, isFolderNode, logger, updateNode, } from "document-drive";
|
|
2
2
|
import { documentModelDocumentType } from "document-model";
|
|
3
3
|
import { baseLoadFromInput, baseSaveToFileHandle, createPresignedHeader, createZip, generateId, replayDocument, } from "document-model/core";
|
|
4
4
|
import { UnsupportedDocumentTypeError } from "../errors.js";
|
|
5
|
-
import { isLegacyReadEnabledSync, isLegacyWriteEnabledSync, } from "../hooks/use-feature-flags.js";
|
|
6
5
|
import { isDocumentTypeSupported } from "../utils/documents.js";
|
|
7
6
|
import { getUserPermissions } from "../utils/user.js";
|
|
8
7
|
import { queueActions, queueOperations, uploadOperations } from "./queue.js";
|
|
9
8
|
async function isDocumentInLocation(document, driveId, parentFolder) {
|
|
10
|
-
const
|
|
9
|
+
const reactorClient = window.ph?.reactorClient;
|
|
10
|
+
if (!reactorClient) {
|
|
11
|
+
return { isDuplicate: false };
|
|
12
|
+
}
|
|
13
|
+
// Get the drive and check its nodes
|
|
14
|
+
let drive;
|
|
11
15
|
try {
|
|
12
|
-
|
|
13
|
-
const reactor = window.ph?.legacyReactor;
|
|
14
|
-
if (!reactor) {
|
|
15
|
-
throw new Error("Legacy reactor not initialized");
|
|
16
|
-
}
|
|
17
|
-
// Get the drive and check its nodes
|
|
18
|
-
const drive = await reactor.getDrive(driveId);
|
|
19
|
-
// Case 1: Check for duplicate by ID
|
|
20
|
-
const nodeById = drive.state.global.nodes.find((node) => node.id === document.header.id);
|
|
21
|
-
if (nodeById && nodeById.parentFolder === (parentFolder ?? null)) {
|
|
22
|
-
return {
|
|
23
|
-
isDuplicate: true,
|
|
24
|
-
duplicateType: "id",
|
|
25
|
-
nodeId: nodeById.id,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
// Case 2: Check for duplicate by name + type in same parent folder
|
|
29
|
-
const nodeByNameAndType = drive.state.global.nodes.find((node) => isFileNode(node) &&
|
|
30
|
-
node.name === document.header.name &&
|
|
31
|
-
node.documentType === document.header.documentType &&
|
|
32
|
-
node.parentFolder === (parentFolder ?? null));
|
|
33
|
-
if (nodeByNameAndType) {
|
|
34
|
-
return {
|
|
35
|
-
isDuplicate: true,
|
|
36
|
-
duplicateType: "name",
|
|
37
|
-
nodeId: nodeByNameAndType.id,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
return { isDuplicate: false };
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
const reactorClient = window.ph?.reactorClient;
|
|
44
|
-
if (!reactorClient) {
|
|
45
|
-
throw new Error("ReactorClient not initialized");
|
|
46
|
-
}
|
|
47
|
-
// Get the drive and check its nodes
|
|
48
|
-
const { document: drive } = await reactorClient.get(driveId);
|
|
49
|
-
// Case 1: Check for duplicate by ID
|
|
50
|
-
const nodeById = drive.state.global.nodes.find((node) => node.id === document.header.id);
|
|
51
|
-
if (nodeById && nodeById.parentFolder === (parentFolder ?? null)) {
|
|
52
|
-
return {
|
|
53
|
-
isDuplicate: true,
|
|
54
|
-
duplicateType: "id",
|
|
55
|
-
nodeId: nodeById.id,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
// Case 2: Check for duplicate by name + type in same parent folder
|
|
59
|
-
const nodeByNameAndType = drive.state.global.nodes.find((node) => isFileNode(node) &&
|
|
60
|
-
node.name === document.header.name &&
|
|
61
|
-
node.documentType === document.header.documentType &&
|
|
62
|
-
node.parentFolder === (parentFolder ?? null));
|
|
63
|
-
if (nodeByNameAndType) {
|
|
64
|
-
return {
|
|
65
|
-
isDuplicate: true,
|
|
66
|
-
duplicateType: "name",
|
|
67
|
-
nodeId: nodeByNameAndType.id,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
return { isDuplicate: false };
|
|
71
|
-
}
|
|
16
|
+
drive = await reactorClient.get(driveId);
|
|
72
17
|
}
|
|
73
18
|
catch {
|
|
74
|
-
// Drive doesn't exist or other error
|
|
75
19
|
return { isDuplicate: false };
|
|
76
20
|
}
|
|
21
|
+
// Case 1: Check for duplicate by ID
|
|
22
|
+
const nodeById = drive.state.global.nodes.find((node) => node.id === document.header.id);
|
|
23
|
+
if (nodeById && nodeById.parentFolder === (parentFolder ?? null)) {
|
|
24
|
+
return {
|
|
25
|
+
isDuplicate: true,
|
|
26
|
+
duplicateType: "id",
|
|
27
|
+
nodeId: nodeById.id,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// Case 2: Check for duplicate by name + type in same parent folder
|
|
31
|
+
const nodeByNameAndType = drive.state.global.nodes.find((node) => isFileNode(node) &&
|
|
32
|
+
node.name === document.header.name &&
|
|
33
|
+
node.documentType === document.header.documentType &&
|
|
34
|
+
node.parentFolder === (parentFolder ?? null));
|
|
35
|
+
if (nodeByNameAndType) {
|
|
36
|
+
return {
|
|
37
|
+
isDuplicate: true,
|
|
38
|
+
duplicateType: "name",
|
|
39
|
+
nodeId: nodeByNameAndType.id,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return { isDuplicate: false };
|
|
77
43
|
}
|
|
78
44
|
function getDocumentTypeIcon(document) {
|
|
79
45
|
const documentType = document.header.documentType;
|
|
@@ -120,27 +86,30 @@ export function downloadFile(document, fileName) {
|
|
|
120
86
|
})
|
|
121
87
|
.catch(logger.error);
|
|
122
88
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
throw new Error("Legacy reactor not initialized");
|
|
129
|
-
}
|
|
89
|
+
async function getDocumentExtension(document) {
|
|
90
|
+
const documentType = document.header.documentType;
|
|
91
|
+
// DocumentModel definitions always use "phdm"
|
|
92
|
+
if (documentType === documentModelDocumentType) {
|
|
93
|
+
return "phdm";
|
|
130
94
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
95
|
+
let rawExtension;
|
|
96
|
+
const reactorClient = window.ph?.reactorClient;
|
|
97
|
+
if (reactorClient) {
|
|
98
|
+
const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
|
|
99
|
+
const module = documentModelModules.find((m) => m.documentModel.global.id === documentType);
|
|
100
|
+
rawExtension = module?.utils.fileExtension;
|
|
135
101
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
102
|
+
// Clean the extension (remove leading/trailing dots) and fallback to "phdm"
|
|
103
|
+
const cleanExtension = (rawExtension ?? "phdm").replace(/^\.+|\.+$/g, "");
|
|
104
|
+
return cleanExtension || "phdm";
|
|
105
|
+
}
|
|
106
|
+
export async function exportFile(document, suggestedName) {
|
|
107
|
+
if (!window.ph?.reactorClient) {
|
|
108
|
+
throw new Error("ReactorClient not initialized");
|
|
142
109
|
}
|
|
143
|
-
|
|
110
|
+
// Get the extension from the document model module
|
|
111
|
+
const extension = await getDocumentExtension(document);
|
|
112
|
+
const name = `${suggestedName || document.header.name || "Untitled"}.${extension}.phd`;
|
|
144
113
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
145
114
|
if (!window.showSaveFilePicker) {
|
|
146
115
|
return downloadFile(document, name);
|
|
@@ -160,99 +129,51 @@ export async function exportFile(document, suggestedName) {
|
|
|
160
129
|
}
|
|
161
130
|
}
|
|
162
131
|
export async function loadFile(path) {
|
|
163
|
-
const useLegacy = isLegacyReadEnabledSync();
|
|
164
132
|
const baseDocument = await baseLoadFromInput(path, (state) => state, { checkHashes: true });
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
throw new Error("Legacy reactor not initialized");
|
|
169
|
-
}
|
|
170
|
-
const documentModelModules = reactor.getDocumentModelModules();
|
|
171
|
-
const documentModelModule = documentModelModules.find((module) => module.documentModel.global.id === baseDocument.header.documentType);
|
|
172
|
-
if (!documentModelModule) {
|
|
173
|
-
throw new Error(`Document "${baseDocument.header.documentType}" is not supported`);
|
|
174
|
-
}
|
|
175
|
-
return documentModelModule.utils.loadFromInput(path);
|
|
133
|
+
const reactorClient = window.ph?.reactorClient;
|
|
134
|
+
if (!reactorClient) {
|
|
135
|
+
throw new Error("ReactorClient not initialized");
|
|
176
136
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
|
|
183
|
-
const documentModelModule = documentModelModules.find((module) => module.documentModel.global.id === baseDocument.header.documentType);
|
|
184
|
-
if (!documentModelModule) {
|
|
185
|
-
throw new Error(`Document "${baseDocument.header.documentType}" is not supported`);
|
|
186
|
-
}
|
|
187
|
-
return documentModelModule.utils.loadFromInput(path);
|
|
137
|
+
const { results: documentModelModules } = await reactorClient.getDocumentModelModules();
|
|
138
|
+
const documentModelModule = documentModelModules.find((module) => module.documentModel.global.id === baseDocument.header.documentType);
|
|
139
|
+
if (!documentModelModule) {
|
|
140
|
+
throw new Error(`Document "${baseDocument.header.documentType}" is not supported`);
|
|
188
141
|
}
|
|
142
|
+
return documentModelModule.utils.loadFromInput(path);
|
|
189
143
|
}
|
|
190
144
|
export async function addDocument(driveId, name, documentType, parentFolder, document, id, preferredEditor) {
|
|
191
145
|
const { isAllowedToCreateDocuments } = getUserPermissions();
|
|
192
146
|
if (!isAllowedToCreateDocuments) {
|
|
193
147
|
throw new Error("User is not allowed to create documents");
|
|
194
148
|
}
|
|
195
|
-
const
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
if (!reactor) {
|
|
199
|
-
throw new Error("Legacy reactor not initialized");
|
|
200
|
-
}
|
|
201
|
-
const drive = await reactor.getDrive(driveId);
|
|
202
|
-
const documentId = id ?? generateId();
|
|
203
|
-
const reactorDocumentModelModules = reactor.getDocumentModelModules();
|
|
204
|
-
const documentModelModuleFromReactor = reactorDocumentModelModules.find((module) => module.documentModel.global.id === documentType);
|
|
205
|
-
if (!documentModelModuleFromReactor) {
|
|
206
|
-
throw new Error(`Document model module for type ${documentType} not found`);
|
|
207
|
-
}
|
|
208
|
-
const newDocument = documentModelModuleFromReactor.utils.createDocument({
|
|
209
|
-
...document?.state,
|
|
210
|
-
});
|
|
211
|
-
newDocument.header = createPresignedHeader(documentId, documentType);
|
|
212
|
-
newDocument.header.name = name;
|
|
213
|
-
const documentMeta = preferredEditor ? { preferredEditor } : undefined;
|
|
214
|
-
await reactor.addDocument(newDocument, documentMeta);
|
|
215
|
-
const action = baseAddFile({
|
|
216
|
-
id: documentId,
|
|
217
|
-
name,
|
|
218
|
-
documentType,
|
|
219
|
-
parentFolder: parentFolder ?? null,
|
|
220
|
-
});
|
|
221
|
-
const unsafeCastAsDrive = (await queueActions(drive, action));
|
|
222
|
-
const node = unsafeCastAsDrive.state.global.nodes.find((node) => node.id === documentId);
|
|
223
|
-
if (!node || !isFileNode(node)) {
|
|
224
|
-
throw new Error("There was an error adding document");
|
|
225
|
-
}
|
|
226
|
-
return node;
|
|
149
|
+
const reactorClient = window.ph?.reactorClient;
|
|
150
|
+
if (!reactorClient) {
|
|
151
|
+
throw new Error("ReactorClient not initialized");
|
|
227
152
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
catch (e) {
|
|
244
|
-
logger.error("Error adding document", e);
|
|
245
|
-
throw new Error("There was an error adding document");
|
|
246
|
-
}
|
|
247
|
-
// Return a file node structure for compatibility
|
|
248
|
-
return {
|
|
249
|
-
id: newDoc.header.id,
|
|
250
|
-
name: newDoc.header.name,
|
|
251
|
-
documentType,
|
|
252
|
-
parentFolder: parentFolder ?? null,
|
|
253
|
-
kind: "file",
|
|
254
|
-
};
|
|
153
|
+
// get the module
|
|
154
|
+
const documentModelModule = await reactorClient.getDocumentModelModule(documentType);
|
|
155
|
+
// create - use passed document's state if available
|
|
156
|
+
const newDocument = documentModelModule.utils.createDocument({
|
|
157
|
+
...document?.state,
|
|
158
|
+
});
|
|
159
|
+
newDocument.header.name = name;
|
|
160
|
+
// Create document using ReactorClient
|
|
161
|
+
let newDoc;
|
|
162
|
+
try {
|
|
163
|
+
newDoc = await reactorClient.createDocumentInDrive(driveId, newDocument, parentFolder);
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
logger.error("Error adding document", e);
|
|
167
|
+
throw new Error("There was an error adding document");
|
|
255
168
|
}
|
|
169
|
+
// Return a file node structure for compatibility
|
|
170
|
+
return {
|
|
171
|
+
id: newDoc.header.id,
|
|
172
|
+
name: newDoc.header.name,
|
|
173
|
+
documentType,
|
|
174
|
+
parentFolder: parentFolder ?? null,
|
|
175
|
+
kind: "file",
|
|
176
|
+
};
|
|
256
177
|
}
|
|
257
178
|
export async function addFile(file, driveId, name, parentFolder) {
|
|
258
179
|
logger.verbose(`addFile(drive: ${driveId}, name: ${name}, folder: ${parentFolder})`);
|
|
@@ -260,40 +181,18 @@ export async function addFile(file, driveId, name, parentFolder) {
|
|
|
260
181
|
if (!isAllowedToCreateDocuments) {
|
|
261
182
|
throw new Error("User is not allowed to create files");
|
|
262
183
|
}
|
|
263
|
-
const useLegacy = isLegacyWriteEnabledSync();
|
|
264
184
|
const document = await loadFile(file);
|
|
265
185
|
let duplicateId = false;
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
throw new Error("Legacy reactor not initialized");
|
|
270
|
-
}
|
|
271
|
-
const documentModule = reactor
|
|
272
|
-
.getDocumentModelModules()
|
|
273
|
-
.find((module) => module.documentModel.global.id === document.header.documentType);
|
|
274
|
-
if (!documentModule) {
|
|
275
|
-
throw new Error(`Document model module for type ${document.header.documentType} not found`);
|
|
276
|
-
}
|
|
277
|
-
try {
|
|
278
|
-
await reactor.getDocument(document.header.id);
|
|
279
|
-
duplicateId = true;
|
|
280
|
-
}
|
|
281
|
-
catch {
|
|
282
|
-
// document id not found
|
|
283
|
-
}
|
|
186
|
+
const reactorClient = window.ph?.reactorClient;
|
|
187
|
+
if (!reactorClient) {
|
|
188
|
+
throw new Error("ReactorClient not initialized");
|
|
284
189
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
await reactorClient.get(document.header.id);
|
|
292
|
-
duplicateId = true;
|
|
293
|
-
}
|
|
294
|
-
catch {
|
|
295
|
-
// document id not found
|
|
296
|
-
}
|
|
190
|
+
try {
|
|
191
|
+
await reactorClient.get(document.header.id);
|
|
192
|
+
duplicateId = true;
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
// document id not found
|
|
297
196
|
}
|
|
298
197
|
const documentId = duplicateId ? generateId() : document.header.id;
|
|
299
198
|
const header = createPresignedHeader(documentId, document.header.documentType);
|
|
@@ -318,7 +217,7 @@ export async function addFile(file, driveId, name, parentFolder) {
|
|
|
318
217
|
}
|
|
319
218
|
export async function addFileWithProgress(file, driveId, name, parentFolder, onProgress, documentTypes, resolveConflict) {
|
|
320
219
|
logger.verbose(`addFileWithProgress(drive: ${driveId}, name: ${name}, folder: ${parentFolder})`);
|
|
321
|
-
const reactor = window.ph?.
|
|
220
|
+
const reactor = window.ph?.reactorClient;
|
|
322
221
|
if (!reactor) {
|
|
323
222
|
return;
|
|
324
223
|
}
|
|
@@ -330,9 +229,6 @@ export async function addFileWithProgress(file, driveId, name, parentFolder, onP
|
|
|
330
229
|
try {
|
|
331
230
|
onProgress?.({ stage: "loading", progress: 0 });
|
|
332
231
|
const document = await loadFile(file);
|
|
333
|
-
if (!document) {
|
|
334
|
-
throw new Error("No document loaded");
|
|
335
|
-
}
|
|
336
232
|
// Check for duplicate in same location
|
|
337
233
|
const duplicateCheck = await isDocumentInLocation(document, driveId, parentFolder);
|
|
338
234
|
if (duplicateCheck.isDuplicate && !resolveConflict) {
|
|
@@ -368,17 +264,13 @@ export async function addFileWithProgress(file, driveId, name, parentFolder, onP
|
|
|
368
264
|
});
|
|
369
265
|
throw new UnsupportedDocumentTypeError(document.header.documentType);
|
|
370
266
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
.find((module) => module.documentModel.global.id === document.header.documentType);
|
|
374
|
-
if (!documentModule) {
|
|
375
|
-
throw new Error(`Document model module for type ${document.header.documentType} not found`);
|
|
376
|
-
}
|
|
267
|
+
// ensure we have the module + can load it (throws if not found)
|
|
268
|
+
await reactor.getDocumentModelModule(document.header.documentType);
|
|
377
269
|
// Initializing stage (10-20%)
|
|
378
270
|
onProgress?.({ stage: "initializing", progress: 10 });
|
|
379
271
|
let duplicateId = false;
|
|
380
272
|
try {
|
|
381
|
-
await reactor.
|
|
273
|
+
await reactor.get(document.header.id);
|
|
382
274
|
duplicateId = true;
|
|
383
275
|
}
|
|
384
276
|
catch {
|
|
@@ -440,7 +332,7 @@ export async function addFileWithProgress(file, driveId, name, parentFolder, onP
|
|
|
440
332
|
}
|
|
441
333
|
}
|
|
442
334
|
export async function updateFile(driveId, nodeId, documentType, name, parentFolder) {
|
|
443
|
-
const reactor = window.ph?.
|
|
335
|
+
const reactor = window.ph?.reactorClient;
|
|
444
336
|
if (!reactor) {
|
|
445
337
|
return;
|
|
446
338
|
}
|
|
@@ -448,7 +340,7 @@ export async function updateFile(driveId, nodeId, documentType, name, parentFold
|
|
|
448
340
|
if (!isAllowedToCreateDocuments) {
|
|
449
341
|
throw new Error("User is not allowed to update files");
|
|
450
342
|
}
|
|
451
|
-
const drive = await reactor.
|
|
343
|
+
const drive = await reactor.get(driveId);
|
|
452
344
|
const unsafeCastAsDrive = (await queueActions(drive, baseUpdateFile({
|
|
453
345
|
id: nodeId,
|
|
454
346
|
name: name ?? undefined,
|
|
@@ -466,148 +358,94 @@ export async function addFolder(driveId, name, parentFolder) {
|
|
|
466
358
|
if (!isAllowedToCreateDocuments) {
|
|
467
359
|
throw new Error("User is not allowed to create folders");
|
|
468
360
|
}
|
|
469
|
-
const
|
|
470
|
-
if (
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
361
|
+
const reactorClient = window.ph?.reactorClient;
|
|
362
|
+
if (!reactorClient) {
|
|
363
|
+
throw new Error("ReactorClient not initialized");
|
|
364
|
+
}
|
|
365
|
+
// Get the drive document and add folder action
|
|
366
|
+
const drive = await reactorClient.get(driveId);
|
|
367
|
+
const folderId = generateId();
|
|
368
|
+
const updatedDrive = await reactorClient.execute(driveId, "main", [
|
|
369
|
+
baseAddFolder({
|
|
478
370
|
id: folderId,
|
|
479
371
|
name,
|
|
480
372
|
parentFolder,
|
|
481
|
-
})
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
return node;
|
|
487
|
-
}
|
|
488
|
-
else {
|
|
489
|
-
const reactorClient = window.ph?.reactorClient;
|
|
490
|
-
if (!reactorClient) {
|
|
491
|
-
throw new Error("ReactorClient not initialized");
|
|
492
|
-
}
|
|
493
|
-
// Get the drive document and add folder action
|
|
494
|
-
const { document: drive } = await reactorClient.get(driveId);
|
|
495
|
-
const folderId = generateId();
|
|
496
|
-
const updatedDrive = await reactorClient.execute(driveId, "main", [
|
|
497
|
-
baseAddFolder({
|
|
498
|
-
id: folderId,
|
|
499
|
-
name,
|
|
500
|
-
parentFolder,
|
|
501
|
-
}),
|
|
502
|
-
]);
|
|
503
|
-
const node = updatedDrive.state.global.nodes.find((node) => node.id === folderId);
|
|
504
|
-
if (!node || !isFolderNode(node)) {
|
|
505
|
-
throw new Error("There was an error adding folder");
|
|
506
|
-
}
|
|
507
|
-
return node;
|
|
373
|
+
}),
|
|
374
|
+
]);
|
|
375
|
+
const node = updatedDrive.state.global.nodes.find((node) => node.id === folderId);
|
|
376
|
+
if (!node || !isFolderNode(node)) {
|
|
377
|
+
throw new Error("There was an error adding folder");
|
|
508
378
|
}
|
|
379
|
+
return node;
|
|
509
380
|
}
|
|
510
381
|
export async function deleteNode(driveId, nodeId) {
|
|
511
382
|
const { isAllowedToCreateDocuments } = getUserPermissions();
|
|
512
383
|
if (!isAllowedToCreateDocuments) {
|
|
513
384
|
throw new Error("User is not allowed to delete documents");
|
|
514
385
|
}
|
|
515
|
-
const
|
|
516
|
-
if (
|
|
517
|
-
|
|
518
|
-
if (!reactor) {
|
|
519
|
-
throw new Error("Legacy reactor not initialized");
|
|
520
|
-
}
|
|
521
|
-
const drive = await reactor.getDrive(driveId);
|
|
522
|
-
await queueActions(drive, baseDeleteNode({ id: nodeId }));
|
|
523
|
-
// now delete the document
|
|
524
|
-
await reactor.deleteDocument(nodeId);
|
|
525
|
-
}
|
|
526
|
-
else {
|
|
527
|
-
const reactorClient = window.ph?.reactorClient;
|
|
528
|
-
if (!reactorClient) {
|
|
529
|
-
throw new Error("ReactorClient not initialized");
|
|
530
|
-
}
|
|
531
|
-
// delete the node in the drive document
|
|
532
|
-
await reactorClient.execute(driveId, "main", [
|
|
533
|
-
baseDeleteNode({ id: nodeId }),
|
|
534
|
-
]);
|
|
535
|
-
// now delete the document
|
|
536
|
-
await reactorClient.deleteDocument(nodeId);
|
|
386
|
+
const reactorClient = window.ph?.reactorClient;
|
|
387
|
+
if (!reactorClient) {
|
|
388
|
+
throw new Error("ReactorClient not initialized");
|
|
537
389
|
}
|
|
390
|
+
// delete the node in the drive document
|
|
391
|
+
await reactorClient.execute(driveId, "main", [
|
|
392
|
+
baseDeleteNode({ id: nodeId }),
|
|
393
|
+
]);
|
|
394
|
+
// now delete the document
|
|
395
|
+
await reactorClient.deleteDocument(nodeId);
|
|
538
396
|
}
|
|
539
397
|
export async function renameNode(driveId, nodeId, name) {
|
|
540
398
|
const { isAllowedToCreateDocuments } = getUserPermissions();
|
|
541
399
|
if (!isAllowedToCreateDocuments) {
|
|
542
400
|
throw new Error("User is not allowed to rename documents");
|
|
543
401
|
}
|
|
544
|
-
const
|
|
545
|
-
if (
|
|
546
|
-
|
|
547
|
-
if (!reactor) {
|
|
548
|
-
throw new Error("Legacy reactor not initialized");
|
|
549
|
-
}
|
|
550
|
-
const drive = await reactor.getDrive(driveId);
|
|
551
|
-
const unsafeCastAsDrive = (await queueActions(drive, updateNode({ id: nodeId, name })));
|
|
552
|
-
const node = unsafeCastAsDrive.state.global.nodes.find((node) => node.id === nodeId);
|
|
553
|
-
if (!node) {
|
|
554
|
-
throw new Error("There was an error renaming node");
|
|
555
|
-
}
|
|
556
|
-
return node;
|
|
402
|
+
const reactorClient = window.ph?.reactorClient;
|
|
403
|
+
if (!reactorClient) {
|
|
404
|
+
throw new Error("ReactorClient not initialized");
|
|
557
405
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
await reactorClient.rename(nodeId, name);
|
|
564
|
-
// Fetch the updated drive to return the node
|
|
565
|
-
const { document: drive } = await reactorClient.get(driveId);
|
|
566
|
-
const node = drive.state.global.nodes.find((n) => n.id === nodeId);
|
|
567
|
-
if (!node) {
|
|
568
|
-
throw new Error("There was an error renaming node");
|
|
569
|
-
}
|
|
570
|
-
return node;
|
|
406
|
+
// Rename the node in the drive document using updateNode action
|
|
407
|
+
const drive = await reactorClient.execute(driveId, "main", [updateNode({ id: nodeId, name })]);
|
|
408
|
+
const node = drive.state.global.nodes.find((n) => n.id === nodeId);
|
|
409
|
+
if (!node) {
|
|
410
|
+
throw new Error("There was an error renaming node");
|
|
571
411
|
}
|
|
412
|
+
return node;
|
|
413
|
+
}
|
|
414
|
+
export async function renameDriveNode(driveId, nodeId, name) {
|
|
415
|
+
const { isAllowedToCreateDocuments } = getUserPermissions();
|
|
416
|
+
if (!isAllowedToCreateDocuments) {
|
|
417
|
+
throw new Error("User is not allowed to rename documents");
|
|
418
|
+
}
|
|
419
|
+
const reactorClient = window.ph?.reactorClient;
|
|
420
|
+
if (!reactorClient) {
|
|
421
|
+
throw new Error("ReactorClient not initialized");
|
|
422
|
+
}
|
|
423
|
+
await reactorClient.execute(driveId, "main", [
|
|
424
|
+
updateNode({ id: nodeId, name }),
|
|
425
|
+
]);
|
|
426
|
+
const drive = await reactorClient.get(driveId);
|
|
427
|
+
return drive.state.global.nodes.find((n) => n.id === nodeId);
|
|
572
428
|
}
|
|
573
429
|
export async function moveNode(driveId, src, target) {
|
|
574
430
|
const { isAllowedToCreateDocuments } = getUserPermissions();
|
|
575
431
|
if (!isAllowedToCreateDocuments) {
|
|
576
432
|
throw new Error("User is not allowed to move documents");
|
|
577
433
|
}
|
|
578
|
-
const
|
|
579
|
-
if (
|
|
580
|
-
|
|
581
|
-
if (!reactor) {
|
|
582
|
-
throw new Error("Legacy reactor not initialized");
|
|
583
|
-
}
|
|
584
|
-
const drive = await reactor.getDrive(driveId);
|
|
585
|
-
return await queueActions(drive, baseMoveNode({ srcFolder: src.id, targetParentFolder: target?.id }));
|
|
586
|
-
}
|
|
587
|
-
else {
|
|
588
|
-
const reactorClient = window.ph?.reactorClient;
|
|
589
|
-
if (!reactorClient) {
|
|
590
|
-
throw new Error("ReactorClient not initialized");
|
|
591
|
-
}
|
|
592
|
-
// Get current parent folder from source node
|
|
593
|
-
const sourceParent = src.parentFolder ?? driveId;
|
|
594
|
-
const targetParent = target?.id ?? driveId;
|
|
595
|
-
return await reactorClient.moveChildren(sourceParent, targetParent, [
|
|
596
|
-
src.id,
|
|
597
|
-
]);
|
|
434
|
+
const reactorClient = window.ph?.reactorClient;
|
|
435
|
+
if (!reactorClient) {
|
|
436
|
+
throw new Error("ReactorClient not initialized");
|
|
598
437
|
}
|
|
438
|
+
// Get current parent folder from source node
|
|
439
|
+
const sourceParent = src.parentFolder ?? driveId;
|
|
440
|
+
const targetParent = target?.id ?? driveId;
|
|
441
|
+
return await reactorClient.moveChildren(sourceParent, targetParent, [src.id]);
|
|
599
442
|
}
|
|
600
|
-
function _duplicateDocument(reactor, document, newId = generateId()) {
|
|
601
|
-
const documentModule = reactor
|
|
602
|
-
.getDocumentModelModules()
|
|
603
|
-
.find((module) => module.documentModel.global.id === document.header.documentType);
|
|
604
|
-
if (!documentModule) {
|
|
605
|
-
throw new Error(`Document model module for type ${document.header.documentType} not found`);
|
|
606
|
-
}
|
|
443
|
+
async function _duplicateDocument(reactor, document, newId = generateId()) {
|
|
444
|
+
const documentModule = await reactor.getDocumentModelModule(document.header.documentType);
|
|
607
445
|
return replayDocument(document.initialState, document.operations, documentModule.reducer, createPresignedHeader(newId, document.header.documentType));
|
|
608
446
|
}
|
|
609
447
|
export async function copyNode(driveId, src, target) {
|
|
610
|
-
const reactor = window.ph?.
|
|
448
|
+
const reactor = window.ph?.reactorClient;
|
|
611
449
|
if (!reactor) {
|
|
612
450
|
return;
|
|
613
451
|
}
|
|
@@ -615,7 +453,7 @@ export async function copyNode(driveId, src, target) {
|
|
|
615
453
|
if (!isAllowedToCreateDocuments) {
|
|
616
454
|
throw new Error("User is not allowed to copy documents");
|
|
617
455
|
}
|
|
618
|
-
const drive = await reactor.
|
|
456
|
+
const drive = await reactor.get(driveId);
|
|
619
457
|
const copyNodesInput = generateNodesCopy({
|
|
620
458
|
srcId: src.id,
|
|
621
459
|
targetParentFolder: target?.id,
|
|
@@ -640,14 +478,14 @@ export async function copyNode(driveId, src, target) {
|
|
|
640
478
|
});
|
|
641
479
|
for (const fileNodeToCopy of fileNodesToCopy) {
|
|
642
480
|
try {
|
|
643
|
-
const document = await reactor.
|
|
644
|
-
const duplicatedDocument = _duplicateDocument(reactor, document, fileNodeToCopy.targetId);
|
|
481
|
+
const document = await reactor.get(fileNodeToCopy.srcId);
|
|
482
|
+
const duplicatedDocument = await _duplicateDocument(reactor, document, fileNodeToCopy.targetId);
|
|
645
483
|
// Set the header name to match the collision-resolved node name
|
|
646
484
|
const resolvedName = resolvedNamesMap.get(fileNodeToCopy.targetId);
|
|
647
485
|
if (resolvedName) {
|
|
648
486
|
duplicatedDocument.header.name = resolvedName;
|
|
649
487
|
}
|
|
650
|
-
await reactor.
|
|
488
|
+
await reactor.createDocumentInDrive(driveId, duplicatedDocument, target?.id);
|
|
651
489
|
}
|
|
652
490
|
catch (e) {
|
|
653
491
|
logger.error(`Error copying document ${fileNodeToCopy.srcId}: ${String(e)}`);
|