-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #395 from penge/save-image-to-note
Save image to note using context menu
- Loading branch information
Showing
12 changed files
with
231 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/background/init/context-menu/__tests__/get-text-to-save.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import getTextToSave from "../get-text-to-save"; | ||
|
||
const irrelevant: Pick<chrome.contextMenus.OnClickData, "menuItemId" | "editable"> = { | ||
menuItemId: "", | ||
editable: true, | ||
}; | ||
|
||
describe("getTextToSave()", () => { | ||
describe("page context", () => { | ||
it("returns url to save", () => { | ||
expect(getTextToSave("page", { | ||
...irrelevant, | ||
pageUrl: "https://github.com/penge/my-notes", | ||
})).toBe('<a href="https://github.com/penge/my-notes" target="_blank">https://github.com/penge/my-notes</a>' | ||
+ "<br><br>"); | ||
}); | ||
}); | ||
|
||
describe("image context", () => { | ||
it("returns img to save", () => { | ||
expect(getTextToSave("image", { | ||
...irrelevant, | ||
pageUrl: "https://domain.com", | ||
srcUrl: "https://domain.com/image.png", | ||
})).toBe('<img src="https://domain.com/image.png">' | ||
+ "<br><br>"); | ||
}); | ||
}); | ||
|
||
describe("selection context", () => { | ||
it("returns selection to save", () => { | ||
expect(getTextToSave("selection", { | ||
...irrelevant, | ||
pageUrl: "https://articles.com/good-article", | ||
selectionText: "Text selected from the article...", | ||
})).toBe("Text selected from the article...<br>" | ||
+ '<b>(<a href="https://articles.com/good-article" target="_blank">https://articles.com/good-article</a>)</b>' | ||
+ "<br><br>"); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
src/background/init/context-menu/__tests__/is-note-locked.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NotesObject, Note } from "shared/storage/schema"; | ||
import isNoteLocked from "../is-note-locked"; | ||
|
||
const dummyNote: Note = { | ||
content: "", | ||
createdTime: "", | ||
modifiedTime: "", | ||
}; | ||
|
||
const notes: NotesObject = { | ||
"@clipboard": { ...dummyNote, locked: false }, | ||
"@images": { ...dummyNote, locked: true }, | ||
Todo: { ...dummyNote, locked: undefined }, | ||
}; | ||
|
||
test("isNoteLocked()", () => { | ||
expect(isNoteLocked(notes, "@clipboard")).toBe(false); | ||
expect(isNoteLocked(notes, "@images")).toBe(true); | ||
expect(isNoteLocked(notes, "Todo")).toBe(false); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/background/init/context-menu/__tests__/split-id.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import splitId, { SplitIdResult } from "../split-id"; | ||
|
||
type T = SplitIdResult<chrome.contextMenus.ContextType, "note" | "remote">; | ||
|
||
test("splitId()", () => { | ||
expect(splitId("page-note-@clipboard")).toEqual<T>({ | ||
context: "page", | ||
destination: "note", | ||
noteName: "@clipboard", | ||
}); | ||
|
||
expect(splitId("page-note-Todo")).toEqual<T>({ | ||
context: "page", | ||
destination: "note", | ||
noteName: "Todo", | ||
}); | ||
|
||
expect(splitId("page-note-Tables and Chairs")).toEqual<T>({ | ||
context: "page", | ||
destination: "note", | ||
noteName: "Tables and Chairs", | ||
}); | ||
|
||
expect(splitId("page-note-npm-packages")).toEqual<T>({ | ||
context: "page", | ||
destination: "note", | ||
noteName: "npm-packages", | ||
}); | ||
|
||
expect(splitId("image-note-@images")).toEqual<T>({ | ||
context: "image", | ||
destination: "note", | ||
noteName: "@images", | ||
}); | ||
|
||
expect(splitId("selection-remote")).toEqual<T>({ | ||
context: "selection", | ||
destination: "remote", | ||
noteName: "", | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { NotesObject } from "shared/storage/schema"; | ||
|
||
export default (notes: NotesObject, noteName: string): boolean => !!(notes[noteName]?.locked); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type SplitIdResult<C extends unknown, D extends unknown> = { | ||
context: C | ||
destination: D | ||
noteName: string | ||
}; | ||
|
||
export default <C extends unknown, D extends unknown>(id: string): SplitIdResult<C, D> => { | ||
const [context, destination, ...noteNameParts] = id.split("-") as [C, D, string[]]; | ||
return { | ||
context, | ||
destination, | ||
noteName: noteNameParts.join("-"), | ||
}; | ||
}; |
Oops, something went wrong.