-
Notifications
You must be signed in to change notification settings - Fork 23
Add text counter #86
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
Merged
Merged
Add text counter #86
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e610a7f
feat-37: Add text counter
ryan331913 b594461
Add corresponding translation
ryan331913 bd17b3b
Change the condition of exceeding characters limitation
ryan331913 7e3cba1
Fix lint issue
ryan331913 0c722fa
Attach while editor used
ryan331913 9a59825
Update paste text logic
ryan331913 f216e1c
Fix lint
ryan331913 7bbdc35
fix: Refactor CardEditor and useTextCounter
0010aor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| import { MAX_CHARACTERS, WARNING_THRESHOLD } from '@/utils/text' | ||
| import { Text } from '@chakra-ui/react' | ||
|
|
||
| interface TextCounterProps { | ||
| textLength: number | ||
| } | ||
|
|
||
| const getTextColor = (currentLength: number) => { | ||
| const warningLimit = MAX_CHARACTERS - WARNING_THRESHOLD | ||
| if (currentLength > warningLimit) { | ||
| return 'red.500' | ||
| } | ||
| return 'gray.500' | ||
| } | ||
|
|
||
| function TextCounter({ textLength }: TextCounterProps) { | ||
| return ( | ||
| <Text position="absolute" bottom="2" right="2" fontSize="sm" color={getTextColor(textLength)}> | ||
| {textLength}/{MAX_CHARACTERS} | ||
| </Text> | ||
| ) | ||
| } | ||
|
|
||
| export default TextCounter |
This file contains hidden or 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,79 @@ | ||
| import { toaster } from '@/components/ui/toaster' | ||
| import { MAX_CHARACTERS } from '@/utils/text' | ||
| import type { Editor } from '@tiptap/react' | ||
| import { useCallback, useEffect, useRef } from 'react' | ||
| import { useTranslation } from 'react-i18next' | ||
|
|
||
| function useTextCounter(editor: Editor | null, setTextLength: (length: number) => void) { | ||
| const { t } = useTranslation() | ||
| const timeoutRef = useRef<NodeJS.Timeout | null>(null) | ||
| const hasShownToastRef = useRef(false) | ||
|
|
||
| const handleTextChange = useCallback( | ||
| (currentEditor: Editor) => { | ||
| const currentText = currentEditor.getText() | ||
| const textLength = currentText.length | ||
|
|
||
| if (textLength > MAX_CHARACTERS) { | ||
| const truncatedText = currentText.substring(0, MAX_CHARACTERS) | ||
| currentEditor.chain().focus().setContent(truncatedText).run() | ||
| if (!hasShownToastRef.current) { | ||
| hasShownToastRef.current = true | ||
| toaster.create({ | ||
| title: t('components.editorTextCounter.title'), | ||
| description: t('components.editorTextCounter.description'), | ||
| type: 'info', | ||
| }) | ||
|
|
||
| timeoutRef.current = setTimeout(() => { | ||
| hasShownToastRef.current = false | ||
| }, 3000) | ||
| } | ||
|
|
||
| setTextLength(MAX_CHARACTERS) | ||
| } else { | ||
| hasShownToastRef.current = false | ||
| setTextLength(textLength) | ||
| } | ||
| }, | ||
| [setTextLength, t], | ||
| ) | ||
|
|
||
| useEffect(() => { | ||
| if (!editor) { | ||
| setTextLength(0) | ||
| return | ||
| } | ||
|
|
||
| const handleUpdate = ({ editor: currentEditor }: { editor: Editor }) => { | ||
| if (currentEditor && !currentEditor.isDestroyed) { | ||
| handleTextChange(currentEditor) | ||
| } | ||
| } | ||
|
|
||
| editor.on('update', handleUpdate) | ||
|
|
||
| handleTextChange(editor) | ||
|
|
||
| return () => { | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current) | ||
| timeoutRef.current = null | ||
| } | ||
|
|
||
| if (editor && !editor.isDestroyed) { | ||
| editor.off('update', handleUpdate) | ||
| } | ||
| } | ||
| }, [editor, handleTextChange, setTextLength]) | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current) | ||
| } | ||
| } | ||
| }, []) | ||
| } | ||
|
|
||
| export default useTextCounter | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the toaster is not being displayed. maybe we can remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The toaster will display under this situation, for example, paste 3 characters when current characters is 2998, the toaster will show up.
Or you think the undo is enough for notifying users, then we can remove the toaster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad, i dont know why the toaster was not appearing in my end.