175 lines
No EOL
4.8 KiB
JavaScript
175 lines
No EOL
4.8 KiB
JavaScript
|
|
addToLibrary({
|
|
open_filepicker: function() {
|
|
window.filePicker.show();
|
|
},
|
|
set_filter: function(filter) {
|
|
window.filePicker.setFilter(Module.UTF8ToString(filter));
|
|
},
|
|
file_picker_confirmed: function() {
|
|
return window.filePicker.wasConfirmed();
|
|
},
|
|
file_picker_closed: function() {
|
|
return window.filePicker.wasClosed();
|
|
},
|
|
file_picker_cancelled: function() {
|
|
return window.filePicker.wasCancelled();
|
|
},
|
|
get_first_file: function() {
|
|
if (window.filePicker.wasConfirmed()) {
|
|
let output = window.filePicker.getFirstFile();
|
|
let len = Module.lengthBytesUTF8(output) + 1;
|
|
let outptr = Module._malloc(len);
|
|
for (let i = 0; i < len; i++) {
|
|
setValue(outptr + i, 0, 'i8');
|
|
}
|
|
Module.stringToUTF8(output, outptr, len);
|
|
return outptr;
|
|
} else {
|
|
return 0;
|
|
}
|
|
},
|
|
file_picker_visible: function() {
|
|
return window.filePicker.visible;
|
|
},
|
|
file_picker_loading: function() {
|
|
return window.filePicker.loading;
|
|
},
|
|
clear_file_selection: function() {
|
|
window.filePicker.clearSelection();
|
|
},
|
|
get_size: function(x, y) {
|
|
let canvas = document.getElementById("canvas");
|
|
setValue(x, canvas.offsetWidth, "i32");
|
|
setValue(y, canvas.offsetHeight, "i32");
|
|
},
|
|
is_puter_enabled: function() {
|
|
return window.puterEnabled;
|
|
},
|
|
enable_puter: function(enable) {
|
|
window.puterEnabled = enable;
|
|
},
|
|
write_storage: async function(keyPtr, valuePtr, valueLen) {
|
|
let key = Module.UTF8ToString(keyPtr)
|
|
/**
|
|
* @type {Uint8Array}
|
|
*/
|
|
let valueData = HEAPU8.subarray(valuePtr, valuePtr + valueLen)
|
|
let valueBase64 = window.bytesToBase64(valueData);
|
|
if (valueBase64 === null) {
|
|
console.error("Couldn't encode key '", key, "' to base64.");
|
|
return;
|
|
}
|
|
if (window.puterEnabled) {
|
|
value = await puter.kv.set(key, valueBase64);
|
|
}
|
|
localStorage.setItem(key, valueBase64);
|
|
},
|
|
remove_storage: async function(keyPtr) {
|
|
let key = Module.UTF8ToString(keyPtr)
|
|
if (window.puterEnabled) {
|
|
await puter.kv.del(key)
|
|
}
|
|
localStorage.removeItem(Module.UTF8ToString(key));
|
|
},
|
|
get_dpi: function() {
|
|
return window.devicePixelRatio;
|
|
},
|
|
read_storage: async function(keyPtr, valuePtr, lenPtr) {
|
|
let key = Module.UTF8ToString(keyPtr);
|
|
let valueBase64 = localStorage.getItem(key);
|
|
if (window.puterEnabled) {
|
|
let puterValue = await puter.kv.get(key)
|
|
if (puterValue !== null) {
|
|
valueBase64 = puterValue;
|
|
}
|
|
}
|
|
if (valueBase64 === null) {
|
|
console.error("Could not find key '", key, "'.")
|
|
}
|
|
/**
|
|
* @type {Uint8Array|null}
|
|
*/
|
|
let value = null;
|
|
try {
|
|
value = window.base64ToBytes(valueBase64)
|
|
} catch (e) {
|
|
console.error(e)
|
|
value = null;
|
|
}
|
|
if (value == null) {
|
|
console.error("Could not decode key '", key, "'.")
|
|
} else {
|
|
let len = value.length;
|
|
let outptr = Module._malloc(len + 1);
|
|
for (let i = 0; i < len; i++) {
|
|
setValue(outptr + i, value[i], 'i8');
|
|
}
|
|
if (lenPtr !== 0) {
|
|
setValue(lenPtr, value.length, "i32")
|
|
}
|
|
setValue(outptr + len, 0, 'i8')
|
|
setValue(valuePtr, outptr, "i32");
|
|
}
|
|
},
|
|
find_keys: async function(prefixPtr, pppOut) {
|
|
let prefix = Module.UTF8ToString(prefixPtr);
|
|
let storageLen = localStorage.length;
|
|
let puterList = [];
|
|
if (window.puterEnabled) {
|
|
puterList = await puter.kv.list(false);
|
|
}
|
|
let arr = [];
|
|
let dict = {};
|
|
for (let i = 0; i < storageLen; i++) {
|
|
let key = localStorage.key(i);
|
|
if (key.startsWith(prefix)) {
|
|
let base64Test = localStorage.getItem(key);
|
|
try {
|
|
window.base64ToBytes(base64Test)
|
|
} catch (e) {
|
|
continue;
|
|
}
|
|
arr = [...arr, key];
|
|
dict[key] = true;
|
|
}
|
|
}
|
|
if (window.puterEnabled) {
|
|
for (let i = 0; i < puterList.length; i++) {
|
|
let key = puterList[i];
|
|
if (key.startsWith(prefix)) {
|
|
if (dict[key] !== true) {
|
|
let base64Test = await puter.kv.get(key);
|
|
try {
|
|
window.base64ToBytes(base64Test)
|
|
} catch (e) {
|
|
continue;
|
|
}
|
|
arr = [...arr, key];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let arrLen = arr.length;
|
|
let ptrLen = arrLen + 1;
|
|
let arrOutPtr = Module._malloc(ptrLen * 4);
|
|
|
|
for (let i = 0; i < ptrLen; i++) {
|
|
setValue(arrOutPtr + (i * 4), 0, 'i32');
|
|
}
|
|
|
|
for (let i = 0; i < arrLen; i++) {
|
|
let len = Module.lengthBytesUTF8(arr[i]) + 1;
|
|
let outptr = Module._malloc(len);
|
|
for (let j = 0; j < len; j++) {
|
|
setValue(outptr + j, 0, 'i8');
|
|
}
|
|
Module.stringToUTF8(arr[i], outptr, len);
|
|
setValue(arrOutPtr + (i * 4), outptr, "i32");
|
|
}
|
|
setValue(pppOut, arrOutPtr, "i32")
|
|
},
|
|
quit: function() {
|
|
puter.exit();
|
|
}
|
|
}) |