diff --git a/package.json b/package.json index 2dc2759..cfdc227 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,6 @@ "private": true, "dependencies": { "copy-to-clipboard": "^3.3.1", - "fix-orientation": "^1.1.0", "gh-pages": "^3.0.0", "highlight.js": "^10.1.1", "html-to-react": "^1.4.3", diff --git a/src/Config.js b/src/Config.js index fc344eb..7a3a0b0 100644 --- a/src/Config.js +++ b/src/Config.js @@ -29,10 +29,9 @@ const DEFAULT_CONFIG = { pressure: false, easter_egg: true, color_scheme: 'default', - no_c_post: false, - by_c: false, block_words_v2: ['#天火', '#桃花石'], whitelist_cw: [], + ipfs_gateway: ['https://.ipfs.dweb.link/'], }; export function load_config() { @@ -386,6 +385,16 @@ export class ConfigUI extends PureComponent { parse={(string) => string.split('\n')} />
+ 表示要替换的哈希值。只会使用第一行的。'} + display={(array) => array.join('\n')} + sift={(array) => array.filter((v) => v)} + parse={(string) => string.split('\n')} + /> +

- -
- -

新功能建议或问题反馈请在  { - function return_url(url) { - const idx = url.indexOf(';base64,'); - if (idx === -1) throw new Error('img not base64 encoded'); - - return url.substr(idx + 8); - } - - let reader = new FileReader(); - function on_got_img(url) { - const image = new Image(); - image.onload = () => { - let width = image.width; - let height = image.height; - let compressed = false; - - if (width > MAX_IMG_DIAM) { - height = (height * MAX_IMG_DIAM) / width; - width = MAX_IMG_DIAM; - compressed = true; - } - if (height > MAX_IMG_DIAM) { - width = (width * MAX_IMG_DIAM) / height; - height = MAX_IMG_DIAM; - compressed = true; - } - if (height * width > MAX_IMG_PX) { - let rate = Math.sqrt((height * width) / MAX_IMG_PX); - height /= rate; - width /= rate; - compressed = true; - } - console.log('chosen img size', width, height); - - let canvas = document.createElement('canvas'); - let ctx = canvas.getContext('2d'); - canvas.width = width; - canvas.height = height; - ctx.drawImage(image, 0, 0, width, height); - - let quality_l = 0.1, - quality_r = 0.9, - quality, - new_url; - while (quality_r - quality_l >= 0.03) { - quality = (quality_r + quality_l) / 2; - new_url = canvas.toDataURL('image/jpeg', quality); - console.log( - quality_l, - quality_r, - 'trying quality', - quality, - 'size', - new_url.length, - ); - if (new_url.length <= MAX_IMG_FILESIZE) quality_l = quality; - else quality_r = quality; - } - if (quality_l >= 0.101) { - console.log('chosen img quality', quality); - resolve({ - img: return_url(new_url), - quality: quality, - width: Math.round(width), - height: Math.round(height), - compressed: compressed, - }); - } else { - reject('图片过大,无法上传'); - } - }; - image.src = url; - } - reader.onload = (event) => { - fixOrientation(event.target.result, {}, (fixed_dataurl) => { - on_got_img(fixed_dataurl); - }); - }; - reader.readAsDataURL(file); - }); - } - - on_img_change() { - if (this.img_ref.current && this.img_ref.current.files.length) - this.setState( - { - img_tip: '(正在处理图片……)', - }, - () => { - this.proc_img(this.img_ref.current.files[0]) - .then((d) => { - this.setState({ - img_tip: - `(${d.compressed ? '压缩到' : '尺寸'} ${d.width}*${ - d.height - } / ` + - `质量 ${Math.floor(d.quality * 100)}% / ${Math.floor( - d.img.length / BASE64_RATE / 1000, - )}KB)`, - }); - }) - .catch((e) => { - this.setState({ - img_tip: `图片无效:${e}`, - }); - }); - }, - ); - else - this.setState({ - img_tip: null, - }); - } - on_submit(event) { if (event) event.preventDefault(); if (this.state.loading_status === 'loading') return; if (!this.state.text) return; - /* - if (this.img_ref.current.files.length) { - this.setState({ - loading_status: 'processing', - }); - this.proc_img(this.img_ref.current.files[0]) - .then((d) => { - this.setState({ - loading_status: 'loading', - }); - this.do_post(this.state.text, d.img); - }) - .catch((e) => { - alert(e); - }); - } else */ { this.setState({ loading_status: 'loading', @@ -712,6 +572,51 @@ export class PostForm extends Component { this.setState({ poll_options: poll_options }); } + on_file_change(event) { + console.log(event); + let tar = event.target; + let f = event.target.files[0]; + if (f) { + tar.setAttribute('disabled', 'disabled'); + let data = new FormData(); + data.append('file', f); + + fetch(API_BASE + '/upload', { + method: 'POST', + headers: { + 'User-Token': this.props.token, + }, + body: data, + }) + .then(get_json) + .then((json) => { + if (json.code !== 0) { + throw new Error(json.msg); + } + console.log(json); + let url = + (window.config.ipfs_gateway[0] || '(无ipfs网关)').replaceAll( + '', + json.data.hash, + ) + json.data.filename; + let new_text = + this.state.text + + '\n' + + (f.type.startsWith('image/') ? `![](${url})` : url); + this.setState({ text: new_text }); + this.area_ref.current.set(new_text); + tar.removeAttribute('disabled'); + }) + .catch((e) => { + console.error(e); + alert('上传失败\n' + e); + tar.removeAttribute('disabled'); + }); + + // event.target.value = null; + } + } + render() { const { has_poll, poll_options, preview, loading_status } = this.state; return ( @@ -743,8 +648,7 @@ export class PostForm extends Component { {loading_status !== 'done' ? ( ) : (