Skip to content

Commit

Permalink
fix(frontend/scroll): no callback for disconnected elements (#11714)
Browse files Browse the repository at this point in the history
* fix(frontend/scroll): no callback for disconnected elements

* Update CHANGELOG.md
  • Loading branch information
saschanaz authored Aug 13, 2023
1 parent bbef2a9 commit ab58b65
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
- Fix: サーバー情報画面(`/instance-info/{domain}`)でブロックができないのを修正
- Fix: 未読のお知らせの「わかった」をクリック・タップしてもその場で「わかった」が消えない問題を修正
- Fix: iOSで画面を回転させるとテキストサイズが変わる問題を修正
- Fix: タイムラインを下にスクロールしてノート画面に移動して再び戻ったら以前のスクロール位置を失う問題を修正

### Server
- cacheRemoteFilesの初期値はfalseになりました
- 一部のfeatured noteを照会できない問題を修正
- ファイルアップロード時等にファイル名の拡張子を修正する関数(correctFilename)の挙動を改善
- fix: muteがapiからのuser list timeline取得で機能しない問題を修正
- Fix: 一部のfeatured noteを照会できない問題を修正
- Fix: muteがapiからのuser list timeline取得で機能しない問題を修正

## 13.14.2

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/scripts/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getScrollPosition(el: HTMLElement | null): number {

export function onScrollTop(el: HTMLElement, cb: () => unknown, tolerance = 1, once = false) {
// とりあえず評価してみる
if (isTopVisible(el)) {
if (el.isConnected && isTopVisible(el)) {
cb();
if (once) return null;
}
Expand All @@ -54,7 +54,7 @@ export function onScrollBottom(el: HTMLElement, cb: () => unknown, tolerance = 1
const container = getScrollContainer(el);

// とりあえず評価してみる
if (isBottomVisible(el, tolerance, container)) {
if (el.isConnected && isBottomVisible(el, tolerance, container)) {
cb();
if (once) return null;
}
Expand Down
64 changes: 64 additions & 0 deletions packages/frontend/test/scroll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { describe, test, assert, afterEach } from 'vitest';
import { Window } from 'happy-dom';
import { onScrollBottom, onScrollTop } from '@/scripts/scroll';

describe('Scroll', () => {
describe('onScrollTop', () => {
test('Initial onScrollTop callback for connected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);

document.body.append(div);

let called = false;
onScrollTop(div as any as HTMLElement, () => called = true);

assert.ok(called);
});

test('No onScrollTop callback for disconnected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);

let called = false;
onScrollTop(div as any as HTMLElement, () => called = true);

assert.ok(!called);
});
});

describe('onScrollBottom', () => {
test('Initial onScrollBottom callback for connected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);
(div as any).scrollHeight = 100; // happy-dom has no scrollHeight

document.body.append(div);

let called = false;
onScrollBottom(div as any as HTMLElement, () => called = true);

assert.ok(called);
});

test('No onScrollBottom callback for disconnected elements', () => {
const { document } = new Window();
const div = document.createElement('div');
assert.strictEqual(div.scrollTop, 0);
(div as any).scrollHeight = 100; // happy-dom has no scrollHeight

let called = false;
onScrollBottom(div as any as HTMLElement, () => called = true);

assert.ok(!called);
});
});
});

0 comments on commit ab58b65

Please sign in to comment.