Skip to content

Commit 394d4cf

Browse files
authored
Merge pull request element-hq#5995 from vector-im/t3chguy/highlight_things
Highlight ViewSource and Devtools ViewSource
2 parents c7146e6 + 22b7298 commit 394d4cf

5 files changed

Lines changed: 91 additions & 8 deletions

File tree

‎src/components/structures/ViewSource.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ limitations under the License.
1616

1717
'use strict';
1818

19-
var React = require('react');
19+
import React from 'react';
20+
import PropTypes from 'prop-types';
21+
import SyntaxHighlight from '../views/elements/SyntaxHighlight';
22+
2023

2124
module.exports = React.createClass({
2225
displayName: 'ViewSource',
2326

2427
propTypes: {
25-
content: React.PropTypes.object.isRequired,
26-
onFinished: React.PropTypes.func.isRequired,
28+
content: PropTypes.object.isRequired,
29+
onFinished: PropTypes.func.isRequired,
2730
},
2831

2932
componentDidMount: function() {
@@ -45,9 +48,9 @@ module.exports = React.createClass({
4548
render: function() {
4649
return (
4750
<div className="mx_ViewSource">
48-
<pre>
49-
{JSON.stringify(this.props.content, null, 2)}
50-
</pre>
51+
<SyntaxHighlight className="json">
52+
{ JSON.stringify(this.props.content, null, 2) }
53+
</SyntaxHighlight>
5154
</div>
5255
);
5356
}

‎src/components/views/dialogs/DevtoolsDialog.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import React from 'react';
1818
import PropTypes from 'prop-types';
1919
import sdk from 'matrix-react-sdk';
20+
import SyntaxHighlight from '../elements/SyntaxHighlight';
2021
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2122
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
2223

@@ -343,7 +344,9 @@ class RoomStateExplorer extends DevtoolsComponent {
343344

344345
return <div className="mx_ViewSource">
345346
<div className="mx_Dialog_content">
346-
<pre>{ JSON.stringify(this.state.event.event, null, 2) }</pre>
347+
<SyntaxHighlight className="json">
348+
{ JSON.stringify(this.state.event.event, null, 2) }
349+
</SyntaxHighlight>
347350
</div>
348351
<div className="mx_Dialog_buttons">
349352
<button onClick={this.onBack}>{ _t('Back') }</button>
@@ -459,7 +462,9 @@ class AccountDataExplorer extends DevtoolsComponent {
459462

460463
return <div className="mx_ViewSource">
461464
<div className="mx_Dialog_content">
462-
<pre>{ JSON.stringify(this.state.event.event, null, 2) }</pre>
465+
<SyntaxHighlight className="json">
466+
{ JSON.stringify(this.state.event.event, null, 2) }
467+
</SyntaxHighlight>
463468
</div>
464469
<div className="mx_Dialog_buttons">
465470
<button onClick={this.onBack}>{ _t('Back') }</button>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from 'react';
18+
import PropTypes from 'prop-types';
19+
import {highlightBlock} from 'highlight.js';
20+
21+
export default class SyntaxHighlight extends React.Component {
22+
static propTypes = {
23+
className: PropTypes.string,
24+
children: PropTypes.node,
25+
};
26+
27+
constructor(props, context) {
28+
super(props, context);
29+
30+
this._ref = this._ref.bind(this);
31+
}
32+
33+
// componentDidUpdate used here for reusability
34+
// componentWillReceiveProps fires too early to call highlightBlock on.
35+
componentDidUpdate() {
36+
if (this._el) highlightBlock(this._el);
37+
}
38+
39+
// call componentDidUpdate because _ref is fired on initial render
40+
// which does not fire componentDidUpdate
41+
_ref(el) {
42+
this._el = el;
43+
this.componentDidUpdate();
44+
}
45+
46+
render() {
47+
const { className, children } = this.props;
48+
49+
return <pre className={`${className} mx_SyntaxHighlight`} ref={this._ref}>
50+
<code>{ children }</code>
51+
</pre>;
52+
}
53+
}

‎src/skins/vector/css/_components.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
@import "./vector-web/views/elements/_ImageView.scss";
9797
@import "./vector-web/views/elements/_InlineSpinner.scss";
9898
@import "./vector-web/views/elements/_Spinner.scss";
99+
@import "./vector-web/views/elements/_SyntaxHighlight.scss";
99100
@import "./vector-web/views/globals/_MatrixToolbar.scss";
100101
@import "./vector-web/views/messages/_DateSeparator.scss";
101102
@import "./vector-web/views/messages/_MessageTimestamp.scss";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
.mx_SyntaxHighlight {
18+
/* inhibit hljs styling */
19+
background: none !important;
20+
color: $light-fg-color !important;
21+
}

0 commit comments

Comments
 (0)