Open create submenu beside context menu and distinguish rename icons
This commit is contained in:
1 parent
c05293a948
commit
1afa755d4f
9 files changed
+193
-26
No files matched your search
@@ -17,7 +17,8 @@
|
||||
</Teleport>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, nextTick, onBeforeUnmount } from "vue";
|
||||
import { ref, watch, nextTick, onBeforeUnmount, provide, useId } from "vue";
|
||||
import { contextMenuKey } from "@/utils/context-menu";
|
||||
const emit = defineEmits(["hide"]);
|
||||
const props = defineProps<{ show: boolean; pos: { x: number; y: number } }>();
|
||||
const contextMenu = ref<HTMLElement | null>(null),
|
||||
@@ -25,6 +26,8 @@ const contextMenu = ref<HTMLElement | null>(null),
|
||||
top = ref(0);
|
||||
let previousFocus: HTMLElement | null = null;
|
||||
const hide = () => emit("hide");
|
||||
const menuId = useId();
|
||||
provide(contextMenuKey, { id: menuId, close: hide });
|
||||
function menuClick(event: MouseEvent) {
|
||||
if (
|
||||
event.target instanceof Element &&
|
||||
@@ -50,6 +53,13 @@ async function schedulePosition() {
|
||||
position();
|
||||
}
|
||||
function outside(event: Event) {
|
||||
if (
|
||||
event.target instanceof Element &&
|
||||
event.target
|
||||
.closest("[data-context-owner]")
|
||||
?.getAttribute("data-context-owner") === menuId
|
||||
)
|
||||
return;
|
||||
if (
|
||||
event.target instanceof Node &&
|
||||
!contextMenu.value?.contains(event.target)
|
||||
|
||||
@@ -122,7 +122,7 @@
|
||||
role="menuitem"
|
||||
to="/settings/profile"
|
||||
@click="navigate"
|
||||
><AppIcon name="settings" />{{
|
||||
><AppIcon name="settings" />{{
|
||||
t("sidebar.settings")
|
||||
}}</router-link
|
||||
>
|
||||
|
||||
@@ -9,34 +9,127 @@
|
||||
@keydown.right.stop.prevent="expand"
|
||||
>
|
||||
<AppIcon name="add" /><span>{{ t("buttons.create") }}</span
|
||||
><AppIcon :name="expanded ? 'expand_more' : 'chevron_right'" />
|
||||
><AppIcon name="chevron_right" />
|
||||
</button>
|
||||
<div
|
||||
v-if="expanded"
|
||||
ref="submenu"
|
||||
class="context-submenu"
|
||||
role="group"
|
||||
:aria-label="t('buttons.create')"
|
||||
@keydown.left.stop.prevent="collapse"
|
||||
>
|
||||
<CreateOptions />
|
||||
</div>
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="expanded"
|
||||
ref="submenu"
|
||||
class="context-menu context-submenu"
|
||||
role="menu"
|
||||
:data-context-owner="owner?.id"
|
||||
:aria-label="t('buttons.create')"
|
||||
:style="{ left: left + 'px', top: top + 'px' }"
|
||||
@click.stop="choose"
|
||||
@contextmenu.prevent.stop
|
||||
@keydown.stop="keyboard"
|
||||
>
|
||||
<button
|
||||
v-if="compact"
|
||||
type="button"
|
||||
class="action"
|
||||
@click.stop="collapse"
|
||||
>
|
||||
<AppIcon name="arrow_back" /><span>{{ t("buttons.create") }}</span>
|
||||
</button>
|
||||
<CreateOptions />
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { nextTick, ref } from "vue";
|
||||
import { inject, nextTick, onBeforeUnmount, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { contextMenuKey, submenuPosition } from "@/utils/context-menu";
|
||||
import CreateOptions from "./CreateOptions.vue";
|
||||
const { t } = useI18n(),
|
||||
expanded = ref(false),
|
||||
submenu = ref<HTMLElement>(),
|
||||
owner = inject(contextMenuKey);
|
||||
const expanded = ref(false),
|
||||
compact = ref(false),
|
||||
left = ref(0),
|
||||
top = ref(0);
|
||||
const submenu = ref<HTMLElement>(),
|
||||
trigger = ref<HTMLElement>();
|
||||
function position() {
|
||||
if (!submenu.value || !trigger.value) return;
|
||||
const parent = trigger.value
|
||||
.closest(".context-menu")
|
||||
?.getBoundingClientRect();
|
||||
if (!parent) return;
|
||||
const row = trigger.value.getBoundingClientRect(),
|
||||
panel = submenu.value.getBoundingClientRect();
|
||||
compact.value = window.innerWidth < parent.width + panel.width + 24;
|
||||
const coordinates = submenuPosition(
|
||||
{ left: parent.left, right: parent.right, top: row.top - 6 },
|
||||
panel.width,
|
||||
panel.height,
|
||||
{ width: window.innerWidth, height: window.innerHeight }
|
||||
);
|
||||
left.value = compact.value
|
||||
? Math.max(8, Math.min(parent.left, window.innerWidth - panel.width - 8))
|
||||
: coordinates.left;
|
||||
top.value = coordinates.top;
|
||||
}
|
||||
async function expand() {
|
||||
expanded.value = true;
|
||||
await nextTick();
|
||||
submenu.value?.querySelector("button")?.focus();
|
||||
position();
|
||||
submenu.value?.querySelector("button")?.focus({ preventScroll: true });
|
||||
}
|
||||
function collapse() {
|
||||
expanded.value = false;
|
||||
trigger.value?.focus();
|
||||
trigger.value?.focus({ preventScroll: true });
|
||||
}
|
||||
function choose(event: MouseEvent) {
|
||||
if (!(event.target instanceof Element) || !event.target.closest("button"))
|
||||
return;
|
||||
expanded.value = false;
|
||||
owner?.close();
|
||||
}
|
||||
function keyboard(event: KeyboardEvent) {
|
||||
if (event.key === "Escape" || event.key === "ArrowLeft") {
|
||||
event.preventDefault();
|
||||
collapse();
|
||||
return;
|
||||
}
|
||||
if (event.key === "Tab") {
|
||||
expanded.value = false;
|
||||
owner?.close();
|
||||
return;
|
||||
}
|
||||
if (!["ArrowUp", "ArrowDown", "Home", "End"].includes(event.key)) return;
|
||||
event.preventDefault();
|
||||
const buttons = Array.from(
|
||||
submenu.value?.querySelectorAll<HTMLButtonElement>(
|
||||
"button:not(:disabled)"
|
||||
) || []
|
||||
);
|
||||
if (!buttons.length) return;
|
||||
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
||||
const next =
|
||||
event.key === "Home"
|
||||
? 0
|
||||
: event.key === "End"
|
||||
? buttons.length - 1
|
||||
: (index + (event.key === "ArrowUp" ? -1 : 1) + buttons.length) %
|
||||
buttons.length;
|
||||
buttons[next].focus();
|
||||
}
|
||||
function scroll(event: Event) {
|
||||
if (!(event.target instanceof Node) || !submenu.value?.contains(event.target))
|
||||
position();
|
||||
}
|
||||
watch(expanded, async (open) => {
|
||||
window.removeEventListener("scroll", scroll, true);
|
||||
if (!open) return;
|
||||
await nextTick();
|
||||
if (!expanded.value) return;
|
||||
position();
|
||||
await nextTick();
|
||||
position();
|
||||
submenu.value
|
||||
?.querySelectorAll("button")
|
||||
.forEach((button) => button.setAttribute("role", "menuitem"));
|
||||
window.addEventListener("scroll", scroll, true);
|
||||
});
|
||||
onBeforeUnmount(() => window.removeEventListener("scroll", scroll, true));
|
||||
</script>
|
||||
@@ -109,10 +109,13 @@
|
||||
min-height: 42px;
|
||||
}
|
||||
}
|
||||
.context-submenu {
|
||||
margin: 4px 0 6px 12px;
|
||||
padding-inline-start: 8px;
|
||||
border-inline-start: 1px solid var(--borderPrimary);
|
||||
.context-menu.context-submenu {
|
||||
margin: 0;
|
||||
padding: 6px;
|
||||
border: 1px solid var(--borderPrimary);
|
||||
width: 260px;
|
||||
min-width: 0;
|
||||
z-index: 12001;
|
||||
}
|
||||
.create-type-options {
|
||||
padding: 8px 14px 16px;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { submenuPosition } from "../context-menu";
|
||||
import { iconPath } from "../icons";
|
||||
it("opens beside the parent and flips left at the screen edge", () => {
|
||||
const viewport = { width: 1000, height: 700 };
|
||||
expect(
|
||||
submenuPosition({ left: 100, right: 340, top: 80 }, 260, 400, viewport)
|
||||
).toEqual({ left: 344, top: 80 });
|
||||
expect(
|
||||
submenuPosition({ left: 700, right: 940, top: 80 }, 260, 400, viewport)
|
||||
).toEqual({ left: 436, top: 80 });
|
||||
});
|
||||
it("keeps the panel in the viewport near the bottom and on narrow screens", () => {
|
||||
expect(
|
||||
submenuPosition({ left: 100, right: 340, top: 600 }, 260, 400, {
|
||||
width: 1000,
|
||||
height: 700,
|
||||
})
|
||||
).toEqual({ left: 344, top: 292 });
|
||||
const position = submenuPosition(
|
||||
{ left: 8, right: 260, top: -10 },
|
||||
260,
|
||||
400,
|
||||
{ width: 320, height: 600 }
|
||||
);
|
||||
expect(position).toEqual({ left: 8, top: 8 });
|
||||
});
|
||||
it("distinguishes rename from editing file contents", () => {
|
||||
expect(iconPath("mode_edit")).not.toEqual(iconPath("edit_note"));
|
||||
expect(iconPath("edit_note")).not.toEqual(iconPath("file"));
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { InjectionKey } from "vue";
|
||||
export const contextMenuKey: InjectionKey<{ id: string; close: () => void }> =
|
||||
Symbol("context-menu");
|
||||
|
||||
export function submenuPosition(
|
||||
parent: { left: number; right: number; top: number },
|
||||
width: number,
|
||||
height: number,
|
||||
viewport: { width: number; height: number }
|
||||
) {
|
||||
const gap = 4,
|
||||
margin = 8;
|
||||
const preferred =
|
||||
parent.right + gap + width <= viewport.width - margin
|
||||
? parent.right + gap
|
||||
: parent.left - gap - width;
|
||||
return {
|
||||
left: Math.max(
|
||||
margin,
|
||||
Math.min(preferred, viewport.width - width - margin)
|
||||
),
|
||||
top: Math.max(
|
||||
margin,
|
||||
Math.min(parent.top, viewport.height - height - margin)
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -16,6 +16,9 @@ const paths: Record<string, string> = {
|
||||
upload: "M12 16V4 m-5 5 5-5 5 5 M4 16v5h16v-5",
|
||||
trash: "M3 6h18 M9 6V3h6v3 M5 6l1 15h12l1-15 M10 10v7 M14 10v7",
|
||||
edit: "m15 4 5 5 M4 20l5-1L21 7l-5-5L4 14z",
|
||||
rename: "M3 7h7 M3 12h5 M3 17h7 M13 3h7 M16.5 3v18 M13 21h7",
|
||||
editDocument:
|
||||
"M5 3h9l5 5v3 M14 3v5h5 M5 3v18h6 M8 11h4 M8 15h2 m3 6 1-4 6-6 3 3-6 6z",
|
||||
copy: "M8 8h12v13H8z M4 16H2V2h13v2",
|
||||
paste: "M9 4H5v17h14V4h-4 M9 2h6v5H9z M8 11h8 M8 15h8",
|
||||
cut: "M9 8 20 20 M9 16 20 4 M9 7a3 3 0 1 1-6 0 3 3 0 0 1 6 0 M9 17a3 3 0 1 1-6 0 3 3 0 0 1 6 0",
|
||||
@@ -68,7 +71,7 @@ const paths: Record<string, string> = {
|
||||
const aliases: Record<string, string> = {
|
||||
cloud_off: "warning",
|
||||
gps_off: "search",
|
||||
edit_note: "edit",
|
||||
edit_note: "editDocument",
|
||||
view_module: "table",
|
||||
grid_view: "table",
|
||||
view_list: "menu",
|
||||
@@ -94,7 +97,7 @@ const aliases: Record<string, string> = {
|
||||
cancel: "close",
|
||||
delete: "trash",
|
||||
delete_outline: "trash",
|
||||
mode_edit: "edit",
|
||||
mode_edit: "rename",
|
||||
content_copy: "copy",
|
||||
content_paste: "paste",
|
||||
content_cut: "cut",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
param([string]$Version = '3.0.0-sso.18')
|
||||
param([string]$Version = '3.0.0-sso.19')
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$repo = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
||||
Push-Location $repo
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
VERSION="${VERSION:-3.0.0-sso.18}"
|
||||
VERSION="${VERSION:-3.0.0-sso.19}"
|
||||
COMMIT=$(git rev-parse --short HEAD)
|
||||
(cd frontend && pnpm install --frozen-lockfile && pnpm run build)
|
||||
mkdir -p build/release/linux-amd64
|
||||
|
||||
Reference in new issue
Block a user