forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.coffee
205 lines (167 loc) · 5 KB
/
core.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class Simditor extends SimpleModule
@connect Util
@connect InputManager
@connect Selection
@connect UndoManager
@connect Keystroke
@connect Formatter
@connect Toolbar
@connect Indentation
@connect Clipboard
@count: 0
opts:
textarea: null
placeholder: ''
defaultImage: 'images/image.png'
params: {}
upload: false
indentWidth: 40
_init: ->
@textarea = $(@opts.textarea)
@opts.placeholder = @opts.placeholder || @textarea.attr('placeholder')
unless @textarea.length
throw new Error 'simditor: param textarea is required.'
return
editor = @textarea.data 'simditor'
if editor?
editor.destroy()
@id = ++ Simditor.count
@_render()
if simpleHotkeys
@hotkeys = simpleHotkeys
el: @body
else
throw new Error 'simditor: simple-hotkeys is required.'
return
if @opts.upload and simpleUploader
uploadOpts = if typeof @opts.upload == 'object' then @opts.upload else {}
@uploader = simpleUploader(uploadOpts)
form = @textarea.closest 'form'
if form.length
form.on 'submit.simditor-' + @id, =>
@sync()
form.on 'reset.simditor-' + @id, =>
@setValue ''
# set default value after all plugins are connected
@on 'initialized', =>
if @opts.placeholder
@on 'valuechanged', =>
@_placeholder()
@setValue @textarea.val().trim() || ''
# Disable the resizing of `img` and `table`
if @util.browser.mozilla
@util.reflow()
try
document.execCommand 'enableObjectResizing', false, false
document.execCommand 'enableInlineTableEditing', false, false
catch e
_tpl:"""
<div class="simditor">
<div class="simditor-wrapper">
<div class="simditor-placeholder"></div>
<div class="simditor-body" contenteditable="true">
</div>
</div>
</div>
"""
_render: ->
@el = $(@_tpl).insertBefore @textarea
@wrapper = @el.find '.simditor-wrapper'
@body = @wrapper.find '.simditor-body'
@placeholderEl = @wrapper.find('.simditor-placeholder')
.append(@opts.placeholder)
@el.data 'simditor', @
@wrapper.append(@textarea)
@textarea.data('simditor', @).blur()
@body.attr 'tabindex', @textarea.attr('tabindex')
if @util.os.mac
@el.addClass 'simditor-mac'
else if @util.os.linux
@el.addClass 'simditor-linux'
if @util.os.mobile
@el.addClass 'simditor-mobile'
if @opts.params
for key, val of @opts.params
$('<input/>', {
type: 'hidden'
name: key,
value: val
}).insertAfter(@textarea)
_placeholder: ->
children = @body.children()
if children.length == 0 or (children.length == 1 and
@util.isEmptyNode(children) and
parseInt(children.css('margin-left') || 0) < @opts.indentWidth)
@placeholderEl.show()
else
@placeholderEl.hide()
setValue: (val) ->
@hidePopover()
@textarea.val val
@body.html val
@formatter.format()
@formatter.decorate()
@util.reflow @body
@inputManager.lastCaretPosition = null
@trigger 'valuechanged'
getValue: () ->
@sync()
sync: ->
cloneBody = @body.clone()
@formatter.undecorate cloneBody
@formatter.format cloneBody
# generate `a` tag automatically
@formatter.autolink cloneBody
# remove empty `p` tag at the start/end of content
children = cloneBody.children()
lastP = children.last 'p'
firstP = children.first 'p'
while lastP.is('p') and @util.isEmptyNode(lastP)
emptyP = lastP
lastP = lastP.prev 'p'
emptyP.remove()
while firstP.is('p') and @util.isEmptyNode(firstP)
emptyP = firstP
firstP = lastP.next 'p'
emptyP.remove()
# remove images being uploaded
cloneBody.find('img.uploading').remove()
val = $.trim(cloneBody.html())
@textarea.val val
val
focus: ->
unless @body.is(':visible') and @body.is('[contenteditable]')
@el.find('textarea:visible').focus()
return
if @inputManager.lastCaretPosition
@undoManager.caretPosition @inputManager.lastCaretPosition
else
$blockEl = @body.children().last()
unless $blockEl.is('p')
$blockEl = $('<p/>').append(@util.phBr).appendTo(@body)
range = document.createRange()
@selection.setRangeAtEndOf $blockEl, range
@body.focus()
blur: ->
if @body.is(':visible') and @body.is('[contenteditable]')
@body.blur()
else
@body.find('textarea:visible').blur()
hidePopover: ()->
@el.find('.simditor-popover').each (i, popover) ->
popover = $(popover).data('popover')
popover.hide() if popover.active
destroy: ->
@triggerHandler 'destroy'
@textarea.closest('form')
.off('.simditor .simditor-' + @id)
@selection.clear()
@inputManager.focused = false
@textarea.insertBefore(@el)
.hide()
.val('')
.removeData 'simditor'
@el.remove()
$(document).off '.simditor-' + @id
$(window).off '.simditor-' + @id
@off()