From 2dea567dcfcdfcd016c0da55a120c6e854760fb0 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Wed, 28 Jun 2017 10:30:02 -0400 Subject: Added MarkdownImage component (#6774) * Added MarkdownImage component * Fixed unit tests --- webapp/components/markdown_image.jsx | 19 +++++++++++++++++++ .../post_message_view/post_message_view.jsx | 17 +++++++++++++++++ webapp/tests/utils/formatting_imgs.test.jsx | 10 +++++----- webapp/utils/markdown.jsx | 22 +--------------------- 4 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 webapp/components/markdown_image.jsx diff --git a/webapp/components/markdown_image.jsx b/webapp/components/markdown_image.jsx new file mode 100644 index 000000000..75a6ce9ea --- /dev/null +++ b/webapp/components/markdown_image.jsx @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import React, {PureComponent} from 'react'; + +import {postListScrollChange} from 'actions/global_actions.jsx'; + +export default class MarkdownImage extends PureComponent { + handleLoad = () => { + postListScrollChange(); + } + + render() { + const props = {...this.props}; + props.onLoad = this.handleLoad; + + return ; + } +} diff --git a/webapp/components/post_view/post_message_view/post_message_view.jsx b/webapp/components/post_view/post_message_view/post_message_view.jsx index d066183ff..76037741f 100644 --- a/webapp/components/post_view/post_message_view/post_message_view.jsx +++ b/webapp/components/post_view/post_message_view/post_message_view.jsx @@ -7,6 +7,7 @@ import React from 'react'; import {FormattedMessage} from 'react-intl'; import AtMention from 'components/at_mention'; +import MarkdownImage from 'components/markdown_image'; import store from 'stores/redux_store.jsx'; @@ -113,6 +114,22 @@ export default class PostMessageView extends React.PureComponent { return ; } }, + { + shouldProcessNode: (node) => node.type === 'tag' && node.name === 'img', + processNode: (node) => { + const { + class: className, + ...attribs + } = node.attribs; + + return ( + + ); + } + }, { shouldProcessNode: () => true, processNode: processNodeDefinitions.processDefaultNode diff --git a/webapp/tests/utils/formatting_imgs.test.jsx b/webapp/tests/utils/formatting_imgs.test.jsx index 645a648d1..3e19a1e06 100644 --- a/webapp/tests/utils/formatting_imgs.test.jsx +++ b/webapp/tests/utils/formatting_imgs.test.jsx @@ -9,7 +9,7 @@ describe('Markdown.Imgs', function() { it('Inline mage', function(done) { assert.equal( Markdown.format('![Mattermost](/images/icon.png)').trim(), - '

Mattermost

' + '

Mattermost

' ); done(); @@ -18,7 +18,7 @@ describe('Markdown.Imgs', function() { it('Image with hover text', function(done) { assert.equal( Markdown.format('![Mattermost](/images/icon.png "Mattermost Icon")').trim(), - '

Mattermost

' + '

Mattermost

' ); done(); @@ -27,7 +27,7 @@ describe('Markdown.Imgs', function() { it('Image with link', function(done) { assert.equal( Markdown.format('[![Mattermost](../../images/icon-76x76.png)](https://github.com/mattermost/platform)').trim(), - '

Mattermost

' + '

Mattermost

' ); done(); @@ -36,7 +36,7 @@ describe('Markdown.Imgs', function() { it('Image with width and height', function(done) { assert.equal( Markdown.format('![Mattermost](../../images/icon-76x76.png =50x76 "Mattermost Icon")').trim(), - '

Mattermost

' + '

Mattermost

' ); done(); @@ -45,7 +45,7 @@ describe('Markdown.Imgs', function() { it('Image with width', function(done) { assert.equal( Markdown.format('![Mattermost](../../images/icon-76x76.png =50 "Mattermost Icon")').trim(), - '

Mattermost

' + '

Mattermost

' ); done(); diff --git a/webapp/utils/markdown.jsx b/webapp/utils/markdown.jsx index 65c4c0396..8733e6200 100644 --- a/webapp/utils/markdown.jsx +++ b/webapp/utils/markdown.jsx @@ -4,29 +4,9 @@ import * as TextFormatting from './text_formatting.jsx'; import * as SyntaxHighlighting from './syntax_highlighting.jsx'; -import {postListScrollChange} from 'actions/global_actions.jsx'; - import marked from 'marked'; import katex from 'katex'; -function markdownImageLoaded(image) { - if (image.hasAttribute('height') && image.attributes.height.value !== 'auto') { - const maxHeight = parseInt(global.getComputedStyle(image).maxHeight, 10); - - if (image.attributes.height.value > maxHeight) { - image.style.height = maxHeight + 'px'; - image.style.width = ((maxHeight * image.attributes.width.value) / image.attributes.height.value) + 'px'; - } else { - image.style.height = image.attributes.height.value + 'px'; - } - } else { - image.style.height = 'auto'; - } - - postListScrollChange(); -} -global.markdownImageLoaded = markdownImageLoaded; - class MattermostMarkdownRenderer extends marked.Renderer { constructor(options, formattingOptions = {}) { super(options); @@ -155,7 +135,7 @@ class MattermostMarkdownRenderer extends marked.Renderer { if (dimensions.length > 1) { out += ' height="' + dimensions[1] + '"'; } - out += ' onload="window.markdownImageLoaded(this)" onerror="window.markdownImageLoaded(this)" class="markdown-inline-img"'; + out += ' class="markdown-inline-img"'; out += this.options.xhtml ? '/>' : '>'; return out; } -- cgit v1.2.3-1-g7c22