|
|
@@ -4,8 +4,12 @@ import PropTypes from 'prop-types';
|
|
|
import * as toastr from 'toastr';
|
|
|
import { throttle, debounce } from 'throttle-debounce';
|
|
|
|
|
|
+import GrowiRenderer from '../util/GrowiRenderer';
|
|
|
+
|
|
|
+import { EditorOptions, PreviewOptions } from './PageEditor/OptionsSelector';
|
|
|
import Editor from './PageEditor/Editor';
|
|
|
import Preview from './PageEditor/Preview';
|
|
|
+import scrollSyncHelper from './PageEditor/ScrollSyncHelper';
|
|
|
|
|
|
export default class PageEditor extends React.Component {
|
|
|
|
|
|
@@ -15,31 +19,44 @@ export default class PageEditor extends React.Component {
|
|
|
const config = this.props.crowi.getConfig();
|
|
|
const isUploadable = config.upload.image || config.upload.file;
|
|
|
const isUploadableFile = config.upload.file;
|
|
|
+ const isMathJaxEnabled = !!config.env.MATHJAX;
|
|
|
|
|
|
this.state = {
|
|
|
revisionId: this.props.revisionId,
|
|
|
markdown: this.props.markdown,
|
|
|
isUploadable,
|
|
|
isUploadableFile,
|
|
|
+ isMathJaxEnabled,
|
|
|
editorOptions: this.props.editorOptions,
|
|
|
+ previewOptions: this.props.previewOptions,
|
|
|
};
|
|
|
|
|
|
+ this.growiRenderer = new GrowiRenderer(this.props.crowi, this.props.crowiRenderer, {mode: 'editor'});
|
|
|
+
|
|
|
this.setCaretLine = this.setCaretLine.bind(this);
|
|
|
this.focusToEditor = this.focusToEditor.bind(this);
|
|
|
this.onMarkdownChanged = this.onMarkdownChanged.bind(this);
|
|
|
this.onSave = this.onSave.bind(this);
|
|
|
this.onUpload = this.onUpload.bind(this);
|
|
|
this.onEditorScroll = this.onEditorScroll.bind(this);
|
|
|
- this.getMaxScrollTop = this.getMaxScrollTop.bind(this);
|
|
|
- this.getScrollTop = this.getScrollTop.bind(this);
|
|
|
+ this.onEditorScrollCursorIntoView = this.onEditorScrollCursorIntoView.bind(this);
|
|
|
+ this.onPreviewScroll = this.onPreviewScroll.bind(this);
|
|
|
this.saveDraft = this.saveDraft.bind(this);
|
|
|
this.clearDraft = this.clearDraft.bind(this);
|
|
|
this.pageSavedHandler = this.pageSavedHandler.bind(this);
|
|
|
this.apiErrorHandler = this.apiErrorHandler.bind(this);
|
|
|
|
|
|
+ // for scrolling
|
|
|
+ this.lastScrolledDateWithCursor = null;
|
|
|
+ this.isOriginOfScrollSyncEditor = false;
|
|
|
+ this.isOriginOfScrollSyncEditor = false;
|
|
|
+
|
|
|
// create throttled function
|
|
|
+ this.scrollPreviewByEditorLineWithThrottle = throttle(20, this.scrollPreviewByEditorLine);
|
|
|
+ this.scrollPreviewByCursorMovingWithThrottle = throttle(20, this.scrollPreviewByCursorMoving);
|
|
|
+ this.scrollEditorByPreviewScrollWithThrottle = throttle(20, this.scrollEditorByPreviewScroll);
|
|
|
this.renderWithDebounce = debounce(50, throttle(100, this.renderPreview));
|
|
|
- this.saveDraftWithDebounce = debounce(300, this.saveDraft);
|
|
|
+ this.saveDraftWithDebounce = debounce(800, this.saveDraft);
|
|
|
}
|
|
|
|
|
|
componentWillMount() {
|
|
|
@@ -59,6 +76,7 @@ export default class PageEditor extends React.Component {
|
|
|
*/
|
|
|
setCaretLine(line) {
|
|
|
this.refs.editor.setCaretLine(line);
|
|
|
+ scrollSyncHelper.scrollPreview(this.previewElement, line);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -69,6 +87,14 @@ export default class PageEditor extends React.Component {
|
|
|
this.setState({ editorOptions });
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * set options (used from the outside)
|
|
|
+ * @param {object} previewOptions
|
|
|
+ */
|
|
|
+ setPreviewOptions(previewOptions) {
|
|
|
+ this.setState({ previewOptions });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* the change event handler for `markdown` state
|
|
|
* @param {string} value
|
|
|
@@ -165,32 +191,99 @@ export default class PageEditor extends React.Component {
|
|
|
/**
|
|
|
* the scroll event handler from codemirror
|
|
|
* @param {any} data {left, top, width, height, clientWidth, clientHeight} object that represents the current scroll position, the size of the scrollable area, and the size of the visible area (minus scrollbars).
|
|
|
- * see https://codemirror.net/doc/manual.html#events
|
|
|
+ * And data.line is also available that is added by Editor component
|
|
|
+ * @see https://codemirror.net/doc/manual.html#events
|
|
|
*/
|
|
|
onEditorScroll(data) {
|
|
|
- const rate = data.top / (data.height - data.clientHeight)
|
|
|
- const top = this.getScrollTop(this.previewElement, rate);
|
|
|
+ // prevent scrolling
|
|
|
+ // if the elapsed time from last scroll with cursor is shorter than 40ms
|
|
|
+ const now = new Date();
|
|
|
+ if (now - this.lastScrolledDateWithCursor < 40) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- this.previewElement.scrollTop = top;
|
|
|
+ this.scrollPreviewByEditorLineWithThrottle(data.line);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * transplanted from crowi-form.js -- 2018.01.21 Yuki Takei
|
|
|
- * @param {*} dom
|
|
|
+ * the scroll event handler from codemirror
|
|
|
+ * @param {number} line
|
|
|
+ * @see https://codemirror.net/doc/manual.html#events
|
|
|
*/
|
|
|
- getMaxScrollTop(dom) {
|
|
|
- var rect = dom.getBoundingClientRect();
|
|
|
- return dom.scrollHeight - rect.height;
|
|
|
+ onEditorScrollCursorIntoView(line) {
|
|
|
+ // record date
|
|
|
+ this.lastScrolledDateWithCursor = new Date();
|
|
|
+ this.scrollPreviewByCursorMovingWithThrottle(line);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * scroll Preview element by scroll event
|
|
|
+ * @param {number} line
|
|
|
+ */
|
|
|
+ scrollPreviewByEditorLine(line) {
|
|
|
+ if (this.previewElement == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // prevent circular invocation
|
|
|
+ if (this.isOriginOfScrollSyncPreview) {
|
|
|
+ this.isOriginOfScrollSyncPreview = false; // turn off the flag
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // turn on the flag
|
|
|
+ this.isOriginOfScrollSyncEditor = true;
|
|
|
+ scrollSyncHelper.scrollPreview(this.previewElement, line);
|
|
|
};
|
|
|
+
|
|
|
/**
|
|
|
- * transplanted from crowi-form.js -- 2018.01.21 Yuki Takei
|
|
|
- * @param {*} dom
|
|
|
+ * scroll Preview element by cursor moving
|
|
|
+ * @param {number} line
|
|
|
*/
|
|
|
- getScrollTop(dom, rate) {
|
|
|
- var maxScrollTop = this.getMaxScrollTop(dom);
|
|
|
- var top = maxScrollTop * rate;
|
|
|
- return top;
|
|
|
+ scrollPreviewByCursorMoving(line) {
|
|
|
+ if (this.previewElement == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // prevent circular invocation
|
|
|
+ if (this.isOriginOfScrollSyncPreview) {
|
|
|
+ this.isOriginOfScrollSyncPreview = false; // turn off the flag
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // turn on the flag
|
|
|
+ this.isOriginOfScrollSyncEditor = true;
|
|
|
+ scrollSyncHelper.scrollPreviewToRevealOverflowing(this.previewElement, line);
|
|
|
};
|
|
|
|
|
|
+ /**
|
|
|
+ * the scroll event handler from Preview component
|
|
|
+ * @param {number} offset
|
|
|
+ */
|
|
|
+ onPreviewScroll(offset) {
|
|
|
+ this.scrollEditorByPreviewScrollWithThrottle(offset);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * scroll Editor component by scroll event of Preview component
|
|
|
+ * @param {number} offset
|
|
|
+ */
|
|
|
+ scrollEditorByPreviewScroll(offset) {
|
|
|
+ if (this.previewElement == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // prevent circular invocation
|
|
|
+ if (this.isOriginOfScrollSyncEditor) {
|
|
|
+ this.isOriginOfScrollSyncEditor = false; // turn off the flag
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // turn on the flag
|
|
|
+ this.isOriginOfScrollSyncPreview = true;
|
|
|
+ scrollSyncHelper.scrollEditor(this.refs.editor, this.previewElement, offset);
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
* methods for draft
|
|
|
*/
|
|
|
@@ -244,14 +337,6 @@ export default class PageEditor extends React.Component {
|
|
|
|
|
|
this.setState({ markdown: value });
|
|
|
|
|
|
- // generate options obj
|
|
|
- const rendererOptions = {
|
|
|
- // see: https://www.npmjs.com/package/marked
|
|
|
- marked: {
|
|
|
- breaks: config.isEnabledLineBreaks,
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
// render html
|
|
|
var context = {
|
|
|
markdown: this.state.markdown,
|
|
|
@@ -259,26 +344,31 @@ export default class PageEditor extends React.Component {
|
|
|
currentPagePath: decodeURIComponent(location.pathname)
|
|
|
};
|
|
|
|
|
|
- this.props.crowi.interceptorManager.process('preRenderPreview', context)
|
|
|
- .then(() => crowi.interceptorManager.process('prePreProcess', context))
|
|
|
+ const growiRenderer = this.growiRenderer;
|
|
|
+ const interceptorManager = this.props.crowi.interceptorManager;
|
|
|
+ interceptorManager.process('preRenderPreview', context)
|
|
|
+ .then(() => interceptorManager.process('prePreProcess', context))
|
|
|
.then(() => {
|
|
|
- context.markdown = crowiRenderer.preProcess(context.markdown, context.dom);
|
|
|
+ context.markdown = growiRenderer.preProcess(context.markdown);
|
|
|
})
|
|
|
- .then(() => crowi.interceptorManager.process('postPreProcess', context))
|
|
|
+ .then(() => interceptorManager.process('postPreProcess', context))
|
|
|
.then(() => {
|
|
|
- var parsedHTML = crowiRenderer.render(context.markdown, context.dom, rendererOptions);
|
|
|
+ var parsedHTML = growiRenderer.process(context.markdown);
|
|
|
context['parsedHTML'] = parsedHTML;
|
|
|
})
|
|
|
- .then(() => crowi.interceptorManager.process('postRenderPreview', context))
|
|
|
- .then(() => crowi.interceptorManager.process('preRenderPreviewHtml', context))
|
|
|
+ .then(() => interceptorManager.process('prePostProcess', context))
|
|
|
+ .then(() => {
|
|
|
+ context.parsedHTML = growiRenderer.postProcess(context.parsedHTML, context.dom);
|
|
|
+ })
|
|
|
+ .then(() => interceptorManager.process('postPostProcess', context))
|
|
|
+ .then(() => interceptorManager.process('preRenderPreviewHtml', context))
|
|
|
.then(() => {
|
|
|
this.setState({ html: context.parsedHTML });
|
|
|
-
|
|
|
// set html to the hidden input (for submitting to save)
|
|
|
$('#form-body').val(this.state.markdown);
|
|
|
})
|
|
|
// process interceptors for post rendering
|
|
|
- .then(() => crowi.interceptorManager.process('postRenderPreviewHtml', context));
|
|
|
+ .then(() => interceptorManager.process('postRenderPreviewHtml', context));
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -291,13 +381,20 @@ export default class PageEditor extends React.Component {
|
|
|
isUploadable={this.state.isUploadable}
|
|
|
isUploadableFile={this.state.isUploadableFile}
|
|
|
onScroll={this.onEditorScroll}
|
|
|
+ onScrollCursorIntoView={this.onEditorScrollCursorIntoView}
|
|
|
onChange={this.onMarkdownChanged}
|
|
|
onSave={this.onSave}
|
|
|
onUpload={this.onUpload}
|
|
|
/>
|
|
|
</div>
|
|
|
<div className="col-md-6 hidden-sm hidden-xs page-editor-preview-container">
|
|
|
- <Preview html={this.state.html} inputRef={el => this.previewElement = el} />
|
|
|
+ <Preview html={this.state.html}
|
|
|
+ inputRef={el => this.previewElement = el}
|
|
|
+ isMathJaxEnabled={this.state.isMathJaxEnabled}
|
|
|
+ renderMathJaxOnInit={false}
|
|
|
+ previewOptions={this.state.previewOptions}
|
|
|
+ onScroll={this.onPreviewScroll}
|
|
|
+ />
|
|
|
</div>
|
|
|
</div>
|
|
|
)
|
|
|
@@ -306,10 +403,12 @@ export default class PageEditor extends React.Component {
|
|
|
|
|
|
PageEditor.propTypes = {
|
|
|
crowi: PropTypes.object.isRequired,
|
|
|
+ crowiRenderer: PropTypes.object.isRequired,
|
|
|
markdown: PropTypes.string.isRequired,
|
|
|
pageId: PropTypes.string,
|
|
|
revisionId: PropTypes.string,
|
|
|
pagePath: PropTypes.string,
|
|
|
onSaveSuccess: PropTypes.func,
|
|
|
- editorOptions: PropTypes.object,
|
|
|
+ editorOptions: PropTypes.instanceOf(EditorOptions),
|
|
|
+ previewOptions: PropTypes.instanceOf(PreviewOptions),
|
|
|
};
|