Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Scroll cursor back into view when it goes out of bounds #34

Merged
merged 10 commits into from
May 13, 2016

Conversation

jesiegel1
Copy link
Collaborator

Fix #3

  • Add JS method to calculate the caret position within the RichEditorView
  • Set scrollView contentSize.height based on html content height
  • Change scrollView's contentOffset if caret is outside of RichEditorView's bounds

@cjwirth
Copy link
Owner

cjwirth commented Mar 16, 2016

Hey! Excellent job so far!

Just a few glitches I noticed when giving it a run down --

  • Entering the first character on a new line will scroll it again
  • After having scrolled, adding a new line inside a portion that is in view still scrolls -- it would be nice if it scrolled only when it needed to

I've illustrated both issues in this gif:

untitled2

Definitely better than it was before though!

Thanks 👍

@jesiegel1
Copy link
Collaborator Author

Should now be fixed. I modified the CSS file slightly and it seems to have done the trick.

@cjwirth
Copy link
Owner

cjwirth commented Mar 17, 2016

It's definitely getting there. It's even better than before!
One bug I did notice is that if you start somewhere "in the middle" and make newlines until it should scroll, instead of just scrolling to where the cursor is visible, it moves the cursor back to (near to) its original position:

toolbar3

@jesiegel1
Copy link
Collaborator Author

When recreating the bug, it seems that the cursor position ISN'T actually changing, it's just the offset of the view. If you scroll the view all the way down, you'll see the cursor is exactly where it should be. I just need to fix the change in scroll offset when doing the above.

@jesiegel1
Copy link
Collaborator Author

Aright, I think it's fixed...let me know if you find more bugs. Let the testing begin!

var sel=window.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0);
var needsWorkAround = (range.startContainer.nodeName.toLowerCase() == 'div' && range.startOffset == 0);
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a comment or something about what this workaround is for and what it's doing?
Like, why is it needed? What is it returning instead of the normal case?

@cjwirth
Copy link
Owner

cjwirth commented Mar 19, 2016

Oh I found another bug! Isn't this fun? lol 😸

If the cursor is out of view (below the cutoff) and you input something, it scrolls up when you input something -- that is good. However, if the cursor is out of bounds (above the cutoff) and you input something, it doesn't scroll into view. Also making newlines in that case is kind of weird too.

toolbar3

@jesiegel1
Copy link
Collaborator Author

good catch! right now the code just checks the caret position against the lower bounds, so my guess is I just need to check the caret position against the upper bounds as well and it'll be fixed.

@mfrawley
Copy link
Contributor

Really looking forward to seeing this get merged!

var scrollView = editor.webView.scrollView

var contentHeight: CGFloat?
let htmlHeight = editor.runJS("document.getElementById('editor').clientHeight;")
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally a good practice to wrap the JS implementation details with a public accessor method on the RE namespace IMHO

Copy link
Owner

Choose a reason for hiding this comment

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

I agree. Even in this case, since document.getElementById('editor').clientHeight is literally the same as RE.editor, I might not be too opposed to just RE.editor.clientHeight (I'm not sure how I'm doing it in other places.)

Copy link
Collaborator Author

@jesiegel1 jesiegel1 Apr 18, 2016

Choose a reason for hiding this comment

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

@mfrawley
Copy link
Contributor

@cjwirth I'd say it's best to keep things working consistently. Updating the example project isn't going to break anyone's code and it probably is good to mention such things in release notes as breaking changes.

@cjwirth
Copy link
Owner

cjwirth commented Apr 4, 2016

@mfrawley That's a good point. We should probably go with that then!

@jesiegel1 Not trying to rush your or anything, but any progress with the issue of when the cursor is above the visible area?

@KKKiller
Copy link

how could i use this branch.

@jesiegel1
Copy link
Collaborator Author

@cjwirth only just saw this notification. I'll try to work on it today if I can, or by the end of the week at least.

@jesiegel1
Copy link
Collaborator Author

jesiegel1 commented Apr 17, 2016

@cjwirth Review and lemme know if you find any issues - this was a tricky one!

@cjwirth
Copy link
Owner

cjwirth commented Apr 18, 2016

Awesome! Yeah looking at the code it looks like something that might have taken a while to figure out. I'l take a look later today, probably tonight or something!

But yeah, if it's all working, that's pretty exciting.

@cjwirth
Copy link
Owner

cjwirth commented Apr 18, 2016

@KKKiller Are you using CocoaPods? If so, you need to change the podfile to reference the in-development branch.

Bascially, you will change

pod 'RichEditorView'

to

