2017-11-05 02:17:48 +02:00
|
|
|
const React = require('react');
|
2017-11-05 20:36:27 +02:00
|
|
|
const { Note } = require('lib/models/note.js');
|
2017-11-05 02:17:48 +02:00
|
|
|
const { connect } = require('react-redux');
|
2017-11-07 23:11:14 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
2017-11-07 20:39:11 +02:00
|
|
|
const MdToHtml = require('lib/MdToHtml');
|
2017-11-05 20:36:27 +02:00
|
|
|
const shared = require('lib/components/shared/note-screen-shared.js');
|
2017-11-07 23:11:14 +02:00
|
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
2017-11-08 19:51:55 +02:00
|
|
|
const { themeStyle } = require('../theme.js');
|
2017-11-10 00:44:10 +02:00
|
|
|
const AceEditor = require('react-ace').default;
|
|
|
|
require('brace/mode/markdown');
|
|
|
|
require('brace/theme/chrome');
|
2017-11-05 02:17:48 +02:00
|
|
|
|
2017-11-05 01:27:13 +02:00
|
|
|
class NoteTextComponent extends React.Component {
|
|
|
|
|
2017-11-05 20:36:27 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.state = {
|
2017-11-10 19:58:17 +02:00
|
|
|
note: null,
|
2017-11-05 20:36:27 +02:00
|
|
|
noteMetadata: '',
|
|
|
|
showNoteMetadata: false,
|
|
|
|
folder: null,
|
|
|
|
lastSavedNote: null,
|
|
|
|
isLoading: true,
|
|
|
|
webviewReady: false,
|
2017-11-07 23:11:14 +02:00
|
|
|
scrollHeight: null,
|
2017-11-10 00:44:10 +02:00
|
|
|
editorScrollTop: 0,
|
2017-11-05 20:36:27 +02:00
|
|
|
};
|
2017-11-07 01:56:33 +02:00
|
|
|
|
|
|
|
this.lastLoadedNoteId_ = null;
|
2017-11-07 23:11:14 +02:00
|
|
|
|
|
|
|
this.webviewListeners_ = null;
|
|
|
|
this.ignoreNextEditorScroll_ = false;
|
2017-11-07 23:46:23 +02:00
|
|
|
this.scheduleSaveTimeout_ = null;
|
2017-11-10 00:44:10 +02:00
|
|
|
this.restoreScrollTop_ = null;
|
|
|
|
|
|
|
|
// Complicated but reliable method to get editor content height
|
|
|
|
// https://github.com/ajaxorg/ace/issues/2046
|
|
|
|
this.editorMaxScrollTop_ = 0;
|
|
|
|
this.onAfterEditorRender_ = () => {
|
|
|
|
const r = this.editor_.editor.renderer;
|
|
|
|
this.editorMaxScrollTop_ = Math.max(0, r.layerConfig.maxHeight - r.$size.scrollerHeight);
|
|
|
|
|
|
|
|
if (this.restoreScrollTop_) {
|
|
|
|
this.editorSetScrollTop(this.restoreScrollTop_);
|
|
|
|
this.restoreScrollTop_ = null;
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 20:36:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
mdToHtml() {
|
|
|
|
if (this.mdToHtml_) return this.mdToHtml_;
|
2017-11-05 18:51:03 +02:00
|
|
|
this.mdToHtml_ = new MdToHtml();
|
2017-11-07 23:11:14 +02:00
|
|
|
return this.mdToHtml_;
|
2017-11-05 01:27:13 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
async componentWillMount() {
|
2017-11-10 19:58:17 +02:00
|
|
|
let note = null;
|
|
|
|
if (this.props.noteId) {
|
|
|
|
note = await Note.load(this.props.noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const folder = note ? Folder.byId(this.props.folders, note.parent_id) : null;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
|
|
|
folder: folder,
|
|
|
|
isLoading: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.lastLoadedNoteId_ = note ? note.id : null;
|
2017-11-05 18:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-11-07 23:46:23 +02:00
|
|
|
this.saveIfNeeded();
|
|
|
|
|
2017-11-05 18:51:03 +02:00
|
|
|
this.mdToHtml_ = null;
|
2017-11-07 23:11:14 +02:00
|
|
|
this.destroyWebview();
|
2017-11-05 18:51:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:46:23 +02:00
|
|
|
async saveIfNeeded() {
|
|
|
|
if (this.scheduleSaveTimeout_) clearTimeout(this.scheduleSaveTimeout_);
|
|
|
|
this.scheduleSaveTimeout_ = null;
|
|
|
|
if (!shared.isModified(this)) return;
|
|
|
|
await shared.saveNoteButton_press(this);
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
async saveOneProperty(name, value) {
|
|
|
|
await shared.saveOneProperty(this, name, value);
|
|
|
|
}
|
|
|
|
|
2017-11-07 23:46:23 +02:00
|
|
|
scheduleSave() {
|
|
|
|
if (this.scheduleSaveTimeout_) clearTimeout(this.scheduleSaveTimeout_);
|
|
|
|
this.scheduleSaveTimeout_ = setTimeout(() => {
|
|
|
|
this.saveIfNeeded();
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
2017-11-07 01:56:33 +02:00
|
|
|
async componentWillReceiveProps(nextProps) {
|
2017-11-07 23:46:23 +02:00
|
|
|
if ('noteId' in nextProps && nextProps.noteId !== this.props.noteId) {
|
2017-11-07 23:11:14 +02:00
|
|
|
this.mdToHtml_ = null;
|
|
|
|
|
2017-11-07 01:56:33 +02:00
|
|
|
const noteId = nextProps.noteId;
|
|
|
|
this.lastLoadedNoteId_ = noteId;
|
|
|
|
const note = noteId ? await Note.load(noteId) : null;
|
|
|
|
if (noteId !== this.lastLoadedNoteId_) return; // Race condition - current note was changed while this one was loading
|
|
|
|
|
2017-11-10 19:58:17 +02:00
|
|
|
// If we are loading nothing (noteId == null), make sure to
|
|
|
|
// set webviewReady to false too because the webview component
|
|
|
|
// is going to be removed in render().
|
|
|
|
const webviewReady = this.webview_ && this.state.webviewReady && noteId;
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
this.setState({
|
|
|
|
note: note,
|
2017-11-08 19:51:55 +02:00
|
|
|
lastSavedNote: Object.assign({}, note),
|
2017-11-10 19:58:17 +02:00
|
|
|
webviewReady: webviewReady,
|
2017-11-07 23:11:14 +02:00
|
|
|
});
|
2017-11-07 01:56:33 +02:00
|
|
|
}
|
2017-11-05 20:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isModified() {
|
|
|
|
return shared.isModified(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshNoteMetadata(force = null) {
|
|
|
|
return shared.refreshNoteMetadata(this, force);
|
|
|
|
}
|
|
|
|
|
|
|
|
title_changeText(text) {
|
|
|
|
shared.noteComponent_change(this, 'title', text);
|
2017-11-07 23:46:23 +02:00
|
|
|
this.scheduleSave();
|
2017-11-05 20:36:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:46:23 +02:00
|
|
|
editor_change(event) {
|
|
|
|
shared.noteComponent_change(this, 'body', event.target.value);
|
|
|
|
this.scheduleSave();
|
2017-11-05 20:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleIsTodo_onPress() {
|
|
|
|
shared.toggleIsTodo_onPress(this);
|
2017-11-07 23:46:23 +02:00
|
|
|
this.scheduleSave();
|
2017-11-05 20:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showMetadata_onPress() {
|
|
|
|
shared.showMetadata_onPress(this);
|
2017-11-05 01:27:13 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
webview_ipcMessage(event) {
|
|
|
|
const msg = event.channel ? event.channel : '';
|
|
|
|
const args = event.args;
|
|
|
|
const arg0 = args && args.length >= 1 ? args[0] : null;
|
|
|
|
const arg1 = args && args.length >= 2 ? args[1] : null;
|
|
|
|
|
|
|
|
reg.logger().info('Got ipc-message: ' + msg, args);
|
|
|
|
|
|
|
|
if (msg.indexOf('checkboxclick:') === 0) {
|
2017-11-10 00:44:10 +02:00
|
|
|
// Ugly hack because setting the body here will make the scrollbar
|
|
|
|
// go to some random position. So we save the scrollTop here and it
|
|
|
|
// will be restored after the editor ref has been reset, and the
|
|
|
|
// "afterRender" event has been called.
|
|
|
|
this.restoreScrollTop_ = this.editorScrollTop();
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const newBody = this.mdToHtml_.handleCheckboxClick(msg, this.state.note.body);
|
|
|
|
this.saveOneProperty('body', newBody);
|
|
|
|
} else if (msg.toLowerCase().indexOf('http') === 0) {
|
|
|
|
require('electron').shell.openExternal(msg);
|
|
|
|
} else if (msg === 'percentScroll') {
|
|
|
|
this.ignoreNextEditorScroll_ = true;
|
|
|
|
this.setEditorPercentScroll(arg0);
|
|
|
|
} else {
|
|
|
|
bridge().showMessageBox({
|
|
|
|
type: 'error',
|
|
|
|
message: _('Unsupported link or message: %s', msg),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editorMaxScroll() {
|
2017-11-10 00:44:10 +02:00
|
|
|
return this.editorMaxScrollTop_;
|
|
|
|
}
|
|
|
|
|
|
|
|
editorScrollTop() {
|
|
|
|
return this.editor_.editor.getSession().getScrollTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
editorSetScrollTop(v) {
|
|
|
|
this.editor_.editor.getSession().setScrollTop(v);
|
2017-11-07 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setEditorPercentScroll(p) {
|
2017-11-10 00:44:10 +02:00
|
|
|
this.editorSetScrollTop(p * this.editorMaxScroll());
|
2017-11-07 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setViewerPercentScroll(p) {
|
|
|
|
this.webview_.send('setPercentScroll', p);
|
|
|
|
}
|
|
|
|
|
|
|
|
editor_scroll() {
|
|
|
|
if (this.ignoreNextEditorScroll_) {
|
|
|
|
this.ignoreNextEditorScroll_ = false;
|
|
|
|
return;
|
|
|
|
}
|
2017-11-10 00:44:10 +02:00
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const m = this.editorMaxScroll();
|
2017-11-10 00:44:10 +02:00
|
|
|
this.setViewerPercentScroll(m ? this.editorScrollTop() / m : 0);
|
2017-11-07 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 18:51:03 +02:00
|
|
|
webview_domReady() {
|
2017-11-07 23:11:14 +02:00
|
|
|
if (!this.webview_) return;
|
|
|
|
|
2017-11-05 18:51:03 +02:00
|
|
|
this.setState({
|
|
|
|
webviewReady: true,
|
|
|
|
});
|
|
|
|
|
2017-11-10 02:07:37 +02:00
|
|
|
this.webview_.openDevTools();
|
2017-11-07 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
webview_ref(element) {
|
|
|
|
if (this.webview_) {
|
|
|
|
if (this.webview_ === element) return;
|
|
|
|
this.destroyWebview();
|
|
|
|
}
|
2017-11-05 18:51:03 +02:00
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
if (!element) {
|
|
|
|
this.destroyWebview();
|
|
|
|
} else {
|
|
|
|
this.initWebview(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editor_ref(element) {
|
|
|
|
if (this.editor_ === element) return;
|
2017-11-10 00:44:10 +02:00
|
|
|
|
|
|
|
if (this.editor_) {
|
|
|
|
this.editorMaxScrollTop_ = 0;
|
|
|
|
this.editor_.editor.renderer.off('afterRender', this.onAfterEditorRender_);
|
|
|
|
}
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
this.editor_ = element;
|
2017-11-10 00:44:10 +02:00
|
|
|
|
|
|
|
if (this.editor_) {
|
|
|
|
this.editor_.editor.renderer.on('afterRender', this.onAfterEditorRender_);
|
|
|
|
}
|
2017-11-07 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initWebview(wv) {
|
|
|
|
if (!this.webviewListeners_) {
|
|
|
|
this.webviewListeners_ = {
|
|
|
|
'dom-ready': this.webview_domReady.bind(this),
|
|
|
|
'ipc-message': this.webview_ipcMessage.bind(this),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let n in this.webviewListeners_) {
|
|
|
|
if (!this.webviewListeners_.hasOwnProperty(n)) continue;
|
|
|
|
const fn = this.webviewListeners_[n];
|
|
|
|
wv.addEventListener(n, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.webview_ = wv;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyWebview() {
|
|
|
|
if (!this.webview_) return;
|
2017-11-05 18:51:03 +02:00
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
for (let n in this.webviewListeners_) {
|
|
|
|
if (!this.webviewListeners_.hasOwnProperty(n)) continue;
|
|
|
|
const fn = this.webviewListeners_[n];
|
|
|
|
this.webview_.removeEventListener(n, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.webview_ = null;
|
2017-11-05 18:51:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 00:44:10 +02:00
|
|
|
aceEditor_change(body) {
|
|
|
|
shared.noteComponent_change(this, 'body', body);
|
|
|
|
this.scheduleSave();
|
|
|
|
}
|
|
|
|
|
2017-11-05 01:27:13 +02:00
|
|
|
render() {
|
2017-11-07 23:11:14 +02:00
|
|
|
const style = this.props.style;
|
2017-11-05 01:27:13 +02:00
|
|
|
const note = this.state.note;
|
2017-11-07 23:11:14 +02:00
|
|
|
const body = note ? note.body : '';
|
2017-11-08 19:51:55 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
2017-11-07 23:11:14 +02:00
|
|
|
|
2017-11-10 19:58:17 +02:00
|
|
|
if (!note) {
|
|
|
|
const emptyDivStyle = Object.assign({
|
|
|
|
backgroundColor: 'black',
|
|
|
|
opacity: 0.1,
|
|
|
|
}, style);
|
|
|
|
return <div style={emptyDivStyle}></div>
|
|
|
|
}
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const viewerStyle = {
|
|
|
|
width: Math.floor(style.width / 2),
|
|
|
|
height: style.height,
|
|
|
|
overflow: 'hidden',
|
|
|
|
float: 'left',
|
|
|
|
verticalAlign: 'top',
|
|
|
|
};
|
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
const paddingTop = 14;
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const editorStyle = {
|
|
|
|
width: style.width - viewerStyle.width,
|
2017-11-08 19:51:55 +02:00
|
|
|
height: style.height - paddingTop,
|
2017-11-10 00:44:10 +02:00
|
|
|
overflowY: 'hidden',
|
2017-11-07 23:11:14 +02:00
|
|
|
float: 'left',
|
|
|
|
verticalAlign: 'top',
|
2017-11-08 19:51:55 +02:00
|
|
|
paddingTop: paddingTop + 'px',
|
|
|
|
lineHeight: theme.textAreaLineHeight + 'px',
|
|
|
|
fontSize: theme.fontSize + 'px',
|
2017-11-07 23:11:14 +02:00
|
|
|
};
|
2017-11-05 01:27:13 +02:00
|
|
|
|
2017-11-05 18:51:03 +02:00
|
|
|
if (this.state.webviewReady) {
|
|
|
|
const mdOptions = {
|
|
|
|
onResourceLoaded: () => {
|
|
|
|
this.forceUpdate();
|
|
|
|
},
|
|
|
|
postMessageSyntax: 'ipcRenderer.sendToHost',
|
|
|
|
};
|
2017-11-08 19:51:55 +02:00
|
|
|
const html = this.mdToHtml().render(body, theme, mdOptions);
|
2017-11-05 18:51:03 +02:00
|
|
|
this.webview_.send('setHtml', html);
|
|
|
|
}
|
|
|
|
|
2017-11-07 23:11:14 +02:00
|
|
|
const viewer = <webview style={viewerStyle} nodeintegration="1" src="note-content.html" ref={(elem) => { this.webview_ref(elem); } } />
|
2017-11-10 00:44:10 +02:00
|
|
|
|
|
|
|
const editorRootStyle = Object.assign({}, editorStyle);
|
|
|
|
delete editorRootStyle.width;
|
|
|
|
delete editorRootStyle.height;
|
|
|
|
delete editorRootStyle.fontSize;
|
|
|
|
|
|
|
|
const editor = <AceEditor
|
|
|
|
value={body}
|
|
|
|
mode="markdown"
|
|
|
|
theme="chrome"
|
|
|
|
style={editorRootStyle}
|
|
|
|
width={editorStyle.width + 'px'}
|
|
|
|
height={editorStyle.height + 'px'}
|
|
|
|
fontSize={editorStyle.fontSize}
|
|
|
|
showGutter={false}
|
|
|
|
name="note-editor"
|
|
|
|
wrapEnabled={true}
|
|
|
|
onScroll={(event) => { this.editor_scroll(); }}
|
|
|
|
ref={(elem) => { this.editor_ref(elem); } }
|
|
|
|
onChange={(body) => { this.aceEditor_change(body) }}
|
|
|
|
|
|
|
|
// Disable warning: "Automatically scrolling cursor into view after
|
|
|
|
// selection change this will be disabled in the next version set
|
|
|
|
// editor.$blockScrolling = Infinity to disable this message"
|
|
|
|
editorProps={{$blockScrolling: true}}
|
2017-11-10 01:28:08 +02:00
|
|
|
|
|
|
|
// This is buggy (gets outside the container)
|
|
|
|
highlightActiveLine={false}
|
2017-11-10 00:44:10 +02:00
|
|
|
/>
|
2017-11-05 18:51:03 +02:00
|
|
|
|
2017-11-05 01:27:13 +02:00
|
|
|
return (
|
2017-11-07 23:11:14 +02:00
|
|
|
<div style={style}>
|
|
|
|
{ editor }
|
|
|
|
{ viewer }
|
2017-11-05 01:27:13 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
return {
|
|
|
|
noteId: state.selectedNoteId,
|
2017-11-05 20:36:27 +02:00
|
|
|
folderId: state.selectedFolderId,
|
|
|
|
itemType: state.selectedItemType,
|
|
|
|
folders: state.folders,
|
|
|
|
theme: state.settings.theme,
|
|
|
|
showAdvancedOptions: state.settings.showAdvancedOptions,
|
2017-11-05 01:27:13 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const NoteText = connect(mapStateToProps)(NoteTextComponent);
|
|
|
|
|
|
|
|
module.exports = { NoteText };
|