This commit is contained in:
retoor 2025-06-15 02:16:08 +02:00
parent 0dc75452d6
commit 213136b92e
5 changed files with 94 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import { app } from "./app.js";
import { NjetComponent } from "./njet.js";
import { NjetComponent,eventBus } from "./njet.js";
import { FileUploadGrid } from "./file-upload-grid.js";
class ChatInputComponent extends NjetComponent {
@ -179,17 +179,31 @@ class ChatInputComponent extends NjetComponent {
e.preventDefault();
this.fileUploadGrid.openFileDialog();
});
this.subscribe("file-uploading", (e) => {
eventBus.subscribe("file-uploading", (e) => {
this.fileUploadGrid.style.display = "block";
this.uploadButton.style.display = "none";
this.textarea.style.display = "none";
});
})
document.eventBus = eventBus;
this.appendChild(this.uploadButton);
this.textarea.addEventListener("blur", () => {
this.updateFromInput("");
});
eventBus.subscribe("file-uploads-done", (data)=>{
console.info("JEEJ", data)
this.textarea.style.display = "block";
this.uploadButton.style.display = "block";
this.fileUploadGrid.style.display = "none";
let message =data.reduce((file) => {
return `${message}[${file.filename}](/channel/attachment/${file.file})`;
}, '');
app.rpc.sendMessage(this.channelUid, message, true);
});
this.textarea.addEventListener("keyup", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
const message = this.replaceMentionsWithAuthors(this.value);

View File

@ -57,6 +57,7 @@ class FileUploadGrid extends NjetComponent {
}
reset(){
this.uploadResponses = [];
this.uploadsDone = 0;
this.uploadsStarted = 0;
this._grid.innerHTML = '';
@ -70,6 +71,7 @@ class FileUploadGrid extends NjetComponent {
this.reset()
this.uploadsDone = 0;
this.uploadsStarted = files.length;
[...files].forEach(file => this.createTile(file));
}
connectedCallback() {
@ -157,14 +159,20 @@ class FileUploadGrid extends NjetComponent {
progress.style.width = pct + '%';
this.publish('file-uploading', {file: file, tile: tile, progress: progress});
} else if (data.type === 'done') {
console.info("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH")
console.info("Done")
console.info(this.uploadResponses)
this.uploadsDone += 1;
this.publish('file-uploaded', {file: file, tile: tile, progress: progress});
progress.style.width = '100%';
tile.classList.add('fug-done');
console.info("Closed")
ws.close();
this.uploadResponses.push({file:file, remoteFile:data.file})
console.info(this.uploadsDone, this.uploadsStarted)
if(this.uploadsDone == this.uploadsStarted){
this.publish('file-uploads-done', this.uploadResponses);
}
this.reset()
}
};

View File

@ -210,19 +210,18 @@ class Njet extends HTMLElement {
customElements.define(name, component);
}
constructor(config = {}) {
constructor() {
super();
if (!Njet._root) {
Njet._root = this
Njet._rest = new RestClient({ baseURL: config.baseURL || null })
Njet._rest = new RestClient({ baseURL: '/' || null })
}
this.root._elements.push(this)
this.classList.add('njet');
this.config = config;
this.render.call(this);
this.initProps(config);
if (typeof this.construct === 'function')
this.construct.call(this)
//this.initProps(config);
//if (typeof this.config.construct === 'function')
// this.config.construct.call(this)
}
initProps(config) {
@ -284,9 +283,12 @@ class Njet extends HTMLElement {
render() {}
}
Njet.registerComponent('njet-root', Njet);
class Component extends Njet {}
Njet.registerComponent('njet-component', Component);
class NjetPanel extends Component {
render() {
this.innerHTML = '';
@ -491,18 +493,67 @@ document.body.appendChild(dialog);
*/
class NjetComponent extends Component {}
const njet = Njet;
const njet = Njet
njet.showDialog = function(args){
const dialog = new NjetDialog(args)
dialog.show()
return dialog
}
class EventBus extends EventTarget {
constructor() {
super();
this.eventMap = new Map();
}
subscribe(eventName, callback) {
this.addEventListener(eventName, callback);
if (!this.eventMap.has(eventName)) {
this.eventMap.set(eventName, []);
}
this.eventMap.get(eventName).push(callback);
}
publish(eventName, detail = {}) {
const event = new CustomEvent(eventName, {
detail,
bubbles: true,
cancelable: true
});
document.dispatchEvent(event);
}
unsubscribe(eventName, callback) {
this.removeEventListener(eventName, callback);
const subscribers = this.eventMap.get(eventName);
if (subscribers) {
const index = subscribers.indexOf(callback);
if (index > -1) subscribers.splice(index, 1);
}
}
}
const eventBus = new EventBus()
njet.showWindow = function(args) {
const w = new NjetWindow(args)
w.show()
return w
}
njet.publish = function(event, data) {
if (this.root._subscriptions[event]) {
this.root._subscriptions[event].forEach(callback => callback(data))
}
}
njet.subscribe = function(event, callback) {
if (!this.root._subscriptions[event]) {
this.root._subscriptions[event] = []
}
this.root._subscriptions[event].push(callback)
}
window.njet = njet
export { Njet, NjetButton, NjetPanel, NjetDialog, NjetGrid, NjetComponent, njet, NjetWindow };
export { Njet, NjetButton, NjetPanel, NjetDialog, NjetGrid, NjetComponent, njet, NjetWindow,eventBus };

View File

@ -206,7 +206,8 @@ class ChannelAttachmentUploadView(BaseView):
print(msg.json())
data = msg.json()
if data.get('type') == 'end':
await ws.send_json({"type": "done", "filename": filename})
relative_url = urllib.parse.quote(attachment_record["relative_url"])
await ws.send_json({"type": "done", "file": relative_url, "filename": filename})
elif msg.type == web.WSMsgType.ERROR:
break
return ws

View File

@ -269,6 +269,10 @@ class RPCView(BaseView):
async def send_message(self, channel_uid, message, is_final=True):
self._require_login()
message = await self.services.channel_message.get(channel_uid=channel_uid, user_uid=self.user_uid,is_final=False)
if message:
return await self.update_message_text(message["uid"], message)
message = await self.services.chat.send(
self.user_uid, channel_uid, message, is_final
)