33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
55
6- import { dirname } from 'path'
76import { emit } from '@nextcloud/event-bus'
87import { getCurrentUser } from '@nextcloud/auth'
98import { getSharingToken } from '@nextcloud/sharing/public'
@@ -111,8 +110,6 @@ const registerFileActionFallback = () => {
111110
112111}
113112
114- let newWorkspaceCreated = false
115-
116113export const addMenuRichWorkspace = ( ) => {
117114 const descriptionFile = t ( 'text' , 'Readme' ) + '.' + loadState ( 'text' , 'default_file_extension' )
118115 addNewFileMenuEntry ( {
@@ -155,17 +152,17 @@ export const addMenuRichWorkspace = () => {
155152
156153 showSuccess ( t ( 'text' , 'Created "{name}"' , { name : descriptionFile } ) )
157154
158- if ( contentNames . length === 0 ) {
159- // We currently have no way to reliably trigger the filelist header rendering
160- // When starting off in a new empty folder the header will only be rendered on a new PROPFIND
161- newWorkspaceCreated = file
162- }
155+ context . attributes [ 'rich-workspace-file' ] = fileid
156+ context . attributes [ 'rich-workspace' ] = ''
157+
163158 emit ( 'files:node:created' , file )
159+ emit ( 'files:node:updated' , context )
164160 } ,
165161 } )
166162}
167163
168- let vm = null
164+ let FilesHeaderRichWorkspaceView
165+ let FilesHeaderRichWorkspaceInstance
169166
170167export const FilesWorkspaceHeader = new Header ( {
171168 id : 'workspace' ,
@@ -174,23 +171,34 @@ export const FilesWorkspaceHeader = new Header({
174171 enabled ( _ , view ) {
175172 return [ 'files' , 'favorites' , 'public-share' ] . includes ( view . id )
176173 } ,
177-
178- async render ( el , folder , view ) {
179- if ( vm ) {
180- // Enforce destroying of the old rendering and rerender as the FilesListHeader calls render on every folder change
181- vm . $destroy ( )
182- vm = null
174+ render : async ( el , folder ) => {
175+ // Import the RichWorkspace component only when needed
176+ if ( ! FilesHeaderRichWorkspaceView ) {
177+ FilesHeaderRichWorkspaceView = (
178+ await import ( '../views/RichWorkspace.vue' )
179+ ) . default
183180 }
184181 const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ] || ! ! newWorkspaceCreated
185182 const path = newWorkspaceCreated ? dirname ( newWorkspaceCreated . path ) : folder . path
186183 const content = newWorkspaceCreated ? '' : folder . attributes [ 'rich-workspace' ]
187184
188- newWorkspaceCreated = false
185+ // If an instance already exists, destroy it before creating a new one
186+ if ( FilesHeaderRichWorkspaceInstance ) {
187+ FilesHeaderRichWorkspaceInstance . $destroy ( )
188+ console . debug ( 'Destroying existing FilesHeaderRichWorkspaceInstance' )
189+ }
189190
190191 const { default : RichWorkspace } = await import ( './../views/RichWorkspace.vue' )
191192
192- import ( 'vue' ) . then ( ( module ) => {
193- el . id = 'files-workspace-wrapper'
193+ // Create a new instance of the RichWorkspace component
194+ FilesHeaderRichWorkspaceInstance = new Vue ( {
195+ extends : FilesHeaderRichWorkspaceView ,
196+ propsData : {
197+ content,
198+ hasRichWorkspace,
199+ path,
200+ } ,
201+ } ) . $mount ( el )
194202
195203 // Todo: remove this hack
196204 const Vue = module . default
@@ -210,22 +218,17 @@ export const FilesWorkspaceHeader = new Header({
210218 } )
211219 } ,
212220
213- updated ( folder , view ) {
214- newWorkspaceCreated = false
215-
216- if ( ! vm ) {
217- console . warn ( 'No vue instance found for FilesWorkspaceHeader' )
221+ updated ( folder ) {
222+ if ( ! FilesHeaderRichWorkspaceInstance ) {
223+ console . error ( 'No vue instance found for FilesWorkspaceHeader' )
218224 return
219225 }
220226
221- // Currently there is not much use in updating the vue instance props since render is called on every folder change
222- // removing the rendered element from the DOM
223- // This is only relevant if switching to a folder that has no content as then the render function is not called
224-
225227 const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ]
226- vm . path = folder . path
227- vm . hasRichWorkspace = hasRichWorkspace
228- vm . content = folder . attributes [ 'rich-workspace' ]
228+ FilesHeaderRichWorkspaceInstance . hasRichWorkspace = hasRichWorkspace
229+ FilesHeaderRichWorkspaceInstance . content =
230+ folder . attributes [ 'rich-workspace' ] || ''
231+ FilesHeaderRichWorkspaceInstance . path = folder . path || ''
229232 } ,
230233} )
231234
0 commit comments