diff --git a/src/app/components/create-status/create-status.component.spec.ts b/src/app/components/create-status/create-status.component.spec.ts index 26a1c223..4cf64310 100644 --- a/src/app/components/create-status/create-status.component.spec.ts +++ b/src/app/components/create-status/create-status.component.spec.ts @@ -223,4 +223,60 @@ describe('CreateStatusComponent', () => { const result = (component).tranformHtmlRepliesToReplies(pleromaMention); expect(result).toBe('

test @sengi_app@pleroma.site qsdqds qsd qsd qsd q @test@pleroma.site @no

'); }); + + it('should autocomplete - at the end', () => { + let text = 'data @sengi'; + let pattern = '@sengi'; + let autosuggest = '@sengi@mastodon.social'; + + const result = (component).replacePatternWithAutosuggest(text, pattern, autosuggest); + expect(result).toBe('data @sengi@mastodon.social '); + }); + + it('should autocomplete - at the start', () => { + let text = '@sengi data'; + let pattern = '@sengi'; + let autosuggest = '@sengi@mastodon.social'; + + const result = (component).replacePatternWithAutosuggest(text, pattern, autosuggest); + expect(result).toBe('@sengi@mastodon.social data'); + }); + + it('should autocomplete - at the middle', () => { + let text = 'data @sengi data'; + let pattern = '@sengi'; + let autosuggest = '@sengi@mastodon.social'; + + const result = (component).replacePatternWithAutosuggest(text, pattern, autosuggest); + expect(result).toBe('data @sengi@mastodon.social data'); + }); + + it('should autocomplete - duplicate', () => { + let text = 'data @sengi @sengi2 data'; + let pattern = '@sengi'; + let autosuggest = '@sengi@mastodon.social'; + + const result = (component).replacePatternWithAutosuggest(text, pattern, autosuggest); + expect(result).toBe('data @sengi@mastodon.social @sengi2 data'); + }); + + it('should autocomplete - duplicate 2', () => { + let text = 'data @sengi2 @sengi data'; + let pattern = '@sengi'; + let autosuggest = '@sengi@mastodon.social'; + + const result = (component).replacePatternWithAutosuggest(text, pattern, autosuggest); + expect(result).toBe('data @sengi2 @sengi@mastodon.social data'); + }); + + it('should autocomplete - new lines', () => { + const newLine = String.fromCharCode(13, 10); + let text = `@sengi${newLine}${newLine}data`; + let pattern = '@sengi'; + let autosuggest = '@sengi@mastodon.social'; + + const result = (component).replacePatternWithAutosuggest(text, pattern, autosuggest); + expect(result).toBe(`@sengi@mastodon.social${newLine}${newLine}data`); + }); + }); \ No newline at end of file diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index 0877ad55..15d92f7f 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -222,7 +222,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { // if (state && state !== '') { // this.status = state; // } else { - if(!this.status || this.status === '') { + if (!this.status || this.status === '') { const uniqueMentions = this.getMentions(this.statusReplyingTo, this.statusReplyingToWrapper.provider); for (const mention of uniqueMentions) { this.status += `@${mention} `; @@ -242,8 +242,8 @@ export class CreateStatusComponent implements OnInit, OnDestroy { this.innerHeight = window.innerHeight; } - ngOnDestroy() { - if(this.isRedrafting){ + ngOnDestroy() { + if (this.isRedrafting) { this.statusStateService.resetStatusContent(null); } @@ -577,7 +577,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy { this.scheduledStatusService.statusAdded(acc); } - if(this.isRedrafting){ + if (this.isRedrafting) { this.statusStateService.resetStatusContent(null); } else { this.statusStateService.resetStatusContent(this.statusReplyingToWrapper); @@ -685,12 +685,17 @@ export class CreateStatusComponent implements OnInit, OnDestroy { suggestionSelected(selection: AutosuggestSelection) { if (this.status.includes(selection.pattern)) { + // let transformedStatus = this.status; + // transformedStatus = transformedStatus.replace(new RegExp(` ${selection.pattern} `), ` ${selection.autosuggest} `).replace(' ', ' '); + // transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern} `), `${selection.autosuggest} `).replace(' ', ' '); + // transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern}$`), `${selection.autosuggest} `).replace(' ', ' '); - let transformedStatus = this.status; - transformedStatus = transformedStatus.replace(new RegExp(` ${selection.pattern} `), ` ${selection.autosuggest} `).replace(' ', ' '); - transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern} `), `${selection.autosuggest} `).replace(' ', ' '); - transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern}$`), `${selection.autosuggest} `).replace(' ', ' '); - this.status = transformedStatus; + // const newLine = String.fromCharCode(13, 10); + // transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern}${newLine}`), `${selection.autosuggest}${newLine}`).replace(' ', ' '); + + // this.status = transformedStatus; + + this.status = this.replacePatternWithAutosuggest(this.status, selection.pattern, selection.autosuggest); let newCaretPosition = this.status.indexOf(`${selection.autosuggest} `) + selection.autosuggest.length + 1; if (newCaretPosition > this.status.length) newCaretPosition = this.status.length; @@ -708,6 +713,22 @@ export class CreateStatusComponent implements OnInit, OnDestroy { } } + private replacePatternWithAutosuggest(status: string, pattern: string, autosuggest: string): string { + status = status.replace(new RegExp(` ${pattern} `), ` ${autosuggest} `).replace(' ', ' '); + 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); + // status = status.replace(new RegExp(`${pattern}${newLine}`), `${autosuggest}${newLine}`).replace(' ', ' '); + + //status = status.replace(new RegExp(`${pattern}$`, 'i'), `${autosuggest} `).replace(' ', ' '); + //status = status.replace(new RegExp(`/^${pattern}$/`, 'i'), `${autosuggest} `).replace(' ', ' '); + //status = status.replace(new RegExp(pattern, 'i'), `${autosuggest} `).replace(' ', ' '); + + return status; + } + suggestionsChanged(hasSuggestions: boolean) { this.hasSuggestions = hasSuggestions; }