ueberdosis / hocuspocus

The CRDT Yjs WebSocket backend for conflict-free real-time collaboration in your app.
https://tiptap.dev/docs/hocuspocus/introduction
MIT License
1.21k stars 117 forks source link

How do I add a last update time to the doc? #758

Closed lin52025iq closed 8 months ago

lin52025iq commented 9 months ago

The problem I am facing I want to save the last modified time.

The solution I would like tiptap-extension-document

import { Node } from '@tiptap/core'

export const Document = Node.create({
  name: 'doc',
  topNode: true,
  content: 'block+',
    addGlobalAttributes() {
        return [
            {
                types: ['doc'],
                attributes: {
                    lastChangedTime: {
                        default: -1
                    }
                }
            }
        ]
    },
    addCommands() {
        return {
            updateLastChangeTime: () => {
                return ({ commands }) => {
                    return commands.updateAttributes('doc', { lastChangedTime: +new Date() })
                }
            }
        }
    }
})

Add lastChangedTime to the document extension, and call updateLastChangeTime in hocuspocus onChange or onStoreDocument to update the last change time. But there is a problem with setting it up this way and I don't know where to call commands. What should I do?

janthurau commented 8 months ago

If you are using self-hosted hocuspocus, you can set up an extension that subscribes to onChange, and then sets a value in a custom map in the yjs document.

When using collab, you can probably use the onUpdate event (of tiptap) and again save the current time the ydoc. A request for a tiptap extension should go in the tiptap github :)