Refresh quote list, if new quote added
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 19 Dec 2024 12:09:04 +0000 (13:09 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 8 Jan 2025 16:25:19 +0000 (17:25 +0100)
ts/WoltLabSuite/Core/Component/Quote/List.ts
ts/WoltLabSuite/Core/Component/Quote/Storage.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/Author.js [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/RenderQuote.js [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/List.js
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/Storage.js [new file with mode: 0644]

index 83ff19aa222f53b1bae7372d81fe80793514f4a7..9445fa9578353b40e08b5afc652381c34404000b 100644 (file)
@@ -109,6 +109,12 @@ export function getQuoteList(editorId: string): QuoteList | undefined {
   return quoteLists.get(editorId);
 }
 
+export function refreshQuoteLists() {
+  for (const quoteList of quoteLists.values()) {
+    quoteList.renderQuotes();
+  }
+}
+
 export function setup(editorId: string): void {
   if (quoteLists.has(editorId)) {
     return;
index 788894b3046ccbb3ebc588cc2ee485e5bdf0e7c3..8c75371ddb8a1a9ae775428e0ae35ddb81f0b0b9 100644 (file)
@@ -11,6 +11,7 @@
 import * as Core from "WoltLabSuite/Core/Core";
 import { renderQuote } from "WoltLabSuite/Core/Api/Messages/RenderQuote";
 import { messageAuthor } from "WoltLabSuite/Core/Api/Messages/Author";
+import { refreshQuoteLists } from "WoltLabSuite/Core/Component/Quote/List";
 
 interface Message {
   objectID: number;
@@ -37,6 +38,8 @@ export async function saveQuote(objectType: string, objectId: number, objectClas
   }
 
   storeQuote(objectType, result.value, message);
+
+  refreshQuoteLists();
 }
 
 export async function saveFullQuote(objectType: string, objectClassName: string, objectId: number) {
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/Author.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/Author.js
new file mode 100644 (file)
index 0000000..bb3051c
--- /dev/null
@@ -0,0 +1,27 @@
+/**
+ * Requests render a full quote of a message.
+ *
+ * @author Olaf Braun
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @woltlabExcludeBundle tiny
+ */
+define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend", "../Result"], function (require, exports, Backend_1, Result_1) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.messageAuthor = messageAuthor;
+    async function messageAuthor(className, objectID) {
+        const url = new URL(window.WSC_RPC_API_URL + "core/messages/messageauthor");
+        url.searchParams.set("className", className);
+        url.searchParams.set("objectID", objectID.toString());
+        let response;
+        try {
+            response = (await (0, Backend_1.prepareRequest)(url).get().allowCaching().fetchAsJson());
+        }
+        catch (e) {
+            return (0, Result_1.apiResultFromError)(e);
+        }
+        return (0, Result_1.apiResultFromValue)(response);
+    }
+});
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/RenderQuote.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/RenderQuote.js
new file mode 100644 (file)
index 0000000..aed4fcb
--- /dev/null
@@ -0,0 +1,29 @@
+/**
+ * Requests render a full quote of a message.
+ *
+ * @author Olaf Braun
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @woltlabExcludeBundle tiny
+ */
+define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend", "../Result"], function (require, exports, Backend_1, Result_1) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.renderQuote = renderQuote;
+    async function renderQuote(objectType, className, objectID) {
+        const url = new URL(window.WSC_RPC_API_URL + "core/messages/renderquote");
+        url.searchParams.set("objectType", objectType);
+        url.searchParams.set("className", className);
+        url.searchParams.set("fullQuote", "true");
+        url.searchParams.set("objectID", objectID.toString());
+        let response;
+        try {
+            response = (await (0, Backend_1.prepareRequest)(url).get().fetchAsJson());
+        }
+        catch (e) {
+            return (0, Result_1.apiResultFromError)(e);
+        }
+        return (0, Result_1.apiResultFromValue)(response);
+    }
+});
index 9cb14f1db32f76cf4a03f598659f51b99d55af1f..a8e1c59bf3d50ffac936efbfc8ad72c574ddb51a 100644 (file)
@@ -11,6 +11,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Ckeditor/Eve
     "use strict";
     Object.defineProperty(exports, "__esModule", { value: true });
     exports.getQuoteList = getQuoteList;
+    exports.refreshQuoteLists = refreshQuoteLists;
     exports.setup = setup;
     Util_1 = tslib_1.__importDefault(Util_1);
     const quoteLists = new Map();
@@ -66,9 +67,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Ckeditor/Eve
   </span>
   
   <div class="jsQuote">
-  <label for="quote_{@$quoteID}">
     ${quote}
-  </label>
   </div>
 </li>`)
                     .join("")}
@@ -92,6 +91,11 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Ckeditor/Eve
     function getQuoteList(editorId) {
         return quoteLists.get(editorId);
     }
+    function refreshQuoteLists() {
+        for (const quoteList of quoteLists.values()) {
+            quoteList.renderQuotes();
+        }
+    }
     function setup(editorId) {
         if (quoteLists.has(editorId)) {
             return;
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/Storage.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/Storage.js
new file mode 100644 (file)
index 0000000..4de88f6
--- /dev/null
@@ -0,0 +1,99 @@
+/**
+ * Stores the quote data.
+ *
+ * @author Olaf Braun
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.2
+ * @woltlabExcludeBundle tiny
+ */
+define(["require", "exports", "tslib", "WoltLabSuite/Core/Core", "WoltLabSuite/Core/Api/Messages/RenderQuote", "WoltLabSuite/Core/Api/Messages/Author", "WoltLabSuite/Core/Component/Quote/List"], function (require, exports, tslib_1, Core, RenderQuote_1, Author_1, List_1) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.saveQuote = saveQuote;
+    exports.saveFullQuote = saveFullQuote;
+    exports.getQuotes = getQuotes;
+    exports.getMessage = getMessage;
+    Core = tslib_1.__importStar(Core);
+    const STORAGE_KEY = Core.getStoragePrefix() + "quotes";
+    async function saveQuote(objectType, objectId, objectClassName, message) {
+        const result = await (0, Author_1.messageAuthor)(objectClassName, objectId);
+        if (!result.ok) {
+            // TODO error handling
+            return;
+        }
+        storeQuote(objectType, result.value, message);
+        (0, List_1.refreshQuoteLists)();
+    }
+    async function saveFullQuote(objectType, objectClassName, objectId) {
+        const result = await (0, RenderQuote_1.renderQuote)(objectType, objectClassName, objectId);
+        if (!result.ok) {
+            // TODO error handling
+            return;
+        }
+        storeQuote(objectType, {
+            objectID: result.value.objectID,
+            time: result.value.time,
+            title: result.value.title,
+            link: result.value.link,
+            authorID: result.value.authorID,
+            author: result.value.author,
+            avatar: result.value.avatar,
+        }, result.value.message);
+    }
+    function storeQuote(objectType, message, quote) {
+        const storage = getStorage();
+        const key = getKey(objectType, message.objectID);
+        if (!storage.quotes.has(key)) {
+            storage.quotes.set(key, new Set());
+        }
+        storage.messages.set(key, message);
+        storage.quotes.get(key).add(quote);
+        saveStorage(storage);
+    }
+    function getQuotes() {
+        return getStorage().quotes;
+    }
+    function getMessage(objectType, objectId) {
+        const key = objectId ? getKey(objectType, objectId) : objectType;
+        return getStorage().messages.get(key);
+    }
+    function getStorage() {
+        const data = window.localStorage.getItem(STORAGE_KEY);
+        if (data === null) {
+            return {
+                quotes: new Map(),
+                messages: new Map(),
+            };
+        }
+        else {
+            return JSON.parse(data, (key, value) => {
+                if (key === "quotes") {
+                    const result = new Map(value);
+                    for (const [key, setValue] of result) {
+                        result.set(key, new Set(setValue));
+                    }
+                    return result;
+                }
+                else if (key === "messages") {
+                    return new Map(value);
+                }
+                return value;
+            });
+        }
+    }
+    function getKey(objectType, objectId) {
+        return `${objectType}:${objectId}`;
+    }
+    function saveStorage(data) {
+        window.localStorage.setItem(STORAGE_KEY, JSON.stringify(data, (key, value) => {
+            if (value instanceof Map) {
+                return Array.from(value.entries());
+            }
+            else if (value instanceof Set) {
+                return Array.from(value);
+            }
+            return value;
+        }));
+    }
+});