mirror of
https://github.com/outline/outline.git
synced 2026-11-30 09:00:18 +03:00
Restructure the QuickMenuStore
This commit is contained in:
1 parent
2070b1ffc5
commit
3a67d94eb0
2 files changed
+114
-127
No files matched your search
@@ -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 (
|
||||
<CommandItem
|
||||
tabIndex="0"
|
||||
data-order={order}
|
||||
role="option"
|
||||
ref={activeCommand === order ? activeCommandRef : undefined}
|
||||
onMouseOver={() => 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 (
|
||||
<CommandItem
|
||||
tabIndex="0"
|
||||
data-order={order}
|
||||
role="option"
|
||||
ref={activeCommand === order ? activeCommandRef : undefined}
|
||||
onMouseOver={() => 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);
|
||||
}}
|
||||
>
|
||||
<Container align="center">
|
||||
{item.selected !== undefined && (
|
||||
<>
|
||||
{item.selected ? (
|
||||
<MenuIconWrapper>
|
||||
<CheckmarkIcon color="currentColor" />
|
||||
</MenuIconWrapper>
|
||||
) : (
|
||||
<MenuIconWrapper />
|
||||
)}
|
||||
|
||||
</>
|
||||
)}
|
||||
{item.selected === undefined && (
|
||||
<MenuIconWrapper>
|
||||
{item.icon ? (
|
||||
item.icon
|
||||
) : item.href || item.to ? (
|
||||
<LinkIcon />
|
||||
) : null}
|
||||
</MenuIconWrapper>
|
||||
)}
|
||||
{item.title}
|
||||
</Container>
|
||||
</CommandItem>
|
||||
);
|
||||
}
|
||||
console.log("unhandled item", item);
|
||||
return <div>{item?.title}</div>;
|
||||
}}
|
||||
onMouseEnter={() => {
|
||||
setActiveCommand(order);
|
||||
}}
|
||||
>
|
||||
<Container align="center">
|
||||
<MenuIconWrapper>{item.icon}</MenuIconWrapper>
|
||||
{item.title}
|
||||
</Container>
|
||||
</CommandItem>
|
||||
);
|
||||
};
|
||||
|
||||
const data = quickMenu.resolvedMenuItems.map((context) => {
|
||||
console.log("dataaa: ", quickMenu.resolvedMenuItems);
|
||||
const data = quickMenu.resolvedMenuItems?.map((context) => {
|
||||
return (
|
||||
<>
|
||||
<Header>{context.title}</Header>
|
||||
@@ -139,8 +121,6 @@ function QuickMenu() {
|
||||
);
|
||||
});
|
||||
|
||||
console.log(activeCommand);
|
||||
|
||||
const term = quickMenu.searchTerm;
|
||||
|
||||
return (
|
||||
@@ -155,12 +135,15 @@ function QuickMenu() {
|
||||
>
|
||||
{(props) => (
|
||||
<Content {...props} column>
|
||||
<Badges>
|
||||
{quickMenu.path.map((b) => (
|
||||
<Path onClick={() => quickMenu.handlePathClick(b)} key={b}>
|
||||
{b}
|
||||
</Path>
|
||||
))}
|
||||
</Badges>
|
||||
<InputWrapper>
|
||||
<InputSearch
|
||||
// onKeyDown={handleKeyDown}
|
||||
onChange={handleChange}
|
||||
value={term}
|
||||
/>
|
||||
<InputSearch onChange={handleChange} value={term} />
|
||||
</InputWrapper>
|
||||
<Results>
|
||||
<AutoSizer>
|
||||
@@ -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`
|
||||
|
||||
@@ -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<void>,
|
||||
items?: CommandItem[],
|
||||
|};
|
||||
|
||||
type QuickMenuContext = {|
|
||||
id: string,
|
||||
title: string,
|
||||
items: MenuItem[],
|
||||
items: CommandItem[],
|
||||
priority?: number,
|
||||
|};
|
||||
|
||||
class QuickMenuStore {
|
||||
@observable searchTerm: string = "";
|
||||
@observable contextItems: Map<string, QuickMenuContext> = 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<string, any> = 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user