|
1 |
| -import { LinkCapture, parseSnippet, Snippet } from '@mexit/core' |
| 1 | +import { LinkCapture, parseSnippet, QuickLinkType, Snippet } from '@mexit/core' |
2 | 2 | import React, { useEffect, useRef, useState } from 'react'
|
3 | 3 | import { Icon } from '@iconify/react'
|
4 | 4 | import fuzzysort from 'fuzzysort'
|
5 | 5 |
|
6 | 6 | import { useSnippets } from '../../Hooks/useSnippets'
|
7 | 7 | import { useSputlitContext, VisualState } from '../../Hooks/useSputlitContext'
|
8 |
| -import { ComboboxItem, ComboboxRoot, Img, ItemCenterWrapper, ItemDesc, ItemTitle } from './styled' |
| 8 | +import { ComboboxItem, ComboboxRoot, ItemCenterWrapper, ItemDesc, ItemRightIcons, ItemTitle } from './styled' |
9 | 9 | import { useShortenerStore } from '../../Hooks/useShortener'
|
10 | 10 | import { getDibbaText } from '../../Utils/getDibbaText'
|
| 11 | +import { ComboboxShortcuts, ComboSeperator, DisplayShortcut, ShortcutText } from '@mexit/shared' |
| 12 | +import { ElementTypeBasedShortcut } from '../../Editor/components/ComboBox' |
| 13 | +import EditorPreviewRenderer from '../EditorPreviewRenderer' |
| 14 | +import usePointerMovedSinceMount from '../../Hooks/usePointerMovedSinceMount' |
11 | 15 |
|
12 | 16 | // This functions provides the 'to be' range and text content
|
13 | 17 | // Needed because keydown event happens before there is a selection or content change
|
@@ -40,16 +44,21 @@ export default function Dibba() {
|
40 | 44 |
|
41 | 45 | const linkCaptures = useShortenerStore((store) => store.linkCaptures)
|
42 | 46 | const snippets = useSnippets().getSnippets()
|
| 47 | + const pointerMoved = usePointerMovedSinceMount() |
43 | 48 |
|
44 | 49 | const data = [
|
| 50 | + // TODO: fix link captures after discussion |
45 | 51 | ...linkCaptures.map((item) => ({
|
46 | 52 | id: item.shortenedURL,
|
47 | 53 | title: item.short,
|
48 |
| - // TODO: find a way to use favicons but single array of results |
49 | 54 | icon: 'ri:link',
|
50 | 55 | content: item.shortenedURL
|
51 | 56 | })),
|
52 |
| - ...snippets |
| 57 | + ...snippets.map((item) => ({ |
| 58 | + type: QuickLinkType.snippet, |
| 59 | + icon: item?.icon || 'ri:quill-pen-line', |
| 60 | + ...item |
| 61 | + })) |
53 | 62 | ]
|
54 | 63 |
|
55 | 64 | const insertSnippet = (item: Snippet) => {
|
@@ -86,7 +95,7 @@ export default function Dibba() {
|
86 | 95 | console.log(error)
|
87 | 96 | }
|
88 | 97 |
|
89 |
| - if (item.icon === 'ri:quill-pen-line') { |
| 98 | + if (item.type === QuickLinkType.snippet) { |
90 | 99 | insertSnippet(item as Snippet)
|
91 | 100 | } else if (item.icon === 'ri:link') {
|
92 | 101 | // TODO: transform again to type linkCapture
|
@@ -174,32 +183,65 @@ export default function Dibba() {
|
174 | 183 | setOffsetTop(window.innerHeight < top + dibbaRef.current.clientHeight)
|
175 | 184 | })
|
176 | 185 |
|
| 186 | + const listItem = results[activeIndex] |
| 187 | + const itemShortcut = listItem?.type ? ElementTypeBasedShortcut[listItem?.type] : undefined |
| 188 | + |
177 | 189 | return (
|
178 | 190 | <ComboboxRoot
|
179 | 191 | id="dibba-container"
|
180 | 192 | ref={dibbaRef}
|
181 | 193 | top={top}
|
182 | 194 | left={left}
|
183 | 195 | offsetTop={offsetTop}
|
184 |
| - offsetRight={window.innerWidth < left + 225} |
| 196 | + offsetRight={window.innerWidth < left + 500} |
185 | 197 | isOpen={dibbaState.visualState === VisualState.showing}
|
186 | 198 | >
|
187 |
| - {results.map((item, index) => { |
188 |
| - return ( |
189 |
| - <ComboboxItem |
190 |
| - key={index} |
191 |
| - highlighted={index === activeIndex} |
192 |
| - onMouseDown={() => { |
193 |
| - handleClick(item) |
194 |
| - }} |
195 |
| - > |
196 |
| - <Icon height={18} key={item.id} icon={item.icon} /> |
197 |
| - <ItemCenterWrapper> |
198 |
| - <ItemTitle>{item.title}</ItemTitle> |
199 |
| - </ItemCenterWrapper> |
200 |
| - </ComboboxItem> |
201 |
| - ) |
202 |
| - })} |
| 199 | + <div style={{ flex: 1 }}> |
| 200 | + {results.map((item, index) => { |
| 201 | + return ( |
| 202 | + <ComboboxItem |
| 203 | + key={index} |
| 204 | + highlighted={index === activeIndex} |
| 205 | + onMouseDown={() => { |
| 206 | + handleClick(item) |
| 207 | + }} |
| 208 | + onPointerMove={() => pointerMoved && setActiveIndex(index)} |
| 209 | + > |
| 210 | + <Icon height={18} key={item.id} icon={item.icon} /> |
| 211 | + <ItemCenterWrapper> |
| 212 | + <ItemTitle>{item.title}</ItemTitle> |
| 213 | + {item.desc && <ItemDesc>{item.desc}</ItemDesc>} |
| 214 | + </ItemCenterWrapper> |
| 215 | + {item.rightIcons && ( |
| 216 | + <ItemRightIcons> |
| 217 | + {item.rightIcons.map((i: string) => ( |
| 218 | + <Icon key={item.key + i} icon={i} /> |
| 219 | + ))} |
| 220 | + </ItemRightIcons> |
| 221 | + )} |
| 222 | + </ComboboxItem> |
| 223 | + ) |
| 224 | + })} |
| 225 | + {itemShortcut && ( |
| 226 | + <ComboboxShortcuts> |
| 227 | + {Object.entries(itemShortcut).map(([key, shortcut]) => { |
| 228 | + return ( |
| 229 | + <ShortcutText key={key}> |
| 230 | + <DisplayShortcut shortcut={shortcut.keystrokes} /> <div className="text">{shortcut.title}</div> |
| 231 | + </ShortcutText> |
| 232 | + ) |
| 233 | + })} |
| 234 | + </ComboboxShortcuts> |
| 235 | + )} |
| 236 | + </div> |
| 237 | + |
| 238 | + {listItem?.content && ( |
| 239 | + <ComboSeperator> |
| 240 | + <section> |
| 241 | + <EditorPreviewRenderer noMouseEvents content={listItem.content} editorId={listItem.id} /> |
| 242 | + </section> |
| 243 | + </ComboSeperator> |
| 244 | + )} |
203 | 245 | </ComboboxRoot>
|
204 | 246 | )
|
205 | 247 | }
|
0 commit comments