Skip to content

Commit

Permalink
fix: Usage of pushToHistory function on prev/next
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Nov 4, 2024
1 parent 77c8e49 commit 39ff237
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 11 deletions.
4 changes: 4 additions & 0 deletions css/main--fK07A8K.chunk.css

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions css/main-DA7qfHYK.chunk.css

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions css/main-DnH6w7F4.chunk.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion css/viewer-main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* extracted by css-entry-points-plugin */
@import './main--fK07A8K.chunk.css';
@import './main-DnH6w7F4.chunk.css';
@import './logger-B5Yp6f8I.chunk.css';
2 changes: 1 addition & 1 deletion js/viewer-init.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-init.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/viewer-main.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-main.mjs.map

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions src/files_actions/viewerAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Node, View } from '@nextcloud/files'

import { DefaultType, FileAction, Permission, registerFileAction } from '@nextcloud/files'
import { t } from '@nextcloud/l10n'
import svgEye from '@mdi/svg/svg/eye.svg?raw'

/**
* @param node The file to open
* @param view any The files view
* @param dir the directory path
*/
function pushToHistory(node: Node, view: View, dir: string) {
window.OCP.Files.Router.goToRoute(
null,
{ view: view.id, fileid: String(node.fileid) },
{ dir, openfile: 'true' },
true,
)
}

/**
* Execute the viewer files action
* @param node The active node
* @param view The current view
* @param dir The current path
*/
async function execAction(node: Node, view: View, dir: string): Promise<boolean|null> {
const onClose = () => {
// This can sometime be called with the openfile set to true already. But we don't want to keep openfile when closing the viewer.
const newQuery = { ...window.OCP.Files.Router.query }
delete newQuery.openfile
window.OCP.Files.Router.goToRoute(null, window.OCP.Files.Router.params, newQuery)
}

pushToHistory(node, view, dir)
window.OCA.Viewer.open({
path: node.path,
onPrev(fileInfo) {
pushToHistory(fileInfo, view, dir)
},
onNext(fileInfo) {
pushToHistory(fileInfo, view, dir)
},
onClose

Check warning on line 48 in src/files_actions/viewerAction.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
})

return null
}

/**
* Register the viewer action on the files API
*/
export function registerViewerAction() {
registerFileAction(new FileAction({
id: 'view',
displayName: () => t('viewer', 'View'),
iconSvgInline: () => svgEye,
default: DefaultType.DEFAULT,
enabled: (nodes) => {
// Disable if not located in user root
if (nodes.some(node => !(node.isDavRessource && node.root?.startsWith('/files')))) {
return false
}

return nodes.every((node) =>
Boolean(node.permissions & Permission.READ)
&& window.OCA.Viewer.mimetypes.includes(node.mime),
)
},
exec: execAction,
}))
}
11 changes: 10 additions & 1 deletion src/services/FilesActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ function filesActionHandler(node, view, dir) {
window.OCP.Files.Router.goToRoute(null, window.OCP.Files.Router.params, newQuery)
}
pushToHistory(node, view, dir)
OCA.Viewer.open({ path, onPrev: pushToHistory, onNext: pushToHistory, onClose })
window.OCA.Viewer.open({
path,
onPrev(fileInfo) {
pushToHistory(fileInfo, view, dir)
},
onNext(fileInfo) {
pushToHistory(fileInfo, view, dir)
},
onClose,
})
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1017,31 +1017,30 @@ export default {
* Open previous available file
*/
previous() {
const oldFileInfo = this.fileList[this.currentIndex]
this.currentIndex--
if (this.currentIndex < 0) {
this.currentIndex = this.fileList.length - 1
}

const fileInfo = this.fileList[this.currentIndex]
this.openFileFromList(fileInfo)
this.Viewer.onPrev(fileInfo, oldFileInfo)
this.Viewer.onPrev(fileInfo)
this.updateTitle(this.currentFile.basename)
},

/**
* Open next available file
*/
next() {
const oldFileInfo = this.fileList[this.currentIndex]
this.currentIndex++
if (this.currentIndex > this.fileList.length - 1) {
this.currentIndex = 0
}

const fileInfo = this.fileList[this.currentIndex]
this.openFileFromList(fileInfo)
this.Viewer.onNext(fileInfo, oldFileInfo)
this.Viewer.onNext(fileInfo)

this.updateTitle(this.currentFile.basename)
},

Expand Down

0 comments on commit 39ff237

Please sign in to comment.