Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/term view #21

Merged
merged 4 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"name": "pttchrome",
"version": "1.2.0",
"dependencies": {
"base58": "^1.0.1",
"classnames": "^2.2.5"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"cross-env": "^5.1.0",
Expand Down Expand Up @@ -39,9 +42,11 @@
"modules": false
}
],
[
"react"
]
["react"]
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
},
"production": {
Expand All @@ -52,9 +57,11 @@
"modules": false
}
],
[
"react"
]
["react"]
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
}
}
Expand Down
207 changes: 207 additions & 0 deletions src/components/ImagePreviewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import { stringify } from "querystring";
import { decode } from "base58";

const noop = () => {};

export const of = src => Promise.resolve({ src });

export const resolveSrcToImageUrl = ({ src }) =>
imageUrlResolvers.find(r => r.test(src)).request(src);

export const resolveWithImageDOM = ({ src }) =>
new Promise((resolve, reject) => {
const img = new Image();
img.onload = () =>
resolve({
src,
height: img.height
});
img.onerror = reject;
img.src = src;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to preload image or to make sure it's a image? I think it should also do a simple check like in prePicRel before sending a request. We don't want to load every link user hovers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just preload image.
The non-parsable links are now handled in promise reject case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that prePicRel is no longer called in any places so I removed it (correct me if I'm wrong).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Call to that function was removed in 8618472, so it currently only loads whitelisted sites. It might be good to resurrect that back later. It doesn't have to be this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It's used here:

s0 += '<a scol="'+col+'" class="y q'+this.deffg+' b'+this.defbg+'" href="' +ch.getFullURL() + '"' + this.prePicRel( ch.getFullURL()) + ' rel="noreferrer" target="_blank" srow="'+row+'">';

But I can't really tell why we need prePicRel with type=? on the anchor tag. Mind to open an issue with more detailed description instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my understanding, it's used to annotate that the link is an image. Later, the it uses css selector to find all the images and does further processing:

".main a[type='p']",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Created #25

});

export class ImagePreviewer extends React.PureComponent {
state = {
pending: undefined,
value: undefined,
error: undefined
};

componentDidMount() {
this.handleStart();
}

componentDidUpdate(prevProps) {
if (this.props.request !== prevProps.request) {
this.handleStart();
}
}

handleStart(props) {
this.setState((state, { request }) => {
request.then(this.handleResolve, this.handleReject);
return {
pending: request,
value: undefined,
error: undefined
};
});
}

handleResolve = value => {
this.setState(({ pending }, { request }) => {
if (pending !== request) {
return;
}
return { value };
});
};

handleReject = error => {
this.setState(({ pending }, { request }) => {
if (pending !== request) {
return;
}
return { error };
});
};

render() {
return React.createElement(this.props.component, {
...this.props,
component: undefined,
request: undefined,
value: this.state.value,
error: this.state.error
});
}
}

const getTop = (top, height) => {
const pageHeight = $(window).height();

// opening image would pass the bottom of the page
if (top + height / 2 > pageHeight - 20) {
if (height / 2 < top) {
return pageHeight - 20 - height;
}
} else if (top - 20 > height / 2) {
return top - height / 2;
}
return 20;
};

ImagePreviewer.OnHover = ({ left, top, value, error }) => {
if (error) {
return false;
} else if (value) {
return (
<img
src={value.src}
style={{
display: "block",
position: "absolute",
left: left + 20,
top: getTop(top, value.height),
maxHeight: "80%",
maxWidth: "90%",
zIndex: 2
}}
/>
);
} else {
return (
<i
className="glyphicon glyphicon-refresh glyphicon-refresh-animate"
style={{
position: "absolute",
left: left + 20,
top: top,
zIndex: 2
}}
/>
);
}
};

ImagePreviewer.Inline = ({ value, error }) => {
if (error) {
return false;
} else if (value) {
return <img className="easyReadingImg hyperLinkPreview" src={value.src} />;
} else {
return (
<i className="glyphicon glyphicon-refresh glyphicon-refresh-animate" />
);
}
};

const imageUrlResolvers = [
{
/*
* Default
*/
test() {
return true;
},
request() {
return Promise.reject(new Error("Unimplemented"));
}
}
];

const registerImageUrlResolver = imageUrlResolvers.unshift.bind(
imageUrlResolvers
);

registerImageUrlResolver({
/*
* Flic.kr
*/
regex: /flic\.kr\/p\/(\w+)|flickr\.com\/photos\/[\w@]+\/(\d+)/,
test(src) {
return this.regex.test(src);
},
request(src) {
const [, flickrBase58Id, flickrPhotoId] = src.match(this.regex);
const photoId = flickrBase58Id ? decode(flickrBase58Id) : flickrPhotoId;

const apiURL = `https://api.flickr.com/services/rest/?${stringify({
method: "flickr.photos.getInfo",
api_key: "c8c95356e465b8d7398ff2847152740e",
photo_id: photoId,
format: "json",
nojsoncallback: 1
})}`;
return fetch(apiURL, {
mode: "cors"
})
.then(r => r.json())
.then(data => {
if (!data.photo) {
throw new Error("Not found");
}
const { farm, server: svr, id, secret } = data.photo;
return {
src: `https://farm${farm}.staticflickr.com/${svr}/${id}_${secret}.jpg`
};
});
}
});

registerImageUrlResolver({
/*
* imgur.com
*/
regex: /^https?:\/\/(i\.)?imgur\.com/,
test(src) {
return this.regex.test(src);
},
request(src) {
return Promise.resolve({
src: `${src.replace(this.regex, "https://i.imgur.com")}.jpg`
});
}
});

export default ImagePreviewer;
11 changes: 10 additions & 1 deletion src/components/Row/HyperLink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export const HyperLink = ({ col, row, href, inner }) => (
export const HyperLink = ({
col,
row,
href,
inner,
onMouseOver,
onMouseOut
}) => (
<a
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
scol={col} // FIXME: data-?
srow={row} // FIXME: data-?
className="y"
Expand Down
30 changes: 22 additions & 8 deletions src/components/Row/LinkSegmentBuilder.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import cx from "classnames";
import { HyperLinkPreview } from "../../js/image_preview";
import HyperLink from "./HyperLink";
import ColorSegmentBuilder from "./ColorSegmentBuilder";
import ImagePreviewer, { of, resolveSrcToImageUrl } from "../ImagePreviewer";

export class LinkSegmentBuilder {
constructor(row, showsLinkPreviews, forceWidth, highlighted) {
constructor(
row,
enableLinkInlinePreview,
forceWidth,
highlighted,
onHyperLinkMouseOver,
onHyperLinkMouseOut
) {
this.row = row;
this.forceWidth = forceWidth;
this.highlighted = highlighted;
this.onHyperLinkMouseOver = onHyperLinkMouseOver;
this.onHyperLinkMouseOut = onHyperLinkMouseOut;
//
this.segs = [];
this.linkPreviews = showsLinkPreviews ? [] : false;
this.inlineLinkPreviews = enableLinkInlinePreview ? [] : false;
//
this.colorSegBuilder = null;
this.col = null;
Expand All @@ -27,12 +36,18 @@ export class LinkSegmentBuilder {
inner={element}
data-scol={this.col}
data-srow={this.row}
onMouseOver={this.onHyperLinkMouseOver}
onMouseOut={this.onHyperLinkMouseOut}
/>
);
// TODO: Modularize this.
if (this.linkPreviews) {
this.linkPreviews.push(
<HyperLinkPreview key={this.col} src={this.href} />
if (this.inlineLinkPreviews) {
this.inlineLinkPreviews.push(
<ImagePreviewer
key={`${this.col}-${this.href}`}
request={of(this.href).then(resolveSrcToImageUrl)}
component={ImagePreviewer.Inline}
/>
);
}
} else {
Expand Down Expand Up @@ -61,14 +76,13 @@ export class LinkSegmentBuilder {
return (
<div>
<span
key="line"
className={cx({ b2: this.highlighted })}
data-type="bbsline"
data-row={this.row}
>
{this.segs}
</span>
<div key="previews">{this.linkPreviews}</div>
<div>{this.inlineLinkPreviews}</div>
</div>
);
}
Expand Down
41 changes: 20 additions & 21 deletions src/components/Row/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import LinkSegmentBuilder from "./LinkSegmentBuilder";

export class Row extends React.Component {
constructor() {
super();
this.state = { highlighted: false };
}

render() {
return this.props.chars
export const Row = ({
chars,
row,
enableLinkInlinePreview,
forceWidth,
highlighted,
onHyperLinkMouseOver,
onHyperLinkMouseOut
}) => (
<span type="bbsrow" srow={row}>
{chars
.reduce(
LinkSegmentBuilder.accumulator,
new LinkSegmentBuilder(
this.props.row,
this.props.showsLinkPreviews,
this.props.forceWidth,
this.state.highlighted
row,
enableLinkInlinePreview,
forceWidth,
highlighted,
onHyperLinkMouseOver,
onHyperLinkMouseOut
)
)
.build();
}

setHighlight(shouldHighlight) {
if (this.state.highlighted != shouldHighlight) {
this.setState({ highlighted: shouldHighlight });
}
}
}
.build()}
</span>
);

export default Row;
Loading