Skip to content

Commit ec7badd

Browse files
committed
Mobile conntext menu
1 parent 5a6cd17 commit ec7badd

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

Build/src/main.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class HTMLChatApp {
6363
// Inline styles (including display:none, inline widths, etc)
6464
const styleString = node.getAttribute('style') || '';
6565
const width = node.getAttribute('width') || 16;
66-
66+
6767
const svgHTML = createIconHTML(iconName, {
6868
size: width,
6969
class: classString,
@@ -302,14 +302,53 @@ class HTMLChatApp {
302302
});
303303
}
304304

305+
addLongPressListener(target, callback, duration = 500) {
306+
let timer = null;
307+
let startX = 0, startY = 0;
308+
309+
target.addEventListener('touchstart', function (e) {
310+
if (e.touches.length !== 1) return;
311+
startX = e.touches[0].clientX;
312+
startY = e.touches[0].clientY;
313+
timer = setTimeout(() => {
314+
// Prevent system context menu (copy/paste/share)
315+
e.preventDefault();
316+
callback({
317+
clientX: startX,
318+
clientY: startY,
319+
preventDefault: () => e.preventDefault(),
320+
originalEvent: e
321+
}, target);
322+
}, duration);
323+
}, { passive: false });
324+
325+
['touchend', 'touchmove', 'touchcancel'].forEach(evt => {
326+
target.addEventListener(evt, function (e) {
327+
clearTimeout(timer);
328+
timer = null;
329+
});
330+
});
331+
332+
// Block browser's contextmenu (system) on this element on all platforms
333+
// (it does show up if you hold for longer (must mean I'm doing something wrong haha, but not complaining))
334+
target.addEventListener('contextmenu', function (e) {
335+
e.preventDefault();
336+
});
337+
}
338+
305339
attachMessageEventListeners() {
306-
// Add context menu event listeners for messages
307340
if (!this.elements.chatBox) return;
308341
const messages = this.elements.chatBox.querySelectorAll('.msg');
309342
messages.forEach(msgEl => {
343+
// Desktop: right-click (we show AND block default)
310344
msgEl.addEventListener('contextmenu', (e) => {
345+
e.preventDefault(); // Block browser's menu
311346
this.contextMenu.show(e, msgEl);
312347
});
348+
// Mobile: long-press (we show AND block default)
349+
this.addLongPressListener(msgEl, (touchEvent, el) => {
350+
this.contextMenu.show(touchEvent, el);
351+
});
313352
});
314353

315354
// Add click event listeners for reply references
@@ -776,7 +815,7 @@ class HTMLChatApp {
776815
this.elements.replyPreview.querySelector(
777816
".reply-text"
778817
).textContent = `${user}: ${text.substring(0, 50)}${text.length > 50 ? "..." : ""
779-
}`;
818+
}`;
780819
this.elements.input.focus();
781820
}
782821

0 commit comments

Comments
 (0)