1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ElectronClient/app/gui/NoteText.jsx

146 lines
3.1 KiB
React
Raw Normal View History

const React = require('react');
2017-11-05 20:36:27 +02:00
const { Note } = require('lib/models/note.js');
const { connect } = require('react-redux');
2017-11-05 18:51:03 +02:00
const { MdToHtml } = require('lib/markdown-utils.js');
2017-11-05 20:36:27 +02:00
const shared = require('lib/components/shared/note-screen-shared.js');
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 = {
note: Note.new(),
mode: 'view',
noteMetadata: '',
showNoteMetadata: false,
folder: null,
lastSavedNote: null,
isLoading: true,
webviewReady: false,
};
}
async componentWillMount() {
2017-11-05 18:51:03 +02:00
this.mdToHtml_ = new MdToHtml();
2017-11-05 20:36:27 +02:00
await shared.initState(this);
2017-11-05 01:27:13 +02:00
}
2017-11-05 18:51:03 +02:00
componentDidMount() {
this.webview_.addEventListener('dom-ready', this.webview_domReady.bind(this));
}
componentWillUnmount() {
this.mdToHtml_ = null;
this.webview_.addEventListener('dom-ready', this.webview_domReady.bind(this));
}
2017-11-05 01:27:13 +02:00
componentWillReceiveProps(nextProps) {
2017-11-05 20:36:27 +02:00
if (nextProps.noteId) this.reloadNote(nextProps.noteId);
}
isModified() {
return shared.isModified(this);
}
refreshNoteMetadata(force = null) {
return shared.refreshNoteMetadata(this, force);
}
title_changeText(text) {
shared.noteComponent_change(this, 'title', text);
}
body_changeText(text) {
shared.noteComponent_change(this, 'body', text);
}
async saveNoteButton_press() {
await shared.saveNoteButton_press(this);
}
async saveOneProperty(name, value) {
await shared.saveOneProperty(this, name, value);
}
toggleIsTodo_onPress() {
shared.toggleIsTodo_onPress(this);
}
showMetadata_onPress() {
shared.showMetadata_onPress(this);
2017-11-05 01:27:13 +02:00
}
2017-11-05 18:51:03 +02:00
webview_domReady() {
this.setState({
webviewReady: true,
});
2017-11-05 20:36:27 +02:00
// this.webview_.openDevTools();
2017-11-05 18:51:03 +02:00
this.webview_.addEventListener('ipc-message', (event) => {
const msg = event.channel;
if (msg.indexOf('checkboxclick:') === 0) {
const newBody = this.mdToHtml_.handleCheckboxClick(msg, this.state.note.body);
2017-11-05 20:36:27 +02:00
this.saveOneProperty('body', newBody);
2017-11-05 18:51:03 +02:00
}
})
}
2017-11-05 20:36:27 +02:00
async reloadNote(noteId) {
const note = noteId ? await Note.load(noteId) : null;
2017-11-05 01:27:13 +02:00
this.setState({
note: note,
});
}
render() {
const note = this.state.note;
const body = note ? note.body : 'no note';
2017-11-05 18:51:03 +02:00
if (this.state.webviewReady) {
const mdOptions = {
onResourceLoaded: () => {
this.forceUpdate();
},
postMessageSyntax: 'ipcRenderer.sendToHost',
};
const html = this.mdToHtml_.render(note ? note.body : '', {}, mdOptions);
this.webview_.send('setHtml', html);
}
const webviewStyle = {
width: this.props.style.width,
height: this.props.style.height,
2017-11-06 01:55:01 +02:00
overflow: 'hidden',
2017-11-05 18:51:03 +02:00
};
2017-11-05 01:27:13 +02:00
return (
<div style={this.props.style}>
2017-11-05 18:51:03 +02:00
<webview style={webviewStyle} nodeintegration="1" src="note-content.html" ref={elem => this.webview_ = elem} />
2017-11-05 01:27:13 +02:00
</div>
);
}
}
const mapStateToProps = (state) => {
return {
noteId: state.selectedNoteId,
notes: state.notes,
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 };