Skip to content

Commit

Permalink
Emoji picker inserts emojis and can be closed
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Dec 5, 2023
1 parent 8538d8a commit 4390e3a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 21 deletions.
26 changes: 15 additions & 11 deletions resources/js/components/forms/editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export default class VueEditor extends Vue {
}
}
insertImage(href, title) {
insertImage(href: string, title: string) {
this.editor!.insertImage(href, title);
}
insertLink(href, title) {
insertLink(href: string, title: string) {
this.editor!.insertLink(href, title);
}
Expand All @@ -87,35 +87,35 @@ export default class VueEditor extends Vue {
this.editor!.putStrikeThrough();
}
insertBlockQuote(placeholder) {
insertBlockQuote(placeholder: string) {
this.editor!.putBlockQuote(placeholder);
}
makeLink(placeholder) {
makeLink(placeholder: string) {
this.editor!.putLink(placeholder);
}
makeImage(placeholder) {
makeImage(placeholder: string) {
this.editor!.putImage(placeholder);
}
makeKeyNotation(key) {
makeKeyNotation(key: string) {
this.editor!.putKey(key);
}
appendBlockQuote(content) {
appendBlockQuote(content: string) {
this.editor!.appendBlockQuote(content);
}
appendUserMention(username) {
appendUserMention(username: string) {
this.editor!.appendUserMention(username);
}
insertListOrdered(placeholder) {
insertListOrdered(placeholder: string) {
this.editor!.putListOrdered(placeholder);
}
insertListUnordered(placeholder) {
insertListUnordered(placeholder: string) {
this.editor!.putListUnordered(placeholder);
}
Expand All @@ -131,10 +131,14 @@ export default class VueEditor extends Vue {
this.editor!.indentLess();
}
addTable(header, placeholder) {
addTable(header: string, placeholder: string) {
this.editor!.putTable(index => header + ' ' + (index + 1), placeholder);
}
insertEmoji(emojiName: string) {
this.editor!.insertEmoji(emojiName);
}
focus() {
this.editor!.focus();
}
Expand Down
28 changes: 25 additions & 3 deletions resources/js/components/forms/emoji-picker.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="emoji-picker">
<div class="emoji-picker" v-on-clickaway="blur">
<div class="card card-body">
<div style="display:flex; flex-direction:column;">
<div class="emoji-grid">
Expand All @@ -15,7 +15,8 @@
:src="url(emojis.emoticons[code])"
:title="emojis.emoticons[code].name"
:alt="emojis.emoticons[code].native"
@mouseover="mouseOver(emojis.emoticons[code])"/>
@mouseover="mouseOver(emojis.emoticons[code])"
@click="select(code)"/>
</template>
</div>
</div>
Expand All @@ -37,7 +38,7 @@

<div class="search-box">
<input class="form-control" v-model="searchPhrase" placeholder="Wyszukaj emoji..."/>
<button class="btn btn-primary" type="button">
<button class="btn btn-primary" type="button" ref="close-button" @click="close">
<i class="fas fa-times"/>
Zamknij
</button>
Expand All @@ -48,7 +49,10 @@
</template>

<script>
import {mixin as clickAway} from 'vue-clickaway';
export default {
mixins: [clickAway],
props: {
emojis: {require: true},
},
Expand All @@ -59,6 +63,17 @@ export default {
};
},
methods: {
select(emoji) {
this.$emit('input', emoji);
},
blur(blurEvent) {
if (this.isDesktop()) {
this.$emit('blur', blurEvent);
}
},
close(closeEvent) {
this.$emit('close', closeEvent);
},
url(emoji) {
return 'https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/svg/' + emoji.unified + '.svg';
},
Expand All @@ -76,6 +91,9 @@ export default {
}
return false;
},
clearSearchPhrase() {
this.searchPhrase = '';
},
keywords(emoji) {
if (emoji) {
return emoji.keywords.map(keyword => '"' + keyword + '"').join(", ");
Expand All @@ -87,6 +105,10 @@ export default {
},
mouseLeave() {
this.emoji = null;
},
isDesktop() {
const style = window.getComputedStyle(this.$refs["close-button"]);
return style.display === 'none';
}
},
computed: {
Expand Down
57 changes: 50 additions & 7 deletions resources/js/components/forms/markdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@

<div v-show="isPreview" v-html="previewHtml" class="preview post-content"/>

<div class="emoji-picker-container">
<div v-if="emojiPickerOpen" class="emoji-picker-container">
<div class="triangle"/>
<vue-emoji-picker :emojis="emojis"/>
<vue-emoji-picker
ref="emoji-picker"
:emojis="emojis"
@input="insertEmoji"
@blur="blurEmojiPicker"
@close="closeEmojiPicker"/>
</div>
</div>

Expand Down Expand Up @@ -132,6 +137,7 @@ export default class VueMarkdown extends Vue {
isProcessing = false;
progress = 0;
tabs: string[] = [CONTENT, PREVIEW];
emojiPickerOpen = false;
buttons = {
bold: {
click: this.makeBold,
Expand Down Expand Up @@ -232,8 +238,7 @@ export default class VueMarkdown extends Vue {
icon: 'fa-indent'
},
insertEmoji: {
click: () => {
},
click: this.toggleEmojiPicker,
can: false,
title: 'Dodaj emotikonę',
break: 'Dodanie tutaj emotikony mogłoby spowodować uszkodzenie składni',
Expand All @@ -247,6 +252,9 @@ export default class VueMarkdown extends Vue {
@Ref('search')
readonly search!: HTMLInputElement;
@Ref('emoji-picker')
readonly emojiPicker!: any;
@Prop()
value!: string;
Expand Down Expand Up @@ -298,9 +306,9 @@ export default class VueMarkdown extends Vue {
insertAssetAtCaret(asset: Asset) {
if (isImage(asset.name!)) {
this.editor.insertImage(asset.url, asset.name);
this.editor.insertImage(asset.url, asset.name!);
} else {
this.editor.insertLink(asset.url, asset.name);
this.editor.insertLink(asset.url, asset.name!);
}
}
Expand All @@ -326,7 +334,7 @@ export default class VueMarkdown extends Vue {
this.buttons.key.can = state.canKey;
this.buttons.indentMore.can = state.canIndent;
this.buttons.indentLess.can = state.canIndent;
this.buttons.insertEmoji.can = true;
this.buttons.insertEmoji.can = state.canEmoji;
}
autocomplete(nick: string) {
Expand Down Expand Up @@ -413,6 +421,10 @@ export default class VueMarkdown extends Vue {
this.editor.indentLess()
}
insertEmoji(emoji: string) {
this.editor.insertEmoji(emoji);
}
appendBlockQuote(username: string, postId: number, content: string) {
const title = username + ' napisał(a)';
const href = '/Forum/' + postId;
Expand All @@ -424,6 +436,37 @@ export default class VueMarkdown extends Vue {
this.editor.appendUserMention(username);
}
toggleEmojiPicker() {
this.emojiPickerOpen = !this.emojiPickerOpen;
this.$nextTick(() => {
if (this.emojiPicker) {
this.emojiPicker.clearSearchPhrase();
}
});
}
blurEmojiPicker(event: Event) {
if (!this.isEmojiToggleControl(event.target as HTMLElement)) {
this.closeEmojiPicker();
}
}
closeEmojiPicker() {
this.emojiPickerOpen = false;
}
isEmojiToggleControl(element: HTMLElement) {
if (element.title === 'Dodaj emotikonę') {
return true;
}
if (element.parentElement) {
if (element.parentElement.title === 'Dodaj emotikonę') {
return true;
}
}
return false;
}
focus() {
this.editor.focus();
}
Expand Down

0 comments on commit 4390e3a

Please sign in to comment.