Skip to content

Commit

Permalink
perf: slightly faster key up/down (#8356)
Browse files Browse the repository at this point in the history
* perf: slightly faster key up/down

* Update packages/web-app-files/tests/unit/components/FilesList/KeyboardActions.spec.ts

Co-authored-by: Jannik Stehle <[email protected]>

* fix: linter complaints

---------

Co-authored-by: Jannik Stehle <[email protected]>
  • Loading branch information
kulmann and JammingBen authored Feb 16, 2023
1 parent c62f004 commit ac2475e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/enhancement-key-up-down-performance
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Slight improvement of key up/down performance

The render performance of the key up/down events in file lists has been improved slightly.

https://github.com/owncloud/web/pull/8356
35 changes: 11 additions & 24 deletions packages/web-app-files/src/components/FilesList/KeyboardActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
ref,
unref
} from 'vue'
import { SpaceResource } from 'web-client/src/helpers'
import { Resource, SpaceResource } from 'web-client/src/helpers'
import { useScrollTo } from 'web-app-files/src/composables/scrollTo'
import { useClientService, useStore } from 'web-pkg'
import { useGettext } from 'vue3-gettext'
export default defineComponent({
props: {
paginatedResources: {
type: Array,
type: Array as PropType<Resource[]>,
required: true
},
keybindOnElementId: {
Expand Down Expand Up @@ -82,28 +82,20 @@ export default defineComponent({
selectionCursor.value = 0
}
const getNextResourceId = (previous = false) => {
const latestSelectedRow = document.querySelectorAll(
`[data-item-id='${unref(latestSelectedId)}']`
)[0]
let nextRow
try {
nextRow = (
previous ? latestSelectedRow.previousElementSibling : latestSelectedRow.nextElementSibling
) as HTMLElement
} catch {
const latestSelectedResourceIndex = props.paginatedResources.findIndex(
(resource) => resource.id === latestSelectedId.value
)
if (latestSelectedResourceIndex === -1) {
return -1
}
if (nextRow === null) {
const nextResourceIndex = latestSelectedResourceIndex + (previous ? -1 : 1)
if (nextResourceIndex < 0 || nextResourceIndex >= props.paginatedResources.length) {
return -1
}
return nextRow.getAttribute('data-item-id')
return props.paginatedResources[nextResourceIndex].id
}
const getFirstResourceId = () => {
const firstRow = document.getElementsByClassName('oc-tbody-tr')[0]
if (!firstRow) {
return -1
}
return firstRow.getAttribute('data-item-id')
return props.paginatedResources.length ? props.paginatedResources[0].id : -1
}
const handleSpaceAction = (event) => {
Expand All @@ -124,12 +116,7 @@ export default defineComponent({
}
const handleNavigateAction = (event, up = false) => {
event.preventDefault()
let nextId
if (!unref(latestSelectedId)) {
nextId = getFirstResourceId()
} else {
nextId = getNextResourceId(up)
}
const nextId = !unref(latestSelectedId) ? getFirstResourceId() : getNextResourceId(up)
if (nextId === -1) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ describe('KeyboardActions', () => {
})
})
describe('global shortcuts', () => {
beforeEach(() => {
document.body.innerHTML =
'<table>' +
'<tr class="oc-tbody-tr" data-item-id="0"><td></td></tr>' +
'<tr class="oc-tbody-tr" data-item-id="1"><td></td></tr>' +
'<tr class="oc-tbody-tr" data-item-id="2"><td></td></tr>' +
'</table>'
})

it('copy selected files', () => {
const event = new KeyboardEvent('keyDown', {
keyCode: keycode('c'),
Expand Down Expand Up @@ -123,7 +114,6 @@ describe('KeyboardActions', () => {
const event = new KeyboardEvent('keyDown', { keyCode: keycode(key) })
const { wrapper, storeOptions } = getWrapper()
wrapper.vm.handleGlobalShortcuts(event)
expect(storeOptions.modules.Files.actions.resetFileSelection).toHaveBeenCalled()
expect(storeOptions.modules.Files.mutations.ADD_FILE_SELECTION).toHaveBeenCalled()
})
it.each(['down', 'up'])('navigate via shift + up and down keys', (key) => {
Expand Down Expand Up @@ -178,7 +168,15 @@ const getWrapper = ({ props = {}, latestSelectedId = undefined } = {}) => {
return {
storeOptions,
wrapper: mount(KeyboardActions, {
props: { paginatedResources: [], space: mock<SpaceResource>(), ...props },
props: {
paginatedResources: [
mock<Resource>({ id: 0 }),
mock<Resource>({ id: 1 }),
mock<Resource>({ id: 2 })
],
space: mock<SpaceResource>(),
...props
},
global: {
plugins: [...defaultPlugins(), store]
}
Expand Down

0 comments on commit ac2475e

Please sign in to comment.