adding clone from recent notes dialog

This commit is contained in:
azivner 2017-11-19 16:35:35 -05:00
parent 00151beded
commit 3610926804
7 changed files with 109 additions and 12 deletions

View File

@ -0,0 +1,24 @@
CREATE TABLE "notes_tree_mig" (
[note_tree_id] VARCHAR(30) PRIMARY KEY NOT NULL,
[note_id] VARCHAR(30) NOT NULL,
[note_pid] VARCHAR(30) NOT NULL,
[note_pos] INTEGER NOT NULL,
[is_expanded] BOOLEAN NULL ,
date_modified INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0
);
INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pid, note_pos, is_expanded, date_modified, is_deleted)
SELECT note_tree_id, note_id, note_pid, note_pos, is_expanded, date_modified, is_deleted FROM notes_tree;
DROP TABLE notes_tree;
ALTER TABLE notes_tree_mig RENAME TO notes_tree;
CREATE INDEX `IDX_notes_tree_note_tree_id` ON `notes_tree` (
`note_tree_id`
);
CREATE INDEX `IDX_notes_tree_note_id_note_pid` ON `notes_tree` (
`note_id`,
`note_pid`
);

View File

@ -5,6 +5,8 @@ const recentNotes = (function() {
const selectBoxEl = $('#recent-notes-select-box');
const jumpToButtonEl = $('#recentNotesJumpTo');
const addLinkButtonEl = $('#recentNotesAddLink');
const addCurrentAsChildEl = $("#recent-notes-add-current-as-child");
const addRecentAsChildEl = $("#recent-notes-add-recent-as-child");
const noteDetailEl = $('#note-detail');
let list = [];
@ -31,9 +33,10 @@ const recentNotes = (function() {
}, 1500);
}
// FIXME: this should be probably just refresh upon deletion, not explicit delete
function removeRecentNote(notePathIdToRemove) {
$.ajax({
url: baseApiUrl + 'recent-notes/' + notePathIdToRemove,
url: baseApiUrl + 'recent-notes/' + encodeURIComponent(notePathIdToRemove),
type: 'DELETE',
error: () => showError("Error removing note from recent notes.")
}).then(result => {
@ -72,12 +75,12 @@ const recentNotes = (function() {
});
}
function getSelectedNotePathFromRecentNotes() {
function getSelectedNotePath() {
return selectBoxEl.find("option:selected").val();
}
function setActiveNoteBasedOnRecentNotes() {
const notePath = getSelectedNotePathFromRecentNotes();
const notePath = getSelectedNotePath();
noteTree.activateNode(notePath);
@ -85,9 +88,9 @@ const recentNotes = (function() {
}
function addLinkBasedOnRecentNotes() {
const notePath = getSelectedNotePathFromRecentNotes();
const notePath = getSelectedNotePath();
const linkTitle = treeUtils.getNoteTitle(notePath);
const linkTitle = noteTree.getNoteTitle(notePath);
dialogEl.dialog("close");
@ -100,9 +103,33 @@ const recentNotes = (function() {
});
}
async function addAsChild(parentNotePath, childNotePath) {
const parentNoteId = treeUtils.getNoteIdFromNotePath(parentNotePath);
const childNoteId = treeUtils.getNoteIdFromNotePath(childNotePath);
await $.ajax({
url: baseApiUrl + 'tree/' + parentNoteId + '/addChild/' + childNoteId,
type: 'PUT',
error: () => showError("Error adding child.")
});
dialogEl.dialog("close");
await noteTree.reload();
}
async function addCurrentAsChild() {
await addAsChild(getSelectedNotePath(), noteTree.getCurrentNotePath());
}
async function addRecentAsChild() {
addAsChild(noteTree.getCurrentNotePath(), getSelectedNotePath());
}
selectBoxEl.keydown(e => {
const key = e.which;
// to get keycodes use http://keycode.info/
if (key === 13)// the enter key code
{
setActiveNoteBasedOnRecentNotes();
@ -110,6 +137,12 @@ const recentNotes = (function() {
else if (key === 76 /* l */) {
addLinkBasedOnRecentNotes();
}
else if (key === 67 /* c */) {
addCurrentAsChild();
}
else if (key === 82 /* r */) {
addRecentAsChild()
}
else {
return; // avoid prevent default
}
@ -125,6 +158,8 @@ const recentNotes = (function() {
jumpToButtonEl.click(setActiveNoteBasedOnRecentNotes);
addLinkButtonEl.click(addLinkBasedOnRecentNotes);
addCurrentAsChildEl.click(addCurrentAsChild);
addRecentAsChildEl.click(addRecentAsChild);
return {
showDialog,

View File

@ -256,10 +256,12 @@ const noteTree = (function() {
setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false);
},
init: (event, data) => {
showAppIfHidden();
if (startNoteTreeId) {
activateNode(startNoteTreeId);
}
else {
showAppIfHidden();
}
},
hotkeys: {
keydown: keybindings

View File

@ -6,10 +6,10 @@ const sql = require('../../services/sql');
const options = require('../../services/options');
const utils = require('../../services/utils');
const auth = require('../../services/auth');
const log = require('../../services/log');
const protected_session = require('../../services/protected_session');
const data_encryption = require('../../services/data_encryption');
const notes = require('../../services/notes');
const sync_table = require('../../services/sync_table');
router.get('/', auth.checkApiAuth, async (req, res, next) => {
const notes = await sql.getResults("select "
@ -48,4 +48,37 @@ router.put('/:noteId/protectSubTree/:isProtected', auth.checkApiAuth, async (req
res.send({});
});
router.put('/:parentNoteId/addChild/:childNoteId', auth.checkApiAuth, async (req, res, next) => {
const parentNoteId = req.params.parentNoteId;
const childNoteId = req.params.childNoteId;
const existing = await sql.getSingleValue('select * from notes_tree where note_id = ? and note_pid = ?', [childNoteId, parentNoteId]);
if (!existing) {
const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const noteTreeId = utils.newNoteTreeId();
await sql.doInTransaction(async () => {
await sync_table.addNoteTreeSync(noteTreeId);
await sql.insert("notes_tree", {
'note_tree_id': noteTreeId,
'note_id': childNoteId,
'note_pid': parentNoteId,
'note_pos': newNotePos,
'is_expanded': 0,
'date_modified': utils.nowTimestamp(),
'is_deleted': 0
});
});
}
else if (existing && existing.is_deleted) {
await sql.execute("UPDATE notes_tree SET is_deleted = 0 WHERE note_tree_id = ?", [existing.note_tree_id]);
}
res.send({});
});
module.exports = router;

View File

@ -4,7 +4,7 @@ const options = require('./options');
const fs = require('fs-extra');
const log = require('./log');
const APP_DB_VERSION = 41;
const APP_DB_VERSION = 42;
const MIGRATIONS_DIR = "migrations";
async function migrate() {

View File

@ -13,8 +13,7 @@ async function createNewNote(parentNoteId, note, browserId) {
let newNotePos = 0;
if (note.target === 'into') {
const res = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]);
const maxNotePos = res['max_note_pos'];
const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]);
if (maxNotePos === null) // no children yet
newNotePos = 0;
@ -36,7 +35,7 @@ async function createNewNote(parentNoteId, note, browserId) {
await sql.doInTransaction(async () => {
await sql.addAudit(audit_category.CREATE_NOTE, browserId, noteId);
await sync_table.addNoteTreeSync(noteId);
await sync_table.addNoteTreeSync(noteTreeId);
await sync_table.addNoteSync(noteId);
const now = utils.nowTimestamp();
@ -56,7 +55,7 @@ async function createNewNote(parentNoteId, note, browserId) {
'note_pid': parentNoteId,
'note_pos': newNotePos,
'is_expanded': 0,
'date_modified': utils.nowTimestamp(),
'date_modified': now,
'is_deleted': 0
});
});

View File

@ -107,6 +107,10 @@
<button class="btn btn-sm" id="recent-notes-jump-to">Jump to (enter)</button>
&nbsp;
<button class="btn btn-sm" id="recent-notes-add-link">Add link (l)</button>
<button class="btn btn-sm" id="recent-notes-add-current-as-child">Add current as child (c)</button>
<button class="btn btn-sm" id="recent-notes-add-recent-as-child">Add recent as child (r)</button>
</p>
</div>