Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/include notes: add a button to quickly open in a new tab

Adorian Doran390dd2586e9f
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index abf1db5f74..7fe2be0eb9 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1581,6 +1581,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t
}
.include-note-toggle,
+.include-note-open,
.include-note-fullscreen {
background: none;
border: none;
@@ -1596,6 +1597,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t
}
.include-note-toggle:hover,
+.include-note-open:hover,
.include-note-fullscreen:hover {
color: var(--main-link-color);
}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 50076e7b86..f2c9a405f6 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4218,7 +4218,8 @@
"save": "Save",
"cancel": "Cancel",
"fullscreen": "Fullscreen",
- "exit_fullscreen": "Exit fullscreen"
+ "exit_fullscreen": "Exit fullscreen",
+ "open_in_new_tab": "Open in new tab"
},
"spreadsheet": {
"read-only": "This note is read-only.",
diff --git a/apps/client/src/widgets/type_widgets/text/utils.spec.ts b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
index 51f34709f6..044d7c6048 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
@@ -23,6 +23,10 @@ vi.mock("../../../services/content_renderer", async () => {
};
});
vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
+const openTabWithNoteWithHoisting = vi.hoisted(() => vi.fn());
+vi.mock("../../../components/app_context", () => ({
+ default: { tabManager: { openTabWithNoteWithHoisting } }
+}));
import content_renderer from "../../../services/content_renderer";
import froca from "../../../services/froca";
@@ -47,7 +51,8 @@ describe("loadIncludedNote", () => {
const wrappers = $el.find(".include-note-wrapper");
expect(wrappers.length).toBe(0);
- expect($el.children(".include-note-title").length).toBe(1);
+ expect($el.children(".include-note-title-row").children(".include-note-title").length)
+ .toBe(1);
expect($el.children(".include-note-content").length).toBe(1);
});
@@ -59,7 +64,8 @@ describe("loadIncludedNote", () => {
const wrappers = $el.find(".include-note-wrapper");
expect(wrappers.length).toBe(1);
- expect(wrappers.children(".include-note-title").length).toBe(1);
+ expect(wrappers.children(".include-note-title-row").children(".include-note-title").length)
+ .toBe(1);
expect(wrappers.children(".include-note-content").length).toBe(1);
});
@@ -137,8 +143,11 @@ describe("loadIncludedAttachment", () => {
const $row = $el.find(".include-note-title-row");
const button = $row.children("button.include-note-fullscreen.bx-fullscreen")[0];
- expect($row.children().map((_, child) => child.className).get())
- .toEqual([ "include-note-title", "include-note-fullscreen bx bx-fullscreen" ]);
+ expect($row.children().map((_, child) => child.className).get()).toEqual([
+ "include-note-title",
+ "include-note-open bx bx-link-external",
+ "include-note-fullscreen bx bx-fullscreen"
+ ]);
expect(button.title).toBe("common.fullscreen");
const content = $el.find(".include-note-content")[0];
@@ -214,6 +223,52 @@ describe("loadIncludedAttachment", () => {
});
});
+describe("the button opening an include in a new tab", () => {
+ beforeEach(() => {
+ vi.mocked(froca.getNote).mockResolvedValue(note);
+ vi.mocked(froca.getAttachment).mockResolvedValue(
+ { attachmentId: "att1", ownerId: "owner" } as unknown as FAttachment
+ );
+ vi.mocked(link.createLink)
+ .mockImplementation(async () => $('<span><a href="#">x</a></span>'));
+ vi.mocked(content_renderer.getRenderedContent).mockImplementation(async () => (
+ { $renderedContent: $("<p>body</p>"), type: "text" } as never
+ ));
+ });
+
+ it("follows the title of a note or attachment at any size, and opens it", async () => {
+ const attachmentScope = { viewMode: "attachments", attachmentId: "att1" };
+
+ for (const boxSize of [ "small", "medium", "full", "expandable" ]) {
+ const $note = $('<div class="include-note-wrapper">');
+ const $attachment = $('<div class="include-note-wrapper">');
+ await loadIncludedNote("noteY", $note, boxSize);
+ await loadIncludedAttachment("att1", $attachment, boxSize);
+
+ const boxes: [ JQuery<HTMLElement>, string, object | undefined ][] = [
+ [ $note, "noteY", undefined ],
+ [ $attachment, "owner", attachmentScope ]
+ ];
+ for (const [ $el, notePath, viewScope ] of boxes) {
+ const $button = $el.find(".include-note-title").next();
+ expect($button.attr("class")).toBe("include-note-open bx bx-link-external");
+ expect($button.attr("title")).toBe("common.open_in_new_tab");
+
+ const click = new MouseEvent("click", { bubbles: true });
+ const stopPropagation = vi.spyOn(click, "stopPropagation");
+ $button[0].dispatchEvent(click);
+ expect(openTabWithNoteWithHoisting).toHaveBeenLastCalledWith(notePath, {
+ viewScope,
+ activate: true,
+ placement: "afterCurrent"
+ });
+ expect(stopPropagation).toHaveBeenCalled();
+ }
+ }
+ expect(openTabWithNoteWithHoisting).toHaveBeenCalledTimes(8);
+ });
+});
+
describe("getAttachmentHref", () => {
it("links to the attachment in its note, or to nothing once it is deleted", async () => {
vi.mocked(froca.getAttachment)
diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.ts
index e146614274..c8968997db 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.ts
@@ -23,7 +23,7 @@ export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>,
interactive: true,
includesAsReferenceLinks: true,
mediaEnvironment: "embedded"
- }));
+ }), { notePath: note.noteId });
}
/** Fills an include box with an embedded attachment, under a title linking to it. */
@@ -35,16 +35,17 @@ export async function loadIncludedAttachment(
const attachment = await froca.getAttachment(attachmentId, true);
if (!attachment) return;
+ const viewScope: ViewScope = { viewMode: "attachments", attachmentId };
const $link = await link.createLink(attachment.ownerId, {
showTooltip: false,
showNoteIcon: true,
- viewScope: { viewMode: "attachments", attachmentId }
+ viewScope
});
await fillIncludeBox($el, boxSize, $link, () => content_renderer.getRenderedContent(
attachment,
{ interactive: true, mediaEnvironment: "embedded" }
- ), { offersFullscreen: true });
+ ), { notePath: attachment.ownerId, viewScope, offersFullscreen: true });
}
/** The href of a reference link to the attachment, or `null` once it is deleted. */
@@ -58,6 +59,10 @@ export async function getAttachmentHref(attachmentId: string) {
type RenderedContent = Awaited<ReturnType<typeof content_renderer.getRenderedContent>>;
interface IncludeBoxOptions {
+ /** The note that the box's button opens in a new tab. */
+ notePath: string;
+ /** The view of that note to open, such as one of its attachments. */
+ viewScope?: ViewScope;
/** Whether a medium or full box ends its title row with a fullscreen button. */
offersFullscreen?: boolean;
}
@@ -67,7 +72,7 @@ async function fillIncludeBox(
boxSize: string | undefined,
$link: JQuery<HTMLElement>,
renderContent: () => Promise<RenderedContent>,
- { offersFullscreen = false }: IncludeBoxOptions = {}
+ { notePath, viewScope, offersFullscreen = false }: IncludeBoxOptions
) {
// The box size is supplied explicitly by the editing-view downcast; for the other
// callers (read-only rendering, script API refresh) fall back to reading it from the DOM.
@@ -88,7 +93,7 @@ async function fillIncludeBox(
const $toggle = $('<button class="include-note-toggle bx bx-chevron-right" aria-expanded="false">');
const $title = $('<h4 class="include-note-title">').append($link);
- $titleRow.append($toggle, $title);
+ $titleRow.append($toggle, $title, createOpenInNewTabButton(notePath, viewScope));
$wrapper.append($titleRow);
const { $renderedContent, type } = await renderContent();
@@ -108,15 +113,15 @@ async function fillIncludeBox(
const { $renderedContent, type } = await renderContent();
const $content = $(`<div class="include-note-content type-${type}">`)
.append($renderedContent);
+ const $titleRow = $('<div class="include-note-title-row">')
+ .append($title, createOpenInNewTabButton(notePath, viewScope));
if (offersFullscreen && (effectiveBoxSize === "medium" || effectiveBoxSize === "full")) {
- const $titleRow = $('<div class="include-note-title-row">')
- .append($title, createFullscreenButton($content[0]));
+ $titleRow.append(createFullscreenButton($content[0]));
await mountExitFullscreenControls($content);
- $wrapper.append($titleRow, $content);
- } else {
- $wrapper.append($title, $content);
}
+
+ $wrapper.append($titleRow, $content);
}
// Unmount any interactive widgets from a previous render of this include (e.g. on a box-size
@@ -126,6 +131,20 @@ async function fillIncludeBox(
$el.empty().append(isWrapper ? $wrapper.children() : $wrapper);
}
+/** A button opening a note in a new tab after the current one and switching to it. */
+function createOpenInNewTabButton(notePath: string, viewScope: ViewScope | undefined) {
+ return $('<button type="button" class="include-note-open bx bx-link-external">')
+ .attr({ title: t("common.open_in_new_tab") })
+ .on("click", (e) => {
+ e.stopPropagation();
+ void appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
+ viewScope,
+ activate: true,
+ placement: "afterCurrent"
+ });
+ });
+}
+
/** A button giving `content` the whole screen. */
function createFullscreenButton(content: HTMLElement) {
return $('<button type="button" class="include-note-fullscreen bx bx-fullscreen">')
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html
index 03d44612ff..b1fb3debc5 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html
@@ -77,6 +77,8 @@
at the end of the menu.</li>
<li>An embed takes the default box size of included notes. To change its size,
select the embed and use the box size menu in its toolbar.</li>
+ <li>To open the attachment in a new tab, press the <span class="tn-icon bx bx-link-external"></span> <em>Open in new tab</em> button
+ at the end of the embed's title.</li>
<li>To show an embed of size <em>Medium</em> or <em>Full</em> on the whole screen,
press the <span class="tn-icon bx bx-fullscreen"></span> <em>Fullscreen</em> button
at the end of its title. To return, press <span class="tn-icon bx bx-exit"></span> <em>Exit fullscreen</em> in
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html
index 932e49899a..dab7c5051f 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html
@@ -9,6 +9,9 @@
by default.</p>
<p>An <a href="#root/_help_0vhv7lsOLy82">attachment</a> can be included the
same way, from the context menu of a link to it (see <em>Embedding an attachment</em> there).</p>
+<h2>Opening an included note</h2>
+<p>To open an included note in a new tab, press the <span class="tn-icon bx bx-link-external"></span> <em>Open in new tab</em> button
+ at the end of its title. An embedded attachment has the same button.</p>
<h2>Included notes in the share functionality</h2>
<p>If a <a href="#root/_help_R9pX4DGra2Vt">shared note</a> contains one or
more included notes, they will be displayed in the content of the note
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md
index 9d61eabb0d..7e7330cb1f 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md
@@ -42,6 +42,7 @@ An attachment can be shown in the text instead of linked, the way <a class="ref
* To attach files and embed them straight away, select _Attach and embed file_ from the arrow beside the <span class="tn-icon cke cke-paper-clip"></span> _Attach file_ button. Each file gets an embed of its own, which shows the file name while the file uploads.
* To embed an attachment that is already linked, right-click a link to it in a text note being edited and select _Convert link to an embed_, at the end of the menu.
* An embed takes the default box size of included notes. To change its size, select the embed and use the box size menu in its toolbar.
+* To open the attachment in a new tab, press the <span class="tn-icon bx bx-link-external"></span> _Open in new tab_ button at the end of the embed's title.
* To show an embed of size _Medium_ or _Full_ on the whole screen, press the <span class="tn-icon bx bx-fullscreen"></span> _Fullscreen_ button at the end of its title. To return, press <span class="tn-icon bx bx-exit"></span> _Exit fullscreen_ in the top-right corner, or <kbd>Esc</kbd>.
* To turn the embed back into a link, select it and press the <span class="tn-icon cke cke-link"></span> _Convert to link_ button in its toolbar, or right-click its title and select _Convert to link_, at the end of the menu.
diff --git a/docs/User Guide/User Guide/Note Types/Text/Include Note.md b/docs/User Guide/User Guide/Note Types/Text/Include Note.md
index 29a6d15f62..25aa198665 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Include Note.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Include Note.md
@@ -9,6 +9,10 @@ In the <a class="reference-link" href="Formatting%20toolbar.md">Formatting tool
An [attachment](../../Basic%20Concepts%20and%20Features/Notes/Attachments.md) can be included the same way, from the context menu of a link to it (see _Embedding an attachment_ there).
+## Opening an included note
+
+To open an included note in a new tab, press the <span class="tn-icon bx bx-link-external"></span> _Open in new tab_ button at the end of its title. An embedded attachment has the same button.
+
## Included notes in the share functionality
If a [shared note](../../Advanced%20Usage/Sharing.md) contains one or more included notes, they will be displayed in the content of the note as if they were part of the note itself.