|
|
|
|
@ -1,13 +1,15 @@
|
|
|
|
|
<svelte:options accessors={true} />
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
import { createEventDispatcher } from "svelte";
|
|
|
|
|
import { createEventDispatcher, tick, onDestroy, onMount } from "svelte";
|
|
|
|
|
|
|
|
|
|
import type { MenuListEntry } from "@graphite/wasm-communication/messages";
|
|
|
|
|
|
|
|
|
|
import MenuList from "@graphite/components/floating-menus/MenuList.svelte";
|
|
|
|
|
import FloatingMenu, { type MenuDirection } from "@graphite/components/layout/FloatingMenu.svelte";
|
|
|
|
|
import LayoutCol from "@graphite/components/layout/LayoutCol.svelte";
|
|
|
|
|
import LayoutRow from "@graphite/components/layout/LayoutRow.svelte";
|
|
|
|
|
import TextInput from "@graphite/components/widgets/inputs/TextInput.svelte";
|
|
|
|
|
import IconLabel from "@graphite/components/widgets/labels/IconLabel.svelte";
|
|
|
|
|
import Separator from "@graphite/components/widgets/labels/Separator.svelte";
|
|
|
|
|
import TextLabel from "@graphite/components/widgets/labels/TextLabel.svelte";
|
|
|
|
|
@ -15,8 +17,9 @@
|
|
|
|
|
|
|
|
|
|
let self: FloatingMenu | undefined;
|
|
|
|
|
let scroller: LayoutCol | undefined;
|
|
|
|
|
let searchTextInput: TextInput | undefined;
|
|
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher<{ open: boolean; activeEntry: MenuListEntry }>();
|
|
|
|
|
const dispatch = createEventDispatcher<{ open: boolean; activeEntry: MenuListEntry; naturalWidth: number }>();
|
|
|
|
|
|
|
|
|
|
export let entries: MenuListEntry[][];
|
|
|
|
|
export let activeEntry: MenuListEntry | undefined = undefined;
|
|
|
|
|
@ -29,21 +32,104 @@
|
|
|
|
|
export let virtualScrollingEntryHeight = 0;
|
|
|
|
|
export let tooltip: string | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
// Keep the child references outside of the entries array so as to avoid infinite recursion.
|
|
|
|
|
let childReferences: MenuList[][] = [];
|
|
|
|
|
let search = "";
|
|
|
|
|
|
|
|
|
|
let highlighted = activeEntry as MenuListEntry | undefined;
|
|
|
|
|
let virtualScrollingEntriesStart = 0;
|
|
|
|
|
|
|
|
|
|
// Called only when `open` is changed from outside this component
|
|
|
|
|
$: watchOpen(open);
|
|
|
|
|
$: watchRemeasureWidth(entries, drawIcon);
|
|
|
|
|
$: watchEntries(entries);
|
|
|
|
|
$: watchRemeasureWidth(filteredEntries, drawIcon);
|
|
|
|
|
$: watchHighlightedWithSearch(filteredEntries, open);
|
|
|
|
|
|
|
|
|
|
$: virtualScrollingTotalHeight = entries.length === 0 ? 0 : entries[0].length * virtualScrollingEntryHeight;
|
|
|
|
|
$: filteredEntries = entries.map((section) => section.filter((entry) => inSearch(search, entry)));
|
|
|
|
|
$: virtualScrollingTotalHeight = filteredEntries.length === 0 ? 0 : filteredEntries[0].length * virtualScrollingEntryHeight;
|
|
|
|
|
$: virtualScrollingStartIndex = Math.floor(virtualScrollingEntriesStart / virtualScrollingEntryHeight) || 0;
|
|
|
|
|
$: virtualScrollingEndIndex = entries.length === 0 ? 0 : Math.min(entries[0].length, virtualScrollingStartIndex + 1 + 400 / virtualScrollingEntryHeight);
|
|
|
|
|
$: virtualScrollingEndIndex = filteredEntries.length === 0 ? 0 : Math.min(filteredEntries[0].length, virtualScrollingStartIndex + 1 + 400 / virtualScrollingEntryHeight);
|
|
|
|
|
$: startIndex = virtualScrollingEntryHeight ? virtualScrollingStartIndex : 0;
|
|
|
|
|
|
|
|
|
|
// TODO: Move keyboard input handling entirely to the unified system in `input.ts`.
|
|
|
|
|
// TODO: The current approach is hacky and blocks the allowances for shortcuts like the key to open the browser's dev tools.
|
|
|
|
|
onMount(async () => {
|
|
|
|
|
await tick();
|
|
|
|
|
if (open && !inNestedMenuList()) addEventListener("keydown", keydown);
|
|
|
|
|
});
|
|
|
|
|
onDestroy(async () => {
|
|
|
|
|
await tick();
|
|
|
|
|
if (!inNestedMenuList()) removeEventListener("keydown", keydown);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function inNestedMenuList(): boolean {
|
|
|
|
|
const div = self?.div();
|
|
|
|
|
if (!(div instanceof HTMLDivElement)) return false;
|
|
|
|
|
return Boolean(div.closest("[data-floating-menu-content]"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Required to keep the highlighted item centered and to find a new highlighted item if necessary
|
|
|
|
|
async function watchHighlightedWithSearch(filteredEntries: MenuListEntry[][], open: boolean) {
|
|
|
|
|
if (highlighted && open) {
|
|
|
|
|
// Allows the scrollable area to expand if necessary
|
|
|
|
|
await tick();
|
|
|
|
|
|
|
|
|
|
const flattened = filteredEntries.flat();
|
|
|
|
|
const highlightedFound = highlighted?.label && flattened.map((entry) => entry.label).includes(highlighted.label);
|
|
|
|
|
const newHighlighted = highlightedFound ? highlighted : flattened[0];
|
|
|
|
|
setHighlighted(newHighlighted);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Detect when the user types, which creates a search box
|
|
|
|
|
async function startSearch(e: KeyboardEvent) {
|
|
|
|
|
// Only accept single-character symbol inputs other than space
|
|
|
|
|
if (e.key.length !== 1 || e.key === " ") return;
|
|
|
|
|
|
|
|
|
|
// Stop shortcuts being activated
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
// Forward the input's first character to the search box, which after that point the user will continue typing into directly
|
|
|
|
|
search = e.key;
|
|
|
|
|
|
|
|
|
|
// Must wait until the DOM elements have been created (after the if condition becomes true) before the search box exists
|
|
|
|
|
await tick();
|
|
|
|
|
|
|
|
|
|
// Get the search box element
|
|
|
|
|
const searchElement = searchTextInput?.element();
|
|
|
|
|
if (!searchTextInput || !searchElement) return;
|
|
|
|
|
|
|
|
|
|
// Focus the search box and move the cursor to the end
|
|
|
|
|
searchTextInput.focus();
|
|
|
|
|
searchElement.setSelectionRange(search.length, search.length);
|
|
|
|
|
|
|
|
|
|
// Continue listening for keyboard navigation even when the search box is focused
|
|
|
|
|
// searchElement.onkeydown = (e) => {
|
|
|
|
|
// if (["Enter", "Escape", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.key)) {
|
|
|
|
|
// keydown(e, false);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function inSearch(search: string, entry: MenuListEntry): boolean {
|
|
|
|
|
return !search || entry.label.toLowerCase().includes(search.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function watchOpen(open: boolean) {
|
|
|
|
|
if (open && !inNestedMenuList()) addEventListener("keydown", keydown);
|
|
|
|
|
else if (!inNestedMenuList()) removeEventListener("keydown", keydown);
|
|
|
|
|
|
|
|
|
|
highlighted = activeEntry;
|
|
|
|
|
dispatch("open", open);
|
|
|
|
|
|
|
|
|
|
search = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function watchEntries(entries: MenuListEntry[][]) {
|
|
|
|
|
entries.forEach((_, index) => {
|
|
|
|
|
if (!childReferences[index]) childReferences[index] = [];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function watchRemeasureWidth(_: MenuListEntry[][], __: boolean) {
|
|
|
|
|
@ -55,6 +141,11 @@
|
|
|
|
|
virtualScrollingEntriesStart = (e.target as HTMLElement)?.scrollTop || 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getChildReference(menuListEntry: MenuListEntry): MenuList | undefined {
|
|
|
|
|
const index = filteredEntries.flat().indexOf(menuListEntry);
|
|
|
|
|
return childReferences.flat().filter((x) => x)[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onEntryClick(menuListEntry: MenuListEntry) {
|
|
|
|
|
// Call the action if available
|
|
|
|
|
if (menuListEntry.action) menuListEntry.action();
|
|
|
|
|
@ -63,8 +154,9 @@
|
|
|
|
|
dispatch("activeEntry", menuListEntry);
|
|
|
|
|
|
|
|
|
|
// Close the containing menu
|
|
|
|
|
if (menuListEntry.ref) {
|
|
|
|
|
menuListEntry.ref.open = false;
|
|
|
|
|
let childReference = getChildReference(menuListEntry);
|
|
|
|
|
if (childReference) {
|
|
|
|
|
childReference.open = false;
|
|
|
|
|
entries = entries;
|
|
|
|
|
}
|
|
|
|
|
dispatch("open", false);
|
|
|
|
|
@ -74,8 +166,9 @@
|
|
|
|
|
function onEntryPointerEnter(menuListEntry: MenuListEntry) {
|
|
|
|
|
if (!menuListEntry.children?.length) return;
|
|
|
|
|
|
|
|
|
|
if (menuListEntry.ref) {
|
|
|
|
|
menuListEntry.ref.open = true;
|
|
|
|
|
let childReference = getChildReference(menuListEntry);
|
|
|
|
|
if (childReference) {
|
|
|
|
|
childReference.open = true;
|
|
|
|
|
entries = entries;
|
|
|
|
|
} else dispatch("open", true);
|
|
|
|
|
}
|
|
|
|
|
@ -83,8 +176,9 @@
|
|
|
|
|
function onEntryPointerLeave(menuListEntry: MenuListEntry) {
|
|
|
|
|
if (!menuListEntry.children?.length) return;
|
|
|
|
|
|
|
|
|
|
if (menuListEntry.ref) {
|
|
|
|
|
menuListEntry.ref.open = false;
|
|
|
|
|
let childReference = getChildReference(menuListEntry);
|
|
|
|
|
if (childReference) {
|
|
|
|
|
childReference.open = false;
|
|
|
|
|
entries = entries;
|
|
|
|
|
} else dispatch("open", false);
|
|
|
|
|
}
|
|
|
|
|
@ -92,49 +186,82 @@
|
|
|
|
|
function isEntryOpen(menuListEntry: MenuListEntry): boolean {
|
|
|
|
|
if (!menuListEntry.children?.length) return false;
|
|
|
|
|
|
|
|
|
|
return menuListEntry.ref?.open || false;
|
|
|
|
|
return getChildReference(menuListEntry)?.open || false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles keyboard navigation for the menu. Returns if the entire menu stack should be dismissed
|
|
|
|
|
export function keydown(e: KeyboardEvent, submenu: boolean): boolean {
|
|
|
|
|
function includeSeparator(entries: MenuListEntry[][], section: MenuListEntry[], sectionIndex: number, search: string): boolean {
|
|
|
|
|
const elementsBeforeCurrentSection = entries
|
|
|
|
|
.slice(0, sectionIndex)
|
|
|
|
|
.flat()
|
|
|
|
|
.filter((entry) => inSearch(search, entry));
|
|
|
|
|
const entriesInCurrentSection = section.filter((entry) => inSearch(search, entry));
|
|
|
|
|
|
|
|
|
|
return elementsBeforeCurrentSection.length > 0 && entriesInCurrentSection.length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function currentEntries(section: MenuListEntry[], virtualScrollingEntryHeight: number, virtualScrollingStartIndex: number, virtualScrollingEndIndex: number, search: string) {
|
|
|
|
|
if (!virtualScrollingEntryHeight) {
|
|
|
|
|
return section.filter((entry) => inSearch(search, entry));
|
|
|
|
|
}
|
|
|
|
|
return section.filter((entry) => inSearch(search, entry)).slice(virtualScrollingStartIndex, virtualScrollingEndIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openSubmenu(highlightedEntry: MenuListEntry): boolean {
|
|
|
|
|
let childReference = getChildReference(highlightedEntry);
|
|
|
|
|
// No submenu to open
|
|
|
|
|
if (!childReference || !highlightedEntry.children?.length) return false;
|
|
|
|
|
|
|
|
|
|
childReference.open = true;
|
|
|
|
|
// The reason we bother taking `highlightdEntry` as an argument is because, when this function is called, it can ensure `highlightedEntry` is not undefined.
|
|
|
|
|
// But here we still have to set `highlighted` to itself so Svelte knows to reactively update it after we set its `.ref.open` property.
|
|
|
|
|
highlighted = highlighted;
|
|
|
|
|
|
|
|
|
|
// Highlight first item
|
|
|
|
|
childReference.setHighlighted(highlightedEntry.children[0][0]);
|
|
|
|
|
|
|
|
|
|
// Submenu was opened
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles keyboard navigation for the menu.
|
|
|
|
|
// Returns a boolean indicating whether the entire menu stack should be dismissed.
|
|
|
|
|
export function keydown(e: KeyboardEvent, submenu = false): boolean {
|
|
|
|
|
// Interactive menus should keep the active entry the same as the highlighted one
|
|
|
|
|
if (interactive) highlighted = activeEntry;
|
|
|
|
|
|
|
|
|
|
const menuOpen = open;
|
|
|
|
|
const flatEntries = entries.flat().filter((entry) => !entry.disabled);
|
|
|
|
|
const openChild = flatEntries.findIndex((entry) => (entry.children?.length ?? 0) > 0 && entry.ref?.open);
|
|
|
|
|
|
|
|
|
|
const openSubmenu = (highlightedEntry: MenuListEntry) => {
|
|
|
|
|
if (highlightedEntry.ref && highlightedEntry.children?.length) {
|
|
|
|
|
highlightedEntry.ref.open = true;
|
|
|
|
|
// The reason we bother taking `highlightdEntry` as an argument is because, when this function is called, it can ensure `highlightedEntry` is not undefined.
|
|
|
|
|
// But here we still have to set `highlighted` to itself so Svelte knows to reactively update it after we set its `.ref.open` property.
|
|
|
|
|
highlighted = highlighted;
|
|
|
|
|
|
|
|
|
|
// Highlight first item
|
|
|
|
|
highlightedEntry.ref.setHighlighted(highlightedEntry.children[0][0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const flatEntries = filteredEntries.flat().filter((entry) => !entry.disabled);
|
|
|
|
|
const openChild = flatEntries.findIndex((entry) => (entry.children?.length ?? 0) > 0 && getChildReference(entry)?.open);
|
|
|
|
|
|
|
|
|
|
// Allow opening menu with space or enter
|
|
|
|
|
if (!menuOpen && (e.key === " " || e.key === "Enter")) {
|
|
|
|
|
// Allow opening menu with space or enter
|
|
|
|
|
open = true;
|
|
|
|
|
highlighted = activeEntry;
|
|
|
|
|
} else if (menuOpen && openChild >= 0) {
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If a submenu is open, have it handle this instead
|
|
|
|
|
if (menuOpen && openChild >= 0) {
|
|
|
|
|
const childMenuListEntry = flatEntries[openChild];
|
|
|
|
|
const childMenu = getChildReference(childMenuListEntry);
|
|
|
|
|
|
|
|
|
|
// Redirect the keyboard navigation to a submenu if one is open
|
|
|
|
|
const shouldCloseStack = flatEntries[openChild].ref?.keydown(e, true);
|
|
|
|
|
const shouldCloseStack = childMenu?.keydown(e, true) || false;
|
|
|
|
|
|
|
|
|
|
// Highlight the menu item in the parent list that corresponds with the open submenu
|
|
|
|
|
if (e.key !== "Escape" && highlighted) setHighlighted(flatEntries[openChild]);
|
|
|
|
|
if (highlighted && e.key !== "Escape") setHighlighted(childMenuListEntry);
|
|
|
|
|
|
|
|
|
|
// Handle the child closing the entire menu stack
|
|
|
|
|
if (shouldCloseStack) {
|
|
|
|
|
open = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else if ((menuOpen || interactive) && (e.key === "ArrowUp" || e.key === "ArrowDown")) {
|
|
|
|
|
// Navigate to the next and previous entries with arrow keys
|
|
|
|
|
if (shouldCloseStack) open = false;
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return shouldCloseStack;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Navigate to the next and previous entries with arrow keys
|
|
|
|
|
if ((menuOpen || interactive) && (e.key === "ArrowUp" || e.key === "ArrowDown")) {
|
|
|
|
|
let newIndex = e.key === "ArrowUp" ? flatEntries.length - 1 : 0;
|
|
|
|
|
if (highlighted) {
|
|
|
|
|
const index = highlighted ? flatEntries.map((entry) => entry.label).indexOf(highlighted.label) : 0;
|
|
|
|
|
@ -147,13 +274,26 @@
|
|
|
|
|
|
|
|
|
|
const newEntry = flatEntries[newIndex];
|
|
|
|
|
setHighlighted(newEntry);
|
|
|
|
|
} else if (menuOpen && e.key === "Escape") {
|
|
|
|
|
// Close menu with escape key
|
|
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close menu with escape key
|
|
|
|
|
if (menuOpen && e.key === "Escape") {
|
|
|
|
|
open = false;
|
|
|
|
|
|
|
|
|
|
// Reset active to before open
|
|
|
|
|
setHighlighted(activeEntry);
|
|
|
|
|
} else if (menuOpen && highlighted && e.key === "Enter") {
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Click on a highlighted entry with the enter key
|
|
|
|
|
if (menuOpen && highlighted && e.key === "Enter") {
|
|
|
|
|
// Handle clicking on an option if enter is pressed
|
|
|
|
|
if (!highlighted.children?.length) onEntryClick(highlighted);
|
|
|
|
|
else openSubmenu(highlighted);
|
|
|
|
|
@ -163,26 +303,81 @@
|
|
|
|
|
|
|
|
|
|
// Enter should close the entire menu stack
|
|
|
|
|
return true;
|
|
|
|
|
} else if (menuOpen && highlighted && e.key === "ArrowRight") {
|
|
|
|
|
// Right arrow opens a submenu
|
|
|
|
|
openSubmenu(highlighted);
|
|
|
|
|
} else if (menuOpen && e.key === "ArrowLeft") {
|
|
|
|
|
// Left arrow closes a submenu
|
|
|
|
|
if (submenu) open = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// By default, keep the menu stack open
|
|
|
|
|
// Open a submenu with the right arrow key, space, or enter
|
|
|
|
|
if (menuOpen && highlighted && (e.key === "ArrowRight" || e.key === " " || e.key === "Enter")) {
|
|
|
|
|
// Right arrow opens a submenu
|
|
|
|
|
const openable = openSubmenu(highlighted);
|
|
|
|
|
|
|
|
|
|
// Prevent the right arrow from moving the search text cursor if we are opening a submenu
|
|
|
|
|
if (openable) e.preventDefault();
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close a submenu with the left arrow key
|
|
|
|
|
if (menuOpen && e.key === "ArrowLeft") {
|
|
|
|
|
// Left arrow closes a submenu
|
|
|
|
|
if (submenu) {
|
|
|
|
|
open = false;
|
|
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start a search with any other key
|
|
|
|
|
if (menuOpen && search === "") {
|
|
|
|
|
startSearch(e);
|
|
|
|
|
|
|
|
|
|
// Keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If nothing happened, keep the menu stack open
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setHighlighted(newHighlight: MenuListEntry | undefined) {
|
|
|
|
|
highlighted = newHighlight;
|
|
|
|
|
|
|
|
|
|
// Interactive menus should keep the active entry the same as the highlighted one
|
|
|
|
|
if (interactive && newHighlight?.value !== activeEntry?.value && newHighlight) dispatch("activeEntry", newHighlight);
|
|
|
|
|
if (interactive && newHighlight?.value !== activeEntry?.value && newHighlight) {
|
|
|
|
|
dispatch("activeEntry", newHighlight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scroll into view
|
|
|
|
|
let container = scroller?.div?.();
|
|
|
|
|
if (!container || !highlighted) return;
|
|
|
|
|
let containerBoundingRect = container.getBoundingClientRect();
|
|
|
|
|
let highlightedIndex = filteredEntries.flat().findIndex((entry) => entry === highlighted);
|
|
|
|
|
|
|
|
|
|
let selectedBoundingRect = new DOMRect();
|
|
|
|
|
if (virtualScrollingEntryHeight) {
|
|
|
|
|
// Special case for virtual scrolling
|
|
|
|
|
selectedBoundingRect.y = highlightedIndex * virtualScrollingEntryHeight - container.scrollTop + containerBoundingRect.y;
|
|
|
|
|
selectedBoundingRect.height = virtualScrollingEntryHeight;
|
|
|
|
|
} else {
|
|
|
|
|
let entries = Array.from(container.children).filter((element) => element.classList.contains("row"));
|
|
|
|
|
let element = entries[highlightedIndex - startIndex];
|
|
|
|
|
if (!element) return;
|
|
|
|
|
containerBoundingRect = element.getBoundingClientRect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (containerBoundingRect.y > selectedBoundingRect.y) {
|
|
|
|
|
container.scrollBy(0, selectedBoundingRect.y - containerBoundingRect.y);
|
|
|
|
|
}
|
|
|
|
|
if (containerBoundingRect.y + containerBoundingRect.height < selectedBoundingRect.y + selectedBoundingRect.height) {
|
|
|
|
|
container.scrollBy(0, selectedBoundingRect.y - (containerBoundingRect.y + containerBoundingRect.height) + selectedBoundingRect.height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function scrollViewTo(distanceDown: number) {
|
|
|
|
|
scroller?.div()?.scrollTo(0, distanceDown);
|
|
|
|
|
scroller?.div?.()?.scrollTo(0, distanceDown);
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
@ -199,6 +394,9 @@
|
|
|
|
|
scrollableY={scrollableY && virtualScrollingEntryHeight === 0}
|
|
|
|
|
bind:this={self}
|
|
|
|
|
>
|
|
|
|
|
{#if search.length > 0}
|
|
|
|
|
<TextInput class="search" value={search} on:value={({ detail }) => (search = detail)} bind:this={searchTextInput}></TextInput>
|
|
|
|
|
{/if}
|
|
|
|
|
<!-- If we put the scrollableY on the layoutcol for non-font dropdowns then for some reason it always creates a tiny scrollbar.
|
|
|
|
|
However when we are using the virtual scrolling then we need the layoutcol to be scrolling so we can bind the events without using `self`. -->
|
|
|
|
|
<LayoutCol
|
|
|
|
|
@ -211,10 +409,10 @@
|
|
|
|
|
<LayoutRow class="scroll-spacer" styles={{ height: `${virtualScrollingStartIndex * virtualScrollingEntryHeight}px` }} />
|
|
|
|
|
{/if}
|
|
|
|
|
{#each entries as section, sectionIndex (sectionIndex)}
|
|
|
|
|
{#if sectionIndex > 0}
|
|
|
|
|
{#if includeSeparator(entries, section, sectionIndex, search)}
|
|
|
|
|
<Separator type="Section" direction="Vertical" />
|
|
|
|
|
{/if}
|
|
|
|
|
{#each virtualScrollingEntryHeight ? section.slice(virtualScrollingStartIndex, virtualScrollingEndIndex) : section as entry, entryIndex (entryIndex + startIndex)}
|
|
|
|
|
{#each currentEntries(section, virtualScrollingEntryHeight, virtualScrollingStartIndex, virtualScrollingEndIndex, search) as entry, entryIndex (entryIndex + startIndex)}
|
|
|
|
|
<LayoutRow
|
|
|
|
|
class="row"
|
|
|
|
|
classes={{ open: isEntryOpen(entry), active: entry.label === highlighted?.label, disabled: Boolean(entry.disabled) }}
|
|
|
|
|
@ -247,8 +445,21 @@
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#if entry.children}
|
|
|
|
|
<!-- TODO: Solve the red underline error on the bind:this below -->
|
|
|
|
|
<svelte:self on:naturalWidth open={entry.ref?.open || false} direction="TopRight" entries={entry.children} {minWidth} {drawIcon} {scrollableY} bind:this={entry.ref} />
|
|
|
|
|
<MenuList
|
|
|
|
|
on:naturalWidth={() => {
|
|
|
|
|
// We do a manual dispatch here instead of just `on:naturalWidth` as a workaround for the <script> tag
|
|
|
|
|
// at the top of this file displaying a "'render' implicitly has return type 'any' because..." error.
|
|
|
|
|
// See explanation at <https://github.com/sveltejs/language-tools/issues/452#issuecomment-723148184>.
|
|
|
|
|
dispatch("naturalWidth");
|
|
|
|
|
}}
|
|
|
|
|
open={getChildReference(entry)?.open || false}
|
|
|
|
|
direction="TopRight"
|
|
|
|
|
entries={entry.children}
|
|
|
|
|
{minWidth}
|
|
|
|
|
{drawIcon}
|
|
|
|
|
{scrollableY}
|
|
|
|
|
bind:this={childReferences[sectionIndex][entryIndex + startIndex]}
|
|
|
|
|
/>
|
|
|
|
|
{/if}
|
|
|
|
|
</LayoutRow>
|
|
|
|
|
{/each}
|
|
|
|
|
@ -261,6 +472,11 @@
|
|
|
|
|
|
|
|
|
|
<style lang="scss" global>
|
|
|
|
|
.menu-list {
|
|
|
|
|
.search {
|
|
|
|
|
margin: 4px;
|
|
|
|
|
margin-top: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.floating-menu-container .floating-menu-content.floating-menu-content {
|
|
|
|
|
padding: 4px 0;
|
|
|
|
|
|
|
|
|
|
|