About a document I can't say anything but I would start on my own by adding an array for a record that keeps track about changes and use that array within a keyup event to act.
Of course to add or remove an entry should be better checked, above is just a quick and dirty variant but it help to understand how it might work I hope.
When the minimalistic variant works, you can work on the logic that it for example replace many single letters to one string if they are written continues etc...
Or that a remove should only be done when new data is added etc ....
Lots of possibilities.
My idea would be like
type
TEditHistory = record
Line: Integer; // in what line did a change happen
Position: Integer; // at what position of above line did the change happen
Action: TEditAction; // simple enum that tell if you added a char or pressed delete etc ...
Old: string; // original content if enum told delete or replace ...
New: string; // new char(s)
end;
And make heavy use of the OnKeyUp() event for all possibilities that can happen, including checks if something was pasted, replaced, deleted etc ....