forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.coffee
137 lines (108 loc) · 3.62 KB
/
link.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
class LinkButton extends Button
name: 'link'
icon: 'link'
htmlTag: 'a'
disableTag: 'pre'
render: (args...) ->
super args...
@popover = new LinkPopover
button: @
_status: ->
super()
if @active and [email protected](@node)
@popover.show @node
else
@popover.hide()
command: ->
range = @editor.selection.range()
if @active
txtNode = document.createTextNode @node.text()
@node.replaceWith txtNode
range.selectNode txtNode
else
$contents = $(range.extractContents())
linkText = @editor.formatter.clearHtml($contents.contents(), false)
$link = $('<a/>', {
href: 'http://www.example.com',
target: '_blank',
text: linkText || @_t('linkText')
})
if @editor.selection.blockNodes().length > 0
range.insertNode $link[0]
else
$newBlock = $('<p/>').append($link)
range.insertNode $newBlock[0]
range.selectNodeContents $link[0]
@popover.one 'popovershow', =>
if linkText
@popover.urlEl.focus()
@popover.urlEl[0].select()
else
@popover.textEl.focus()
@popover.textEl[0].select()
@editor.selection.range range
@editor.trigger 'valuechanged'
class LinkPopover extends Popover
render: ->
tpl = """
<div class="link-settings">
<div class="settings-field">
<label>#{ @_t 'linkText' }</label>
<input class="link-text" type="text"/>
<a class="btn-unlink" href="javascript:;" title="#{ @_t 'removeLink' }"
tabindex="-1">
<span class="simditor-icon simditor-icon-unlink"></span>
</a>
</div>
<div class="settings-field">
<label>#{ @_t 'linkUrl' }</label>
<input class="link-url" type="text"/>
</div>
<div class="settings-field">
<label>#{ @_t 'linkTarget'}</label>
<select class="link-target">
<option value="_blank">#{ @_t 'openLinkInNewWindow' } (_blank)</option>
<option value="_self">#{ @_t 'openLinkInCurrentWindow' } (_self)</option>
</select>
</div>
</div>
"""
@el.addClass('link-popover')
.append(tpl)
@textEl = @el.find '.link-text'
@urlEl = @el.find '.link-url'
@unlinkEl = @el.find '.btn-unlink'
@selectTarget = @el.find '.link-target'
@textEl.on 'keyup', (e) =>
return if e.which == 13
@target.text @textEl.val()
@editor.inputManager.throttledValueChanged()
@urlEl.on 'keyup', (e) =>
return if e.which == 13
val = @urlEl.val()
val = 'http://' + val unless /https?:\/\/|^\//ig.test(val) or !val
@target.attr 'href', val
@editor.inputManager.throttledValueChanged()
$([@urlEl[0], @textEl[0]]).on 'keydown', (e) =>
if e.which == 13 or e.which == 27 or
(!e.shiftKey and e.which == 9 and $(e.target).hasClass('link-url'))
e.preventDefault()
range = document.createRange()
@editor.selection.setRangeAfter @target, range
@hide()
@editor.inputManager.throttledValueChanged()
@unlinkEl.on 'click', (e) =>
txtNode = document.createTextNode @target.text()
@target.replaceWith txtNode
@hide()
range = document.createRange()
@editor.selection.setRangeAfter txtNode, range
@editor.inputManager.throttledValueChanged()
@selectTarget.on 'change', (e) =>
@target.attr 'target', @selectTarget.val()
@editor.inputManager.throttledValueChanged()
show: (args...) ->
super args...
@textEl.val @target.text()
@urlEl.val @target.attr('href')
Simditor.Toolbar.addButton LinkButton