decorations are cleared after replace doc fully

hello everyone.
I extented highlight lines feature like this demo(select some text , then press shift+ctrl/cmd+h):
highlight line demo

and then I import Code Formatting feature by js-beautify

when I use dispatch replace doc with formattedCode

dispatch({changes: {from: 0, to:doc.length, insert: formattedCode}})

decorations will be cleared

how to replace text only?
how to apply old decorations again?

The decorations are associated with positions in the text. If you replace the text, those positions are lost (the editor doesn’t magically know which parts of the new text correspond to parts of the old text).

thanks you reply.

but, when i change text by keyboard
like select all characters at some line, and input new characters
the decoration will be remain

what are difference between them