9fe8054e21f8e38ea2c33b3526409743af9aa2f4
[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         this.markdownEl.dom.innerHTML = Roo.Markdown.toHtml(Roo.util.Format.htmlEncode(this.getValue()));
34         this.markdownEl.on('click', this.toggleTextEdit, this);
35         this.on('blur', this.toggleTextEdit, this);
36         this.on('specialkey', this.resizeTextArea, this);
37     },
38     
39     toggleTextEdit : function()
40     {
41         var sh = this.markdownEl.getHeight();
42         this.inputEl().addClass('d-none');
43         this.markdownEl.addClass('d-none');
44         if (!this.editing) {
45             // show editor?
46             this.inputEl().setHeight(Math.min(500, Math.max(sh,(this.getValue().split("\n").length+1) * 30)));
47             this.inputEl().removeClass('d-none');
48             this.inputEl().focus();
49             this.editing = true;
50             return;
51         }
52         // show showdown...
53         this.updateMarkdown();
54         this.markdownEl.removeClass('d-none');
55         this.editing = false;
56         return;
57     },
58     updateMarkdown : function()
59     {
60         if (this.getValue() == '') {
61             this.markdownEl.dom.innerHTML = String.format('<span class="roo-placeholder">{0}</span>', this.placeholder);
62             return;
63         }
64         this.markdownEl.dom.innerHTML = Roo.Markdown.toHtml(Roo.util.Format.htmlEncode(this.getValue()));
65     },
66     
67     resizeTextArea: function () {
68         
69         var sh = 100;
70         Roo.log([sh, this.getValue().split("\n").length * 30]);
71         this.inputEl().setHeight(Math.min(500, Math.max(sh, (this.getValue().split("\n").length +1) * 30)));
72     },
73     setValue : function(val)
74     {
75         Roo.bootstrap.TextArea.prototype.setValue.call(this,val);
76         if (!this.editing) {
77             this.updateMarkdown();
78         }
79         
80     },
81     focus : function()
82     {
83         if (!this.editing) {
84             this.toggleTextEdit();
85         }
86         
87     }
88
89
90 });