Skip to content

Commit 03622ba

Browse files
max-nextcloudbackportbot[bot]
authored andcommitted
Fix(debug): remove component in beforeDestroy hook
* Use a set so components can only be in there once. * Debug data can be written even if syncService is undefined. Signed-off-by: Max <[email protected]>
1 parent eb094a2 commit 03622ba

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

src/components/Editor.vue

+6-34
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ import { extensionHighlight } from '../helpers/mappings.js'
101101
import { createEditor, serializePlainText, loadSyntaxHighlight } from './../EditorFactory.js'
102102
import { createMarkdownSerializer } from './../extensions/Markdown.js'
103103
import markdownit from './../markdownit/index.js'
104-
104+
import { exposeForDebugging, removeFromDebugging } from '../helpers/debug.js'
105105
import { CollaborationCursor } from '../extensions/index.js'
106106
import DocumentStatus from './Editor/DocumentStatus.vue'
107107
import isMobile from './../mixins/isMobile.js'
@@ -338,7 +338,7 @@ export default {
338338
subscribe('text:image-node:delete', this.onDeleteImageNode)
339339
this.emit('update:loaded', true)
340340
subscribe('text:translate-modal:show', this.showTranslateModal)
341-
this.setupEditorDebug()
341+
exposeForDebugging(this)
342342
},
343343
created() {
344344
this.$ydoc = new Doc()
@@ -365,7 +365,8 @@ export default {
365365
const timeout = new Promise((resolve) => setTimeout(resolve, 2000))
366366
await Promise.any([timeout, this.$syncService.save()])
367367
}
368-
this.close()
368+
await this.close()
369+
removeFromDebugging(this)
369370
},
370371
methods: {
371372
initSession() {
@@ -750,46 +751,17 @@ export default {
750751
console.debug(editor.getHTML())
751752
},
752753

753-
/**
754-
* Setup OCA.Text.debugYjs() and expose editor component in OCA.Text.editorComponents
755-
*/
756-
setupEditorDebug() {
757-
if (!window.OCA.Text) {
758-
window.OCA.Text = {}
759-
}
760-
if (!window.OCA.Text.editorComponents) {
761-
window.OCA.Text.editorComponents = []
762-
}
763-
window.OCA.Text.editorComponents.push(this)
764-
765-
if (!window.OCA.Text.debugYjs) {
766-
window.OCA.Text.debugYjs = () => {
767-
const intro = 'Editor Yjs debug data. Copy the objects above that start with "fileId".'
768-
const introChrome = '- In Chrome, select "Copy" at the end of the line.'
769-
const introFirefox = '- In Firefox, right-click on the object and select "Copy object".'
770-
const styleBold = 'font-weight: bold;'
771-
const styleItalic = 'font-weight: normal; font-style: italic;'
772-
773-
for (const editorComponent of window.OCA.Text.editorComponents) {
774-
console.warn(JSON.stringify(editorComponent.debugYjsData(), null, ' '))
775-
}
776-
777-
console.warn('%c%s\n%c%s\n%s', styleBold, intro, styleItalic, introChrome, introFirefox)
778-
}
779-
}
780-
},
781-
782754
/**
783755
* Helper method to debug yjs issues
784756
*/
785-
debugYjsData() {
757+
debugData() {
786758
const yjsData = {
787759
fileId: this.fileId,
788760
filePath: this.relativePath,
789761
clientId: this.$ydoc.clientID,
790762
pendingStructs: this.$ydoc.store.pendingStructs,
791763
clientVectors: [],
792-
documentState: this.$syncService.getDocumentState(),
764+
documentState: this.$syncService?.getDocumentState(),
793765
}
794766
for (const client of this.$ydoc.store.clients.values()) {
795767
yjsData.clientVectors.push(client.at(-1).id)

src/helpers/debug.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
/**
7+
* Setup OCA.Text.debugYjs() and expose editor component in OCA.Text.editorComponents
8+
*/
9+
10+
if (!window.OCA.Text) {
11+
window.OCA.Text = {}
12+
}
13+
14+
const editorComponents = window.OCA.Text.editorComponents ?? new Set()
15+
window.OCA.Text.editorComponents = editorComponents
16+
17+
/**
18+
* Print debug info for all editor components as a warning.
19+
*/
20+
export function debugYjs() {
21+
const intro = 'Editor Yjs debug data. Copy the objects above that start with "fileId".'
22+
const introChrome = '- In Chrome, select "Copy" at the end of the line.'
23+
const introFirefox = '- In Firefox, right-click on the object and select "Copy object".'
24+
const styleBold = 'font-weight: bold;'
25+
const styleItalic = 'font-weight: normal; font-style: italic;'
26+
27+
for (const editorComponent of editorComponents.values()) {
28+
console.warn(JSON.stringify(editorComponent.debugData(), null, ' '))
29+
}
30+
31+
console.warn('%c%s\n%c%s\n%s', styleBold, intro, styleItalic, introChrome, introFirefox)
32+
}
33+
34+
if (!window.OCA.Text.debugYjs) {
35+
window.OCA.Text.debugYjs = debugYjs
36+
}
37+
38+
/**
39+
* Expose editor component in OCA.Text.editorComponents
40+
* @param {object} component - the editor component to include in debug output
41+
*/
42+
export function exposeForDebugging(component) {
43+
editorComponents.add(component)
44+
}
45+
46+
/**
47+
* Drop editor component from OCA.Text.editorComponents
48+
* @param {object} component - the editor component to remove from debug output
49+
*/
50+
export function removeFromDebugging(component) {
51+
editorComponents.delete(component)
52+
}

0 commit comments

Comments
 (0)