|
| 1 | +import PropTypes from 'prop-types' |
| 2 | +import React from 'react' |
| 3 | +import CSSModules from 'browser/lib/CSSModules' |
| 4 | +import styles from './CreateMarkdownFromURLModal.styl' |
| 5 | +import dataApi from 'browser/main/lib/dataApi' |
| 6 | +import store from 'browser/main/store' |
| 7 | +import consts from 'browser/lib/consts' |
| 8 | +import ModalEscButton from 'browser/components/ModalEscButton' |
| 9 | +import AwsMobileAnalyticsConfig from 'browser/main/lib/AwsMobileAnalyticsConfig' |
| 10 | +import i18n from 'browser/lib/i18n' |
| 11 | +const http = require('http') |
| 12 | +const https = require('https') |
| 13 | +const TurndownService = require('turndown') |
| 14 | +import { hashHistory } from 'react-router' |
| 15 | +import ee from 'browser/main/lib/eventEmitter' |
| 16 | + |
| 17 | +class CreateMarkdownFromURLModal extends React.Component { |
| 18 | + constructor (props) { |
| 19 | + super(props) |
| 20 | + let td = new TurndownService(); |
| 21 | + |
| 22 | + this.state = { |
| 23 | + name: '', |
| 24 | + showerror: false, |
| 25 | + errormessage: '', |
| 26 | + turndownService: td |
| 27 | + } |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + componentDidMount () { |
| 32 | + this.refs.name.focus() |
| 33 | + this.refs.name.select() |
| 34 | + } |
| 35 | + |
| 36 | + handleCloseButtonClick (e) { |
| 37 | + this.props.close() |
| 38 | + } |
| 39 | + |
| 40 | + handleChange (e) { |
| 41 | + this.setState({ |
| 42 | + name: this.refs.name.value |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + handleKeyDown (e) { |
| 47 | + if (e.keyCode === 27) { |
| 48 | + this.props.close() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + handleInputKeyDown (e) { |
| 53 | + switch (e.keyCode) { |
| 54 | + case 13: |
| 55 | + this.confirm() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + handleConfirmButtonClick (e) { |
| 60 | + this.confirm() |
| 61 | + } |
| 62 | + |
| 63 | + showError(message) { |
| 64 | + this.setState({ |
| 65 | + showerror: true, |
| 66 | + errormessage: message |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + hideError() { |
| 71 | + this.setState({ |
| 72 | + showerror: false, |
| 73 | + errormessage: '' |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + validateUrl(str) { |
| 78 | + if(/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(str)) { |
| 79 | + return true; |
| 80 | + } else { |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + confirm () { |
| 86 | + if(this.validateUrl(this.state.name)) { |
| 87 | + this.hideError() |
| 88 | + let url = this.state.name; |
| 89 | + let request = http; |
| 90 | + if(url.includes('https')) |
| 91 | + request = https; |
| 92 | + let req = request.request(url, (res) => { |
| 93 | + let data = ''; |
| 94 | + res.on('data', (chunk) => { |
| 95 | + data += chunk |
| 96 | + }) |
| 97 | + res.on('end', () => { |
| 98 | + console.log("receiving data", data); |
| 99 | + |
| 100 | + let html = document.createElement('html'); |
| 101 | + html.innerHTML = data; |
| 102 | + |
| 103 | + let scripts = html.getElementsByTagName('script'); |
| 104 | + for(let i = scripts.length - 1; i >= 0; i--) { |
| 105 | + scripts[i].parentNode.removeChild(scripts[i]); |
| 106 | + } |
| 107 | + |
| 108 | + let body = html.getElementsByTagName('body')[0].innerHTML; |
| 109 | + let markdownHTML = this.state.turndownService.turndown(body); |
| 110 | + console.log('markdown', markdownHTML); |
| 111 | + html.innerHTML = ''; |
| 112 | + const { storage, folder, dispatch, location } = this.props |
| 113 | + |
| 114 | + dataApi |
| 115 | + .createNote(storage, { |
| 116 | + type: 'MARKDOWN_NOTE', |
| 117 | + folder: folder, |
| 118 | + title: '', |
| 119 | + content: markdownHTML |
| 120 | + }) |
| 121 | + .then((note) => { |
| 122 | + const noteHash = note.key |
| 123 | + dispatch({ |
| 124 | + type: 'UPDATE_NOTE', |
| 125 | + note: note |
| 126 | + }) |
| 127 | + hashHistory.push({ |
| 128 | + pathname: location.pathname, |
| 129 | + query: {key: noteHash} |
| 130 | + }) |
| 131 | + ee.emit('list:jump', noteHash) |
| 132 | + ee.emit('detail:focus') |
| 133 | + this.props.close() |
| 134 | + }) |
| 135 | + }) |
| 136 | + }); |
| 137 | + req.on('error', (e) => { |
| 138 | + console.log("Error in request", e.message); |
| 139 | + }); |
| 140 | + req.end(); |
| 141 | + } else { |
| 142 | + this.showError("Please check your URL is in correct format. (Example, 'https://google.com')") |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + render () { |
| 147 | + return ( |
| 148 | + <div styleName='root' |
| 149 | + tabIndex='-1' |
| 150 | + onKeyDown={(e) => this.handleKeyDown(e)} |
| 151 | + > |
| 152 | + <div styleName='header'> |
| 153 | + <div styleName='title'>{i18n.__('Import Markdown From URL')}</div> |
| 154 | + </div> |
| 155 | + <ModalEscButton handleEscButtonClick={(e) => this.handleCloseButtonClick(e)} /> |
| 156 | + <div styleName='control'> |
| 157 | + <div styleName='control-folder'> |
| 158 | + <div styleName='control-folder-label'>{i18n.__('Insert URL Here')}</div> |
| 159 | + <input styleName='control-folder-input' |
| 160 | + ref='name' |
| 161 | + value={this.state.name} |
| 162 | + onChange={(e) => this.handleChange(e)} |
| 163 | + onKeyDown={(e) => this.handleInputKeyDown(e)} |
| 164 | + /> |
| 165 | + </div> |
| 166 | + <button styleName='control-confirmButton' |
| 167 | + onClick={(e) => this.handleConfirmButtonClick(e)} |
| 168 | + > |
| 169 | + {i18n.__('Import')} |
| 170 | + </button> |
| 171 | + <div className="error" styleName="error">{this.state.errormessage}</div> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + ) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +CreateMarkdownFromURLModal.propTypes = { |
| 179 | + storage: PropTypes.shape({ |
| 180 | + key: PropTypes.string |
| 181 | + }) |
| 182 | +} |
| 183 | + |
| 184 | +export default CSSModules(CreateMarkdownFromURLModal, styles) |
0 commit comments