From e88a568a99e3da9f2c7e48a4a4804d29e44a2fa2 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 4 Oct 2019 23:05:36 -0400 Subject: [PATCH] fix autosuggest --- .../autosuggest/autosuggest.component.ts | 1 + .../create-status/create-status.component.ts | 45 ++++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/app/components/create-status/autosuggest/autosuggest.component.ts b/src/app/components/create-status/autosuggest/autosuggest.component.ts index cd4097fd..4dd404d3 100644 --- a/src/app/components/create-status/autosuggest/autosuggest.component.ts +++ b/src/app/components/create-status/autosuggest/autosuggest.component.ts @@ -29,6 +29,7 @@ export class AutosuggestComponent implements OnInit, OnDestroy { this._pattern = value; this.analysePattern(value); } else { + this._pattern = null; this.accounts.length = 0; this.hashtags.length = 0; } diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index 40e99007..b379f0fc 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -223,23 +223,50 @@ export class CreateStatusComponent implements OnInit, OnDestroy { if (!this.statusLoaded) return; const caretPosition = this.replyElement.nativeElement.selectionStart; - const word = this.getWordByPos(status, caretPosition); - if (word && word.length > 0 && (word.startsWith('@') || word.startsWith('#'))) { + + const lastChar = status.substr(caretPosition - 1, 1); + const lastCharIsSpace = lastChar === ' '; + + const splitedStatus = status.split(/(\r\n|\n|\r)/); + let offset = 0; + let currentSection = ''; + for(let x of splitedStatus){ + const sectionLength = [...x].length; + if(offset + sectionLength >= caretPosition){ + currentSection = x; + break; + } else { + offset += sectionLength; + } + }; + + const word = this.getWordByPos(currentSection, caretPosition - offset); + if (!lastCharIsSpace && word && word.length > 0 && (word.startsWith('@') || word.startsWith('#'))) { this.autosuggestData = word; return; } this.autosuggestData = null; + this.hasSuggestions = false; } private getWordByPos(str, pos) { - str = str.replace(/(\r\n|\n|\r)/gm, ""); - var left = str.substr(0, pos); - var right = str.substr(pos); + var preText = str.substring(0, pos); + if (preText.indexOf(" ") > 0) { + var words = preText.split(" "); + return words[words.length - 1]; //return last word + } + else { + return preText; + } - left = left.replace(/^.+ /g, ""); - right = right.replace(/ .+$/g, ""); + // // str = str.replace(/(\r\n|\n|\r)/gm, ""); + // var left = str.substr(0, pos); + // var right = str.substr(pos); - return left + right; + // left = left.replace(/^.+ /g, ""); + // right = right.replace(/ .+$/g, ""); + + // return left + right; } private focus(caretPos = null) { @@ -477,7 +504,6 @@ export class CreateStatusComponent implements OnInit, OnDestroy { return false; } - private sendStatus(account: AccountInfo, status: string, visibility: VisibilityEnum, title: string, previousStatus: Status, attachments: Attachment[], poll: PollParameters, scheduledAt: string): Promise { let parsedStatus = this.parseStatus(status); let resultPromise = Promise.resolve(previousStatus); @@ -567,7 +593,6 @@ export class CreateStatusComponent implements OnInit, OnDestroy { return status.split(' ').filter(x => x.indexOf('@') === 0 && x.length > 1); } - suggestionSelected(selection: AutosuggestSelection) { if (this.status.includes(selection.pattern)) {