Ghost/core/server/lib/mobiledoc/create-card.js
Kevin Ansfield 8b787ac803 🐛 Fixed unnecessary "unsaved changes" modal when using HR cards
no issue

- the `hr` mobiledoc card does not specify an `absoluteToRelative` or `relativeToAbsolute` transformer function so falls back to the default transformer
- the default transformer function's arguments were not correct which meant that the UrlUtils object was replacing the card's typical empty-object payload
- the card's payload changing when saving mobiledoc was triggering the editor's unsaved changes warning because the API response no longer matched what was in the editor
2020-01-22 11:59:13 +00:00

66 lines
2.0 KiB
JavaScript

let urlUtils;
module.exports = function createCard(card) {
const defaultTransformer = function (urlUtils, payload) {
return payload;
};
const {
name,
type,
config = {},
absoluteToRelative = defaultTransformer,
relativeToAbsolute = defaultTransformer
} = card;
return {
name,
type,
render({env, payload, options}) {
const {dom} = env;
const cleanName = name.replace(/^card-/, '');
const cardOutput = card.render({env, payload, options});
if (!cardOutput) {
return cardOutput;
}
if (config.commentWrapper) {
const beginComment = dom.createComment(`kg-card-begin: ${cleanName}`);
const endComment = dom.createComment(`kg-card-end: ${cleanName}`);
const fragment = dom.createDocumentFragment();
fragment.appendChild(beginComment);
fragment.appendChild(cardOutput);
fragment.appendChild(endComment);
return fragment;
}
return cardOutput;
},
absoluteToRelative() {
// it's necessary to wait until the method is called to require
// urlUtils to ensure the class has actually been instantiated
// as cards are passed in as an arg to the class instantiation
if (!urlUtils) {
urlUtils = require('../url-utils');
}
return absoluteToRelative(urlUtils, ...arguments);
},
relativeToAbsolute() {
// it's necessary to wait until the method is called to require
// urlUtils to ensure the class has actually been instantiated
// as cards are passed in as an arg to the class instantiation
if (!urlUtils) {
urlUtils = require('../url-utils');
}
return relativeToAbsolute(urlUtils, ...arguments);
}
};
};