forked from Wolox/react-chat-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2df5430
commit 637894b
Showing
9 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "chrome", | ||
"request": "launch", | ||
"name": "React Chrome", | ||
"url": "http://localhost:3000", | ||
"webRoot": "${workspaceFolder}" | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ponents/Widget/components/Conversation/components/Sender/components/EmojiPicker/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { Picker } from 'emoji-mart'; | ||
|
||
import { AnyFunction } from '../../../../../../../../utils/types'; | ||
|
||
const emoji = require('../../../../../../../../../assets/icon-smiley.svg') as string; | ||
|
||
import './style.scss'; | ||
|
||
type Props = { | ||
onSelectEmoji: AnyFunction | ||
} | ||
|
||
function EmojiPicker({ onSelectEmoji } : Props) { | ||
const [pickerStatus, setPickerState] = useState(false) | ||
const togglePicker = () => { | ||
setPickerState(prevPickerStatus => !prevPickerStatus) | ||
} | ||
|
||
useEffect(() => { | ||
function onKeyup(e) { | ||
if (e.key === "Escape" && !pickerStatus) togglePicker() | ||
} | ||
window.addEventListener('keyup', onKeyup); | ||
return () => window.removeEventListener('keyup', onKeyup); | ||
}, []) | ||
|
||
|
||
return ( | ||
<> | ||
{pickerStatus && ( | ||
<Picker | ||
emoji='point_up' | ||
title='Pick your emoji…' | ||
style={{ position: 'absolute', bottom: '100px', left: '0', width: '100%' }} | ||
onSelect={onSelectEmoji} | ||
/> | ||
)} | ||
<button className='rcw-picker-btn' type="submit" onClick={togglePicker}> | ||
<img src={emoji} className="rcw-picker-icon" alt="" /> | ||
</button> | ||
</> | ||
) | ||
} | ||
|
||
export default EmojiPicker; |
42 changes: 42 additions & 0 deletions
42
...onents/Widget/components/Conversation/components/Sender/components/EmojiPicker/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@import 'variables/colors'; | ||
@import "~emoji-mart/css/emoji-mart.css"; | ||
|
||
.rcw-picker-container { | ||
// position: relative; | ||
|
||
} | ||
|
||
.rcw-picker-btn { // Create mixin | ||
background: $grey-2; | ||
border: 0; | ||
cursor: pointer; | ||
|
||
.rcw-piker-icon { | ||
height: 25px; | ||
} | ||
} | ||
|
||
// Library style | ||
|
||
// .emoji-mart-category-list { | ||
// display: flex; | ||
// flex-wrap: wrap; | ||
// list-style: none; | ||
// background-color: transparent; | ||
// } | ||
|
||
// .emoji-mart-anchors { | ||
// display: flex; | ||
// } | ||
|
||
// .emoji-mart .emoji-mart-light { | ||
// width: 355px; | ||
// position: absolute; | ||
// top: 0; | ||
// left: 0; | ||
// height: 400px; | ||
// } | ||
|
||
.emoji-mart-preview { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export const getCaretIndex = (el) => { | ||
let position = 0; | ||
const selection = window.getSelection()!; | ||
if (selection.rangeCount !== 0) { | ||
const range = window.getSelection()!.getRangeAt(0) | ||
const preCaretRange = range.cloneRange() | ||
preCaretRange.selectNodeContents(el); | ||
preCaretRange.setEnd(range.endContainer, range.endOffset); | ||
position = preCaretRange.toString().length; | ||
} | ||
return position; | ||
} | ||
|
||
export const getSelection = (el) => { | ||
const range = window.getSelection()!.getRangeAt(0); | ||
const preSelectionRange = range.cloneRange(); | ||
preSelectionRange.selectNodeContents(el) | ||
preSelectionRange.setEnd(range.startContainer, range.startOffset); | ||
|
||
const start = preSelectionRange.toString().length; | ||
return { | ||
start: start, | ||
end: start + range.toString().length | ||
} | ||
} | ||
export const isFirefox = () => navigator.userAgent.search("Firefox") > 0; | ||
|
||
export const updateCaret = (el, caret, offset) => { | ||
const range = document.createRange(); | ||
const selection = window.getSelection()!; | ||
range.setStart(el.childNodes[0], caret+offset); | ||
range.collapse(true); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
el.focus(); | ||
} | ||
|
||
export const insertNodeAtCaret = (el) => { | ||
const position = getCaretIndex(el) | ||
let characterToEnter = '\n\n'; | ||
let prevChar, char = ''; | ||
if (position > 0) { // Change this | ||
prevChar = el.innerHTML.charAt(position - 1); | ||
char = el.innerHTML.charAt(position); | ||
const newLines = el.innerHTML.match(/\n/g) | ||
if( | ||
prevChar === char || | ||
(prevChar === '\n' && char === '') || | ||
(isFirefox() && newLines?.length > 0) | ||
) { | ||
characterToEnter = '\n' | ||
} | ||
} | ||
const selection = window.getSelection()!; | ||
const node = document.createTextNode(characterToEnter); | ||
const range = selection.getRangeAt(0); | ||
range.collapse(false); | ||
range.insertNode(node); | ||
const cloneRange = range.cloneRange(); | ||
cloneRange.selectNodeContents(node); | ||
cloneRange.collapse(false); | ||
selection.removeAllRanges(); | ||
selection.addRange(cloneRange); | ||
el.innerHTML = el.innerHTML.replace(/<br>/g, '') | ||
updateCaret(el, position, 1) | ||
} |