-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #471 from FluxNotes/prioritize-suggestions-portal-…
…options Suggestion portals should prioritize options based on context and matching score
- Loading branch information
Showing
6 changed files
with
236 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import SuggestionPortalSearchIndex from './SuggestionPortalSearchIndex'; | ||
import Fuse from 'fuse.js'; | ||
import Lang from 'lodash'; | ||
|
||
class SuggestionPortalPlaceholderSearchIndex extends SuggestionPortalSearchIndex { | ||
constructor(list, initialChar, shortcutManager) { | ||
super(list, initialChar, shortcutManager) | ||
this.currentlyValidPlaceholders = []; | ||
} | ||
updateIndex = (contextManager) => { | ||
const placeholders = this.shortcutManager.getAllPlaceholderShortcuts(); | ||
// If shortcuts haven't updated, we don't need to update our fuse index | ||
if (Lang.isEqual(placeholders, this.currentlyValidPlaceholders)) return | ||
this.currentlyValidPlaceholders = placeholders; | ||
const relevantShortcuts = []; | ||
|
||
placeholders.forEach((placeholder) => { | ||
const triggers = this.shortcutManager.getTriggersForShortcut(placeholder.id); | ||
triggers.forEach((trigger) => { | ||
const triggerNoPrefix = trigger.name.substring(1); | ||
relevantShortcuts.push({ | ||
key: triggerNoPrefix, | ||
value: `${this.initialChar}${triggerNoPrefix}>`, | ||
suggestion: triggerNoPrefix, | ||
}); | ||
}); | ||
}); | ||
|
||
this.shortcutsFuse = new Fuse(relevantShortcuts, this.fuseOptions); | ||
} | ||
}; | ||
|
||
export default SuggestionPortalPlaceholderSearchIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import Fuse from 'fuse.js'; | ||
import Lang from 'lodash'; | ||
|
||
class SuggestionPortalSearchIndex { | ||
constructor(list, initialChar, shortcutManager) { | ||
this.initialChar = initialChar; | ||
this.shortcutManager = shortcutManager; | ||
// Metdata common to all suggestionSearchIndexs | ||
this.fuseOptions = { | ||
includeScore: true, | ||
includeMatches: true, | ||
threshold: 0.3, | ||
location: 0, | ||
distance: 100, | ||
maxPatternLength: 32, | ||
minMatchCharLength: 1, | ||
keys: [ | ||
"suggestion" | ||
] | ||
} | ||
this.shortcutsFuse = new Fuse([], this.fuseOptions); | ||
} | ||
|
||
// Takes a contextmanager and uses the current context to update it's current shortcutsFuse Index | ||
updateIndex = (contextManager) => { | ||
// Every kind of suggestion portal index is going to be different, so we don't have a common way of building an index. | ||
} | ||
|
||
sortSuggestionsAlphabetically = (a, b) => { | ||
if(a.data.score > b.data.score) { | ||
return 1; | ||
} | ||
if(a.data.score < b.data.score){ | ||
return -1; | ||
} | ||
if(a.suggestion.toLowerCase() > b.suggestion.toLowerCase()){ | ||
return 1; | ||
} | ||
if(a.suggestion.toLowerCase() < b.suggestion.toLowerCase()){ | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
search = (searchText) => { | ||
if (Lang.isUndefined(searchText)) return []; | ||
|
||
const maxLength = 25; | ||
const searchTextLowercase = searchText.toLowerCase(); | ||
let results = this.shortcutsFuse.search(searchTextLowercase); | ||
|
||
// If there are no results, if the searchText is empty, and if the list being searched on is nonempty | ||
// return a list of shortcutsFuseOptions formatted with this extra data field | ||
if (results.length === 0 && Lang.isEmpty(searchText)) { | ||
return this.shortcutsFuse.list.slice(0, maxLength).map((suggestionObj) => { | ||
suggestionObj.data = { | ||
score: 0.1, | ||
matches: [], | ||
} | ||
return suggestionObj | ||
}); | ||
} | ||
|
||
|
||
const resultFormatted = results.map((result) => { | ||
return { | ||
key: result.item.key, | ||
value: result.item.value, | ||
suggestion: result.item.suggestion, | ||
data: { | ||
// Use the bonus score to drag the most recent shortcuts to the top and weight the older ones to the bottom | ||
score: result.score + result.item.scoreBonusBasedOnContext, | ||
matches: result.matches, | ||
}, | ||
}; | ||
}).sort(this.sortSuggestionsAlphabetically).slice(0,maxLength);; | ||
|
||
return resultFormatted | ||
} | ||
} | ||
|
||
export default SuggestionPortalSearchIndex; |
Oops, something went wrong.