From 3497c664d5274185c649fe8d21d811923030a65a Mon Sep 17 00:00:00 2001
From: xmcp
Date: Tue, 21 Aug 2018 13:03:58 +0800
Subject: [PATCH] add auto link
---
package-lock.json | 28 ++++++++++++++++++++++++++++
package.json | 3 ++-
src/Common.js | 19 +++++++++++++++++++
src/Flows.css | 2 +-
src/Flows.js | 25 ++++++++++++++-----------
src/Title.css | 4 ++++
src/Title.js | 10 ++++++++++
src/index.css | 1 -
8 files changed, 78 insertions(+), 14 deletions(-)
diff --git a/package-lock.json b/package-lock.json
index f208b89..ba4a60f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -6305,6 +6305,14 @@
"type-check": "0.3.2"
}
},
+ "linkify-it": {
+ "version": "2.0.3",
+ "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-2.0.3.tgz",
+ "integrity": "sha1-2UpGSPmxwXnWT6lykSaL22zpQ08=",
+ "requires": {
+ "uc.micro": "1.0.5"
+ }
+ },
"load-json-file": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz",
@@ -8788,6 +8796,16 @@
"resolved": "https://registry.npmjs.org/react-lazyload/-/react-lazyload-2.3.0.tgz",
"integrity": "sha512-0z3qmL+qtSERdfKFpn0yKXm+1Gg1ZLZBXnCzHhSGiu1L8iDARuCkbOypxEx9+ETxZvMnXj98xvWCs5jyXTuM2w=="
},
+ "react-linkify": {
+ "version": "0.2.2",
+ "resolved": "https://registry.npmjs.org/react-linkify/-/react-linkify-0.2.2.tgz",
+ "integrity": "sha512-0S8cvUNtEgfJpIGDPKklyrnrTffJ63WuJAc4KaYLBihl5TjgH5cHUmYD+AXLpsV+CVmfoo/56SUNfrZcY4zYMQ==",
+ "requires": {
+ "linkify-it": "2.0.3",
+ "prop-types": "15.6.2",
+ "tlds": "1.203.1"
+ }
+ },
"react-scripts": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-1.1.4.tgz",
@@ -10091,6 +10109,11 @@
"setimmediate": "1.0.5"
}
},
+ "tlds": {
+ "version": "1.203.1",
+ "resolved": "https://registry.npmjs.org/tlds/-/tlds-1.203.1.tgz",
+ "integrity": "sha512-7MUlYyGJ6rSitEZ3r1Q1QNV8uSIzapS8SmmhSusBuIc7uIxPPwsKllEP0GRp1NS6Ik6F+fRZvnjDWm3ecv2hDw=="
+ },
"tmp": {
"version": "0.0.33",
"resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz",
@@ -10234,6 +10257,11 @@
"resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.18.tgz",
"integrity": "sha512-LtzwHlVHwFGTptfNSgezHp7WUlwiqb0gA9AALRbKaERfxwJoiX0A73QbTToxteIAuIaFshhgIZfqK8s7clqgnA=="
},
+ "uc.micro": {
+ "version": "1.0.5",
+ "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.5.tgz",
+ "integrity": "sha512-JoLI4g5zv5qNyT09f4YAvEZIIV1oOjqnewYg5D38dkQljIzpPT296dbIGvKro3digYI1bkb7W6EP1y4uDlmzLg=="
+ },
"uglify-js": {
"version": "3.4.7",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.4.7.tgz",
diff --git a/package.json b/package.json
index 8f733ad..cbfbc10 100644
--- a/package.json
+++ b/package.json
@@ -6,8 +6,9 @@
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-lazyload": "^2.3.0",
+ "react-linkify": "^0.2.2",
"react-scripts": "1.1.4",
- "react-timeago": "^4.1.9"
+ "react-timeago": "^4.1.9",
},
"scripts": {
"start": "react-scripts start",
diff --git a/src/Common.js b/src/Common.js
index 42a72d0..b0498d1 100644
--- a/src/Common.js
+++ b/src/Common.js
@@ -1,10 +1,21 @@
import React, {Component} from 'react';
+
import TimeAgo from 'react-timeago';
+import Linkify, {linkify} from 'react-linkify';
import chineseStrings from 'react-timeago/lib/language-strings/zh-CN';
import buildFormatter from 'react-timeago/lib/formatters/buildFormatter';
+
import './Common.css';
const chinese_format=buildFormatter(chineseStrings);
+const PID_RE_TEXT=/(?:^|[^\d])(\d{5,6})(?!\d)/g;
+
+linkify.add('#', {
+ validate: /^(\d{5,6})/,
+ normalize: (match) => {
+ match.url='#'+match.url;
+ },
+});
function pad2(x) {
return x<10 ? '0'+x : ''+x;
@@ -28,4 +39,12 @@ export function TitleLine(props) {
{props.text}
)
+}
+
+export function AutoLink(props) {
+ return (
+
+ {props.text.replace(new RegExp(PID_RE_TEXT,'g'),' #$1 ')}
+
+ )
}
\ No newline at end of file
diff --git a/src/Flows.css b/src/Flows.css
index bea850c..26c6db2 100644
--- a/src/Flows.css
+++ b/src/Flows.css
@@ -35,7 +35,7 @@
}
.left-container .flow-item-row {
- cursor: pointer;
+ cursor: default;
transition: margin-left 200ms ease-out;
}
.left-container .flow-item-row:hover {
diff --git a/src/Flows.js b/src/Flows.js
index 1086261..0808144 100644
--- a/src/Flows.js
+++ b/src/Flows.js
@@ -1,5 +1,5 @@
import React, {Component} from 'react';
-import {Time, TitleLine} from './Common.js';
+import {Time, TitleLine, AutoLink} from './Common.js';
import './Flows.css';
import LazyLoad from 'react-lazyload';
@@ -14,7 +14,7 @@ function Reply(props) {
#{props.info.cid}
- {props.info.text}
+
);
}
@@ -36,7 +36,7 @@ function FlowItem(props) {
#{props.info.pid}
- {props.info.text}
+
{props.info.type==='image' ?
: null}
{props.info.type==='audio' ? : null}
@@ -79,13 +79,16 @@ class FlowItemRow extends Component {
render() {
// props.do_show_details
return (
- {this.props.callback(
- '帖子详情',
-
-
- {this.state.replies.map((reply)=>)}
-
- )}}>
+
{
+ if(event.target.tagName.toLowerCase()!=='a')
+ this.props.callback(
+ '帖子详情',
+
+
+ {this.state.replies.map((reply)=>)}
+
+ );
+ }}>
{!!this.state.reply_loading &&
}
{this.state.replies.map((reply)=>
)}
@@ -121,7 +124,6 @@ export class Flow extends Component {
chunks: [],
loading: false,
};
- setTimeout(this.load_page.bind(this,1), 0);
}
load_page(page) {
@@ -212,6 +214,7 @@ export class Flow extends Component {
}
componentDidMount() {
+ this.load_page(1);
window.addEventListener('scroll',this.on_scroll.bind(this));
window.addEventListener('resize',this.on_scroll.bind(this));
}
diff --git a/src/Title.css b/src/Title.css
index 69eb092..bbd6418 100644
--- a/src/Title.css
+++ b/src/Title.css
@@ -10,6 +10,10 @@
margin-bottom: 1em;
}
+.title-bar a {
+ padding: 0 .5em;
+}
+
.title {
font-size: 2em;
line-height: 3em;
diff --git a/src/Title.js b/src/Title.js
index 22a1df7..78be9ca 100644
--- a/src/Title.js
+++ b/src/Title.js
@@ -42,6 +42,16 @@ class ControlBar extends Component {
this.set_search_text=props.set_search_text;
}
+ componentDidMount() {
+ if(window.location.hash) {
+ const text=window.location.hash.substr(1);
+ this.setState({
+ search_text: text,
+ });
+ this.set_search_text(text);
+ }
+ }
+
on_change(event) {
this.setState({
search_text: event.target.value,
diff --git a/src/index.css b/src/index.css
index c977ccd..9c0346a 100644
--- a/src/index.css
+++ b/src/index.css
@@ -22,7 +22,6 @@ a {
text-decoration: none;
color: #00c;
cursor: pointer;
- padding: 0 .5em;
}
input {