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

Add draw.io Integration #1685

Merged
merged 9 commits into from Mar 9, 2020
@@ -192,6 +192,7 @@
"lodash-webpack-plugin": "^0.11.5",
"markdown-it": "^10.0.0",
"markdown-it-blockdiag": "^1.0.2",
"markdown-it-drawio-viewer": "^1.1.0",
"markdown-it-emoji": "^1.4.0",
"markdown-it-footnote": "^3.0.1",
"markdown-it-mathjax": "^2.0.0",
@@ -79,6 +79,14 @@ module.exports = {
integrity: '',
},
},
{
name: 'drawio-viewer',
url: 'https://jgraph.github.io/drawio/src/main/webapp/js/viewer.min.js',
groups: ['basis'],
args: {
integrity: '',
},
},
],
style: [
{
@@ -0,0 +1,78 @@
import React from 'react';
import PropTypes from 'prop-types';

import { withTranslation } from 'react-i18next';

class Drawio extends React.Component {

constructor(props) {
super(props);

this.drawioContainer = React.createRef();

this.style = {
borderRadius: 3,
border: '1px solid #d7d7d7',
margin: '20px 0',
};

this.isPreview = this.props.isPreview;
this.drawioContent = this.props.drawioContent;

this.onEdit = this.onEdit.bind(this);
}

onEdit() {
if (window.crowi != null) {
window.crowi.launchDrawioIFrame('page',
this.props.rangeLineNumberOfMarkdown.beginLineNumber,
this.props.rangeLineNumberOfMarkdown.endLineNumber);
}
}

componentDidMount() {
const DrawioViewer = window.GraphViewer;
if (DrawioViewer != null) {
DrawioViewer.processElements();
}
}

renderContents() {
return this.drawioContent;
}

render() {
return (
<div className="editable-with-drawio position-relative">
{ !this.isPreview
&& (
<button type="button" className="drawio-iframe-trigger position-absolute btn" onClick={this.onEdit}>
<i className="icon-note mr-1"></i>{this.props.t('Edit')}
</button>
)
}
<div
className="drawio"
style={this.style}
ref={(c) => { this.drawioContainer = c }}
onScroll={(event) => {
event.preventDefault();
}}
dangerouslySetInnerHTML={{ __html: this.renderContents() }}
>
</div>
</div>
);
}

}

Drawio.propTypes = {
t: PropTypes.func.isRequired, // i18next
appContainer: PropTypes.object.isRequired,
drawioContent: PropTypes.any.isRequired,
isPreview: PropTypes.bool,
rangeLineNumberOfMarkdown: PropTypes.object.isRequired,
};

export default withTranslation()(Drawio);
@@ -11,7 +11,9 @@ import MarkdownTable from '../models/MarkdownTable';

import RevisionRenderer from './Page/RevisionRenderer';
import HandsontableModal from './PageEditor/HandsontableModal';
import DrawioIFrame from './PageEditor/DrawioIFrame';
import mtu from './PageEditor/MarkdownTableUtil';
import mdu from './PageEditor/MarkdownDrawioUtil';

const logger = loggerFactory('growi:Page');

@@ -22,11 +24,13 @@ class Page extends React.Component {

this.state = {
currentTargetTableArea: null,
currentTargetDrawioArea: null,
};

this.growiRenderer = this.props.appContainer.getRenderer('page');

this.saveHandlerForHandsontableModal = this.saveHandlerForHandsontableModal.bind(this);
this.saveHandlerForDrawioIFrame = this.saveHandlerForDrawioIFrame.bind(this);
}

componentWillMount() {
@@ -45,6 +49,19 @@ class Page extends React.Component {
this.handsontableModal.show(MarkdownTable.fromMarkdownString(tableLines));
}

/**
* launch DrawioIFrame with data specified by arguments
* @param beginLineNumber
* @param endLineNumber
*/
launchDrawioIFrame(beginLineNumber, endLineNumber) {
const markdown = this.props.pageContainer.state.markdown;
const drawioMarkdownArray = markdown.split(/\r\n|\r|\n/).slice(beginLineNumber, endLineNumber);
const drawioData = drawioMarkdownArray.slice(1, drawioMarkdownArray.length - 1).join('\n').trim();
this.setState({ currentTargetDrawioArea: { beginLineNumber, endLineNumber } });
this.drawioIFrame.show(drawioData);
}

async saveHandlerForHandsontableModal(markdownTable) {
const { pageContainer, editorContainer } = this.props;
const optionsToSave = editorContainer.getCurrentOptionsToSave();
@@ -75,6 +92,36 @@ class Page extends React.Component {
}
}

async saveHandlerForDrawioIFrame(drawioData) {
const { pageContainer, editorContainer } = this.props;
const optionsToSave = editorContainer.getCurrentOptionsToSave();

const newMarkdown = mdu.replaceDrawioInMarkdown(
drawioData,
this.props.pageContainer.state.markdown,
this.state.currentTargetDrawioArea.beginLineNumber,
this.state.currentTargetDrawioArea.endLineNumber,
);

try {
// disable unsaved warning
editorContainer.disableUnsavedWarning();

// eslint-disable-next-line no-unused-vars
const { page, tags } = await pageContainer.save(newMarkdown, optionsToSave);
logger.debug('success to save');

pageContainer.showSuccessToastr();
}
catch (error) {
logger.error('failed to save', error);
pageContainer.showErrorToastr(error);
}
finally {
this.setState({ currentTargetDrawioArea: null });
}
}

render() {
const isMobile = this.props.appContainer.isMobile;
const { markdown } = this.props.pageContainer.state;
@@ -83,6 +130,7 @@ class Page extends React.Component {
<div className={isMobile ? 'page-mobile' : ''}>
<RevisionRenderer growiRenderer={this.growiRenderer} markdown={markdown} />
<HandsontableModal ref={(c) => { this.handsontableModal = c }} onSave={this.saveHandlerForHandsontableModal} />
<DrawioIFrame ref={(c) => { this.drawioIFrame = c }} onSave={this.saveHandlerForDrawioIFrame} />
</div>
);
}
@@ -17,8 +17,10 @@ import EmojiAutoCompleteHelper from './EmojiAutoCompleteHelper';
import PreventMarkdownListInterceptor from './PreventMarkdownListInterceptor';
import MarkdownTableInterceptor from './MarkdownTableInterceptor';
import mtu from './MarkdownTableUtil';
import mdu from './MarkdownDrawioUtil';
import HandsontableModal from './HandsontableModal';
import EditorIcon from './EditorIcon';
import DrawioIFrame from './DrawioIFrame';

const loadScript = require('simple-load-script');
const loadCssSync = require('load-css-file');
@@ -94,6 +96,7 @@ export default class CodeMirrorEditor extends AbstractEditor {

this.makeHeaderHandler = this.makeHeaderHandler.bind(this);
this.showHandsonTableHandler = this.showHandsonTableHandler.bind(this);
this.showDrawioHandler = this.showDrawioHandler.bind(this);
}

init() {
@@ -647,6 +650,10 @@ export default class CodeMirrorEditor extends AbstractEditor {
this.handsontableModal.show(mtu.getMarkdownTable(this.getCodeMirror()));
}

showDrawioHandler() {
this.drawioIFrame.show(mdu.getMarkdownDrawioMxfile(this.getCodeMirror()));
}

getNavbarItems() {
return [
/* eslint-disable max-len */
@@ -746,6 +753,14 @@ export default class CodeMirrorEditor extends AbstractEditor {
>
<EditorIcon icon="Table" />
</Button>,
<Button
key="nav-item-drawio"
bsSize="small"
title="draw.io"
onClick={this.showDrawioHandler}
>
<EditorIcon icon="Drawio" />
</Button>,
/* eslint-able max-len */
];
}
@@ -824,6 +839,10 @@ export default class CodeMirrorEditor extends AbstractEditor {
ref={(c) => { this.handsontableModal = c }}
onSave={(table) => { return mtu.replaceFocusedMarkdownTableWithEditor(this.getCodeMirror(), table) }}
/>
<DrawioIFrame
ref={(c) => { this.drawioIFrame = c }}
onSave={(drawioData) => { return mdu.replaceFocusedDrawioWithEditor(this.getCodeMirror(), drawioData) }}
/>

</React.Fragment>
);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.