Added new mooncore js dependencies. Added ace editor build
This commit is contained in:
24
Moonlight.Client/wwwroot/js/codeEditor.js
Normal file
24
Moonlight.Client/wwwroot/js/codeEditor.js
Normal file
@@ -0,0 +1,24 @@
|
||||
window.moonCoreCodeEditor = {
|
||||
instances: new Map(),
|
||||
attach: function (id, options) {
|
||||
const editor = ace.edit(id, options);
|
||||
|
||||
this.instances.set(id, editor);
|
||||
},
|
||||
updateOptions: function (id, options) {
|
||||
const editor = this.instances.get(id);
|
||||
|
||||
editor.setOptions(options);
|
||||
},
|
||||
getValue: function (id) {
|
||||
const editor = this.instances.get(id);
|
||||
|
||||
return editor.getValue();
|
||||
},
|
||||
destroy: function (id){
|
||||
const editor = this.instances.get(id);
|
||||
|
||||
editor.destroy();
|
||||
editor.container.remove();
|
||||
}
|
||||
}
|
||||
13
Moonlight.Client/wwwroot/js/downloadService.js
Normal file
13
Moonlight.Client/wwwroot/js/downloadService.js
Normal file
@@ -0,0 +1,13 @@
|
||||
window.moonCoreDownloadService = {
|
||||
download: async function (fileName, contentStreamReference) {
|
||||
const arrayBuffer = await contentStreamReference.arrayBuffer();
|
||||
const blob = new Blob([arrayBuffer]);
|
||||
const url = URL.createObjectURL(blob);
|
||||
const anchorElement = document.createElement('a');
|
||||
anchorElement.href = url;
|
||||
anchorElement.download = fileName ?? '';
|
||||
anchorElement.click();
|
||||
anchorElement.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
117
Moonlight.Client/wwwroot/js/fileManager.js
Normal file
117
Moonlight.Client/wwwroot/js/fileManager.js
Normal file
@@ -0,0 +1,117 @@
|
||||
window.moonCoreFileManager = {
|
||||
uploadCache: [],
|
||||
readCache: async function (index) {
|
||||
const item = this.uploadCache.at(index);
|
||||
const streamRef = await this.createStreamRef(item);
|
||||
|
||||
return {
|
||||
path: item.fullPath,
|
||||
streamRef: streamRef
|
||||
};
|
||||
},
|
||||
createStreamRef: async function (fileEntry) {
|
||||
const promise = new Promise(resolve => {
|
||||
fileEntry.file(file => {
|
||||
resolve(file);
|
||||
}, err => console.log(err));
|
||||
});
|
||||
|
||||
const processedFile = await promise;
|
||||
|
||||
// Prevent uploads of empty files
|
||||
if (processedFile.size <= 0) {
|
||||
console.log("Skipping upload of '" + fileEntry.fullPath + "' as its empty");
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileReader = new FileReader();
|
||||
|
||||
const readerPromise = new Promise(resolve => {
|
||||
fileReader.addEventListener("loadend", ev => {
|
||||
resolve(fileReader.result)
|
||||
});
|
||||
});
|
||||
|
||||
fileReader.readAsArrayBuffer(processedFile);
|
||||
|
||||
const arrayBuffer = await readerPromise;
|
||||
|
||||
return DotNet.createJSStreamReference(arrayBuffer);
|
||||
},
|
||||
setup: function (id, callbackRef) {
|
||||
// Check which features are supported by the browser
|
||||
const supportsFileSystemAccessAPI =
|
||||
'getAsFileSystemHandle' in DataTransferItem.prototype;
|
||||
const supportsWebkitGetAsEntry =
|
||||
'webkitGetAsEntry' in DataTransferItem.prototype;
|
||||
|
||||
// This is the drag and drop zone.
|
||||
const elem = document.getElementById(id);
|
||||
|
||||
// Prevent navigation.
|
||||
elem.addEventListener('dragover', (e) => {
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
elem.addEventListener('drop', async (e) => {
|
||||
// Prevent navigation.
|
||||
e.preventDefault();
|
||||
|
||||
if (!supportsFileSystemAccessAPI && !supportsWebkitGetAsEntry) {
|
||||
// Cannot handle directories.
|
||||
console.log("Cannot handle directories");
|
||||
return;
|
||||
}
|
||||
|
||||
this.getAllWebkitFileEntries(e.dataTransfer.items).then(async value => {
|
||||
this.uploadCache = value;
|
||||
await callbackRef.invokeMethodAsync("OnFilesDropped", this.uploadCache.length);
|
||||
});
|
||||
});
|
||||
},
|
||||
getAllWebkitFileEntries: async function (dataTransferItemList) {
|
||||
function readAllEntries(reader) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const entries = [];
|
||||
function readEntries() {
|
||||
reader.readEntries((batch) => {
|
||||
if (batch.length === 0) {
|
||||
resolve(entries);
|
||||
} else {
|
||||
entries.push(...batch);
|
||||
readEntries();
|
||||
}
|
||||
}, reject);
|
||||
}
|
||||
readEntries();
|
||||
});
|
||||
}
|
||||
|
||||
async function traverseEntry(entry) {
|
||||
if (entry.isFile) {
|
||||
return [entry];
|
||||
} else if (entry.isDirectory) {
|
||||
const reader = entry.createReader();
|
||||
const entries = await readAllEntries(reader);
|
||||
const subEntries = await Promise.all(entries.map(traverseEntry));
|
||||
return subEntries.flat();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
const entries = [];
|
||||
|
||||
// Convert DataTransferItemList to entries
|
||||
for (let i = 0; i < dataTransferItemList.length; i++) {
|
||||
const item = dataTransferItemList[i];
|
||||
const entry = item.webkitGetAsEntry();
|
||||
if (entry) {
|
||||
entries.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
// Traverse all entries and collect file entries
|
||||
const allFileEntries = await Promise.all(entries.map(traverseEntry));
|
||||
return allFileEntries.flat();
|
||||
}
|
||||
}
|
||||
25
Moonlight.Client/wwwroot/js/keyBinds.js
Normal file
25
Moonlight.Client/wwwroot/js/keyBinds.js
Normal file
@@ -0,0 +1,25 @@
|
||||
window.moonCoreKeyBinds = {
|
||||
storage: {},
|
||||
|
||||
registerHotkey: function (key, modifier, action, dotNetObjRef) {
|
||||
|
||||
const hotkeyListener = async (event) => {
|
||||
if (event.code === key && (!modifier || event[modifier + 'Key'])) {
|
||||
event.preventDefault();
|
||||
|
||||
await dotNetObjRef.invokeMethodAsync("OnHotkeyPressed", action);
|
||||
}
|
||||
};
|
||||
|
||||
this.storage[`${key}${modifier}`] = hotkeyListener;
|
||||
window.addEventListener('keydown', hotkeyListener);
|
||||
},
|
||||
|
||||
unregisterHotkey: function (key, modifier) {
|
||||
const listenerKey = `${key}${modifier}`;
|
||||
if (this.storage[listenerKey]) {
|
||||
window.removeEventListener('keydown', this.storage[listenerKey]);
|
||||
delete this.storage[listenerKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,18 +35,5 @@ window.moonlight = {
|
||||
|
||||
(document.head || document.documentElement).appendChild(scriptElement);
|
||||
}
|
||||
},
|
||||
utils: {
|
||||
download: async function (fileName, contentStreamReference) {
|
||||
const arrayBuffer = await contentStreamReference.arrayBuffer();
|
||||
const blob = new Blob([arrayBuffer]);
|
||||
const url = URL.createObjectURL(blob);
|
||||
const anchorElement = document.createElement('a');
|
||||
anchorElement.href = url;
|
||||
anchorElement.download = fileName ?? '';
|
||||
anchorElement.click();
|
||||
anchorElement.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user