-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composer: mention tagging now works in middle of text (close #105)
- Loading branch information
Showing
3 changed files
with
144 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
getMentionAt, | ||
insertMentionAt, | ||
} from '../../../src/lib/strings/mention-manip' | ||
|
||
describe('getMentionAt', () => { | ||
type Case = [string, number, string | undefined] | ||
const cases: Case[] = [ | ||
['hello @alice goodbye', 0, undefined], | ||
['hello @alice goodbye', 1, undefined], | ||
['hello @alice goodbye', 2, undefined], | ||
['hello @alice goodbye', 3, undefined], | ||
['hello @alice goodbye', 4, undefined], | ||
['hello @alice goodbye', 5, undefined], | ||
['hello @alice goodbye', 6, 'alice'], | ||
['hello @alice goodbye', 7, 'alice'], | ||
['hello @alice goodbye', 8, 'alice'], | ||
['hello @alice goodbye', 9, 'alice'], | ||
['hello @alice goodbye', 10, 'alice'], | ||
['hello @alice goodbye', 11, 'alice'], | ||
['hello @alice goodbye', 12, 'alice'], | ||
['hello @alice goodbye', 13, undefined], | ||
['hello @alice goodbye', 14, undefined], | ||
['@alice', 0, 'alice'], | ||
['@alice hello', 0, 'alice'], | ||
['@alice hello', 1, 'alice'], | ||
['@alice hello', 2, 'alice'], | ||
['@alice hello', 3, 'alice'], | ||
['@alice hello', 4, 'alice'], | ||
['@alice hello', 5, 'alice'], | ||
['@alice hello', 6, 'alice'], | ||
['@alice hello', 7, undefined], | ||
['alice@alice', 0, undefined], | ||
['alice@alice', 6, undefined], | ||
] | ||
|
||
it.each(cases)( | ||
'given input string %p and cursor position %p, returns %p', | ||
(str, cursorPos, expected) => { | ||
const output = getMentionAt(str, cursorPos) | ||
expect(output?.value).toEqual(expected) | ||
}, | ||
) | ||
}) | ||
|
||
describe('insertMentionAt', () => { | ||
type Case = [string, number, string] | ||
const cases: Case[] = [ | ||
['hello @alice goodbye', 0, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 1, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 2, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 3, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 4, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 5, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 6, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 7, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 8, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 9, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 10, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 11, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 12, 'hello @alice.com goodbye'], | ||
['hello @alice goodbye', 13, 'hello @alice goodbye'], | ||
['hello @alice goodbye', 14, 'hello @alice goodbye'], | ||
['@alice', 0, '@alice.com '], | ||
['@alice hello', 0, '@alice.com hello'], | ||
['@alice hello', 1, '@alice.com hello'], | ||
['@alice hello', 2, '@alice.com hello'], | ||
['@alice hello', 3, '@alice.com hello'], | ||
['@alice hello', 4, '@alice.com hello'], | ||
['@alice hello', 5, '@alice.com hello'], | ||
['@alice hello', 6, '@alice.com hello'], | ||
['@alice hello', 7, '@alice hello'], | ||
['alice@alice', 0, 'alice@alice'], | ||
['alice@alice', 6, 'alice@alice'], | ||
] | ||
|
||
it.each(cases)( | ||
'given input string %p and cursor position %p, returns %p', | ||
(str, cursorPos, expected) => { | ||
const output = insertMentionAt(str, cursorPos, 'alice.com') | ||
expect(output).toEqual(expected) | ||
}, | ||
) | ||
}) |
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,37 @@ | ||
interface FoundMention { | ||
value: string | ||
index: number | ||
} | ||
|
||
export function getMentionAt( | ||
text: string, | ||
cursorPos: number, | ||
): FoundMention | undefined { | ||
let re = /(^|\s)@([a-z0-9.]*)/gi | ||
let match | ||
while ((match = re.exec(text))) { | ||
const spaceOffset = match[1].length | ||
const index = match.index + spaceOffset | ||
if ( | ||
cursorPos >= index && | ||
cursorPos <= index + match[0].length - spaceOffset | ||
) { | ||
return {value: match[2], index} | ||
} | ||
} | ||
return undefined | ||
} | ||
|
||
export function insertMentionAt( | ||
text: string, | ||
cursorPos: number, | ||
mention: string, | ||
) { | ||
const target = getMentionAt(text, cursorPos) | ||
if (target) { | ||
return `${text.slice(0, target.index)}@${mention} ${text.slice( | ||
target.index + target.value.length + 1, // add 1 to include the "@" | ||
)}` | ||
} | ||
return text | ||
} |
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