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

Added image dimensions to CTA Card #1446

Merged
merged 2 commits into from
Feb 13, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class CallToActionNode extends generateDecoratorNode({
{name: 'backgroundColor', default: 'grey'},
{name: 'hasImage', default: false},
{name: 'imageUrl', default: ''},
{name: 'imageWidth', default: null},
{name: 'imageHeight', default: null},
{name: 'href', default: '', urlType: 'url'}
]
}) {
Expand Down
18 changes: 16 additions & 2 deletions packages/kg-default-nodes/test/nodes/call-to-action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('CallToActionNode', function () {
backgroundColor: 'none',
hasImage: true,
imageUrl: 'http://blog.com/image1.jpg',
imageWidth: 200,
imageHeight: 100,
href: ''
};
exportOptions = {
Expand Down Expand Up @@ -69,11 +71,12 @@ describe('CallToActionNode', function () {
callToActionNode.imageUrl.should.equal(dataset.imageUrl);
callToActionNode.visibility.should.deepEqual(utils.visibility.buildDefaultVisibility());
callToActionNode.href.should.equal(dataset.href);
callToActionNode.imageHeight.should.equal(dataset.imageHeight);
callToActionNode.imageWidth.should.equal(dataset.imageWidth);
}));

it('has setters for all properties', editorTest(function () {
const callToActionNode = new CallToActionNode();

callToActionNode.layout.should.equal('minimal');
callToActionNode.layout = 'compact';
callToActionNode.layout.should.equal('compact');
Expand Down Expand Up @@ -122,6 +125,14 @@ describe('CallToActionNode', function () {
callToActionNode.imageUrl = 'http://blog.com/image1.jpg';
callToActionNode.imageUrl.should.equal('http://blog.com/image1.jpg');

should(callToActionNode.imageHeight).be.null();
callToActionNode.imageHeight = 100;
callToActionNode.imageHeight.should.equal(100);

should(callToActionNode.imageWidth).be.null();
callToActionNode.imageWidth = 200;
callToActionNode.imageWidth.should.equal(200);

callToActionNode.href.should.equal('');
callToActionNode.href = 'http://blog.com/post1';
callToActionNode.href.should.equal('http://blog.com/post1');
Expand Down Expand Up @@ -239,7 +250,6 @@ describe('CallToActionNode', function () {

const html = element.outerHTML.toString();
html.should.containEql('cta-card-email');
html.should.containEql('background-color: green');
html.should.containEql('background-color: #F0F0F0');
html.should.containEql('Get access now');
html.should.containEql('http://someblog.com/somepost');
Expand Down Expand Up @@ -311,6 +321,8 @@ describe('CallToActionNode', function () {
hasSponsorLabel: true,
sponsorLabel: '<p>This post is brought to you by our sponsors</p>',
imageUrl: '/content/images/2022/11/koenig-lexical.jpg',
imageWidth: 200,
imageHeight: 100,
layout: 'minimal',
showButton: true,
textValue: '<p><span style="white-space: pre-wrap;">This is a new CTA Card.</span></p>',
Expand All @@ -331,6 +343,8 @@ describe('CallToActionNode', function () {
hasSponsorLabel: true,
sponsorLabel: '<p>This post is brought to you by our sponsors</p>',
imageUrl: '/content/images/2022/11/koenig-lexical.jpg',
imageWidth: 200,
imageHeight: 100,
layout: 'minimal',
showButton: true,
textValue: '<p><span style="white-space: pre-wrap;">This is a new CTA Card.</span></p>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {CallToActionCard} from '../components/ui/cards/CallToActionCard.jsx';
import {LinkInput} from '../components/ui/LinkInput';
import {SnippetActionToolbar} from '../components/ui/SnippetActionToolbar.jsx';
import {ToolbarMenu, ToolbarMenuItem, ToolbarMenuSeparator} from '../components/ui/ToolbarMenu.jsx';
import {getImageDimensions} from '../utils/getImageDimensions';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useVisibilityToggle} from '../hooks/useVisibilityToggle.js';

Expand Down Expand Up @@ -91,12 +92,16 @@ export const CallToActionNodeComponent = ({
};

const handleImageChange = async (files) => {
const imgPreviewUrl = URL.createObjectURL(files[0]);
const {width, height} = await getImageDimensions(imgPreviewUrl);
const result = await imageUploader.upload(files);
// reset original src so it can be replaced with preview and upload progress
editor.update(() => {
const node = $getNodeByKey(nodeKey);
node.imageUrl = result?.[0].url;
node.hasImage = true;
node.imageWidth = width;
node.imageHeight = height;
});
};
Comment on lines 94 to 106
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider cleanup of object URL.

The implementation correctly retrieves and stores image dimensions. However, the created object URL should be revoked after use to prevent memory leaks.

Apply this diff to add cleanup:

 const handleImageChange = async (files) => {
   const imgPreviewUrl = URL.createObjectURL(files[0]);
-  const {width, height} = await getImageDimensions(imgPreviewUrl);
+  try {
+    const {width, height} = await getImageDimensions(imgPreviewUrl);
+    const result = await imageUploader.upload(files);
+    editor.update(() => {
+      const node = $getNodeByKey(nodeKey);
+      node.imageUrl = result?.[0].url;
+      node.hasImage = true;
+      node.imageWidth = width;
+      node.imageHeight = height;
+    });
+  } finally {
+    URL.revokeObjectURL(imgPreviewUrl);
+  }
-  const result = await imageUploader.upload(files);
-  editor.update(() => {
-    const node = $getNodeByKey(nodeKey);
-    node.imageUrl = result?.[0].url;
-    node.hasImage = true;
-    node.imageWidth = width;
-    node.imageHeight = height;
-  });
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handleImageChange = async (files) => {
const imgPreviewUrl = URL.createObjectURL(files[0]);
const {width, height} = await getImageDimensions(imgPreviewUrl);
const result = await imageUploader.upload(files);
// reset original src so it can be replaced with preview and upload progress
editor.update(() => {
const node = $getNodeByKey(nodeKey);
node.imageUrl = result?.[0].url;
node.hasImage = true;
node.imageWidth = width;
node.imageHeight = height;
});
};
const handleImageChange = async (files) => {
const imgPreviewUrl = URL.createObjectURL(files[0]);
try {
const {width, height} = await getImageDimensions(imgPreviewUrl);
const result = await imageUploader.upload(files);
editor.update(() => {
const node = $getNodeByKey(nodeKey);
node.imageUrl = result?.[0].url;
node.hasImage = true;
node.imageWidth = width;
node.imageHeight = height;
});
} finally {
URL.revokeObjectURL(imgPreviewUrl);
}
};


Expand Down
Loading