1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00

Koenig - Display-only image card with markdown text expansion

refs https://github.com/TryGhost/Ghost/issues/9311
- adds the `koenig-card-image` card that renders an `<img>` element
- adds text expansion to convert markdown images into the new image card
This commit is contained in:
Kevin Ansfield 2018-01-30 20:46:03 +00:00
parent 2093b34c36
commit 31fbb042de
6 changed files with 71 additions and 1 deletions

View file

@ -0,0 +1,7 @@
import Component from '@ember/component';
import layout from '../templates/components/koenig-card-image';
export default Component.extend({
tagName: '',
layout
});

View file

@ -1,5 +1,6 @@
import createComponentCard from '../utils/create-component-card';
export default [
createComponentCard('koenig-card-hr')
createComponentCard('koenig-card-hr'),
createComponentCard('koenig-card-image')
];

View file

@ -34,6 +34,7 @@ export default function (editor) {
break;
case ')':
matchLink(postEditor, text);
matchImage(postEditor, text);
break;
case '~':
matchStrikethrough(postEditor, text);
@ -173,6 +174,41 @@ export default function (editor) {
}
}
function matchImage(editor, text) {
let matches = text.match(/^!\[(.*?)\]\((.*?)\)$/);
if (matches) {
let {range: {head, head: {section}}} = editor;
let src = matches[2];
let alt = matches[1];
// skip if cursor is not at end of section
if (!head.isTail()) {
return;
}
// mobiledoc lists don't support cards
if (section.isListItem) {
return;
}
editor.run((postEditor) => {
let card = postEditor.builder.createCardSection('koenig-card-image', {src, alt});
// need to check the section before replacing else it will always
// add a trailing paragraph
let needsTrailingParagraph = !section.next;
editor.range.extend(-(matches[0].length));
postEditor.replaceSection(editor.range.headSection, card);
if (needsTrailingParagraph) {
let newSection = editor.builder.createMarkupSection('p');
postEditor.insertSectionAtEnd(newSection);
postEditor.setRange(newSection.tailPosition());
}
});
}
}
function matchStrikethrough(editor, text) {
let {range} = editor;
let matches = text.match(/~(.+?)~$/);

View file

@ -0,0 +1 @@
<img src={{payload.src}} alt={{payload.alt}} />

View file

@ -0,0 +1 @@
export {default} from 'koenig-editor/components/koenig-card-image';

View file

@ -0,0 +1,24 @@
import hbs from 'htmlbars-inline-precompile';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {setupComponentTest} from 'ember-mocha';
describe('Integration: Component: koenig-card-image', function () {
setupComponentTest('koenig-card-image', {
integration: true
});
it('renders', function () {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });
// Template block usage:
// this.render(hbs`
// {{#koenig-card-image}}
// template content
// {{/koenig-card-image}}
// `);
this.render(hbs`{{koenig-card-image}}`);
expect(this.$()).to.have.length(1);
});
});