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

Got tags working and fixed sync issue

This commit is contained in:
Laurent Cozic 2017-10-22 18:12:16 +01:00
parent 0ee82bd5ce
commit 17b8df2abc
10 changed files with 238 additions and 34 deletions

View File

@ -1,5 +1,6 @@
import { Logger } from 'lib/logger.js';
import { Folder } from 'lib/models/folder.js';
import { Tag } from 'lib/models/tag.js';
import { Note } from 'lib/models/note.js';
import { cliUtils } from './cli-utils.js';
import { reducer, defaultState } from 'lib/reducer.js';
@ -95,16 +96,29 @@ class AppGui {
folderList.name = 'folderList';
folderList.vStretch = true;
folderList.on('currentItemChange', async () => {
const folder = folderList.currentItem;
this.store_.dispatch({
type: 'FOLDERS_SELECT',
folderId: folder ? folder.id : 0,
});
const item = folderList.currentItem;
if (item.type_ === Folder.modelType()) {
this.store_.dispatch({
type: 'FOLDERS_SELECT',
folderId: item ? item.id : 0,
});
} else if (item.type_ === Tag.modelType()) {
this.store_.dispatch({
type: 'TAGS_SELECT',
tagId: item ? item.id : 0,
});
}
});
this.rootWidget_.connect(folderList, (state) => {
this.logger().info('Updating folder list...');
return {
selectedFolderId: state.selectedFolderId,
items: state.folders,
selectedTagId: state.selectedTagId,
notesParentType: state.notesParentType,
folders: state.folders,
tags: state.tags,
};
});

View File

@ -8,6 +8,7 @@ import { BaseModel } from 'lib/base-model.js';
import { Folder } from 'lib/models/folder.js';
import { BaseItem } from 'lib/models/base-item.js';
import { Note } from 'lib/models/note.js';
import { Tag } from 'lib/models/tag.js';
import { Setting } from 'lib/models/setting.js';
import { Logger } from 'lib/logger.js';
import { sprintf } from 'sprintf-js';
@ -67,7 +68,6 @@ class Application {
}
switchCurrentFolder(folder) {
this.logger().info('SWITCHING TO ' + (folder ? folder.id : ''));
this.currentFolder_ = folder;
Setting.setValue('activeFolderId', folder ? folder.id : '');
@ -420,7 +420,7 @@ class Application {
return this.activeCommand_;
}
async refreshNotes() {
async refreshNotes(parentType, parentId) {
const state = this.store().getState();
let options = {
@ -428,11 +428,17 @@ class Application {
uncompletedTodosOnTop: Setting.value('uncompletedTodosOnTop'),
};
const notes = await Note.previews(state.selectedFolderId, options);
const source = JSON.stringify({
options: options,
parentId: parentId,
});
const notes = parentType === Folder.modelType() ? await Note.previews(parentId, options) : await Tag.notes(parentId);
this.store().dispatch({
type: 'NOTES_UPDATE_ALL',
notes: notes,
notesSource: source,
});
this.store().dispatch({
@ -444,20 +450,28 @@ class Application {
reducerActionToString(action) {
let o = [action.type];
if (action.noteId) o.push(action.noteId);
if (action.folderI) o.push(action.folderI);
if (action.folderId) o.push(action.folderId);
if (action.tagId) o.push(action.tagId);
if (action.tag) o.push(action.tag.id);
if (action.folder) o.push(action.folder.id);
if (action.notesSource) o.push(JSON.stringify(action.notesSource));
return o.join(', ');
}
generalMiddleware() {
const middleware = store => next => async (action) => {
this.logger().info('Reducer action', this.reducerActionToString(action));
this.logger().debug('Reducer action', this.reducerActionToString(action));
const result = next(action);
const newState = store.getState();
if (action.type == 'FOLDERS_SELECT') {
Setting.setValue('activeFolderId', newState.selectedFolderId);
await this.refreshNotes();
await this.refreshNotes(Folder.modelType(), newState.selectedFolderId);
}
if (action.type == 'TAGS_SELECT') {
await this.refreshNotes(Tag.modelType(), action.tagId);
}
if (this.gui() && action.type == 'SETTINGS_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTINGS_UPDATE_ALL') {
@ -512,7 +526,7 @@ class Application {
this.dbLogger_.setLevel(initArgs.logLevel);
if (Setting.value('env') === 'dev') {
this.dbLogger_.setLevel(Logger.LEVEL_DEBUG);
this.dbLogger_.setLevel(Logger.LEVEL_WARN);
}
const packageJson = require('./package.json');
@ -582,6 +596,13 @@ class Application {
await FoldersScreenUtils.refreshFolders();
const tags = await Tag.allWithNotes();
this.dispatch({
type: 'TAGS_UPDATE_ALL',
tags: tags,
});
this.store().dispatch({
type: 'FOLDERS_SELECT',
folderId: Setting.value('activeFolderId'),

View File

@ -23,6 +23,8 @@ class Command extends BaseCommand {
note = await Note.save(note);
Note.updateGeolocation(note.id);
app().switchCurrentFolder(app().currentFolder());
}
}

View File

@ -24,6 +24,8 @@ class Command extends BaseCommand {
note = await Note.save(note);
Note.updateGeolocation(note.id);
app().switchCurrentFolder(app().currentFolder());
}
}

View File

@ -1,16 +1,29 @@
const Folder = require('lib/models/folder.js').Folder;
const Tag = require('lib/models/tag.js').Tag;
const ListWidget = require('tkwidgets/ListWidget.js');
class FolderListWidget extends ListWidget {
constructor() {
super();
this.selectedFolderId_ = 0;
this.tags_ = [];
this.folders_ = [];
this.selectedFolderId_ = null;
this.selectedTagId_ = null;
this.notesParentType_ = 'Folder';
this.updateIndexFromSelectedFolderId_ = false;
this.updateItems_ = false;
this.itemRenderer = (item) => {
return item.title; //+ ' ' + item.id;
let output = [];
if (item.type_ === Folder.modelType()) {
output.push('[n]');
} else if (item.type_ === Tag.modelType()) {
output.push('[t]');
}
output.push(item.title);
return output.join(' ');
};
}
@ -21,11 +34,66 @@ class FolderListWidget extends ListWidget {
set selectedFolderId(v) {
this.updateIndexFromSelectedFolderId_ = true;
this.selectedFolderId_ = v;
this.invalidate();
}
get selectedTagId() {
return this.selectedTagId_;
}
set selectedTagId(v) {
this.updateIndexFromSelectedFolderId_ = true;
this.selectedTagId_ = v;
this.invalidate();
}
get notesParentType() {
return this.notesParentType_;
}
set notesParentType(v) {
if (this.notesParentType_ === v) return;
this.notesParentType_ = v;
this.updateIndexFromSelectedFolderId_ = true;
this.invalidate();
}
get tags() {
return this.tags_;
}
set tags(v) {
if (this.tags_ === v) return;
this.tags_ = v;
this.updateItems_ = true;
this.updateIndexFromSelectedFolderId_ = true;
this.invalidate();
}
get folders() {
return this.folders_;
}
set folders(v) {
if (this.folders_ === v) return;
this.folders_ = v;
this.updateItems_ = true;
this.updateIndexFromSelectedFolderId_ = true;
this.invalidate();
}
async onWillRender() {
if (this.updateItems_) {
this.items = this.folders.concat(this.tags);
this.updateItems_ = false;
}
}
render() {
if (this.updateIndexFromSelectedFolderId_) {
const index = this.itemIndexByKey('id', this.selectedFolderId_);
const index = this.itemIndexByKey('id', this.notesParentType === 'Folder' ? this.selectedFolderId : this.selectedTagId);
this.currentIndex = index >= 0 ? index : 0;
this.updateIndexFromSelectedFolderId_ = false;
}

View File

@ -238,7 +238,11 @@ class BaseModel {
}
if (!o.user_created_time && this.hasField('user_created_time')) {
o.user_created_time = timeNow;
o.user_created_time = o.created_time ? o.created_time : timeNow;
}
if (!o.user_updated_time && this.hasField('user_updated_time')) {
o.user_updated_time = o.updated_time ? o.updated_time : timeNow;
}
query = Database.insertQuery(this.tableName(), o);

View File

@ -193,6 +193,14 @@ class JoplinDatabase extends Database {
// 1. Add the new version number to the existingDatabaseVersions array
// 2. Add the upgrade logic to the "switch (targetVersion)" statement below
// IMPORTANT:
//
// Whenever adding a new database property, some additional logic might be needed
// in the synchronizer to handle this property. For example, when adding a property
// that should have a default value, existing remote items will not have this
// default value and thus might cause problems. In that case, the default value
// must be set in the synchronizer too.
const existingDatabaseVersions = [0, 1, 2, 3, 4, 5];
let currentVersionIndex = existingDatabaseVersions.indexOf(fromVersion);

View File

@ -43,10 +43,17 @@ class Tag extends BaseItem {
let hasIt = await this.hasNote(tagId, noteId);
if (hasIt) return;
return NoteTag.save({
const output = await NoteTag.save({
tag_id: tagId,
note_id: noteId,
});
this.dispatch({
type: 'TAGS_UPDATE_ONE',
tag: await Tag.load(tagId),
});
return output;
}
static async removeNote(tagId, noteId) {
@ -54,6 +61,11 @@ class Tag extends BaseItem {
for (let i = 0; i < noteTags.length; i++) {
await NoteTag.delete(noteTags[i].id);
}
this.dispatch({
type: 'TAGS_UPDATE_ONE',
tag: await Tag.load(tagId),
});
}
static async hasNote(tagId, noteId) {
@ -61,6 +73,20 @@ class Tag extends BaseItem {
return !!r;
}
static async allWithNotes() {
return await Tag.modelSelectAll('SELECT * FROM tags WHERE id IN (SELECT DISTINCT tag_id FROM note_tags)');
}
static async save(o, options = null) {
return super.save(o, options).then((tag) => {
this.dispatch({
type: 'TAGS_UPDATE_ONE',
tag: tag,
});
return tag;
});
}
}
export { Tag };

View File

@ -37,6 +37,33 @@ function historyCanGoBackTo(route) {
return true;
}
function updateOneTagOrFolder(state, action) {
let newItems = action.type === 'TAGS_UPDATE_ONE' ? state.tags.splice(0) : state.folders.splice(0);
let item = action.type === 'TAGS_UPDATE_ONE' ? action.tag : action.folder;
var found = false;
for (let i = 0; i < newItems.length; i++) {
let n = newItems[i];
if (n.id == item.id) {
newItems[i] = Object.assign(newItems[i], item);
found = true;
break;
}
}
if (!found) newItems.push(item);
let newState = Object.assign({}, state);
if (action.type === 'TAGS_UPDATE_ONE') {
newState.tags = newItems;
} else {
newState.folders = newItems;
}
return newState;
}
const reducer = (state = defaultState, action) => {
let newState = state;
let historyGoingBack = false;
@ -128,6 +155,7 @@ const reducer = (state = defaultState, action) => {
newState = Object.assign({}, state);
newState.selectedFolderId = action.folderId;
newState.notesParentType = 'Folder';
break;
case 'SETTINGS_UPDATE_ALL':
@ -231,23 +259,52 @@ const reducer = (state = defaultState, action) => {
newState.tags = action.tags;
break;
case 'FOLDERS_UPDATE_ONE':
var newFolders = state.folders.splice(0);
var found = false;
for (let i = 0; i < newFolders.length; i++) {
let n = newFolders[i];
if (n.id == action.folder.id) {
newFolders[i] = Object.assign(newFolders[i], action.folder);
found = true;
break;
}
}
if (!found) newFolders.push(action.folder);
case 'TAGS_SELECT':
newState = Object.assign({}, state);
newState.folders = newFolders;
newState.selectedTagId = action.tagId;
newState.notesParentType = 'Tag';
break;
case 'TAGS_UPDATE_ONE':
newState = updateOneTagOrFolder(state, action);
// var newTags = state.tags.splice(0);
// var found = false;
// for (let i = 0; i < newTags.length; i++) {
// let n = newTags[i];
// if (n.id == action.tag.id) {
// newTags[i] = Object.assign(newTags[i], action.tag);
// found = true;
// break;
// }
// }
// if (!found) newTags.push(action.tag);
// newState = Object.assign({}, state);
// newState.tags = newTags;
break;
case 'FOLDERS_UPDATE_ONE':
newState = updateOneTagOrFolder(state, action);
// var newFolders = state.folders.splice(0);
// var found = false;
// for (let i = 0; i < newFolders.length; i++) {
// let n = newFolders[i];
// if (n.id == action.folder.id) {
// newFolders[i] = Object.assign(newFolders[i], action.folder);
// found = true;
// break;
// }
// }
// if (!found) newFolders.push(action.folder);
// newState = Object.assign({}, state);
// newState.folders = newFolders;
break;
case 'FOLDER_DELETE':

View File

@ -413,7 +413,6 @@ class Synchronizer {
let newContent = Object.assign({}, content);
let options = {
autoTimestamp: false,
applyMetadataChanges: true,
nextQueries: BaseItem.updateSyncTimeQueries(syncTargetId, newContent, time.unixMs()),
};
if (action == 'createLocal') options.isNew = true;
@ -424,6 +423,9 @@ class Synchronizer {
await this.api().get(remoteResourceContentPath, { path: localResourceContentPath, target: 'file' });
}
if (!newContent.user_updated_time) newContent.user_updated_time = newContent.updated_time;
if (!newContent.user_created_time) newContent.user_created_time = newContent.created_time;
await ItemClass.save(newContent, options);
} else if (action == 'deleteLocal') {