feat: more editor shortcuts (#338)

This commit is contained in:
Xuezhou Dai
2023-04-14 21:41:50 +01:00
committed by GitHub
parent eaaaa193fd
commit 68cee2aaef
20 changed files with 192 additions and 23 deletions
+15 -2
View File
@@ -1,23 +1,36 @@
import { useEffect, useRef } from "react"
import { useTranslation } from "react-i18next"
import { useMediaStore } from "~/hooks/useDarkMode"
export const EmojiPicker: React.FC<{
onEmojiSelect: (e: any) => void
}> = ({ onEmojiSelect }) => {
const ref = useRef<any>()
const isDark = useMediaStore((state) => state.isDark)
useEffect(() => {
import("emoji-mart").then(async (EmojiMart) => {
const response = await fetch(
"https://cdn.jsdelivr.net/npm/@emoji-mart/data",
"https://fastly.jsdelivr.net/npm/@emoji-mart/data",
)
const data = await response.json()
new EmojiMart.Picker({
onEmojiSelect,
data,
ref,
theme: isDark ? "dark" : "light",
})
})
}, [])
return <div ref={ref}></div>
const { t } = useTranslation("common")
return (
<div
ref={ref}
className="min-w-[352px] min-h-[170px] bg-white border-border border rounded-[10px] flex justify-center items-center"
>
<span>{t("Loading")}</span>
</div>
)
}
+81 -4
View File
@@ -1,9 +1,11 @@
import { EditorView } from "@codemirror/view"
import { cn } from "~/lib/utils"
import { Dispatch, FC, SetStateAction } from "react"
import { FC, useState } from "react"
import { ICommand } from "~/editor"
import { Tooltip } from "./Tooltip"
import { useTranslation } from "next-i18next"
import { Popover } from "@headlessui/react"
import { usePopper } from "react-popper"
export interface EditorToolbarProps {
view?: EditorView
@@ -15,22 +17,97 @@ enum ToolbarMode {
Preview,
}
type IToolbarItemProps = ICommand<any> & { view?: EditorView }
const ToolbarItemWithPopover: FC<IToolbarItemProps> = ({
name,
icon,
label,
execute,
ui,
view,
}) => {
const { t } = useTranslation("dashboard")
let [popoverButtonRef, setPopoverButtonRef] =
useState<HTMLButtonElement | null>(null)
let [popoverPanelRef, setPopoverPanelRef] = useState<HTMLDivElement | null>(
null,
)
let { styles: popStyles, attributes: popAttributes } = usePopper(
popoverButtonRef,
popoverPanelRef,
)
return (
<Popover key={name}>
{({ open }: { open: boolean }) => (
<>
<Tooltip key={name} label={t(label)} placement="bottom">
<Popover.Button
key={name}
title={name}
className="w-9 h-9 transition-colors text-lg border border-transparent rounded flex items-center justify-center text-zinc-500 group-hover:text-zinc-600 hover:text-zinc-500 hover:border-zinc-300 hover:bg-zinc-100"
ref={setPopoverButtonRef}
>
<span className={icon}></span>
</Popover.Button>
</Tooltip>
<Popover.Panel
ref={setPopoverPanelRef}
className="z-10"
style={popStyles.popper}
{...popAttributes.popper}
>
{ui &&
view &&
ui({
transferPayload: (payload) => {
view &&
execute({
view,
options: { container: view.dom },
payload,
})
},
view,
})}
</Popover.Panel>
</>
)}
</Popover>
)
}
export const EditorToolbar: FC<EditorToolbarProps> = ({ view, toolbars }) => {
const { t } = useTranslation("dashboard")
const renderToolbar =
(mode: ToolbarMode) =>
// eslint-disable-next-line react/display-name
({ name, icon, label, execute }: ICommand) => {
return (
({ name, icon, label, execute, ui }: ICommand) => {
return ui ? (
<ToolbarItemWithPopover
key={name}
name={name}
icon={icon}
label={label}
execute={execute}
ui={ui}
view={view}
/>
) : (
<Tooltip key={name} label={t(label)} placement="bottom">
<button
key={name}
title={name}
type="button"
className={
"w-9 h-9 transition-colors text-lg border border-transparent rounded flex items-center justify-center text-zinc-500 group-hover:text-zinc-600 hover:text-zinc-500 hover:border-zinc-300 hover:bg-zinc-100"
}
onClick={() => {
view && execute(view, { container: view.dom })
view &&
execute({
view,
options: { container: view.dom },
})
}}
>
<span className={icon}></span>