Skip to content

Commit

Permalink
Return matched item in tribute-replaced event.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Humphreys committed Aug 18, 2017
1 parent aabdfa4 commit b42e970
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,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 with:', e.detail.text);
console.log('Original event that triggered text replacement:', e.detail.event);
console.log('Matched item:', e.detail.item);
});
```

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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +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!', e.detail.text);
console.log('Original Event:', e.detail.event);
console.log('Matched item:', e.detail.item);
});

document.getElementById('test').addEventListener('tribute-no-match', function (e) {
Expand Down
6 changes: 3 additions & 3 deletions src/Tribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ class Tribute {
if (typeof index !== 'number') return
let item = this.current.filteredItems[index]
let content = this.current.collection.selectTemplate(item)
this.replaceText(content, originalEvent)
this.replaceText(content, originalEvent, item)
}

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

_append(collection, newValues, replace) {
Expand Down
4 changes: 2 additions & 2 deletions src/TributeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TributeRange {
}
}

replaceTriggerText(text, requireLeadingSpace, hasTrailingSpace, originalEvent) {
replaceTriggerText(text, requireLeadingSpace, hasTrailingSpace, originalEvent, item) {
let context = this.tribute.current
// TODO: this may not be necessary anymore as we are using mouseup instead of click
// this.resetSelection(context.element, context.selectedPath, context.selectedOffset)
Expand All @@ -102,7 +102,7 @@ class TributeRange {
// Create the event
let replaceEvent = new CustomEvent('tribute-replaced', {
detail: {
text: text,
item: item,
event: originalEvent
}
})
Expand Down

0 comments on commit b42e970

Please sign in to comment.