Commit 458136409ea69ea5ff8a16ade0e29eab2f722343

Authored by Victor Costa
1 parent 7fd79948

comment_paragraph: fix range selection

plugins/comment_paragraph/public/comment_paragraph_macro.js
@@ -49,6 +49,8 @@ jQuery(document).ready(function($) { @@ -49,6 +49,8 @@ jQuery(document).ready(function($) {
49 49
50 rangy.init(); 50 rangy.init();
51 cssApplier = rangy.createCssClassApplier("commented-area", {normalize: false}); 51 cssApplier = rangy.createCssClassApplier("commented-area", {normalize: false});
  52 + cssApplierSelected = rangy.createCssClassApplier("commented-area-selected", {normalize: false});
  53 +
52 //Add marked text bubble 54 //Add marked text bubble
53 $("body").append('\ 55 $("body").append('\
54 <a id="comment-bubble">\ 56 <a id="comment-bubble">\
@@ -83,11 +85,14 @@ jQuery(document).ready(function($) { @@ -83,11 +85,14 @@ jQuery(document).ready(function($) {
83 $('#comment-paragraph-plugin_' + paragraph).find('.side-comments-counter').click(); 85 $('#comment-paragraph-plugin_' + paragraph).find('.side-comments-counter').click();
84 }); 86 });
85 87
86 - function hideAllSelectedAreasExcept(clickedParagraph){ 88 + function hideAllSelectedAreasExcept(clickedParagraph, areaClass) {
  89 + if(!areaClass) {
  90 + areaClass = '.commented-area';
  91 + }
87 $(".comment_paragraph").each(function(){ 92 $(".comment_paragraph").each(function(){
88 paragraph = $(this).closest('.comment-paragraph-plugin').data('paragraph'); 93 paragraph = $(this).closest('.comment-paragraph-plugin').data('paragraph');
89 if(paragraph != clickedParagraph){ 94 if(paragraph != clickedParagraph){
90 - $(this).find(".commented-area").contents().unwrap(); 95 + $(this).find(areaClass).contents().unwrap();
91 $(this).html($(this).html()); //XXX: workaround to prevent creation of text nodes 96 $(this).html($(this).html()); //XXX: workaround to prevent creation of text nodes
92 } 97 }
93 }); 98 });
@@ -103,6 +108,14 @@ jQuery(document).ready(function($) { @@ -103,6 +108,14 @@ jQuery(document).ready(function($) {
103 return text; 108 return text;
104 } 109 }
105 110
  111 + function clearSelection() {
  112 + if ( document.selection ) {
  113 + document.selection.empty();
  114 + } else if ( window.getSelection ) {
  115 + window.getSelection().removeAllRanges();
  116 + }
  117 + }
  118 +
106 function setCommentBubblePosition(posX, posY) { 119 function setCommentBubblePosition(posX, posY) {
107 $("#comment-bubble").css({ 120 $("#comment-bubble").css({
108 top: (posY - 80), 121 top: (posY - 80),
@@ -164,10 +177,12 @@ jQuery(document).ready(function($) { @@ -164,10 +177,12 @@ jQuery(document).ready(function($) {
164 form.find('input.selected_content').val(selected_content) 177 form.find('input.selected_content').val(selected_content)
165 } 178 }
166 rootElement.focus(); 179 rootElement.focus();
167 - cssApplier.toggleSelection(); 180 + cssApplierSelected.toggleSelection();
  181 + clearSelection();
  182 +
168 //set a one time handler to prevent multiple selections 183 //set a one time handler to prevent multiple selections
169 var fn = function() { 184 var fn = function() {
170 - hideAllSelectedAreasExcept(); 185 + hideAllSelectedAreasExcept(null, '.commented-area-selected');
171 $('.comment-paragraph-plugin').off('mousedown', '.comment_paragraph', fn); 186 $('.comment-paragraph-plugin').off('mousedown', '.comment_paragraph', fn);
172 } 187 }
173 $('.comment-paragraph-plugin').on('mousedown', '.comment_paragraph', fn); 188 $('.comment-paragraph-plugin').on('mousedown', '.comment_paragraph', fn);
@@ -193,6 +208,7 @@ jQuery(document).ready(function($) { @@ -193,6 +208,7 @@ jQuery(document).ready(function($) {
193 processAnchor(); 208 processAnchor();
194 209
195 $(document).on('mouseenter', 'li.article-comment', function() { 210 $(document).on('mouseenter', 'li.article-comment', function() {
  211 + hideAllSelectedAreasExcept(null, '.commented-area-selected');
196 var selected_area = $(this).find('input.paragraph_comment_area').val(); 212 var selected_area = $(this).find('input.paragraph_comment_area').val();
197 var container = $(this).closest('.comment-paragraph-plugin'); 213 var container = $(this).closest('.comment-paragraph-plugin');
198 var rootElement = container.find('.comment_paragraph')[0]; 214 var rootElement = container.find('.comment_paragraph')[0];
@@ -205,5 +221,14 @@ jQuery(document).ready(function($) { @@ -205,5 +221,14 @@ jQuery(document).ready(function($) {
205 221
206 $(document).on('mouseleave', 'li.article-comment', function() { 222 $(document).on('mouseleave', 'li.article-comment', function() {
207 hideAllSelectedAreasExcept(); 223 hideAllSelectedAreasExcept();
  224 +
  225 + var container = $(this).closest('.comment-paragraph-plugin');
  226 + var selected_area = container.find('input.selected_area').val();
  227 + var rootElement = container.find('.comment_paragraph')[0];
  228 + if(selected_area != ""){
  229 + rangy.deserializeSelection(selected_area, rootElement);
  230 + cssApplierSelected.toggleSelection();
  231 + }
  232 + clearSelection();
208 }); 233 });
209 }); 234 });
plugins/comment_paragraph/public/style.css
@@ -32,16 +32,24 @@ div.article-comments-list-more{ @@ -32,16 +32,24 @@ div.article-comments-list-more{
32 font-weight: bold; 32 font-weight: bold;
33 } 33 }
34 34
35 -.commented-area { 35 +.comment-paragraph-plugin .commented-area {
36 background-color: lightseagreen; 36 background-color: lightseagreen;
  37 + color: white;
  38 +}
  39 +
  40 +.comment-paragraph-plugin .commented-area-selected {
  41 + background-color: rgb(255, 86, 86);
  42 + color: white;
37 } 43 }
38 44
39 .comment_paragraph ::selection { 45 .comment_paragraph ::selection {
40 - background: lightseagreen; /* WebKit/Blink Browsers */ 46 + background-color: lightseagreen; /* WebKit/Blink Browsers */
  47 + color: white;
41 } 48 }
42 49
43 .comment_paragraph ::-moz-selection { 50 .comment_paragraph ::-moz-selection {
44 - background: lightseagreen; /* Gecko Browsers */ 51 + background-color: lightseagreen; /* Gecko Browsers */
  52 + color: white;
45 } 53 }
46 54
47 .comment_paragraph{ 55 .comment_paragraph{