Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EditorWrapper.vue « components « src - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ec1d0cb1a29100f3ce30fc1f2e3422989c9371b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
<!--
  - @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  -
  - @author Julius Härtl <jus@bitgrid.net>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
  -
  -->

<template>
	<div id="editor-container" data-text-el="editor-container" class="text-editor">
		<div v-if="displayed" class="document-status">
			<p v-if="idle" class="msg">
				{{ t('text', 'Document idle for {timeout} minutes, click to continue editing', { timeout: IDLE_TIMEOUT }) }} <a class="button primary" @click="reconnect">{{ t('text', 'Reconnect') }}</a>
			</p>
			<p v-else-if="hasSyncCollission" class="msg icon-error">
				{{ t('text', 'The document has been changed outside of the editor. The changes cannot be applied.') }}
			</p>
			<p v-else-if="hasConnectionIssue" class="msg">
				{{ t('text', 'File could not be loaded. Please check your internet connection.') }} <a class="button primary" @click="reconnect">{{ t('text', 'Reconnect') }}</a>
			</p>
			<p v-if="lock" class="msg msg-locked">
				<Lock /> {{ t('text', 'This file is opened read-only as it is currently locked by {user}.', { user: lock.displayName }) }}
			</p>
		</div>
		<div v-if="displayed"
			id="editor-wrapper"
			class="text-editor__wrapper"
			:class="{
				'has-conflicts': hasSyncCollission,
				'icon-loading': !contentLoaded && !hasConnectionIssue,
				'is-rich-workspace': isRichWorkspace,
				'is-rich-editor': isRichEditor,
				'show-color-annotations': showAuthorAnnotations
			}">
			<EditorMidiaHandler v-if="$editor"
				id="editor"
				class="text-editor__main">
				<MenuBar v-if="renderMenus"
					ref="menubar"
					:autohide="autohide"
					:loaded.sync="menubarLoaded">
					<div class="text-editor__session-list">
						<div v-if="isMobile" v-tooltip="lastSavedStatusTooltip" :class="saveStatusClass" />
						<div v-else
							v-tooltip="lastSavedStatusTooltip"
							class="save-status"
							:class="lastSavedStatusClass">
							{{ lastSavedStatus }}
						</div>
						<SessionList :sessions="filteredSessions">
							<p slot="lastSaved" class="last-saved">
								{{ t('text', 'Last saved') }}: {{ lastSavedString }}
							</p>
							<GuestNameDialog v-if="isPublic && !currentSession.userId" :session="currentSession" />
						</SessionList>
					</div>
					<slot name="header" />
				</MenuBar>
				<div v-if="!menubarLoaded" class="menubar-placeholder" />
				<div ref="contentWrapper"
					data-text-el="editor-content-wrapper"
					class="content-wrapper text-editor__content-wrapper">
					<MenuBubble v-if="renderMenus"
						:content-wrapper="contentWrapper"
						:file-path="relativePath" />
					<EditorContent v-show="contentLoaded"
						class="editor__content text-editor__content"
						:editor="$editor" />
				</div>
			</EditorMidiaHandler>
			<Reader v-if="hasSyncCollission"
				:content="syncError.data.outsideChange"
				:is-rich-editor="isRichEditor" />
		</div>

		<CollisionResolveDialog v-if="hasSyncCollission && !readOnly"
			@resolve-use-this-version="resolveUseThisVersion"
			@resolve-use-server-version="resolveUseServerVersion" />
	</div>
</template>

<script>
import Vue from 'vue'
import escapeHtml from 'escape-html'
import moment from '@nextcloud/moment'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
import { EditorContent } from '@tiptap/vue-2'
import { getVersion, receiveTransaction } from 'prosemirror-collab'
import { Step } from 'prosemirror-transform'
import { getCurrentUser } from '@nextcloud/auth'

import {
	EDITOR,
	FILE,
	IMAGE_RESOLVER,
	IS_MOBILE,
	IS_PUBLIC,
	IS_RICH_EDITOR,
	IS_RICH_WORKSPACE,
	SYNC_SERVICE,
} from './EditorWrapper.provider.js'

import { SyncService, ERROR_TYPE, IDLE_TIMEOUT } from './../services/SyncService.js'
import ImageResolver from './../services/ImageResolver.js'
import { extensionHighlight } from '../helpers/mappings.js'
import { createEditor, serializePlainText, loadSyntaxHighlight } from './../EditorFactory.js'
import { createMarkdownSerializer } from './../extensions/Markdown.js'
import markdownit from './../markdownit/index.js'

