Skip to content

Commit e6c3197

Browse files
Merge pull request #7418 from nextcloud/fix/workspace
fix(files): workspace implementation
2 parents 9ea5752 + cb3235b commit e6c3197

2 files changed

Lines changed: 46 additions & 63 deletions

File tree

src/helpers/files.js

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { dirname } from 'path'
76
import { emit } from '@nextcloud/event-bus'
87
import { getCurrentUser } from '@nextcloud/auth'
98
import { getSharingToken } from '@nextcloud/sharing/public'
@@ -22,6 +21,7 @@ import axios from '@nextcloud/axios'
2221
import TextSvg from '@mdi/svg/svg/text.svg?raw'
2322

2423
import { openMimetypes } from './mime.js'
24+
import Vue from 'vue'
2525

2626
const FILE_ACTION_IDENTIFIER = 'Edit with text app'
2727

@@ -135,8 +135,6 @@ const registerFileActionFallback = () => {
135135
}
136136
}
137137

138-
let newWorkspaceCreated = false
139-
140138
export 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

198196
export 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

src/views/RichWorkspace.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@
3131
</template>
3232

3333
<script>
34-
import axios from '@nextcloud/axios'
3534
import { generateOcsUrl } from '@nextcloud/router'
36-
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
3735
import { getSharingToken, isPublicShare } from '@nextcloud/sharing/public'
36+
import { loadState } from '@nextcloud/initial-state'
37+
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
38+
import axios from '@nextcloud/axios'
39+
3840
import getEditorInstance from '../components/Editor.singleton.js'
3941
import RichTextReader from '../components/RichTextReader.vue'
40-
import { loadState } from '@nextcloud/initial-state'
4142
4243
const IS_PUBLIC = isPublicShare()
4344
const WORKSPACE_URL = generateOcsUrl(
@@ -88,8 +89,8 @@ export default {
8889
ready: false,
8990
autofocus: false,
9091
hideMenu: true,
91-
darkTheme: OCA.Accessibility && OCA.Accessibility.theme === 'dark',
92-
enabled: OCA.Text.RichWorkspaceEnabled,
92+
darkTheme: window?.OCA?.Accessibility?.theme === 'dark',
93+
enabled: window?.OCA?.Text?.RichWorkspaceEnabled,
9394
}
9495
},
9596
computed: {

0 commit comments

Comments
 (0)