From: Cyperghost Date: Thu, 19 Dec 2024 12:09:04 +0000 (+0100) Subject: Refresh quote list, if new quote added X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8dc0b62cea234d068258f6842ae81e31f19d710c;p=GitHub%2FWoltLab%2FWCF.git Refresh quote list, if new quote added --- diff --git a/ts/WoltLabSuite/Core/Component/Quote/List.ts b/ts/WoltLabSuite/Core/Component/Quote/List.ts index 83ff19aa22..9445fa9578 100644 --- a/ts/WoltLabSuite/Core/Component/Quote/List.ts +++ b/ts/WoltLabSuite/Core/Component/Quote/List.ts @@ -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; diff --git a/ts/WoltLabSuite/Core/Component/Quote/Storage.ts b/ts/WoltLabSuite/Core/Component/Quote/Storage.ts index 788894b304..8c75371ddb 100644 --- a/ts/WoltLabSuite/Core/Component/Quote/Storage.ts +++ b/ts/WoltLabSuite/Core/Component/Quote/Storage.ts @@ -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 index 0000000000..bb3051cc90 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/Author.js @@ -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 + * @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 index 0000000000..aed4fcbe42 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Api/Messages/RenderQuote.js @@ -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 + * @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); + } +}); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/List.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/List.js index 9cb14f1db3..a8e1c59bf3 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/List.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/List.js @@ -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
-
`) .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 index 0000000000..4de88f6177 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Quote/Storage.js @@ -0,0 +1,99 @@ +/** + * Stores the quote data. + * + * @author Olaf Braun + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License + * @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; + })); + } +});