-
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.
Implement synonyms search and inserting
- Loading branch information
Showing
10 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { SIMPLE_ACTION_PLUGIN_CONFIG } from './simple-action'; | ||
import { SYNONYMS_ACTION_PLUGIN_CONFIG } from './synonyms'; | ||
|
||
const PLUGINS = [ | ||
...SIMPLE_ACTION_PLUGIN_CONFIG | ||
...SIMPLE_ACTION_PLUGIN_CONFIG, | ||
...SYNONYMS_ACTION_PLUGIN_CONFIG | ||
]; | ||
|
||
export default PLUGINS; |
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,37 @@ | ||
.synonyms { | ||
position: absolute; | ||
background: #ffffff; | ||
border: 1px solid #bebebe; | ||
box-shadow: 1px 1px 1px #bebebe; | ||
} | ||
|
||
.synonyms__title { | ||
background-color: #e9e9e9; | ||
border-bottom: 1px solid #ccc; | ||
font-size: 14px; | ||
padding: 4px; | ||
} | ||
|
||
.synonyms-list { | ||
list-style: none; | ||
padding: 0; | ||
margin: 0; | ||
min-width: 150px; | ||
max-height: 250px; | ||
overflow-y: auto; | ||
} | ||
|
||
.synonyms-list__button { | ||
background: none; | ||
border: none; | ||
width: 100%; | ||
padding: 5px 15px 5px 15px; | ||
font-size: 14px; | ||
cursor: pointer; | ||
text-align: left; | ||
border-bottom: 1px solid #eee; | ||
} | ||
|
||
.synonyms-list__button:hover { | ||
background: #f1f0f0; | ||
} |
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,63 @@ | ||
import React, { useEffect, useLayoutEffect, useRef } from 'react'; | ||
import useConstant from 'use-constant'; | ||
import { useSearchSynonyms } from './hooks'; | ||
import './Synonyms.css'; | ||
import { replaceSelectedText } from '../../shared/selection-helpers'; | ||
|
||
function Synonyms() { | ||
const {synonyms, setText} = useSearchSynonyms(); | ||
const listRefContainer = useRef(null); | ||
|
||
const selectionChangeListener = useConstant(() => () => { | ||
const selection = document.getSelection(); | ||
if (selection) { | ||
setText(selection.toString()); | ||
} | ||
}); | ||
|
||
useEffect( | ||
() => { | ||
document.addEventListener('selectionchange', selectionChangeListener); | ||
return () => document.removeEventListener('selectionchange', selectionChangeListener); | ||
}, | ||
[] | ||
); | ||
|
||
useLayoutEffect( | ||
() => { | ||
const selection = document.getSelection(); | ||
if (selection && selection.rangeCount > 0 && listRefContainer.current) { | ||
const range = selection.getRangeAt(0); | ||
const rect = range.getBoundingClientRect(); | ||
listRefContainer.current.style.top = `${rect.top + rect.height + 2}px`; | ||
listRefContainer.current.style.left = `${rect.left - 4}px`; | ||
} | ||
}, | ||
[synonyms.result] | ||
); | ||
|
||
if (synonyms.result && synonyms.result.length) { | ||
return ( | ||
<div className="synonyms" ref={listRefContainer}> | ||
<div className="synonyms__title">Select word to replace:</div> | ||
|
||
<ul className="synonyms-list"> | ||
{synonyms.result.map(({word}) => ( | ||
<li key={word}> | ||
<button | ||
type="button" | ||
className="synonyms-list__button" | ||
onClick={() => replaceSelectedText(word)} | ||
>{word}</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default Synonyms; | ||
|
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,35 @@ | ||
import { useState } from 'react'; | ||
import useConstant from 'use-constant'; | ||
import AwesomeDebouncePromise from 'awesome-debounce-promise'; | ||
import SynonymsApi from './synonyms-api'; | ||
import { useAsync } from 'react-async-hook'; | ||
|
||
const useSearchSynonyms = () => { | ||
const [text, setText] = useState(''); | ||
|
||
const debouncedLoadSynonyms = useConstant(() => AwesomeDebouncePromise(SynonymsApi.loadSynonyms, 300)); | ||
|
||
const synonyms = useAsync( | ||
async text => { | ||
if (text.trim().length === 0) { | ||
return []; | ||
} | ||
return debouncedLoadSynonyms(text.trim()) | ||
.catch(error => { | ||
console.error(`Error occurred while fetching synonyms: ${error.message}`); | ||
return []; | ||
}); | ||
}, | ||
[text] | ||
); | ||
|
||
return { | ||
text, | ||
synonyms, | ||
setText | ||
}; | ||
}; | ||
|
||
export { | ||
useSearchSynonyms | ||
}; |
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,5 @@ | ||
import Synonyms from './Synonyms'; | ||
|
||
export const SYNONYMS_ACTION_PLUGIN_CONFIG = [{ | ||
component: Synonyms | ||
}]; |
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,13 @@ | ||
const SYNONYMS_API_ROOT = 'https://api.datamuse.com/words'; | ||
|
||
const loadSynonyms = async word => { | ||
const response = await fetch(`${SYNONYMS_API_ROOT}?rel_syn=${word}`); | ||
if (!response.ok || response.status !== 200) { | ||
throw new Error('Failed to load synonyms'); | ||
} | ||
return response.json(); | ||
}; | ||
|
||
export default { | ||
loadSynonyms | ||
}; |
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,18 @@ | ||
const replaceSelectedText = text => { | ||
const selection = document.getSelection(); | ||
if (selection.rangeCount) { | ||
// keep accidentally selected space after the word | ||
if (/.+?\s+$/.test(selection.toString())) { | ||
selection.extend(selection.focusNode, selection.focusOffset - 1); | ||
} | ||
|
||
const range = selection.getRangeAt(0); | ||
range.deleteContents(); | ||
range.insertNode(document.createTextNode(text)); | ||
selection.collapseToEnd(); | ||
} | ||
} | ||
|
||
export { | ||
replaceSelectedText | ||
} |