import { Collaboration, Keymap, UserColor } from './../extensions/index.js'
import isMobile from './../mixins/isMobile.js'
import store from './../mixins/store.js'
import Lock from 'vue-material-design-icons/Lock'
import MenuBar from './Menu/MenuBar.vue'
import EditorMidiaHandler from './EditorMediaHandler.vue'

const EDITOR_PUSH_DEBOUNCE = 200

export default {
	name: 'EditorWrapper',
	components: {
		EditorContent,
		EditorMidiaHandler,
		MenuBar,
		MenuBubble: () => import(/* webpackChunkName: "editor-rich" */'./MenuBubble.vue'),
		Reader: () => import(/* webpackChunkName: "editor" */'./Reader.vue'),
		CollisionResolveDialog: () => import(/* webpackChunkName: "editor" */'./CollisionResolveDialog.vue'),
		GuestNameDialog: () => import(/* webpackChunkName: "editor-guest" */'./GuestNameDialog.vue'),
		SessionList: () => import(/* webpackChunkName: "editor-collab" */'./SessionList.vue'),
		Lock,
	},
	directives: {
		Tooltip,
	},
	mixins: [
		isMobile,
		store,
	],
	provide() {
		const val = {}

		// providers aren't naturally reactive
		// and $editor will start as null
		// using getters we can always provide the
		// actual $editor, and other values without being reactive
		Object.defineProperties(val, {
			[EDITOR]: {
				get: () => this.$editor,
			},
			[SYNC_SERVICE]: {
				get: () => this.$syncService,
			},
			[FILE]: {
				get: () => this.fileData,
			},
			[IMAGE_RESOLVER]: {
				get: () => this.$imageResolver,
			},
			[IS_PUBLIC]: {
				get: () => this.isPublic,
			},
			[IS_RICH_EDITOR]: {
				get: () => this.isRichEditor,
			},
			[IS_RICH_WORKSPACE]: {
				get: () => this.isRichWorkspace,
			},
			[IS_MOBILE]: {
				get: () => this.isMobile,
			},
		})

		return val
	},
	props: {
		richWorkspace: {
			type: Boolean,
			require: false,
			default: false,
		},
		initialSession: {
			type: Object,
			default: null,
		},
		relativePath: {
			type: String,
			default: '',
		},
		fileId: {
			type: Number,
			default: null,
		},
		active: {
			type: Boolean,
			default: false,
		},
		autofocus: {
			type: Boolean,
			default: true,
		},
		shareToken: {
			type: String,
			default: null,
		},
		mime: {
			type: String,
			default: null,
		},
		autohide: {
			type: Boolean,
			default: false,
		},
		isDirectEditing: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			IDLE_TIMEOUT,

			document: null,
			sessions: [],
			currentSession: null,

			filteredSessions: {},

			idle: false,
			dirty: false,
			contentLoaded: false,
			lastSavedString: '',
			syncError: null,
			hasConnectionIssue: false,
			readOnly: true,
			forceRecreate: false,
			menubarLoaded: false,
			draggedOver: false,

			saveStatusPolling: null,
			contentWrapper: null,
		}
	},
	computed: {
		isRichWorkspace() {
			return this.richWorkspace
		},
		showAuthorAnnotations() {
			return this.$store.state.showAuthorAnnotations
		},
		lastSavedStatus() {
			if (this.hasConnectionIssue) {
				return t('text', this.isMobile ? 'Offline' : 'Offline, changes will be saved when online')
			}
			return this.dirtyStateIndicator ? t('text', 'Saving …') : t('text', 'Saved')
		},
		lastSavedStatusClass() {
			return this.syncError && this.lastSavedString !== '' ? 'error' : ''
		},
		dirtyStateIndicator() {
			return this.hasUnpushedChanges || this.hasUnsavedChanges
		},
		lastSavedStatusTooltip() {
			let message = t('text', 'Last saved {lastSaved}', { lastSaved: this.lastSavedString })
			if (this.hasSyncCollission) {
				message = t('text', 'The document has been changed outside of the editor. The changes cannot be applied.')
			}
			if (this.hasUnpushedChanges || this.hasUnsavedChanges) {
				message += ' - ' + t('text', 'Unsaved changes')
			}
			return { content: message, placement: 'bottom' }
		},
		hasSyncCollission() {
			return this.syncError && this.syncError.type === ERROR_TYPE.SAVE_COLLISSION
		},
		hasUnpushedChanges() {
			return this.dirty
		},
		hasUnsavedChanges() {
			return this.document && this.document.lastSavedVersion < this.document.currentVersion
		},
		hasDocumentParameters() {
			return this.fileId || this.shareToken || this.initialSession
		},
		isPublic() {
			return this.isDirectEditing || (document.getElementById('isPublic') && document.getElementById('isPublic').value === '1')
		},
		isRichEditor() {
			return this.mime === 'text/markdown'
		},
		fileExtension() {
			return this.relativePath ? this.relativePath.split('/').pop().split('.').pop() : 'txt'
		},
		currentDirectory() {
			return this.relativePath
				? this.relativePath.split('/').slice(0, -1).join('/')
				: '/'
		},
		displayed() {
			return this.currentSession && this.active
		},
		renderMenus() {
			return this.contentLoaded
				&& this.isRichEditor
				&& !this.syncError
				&& !this.readOnly
		},
		imagePath() {
			return this.relativePath.split('/').slice(0, -1).join('/')
		},
		fileData() {
			return {
				fileId: this.fileId,
				relativePath: this.relativePath,
				document: {
					...this.document,
				},
			}
		},
		saveStatusClass() {
			if (this.syncError && this.lastSavedString !== '') {
				return 'save-error'
			}
			return this.dirtyStateIndicator ? 'saving-status' : 'saved-status'
		},
	},
	watch: {
		displayed() {
			this.$nextTick(() => {
				this.contentWrapper = this.$refs.contentWrapper
			})
		},
	},
	mounted() {
		if (this.active && (this.hasDocumentParameters)) {
			this.initSession()
		}
		this.$parent.$emit('update:loaded', true)
	},
	created() {
		this.$editor = null
		this.$syncService = null
		this.$imageResolver = null
		this.saveStatusPolling = setInterval(() => {
			this.updateLastSavedStatus()
		}, 2000)
	},
	beforeDestroy() {
		this.close()
	},
	methods: {
		updateLastSavedStatus() {
			if (this.document) {
				this.lastSavedString = moment(this.document.lastSavedVersionTime * 1000).fromNow()
			}
		},

		initSession() {
			if (!this.hasDocumentParameters) {
				this.$parent.$emit('error', 'No valid file provided')
				return
			}
			const guestName = localStorage.getItem('nick') ? localStorage.getItem('nick') : ''

			this.$syncService = new SyncService({
				guestName,
				shareToken: this.shareToken,
				filePath: this.relativePath,
				forceRecreate: this.forceRecreate,
				serialize: (document) => {
					if (this.isRichEditor) {
						return (createMarkdownSerializer(this.$editor.schema)).serialize(document)
					}
					return serializePlainText(this.$editor)

				},
			})

			this.listenSyncServiceEvents()

			this.$syncService.open({
				fileId: this.fileId,
				filePath: this.relativePath,
				initialSession: this.initialSession,
			}).catch((e) => {
				this.hasConnectionIssue = true
			})
			this.forceRecreate = false
		},

		listenSyncServiceEvents() {
			this.$syncService
				.on('opened', this.onOpened)
				.on('change', this.onChange)
				.on('loaded', this.onLoaded)
				.on('sync', this.onSync)
				.on('error', this.onError)
				.on('stateChange', this.onStateChange)
				.on('idle', this.onIdle)
				.on('save', this.onSave)
		},

		unlistenSyncServiceEvents() {
			this.$syncService
				.off('opened', this.onOpened)
				.off('change', this.onChange)
				.off('loaded', this.onLoaded)
				.off('sync', this.onSync)
				.off('error', this.onError)
				.off('stateChange', this.onStateChange)
				.off('idle', this.onIdle)
				.off('save', this.onSave)
		},

		resolveUseThisVersion() {
			this.$syncService.forceSave()
			this.$editor.setOptions({ editable: !this.readOnly })
		},

		resolveUseServerVersion() {
			this.forceRecreate = true
			this.reconnect()
		},

		reconnect() {
			this.contentLoaded = false
			this.hasConnectionIssue = false

			const connect = () => {
				this.unlistenSyncServiceEvents()
				this.$syncService = null
				this.$editor.destroy()
				this.initSession()
			}

			if (this.$syncService) {
				this.$syncService
					.close()
					.then(connect)
					.catch((e) => {
					// Ignore issues closing the session since those might happen due to network issues
					})
			} else {
				connect()
			}

			this.idle = false
		},

		updateSessions(sessions) {
			this.sessions = sessions.sort((a, b) => b.lastContact - a.lastContact)

			// Make sure we get our own session updated
			// This should ideally be part of a global store where we can have that updated on the actual name change for guests
			const currentUpdatedSession = this.sessions.find(session => session.id === this.currentSession.id)
			Vue.set(this, 'currentSession', currentUpdatedSession)

			const currentSessionIds = this.sessions.map((session) => session.userId)
			const currentGuestIds = this.sessions.map((session) => session.guestId)

			const removedSessions = Object.keys(this.filteredSessions)
				.filter(sessionId => !currentSessionIds.includes(sessionId) && !currentGuestIds.includes(sessionId))

			for (const index in removedSessions) {
				Vue.delete(this.filteredSessions, removedSessions[index])
			}
			for (const index in this.sessions) {
				const session = this.sessions[index]
				const sessionKey = session.displayName ? session.userId : session.id
				if (this.filteredSessions[sessionKey]) {
					// update timestamp if relevant
					if (this.filteredSessions[sessionKey].lastContact < session.lastContact) {
						Vue.set(this.filteredSessions[sessionKey], 'lastContact', session.lastContact)
					}
				} else {
					Vue.set(this.filteredSessions, sessionKey, session)
				}
				if (session.id === this.currentSession.id) {
					Vue.set(this.filteredSessions[sessionKey], 'isCurrent', true)
				}
			}
		},

		onOpened({ document, session }) {
			this.currentSession = session
			this.document = document
			this.readOnly = document.readOnly
			this.lock = this.$syncService.lock
			localStorage.setItem('nick', this.currentSession.guestName)
			this.$store.dispatch('setCurrentSession', this.currentSession)
			this.$imageResolver = new ImageResolver({
				session: this.currentSession,
				user: getCurrentUser(),
				shareToken: this.shareToken,
				currentDirectory: this.currentDirectory,
			})
		},

		onLoaded({ documentSource }) {
			this.hasConnectionIssue = false
			const content = this.isRichEditor
				? markdownit.render(documentSource)
				: '<pre>' + escapeHtml(documentSource) + '</pre>'
			const language = extensionHighlight[this.fileExtension] || this.fileExtension

			loadSyntaxHighlight(language)
				.then(() => {
					this.$editor = createEditor({
						content,
						onCreate: ({ editor }) => {
							this.$syncService.state = editor.state
							this.$syncService.startSync()
						},
						onUpdate: ({ editor }) => {
							this.$syncService.state = editor.state
						},
						extensions: [
							Collaboration.configure({
								// the initial version we start with
								// version is an integer which is incremented with every change
								version: this.document.initialVersion,
								clientID: this.currentSession.id,
								// debounce changes so we can save some bandwidth
								debounce: EDITOR_PUSH_DEBOUNCE,
								onSendable: ({ sendable }) => {
									if (this.$syncService) {
										this.$syncService.sendSteps()
									}
								},
								update: ({ steps, version, editor }) => {
									const { state, view, schema } = editor
									if (getVersion(state) > version) {
										return
									}
									const tr = receiveTransaction(
										state,
										steps.map(item => Step.fromJSON(schema, item.step)),
										steps.map(item => item.clientID),
									)
									tr.setMeta('clientID', steps.map(item => item.clientID))
									view.dispatch(tr)
								},
							}),
							Keymap.configure({
								'Mod-s': () => {
									this.$syncService.save()
									return true
								},
							}),
							UserColor.configure({
								clientID: this.currentSession.id,
								color: (clientID) => {
									const session = this.sessions.find(item => '' + item.id === '' + clientID)
									return session?.color
								},
								name: (clientID) => {
									const session = this.sessions.find(item => '' + item.id === '' + clientID)
									return session?.userId ? session.userId : session?.guestName
								},
							}),
						],
						enableRichEditing: this.isRichEditor,
					})
					this.$editor.on('focus', () => {
						this.$emit('focus')
					})
					this.$editor.on('blur', () => {
						this.$emit('blur')
					})
					this.$syncService.state = this.$editor.state
				})

		},

		onChange({ document, sessions }) {
			if (this.document.baseVersionEtag !== '' && document.baseVersionEtag !== this.document.baseVersionEtag) {
				this.resolveUseServerVersion()
				return
			}
			this.updateSessions.bind(this)(sessions)
			this.document = document

			this.syncError = null
			this.$editor.setOptions({ editable: !this.readOnly })
		},

		onSync({ steps, document }) {
			this.hasConnectionIssue = false
			try {
				const collaboration = this.$editor.extensionManager.extensions.find(e => e.name === 'collaboration')
				collaboration.options.update({
					version: document.currentVersion,
					steps,
					editor: this.$editor,
				})
				this.$syncService.state = this.$editor.state
				this.updateLastSavedStatus()
				this.$nextTick(() => {
					this.$emit('sync-service:sync')
				})
			} catch (e) {
				console.error('Failed to update steps in collaboration plugin', e)
				// TODO: we should recreate the editing session when this happens
			}
			this.document = document
		},

		onError({ type, data }) {
			this.$editor.setOptions({ editable: false })

			this.$nextTick(() => {
				this.$emit('sync-service:error')
			})

			if (type === ERROR_TYPE.SAVE_COLLISSION && (!this.syncError || this.syncError.type !== ERROR_TYPE.SAVE_COLLISSION)) {
				this.contentLoaded = true
				this.syncError = {
					type,
					data,
				}
			}
			if (type === ERROR_TYPE.CONNECTION_FAILED && !this.hasConnectionIssue) {
				this.hasConnectionIssue = true
				// FIXME: ideally we just try to reconnect in the service, so we don't loose steps
				OC.Notification.showTemporary('Connection failed, reconnecting')
				if (data.retry !== false) {
					setTimeout(this.reconnect.bind(this), 5000)
				}
			}
			if (type === ERROR_TYPE.SOURCE_NOT_FOUND) {
				this.hasConnectionIssue = true
			}
			this.$emit('ready')
		},

		onStateChange(state) {
			if (state.initialLoading && !this.contentLoaded) {
				this.contentLoaded = true
				if (this.autofocus && !this.readOnly) {
					this.$editor.commands.focus()
				}
				this.$emit('ready')
				// TODO: remove $parent access
				this.$parent.$emit('ready', true)
			}
			if (Object.prototype.hasOwnProperty.call(state, 'dirty')) {
				this.dirty = state.dirty
			}
		},

		onIdle() {
			this.$syncService.close()
			this.idle = true
			this.readOnly = true
			this.$editor.setOptions({ editable: !this.readOnly })

			this.$nextTick(() => {
				this.$emit('sync-service:idle')
			})
		},

		onSave() {
			this.$nextTick(() => {
				this.$emit('sync-service:save')
			})
		},

		async close() {
			clearInterval(this.saveStatusPolling)
			if (this.currentSession && this.$syncService) {
				try {
					await this.$syncService.close()
					this.unlistenSyncServiceEvents()
					this.currentSession = null
					this.$syncService = null
				} catch (e) {
					// Ignore issues closing the session since those might happen due to network issues
				}
			}
			return true
		},
	},
}
</script>

