fix: editor toolbar re-render (#358)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React from "react"
|
||||
import React, { memo } from "react"
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
export const ButtonGroup: React.FC<{ children: React.ReactNode }> = ({
|
||||
@@ -31,48 +31,50 @@ type ButtonProps = {
|
||||
rounded?: "full" | "lg"
|
||||
}
|
||||
|
||||
export const Button = React.forwardRef<
|
||||
HTMLButtonElement,
|
||||
React.ButtonHTMLAttributes<HTMLButtonElement> & ButtonProps
|
||||
>(function Button(
|
||||
{
|
||||
type,
|
||||
children,
|
||||
className,
|
||||
isLoading,
|
||||
isDisabled,
|
||||
isBlock,
|
||||
variant,
|
||||
variantColor,
|
||||
outlineColor,
|
||||
size,
|
||||
rounded,
|
||||
isAutoWidth,
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<button
|
||||
{...props}
|
||||
ref={ref}
|
||||
type={type || "button"}
|
||||
disabled={isDisabled || isLoading}
|
||||
className={cn(
|
||||
className,
|
||||
"button",
|
||||
isLoading && "is-loading",
|
||||
isBlock && `is-block`,
|
||||
variantColor && `is-${variantColor}`,
|
||||
variant === "outline" && outlineColor && `is-outline-${outlineColor}`,
|
||||
isDisabled && `is-disabled`,
|
||||
isAutoWidth && `is-auto-width`,
|
||||
size && `is-${size}`,
|
||||
`is-${variant || "primary"}`,
|
||||
rounded === "full" ? "rounded-full" : "rounded-lg",
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
})
|
||||
export const Button = memo(
|
||||
React.forwardRef<
|
||||
HTMLButtonElement,
|
||||
React.ButtonHTMLAttributes<HTMLButtonElement> & ButtonProps
|
||||
>(function Button(
|
||||
{
|
||||
type,
|
||||
children,
|
||||
className,
|
||||
isLoading,
|
||||
isDisabled,
|
||||
isBlock,
|
||||
variant,
|
||||
variantColor,
|
||||
outlineColor,
|
||||
size,
|
||||
rounded,
|
||||
isAutoWidth,
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<button
|
||||
{...props}
|
||||
ref={ref}
|
||||
type={type || "button"}
|
||||
disabled={isDisabled || isLoading}
|
||||
className={cn(
|
||||
className,
|
||||
"button",
|
||||
isLoading && "is-loading",
|
||||
isBlock && `is-block`,
|
||||
variantColor && `is-${variantColor}`,
|
||||
variant === "outline" && outlineColor && `is-outline-${outlineColor}`,
|
||||
isDisabled && `is-disabled`,
|
||||
isAutoWidth && `is-auto-width`,
|
||||
size && `is-${size}`,
|
||||
`is-${variant || "primary"}`,
|
||||
rounded === "full" ? "rounded-full" : "rounded-lg",
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { EditorView } from "@codemirror/view"
|
||||
import { cn } from "~/lib/utils"
|
||||
import { FC, useState } from "react"
|
||||
import { FC, memo, useCallback, useState } from "react"
|
||||
import { ICommand } from "~/editor"
|
||||
import { Tooltip } from "./Tooltip"
|
||||
import { useTranslation } from "next-i18next"
|
||||
@@ -77,50 +76,56 @@ const ToolbarItemWithPopover: FC<IToolbarItemProps> = ({
|
||||
)
|
||||
}
|
||||
|
||||
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, 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,
|
||||
options: { container: view.dom },
|
||||
})
|
||||
}}
|
||||
>
|
||||
<span className={icon}></span>
|
||||
</button>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
export const EditorToolbar: FC<EditorToolbarProps> = memo(
|
||||
({ view, toolbars }) => {
|
||||
const { t } = useTranslation("dashboard")
|
||||
const renderToolbar = useCallback(
|
||||
(mode: ToolbarMode) =>
|
||||
// eslint-disable-next-line react/display-name
|
||||
({ 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,
|
||||
options: { container: view.dom },
|
||||
})
|
||||
}}
|
||||
>
|
||||
<span className={icon}></span>
|
||||
</button>
|
||||
</Tooltip>
|
||||
)
|
||||
},
|
||||
[view, t],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="flex group">
|
||||
<div className="flex-1 flex space-x-1">
|
||||
{toolbars?.map(renderToolbar(ToolbarMode.Normal))}
|
||||
return (
|
||||
<div className="flex group">
|
||||
<div className="flex-1 flex space-x-1">
|
||||
{toolbars?.map(renderToolbar(ToolbarMode.Normal))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
EditorToolbar.displayName = "EditorToolbar"
|
||||
|
||||
+19
-13
@@ -10,6 +10,7 @@ import "dayjs/locale/en"
|
||||
import "dayjs/locale/zh"
|
||||
import "dayjs/locale/zh-tw"
|
||||
import "dayjs/locale/ja"
|
||||
import { useMemo } from "react"
|
||||
|
||||
dayjs.extend(localizedFormat)
|
||||
dayjs.extend(utc)
|
||||
@@ -19,18 +20,23 @@ dayjs.extend(relativeTime)
|
||||
|
||||
export function useDate() {
|
||||
const { i18n } = useTranslation()
|
||||
dayjs.locale(i18n.resolvedLanguage)
|
||||
|
||||
return {
|
||||
dayjs,
|
||||
formatDate: (date: string | Date, format = "ll", timezone?: string) => {
|
||||
return dayjs(date).tz(timezone).format(format)
|
||||
},
|
||||
formatToISO: (date: string | Date) => {
|
||||
return dayjs(date).toISOString()
|
||||
},
|
||||
inLocalTimezone: (date: string | Date) => {
|
||||
return dayjs(date).tz().toDate()
|
||||
},
|
||||
}
|
||||
const memoizedDateUtils = useMemo(() => {
|
||||
dayjs.locale(i18n.resolvedLanguage)
|
||||
|
||||
return {
|
||||
dayjs,
|
||||
formatDate: (date: string | Date, format = "ll", timezone?: string) => {
|
||||
return dayjs(date).tz(timezone).format(format)
|
||||
},
|
||||
formatToISO: (date: string | Date) => {
|
||||
return dayjs(date).toISOString()
|
||||
},
|
||||
inLocalTimezone: (date: string | Date) => {
|
||||
return dayjs(date).tz().toDate()
|
||||
},
|
||||
}
|
||||
}, [i18n.resolvedLanguage])
|
||||
|
||||
return memoizedDateUtils
|
||||
}
|
||||
|
||||
@@ -198,94 +198,111 @@ export default function SubdomainEditor() {
|
||||
|
||||
const [isCheersOpen, setIsCheersOpen] = useState(false)
|
||||
|
||||
const ExtraProperties = (
|
||||
<div className="h-full overflow-auto flex-shrink-0 w-[280px] border-l bg-zinc-50 p-5 space-y-5">
|
||||
<div>
|
||||
<Input
|
||||
type="datetime-local"
|
||||
label={t("Publish at") || ""}
|
||||
isBlock
|
||||
name="publishAt"
|
||||
id="publishAt"
|
||||
value={getInputDatetimeValue(values.publishedAt, date.dayjs)}
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||
try {
|
||||
const value = date.inLocalTimezone(e.target.value).toISOString()
|
||||
updateValue("publishedAt", value)
|
||||
} catch (error) {}
|
||||
}}
|
||||
help={t(
|
||||
`This ${
|
||||
isPost ? "post" : "page"
|
||||
} will be accessible from this time`,
|
||||
)}
|
||||
/>
|
||||
const extraProperties = useMemo(
|
||||
() => (
|
||||
<div className="h-full overflow-auto flex-shrink-0 w-[280px] border-l bg-zinc-50 p-5 space-y-5">
|
||||
<div>
|
||||
<Input
|
||||
type="datetime-local"
|
||||
label={t("Publish at") || ""}
|
||||
isBlock
|
||||
name="publishAt"
|
||||
id="publishAt"
|
||||
value={getInputDatetimeValue(values.publishedAt, date.dayjs)}
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||
try {
|
||||
const value = date.inLocalTimezone(e.target.value).toISOString()
|
||||
updateValue("publishedAt", value)
|
||||
} catch (error) {}
|
||||
}}
|
||||
help={t(
|
||||
`This ${
|
||||
isPost ? "post" : "page"
|
||||
} will be accessible from this time`,
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
name="slug"
|
||||
value={values.slug}
|
||||
placeholder={defaultSlug}
|
||||
label={t(`${isPost ? "Post" : "Page"} slug`) || ""}
|
||||
id="slug"
|
||||
isBlock
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
||||
updateValue("slug", e.target.value)
|
||||
}
|
||||
help={
|
||||
<>
|
||||
{(values.slug || defaultSlug) && (
|
||||
<>
|
||||
{t(
|
||||
`This ${isPost ? "post" : "page"} will be accessible at`,
|
||||
)}{" "}
|
||||
<UniLink
|
||||
href={`${getSiteLink({
|
||||
subdomain,
|
||||
domain: site.data?.custom_domain,
|
||||
})}/${encodeURIComponent(values.slug || defaultSlug)}`}
|
||||
className="hover:underline"
|
||||
>
|
||||
{getSiteLink({
|
||||
subdomain,
|
||||
domain: site.data?.custom_domain,
|
||||
noProtocol: true,
|
||||
})}
|
||||
/{encodeURIComponent(values.slug || defaultSlug)}
|
||||
</UniLink>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
name="tags"
|
||||
value={values.tags}
|
||||
label={t("Tags") || ""}
|
||||
id="tags"
|
||||
isBlock
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
||||
updateValue("tags", e.target.value)
|
||||
}
|
||||
help={t("Separate multiple tags with English commas") + ` ","`}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
label={t("Excerpt") || ""}
|
||||
isBlock
|
||||
name="excerpt"
|
||||
id="excerpt"
|
||||
value={values.excerpt}
|
||||
multiline
|
||||
rows={5}
|
||||
onChange={(e: ChangeEvent<HTMLTextAreaElement>) => {
|
||||
updateValue("excerpt", e.target.value)
|
||||
}}
|
||||
help={t("Leave it blank to use auto-generated excerpt")}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
name="slug"
|
||||
value={values.slug}
|
||||
placeholder={defaultSlug}
|
||||
label={t(`${isPost ? "Post" : "Page"} slug`) || ""}
|
||||
id="slug"
|
||||
isBlock
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
||||
updateValue("slug", e.target.value)
|
||||
}
|
||||
help={
|
||||
<>
|
||||
{(values.slug || defaultSlug) && (
|
||||
<>
|
||||
{t(`This ${isPost ? "post" : "page"} will be accessible at`)}{" "}
|
||||
<UniLink
|
||||
href={`${getSiteLink({
|
||||
subdomain,
|
||||
domain: site.data?.custom_domain,
|
||||
})}/${encodeURIComponent(values.slug || defaultSlug)}`}
|
||||
className="hover:underline"
|
||||
>
|
||||
{getSiteLink({
|
||||
subdomain,
|
||||
domain: site.data?.custom_domain,
|
||||
noProtocol: true,
|
||||
})}
|
||||
/{encodeURIComponent(values.slug || defaultSlug)}
|
||||
</UniLink>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
name="tags"
|
||||
value={values.tags}
|
||||
label={t("Tags") || ""}
|
||||
id="tags"
|
||||
isBlock
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
||||
updateValue("tags", e.target.value)
|
||||
}
|
||||
help={t("Separate multiple tags with English commas") + ` ","`}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
label={t("Excerpt") || ""}
|
||||
isBlock
|
||||
name="excerpt"
|
||||
id="excerpt"
|
||||
value={values.excerpt}
|
||||
multiline
|
||||
rows={5}
|
||||
onChange={(e: ChangeEvent<HTMLTextAreaElement>) => {
|
||||
updateValue("excerpt", e.target.value)
|
||||
}}
|
||||
help={t("Leave it blank to use auto-generated excerpt")}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
[
|
||||
date,
|
||||
defaultSlug,
|
||||
isPost,
|
||||
site.data?.custom_domain,
|
||||
subdomain,
|
||||
t,
|
||||
updateValue,
|
||||
values.excerpt,
|
||||
values.publishedAt,
|
||||
values.slug,
|
||||
values.tags,
|
||||
],
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -495,6 +512,15 @@ export default function SubdomainEditor() {
|
||||
[onScroll],
|
||||
)
|
||||
|
||||
const onPreviewButtonClick = useCallback(() => {
|
||||
window.open(
|
||||
`/_site/${subdomain}/preview/${draftKey.replace(
|
||||
`draft-${subdomain}-`,
|
||||
"",
|
||||
)}`,
|
||||
)
|
||||
}, [draftKey, subdomain])
|
||||
|
||||
return (
|
||||
<>
|
||||
<DashboardMain fullWidth>
|
||||
@@ -560,15 +586,8 @@ export default function SubdomainEditor() {
|
||||
published={visibility !== PageVisibilityEnum.Draft}
|
||||
isRendering={isRendering}
|
||||
renderPage={setIsRendering}
|
||||
propertiesWidget={ExtraProperties}
|
||||
previewPage={() => {
|
||||
window.open(
|
||||
`/_site/${subdomain}/preview/${draftKey.replace(
|
||||
`draft-${subdomain}-`,
|
||||
"",
|
||||
)}`,
|
||||
)
|
||||
}}
|
||||
propertiesWidget={extraProperties}
|
||||
previewPage={onPreviewButtonClick}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
@@ -585,17 +604,7 @@ export default function SubdomainEditor() {
|
||||
>
|
||||
{t(visibility as string)}
|
||||
</span>
|
||||
<Button
|
||||
isAutoWidth
|
||||
onClick={() => {
|
||||
window.open(
|
||||
`/_site/${subdomain}/preview/${draftKey.replace(
|
||||
`draft-${subdomain}-`,
|
||||
"",
|
||||
)}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
<Button isAutoWidth onClick={onPreviewButtonClick}>
|
||||
{t("Preview")}
|
||||
</Button>
|
||||
<PublishButton
|
||||
@@ -690,7 +699,7 @@ export default function SubdomainEditor() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{!isMobileLayout && ExtraProperties}
|
||||
{!isMobileLayout && extraProperties}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user