-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathjquery.ghostdown.js
85 lines (78 loc) · 3.3 KB
/
jquery.ghostdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
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
(function ($, ShowDown, CodeMirror) {
"use strict";
$.widget( "b4m.ghostDown", {
editor: null,
markdown: null,
html: null,
converter: null,
_create: function() {
this.converter = new ShowDown.converter();
this.editor = CodeMirror.fromTextArea(this.element.find('textarea')[0], {
mode: 'markdown',
tabMode: 'indent',
lineWrapping: true
});
this.editor.on("change", $.proxy(function () {
this._updatePreview();
}, this));
$('.entry-markdown header, .entry-preview header', this.element).click(function (e) {
$('.entry-markdown, .entry-preview', this.element).removeClass('active');
$(e.target, this.element).closest('section').addClass('active');
});
$('.CodeMirror-scroll', this.element).on('scroll', $.proxy(function (e) {
this._syncScroll(e);
}, this));
// Shadow on Markdown if scrolled
$('.CodeMirror-scroll', this.element).scroll(function(e) {
if ($(e.target).scrollTop() > 10) {
$('.entry-markdown', this.element).addClass('scrolling');
} else {
$('.entry-markdown', this.element).removeClass('scrolling');
}
});
// Shadow on Preview if scrolled
$('.entry-preview-content', this.element).scroll(function(e) {
if ($('.entry-preview-content', $(e.target).scrollTop()).scrollTop() > 10) {
$('.entry-preview', this.element).addClass('scrolling');
} else {
$('.entry-preview', this.element).removeClass('scrolling');
}
});
this._updatePreview();
},
_updatePreview: function() {
var preview = this.element.find('.rendered-markdown');
this.markdown = this.editor.getValue();
this.html = this.converter.makeHtml(this.markdown);
preview.html(this.html);
this._updateWordCount();
},
getHtml: function () {
return this.html;
},
getMarkdown: function () {
return this.markdown;
},
_syncScroll: function (e) {
// vars
var $codeViewport = $(e.target),
$previewViewport = $('.entry-preview-content'),
$codeContent = $('.CodeMirror-sizer'),
$previewContent = $('.rendered-markdown'),
// calc position
codeHeight = $codeContent.height() - $codeViewport.height(),
previewHeight = $previewContent.height() - $previewViewport.height(),
ratio = previewHeight / codeHeight,
previewPostition = $codeViewport.scrollTop() * ratio;
// apply new scroll
$previewViewport.scrollTop(previewPostition);
},
_updateWordCount: function() {
var wordCount = this.element.find('.entry-word-count'),
editorValue = this.markdown;
if (editorValue.length) {
wordCount.html(editorValue.match(/\S+/g).length + ' words');
}
}
});
}(jQuery, Showdown, CodeMirror));