<style scoped lang="scss">
	.modal-container .text-editor {
		top: 0;
		height: calc(100vh - var(--header-height));
	}

	.text-editor {
		display: block;
		width: 100%;
		max-width: 100%;
		height: 100%;
		left: 0;
		margin: 0 auto;
		position: relative;
		background-color: var(--color-main-background);
	}

	.text-editor__wrapper {
		display: flex;
		width: 100%;
		height: 100%;
		overflow: hidden;
		position: absolute;

		&.show-color-annotations::v-deep .author-annotation {
			padding-top: 2px;
			padding-bottom: 2px;
		}

		&:not(.show-color-annotations)::v-deep .author-annotation,
		&:not(.show-color-annotations)::v-deep .image {
			background-color: transparent !important;
		}

		.ProseMirror {
			margin-top: 0 !important;
		}
		&.icon-loading {
			.text-editor__main {
				opacity: 0.3;
			}
		}
	}

	.text-editor__main, .editor {
		background: var(--color-main-background);
		color: var(--color-main-text);
		background-clip: padding-box;
		border-radius: var(--border-radius);
		padding: 0;
		position: relative;
		overflow-y: auto;
		overflow-x: hidden;
		width: 100%;
	}

	.document-status {
		position: relative;
		background-color: var(--color-main-background);

		.msg {
			padding: 12px;
			background-position: 8px center;
			color: var(--color-text-maxcontrast);

			&.icon-error {
				padding-left: 30px;
			}

			.button {
				margin-left: 8px;
			}

			&.msg-locked .lock-icon {
				padding: 0 10px;
				float: left;
			}
		}
	}

	.save-status {
		display: inline-flex;
		padding: 0;
		text-overflow: ellipsis;
		color: var(--color-text-lighter);
		position: relative;
		top: 9px;
		min-width: 85px;
		max-height: 36px;

		&.error {
			background-color: var(--color-error);
			color: var(--color-main-background);
			border-radius: 3px;
		}
	}

	.text-editor .text-editor__wrapper.has-conflicts {
		height: calc(100% - 50px);

		.text-editor__main, #read-only-editor {
			width: 50%;
			height: 100%;
		}
	}

	.text-editor__session-list {
		display: flex;

		input, div {
			vertical-align: middle;
			margin-left: 3px;
		}
	}

	.editor__content {
		max-width: var(--text-editor-max-width);
		margin: auto;
		position: relative;
	}

	#body-public {
		height: auto;
	}

	#files-public-content {
		.text-editor {
			top: 0;
			width: 100%;

			.text-editor__main {
				overflow: auto;
				z-index: 20;
			}
			.has-conflicts .text-editor__main {
				padding-top: 0;
			}
		}
	}

	.ie {
		.editor__content::v-deep .ProseMirror {
			padding-top: 50px;
		}
	}

	.menubar-placeholder {
		position: fixed;
		position: -webkit-sticky;
		position: sticky;
		top: 0;
		opacity: 0;
		visibility: hidden;
		height: 44px; // important for mobile so that the buttons are always inside the container
		padding-top:3px;
		padding-bottom: 3px;
	}

