From c8205f535e588dcc872cdd4803afbe3b7e062274 Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Wed, 18 Oct 2023 00:25:53 +0200 Subject: [PATCH] Link click behavior and colors --- frontend/packages/app/pages/publication.tsx | 8 ++ .../shared/src/static/static-renderer.tsx | 129 +++++++++--------- 2 files changed, 75 insertions(+), 62 deletions(-) diff --git a/frontend/packages/app/pages/publication.tsx b/frontend/packages/app/pages/publication.tsx index fe30598a53..d8c817222f 100644 --- a/frontend/packages/app/pages/publication.tsx +++ b/frontend/packages/app/pages/publication.tsx @@ -28,6 +28,7 @@ import { import {VersionChangesInfo} from '../components/version-changes-info' import {usePublicationInContext} from '../models/publication' import {DocumentPlaceholder} from './document-placeholder' +import {useOpenUrl} from '../open-url' export default function PublicationPage() { const route = useNavRoute() @@ -55,6 +56,8 @@ export default function PublicationPage() { publication.status == 'success' ? docId : undefined, ) + const openUrl = useOpenUrl() + if (publication.data) { return ( { + e.preventDefault() + e.stopPropagation() + openUrl(href) + }} > diff --git a/frontend/packages/shared/src/static/static-renderer.tsx b/frontend/packages/shared/src/static/static-renderer.tsx index 21366abc9c..d823279192 100644 --- a/frontend/packages/shared/src/static/static-renderer.tsx +++ b/frontend/packages/shared/src/static/static-renderer.tsx @@ -43,30 +43,35 @@ let blockVerticalPadding: FontSizeTokens = '$4' let blockHorizontalPadding: FontSizeTokens = '$4' let blockBorderRadius = '$3' -export type StaticPublicationContextValue = EntityComponentsRecord | null - -export const staticPublicationContext = - createContext(null) - -export type StaticEmbedProps = StaticBlockProps & ReturnType export type EntityComponentsRecord = { StaticAccount: React.FC StaticGroup: React.FC StaticPublication: React.FC } +export type StaticPublicationContextValue = { + entityComponents: EntityComponentsRecord + onLinkClick: (dest: string, e: any) => void +} + +export const staticPublicationContext = + createContext(null) + +export type StaticEmbedProps = StaticBlockProps & ReturnType + export function StaticPublicationProvider({ children, - entityComponents = null, -}: PropsWithChildren<{entityComponents: StaticPublicationContextValue}>) { + entityComponents, + onLinkClick, +}: PropsWithChildren) { return ( - + {children} ) } -export function useStaticPublication() { +export function useStaticPublicationContext() { let context = useContext(staticPublicationContext) if (!context) { @@ -510,13 +515,24 @@ function StaticBlockVideo({block, depth}: StaticBlockProps) { ) } +type LinkType = null | 'basic' | 'hypermedia' + +function hmTextColor(linkType: LinkType): string { + if (linkType === 'basic') return '$blue11' + if (linkType === 'hypermedia') return '$mint11' + return '$color12' +} + function InlineContentView({ inline, style, + linkType = null, ...props }: SizableTextProps & { inline: HMInlineContent[] + linkType?: LinkType }) { + const {onLinkClick} = useStaticPublicationContext() return ( {inline.map((content, index) => { @@ -527,7 +543,8 @@ function InlineContentView({ | 'underline' | 'underline line-through' | undefined - if (content.styles.underline) { + const underline = linkType || content.styles.underline + if (underline) { if (content.styles.strike) { textDecorationLine = 'underline line-through' } else { @@ -559,71 +576,59 @@ function InlineContentView({ ) } - if (content.styles.backgroundColor) { - children = ( - - {children} - - ) - } + // does anything use this? + // if (content.styles.backgroundColor) { + // children = ( + // + // {children} + // + // ) + // } if (content.styles.strike) { children = {children} } - if (content.styles.textColor) { - children = ( - {children} - ) - } + // does anything use this? + // if (content.styles.textColor) { + // children = ( + // {children} + // ) + // } return ( - + {children} - + ) } if (content.type === 'link') { const href = isHypermediaScheme(content.href) ? idToUrl(content.href, null) : content.href - - const isExternal = isHypermediaScheme(content.href) + if (!href) return null + const isHmLink = isHypermediaScheme(content.href) return ( - href && ( // - // - // - // - // {isExternal ? : null} - // - // - // - - - {isExternal ? : null} - - ) + onLinkClick(content.href, e)} + style={{ + cursor: 'pointer', + display: 'inline', + textDecoration: 'none', + }} + > + + ) } return null @@ -638,7 +643,7 @@ function stripHMLinkPrefix(link: string) { export function StaticBlockEmbed(props: StaticBlockProps & {blockRef: string}) { console.log('EMBED HERE', props) - const EmbedTypes = useStaticPublication() + const EmbedTypes = useStaticPublicationContext().entityComponents const id = unpackHmId(props.blockRef) if (id?.type == 'a') {