Skip to content

Commit

Permalink
Fix scroll glue behavior (#923)
Browse files Browse the repository at this point in the history
Fixes #18.
  • Loading branch information
vortex852456 authored and threema-danilo committed Nov 13, 2019
1 parent 1b1705c commit 3e1f7e0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
26 changes: 0 additions & 26 deletions src/helpers.ts
Expand Up @@ -219,32 +219,6 @@ export function isString(val: any): boolean {
return typeof val === 'string' || val instanceof String;
}

/**
* Throttle function.
*
* Taken from https://remysharp.com/2010/07/21/throttling-function-calls
*/
export function throttle(fn, threshold: number = 250, scope) {
let last;
let deferTimer;
return function() {
const context = scope || this;
const now = +(new Date());
const args = arguments;
if (last && now < last + threshold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function() {
last = now;
fn.apply(context, args);
}, threshold);
} else {
last = now;
fn.apply(context, args);
}
};
}

/**
* Detect whether browser supports passive event listeners.
*
Expand Down
10 changes: 5 additions & 5 deletions src/partials/messenger.ts
Expand Up @@ -27,7 +27,7 @@ import {Logger} from 'ts-log';
import {ContactControllerModel} from '../controller_model/contact';
import {DialogController} from '../controllers/dialog';
import {TroubleshootingController} from '../controllers/troubleshooting';
import {bufferToUrl, hasValue, supportsPassive, throttle, u8aToHex} from '../helpers';
import {bufferToUrl, hasValue, supportsPassive, u8aToHex} from '../helpers';
import {emojify} from '../helpers/emoji';
import {ContactService} from '../services/contact';
import {ControllerService} from '../services/controller';
Expand Down Expand Up @@ -406,11 +406,11 @@ class ConversationController {
this.domChatElement = document.querySelector('#conversation-chat') as HTMLElement;

// Add custom event handlers
this.domChatElement.addEventListener('scroll', throttle(() => {
this.domChatElement.addEventListener('scroll', () => {
$rootScope.$apply(() => {
this.updateScrollJump();
});
}, 100, this), supportsPassive() ? {passive: true} : false);
}, supportsPassive() ? {passive: true} : false);
}

// Set receiver, conversation and type
Expand Down Expand Up @@ -915,12 +915,12 @@ class ConversationController {
}

/**
* Only show the scroll to bottom button if user scrolled more than 10px
* Only show the scroll to bottom button if user scrolled more than 1px
* away from bottom.
*/
private updateScrollJump(): void {
const chat = this.domChatElement;
this.showScrollJump = chat.scrollHeight - (chat.scrollTop + chat.offsetHeight) > 10;
this.showScrollJump = chat.scrollHeight - (chat.scrollTop + chat.offsetHeight) > 1;
}

/**
Expand Down

0 comments on commit 3e1f7e0

Please sign in to comment.