Skip to content

Commit cb901d7

Browse files
committed
enh(OCA.text): accept context in CreateCollaborativeEditor
Signed-off-by: Max <max@nextcloud.com>
1 parent f38ad61 commit cb901d7

8 files changed

Lines changed: 45 additions & 33 deletions

File tree

src/components/CollaborativeEditor.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,11 @@ export default defineComponent({
188188
default: '',
189189
},
190190
191-
fileId: {
192-
type: Number,
193-
default: null,
191+
// used in provideConnection
192+
// eslint-disable-next-line vue/no-unused-properties
193+
context: {
194+
type: Object,
195+
required: true,
194196
},
195197
196198
active: {

src/components/ViewerComponent.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<template>
77
<EditorReloader
88
v-if="!useSourceView"
9-
:fileId="fileid"
9+
:context="{ id: fileid, type: 'file' }"
1010
:relativePath="filename"
1111
:active="active || isEmbedded"
1212
:autofocus

src/composables/useConnection.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import type { Document, Session } from '../services/SyncService.ts'
1010
import { inject, provide, shallowRef } from 'vue'
1111
import * as api from '../apis/connect.ts'
1212

13+
export interface Context {
14+
type: string
15+
id: number
16+
}
17+
1318
export interface Connection {
1419
documentId: number
1520
sessionId: number
@@ -41,7 +46,7 @@ export const openDataKey = Symbol('text:opendata') as InjectionKey<
4146
* Handle the connection to the text api and provide it to child components
4247
*
4348
* @param props Props of the editor component.
44-
* @param props.fileId Fileid of the file.
49+
* @param props.context of the document (i.e. type 'file' and id of the file)
4550
* @param props.relativePath Relative path to the file.
4651
* @param props.initialSession Initial session handed to the editor in direct editing
4752
* @param props.shareToken Share token of the file.
@@ -50,7 +55,7 @@ export const openDataKey = Symbol('text:opendata') as InjectionKey<
5055
*/
5156
export function provideConnection(
5257
props: {
53-
fileId: number
58+
context: Context
5459
relativePath: string
5560
initialSession?: InitialData
5661
shareToken?: string
@@ -136,13 +141,13 @@ function openInitialSession(
136141
* @param props Props of the editor component
137142
* @param props.relativePath Relative path to the file.
138143
* @param props.shareToken Share token of the file.
139-
* @param props.fileId id of the file
144+
* @param props.context of the document (i.e. type 'file' and id of the file)
140145
* @param baseVersionEtag Etag from the last editing session.
141146
* @param guestName to be shown to other participants.
142147
*/
143148
async function openShare(
144149
props: {
145-
fileId: number
150+
context: Context
146151
relativePath: string
147152
shareToken?: string
148153
},
@@ -154,7 +159,7 @@ async function openShare(
154159
guestName,
155160
token: props.shareToken,
156161
filePath: props.relativePath,
157-
fileId: props.fileId,
162+
fileId: props.context.id,
158163
baseVersionEtag,
159164
})
160165
}
@@ -164,20 +169,19 @@ async function openShare(
164169
* Get the connection and additional data from the initial session if available.
165170
*
166171
* @param props Props of the editor component
167-
* @param props.fileId id of the file
172+
* @param props.context of the document (i.e. type 'file' and id of the file)
168173
* @param props.relativePath Relative path to the file.
169174
* @param baseVersionEtag Etag from the last editing session.
170175
*/
171176
async function openFile(
172177
props: {
173-
fileId: number
178+
context: Context
174179
relativePath: string
175180
},
176181
baseVersionEtag: string | undefined,
177182
) {
178183
return api.openContext({
179-
type: 'file',
180-
id: props.fileId,
184+
...props.context,
181185
filePath: props.relativePath,
182186
baseVersionEtag,
183187
})

src/createEditor.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import type { EventHandler } from '@nextcloud/event-bus'
77
import type { App } from 'vue'
8+
import type { Context } from './composables/useConnection.ts'
89
import type { TextEditorEmbed } from './TextEditorEmbed.ts'
910

1011
import { createApp, reactive, shallowRef } from 'vue'
@@ -30,7 +31,7 @@ interface CollaborativeEditorOptions {
3031
* Element to render the editor to
3132
*/
3233
el: HTMLElement
33-
fileId: number
34+
context: Context
3435
filePath?: string
3536
shareToken?: string
3637
content?: string
@@ -53,6 +54,8 @@ interface CollaborativeEditorOptions {
5354
onAttachmentsUpdated?: (event: { attachmentSrcs: string[] }) => void
5455
}
5556

57+
type DeprecatedCollaborativeEditorOptions = Omit<CollaborativeEditorOptions, 'context'> & { fileId: number }
58+
5659
interface MarkdownContentEditorOptions {
5760
/**
5861
* Element to render the editor to
@@ -81,15 +84,15 @@ interface MarkdownContentEditorOptions {
8184
onAttachmentsUpdated?: (event: { attachmentSrcs: string[] }) => void
8285
}
8386

84-
type EditorOptions = (CollaborativeEditorOptions & { useSession?: true })
87+
type EditorOptions = (DeprecatedCollaborativeEditorOptions & { useSession?: true })
8588
| (MarkdownContentEditorOptions & { useSession?: false })
8689

8790
/**
8891
* Type guard to choose which editor to create based on the options passed to createEditor
8992
*
9093
* @param options for the editor in question
9194
*/
92-
function optionsForCollaboration(options: EditorOptions): options is CollaborativeEditorOptions {
95+
function optionsForCollaboration(options: EditorOptions): options is DeprecatedCollaborativeEditorOptions {
9396
return Boolean(options.fileId) && (options.useSession ?? true)
9497
}
9598

@@ -101,7 +104,10 @@ function optionsForCollaboration(options: EditorOptions): options is Collaborati
101104
*/
102105
export async function createEditor(options: EditorOptions) {
103106
if (optionsForCollaboration(options)) {
104-
return createCollaborativeEditor(options)
107+
return createCollaborativeEditor({
108+
context: { type: 'file', id: options.fileId },
109+
...options,
110+
})
105111
} else {
106112
return createMarkdownContentEditor(options)
107113
}
@@ -120,7 +126,7 @@ export async function createCollaborativeEditor(options: CollaborativeEditorOpti
120126
{
121127
...data,
122128
active: true,
123-
fileId: options.fileId,
129+
context: options.context,
124130
mime: 'text/markdown',
125131
autofocus: options.autofocus ?? true,
126132
noLazyImages: options.noLazyImages ?? false,

src/tests/services/SyncService.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('Sync service', () => {
4747
const setBaseVersionEtag = vi.fn()
4848
const { connection, openConnection, openData } = provideConnection(
4949
{
50-
fileId: 123,
50+
context: { type: 'file', id: 123 },
5151
relativePath: './',
5252
},
5353
getBaseVersionEtag,

src/views/CollaborativeEditorApp.vue

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<template>
77
<EditorReloader
88
ref="editor-container"
9-
:fileId
9+
:context
1010
:noLazyImages
1111
:relativePath="filePath"
1212
:shareToken
@@ -17,27 +17,20 @@
1717
@create:content="(c: { markdown: string }) => emit('create:content', c)"
1818
@update:content="(c: { markdown: string }) => emit('update:content', c)">
1919
<template v-if="readonlyBarComponent" #readonlyBar>
20-
<component :is="readonlyBarComponent" v-bind="readonlyBarProps" />
20+
<component :is="readonlyBarComponent" v-bind="readonlyBarProps || {}" />
2121
</template>
2222
</EditorReloader>
2323
</template>
2424

2525
<script setup lang='ts'>
2626
import type { ComponentInstance, ShallowRef } from 'vue'
27+
import type { Context } from '../composables/useConnection.ts'
2728
2829
import { useTemplateRef } from 'vue'
2930
import EditorReloader from '../components/EditorReloader.vue'
3031
31-
const {
32-
fileId = undefined,
33-
filePath = undefined,
34-
autofocus = false,
35-
noLazyImages = false,
36-
readonlyBarComponent = undefined,
37-
readonlyBarProps = {},
38-
shareToken = undefined,
39-
} = defineProps<{
40-
fileId?: number
32+
defineProps<{
33+
context: Context
4134
filePath?: string
4235
autofocus?: boolean
4336
noLazyImages?: boolean

src/views/DirectEditing.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<EditorReloader
99
ref="editor"
1010
:initialSession
11-
:fileId="initial.fileId"
11+
:context
1212
active
1313
autofocus
1414
:mime="initial.mimetype"
@@ -109,6 +109,13 @@ export default {
109109
&& window.webkit.messageHandlers.DirectEditingMobileInterface)
110110
)
111111
},
112+
113+
context() {
114+
return {
115+
id: this.initial.fileId,
116+
type: 'file',
117+
}
118+
},
112119
},
113120
114121
beforeMount() {

src/views/RichWorkspace.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
v-if="file"
1717
v-show="ready"
1818
:key="file.path"
19-
:fileId="file.id"
19+
:context="{id: file.id, type: 'file'}"
2020
:relativePath="file.path"
2121
:shareToken
2222
:mime="file.mimetype"

0 commit comments

Comments
 (0)