⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
finished highlight feature implementation
daniel31x130e5e3ea00ea4
diff --git a/components/Preservation/ReadableView.tsx b/components/Preservation/ReadableView.tsxindex 435ac268..c681f7ec 100644--- a/components/Preservation/ReadableView.tsx+++ b/components/Preservation/ReadableView.tsx@@ -101,301 +101,137 @@ export default function ReadableView({ link }: Props) {}};- function getTextNodesIn(root: Node): Text[] {- const textNodes: Text[] = [];- for (const child of Array.from(root.childNodes)) {- if (child.nodeType === Node.TEXT_NODE) {- textNodes.push(child as Text);- } else {- textNodes.push(...getTextNodesIn(child));- }- }- return textNodes;- }+ function getHighlightedSection(color: string) {+ const selection = window.getSelection?.();- function getNodeName(node: Node): string {- return node.nodeType === Node.TEXT_NODE- ? "text()"- : node.nodeName.toLowerCase();- }+ if (!selection || selection.isCollapsed) return null;- function getNodePositionAmongSameType(node: Node): number {- const nodeName = getNodeName(node);- let index = 1;- let prev = node.previousSibling;- while (prev) {- if (getNodeName(prev) === nodeName) {- index++;- }- prev = prev.previousSibling;- }- return index;- }+ const range = selection.getRangeAt(0);+ if (!range) return null;- function getXPathForNode(node: Node, root: Node): string {- if (node === root) {- return "";- }- const segments: string[] = [];- let currentNode: Node | null = node;-- while (currentNode && currentNode !== root) {- const nodeName = getNodeName(currentNode);- const index = getNodePositionAmongSameType(currentNode);- segments.push(`${nodeName}[${index}]`);- currentNode = currentNode.parentNode;+ const container = document.getElementById("readable-view");+ if (!container || !container.contains(range.commonAncestorContainer)) {+ return null;}- segments.reverse();- return "/" + segments.join("/");- }-- function resolveNodeFromXPath(root: Node, xPath: string): Node | null {- if (!xPath || xPath === "/") return root;- let path = xPath;- if (path.startsWith("/")) {- path = path.slice(1);- }+ let startOffset = -1;+ let endOffset = -1;+ let currentOffset = 0;- let currentNode: Node | null = root;- const segments = path.split("/");+ const treeWalker = document.createTreeWalker(+ container,+ NodeFilter.SHOW_TEXT+ );+ while (treeWalker.nextNode()) {+ const node = treeWalker.currentNode;+ const nodeLength = node.textContent?.length ?? 0;- for (const segment of segments) {- const match = segment.match(/^([^\[]+)\[(\d+)\]$/);- if (!match) {- return null;+ if (node === range.startContainer) {+ startOffset = currentOffset + range.startOffset;}- const [_, nodeName, indexStr] = match;- const index = parseInt(indexStr, 10);- currentNode = findChildByNameAndIndex(currentNode, nodeName, index);- if (!currentNode) return null;- }-- return currentNode;- }-- function findChildByNameAndIndex(- parent: Node,- nodeName: string,- index: number- ): Node | null {- let count = 0;- for (const child of Array.from(parent.childNodes)) {- if (getNodeName(child) === nodeName) {- count++;- if (count === index) return child;+ if (node === range.endContainer) {+ endOffset = currentOffset + range.endOffset;+ break;}++ currentOffset += nodeLength;}- return null;- }- function getHighlightedSection(color: string) {- const selection = window.getSelection();- if (!selection || selection.isCollapsed || selection.rangeCount === 0)+ if (startOffset === -1 || endOffset === -1) {return null;-- const range = selection.getRangeAt(0);-- let startNode = range.startContainer;- let endNode = range.endContainer;- let startOffset = range.startOffset;- let endOffset = range.endOffset;-- if (startNode === endNode && startOffset > endOffset) {- [startOffset, endOffset] = [endOffset, startOffset];- } else if (startNode !== endNode) {- const selectionBackwards = range.collapsed;- if (selectionBackwards) {- [startNode, endNode] = [endNode, startNode];- [startOffset, endOffset] = [endOffset, startOffset];- } else {- const position = startNode.compareDocumentPosition(endNode);- if (position & Node.DOCUMENT_POSITION_PRECEDING) {- [startNode, endNode] = [endNode, startNode];- [startOffset, endOffset] = [endOffset, startOffset];- }- }}- const text = selection.toString();- if (!text.trim()) return null;-- const container = document.getElementById("readable-view");- if (!container) return null;-- const startXPath = getXPathForNode(startNode, container);- const endXPath = getXPathForNode(endNode, container);- if (!startXPath || !endXPath) return null;-return {- linkId: link?.id as number,+ linkId: link?.id,color,- text,- startXPath,+ text: range.toString(),startOffset,- endXPath,endOffset,};}function getHighlightedHtml(htmlContent: string,- highlights: Array<{- id: number;- color: string;- text: string;- startXPath: string;- startOffset: number;- endXPath: string;- endOffset: number;- }>- ) {- if (!htmlContent) return "";- if (!highlights || !highlights.length) return htmlContent;-- const parser = new DOMParser();- const doc = parser.parseFromString(htmlContent, "text/html");- const docRoot = doc.body || doc.documentElement;- const textNodes = getTextNodesIn(docRoot);- type IndexedHighlight = {- id: number;- color: string;- text: string;- startXPath: string;- startOffset: number;- endXPath: string;- endOffset: number;- startNodeIndex: number;- endNodeIndex: number;- earliestNodeIndex: number;- earliestOffset: number;- };-- const indexed: IndexedHighlight[] = highlights.map((hl) => {- const startNode = resolveNodeFromXPath(docRoot, hl.startXPath);- const endNode = resolveNodeFromXPath(docRoot, hl.endXPath);- let startIndex = startNode ? textNodes.indexOf(startNode as Text) : -1;- let endIndex = endNode ? textNodes.indexOf(endNode as Text) : -1;-- if (!startNode || startNode.nodeType !== Node.TEXT_NODE) {- startIndex = -1;- }- if (!endNode || endNode.nodeType !== Node.TEXT_NODE) {- endIndex = -1;- }-- let sIndex = startIndex;- let eIndex = endIndex;- let sOffset = hl.startOffset;- let eOffset = hl.endOffset;- if (sIndex > eIndex || (sIndex === eIndex && sOffset > eOffset)) {- [sIndex, eIndex] = [eIndex, sIndex];- [sOffset, eOffset] = [eOffset, sOffset];- }+ highlights: Highlight[]+ ): string {+ if (!htmlContent || !highlights || highlights.length === 0) {+ return htmlContent;+ }- const earliestNodeIndex = sIndex;- const earliestOffset = sOffset;-- return {- ...hl,- startNodeIndex: sIndex,- endNodeIndex: eIndex,- startOffset: sOffset,- endOffset: eOffset,- earliestNodeIndex,- earliestOffset,- };- });+ const container = document.createElement("div");+ container.innerHTML = htmlContent;- indexed.sort((a, b) => {- if (a.earliestNodeIndex !== b.earliestNodeIndex) {- return b.earliestNodeIndex - a.earliestNodeIndex;- }- return b.earliestOffset - a.earliestOffset;- });+ const sortedHighlights = [...highlights].sort(+ (a, b) => a.startOffset - b.startOffset+ );- for (const hl of indexed) {- applyRangeHighlight(docRoot, textNodes, hl);+ for (const highlight of sortedHighlights) {+ applyHighlight(container, highlight);}- return docRoot.innerHTML;+ return container.innerHTML;}- function applyRangeHighlight(- docRoot: HTMLElement,- textNodes: Text[],- hl: {- id: number;- color: string;- startNodeIndex: number;- startOffset: number;- endNodeIndex: number;- endOffset: number;- }- ) {- const { startNodeIndex, endNodeIndex } = hl;- if (startNodeIndex < 0 || endNodeIndex < 0) return;- if (startNodeIndex >= textNodes.length || endNodeIndex >= textNodes.length)- return;-- for (let i = startNodeIndex; i <= endNodeIndex; i++) {- const node = textNodes[i];- const nodeLength = node.nodeValue?.length ?? 0;- if (nodeLength === 0) continue;+ function applyHighlight(container: HTMLElement, highlight: Highlight) {+ let currentOffset = 0;+ const treeWalker = document.createTreeWalker(+ container,+ NodeFilter.SHOW_TEXT+ );++ const rangesToWrap: Array<{+ node: Text;+ start: number;+ end: number;+ }> = [];++ while (treeWalker.nextNode()) {+ const node = treeWalker.currentNode as Text;+ const nodeLength = node.textContent?.length ?? 0;+ const nodeStart = currentOffset;+ const nodeEnd = nodeStart + nodeLength;++ if (nodeStart < highlight.endOffset && nodeEnd > highlight.startOffset) {+ rangesToWrap.push({+ node,+ start: Math.max(0, highlight.startOffset - nodeStart),+ end: Math.min(nodeLength, highlight.endOffset - nodeStart),+ });+ }- let chunkStart = 0;- let chunkEnd = nodeLength;+ currentOffset += nodeLength;+ }- if (i === startNodeIndex) {- chunkStart = hl.startOffset;+ rangesToWrap.forEach(({ node, start, end }) => {+ if (start > 0) {+ node.splitText(start);+ node = node.nextSibling as Text;+ end -= start;}- if (i === endNodeIndex) {- chunkEnd = hl.endOffset;++ if (end < node.length) {+ node.splitText(end);}- if (chunkStart < 0) chunkStart = 0;- if (chunkEnd > nodeLength) chunkEnd = nodeLength;- if (chunkStart >= chunkEnd) continue;+ const highlightWrapper = document.createElement("span");+ highlightWrapper.dataset.highlightId = highlight.id.toString();- wrapTextRangeInNode(node, chunkStart, chunkEnd, hl);- }- }+ highlightWrapper.classList.add("cursor-pointer");- function wrapTextRangeInNode(- textNode: Text,- nodeStart: number,- nodeEnd: number,- hl: { id: number; color: string }- ) {- const range = document.createRange();- range.setStart(textNode, nodeStart);- range.setEnd(textNode, nodeEnd);-- const highlightSpan = document.createElement("span");- highlightSpan.setAttribute("data-highlight-id", hl.id.toString());- highlightSpan.classList.add("highlighted-span");-- switch (hl.color) {- case "yellow":- highlightSpan.classList.add("bg-yellow-500");- break;- case "red":- highlightSpan.classList.add("bg-red-500");- break;- case "blue":- highlightSpan.classList.add("bg-blue-500");- break;- case "green":- highlightSpan.classList.add("bg-green-500");- break;- default:- highlightSpan.classList.add("bg-gray-300");- break;- }- highlightSpan.classList.add("bg-opacity-80", "cursor-pointer");+ if (highlight.color === "yellow") {+ highlightWrapper.classList.add("bg-yellow-500/70");+ } else if (highlight.color === "red") {+ highlightWrapper.classList.add("bg-red-500/70");+ } else if (highlight.color === "blue") {+ highlightWrapper.classList.add("bg-blue-500/70");+ } else if (highlight.color === "green") {+ highlightWrapper.classList.add("bg-green-500/70");+ }- range.surroundContents(highlightSpan);+ node.parentNode?.insertBefore(highlightWrapper, node);+ highlightWrapper.appendChild(node);+ });}const highlightedHtml = React.useMemo(() => {diff --git a/lib/api/controllers/highlights/postOrUpdateHighlight.ts b/lib/api/controllers/highlights/postOrUpdateHighlight.tsindex 528c4184..cac4f442 100644--- a/lib/api/controllers/highlights/postOrUpdateHighlight.ts+++ b/lib/api/controllers/highlights/postOrUpdateHighlight.ts@@ -63,8 +63,6 @@ export default async function postOrUpdateHighlight(linkId: body.linkId,startOffset: body.startOffset,endOffset: body.endOffset,- startXPath: body.startXPath,- endXPath: body.endXPath,color: body.color,text: body.text,comment: body.comment,diff --git a/lib/shared/schemaValidation.ts b/lib/shared/schemaValidation.tsindex ac586b07..7aec786a 100644--- a/lib/shared/schemaValidation.ts+++ b/lib/shared/schemaValidation.ts@@ -246,8 +246,6 @@ export const PostHighlightSchema = z.object({comment: z.string().trim().max(2048).nullish(),startOffset: z.number(),endOffset: z.number(),- startXPath: z.string().trim().max(2048),- endXPath: z.string().trim().max(2048),text: z.string().trim().max(2048),linkId: z.number(),});diff --git a/prisma/migrations/20250404153610_remove_fields/migration.sql b/prisma/migrations/20250404153610_remove_fields/migration.sqlnew file mode 100644index 00000000..9af86b58--- /dev/null+++ b/prisma/migrations/20250404153610_remove_fields/migration.sql@@ -0,0 +1,10 @@+/*+ Warnings:++ - You are about to drop the column `endXPath` on the `Highlight` table. All the data in the column will be lost.+ - You are about to drop the column `startXPath` on the `Highlight` table. All the data in the column will be lost.++*/+-- AlterTable+ALTER TABLE "Highlight" DROP COLUMN "endXPath",+DROP COLUMN "startXPath";diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 7aef54e6..b7517e1e 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -248,8 +248,6 @@ model Highlight {userId IntstartOffset IntendOffset Int- startXPath String @default("")- endXPath String @default("")text StringcreatedAt DateTime @default(now())updatedAt DateTime @default(now()) @updatedAt