From 12041d076b89d6a57744457c3b283df4d24ea2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 1 Dec 2022 09:52:48 -0500 Subject: [PATCH] breaking: closes #11070, move quickreply to core --- public/src/client/topic.js | 11 +++- public/src/modules/quickreply.js | 95 ++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 public/src/modules/quickreply.js diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 48bb88eac4..a37b56d222 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -9,6 +9,7 @@ define('forum/topic', [ 'forum/topic/posts', 'navigator', 'sort', + 'quickreply', 'components', 'storage', 'hooks', @@ -16,7 +17,7 @@ define('forum/topic', [ 'alerts', ], function ( infinitescroll, threadTools, postTools, - events, posts, navigator, sort, + events, posts, navigator, sort, quickreply, components, storage, hooks, api, alerts ) { const Topic = {}; @@ -62,7 +63,7 @@ define('forum/topic', [ addDropupHandler(); addRepliesHandler(); addPostsPreviewHandler(); - + setupQuickReply(); handleBookmark(tid); $(window).on('scroll', utils.debounce(updateTopicTitle, 250)); @@ -292,6 +293,12 @@ define('forum/topic', [ }); } + function setupQuickReply() { + if (config.enableQuickReply) { + quickreply.init(); + } + } + function updateTopicTitle() { const span = components.get('navbar/title').find('span'); if ($(window).scrollTop() > 50 && span.hasClass('hidden')) { diff --git a/public/src/modules/quickreply.js b/public/src/modules/quickreply.js new file mode 100644 index 0000000000..b1f61d25a5 --- /dev/null +++ b/public/src/modules/quickreply.js @@ -0,0 +1,95 @@ +'use strict'; + +define('quickreply', [ + 'components', 'composer', 'composer/autocomplete', 'api', + 'alerts', 'uploadHelpers', 'mousetrap', +], function ( + components, composer, autocomplete, api, + alerts, uploadHelpers, mousetrap +) { + const QuickReply = {}; + + QuickReply.init = function () { + const element = components.get('topic/quickreply/text'); + const data = { + element: element, + strategies: [], + options: { + style: { + 'z-index': 100, + }, + }, + }; + + $(window).trigger('composer:autocomplete:init', data); + autocomplete._active.core_qr = autocomplete.setup(data); + + mousetrap.bind('ctrl+return', (e) => { + if (e.target === element.get(0)) { + components.get('topic/quickreply/button').get(0).click(); + } + }); + + uploadHelpers.init({ + dragDropAreaEl: $('[component="topic/quickreply/container"] .quickreply-message'), + pasteEl: element, + uploadFormEl: $('[component="topic/quickreply/upload"]'), + inputEl: element, + route: '/api/post/upload', + callback: function (uploads) { + let text = element.val(); + uploads.forEach((upload) => { + text = text + (text ? '\n' : '') + (upload.isImage ? '!' : '') + `[${upload.filename}](${upload.url})`; + }); + element.val(text); + }, + }); + + let ready = true; + components.get('topic/quickreply/button').on('click', function (e) { + e.preventDefault(); + if (!ready) { + return; + } + + var replyMsg = components.get('topic/quickreply/text').val(); + var replyData = { + tid: ajaxify.data.tid, + handle: undefined, + content: replyMsg, + }; + + ready = false; + api.post(`/topics/${ajaxify.data.tid}`, replyData, function (err, data) { + ready = true; + if (err) { + return alerts.error(err); + } + if (data && data.queued) { + alerts.alert({ + type: 'success', + title: '[[global:alert.success]]', + message: data.message, + timeout: 10000, + clickfn: function () { + ajaxify.go(`/post-queue/${data.id}`); + }, + }); + } + + components.get('topic/quickreply/text').val(''); + autocomplete._active.core_qr.hide(); + }); + }); + + components.get('topic/quickreply/expand').on('click', (e) => { + e.preventDefault(); + + const textEl = components.get('topic/quickreply/text'); + composer.newReply(ajaxify.data.tid, undefined, ajaxify.data.title, utils.escapeHTML(textEl.val())); + textEl.val(''); + }); + }; + + return QuickReply; +});