Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:quilljs/quill into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jan 11, 2015
2 parents f8277d6 + 2ebf1d4 commit c60ba5f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
37 changes: 34 additions & 3 deletions src/modules/link-tooltip.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ class LinkTooltip extends Tooltip
<input class="input" type="text">
<span>&nbsp;&#45;&nbsp;</span>
<a href="javascript:;" class="change">Change</a>
<a href="javascript:;" class="remove">Remove</a>
<a href="javascript:;" class="done">Done</a>'

@hotkeys:
LINK: { key: 'K', metaKey: true }

constructor: (@quill, @options) ->
@options = _.defaults(@options, Tooltip.DEFAULTS)
super(@quill, @options)
Expand All @@ -35,13 +39,20 @@ class LinkTooltip extends Tooltip
this.hide()
)
dom(@container.querySelector('.done')).on('click', _.bind(this.saveLink, this))
dom(@container.querySelector('.remove')).on('click', =>
this.removeLink(@range)
)
dom(@container.querySelector('.change')).on('click', =>
this.setMode(@link.href, true)
)
this.initTextbox(@textbox, this.saveLink, this.hide)
@quill.onModuleLoad('toolbar', (toolbar) =>
@toolbar = toolbar
toolbar.initFormat('link', _.bind(this._onToolbar, this))
)
@quill.onModuleLoad('keyboard', (keyboard) =>
keyboard.addHotkey(LinkTooltip.hotkeys.LINK, _.bind(this._onKeyboard, this))
)

saveLink: ->
url = this._normalizeURL(@textbox.value)
Expand All @@ -53,6 +64,13 @@ class LinkTooltip extends Tooltip
@quill.formatText(@range, 'link', url, 'user')
this.setMode(url, false)

removeLink: (range) ->
# Expand range to the entire leaf
if range.isCollapsed()
range = this._expandRange(range)
@quill.formatText(range, 'link', false, 'user')
@toolbar.setActive('link', false) if @toolbar?

setMode: (url, edit = false) ->
if edit
@textbox.value = url
Expand All @@ -75,14 +93,27 @@ class LinkTooltip extends Tooltip
node = node.parentNode
return null

_expandRange: (range) ->
[leaf, offset] = @quill.editor.doc.findLeafAt(range.start, true)
start = range.start - offset
end = start + leaf.length
return { start, end }

_onToolbar: (range, value) ->
return unless range and !range.isCollapsed()
if value
this._toggle(range, value)

_onKeyboard: ->
range = @quill.getSelection()
this._toggle(range, true)

_toggle: (range, value) ->
return unless range
if value and !range.isCollapsed()
this.setMode(this._suggestURL(range), true)
nativeRange = @quill.editor.selection._getNativeRange()
this.show(nativeRange)
else
@quill.formatText(range, 'link', false, 'user')
this.removeLink(range)

_normalizeURL: (url) ->
url = 'http://' + url unless /^(https?:\/\/|mailto:)/.test(url)
Expand Down
1 change: 1 addition & 0 deletions src/quill.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class Quill extends EventEmitter2
this.setContents(delta, source)

updateContents: (delta, source = Quill.sources.API) ->
delta = { ops: delta } if Array.isArray(delta)
@editor.applyDelta(delta, source)

# fn(Number start, Number end, String name, String value, String source)
Expand Down
4 changes: 3 additions & 1 deletion src/themes/base/modules/link-tooltip.styl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
width: 170px
input.input, a.done
display: none
a.change
margin-right: 4px
.ql-link-tooltip.editing
input.input, a.done
display: inline-block
a.url, a.change
a.url, a.change, a.remove
display: none

0 comments on commit c60ba5f

Please sign in to comment.