Refactor preview panel JS into CS