-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from topthithu/dev
v0.0.1
- Loading branch information
Showing
39 changed files
with
4,588 additions
and
554 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const postcss = require('rollup-plugin-postcss'); | ||
|
||
module.exports = { | ||
rollup(config, options) { | ||
config.plugins.push( | ||
postcss({ | ||
inject: true, | ||
modules: true, | ||
extract: !!options.writeMeta, | ||
}) | ||
); | ||
return config; | ||
}, | ||
}; |
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
Large diffs are not rendered by default.
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
112 changes: 112 additions & 0 deletions
112
src/editor/extensions/command-menu.ext/command-list.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,112 @@ | ||
import { Box, Center, HStack, Icon, Square, Text } from '@chakra-ui/react'; | ||
import { useState, useEffect, forwardRef, useImperativeHandle } from 'react'; | ||
import { HiOutlineArchive } from 'react-icons/hi'; | ||
import styles from './index.module.scss'; | ||
|
||
const CommandList = forwardRef<any, any>((props, ref) => { | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
||
const selectItem = (index: number) => { | ||
const item = props.items[index]; | ||
|
||
if (item) { | ||
props.command({ id: item }); | ||
} | ||
}; | ||
|
||
const upHandler = () => { | ||
setSelectedIndex( | ||
(selectedIndex + props.items.length - 1) % props.items.length | ||
); | ||
}; | ||
|
||
const downHandler = () => { | ||
setSelectedIndex((selectedIndex + 1) % props.items.length); | ||
}; | ||
|
||
const enterHandler = () => { | ||
selectItem(selectedIndex); | ||
}; | ||
|
||
useEffect(() => setSelectedIndex(0), [props.items]); | ||
|
||
useEffect(() => { | ||
const commandElm = document.querySelector( | ||
`#command-suggestion__item-${selectedIndex}` | ||
); | ||
commandElm?.scrollIntoView(); | ||
}, [selectedIndex]); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
onKeyDown: ({ event }: any) => { | ||
if (event.key === 'ArrowUp') { | ||
upHandler(); | ||
return true; | ||
} | ||
|
||
if (event.key === 'ArrowDown') { | ||
downHandler(); | ||
return true; | ||
} | ||
|
||
if (event.key === 'Enter') { | ||
enterHandler(); | ||
return true; | ||
} | ||
|
||
return false; | ||
}, | ||
})); | ||
|
||
return ( | ||
<Box | ||
shadow="base" | ||
rounded="base" | ||
borderWidth={1} | ||
bg="white" | ||
w="300px" | ||
maxH="300px" | ||
overflow="auto" | ||
className={styles['command-list-container']} | ||
> | ||
{props.items.length ? ( | ||
props.items.map((item: any, index: number) => ( | ||
<HStack | ||
id={`command-suggestion__item-${index}`} | ||
key={index} | ||
onClick={() => selectItem(index)} | ||
bg={index === selectedIndex ? 'gray.100' : 'inherit'} | ||
_hover={{ | ||
bg: 'gray.50', | ||
cursor: 'pointer', | ||
}} | ||
align="center" | ||
px={4} | ||
py={2} | ||
> | ||
<Square p={2} borderWidth={1} rounded="base" bg="white"> | ||
<Icon as={item.icon} boxSize={6} color="gray.600" /> | ||
</Square> | ||
<Box flex={1}> | ||
<Text>{item.title}</Text> | ||
{item.desc && ( | ||
<Text color="gray.400" fontSize="sm"> | ||
{item.desc} | ||
</Text> | ||
)} | ||
</Box> | ||
</HStack> | ||
)) | ||
) : ( | ||
<Center flexDir="column" px={4} py={2}> | ||
<Icon as={HiOutlineArchive} boxSize={6} color="gray.500" /> | ||
<Text fontSize="sm" color="gray.400"> | ||
No results | ||
</Text> | ||
</Center> | ||
)} | ||
</Box> | ||
); | ||
}); | ||
|
||
export default CommandList; |
157 changes: 157 additions & 0 deletions
157
src/editor/extensions/command-menu.ext/command-suggestion.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,157 @@ | ||
import tippy from 'tippy.js'; | ||
import { ReactRenderer } from '@tiptap/react'; | ||
import { | ||
MdFormatQuote, | ||
MdLooks3, | ||
MdLooks4, | ||
MdLooks5, | ||
MdLooks6, | ||
MdLooksOne, | ||
MdLooksTwo, | ||
} from 'react-icons/md'; | ||
|
||
import CommandList from './command-list'; | ||
|
||
export const CommandSuggestion = { | ||
items: ({ query }: { query: string }) => { | ||
return [ | ||
{ | ||
title: 'H1', | ||
desc: 'Heading 1', | ||
icon: MdLooksOne, | ||
command: ({ editor, range }: any) => { | ||
editor | ||
.chain() | ||
.focus() | ||
.deleteRange(range) | ||
.setNode('heading', { level: 1 }) | ||
.run(); | ||
}, | ||
}, | ||
{ | ||
title: 'H2', | ||
desc: 'Heading 2', | ||
icon: MdLooksTwo, | ||
command: ({ editor, range }: any) => { | ||
editor | ||
.chain() | ||
.focus() | ||
.deleteRange(range) | ||
.setNode('heading', { level: 2 }) | ||
.run(); | ||
}, | ||
}, | ||
{ | ||
title: 'H3', | ||
desc: 'Heading 3', | ||
icon: MdLooks3, | ||
command: ({ editor, range }: any) => { | ||
editor | ||
.chain() | ||
.focus() | ||
.deleteRange(range) | ||
.setNode('heading', { level: 3 }) | ||
.run(); | ||
}, | ||
}, | ||
{ | ||
title: 'H4', | ||
desc: 'Heading 4', | ||
icon: MdLooks4, | ||
command: ({ editor, range }: any) => { | ||
editor | ||
.chain() | ||
.focus() | ||
.deleteRange(range) | ||
.setNode('heading', { level: 4 }) | ||
.run(); | ||
}, | ||
}, | ||
{ | ||
title: 'H5', | ||
desc: 'Heading 5', | ||
icon: MdLooks5, | ||
command: ({ editor, range }: any) => { | ||
editor | ||
.chain() | ||
.focus() | ||
.deleteRange(range) | ||
.setNode('heading', { level: 5 }) | ||
.run(); | ||
}, | ||
}, | ||
{ | ||
title: 'H6', | ||
desc: 'Heading 6', | ||
icon: MdLooks6, | ||
command: ({ editor, range }: any) => { | ||
editor | ||
.chain() | ||
.focus() | ||
.deleteRange(range) | ||
.setNode('heading', { level: 6 }) | ||
.run(); | ||
}, | ||
}, | ||
{ | ||
title: 'Quote', | ||
desc: 'Capture a quote', | ||
icon: MdFormatQuote, | ||
command: ({ editor, range }: any) => { | ||
editor.chain().focus().deleteRange(range).toggleBlockquote().run(); | ||
}, | ||
}, | ||
] | ||
.filter((item) => | ||
item.title.toLowerCase().startsWith(query.toLowerCase()) | ||
) | ||
.slice(0, 10); | ||
}, | ||
|
||
render: () => { | ||
let component: any; | ||
let popup: any; | ||
|
||
return { | ||
onStart: (props: any) => { | ||
component = new ReactRenderer(CommandList, { | ||
props, | ||
editor: props.editor, | ||
}); | ||
|
||
popup = tippy('body', { | ||
getReferenceClientRect: props.clientRect, | ||
appendTo: () => document.body, | ||
content: component.element, | ||
showOnCreate: true, | ||
interactive: true, | ||
trigger: 'manual', | ||
placement: 'bottom-start', | ||
}); | ||
}, | ||
|
||
onUpdate(props: any) { | ||
component.updateProps(props); | ||
|
||
popup[0].setProps({ | ||
getReferenceClientRect: props.clientRect, | ||
}); | ||
}, | ||
|
||
onKeyDown(props: any) { | ||
if (props.event.key === 'Escape') { | ||
popup[0].hide(); | ||
|
||
return true; | ||
} | ||
|
||
return component.ref?.onKeyDown(props); | ||
}, | ||
|
||
onExit() { | ||
popup[0].destroy(); | ||
component.destroy(); | ||
}, | ||
}; | ||
}, | ||
}; |
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,21 @@ | ||
.command-list-container { | ||
|
||
&::-webkit-scrollbar { | ||
background-color: #fff; | ||
width: 16px; | ||
padding: 0px; | ||
} | ||
|
||
&::-webkit-scrollbar-track { | ||
background-color: #fff; | ||
} | ||
|
||
&::-webkit-scrollbar-thumb { | ||
background-color: #babac0; | ||
border: 4px solid #fff; | ||
} | ||
|
||
&::-webkit-scrollbar-button { | ||
display: none; | ||
} | ||
} |
Oops, something went wrong.