Rewrite of js code, adding cursor support to the encoder.