-
Notifications
You must be signed in to change notification settings - Fork 38
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0fe75c6
chore(package.json): add two babel plugins
tomchentw 7933064
chore(js/term_view): remove unused properties
tomchentw fecb472
refactor(js/term_view): create components/Screen
tomchentw 2ebdef0
refactor(components/ImagePreviewer): promise-based resolver APIs
tomchentw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
But I can't really tell why we need
prePicRel
withtype=?
on the anchor tag. Mind to open an issue with more detailed description instead?There was a problem hiding this comment.
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:
PttChrome/src/js/term_view.js
Line 788 in e317cae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Created #25