mirror of
https://github.com/flarum/core.git
synced 2025-08-11 19:04:29 +02:00
Made frontend discussion slug independent
This commit is contained in:
@@ -14,6 +14,7 @@ import DiscussionControls from '../utils/DiscussionControls';
|
|||||||
import slidable from '../utils/slidable';
|
import slidable from '../utils/slidable';
|
||||||
import extractText from '../../common/utils/extractText';
|
import extractText from '../../common/utils/extractText';
|
||||||
import classList from '../../common/utils/classList';
|
import classList from '../../common/utils/classList';
|
||||||
|
import DiscussionPage from './DiscussionPage';
|
||||||
|
|
||||||
import { escapeRegExp } from 'lodash-es';
|
import { escapeRegExp } from 'lodash-es';
|
||||||
/**
|
/**
|
||||||
@@ -156,9 +157,7 @@ export default class DiscussionListItem extends Component {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
active() {
|
active() {
|
||||||
const idParam = m.route.param('id');
|
return app.current.matches(DiscussionPage, { discussion: this.attrs.discussion });
|
||||||
|
|
||||||
return idParam && idParam.split('-')[0] === this.attrs.discussion.id();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,15 +1,6 @@
|
|||||||
import DefaultResolver from '../../common/resolvers/DefaultResolver';
|
import DefaultResolver from '../../common/resolvers/DefaultResolver';
|
||||||
import DiscussionPage from '../components/DiscussionPage';
|
import DiscussionPage from '../components/DiscussionPage';
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* 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
|
* on the same discussion. It triggers a scroll when going from one post to another
|
||||||
@@ -18,17 +9,32 @@ function getDiscussionIdFromSlug(slug: string | undefined) {
|
|||||||
export default class DiscussionPageResolver extends DefaultResolver {
|
export default class DiscussionPageResolver extends DefaultResolver {
|
||||||
static scrollToPostNumber: string | null = null;
|
static scrollToPostNumber: string | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove optional parts of a discussion's slug to keep the substring
|
||||||
|
* that bijectively maps to a discussion object. By default this just
|
||||||
|
* extracts the numerical ID from the slug. If a custom discussion
|
||||||
|
* slugging driver is used, this may need to be overriden.
|
||||||
|
* @param slug
|
||||||
|
*/
|
||||||
|
canonicalizeDiscussionSlug(slug: string | undefined) {
|
||||||
|
if (!slug) return;
|
||||||
|
return slug.split('-')[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
makeKey() {
|
makeKey() {
|
||||||
const params = { ...m.route.param() };
|
const params = { ...m.route.param() };
|
||||||
if ('near' in params) {
|
if ('near' in params) {
|
||||||
delete params.near;
|
delete params.near;
|
||||||
}
|
}
|
||||||
params.id = getDiscussionIdFromSlug(params.id);
|
params.id = this.canonicalizeDiscussionSlug(params.id);
|
||||||
return this.routeName.replace('.near', '') + JSON.stringify(params);
|
return this.routeName.replace('.near', '') + JSON.stringify(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
onmatch(args, requestedPath, route) {
|
onmatch(args, requestedPath, route) {
|
||||||
if (app.current.matches(DiscussionPage) && getDiscussionIdFromSlug(args.id) === getDiscussionIdFromSlug(m.route.param('id'))) {
|
if (app.current.matches(DiscussionPage) && this.canonicalizeDiscussionSlug(args.id) === this.canonicalizeDiscussionSlug(m.route.param('id'))) {
|
||||||
// By default, the first post number of any discussion is 1
|
// By default, the first post number of any discussion is 1
|
||||||
DiscussionPageResolver.scrollToPostNumber = args.near || '1';
|
DiscussionPageResolver.scrollToPostNumber = args.near || '1';
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user