-
-
Notifications
You must be signed in to change notification settings - Fork 846
Allow extensions to use route resolvers #2275
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6faec29
mapRoutes: don't wrap components in resolvers if they are already res…
askvortsov1 cad54c3
Extract defaultResolver into it's own util
askvortsov1 53d6267
Use class for DefaultResolver to improve extensibility
askvortsov1 a497f5f
Allow either route resolver instances, or components with an optional…
askvortsov1 6d80b17
Move default resolver to resolvers folder, add a makeKey method
askvortsov1 19ee095
Improve resolver docblock
askvortsov1 2fba8e3
Introduce a resolver for DiscussionPage, so that routing from one pos…
askvortsov1 ae7f08e
Add makeKey docblock
askvortsov1 a83d18e
Ensure that we're passing in an integer to scroll
askvortsov1 4ae7391
Return a single-child component in DefaultResolver.render()
askvortsov1 37ef9c3
Move getDiscussionIdFromSlug into its own function
askvortsov1 a908e4e
Typo fix
askvortsov1 596a1d5
Move scrolling into a timeout
askvortsov1 0a1d4ca
Export DiscussionPageResolver in compat
askvortsov1 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 hidden or 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 hidden or 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,34 @@ | ||
import Mithril from 'mithril'; | ||
|
||
/** | ||
* Generates a route resolver for a given component. | ||
* In addition to regular route resolver functionality: | ||
* - It provide the current route name as an attr | ||
* - It sets a key on the component so a rerender will be triggered on route change. | ||
*/ | ||
export default class DefaultResolver { | ||
component: Mithril.Component; | ||
routeName: string; | ||
|
||
constructor(component, routeName) { | ||
this.component = component; | ||
this.routeName = routeName; | ||
} | ||
|
||
/** | ||
* When a route change results in a changed key, a full page | ||
* rerender occurs. This method can be overriden in subclasses | ||
* to prevent rerenders on some route changes. | ||
*/ | ||
makeKey() { | ||
return this.routeName + JSON.stringify(m.route.param()); | ||
} | ||
|
||
onmatch(args, requestedPath, route) { | ||
return this.component; | ||
} | ||
|
||
render(vnode) { | ||
return [{ ...vnode, routeName: this.routeName, key: this.makeKey() }]; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,47 @@ | ||
import DefaultResolver from '../../common/resolvers/DefaultResolver'; | ||
|
||
/** | ||
* This isn't exported as it is a temporary measure. | ||
* A more robust system will be implemented alongside UTF-8 support in beta 15. | ||
*/ | ||
function getDiscussionIdFromSlug(slug: string | undefined) { | ||
if (!slug) return; | ||
return slug.split('-')[0]; | ||
} | ||
|
||
/** | ||
* A custom route resolver for DiscussionPage that generates the same key to all posts | ||
* on the same discussion. It triggers a scroll when going from one post to another | ||
* in the same discussion. | ||
*/ | ||
export default class DiscussionPageResolver extends DefaultResolver { | ||
static scrollToPostNumber: number | null = null; | ||
|
||
makeKey() { | ||
const params = { ...m.route.param() }; | ||
if ('near' in params) { | ||
delete params.near; | ||
} | ||
params.id = getDiscussionIdFromSlug(params.id); | ||
return this.routeName.replace('.near', '') + JSON.stringify(params); | ||
} | ||
|
||
onmatch(args, requestedPath, route) { | ||
if (route.includes('/d/:id') && getDiscussionIdFromSlug(args.id) === getDiscussionIdFromSlug(m.route.param('id'))) { | ||
DiscussionPageResolver.scrollToPostNumber = parseInt(args.near); | ||
} | ||
|
||
return super.onmatch(args, requestedPath, route); | ||
} | ||
|
||
render(vnode) { | ||
if (DiscussionPageResolver.scrollToPostNumber !== null) { | ||
const number = DiscussionPageResolver.scrollToPostNumber; | ||
// Scroll after a timeout to avoid clashes with the render. | ||
setTimeout(() => app.current.get('stream').goToNumber(number)); | ||
DiscussionPageResolver.scrollToPostNumber = null; | ||
} | ||
|
||
return super.render(vnode); | ||
} | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.