From 213136b92e6dbbc2af4f52e616bc5ccf63c5ba2b Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 15 Jun 2025 02:16:08 +0200 Subject: [PATCH] Update. --- src/snek/static/chat-input.js | 20 ++++++-- src/snek/static/file-upload-grid.js | 12 ++++- src/snek/static/njet.js | 71 +++++++++++++++++++++++++---- src/snek/view/channel.py | 3 +- src/snek/view/rpc.py | 4 ++ 5 files changed, 94 insertions(+), 16 deletions(-) diff --git a/src/snek/static/chat-input.js b/src/snek/static/chat-input.js index 45c63d7..0b1d760 100644 --- a/src/snek/static/chat-input.js +++ b/src/snek/static/chat-input.js @@ -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); diff --git a/src/snek/static/file-upload-grid.js b/src/snek/static/file-upload-grid.js index 32669a7..730f718 100644 --- a/src/snek/static/file-upload-grid.js +++ b/src/snek/static/file-upload-grid.js @@ -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() } }; diff --git a/src/snek/static/njet.js b/src/snek/static/njet.js index 65aee05..9223942 100644 --- a/src/snek/static/njet.js +++ b/src/snek/static/njet.js @@ -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 }; diff --git a/src/snek/view/channel.py b/src/snek/view/channel.py index 604725a..e2efe12 100644 --- a/src/snek/view/channel.py +++ b/src/snek/view/channel.py @@ -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 diff --git a/src/snek/view/rpc.py b/src/snek/view/rpc.py index 0b5ce96..1c6544b 100644 --- a/src/snek/view/rpc.py +++ b/src/snek/view/rpc.py @@ -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 )