Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] feat(debug): Expose yjs debug function in editor API #6607

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export default {
subscribe('text:image-node:add', this.onAddImageNode)
subscribe('text:image-node:delete', this.onDeleteImageNode)
this.emit('update:loaded', true)
this.setupEditorDebug()
},
created() {
this.$ydoc = new Doc()
Expand Down Expand Up @@ -761,6 +762,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 @@ -128,6 +128,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
Loading