Commit 7968c9e9 authored by Jacques Erasmus's avatar Jacques Erasmus Committed by Miguel Rincon

Add custom renderer for images

Added a custom renderer for images
parent 3bb8e20d
...@@ -10,6 +10,7 @@ import { DEFAULT_IMAGE_UPLOAD_PATH } from '../constants'; ...@@ -10,6 +10,7 @@ import { DEFAULT_IMAGE_UPLOAD_PATH } from '../constants';
import imageRepository from '../image_repository'; import imageRepository from '../image_repository';
import formatter from '../services/formatter'; import formatter from '../services/formatter';
import templater from '../services/templater'; import templater from '../services/templater';
import renderImage from '../services/renderers/render_image';
export default { export default {
components: { components: {
...@@ -41,6 +42,10 @@ export default { ...@@ -41,6 +42,10 @@ export default {
type: Array, type: Array,
required: true, required: true,
}, },
project: {
type: String,
required: true,
},
imageRoot: { imageRoot: {
type: String, type: String,
required: false, required: false,
...@@ -72,6 +77,12 @@ export default { ...@@ -72,6 +77,12 @@ export default {
isWysiwygMode() { isWysiwygMode() {
return this.editorMode === EDITOR_TYPES.wysiwyg; return this.editorMode === EDITOR_TYPES.wysiwyg;
}, },
customRenderers() {
const imageRenderer = renderImage.build(this.mounts, this.project);
return {
image: [imageRenderer],
};
},
}, },
created() { created() {
this.refreshEditHelpers(); this.refreshEditHelpers();
...@@ -140,6 +151,7 @@ export default { ...@@ -140,6 +151,7 @@ export default {
:content="editableContent" :content="editableContent"
:initial-edit-type="editorMode" :initial-edit-type="editorMode"
:image-root="imageRoot" :image-root="imageRoot"
:options="{ customRenderers }"
class="mb-9 pb-6 h-100" class="mb-9 pb-6 h-100"
@modeChange="onModeChange" @modeChange="onModeChange"
@input="onInputChange" @input="onInputChange"
......
...@@ -139,6 +139,7 @@ export default { ...@@ -139,6 +139,7 @@ export default {
:saving-changes="isSavingChanges" :saving-changes="isSavingChanges"
:return-url="appData.returnUrl" :return-url="appData.returnUrl"
:mounts="appData.mounts" :mounts="appData.mounts"
:project="appData.project"
@submit="onPrepareSubmit" @submit="onPrepareSubmit"
/> />
<edit-meta-modal <edit-meta-modal
......
const canRender = ({ type }) => type === 'image';
// NOTE: the `metadata` is not used yet, but will be used in a follow-up iteration
// To be removed with the next iteration of https://gitlab.com/gitlab-org/gitlab/-/issues/218531
// eslint-disable-next-line no-unused-vars
let metadata;
const render = (node, { skipChildren }) => {
skipChildren();
// To be removed with the next iteration of https://gitlab.com/gitlab-org/gitlab/-/issues/218531
// TODO resolve relative paths
return {
type: 'openTag',
tagName: 'img',
selfClose: true,
attributes: {
src: node.destination,
alt: node.firstChild.literal,
},
};
};
const build = (mounts, project) => {
metadata = { mounts, project };
return { canRender, render };
};
export default { build };
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
sourceContentBody as body, sourceContentBody as body,
returnUrl, returnUrl,
mounts, mounts,
project,
} from '../mock_data'; } from '../mock_data';
jest.mock('~/static_site_editor/services/formatter', () => jest.fn(str => `${str} format-pass`)); jest.mock('~/static_site_editor/services/formatter', () => jest.fn(str => `${str} format-pass`));
...@@ -33,6 +34,7 @@ describe('~/static_site_editor/components/edit_area.vue', () => { ...@@ -33,6 +34,7 @@ describe('~/static_site_editor/components/edit_area.vue', () => {
content, content,
returnUrl, returnUrl,
mounts, mounts,
project,
savingChanges, savingChanges,
...propsData, ...propsData,
}, },
......
...@@ -27,6 +27,7 @@ export const sourceContentTitle = 'Handbook'; ...@@ -27,6 +27,7 @@ export const sourceContentTitle = 'Handbook';
export const username = 'gitlabuser'; export const username = 'gitlabuser';
export const projectId = '123456'; export const projectId = '123456';
export const project = 'user1/project1';
export const returnUrl = 'https://www.gitlab.com'; export const returnUrl = 'https://www.gitlab.com';
export const sourcePath = 'foobar.md.html'; export const sourcePath = 'foobar.md.html';
export const mergeRequestMeta = { export const mergeRequestMeta = {
......
import imageRenderer from '~/static_site_editor/services/renderers/render_image';
import { mounts, project } from '../../mock_data';
describe('rich_content_editor/renderers/render_image', () => {
let renderer;
beforeEach(() => {
renderer = imageRenderer.build(mounts, project);
});
describe('build', () => {
it('builds a renderer object containing `canRender` and `render` functions', () => {
expect(renderer).toHaveProperty('canRender', expect.any(Function));
expect(renderer).toHaveProperty('render', expect.any(Function));
});
});
describe('canRender', () => {
it.each`
input | result
${{ type: 'image' }} | ${true}
${{ type: 'text' }} | ${false}
${{ type: 'htmlBlock' }} | ${false}
`('returns $result when input is $input', ({ input, result }) => {
expect(renderer.canRender(input)).toBe(result);
});
});
describe('render', () => {
let context;
let result;
const skipChildren = jest.fn();
beforeEach(() => {
const node = {
destination: '/some/path/image.png',
firstChild: {
type: 'img',
literal: 'Some Image',
},
};
context = { skipChildren };
result = renderer.render(node, context);
});
it('invokes `skipChildren`', () => {
expect(skipChildren).toHaveBeenCalled();
});
it('returns an image', () => {
expect(result).toEqual({
type: 'openTag',
tagName: 'img',
selfClose: true,
attributes: {
src: '/some/path/image.png',
alt: 'Some Image',
},
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment