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: add emoji picker in editor #221

Merged
merged 8 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="theme-color" content="#f6f5f4" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="manifest" href="/manifest.json" />
<script>var global = global || window</script>
f97-26082023 marked this conversation as resolved.
Show resolved Hide resolved
<title>Memos</title>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"axios": "^0.27.2",
"copy-to-clipboard": "^3.3.2",
"dayjs": "^1.11.3",
"emoji-picker-react": "^3.6.2",
"lodash-es": "^4.17.21",
"qs": "^6.11.0",
"react": "^18.1.0",
Expand Down
41 changes: 41 additions & 0 deletions web/src/components/Editor/EmojiPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { forwardRef, useEffect } from "react";
import Picker, { IEmojiPickerProps } from "emoji-picker-react";

export type EmojiPickerElement = HTMLDivElement;

interface Props {
isShowEmojiPicker: boolean;
onEmojiClick: IEmojiPickerProps["onEmojiClick"];
handleChangeIsShowEmojiPicker: (status: boolean) => void;
}

export const EmojiPicker = forwardRef<EmojiPickerElement, Props>((props: Props, ref) => {
const { isShowEmojiPicker, onEmojiClick, handleChangeIsShowEmojiPicker } = props;

useEffect(() => {
if (isShowEmojiPicker) {
const handleClickOutside = (event: MouseEvent) => {
const emojiWrapper = document.querySelector(".emoji-picker-react");
const isContains = emojiWrapper?.contains(event.target as Node);
if (!isContains) {
handleChangeIsShowEmojiPicker(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
// Unbind the event listener on clean up
document.removeEventListener("mousedown", handleClickOutside);
};
}
});
f97-26082023 marked this conversation as resolved.
Show resolved Hide resolved

return (
<div className="emoji-picker" ref={ref}>
<Picker onEmojiClick={onEmojiClick} />
</div>
);
});

EmojiPicker.displayName = "EmojiPicker";

export default EmojiPicker;
28 changes: 28 additions & 0 deletions web/src/components/MemoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import * as storage from "../helpers/storage";
import Icon from "./Icon";
import toastHelper from "./Toast";
import Editor, { EditorRefActions } from "./Editor/Editor";
import EmojiPicker from "./Editor/EmojiPicker";
import "../less/memo-editor.less";

interface State {
isUploadingResource: boolean;
fullscreen: boolean;
isShowEmojiPicker: boolean;
}

const MemoEditor = () => {
Expand All @@ -22,6 +24,7 @@ const MemoEditor = () => {
const [state, setState] = useState<State>({
isUploadingResource: false,
fullscreen: false,
isShowEmojiPicker: false,
});
const editorRef = useRef<EditorRefActions>(null);
const prevGlobalStateRef = useRef(editorState);
Expand Down Expand Up @@ -244,6 +247,21 @@ const MemoEditor = () => {
}
}, []);

const handleChangeIsShowEmojiPicker = (status: boolean) => {
setState({
...state,
isShowEmojiPicker: status,
});
};

const handleEmojiClick = (event: any, emojiObject: any) => {
if (!editorRef.current) {
return;
}
editorRef.current?.insertText(`${emojiObject.emoji}`);
handleChangeIsShowEmojiPicker(false);
};

const isEditing = Boolean(editorState.editMemoId && editorState.editMemoId !== UNKNOWN_ID);

const editorConfig = useMemo(
Expand Down Expand Up @@ -300,12 +318,22 @@ const MemoEditor = () => {
<Icon.Image className="icon-img" onClick={handleUploadFileBtnClick} />
<span className={`tip-text ${state.isUploadingResource ? "!block" : ""}`}>Uploading</span>
</button>
<button className="action-btn">
<Icon.Smile className="icon-img" onClick={() => handleChangeIsShowEmojiPicker(!state.isShowEmojiPicker)} />
f97-26082023 marked this conversation as resolved.
Show resolved Hide resolved
</button>
<button className="action-btn" onClick={handleFullscreenBtnClick}>
{state.fullscreen ? <Icon.Minimize className="icon-img" /> : <Icon.Maximize className="icon-img" />}
</button>
</>
}
/>
{state.isShowEmojiPicker && (
<EmojiPicker
onEmojiClick={handleEmojiClick}
isShowEmojiPicker={state.isShowEmojiPicker}
handleChangeIsShowEmojiPicker={handleChangeIsShowEmojiPicker}
/>
)}
</div>
);
};
Expand Down
8 changes: 8 additions & 0 deletions web/src/less/memo-editor.less
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@
}
}
}

.emoji-picker-react {
@apply absolute;

li.emoji::before {
@apply hidden;
}
}
}
5 changes: 5 additions & 0 deletions web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@ electron-to-chromium@^1.4.172:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.185.tgz#3432d7944f1c5fe20664bb45d9cced2151405ce2"
integrity sha512-9kV/isoOGpKkBt04yYNaSWIBn3187Q5VZRtoReq8oz5NY/A4XmU6cAoqgQlDp7kKJCZMRjWZ8nsQyxfpFHvfyw==

emoji-picker-react@^3.6.2:
version "3.6.2"
resolved "https://registry.yarnpkg.com/emoji-picker-react/-/emoji-picker-react-3.6.2.tgz#e414971bf9421b0825484f3b82623fef995c3c4b"
integrity sha512-PK3dfljGxeyN8fDz2FAsDYKPYGgo6/tkRyzJjLVaw0fksJg7jA3OJPIlHq2IIzTmlC/NKhcI/oaf0uEo5azYGA==

errno@^0.1.1:
version "0.1.8"
resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.8.tgz#8bb3e9c7d463be4976ff888f76b4809ebc2e811f"
Expand Down