fix greentext selection

This commit is contained in:
plebeius.eth
2023-06-24 16:36:59 +02:00
parent 79afa78142
commit 31ce77d77d
6 changed files with 64 additions and 4 deletions
+9 -4
View File
@@ -20,6 +20,7 @@ const ReplyModal = ({ isOpen, closeModal }) => {
selectedParentCid,
selectedShortCid,
selectedStyle,
selectedText, setSelectedText,
triggerInsertion,
} = useGeneralStore(state => state);
@@ -33,7 +34,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
const linkRef = useRef();
const [triggerPublishComment, setTriggerPublishComment] = useState(false);
const [selectedText, setSelectedText] = useState('');
const [isMobile, setIsMobile] = useState(window.innerWidth <= 480);
@@ -50,7 +50,12 @@ const ReplyModal = ({ isOpen, closeModal }) => {
const onModalOpen = () => {
if (commentRef.current) {
if (selectedText) {
commentRef.current.value += '\n';
}
commentRef.current.focus();
const len = commentRef.current.value.length;
commentRef.current.setSelectionRange(len, len);
}
};
@@ -73,16 +78,16 @@ const ReplyModal = ({ isOpen, closeModal }) => {
const getSelectedText = useCallback(() => {
const text = document.getSelection().toString();
setSelectedText(text ? `>${text}\n` : '');
}, []);
}, [setSelectedText]);
useEffect(() => {
if (isOpen) {
getSelectedText();
setTimeout(getSelectedText, 0);
} else {
setSelectedText('');
}
}, [isOpen, getSelectedText]);
}, [isOpen, getSelectedText, setSelectedText]);
const onChallengeVerification = (challengeVerification) => {
+13
View File
@@ -62,6 +62,7 @@ const All = () => {
setSelectedShortCid,
selectedStyle,
setSelectedThread,
setSelectedText,
setSelectedTitle,
setTriggerInsertion,
} = useGeneralStore(state => state);
@@ -706,6 +707,9 @@ const All = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(thread.shortCid);
setTriggerInsertion(Date.now());
@@ -991,6 +995,9 @@ const All = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
@@ -1526,6 +1533,9 @@ const All = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(thread.shortCid);
setTriggerInsertion(Date.now());
@@ -1704,6 +1714,9 @@ const All = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
+13
View File
@@ -64,6 +64,7 @@ const Board = () => {
setSelectedShortCid,
selectedStyle,
setSelectedThread,
setSelectedText,
selectedTitle, setSelectedTitle,
showPostForm,
showPostFormLink,
@@ -1150,6 +1151,9 @@ const Board = () => {
if (e.button === 2) return;
e.preventDefault();
setSelectedThreadCid(thread.cid);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(thread.shortCid);
setTriggerInsertion(Date.now());
@@ -1433,6 +1437,9 @@ const Board = () => {
if (e.button === 2) return;
e.preventDefault();
setSelectedThreadCid(thread.cid);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
@@ -1939,6 +1946,9 @@ const Board = () => {
if (e.button === 2) return;
e.preventDefault();
setSelectedThreadCid(thread.cid);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(thread.shortCid);
setTriggerInsertion(Date.now());
@@ -2095,6 +2105,9 @@ const Board = () => {
if (e.button === 2) return;
e.preventDefault();
setSelectedThreadCid(thread.cid);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
+13
View File
@@ -62,6 +62,7 @@ const Subscriptions = () => {
setSelectedShortCid,
selectedStyle,
setSelectedThread,
setSelectedText,
setSelectedTitle,
setTriggerInsertion,
} = useGeneralStore(state => state);
@@ -714,6 +715,9 @@ const Subscriptions = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(thread.shortCid);
setTriggerInsertion(Date.now());
@@ -999,6 +1003,9 @@ const Subscriptions = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
@@ -1534,6 +1541,9 @@ const Subscriptions = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(thread.shortCid);
setTriggerInsertion(Date.now());
@@ -1712,6 +1722,9 @@ const Subscriptions = () => {
e.preventDefault();
setSelectedThreadCid(thread.cid);
setSelectedAddress(thread.subplebbitAddress);
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
+13
View File
@@ -63,6 +63,7 @@ const Thread = () => {
setSelectedShortCid,
selectedStyle,
selectedThread, setSelectedThread,
setSelectedText,
selectedTitle, setSelectedTitle,
showPostForm,
showPostFormLink,
@@ -844,6 +845,9 @@ const Thread = () => {
<span>c/</span>
<Link to={() => {}} id="reply-button" style={{ cursor: 'pointer' }}
onClick={() => {
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(comment.shortCid);
setTriggerInsertion(Date.now());
@@ -1085,6 +1089,9 @@ const Thread = () => {
{reply.shortCid ? (
<Link id="reply-button" key={`pl2-${index}`}
onClick={() => {
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
@@ -1463,6 +1470,9 @@ const Thread = () => {
<span key={`mob-no-${comment.cid}`}>c/</span>
<Link to={() => {}} id="reply-button" key={`mob-no2-${comment.cid}`} title="Reply to this post"
onClick={() => {
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(comment.shortCid);
setTriggerInsertion(Date.now());
@@ -1588,6 +1598,9 @@ const Thread = () => {
{reply.shortCid ? (
<Link id="reply-button" key={`mob-pl2-${index}`}
onClick={() => {
let text = document.getSelection().toString();
text = text ? `>${text}` : text;
setSelectedText(text);
if (isReplyOpen) {
setReplyQuoteCid(reply.shortCid);
setTriggerInsertion(Date.now());
+3
View File
@@ -73,6 +73,9 @@ const useGeneralStore = create((set) => ({
localStorage.setItem('selectedStyle', style);
set({ selectedStyle: style });
},
selectedText: '',
setSelectedText: (text) => set({ selectedText: text }),
selectedThread: '',
setSelectedThread: (thread) => set({ selectedThread: thread }),