Skip to content

Commit dda8a8e

Browse files
pranav-jindal-wingifytohosaku
authored andcommitted
feat: add support for dom element in template functions
1 parent 56352be commit dda8a8e

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ Collection object shown with defaults:
144144
// class added to each list item
145145
itemClass: '',
146146

147-
// function called on select that returns the content to insert
147+
// function called on select that returns the content to insert (return dom string or dom node)
148148
selectTemplate: function (item) {
149149
return '@' + item.original.value;
150150
},
151151

152-
// template for displaying item in menu
152+
// template for displaying item in menu (return dom string or dom node)
153153
menuItemTemplate: function (item) {
154154
return item.string;
155155
},

src/Tribute.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,14 @@ class Tribute {
395395
if (this.menuSelected === index) {
396396
li.classList.add(this.current.collection.selectClass);
397397
}
398-
li.innerHTML = this.current.collection.menuItemTemplate(item);
398+
// remove all content in the li and append the content of menuItemTemplate
399+
const menuItemDomOrString = this.current.collection.menuItemTemplate(item);
400+
if (menuItemDomOrString instanceof Element) {
401+
li.innerHTML = "";
402+
li.appendChild(menuItemDomOrString);
403+
} else {
404+
li.innerHTML = menuItemDomOrString;
405+
}
399406
fragment.appendChild(li);
400407
});
401408
ul.appendChild(fragment);

src/TributeRange.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ class TributeRange {
138138
let textSuffix = typeof this.tribute.replaceTextSuffix == 'string'
139139
? this.tribute.replaceTextSuffix
140140
: '\xA0'
141-
text += textSuffix
141+
if(text instanceof HTMLElement) {
142+
// skip adding suffix yet - TODO later
143+
// text.appendChild(this.getDocument().createTextNode(textSuffix))
144+
} else {
145+
text += textSuffix
146+
}
142147
let endPos = info.mentionPosition + info.mentionText.length
143148
if (!this.tribute.autocompleteMode) {
144149
endPos += info.mentionTriggerChar.length
@@ -151,7 +156,7 @@ class TributeRange {
151156
}
152157
}
153158

154-
pasteHtml(html, startPos, endPos) {
159+
pasteHtml(htmlOrElem, startPos, endPos) {
155160
let range, sel
156161
sel = this.getWindowSelection()
157162
range = this.getDocument().createRange()
@@ -160,7 +165,11 @@ class TributeRange {
160165
range.deleteContents()
161166

162167
let el = this.getDocument().createElement('div')
163-
el.innerHTML = html
168+
if(htmlOrElem instanceof HTMLElement) {
169+
el.appendChild(htmlOrElem)
170+
} else {
171+
el.innerHTML = htmlOrElem
172+
}
164173
let frag = this.getDocument().createDocumentFragment(),
165174
node, lastNode
166175
while ((node = el.firstChild)) {
@@ -487,7 +496,7 @@ class TributeRange {
487496
div.appendChild(span0)
488497

489498
if (element.nodeName === 'INPUT') {
490-
div.textContent = div.textContent.replace(/\s/g, ' ')
499+
div.textContent = div.textContent.replace(/\s/g, ' ')
491500
}
492501

493502
//Create a span in the div that represents where the cursor

tributejs.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export type TributeCollection<T extends {}> = {
3232
itemClass?: string;
3333

3434
// function called on select that returns the content to insert
35-
selectTemplate?: (item: TributeItem<T>|undefined) => string;
35+
selectTemplate?: (item: TributeItem<T>|undefined) => string | HTMLElement;
3636

3737
// template for displaying item in menu
38-
menuItemTemplate?: (item: TributeItem<T>) => string;
38+
menuItemTemplate?: (item: TributeItem<T>) => string | HTMLElement;
3939

4040
// template for when no match is found (optional),
4141
// If no template is provided, menu is hidden.
@@ -94,7 +94,7 @@ export type TributeCollection<T extends {}> = {
9494
closeOnScroll?: any
9595
};
9696

97-
export type TributeOptions<T> =
97+
export type TributeOptions<T extends {}> =
9898
| TributeCollection<T>
9999
| {
100100
// pass an array of config objects

0 commit comments

Comments
 (0)