-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7296a68
commit 9bb7b0a
Showing
12 changed files
with
448 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"cSpell.words": [ | ||
"fuzzysort", | ||
"lowlight", | ||
"smilie" | ||
] | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './searchAndReplace' | ||
export * from './smilieReplacer' | ||
export * from './slash-menu' |
86 changes: 86 additions & 0 deletions
86
src/pages/Newtab/components/editor/extensions/slash-menu/CommandList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { stopPrevent } from '../../../../utils' | ||
|
||
import './styles/CommandList.scss' | ||
|
||
interface CommandListProps { | ||
items: any[], | ||
command: Function, | ||
event: any | ||
} | ||
|
||
export const CommandList: React.FC<CommandListProps> = ({ items, command, event }) => { | ||
const [selectedIndex, setSelectedIndex] = useState(0) | ||
|
||
useEffect(() => setSelectedIndex(0), [items]) | ||
|
||
const onKeyDown = (event: KeyboardEvent) => { | ||
if (event.key === 'ArrowUp') { | ||
stopPrevent(event) | ||
upHandler() | ||
return true | ||
} | ||
|
||
if (event.key === 'ArrowDown') { | ||
stopPrevent(event) | ||
downHandler() | ||
return true | ||
} | ||
|
||
if (event.key === 'Enter') { | ||
stopPrevent(event) | ||
enterHandler() | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
useEffect(() => { onKeyDown(event) }, [event]) | ||
|
||
const upHandler = () => { | ||
setSelectedIndex(((selectedIndex + items.length) - 1) % items.length) | ||
} | ||
|
||
const downHandler = () => { | ||
setSelectedIndex((selectedIndex + 1) % items.length) | ||
} | ||
|
||
const enterHandler = () => { | ||
selectItem(selectedIndex) | ||
} | ||
|
||
const selectItem = (index: number) => { | ||
const item = items[index] | ||
|
||
if (item) setTimeout(() => command(item)) | ||
} | ||
|
||
return ( | ||
<div className="items"> | ||
{ | ||
items.length | ||
? ( | ||
<> | ||
{ | ||
items.map((item, index) => { | ||
return ( | ||
<article | ||
className={`item flex ${index === selectedIndex ? 'is-selected' : ''}`} | ||
key={index} | ||
onClick={() => selectItem(index)} | ||
> | ||
<span className='flex align-center gap-8px'> | ||
{item.icon()} <span dangerouslySetInnerHTML={{ __html: item.highlightedTitle || item.title }} /> | ||
</span> | ||
{item.shortcut && <code>{item.shortcut}</code>} | ||
</article> | ||
) | ||
}) | ||
} | ||
</> | ||
) : <div className="item"> No result </div> | ||
} | ||
</div > | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
src/pages/Newtab/components/editor/extensions/slash-menu/command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Extension } from '@tiptap/core' | ||
import Suggestion from '@tiptap/suggestion' | ||
|
||
export const Commands = Extension.create({ | ||
name: 'commands', | ||
|
||
addOptions() { | ||
return { | ||
suggestions: { | ||
char: '/', | ||
command: ({ editor, range, props } : any) => props.command({ editor, range }), | ||
}, | ||
} | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
return [ | ||
Suggestion({ | ||
editor: this.editor, | ||
...this.options.suggestions, | ||
}), | ||
] | ||
}, | ||
}) |
3 changes: 3 additions & 0 deletions
3
src/pages/Newtab/components/editor/extensions/slash-menu/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './CommandList' | ||
export * from './command' | ||
export * from './suggestions' |
29 changes: 29 additions & 0 deletions
29
src/pages/Newtab/components/editor/extensions/slash-menu/styles/CommandList.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
.items { | ||
position: relative; | ||
border-radius: var(--nextui-radii-sm); | ||
background: var(--nextui-colors-background); | ||
color: var(--nextui-colors-text); | ||
box-shadow: var(--nextui-shadows-md); | ||
padding: 6px; | ||
box-sizing: border-box; | ||
} | ||
|
||
.item { | ||
width: 100%; | ||
border-radius: 0.4rem; | ||
align-items: center; | ||
gap: 8px; | ||
padding: 6px 8px; | ||
box-sizing: border-box; | ||
justify-content: space-between; | ||
|
||
svg { | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
&.is-selected { | ||
background-color: var(--nextui-colors-selection); | ||
} | ||
} |
Oops, something went wrong.