Skip to content

Commit 4fc23ad

Browse files
committed
chore(migrate): useEditorMixin to useEditor composable
Signed-off-by: Max <max@nextcloud.com>
1 parent 45ba67a commit 4fc23ad

24 files changed

+215
-182
lines changed

src/components/Assistant.vue

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<template>
66
<div v-if="showAssistant" class="text-assistant">
77
<FloatingMenu
8-
v-if="$editor"
8+
v-if="editor"
99
plugin-key="assistantMenu"
10-
:editor="$editor"
10+
:editor="editor"
1111
:tippy-options="floatingOptions()"
1212
:should-show="floatingShow"
1313
class="floating-menu"
@@ -163,11 +163,11 @@ import NcActionSeparator from '@nextcloud/vue/components/NcActionSeparator'
163163
import NcListItem from '@nextcloud/vue/components/NcListItem'
164164
import NcModal from '@nextcloud/vue/components/NcModal'
165165
import {
166-
useEditorMixin,
166+
useEditor,
167167
useIsRichWorkspaceMixin,
168168
useFileMixin,
169169
useIsPublicMixin,
170-
} from './Editor.provider.js'
170+
} from './Editor.provider.ts'
171171
import { FloatingMenu } from '@tiptap/vue-2'
172172
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
173173
import markdownit from '../markdownit/index.js'
@@ -206,12 +206,11 @@ export default {
206206
NcListItem,
207207
NcModal,
208208
},
209-
mixins: [
210-
useEditorMixin,
211-
useIsPublicMixin,
212-
useIsRichWorkspaceMixin,
213-
useFileMixin,
214-
],
209+
mixins: [useIsPublicMixin, useIsRichWorkspaceMixin, useFileMixin],
210+
setup() {
211+
const { editor } = useEditor()
212+
return { editor }
213+
},
215214
data() {
216215
return {
217216
taskTypes: OCP.InitialState.loadState('text', 'taskprocessing'),
@@ -271,7 +270,7 @@ export default {
271270
return
272271
}
273272
274-
this.$editor.on('selectionUpdate', this.onSelection)
273+
this.editor?.on('selectionUpdate', this.onSelection)
275274
this.fetchTasks()
276275
subscribe('notifications:notification:received', this.checkNotification)
277276
},
@@ -280,7 +279,7 @@ export default {
280279
return
281280
}
282281
283-
this.$editor.off('selectionUpdate', this.onSelection)
282+
this.editor?.off('selectionUpdate', this.onSelection)
284283
unsubscribe('notifications:notification:received', this.checkNotification)
285284
},
286285
methods: {
@@ -313,7 +312,10 @@ export default {
313312
await this.fetchTasks()
314313
},
315314
onSelection() {
316-
const { state } = this.$editor
315+
const { state } = this.editor ?? {}
316+
if (!state) {
317+
return
318+
}
317319
const { from, to } = state.selection
318320
this.selection = state.doc.textBetween(from, to, ' ')
319321
},
@@ -343,7 +345,7 @@ export default {
343345
},
344346
openTranslateDialog() {
345347
if (!this.selection.trim().length) {
346-
this.$editor.commands.selectAll()
348+
this.editor?.commands.selectAll()
347349
}
348350
emit('text:translate-modal:show', { content: this.selection || '' })
349351
},
@@ -367,7 +369,7 @@ export default {
367369
const content = isMarkdown
368370
? markdownit.render(task.output.output)
369371
: task.output.output
370-
this.$editor.commands.insertContent(content)
372+
this.editor?.commands.insertContent(content)
371373
this.showTaskList = false
372374
},
373375
async copyResult(task) {
@@ -402,9 +404,9 @@ export default {
402404
.querySelector('.ProseMirror')
403405
.getBoundingClientRect()
404406
const pos = posToDOMRect(
405-
this.$editor.view,
406-
this.$editor.state.selection.from,
407-
this.$editor.state.selection.to,
407+
this.editor?.view,
408+
this.editor?.state.selection.from,
409+
this.editor?.state.selection.to,
408410
)
409411
let rightSpacing = 0
410412

src/components/BaseReader.vue

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<script>
2626
import { Editor } from '@tiptap/core'
2727
import { EditorContent } from '@tiptap/vue-2'
28-
import { EDITOR } from './Editor.provider.js'
28+
import { provideEditor } from './Editor.provider.ts'
2929
import {
3030
useOutlineStateMixin,
3131
useOutlineActions,
@@ -41,18 +41,6 @@ export default {
4141
4242
mixins: [useOutlineStateMixin, useOutlineActions],
4343
44-
provide() {
45-
const val = {}
46-
47-
Object.defineProperties(val, {
48-
[EDITOR]: {
49-
get: () => this.$editor,
50-
},
51-
})
52-
53-
return val
54-
},
55-
5644
// extensions is a factory building a list of extensions for the editor
5745
inject: ['renderHtml', 'extensions'],
5846
@@ -63,6 +51,11 @@ export default {
6351
},
6452
},
6553
54+
setup() {
55+
const { editor, setEditable } = provideEditor()
56+
return { editor, setEditable }
57+
},
58+
6659
computed: {
6760
htmlContent() {
6861
return this.renderHtml(this.content)
@@ -79,12 +72,12 @@ export default {
7972
},
8073
8174
created() {
82-
this.$editor = this.createEditor()
83-
this.$editor.setEditable(false)
75+
this.editor = this.createEditor()
76+
this.setEditable(false)
8477
},
8578
8679
beforeDestroy() {
87-
this.$editor.destroy()
80+
this.editor.destroy()
8881
},
8982
9083
methods: {
@@ -96,7 +89,7 @@ export default {
9689
},
9790
9891
updateContent() {
99-
this.$editor.commands.setContent(this.htmlContent, true)
92+
this.editor.commands.setContent(this.htmlContent, true)
10093
},
10194
},
10295
}

src/components/CollisionResolveDialog.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,28 @@
3030

3131
<script>
3232
import {
33-
useEditorMixin,
33+
useEditor,
3434
useIsRichEditorMixin,
3535
useSyncServiceMixin,
36-
} from './Editor.provider.js'
36+
} from './Editor.provider.ts'
3737
import NcButton from '@nextcloud/vue/components/NcButton'
3838
import setContent from './../mixins/setContent.js'
3939
export default {
4040
name: 'CollisionResolveDialog',
4141
components: {
4242
NcButton,
4343
},
44-
mixins: [useEditorMixin, useIsRichEditorMixin, setContent, useSyncServiceMixin],
44+
mixins: [useIsRichEditorMixin, setContent, useSyncServiceMixin],
4545
props: {
4646
syncError: {
4747
type: Object,
4848
default: null,
4949
},
5050
},
51+
setup() {
52+
const { setEditable } = useEditor()
53+
return { setEditable }
54+
},
5155
data() {
5256
return {
5357
clicked: false,
@@ -57,12 +61,12 @@ export default {
5761
resolveThisVersion() {
5862
this.clicked = true
5963
this.$syncService.forceSave().then(() => this.$syncService.syncUp())
60-
this.$editor.setEditable(!this.readOnly)
64+
this.setEditable(!this.readOnly)
6165
},
6266
resolveServerVersion() {
6367
const { outsideChange } = this.syncError.data
6468
this.clicked = true
65-
this.$editor.setEditable(!this.readOnly)
69+
this.setEditable(!this.readOnly)
6670
this.setContent(outsideChange, { isRichEditor: this.$isRichEditor })
6771
this.$syncService.forceSave().then(() => this.$syncService.syncUp())
6872
},

src/components/Editor.provider.js renamed to src/components/Editor.provider.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6+
import type { Editor } from '@tiptap/core'
67
import { logger } from '../helpers/logger.js'
8+
import { provide, inject, shallowRef } from 'vue'
9+
import type { InjectionKey, ShallowRef } from 'vue'
10+
11+
export const editorKey = Symbol('tiptap:editor') as InjectionKey<
12+
ShallowRef<Editor | undefined>
13+
>
14+
export const provideEditor = () => {
15+
const editor: ShallowRef<Editor | undefined> = shallowRef(undefined)
16+
provide(editorKey, editor)
17+
const setEditable = (val: boolean) => {
18+
if (editor.value && editor.value.isEditable !== val) {
19+
editor.value.setEditable(val)
20+
}
21+
}
22+
return { editor, setEditable }
23+
}
24+
export const useEditor = () => ({
25+
editor: inject(editorKey, shallowRef(undefined)),
26+
})
727

8-
export const EDITOR = Symbol('tiptap:editor')
928
export const FILE = Symbol('editor:file')
1029
export const ATTACHMENT_RESOLVER = Symbol('attachment:resolver')
1130
export const IS_MOBILE = Symbol('editor:is-mobile')
@@ -17,12 +36,6 @@ export const EDITOR_UPLOAD = Symbol('editor:upload')
1736
export const HOOK_MENTION_SEARCH = Symbol('hook:mention-search')
1837
export const HOOK_MENTION_INSERT = Symbol('hook:mention-insert')
1938

20-
export const useEditorMixin = {
21-
inject: {
22-
$editor: { from: EDITOR, default: null },
23-
},
24-
}
25-
2639
export const useSyncServiceMixin = {
2740
inject: {
2841
$syncService: { from: SYNC_SERVICE, default: null },
@@ -71,7 +84,7 @@ export const useAttachmentResolver = {
7184
$attachmentResolver: {
7285
from: ATTACHMENT_RESOLVER,
7386
default: {
74-
resolve(src) {
87+
resolve(src: string) {
7588
logger.warn(
7689
'No attachment resolver provided. Some attachment sources cannot be resolved.',
7790
)

0 commit comments

Comments
 (0)