Skip to content

Node.js based forum software built for the modern web

fix: rtl resizable issue, closes #13340

Barış Soner Uşaklı0594bff860cd
diff --git a/public/scss/chats.scss b/public/scss/chats.scss
index d61965088e..83954d5b55 100644
--- a/public/scss/chats.scss
+++ b/public/scss/chats.scss
@@ -121,12 +121,6 @@ body.page-user-chats {
}
.chat-modal {
- left: auto;
- top: auto;
- bottom: 0px;
- right: 2rem;
- width: auto!important;
- height: auto!important;
[component="chat/user/list/btn"], [component="chat/pinned/messages/btn"] {
display: none!important;
}
diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js
index be70ba0937..836d85ddf0 100644
--- a/public/src/modules/chat.js
+++ b/public/src/modules/chat.js
@@ -1,8 +1,8 @@
'use strict';
define('chat', [
- 'components', 'taskbar', 'translator', 'hooks', 'bootbox', 'alerts', 'api', 'scrollStop',
-], function (components, taskbar, translator, hooks, bootbox, alerts, api, scrollStop) {
+ 'components', 'taskbar', 'translator', 'hooks', 'bootbox', 'alerts', 'api', 'scrollStop', 'resizable',
+], function (components, taskbar, translator, hooks, bootbox, alerts, api, scrollStop, resizable) {
const Chat = {};
Chat.openChat = function (roomId, uid) {
@@ -10,9 +10,9 @@ define('chat', [
return alerts.error('[[error:not-logged-in]]');
}
- function loadAndCenter(chatModal) {
+ function loadAndPosition(chatModal) {
Chat.load(chatModal.attr('data-uuid'));
- Chat.center(chatModal);
+ Chat.position(chatModal);
Chat.focusInput(chatModal);
}
hooks.fire('filter:chat.openChat', {
@@ -24,7 +24,7 @@ define('chat', [
return ajaxify.go(`/chats/${roomId}`);
}
if (Chat.modalExists(roomId)) {
- loadAndCenter(Chat.getModal(roomId));
+ loadAndPosition(Chat.getModal(roomId));
} else {
api.get(`/chats/${roomId}`, {
uid: uid || app.user.uid,
@@ -34,7 +34,7 @@ define('chat', [
});
roomData.uid = uid || app.user.uid;
roomData.isSelf = true;
- Chat.createModal(roomData, loadAndCenter);
+ Chat.createModal(roomData, loadAndPosition);
}).catch(alerts.error);
}
});
@@ -369,7 +369,7 @@ define('chat', [
scrollStop.apply(chatModal.find('[component="chat/messages"] .chat-content'));
- Chat.center(chatModal);
+ Chat.position(chatModal);
makeModalResizeableDraggable(chatModal, uuid);
@@ -463,21 +463,22 @@ define('chat', [
};
function makeModalResizeableDraggable(chatModal, uuid) {
- app.loadJQueryUI(function () {
- chatModal.find('.modal-content').resizable({
- handles: 'n, e, s, w, se',
- minHeight: 250,
- minWidth: 400,
- });
+ resizable.init(chatModal.find('.modal-content'), {
+ minWidth: 400,
+ minHeight: 250,
+ handles: 'n, e, s, w, se, sw',
+ });
- chatModal.find('.modal-content').on('resize', function (event, ui) {
- if (ui.originalSize.height === ui.size.height) {
- return;
- }
+ chatModal.find('.modal-content').on('resize', function (event) {
+ const detail = event.originalEvent ? event.originalEvent.detail : event.detail;
+ if (!detail || detail.originalSize.height === detail.size.height) {
+ return;
+ }
- chatModal.find('.modal-body').css('height', Chat.calculateChatListHeight(chatModal));
- });
+ chatModal.find('.modal-body').css('height', Chat.calculateChatListHeight(chatModal));
+ });
+ app.loadJQueryUI(() => {
chatModal.draggable({
start: function () {
taskbar.updateActive(uuid);
@@ -518,18 +519,20 @@ define('chat', [
});
};
- Chat.center = function (chatModal) {
- const center = chatModal.attr('data-center');
- if (!center || center === 'false') {
- return;
- }
+ Chat.position = function (chatModal) {
let hideAfter = false;
if (chatModal.hasClass('hide')) {
chatModal.removeClass('hide');
hideAfter = true;
}
- chatModal.css('left', Math.max(0, (($(window).width() - $(chatModal).outerWidth()) / 2) + $(window).scrollLeft()) + 'px');
- chatModal.css('top', Math.max(0, ($(window).height() / 2) - ($(chatModal).outerHeight() / 2)) + 'px');
+ const $window = $(window);
+ const $chatContent = chatModal.find('.modal-content');
+ const width = $chatContent.outerWidth(true);
+ const height = $chatContent.outerHeight(true);
+ const isRtl = $('html').attr('data-dir') === 'rtl';
+
+ chatModal.css(isRtl ? 'right' : 'left', Math.max(0, ($window.width() - width)) + 'px');
+ chatModal.css('top', Math.max(0, ($window.height() - height)) + 'px');
if (hideAfter) {
chatModal.addClass('hide');
diff --git a/public/src/modules/resizable.js b/public/src/modules/resizable.js
new file mode 100644
index 0000000000..585a599ca8
--- /dev/null
+++ b/public/src/modules/resizable.js
@@ -0,0 +1,255 @@
+'use strict';
+
+/**
+ * Vanilla JS resizable module with jQuery UI-like handle behavior.
+ *
+ * Supports:
+ * - LTR and RTL layouts
+ * - Mouse and touch dragging
+ * - Selector strings and jQuery-wrapped elements in init/destroy
+ * - jQuery-style resize payload shape: detail.originalSize / detail.size
+ */
+
+
+const HANDLE_SIZE = 8;
+
+const defaultOptions = {
+ minWidth: 50,
+ minHeight: 50,
+ maxWidth: Infinity,
+ maxHeight: Infinity,
+ handles: 'se',
+};
+
+function clamp(value, min, max) {
+ return Math.min(Math.max(value, min), max);
+}
+
+function resolveElement(el) {
+ if (!el) {
+ return null;
+ }
+
+ if (typeof el === 'string') {
+ return document.querySelector(el);
+ }
+
+ if (el instanceof Element) {
+ return el;
+ }
+
+ if (typeof el === 'object' && el !== null && el[0] instanceof Element) {
+ return el[0];
+ }
+
+ return null;
+}
+
+function createHandle(el, position) {
+ const handle = document.createElement('div');
+ handle.className = `nbb-ui-resizable-handle nbb-ui-resizable-${position}`;
+ handle.setAttribute('data-resizable-handle', position);
+
+ Object.assign(handle.style, {
+ position: 'absolute',
+ zIndex: '90',
+ userSelect: 'none',
+ });
+
+ const s = `${HANDLE_SIZE}px`;
+ const half = `${HANDLE_SIZE / 2}px`;
+
+ switch (position) {
+ case 'n':
+ Object.assign(handle.style, { top: `-${half}`, left: '0', right: '0', height: s, cursor: 'n-resize' });
+ break;
+ case 's':
+ Object.assign(handle.style, { bottom: `-${half}`, left: '0', right: '0', height: s, cursor: 's-resize' });
+ break;
+ case 'e':
+ Object.assign(handle.style, { top: '0', bottom: '0', right: `-${half}`, width: s, cursor: 'e-resize' });
+ break;
+ case 'w':
+ Object.assign(handle.style, { top: '0', bottom: '0', left: `-${half}`, width: s, cursor: 'w-resize' });
+ break;
+ case 'ne':
+ Object.assign(handle.style, { top: `-${half}`, right: `-${half}`, width: s, height: s, cursor: 'ne-resize' });
+ break;
+ case 'nw':
+ Object.assign(handle.style, { top: `-${half}`, left: `-${half}`, width: s, height: s, cursor: 'nw-resize' });
+ break;
+ case 'se':
+ Object.assign(handle.style, { bottom: `-${half}`, right: `-${half}`, width: s, height: s, cursor: 'se-resize' });
+ break;
+ case 'sw':
+ Object.assign(handle.style, { bottom: `-${half}`, left: `-${half}`, width: s, height: s, cursor: 'sw-resize' });
+ break;
+ default:
+ return null;
+ }
+
+ el.appendChild(handle);
+ return handle;
+}
+
+function attachDrag(el, handle, position, opts) {
+ let startX = 0;
+ let startY = 0;
+ let startW = 0;
+ let startH = 0;
+ let startStyleLeft = 0;
+ let startStyleRight = 0;
+ let startStyleTop = 0;
+
+ function onStart(e) {
+ e.preventDefault();
+ const point = e.touches ? e.touches[0] : e;
+ startX = point.clientX;
+ startY = point.clientY;
+
+ // const rect = el.getBoundingClientRect();
+ startW = el.offsetWidth;
+ startH = el.offsetHeight;
+
+ // Read and write style offsets in the same coordinate space.
+ const styles = window.getComputedStyle(el);
+ startStyleLeft = parseFloat(styles.left) || 0;
+ startStyleRight = parseFloat(styles.right) || 0;
+ startStyleTop = parseFloat(styles.top) || 0;
+
+ document.addEventListener('mousemove', onMove);
+ document.addEventListener('touchmove', onMove, { passive: false });
+ document.addEventListener('mouseup', onEnd);
+ document.addEventListener('touchend', onEnd);
+
+ el.dispatchEvent(new CustomEvent('resizestart', { bubbles: true }));
+ }
+
+ function onMove(e) {
+ e.preventDefault();
+ const point = e.touches ? e.touches[0] : e;
+ const dx = point.clientX - startX;
+ const dy = point.clientY - startY;
+
+ let newW = startW;
+ let newH = startH;
+ let newLeft = startStyleLeft;
+ let newRight = startStyleRight;
+ let newTop = startStyleTop;
+
+ const affects = {
+ n: position === 'n' || position === 'ne' || position === 'nw',
+ s: position === 's' || position === 'se' || position === 'sw',
+ e: position === 'e' || position === 'ne' || position === 'se',
+ w: position === 'w' || position === 'nw' || position === 'sw',
+ };
+
+ // 1. Calculate intended dimensions
+ if (affects.s) newH = clamp(startH + dy, opts.minHeight, opts.maxHeight);
+ if (affects.n) newH = clamp(startH - dy, opts.minHeight, opts.maxHeight);
+ if (affects.e) newW = clamp(startW + dx, opts.minWidth, opts.maxWidth);
+ if (affects.w) newW = clamp(startW - dx, opts.minWidth, opts.maxWidth);
+
+ // 2. Apply dimensions FIRST so the browser can enforce constraints
+ el.style.width = `${newW}px`;
+ el.style.height = `${newH}px`;
+
+ // 3. Read back the ACTUAL dimensions rendered by the browser
+ const actualW = el.offsetWidth;
+ const actualH = el.offsetHeight;
+
+ // 4. Calculate positions based on ACTUAL browser sizes
+ if (affects.n) {
+ newTop = startStyleTop + (startH - actualH);
+ }
+
+ const isRtl = getComputedStyle(el).direction === 'rtl';
+
+ if (isRtl) {
+ if (affects.e) {
+ newRight = startStyleRight + (startW - actualW);
+ }
+ // If affects.w is true, newRight safely remains startStyleRight
+ el.style.right = `${newRight}px`;
+ el.style.left = ''; // Clear left to prevent layout conflicts in RTL
+ el.style.top = `${newTop}px`;
+ el.style.bottom = ''; // Clear bottom to prevent layout conflicts
+ } else {
+ if (affects.w) {
+ newLeft = startStyleLeft + (startW - actualW);
+ }
+ // If affects.e is true, newLeft safely remains startStyleLeft
+ el.style.left = `${newLeft}px`;
+ el.style.right = ''; // Clear right to prevent layout conflicts in LTR
+ el.style.top = `${newTop}px`;
+ el.style.bottom = ''; // Clear bottom to prevent layout conflicts
+ }
+
+ // 5. Dispatch event with the actual rendered sizes
+ el.dispatchEvent(new CustomEvent('resize', {
+ bubbles: true,
+ detail: {
+ originalSize: { width: startW, height: startH },
+ size: { width: actualW, height: actualH },
+ },
+ }));
+ }
+
+ function onEnd() {
+ document.removeEventListener('mousemove', onMove);
+ document.removeEventListener('touchmove', onMove, { passive: false });
+ document.removeEventListener('mouseup', onEnd);
+ document.removeEventListener('touchend', onEnd);
+
+ el.dispatchEvent(new CustomEvent('resizestop', { bubbles: true }));
+ }
+
+ handle.addEventListener('mousedown', onStart);
+ handle.addEventListener('touchstart', onStart, { passive: false });
+}
+
+export function init(el, options) {
+ const target = resolveElement(el);
+ if (!target) {
+ return;
+ }
+
+ const opts = Object.assign({}, defaultOptions, options || {});
+
+ const pos = getComputedStyle(target).position;
+ if (pos === 'static') {
+ target.style.position = 'relative';
+ }
+
+ if (target.dataset.resizable === 'true') {
+ return;
+ }
+
+ target.dataset.resizable = 'true';
+ target.classList.add('ui-resizable');
+
+ const handleKeys = opts.handles
+ .split(',')
+ .map((h) => h.trim().toLowerCase())
+ .filter(Boolean);
+
+ for (const position of handleKeys) {
+ const handle = createHandle(target, position);
+ if (!handle) {
+ continue;
+ }
+ attachDrag(target, handle, position, opts);
+ }
+}
+
+export function destroy(el) {
+ const target = resolveElement(el);
+ if (!target || target.dataset.resizable !== 'true') {
+ return;
+ }
+
+ target.querySelectorAll('[data-resizable-handle]').forEach((h) => h.remove());
+ target.classList.remove('ui-resizable');
+ delete target.dataset.resizable;
+}
+
diff --git a/src/views/chat.tpl b/src/views/chat.tpl
index 9952fe2ef1..682effc82e 100644
--- a/src/views/chat.tpl
+++ b/src/views/chat.tpl
@@ -1,6 +1,6 @@
-<div id="chat-modal" class="chat-modal d-flex flex-nowrap modal overflow-visible" tabindex="-1" role="dialog" aria-labelledby="chat-room-title-{roomId}" data-center="false">
- <div class="modal-dialog">
- <div class="modal-content" component="chat/message/window">
+<div id="chat-modal" class="chat-modal position-fixed pe-none d-flex flex-nowrap modal overflow-visible" tabindex="-1" role="dialog" aria-labelledby="chat-room-title-{roomId}">
+ <div class="modal-dialog m-0">
+ <div class="modal-content m-4" component="chat/message/window">
<div class="modal-header d-flex gap-4 justify-content-between">
<div id="chat-room-title-{roomId}" class="fs-6 flex-grow-1 fw-semibold tracking-tight text-truncate text-nowrap" component="chat/room/name" data-icon="{icon}">{{{ if ./roomName }}}<i class="fa {icon} text-muted"></i> {roomName}{{{ else }}}{./chatWithMessage}{{{ end}}}</div>
<div class="d-flex gap-1 align-items-center">