This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
/
pretty.coffee
157 lines (135 loc) · 3.78 KB
/
pretty.coffee
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
# write_pretty_story, a writing program for writers
# who don't want to edit what they write.
filterText = (text) ->
filteredText = text
filteredText = filteredText.replace(/&/g, '&')
filteredText = filteredText.replace(/--/g, '—')
# Split on empty line (markdown style).
paragraphs = filteredText.split(/\n\n\n*/)
return paragraphs
# Given a chunk of plain text, filter it
# and print it out in the body of the page.
formatNewParagraph = (text) ->
paragraphs = filterText(text)
for paragraph in paragraphs
newParagraph = $(document.createElement('p'))
newParagraph.html(paragraph)
$('#written-text').append(newParagraph)
wordCount = (text) ->
if text == ""
0
else
splitText = text.split(' ')
realSplits = (word for word in splitText when word.length > 0)
realSplits.length
# Update the word count that appears below the write-box.
syncWordCount = () ->
display = $('#word-count')
uncommittedText = $('textarea[name=write]').val()
text = $('#written-text p').text()
wc = wordCount(text) + wordCount(uncommittedText)
display.text(wc)
# Clear the textbox and format the new paragraph
# in the body.
makeNewParagraph = () ->
textarea = $('#writebox textarea')
text = textarea.val()
formatNewParagraph(text)
textarea.val('')
syncWordCount()
textareaKeyUp = (event) ->
if event.which is 13
$('#write-form').submit()
h = $('#written-text p:last').height()
# Autoscroll so the directions, word count
# and uncommitted text stay in view.
if h
window.scrollBy(0, h + 50)
saveStory()
else if event.which in [32, 8]
syncWordCount()
onSubmit = (event) ->
event.preventDefault()
makeNewParagraph()
mainText =
textArea: () ->
$('#writebox textarea')
title: () ->
$('h1.story-title')
appStuff: () ->
@title().add('#bd')
getTitle: () ->
@title().html()
setTitle: (s) ->
@title().html(s)
show: () ->
@appStuff().show()
@textArea().focus()
hide: () ->
@appStuff().hide()
clear: () ->
$('#written-text').html('')
titleTextBox = () ->
$('#title-edit input[name="title"]')
showTitleRequest = () ->
$('#title-edit').addClass('requesting')
titleTextBox().focus().val('')
hideTitleRequest = () ->
$('#title-edit').removeClass('requesting')
getTitle = () ->
titleInput = titleTextBox()
title = titleInput.val()
mainText.setTitle(title)
requestTitle = () ->
mainText.hide()
showTitleRequest()
clearStory = () ->
localStorage.removeItem('title')
localStorage.removeItem('paragraphs')
saveStory = () ->
localStorage['title'] = $('h1.story-title').html()
paragraphs = []
$('#written-text p').each((el, i) ->
paragraphs.push($(i).text())
)
localStorage['paragraphs'] = paragraphs.join("\n\n\n")
startWordCountOffset = () ->
rawWordCount = $('#word-count').text()
parsedWordCount = parseInt(rawWordCount)
wordCountOffset = parsedWordCount
restoreStory = () ->
title = localStorage['title']
paragraphs = localStorage['paragraphs']
mainText.setTitle(title)
if paragraphs?
formatNewParagraph(paragraphs)
mainText.textArea().focus()
syncWordCount()
startWordCountOffset()
savedStoryExists = () ->
localStorage? and (localStorage['paragraphs']? or localStorage['title']?)
newStory = () ->
if Modernizr.localstorage
clearStory()
requestTitle()
titleTextBox().keyup((event) ->
if event.which is 13
getTitle()
hideTitleRequest()
mainText.clear()
mainText.show()
if Modernizr.localstorage
saveStory()
)
onReady = () ->
$('#write-form').submit(onSubmit)
$('#writebox textarea').keyup(textareaKeyUp)
$('#new-story').click((event) ->
event.preventDefault()
newStory()
)
if Modernizr.localstorage and savedStoryExists()
restoreStory()
else
newStory()
$(document).ready(onReady)