refactor
This commit is contained in:
parent
99b4a48522
commit
7173deaa01
|
@ -37,8 +37,8 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import { computed, ComputedRef, isRef, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onDeactivated, ref, shallowRef, watch } from 'vue';
|
import { computed, ComputedRef, isRef, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onDeactivated, ref, shallowRef, watch } from 'vue';
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
import { useDocumentVisibility } from '@@/js/use-document-visibility.js';
|
import { useDocumentVisibility } from '@@/js/use-document-visibility.js';
|
||||||
|
import { onScrollTop, isTopVisible, getBodyScrollHeight, getScrollContainer, onScrollBottom, scrollToBottom, scroll, isBottomVisible } from '@@/js/scroll.js';
|
||||||
import { misskeyApi } from '@/misskey-api.js';
|
import { misskeyApi } from '@/misskey-api.js';
|
||||||
import { onScrollTop, isTopVisible, getBodyScrollHeight, getScrollContainer, onScrollBottom, scrollToBottom, scroll, isBottomVisible } from '@/to-be-shared/scroll.js';
|
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
|
|
||||||
const SECOND_FETCH_LIMIT = 30;
|
const SECOND_FETCH_LIMIT = 30;
|
||||||
|
|
|
@ -50,7 +50,7 @@ import { misskeyApi } from '@/scripts/misskey-api.js';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
import { instance } from '@/instance.js';
|
import { instance } from '@/instance.js';
|
||||||
import { url, instanceName } from '@/config.js';
|
import { url, instanceName } from '@/config.js';
|
||||||
import { scrollToTop } from '@/to-be-shared/scroll.js';
|
import { scrollToTop } from '@@/js/scroll.js';
|
||||||
import { isLink } from '@/scripts/is-link.js';
|
import { isLink } from '@/scripts/is-link.js';
|
||||||
import { defaultEmbedParams } from '@/embed-page.js';
|
import { defaultEmbedParams } from '@/embed-page.js';
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ import EmTimelineContainer from '@/components/EmTimelineContainer.vue';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
import { instance } from '@/instance.js';
|
import { instance } from '@/instance.js';
|
||||||
import { url, instanceName } from '@/config.js';
|
import { url, instanceName } from '@/config.js';
|
||||||
import { scrollToTop } from '@/scripts/scroll.js';
|
import { scrollToTop } from '@@/js/scroll.js';
|
||||||
import { isLink } from '@/scripts/is-link.js';
|
import { isLink } from '@/scripts/is-link.js';
|
||||||
import { defaultEmbedParams } from '@/embed-page.js';
|
import { defaultEmbedParams } from '@/embed-page.js';
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ import { i18n } from '@/i18n.js';
|
||||||
import { PageMetadata, provideMetadataReceiver, provideReactiveMetadata } from '@/scripts/page-metadata.js';
|
import { PageMetadata, provideMetadataReceiver, provideReactiveMetadata } from '@/scripts/page-metadata.js';
|
||||||
import { openingWindowsCount } from '@/os.js';
|
import { openingWindowsCount } from '@/os.js';
|
||||||
import { claimAchievement } from '@/scripts/achievements.js';
|
import { claimAchievement } from '@/scripts/achievements.js';
|
||||||
import { getScrollContainer } from '@/scripts/scroll.js';
|
import { getScrollContainer } from '@@/js/scroll.js';
|
||||||
import { useRouterFactory } from '@/router/supplier.js';
|
import { useRouterFactory } from '@/router/supplier.js';
|
||||||
import { mainRouter } from '@/router/main.js';
|
import { mainRouter } from '@/router/main.js';
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,9 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import { computed, ComputedRef, isRef, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onDeactivated, ref, shallowRef, watch } from 'vue';
|
import { computed, ComputedRef, isRef, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onDeactivated, ref, shallowRef, watch } from 'vue';
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
import { useDocumentVisibility } from '@@/js/use-document-visibility.js';
|
import { useDocumentVisibility } from '@@/js/use-document-visibility.js';
|
||||||
|
import { onScrollTop, isTopVisible, getBodyScrollHeight, getScrollContainer, onScrollBottom, scrollToBottom, scroll, isBottomVisible } from '@@/js/scroll.js';
|
||||||
import * as os from '@/os.js';
|
import * as os from '@/os.js';
|
||||||
import { misskeyApi } from '@/scripts/misskey-api.js';
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
||||||
import { onScrollTop, isTopVisible, getBodyScrollHeight, getScrollContainer, onScrollBottom, scrollToBottom, scroll, isBottomVisible } from '@/scripts/scroll.js';
|
|
||||||
import { defaultStore } from '@/store.js';
|
import { defaultStore } from '@/store.js';
|
||||||
import { MisskeyEntity } from '@/types/date-separated-list.js';
|
import { MisskeyEntity } from '@/types/date-separated-list.js';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
|
|
|
@ -25,7 +25,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, onUnmounted, ref, shallowRef } from 'vue';
|
import { onMounted, onUnmounted, ref, shallowRef } from 'vue';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
import { getScrollContainer } from '@/scripts/scroll.js';
|
import { getScrollContainer } from '@@/js/scroll.js';
|
||||||
import { isHorizontalSwipeSwiping } from '@/scripts/touch.js';
|
import { isHorizontalSwipeSwiping } from '@/scripts/touch.js';
|
||||||
|
|
||||||
const SCROLL_STOP = 10;
|
const SCROLL_STOP = 10;
|
||||||
|
|
|
@ -44,7 +44,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import { onMounted, onUnmounted, ref, inject, shallowRef, computed } from 'vue';
|
import { onMounted, onUnmounted, ref, inject, shallowRef, computed } from 'vue';
|
||||||
import tinycolor from 'tinycolor2';
|
import tinycolor from 'tinycolor2';
|
||||||
import XTabs, { Tab } from './MkPageHeader.tabs.vue';
|
import XTabs, { Tab } from './MkPageHeader.tabs.vue';
|
||||||
import { scrollToTop } from '@/scripts/scroll.js';
|
import { scrollToTop } from '@@/js/scroll.js';
|
||||||
import { globalEvents } from '@/events.js';
|
import { globalEvents } from '@/events.js';
|
||||||
import { injectReactiveMetadata } from '@/scripts/page-metadata.js';
|
import { injectReactiveMetadata } from '@/scripts/page-metadata.js';
|
||||||
import { $i, openAccountMenu as openAccountMenu_ } from '@/account.js';
|
import { $i, openAccountMenu as openAccountMenu_ } from '@/account.js';
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Directive } from 'vue';
|
import { Directive } from 'vue';
|
||||||
import { getScrollContainer, getScrollPosition } from '@/scripts/scroll.js';
|
import { getScrollContainer, getScrollPosition } from '@@/js/scroll.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
mounted(src, binding, vn) {
|
mounted(src, binding, vn) {
|
||||||
|
|
|
@ -36,7 +36,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import { computed, onMounted, onUnmounted, ref, shallowRef, watch, nextTick } from 'vue';
|
import { computed, onMounted, onUnmounted, ref, shallowRef, watch, nextTick } from 'vue';
|
||||||
import tinycolor from 'tinycolor2';
|
import tinycolor from 'tinycolor2';
|
||||||
import { popupMenu } from '@/os.js';
|
import { popupMenu } from '@/os.js';
|
||||||
import { scrollToTop } from '@/scripts/scroll.js';
|
import { scrollToTop } from '@@/js/scroll.js';
|
||||||
import MkButton from '@/components/MkButton.vue';
|
import MkButton from '@/components/MkButton.vue';
|
||||||
import { globalEvents } from '@/events.js';
|
import { globalEvents } from '@/events.js';
|
||||||
import { injectReactiveMetadata } from '@/scripts/page-metadata.js';
|
import { injectReactiveMetadata } from '@/scripts/page-metadata.js';
|
||||||
|
|
|
@ -27,7 +27,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import { computed, watch, ref, shallowRef } from 'vue';
|
import { computed, watch, ref, shallowRef } from 'vue';
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
import MkTimeline from '@/components/MkTimeline.vue';
|
import MkTimeline from '@/components/MkTimeline.vue';
|
||||||
import { scroll } from '@/scripts/scroll.js';
|
import { scroll } from '@@/js/scroll.js';
|
||||||
import * as os from '@/os.js';
|
import * as os from '@/os.js';
|
||||||
import { misskeyApi } from '@/scripts/misskey-api.js';
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
||||||
import { definePageMetadata } from '@/scripts/page-metadata.js';
|
import { definePageMetadata } from '@/scripts/page-metadata.js';
|
||||||
|
|
|
@ -40,7 +40,7 @@ import MkTimeline from '@/components/MkTimeline.vue';
|
||||||
import MkInfo from '@/components/MkInfo.vue';
|
import MkInfo from '@/components/MkInfo.vue';
|
||||||
import MkPostForm from '@/components/MkPostForm.vue';
|
import MkPostForm from '@/components/MkPostForm.vue';
|
||||||
import MkHorizontalSwipe from '@/components/MkHorizontalSwipe.vue';
|
import MkHorizontalSwipe from '@/components/MkHorizontalSwipe.vue';
|
||||||
import { scroll } from '@/scripts/scroll.js';
|
import { scroll } from '@@/js/scroll.js';
|
||||||
import * as os from '@/os.js';
|
import * as os from '@/os.js';
|
||||||
import { misskeyApi } from '@/scripts/misskey-api.js';
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
||||||
import { defaultStore } from '@/store.js';
|
import { defaultStore } from '@/store.js';
|
||||||
|
|
|
@ -27,7 +27,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import { computed, watch, ref, shallowRef } from 'vue';
|
import { computed, watch, ref, shallowRef } from 'vue';
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
import MkTimeline from '@/components/MkTimeline.vue';
|
import MkTimeline from '@/components/MkTimeline.vue';
|
||||||
import { scroll } from '@/scripts/scroll.js';
|
import { scroll } from '@@/js/scroll.js';
|
||||||
import { misskeyApi } from '@/scripts/misskey-api.js';
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
||||||
import { definePageMetadata } from '@/scripts/page-metadata.js';
|
import { definePageMetadata } from '@/scripts/page-metadata.js';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
|
|
|
@ -161,7 +161,7 @@ import MkTextarea from '@/components/MkTextarea.vue';
|
||||||
import MkOmit from '@/components/MkOmit.vue';
|
import MkOmit from '@/components/MkOmit.vue';
|
||||||
import MkInfo from '@/components/MkInfo.vue';
|
import MkInfo from '@/components/MkInfo.vue';
|
||||||
import MkButton from '@/components/MkButton.vue';
|
import MkButton from '@/components/MkButton.vue';
|
||||||
import { getScrollPosition } from '@/scripts/scroll.js';
|
import { getScrollPosition } from '@@/js/scroll.js';
|
||||||
import { getUserMenu } from '@/scripts/get-user-menu.js';
|
import { getUserMenu } from '@/scripts/get-user-menu.js';
|
||||||
import number from '@/filters/number.js';
|
import number from '@/filters/number.js';
|
||||||
import { userPage } from '@/filters/user.js';
|
import { userPage } from '@/filters/user.js';
|
||||||
|
|
|
@ -24,7 +24,7 @@ import * as Misskey from 'misskey-js';
|
||||||
import { onUpdated, ref, shallowRef } from 'vue';
|
import { onUpdated, ref, shallowRef } from 'vue';
|
||||||
import XNote from '@/pages/welcome.timeline.note.vue';
|
import XNote from '@/pages/welcome.timeline.note.vue';
|
||||||
import { misskeyApiGet } from '@/scripts/misskey-api.js';
|
import { misskeyApiGet } from '@/scripts/misskey-api.js';
|
||||||
import { getScrollContainer } from '@/scripts/scroll.js';
|
import { getScrollContainer } from '@@/js/scroll.js';
|
||||||
|
|
||||||
const notes = ref<Misskey.entities.Note[]>([]);
|
const notes = ref<Misskey.entities.Note[]>([]);
|
||||||
const isScrolling = ref(false);
|
const isScrolling = ref(false);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getScrollPosition, getScrollContainer, getStickyBottom, getStickyTop } from '@/scripts/scroll.js';
|
import { getScrollPosition, getScrollContainer, getStickyBottom, getStickyTop } from '@@/js/scroll.js';
|
||||||
import { getElementOrNull, getNodeOrNull } from '@/scripts/get-dom-node-or-null.js';
|
import { getElementOrNull, getNodeOrNull } from '@/scripts/get-dom-node-or-null.js';
|
||||||
|
|
||||||
type MaybeHTMLElement = EventTarget | Node | Element | HTMLElement;
|
type MaybeHTMLElement = EventTarget | Node | Element | HTMLElement;
|
||||||
|
|
|
@ -1,144 +0,0 @@
|
||||||
/*
|
|
||||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
*/
|
|
||||||
|
|
||||||
type ScrollBehavior = 'auto' | 'smooth' | 'instant';
|
|
||||||
|
|
||||||
export function getScrollContainer(el: HTMLElement | null): HTMLElement | null {
|
|
||||||
if (el == null || el.tagName === 'HTML') return null;
|
|
||||||
const overflow = window.getComputedStyle(el).getPropertyValue('overflow-y');
|
|
||||||
if (overflow === 'scroll' || overflow === 'auto') {
|
|
||||||
return el;
|
|
||||||
} else {
|
|
||||||
return getScrollContainer(el.parentElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getStickyTop(el: HTMLElement, container: HTMLElement | null = null, top = 0) {
|
|
||||||
if (!el.parentElement) return top;
|
|
||||||
const data = el.dataset.stickyContainerHeaderHeight;
|
|
||||||
const newTop = data ? Number(data) + top : top;
|
|
||||||
if (el === container) return newTop;
|
|
||||||
return getStickyTop(el.parentElement, container, newTop);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getStickyBottom(el: HTMLElement, container: HTMLElement | null = null, bottom = 0) {
|
|
||||||
if (!el.parentElement) return bottom;
|
|
||||||
const data = el.dataset.stickyContainerFooterHeight;
|
|
||||||
const newBottom = data ? Number(data) + bottom : bottom;
|
|
||||||
if (el === container) return newBottom;
|
|
||||||
return getStickyBottom(el.parentElement, container, newBottom);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getScrollPosition(el: HTMLElement | null): number {
|
|
||||||
const container = getScrollContainer(el);
|
|
||||||
return container == null ? window.scrollY : container.scrollTop;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onScrollTop(el: HTMLElement, cb: () => unknown, tolerance = 1, once = false) {
|
|
||||||
// とりあえず評価してみる
|
|
||||||
if (el.isConnected && isTopVisible(el)) {
|
|
||||||
cb();
|
|
||||||
if (once) return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const container = getScrollContainer(el) ?? window;
|
|
||||||
|
|
||||||
const onScroll = ev => {
|
|
||||||
if (!document.body.contains(el)) return;
|
|
||||||
if (isTopVisible(el, tolerance)) {
|
|
||||||
cb();
|
|
||||||
if (once) removeListener();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function removeListener() { container.removeEventListener('scroll', onScroll); }
|
|
||||||
|
|
||||||
container.addEventListener('scroll', onScroll, { passive: true });
|
|
||||||
return removeListener;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onScrollBottom(el: HTMLElement, cb: () => unknown, tolerance = 1, once = false) {
|
|
||||||
const container = getScrollContainer(el);
|
|
||||||
|
|
||||||
// とりあえず評価してみる
|
|
||||||
if (el.isConnected && isBottomVisible(el, tolerance, container)) {
|
|
||||||
cb();
|
|
||||||
if (once) return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const containerOrWindow = container ?? window;
|
|
||||||
const onScroll = ev => {
|
|
||||||
if (!document.body.contains(el)) return;
|
|
||||||
if (isBottomVisible(el, 1, container)) {
|
|
||||||
cb();
|
|
||||||
if (once) removeListener();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function removeListener() {
|
|
||||||
containerOrWindow.removeEventListener('scroll', onScroll);
|
|
||||||
}
|
|
||||||
|
|
||||||
containerOrWindow.addEventListener('scroll', onScroll, { passive: true });
|
|
||||||
return removeListener;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function scroll(el: HTMLElement, options: ScrollToOptions | undefined) {
|
|
||||||
const container = getScrollContainer(el);
|
|
||||||
if (container == null) {
|
|
||||||
window.scroll(options);
|
|
||||||
} else {
|
|
||||||
container.scroll(options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Scroll to Top
|
|
||||||
* @param el Scroll container element
|
|
||||||
* @param options Scroll options
|
|
||||||
*/
|
|
||||||
export function scrollToTop(el: HTMLElement, options: { behavior?: ScrollBehavior; } = {}) {
|
|
||||||
scroll(el, { top: 0, ...options });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Scroll to Bottom
|
|
||||||
* @param el Content element
|
|
||||||
* @param options Scroll options
|
|
||||||
* @param container Scroll container element
|
|
||||||
*/
|
|
||||||
export function scrollToBottom(
|
|
||||||
el: HTMLElement,
|
|
||||||
options: ScrollToOptions = {},
|
|
||||||
container = getScrollContainer(el),
|
|
||||||
) {
|
|
||||||
if (container) {
|
|
||||||
container.scroll({ top: el.scrollHeight - container.clientHeight + getStickyTop(el, container) || 0, ...options });
|
|
||||||
} else {
|
|
||||||
window.scroll({
|
|
||||||
top: (el.scrollHeight - window.innerHeight + getStickyTop(el, container) + (window.innerWidth <= 500 ? 96 : 0)) || 0,
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isTopVisible(el: HTMLElement, tolerance = 1): boolean {
|
|
||||||
const scrollTop = getScrollPosition(el);
|
|
||||||
return scrollTop <= tolerance;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isBottomVisible(el: HTMLElement, tolerance = 1, container = getScrollContainer(el)) {
|
|
||||||
if (container) return el.scrollHeight <= container.clientHeight + Math.abs(container.scrollTop) + tolerance;
|
|
||||||
return el.scrollHeight <= window.innerHeight + window.scrollY + tolerance;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://ja.javascript.info/size-and-scroll-window#ref-932
|
|
||||||
export function getBodyScrollHeight() {
|
|
||||||
return Math.max(
|
|
||||||
document.body.scrollHeight, document.documentElement.scrollHeight,
|
|
||||||
document.body.offsetHeight, document.documentElement.offsetHeight,
|
|
||||||
document.body.clientHeight, document.documentElement.clientHeight,
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -26,7 +26,7 @@ import * as os from '@/os.js';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
import { PageMetadata, provideMetadataReceiver, provideReactiveMetadata } from '@/scripts/page-metadata.js';
|
import { PageMetadata, provideMetadataReceiver, provideReactiveMetadata } from '@/scripts/page-metadata.js';
|
||||||
import { useScrollPositionManager } from '@/nirax.js';
|
import { useScrollPositionManager } from '@/nirax.js';
|
||||||
import { getScrollContainer } from '@/scripts/scroll.js';
|
import { getScrollContainer } from '@@/js/scroll.js';
|
||||||
import { mainRouter } from '@/router/main.js';
|
import { mainRouter } from '@/router/main.js';
|
||||||
|
|
||||||
defineProps<{
|
defineProps<{
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import { describe, test, assert, afterEach } from 'vitest';
|
import { describe, test, assert, afterEach } from 'vitest';
|
||||||
import { Window } from 'happy-dom';
|
import { Window } from 'happy-dom';
|
||||||
import { onScrollBottom, onScrollTop } from '@/scripts/scroll.js';
|
import { onScrollBottom, onScrollTop } from '@@/js/scroll.js';
|
||||||
|
|
||||||
describe('Scroll', () => {
|
describe('Scroll', () => {
|
||||||
describe('onScrollTop', () => {
|
describe('onScrollTop', () => {
|
||||||
|
|
Loading…
Reference in New Issue