2018-11-17 18:06:49 -08:00
|
|
|
<VirtualListContainer {realm} on:initialized on:noNeedToScroll >
|
2018-03-29 23:16:53 -07:00
|
|
|
<div class="virtual-list"
|
2018-05-01 17:05:36 -07:00
|
|
|
style="height: {$height}px;"
|
2018-03-22 17:33:42 -07:00
|
|
|
ref:node >
|
2018-05-01 17:05:36 -07:00
|
|
|
<VirtualListHeader component={headerComponent} virtualProps={headerProps} shown={$showHeader}/>
|
|
|
|
{#if $visibleItems}
|
|
|
|
{#each $visibleItems as visibleItem (visibleItem.key)}
|
|
|
|
<VirtualListLazyItem {component}
|
|
|
|
offset={visibleItem.offset}
|
|
|
|
{makeProps}
|
|
|
|
key={visibleItem.key}
|
|
|
|
index={visibleItem.index}
|
2018-02-12 22:06:05 -08:00
|
|
|
/>
|
2018-05-01 17:05:36 -07:00
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
{#if $showFooter}
|
|
|
|
<VirtualListFooter component={footerComponent} />
|
|
|
|
{/if}
|
2018-02-12 22:06:05 -08:00
|
|
|
</div>
|
|
|
|
</VirtualListContainer>
|
2018-01-15 10:54:02 -08:00
|
|
|
<style>
|
|
|
|
.virtual-list {
|
|
|
|
position: relative;
|
2019-04-13 20:02:25 +02:00
|
|
|
width: 100%;
|
2018-01-15 10:54:02 -08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
2018-02-12 22:06:05 -08:00
|
|
|
import VirtualListContainer from './VirtualListContainer.html'
|
2018-01-23 18:15:14 -08:00
|
|
|
import VirtualListLazyItem from './VirtualListLazyItem'
|
2018-01-21 16:07:11 -08:00
|
|
|
import VirtualListFooter from './VirtualListFooter.html'
|
2018-02-11 19:15:21 -08:00
|
|
|
import VirtualListHeader from './VirtualListHeader.html'
|
2018-01-25 08:23:14 -08:00
|
|
|
import { virtualListStore } from './virtualListStore'
|
2018-04-05 17:57:36 -07:00
|
|
|
import throttle from 'lodash-es/throttle'
|
2018-01-25 08:23:14 -08:00
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-04-05 17:57:36 -07:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-04-30 08:29:04 -07:00
|
|
|
import { observe } from 'svelte-extras'
|
2018-01-15 10:54:02 -08:00
|
|
|
|
2018-03-20 20:28:53 -07:00
|
|
|
const DISTANCE_FROM_BOTTOM_TO_FIRE = 800
|
2018-02-11 19:15:21 -08:00
|
|
|
const SCROLL_EVENT_THROTTLE = 1000
|
2018-01-15 17:25:32 -08:00
|
|
|
|
2018-01-15 10:54:02 -08:00
|
|
|
export default {
|
2018-01-15 17:25:32 -08:00
|
|
|
oncreate () {
|
2018-02-11 20:12:15 -08:00
|
|
|
this.fireScrollToBottom = throttle(() => {
|
|
|
|
this.fire('scrollToBottom')
|
|
|
|
}, SCROLL_EVENT_THROTTLE)
|
|
|
|
this.fireScrollToTop = throttle(() => {
|
|
|
|
this.fire('scrollToTop')
|
|
|
|
}, SCROLL_EVENT_THROTTLE)
|
2018-02-11 14:17:17 -08:00
|
|
|
this.observe('showFooter', showFooter => {
|
2018-02-11 20:12:15 -08:00
|
|
|
mark('set showFooter')
|
2018-08-29 21:42:57 -07:00
|
|
|
this.store.setForRealm({ showFooter: showFooter })
|
2018-02-11 20:12:15 -08:00
|
|
|
mark('set showFooter')
|
2018-01-21 16:07:11 -08:00
|
|
|
})
|
2018-02-11 19:15:21 -08:00
|
|
|
this.observe('showHeader', showHeader => {
|
2018-02-11 20:12:15 -08:00
|
|
|
mark('set showHeader')
|
2018-08-29 21:42:57 -07:00
|
|
|
this.store.setForRealm({ showHeader: showHeader })
|
2018-02-11 20:12:15 -08:00
|
|
|
stop('set showHeader')
|
2018-02-11 19:15:21 -08:00
|
|
|
})
|
2018-02-25 10:50:04 -08:00
|
|
|
this.observe('items', (newItems, oldItems) => {
|
|
|
|
if (!newItems || isEqual(newItems, oldItems)) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-17 00:59:15 -08:00
|
|
|
mark('set items')
|
2018-08-29 21:42:57 -07:00
|
|
|
this.store.setForRealm({ items: newItems })
|
2018-01-17 00:59:15 -08:00
|
|
|
stop('set items')
|
2018-01-15 10:54:02 -08:00
|
|
|
})
|
2020-08-29 19:19:16 -07:00
|
|
|
// We observe on the component rather than the store to avoid a leak in store listeners
|
|
|
|
// (Svelte automatically removes component listeners, but not store listeners)
|
2018-12-04 07:24:55 -08:00
|
|
|
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
|
feat: Add support for keyboard shortcuts (#870)
* Add support for keyboard shortcuts.
This change introduces a Shortcut component for defining global
keyboard shortcuts from whichever component makes more sense.
This change also adds an initial set of navigation shortcuts:
- Backspace to leave a modal dialog or to go back
- g t to go to the federated timeline
- g f to go to the favorite page
- g h to go to the home page
- g n to go to the notification page
- g c to go to the community page
- s to go to the search page
These shortcuts are loaded asynchronously from _layout.html
In modal dialogs, shortcuts are also modal, to avoid strange or
overly complex behavior. This is implemented by grouping
shortcuts into scopes, and activating a separate 'modal' scope
when entering a modal dialog, so a separate set of shortcuts can
be enabled in modal dialog. Modal dialogs can be exited by
pressing 'Backspace'.
* Navigate up/down lists using keyboard shortcuts.
This change introduces keyboard shortcuts for navigating in lists and
virtual lists. j or arrow up selects the next element, k or arrow down,
the previous element. Selecting an element scrolls the list up and down,
as necessary.
This change also allows directing keyboard shortcuts to the active
element and defines the following shortcuts, for the active status:
- f to favorite or unfavorite it
- b to boost or unboost it
- r to reply to it
- o to open its thread
- x to toggle the display of a CW
- y to toggle the display of sensitive medias
This works by defining a keyboard shortcut scope for each list element.
A new component, ScrollListShortcuts, keeps track of the active element,
based on list or virtual list elements and redirects shortcuts to the
active element's scope. ScrollListShortcuts keeps the active element in
the current realm of the store, so the active element is restored when
going back to the list.
* Typing h or ? displays the list of available keyboard shortcuts.
This change introduces a new modal dialog that documents the list of
available shortcuts.
2019-01-13 19:03:29 +01:00
|
|
|
this.calculateListOffset()
|
2018-01-24 19:26:08 -08:00
|
|
|
if (allVisibleItemsHaveHeight) {
|
|
|
|
this.fire('initializedVisibleItems')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-02-11 14:17:17 -08:00
|
|
|
this.observe('distanceFromBottom', (distanceFromBottom) => {
|
2018-01-17 00:06:24 -08:00
|
|
|
if (distanceFromBottom >= 0 &&
|
2018-01-16 21:43:31 -08:00
|
|
|
distanceFromBottom <= DISTANCE_FROM_BOTTOM_TO_FIRE) {
|
2018-01-18 21:52:58 -08:00
|
|
|
this.fireScrollToBottom()
|
2018-01-16 21:43:31 -08:00
|
|
|
}
|
2020-08-29 19:19:16 -07:00
|
|
|
}, { init: false })
|
2018-02-11 19:15:21 -08:00
|
|
|
|
2018-12-04 07:24:55 -08:00
|
|
|
this.observe('scrollTop', (scrollTop) => {
|
2018-02-13 09:15:10 -08:00
|
|
|
this.fire('scrollTopChanged', scrollTop)
|
|
|
|
if (scrollTop === 0) {
|
2018-02-11 19:15:21 -08:00
|
|
|
this.fireScrollToTop()
|
|
|
|
}
|
2018-03-22 17:33:42 -07:00
|
|
|
this.calculateListOffset()
|
2018-02-11 19:15:21 -08:00
|
|
|
})
|
2018-01-15 18:29:28 -08:00
|
|
|
},
|
2018-01-15 10:54:02 -08:00
|
|
|
data: () => ({
|
2018-01-15 12:23:28 -08:00
|
|
|
component: null
|
2018-01-15 10:54:02 -08:00
|
|
|
}),
|
2018-01-15 12:23:28 -08:00
|
|
|
store: () => virtualListStore,
|
2018-01-15 10:54:02 -08:00
|
|
|
components: {
|
2018-02-12 22:06:05 -08:00
|
|
|
VirtualListContainer,
|
2018-01-23 18:15:14 -08:00
|
|
|
VirtualListLazyItem,
|
2018-02-11 19:15:21 -08:00
|
|
|
VirtualListFooter,
|
2019-02-23 09:47:36 -08:00
|
|
|
VirtualListHeader
|
2018-01-16 23:16:15 -08:00
|
|
|
},
|
|
|
|
computed: {
|
2018-05-01 17:05:36 -07:00
|
|
|
distanceFromBottom: ({ $scrollHeight, $scrollTop, $offsetHeight }) => {
|
2018-01-16 23:16:15 -08:00
|
|
|
return $scrollHeight - $scrollTop - $offsetHeight
|
2018-12-04 07:24:55 -08:00
|
|
|
},
|
|
|
|
scrollTop: ({ $scrollTop }) => $scrollTop,
|
2019-02-22 20:35:13 -08:00
|
|
|
allVisibleItemsHaveHeight: ({ $allVisibleItemsHaveHeight }) => $allVisibleItemsHaveHeight,
|
|
|
|
visibleItemKeys: ({ $visibleItems }) => ($visibleItems || []).map(_ => _.key)
|
2018-03-22 17:33:42 -07:00
|
|
|
},
|
|
|
|
methods: {
|
2018-04-30 08:29:04 -07:00
|
|
|
observe,
|
2018-04-19 21:38:01 -07:00
|
|
|
calculateListOffset () {
|
2018-03-22 17:33:42 -07:00
|
|
|
// TODO: better way to get the offset top?
|
2019-08-03 13:49:37 -07:00
|
|
|
const node = this.refs.node
|
2018-03-22 17:33:42 -07:00
|
|
|
if (!node) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('calculateListOffset')
|
2019-08-03 13:49:37 -07:00
|
|
|
const { offsetParent } = node
|
2019-02-21 23:50:27 -08:00
|
|
|
// TODO: offsetParent is null sometimes in testcafe tests
|
2019-08-03 13:49:37 -07:00
|
|
|
const listOffset = offsetParent ? offsetParent.offsetTop : 0
|
2018-08-29 21:42:57 -07:00
|
|
|
this.store.setForRealm({ listOffset })
|
2018-03-22 17:33:42 -07:00
|
|
|
stop('calculateListOffset')
|
|
|
|
}
|
2018-01-15 10:54:02 -08:00
|
|
|
}
|
|
|
|
}
|
feat: Add support for keyboard shortcuts (#870)
* Add support for keyboard shortcuts.
This change introduces a Shortcut component for defining global
keyboard shortcuts from whichever component makes more sense.
This change also adds an initial set of navigation shortcuts:
- Backspace to leave a modal dialog or to go back
- g t to go to the federated timeline
- g f to go to the favorite page
- g h to go to the home page
- g n to go to the notification page
- g c to go to the community page
- s to go to the search page
These shortcuts are loaded asynchronously from _layout.html
In modal dialogs, shortcuts are also modal, to avoid strange or
overly complex behavior. This is implemented by grouping
shortcuts into scopes, and activating a separate 'modal' scope
when entering a modal dialog, so a separate set of shortcuts can
be enabled in modal dialog. Modal dialogs can be exited by
pressing 'Backspace'.
* Navigate up/down lists using keyboard shortcuts.
This change introduces keyboard shortcuts for navigating in lists and
virtual lists. j or arrow up selects the next element, k or arrow down,
the previous element. Selecting an element scrolls the list up and down,
as necessary.
This change also allows directing keyboard shortcuts to the active
element and defines the following shortcuts, for the active status:
- f to favorite or unfavorite it
- b to boost or unboost it
- r to reply to it
- o to open its thread
- x to toggle the display of a CW
- y to toggle the display of sensitive medias
This works by defining a keyboard shortcut scope for each list element.
A new component, ScrollListShortcuts, keeps track of the active element,
based on list or virtual list elements and redirects shortcuts to the
active element's scope. ScrollListShortcuts keeps the active element in
the current realm of the store, so the active element is restored when
going back to the list.
* Typing h or ? displays the list of available keyboard shortcuts.
This change introduces a new modal dialog that documents the list of
available shortcuts.
2019-01-13 19:03:29 +01:00
|
|
|
</script>
|