mirror of
https://github.com/outline/outline.git
synced 2026-12-05 10:45:07 +03:00
fix CORS err on img download (#10279)
* fix CORS err on img download * add check to prevent accidental double download * disable download button when downloading
This commit is contained in:
1 parent
e71a425268
commit
06a1428cbc
2 files changed
+87
-42
No files matched your search
@@ -21,10 +21,12 @@ type Props = ComponentProps & {
|
||||
/** The editor view */
|
||||
view: EditorView;
|
||||
children?: React.ReactElement;
|
||||
isDownloading?: boolean;
|
||||
};
|
||||
|
||||
const Image = (props: Props) => {
|
||||
const { isSelected, node, isEditable, onChangeSize, onClick } = props;
|
||||
const { isSelected, node, isEditable, onChangeSize, onClick, isDownloading } =
|
||||
props;
|
||||
const { src, layoutClass } = node.attrs;
|
||||
const { t } = useTranslation();
|
||||
const className = layoutClass ? `image image-${layoutClass}` : "image";
|
||||
@@ -102,7 +104,11 @@ const Image = (props: Props) => {
|
||||
<GlobeIcon />
|
||||
</Button>
|
||||
)}
|
||||
<Button onClick={props.onDownload} aria-label={t("Download")}>
|
||||
<Button
|
||||
onClick={props.onDownload}
|
||||
aria-label={t("Download")}
|
||||
disabled={isDownloading}
|
||||
>
|
||||
<DownloadIcon />
|
||||
</Button>
|
||||
</Actions>
|
||||
@@ -224,7 +230,7 @@ const Button = styled.button`
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
display: inline-block;
|
||||
cursor: var(--pointer) !important;
|
||||
cursor: var(--pointer);
|
||||
transition: opacity 150ms ease-in-out;
|
||||
|
||||
&:first-child:not(:last-child) {
|
||||
@@ -244,6 +250,20 @@ const Button = styled.button`
|
||||
&:hover {
|
||||
color: ${s("text")};
|
||||
}
|
||||
|
||||
&:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: wait;
|
||||
pointer-events: none;
|
||||
|
||||
&:hover {
|
||||
color: ${s("textSecondary")};
|
||||
}
|
||||
|
||||
&:active {
|
||||
transform: none;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const ImageWrapper = styled.div<{ isFullWidth: boolean }>`
|
||||
|
||||
@@ -55,22 +55,35 @@ const parseTitleAttribute = (tokenTitle: string): TitleAttributes => {
|
||||
return attributes;
|
||||
};
|
||||
|
||||
export const downloadImageNode = async (node: ProsemirrorNode) => {
|
||||
const image = await fetch(node.attrs.src);
|
||||
const imageBlob = await image.blob();
|
||||
const imageURL = URL.createObjectURL(imageBlob);
|
||||
const extension = imageBlob.type.split(/\/|\+/g)[1];
|
||||
const potentialName = node.attrs.alt || "image";
|
||||
export const downloadImageNode = async (
|
||||
node: ProsemirrorNode,
|
||||
cache?: RequestCache
|
||||
) => {
|
||||
try {
|
||||
const image = await fetch(node.attrs.src, {
|
||||
cache,
|
||||
});
|
||||
const imageBlob = await image.blob();
|
||||
const imageURL = URL.createObjectURL(imageBlob);
|
||||
const extension = imageBlob.type.split(/\/|\+/g)[1];
|
||||
const potentialName = node.attrs.alt || "image";
|
||||
|
||||
// create a temporary link node and click it with our image data
|
||||
const link = document.createElement("a");
|
||||
link.href = imageURL;
|
||||
link.download = `${potentialName}.${extension}`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
// create a temporary link node and click it with our image data
|
||||
const link = document.createElement("a");
|
||||
link.href = imageURL;
|
||||
link.download = `${potentialName}.${extension}`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
// cleanup
|
||||
document.body.removeChild(link);
|
||||
// cleanup
|
||||
document.body.removeChild(link);
|
||||
} catch {
|
||||
if (cache !== "reload") {
|
||||
downloadImageNode(node, "reload");
|
||||
} else {
|
||||
window.open(sanitizeUrl(node.attrs.src), "_blank");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default class Image extends SimpleImage {
|
||||
@@ -255,14 +268,6 @@ export default class Image extends SimpleImage {
|
||||
});
|
||||
};
|
||||
|
||||
handleDownload =
|
||||
({ node }: ComponentProps) =>
|
||||
(event: React.MouseEvent) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
void downloadImageNode(node);
|
||||
};
|
||||
|
||||
handleCaptionKeyDown =
|
||||
({ node, getPos }: ComponentProps) =>
|
||||
(event: React.KeyboardEvent<HTMLParagraphElement>) => {
|
||||
@@ -322,24 +327,44 @@ export default class Image extends SimpleImage {
|
||||
this.editor.updateActiveLightbox(getPos());
|
||||
};
|
||||
|
||||
component = (props: ComponentProps) => (
|
||||
<ImageComponent
|
||||
{...props}
|
||||
onClick={this.handleClick(props)}
|
||||
onDownload={this.handleDownload(props)}
|
||||
onChangeSize={this.handleChangeSize(props)}
|
||||
>
|
||||
<Caption
|
||||
width={props.node.attrs.width}
|
||||
onBlur={this.handleCaptionBlur(props)}
|
||||
onKeyDown={this.handleCaptionKeyDown(props)}
|
||||
isSelected={props.isSelected}
|
||||
placeholder={this.options.dictionary.imageCaptionPlaceholder}
|
||||
component = (props: ComponentProps) => {
|
||||
const [isDownloading, setIsDownloading] = React.useState(false);
|
||||
|
||||
const handleDownload = React.useCallback(
|
||||
async (event: React.MouseEvent<HTMLButtonElement>) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (isDownloading) {
|
||||
return;
|
||||
}
|
||||
setIsDownloading(true);
|
||||
await downloadImageNode(props.node);
|
||||
setIsDownloading(false);
|
||||
},
|
||||
[isDownloading, props]
|
||||
);
|
||||
|
||||
return (
|
||||
<ImageComponent
|
||||
{...props}
|
||||
isDownloading={isDownloading}
|
||||
onClick={this.handleClick(props)}
|
||||
onDownload={handleDownload}
|
||||
onChangeSize={this.handleChangeSize(props)}
|
||||
>
|
||||
{props.node.attrs.alt}
|
||||
</Caption>
|
||||
</ImageComponent>
|
||||
);
|
||||
<Caption
|
||||
width={props.node.attrs.width}
|
||||
onBlur={this.handleCaptionBlur(props)}
|
||||
onKeyDown={this.handleCaptionKeyDown(props)}
|
||||
isSelected={props.isSelected}
|
||||
placeholder={this.options.dictionary.imageCaptionPlaceholder}
|
||||
>
|
||||
{props.node.attrs.alt}
|
||||
</Caption>
|
||||
</ImageComponent>
|
||||
);
|
||||
};
|
||||
|
||||
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
|
||||
// Skip the preceding space for images at the start of a list item or Markdown parsers may
|
||||
|
||||
Reference in new issue
Block a user