duplicate (single) note

This commit is contained in:
zadam
2019-10-19 12:36:16 +02:00
parent 00bb1236ce
commit b16c2d19b6
16 changed files with 147 additions and 70 deletions

View File

@@ -4,7 +4,7 @@ const build = require('./build');
const packageJson = require('../../package');
const {TRILIUM_DATA_DIR} = require('./data_dir');
const APP_DB_VERSION = 147;
const APP_DB_VERSION = 149;
const SYNC_VERSION = 10;
const CLIPPER_PROTOCOL_VERSION = "1.0";

View File

@@ -81,7 +81,7 @@ async function cloneNoteAfter(noteId, afterBranchId) {
// we don't change utcDateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified branches otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
await syncTable.addNoteReorderingSync(afterNote.parentNoteId);
@@ -89,7 +89,7 @@ async function cloneNoteAfter(noteId, afterBranchId) {
const branch = await new Branch({
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
notePosition: afterNote.notePosition + 1,
notePosition: afterNote.notePosition + 10,
isExpanded: 0
}).save();

View File

@@ -198,7 +198,7 @@ async function findExistencyIssues() {
const branches = await repository.getEntities(`SELECT * FROM branches WHERE noteId = ? and parentNoteId = ? and isDeleted = 1`, [noteId, parentNoteId]);
// it's not necessarily "original" branch, it's just the only one which will survive
const origBranch = branches.get(0);
const origBranch = branches[0];
// delete all but the first branch
for (const branch of branches.slice(1)) {

View File

@@ -21,15 +21,15 @@ async function getNewNotePosition(parentNoteId, noteData) {
if (noteData.target === 'into') {
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;
}
else if (noteData.target === 'after') {
const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteData.target_branchId]);
newNotePos = afterNote.notePosition + 1;
newNotePos = afterNote.notePosition + 10;
// not updating utcDateModified to avoig having to sync whole rows
await sql.execute('UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',
await sql.execute('UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',
[parentNoteId, afterNote.notePosition]);
await syncTableService.addNoteReorderingSync(parentNoteId);
@@ -465,6 +465,44 @@ async function cleanupDeletedNotes() {
await sql.execute("UPDATE note_revisions SET content = NULL WHERE note_revisions.content IS NOT NULL AND noteId IN (SELECT noteId FROM notes WHERE isDeleted = 1 AND notes.utcDateModified <= ?)", [dateUtils.utcDateStr(cutoffDate)]);
}
async function duplicateNote(noteId, parentNoteId) {
const origNote = await repository.getNote(noteId);
if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) {
throw new Error(`Cannot duplicate note=${origNote.noteId} because it is protected and protected session is not available`);
}
// might be null if orig note is not in the target parentNoteId
const origBranch = (await origNote.getBranches()).find(branch => branch.parentNoteId === parentNoteId);
const newNote = new Note(origNote);
newNote.noteId = undefined; // force creation of new note
newNote.title += " (dup)";
await newNote.save();
await newNote.setContent(await origNote.getContent());
const newBranch = await new Branch({
noteId: newNote.noteId,
parentNoteId: parentNoteId,
// here increasing just by 1 to make sure it's directly after original
notePosition: origBranch ? origBranch.notePosition + 1 : null
}).save();
for (const attribute of await origNote.getAttributes()) {
const attr = new Attribute(attribute);
attr.attributeId = undefined; // force creation of new attribute
attr.noteId = newNote.noteId;
await attr.save();
}
return {
note: newNote,
branch: newBranch
};
}
sqlInit.dbReady.then(() => {
// first cleanup kickoff 5 minutes after startup
setTimeout(cls.wrap(cleanupDeletedNotes), 5 * 60 * 1000);
@@ -478,5 +516,6 @@ module.exports = {
updateNote,
deleteBranch,
protectNoteRecursively,
scanForLinks
scanForLinks,
duplicateNote
};

View File

@@ -109,7 +109,7 @@ async function createInitialDatabase(username, password, theme) {
noteId: 'root',
parentNoteId: 'none',
isExpanded: true,
notePosition: 0
notePosition: 10
}).save();
const dummyTaskContext = new TaskContext("1", 'import', false);

View File

@@ -110,13 +110,13 @@ async function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
}
});
let position = 1;
let position = 10;
for (const note of notes) {
await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
[position, note.branchId]);
position++;
position += 10;
}
await syncTableService.addNoteReorderingSync(parentNoteId);