mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-02 12:47:41 +02:00
Chore: Mobile: Refactor note-screen-shared utility lib
This commit is contained in:
parent
a24ccb8da9
commit
1139317788
@ -483,6 +483,7 @@ packages/lib/commands/index.js
|
|||||||
packages/lib/commands/openMasterPasswordDialog.js
|
packages/lib/commands/openMasterPasswordDialog.js
|
||||||
packages/lib/commands/synchronize.js
|
packages/lib/commands/synchronize.js
|
||||||
packages/lib/components/EncryptionConfigScreen/utils.js
|
packages/lib/components/EncryptionConfigScreen/utils.js
|
||||||
|
packages/lib/components/shared/note-screen-shared.js
|
||||||
packages/lib/database-driver-better-sqlite.js
|
packages/lib/database-driver-better-sqlite.js
|
||||||
packages/lib/database.js
|
packages/lib/database.js
|
||||||
packages/lib/debug/DebugService.js
|
packages/lib/debug/DebugService.js
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -471,6 +471,7 @@ packages/lib/commands/index.js
|
|||||||
packages/lib/commands/openMasterPasswordDialog.js
|
packages/lib/commands/openMasterPasswordDialog.js
|
||||||
packages/lib/commands/synchronize.js
|
packages/lib/commands/synchronize.js
|
||||||
packages/lib/components/EncryptionConfigScreen/utils.js
|
packages/lib/components/EncryptionConfigScreen/utils.js
|
||||||
|
packages/lib/components/shared/note-screen-shared.js
|
||||||
packages/lib/database-driver-better-sqlite.js
|
packages/lib/database-driver-better-sqlite.js
|
||||||
packages/lib/database.js
|
packages/lib/database.js
|
||||||
packages/lib/debug/DebugService.js
|
packages/lib/debug/DebugService.js
|
||||||
|
@ -1,27 +1,45 @@
|
|||||||
const { reg } = require('../../registry.js');
|
import { NoteEntity } from '../../services/database/types';
|
||||||
const Folder = require('../../models/Folder').default;
|
import { reg } from '../../registry';
|
||||||
const BaseModel = require('../../BaseModel').default;
|
import Folder from '../../models/Folder';
|
||||||
const Note = require('../../models/Note').default;
|
import BaseModel from '../../BaseModel';
|
||||||
const Resource = require('../../models/Resource').default;
|
import Note from '../../models/Note';
|
||||||
const ResourceFetcher = require('../../services/ResourceFetcher').default;
|
import Resource from '../../models/Resource';
|
||||||
const DecryptionWorker = require('../../services/DecryptionWorker').default;
|
import ResourceFetcher from '../../services/ResourceFetcher';
|
||||||
const Setting = require('../../models/Setting').default;
|
import DecryptionWorker from '../../services/DecryptionWorker';
|
||||||
const Mutex = require('async-mutex').Mutex;
|
import Setting from '../../models/Setting';
|
||||||
|
import { Mutex } from 'async-mutex';
|
||||||
|
|
||||||
const shared = {};
|
interface Shared {
|
||||||
|
noteExists?: (noteId: string)=> Promise<boolean>;
|
||||||
|
handleNoteDeletedWhileEditing_?: (note: NoteEntity)=> Promise<NoteEntity>;
|
||||||
|
saveNoteButton_press?: (comp: any, folderId: string, options: any)=> Promise<void>;
|
||||||
|
saveOneProperty?: (comp: any, name: string, value: any)=> void;
|
||||||
|
noteComponent_change?: (comp: any, propName: string, propValue: any)=> void;
|
||||||
|
clearResourceCache?: ()=> void;
|
||||||
|
attachedResources?: (noteBody: string)=> Promise<any>;
|
||||||
|
isModified?: (comp: any)=> boolean;
|
||||||
|
initState?: (comp: any)=> void;
|
||||||
|
toggleIsTodo_onPress?: (comp: any)=> void;
|
||||||
|
toggleCheckboxRange?: (ipcMessage: string, noteBody: string)=> void;
|
||||||
|
toggleCheckbox?: (ipcMessage: string, noteBody: string)=> void;
|
||||||
|
installResourceHandling?: (refreshResourceHandler: any)=> void;
|
||||||
|
uninstallResourceHandling?: (refreshResourceHandler: any)=> void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shared: Shared = {};
|
||||||
|
|
||||||
// If saveNoteButton_press is called multiple times in short intervals, it might result in
|
// If saveNoteButton_press is called multiple times in short intervals, it might result in
|
||||||
// the same new note being created twice, so we need to a mutex to access this function.
|
// the same new note being created twice, so we need to a mutex to access this function.
|
||||||
const saveNoteMutex_ = new Mutex();
|
const saveNoteMutex_ = new Mutex();
|
||||||
|
|
||||||
shared.noteExists = async function(noteId) {
|
shared.noteExists = async function(noteId: string) {
|
||||||
const existingNote = await Note.load(noteId);
|
const existingNote = await Note.load(noteId) as NoteEntity;
|
||||||
return !!existingNote;
|
return !!existingNote;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Note has been deleted while user was modifying it. In that case, we
|
// Note has been deleted while user was modifying it. In that case, we
|
||||||
// just save a new note so that user can keep editing.
|
// just save a new note so that user can keep editing.
|
||||||
shared.handleNoteDeletedWhileEditing_ = async (note) => {
|
shared.handleNoteDeletedWhileEditing_ = async (note: NoteEntity) => {
|
||||||
if (await shared.noteExists(note.id)) return null;
|
if (await shared.noteExists(note.id)) return null;
|
||||||
|
|
||||||
reg.logger().info('Note has been deleted while it was being edited - recreating it.');
|
reg.logger().info('Note has been deleted while it was being edited - recreating it.');
|
||||||
@ -33,7 +51,7 @@ shared.handleNoteDeletedWhileEditing_ = async (note) => {
|
|||||||
return Note.load(newNote.id);
|
return Note.load(newNote.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.saveNoteButton_press = async function(comp, folderId = null, options = null) {
|
shared.saveNoteButton_press = async function(comp: any, folderId: string = null, options: any = null) {
|
||||||
options = Object.assign({}, {
|
options = Object.assign({}, {
|
||||||
autoTitle: true,
|
autoTitle: true,
|
||||||
}, options);
|
}, options);
|
||||||
@ -90,7 +108,7 @@ shared.saveNoteButton_press = async function(comp, folderId = null, options = nu
|
|||||||
note.body = stateNote.body;
|
note.body = stateNote.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newState = {
|
const newState: any = {
|
||||||
lastSavedNote: Object.assign({}, note),
|
lastSavedNote: Object.assign({}, note),
|
||||||
note: note,
|
note: note,
|
||||||
};
|
};
|
||||||
@ -102,8 +120,10 @@ shared.saveNoteButton_press = async function(comp, folderId = null, options = nu
|
|||||||
comp.setState(newState);
|
comp.setState(newState);
|
||||||
|
|
||||||
if (isProvisionalNote) {
|
if (isProvisionalNote) {
|
||||||
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
|
const geoNote: any = await Note.updateGeolocation(note.id);
|
||||||
Note.updateGeolocation(note.id).then(geoNote => {
|
|
||||||
|
// TODO: CHECK - this code has never worked??
|
||||||
|
|
||||||
const stateNote = comp.state.note;
|
const stateNote = comp.state.note;
|
||||||
if (!stateNote || !geoNote) return;
|
if (!stateNote || !geoNote) return;
|
||||||
if (stateNote.id !== geoNote.id) return; // Another note has been loaded while geoloc was being retrieved
|
if (stateNote.id !== geoNote.id) return; // Another note has been loaded while geoloc was being retrieved
|
||||||
@ -121,19 +141,18 @@ shared.saveNoteButton_press = async function(comp, folderId = null, options = nu
|
|||||||
const modLastSavedNote = Object.assign({}, comp.state.lastSavedNote, geoInfo);
|
const modLastSavedNote = Object.assign({}, comp.state.lastSavedNote, geoInfo);
|
||||||
|
|
||||||
comp.setState({ note: modNote, lastSavedNote: modLastSavedNote });
|
comp.setState({ note: modNote, lastSavedNote: modLastSavedNote });
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseMutex();
|
releaseMutex();
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.saveOneProperty = async function(comp, name, value) {
|
shared.saveOneProperty = async function(comp: any, name: string, value: any) {
|
||||||
let note = Object.assign({}, comp.state.note);
|
let note = Object.assign({}, comp.state.note);
|
||||||
|
|
||||||
const recreatedNote = await shared.handleNoteDeletedWhileEditing_(note);
|
const recreatedNote = await shared.handleNoteDeletedWhileEditing_(note);
|
||||||
if (recreatedNote) note = recreatedNote;
|
if (recreatedNote) note = recreatedNote;
|
||||||
|
|
||||||
let toSave = { id: note.id };
|
let toSave: any = { id: note.id };
|
||||||
toSave[name] = value;
|
toSave[name] = value;
|
||||||
toSave = await Note.save(toSave);
|
toSave = await Note.save(toSave);
|
||||||
note[name] = toSave[name];
|
note[name] = toSave[name];
|
||||||
@ -144,8 +163,8 @@ shared.saveOneProperty = async function(comp, name, value) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.noteComponent_change = function(comp, propName, propValue) {
|
shared.noteComponent_change = function(comp: any, propName: string, propValue: any) {
|
||||||
const newState = {};
|
const newState: any = {};
|
||||||
|
|
||||||
const note = Object.assign({}, comp.state.note);
|
const note = Object.assign({}, comp.state.note);
|
||||||
note[propName] = propValue;
|
note[propName] = propValue;
|
||||||
@ -154,17 +173,17 @@ shared.noteComponent_change = function(comp, propName, propValue) {
|
|||||||
comp.setState(newState);
|
comp.setState(newState);
|
||||||
};
|
};
|
||||||
|
|
||||||
let resourceCache_ = {};
|
let resourceCache_: any = {};
|
||||||
|
|
||||||
shared.clearResourceCache = function() {
|
shared.clearResourceCache = function() {
|
||||||
resourceCache_ = {};
|
resourceCache_ = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.attachedResources = async function(noteBody) {
|
shared.attachedResources = async function(noteBody: string) {
|
||||||
if (!noteBody) return {};
|
if (!noteBody) return {};
|
||||||
const resourceIds = await Note.linkedItemIdsByType(BaseModel.TYPE_RESOURCE, noteBody);
|
const resourceIds = await Note.linkedItemIdsByType(BaseModel.TYPE_RESOURCE, noteBody);
|
||||||
|
|
||||||
const output = {};
|
const output: any = {};
|
||||||
for (let i = 0; i < resourceIds.length; i++) {
|
for (let i = 0; i < resourceIds.length; i++) {
|
||||||
const id = resourceIds[i];
|
const id = resourceIds[i];
|
||||||
|
|
||||||
@ -188,14 +207,14 @@ shared.attachedResources = async function(noteBody) {
|
|||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.isModified = function(comp) {
|
shared.isModified = function(comp: any) {
|
||||||
if (!comp.state.note || !comp.state.lastSavedNote) return false;
|
if (!comp.state.note || !comp.state.lastSavedNote) return false;
|
||||||
const diff = BaseModel.diffObjects(comp.state.lastSavedNote, comp.state.note);
|
const diff = BaseModel.diffObjects(comp.state.lastSavedNote, comp.state.note);
|
||||||
delete diff.type_;
|
delete diff.type_;
|
||||||
return !!Object.getOwnPropertyNames(diff).length;
|
return !!Object.getOwnPropertyNames(diff).length;
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.initState = async function(comp) {
|
shared.initState = async function(comp: any) {
|
||||||
const isProvisionalNote = comp.props.provisionalNoteIds.includes(comp.props.noteId);
|
const isProvisionalNote = comp.props.provisionalNoteIds.includes(comp.props.noteId);
|
||||||
|
|
||||||
const note = await Note.load(comp.props.noteId);
|
const note = await Note.load(comp.props.noteId);
|
||||||
@ -243,13 +262,13 @@ shared.initState = async function(comp) {
|
|||||||
comp.lastLoadedNoteId_ = note.id;
|
comp.lastLoadedNoteId_ = note.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.toggleIsTodo_onPress = function(comp) {
|
shared.toggleIsTodo_onPress = function(comp: any) {
|
||||||
const newNote = Note.toggleIsTodo(comp.state.note);
|
const newNote = Note.toggleIsTodo(comp.state.note);
|
||||||
const newState = { note: newNote };
|
const newState = { note: newNote };
|
||||||
comp.setState(newState);
|
comp.setState(newState);
|
||||||
};
|
};
|
||||||
|
|
||||||
function toggleCheckboxLine(ipcMessage, noteBody) {
|
function toggleCheckboxLine(ipcMessage: string, noteBody: string) {
|
||||||
const newBody = noteBody.split('\n');
|
const newBody = noteBody.split('\n');
|
||||||
const p = ipcMessage.split(':');
|
const p = ipcMessage.split(':');
|
||||||
const lineIndex = Number(p[p.length - 1]);
|
const lineIndex = Number(p[p.length - 1]);
|
||||||
@ -285,26 +304,26 @@ function toggleCheckboxLine(ipcMessage, noteBody) {
|
|||||||
return [newBody, lineIndex, line];
|
return [newBody, lineIndex, line];
|
||||||
}
|
}
|
||||||
|
|
||||||
shared.toggleCheckboxRange = function(ipcMessage, noteBody) {
|
shared.toggleCheckboxRange = function(ipcMessage: string, noteBody: string) {
|
||||||
const [lineIndex, line] = toggleCheckboxLine(ipcMessage, noteBody).slice(1);
|
const [lineIndex, line] = toggleCheckboxLine(ipcMessage, noteBody).slice(1);
|
||||||
const from = { line: lineIndex, ch: 0 };
|
const from = { line: lineIndex, ch: 0 };
|
||||||
const to = { line: lineIndex, ch: line.length };
|
const to = { line: lineIndex, ch: (line as any).length };
|
||||||
return { line, from, to };
|
return { line, from, to };
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.toggleCheckbox = function(ipcMessage, noteBody) {
|
shared.toggleCheckbox = function(ipcMessage: string, noteBody: string) {
|
||||||
const [newBody, lineIndex, line] = toggleCheckboxLine(ipcMessage, noteBody);
|
const [newBody, lineIndex, line] = toggleCheckboxLine(ipcMessage, noteBody);
|
||||||
newBody[lineIndex] = line;
|
(newBody as any)[lineIndex as any] = line;
|
||||||
return newBody.join('\n');
|
return (newBody as any).join('\n');
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.installResourceHandling = function(refreshResourceHandler) {
|
shared.installResourceHandling = function(refreshResourceHandler: any) {
|
||||||
ResourceFetcher.instance().on('downloadComplete', refreshResourceHandler);
|
ResourceFetcher.instance().on('downloadComplete', refreshResourceHandler);
|
||||||
ResourceFetcher.instance().on('downloadStarted', refreshResourceHandler);
|
ResourceFetcher.instance().on('downloadStarted', refreshResourceHandler);
|
||||||
DecryptionWorker.instance().on('resourceDecrypted', refreshResourceHandler);
|
DecryptionWorker.instance().on('resourceDecrypted', refreshResourceHandler);
|
||||||
};
|
};
|
||||||
|
|
||||||
shared.uninstallResourceHandling = function(refreshResourceHandler) {
|
shared.uninstallResourceHandling = function(refreshResourceHandler: any) {
|
||||||
ResourceFetcher.instance().off('downloadComplete', refreshResourceHandler);
|
ResourceFetcher.instance().off('downloadComplete', refreshResourceHandler);
|
||||||
ResourceFetcher.instance().off('downloadStarted', refreshResourceHandler);
|
ResourceFetcher.instance().off('downloadStarted', refreshResourceHandler);
|
||||||
DecryptionWorker.instance().off('resourceDecrypted', refreshResourceHandler);
|
DecryptionWorker.instance().off('resourceDecrypted', refreshResourceHandler);
|
Loading…
Reference in New Issue
Block a user