Skip to content

Commit 3aa227e

Browse files
committed
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 <max@nextcloud.com> Signed-off-by: Max <max@nextcloud.com>
1 parent 4b9b174 commit 3aa227e

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

src/components/Editor.vue

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ import { extensionHighlight } from '../helpers/mappings.js'
115115
import { createEditor, serializePlainText, loadSyntaxHighlight } from './../EditorFactory.js'
116116
import { createMarkdownSerializer } from './../extensions/Markdown.js'
117117
import markdownit from './../markdownit/index.js'
118-
118+
import { exposeForDebugging, removeFromDebugging } from '../helpers/debug.js'
119119
import { CollaborationCursor } from '../extensions/index.js'
120120
import DocumentStatus from './Editor/DocumentStatus.vue'
121121
import isMobile from './../mixins/isMobile.js'
@@ -351,7 +351,7 @@ export default {
351351
subscribe('text:image-node:add', this.onAddImageNode)
352352
subscribe('text:image-node:delete', this.onDeleteImageNode)
353353
this.emit('update:loaded', true)
354-
this.setupEditorDebug()
354+
exposeForDebugging(this)
355355
},
356356
created() {
357357
this.$ydoc = new Doc()
@@ -377,7 +377,8 @@ export default {
377377
const timeout = new Promise((resolve) => setTimeout(resolve, 2000))
378378
await Promise.any([timeout, this.$syncService.save()])
379379
}
380-
this.close()
380+
await this.close()
381+
removeFromDebugging(this)
381382
},
382383
methods: {
383384
initSession() {
@@ -762,46 +763,17 @@ export default {
762763
console.debug(editor.getHTML())
763764
},
764765
765-
/**
766-
* Setup OCA.Text.debugYjs() and expose editor component in OCA.Text.editorComponents
767-
*/
768-
setupEditorDebug() {
769-
if (!window.OCA.Text) {
770-
window.OCA.Text = {}
771-
}
772-
if (!window.OCA.Text.editorComponents) {
773-
window.OCA.Text.editorComponents = []
774-
}
775-
window.OCA.Text.editorComponents.push(this)
776-
777-
if (!window.OCA.Text.debugYjs) {
778-
window.OCA.Text.debugYjs = () => {
779-
const intro = 'Editor Yjs debug data. Copy the objects above that start with "fileId".'
780-
const introChrome = '- In Chrome, select "Copy" at the end of the line.'
781-
const introFirefox = '- In Firefox, right-click on the object and select "Copy object".'
782-
const styleBold = 'font-weight: bold;'
783-
const styleItalic = 'font-weight: normal; font-style: italic;'
784-
785-
for (const editorComponent of window.OCA.Text.editorComponents) {
786-
console.warn(JSON.stringify(editorComponent.debugYjsData(), null, ' '))
787-
}
788-
789-
console.warn('%c%s\n%c%s\n%s', styleBold, intro, styleItalic, introChrome, introFirefox)
790-
}
791-
}
792-
},
793-
794766
/**
795767
* Helper method to debug yjs issues
796768
*/
797-
debugYjsData() {
769+
debugData() {
798770
const yjsData = {
799771
fileId: this.fileId,
800772
filePath: this.relativePath,
801773
clientId: this.$ydoc.clientID,
802774
pendingStructs: this.$ydoc.store.pendingStructs,
803775
clientVectors: [],
804-
documentState: this.$syncService.getDocumentState(),
776+
documentState: this.$syncService?.getDocumentState(),
805777
}
806778
for (const client of this.$ydoc.store.clients.values()) {
807779
yjsData.clientVectors.push(client.at(-1).id)

src/helpers/debug.js

Lines changed: 52 additions & 0 deletions
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)