chore: Remove unused link decoration logic

This commit is contained in:
Tom Moor
2024-03-09 00:17:48 -05:00
parent 34e8a64b50
commit 128f6cca77
2 changed files with 2 additions and 71 deletions

View File

@@ -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: {

View File

@@ -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;
}