pod 'RichEditorView', :git => 'git@github.com:jesiegel1/RichEditorView.git'

and then re-run pod install to get the in-dev version.

You can read more about podfile configuration here

@cjwirth
Copy link
Owner

cjwirth commented Apr 21, 2016

Really close! I was really close to merging it. But! Scrolling the cursor out of bounds below and then typing should bring the cursor into view. It's really close, but I think its maybe like 1 line off. Making a new line works, as you can see.

toolbar3

btw: It works if you go out of bounds upwards.

@cjwirth
Copy link
Owner

cjwirth commented Apr 22, 2016

Cool! It seems to be working now!

Let me take a look at the code and see how it looks :)

RE.wrapTextNodes = function() {
var contents = RE.editor.childNodes;
for (var i = 0; i < contents.length; i++) {
if (contents[i].nodeType === 3) {
Copy link
Owner

Choose a reason for hiding this comment

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

It might be nice to have an object or enum or something so we don't have magic numbers flying around. Maybe nodeType == 3 is obvious to JS devs, but i have no idea.

Maybe Node.TEXT_NODE instead? https://developer.mozilla.org/en/docs/Web/API/Node/nodeType

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yea that works, totally reasonable

@cjwirth
Copy link
Owner

cjwirth commented Apr 27, 2016

Do you think you can fix some of the suggestions I had? Do you want me to just go and clean it up myself? It seems to be working pretty well, so just a little bit of cleanup and I'll merge it.

@cjwirth cjwirth changed the title Add JS method for finding caret position to help scrolling Scroll cursor back into view when it goes out of bounds Apr 27, 2016
@jesiegel1
Copy link
Collaborator Author

@cjwirth yessir. hopefully should be able to do it later today/tomorrow.

@KKKiller
Copy link

KKKiller commented May 6, 2016

I have used this branch in my project and it is absolutely much better that the master. but today i got a bug,and i have no idea about how to fix it.

when i set a HTMlL string to richEditor, it shown perfect .but when i click at any position of the text , and then i edit the text, it will scroll to the bottom . and after that ,it will work normal.

this bug is not fatal ,but it feel bad.can you help me or give me some advice! thanks much!

@jesiegel1
Copy link
Collaborator Author

Have you used this branch with the most recent commits?

@KKKiller
Copy link

KKKiller commented May 8, 2016

yes i have update the latest commit.
when i begin edit in any positoin, the corsor will come to the end, and the htmlString will creat a div at the end .
what should i do

@KKKiller
Copy link

KKKiller commented May 8, 2016

when i edit a HTMLstring, the latest cimmits add a mothed named as "wrapTextNodes", it will creat lots of div in my html, what is the function of this method?

@jesiegel1
Copy link
Collaborator Author

@KKKiller Without text nodes being wrapped in divs, it causes issues for calculating where the caret is. Why is creating lots of divs an issue? Generally you want your html content enclosed in some type of container. What is happening visually as a result of the divs being added?

@cjwirth
Copy link
Owner

cjwirth commented May 9, 2016

Hey, I've been away from Github for a while -- how is this going? Is the extra added div a problem? I understand that it's needed to calculate cursor position, but is it causing problems elsewhere?

@KKKiller
Copy link

KKKiller commented May 9, 2016

the divs will add lots of blank rows visually. i have remove the method "wrapTextNodes" in my project, it seems work normal. will there be any other bug if i remove it?

@jesiegel1
Copy link
Collaborator Author

did you actually try to see if it will add lots of blank rows? generally in html/css, if an explicit width/height isn't set for the div, the div takes up no space.

@KKKiller
Copy link

KKKiller commented May 9, 2016

beyond div and /div ,there is much of blank, i think it is the blank takes up the space.

@cjwirth
Copy link
Owner

cjwirth commented May 10, 2016

@KKKiller Maybe I'm not understanding what you're saying, but I'm not able to reproduce it in the sample app. Do you have some example code, or a screenshot or something?

@cjwirth
Copy link
Owner

cjwirth commented May 13, 2016

If not, I'll just merge this and we can open a new issue or something?

@KKKiller
Copy link

OK,look forward to your merge.

@cjwirth cjwirth merged commit b3be0fc into cjwirth:master May 13, 2016
@cjwirth
Copy link
Owner

cjwirth commented May 28, 2016

@jesiegel1 I'm not sure if you're interested or not, but I refactored it a little bit to make it simpler and more type safe. Thought you might like to take a look 😃

@jesiegel1
Copy link
Collaborator Author

@cjwirth always interested. will definitely take a look.

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

Successfully merging this pull request may close these issues.

None yet

4 participants