How does one get the current line, or the currently edited node, within the onChange
or onKeyDown
methods in Slate.js?
I'm trying to add an updatedAt
or createdAt
parameter in the data
attribute for a particular line.
Here's a proof of concept for what I'm trying to do:
onChange = ({ value }) => {
return value.document.nodes.reduce((change, node) => {
return change.setNodeByKey(node.get("key"), {
data: {
createdAt: new Date(),
...node.get("data").toJS(),
},
});
}, value.change()).value;
}
This loops through every node and adds a createdAt
attribute in data if one is not already present. This is obviously not great code since it's looping through every node and has to unserialize the Immutable data object for each node, but it hopefully illustrates what I'm looking to do.
How do I set the data attribute for just the current node?