Module: GeditorHelper

Defined in:
lib/helpers/geditor_helper.rb

Instance Method Summary collapse

Instance Method Details

#g_format(text) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/helpers/geditor_helper.rb', line 2

def g_format(text)
  if text
    result = ""
    while text =~ /(\[code\:([a-z].+?)\](.+?)\[\/code\])/m
      result << GformatString.new($`).to_html_format + $&
      text = $'
    end
    result << GformatString.new(text).to_html_format
    return raw CoderayString.new(result).format
  end
end

#geditor_buttonsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/helpers/geditor_helper.rb', line 14

def geditor_buttons
  raw %q(
  <script>
  /*****************************************/
  // Name: Javascript Textarea HTML Editor
  // Version: 1.3
  // Author: Balakrishnan
  // Modified by: Gary Zhang
  // Last Modified Date: 13/Sep/2012
  // License: Free
  // URL: http://www.corpocrat.com
  /******************************************/

  var textarea;
  var content;
  function gEditor(obj) {
  	document.write("<img class=\"button\" src=\"/assets/geditor/bold.png\" name=\"btnBold\" title=\"Bold\" onClick=\"doAddTags('*','*','" + obj + "')\">");
  	document.write("<img class=\"button\" src=\"/assets/geditor/underline.png\" name=\"btnUnderline\" title=\"Underline\" onClick=\"doAddTags('+','+','" + obj + "')\">");
  	document.write("<img class=\"button\" src=\"/assets/geditor/h1.png\" name=\"btnHead\" title=\"Head1\" onClick=\"doAddTags('[h]','[/h]','" + obj + "')\">");
  	document.write("<img class=\"button\" src=\"/assets/geditor/ordered.png\" name=\"btnList\" title=\"Unordered List\" onClick=\"doList('<ul>','</ul>','" + obj + "')\">");
    	document.write("<img class=\"button\" src=\"/assets/geditor/code.png\" name=\"btnCode\" title=\"Code\" onClick=\"doAddTags('[code:ruby]','[/code]','" + obj + "')\">");
  	document.write("<img class=\"button\" src=\"/assets/geditor/link.png\" name=\"btnLink\" title=\"Insert Link\" onClick=\"doAddTags('[link: link_description]','[/link]','" + obj + "')\">");
  	document.write("<img class=\"button\" src=\"/assets/geditor/image.png\" name=\"btnPicture\" title=\"Insert Picture\" onClick=\"doAddTags('[img]','[/img]','" + obj + "')\">");
  	document.write("<br>"); }

  function doAddTags(tag1,tag2,obj)
  {
  	textarea = document.getElementById(obj);
  	if (document.selection) // Code for IE
  	  {
  		  textarea.focus();
  			var sel = document.selection.createRange();
  			sel.text = tag1 + sel.text + tag2;
  		}
     else // Code for Mozilla Firefox
       {
  			var len = textarea.value.length;
  		   var start = textarea.selectionStart;
  			var end = textarea.selectionEnd;
  			var scrollTop = textarea.scrollTop;
  			var scrollLeft = textarea.scrollLeft;
          var sel = textarea.value.substring(start, end);
  			var rep = tag1 + sel + tag2;
          textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);		
  			textarea.scrollTop = scrollTop;
  			textarea.scrollLeft = scrollLeft;
  		}
  }

  function doList(tag1,tag2,obj)
  {
  	textarea = document.getElementById(obj);	
  	if (document.selection) // Code for IE
  	{
  	  textarea.focus();
  		var sel = document.selection.createRange();
  		var list = sel.text.split('\n');
  		for(i=0;i<list.length;i++) 
  		{ list[i] = '* ' + list[i]; }
  		sel.text = list.join("\n");
  	} 
  	else // Code for Firefox
  	{
  		var len = textarea.value.length;
  	   var start = textarea.selectionStart;
  		var end = textarea.selectionEnd;
  		var i;
  		var scrollTop = textarea.scrollTop;
  		var scrollLeft = textarea.scrollLeft;
       var sel = textarea.value.substring(start, end);
  		var list = sel.split('\n');
  		for(i=0;i<list.length;i++) 
  		{ list[i] = '* ' + list[i]; }
  		var rep = list.join("\n");
  		textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
  		textarea.scrollTop = scrollTop;
  		textarea.scrollLeft = scrollLeft;
   	 }
  }
  gEditor('geditor'); </script>
  )
end