-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (109 loc) · 3.3 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//------------------------------------------------------------------------------
// Externs
require('github-markdown-css/github-markdown.css')
require('highlight.js/styles/github.css')
require('./style.css')
require('jquery-tabby')
//------------------------------------------------------------------------------
// App
// Libraries
var throttle = require('lodash.throttle')
, routing = require('./lib/routing')
, storage = require('./lib/storage')
, render = require('./lib/render')
, hljs = require('highlight.js')
, flow = require('lodash.flow')
, help = require('./lib/help')
, keys = require('./lib/keys')
, pad = require('lodash.pad')
, marked = require('marked')
, cfg = require('./lib/cfg')
, u = require('./lib/util')
, d = require('./lib/dom')
var sync_scroll = scroll_syncer()
var router = routing.init()
// App
$(function() {
load_config()
init_editor()
listen_for_resize()
listen_for_scroll()
listen_for_input()
keys.init()
help.init(d.ed, d.preview)
})
// Private Helpers
function scroll_syncer() {
var dirty = false
return function($source, $target) {
if (!cfg.sync_scroll) { return }
if (dirty) { return (dirty = false) }
var epad = ($source[0].scrollHeight - $source.innerHeight()) || 1
, epct = $source.scrollTop() / epad
var ppad = ($target[0].scrollHeight - $target.innerHeight()) || 1
dirty = true
$target[0].scrollTop = Math.round(ppad * epct)
}
}
function init_editor() {
d.ed.tabby({tabString: ' '})
}
function load_config() {
storage.config('editor-css', u.into(d.ed, 'css', {default: {}}))
storage.config('preview-css', u.into(d.preview, 'css', {default: {}}))
storage.config('sync-scroll', u.into(cfg, 'sync_scroll', {assign:true, default:true}))
storage.config('rs-position', position_divider)
}
function listen_for_resize() {
var $doc = $(document)
, $bod = $('body')
d.rs.on('dragstart.resize', function() {
var ww = window.innerWidth
d.ed.css({"user-select": "none"})
d.preview.css({"user-select": "none"})
d.rs.css({opacity:0})
$doc.on('drop.resizing', function(e) {
e.preventDefault()
d.rs.css({left: e.pageX})
$bod.removeClass('resizing')
d.ed.css({"user-select": ""})
d.preview.css({"user-select": ""})
d.rs.css({opacity:''})
$doc.off('.resizing')
d.rs.off('.resizing')
storage.config('rs-position', e.pageX)
})
d.rs.on('drag.resizing', function (e) {
resize_frames(e.pageX, ww)
})
})
}
function listen_for_scroll() {
d.ed.on('scroll.markula', function() {
sync_scroll($(this), d.preview)
})
d.preview.on('scroll.markula', function() {
sync_scroll($(this), d.ed)
})
}
function listen_for_input() {
var _render = render.bind(null,null)
d.ed.on('input', throttle(flow(_render, u.save_file), 20))
}
function position_divider(xpx) {
if (xpx == undefined) return;
var ww = window.innerWidth
resize_frames(xpx, ww)
d.rs.css({left: xpx})
d.bod.removeClass('resizing')
}
function resize_frames(xpx, ww) {
ww || (ww = window.innerWidth)
var x = (xpx / ww) * 100
, xx = (100 - x)
d.eframe.css({width: x+'%'})
d.preview.css({left:x+'%', width:xx+'%'})
}
// TODO: this is needed by keys.js
// perhaps make a divider.js instead?
window.position_divider = position_divider