Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implementation of autocompletion function for emoji input #8137

Merged
merged 18 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/editor/src/@types/declaration.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// prevent TS2307: Cannot find module './xxx.module.scss' or its corresponding type declarations.
declare module '*.scss';

// prevent TS7016: Could not find a declaration file for module 'emoji-mart'.
declare module 'emoji-mart';
2 changes: 1 addition & 1 deletion packages/editor/src/components/CodeMirrorEditorMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const additionalExtensions: Extension[] = [
scrollPastEnd(),
];


type Props = {
onChange?: (value: string) => void,
onSave?: () => void,
Expand Down Expand Up @@ -57,6 +56,7 @@ export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
return cleanupFunction;
}, [codeMirrorEditor, onSave]);


return (
<CodeMirrorEditor
editorKey={GlobalCodeMirrorEditorKey.MAIN}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { keymap, EditorView } from '@codemirror/view';
import { useCodeMirror, type UseCodeMirror } from '@uiw/react-codemirror';
import deepmerge from 'ts-deepmerge';

import { useEmojiAutocompletion } from '../use-emoji-autocompletion/use-emoji-autocompletion';

import { useAppendExtensions, type AppendExtensions } from './utils/append-extensions';
import { useFocus, type Focus } from './utils/focus';
import { useGetDoc, type GetDoc } from './utils/get-doc';
Expand All @@ -33,13 +35,18 @@ const defaultExtensions: Extension[] = [
Prec.lowest(keymap.of(defaultKeymap)),
];


export const useCodeMirrorEditor = (props?: UseCodeMirror): UseCodeMirrorEditor => {
const emojiAutocompletionSettings = useEmojiAutocompletion();

const mergedProps = useMemo<UseCodeMirror>(() => {
const mergedProps = useMemo(() => {
return deepmerge(
props ?? {},
{
extensions: defaultExtensions,
extensions: [
defaultExtensions,
emojiAutocompletionSettings,
miya marked this conversation as resolved.
Show resolved Hide resolved
],
// Reset settings of react-codemirror.
// The extension defined first will be used, so it must be disabled here.
indentWithTab: false,
Expand All @@ -48,7 +55,7 @@ export const useCodeMirrorEditor = (props?: UseCodeMirror): UseCodeMirrorEditor
},
},
);
}, [props]);
}, [emojiAutocompletionSettings, props]);

const { state, view } = useCodeMirror(mergedProps);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { type CompletionContext, type Completion, autocompletion } from '@codemirror/autocomplete';
import { syntaxTree } from '@codemirror/language';
import { Extension } from '@codemirror/state';
import { emojiIndex } from 'emoji-mart';
import emojiData from 'emoji-mart/data/all.json';

const getEmojiDataArray = (): string[] => {
const rawEmojiDataArray = emojiData.categories;

const emojiCategoriesData = [
'people',
'nature',
'foods',
'activity',
'places',
'objects',
'symbols',
'flags',
];

const fixedEmojiDataArray: string[] = [];

emojiCategoriesData.forEach((value) => {
const tempArray = rawEmojiDataArray.find(obj => obj.id === value)?.emojis;

if (tempArray == null) {
return;
}

fixedEmojiDataArray.push(...tempArray);
});

return fixedEmojiDataArray;
};

const emojiDataArray = getEmojiDataArray();

const emojiOptions = emojiDataArray.map(
tag => ({ label: `:${tag}:`, type: tag }),
);

const emojiAutocompletion = (context: CompletionContext) => {
const nodeBefore = syntaxTree(context.state).resolveInner(context.pos, -1);
const textBefore = context.state.sliceDoc(nodeBefore.from, context.pos);
const emojiBefore = /:\w{2,}$/.exec(textBefore);

if (!emojiBefore && !context.explicit) return null;

return {
from: emojiBefore ? nodeBefore.from + emojiBefore.index : context.pos,
options: emojiOptions,
validFor: /^:\w{2,}$/,
};
};

const emojiAutocompletionSettings = autocompletion({
addToOptions: [{
render: (completion: Completion) => {
const emojiName = completion.type ?? '';
const emojiData = emojiIndex.emojis[emojiName];

const emoji = emojiData.native ?? emojiData[1].native;

const element = document.createElement('span');
element.innerHTML = emoji;
return element;
},
position: 20,
}],
icons: false,
override: [emojiAutocompletion],
});
miya marked this conversation as resolved.
Show resolved Hide resolved

export const useEmojiAutocompletion = (): Extension => {
return emojiAutocompletionSettings;
};