Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dashboard] handle markdown error #9350

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import cx from 'classnames';
import AceEditor from 'react-ace';
import 'brace/mode/markdown';
import 'brace/theme/textmate';
import { t } from '@superset-ui/translation';

import DeleteComponentButton from '../DeleteComponentButton';
import DragDroppable from '../dnd/DragDroppable';
Expand All @@ -49,6 +50,9 @@ const propTypes = {

// from redux
logEvent: PropTypes.func.isRequired,
addDangerToast: PropTypes.func.isRequired,
undoLength: PropTypes.number.isRequired,
redoLength: PropTypes.number.isRequired,

// grid related
availableColumnCount: PropTypes.number.isRequired,
Expand All @@ -65,14 +69,16 @@ const propTypes = {

const defaultProps = {};

const markdownPlaceHolder = `# ✨Markdown
const MARKDOWN_PLACE_HOLDER = `# ✨Markdown
## ✨Markdown
### ✨Markdown

<br />

Click here to edit [markdown](https://bit.ly/1dQOfRK)`;

const MARKDOWN_ERROR_MESSAGE = t('This markdown component has an error.');
Copy link
Author

@graceguo-supercat graceguo-supercat Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rename 2 pre-defined markdown constant as MARKDOWN_ERROR_MESSAGE and MARKDOWN_PLACE_HOLDER


function isSafeMarkup(node) {
if (node.type === 'html') {
return /href="(javascript|vbscript|file):.*"/gim.test(node.value) === false;
Expand All @@ -88,6 +94,8 @@ class Markdown extends React.PureComponent {
markdownSource: props.component.meta.code,
editor: null,
editorMode: 'preview',
undoLength: props.undoLength,
redoLength: props.redoLength,
};
this.renderStartTime = Logger.getTimestamp();

Expand All @@ -107,11 +115,46 @@ class Markdown extends React.PureComponent {
});
}

UNSAFE_componentWillReceiveProps(nextProps) {
const nextSource = nextProps.component.meta.code;
if (this.state.markdownSource !== nextSource) {
this.setState({ markdownSource: nextSource });
static getDerivedStateFromProps(nextProps, state) {
const {
hasError,
editorMode,
markdownSource,
undoLength,
redoLength,
} = state;
const {
component: nextComponent,
undoLength: nextUndoLength,
redoLength: nextRedoLength,
} = nextProps;
// user click undo or redo ?
if (nextUndoLength !== undoLength || nextRedoLength !== redoLength) {
return {
...state,
undoLength: nextUndoLength,
redoLength: nextRedoLength,
markdownSource: nextComponent.meta.code,
hasError: false,
};
} else if (
!hasError &&
editorMode === 'preview' &&
nextComponent.meta.code !== markdownSource
) {
return {
...state,
markdownSource: nextComponent.meta.code,
};
}

return state;
}

static getDerivedStateFromError() {
return {
hasError: true,
};
}

componentDidUpdate(prevProps) {
Expand All @@ -124,6 +167,16 @@ class Markdown extends React.PureComponent {
}
}

componentDidCatch(error, info) {
if (this.state.editor && this.state.editorMode === 'preview') {
this.props.addDangerToast(
t(
'This markdown component has an error. Please revert your recent changes.',
),
);
}
}

setEditor(editor) {
editor.getSession().setUseWrapMode(true);
this.setState({
Expand All @@ -139,7 +192,12 @@ class Markdown extends React.PureComponent {
}

handleChangeEditorMode(mode) {
if (this.state.editorMode === 'edit') {
const nextState = {
...this.state,
editorMode: mode,
};

if (mode === 'preview') {
const { updateComponents, component } = this.props;
if (component.meta.code !== this.state.markdownSource) {
updateComponents({
Expand All @@ -152,11 +210,10 @@ class Markdown extends React.PureComponent {
},
});
}
nextState.hasError = false;
}

this.setState(() => ({
editorMode: mode,
}));
this.setState(nextState);
}

handleMarkdownChange(nextValue) {
Expand Down Expand Up @@ -184,7 +241,7 @@ class Markdown extends React.PureComponent {
// thisl allows "select all => delete" to give an empty editor
typeof this.state.markdownSource === 'string'
? this.state.markdownSource
: markdownPlaceHolder
: MARKDOWN_PLACE_HOLDER
}
readOnly={false}
onLoad={this.setEditor}
Expand All @@ -193,9 +250,14 @@ class Markdown extends React.PureComponent {
}

renderPreviewMode() {
const { hasError } = this.state;
return (
<ReactMarkdown
source={this.state.markdownSource || markdownPlaceHolder}
source={
hasError
? MARKDOWN_ERROR_MESSAGE
: this.state.markdownSource || MARKDOWN_PLACE_HOLDER
}
escapeHtml={false}
allowNode={isSafeMarkup}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../actions/dashboardLayout';
import { setDirectPathToChild } from '../actions/dashboardState';
import { logEvent } from '../../logger/actions';
import { addDangerToast } from '../../messageToasts/actions';

const propTypes = {
component: componentShape.isRequired,
Expand Down Expand Up @@ -66,6 +67,8 @@ function mapStateToProps(
component,
parentComponent: dashboardLayout[parentId],
editMode: dashboardState.editMode,
undoLength: undoableLayout.past.length,
redoLength: undoableLayout.future.length,
filters: getActiveFilters(),
directPathToChild: dashboardState.directPathToChild,
directPathLastUpdated: dashboardState.directPathLastUpdated,
Expand Down Expand Up @@ -97,6 +100,7 @@ function mapStateToProps(
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
addDangerToast,
createComponent,
deleteComponent,
updateComponents,
Expand Down