import React, {Component, createRef} from 'react'; import { ActivityIndicator, Appearance, KeyboardAvoidingView, Modal, Platform, Text, TouchableOpacity, View, } from 'react-native'; import {ScrollView, TextInput} from 'react-native-gesture-handler'; import {COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT} from './src/utils/Colors'; import {db} from './src/utils/DB'; import {SIZE} from './src/utils/SizeUtils'; import Storage from './src/utils/storage'; import {sleep} from './src/utils/TimeUtils'; let validator; let linkPreview; let ShareExtension; export default class NotesnookShare extends Component { constructor(props, context) { super(props, context); this.state = { isOpen: true, text: '', title: '', loading: false, loadingIntent: true, colors: Appearance.getColorScheme() === 'dark' ? COLOR_SCHEME_DARK : COLOR_SCHEME_LIGHT, height: 0, }; this.initialText = ''; this.textInputRef = createRef(); this.titleInputRef = createRef(); } async componentDidMount() { try { ShareExtension = require("rn-extensions-share").default validator = require("validator").default linkPreview = require("link-preview-js") const data = await ShareExtension.data(); let text; let item = data[0]; if (item.type === 'text') { text = item.value; } if (validator.isURL(text)) { linkPreview.getLinkPreview(text) .then((r) => { if (r?.siteName) { this.setState({ title: r.siteName, text: text, loadingIntent: false, }); } else if (r?.title) { this.setState({ title: r.title, text: text, loadingIntent: false, }); } else { this.setState({ title: 'Web Link', text: text, loadingIntent: false, }); } }) .catch((e) => { this.setState({ title: 'Web Link', text: text, loadingIntent: false, }); }); } else { this.setState({ text: text, loadingIntent: false, }); } this.initialText = text; } catch (e) { console.log('errrr', e); } } close = () => { this.setState({ text: null, }); ShareExtension.close(); }; onPress = async () => { this.titleInputRef.current?.blur(); this.textInputRef.current?.blur(); this.setState({ loading: true, }); let tag = validator.isURL(this.initialText) ? `${ this.state.text.split(' ')[0] }

${ this.state.text.split(' ').length > 0 ? this.state.text.split(' ').slice(1).join(' ') : '' }

` : `

${this.state.text}

`; let add = async () => { await db.notes.add({ title: this.state.title, content: { type: 'tiny', data: tag, }, id: null, }); }; if (db && db.notes) { await add(); } else { await db.init(); await add(); } await Storage.write('notesAddedFromIntent', 'added'); await sleep(500); this.close(); }; saveBtn = () => ( {this.state.loading && ( )} Save Note ); render() { return Platform.OS === 'ios' ? ( { ShareExtension.close(); }} style={{ width: '100%', height: '100%', position: 'absolute', }}> {this.state.loadingIntent ? ( Parsing Data... ) : ( <> this.setState({title: v})} onSubmitEditing={() => { this.textInputRef.current?.focus(); }} blurOnSubmit={false} placeholder="Note Title" /> Cancel this.setState({text: v})} multiline={true} value={this.state.text} blurOnSubmit={false} placeholder="Type your note here" /> {this.saveBtn()} )} ) : ( { console.log(event.nativeEvent.layout.height); this.setState({ height: event.nativeEvent.layout.height, }); }} style={{ justifyContent: 'flex-end', width: '100%', height: '100%', }}> { ShareExtension.close(); }} style={{ width: '100%', height: '100%', position: 'absolute', }} /> {this.state.loadingIntent ? ( Parsing Data... ) : ( <> this.setState({title: v})} onSubmitEditing={() => { this.textInputRef.current?.focus(); }} blurOnSubmit={false} placeholder="Note Title" /> Cancel this.setState({text: v})} multiline={true} value={this.state.text} placeholder="Type your note here" /> {this.saveBtn()} )} ); } }