Skip to content

Commit

Permalink
fix(getTriggerInfo): handle leading space (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
radotzki authored and mrsweaters committed Jan 20, 2017
1 parent b205e09 commit 41c4c65
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/TributeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ class TributeRange {

this.tribute.collection.forEach(config => {
let c = config.trigger
let idx = effectiveRange.lastIndexOf(c)
let idx = config.requireLeadingSpace ?
this.lastIndexWithLeadingSpace(effectiveRange, c) :
effectiveRange.lastIndexOf(c)

if (idx > mostRecentTriggerCharPos) {
mostRecentTriggerCharPos = idx
Expand Down Expand Up @@ -303,6 +305,24 @@ class TributeRange {
}
}

lastIndexWithLeadingSpace (str, char) {
let reversedStr = str.split('').reverse().join('')
let index = -1

for (let cidx = 0, len = str.length; cidx < len; cidx++) {
let firstChar = cidx === str.length - 1
let leadingSpace = /\s/.test(reversedStr[cidx + 1])
let match = char === reversedStr[cidx]

if (match && (firstChar || leadingSpace)) {
index = str.length - 1 - cidx
break
}
}

return index
}

isContentEditable(element) {
return element.nodeName !== 'INPUT' && element.nodeName !== 'TEXTAREA'
}
Expand Down

0 comments on commit 41c4c65

Please sign in to comment.