From: Dessalines Date: Fri, 24 Jan 2020 18:59:50 +0000 (-0500) Subject: Adding autocomplete to post, community, message, and site forms. Fixes #453 X-Git-Url: http://these/git/%7BprofileRss%7D?a=commitdiff_plain;h=608856b6cdd42b83f35eee8cb556ac626e75727f;p=lemmy.git Adding autocomplete to post, community, message, and site forms. Fixes #453 --- diff --git a/ui/src/components/comment-form.tsx b/ui/src/components/comment-form.tsx index b8ea0a5a..f4eb1181 100644 --- a/ui/src/components/comment-form.tsx +++ b/ui/src/components/comment-form.tsx @@ -2,28 +2,20 @@ import { Component, linkEvent } from 'inferno'; import { CommentNode as CommentNodeI, CommentForm as CommentFormI, - SearchForm, - SearchType, - SortType, - UserOperation, - SearchResponse, } from '../interfaces'; -import { Subscription } from 'rxjs'; import { - wsJsonToRes, capitalizeFirstLetter, - mentionDropdownFetchLimit, mdToHtml, randomStr, markdownHelpUrl, toast, + setupTribute, } from '../utils'; import { WebSocketService, UserService } from '../services'; import autosize from 'autosize'; +import Tribute from 'tributejs/src/Tribute.js'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; -import Tribute from 'tributejs/src/Tribute.js'; -import emojiShortName from 'emoji-short-name'; interface CommentFormProps { postId?: number; @@ -42,9 +34,7 @@ interface CommentFormState { export class CommentForm extends Component { private id = `comment-form-${randomStr()}`; - private userSub: Subscription; - private communitySub: Subscription; - private tribute: any; + private tribute: Tribute; private emptyState: CommentFormState = { commentForm: { auth: null, @@ -68,55 +58,7 @@ export class CommentForm extends Component { constructor(props: any, context: any) { super(props, context); - this.tribute = new Tribute({ - collection: [ - // Emojis - { - trigger: ':', - menuItemTemplate: (item: any) => { - let emoji = `:${item.original.key}:`; - return `${item.original.val} ${emoji}`; - }, - selectTemplate: (item: any) => { - return `:${item.original.key}:`; - }, - values: Object.entries(emojiShortName).map(e => { - return { key: e[1], val: e[0] }; - }), - allowSpaces: false, - autocompleteMode: true, - menuItemLimit: mentionDropdownFetchLimit, - }, - // Users - { - trigger: '@', - selectTemplate: (item: any) => { - return `[/u/${item.original.key}](/u/${item.original.key})`; - }, - values: (text: string, cb: any) => { - this.userSearch(text, (users: any) => cb(users)); - }, - allowSpaces: false, - autocompleteMode: true, - menuItemLimit: mentionDropdownFetchLimit, - }, - - // Communities - { - trigger: '#', - selectTemplate: (item: any) => { - return `[/c/${item.original.key}](/c/${item.original.key})`; - }, - values: (text: string, cb: any) => { - this.communitySearch(text, (communities: any) => cb(communities)); - }, - allowSpaces: false, - autocompleteMode: true, - menuItemLimit: mentionDropdownFetchLimit, - }, - ], - }); - + this.tribute = setupTribute(); this.state = this.emptyState; if (this.props.node) { @@ -297,68 +239,4 @@ export class CommentForm extends Component { toast(error, 'danger'); }); } - - userSearch(text: string, cb: any) { - if (text) { - let form: SearchForm = { - q: text, - type_: SearchType[SearchType.Users], - sort: SortType[SortType.TopAll], - page: 1, - limit: mentionDropdownFetchLimit, - }; - - WebSocketService.Instance.search(form); - - this.userSub = WebSocketService.Instance.subject.subscribe( - msg => { - let res = wsJsonToRes(msg); - if (res.op == UserOperation.Search) { - let data = res.data as SearchResponse; - let users = data.users.map(u => { - return { key: u.name }; - }); - cb(users); - this.userSub.unsubscribe(); - } - }, - err => console.error(err), - () => console.log('complete') - ); - } else { - cb([]); - } - } - - communitySearch(text: string, cb: any) { - if (text) { - let form: SearchForm = { - q: text, - type_: SearchType[SearchType.Communities], - sort: SortType[SortType.TopAll], - page: 1, - limit: mentionDropdownFetchLimit, - }; - - WebSocketService.Instance.search(form); - - this.communitySub = WebSocketService.Instance.subject.subscribe( - msg => { - let res = wsJsonToRes(msg); - if (res.op == UserOperation.Search) { - let data = res.data as SearchResponse; - let communities = data.communities.map(u => { - return { key: u.name }; - }); - cb(communities); - this.communitySub.unsubscribe(); - } - }, - err => console.error(err), - () => console.log('complete') - ); - } else { - cb([]); - } - } } diff --git a/ui/src/components/community-form.tsx b/ui/src/components/community-form.tsx index 4dc7bfcb..33c63c89 100644 --- a/ui/src/components/community-form.tsx +++ b/ui/src/components/community-form.tsx @@ -11,7 +11,14 @@ import { WebSocketJsonResponse, } from '../interfaces'; import { WebSocketService } from '../services'; -import { wsJsonToRes, capitalizeFirstLetter, toast } from '../utils'; +import { + wsJsonToRes, + capitalizeFirstLetter, + toast, + randomStr, + setupTribute, +} from '../utils'; +import Tribute from 'tributejs/src/Tribute.js'; import autosize from 'autosize'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -36,6 +43,8 @@ export class CommunityForm extends Component< CommunityFormProps, CommunityFormState > { + private id = `community-form-${randomStr()}`; + private tribute: Tribute; private subscription: Subscription; private emptyState: CommunityFormState = { @@ -53,6 +62,7 @@ export class CommunityForm extends Component< constructor(props: any, context: any) { super(props, context); + this.tribute = setupTribute(); this.state = this.emptyState; if (this.props.community) { @@ -80,7 +90,14 @@ export class CommunityForm extends Component< } componentDidMount() { - autosize(document.querySelectorAll('textarea')); + var textarea: any = document.getElementById(this.id); + autosize(textarea); + this.tribute.attach(textarea); + textarea.addEventListener('tribute-replaced', () => { + this.state.communityForm.description = textarea.value; + this.setState(this.state); + autosize.update(textarea); + }); } componentWillUnmount() { @@ -130,6 +147,7 @@ export class CommunityForm extends Component<