roojs-bootstrap.js
[roojs1] / Roo / bootstrap / Markdown.js
1  
2 /*
3  * - LGPL
4  */
5
6 /**
7  * @class Roo.bootstrap.Markdown
8  * @extends Roo.bootstrap.TextArea
9  * Bootstrap Showdown editable area
10  * @cfg {string} content
11  * 
12  * @constructor
13  * Create a new Showdown
14  */
15
16 Roo.bootstrap.Markdown = function(config){
17     Roo.bootstrap.Markdown.superclass.constructor.call(this, config);
18    
19 };
20
21 Roo.extend(Roo.bootstrap.Markdown, Roo.bootstrap.TextArea,  {
22     
23     editing :false,
24     
25     initEvents : function()
26     {
27         
28         Roo.bootstrap.TextArea.prototype.initEvents.call(this);
29         this.markdownEl = this.el.createChild({
30             cls : 'roo-markdown-area'
31         });
32         this.inputEl().addClass('d-none');
33          
34         this.markdownEl.dom.innerHTML = Roo.Markdown.toHtml(Roo.util.Format.htmlEncode(this.getValue()));
35         this.markdownEl.on('click', this.toggleTextEdit, this);
36         this.on('blur', this.toggleTextEdit, this);
37         this.on('specialkey', this.resizeTextArea, this);
38     },
39     
40     toggleTextEdit : function()
41     {
42         var sh = this.markdownEl.getHeight();
43         this.inputEl().addClass('d-none');
44         this.markdownEl.addClass('d-none');
45         if (!this.editing) {
46             // show editor?
47             this.inputEl().setHeight(Math.min(500, Math.max(sh,(this.getValue().split("\n").length+1) * 30)));
48             this.inputEl().removeClass('d-none');
49             this.inputEl().focus();
50             this.editing = true;
51             return;
52         }
53         // show showdown...
54         this.updateMarkdown();
55         this.markdownEl.removeClass('d-none');
56         this.editing = false;
57         return;
58     },
59     updateMarkdown : function()
60     {
61         if (this.getValue() == '') {
62             this.markdownEl.dom.innerHTML = String.format('<span class="roo-placeholder">{0}</span>', this.placeholder || '');
63             return;
64         }
65  
66         this.markdownEl.dom.innerHTML = Roo.Markdown.toHtml(Roo.util.Format.htmlEncode(this.getValue()));
67     },
68     
69     resizeTextArea: function () {
70         
71         var sh = 100;
72         Roo.log([sh, this.getValue().split("\n").length * 30]);
73         this.inputEl().setHeight(Math.min(500, Math.max(sh, (this.getValue().split("\n").length +1) * 30)));
74     },
75     setValue : function(val)
76     {
77         Roo.bootstrap.TextArea.prototype.setValue.call(this,val);
78         if (!this.editing) {
79             this.updateMarkdown();
80         }
81         
82     },
83     focus : function()
84     {
85         if (!this.editing) {
86             this.toggleTextEdit();
87         }
88         
89     }
90
91
92 });