Skip to content

Commit

Permalink
Pass original event that triggered text replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Humphreys committed Aug 18, 2017
1 parent dc89154 commit 1b7e45b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ You can bind to the `tribute-replaced` event to know when we have updated your t
If your element has an ID of `myElement`:
```js
document.getElementById('myElement').addEventListener('tribute-replaced', function (e) {
console.log('Text replaced!');
console.log('Text replaced with:', e.detail.text);
console.log('Original event that triggered text replacement:', e.detail.event);
});
```

Expand Down
21 changes: 12 additions & 9 deletions dist/tribute.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ <h5>Tribute on traditional form elements!</h5>
tributeMultipleTriggers.attach(document.getElementById('testMultiple'));

document.getElementById('test').addEventListener('tribute-replaced', function (e) {
console.log('Text replaced by Tribute!');
console.log('Text replaced by Tribute!', e.detail.text);
console.log('Original Event:', e.detail.event);
});

document.getElementById('test').addEventListener('tribute-no-match', function (e) {
Expand Down
8 changes: 4 additions & 4 deletions src/Tribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,16 @@ class Tribute {
}
}

selectItemAtIndex(index) {
selectItemAtIndex(index, originalEvent) {
index = parseInt(index)
if (typeof index !== 'number') return
let item = this.current.filteredItems[index]
let content = this.current.collection.selectTemplate(item)
this.replaceText(content)
this.replaceText(content, originalEvent)
}

replaceText(content) {
this.range.replaceTriggerText(content, true, true)
replaceText(content, originalEvent) {
this.range.replaceTriggerText(content, true, true, originalEvent)
}

_append(collection, newValues, replace) {
Expand Down
4 changes: 2 additions & 2 deletions src/TributeEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TributeEvents {
throw new Error('cannot find the <li> container for the click')
}
}
tribute.selectItemAtIndex(li.getAttribute('data-index'))
tribute.selectItemAtIndex(li.getAttribute('data-index'), event)
tribute.hideMenu()
} else if (tribute.current.element) {
tribute.hideMenu()
Expand Down Expand Up @@ -161,7 +161,7 @@ class TributeEvents {
e.preventDefault()
e.stopPropagation()
setTimeout(() => {
this.tribute.selectItemAtIndex(this.tribute.menuSelected)
this.tribute.selectItemAtIndex(this.tribute.menuSelected, e)
this.tribute.hideMenu()
}, 0)
}
Expand Down
7 changes: 5 additions & 2 deletions src/TributeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ class TributeRange {
}
}

replaceTriggerText(text, requireLeadingSpace, hasTrailingSpace) {
replaceTriggerText(text, requireLeadingSpace, hasTrailingSpace, originalEvent) {
let context = this.tribute.current
this.resetSelection(context.element, context.selectedPath, context.selectedOffset)

let info = this.getTriggerInfo(true, hasTrailingSpace, requireLeadingSpace, this.tribute.allowSpaces)

// Create the event
let replaceEvent = new CustomEvent('tribute-replaced', {
detail: text
detail: {
text: text,
event: originalEvent
}
})

if (info !== undefined) {
Expand Down

0 comments on commit 1b7e45b

Please sign in to comment.