Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduces the bundle size of attachments preview. #1125

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

# production
/build
/dist/*.js
/dist/*.map
/dist/editor-output.js
/dist/editor-output.js.map
/dist/codeBlockHighlight.js.map
Expand All @@ -26,8 +28,6 @@ yarn-debug.log*
yarn-error.log*

/editor-content.js
/index.js
/index.cjs.js
/index.*.map
/index.*
/formik.js
storybook-static
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"files": [
"index.*",
"types.d.ts",
"src/translations"
"src/translations",
"dist"
],
"author": "BigBinary",
"license": "MIT",
Expand Down
8 changes: 7 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { mergeDeepLeft } from "ramda";
import peerDepsExternal from "rollup-plugin-peer-deps-external";
import styles from "rollup-plugin-styles";
import copy from "rollup-plugin-copy";
import cleaner from "rollup-plugin-cleaner";

import packageJson from "./package.json";

Expand All @@ -22,6 +23,8 @@ const peerDependencies = Object.keys(packageJson.peerDependencies);

const formats = ["esm", "cjs"];

const cleanerTargets = ["./dist/", "index.cjs.js", "index.js", "index.cjs.js.map", "index.js.map"];

const plugins = [
peerDepsExternal(),
alias({ entries: aliasEntries }),
Expand Down Expand Up @@ -54,6 +57,7 @@ const config = args => {
assetFileNames: "[name][extname]",
dir: path.join(destination),
entryFileNames: format === "esm" ? "index.js" : "index.cjs.js",
chunkFileNames: format === "esm" ? "dist/chunk-[hash].js" : "dist/chunk-[hash].cjs.js",
format,
name: "NeetoEditor",
sourcemap: true,
Expand All @@ -64,7 +68,9 @@ const config = args => {
input: "./src/index.js",
external: peerDependencies,
output,
plugins: [ ...plugins,
plugins: [
cleaner({ targets: cleanerTargets }),
...plugins,
args.app && copy({
targets: [
{ src: "package.json", dest: destination },
Expand Down
3 changes: 3 additions & 0 deletions src/components/Attachments/Attachment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MenuVertical, Close } from "neetoicons";
import {
Dropdown,
Input,
Spinner,
Toastr,
Tooltip,
Typography,
Expand All @@ -29,6 +30,7 @@ const Attachment = ({
disabled,
onChange,
setSelectedAttachment,
isLoading,
}) => {
const { t } = useTranslation();

Expand Down Expand Up @@ -154,6 +156,7 @@ const Attachment = ({
<Tooltip content={attachment.filename} position="top">
<Typography style="body2">{attachment.filename}</Typography>
</Tooltip>
{isLoading && <Spinner className="attachment-button-loader" />}
</div>
<Tooltip
content={t("neetoEditor.attachments.actionsBlocked")}
Expand Down
7 changes: 6 additions & 1 deletion src/components/Attachments/Preview.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from "react";
import React, { useEffect, useRef } from "react";

import { findIndexBy, truncate } from "neetocist";
import { Left, Right } from "neetoicons";
Expand All @@ -17,6 +17,7 @@ const Preview = ({
attachments,
selectedAttachment,
setSelectedAttachment,
setDidFetchPreviewBundle,
}) => {
const { t } = useTranslation();
const downloadRef = useRef(null);
Expand Down Expand Up @@ -52,6 +53,10 @@ const Preview = ({
}
};

useEffect(() => {
setDidFetchPreviewBundle(true);
}, []);

const handleDownload = () => downloadFile(url, filename);

const setPreview = () => {
Expand Down
36 changes: 29 additions & 7 deletions src/components/Attachments/index.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React, { useRef, useEffect, useState, useImperativeHandle } from "react";
import React, {
useRef,
useEffect,
useState,
useImperativeHandle,
lazy,
Suspense,
} from "react";

import DropTarget from "@uppy/drop-target";
import classnames from "classnames";
import { removeById, noop } from "neetocist";
import { isPresent, removeById, noop } from "neetocist";
import { Button, Toastr } from "neetoui";
import { concat, isEmpty, isNil } from "ramda";
import { useTranslation } from "react-i18next";
Expand All @@ -11,9 +18,10 @@ import useUppyUploader from "hooks/useUppyUploader";

import Attachment from "./Attachment";
import AttachmentProgress from "./AttachmentProgress";
import Preview from "./Preview";
import { buildUppyConfig, handleDrop, selectFiles } from "./utils";

const Preview = lazy(() => import("./Preview"));

const Attachments = (
{
attachments = [],
Expand All @@ -31,6 +39,7 @@ const Attachments = (

const [pendingAttachments, setPendingAttachments] = useState([]);
const [selectedAttachment, setSelectedAttachment] = useState({});
const [didFetchPreviewBundle, setDidFetchPreviewBundle] = useState(false);

const attachmentInputRef = useRef(null);

Expand Down Expand Up @@ -168,6 +177,10 @@ const Attachments = (
setSelectedAttachment,
}}
key={attachment.signedId}
isLoading={
!didFetchPreviewBundle &&
selectedAttachment.url === attachment.url
}
/>
))}
{pendingAttachments.map(attachment => (
Expand Down Expand Up @@ -197,10 +210,19 @@ const Attachments = (
onChange={handleAddFile}
onClick={handleFileInputClick}
/>
<Preview
{...{ attachments, selectedAttachment, setSelectedAttachment }}
onClose={() => setSelectedAttachment({})}
/>
<Suspense fallback={<span />}>
{isPresent(selectedAttachment) && (
<Preview
{...{
attachments,
selectedAttachment,
setDidFetchPreviewBundle,
setSelectedAttachment,
}}
onClose={() => setSelectedAttachment({})}
/>
)}
</Suspense>
</div>
</div>
);
Expand Down
5 changes: 5 additions & 0 deletions src/styles/components/_attachments.scss
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
}
}
}

.attachment-button-loader {
scale: 0.5;
width: 26px !important;
}
}

.ne-attachments--integrated {
Expand Down
Loading