Skip to content

Commit

Permalink
fix: emoji picker crash (#26)
Browse files Browse the repository at this point in the history
修复 Emoji 选择组件无法正常初始化的问题。

原因是因为原来使用的 Emoji Mart 库可能会因为多次实例化导致异常,目前暂无解决方案,详情可查阅:missive/emoji-mart#812

目前改为使用 https://github.com/serebrov/emoji-mart-vue-test 代替。

Fixes #7
Ref #8 (comment)

```release-note
修复 Emoji 选择组件无法正常初始化的问题。
```
  • Loading branch information
ruibaby committed Mar 29, 2023
1 parent 6c4bf40 commit 895bf50
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 776 deletions.
3 changes: 1 addition & 2 deletions packages/comment-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
"./dist/style.css": "./dist/style.css"
},
"dependencies": {
"@emoji-mart/data": "^1.1.2",
"@halo-dev/api-client": "^2.3.0",
"@halo-dev/components": "^1.2.0",
"@vueuse/components": "8.9.4",
"@vueuse/core": "8.9.4",
"axios": "^0.27.2",
"emoji-mart": "^5.5.2",
"emoji-mart-vue-fast": "^12.0.1",
"lodash.clonedeep": "^4.5.0",
"qs": "^6.11.0",
"uuid": "^9.0.0"
Expand Down
43 changes: 15 additions & 28 deletions packages/comment-widget/src/components/Form.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script lang="ts" setup>
import { VButton, VAvatar } from "@halo-dev/components";
import LoginModal from "./LoginModal.vue";
import data from "@emoji-mart/data";
import i18n from "@emoji-mart/data/i18n/zh.json";
import MdiStickerEmoji from "~icons/mdi/sticker-emoji";
import MdiSendCircleOutline from "~icons/mdi/send-circle-outline";
import type {
Expand All @@ -13,11 +11,14 @@ import type {
User,
} from "@halo-dev/api-client";
// @ts-ignore
import { Picker } from "emoji-mart";
import { computed, inject, ref, watchEffect, type Ref } from "vue";
import { Picker, EmojiIndex } from "emoji-mart-vue-fast/src";
import data from "emoji-mart-vue-fast/data/all.json";
import "emoji-mart-vue-fast/css/emoji-mart.css";
import { inject, ref, watchEffect, type Ref } from "vue";
import { apiClient } from "@/utils/api-client";
import { useLocalStorage, useMagicKeys } from "@vueuse/core";
import axios from "axios";
import { onClickOutside } from "@vueuse/core";
interface CustomAccount {
displayName: string;
Expand All @@ -44,7 +45,6 @@ const currentUser = inject<Ref<User | undefined>>("currentUser");
const kind = inject<string>("kind");
const name = inject<string>("name");
const group = inject<string>("group");
const colorScheme = inject<string>("colorScheme");
const allowAnonymousComments = inject<Ref<boolean | undefined>>(
"allowAnonymousComments"
);
Expand Down Expand Up @@ -193,36 +193,19 @@ const handleLogout = async () => {
};
// Emoji picker
const getColorScheme = computed(() => {
if (colorScheme === "system") {
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
return colorScheme;
});
let emojiIndex = new EmojiIndex(data);
const emojiPickerRef = ref<HTMLElement | null>(null);
const contentInputRef = ref();
const emojiPickerVisible = ref(false);
const emojiPicker = new Picker({
data: data,
theme: getColorScheme.value,
autoFocus: true,
i18n: i18n,
onEmojiSelect: onEmojiSelect,
});
const emojiPickerRef = ref(null);
function onEmojiSelect(emoji: { native: string }) {
raw.value += emoji.native;
contentInputRef.value.focus();
}
watchEffect(() => {
if (emojiPickerRef.value) {
emojiPickerRef.value?.appendChild(emojiPicker);
}
onClickOutside(emojiPickerRef, () => {
emojiPickerVisible.value = false;
});
// KeyBoard shortcuts
Expand Down Expand Up @@ -297,7 +280,7 @@ watchEffect(() => {
</template>
</div>
<div class="flex flex-row items-center gap-3">
<div class="relative">
<div ref="emojiPickerRef" class="relative">
<MdiStickerEmoji
class="h-5 w-5 cursor-pointer text-gray-500 transition-all hover:text-gray-900 dark:text-slate-300 dark:hover:text-slate-50"
@click="emojiPickerVisible = !emojiPickerVisible"
Expand All @@ -314,7 +297,11 @@ watchEffect(() => {
v-show="emojiPickerVisible"
class="absolute right-0 z-10 mt-3 transform px-4 sm:px-0"
>
<div ref="emojiPickerRef"></div>
<Picker
:data="emojiIndex"
:native="true"
@select="onEmojiSelect"
/>
</div>
</transition>
</div>
Expand Down
35 changes: 25 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

755 changes: 20 additions & 735 deletions src/main/resources/static/comment-widget.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/resources/static/style.css

Large diffs are not rendered by default.

0 comments on commit 895bf50

Please sign in to comment.