Make context menu look nicer and handle rendering using v-if #58

Merged
sauceyred merged 12 commits from improved-context-menu into main 2025-08-04 01:55:47 +00:00
12 changed files with 96 additions and 45 deletions

10
app.vue
View file

@ -1,15 +1,21 @@
<template> <template>
<div> <div>
<Banner v-if="banner" /> <Banner v-if="banner" />
<ContextMenu v-if="contextMenu && contextMenu.show" :pointer-x="contextMenu.pointerX" :pointer-y="contextMenu.pointerY" :menu-items="contextMenu.items" />
<NuxtPage :keepalive="true" /> <NuxtPage :keepalive="true" />
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import ContextMenu from '~/components/UserInterface/ContextMenu.vue';
import { render } from 'vue';
import type { ContextMenuInterface } from './types/interfaces';
import loadPreferredTheme from '~/utils/loadPreferredTheme'; import loadPreferredTheme from '~/utils/loadPreferredTheme';
const banner = useState("banner", () => false); const banner = useState("banner", () => false);
const contextMenu = useState<ContextMenuInterface>("contextMenu");
onMounted(() => { onMounted(() => {
loadPreferredTheme() loadPreferredTheme()
@ -19,11 +25,11 @@ onMounted(() => {
contextMenuHandler(e); contextMenuHandler(e);
}); });
document.addEventListener("mousedown", (e) => { document.addEventListener("mousedown", (e) => {
if (e.target instanceof HTMLDivElement && e.target.closest("#context-menu")) return; if (e.target instanceof HTMLElement && e.target.closest("#context-menu")) return;
console.log("click"); console.log("click");
console.log("target:", e.target); console.log("target:", e.target);
console.log(e.target instanceof HTMLDivElement); console.log(e.target instanceof HTMLDivElement);
removeContextMenu(); removeContextMenu(contextMenu);
if (e.target instanceof HTMLElement && e.target.classList.contains("message-text") && e.target.contentEditable) { if (e.target instanceof HTMLElement && e.target.classList.contains("message-text") && e.target.contentEditable) {
e.target.contentEditable = "false"; e.target.contentEditable = "false";
} }

View file

@ -1,5 +1,5 @@
<template> <template>
<div v-if="props.type == 'normal' || props.replyMessage" ref="messageElement" @contextmenu="createContextMenu($event, menuItems)" :id="props.last ? 'last-message' : undefined" <div v-if="props.type == 'normal' || props.replyMessage" ref="messageElement" @contextmenu="showContextMenu($event, contextMenu, menuItems)" :id="props.last ? 'last-message' : undefined"
class="message normal-message" :class="{ 'mentioned': (props.replyMessage || props.isMentioned) && props.message.user.uuid != props.me.uuid && props.replyMessage?.user.uuid == props.me.uuid }" :data-message-id="props.messageId" class="message normal-message" :class="{ 'mentioned': (props.replyMessage || props.isMentioned) && props.message.user.uuid != props.me.uuid && props.replyMessage?.user.uuid == props.me.uuid }" :data-message-id="props.messageId"
:editing.sync="props.editing" :replying-to.sync="props.replyingTo"> :editing.sync="props.editing" :replying-to.sync="props.replyingTo">
<div v-if="props.replyMessage" class="message-reply-svg"> <div v-if="props.replyMessage" class="message-reply-svg">
@ -47,7 +47,7 @@
</div> </div>
<MessageMedia v-if="mediaLinks.length" :links="mediaLinks" /> <MessageMedia v-if="mediaLinks.length" :links="mediaLinks" />
</div> </div>
<div v-else ref="messageElement" @contextmenu="createContextMenu($event, menuItems)" :id="props.last ? 'last-message' : undefined" <div v-else ref="messageElement" @contextmenu="showContextMenu($event, contextMenu, menuItems)" :id="props.last ? 'last-message' : undefined"
class="message grouped-message" :class="{ 'message-margin-bottom': props.marginBottom, 'mentioned': props.replyMessage || props.isMentioned }" class="message grouped-message" :class="{ 'message-margin-bottom': props.marginBottom, 'mentioned': props.replyMessage || props.isMentioned }"
:data-message-id="props.messageId" :editing.sync="props.editing" :replying-to.sync="props.replyingTo"> :data-message-id="props.messageId" :editing.sync="props.editing" :replying-to.sync="props.replyingTo">
<div class="left-column"> <div class="left-column">
@ -68,9 +68,12 @@ import { parse } from 'marked';
import type { MessageProps } from '~/types/props'; import type { MessageProps } from '~/types/props';
import MessageMedia from './MessageMedia.vue'; import MessageMedia from './MessageMedia.vue';
import MessageReply from './UserInterface/MessageReply.vue'; import MessageReply from './UserInterface/MessageReply.vue';
import type { ContextMenuInterface, ContextMenuItem } from '~/types/interfaces';
const props = defineProps<MessageProps>(); const props = defineProps<MessageProps>();
const contextMenu = useState<ContextMenuInterface>("contextMenu", () => ({ show: false, pointerX: 0, pointerY: 0, items: [] }));
const messageElement = ref<HTMLDivElement>(); const messageElement = ref<HTMLDivElement>();
const dateHidden = ref<boolean>(true); const dateHidden = ref<boolean>(true);
@ -139,13 +142,19 @@ console.log("media links:", mediaLinks);
// showHover.value = !showHover.value; // showHover.value = !showHover.value;
//} //}
const menuItems = [ const menuItems: ContextMenuItem[] = [
{ name: "Reply", callback: () => { if (messageElement.value) replyToMessage(messageElement.value, props) } } { name: "Reply", icon: "lucide:reply", type: "normal", callback: () => { if (messageElement.value) replyToMessage(messageElement.value, props) } }
] ]
console.log("me:", props.me); console.log("me:", props.me);
if (props.author?.uuid == props.me.uuid) { if (props.author?.uuid == props.me.uuid) {
menuItems.push({ name: "Edit", callback: () => { if (messageElement.value) editMessage(messageElement.value, props) } }); // Inserts "edit" option at index 1 (below the "reply" option)
menuItems.splice(1, 0, { name: "Edit (WIP)", icon: "lucide:square-pen", type: "normal", callback: () => { /* if (messageElement.value) editMessage(messageElement.value, props) */ } });
}
if (props.author?.uuid == props.me.uuid /* || check message delete permission*/) {
// Inserts "edit" option at index 2 (below the "edit" option)
menuItems.splice(2, 0, { name: "Delete (WIP)", icon: "lucide:trash", type: "danger", callback: () => {} });
} }
function getDayDifference(date1: Date, date2: Date) { function getDayDifference(date1: Date, date2: Date) {

View file

@ -1,11 +1,15 @@
<template> <template>
<div v-for="item of props.menuItems" class="context-menu-item" @click="runCallback(item)"> <div id="context-menu">
{{ item.name }} <button v-for="item of props.menuItems" class="context-menu-item"
:class="'context-menu-item-' + item.type"
@click="runCallback(item)">
{{ item.name }} <Icon v-if="item.icon" :name="item.icon" />
</button>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import type { ContextMenuItem } from '~/types/interfaces'; import type { ContextMenuInterface, ContextMenuItem } from '~/types/interfaces';
const props = defineProps<{ menuItems: ContextMenuItem[], pointerX: number, pointerY: number }>(); const props = defineProps<{ menuItems: ContextMenuItem[], pointerX: number, pointerY: number }>();
@ -19,7 +23,8 @@ onMounted(() => {
function runCallback(item: ContextMenuItem) { function runCallback(item: ContextMenuItem) {
removeContextMenu(); const contextMenu = useState<ContextMenuInterface>("contextMenu");
removeContextMenu(contextMenu);
item.callback(); item.callback();
} }
@ -31,14 +36,33 @@ function runCallback(item: ContextMenuItem) {
position: absolute; position: absolute;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
width: 10dvw; width: 10rem;
border: .15rem solid cyan; border: .1rem solid var(--reply-text-color);
background-color: var(--background-color); background-color: var(--sidebar-highlighted-background-color);
text-align: center; text-align: left;
z-index: 100;
}
.context-menu-item {
display: flex;
align-items: center;
justify-content: space-between;
height: 2rem;
width: 100%;
color: var(--text-color);
background-color: var(--sidebar-highlighted-background-color);
border: none;
text-align: left;
padding-left: 1rem;
padding-right: 1rem;
} }
.context-menu-item:hover { .context-menu-item:hover {
background-color: rgb(50, 50, 50); background-color: rgb(50, 50, 50);
} }
.context-menu-item-danger {
color: var(--danger-text-color);
}
</style> </style>

View file

@ -21,7 +21,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import type { ContextMenuItem } from '~/types/interfaces'; import type { ContextMenuInterface, ContextMenuItem } from '~/types/interfaces';
const props = defineProps<{ width?: string, minWidth: string, maxWidth: string, borderSides: "all" | "top" | "right" | "bottom" | "left" | ("top" | "right" | "bottom" | "left")[], localStorageName?: string }>(); const props = defineProps<{ width?: string, minWidth: string, maxWidth: string, borderSides: "all" | "top" | "right" | "bottom" | "left" | ("top" | "right" | "bottom" | "left")[], localStorageName?: string }>();
@ -31,6 +31,8 @@ const resizableSidebar = ref<HTMLDivElement>();
const widthResizer = ref<HTMLDivElement>(); const widthResizer = ref<HTMLDivElement>();
const storedWidth = ref<string>(); const storedWidth = ref<string>();
const contextMenu = useState<ContextMenuInterface>("contextMenu");
const menuItems: ContextMenuItem[] = [ const menuItems: ContextMenuItem[] = [
{ name: "Reset", callback: () => { { name: "Reset", callback: () => {
const defaultWidth = props.width ?? props.minWidth; const defaultWidth = props.width ?? props.minWidth;
@ -48,7 +50,7 @@ onMounted(() => {
widthResizer.value.addEventListener("pointerdown", (e) => { widthResizer.value.addEventListener("pointerdown", (e) => {
e.preventDefault(); e.preventDefault();
if (e.button == 2) { if (e.button == 2) {
createContextMenu(e, menuItems); showContextMenu(e, contextMenu.value, menuItems);
return return
}; };
document.body.style.cursor = "ew-resize"; document.body.style.cursor = "ew-resize";

View file

@ -2,6 +2,7 @@
--text-color: #f0e5e0; --text-color: #f0e5e0;
--secondary-text-color: #e8e0db; --secondary-text-color: #e8e0db;
--reply-text-color: #969696; --reply-text-color: #969696;
--danger-text-color: #ff0000;
--chat-background-color: #2f2e2d; --chat-background-color: #2f2e2d;
--chat-highlighted-background-color: #3f3b38; --chat-highlighted-background-color: #3f3b38;

View file

@ -2,6 +2,7 @@
--text-color: #f7eee8; --text-color: #f7eee8;
--secondary-text-color: #f0e8e4; --secondary-text-color: #f0e8e4;
--reply-text-color: #969696; --reply-text-color: #969696;
--danger-text-color: #ff0000;
--chat-background-color: #1f1e1d; --chat-background-color: #1f1e1d;
--chat-highlighted-background-color: #2f2b28; --chat-highlighted-background-color: #2f2b28;

View file

@ -2,6 +2,7 @@
--text-color: #170f08; --text-color: #170f08;
--secondary-text-color: #2f2b28; --secondary-text-color: #2f2b28;
--reply-text-color: #969696; --reply-text-color: #969696;
--danger-text-color: #ff0000;
--chat-background-color: #f0ebe8; --chat-background-color: #f0ebe8;
--chat-highlighted-background-color: #e8e4e0; --chat-highlighted-background-color: #e8e4e0;

View file

@ -2,6 +2,7 @@
--text-color: #161518; --text-color: #161518;
--secondary-text-color: #2b2930; --secondary-text-color: #2b2930;
--reply-text-color: #969696; --reply-text-color: #969696;
--danger-text-color: #ff0000;
--chat-background-color: #80808000; --chat-background-color: #80808000;
--chat-highlighted-background-color: #ffffff20; --chat-highlighted-background-color: #ffffff20;

View file

@ -101,5 +101,14 @@ export interface ModalProps {
export interface ContextMenuItem { export interface ContextMenuItem {
name: string, name: string,
icon?: string,
type: "normal" | "danger"
callback: (...args: any[]) => any; callback: (...args: any[]) => any;
} }
export interface ContextMenuInterface {
show: boolean,
pointerX: number,
pointerY: number,
items: ContextMenuItem[]
}

View file

@ -1,21 +0,0 @@
import { render } from "vue";
import ContextMenu from "~/components/UserInterface/ContextMenu.vue";
import type { ContextMenuItem } from "~/types/interfaces";
export default (e: PointerEvent | MouseEvent, menuItems: ContextMenuItem[]) => {
console.log("Rendering new context menu");
const menuContainer = document.createElement("div");
console.log("hello");
menuContainer.id = "context-menu";
document.body.appendChild(menuContainer);
console.log("pointer x:", e.clientX);
console.log("pointer y:", e.clientY);
console.log("menu items:", menuItems);
const contextMenu = h(ContextMenu, {
menuItems,
pointerX: e.clientX,
pointerY: e.clientY
});
render(contextMenu, menuContainer);
console.log("Rendered");
}

View file

@ -1,6 +1,12 @@
export default () => { import type { ContextMenuInterface } from "~/types/interfaces";
const contextMenu = document.getElementById("context-menu");
if (contextMenu) { export default (contextMenu: Ref<ContextMenuInterface>) => {
contextMenu.remove(); console.log("resetting and hiding context menu");
contextMenu.value = {
show: false,
pointerX: 0,
pointerY: 0,
items: []
} }
console.log("hidden context menu");
} }

12
utils/showContextMenu.ts Normal file
View file

@ -0,0 +1,12 @@
import { render } from "vue";
import ContextMenu from "~/components/UserInterface/ContextMenu.vue";
import type { ContextMenuInterface, ContextMenuItem } from "~/types/interfaces";
export default (e: MouseEvent | PointerEvent, contextMenu: ContextMenuInterface, menuItems: ContextMenuItem[]) => {
console.log("Showing context menu");
contextMenu.show = true;
contextMenu.pointerX = e.clientX;
contextMenu.pointerY = e.clientY;
contextMenu.items = menuItems;
console.log("Showed");
}