|
| 1 | +import { atom, computed } from "nanostores"; |
| 2 | +import { useStore } from "@nanostores/react"; |
| 3 | +import { |
| 4 | + collectionComponent, |
| 5 | + componentCategories, |
| 6 | + WsComponentMeta, |
| 7 | +} from "@webstudio-is/react-sdk"; |
| 8 | +import { isFeatureEnabled } from "@webstudio-is/feature-flags"; |
| 9 | +import { |
| 10 | + Kbd, |
| 11 | + Text, |
| 12 | + CommandDialog, |
| 13 | + CommandInput, |
| 14 | + CommandList, |
| 15 | + CommandGroup, |
| 16 | + CommandGroupHeading, |
| 17 | + CommandItem, |
| 18 | + CommandIcon, |
| 19 | + ScrollArea, |
| 20 | + Flex, |
| 21 | +} from "@webstudio-is/design-system"; |
| 22 | +import { compareMedia } from "@webstudio-is/css-engine"; |
| 23 | +import type { Breakpoint } from "@webstudio-is/sdk"; |
| 24 | +import { |
| 25 | + $breakpoints, |
| 26 | + $pages, |
| 27 | + $registeredComponentMetas, |
| 28 | + $selectedBreakpoint, |
| 29 | + $selectedBreakpointId, |
| 30 | +} from "~/shared/nano-states"; |
| 31 | +import { getInstanceLabel } from "~/shared/instance-utils"; |
| 32 | +import { humanizeString } from "~/shared/string-utils"; |
| 33 | +import { setCanvasWidth } from "~/builder/features/breakpoints"; |
| 34 | +import { insert as insertComponent } from "~/builder/features/components/insert"; |
| 35 | +import { $selectedPage, selectPage } from "~/shared/awareness"; |
| 36 | + |
| 37 | +const $commandPanel = atom< |
| 38 | + | undefined |
| 39 | + | { |
| 40 | + lastFocusedElement: null | HTMLElement; |
| 41 | + } |
| 42 | +>(); |
| 43 | + |
| 44 | +export const openCommandPanel = () => { |
| 45 | + if (isFeatureEnabled("command") === false) { |
| 46 | + return; |
| 47 | + } |
| 48 | + const activeElement = |
| 49 | + document.activeElement instanceof HTMLElement |
| 50 | + ? document.activeElement |
| 51 | + : null; |
| 52 | + // store last focused element |
| 53 | + $commandPanel.set({ |
| 54 | + lastFocusedElement: activeElement, |
| 55 | + }); |
| 56 | +}; |
| 57 | + |
| 58 | +const closeCommandPanel = ({ |
| 59 | + restoreFocus = false, |
| 60 | +}: { restoreFocus?: boolean } = {}) => { |
| 61 | + const commandPanel = $commandPanel.get(); |
| 62 | + $commandPanel.set(undefined); |
| 63 | + // restore focus in the next frame |
| 64 | + if (restoreFocus && commandPanel?.lastFocusedElement) { |
| 65 | + requestAnimationFrame(() => { |
| 66 | + commandPanel.lastFocusedElement?.focus(); |
| 67 | + }); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +const getMetaScore = (meta: WsComponentMeta) => { |
| 72 | + const categoryScore = componentCategories.indexOf(meta.category ?? "hidden"); |
| 73 | + const componentScore = meta.order ?? Number.MAX_SAFE_INTEGER; |
| 74 | + // shift category |
| 75 | + return categoryScore * 1000 + componentScore; |
| 76 | +}; |
| 77 | + |
| 78 | +const $visibleMetas = computed( |
| 79 | + [$registeredComponentMetas, $selectedPage], |
| 80 | + (metas, selectedPage) => { |
| 81 | + const entries = Array.from(metas) |
| 82 | + .sort( |
| 83 | + ([_leftComponent, leftMeta], [_rightComponent, rightMeta]) => |
| 84 | + getMetaScore(leftMeta) - getMetaScore(rightMeta) |
| 85 | + ) |
| 86 | + .filter(([component, meta]) => { |
| 87 | + const category = meta.category ?? "hidden"; |
| 88 | + if (category === "hidden" || category === "internal") { |
| 89 | + return false; |
| 90 | + } |
| 91 | + // show only xml category and collection component in xml documents |
| 92 | + if (selectedPage?.meta.documentType === "xml") { |
| 93 | + return category === "xml" || component === collectionComponent; |
| 94 | + } |
| 95 | + // show everything except xml category in html documents |
| 96 | + return category !== "xml"; |
| 97 | + }); |
| 98 | + return new Map(entries); |
| 99 | + } |
| 100 | +); |
| 101 | + |
| 102 | +const ComponentsGroup = () => { |
| 103 | + const metas = useStore($visibleMetas); |
| 104 | + return ( |
| 105 | + <CommandGroup |
| 106 | + heading={<CommandGroupHeading>Components</CommandGroupHeading>} |
| 107 | + > |
| 108 | + {Array.from(metas).map(([component, meta]) => { |
| 109 | + return ( |
| 110 | + <CommandItem |
| 111 | + key={component} |
| 112 | + keywords={["Components"]} |
| 113 | + onSelect={() => { |
| 114 | + closeCommandPanel(); |
| 115 | + insertComponent(component); |
| 116 | + }} |
| 117 | + > |
| 118 | + <CommandIcon |
| 119 | + dangerouslySetInnerHTML={{ __html: meta.icon }} |
| 120 | + ></CommandIcon> |
| 121 | + <Text variant="labelsTitleCase"> |
| 122 | + {getInstanceLabel({ component }, meta)}{" "} |
| 123 | + <Text as="span" color="moreSubtle"> |
| 124 | + ({humanizeString(meta.category ?? "")}) |
| 125 | + </Text> |
| 126 | + </Text> |
| 127 | + </CommandItem> |
| 128 | + ); |
| 129 | + })} |
| 130 | + </CommandGroup> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +const getBreakpointLabel = (breakpoint: Breakpoint) => { |
| 135 | + let label = "All Sizes"; |
| 136 | + if (breakpoint.minWidth !== undefined) { |
| 137 | + label = `≥ ${breakpoint.minWidth} PX`; |
| 138 | + } |
| 139 | + if (breakpoint.maxWidth !== undefined) { |
| 140 | + label = `≤ ${breakpoint.maxWidth} PX`; |
| 141 | + } |
| 142 | + return `${breakpoint.label}: ${label}`; |
| 143 | +}; |
| 144 | + |
| 145 | +const BreakpointsGroup = () => { |
| 146 | + const breakpoints = useStore($breakpoints); |
| 147 | + const sortedBreakpoints = Array.from(breakpoints.values()).sort(compareMedia); |
| 148 | + const selectedBreakpoint = useStore($selectedBreakpoint); |
| 149 | + return ( |
| 150 | + <CommandGroup |
| 151 | + heading={<CommandGroupHeading>Breakpoints</CommandGroupHeading>} |
| 152 | + > |
| 153 | + {sortedBreakpoints.map( |
| 154 | + (breakpoint, index) => |
| 155 | + breakpoint.id !== selectedBreakpoint?.id && ( |
| 156 | + <CommandItem |
| 157 | + key={breakpoint.id} |
| 158 | + keywords={["Breakpoints"]} |
| 159 | + onSelect={() => { |
| 160 | + closeCommandPanel({ restoreFocus: true }); |
| 161 | + $selectedBreakpointId.set(breakpoint.id); |
| 162 | + setCanvasWidth(breakpoint.id); |
| 163 | + }} |
| 164 | + > |
| 165 | + <CommandIcon></CommandIcon> |
| 166 | + <Text variant="labelsTitleCase"> |
| 167 | + {getBreakpointLabel(breakpoint)} |
| 168 | + </Text> |
| 169 | + <Kbd value={[(index + 1).toString()]} /> |
| 170 | + </CommandItem> |
| 171 | + ) |
| 172 | + )} |
| 173 | + </CommandGroup> |
| 174 | + ); |
| 175 | +}; |
| 176 | + |
| 177 | +const PagesGroup = () => { |
| 178 | + const pagesData = useStore($pages); |
| 179 | + const selectedPage = useStore($selectedPage); |
| 180 | + if (pagesData === undefined) { |
| 181 | + return; |
| 182 | + } |
| 183 | + const pages = [pagesData.homePage, ...pagesData.pages]; |
| 184 | + return ( |
| 185 | + <CommandGroup heading={<CommandGroupHeading>Pages</CommandGroupHeading>}> |
| 186 | + {pages.map( |
| 187 | + (page) => |
| 188 | + page.id !== selectedPage?.id && ( |
| 189 | + <CommandItem |
| 190 | + key={page.id} |
| 191 | + keywords={["pages"]} |
| 192 | + onSelect={() => { |
| 193 | + closeCommandPanel(); |
| 194 | + selectPage(page.id); |
| 195 | + }} |
| 196 | + > |
| 197 | + <CommandIcon></CommandIcon> |
| 198 | + <Text variant="labelsTitleCase">{page.name}</Text> |
| 199 | + </CommandItem> |
| 200 | + ) |
| 201 | + )} |
| 202 | + </CommandGroup> |
| 203 | + ); |
| 204 | +}; |
| 205 | + |
| 206 | +const CommandDialogContent = () => { |
| 207 | + return ( |
| 208 | + <> |
| 209 | + <CommandInput /> |
| 210 | + <Flex direction="column" css={{ maxHeight: 300 }}> |
| 211 | + <ScrollArea> |
| 212 | + <CommandList> |
| 213 | + <ComponentsGroup /> |
| 214 | + <BreakpointsGroup /> |
| 215 | + <PagesGroup /> |
| 216 | + </CommandList> |
| 217 | + </ScrollArea> |
| 218 | + </Flex> |
| 219 | + </> |
| 220 | + ); |
| 221 | +}; |
| 222 | + |
| 223 | +export const CommandPanel = () => { |
| 224 | + const isOpen = useStore($commandPanel) !== undefined; |
| 225 | + |
| 226 | + if (isOpen === false) { |
| 227 | + return; |
| 228 | + } |
| 229 | + return ( |
| 230 | + <CommandDialog |
| 231 | + open={isOpen} |
| 232 | + onOpenChange={() => closeCommandPanel({ restoreFocus: true })} |
| 233 | + > |
| 234 | + <CommandDialogContent /> |
| 235 | + </CommandDialog> |
| 236 | + ); |
| 237 | +}; |
0 commit comments