1
0
mirror of https://github.com/TryGhost/Ghost.git synced 2024-12-29 13:52:10 +03:00
Ghost/ghost/admin/app/routes/editor/edit.js
Kevin Ansfield 2d9dd4639d Added redirect between mobiledoc/lexical editors
no issue

- mobiledoc and lexical editors will co-exist for some time so we want to make using both as straight forward as possible
- posts can only have one of `lexical` or `mobiledoc` properties set so in the `edit` route for each editor we detect the opposite property being present and redirect so the right editor is loaded for each post
2022-09-13 18:55:39 +01:00

77 lines
2.4 KiB
JavaScript

import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import {pluralize} from 'ember-inflector';
import {inject as service} from '@ember/service';
export default class EditRoute extends AuthenticatedRoute {
@service router;
beforeModel(transition) {
super.beforeModel(...arguments);
// if the transition is not new->edit, reset the post on the controller
// so that the editor view is cleared before showing the loading state
if (transition.urlMethod !== 'replace') {
let editor = this.controllerFor('editor');
editor.set('post', null);
editor.reset();
}
}
async model(params, transition) {
// eslint-disable-next-line camelcase
let {type: modelName, post_id} = params;
if (!['post', 'page'].includes(modelName)) {
let path = transition.intent.url.replace(/^\//, '');
return this.replaceWith('error404', {path, status: 404});
}
let query = {
// eslint-disable-next-line camelcase
id: post_id
};
const records = await this.store.query(modelName, query);
const post = records.firstObject;
if (post.lexical) {
return this.router.transitionTo('lexical-editor.edit', post);
}
return post;
}
// the API will return a post even if the logged in user doesn't have
// permission to edit it (all posts are public) so we need to do our
// own permissions check and redirect if necessary
afterModel(post) {
super.afterModel(...arguments);
const user = this.session.user;
const returnRoute = pluralize(post.constructor.modelName);
if (user.isAuthorOrContributor && !post.isAuthoredByUser(user)) {
return this.replaceWith(returnRoute);
}
// If the post is not a draft and user is contributor, redirect to index
if (user.isContributor && !post.isDraft) {
return this.replaceWith(returnRoute);
}
}
serialize(model) {
return {
type: model.constructor.modelName,
post_id: model.id
};
}
// there's no specific controller for this route, instead all editor
// handling is done on the editor route/controler
setupController(controller, post) {
let editor = this.controllerFor('editor');
editor.setPost(post);
}
}