2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 03:58:26 +00:00

incr: make Link fuzzy matchable

This commit is contained in:
18alantom 2022-05-05 17:00:09 +05:30
parent bd6f110553
commit 0f2d1284b2
2 changed files with 32 additions and 18 deletions

View File

@ -2,6 +2,7 @@
import { t } from 'fyo'; import { t } from 'fyo';
import Badge from 'src/components/Badge.vue'; import Badge from 'src/components/Badge.vue';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import { fuzzyMatch } from 'src/utils';
import { markRaw } from 'vue'; import { markRaw } from 'vue';
import AutoComplete from './AutoComplete.vue'; import AutoComplete from './AutoComplete.vue';
@ -9,6 +10,9 @@ export default {
name: 'Link', name: 'Link',
extends: AutoComplete, extends: AutoComplete,
emits: ['new-doc'], emits: ['new-doc'],
data() {
return { results: [] };
},
mounted() { mounted() {
if (this.value) { if (this.value) {
this.linkValue = this.value; this.linkValue = this.value;
@ -30,25 +34,21 @@ export default {
getTargetSchemaName() { getTargetSchemaName() {
return this.df.target; return this.df.target;
}, },
async getSuggestions(keyword = '') { async getOptions() {
const schemaName = this.getTargetSchemaName(); const schemaName = this.getTargetSchemaName();
if (!schemaName) { if (!schemaName) {
return []; return [];
} }
const schema = fyo.schemaMap[schemaName]; if (this.results?.length) {
const filters = await this.getFilters(keyword); return this.results;
if (keyword && !filters.keywords) {
filters[schema.titleField] = ['like', keyword];
} }
const schema = fyo.schemaMap[schemaName];
const filters = await this.getFilters();
const fields = [ const fields = [
...new Set([ ...new Set(['name', schema.titleField, this.df.groupBy]),
'name',
schema.titleField,
this.df.groupBy,
]),
].filter(Boolean); ].filter(Boolean);
const results = await fyo.db.getAll(schemaName, { const results = await fyo.db.getAll(schemaName, {
@ -56,7 +56,7 @@ export default {
fields, fields,
}); });
const suggestions = results return (this.results = results
.map((r) => { .map((r) => {
const option = { label: r[schema.titleField], value: r.name }; const option = { label: r[schema.titleField], value: r.name };
if (this.df.groupBy) { if (this.df.groupBy) {
@ -64,10 +64,21 @@ export default {
} }
return option; return option;
}) })
.concat(this.getCreateNewOption()) .filter(Boolean));
.filter(Boolean); },
async getSuggestions(keyword = '') {
let options = await this.getOptions();
if (suggestions.length === 0) { if (keyword) {
options = options
.map((item) => ({ ...fuzzyMatch(keyword, item.label), item }))
.filter(({ isMatch }) => isMatch)
.sort((a, b) => a.distance - b.distance)
.map(({ item }) => item);
}
options = options.concat(this.getCreateNewOption());
if (options.length === 0) {
return [ return [
{ {
component: markRaw({ component: markRaw({
@ -79,7 +90,7 @@ export default {
]; ];
} }
return suggestions; return options;
}, },
getCreateNewOption() { getCreateNewOption() {
return { return {

View File

@ -49,8 +49,11 @@ export function fuzzyMatch(keyword: string, candidate: string) {
let distance = 0; let distance = 0;
while (keywordLetter && candidateLetter) { while (keywordLetter && candidateLetter) {
if (keywordLetter.toLowerCase() === candidateLetter.toLowerCase()) { if (keywordLetter === candidateLetter) {
keywordLetter = keywordLetters.shift(); keywordLetter = keywordLetters.shift();
} else if (keywordLetter.toLowerCase() === candidateLetter.toLowerCase()) {
keywordLetter = keywordLetters.shift();
distance += 0.5;
} else { } else {
distance += 1; distance += 1;
} }
@ -93,4 +96,4 @@ export function getErrorMessage(e: Error, doc?: Doc): string {
} }
return errorMessage; return errorMessage;
} }