editor: cleanup - removing unused code in edumed exercises implementation
[fnpeditor.git] / src / editor / plugins / core / links / linkElement.js
index 503e958..61d2abc 100644 (file)
@@ -26,8 +26,11 @@ _.extend(linkElement, {
         this.box.hide();
         this.addWidget(this.box);
     },
-    markAsCurrent: function(toggle) {
-        this.box.toggle(toggle);
+    onStateChange: function(changes) {
+        genericElement.onStateChange.call(this, changes);
+        if(_.isBoolean(changes.active)) {
+            this.box.toggle(changes.active);
+        }
     },
     onNodeAttrChange: function(event) {
         if(event.meta.attr === 'href') {
@@ -40,6 +43,7 @@ _.extend(linkElement, {
     changeLink: function(e) {
         var el = this,
             doc = this.wlxmlNode.document,
+            offset = el.canvas.getSelection().toDocumentFragment().offset,
             dialog = Dialog.create({
             title: gettext('Edit link'),
             executeButtonText: gettext('Apply'),
@@ -62,7 +66,10 @@ _.extend(linkElement, {
             }, {
                 metadata: {
                     description: gettext('Edit link'),
-                    fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
+                    fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
+                },
+                success: function() {
+                    el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
                 }
             });
         });
@@ -74,11 +81,23 @@ _.extend(linkElement, {
             doc = this.wlxmlNode.document;
 
         el.wlxmlNode.document.transaction(function() {
-            el.wlxmlNode.unwrapContent();
+            var f = el.canvas.getSelection().toDocumentFragment(),
+                prefLen = 0,
+                ret;
+
+            if(el.wlxmlNode.isPrecededByTextNode()) {
+                prefLen = el.wlxmlNode.prev().getText().length;
+            }
+
+            ret = el.wlxmlNode.unwrapContent();
+            return doc.createFragment(doc.CaretFragment, {node: ret.element1, offset: prefLen + f.offset});
         }, {
             metadata: {
                 description: gettext('Remove link'),
                 fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
+            },
+            success: function(ret) {
+                el.canvas.select(ret);
             }
         });
     },