Skip to content

Commit

Permalink
Merge pull request #6608 from nextcloud/backport/6598/stable30
Browse files Browse the repository at this point in the history
[stable30] feat(debug): Expose yjs debug function in editor API
  • Loading branch information
juliusknorr authored Nov 7, 2024
2 parents afc35d1 + 737ca47 commit 1c973ea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export default {
subscribe('text:image-node:delete', this.onDeleteImageNode)
this.emit('update:loaded', true)
subscribe('text:translate-modal:show', this.showTranslateModal)
this.setupEditorDebug()
},
created() {
this.$ydoc = new Doc()
Expand Down Expand Up @@ -749,6 +750,54 @@ export default {
console.debug(editor.getHTML())
},
/**
* Setup OCA.Text.debugYjs() and expose editor component in OCA.Text.editorComponents
*/
setupEditorDebug() {
if (!window.OCA.Text) {
window.OCA.Text = {}
}
if (!window.OCA.Text.editorComponents) {
window.OCA.Text.editorComponents = []
}
window.OCA.Text.editorComponents.push(this)
if (!window.OCA.Text.debugYjs) {
window.OCA.Text.debugYjs = () => {
const intro = 'Editor Yjs debug data. Copy the objects above that start with "fileId".'
const introChrome = '- In Chrome, select "Copy" at the end of the line.'
const introFirefox = '- In Firefox, right-click on the object and select "Copy object".'
const styleBold = 'font-weight: bold;'
const styleItalic = 'font-weight: normal; font-style: italic;'
for (const editorComponent of window.OCA.Text.editorComponents) {
console.warn(JSON.stringify(editorComponent.debugYjsData(), null, ' '))
}
console.warn('%c%s\n%c%s\n%s', styleBold, intro, styleItalic, introChrome, introFirefox)
}
}
},
/**
* Helper method to debug yjs issues
*/
debugYjsData() {
const yjsData = {
fileId: this.fileId,
filePath: this.relativePath,
clientId: this.$ydoc.clientID,
pendingStructs: this.$ydoc.store.pendingStructs,
clientVectors: [],
documentState: this.$syncService.getDocumentState(),
}
for (const client of this.$ydoc.store.clients.values()) {
yjsData.clientVectors.push(client.at(-1).id)
}
return yjsData
},
outlineToggled(visible) {
this.emit('outline-toggled', visible)
},
Expand Down
12 changes: 12 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ class TextEditorEmbed {
this.#getEditorComponent().$editor.commands.focus()
}

debugYjs() {
const yjsData = this.#getEditorComponent().debugYjsData()

const intro = 'Editor Yjs debug data. Copy the object below that starts with "clientId".'
const introChrome = '- In Chrome, select "Copy" at the end of the line.'
const introFirefox = '- In Firefox, right-click on the object and select "Copy object".'
const styleBold = 'font-weight: bold;'
const styleItalic = 'font-weight: normal; font-style: italic;'
console.warn(JSON.stringify(yjsData, null, ' '))
console.warn('%c%s\n%c%s\n%s', styleBold, intro, styleItalic, introChrome, introFirefox)
}

#registerDebug() {
if (window?._oc_debug) {
this.vm = this.#vm
Expand Down

0 comments on commit 1c973ea

Please sign in to comment.