Skip to content

Commit

Permalink
feat: update feed content render
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 26, 2024
1 parent cdeb419 commit 41b3e17
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 39 deletions.
1 change: 1 addition & 0 deletions icons/mgc/arrow_right_up_cute_re.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 36 additions & 5 deletions src/renderer/src/components/ui/link/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Tooltip, TooltipContent, TooltipTrigger } from "../tooltip"
import {
Tooltip,
TooltipContent,
TooltipPortal,
TooltipTrigger,
} from "../tooltip"

interface LinkProps {
href: string
Expand All @@ -10,10 +15,36 @@ interface LinkProps {
export const LinkWithTooltip = (props: LinkProps) => (
<Tooltip delayDuration={0}>
<TooltipTrigger asChild>
<a href={props.href} title={props.title} target={props.target}>{props.children}</a>
<a href={props.href} title={props.title} target={props.target}>
{props.children}
</a>
</TooltipTrigger>
<TooltipContent align="start" side="bottom">
{props.href}
</TooltipContent>
<TooltipPortal>
<TooltipContent align="start" className="break-all" side="bottom">
{props.href}
</TooltipContent>
</TooltipPortal>
</Tooltip>
)

export const MarkdownLink = (props: LinkProps) => (
<Tooltip delayDuration={0}>
<TooltipTrigger asChild>
<a
className="follow-link--underline font-semibold text-foreground no-underline"
href={props.href}
title={props.title}
target="_blank"
>
{props.children}

{typeof props.children === "string" && <i className="i-mgc-arrow-right-up-cute-re size-[0.9em] translate-y-[2px] opacity-70" />}
</a>
</TooltipTrigger>
<TooltipPortal>
<TooltipContent align="start" className="break-all" side="bottom">
{props.href}
</TooltipContent>
</TooltipPortal>
</Tooltip>
)
53 changes: 31 additions & 22 deletions src/renderer/src/components/ui/media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,39 @@ import { useEventCallback } from "usehooks-ts"
import { usePreviewMedia } from "./media/hooks"

const failedList = new Set<string | undefined>()
export type ImageProps =
| (ImgHTMLAttributes<HTMLImageElement> & {
proxy?: {
width: number
height: number
}
disableContextMenu?: boolean
popper?: boolean
type: "photo"
previewImageUrl?: string
})
| (VideoHTMLAttributes<HTMLVideoElement> & {
proxy?: {
width: number
height: number
}
disableContextMenu?: boolean
popper?: boolean
type: "video"
previewImageUrl?: string
})
const MediaImpl: FC<ImageProps> = ({

type BaseProps = {
mediaContainerClassName?: string
}
export type MediaProps = BaseProps &
(
| (ImgHTMLAttributes<HTMLImageElement> & {
proxy?: {
width: number
height: number
}
disableContextMenu?: boolean
popper?: boolean
type: "photo"
previewImageUrl?: string
})
| (VideoHTMLAttributes<HTMLVideoElement> & {
proxy?: {
width: number
height: number
}
disableContextMenu?: boolean
popper?: boolean
type: "video"
previewImageUrl?: string
})
)
const MediaImpl: FC<MediaProps> = ({
className,
proxy,
disableContextMenu,
popper = false,
mediaContainerClassName,
...props
}) => {
const { src, style, type, previewImageUrl, ...rest } = props
Expand Down Expand Up @@ -89,6 +96,7 @@ const MediaImpl: FC<ImageProps> = ({
!(props.width || props.height) && "size-full",
"bg-stone-100 object-cover",
popper && "cursor-zoom-in",
mediaContainerClassName,
)}
src={imgSrc}
onClick={handleClick}
Expand Down Expand Up @@ -136,6 +144,7 @@ const MediaImpl: FC<ImageProps> = ({
hidden && "hidden",
!(props.width || props.height) && "size-full",
"relative bg-stone-100 object-cover",
mediaContainerClassName,
)}
onClick={handleClick}
>
Expand Down
30 changes: 18 additions & 12 deletions src/renderer/src/lib/parse-html.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Checkbox } from "@renderer/components/ui/checkbox"
import { ShikiHighLighter } from "@renderer/components/ui/code-highlighter"
import { LinkWithTooltip } from "@renderer/components/ui/link"
import { MarkdownLink } from "@renderer/components/ui/link"
import { Media } from "@renderer/components/ui/media"
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
import { createElement } from "react"
Expand Down Expand Up @@ -28,10 +28,7 @@ export const parseHtml = async (
...defaultSchema,
tagNames: renderInlineStyle ?
defaultSchema.tagNames :
[
...defaultSchema.tagNames!,
"video",
],
[...defaultSchema.tagNames!, "video"],
attributes: {
...defaultSchema.attributes,

Expand All @@ -58,17 +55,25 @@ export const parseHtml = async (
passNode: true,
components: {
a: ({ node, ...props }) =>
createElement(LinkWithTooltip, { ...props } as any),
createElement(MarkdownLink, { ...props } as any),
img: ({ node, ...props }) => {
if (node?.properties.inline) {
return createElement("img", {
src: props.src,
style: { maxWidth: "100%", display: "inline" },
})
}
return createElement(Media, { ...props, popper: true, type: "photo" })
return createElement(Media, {
...props,
mediaContainerClassName: "w-full rounded",
className: "flex justify-center",

popper: true,
type: "photo",
})
},
video: ({ node, ...props }) => createElement(Media, { ...props, popper: true, type: "video" }),
video: ({ node, ...props }) =>
createElement(Media, { ...props, popper: true, type: "video" }),
p: ({ node, ...props }) => {
if (node?.children) {
for (const item of node.children) {
Expand All @@ -79,10 +84,11 @@ export const parseHtml = async (
}
return createElement("p", undefined, props.children)
},
hr: ({ node, ...props }) => createElement("hr", {
...props,
className: tw`scale-x-50`,
}),
hr: ({ node, ...props }) =>
createElement("hr", {
...props,
className: tw`scale-x-50`,
}),
input: ({ node, ...props }) => {
if (props.type === "checkbox") {
return createElement(Checkbox, {
Expand Down
32 changes: 32 additions & 0 deletions src/renderer/src/styles/tailwind-extend.css
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,35 @@
rgba(0, 0, 0, 0.067) 0px 2px 5px, rgba(0, 0, 0, 0.067) 0px 1px 1px;
}
}

/* Link */
@layer components {
.follow-link--underline {
color: currentColor;
background-image: linear-gradient(
theme(colors.theme.accent.DEFAULT),
theme(colors.theme.accent.DEFAULT)
);
background-size: 0% 1.5px;
background-repeat: no-repeat;
/* NOTE: this won't work with background images */

transition: all 500ms ease;

@apply border-0 no-underline;

background-position: left 1em;

&:hover {
background-size: 100% 1.5px;
text-shadow: 0.05em 0 theme(colors.background),
-0.05em 0 theme(colors.background);

transition: all 250ms ease;
}

&.no-shadow {
text-shadow: none;
}
}
}

0 comments on commit 41b3e17

Please sign in to comment.