Skip to content

Commit 9edf11f

Browse files
authored
feat(emoji): @emoji-mart/data 、@floating-ui/dom 依赖外部化 (#364)
* feat(emoji): @emoji-mart/data 、@floating-ui/dom 依赖外部化 * feat(emoji): emojiData、EmojiPicker、emojiPickerPosition 支持通过 window 传入 * feat(emoji): options 默认参数
1 parent 0458b50 commit 9edf11f

4 files changed

Lines changed: 43 additions & 11 deletions

File tree

packages/docs/fluent-editor/demos/emoji.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<script setup lang="ts">
2+
import type { EmojiMartData } from '@emoji-mart/data'
23
import type FluentEditor from '@opentiny/fluent-editor'
4+
// 这里实际导入的是一个 json 文件,包含了 emoji-mart 所需的所有表情数据,类型是 EmojiMartData
5+
import data from '@emoji-mart/data'
6+
// computePosition 函数用于计算 emoji picker显示的位置,你可以根据需要自定义位置计算逻辑
7+
import { computePosition } from '@floating-ui/dom'
8+
import { Picker } from 'emoji-mart'
39
import { onMounted } from 'vue'
410
511
let editor: FluentEditor
@@ -21,7 +27,11 @@ onMounted(() => {
2127
theme: 'snow',
2228
modules: {
2329
toolbar: TOOLBAR_CONFIG,
24-
emoji: true,
30+
emoji: {
31+
emojiData: data as EmojiMartData,
32+
EmojiPicker: Picker,
33+
emojiPickerPosition: computePosition,
34+
},
2535
},
2636
})
2737
})

packages/docs/fluent-editor/docs/demo/emoji.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ npm i @floating-ui/dom @emoji-mart/data emoji-mart
1010

1111
## 集成示例
1212

13-
工具栏配置增加 `emoji`,modules 增加 `emoji` 为 true, 即可开始使用。
13+
工具栏配置增加 `emoji`,modules 增加 `emoji` 按照如下代码所示配置进行配置后即可使用
14+
15+
> `emojiData``EmojiPicker``emojiPickerPosition` 支持通过 window 传入
1416
1517
:::demo src=demos/emoji.vue
1618
:::
@@ -34,3 +36,5 @@ npm i @floating-ui/dom @emoji-mart/data emoji-mart
3436
| `dynamicWidth` | `boolean` | `false` | 是否根据容器宽度动态决定 `perLine` 的值。 |
3537

3638
> 💡 以上配置项最终会传递给 [`<EmojiPicker>`](https://github.com/missive/emoji-mart#-emoji-component) 组件,你可以根据项目实际需要进行覆盖。
39+
>
40+
> 更多配置可参考 [emoji-mart Options / Props](https://github.com/missive/emoji-mart#options--props)

packages/docs/types/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { EmojiMartData } from '@emoji-mart/data'
2+
import type { computePosition } from '@floating-ui/dom'
3+
import type { Picker } from 'emoji-mart'
14
import type hljs from 'highlight.js'
25
import type Html2Canvas from 'html2canvas'
36
import type katex from 'katex'
@@ -7,6 +10,9 @@ declare global {
710
hljs: typeof hljs
811
katex: typeof katex
912
Html2Canvas: typeof Html2Canvas
13+
emojiData: EmojiMartData
14+
EmojiPicker: new (props: any) => Picker
15+
emojiPickerPosition: typeof computePosition
1016
}
1117
}
1218

packages/fluent-editor/src/modules/emoji.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import type { EmojiMartData } from '@emoji-mart/data'
2+
import type { computePosition } from '@floating-ui/dom'
3+
import type { Picker } from 'emoji-mart'
14
import type TypeToolbar from 'quill/modules/toolbar'
25
import type FluentEditor from '../fluent-editor'
3-
import data from '@emoji-mart/data'
4-
import { computePosition } from '@floating-ui/dom'
5-
import { Picker } from 'emoji-mart'
66
import { debounce } from 'lodash-es'
77

88
export interface EmojiModuleOptions {
9+
emojiData?: EmojiMartData
10+
EmojiPicker?: new (props: any) => Picker
11+
emojiPickerPosition?: typeof computePosition
912
theme?: string
1013
locale?: string
1114
set?: string
@@ -21,6 +24,12 @@ export interface EmojiModuleOptions {
2124
}
2225

2326
const DEFAULT_OPTIONS: EmojiModuleOptions = {
27+
// @ts-ignore
28+
emojiData: window.emojiData,
29+
// @ts-ignore
30+
EmojiPicker: window.EmojiPicker,
31+
// @ts-ignore
32+
emojiPickerPosition: window.emojiPickerPosition,
2433
theme: 'light',
2534
set: 'native',
2635
skinTonePosition: 'none',
@@ -51,7 +60,7 @@ class EmojiModule {
5160
constructor(quill: FluentEditor, options: EmojiModuleOptions = {}) {
5261
this.quill = quill
5362

54-
this.options = options
63+
this.options = { ...DEFAULT_OPTIONS, ...options }
5564

5665
const toolbar = this.quill.getModule('toolbar') as TypeToolbar
5766

@@ -79,8 +88,10 @@ class EmojiModule {
7988
return
8089
}
8190

91+
const { emojiPickerPosition } = this.options
92+
8293
try {
83-
const { x, y } = await computePosition(button, pickerElement)
94+
const { x, y } = await emojiPickerPosition(button, pickerElement)
8495
this.picker.style.top = `${y}px`
8596
this.picker.style.left = `${x}px`
8697
}
@@ -114,17 +125,18 @@ class EmojiModule {
114125

115126
// 创建表情选择弹窗
116127
private createPicker() {
128+
const { EmojiPicker, emojiData, ...options } = this.options
129+
117130
const pickerConfig = {
118-
...DEFAULT_OPTIONS,
119131
// emoji-mart 与 tiny-editor 国际化的的 locale 不一致使用 LOCALE_MAP 转换
120132
locale: LOCALE_MAP[this.quill.lang] ?? 'en',
121-
...this.options,
122-
data,
133+
data: emojiData,
134+
...options,
123135
onEmojiSelect: this.handleEmojiSelect.bind(this),
124136
onClickOutside: this.handleClickOutside.bind(this),
125137
}
126138

127-
const picker = new Picker(pickerConfig) as unknown as HTMLElement
139+
const picker = new EmojiPicker(pickerConfig) as unknown as HTMLElement
128140

129141
// 设置样式和属性
130142
picker.id = PICKER_DOM_ID

0 commit comments

Comments
 (0)