better autocomplete replacement
This commit is contained in:
parent
b0b3c4ec21
commit
c8c17ca44e
|
@ -279,4 +279,14 @@ describe('CreateStatusComponent', () => {
|
||||||
expect(result).toBe(`@sengi@mastodon.social${newLine}${newLine}data`);
|
expect(result).toBe(`@sengi@mastodon.social${newLine}${newLine}data`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should autocomplete - new lines 2', () => {
|
||||||
|
const newLine = String.fromCharCode(13, 10);
|
||||||
|
let text = `@nicolasconstant\n\ndata`;
|
||||||
|
let pattern = '@nicolasconstant';
|
||||||
|
let autosuggest = '@nicolasconstant@social.nicolas-constant.com';
|
||||||
|
|
||||||
|
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
|
||||||
|
expect(result).toBe(`@nicolasconstant@social.nicolas-constant.com${newLine}${newLine}data`);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
|
@ -697,6 +697,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
|
||||||
|
|
||||||
this.status = this.replacePatternWithAutosuggest(this.status, selection.pattern, selection.autosuggest);
|
this.status = this.replacePatternWithAutosuggest(this.status, selection.pattern, selection.autosuggest);
|
||||||
|
|
||||||
|
|
||||||
let newCaretPosition = this.status.indexOf(`${selection.autosuggest} `) + selection.autosuggest.length + 1;
|
let newCaretPosition = this.status.indexOf(`${selection.autosuggest} `) + selection.autosuggest.length + 1;
|
||||||
if (newCaretPosition > this.status.length) newCaretPosition = this.status.length;
|
if (newCaretPosition > this.status.length) newCaretPosition = this.status.length;
|
||||||
|
|
||||||
|
@ -714,19 +715,57 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
|
||||||
}
|
}
|
||||||
|
|
||||||
private replacePatternWithAutosuggest(status: string, pattern: string, autosuggest: string): string {
|
private replacePatternWithAutosuggest(status: string, pattern: string, autosuggest: string): string {
|
||||||
status = status.replace(new RegExp(` ${pattern} `), ` ${autosuggest} `).replace(' ', ' ');
|
status = status.replace(/ /g, ' ');
|
||||||
status = status.replace(new RegExp(`${pattern} `), `${autosuggest} `).replace(' ', ' ');
|
|
||||||
status = status.replace(new RegExp(`${pattern}$`), `${autosuggest} `).replace(' ', ' ');
|
|
||||||
//status = status.replace(/$pattern$/, `${autosuggest} `).replace(' ', ' ');
|
|
||||||
|
|
||||||
// const newLine = String.fromCharCode(13, 10);
|
const newLine = String.fromCharCode(13, 10);
|
||||||
// status = status.replace(new RegExp(`${pattern}${newLine}`), `${autosuggest}${newLine}`).replace(' ', ' ');
|
// let statusPerLines = status.split(newLine);
|
||||||
|
let statusPerLines = status.split(/\r?\n/);
|
||||||
|
let statusPerLinesPerWords: string[][] = [];
|
||||||
|
let regex = new RegExp(`^${pattern}$`, 'i');
|
||||||
|
|
||||||
//status = status.replace(new RegExp(`${pattern}$`, 'i'), `${autosuggest} `).replace(' ', ' ');
|
statusPerLines.forEach(line => {
|
||||||
//status = status.replace(new RegExp(`/^${pattern}$/`, 'i'), `${autosuggest} `).replace(' ', ' ');
|
let words = line.split(' ');
|
||||||
//status = status.replace(new RegExp(pattern, 'i'), `${autosuggest} `).replace(' ', ' ');
|
|
||||||
|
|
||||||
return status;
|
words = words.map(word => {
|
||||||
|
console.warn(`+${word}+`);
|
||||||
|
return word.replace(regex, `${autosuggest}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(words);
|
||||||
|
|
||||||
|
statusPerLinesPerWords.push(words);
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = '';
|
||||||
|
let nberLines = statusPerLinesPerWords.length;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
statusPerLinesPerWords.forEach(line => {
|
||||||
|
i++;
|
||||||
|
|
||||||
|
let wordCount = line.length;
|
||||||
|
let w = 0;
|
||||||
|
line.forEach(word => {
|
||||||
|
w++;
|
||||||
|
result += `${word}`;
|
||||||
|
|
||||||
|
if(w < wordCount || i === nberLines){
|
||||||
|
result += ' ';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (i < nberLines) {
|
||||||
|
result += newLine;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
result = result.replace(' ', ' ');
|
||||||
|
|
||||||
|
let endRegex = new RegExp(`${autosuggest} $`, 'i');
|
||||||
|
if(!result.match(endRegex)){
|
||||||
|
result = result.substring(0, result.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
suggestionsChanged(hasSuggestions: boolean) {
|
suggestionsChanged(hasSuggestions: boolean) {
|
||||||
|
|
Loading…
Reference in New Issue