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'
@@ -17,6 +16,7 @@ import TextSvg from '@mdi/svg/svg/text.svg?raw'
1716
1817import { openMimetypes } from './mime.js'
1918import store from '../store/index.js'
19+ import Vue from 'vue'
2020
2121const FILE_ACTION_IDENTIFIER = 'Edit with text app'
2222
@@ -111,8 +111,6 @@ const registerFileActionFallback = () => {
111111
112112}
113113
114- let newWorkspaceCreated = false
115-
116114export const addMenuRichWorkspace = ( ) => {
117115 const descriptionFile = t ( 'text' , 'Readme' ) + '.' + loadState ( 'text' , 'default_file_extension' )
118116 addNewFileMenuEntry ( {
@@ -155,17 +153,17 @@ export const addMenuRichWorkspace = () => {
155153
156154 showSuccess ( t ( 'text' , 'Created "{name}"' , { name : descriptionFile } ) )
157155
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- }
156+ context . attributes [ 'rich-workspace-file' ] = fileid
157+ context . attributes [ 'rich-workspace' ] = ''
158+
163159 emit ( 'files:node:created' , file )
160+ emit ( 'files:node:updated' , context )
164161 } ,
165162 } )
166163}
167164
168- let vm = null
165+ let FilesHeaderRichWorkspaceView
166+ let FilesHeaderRichWorkspaceInstance
169167
170168export const FilesWorkspaceHeader = new Header ( {
171169 id : 'workspace' ,
@@ -174,58 +172,49 @@ export const FilesWorkspaceHeader = new Header({
174172 enabled ( _ , view ) {
175173 return [ 'files' , 'favorites' , 'public-share' ] . includes ( view . id )
176174 } ,
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
175+ render : async ( el , folder ) => {
176+ // Import the RichWorkspace component only when needed
177+ if ( ! FilesHeaderRichWorkspaceView ) {
178+ FilesHeaderRichWorkspaceView = (
179+ await import ( '../views/RichWorkspace.vue' )
180+ ) . default
183181 }
184- const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ] || ! ! newWorkspaceCreated
185- const path = newWorkspaceCreated ? dirname ( newWorkspaceCreated . path ) : folder . path
186- const content = newWorkspaceCreated ? '' : folder . attributes [ 'rich-workspace' ]
187-
188- newWorkspaceCreated = false
189-
190- const { default : RichWorkspace } = await import ( './../views/RichWorkspace.vue' )
191182
192- import ( 'vue' ) . then ( ( module ) => {
193- el . id = 'files-workspace-wrapper'
183+ // If an instance already exists, destroy it before creating a new one
184+ if ( FilesHeaderRichWorkspaceInstance ) {
185+ FilesHeaderRichWorkspaceInstance . $destroy ( )
186+ console . debug ( 'Destroying existing FilesHeaderRichWorkspaceInstance' )
187+ }
194188
195- // Todo: remove this hack
196- const Vue = module . default
197- Vue . prototype . t = window . t
198- Vue . prototype . n = window . n
199- Vue . prototype . OCA = window . OCA
189+ const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ]
190+ const content = folder . attributes [ 'rich-workspace' ] || ''
191+ const path = folder . path || ''
192+
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+ store,
202+ } ) . $mount ( el )
200203
201- const View = Vue . extend ( RichWorkspace )
202- vm = new View ( {
203- propsData : {
204- path,
205- hasRichWorkspace,
206- content,
207- } ,
208- store,
209- } ) . $mount ( el )
210- } )
204+ window . FilesHeaderRichWorkspaceInstance = FilesHeaderRichWorkspaceInstance
211205 } ,
212206
213- updated ( folder , view ) {
214- newWorkspaceCreated = false
215-
216- if ( ! vm ) {
217- console . warn ( 'No vue instance found for FilesWorkspaceHeader' )
207+ updated ( folder ) {
208+ if ( ! FilesHeaderRichWorkspaceInstance ) {
209+ console . error ( 'No vue instance found for FilesWorkspaceHeader' )
218210 return
219211 }
220212
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-
225213 const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ]
226- vm . path = folder . path
227- vm . hasRichWorkspace = hasRichWorkspace
228- vm . content = folder . attributes [ 'rich-workspace' ]
214+ FilesHeaderRichWorkspaceInstance . hasRichWorkspace = hasRichWorkspace
215+ FilesHeaderRichWorkspaceInstance . content =
216+ folder . attributes [ 'rich-workspace' ] || ''
217+ FilesHeaderRichWorkspaceInstance . path = folder . path || ''
229218 } ,
230219} )
231220
0 commit comments