fix: Media editor crashes page (#10852)

closes #10851
This commit is contained in:
Tom Moor
2025-12-10 19:43:19 -05:00
committed by GitHub
parent 35510fb4be
commit 6cd2346d46
2 changed files with 12 additions and 6 deletions

View File

@@ -12,7 +12,7 @@ import ToolbarButton from "./ToolbarButton";
import useOnClickOutside from "~/hooks/useOnClickOutside";
type Props = {
node: Node;
node?: Node;
view: EditorView;
dictionary: Dictionary;
autoFocus?: boolean;
@@ -31,14 +31,14 @@ export function MediaLinkEditor({
onEscape,
onClickOutside,
}: Props) {
const url = (node.attrs.href ?? node.attrs.src) as string;
const url = (node?.attrs.href ?? node?.attrs.src) as string;
const [localUrl, setLocalUrl] = useState(url);
const wrapperRef = useRef<HTMLDivElement>(null);
// If we're attempting to edit an image, autofocus the input
// Not doing for embed type because it made the editor scroll to top
// unexpectedlyleaving that out for now
const isEditingImgUrl = node.type.name === "image";
const isEditingImgUrl = node?.type.name === "image";
const moveSelectionToEnd = useCallback(() => {
const { state, dispatch } = view;
@@ -65,9 +65,9 @@ export function MediaLinkEditor({
const update = useCallback(() => {
const { state } = view;
const hrefType = node.type.name === "image" ? "src" : "href";
const hrefType = node?.type.name === "image" ? "src" : "href";
const tr = state.tr.setNodeMarkup(state.selection.from, undefined, {
...node.attrs,
...node?.attrs,
[hrefType]: localUrl,
});
@@ -102,6 +102,10 @@ export function MediaLinkEditor({
[update, moveSelectionToEnd]
);
if (!node) {
return null;
}
return (
<Wrapper ref={wrapperRef}>
<Input

View File

@@ -286,7 +286,9 @@ export function SelectionToolbar(props: Props) {
) : activeToolbar === Toolbar.Media ? (
<MediaLinkEditor
key={`embed-${selection.from}`}
node={(selection as NodeSelection).node}
node={
"node" in selection ? (selection as NodeSelection).node : undefined
}
view={view}
dictionary={dictionary}
onLinkUpdate={() => setActiveToolbar(null)}