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

Addition fix to previous fix for #1093 #1140

Merged
merged 3 commits into from
Nov 13, 2024
Merged

Addition fix to previous fix for #1093 #1140

merged 3 commits into from
Nov 13, 2024

Conversation

ntran18
Copy link
Collaborator

@ntran18 ntran18 commented Nov 5, 2024

When exporting the svg, using cloneNode making the image to be all black. This is because style didn't pass correctly. This makes sure the style is correct.

@ntran18 ntran18 changed the base branch from master to beta November 5, 2024 22:42
@ntran18 ntran18 requested a review from dondi November 5, 2024 22:42
Copy link
Owner

@dondi dondi left a comment

Choose a reason for hiding this comment

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

Good find! For this one, I realize you are revising pre-existing code, but it looks like the pre-existing code itself could have used some refactors. Please see if we can switch from a concatenated string to Object.assign when building up the new inline style for the node(s)—thanks!

@@ -110,38 +108,38 @@ export const setupHandlers = grnState => {
}
}
};
visit(svg);
visit(node);
return tree;
};

const explicitlySetStyle = element => {
const cSSStyleDeclarationComputed = window.getComputedStyle(element);
Copy link
Owner

Choose a reason for hiding this comment

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

I realize this was pre-existing code, but since you’re already changing this, let’s fix the capitalization issue: the CSS here should actually be all lowercase, if we are to follow the convention:

Suggested change
const cSSStyleDeclarationComputed = window.getComputedStyle(element);
const cssStyleDeclarationComputed = window.getComputedStyle(element);

if (value !== emptySvgDeclarationComputed.getPropertyValue(key)) {
// Don't set computed style of width and height. Makes SVG elmements disappear.
if ((key !== "height") && (key !== "width")) {
computedStyleStr += key + ":" + value + ";";
computedStyleStr += `${key}:${value};`;
Copy link
Owner

Choose a reason for hiding this comment

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

Great change! Yes, in the age of template strings I generally change any concatenations + into template/formatted strings 💯

Comment on lines 131 to 137
if (element.classList.contains("weight")) {
computedStyleStr += "visibility: hidden;";
}

if (computedStyleStr) {
element.setAttribute("style", computedStyleStr);
}
Copy link
Owner

Choose a reason for hiding this comment

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

The observation here is that we are building a string in order to set what is really an object—style attribute names + their values. In the end, I think this whole block will be more robust if we build the desired style as an object instead of a string. Then, Object.assign can be used to set the final style once the object is built. Here’s a link to show how this would work: https://www.bram.us/2017/01/16/using-object-assign-to-quickly-set-multiple-inline-styles/

This does change this block of code quite a bit, but I think it is a good change in the right direction—what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think Object.assign is better!

while (i--) {
explicitlySetStyle(allElements[i]);
}
allElements.forEach(explicitlySetStyle);
Copy link
Owner

Choose a reason for hiding this comment

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

Another great refactor, using the function-as-iterator trick from a prior PR 🤩 Also nice to see 4 lines become just one, right? 😁

Copy link
Owner

@dondi dondi left a comment

Choose a reason for hiding this comment

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

That Object.assign change works really well I think! But I think you missed one more capitalization change?

For things like that, VS Code’s Rename command really helps—I can show it to you if you aren’t familiar with it yet


for (let i = 0; i < cssStyleDeclarationComputed.length; i++) {
const key = cssStyleDeclarationComputed[i];
const value = cSSStyleDeclarationComputed.getPropertyValue(key);
Copy link
Owner

Choose a reason for hiding this comment

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

Whoops, shouldn’t this be edited also?

Suggested change
const value = cSSStyleDeclarationComputed.getPropertyValue(key);
const value = cssStyleDeclarationComputed.getPropertyValue(key);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't catch this one. Usually, I would use Ctrl + D to select all words, but I think I was fixing it manually. But I would love to learn about VS Code's Rename command!

Copy link
Owner

@dondi dondi left a comment

Choose a reason for hiding this comment

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

Thanks for the quick fix! LGTM

@dondi dondi merged commit 770e4d1 into beta Nov 13, 2024
@dondi dondi deleted the maika-1093 branch November 13, 2024 18:38
@ceciliazaragoza
Copy link
Collaborator

I checked this in beta, and it works as expected!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants