Yes, it is possible to capture changes to a document separately using y.js so that they can be applied again. y.js provides a feature called "transactions" which allows you to group multiple changes into a single operation that can be applied to a document.
Here's how you could achieve the scenario you described using y.js:
- Bob creates a document using y.js. He can do this by creating a new Y.Doc instance and initializing it with some content. For example:
const doc = new Y.Doc();
doc.create('This is my document');
- Tim adds a link to Bob's document using y.js. He can do this by creating a new Y.Text instance and appending it to the document. He can also create a transaction to group his changes together. For example:
const text = new Y.Text('[Link to Bob's document]');
const transaction = doc.transaction();
transaction.insert(text);
transaction.commit();
- Bob changes the document using y.js. He can do this by creating a new Y.Text instance and appending it to the document. For example:
const text = new Y.Text('This is my updated document');
doc.insert(text);
- Tim gets Bob's changes and his link is still applied. He can do this by applying Bob's changes to his own copy of the document using y.js. He can create a new Y.Doc instance and apply Bob's changes to it. For example:
const docCopy = new Y.Doc();
docCopy.apply(doc.getChanges());
Tim's link is still applied because it was added in a separate transaction. The transactions are independent of each other, so changes made in one transaction do not affect the changes made in another transaction.
Note that this is just a basic example, and there are many more advanced features available in y.js that can be used to build more complex collaborative editing applications.