applying snippet

This commit is contained in:
Nouman Tahir 2021-03-14 22:10:16 +05:00
parent 4ed29a9d84
commit 53c5686617
4 changed files with 31 additions and 7 deletions

View File

@ -0,0 +1,11 @@
import {replaceBetween } from './utils';
export default async ({ text, selection, setTextAndSelection, snippetText}) => {
const newText = replaceBetween(text, selection, `\n${snippetText}\n`);
const newSelection = {
start: selection.start + 1,
end: selection.start + 1 + (snippetText && snippetText.length),
};
setTextAndSelection({ text: newText, selection: newSelection });
};

View File

@ -85,8 +85,8 @@ export default EStyleSheet.create({
modalStyle: {
flex: 1,
backgroundColor: '$primaryBackgroundColor',
margin:0,
paddingTop:32,
paddingBottom:16
margin: 0,
paddingTop: 32,
paddingBottom: 16,
},
});

View File

@ -39,6 +39,7 @@ import { ThemeContainer } from '../../../containers';
// Styles
import styles from './markdownEditorStyles';
import applySnippet from './formats/applySnippet';
const MarkdownEditorView = ({
draftBody,
@ -195,6 +196,15 @@ const MarkdownEditorView = ({
</ScrollView>
);
const _handleOnSnippetSelect = (snippetText) => {
applySnippet({
text,
selection,
setTextAndSelection: _setTextAndSelection,
snippetText,
});
};
const _renderMarkupButton = ({ item }) => (
<View style={styles.buttonWrapper}>
<IconButton
@ -353,7 +363,7 @@ const MarkdownEditorView = ({
animationType="slide"
style={styles.modalStyle}
>
<SnippetsModal username={currentAccount.username} handleOnSelect={_setTextAndSelection} />
<SnippetsModal username={currentAccount.username} handleOnSelect={_handleOnSnippetSelect} />
</Modal>
<ActionSheet
ref={galleryRef}

View File

@ -9,9 +9,12 @@ import SnippetEditorModal, { SnippetEditorModalRef } from '../snippetEditorModal
import SnippetItem from './snippetItem';
import { Snippet } from '../../models';
interface SnippetsModalProps {
username:string,
handleOnSelect:(snippetText:string)=>void,
}
const SnippetsModal = ({ username, handleOnSelect }) => {
const SnippetsModal = ({ username, handleOnSelect }:SnippetsModalProps) => {
const editorRef = useRef<SnippetEditorModalRef>(null);
const intl = useIntl();
console.log('username', username);
@ -61,7 +64,7 @@ const SnippetsModal = ({ username, handleOnSelect }) => {
//render list item for snippet and handle actions;
const _renderItem = ({ item, index }:{item:Snippet, index:number}) => {
const _onPress = () => handleOnSelect({ text: item.body, selection: { start: 0, end: 0 } })
const _onPress = () => handleOnSelect(item.body)
const _onRemovePress = () => {
_removeSnippet(item.id);