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

Fix missing style transfer from existing textarea to hwt containers #19

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Dalimil
Copy link

@Dalimil Dalimil commented Sep 24, 2017

This pull request fixes a couple of CSS bugs in the original plugin.

To exactly match our text overlay we must ensure that the two text containers have the same styling. For textareas that already have certain line-height and font CSS properties set, we must ensure that these are copied to our new containers, so that both text overlay perfectly.

This pull request also fixes previously transparent background - for dark (or any non-white) websites, this would modify the appearance of textareas and make them hard to read.

@lonekorean
Copy link
Owner

Thanks for this, I like the idea. Some concerns though. It doesn't play well with existing CSS. For example, on the demo page:

potato-bug

I'd like the solution to be backwards compatible. I don't know if that's feasible to do automatically, or if we put your code behind an option that people enable? Not sure.

Also not sure about which styles are copied from the textarea element and which are not. Just as an example, letter-spacing can cause a text styling mismatch, so we'd probably want to copy it. But there are so many other styles to consider, too. I don't know where the sanity line is.

I'll keep thinking on it, would be sweet to make this work.

@@ -29,10 +29,8 @@
.hwt-input {
display: block !important;
position: relative !important;
margin: 0;
padding: 0;
margin: 0 !important;
Copy link
Owner

Choose a reason for hiding this comment

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

Why was margin left behind?

Copy link
Author

Choose a reason for hiding this comment

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

If the original textarea had any margin set, it would be applied inside the .hwt-container, instead we want to add that amount of margin to the actual .hwt-container (so that it feels like the original textarea element) and remove any margin from the textarea inside

},

getContainerDivCssFix: function(currentTextarea) {
const textareaStyle = window.getComputedStyle(currentTextarea);
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a performance hit with getComputedStyle? Worth caching?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

@@ -339,7 +371,7 @@
this.$backdrop.remove();
this.$el
.unwrap()
.removeClass(ID + '-text ' + ID + '-input')
.removeClass(ID + '-content ' + ID + '-input')
Copy link
Owner

Choose a reason for hiding this comment

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

Good catch!

@Dalimil
Copy link
Author

Dalimil commented Sep 25, 2017

Couple of changes here. I believe users should not try to style .hwt-content class - this is our internal class. Let them style their actual textarea or any classes that they apply to their textarea - we'll simply copy the computed styles from them (font, line-height, etc.).

Once you remove the custom .hwt-content styles from the demo page (style textarea directly instead - simply change the CSS rule selector to update), it should work as expected with the new version.

@lonekorean
Copy link
Owner

I see your reasoning about it being an internal class, but this would still be a breaking change for current implementations. I would only consider breaking backwards compatibility for a new major version release, but I don't think it's ready for that. Still unsure how to determine all the properties that get copied and all the properties that don't.

@Dalimil
Copy link
Author

Dalimil commented Sep 26, 2017

Hmmm, I suppose I also have a slightly different use case for the plugin. Currently, the styling of .hwt-content works fine if the highlight function of the plugin is applied right after the page loads. One should then call $('.string-example').highlightWithinTextarea({ highlight: '' }) to remove the highlight, because calling $('.string-example').highlightWithinTextarea('destroy') (with the class/id bug fixed) would completely change the styling. Demo page:

screenshot-2017-9-26 highlight within textarea

My use case is: I have a couple of textareas (potentially with various CSS styles) on a page and need to start highlighting their text at some point (call the highlight function). What my pull-request is trying to achieve is keeping the visual state the same before (without .hwt-* classes) and after the function is called (with .hwt-* classes).

@lonekorean
Copy link
Owner

Ah, I see. That makes sense.

@GrayedFox
Copy link

GrayedFox commented Sep 5, 2019

Hi @Dalimil - sorry for bringing up such an old thread - but were you at all successful in applying the highlight without changing the original textfields? I need to do exactly that for this plug-in I'm developing, and I think your approach of styling the textarea based on existing attributes is the way to go.

What did you mean however by "Once you remove the custom .hwt-content styles from the demo page (style textarea directly instead - simply change the CSS rule selector to update), it should work as expected with the new version."?

I ask because after implementing your changes I am seeing the same bug (highlight is no longer matched to text -- the textfield now retains it's original style/position/etc however highlighting is off).

highlight-mismatch

Thanks for your time!

@Dalimil
Copy link
Author

Dalimil commented Sep 10, 2019

@GrayedFox I believe I got it to work for my purposes, but it's been such a long time ago I honestly don't remember the code details... : https://github.com/Dalimil/highlight-within-textarea

@GrayedFox
Copy link

@Dalimil hey thanks for the reply, I ended up getting it working too, it was a matter of getting the padding and border values right (or more specifically, inheriting those from the textarea). Cheers :)

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

Successfully merging this pull request may close these issues.

3 participants