chore: Remove unused link decoration logic
This commit is contained in:
@@ -10,12 +10,11 @@ import {
|
|||||||
Mark as ProsemirrorMark,
|
Mark as ProsemirrorMark,
|
||||||
} from "prosemirror-model";
|
} from "prosemirror-model";
|
||||||
import { Command, EditorState, Plugin, TextSelection } from "prosemirror-state";
|
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 * as React from "react";
|
||||||
import ReactDOM from "react-dom";
|
import ReactDOM from "react-dom";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { isExternalUrl, sanitizeUrl } from "../../utils/urls";
|
import { sanitizeUrl } from "../../utils/urls";
|
||||||
import findLinkNodes from "../queries/findLinkNodes";
|
|
||||||
import getMarkRange from "../queries/getMarkRange";
|
import getMarkRange from "../queries/getMarkRange";
|
||||||
import isMarkActive from "../queries/isMarkActive";
|
import isMarkActive from "../queries/isMarkActive";
|
||||||
import { EventType } from "../types";
|
import { EventType } from "../types";
|
||||||
@@ -150,50 +149,6 @@ export default class Link extends Mark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get plugins() {
|
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 handleClick = (view: EditorView, pos: number) => {
|
||||||
const { doc, tr } = view.state;
|
const { doc, tr } = view.state;
|
||||||
const range = getMarkRange(
|
const range = getMarkRange(
|
||||||
@@ -219,11 +174,6 @@ export default class Link extends Mark {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const plugin: Plugin = new Plugin({
|
const plugin: Plugin = new Plugin({
|
||||||
state: {
|
|
||||||
init: (_config, state) => getLinkDecorations(state),
|
|
||||||
apply: (tr, decorationSet, _oldState, newState) =>
|
|
||||||
tr.docChanged ? getLinkDecorations(newState) : decorationSet,
|
|
||||||
},
|
|
||||||
props: {
|
props: {
|
||||||
decorations: (state: EditorState) => plugin.getState(state),
|
decorations: (state: EditorState) => plugin.getState(state),
|
||||||
handleDOMEvents: {
|
handleDOMEvents: {
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user