1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Moved code that can be shared between Electron and RN app to separate file

This commit is contained in:
Laurent Cozic 2017-11-05 00:49:23 +00:00
parent dbf51382c8
commit cc5bd12ba1
5 changed files with 138 additions and 146 deletions

View File

@ -10,8 +10,6 @@ const { app } = require('../app');
const { bridge } = require('electron').remote.require('./bridge');
//const { app } = require('electron').remote.require('./app');
async function initialize(dispatch) {
bridge().window().on('resize', function() {
store.dispatch({

View File

@ -1,3 +1,5 @@
// This is the initialization for the Electron RENDERER process
// Make it possible to require("/lib/...") without specifying full path
require('app-module-path').addPath(__dirname);

View File

@ -1,3 +1,5 @@
// This is the basic initialization for the Electron MAIN process
// Make it possible to require("/lib/...") without specifying full path
require('app-module-path').addPath(__dirname);
@ -17,46 +19,4 @@ initBridge(wrapper);
wrapper.start().catch((error) => {
console.error('Electron App fatal error:');
console.error(error);
});
// const electronApp = require('electron').app;
// const { initApp } = require('./app.js');
// const { Folder } = require('lib/models/folder.js');
// const { Resource } = require('lib/models/resource.js');
// const { BaseItem } = require('lib/models/base-item.js');
// const { Note } = require('lib/models/note.js');
// const { Tag } = require('lib/models/tag.js');
// const { NoteTag } = require('lib/models/note-tag.js');
// const { Setting } = require('lib/models/setting.js');
// const { Logger } = require('lib/logger.js');
// const { FsDriverNode } = require('lib/fs-driver-node.js');
// const { shimInit } = require('lib/shim-init-node.js');
// process.on('unhandledRejection', (reason, p) => {
// console.error('Unhandled promise rejection', p, 'reason:', reason);
// process.exit(1);
// });
// const fsDriver = new FsDriverNode();
// Logger.fsDriver_ = fsDriver;
// Resource.fsDriver_ = fsDriver;
// // That's not good, but it's to avoid circular dependency issues
// // in the BaseItem class.
// BaseItem.loadClass('Note', Note);
// BaseItem.loadClass('Folder', Folder);
// BaseItem.loadClass('Resource', Resource);
// BaseItem.loadClass('Tag', Tag);
// BaseItem.loadClass('NoteTag', NoteTag);
// Setting.setConstant('appId', 'net.cozic.joplin-desktop');
// Setting.setConstant('appType', 'desktop');
// shimInit();
// const app = initApp(electronApp);
// app.start(process.argv).catch((error) => {
// console.error('Fatal error:');
// console.error(error);
// });
});

View File

@ -24,6 +24,7 @@ const { NoteBodyViewer } = require('lib/components/note-body-viewer.js');
const RNFetchBlob = require('react-native-fetch-blob').default;
const { DocumentPicker, DocumentPickerUtil } = require('react-native-document-picker');
const ImageResizer = require('react-native-image-resizer').default;
const shared = require('lib/components/shared/note-screen-shared.js');
class NoteScreenComponent extends BaseScreenComponent {
@ -128,125 +129,37 @@ class NoteScreenComponent extends BaseScreenComponent {
}
isModified() {
if (!this.state.note || !this.state.lastSavedNote) return false;
let diff = BaseModel.diffObjects(this.state.note, this.state.lastSavedNote);
delete diff.type_;
return !!Object.getOwnPropertyNames(diff).length;
return shared.isModified(this);
}
async componentWillMount() {
BackButtonService.addHandler(this.backHandler);
let note = null;
let mode = 'view';
if (!this.props.noteId) {
note = this.props.itemType == 'todo' ? Note.newTodo(this.props.folderId) : Note.new(this.props.folderId);
mode = 'edit';
} else {
note = await Note.load(this.props.noteId);
}
await shared.initState(this);
const folder = Folder.byId(this.props.folders, note.parent_id);
this.setState({
lastSavedNote: Object.assign({}, note),
note: note,
mode: mode,
folder: folder,
isLoading: false,
});
this.refreshNoteMetadata();
shared.refreshNoteMetadata(this);
}
componentWillUnmount() {
BackButtonService.removeHandler(this.backHandler);
}
noteComponent_change(propName, propValue) {
let note = Object.assign({}, this.state.note);
note[propName] = propValue;
this.setState({ note: note });
}
async refreshNoteMetadata(force = null) {
if (force !== true && !this.state.showNoteMetadata) return;
let noteMetadata = await Note.serializeAllProps(this.state.note);
this.setState({ noteMetadata: noteMetadata });
}
title_changeText(text) {
this.noteComponent_change('title', text);
shared.noteComponent_change(this, 'title', text);
}
body_changeText(text) {
this.noteComponent_change('body', text);
}
async noteExists(noteId) {
const existingNote = await Note.load(noteId);
return !!existingNote;
shared.noteComponent_change(this, 'body', text);
}
async saveNoteButton_press() {
let note = Object.assign({}, this.state.note);
// Note has been deleted while user was modifying it. In that, we
// just save a new note by clearing the note ID.
if (note.id && !(await this.noteExists(note.id))) delete note.id;
reg.logger().info('Saving note: ', note);
if (!note.parent_id) {
let folder = await Folder.defaultFolder();
if (!folder) {
Log.warn('Cannot save note without a notebook');
return;
}
note.parent_id = folder.id;
}
let isNew = !note.id;
if (isNew && !note.title) {
note.title = Note.defaultTitle(note);
}
note = await Note.save(note);
this.setState({
lastSavedNote: Object.assign({}, note),
note: note,
});
if (isNew) Note.updateGeolocation(note.id);
this.refreshNoteMetadata();
await shared.saveNoteButton_press(this);
Keyboard.dismiss();
}
async saveOneProperty(name, value) {
let note = Object.assign({}, this.state.note);
// Note has been deleted while user was modifying it. In that, we
// just save a new note by clearing the note ID.
if (note.id && !(await this.noteExists(note.id))) delete note.id;
reg.logger().info('Saving note property: ', note.id, name, value);
if (note.id) {
let toSave = { id: note.id };
toSave[name] = value;
toSave = await Note.save(toSave);
note[name] = toSave[name];
this.setState({
lastSavedNote: Object.assign({}, note),
note: note,
});
} else {
note[name] = value;
this.setState({ note: note });
}
await shared.saveOneProperty(this, name, value);
}
async deleteNote_onPress() {
@ -353,15 +266,11 @@ class NoteScreenComponent extends BaseScreenComponent {
}
toggleIsTodo_onPress() {
let newNote = Note.toggleIsTodo(this.state.note);
let newState = { note: newNote };
//if (!newNote.id) newState.lastSavedNote = Object.assign({}, newNote);
this.setState(newState);
shared.toggleIsTodo_onPress(this);
}
showMetadata_onPress() {
this.setState({ showNoteMetadata: !this.state.showNoteMetadata });
this.refreshNoteMetadata(true);
shared.showMetadata_onPress(this);
}
async showOnMap_onPress() {

View File

@ -0,0 +1,123 @@
const { reg } = require('lib/registry.js');
const { Folder } = require('lib/models/folder.js');
const { BaseModel } = require('lib/base-model.js');
const { Note } = require('lib/models/note.js');
const shared = {};
shared.noteExists = async function(noteId) {
const existingNote = await Note.load(noteId);
return !!existingNote;
}
shared.saveNoteButton_press = async function(comp) {
let note = Object.assign({}, comp.state.note);
// Note has been deleted while user was modifying it. In that, we
// just save a new note by clearing the note ID.
if (note.id && !(await shared.noteExists(note.id))) delete note.id;
reg.logger().info('Saving note: ', note);
if (!note.parent_id) {
let folder = await Folder.defaultFolder();
if (!folder) {
//Log.warn('Cannot save note without a notebook');
return;
}
note.parent_id = folder.id;
}
let isNew = !note.id;
if (isNew && !note.title) {
note.title = Note.defaultTitle(note);
}
note = await Note.save(note);
comp.setState({
lastSavedNote: Object.assign({}, note),
note: note,
});
if (isNew) Note.updateGeolocation(note.id);
shared.refreshNoteMetadata(comp);
}
shared.saveOneProperty = async function(comp, name, value) {
let note = Object.assign({}, comp.state.note);
// Note has been deleted while user was modifying it. In that, we
// just save a new note by clearing the note ID.
if (note.id && !(await shared.noteExists(note.id))) delete note.id;
reg.logger().info('Saving note property: ', note.id, name, value);
if (note.id) {
let toSave = { id: note.id };
toSave[name] = value;
toSave = await Note.save(toSave);
note[name] = toSave[name];
comp.setState({
lastSavedNote: Object.assign({}, note),
note: note,
});
} else {
note[name] = value;
comp.setState({ note: note });
}
}
shared.noteComponent_change = function(comp, propName, propValue) {
let note = Object.assign({}, comp.state.note);
note[propName] = propValue;
comp.setState({ note: note });
}
shared.refreshNoteMetadata = async function(comp, force = null) {
if (force !== true && !comp.state.showNoteMetadata) return;
let noteMetadata = await Note.serializeAllProps(comp.state.note);
comp.setState({ noteMetadata: noteMetadata });
}
shared.isModified = function(comp) {
if (!comp.state.note || !comp.state.lastSavedNote) return false;
let diff = BaseModel.diffObjects(comp.state.note, comp.state.lastSavedNote);
delete diff.type_;
return !!Object.getOwnPropertyNames(diff).length;
}
shared.initState = async function(comp) {
let note = null;
let mode = 'view';
if (!comp.props.noteId) {
note = comp.props.itemType == 'todo' ? Note.newTodo(comp.props.folderId) : Note.new(comp.props.folderId);
mode = 'edit';
} else {
note = await Note.load(comp.props.noteId);
}
const folder = Folder.byId(comp.props.folders, note.parent_id);
comp.setState({
lastSavedNote: Object.assign({}, note),
note: note,
mode: mode,
folder: folder,
isLoading: false,
});
}
shared.showMetadata_onPress = function(comp) {
comp.setState({ showNoteMetadata: !comp.state.showNoteMetadata });
shared.refreshNoteMetadata(comp, true);
}
shared.toggleIsTodo_onPress = function(comp) {
let newNote = Note.toggleIsTodo(comp.state.note);
let newState = { note: newNote };
comp.setState(newState);
}
module.exports = shared;