Skip to content

Commit 216bac4

Browse files
committed
fix(image-editor): open newly created image when closing the image editor
Signed-off-by: Hamza <hamzamahjoubi221@gmail.com>
1 parent e95908e commit 216bac4

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/components/ImageEditor.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,16 @@ export default {
235235
try {
236236
const blob = await new Promise(resolve => imageCanvas.toBlob(resolve, mimeType, quality))
237237
const response = await axios.put(putUrl, new File([blob], fullName))
238-
239238
logger.info('Edited image saved!', { response })
240239
showSuccess(t('viewer', 'Image saved'))
241240
if (putUrl !== this.src) {
242-
emit('files:node:created', { fileid: parseInt(response?.headers?.['oc-fileid']?.split('oc')[0]) || null })
241+
const fileId = parseInt(response?.headers?.['oc-fileid']?.split('oc')[0]) || null
242+
emit('files:node:created', source)
243+
if (fileId) {
244+
const newParams = window.OCP.Files.Router.params
245+
newParams.fileId = fileId
246+
window.OCP.Files.Router.goToRoute(null, newParams, window.OCP.Files.Router.query)
247+
}
243248
} else {
244249
this.$emit('updated')
245250
const updatedFile = await rawStat(origin, decodeURI(pathname))
@@ -260,7 +265,9 @@ export default {
260265
} catch (error) {
261266
logger.error('Error saving image', { error })
262267
showError(t('viewer', 'Error saving image'))
268+
return
263269
}
270+
this.onClose()
264271
},
265272
266273
// Key Handlers, override default Viewer arrow and escape key

src/utils/fileUtils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { FileStat } from 'webdav'
77
import { davRemoteURL, davRootPath } from '@nextcloud/files'
88
import { getLanguage } from '@nextcloud/l10n'
99
import { encodePath } from '@nextcloud/paths'
10+
import { getCurrentUser } from '@nextcloud/auth'
1011
import camelcase from 'camelcase'
1112

1213
import { isNumber } from './numberUtil'
@@ -64,6 +65,24 @@ export function extractFilePaths(path: string): [string, string] {
6465
return [dirPath, fileName]
6566
}
6667

68+
/**
69+
* Extract path from source
70+
*
71+
* @param source the full source URL
72+
* @return path
73+
*/
74+
export function extractFilePathFromSource(source: string): string {
75+
const uid = getCurrentUser()?.uid
76+
77+
if (uid) {
78+
const path = source.split(`${uid}/`)[1]
79+
if (path) {
80+
return path
81+
}
82+
}
83+
throw new Error(`Invalid source URL: ${source}. Unable to extract file paths.`)
84+
}
85+
6786
/**
6887
* Sorting comparison function
6988
*
@@ -161,5 +180,6 @@ export function getDavPath({ filename, source = '' }: { filename: string, source
161180
if (!filename.startsWith(davRootPath)) {
162181
filename = `${davRootPath}${filename}`
163182
}
183+
console.warn('getDavPath is deprecated, use getRemoteURL instead')
164184
return davRemoteURL + encodePath(filename)
165185
}

src/views/Viewer.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
:is-sidebar-shown="isSidebarShown"
149149
:loaded.sync="currentFile.loaded"
150150
class="viewer__file viewer__file--active"
151+
@update:editing="toggleEditor"
151152
@error="currentFailed" />
152153
<Error v-else
153154
:name="currentFile.basename" />
@@ -187,7 +188,7 @@ import isFullscreen from '@nextcloud/vue/dist/Mixins/isFullscreen.js'
187188
import isMobile from '@nextcloud/vue/dist/Mixins/isMobile.js'
188189
189190
import { canDownload } from '../utils/canDownload.ts'
190-
import { extractFilePaths, sortCompare } from '../utils/fileUtils.ts'
191+
import { extractFilePaths, sortCompare, extractFilePathFromSource } from '../utils/fileUtils.ts'
191192
import getSortingConfig from '../services/FileSortingConfig.ts'
192193
import cancelableRequest from '../utils/CancelableRequest.js'
193194
import Error from '../components/Error.vue'
@@ -545,6 +546,7 @@ export default defineComponent({
545546
subscribe('files:node:updated', this.handleFileUpdated)
546547
subscribe('viewer:trapElements:changed', this.handleTrapElementsChange)
547548
subscribe('editor:toggle', this.toggleEditor)
549+
subscribe('files:node:created', this.handleNewFile)
548550
window.addEventListener('keydown', this.keyboardDeleteFile)
549551
window.addEventListener('keydown', this.keyboardDownloadFile)
550552
window.addEventListener('keydown', this.keyboardEditFile)
@@ -659,6 +661,16 @@ export default defineComponent({
659661
}
660662
}
661663
},
664+
handleNewFile(source) {
665+
let path
666+
try {
667+
path = extractFilePathFromSource(source)
668+
} catch (e) {
669+
logger.error('Could not extract file path from source', { source, e })
670+
return
671+
}
672+
this.openFile(path)
673+
},
662674
663675
/**
664676
* Open the view and display the clicked file from a known file info object

0 commit comments

Comments
 (0)