This repository was archived by the owner on Jan 27, 2023. It is now read-only.
forked from skanaar/nomnoml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnomnoml.app.js
More file actions
191 lines (175 loc) · 5.46 KB
/
Copy pathnomnoml.app.js
File metadata and controls
191 lines (175 loc) · 5.46 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var nomnoml = nomnoml || {}
$(function (){
var storage = buildStorage(location.hash)
var jqCanvas = $('#canvas')
var viewport = $(window)
var lineNumbers = $('#linenumbers')
var lineMarker = $('#linemarker')
var jqTextarea = $('#textarea')
var textarea = document.getElementById('textarea')
var imgLink = document.getElementById('savebutton')
var linkLink = document.getElementById('linkbutton')
var canvasElement = document.getElementById('canvas')
var defaultSource = document.getElementById('defaultGraph').innerHTML
var startPosition = false
var positionMoved = {
dx: 0
, dy: 0
}
var graphics = skanaar.Canvas(canvasElement, {
mousedown: function(position) {
startPosition = position
}
, mouseup: function() {
startPosition = false
}
, mousemove: function(position) {
if (!startPosition) {
return
}
var dx = position.x - startPosition.x
var dy = position.y - startPosition.y
var canvasPosition = jqCanvas.position()
positionMoved = {
dx: positionMoved.dx + dx
, dy: positionMoved.dy + dy
}
if (dx == 0 && dy == 0) {
return
}
startPosition = position
jqCanvas.css('top', canvasPosition.top + dy);
jqCanvas.css('left', canvasPosition.left + dx);
}
})
canvasElement.addEventListener('mouseout', function() {
startPosition = false
})
canvasElement.addEventListener('dblclick', function() {
startPosition = false
positionMoved = {
dx: 0
, dy: 0
}
sourceChanged()
})
window.addEventListener('resize', _.throttle(sourceChanged, 750, {leading: true}))
textarea.addEventListener('input', _.debounce(sourceChanged, 300))
textarea.value = storage.read()
if (storage.isReadonly) $('#storage-status').show()
else $('#storage-status').hide()
lineNumbers.val(_.times(60, _.identity).join('\n'))
initImageDownloadLink(imgLink, canvasElement)
sourceChanged()
nomnoml.toggleSidebar = function (id){
var sidebars = ['reference', 'about']
_.chain(sidebars).without(id).each(function (key){
document.getElementById(key).classList.remove('visible')
})
document.getElementById(id).classList.toggle('visible')
}
nomnoml.discardCurrentGraph = function (){
if (confirm('Do you want to discard current diagram and load the default example?')){
textarea.value = defaultSource
sourceChanged()
}
}
function setShareableLink(str){
var base = 'http://www.nomnoml.com/#view/'
linkLink.href = base + str.split('\n').join('%0A').split(' ').join('%20')
}
function isViewMode(locationHash){
}
function buildStorage(locationHash){
if (locationHash.substring(0,6) === '#view/')
return {
read: function (){ return decodeURIComponent(locationHash.substring(6)) },
save: function (source){ setShareableLink(textarea.value) },
isReadonly: true
}
return {
read: function (){ return localStorage['nomnoml.lastSource'] || defaultSource },
save: function (source){
setShareableLink(textarea.value)
localStorage['nomnoml.lastSource'] = source
},
isReadonly: false
}
}
function initImageDownloadLink(link, canvasElement){
link.addEventListener('click', downloadImage, false);
function downloadImage(){
var url = canvasElement.toDataURL('image/png')
link.href = url;
}
}
function getConfig(d){
return {
arrowSize: +d.arrowSize || 1,
bendSize: +d.bendSize || 0.3,
direction: {down: 'TB', right: 'LR'}[d.direction] || 'TB',
gutter: +d.gutter || 5,
edgeMargin: (+d.edgeMargin) || 0,
edges: {hard: 'hard', rounded: 'rounded'}[d.edges] || 'rounded',
fill: (d.fill || '#eee8d5;#fdf6e3;#eee8d5;#fdf6e3').split(';'),
fillArrows: d.fillArrows === 'true',
font: d.font || 'Calibri',
fontSize: (+d.fontSize) || 12,
leading: (+d.leading) || 1.25,
lineWidth: (+d.lineWidth) || 3,
padding: (+d.padding) || 8,
spacing: (+d.spacing) || 40,
stroke: d.stroke || '#33322E',
zoom: +d.zoom || 1
}
}
function fitCanvasSize(rect, scale, superSampling){
var w = rect.width * scale
var h = rect.height * scale
jqCanvas.attr({width: superSampling*w, height: superSampling*h})
jqCanvas.css({
top: 300 * (1 - h/viewport.height()) + positionMoved.dy,
left: 150 + (viewport.width() - w)/2 + positionMoved.dx,
width: w,
height: h
})
console.log(positionMoved)
}
function setFont(config, isBold, isItalic){
var style = (isBold === 'bold' ? 'bold ' : '')
if (isItalic) style = 'italic ' + style
graphics.ctx.font = style+config.fontSize+'pt '+config.font+', Helvetica, sans-serif'
}
function parseAndRender(superSampling){
var ast = nomnoml.parse(textarea.value)
var config = getConfig(ast.directives)
var measurer = {
setFont: setFont,
textWidth: function (s){ return graphics.ctx.measureText(s).width },
textHeight: function (s){ return config.leading * config.fontSize }
}
var layout = nomnoml.layout(measurer, config, ast)
fitCanvasSize(layout, config.zoom, superSampling)
config.zoom *= superSampling
nomnoml.render(graphics, config, layout, setFont)
}
function sourceChanged(){
try {
var superSampling = window.devicePixelRatio || 1;
lineMarker.css('top', -30)
lineNumbers.css({background:'#eee8d5', color:'#D4CEBD'})
parseAndRender(superSampling)
storage.save(textarea.value)
} catch (e){
var matches = e.message.match('line ([0-9]*)')
if (matches){
var lineHeight = parseInt(jqTextarea.css('line-height'), 10)
lineMarker.css('top', 8 + lineHeight*matches[1])
}
else {
lineNumbers.css({background:'rgba(220,50,47,0.4)', color:'#657b83'})
throw e
}
}
}
})