cut selection and put it into subnote, fixes #171

This commit is contained in:
zadam 2018-09-06 22:58:46 +02:00 committed by azivner
parent c76329e671
commit 1ef3188213
4 changed files with 48 additions and 10 deletions

2
.idea/dataSources.xml generated
View File

@ -5,7 +5,7 @@
<driver-ref>sqlite.xerial</driver-ref> <driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize> <synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver> <jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$USER_HOME$/trilium-data/document.db</jdbc-url> <jdbc-url>jdbc:sqlite:$PROJECT_DIR$/../trilium-data/document.db</jdbc-url>
<driver-properties> <driver-properties>
<property name="enable_load_extension" value="true" /> <property name="enable_load_extension" value="true" />
</driver-properties> </driver-properties>

View File

@ -443,10 +443,10 @@ async function setNoteTitle(noteId, title) {
async function createNewTopLevelNote() { async function createNewTopLevelNote() {
const rootNode = getNodesByNoteId('root')[0]; const rootNode = getNodesByNoteId('root')[0];
await createNote(rootNode, "root", "into"); await createNote(rootNode, "root", "into", false);
} }
async function createNote(node, parentNoteId, target, isProtected) { async function createNote(node, parentNoteId, target, isProtected, saveSelection = false) {
utils.assertArguments(node, parentNoteId, target); utils.assertArguments(node, parentNoteId, target);
// if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted // if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted
@ -455,15 +455,33 @@ async function createNote(node, parentNoteId, target, isProtected) {
isProtected = false; isProtected = false;
} }
const newNoteName = "new note"; if (noteDetailService.getCurrentNoteType() !== 'text') {
saveSelection = false;
}
let title, content;
if (saveSelection) {
[title, content] = parseSelectedHtml(window.cutToNote.getSelectedHtml());
}
const newNoteName = title || "new note";
const {note, branch} = await server.post('notes/' + parentNoteId + '/children', { const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
title: newNoteName, title: newNoteName,
content: content,
target: target, target: target,
target_branchId: node.data.branchId, target_branchId: node.data.branchId,
isProtected: isProtected isProtected: isProtected
}); });
if (saveSelection) {
// we remove the selection only after it was saved to server to make sure we don't lose anything
window.cutToNote.removeSelection();
}
await noteDetailService.saveNoteIfChanged();
const noteEntity = new NoteShort(treeCache, note); const noteEntity = new NoteShort(treeCache, note);
const branchEntity = new Branch(treeCache, branch); const branchEntity = new Branch(treeCache, branch);
@ -506,6 +524,22 @@ async function createNote(node, parentNoteId, target, isProtected) {
infoService.showMessage("Created!"); infoService.showMessage("Created!");
} }
/* If first element is heading, parse it out and use it as a new heading. */
function parseSelectedHtml(selectedHtml) {
const dom = $.parseHTML(selectedHtml);
if (dom.length > 0 && dom[0].tagName && dom[0].tagName.match(/h[1-6]/i)) {
const title = $(dom[0]).text();
const domWithoutTitle = dom.slice(1);
const content = domWithoutTitle.map(el => $(el).html()).join("");
return [title, content];
}
else {
return [null, selectedHtml];
}
}
async function sortAlphabetically(noteId) { async function sortAlphabetically(noteId) {
await server.put('notes/' + noteId + '/sort'); await server.put('notes/' + noteId + '/sort');
@ -539,14 +573,18 @@ utils.bindShortcut('ctrl+o', () => {
const parentNoteId = node.data.parentNoteId; const parentNoteId = node.data.parentNoteId;
const isProtected = treeUtils.getParentProtectedStatus(node); const isProtected = treeUtils.getParentProtectedStatus(node);
createNote(node, parentNoteId, 'after', isProtected); createNote(node, parentNoteId, 'after', isProtected, true);
}); });
utils.bindShortcut('ctrl+p', () => { function createNoteInto() {
const node = getCurrentNode(); const node = getCurrentNode();
createNote(node, node.data.noteId, 'into', node.data.isProtected); createNote(node, node.data.noteId, 'into', node.data.isProtected, true);
}); }
window.glob.createNoteInto = createNoteInto;
utils.bindShortcut('ctrl+p', createNoteInto);
utils.bindShortcut('ctrl+del', () => { utils.bindShortcut('ctrl+del', () => {
const node = getCurrentNode(); const node = getCurrentNode();

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long