Commit da9a4b5d by Chris Dodge

alter the HTML editor so that it can do the /static/ <--> /c4x/ substitutions…

alter the HTML editor so that it can do the /static/ <--> /c4x/ substitutions when toggling back and forth between the visual and advanced editors
parent 861f1ef7
<%! from django.utils.translation import ugettext as _ %> <%! from django.utils.translation import ugettext as _ %>
<div class="wrapper-comp-editor" id="editor-tab"> <div class="wrapper-comp-editor" id="editor-tab" data-base-asset-url="${base_asset_url}">
<section class="html-editor editor"> <section class="html-editor editor">
<ul class="editor-tabs"> <ul class="editor-tabs">
<li><a href="#" class="visual-tab tab current" data-tab="visual">${_("Visual")}</a></li> <li><a href="#" class="visual-tab tab current" data-tab="visual">${_("Visual")}</a></li>
......
...@@ -14,6 +14,7 @@ from xmodule.stringify import stringify_children ...@@ -14,6 +14,7 @@ from xmodule.stringify import stringify_children
from xmodule.x_module import XModule from xmodule.x_module import XModule
from xmodule.xml_module import XmlDescriptor, name_to_pathname from xmodule.xml_module import XmlDescriptor, name_to_pathname
import textwrap import textwrap
from xmodule.contentstore.content import StaticContent
log = logging.getLogger("mitx.courseware") log = logging.getLogger("mitx.courseware")
...@@ -79,6 +80,13 @@ class HtmlDescriptor(HtmlFields, XmlDescriptor, EditingDescriptor): ...@@ -79,6 +80,13 @@ class HtmlDescriptor(HtmlFields, XmlDescriptor, EditingDescriptor):
nc.append(candidate[:-4] + '.html') nc.append(candidate[:-4] + '.html')
return candidates + nc return candidates + nc
def get_context(self):
_context = EditingDescriptor.get_context(self)
# Add our specific template information (the raw data body)
_context.update({'base_asset_url': StaticContent.get_base_url_path_for_course_assets(self.location) + '/'})
return _context
# NOTE: html descriptors are special. We do not want to parse and # NOTE: html descriptors are special. We do not want to parse and
# export them ourselves, because that can break things (e.g. lxml # export them ourselves, because that can break things (e.g. lxml
# adds body tags when it exports, but they should just be html # adds body tags when it exports, but they should just be html
......
...@@ -3,6 +3,7 @@ class @HTMLEditingDescriptor ...@@ -3,6 +3,7 @@ class @HTMLEditingDescriptor
constructor: (element) -> constructor: (element) ->
@element = element; @element = element;
@base_asset_url = @element.find("#editor-tab").data('base-asset-url')
@advanced_editor = CodeMirror.fromTextArea($(".edit-box", @element)[0], { @advanced_editor = CodeMirror.fromTextArea($(".edit-box", @element)[0], {
mode: "text/html" mode: "text/html"
...@@ -95,7 +96,10 @@ class @HTMLEditingDescriptor ...@@ -95,7 +96,10 @@ class @HTMLEditingDescriptor
# Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing. # Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing.
showAdvancedEditor: (visualEditor) -> showAdvancedEditor: (visualEditor) ->
if visualEditor.isDirty() if visualEditor.isDirty()
@advanced_editor.setValue(visualEditor.getContent({no_events: 1})) content = visualEditor.getContent({no_events: 1})
regex = new RegExp(@base_asset_url, 'g')
content = content.replace(regex, '/static/')
@advanced_editor.setValue(content)
@advanced_editor.setCursor(0) @advanced_editor.setCursor(0)
@advanced_editor.refresh() @advanced_editor.refresh()
@advanced_editor.focus() @advanced_editor.focus()
...@@ -103,7 +107,6 @@ class @HTMLEditingDescriptor ...@@ -103,7 +107,6 @@ class @HTMLEditingDescriptor
# Show the Visual (tinyMCE) Editor. Pulled out as a helper method for unit testing. # Show the Visual (tinyMCE) Editor. Pulled out as a helper method for unit testing.
showVisualEditor: (visualEditor) -> showVisualEditor: (visualEditor) ->
visualEditor.setContent(@advanced_editor.getValue())
# In order for isDirty() to return true ONLY if edits have been made after setting the text, # 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. # both the startContent must be sync'ed up and the dirty flag set to false.
visualEditor.startContent = visualEditor.getContent({format: "raw", no_events: 1}); visualEditor.startContent = visualEditor.getContent({format: "raw", no_events: 1});
...@@ -111,6 +114,11 @@ class @HTMLEditingDescriptor ...@@ -111,6 +114,11 @@ class @HTMLEditingDescriptor
@showingVisualEditor = true @showingVisualEditor = true
focusVisualEditor: (visualEditor) => focusVisualEditor: (visualEditor) =>
content = @advanced_editor.getValue()
regex = new RegExp('/static/', 'g')
content = content.replace(regex, @base_asset_url)
visualEditor.setContent(content)
visualEditor.focus() visualEditor.focus()
# Need to mark editor as not dirty both when it is initially created and when we switch back to it. # Need to mark editor as not dirty both when it is initially created and when we switch back to it.
visualEditor.isNotDirty = true visualEditor.isNotDirty = true
...@@ -132,4 +140,6 @@ class @HTMLEditingDescriptor ...@@ -132,4 +140,6 @@ class @HTMLEditingDescriptor
visualEditor = @getVisualEditor() visualEditor = @getVisualEditor()
if @showingVisualEditor and visualEditor.isDirty() if @showingVisualEditor and visualEditor.isDirty()
text = visualEditor.getContent({no_events: 1}) text = visualEditor.getContent({no_events: 1})
regex = new RegExp(@base_asset_url, 'g')
text = text.replace(regex, '/static/')
data: text data: text
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment