From 3a67d94eb029325cb59fa9eba16b5d6ca9500717 Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Sat, 11 Sep 2021 18:17:59 +0530 Subject: [PATCH] Restructure the QuickMenuStore --- app/components/QuickMenu/index.js | 124 +++++++++++++++--------------- app/stores/QuickMenuStore.js | 117 ++++++++++++---------------- 2 files changed, 114 insertions(+), 127 deletions(-) diff --git a/app/components/QuickMenu/index.js b/app/components/QuickMenu/index.js index 8418eca4c1..8bf38f7eae 100644 --- a/app/components/QuickMenu/index.js +++ b/app/components/QuickMenu/index.js @@ -1,6 +1,5 @@ // @flow import { observer } from "mobx-react"; -import { CheckmarkIcon, LinkIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import AutoSizer from "react-virtualized-auto-sizer"; @@ -12,6 +11,7 @@ import Flex from "components/Flex"; import Scrollable from "components/Scrollable"; import InputSearch from "../InputSearch"; import useStores from "hooks/useStores"; +import { fadeAndSlideUp } from "styles/animations"; function QuickMenu() { const { quickMenu } = useStores(); @@ -21,14 +21,18 @@ function QuickMenu() { const { t } = useTranslation(); let order = 0; - React.useLayoutEffect(() => { + React.useEffect(() => { + setActiveCommand(1); + }, [quickMenu.path]); + + React.useEffect(() => { if (!dialog.visible) { quickMenu.reset(); setActiveCommand(1); } }, [quickMenu, dialog.visible]); - React.useLayoutEffect(() => { + React.useEffect(() => { const handleKeyDown = (event) => { if (event.key === "k") { dialog.show(); @@ -42,7 +46,6 @@ function QuickMenu() { React.useEffect(() => { if (activeCommandRef.current) { - console.log("calling on", activeCommandRef.current); scrollIntoView(activeCommandRef.current, { scrollMode: "if-needed", behavior: "instant", @@ -60,7 +63,7 @@ function QuickMenu() { if (event.currentTarget.value && event.key === "Escape") { event.preventDefault(); event.stopPropagation(); - quickMenu.setSearchTerm(""); + quickMenu.reset(); } if (event.key === "ArrowDown") { @@ -76,57 +79,36 @@ function QuickMenu() { }; const constructBlock = (item, order, setActiveCommand) => { - if (item.to || item.href || item.onClick) { - return ( - setActiveCommand(order)} - aria-selected={activeCommand === order} - selected={activeCommand === order} - onClick={ - item.to || item.href - ? () => (window.location.href = item.to ? item.to : item.href) - : item.onClick + return ( + setActiveCommand(order)} + aria-selected={activeCommand === order} + selected={activeCommand === order} + onClick={(e) => { + if (item.items) quickMenu.handleNestedItems(item); + else { + dialog.hide(); + item.onClick(e); } - onFocus={() => { - setActiveCommand(order); - }} - > - - {item.selected !== undefined && ( - <> - {item.selected ? ( - - - - ) : ( - - )} -   - - )} - {item.selected === undefined && ( - - {item.icon ? ( - item.icon - ) : item.href || item.to ? ( - - ) : null} - - )} - {item.title} - - - ); - } - console.log("unhandled item", item); - return
{item?.title}
; + }} + onMouseEnter={() => { + setActiveCommand(order); + }} + > + + {item.icon} + {item.title} + +
+ ); }; - const data = quickMenu.resolvedMenuItems.map((context) => { + console.log("dataaa: ", quickMenu.resolvedMenuItems); + const data = quickMenu.resolvedMenuItems?.map((context) => { return ( <>
{context.title}
@@ -139,8 +121,6 @@ function QuickMenu() { ); }); - console.log(activeCommand); - const term = quickMenu.searchTerm; return ( @@ -155,12 +135,15 @@ function QuickMenu() { > {(props) => ( + + {quickMenu.path.map((b) => ( + quickMenu.handlePathClick(b)} key={b}> + {b} + + ))} + - + @@ -180,6 +163,25 @@ function QuickMenu() { ); } +const Path = styled.span` + margin-left: 10px; + padding: 1px 5px 2px; + border: 1px solid; + background-color: ${({ theme }) => theme.slateLight}; + color: ${({ theme }) => theme.slateDark}; + border: 1px solid ${({ theme }) => theme.slateLight}; + border-radius: 4px; + font-size: 14px; + font-weight: 500; + user-select: none; + cursor: pointer; +`; + +const Badges = styled.div` + margin-top: 10px; + margin-left: 10px; +`; + const InputWrapper = styled.div` padding: 16px; `; @@ -250,6 +252,8 @@ const Content = styled(Flex)` overflow: hidden; margin: 20vh auto; box-shadow: ${(props) => props.theme.menuShadow}; + animation: ${fadeAndSlideUp} 200ms ease-in-out; + transform-origin: 25% 0; `; const Backdrop = styled.div` diff --git a/app/stores/QuickMenuStore.js b/app/stores/QuickMenuStore.js index ab949acc1a..a2102e0588 100644 --- a/app/stores/QuickMenuStore.js +++ b/app/stores/QuickMenuStore.js @@ -1,101 +1,72 @@ // @flow import { observable, computed, action } from "mobx"; -import { type MenuItem } from "types"; type CommandItem = {| title: React.Node, icon?: React.Node, onClick: (event: SyntheticEvent<>) => void | Promise, + items?: CommandItem[], |}; type QuickMenuContext = {| id: string, title: string, - items: MenuItem[], + items: CommandItem[], priority?: number, |}; class QuickMenuStore { @observable searchTerm: string = ""; @observable contextItems: Map = new Map(); - @observable showNested: boolean = false; - @observable nestedMenu: {| - title: React.Node, - visible?: boolean, - disabled?: boolean, - style?: Object, - hover?: boolean, - items: MenuItem[], - icon?: React.Node, - |} | void; + @observable states: Map = new Map(); + @observable path: string[] = ["Home"]; @computed get resolvedMenuItems(): QuickMenuContext[] { - let filtered = []; - if (this.showNested && this.nestedMenu) { - return [ - { - id: this.nestedMenu.title, - title: this.nestedMenu.title, - items: this.nestedMenu.items, - }, - ]; - } - this.contextItems.forEach((context) => { - const items = context.items.filter( - (item) => - item.type !== "separator" && - (item.visible || context.id === "account") && - (!this.searchTerm || - (typeof item.title === "string" && - item.title.toLowerCase().includes(this.searchTerm.toLowerCase()))) - ); + const currentPath = this.path[this.path.length - 1]; - items.forEach((item, index) => { - // has nested menu - if (item.items && item.title) { - const newOnClick = () => { - this.activateNestedState(item); - }; - item.onClick = newOnClick; + if (currentPath === "Home") { + let filtered = []; + this.contextItems.forEach((context) => { + const items = context.items.filter( + (item) => + !this.searchTerm || + (typeof item.title === "string" && + item.title.toLowerCase().includes(this.searchTerm.toLowerCase())) + ); + + if (items.length) { + filtered.push({ + ...context, + items, + }); } }); - if (items.length) { - filtered.push({ - ...context, - items, - }); - } - }); + return filtered; + } else { + let items = this.states.get(currentPath); + if (!items) return []; - console.log({ filtered }); - return filtered; + const filtered = items.filter( + (item) => + !this.searchTerm || + (typeof item.title === "string" && + item.title.toLowerCase().includes(this.searchTerm.toLowerCase())) + ); + + return [{ id: currentPath, title: currentPath, items: filtered }]; + } } @action - activateNestedState(item: {| - title: React.Node, - visible?: boolean, - disabled?: boolean, - style?: Object, - hover?: boolean, - items: MenuItem[], - icon?: React.Node, - |}) { - this.showNested = true; - this.nestedMenu = item; - } - - @action - nestedItems(title: string, item: QuickMenuContext): void { - this.contextItems.clear(); - this.contextItems.set(title, item); + handleNestedItems(item: CommandItem) { + this.states.set(item.title, item.items); + this.path = [...this.path, item.title]; } @action setSearchTerm(searchTerm: string): void { - console.log({ searchTerm }); this.searchTerm = searchTerm; } @@ -112,8 +83,20 @@ class QuickMenuStore { @action reset() { this.setSearchTerm(""); - this.showNested = false; - this.nestedMenu = undefined; + this.states.clear(); + this.path = ["Home"]; + } + + @action + handlePathClick(toPath: string) { + if (toPath === "Home") { + this.states.clear(); + this.path = ["Home"]; + } else { + const pathsToRemove = this.path.slice(this.path.indexOf(toPath) + 1); + pathsToRemove.forEach((path) => this.states.delete(path)); + this.path = this.path.slice(0, this.path.indexOf(toPath) + 1); + } } }