</style>

<style lang="scss">
	@import './../../css/style';

	.text-editor__wrapper {
		@import './../../css/prosemirror';

		&:not(.is-rich-editor) .ProseMirror {
			pre {
				background-color: var(--color-main-background);

				&::before {
					content: attr(data-language);
					text-transform: uppercase;
					display: block;
					text-align: right;
					font-weight: bold;
					font-size: 0.6rem;
				}
				code {
					.hljs-comment,
					.hljs-quote {
						color: #999999;
					}
					.hljs-variable,
					.hljs-template-variable,
					.hljs-attribute,
					.hljs-tag,
					.hljs-name,
					.hljs-regexp,
					.hljs-link,
					.hljs-selector-id,
					.hljs-selector-class {
						color: #f2777a;
					}
					.hljs-number,
					.hljs-meta,
					.hljs-built_in,
					.hljs-builtin-name,
					.hljs-literal,
					.hljs-type,
					.hljs-params {
						color: #f99157;
					}
					.hljs-string,
					.hljs-symbol,
					.hljs-bullet {
						color: #99cc99;
					}
					.hljs-title,
					.hljs-section {
						color: #ffcc66;
					}
					.hljs-keyword,
					.hljs-selector-tag {
						color: #6699cc;
					}
					.hljs-emphasis {
						font-style: italic;
					}
					.hljs-strong {
						font-weight: 700;
					}
				}
			}
		}

		// relative position for the alignment of the menububble
		.text-editor__main {
			&.draggedOver {
				background-color: var(--color-primary-light);
			}
			.text-editor__content-wrapper {
				position: relative;
			}
		}
	}

	// Required in order to make the public pages behave the same if talk is enabled or not
	// as Talk overwrites the public page styles and changes the DOM layout for the sidebar injection
	#files-public-content {
		height: 100%;
	}

	.saved-status,.saving-status {
		display: inline-flex;
		padding: 0;
		text-overflow: ellipsis;
		color: var(--color-text-lighter);
		position: relative;
		background-color: white;
		width: 38px !important;
		height: 38px !important;
		left: 25%;
		z-index: 2;
		top: 0px;
	}

	.saved-status {
		border: 2px solid #04AA6D;
		border-radius: 50%;
	}

	.saving-status {
		border: 2px solid #f3f3f3;
		border-top: 2px solid #3498db;
		border-radius: 50%;
		animation: spin 2s linear infinite;
	}

	@keyframes spin {
		0% { transform: rotate(0deg); }
		100% { transform: rotate(360deg); }
	}

	.last-saved {
		padding: 6px;
	}
</style>