Skip to content

Commit

Permalink
Merge pull request #392 from penge/3_23
Browse files Browse the repository at this point in the history
3.23
  • Loading branch information
penge authored Jul 24, 2022
2 parents 972cc74 + a923f3e commit 417d9dd
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 105 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "My Notes",
"description": "Simple and fast note-taking.",
"version": "3.22.1",
"version": "3.23",
"homepage_url": "https://github.com/penge/my-notes",
"icons": {
"128": "images/icon128.png"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "my-notes",
"version": "3.22.1",
"version": "3.23.0",
"description": "Simple and fast note-taking.",
"author": "Pavel Bucka",
"license": "MIT",
Expand Down
14 changes: 7 additions & 7 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ import {
handleChangedPermissions,
} from "background/init/permissions";

// Run when Installed or Updated
chrome.runtime.onInstalled.addListener((details) => {
setId(); // Set unique My Notes ID, if not set before
runMigrations(); // Migrate notes and options (font type, font size, etc.)
showNewVersionNotification(details); // Notifications (NEW VERSION installed)
});

// Click on My Notes icon, or use a keyboard shortcut (see chrome://extensions/shortcuts)
openMyNotesOnIconClick();
openMyNotesOnKeyboardShortcut();
Expand All @@ -49,3 +42,10 @@ saveTextOnRemoteTransfer(); // when you use "Save to remotely open My Notes" fro

// Permissions
handleChangedPermissions(); // react to granted/removed optional permissions: "identity"

// Run when Installed or Updated
chrome.runtime.onInstalled.addListener((details) => {
setId(); // Set unique My Notes ID, if not set before
runMigrations(); // Migrate notes and options (font type, font size, etc.)
showNewVersionNotification(details); // Notifications (NEW VERSION installed)
});
15 changes: 0 additions & 15 deletions src/background/init/context-menu/content.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/background/init/context-menu/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type Handler = (info: chrome.contextMenus.OnClickData) => string;

const getPageUrlHtml = (pageUrl: string) => `<a href="${pageUrl}" target="_blank">${pageUrl}</a>`;

const getUrlToSave: Handler = (info) => {
const { pageUrl } = info;
const pageUrlHtml = getPageUrlHtml(pageUrl);
const toSave = `${pageUrlHtml}<br><br>`;
return toSave;
};

const getSelectionToSave: Handler = (info) => {
const { pageUrl, selectionText } = info;
const pageUrlHtml = getPageUrlHtml(pageUrl);
const toSave = `${selectionText ?? ""}<br><b>(${pageUrlHtml})</b><br><br>`;
return toSave;
};

const handlers: Partial<Record<chrome.contextMenus.ContextType, Handler>> = {
page: getUrlToSave,
selection: getSelectionToSave,
};

export const getTextToSave = (context: chrome.contextMenus.ContextType, info: chrome.contextMenus.OnClickData): string => {
const handler = handlers[context];
if (!handler) {
return "";
}

return handler(info);
};
184 changes: 105 additions & 79 deletions src/background/init/context-menu/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { NotesObject } from "shared/storage/schema";
import { tString } from "i18n";
import { getUrlToSave, getSelectionToSave } from "./content";
import { getTextToSave } from "./context";
import {
CLIPBOARD_NOTE_NAME,
saveTextToLocalMyNotes,
saveTextToRemotelyOpenMyNotes,
CLIPBOARD_NOTE_NAME,
} from "../saving";
import { notify } from "../notifications";

const ID = "my-notes";

const MY_NOTES_SAVE_URL_TO_NOTE_PREFIX = "my-notes-save-url-to-note-";
const MY_NOTES_SAVE_URL_TO_REMOTE = "my-notes-save-url-to-remote";
const PAGE_NOTE_PREFIX = "page-note-";
const PAGE_REMOTE_MY_NOTES = "page-remote-my-notes";

const MY_NOTES_SAVE_SELECTION_TO_NOTE_PREFIX = "my-notes-save-selection-to-note-";
const MY_NOTES_SAVE_SELECTION_TO_REMOTE = "my-notes-save-selection-to-remote";
const SELECTION_NOTE_PREFIX = "selection-note-";
const SELECTION_REMOTE_MY_NOTES = "selection-remote-my-notes";

const isLocked = (notes: NotesObject, noteName: string): boolean => !!(notes[noteName]?.locked);

Expand All @@ -31,115 +31,141 @@ const createContextMenu = (notes: NotesObject): string | number => chrome.contex
title: "My Notes",
contexts: ["page", "selection"],
}, () => {
const noteCreateProperties = ({ context, prefix, noteName, tKey }: { context: chrome.contextMenus.ContextType, prefix: string, noteName: string, tKey: string }): chrome.contextMenus.CreateProperties => ({
const forEveryNoteExceptClipboard = (callback: (noteName: string) => void) => {
Object.keys(notes).filter(noteName => noteName !== CLIPBOARD_NOTE_NAME).sort().forEach(callback);
};

/* -------------< page >------------- */

const pageCommonProperties: chrome.contextMenus.CreateProperties = {
parentId: ID,
id: `${prefix}${noteName}`,
title: tString(tKey, { note: noteName }),
contexts: [context],
enabled: !isLocked(notes, noteName),
contexts: ["page"],
};

chrome.contextMenus.create({
...pageCommonProperties,
id: [PAGE_NOTE_PREFIX, CLIPBOARD_NOTE_NAME].join(""),
title: tString("Context Menu.menus.Save URL to", { note: CLIPBOARD_NOTE_NAME }),
enabled: !isLocked(notes, CLIPBOARD_NOTE_NAME),
});

const remoteCreateProperties = (context: chrome.contextMenus.ContextType, id: string, tKey: string): chrome.contextMenus.CreateProperties => ({
parentId: ID,
id,
title: tString(tKey),
contexts: [context],
chrome.contextMenus.create({
...pageCommonProperties,
type: "separator",
id: "page-separator-one",
});

const separatorCreateProperties = (context: chrome.contextMenus.ContextType, id: string): chrome.contextMenus.CreateProperties => ({
parentId: ID,
id: `my-notes-${context}-separator-${id}`,
forEveryNoteExceptClipboard((noteName) => {
chrome.contextMenus.create({
...pageCommonProperties,
id: [PAGE_NOTE_PREFIX, noteName].join(""),
title: tString("Context Menu.menus.Save URL to", { note: noteName }),
enabled: !isLocked(notes, noteName),
});
});

chrome.contextMenus.create({
...pageCommonProperties,
type: "separator",
contexts: [context],
id: "page-separator-two",
});

const forEveryNote = (callback: (noteName: string) => void) => Object.keys(notes).sort().forEach(callback);
chrome.contextMenus.create({
...pageCommonProperties,
id: PAGE_REMOTE_MY_NOTES,
title: tString("Context Menu.menus.Save URL to remotely open My Notes"),
});

/* ------------- page ------------- */
const pageContext: chrome.contextMenus.ContextType = "page";
const saveUrlToNoteCreateProperties = (noteName: string): chrome.contextMenus.CreateProperties =>
noteCreateProperties({
context: pageContext,
prefix: MY_NOTES_SAVE_URL_TO_NOTE_PREFIX,
noteName,
tKey: "Context Menu.menus.Save URL to",
});
chrome.contextMenus.create(saveUrlToNoteCreateProperties(CLIPBOARD_NOTE_NAME));
chrome.contextMenus.create(separatorCreateProperties(pageContext, "one"));
forEveryNote((noteName) => {
chrome.contextMenus.create(saveUrlToNoteCreateProperties(noteName));
/* -------------< selection >------------- */

const selectionCommonProperties: chrome.contextMenus.CreateProperties = {
parentId: ID,
contexts: ["selection"],
};

chrome.contextMenus.create({
...selectionCommonProperties,
id: [SELECTION_NOTE_PREFIX, CLIPBOARD_NOTE_NAME].join(""),
title: tString("Context Menu.menus.Save to", { note: CLIPBOARD_NOTE_NAME }),
enabled: !isLocked(notes, CLIPBOARD_NOTE_NAME),
});
chrome.contextMenus.create(separatorCreateProperties(pageContext, "two"));
chrome.contextMenus.create(remoteCreateProperties(
pageContext,
MY_NOTES_SAVE_URL_TO_REMOTE,
"Context Menu.menus.Save URL to remotely open My Notes",
));

/* ------------- selection ------------- */
const selectionContext: chrome.contextMenus.ContextType = "selection";
const saveSelectionToNoteCreateProperties = (noteName: string): chrome.contextMenus.CreateProperties =>
noteCreateProperties({
context: selectionContext,
prefix: MY_NOTES_SAVE_SELECTION_TO_NOTE_PREFIX,
noteName,
tKey: "Context Menu.menus.Save to",

chrome.contextMenus.create({
...selectionCommonProperties,
type: "separator",
id: "selection-separator-one",
});

forEveryNoteExceptClipboard((noteName) => {
chrome.contextMenus.create({
...selectionCommonProperties,
id: [SELECTION_NOTE_PREFIX, noteName].join(""),
title: tString("Context Menu.menus.Save to", { note: noteName }),
enabled: !isLocked(notes, noteName),
});
});

chrome.contextMenus.create({
...selectionCommonProperties,
type: "separator",
id: "selection-separator-two",
});

chrome.contextMenus.create(saveSelectionToNoteCreateProperties(CLIPBOARD_NOTE_NAME));
chrome.contextMenus.create(separatorCreateProperties(selectionContext, "one"));
forEveryNote((noteName) => {
chrome.contextMenus.create(saveSelectionToNoteCreateProperties(noteName));
chrome.contextMenus.create({
...selectionCommonProperties,
id: SELECTION_REMOTE_MY_NOTES,
title: tString("Context Menu.menus.Save to remotely open My Notes"),
});
chrome.contextMenus.create(separatorCreateProperties(selectionContext, "two"));
chrome.contextMenus.create(remoteCreateProperties(
selectionContext,
MY_NOTES_SAVE_SELECTION_TO_REMOTE,
"Context Menu.menus.Save to remotely open My Notes",
));
});

let currentNotesString: string;

export const attachContextMenuOnClicked = (): void => chrome.contextMenus.onClicked.addListener((info) => {
const menuId: string = info.menuItemId.toString();
const context = menuId.split("-")[0] as chrome.contextMenus.ContextType;
const textToSave = getTextToSave(context, info);
if (!textToSave) {
return;
}

if (menuId.startsWith(MY_NOTES_SAVE_URL_TO_NOTE_PREFIX)) {
const destinationNoteName = menuId.replace(MY_NOTES_SAVE_URL_TO_NOTE_PREFIX, "");
const urlToSave = getUrlToSave(info);
saveTextToLocalMyNotes(urlToSave, destinationNoteName);
/* -------------< page >------------- */

notify(tString("Context Menu.notifications.Saved URL to", { note: destinationNoteName }));
if (menuId.startsWith(PAGE_NOTE_PREFIX)) {
const noteName = menuId.replace(PAGE_NOTE_PREFIX, "");
saveTextToLocalMyNotes(textToSave, noteName);
notify(tString("Context Menu.notifications.Saved URL to", { note: noteName }));
return;
}

if (menuId === MY_NOTES_SAVE_URL_TO_REMOTE) {
const urlToSave = getUrlToSave(info);
saveTextToRemotelyOpenMyNotes(urlToSave);

if (menuId === PAGE_REMOTE_MY_NOTES) {
saveTextToRemotelyOpenMyNotes(textToSave);
notify(tString("Context Menu.notifications.Sent URL to remotely open My Notes"));
return;
}

if (menuId.startsWith(MY_NOTES_SAVE_SELECTION_TO_NOTE_PREFIX)) {
const destinationNoteName = menuId.replace(MY_NOTES_SAVE_SELECTION_TO_NOTE_PREFIX, "");
const selectionToSave = getSelectionToSave(info);
saveTextToLocalMyNotes(selectionToSave, destinationNoteName);
/* -------------< selection >------------- */

notify(tString("Context Menu.notifications.Saved text to", { note: destinationNoteName }));
if (menuId.startsWith(SELECTION_NOTE_PREFIX)) {
const noteName = menuId.replace(SELECTION_NOTE_PREFIX, "");
saveTextToLocalMyNotes(textToSave, noteName);
notify(tString("Context Menu.notifications.Saved text to", { note: noteName }));
return;
}

if (menuId === MY_NOTES_SAVE_SELECTION_TO_REMOTE) {
const selectionToSave = getSelectionToSave(info);
saveTextToRemotelyOpenMyNotes(selectionToSave);

if (menuId === SELECTION_REMOTE_MY_NOTES) {
saveTextToRemotelyOpenMyNotes(textToSave);
notify(tString("Context Menu.notifications.Sent text to remotely open My Notes"));
return;
}
});

const recreateContextMenuFromNotes = (notes: NotesObject): void => {
const recreateContextMenuFromNotes = (notes: NotesObject | undefined): void => {
if (!notes) {
currentNotesString = "";
chrome.contextMenus.removeAll();
return;
}

const notesString = JSON.stringify(Object.keys(notes).map((noteName) => `${noteName}_${notes[noteName].locked}`));

// re-create context menu only when note names have changed or their "locked" property
Expand All @@ -161,7 +187,7 @@ export const createAndUpdateContextMenuFromNotes = (): void => {

chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === "local" && changes["notes"]) {
recreateContextMenuFromNotes(changes.notes.newValue as NotesObject);
recreateContextMenuFromNotes(changes.notes.newValue);
}
});
};

0 comments on commit 417d9dd

Please sign in to comment.