Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: support internal link titles for all available entities Signed-off-by: 01zulfi <[email protected]>

01zulfi543766d5642d
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 2ba994bd8..291f95441 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -428,11 +428,46 @@ function TipTap(props: TipTapProps) {
if (link) handleInternalLink(url, openInNewTab);
else window.open(url, "_blank");
},
- getLinkTitle: async (url) => {
+ getLinkData: async (url) => {
const link = parseInternalLink(url);
- if (link && link.type === "note") {
- const note = await db.notes.note(link.id);
- return note?.title;
+ if (!link) return;
+
+ switch (link.type) {
+ case "note":
+ case "notebook":
+ case "tag": {
+ const table =
+ link.type === "note"
+ ? "notes"
+ : link.type === "notebook"
+ ? "notebooks"
+ : "tags";
+ const item = await db
+ .sql()
+ .selectFrom(table)
+ .where("id", "=", link.id)
+ .select("title")
+ .executeTakeFirst();
+ return {
+ type: link.type,
+ title: item?.title || ""
+ };
+ }
+ case "color": {
+ const color = await db
+ .sql()
+ .selectFrom("colors")
+ .where("id", "=", link.id)
+ .select(["title", "colorCode"])
+ .executeTakeFirst();
+ return {
+ type: "color",
+ title: color?.title || "",
+ metadata: {
+ colorCode: color?.colorCode || ""
+ }
+ };
+ }
}
}
};
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 4a046c3ca..b75cf9531 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -88,13 +88,14 @@ import { strings } from "@notesnook/intl";
import { InlineCode } from "./extensions/inline-code/inline-code.js";
import { FontLigature } from "./extensions/font-ligature/font-ligature.js";
import { SearchResult } from "./extensions/search-result/search-result.js";
+import { LinkData } from "./types.js";
interface TiptapStorage {
dateFormat?: DateTimeOptions["dateFormat"];
timeFormat?: DateTimeOptions["timeFormat"];
dayFormat?: DateTimeOptions["dayFormat"];
openLink?: (url: string, openInNewTab?: boolean) => void;
- getLinkTitle?: (url: string) => Promise<string | undefined>;
+ getLinkData?: (url: string) => Promise<LinkData | undefined>;
downloadAttachment?: (attachment: Attachment) => void;
openAttachmentPicker?: (type: AttachmentType) => void;
previewAttachment?: (attachment: Attachment) => void;
@@ -149,7 +150,7 @@ const useTiptap = (
openAttachmentPicker,
previewAttachment,
openLink,
- getLinkTitle,
+ getLinkData,
onBeforeCreate,
dateFormat,
timeFormat,
@@ -397,7 +398,7 @@ const useTiptap = (
editor.storage.createInternalLink = createInternalLink;
editor.storage.getAttachmentData = getAttachmentData;
editor.storage.downloadCsvTable = downloadCsvTable;
- editor.storage.getLinkTitle = getLinkTitle;
+ editor.storage.getLinkData = getLinkData;
if (onBeforeCreate) onBeforeCreate({ editor });
},
diff --git a/packages/editor/src/toolbar/tools/link.tsx b/packages/editor/src/toolbar/tools/link.tsx
index add82b175..873efd82e 100644
--- a/packages/editor/src/toolbar/tools/link.tsx
+++ b/packages/editor/src/toolbar/tools/link.tsx
@@ -33,6 +33,10 @@ import { getMarkAttributes } from "@tiptap/core";
import { useHoverPopupContext } from "../floating-menus/hover-popup/context.js";
import { strings } from "@notesnook/intl";
import { find } from "linkifyjs";
+import { Icons } from "../icons.js";
+import { mdiNoteOutline, mdiBookOutline, mdiPound } from "@mdi/js";
+import { Icon } from "@notesnook/ui";
+import { LinkData } from "../../types.js";
export function LinkSettings(props: ToolProps) {
const { editor } = props;
@@ -205,21 +209,26 @@ export function OpenLink(props: ToolProps) {
const { node } = selectedNode.current || {};
const link = node ? findMark(node, "link") : null;
const href = link?.attrs.href ?? null;
- const [title, setTitle] = useState(href ?? "");
+ const [linkData, setLinkData] = useState<LinkData | undefined>(undefined);
useEffect(() => {
if (!href) return;
(async () => {
- const result = await editor.storage.getLinkTitle?.(href);
- setTitle(result || href);
+ const result = await editor.storage.getLinkData?.(href);
+ setLinkData(result);
})();
}, [href]);
if (!link || !href) return null;
+ const title = linkData?.title || href;
+
return (
<Flex sx={{ alignItems: "center" }}>
+ {linkData?.type && (
+ <LinkTypeIcon type={linkData.type} metadata={linkData.metadata} />
+ )}
<Link
href={href}
onClick={(e) => {
@@ -232,7 +241,7 @@ export function OpenLink(props: ToolProps) {
sx={{
fontSize: "subBody",
fontFamily: "body",
- mr: 1,
+ mr: 4,
color: "accent",
maxWidth: [150, 250],
overflow: "hidden",
@@ -345,6 +354,31 @@ function LinkTool(props: LinkToolProps) {
);
}
+const LINK_TYPE_ICONS: Record<LinkData["type"], string> = {
+ note: mdiNoteOutline,
+ notebook: mdiBookOutline,
+ tag: mdiPound,
+ color: Icons.circle
+};
+
+function LinkTypeIcon({
+ type,
+ metadata
+}: {
+ type: LinkData["type"];
+ metadata?: LinkData["metadata"];
+}) {
+ const path = LINK_TYPE_ICONS[type];
+ if (!path) return null;
+
+ const color =
+ type === "color" && metadata?.colorCode
+ ? metadata.colorCode
+ : "icon-secondary";
+
+ return <Icon path={path} color={color} size={13} sx={{ mr: 1 }} />;
+}
+
export function isInternalLink(href?: string | null) {
return typeof href === "string" ? href.startsWith("nn://") : false;
}
diff --git a/packages/editor/src/types.ts b/packages/editor/src/types.ts
index a906b2144..9df0313fc 100644
--- a/packages/editor/src/types.ts
+++ b/packages/editor/src/types.ts
@@ -25,6 +25,12 @@ export type PermissionRequestEvent = CustomEvent<{
silent: boolean;
}>;
+export type LinkData = {
+ type: "note" | "notebook" | "color" | "tag";
+ title?: string;
+ metadata?: Record<string, string | undefined>;
+};
+
export class Editor extends TiptapEditor {
private mutex: Mutex = new Mutex();