mirror of
https://github.com/outline/outline.git
synced 2026-12-03 10:00:13 +03:00
fix: Dragging text within an image caption corrupts the document (#13449)
The caption is editable DOM outside of the document, so ProseMirror handled the drag against the document selection instead — moving unrelated content and throwing a mismatched transaction as the caption commit landed mid-dispatch. Fixes OUTLINE-CLOUD-AB8
This commit is contained in:
1 parent
df8f8848ef
commit
628615f9bc
1 file changed
+30
@@ -1,6 +1,7 @@
|
||||
import * as React from "react";
|
||||
import styled from "styled-components";
|
||||
import { s } from "../../styles";
|
||||
import { getDataTransferFiles } from "../../utils/files";
|
||||
import { EditorStyleHelper } from "../styles/EditorStyleHelper";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
@@ -32,6 +33,34 @@ function Caption({
|
||||
...rest
|
||||
}: Props) {
|
||||
const { t } = useTranslation();
|
||||
const ref = React.useRef<HTMLParagraphElement>(null);
|
||||
|
||||
// The caption is not part of the document, so drags within it must not reach
|
||||
// the editor, which would act on the document selection instead. Native
|
||||
// listeners are used because they run before the editor's, React's do not.
|
||||
React.useEffect(() => {
|
||||
const element = ref.current;
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
|
||||
const handleDrag = (event: DragEvent) => {
|
||||
// File drops are left alone so that they still upload.
|
||||
if (getDataTransferFiles(event).length) {
|
||||
return;
|
||||
}
|
||||
event.stopPropagation();
|
||||
};
|
||||
|
||||
element.addEventListener("dragstart", handleDrag);
|
||||
element.addEventListener("drop", handleDrag);
|
||||
|
||||
return () => {
|
||||
element.removeEventListener("dragstart", handleDrag);
|
||||
element.removeEventListener("drop", handleDrag);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handlePaste = (event: React.ClipboardEvent<HTMLParagraphElement>) => {
|
||||
event.preventDefault();
|
||||
const text = event.clipboardData.getData("text/plain");
|
||||
@@ -60,6 +89,7 @@ function Caption({
|
||||
|
||||
return (
|
||||
<Content
|
||||
ref={ref}
|
||||
$width={width}
|
||||
$isSelected={isSelected}
|
||||
onMouseDown={handleMouseDown}
|
||||
|
||||
Reference in new issue
Block a user