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

Report proper height of text being edited #132

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MarkupEditor/MarkupWKWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ public class MarkupWKWebView: WKWebView, ObservableObject {
}

public func updateHeight(handler: ((Int)->Void)?) {
getClientHeight() { clientHeight in
getHeight() { clientHeight in
if self.editorHeight != clientHeight {
self.editorHeight = clientHeight
handler?(self.contentHeight(from: clientHeight))
Expand Down Expand Up @@ -790,8 +790,8 @@ public class MarkupWKWebView: WKWebView, ObservableObject {
pasteboard.setItems([items])
}

private func getClientHeight(_ handler: @escaping ((Int)->Void)) {
evaluateJavaScript("document.getElementById('editor').clientHeight") { result, error in
private func getHeight(_ handler: @escaping ((Int)->Void)) {
evaluateJavaScript("MU.getHeight()") { result, error in
handler(result as? Int ?? 0)
}
}
Expand Down
29 changes: 29 additions & 0 deletions MarkupEditor/Resources/markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,35 @@ MU.setHTML = function(contents, select=true) {
_callback('updateHeight');
};

/**
* Return the height of the editor element that encloses the text.
*
* The padding-block is set in CSS to allow touch selection outside of text on iOS.
* An unfortunate side-effect of that setting is that getBoundingClientRect() returns
* a height that has nothing to do with the actual text, because it's been padded.
* A workaround for this is to get the computed style for editor using
* window.getComputedStyle(editor, null), and then asking that for the height. It does
* not include padding. This kind of works, except that I found the height changed as
* soon as I add a single character to the text. So, for example, it shows 21px when it
* opens with just a single <p>Foo</p>, but once you add a character to the text, the
* height shows up as 36px. If you remove padding-block, then the behavior goes away.
* To work around the problem, we set the padding block to 0 before getting height, and
* then set it back afterward. With this change, both the touch-outside-of-text works
* and the height is reported accurately. Height needs to be reported accurately for
* auto-sizing of a WKWebView based on its contents.
*/
MU.getHeight = function() {
const paddingBlockStart = editor.style.getPropertyValue("padding-block-start");
const paddingBlockEnd = editor.style.getPropertyValue("padding-block-end");
editor.style["padding-block-start"] = "0px";
editor.style["padding-block-end"] = "0px";
const style = window.getComputedStyle(editor, null);
const height = parseInt(style.getPropertyValue("height"));
editor.style["padding-block-start"] = paddingBlockStart;
editor.style["padding-block-end"] = paddingBlockEnd;
return height;
};

/**
* Focus immediately, leaving range alone
*/
Expand Down