-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP feat: add fuzzy search to popup menu and search pad
- Loading branch information
1 parent
a248bfe
commit cb6565f
Showing
6 changed files
with
188 additions
and
1 deletion.
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
39 changes: 39 additions & 0 deletions
39
lib/base/features/fuzzy-search/FuzzySearchPopupMenuProvider.js
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,39 @@ | ||
import Fuse from 'fuse.js/basic'; | ||
|
||
const options = { | ||
includeScore: true, | ||
ignoreLocation: true, | ||
includeMatches: true, | ||
threshold: 0.25, | ||
keys: [ | ||
{ | ||
name: 'label', | ||
weight: 3 | ||
}, | ||
{ | ||
name: 'description', | ||
weight: 2 | ||
}, | ||
'search' | ||
] | ||
}; | ||
|
||
export default class FuzzySearchPopupMenuProvider { | ||
constructor(popupMenu) { | ||
popupMenu.registerProvider('bpmn-append', this); | ||
popupMenu.registerProvider('bpmn-create', this); | ||
popupMenu.registerProvider('bpmn-replace', this); | ||
} | ||
|
||
getPopupMenuEntries() { | ||
return {}; | ||
} | ||
|
||
findPopupMenuEntries(entries, pattern) { | ||
const fuse = new Fuse(entries, options); | ||
|
||
const result = fuse.search(pattern); | ||
|
||
return result.map(({ item }) => item); | ||
} | ||
} |
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,122 @@ | ||
import Fuse from 'fuse.js/basic'; | ||
|
||
import { | ||
getLabel, | ||
isLabel | ||
} from 'bpmn-js/lib/util/LabelUtil'; | ||
|
||
const options = { | ||
includeScore: true, | ||
ignoreLocation: true, | ||
includeMatches: true, | ||
threshold: 0.25, | ||
keys: [ | ||
{ | ||
name: 'label', | ||
weight: 2 | ||
}, | ||
'id' | ||
] | ||
}; | ||
|
||
export default function BpmnSearchProvider(canvas, elementRegistry, searchPad) { | ||
this._canvas = canvas; | ||
this._elementRegistry = elementRegistry; | ||
|
||
searchPad.registerProvider(this); | ||
} | ||
|
||
BpmnSearchProvider.$inject = [ | ||
'canvas', | ||
'elementRegistry', | ||
'searchPad' | ||
]; | ||
|
||
/** | ||
* @param {string} pattern | ||
* | ||
* @return {SearchResult[]} | ||
*/ | ||
BpmnSearchProvider.prototype.find = function(pattern) { | ||
var rootElements = this._canvas.getRootElements(); | ||
|
||
var elements = this._elementRegistry | ||
.filter(function(element) { | ||
return !isLabel(element) && !rootElements.includes(element); | ||
}) | ||
.map(function(element) { | ||
return { | ||
element, | ||
id: element.id, | ||
label: getLabel(element) | ||
}; | ||
}); | ||
|
||
const fuse = new Fuse(elements, options); | ||
|
||
const result = fuse.search(pattern); | ||
|
||
return result.map(({ item }) => { | ||
const { element } = item; | ||
|
||
return { | ||
element, | ||
primaryTokens: highlightSubstring(getLabel(element), pattern), | ||
secondaryTokens: highlightSubstring(element.id, pattern) | ||
}; | ||
}); | ||
}; | ||
|
||
function highlightSubstring(string, substring) { | ||
if (!substring.length) return [ { normal: string } ]; | ||
|
||
const occurances = findAllSubstringOccurrences(string, substring); | ||
|
||
if (!occurances.length) return [ { normal: string } ]; | ||
|
||
let lastIndexEnd = 0; | ||
|
||
const tokens = []; | ||
|
||
occurances.forEach((start, index) => { | ||
const end = start + substring.length; | ||
|
||
if (start !== 0) { | ||
tokens.push({ | ||
normal: string.slice(lastIndexEnd, start) | ||
}); | ||
} | ||
|
||
tokens.push({ | ||
matched: string.slice(start, end) | ||
}); | ||
|
||
if (index === occurances.length - 1 && end !== string.length - 1) { | ||
tokens.push({ | ||
normal: string.slice(end) | ||
}); | ||
} | ||
|
||
lastIndexEnd = end; | ||
}); | ||
|
||
return tokens; | ||
} | ||
|
||
function findAllSubstringOccurrences(string, subString) { | ||
let indices = []; | ||
let startIndex = 0; | ||
let index; | ||
|
||
while ( | ||
(index = string | ||
.toLowerCase() | ||
.indexOf(subString.toLowerCase(), startIndex)) > -1 | ||
) { | ||
indices.push(index); | ||
|
||
startIndex = index + 1; | ||
} | ||
|
||
return indices; | ||
} |
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,8 @@ | ||
import FuzzySearchProvider from './FuzzySearchProvider'; | ||
import FuzzySearchPopupMenuProvider from './FuzzySearchPopupMenuProvider'; | ||
|
||
export default { | ||
__init__: [ 'bpmnSearch', 'fuzzySearchPopupMenuProvider' ], | ||
bpmnSearch: [ 'type', FuzzySearchProvider ], | ||
fuzzySearchPopupMenuProvider: [ 'type', FuzzySearchPopupMenuProvider ] | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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