diff --git a/common/lib/xmodule/xmodule/js/src/html/edit.coffee b/common/lib/xmodule/xmodule/js/src/html/edit.coffee index de92a54b1f..fc0045a297 100644 --- a/common/lib/xmodule/xmodule/js/src/html/edit.coffee +++ b/common/lib/xmodule/xmodule/js/src/html/edit.coffee @@ -48,7 +48,7 @@ class @HTMLEditingDescriptor setup : @setupTinyMCE, # Cannot get access to tinyMCE Editor instance (for focusing) until after it is rendered. # The tinyMCE callback passes in the editor as a paramter. - init_instance_callback: @focusVisualEditor + init_instance_callback: @initInstanceCallback }) @showingVisualEditor = true @@ -96,29 +96,32 @@ class @HTMLEditingDescriptor # Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing. showAdvancedEditor: (visualEditor) -> if visualEditor.isDirty() - content = visualEditor.getContent({no_events: 1}) - regex = new RegExp(@base_asset_url, 'g') - content = content.replace(regex, '/static/') + content = @rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/') @advanced_editor.setValue(content) @advanced_editor.setCursor(0) @advanced_editor.refresh() @advanced_editor.focus() @showingVisualEditor = false + rewriteStaticLinks: (content, from, to) -> + regex = new RegExp(from, 'g') + return content.replace(regex, to) + # Show the Visual (tinyMCE) Editor. Pulled out as a helper method for unit testing. showVisualEditor: (visualEditor) -> # In order for isDirty() to return true ONLY if edits have been made after setting the text, # both the startContent must be sync'ed up and the dirty flag set to false. - visualEditor.startContent = visualEditor.getContent({format: "raw", no_events: 1}); + content = @rewriteStaticLinks(@advanced_editor.getValue(), '/static/', @base_asset_url) + visualEditor.setContent(content) + visualEditor.startContent = content @focusVisualEditor(visualEditor) @showingVisualEditor = true - focusVisualEditor: (visualEditor) => - content = @advanced_editor.getValue() - regex = new RegExp('/static/', 'g') - content = content.replace(regex, @base_asset_url) - visualEditor.setContent(content) + initInstanceCallback: (visualEditor) => + visualEditor.setContent(@rewriteStaticLinks(@advanced_editor.getValue(), '/static/', @base_asset_url)) + @focusVisualEditor(visualEditor) + focusVisualEditor: (visualEditor) => visualEditor.focus() # Need to mark editor as not dirty both when it is initially created and when we switch back to it. visualEditor.isNotDirty = true @@ -139,7 +142,5 @@ class @HTMLEditingDescriptor text = @advanced_editor.getValue() visualEditor = @getVisualEditor() if @showingVisualEditor and visualEditor.isDirty() - text = visualEditor.getContent({no_events: 1}) - regex = new RegExp(@base_asset_url, 'g') - text = text.replace(regex, '/static/') + text = @rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/') data: text