From 128f6cca776c5609689dba23e5bb9bba0ad6c313 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 9 Mar 2024 00:17:48 -0500 Subject: [PATCH] chore: Remove unused link decoration logic --- shared/editor/marks/Link.tsx | 54 +------------------------- shared/editor/queries/findLinkNodes.ts | 19 --------- 2 files changed, 2 insertions(+), 71 deletions(-) delete mode 100644 shared/editor/queries/findLinkNodes.ts diff --git a/shared/editor/marks/Link.tsx b/shared/editor/marks/Link.tsx index ba84e3a3b..6687096b1 100644 --- a/shared/editor/marks/Link.tsx +++ b/shared/editor/marks/Link.tsx @@ -10,12 +10,11 @@ import { Mark as ProsemirrorMark, } from "prosemirror-model"; import { Command, EditorState, Plugin, TextSelection } from "prosemirror-state"; -import { Decoration, DecorationSet, EditorView } from "prosemirror-view"; +import { EditorView } from "prosemirror-view"; import * as React from "react"; import ReactDOM from "react-dom"; import { toast } from "sonner"; -import { isExternalUrl, sanitizeUrl } from "../../utils/urls"; -import findLinkNodes from "../queries/findLinkNodes"; +import { sanitizeUrl } from "../../utils/urls"; import getMarkRange from "../queries/getMarkRange"; import isMarkActive from "../queries/isMarkActive"; import { EventType } from "../types"; @@ -150,50 +149,6 @@ export default class Link extends Mark { } get plugins() { - const getLinkDecorations = (state: EditorState) => { - const decorations: Decoration[] = []; - const links = findLinkNodes(state.doc); - - links.forEach((nodeWithPos) => { - const linkMark = nodeWithPos.node.marks.find( - (mark) => mark.type.name === "link" - ); - if (linkMark && isExternalUrl(linkMark.attrs.href)) { - decorations.push( - Decoration.widget( - // place the decoration at the end of the link - nodeWithPos.pos + nodeWithPos.node.nodeSize, - () => { - const cloned = icon.cloneNode(true); - cloned.addEventListener("click", (event) => { - try { - if (this.options.onClickLink) { - event.stopPropagation(); - event.preventDefault(); - this.options.onClickLink( - sanitizeUrl(linkMark.attrs.href), - event - ); - } - } catch (err) { - toast.error(this.options.dictionary.openLinkError); - } - }); - return cloned; - }, - { - // position on the right side of the position - side: 1, - key: "external-link", - } - ) - ); - } - }); - - return DecorationSet.create(state.doc, decorations); - }; - const handleClick = (view: EditorView, pos: number) => { const { doc, tr } = view.state; const range = getMarkRange( @@ -219,11 +174,6 @@ export default class Link extends Mark { }; const plugin: Plugin = new Plugin({ - state: { - init: (_config, state) => getLinkDecorations(state), - apply: (tr, decorationSet, _oldState, newState) => - tr.docChanged ? getLinkDecorations(newState) : decorationSet, - }, props: { decorations: (state: EditorState) => plugin.getState(state), handleDOMEvents: { diff --git a/shared/editor/queries/findLinkNodes.ts b/shared/editor/queries/findLinkNodes.ts deleted file mode 100644 index a1de32928..000000000 --- a/shared/editor/queries/findLinkNodes.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Node } from "prosemirror-model"; -import { findChildren, NodeWithPos } from "./findChildren"; - -export default function findLinkNodes(doc: Node): NodeWithPos[] { - const textNodes = findChildren(doc, (child) => child.isText); - const nodes: NodeWithPos[] = []; - - for (const nodeWithPos of textNodes) { - const hasLinkMark = nodeWithPos.node.marks.find( - (mark) => mark.type.name === "link" - ); - - if (hasLinkMark) { - nodes.push(nodeWithPos); - } - } - - return nodes; -}