Commits

Luke Plant committed 961ffb2

Fixed some bugs with saving/restoring the current element id, and re-factored.

Comments (0)

Files changed (1)

semanticeditor/media/semanticeditor/javascript/wymeditor/plugins/semantic/wymeditor.semantic.js

     // for one keyup event, so we handle that. This appears to happen for any
     // elements that are created by pressing 'Enter'
 
-    // We also need to update the disable/enabled state of the classList and commandList
-    var self = this;
-    var wym = self.wym;
-    var container = jQuery(wym.selected()).parentsOrSelf(this.blockdefSelector);
+    var node = this.getCurrentContainerNode();
+    var container;
+    if (node == null) {
+        return;
+    } else {
+        container = jQuery(node);
+    }
     if (evt.keyCode == 13 && !evt.shiftKey) {
         if (container.is("p[id],li[id]")) {
             // Need to clear id on all elem's with that id except the first.
             // (hoping that jQuery will return them in document order, which it
             // seems to)
-            jQuery(wym._doc).find(container.get(0).tagName + "#" +  container.attr("id")).
+            jQuery(this.wym._doc).find(container.get(0).tagName + "#" +  container.attr("id")).
                 each(function(i){
                          var node = this;
                          if (i > 0) { // skip the first
                      });
         }
     }
+    // We also need to update the disable/enabled state of the classList and commandList
     this.updateAppliedButtons(container);
 };
 
     // result in strange ids e.g. an h2 element with the id='h1_1'.  However,
     // this doesn't matter at all - it only matters that ids are unique
     var nodeId = null;
-    var s = this.wym._iframe.contentWindow.getSelection();
-    if (s.focusNode != null) {
-        var node = s.focusNode;
-        if (node.nodeName == "#text") {
-            // always true?
-            node = node.parentNode;
-        }
+    var node = this.getCurrentContainerNode();
+    if (node != null) {
         nodeId = node.id;
     }
     this._savedNodeId = nodeId;
     if (this._savedNodeId == null) {
         return;
     }
-    var s = this.wym._iframe.contentWindow.getSelection();
-    if (s.focusNode != null) {
-        var node = s.focusNode;
-        if (node.nodeName == "#text") {
-            // always true?
-            node = node.parentNode;
-        }
-        if (node.id == null || node.id == "") {
-            node.id = this._savedNodeId;
-        }
+    var node = this.getCurrentContainerNode();
+    if (node != null &&
+        (node.id == null || node.id == "")) {
+        node.id = this._savedNodeId;
     }
     this._savedNodeId = null;
 };
 
 PresentationControls.prototype.updateAppliedButtons = function(curContainer) {
     var self = this;
+    var node;
     if (curContainer == undefined) {
-        curContainer = jQuery(this.wym.selected()).parentsOrSelf(this.blockdefSelector);
+        node = this.getCurrentContainerNode();
+    } else {
+        node = curContainer.get(0);
     }
-    var node = curContainer.get(0);
     if (node != undefined && node != this.currentNode) {
         // if current node has changed, might need to update list
         this.currentNode = node;
     this.presentationInfo[sectId] = new Array();
 };
 
+PresentationControls.prototype.getCurrentContainerNode = function() {
+    // Returns the node of the closest container element to the selection
+    var container = jQuery(this.wym.selected()).parentsOrSelf(this.blockdefSelector);
+    if (container.is(this.blockdefSelector)) {
+        return container.get(0);
+    } else {
+        return null;
+    }
+};
+
 PresentationControls.prototype.getCurrentSection = function(style) {
     // Returns the section ID of the current section that is applicable
     // to the given style, or undefined if there is none.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.