Skip to content

Commit 34714cd

Browse files
skjnldsvmax-nextcloud
authored andcommitted
fix(files): workspace implementation
Signed-off-by: skjnldsv <skjnldsv@protonmail.com> Signed-off-by: Max <max@nextcloud.com>
1 parent 0c52449 commit 34714cd

2 files changed

Lines changed: 46 additions & 56 deletions

File tree

src/helpers/files.js

Lines changed: 40 additions & 51 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'
@@ -17,6 +16,7 @@ import TextSvg from '@mdi/svg/svg/text.svg?raw'
1716

1817
import { openMimetypes } from './mime.js'
1918
import store from '../store/index.js'
19+
import Vue from 'vue'
2020

2121
const FILE_ACTION_IDENTIFIER = 'Edit with text app'
2222

@@ -111,8 +111,6 @@ const registerFileActionFallback = () => {
111111

112112
}
113113

114-
let newWorkspaceCreated = false
115-
116114
export 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

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

src/views/RichWorkspace.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
</template>
2727

2828
<script>
29-
import axios from '@nextcloud/axios'
3029
import { generateOcsUrl } from '@nextcloud/router'
31-
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
3230
import { getSharingToken, isPublicShare } from '@nextcloud/sharing/public'
31+
import { loadState } from '@nextcloud/initial-state'
32+
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
33+
import axios from '@nextcloud/axios'
34+
3335
import getEditorInstance from '../components/Editor.singleton.js'
3436
import RichTextReader from '../components/RichTextReader.vue'
35-
import { loadState } from '@nextcloud/initial-state'
3637
3738
const IS_PUBLIC = isPublicShare()
3839
const WORKSPACE_URL = generateOcsUrl('apps/text' + (IS_PUBLIC ? '/public' : '') + '/workspace', 2)
@@ -74,8 +75,8 @@ export default {
7475
ready: false,
7576
autofocus: false,
7677
hideMenu: true,
77-
darkTheme: OCA.Accessibility && OCA.Accessibility.theme === 'dark',
78-
enabled: OCA.Text.RichWorkspaceEnabled,
78+
darkTheme: window?.OCA?.Accessibility?.theme === 'dark',
79+
enabled: window?.OCA?.Text?.RichWorkspaceEnabled,
7980
}
8081
},
8182
computed: {

0 commit comments

Comments
 (0)