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 maxLines config option to MultilineInput #4477

Merged

Conversation

gagiopapinni
Copy link
Contributor

@gagiopapinni gagiopapinni commented Nov 27, 2020

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide

The details

Resolves

#3969

Proposed Changes

Added a maxLines config option to multiline text input

Reason for Changes

#3969 proposed to add a scrolling functionality, to make working with large amounts of text more comfortable.
So one of the reasons is to close the issue, another - to make working with large amounts of text more comfortable :)

Test Coverage

Tested on:

  • Desktop Chrome
  • Desktop Firefox

Additional Information

Additionally, I have added a clamsy block to playground here to test it, so you can also play with it

@google-cla
Copy link

google-cla bot commented Nov 27, 2020

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@gagiopapinni
Copy link
Contributor Author

@googlebot I signed it!

Copy link
Contributor

@maribethb maribethb 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 contribution! I left a few comments and questions.

* If exceeded, scrolling functionality is enabled.
* @type {number}
*/
this.maxLines_ = Infinity;
Copy link
Contributor

Choose a reason for hiding this comment

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

The trailing underscore should only be used for private/protected properties. In this case, since you already have public getters and setters, you should mark this property as protected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, will fix!

var text = lines[i];
if (text.length > this.maxDisplayLength) {
if (text.length > this.maxDisplayLength || overflow && i === displayLinesNumber - 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's correct either way, but could you add parentheses around the condition you added here? That way a reader doesn't have to stop and think about the order of operations like I did when I got here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

@@ -211,6 +228,27 @@ Blockly.FieldMultilineInput.prototype.updateSize_ = function() {
totalHeight += this.getConstants().FIELD_TEXT_HEIGHT +
(i > 0 ? this.getConstants().FIELD_BORDER_RECT_Y_PADDING : 0);
}
if (this.isBeingEdited_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you tell me a little more about what this section of code is doing? I'm not sure what you're trying to solve that isn't covered by the existing code (I checked out your PR and removed this bit and it seems fine, but I could be missing something). One difference I noticed is that when you have a truncated line (referring to maxDisplayLength, not maxLines), the block gets slightly wider when you start editing, then shrinks again when you finish. In the existing behavior, the block stays the same width. Unless we have good reason, I think we should prefer the existing behavior here.

Then, if the size doesn't change, I don't think you need to override showEditor_ either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you type in the editor, the underlying SVG text changes, and the width of the block is originally set as the longest SVG text element (above this section).
When we restrict the number of lines, then the block width is always restricted by the first maxLines number of SVG lines.
This results in a behavior where if maxLines is exceeded in editor mode, editor size no longer matches the longest line in the editor.
So here I set the block width as the longest string in editor, if it is being edited. This is important, otherwise you will have situations where the first maxLines lines have length of say 1, and then if another maxLines+1-th line followed with more characters, it would keep breaking down into tiny peaces, as shown below.

here's what it looks like without this section. The last line should not break but should resize the editor.
before2
With this section enabled:
after2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, the difference you were talking about here, that you had noticed, seems to be it was a bug, I think it's fixed now. That was happening because I was scaling the fontsize here before calculating line width. Actually, I still don't know why, but it seems to work perfectly fine without that, and even the bug is fixed :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, makes sense, and glad you were able to fix the resizing issue! Could you add a comment summarizing what you said here? Something like "The default width is based on the longest line in the display text, but we need to calculate based on the absolute longest line even if it would be truncated after editing."

@@ -286,9 +359,14 @@ Blockly.Css.register([
'.blocklyHtmlTextAreaInput {',
'font-family: monospace;',
'resize: none;',
'overflow: hidden;',
'overflow: scroll;',
'-ms-overflow-style: none;',
Copy link
Contributor

Choose a reason for hiding this comment

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

The default should show the scrollbars since that is the typical paradigm for a scrollable text area. You can remove them for your project in your own css if you really don't want to show them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will fix this too in a few days.

I cant just enable them because it will result in improper block size, and text will break down in places where it should not. Last time I tried updating block size in a way to include scrollbar width by using clientWidth, got some strange glitches, so I need a little more time to figure this one out.

Comment on lines +213 to +221
if (this.isBeingEdited_) {
var htmlInput = /** @type {!HTMLElement} */(this.htmlInput_);
if (this.isOverflowedY_) {
Blockly.utils.dom.addClass(htmlInput, 'blocklyHtmlTextAreaInputOverflowedY');
} else {
Blockly.utils.dom.removeClass(htmlInput, 'blocklyHtmlTextAreaInputOverflowedY');
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I should probably explain why I have decided enabling the scrollbar manually like this.

Initially I tried using overflow-y:auto;, but because the textarea is not resizable, I would get a glitch like this:
glitch

As I understand it, when we have a non-resizable textarea filled with text, another symbol automatically triggers scrollbar, and even though the next moment we set the correct width, the scrollbars' width is still there and it still counts as existing. Probably has something to do with the reflow order... and so we get the phantom scrollbar space. I tried fixing this thing, but eventually it got too painful, and I settled with current approach.

Copy link
Contributor

@maribethb maribethb left a comment

Choose a reason for hiding this comment

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

Looks good, just requesting that you add some of the explanations you gave here into the code so it's clear for future readers.

Please note that as we have the release next week, develop is currently frozen except for bug fixes so that we have adequate time to test all feature additions. This means after approval, your PR won't be merged for a few days until after the release (and possibly a bit longer than that since most of us will be taking vacation through the end of December) and your feature will be included in the following (q1 2021) release of Blockly. Thanks again for your work on this!

@@ -211,6 +228,27 @@ Blockly.FieldMultilineInput.prototype.updateSize_ = function() {
totalHeight += this.getConstants().FIELD_TEXT_HEIGHT +
(i > 0 ? this.getConstants().FIELD_BORDER_RECT_Y_PADDING : 0);
}
if (this.isBeingEdited_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, makes sense, and glad you were able to fix the resizing issue! Could you add a comment summarizing what you said here? Something like "The default width is based on the longest line in the display text, but we need to calculate based on the absolute longest line even if it would be truncated after editing."

@maribethb maribethb merged commit e4decd8 into google:develop Jan 7, 2021
@maribethb
Copy link
Contributor

Thanks for your contribution! And your patience, as develop was frozen for release and then most of us were on holiday. :) This should be available in the next release.

@LarryKlugerDS
Copy link

This is a great PR! Thank you so much @gagiopapinni !

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.

None yet

3 participants