forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.coffee
187 lines (147 loc) · 4.34 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
class Simditor extends Widget
@connect Util
@connect UndoManager
@connect InputManager
@connect Keystroke
@connect Formatter
@connect Selection
@connect Toolbar
@count: 0
opts:
textarea: null
placeholder: ''
defaultImage: 'images/image.png'
params: {}
upload: false
tabIndent: true
_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 @opts.upload and simple?.uploader
uploadOpts = if typeof @opts.upload == 'object' then @opts.upload else {}
@uploader = simple.uploader(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 'pluginconnected', =>
if @opts.placeholder
@on 'valuechanged', =>
@_placeholder()
@setValue @textarea.val() || ''
# Disable the resizing of `img` and `table`
if @util.browser.mozilla
document.execCommand "enableObjectResizing", false, false
document.execCommand "enableInlineTableEditing", false, false
_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.append(@textarea)
.data 'simditor', this
@textarea.data('simditor', this)
.hide()
.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 @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 (children.data('indent') ? 0) < 1)
@placeholderEl.show()
else
@placeholderEl.hide()
setValue: (val) ->
@hidePopover()
@textarea.val val
@body.html val
@formatter.format()
@formatter.decorate()
setTimeout =>
@trigger 'valuechanged'
, 0
getValue: () ->
@sync()
sync: ->
@hidePopover
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: ->
$blockEl = @body.find('p, li, pre, h1, h2, h3, h4, td').first()
return unless $blockEl.length > 0
range = document.createRange()
@selection.setRangeAtStartOf $blockEl, range
@body.focus()
blur: ->
@body.blur()
hidePopover: ->
@wrapper.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()
@textarea.insertBefore(@el)
.hide()
.val('')
.removeData 'simditor'
@el.remove()
$(document).off '.simditor-' + @id
$(window).off '.simditor-' + @id
@off()
window.Simditor = Simditor
class TestPlugin extends Plugin
class Test extends Widget
@connect TestPlugin