-
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.
Added auto corrections and punctuation
- Loading branch information
Showing
11 changed files
with
1,398 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,38 @@ | ||
export default { | ||
"ur" : [ | ||
{ incorrectText: ".", correctText: "۔", completeWord: false }, | ||
{ incorrectText: ",", correctText: "،", completeWord: false }, | ||
{ incorrectText: " ۔", correctText: "۔", completeWord: false }, | ||
{ incorrectText: " - ", correctText: "۔ ", completeWord: false }, | ||
{ incorrectText: " ،", correctText: "،", completeWord: false }, | ||
{ incorrectText: "?", correctText: "؟", completeWord: false }, | ||
{ incorrectText: " ؟", correctText: "؟", completeWord: false }, | ||
{ incorrectText: " !", correctText: "!", completeWord: false }, | ||
{ incorrectText: "( ", correctText: "(", completeWord: false }, | ||
{ incorrectText: " (", correctText: "(", completeWord: false }, | ||
{ incorrectText: "ه", correctText: "ہ", completeWord: false }, | ||
{ incorrectText: "ک", correctText: "ک", completeWord: false }, | ||
{ incorrectText: "ئو", correctText: "ؤ", completeWord: false }, | ||
{ incorrectText: "’’", correctText: "”", completeWord: false }, | ||
{ incorrectText: "‘‘", correctText: "“", completeWord: false }, | ||
{ incorrectText: "…", correctText: "۔۔۔", completeWord: false }, | ||
{ incorrectText: "——", correctText: "۔۔۔", completeWord: false }, | ||
{ incorrectText: "—", correctText: "۔۔۔", completeWord: false }, | ||
{ incorrectText: " ۔۔۔", correctText: "۔۔۔ ", completeWord: false }, | ||
{ incorrectText: "،،", correctText: "“", completeWord: false }, | ||
{ incorrectText: '۔"', correctText: "۔“", completeWord: false }, | ||
{ incorrectText: '، "', correctText: "۔ ”", completeWord: false }, | ||
{ incorrectText: '؟"', correctText: "؟“", completeWord: false }, | ||
{ incorrectText: '!"', correctText: "!“", completeWord: false }, | ||
{ incorrectText: "-", correctText: "۔", completeWord: false }, | ||
{ incorrectText: " ، ", correctText: "، ", completeWord: false }, | ||
{ incorrectText: " ۔", correctText: "۔ ", completeWord: false }, | ||
{ incorrectText: " : ", correctText: ": ", completeWord: false }, | ||
{ incorrectText: "آ", correctText: "آ", completeWord: false }, | ||
{ incorrectText: "ؤ", correctText: "ؤ", completeWord: false }, | ||
{ incorrectText: " ِ", correctText: "ِ", completeWord: false }, | ||
{ incorrectText: " ً", correctText: "ً", completeWord: false }, | ||
], | ||
"en": [ | ||
{ incorrectText: " ۔ ", correctText: "ً۔ ", completeWord: false }, | ||
]}; |
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 @@ | ||
export default { "ur" : [], "en" : [] }; |
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,7 @@ | ||
import { | ||
createCommand, | ||
} from 'lexical'; | ||
|
||
export const SPELLCHECK_COMMAND = createCommand('SPELLCHECK_COMMAND'); | ||
export const AUTO_CORRECT_COMMAND = createCommand('AUTO_CORRECT_COMMAND'); | ||
export const PUNCTUATION_CORRECT_COMMAND = createCommand('PUNCTUATION_CORRECT_COMMAND'); |
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
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
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,141 @@ | ||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import { | ||
$getRoot, | ||
COMMAND_PRIORITY_LOW, | ||
} from 'lexical'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
import { AUTO_CORRECT_COMMAND, SPELLCHECK_COMMAND, PUNCTUATION_CORRECT_COMMAND } from "../../commands/spellCheckCommand"; | ||
import { Drawer } from 'antd'; | ||
|
||
const getReplaceAllRegex = (corrections) => { | ||
let retVal = ''; | ||
corrections.forEach((c) => { | ||
// retVal += `(${c.completeWord ? `\\b${c.incorrectText.trim()}\\b` : c.incorrectText.trim()})|`; | ||
retVal += `(${c.incorrectText.trim()})|`; | ||
}); | ||
|
||
// return new RegExp(retVal.slice(0, -1), 'gimu'); | ||
return new RegExp(`\\b${retVal.slice(0, -1)}\\b`, 'giu'); | ||
}; | ||
const correctPunctuations = (punctuationCorrections, text) => { | ||
text = text.replace(/ +/g, ' '); | ||
punctuationCorrections.forEach((c) => { | ||
text = text.replaceAll(c.completeWord ? `${c.incorrectText}\\b` : c.incorrectText, c.correctText); | ||
}); | ||
return text; | ||
}; | ||
|
||
const autoCorrectText = (autoCorrections, text) => { | ||
const correctionRegex = getReplaceAllRegex(autoCorrections); | ||
return text.replaceAll(correctionRegex, (matched) => autoCorrections.find((o) => o.incorrectText === matched)?.correctText.trim()); | ||
}; | ||
|
||
export default function SpellCheckerPlugin({ locale, language, configuration = { enabled : false} }) { | ||
if (!configuration.enabled) return null; | ||
const [editor] = useLexicalComposerContext(); | ||
const [open, setOpen] = useState(false); | ||
|
||
const onClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
useEffect(() => { | ||
editor.registerCommand( | ||
SPELLCHECK_COMMAND, | ||
() => { | ||
setOpen(true); | ||
}, | ||
COMMAND_PRIORITY_LOW, | ||
); | ||
|
||
/* automatic correction */ | ||
|
||
editor.registerCommand( | ||
AUTO_CORRECT_COMMAND, | ||
() => { | ||
autoCorrect(); | ||
}, | ||
COMMAND_PRIORITY_LOW, | ||
); | ||
|
||
const autoCorrectNode = (node, corrections) => { | ||
if (node.getChildren) { | ||
node.getChildren().map((child) => { | ||
autoCorrectNode(child, corrections); | ||
}); | ||
} | ||
|
||
if (node.getType() === 'text') { | ||
node.setTextContent(autoCorrectText(corrections, node.getTextContent())); | ||
} | ||
|
||
return node | ||
} | ||
|
||
const autoCorrect = () => { | ||
var corrections = configuration.autoCorrections(language); | ||
editor.update(() => { | ||
var root = $getRoot(editor); | ||
var children = root.getChildren(); | ||
children.forEach((child) => { | ||
autoCorrectNode(child, corrections); | ||
}); | ||
}); | ||
} | ||
/* automatic correction ends */ | ||
|
||
/* Punctuation correction */ | ||
editor.registerCommand( | ||
PUNCTUATION_CORRECT_COMMAND, | ||
() => { | ||
punctuationCorrection(); | ||
}, | ||
COMMAND_PRIORITY_LOW, | ||
); | ||
|
||
}, [editor]); | ||
|
||
|
||
const punctuationCorrectionNode = (node, corrections) => { | ||
if (node.getChildren) { | ||
node.getChildren().map((child) => { | ||
punctuationCorrectionNode(child, corrections); | ||
}); | ||
} | ||
|
||
if (node.getType() === 'text') { | ||
node.setTextContent(correctPunctuations(corrections, node.getTextContent())); | ||
} | ||
|
||
return node | ||
} | ||
|
||
const punctuationCorrection = () => { | ||
var corrections = configuration.punctuationCorrections(language); | ||
editor.update(() => { | ||
var root = $getRoot(editor); | ||
var children = root.getChildren(); | ||
children.forEach((child) => { | ||
punctuationCorrectionNode(child, corrections); | ||
}); | ||
}); | ||
} | ||
|
||
/* Punctuation correction ends */ | ||
|
||
return ( | ||
<> | ||
<Drawer | ||
title={locale?.resources?.spellchecker} | ||
placement="right" | ||
closable={true} | ||
onClose={onClose} | ||
open={open} | ||
> | ||
<p>Some contents...</p> | ||
<p>Some contents...</p> | ||
<p>Some contents...</p> | ||
</Drawer> | ||
</>); | ||
} |
Oops, something went wrong.