forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.coffee
152 lines (116 loc) · 3.91 KB
/
code.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
class CodeButton extends Button
name: 'code'
icon: 'code'
htmlTag: 'pre'
disableTag: 'li, table'
_init: ->
super()
@editor.on 'decorate', (e, $el) =>
$el.find('pre').each (i, pre) =>
@decorate $(pre)
@editor.on 'undecorate', (e, $el) =>
$el.find('pre').each (i, pre) =>
@undecorate $(pre)
render: (args...) ->
super args...
@popover = new CodePopover
button: @
status: ($node) ->
result = super $node
if @active
@popover.show($node)
else if @editor.util.isBlockNode($node)
@popover.hide()
result
decorate: ($pre) ->
lang = $pre.attr('data-lang')
$pre.removeClass()
$pre.addClass('lang-' + lang) if lang and lang != -1
undecorate: ($pre) ->
lang = $pre.attr('data-lang')
$pre.removeClass()
$pre.addClass('lang-' + lang) if lang and lang != -1
command: ->
range = @editor.selection.getRange()
startNode = range.startContainer
endNode = range.endContainer
$startBlock = @editor.util.closestBlockEl(startNode)
$endBlock = @editor.util.closestBlockEl(endNode)
range.setStartBefore $startBlock[0]
range.setEndAfter $endBlock[0]
$contents = $(range.extractContents())
results = []
$contents.children().each (i, el) =>
converted = @_convertEl el
for c in converted
if results.length and results[results.length - 1].is(@htmlTag) and c.is(@htmlTag)
results[results.length - 1].append(c.contents())
else
results.push(c)
range.insertNode node[0] for node in results.reverse()
@editor.selection.setRangeAtEndOf results[0]
@editor.trigger 'valuechanged'
_convertEl: (el) ->
$el = $(el)
results = []
if $el.is @htmlTag
block = $('<p/>').append($el.html().replace('\n', '<br/>'))
results.push block
else
if !$el.text() and $el.children().length == 1 and $el.children().is('br')
codeStr = '\n'
else
codeStr = @editor.formatter.clearHtml($el)
block = $('<' + @htmlTag + '/>').text(codeStr)
results.push(block)
results
class CodePopover extends Popover
_tpl: """
<div class="code-settings">
<div class="settings-field">
<select class="select-lang">
<option value="-1">选择程序语言</option>
<option value="bash">Bash</option>
<option value="c++">C++</option>
<option value="cs">C#</option>
<option value="css">CSS</option>
<option value="erlang">Erlang</option>
<option value="less">Less</option>
<option value="scss">Sass</option>
<option value="diff">Diff</option>
<option value="coffeeScript">CoffeeScript</option>
<option value="html">Html,XML</option>
<option value="json">JSON</option>
<option value="java">Java</option>
<option value="js">JavaScript</option>
<option value="markdown">Markdown</option>
<option value="oc">Objective C</option>
<option value="php">PHP</option>
<option value="perl">Perl</option>
<option value="python">Python</option>
<option value="ruby">Ruby</option>
<option value="sql">SQL</option>
</select>
</div>
</div>
"""
render: ->
@el.addClass('code-popover')
.append(@_tpl)
@selectEl = @el.find '.select-lang'
@selectEl.on 'change', (e) =>
@lang = @selectEl.val()
selected = @target.hasClass('selected')
@target.removeClass()
.removeAttr('data-lang')
if @lang isnt -1
@target.addClass('lang-' + @lang)
@target.attr('data-lang', @lang)
@target.addClass('selected') if selected
@editor.on 'valuechanged', (e) =>
@refresh() if @active
show: (args...) ->
super args...
@lang = @target.attr('data-lang')
if @lang? then @selectEl.val(@lang) else @selectEl.val(-1)
Simditor.Toolbar.addButton CodeButton