Merge branch 'fix/autocomplete-fixes' into 'develop'

Rewrite word split for autocomplete, fix #930 fix #681

Closes #681 and #930

See merge request pleroma/pleroma-fe!1218
This commit is contained in:
lain 2020-09-03 14:08:36 +00:00
commit 4adccf2216
3 changed files with 59 additions and 39 deletions

View file

@ -5,9 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased] ## [Unreleased]
## [Unreleased patch] ## [Unreleased patch]
### Changed ### Changed
- Polls will be hidden with status content if "Collapse posts with subjects" is enabled and the post is collapsed. - Polls will be hidden with status content if "Collapse posts with subjects" is enabled and the post is collapsed.
### Fixed
- Autocomplete won't stop at the second @, so it'll still work with "@lain@l" and not start over.
- Fixed weird autocomplete behavior when you write ":custom_emoji: ?"
## [2.1.0] - 2020-08-28 ## [2.1.0] - 2020-08-28
### Added ### Added
- Autocomplete domains from list of known instances - Autocomplete domains from list of known instances

View file

@ -5,7 +5,7 @@ export const replaceWord = (str, toReplace, replacement) => {
} }
export const wordAtPosition = (str, pos) => { export const wordAtPosition = (str, pos) => {
const words = splitIntoWords(str) const words = splitByWhitespaceBoundary(str)
const wordsWithPosition = addPositionToWords(words) const wordsWithPosition = addPositionToWords(words)
return find(wordsWithPosition, ({ start, end }) => start <= pos && end > pos) return find(wordsWithPosition, ({ start, end }) => start <= pos && end > pos)
@ -34,36 +34,36 @@ export const addPositionToWords = (words) => {
}, []) }, [])
} }
export const splitIntoWords = (str) => { export const splitByWhitespaceBoundary = (str) => {
// Split at word boundaries let result = []
const regex = /\b/ let currentWord = ''
const triggers = /[@#:]+$/ for (let i = 0; i < str.length; i++) {
const currentChar = str[i]
let split = str.split(regex) // Starting a new word
if (!currentWord) {
// Add trailing @ and # to the following word. currentWord = currentChar
const words = reduce(split, (result, word) => { continue
if (result.length > 0) {
let previous = result.pop()
const matches = previous.match(triggers)
if (matches) {
previous = previous.replace(triggers, '')
word = matches[0] + word
}
result.push(previous)
} }
result.push(word) // current character is whitespace while word isn't, or vice versa:
// add our current word to results, start over the current word.
return result if (!!currentChar.trim() !== !!currentWord.trim()) {
}, []) result.push(currentWord)
currentWord = currentChar
return words continue
}
currentWord += currentChar
}
// Add the last word we were working on
if (currentWord) {
result.push(currentWord)
}
return result
} }
const completion = { const completion = {
wordAtPosition, wordAtPosition,
addPositionToWords, addPositionToWords,
splitIntoWords, splitByWhitespaceBoundary,
replaceWord replaceWord
} }

View file

@ -1,8 +1,8 @@
import { replaceWord, addPositionToWords, wordAtPosition, splitIntoWords } from '../../../../../src/services/completion/completion.js' import { replaceWord, addPositionToWords, wordAtPosition, splitByWhitespaceBoundary } from '../../../../../src/services/completion/completion.js'
describe('addPositiontoWords', () => { describe('addPositiontoWords', () => {
it('adds the position to a word list', () => { it('adds the position to a word list', () => {
const words = ['hey', 'this', 'is', 'fun'] const words = ['hey', ' ', 'this', ' ', 'is', ' ', 'fun']
const expected = [ const expected = [
{ {
@ -11,19 +11,34 @@ describe('addPositiontoWords', () => {
end: 3 end: 3
}, },
{ {
word: 'this', word: ' ',
start: 3, start: 3,
end: 7 end: 4
}, },
{ {
word: 'is', word: 'this',
start: 7, start: 4,
end: 8
},
{
word: ' ',
start: 8,
end: 9 end: 9
}, },
{ {
word: 'fun', word: 'is',
start: 9, start: 9,
end: 11
},
{
word: ' ',
start: 11,
end: 12 end: 12
},
{
word: 'fun',
start: 12,
end: 15
} }
] ]
@ -33,11 +48,11 @@ describe('addPositiontoWords', () => {
}) })
}) })
describe('splitIntoWords', () => { describe('splitByWhitespaceBoundary', () => {
it('splits at whitespace boundaries', () => { it('splits at whitespace boundaries', () => {
const str = 'This is a #nice @test for you, @idiot.' const str = 'This is a #nice @test for you, @idiot@idiot.com'
const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you', ', ', '@idiot', '.'] const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you,', ' ', '@idiot@idiot.com']
const res = splitIntoWords(str) const res = splitByWhitespaceBoundary(str)
expect(res).to.eql(expected) expect(res).to.eql(expected)
}) })
@ -57,13 +72,13 @@ describe('wordAtPosition', () => {
describe('replaceWord', () => { describe('replaceWord', () => {
it('replaces a word (with start and end) with another word in a given string', () => { it('replaces a word (with start and end) with another word in a given string', () => {
const str = 'hey @take, how are you' const str = 'hey @take , how are you'
const wordsWithPosition = addPositionToWords(splitIntoWords(str)) const wordsWithPosition = addPositionToWords(splitByWhitespaceBoundary(str))
const toReplace = wordsWithPosition[2] const toReplace = wordsWithPosition[2]
expect(toReplace.word).to.eql('@take') expect(toReplace.word).to.eql('@take')
const expected = 'hey @takeshitakenji, how are you' const expected = 'hey @takeshitakenji , how are you'
const res = replaceWord(str, toReplace, '@takeshitakenji') const res = replaceWord(str, toReplace, '@takeshitakenji')
expect(res).to.eql(expected) expect(res).to.eql(expected)
}) })