"save note" is now "save link with note" - i.e. we're saving a current URL together with the text note

This commit is contained in:
zadam 2021-03-31 22:23:43 +02:00
parent 1c75ea89ad
commit 0811276fbb
6 changed files with 64 additions and 55 deletions

View File

@ -237,18 +237,25 @@ async function saveWholePage() {
toast("Page has been saved to Trilium.", resp.noteId);
}
async function saveNote(title, content) {
async function saveLinkWithNote(title, content) {
const activeTab = await getActiveTab();
if (!title.trim()) {
title = activeTab.title;
}
const resp = await triliumServerFacade.callService('POST', 'notes', {
title: title,
content: content,
clipType: 'note'
clipType: 'note',
pageUrl: activeTab.url
});
if (!resp) {
return false;
}
toast("Note has been saved to Trilium.", resp.noteId);
toast("Link with note has been saved to Trilium.", resp.noteId);
return true;
}
@ -330,8 +337,8 @@ browser.runtime.onMessage.addListener(async request => {
else if (request.name === 'save-whole-page') {
return await saveWholePage();
}
else if (request.name === 'save-note') {
return await saveNote(request.title, request.content);
else if (request.name === 'save-link-with-note') {
return await saveLinkWithNote(request.title, request.content);
}
else if (request.name === 'trigger-trilium-search') {
triliumServerFacade.triggerSearchForTrilium();

View File

@ -23,24 +23,6 @@ function pageTitle() {
return titleElements.length ? titleElements[0].text.trim() : document.title.trim();
}
function getPageLocationOrigin() {
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.
return location.protocol === 'file:' ? 'file://' : location.origin;
}
function getBaseUrl() {
let output = getPageLocationOrigin() + location.pathname;
if (output[output.length - 1] !== '/') {
output = output.split('/');
output.pop();
output = output.join('/');
}
return output;
}
function getReadableDocument() {
// Readability directly change the passed document so clone it so as
// to preserve the original web page.
@ -304,4 +286,4 @@ async function requireLib(libPath) {
await browser.runtime.sendMessage({name: 'load-script', file: libPath});
}
}
}

View File

@ -24,11 +24,11 @@ body {
width: 100%;
}
#create-text-note-wrapper {
#save-link-with-note-wrapper {
display: none;
}
#create-text-note-textarea {
#save-link-with-note-textarea {
width: 100%;
}
@ -45,4 +45,4 @@ body {
button[disabled] {
color: #aaa;
}
}

View File

@ -20,10 +20,10 @@
<button class="button full needs-connection" id="clip-screenshot-button">Clip screenshot</button>
<button class="button full needs-connection" id="save-whole-page-button">Save whole page</button>
<button class="button full needs-connection" id="create-text-note-button">Create text note</button>
<button class="button full needs-connection" id="save-link-with-note-button">Save link with a note</button>
<div id="create-text-note-wrapper">
<textarea id="create-text-note-textarea" rows="5"></textarea>
<div id="save-link-with-note-wrapper">
<textarea id="save-link-with-note-textarea" rows="5"></textarea>
<div style="display: flex;">
<button type="submit" class="button wide" id="save-button">Save</button>
@ -41,6 +41,7 @@
<script src="../lib/browser-polyfill.js"></script>
<script src="../lib/cash.min.js"></script>
<script src="popup.js"></script>
<script src="../utils.js"></script>
</body>
</html>
</html>

View File

@ -19,54 +19,55 @@ $clipScreenShotButton.on("click", () => sendMessage({name: 'save-screenshot'}));
$saveWholePageButton.on("click", () => sendMessage({name: 'save-whole-page'}));
const $createTextNoteWrapper = $("#create-text-note-wrapper");
const $textNote = $("#create-text-note-textarea");
const $saveLinkWithNoteWrapper = $("#save-link-with-note-wrapper");
const $textNote = $("#save-link-with-note-textarea");
$textNote.on('keypress', function (event) {
if (event.which === 10 || event.which === 13 && event.ctrlKey) {
saveNote();
if ((event.which === 10 || event.which === 13) && event.ctrlKey) {
saveLinkWithNote();
return false;
}
return true;
});
$("#create-text-note-button").on("click", () => {
$createTextNoteWrapper.show();
$("#save-link-with-note-button").on("click", () => {
$saveLinkWithNoteWrapper.show();
$textNote[0].focus();
});
$("#cancel-button").on("click", () => {
$createTextNoteWrapper.hide();
$saveLinkWithNoteWrapper.hide();
$textNote.val("");
window.close();
});
async function saveNote() {
async function saveLinkWithNote() {
const textNoteVal = $textNote.val().trim();
if (textNoteVal.length === 0) {
alert("Note is empty. Please enter some text");
return;
}
const match = /^(.*?)([.?!]\s|\n)/.exec(textNoteVal);
let title, content;
if (match) {
title = match[0].trim();
content = textNoteVal.substr(title.length).trim();
if (!textNoteVal) {
title = '';
content = '';
}
else {
title = textNoteVal;
content = '';
const match = /^(.*?)([.?!]\s|\n)/.exec(textNoteVal);
if (match) {
title = match[0].trim();
content = textNoteVal.substr(title.length).trim();
}
else {
title = textNoteVal;
content = '';
}
}
content = escapeHtml(content);
const result = await sendMessage({name: 'save-note', title, content});
const result = await sendMessage({name: 'save-link-with-note', title, content});
if (result) {
$textNote.val('');
@ -75,7 +76,7 @@ async function saveNote() {
}
}
$("#save-button").on("click", saveNote);
$("#save-button").on("click", saveLinkWithNote);
$("#show-help-button").on("click", () => {
window.open("https://github.com/zadam/trilium/wiki/Web-clipper", '_blank');
@ -141,4 +142,4 @@ $checkConnectionButton.on("click", () => {
})
});
$(() => browser.runtime.sendMessage({name: "send-trilium-search-status"}));
$(() => browser.runtime.sendMessage({name: "send-trilium-search-status"}));

View File

@ -7,4 +7,22 @@ function randomString(len) {
}
return text;
}
}
function getBaseUrl() {
let output = getPageLocationOrigin() + location.pathname;
if (output[output.length - 1] !== '/') {
output = output.split('/');
output.pop();
output = output.join('/');
}
return output;
}
function getPageLocationOrigin() {
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.
return location.protocol === 'file:' ? 'file://' : location.origin;
}