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'
@@ -22,6 +21,7 @@ import axios from '@nextcloud/axios'
2221import TextSvg from '@mdi/svg/svg/text.svg?raw'
2322
2423import { openMimetypes } from './mime.js'
24+ import Vue from 'vue'
2525
2626const FILE_ACTION_IDENTIFIER = 'Edit with text app'
2727
@@ -135,8 +135,6 @@ const registerFileActionFallback = () => {
135135 }
136136}
137137
138- let newWorkspaceCreated = false
139-
140138export const addMenuRichWorkspace = ( ) => {
141139 const descriptionFile =
142140 t ( 'text' , 'Readme' ) + '.' + loadState ( 'text' , 'default_file_extension' )
@@ -183,17 +181,17 @@ export const addMenuRichWorkspace = () => {
183181
184182 showSuccess ( t ( 'text' , 'Created "{name}"' , { name : descriptionFile } ) )
185183
186- if ( contentNames . length === 0 ) {
187- // We currently have no way to reliably trigger the filelist header rendering
188- // When starting off in a new empty folder the header will only be rendered on a new PROPFIND
189- newWorkspaceCreated = file
190- }
184+ context . attributes [ 'rich-workspace-file' ] = fileid
185+ context . attributes [ 'rich-workspace' ] = ''
186+
191187 emit ( 'files:node:created' , file )
188+ emit ( 'files:node:updated' , context )
192189 } ,
193190 } )
194191}
195192
196- let vm = null
193+ let FilesHeaderRichWorkspaceView
194+ let FilesHeaderRichWorkspaceInstance
197195
198196export const FilesWorkspaceHeader = new Header ( {
199197 id : 'workspace' ,
@@ -202,64 +200,48 @@ export const FilesWorkspaceHeader = new Header({
202200 enabled ( _ , view ) {
203201 return [ 'files' , 'favorites' , 'public-share' ] . includes ( view . id )
204202 } ,
205-
206- async render ( el , folder , view ) {
207- if ( vm ) {
208- // Enforce destroying of the old rendering and rerender as the FilesListHeader calls render on every folder change
209- vm . $destroy ( )
210- vm = null
203+ render : async ( el , folder ) => {
204+ // Import the RichWorkspace component only when needed
205+ if ( ! FilesHeaderRichWorkspaceView ) {
206+ FilesHeaderRichWorkspaceView = (
207+ await import ( '../views/RichWorkspace.vue' )
208+ ) . default
211209 }
212- const hasRichWorkspace =
213- ! ! folder . attributes [ 'rich-workspace-file' ] || ! ! newWorkspaceCreated
214- const path = newWorkspaceCreated
215- ? dirname ( newWorkspaceCreated . path )
216- : folder . path
217- const content = newWorkspaceCreated
218- ? ''
219- : folder . attributes [ 'rich-workspace' ]
220-
221- newWorkspaceCreated = false
222-
223- const { default : RichWorkspace } = await import (
224- './../views/RichWorkspace.vue'
225- )
226210
227- import ( 'vue' ) . then ( ( module ) => {
228- el . id = 'files-workspace-wrapper'
229-
230- // Todo: remove this hack
231- const Vue = module . default
232- Vue . prototype . t = window . t
233- Vue . prototype . n = window . n
234- Vue . prototype . OCA = window . OCA
211+ // If an instance already exists, destroy it before creating a new one
212+ if ( FilesHeaderRichWorkspaceInstance ) {
213+ FilesHeaderRichWorkspaceInstance . $destroy ( )
214+ console . debug ( 'Destroying existing FilesHeaderRichWorkspaceInstance' )
215+ }
235216
236- const View = Vue . extend ( RichWorkspace )
237- vm = new View ( {
238- propsData : {
239- path,
240- hasRichWorkspace,
241- content,
242- } ,
243- } ) . $mount ( el )
244- } )
217+ const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ]
218+ const content = folder . attributes [ 'rich-workspace' ] || ''
219+ const path = folder . path || ''
220+
221+ // Create a new instance of the RichWorkspace component
222+ FilesHeaderRichWorkspaceInstance = new Vue ( {
223+ extends : FilesHeaderRichWorkspaceView ,
224+ propsData : {
225+ content,
226+ hasRichWorkspace,
227+ path,
228+ } ,
229+ } ) . $mount ( el )
230+
231+ window . FilesHeaderRichWorkspaceInstance = FilesHeaderRichWorkspaceInstance
245232 } ,
246233
247- updated ( folder , view ) {
248- newWorkspaceCreated = false
249-
250- if ( ! vm ) {
251- console . warn ( 'No vue instance found for FilesWorkspaceHeader' )
234+ updated ( folder ) {
235+ if ( ! FilesHeaderRichWorkspaceInstance ) {
236+ console . error ( 'No vue instance found for FilesWorkspaceHeader' )
252237 return
253238 }
254239
255- // Currently there is not much use in updating the vue instance props since render is called on every folder change
256- // removing the rendered element from the DOM
257- // This is only relevant if switching to a folder that has no content as then the render function is not called
258-
259240 const hasRichWorkspace = ! ! folder . attributes [ 'rich-workspace-file' ]
260- vm . path = folder . path
261- vm . hasRichWorkspace = hasRichWorkspace
262- vm . content = folder . attributes [ 'rich-workspace' ]
241+ FilesHeaderRichWorkspaceInstance . hasRichWorkspace = hasRichWorkspace
242+ FilesHeaderRichWorkspaceInstance . content =
243+ folder . attributes [ 'rich-workspace' ] || ''
244+ FilesHeaderRichWorkspaceInstance . path = folder . path || ''
263245 } ,
264246} )
265247
0 commit comments