-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow resizing of embed height (#8154)
* stash * tsc * remove console log * Restore bottom bar on embeds * fix: Cannot see selected state * fix layout issue
- Loading branch information
Showing
5 changed files
with
251 additions
and
105 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 |
---|---|---|
@@ -1,73 +1,154 @@ | ||
import * as React from "react"; | ||
import styled from "styled-components"; | ||
import { EmbedDescriptor } from "../embeds"; | ||
import { getMatchingEmbed } from "../lib/embeds"; | ||
import { ComponentProps } from "../types"; | ||
import DisabledEmbed from "./DisabledEmbed"; | ||
import Frame from "./Frame"; | ||
import { ResizeBottom, ResizeLeft, ResizeRight } from "./ResizeHandle"; | ||
import useDragResize from "./hooks/useDragResize"; | ||
|
||
const EmbedComponent = ({ | ||
isEditable, | ||
isSelected, | ||
theme, | ||
node, | ||
embeds, | ||
embedsDisabled, | ||
}: ComponentProps & { | ||
type Props = ComponentProps & { | ||
embeds: EmbedDescriptor[]; | ||
embedsDisabled?: boolean; | ||
}) => { | ||
const cache = React.useMemo( | ||
() => getMatchingEmbed(embeds, node.attrs.href), | ||
[embeds, node.attrs.href] | ||
style?: React.CSSProperties; | ||
onChangeSize?: (props: { width: number; height?: number }) => void; | ||
}; | ||
|
||
const Embed = (props: Props) => { | ||
const ref = React.useRef<HTMLIFrameElement>(null); | ||
const { node, isEditable, onChangeSize } = props; | ||
const naturalWidth = 10000; | ||
const naturalHeight = 400; | ||
const isResizable = !!onChangeSize; | ||
|
||
const { width, height, setSize, handlePointerDown, dragging } = useDragResize( | ||
{ | ||
width: node.attrs.width ?? naturalWidth, | ||
height: node.attrs.height ?? naturalHeight, | ||
naturalWidth, | ||
naturalHeight, | ||
gridSnap: 5, | ||
onChangeSize, | ||
ref, | ||
} | ||
); | ||
|
||
if (!cache) { | ||
return null; | ||
} | ||
React.useEffect(() => { | ||
if (node.attrs.height && node.attrs.height !== height) { | ||
setSize({ | ||
width: node.attrs.width, | ||
height: node.attrs.height, | ||
}); | ||
} | ||
}, [node.attrs.height]); | ||
|
||
const { embed, matches } = cache; | ||
|
||
if (embedsDisabled) { | ||
return ( | ||
<DisabledEmbed | ||
href={node.attrs.href} | ||
embed={embed} | ||
isEditable={isEditable} | ||
isSelected={isSelected} | ||
theme={theme} | ||
/> | ||
); | ||
} | ||
const style: React.CSSProperties = { | ||
width: width || "100%", | ||
height: height || 400, | ||
maxWidth: "100%", | ||
pointerEvents: dragging ? "none" : "all", | ||
}; | ||
|
||
if (embed.transformMatch) { | ||
const src = embed.transformMatch(matches); | ||
return ( | ||
<Frame | ||
src={src} | ||
isSelected={isSelected} | ||
canonicalUrl={embed.hideToolbar ? undefined : node.attrs.href} | ||
title={embed.title} | ||
referrerPolicy="origin" | ||
border | ||
/> | ||
); | ||
} | ||
return ( | ||
<FrameWrapper ref={ref}> | ||
<InnerEmbed ref={ref} style={style} {...props} /> | ||
{isEditable && isResizable && ( | ||
<> | ||
<ResizeBottom | ||
onPointerDown={handlePointerDown("bottom")} | ||
$dragging={!!dragging} | ||
/> | ||
</> | ||
)} | ||
</FrameWrapper> | ||
); | ||
}; | ||
|
||
if ("component" in embed) { | ||
return ( | ||
// @ts-expect-error Component type | ||
<embed.component | ||
attrs={node.attrs} | ||
matches={matches} | ||
isEditable={isEditable} | ||
isSelected={isSelected} | ||
embed={embed} | ||
theme={theme} | ||
/> | ||
const InnerEmbed = React.forwardRef<HTMLIFrameElement, Props>( | ||
function InnerEmbed_( | ||
{ isEditable, isSelected, theme, node, embeds, embedsDisabled, style }, | ||
ref | ||
) { | ||
const cache = React.useMemo( | ||
() => getMatchingEmbed(embeds, node.attrs.href), | ||
[embeds, node.attrs.href] | ||
); | ||
|
||
if (!cache) { | ||
return null; | ||
} | ||
|
||
const { embed, matches } = cache; | ||
|
||
if (embedsDisabled) { | ||
return ( | ||
<DisabledEmbed | ||
href={node.attrs.href} | ||
embed={embed} | ||
isEditable={isEditable} | ||
isSelected={isSelected} | ||
theme={theme} | ||
/> | ||
); | ||
} | ||
|
||
if (embed.transformMatch) { | ||
const src = embed.transformMatch(matches); | ||
return ( | ||
<Frame | ||
ref={ref} | ||
src={src} | ||
style={style} | ||
isSelected={isSelected} | ||
canonicalUrl={embed.hideToolbar ? undefined : node.attrs.href} | ||
title={embed.title} | ||
referrerPolicy="origin" | ||
border | ||
/> | ||
); | ||
} | ||
|
||
if ("component" in embed) { | ||
return ( | ||
// @ts-expect-error Component type | ||
<embed.component | ||
ref={ref} | ||
attrs={node.attrs} | ||
style={style} | ||
matches={matches} | ||
isEditable={isEditable} | ||
isSelected={isSelected} | ||
embed={embed} | ||
theme={theme} | ||
/> | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
); | ||
|
||
return null; | ||
}; | ||
const FrameWrapper = styled.div` | ||
line-height: 0; | ||
position: relative; | ||
margin-left: auto; | ||
margin-right: auto; | ||
white-space: nowrap; | ||
cursor: default; | ||
border-radius: 8px; | ||
user-select: none; | ||
max-width: 100%; | ||
transition-property: width, max-height; | ||
transition-duration: 150ms; | ||
transition-timing-function: ease-in-out; | ||
&:hover { | ||
${ResizeLeft}, ${ResizeRight} { | ||
opacity: 1; | ||
} | ||
} | ||
`; | ||
|
||
export default EmbedComponent; | ||
export default Embed; |
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
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
Oops, something went wrong.