-
Notifications
You must be signed in to change notification settings - Fork 446
Scroll cursor back into view when it goes out of bounds #34
Changes from 4 commits
154e5e1
55396c1
5dbbf81
d3bf5b1
ae7c2fe
50597b9
c6c5be5
13ebbbf
7c59a46
d39d4c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,18 +17,24 @@ | |
@charset "UTF-8"; | ||
|
||
html { | ||
height: 100%; | ||
min-height: 100%; | ||
} | ||
|
||
body { | ||
min-height: 100%; | ||
overflow: hidden; | ||
overflow: auto; | ||
} | ||
|
||
body, h1, p { | ||
margin: 0; | ||
} | ||
|
||
div { | ||
display: block; | ||
position: static; | ||
min-height: 28px; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this 28px about? Is it necessary? I actually don't know the implications of much of this CSS that's being added. I hope it doesn't change the way it looks for people who are already using it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the |
||
} | ||
|
||
#editor { | ||
-webkit-user-select: auto !important; | ||
-webkit-tap-highlight-color: rgba(0, 0, 0, 0); | ||
|
@@ -47,6 +53,9 @@ body, h1, p { | |
|
||
#editor { | ||
min-height: 100%; | ||
height: 100%; | ||
overflow: auto; | ||
display: block; | ||
font-family: 'HelveticaNeue'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like it wasn't your fault, but could you get this indention to be the same? |
||
color:#666666; | ||
line-height:28px; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,12 @@ extension RichEditorView { | |
return runJS("RE.rangeOrCaretSelectionExists();") == "true" ? true : false | ||
} | ||
|
||
/** Returns caret vertical position */ | ||
|
||
public func getCaretPosition() -> String { | ||
return runJS("RE.getCaretPosition();") | ||
} | ||
|
||
/** | ||
* If the current selection's parent is an anchor tag, get the href. | ||
* @returns nil if href is empty, otherwise a non-empty String | ||
|
@@ -439,6 +445,35 @@ extension RichEditorView: UIGestureRecognizerDelegate { | |
// MARK: - Utilities | ||
extension RichEditorView { | ||
|
||
/** Sets content offset for scrollView based on caret location **/ | ||
|
||
private func fixScrollView(editor: RichEditorView) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an instance method, you don't need to pass in the Also |
||
|
||
var scrollView = editor.webView.scrollView | ||
|
||
var contentHeight: CGFloat? | ||
let htmlHeight = editor.runJS("document.getElementById('editor').clientHeight;") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Even in this case, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did it that way to match: https://github.com/jesiegel1/RichEditorView/blob/master/RichEditorView/Classes/RichEditorView.swift#L176 |
||
if let n = NSNumberFormatter().numberFromString(htmlHeight) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Swift you can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
let floatValue = CGFloat(n) | ||
contentHeight = floatValue | ||
} else { | ||
contentHeight = scrollView.frame.height | ||
} | ||
scrollView.contentSize = CGSizeMake(scrollView.frame.width, contentHeight!) | ||
|
||
let caretPosition: String = editor.getCaretPosition() | ||
|
||
if let caretPositionNumeric = NSNumberFormatter().numberFromString(caretPosition) { | ||
let caretFloat = CGFloat(caretPositionNumeric) - scrollView.contentOffset.y | ||
if caretFloat >= (scrollView.bounds.size.height) { | ||
let bottomOffset = CGPointMake(0, (caretFloat + scrollView.contentOffset.y) - scrollView.bounds.size.height + scrollView.contentInset.bottom | ||
) | ||
scrollView.setContentOffset(bottomOffset, animated: true) | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
Runs some JavaScript on the UIWebView and returns the result | ||
If there is no result, returns an empty string | ||
|
@@ -513,6 +548,7 @@ extension RichEditorView { | |
updateHeight() | ||
} | ||
else if method.hasPrefix("input") { | ||
fixScrollView(self) | ||
let content = runJS("RE.getHtml()") | ||
contentHTML = content | ||
updateHeight() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ class KeyboardManager: NSObject { | |
Starts monitoring for keyboard notifications in order to show/hide the toolbar | ||
*/ | ||
func beginMonitoring() { | ||
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(KeyboardManager.keyboardWillShowOrHide(_:)), name: UIKeyboardWillShowNotification, object: nil) | ||
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(KeyboardManager.keyboardWillShowOrHide(_:)), name: UIKeyboardWillHideNotification, object: nil) | ||
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("KeyboardManager.keyboardWillShowOrHide:"), name: UIKeyboardWillShowNotification, object: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The project is already using Swift 2.2 selector syntax, doesn't it make sense to use here too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. Now that Xcode 7.3 official is out, I think we should migrate everything to being Swift 2.2 only. Although -- this file is just in the sample project. Do you think we should make a major version change if we have changes which break in Xcode 7.2 but work in 7.3? I feel like I want to say yes... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, I simply switched it back since 7.3 wasn't out and switching it back every time i updated a branch was getting annoying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it would be nice to have these in Xcode 7.3 form |
||
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("KeyboardManager.keyboardWillShowOrHide:"), name: UIKeyboardWillHideNotification, object: nil) | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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?