-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsandbox.ts
141 lines (123 loc) · 3.57 KB
/
sandbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* istanbul ignore file */
import { QueryClient } from "@tanstack/react-query";
import ZoteroRoam from "./src/api";
import { initialize } from "./src/setup";
import { simplifyZoteroAnnotations } from "./src/utils";
import { apiKeys, findCollections, findItems, libraries, sampleAnnot, sampleImageAnnot, sampleNote, samplePDF } from "Mocks";
import { RImportableBlock, RImportableElement } from "Types/transforms";
const { keyWithFullAccess: { key: masterKey } } = apiKeys;
const { userLibrary } = libraries;
/**
* Creates a public API instance for sandbox environments (e.g Observable). Data is accessed via cache, and any methods that involve live API calls are overwritten.
*/
class ZoteroRoamSandbox extends ZoteroRoam {
constructor({ annotations = {}, metadata = {} }){
const INSTALL_CONTEXT = "sandbox";
const { requests, settings } = initialize({
context: INSTALL_CONTEXT,
manualSettings: {
dataRequests: [
{
apikey: masterKey,
library: {
type: userLibrary.type,
id: `${userLibrary.id}`
}
}
],
// @ts-ignore
annotations: {
func: "customAnnotsFunction",
use: "function",
...annotations
},
// @ts-ignore
metadata: {
func: "customFunction",
use: "function",
...metadata
}
}
});
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: Infinity,
staleTime: Infinity
}
}
});
queryClient.setQueryData(
["items", { dataURI: userLibrary.path + "/items", library: userLibrary.path }],
(_prev) => ({
data: [
...findItems({ type: userLibrary.type, id: userLibrary.id, since: 0 }),
sampleAnnot,
sampleImageAnnot,
sampleNote,
samplePDF
],
lastUpdated: userLibrary.version
})
);
queryClient.setQueryData(
["collections", { library: userLibrary.path }],
(_prev) => ({
data: findCollections(userLibrary.type, userLibrary.id, 0),
lastUpdated: userLibrary.version
})
);
super({ queryClient, requests, settings });
}
updateLibraries(){
throw new Error("Updating the libraries used in the sandbox is not allowed.");
}
// @ts-ignore
getBibEntries(){
throw new Error("Generating bibliographic information is not available in the sandbox.");
}
// @ts-ignore
getItemCitation(){
throw new Error("Generating citation information is not available in the sandbox.");
}
// @ts-ignore
getTags(){
throw new Error("Retrieving data on library tags is not allowed in the sandbox.");
}
}
/** Wraps contents of a block object inside of a `<li>` tag. */
function blockObjectToHTML(object: RImportableBlock): string{
let objectHTML = "";
if(typeof(object.string) === "undefined"){
throw new Error("All blocks passed as an Object must have a string property");
} else {
objectHTML = objectHTML + `<li>${object.string}`;
if(typeof(object.children) !== "undefined"){
if(object.children.length > 0){
objectHTML = objectHTML + arrayToHTML(object.children);
}
}
objectHTML = objectHTML + " </li>";
}
return objectHTML;
}
/** Generates HTML list from metadata blocks. */
function arrayToHTML(arr: RImportableElement[]): string{
let renderedHTML = "<ul>";
arr.forEach((el) => {
if(typeof(el) == "object"){
renderedHTML = renderedHTML + blockObjectToHTML(el);
} else if(typeof(el) == "string") {
renderedHTML = renderedHTML + `<li>${el} </li>`;
} else {
throw new Error("All array items should be of type String or Object");
}
});
renderedHTML = renderedHTML + "</ul>";
return renderedHTML;
}
export {
arrayToHTML,
simplifyZoteroAnnotations,
ZoteroRoamSandbox
};