commit
3785a863cb
@ -0,0 +1,22 @@
|
||||
const UserFinder = {
|
||||
data: () => ({
|
||||
username: undefined,
|
||||
hidden: true
|
||||
}),
|
||||
methods: {
|
||||
findUser (username) {
|
||||
this.$store.state.api.backendInteractor.externalProfile(username)
|
||||
.then((user) => {
|
||||
if (!user.error) {
|
||||
this.$store.commit('addNewUsers', [user])
|
||||
this.$router.push({name: 'user-profile', params: {id: user.id}})
|
||||
}
|
||||
})
|
||||
},
|
||||
toggleHidden () {
|
||||
this.hidden = !this.hidden
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default UserFinder
|
@ -0,0 +1,23 @@
|
||||
<template>
|
||||
<a href="#" v-if="hidden"><i class="icon-user-plus user-finder-icon" @click.prevent="toggleHidden"/></a>
|
||||
<span v-else>
|
||||
<input class="user-finder-input base03-border" @keyup.enter="findUser(username)" v-model="username" placeholder="Find user" id="user-finder-input" type="text"/>
|
||||
<i class="icon-cancel user-finder-icon" @click="toggleHidden"/>
|
||||
</span>
|
||||
</template>
|
||||
|
||||
<script src="./user_finder.js"></script>
|
||||
|
||||
<style lang="scss">
|
||||
.user-finder-icon {
|
||||
margin-right: 0.25em;
|
||||
}
|
||||
|
||||
.user-finder-input {
|
||||
border-width: 1px;
|
||||
border-style: solid;
|
||||
border-color: inherit;
|
||||
border-radius: 5px;
|
||||
padding: 0.1em 0.2em 0.2em 0.2em;
|
||||
}
|
||||
</style>
|
@ -0,0 +1,70 @@
|
||||
import { reduce, find } from 'lodash'
|
||||
|
||||
export const replaceWord = (str, toReplace, replacement) => {
|
||||
return str.slice(0, toReplace.start) + replacement + str.slice(toReplace.end)
|
||||
}
|
||||
|
||||
export const wordAtPosition = (str, pos) => {
|
||||
const words = splitIntoWords(str)
|
||||
const wordsWithPosition = addPositionToWords(words)
|
||||
|
||||
return find(wordsWithPosition, ({start, end}) => start <= pos && end > pos)
|
||||
}
|
||||
|
||||
export const addPositionToWords = (words) => {
|
||||
return reduce(words, (result, word) => {
|
||||
const data = {
|
||||
word,
|
||||
start: 0,
|
||||
end: word.length
|
||||
}
|
||||
|
||||
if (result.length > 0) {
|
||||
const previous = result.pop()
|
||||
|
||||
data.start += previous.end
|
||||
data.end += previous.end
|
||||
|
||||
result.push(previous)
|
||||
}
|
||||
|
||||
result.push(data)
|
||||
|
||||
return result
|
||||
}, [])
|
||||
}
|
||||
|
||||
export const splitIntoWords = (str) => {
|
||||
// Split at word boundaries
|
||||
const regex = /\b/
|
||||
const triggers = /[@#]+$/
|
||||
|
||||
let split = str.split(regex)
|
||||
|
||||
// Add trailing @ and # to the following word.
|
||||
const words = reduce(split, (result, word) => {
|
||||
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)
|
||||
|
||||
return result
|
||||
}, [])
|
||||
|
||||
return words
|
||||
}
|
||||
|
||||
const completion = {
|
||||
wordAtPosition,
|
||||
addPositionToWords,
|
||||
splitIntoWords,
|
||||
replaceWord
|
||||
}
|
||||
|
||||
export default completion
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Before Width: | Height: | Size: 5.6 KiB After Width: | Height: | Size: 6.5 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,10 @@
|
||||
[
|
||||
"now",
|
||||
["%ss", "%ss"],
|
||||
["%smin", "%smin"],
|
||||
["%sh", "%sh"],
|
||||
["%sd", "%sd"],
|
||||
["%sw", "%sw"],
|
||||
["%sm", "%sm"],
|
||||
["%sy", "%sy"]
|
||||
]
|
@ -0,0 +1,70 @@
|
||||
import { replaceWord, addPositionToWords, wordAtPosition, splitIntoWords } from '../../../../../src/services/completion/completion.js'
|
||||
|
||||
describe('addPositiontoWords', () => {
|
||||
it('adds the position to a word list', () => {
|
||||
const words = ['hey', 'this', 'is', 'fun']
|
||||
|
||||
const expected = [
|
||||
{
|
||||
word: 'hey',
|
||||
start: 0,
|
||||
end: 3
|
||||
},
|
||||
{
|
||||
word: 'this',
|
||||
start: 3,
|
||||
end: 7
|
||||
},
|
||||
{
|
||||
word: 'is',
|
||||
start: 7,
|
||||
end: 9
|
||||
},
|
||||
{
|
||||
word: 'fun',
|
||||
start: 9,
|
||||
end: 12
|
||||
}
|
||||
]
|
||||
|
||||
const res = addPositionToWords(words)
|
||||
|
||||
expect(res).to.eql(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('splitIntoWords', () => {
|
||||
it('splits at whitespace boundaries', () => {
|
||||
const str = 'This is a #nice @test for you, @idiot.'
|
||||
const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you', ', ', '@idiot', '.']
|
||||
const res = splitIntoWords(str)
|
||||
|
||||
expect(res).to.eql(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('wordAtPosition', () => {
|
||||
it('returns the word for a given string and postion, plus the start and end position of that word', () => {
|
||||
const str = 'Hey this is fun'
|
||||
|
||||
const { word, start, end } = wordAtPosition(str, 4)
|
||||
|
||||
expect(word).to.eql('this')
|
||||
expect(start).to.eql(4)
|
||||
expect(end).to.eql(8)
|
||||
})
|
||||
})
|
||||
|
||||
describe('replaceWord', () => {
|
||||
it('replaces a word (with start and end) with another word in a given string', () => {
|
||||
const str = 'hey @take, how are you'
|
||||
const wordsWithPosition = addPositionToWords(splitIntoWords(str))
|
||||
const toReplace = wordsWithPosition[2]
|
||||
|
||||
expect(toReplace.word).to.eql('@take')
|
||||
|
||||
const expected = 'hey @takeshitakenji, how are you'
|
||||
const res = replaceWord(str, toReplace, '@takeshitakenji')
|
||||
expect(res).to.eql(expected)
|
||||
})
|
||||
})
|
Loading…
Reference in new issue