Commits

Don Brown committed 8f9db35

Don't record anchor if cannot reproduce selection, show warning

Comments (0)

Files changed (2)

                         <pluginArtifact>
                             <groupId>com.atlassian.labs</groupId>
                             <artifactId>speakeasy-plugin</artifactId>
-                            <version>1.0-SNAPSHOT</version>
+                            <version>0.3.1</version>
                         </pluginArtifact>
                     </pluginArtifacts>
                 </configuration>

src/main/resources/js/annotate.js

         var pageId = jQuery('#pageId').val();
         var contextPath = jQuery('#contextPath').val();
         var token = jQuery('meta[name=atlassian-token]').attr('content');
-        data.anchorMacro = "{anchor:" + data.anchorValue + "}";
+        data.anchorMacro = data.anchorValue ? "{anchor:" + data.anchorValue + "}" : "";
         jQuery.ajax({
             type: 'post',
             url: contextPath + '/pages/doaddcomment.action?pageId=' + pageId,
     }
 
     function handleSelection(selection) {
+        var parsed = selection.split(' ');
+        var firstWord = parsed[0];
+        var lastWord = parsed[parsed.length-1];
+        var numWordsInMiddle = (parsed.length - 2);
+        var signature = "";
+        var warning = "<p>Warning: Unable to find selection, so the inline notation will not be available.  This could " +
+                      "be due you selecting text in a comment or text spanning multiple paragraphs or HTML tags.</p>";
+        forEachLinkMatch([{
+            firstWord : firstWord,
+            lastWord : lastWord,
+            numWordsInMiddle : numWordsInMiddle
+        }], function(link, textNode, firstWordPos, endPos) {
+            warning = "";
+            signature = MAGIC_DELIMITOR + firstWord + MAGIC_DELIMITOR + numWordsInMiddle + MAGIC_DELIMITOR + lastWord;
+        });
+
         var dialog = new AJS.Dialog({width:470, height:400});
         dialog.addHeader("Inline Comment");
         dialog.addPanel("Inline Comment",
+                warning +
                 '<blockquote>' + selection + '</blockquote>' +
                 '<textarea id="inlineComment" rows="10" cols="50"></textarea>',
                 "panel-body");
-        var parsed = selection.split(' ');
-        var signature = MAGIC_DELIMITOR + parsed[0] + MAGIC_DELIMITOR + (parsed.length - 2) + MAGIC_DELIMITOR +  parsed[parsed.length-1];
         dialog.addButton("Save", function (dialog) {
             submitComment({quoteText: selection, commentText: jQuery('#inlineComment').val(), anchorValue : signature});
             dialog.hide();
         jQuery('#inlineComment').focus();
     }
 
-    function insertAfter( referenceNode, newNode )
-    {
-        referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
-    }
-
-
-
     function scanComments() {
         var links = {};
         var counter = 0;
             }
         });
 
+        forEachLinkMatch(links, function(link, textNode, firstWordPos, endPos) {
+            var text = textNode.nodeValue;
+            textNode.nodeValue = text.substring(0, firstWordPos);
+            jQuery(textNode).after(
+                    '<a name="inline-id-' + link.index + '">' +
+                    '<span id="inline-text-' + link.index + '">' +
+                    text.substring(firstWordPos, endPos) +
+                    '</span>' +
+                    '<sub id="inline-sub-' + link.index + '">' + link.comments.length + '</sub></a> ' + text.substring(endPos));
+            AJS.InlineDialog(jQuery('#inline-sub-' + link.index), 1, function(contents, trigger, showPopup) {
+                contents.empty();
+                jQuery.each(link.comments, function() {
+                    contents.append('<p>' + this.user  + ': ' + this.comment + ' <a href="#' + this.id + '">' +
+                            ' <img src="' + contextPath + '/images/icons/down_16.gif" /></a></p>');
+                });
+                showPopup();
+            });
+
+        });
+    }
+
+    function forEachLinkMatch(links, linkHandler)
+    {
         forEachTextNode('div.wiki-content', function(textNode) {
             var text = textNode.nodeValue;
             var matched = [];
                         var words = text.substring(firstWordPos, endPos).split(' ');
                         if (words.length - 2 == this.numWordsInMiddle) {
                             matched.push(matchText);
-                            textNode.nodeValue = text.substring(0, firstWordPos);
-                            jQuery(textNode).after(
-                                    '<a name="inline-id-' + this.index + '">' +
-                                    '<span id="inline-text-' + this.index + '">' +
-                                    text.substring(firstWordPos, endPos) +
-                                    '</span>' +
-                                    '<sub id="inline-sub-' + this.index + '">' + this.comments.length + '</sub></a> ' + text.substring(endPos));
-                            AJS.InlineDialog(jQuery('#inline-sub-' + this.index), 1, function(contents, trigger, showPopup) {
-                                contents.empty();
-                                jQuery.each(match.comments, function() {
-                                    contents.append('<p>' + this.user  + ': ' + this.comment + ' <a href="#' + this.id + '">' +
-                                            ' <img src="' + contextPath + '/images/icons/down_16.gif" /></a></p>');
-                                });
-                                showPopup();
-                            });
-
-
+                            linkHandler(match, textNode, firstWordPos, endPos);
                         }
                     }
                 }