diff --git a/app/editor/components/SelectionToolbar.tsx b/app/editor/components/SelectionToolbar.tsx
index cf1d573a9e..efeefd58ec 100644
--- a/app/editor/components/SelectionToolbar.tsx
+++ b/app/editor/components/SelectionToolbar.tsx
@@ -1,7 +1,10 @@
import { Selection, NodeSelection, TextSelection } from "prosemirror-state";
import * as React from "react";
import filterExcessSeparators from "@shared/editor/lib/filterExcessSeparators";
-import { getMarkRange } from "@shared/editor/queries/getMarkRange";
+import {
+ getMarkRange,
+ getMarkRangeNodeSelection,
+} from "@shared/editor/queries/getMarkRange";
import { isInCode } from "@shared/editor/queries/isInCode";
import { isInNotice } from "@shared/editor/queries/isInNotice";
import { isNodeActive } from "@shared/editor/queries/isNodeActive";
@@ -117,7 +120,6 @@ export function SelectionToolbar(props: Props) {
const isDividerSelection = isNodeActive(state.schema.nodes.hr)(state);
const colIndex = getColumnIndex(state);
const rowIndex = getRowIndex(state);
- const link = getMarkRange(selection.$from, state.schema.marks.link);
const isImageSelection =
selection instanceof NodeSelection && selection.node.type.name === "image";
const isAttachmentSelection =
@@ -127,6 +129,10 @@ export function SelectionToolbar(props: Props) {
selection instanceof NodeSelection && selection.node.type.name === "embed";
const isCodeSelection = isInCode(state, { onlyBlock: true });
const isNoticeSelection = isInNotice(state);
+ const link =
+ selection instanceof NodeSelection
+ ? getMarkRangeNodeSelection(selection, state.schema.marks.link)
+ : getMarkRange(selection.$from, state.schema.marks.link);
let items: MenuItem[] = [];
let align: "center" | "start" | "end" = "center";
diff --git a/app/editor/menus/image.tsx b/app/editor/menus/image.tsx
index f98dc2ea67..b365277a4a 100644
--- a/app/editor/menus/image.tsx
+++ b/app/editor/menus/image.tsx
@@ -7,6 +7,7 @@ import {
AlignImageCenterIcon,
AlignFullWidthIcon,
CommentIcon,
+ LinkIcon,
} from "outline-icons";
import { EditorState } from "prosemirror-state";
import { isNodeActive } from "@shared/editor/queries/isNodeActive";
@@ -102,6 +103,12 @@ export default function imageMenuItems(
{
name: "separator",
},
+ {
+ name: "linkOnImage",
+ tooltip: dictionary.createLink,
+ shortcut: `${metaDisplay}+K`,
+ icon: ,
+ },
{
name: "commentOnImage",
tooltip: dictionary.comment,
diff --git a/shared/editor/commands/link.ts b/shared/editor/commands/link.ts
new file mode 100644
index 0000000000..5c1b6b58ac
--- /dev/null
+++ b/shared/editor/commands/link.ts
@@ -0,0 +1,211 @@
+import { chainCommands, toggleMark } from "prosemirror-commands";
+import { Attrs } from "prosemirror-model";
+import {
+ Command,
+ NodeSelection,
+ Selection,
+ TextSelection,
+} from "prosemirror-state";
+import { getMarkRange } from "../queries/getMarkRange";
+import { toast } from "sonner";
+import { sanitizeUrl } from "@shared/utils/urls";
+import { addMark } from "./addMark";
+
+const addLinkTextSelection =
+ (attrs: Attrs): Command =>
+ (state, dispatch) => {
+ if (!(state.selection instanceof TextSelection)) {
+ return false;
+ }
+
+ return addMark(state.schema.marks.link, attrs)(state, dispatch);
+ };
+
+const addLinkNodeSelection =
+ (attrs: Attrs): Command =>
+ (state, dispatch) => {
+ if (!(state.selection instanceof NodeSelection)) {
+ return false;
+ }
+ const { selection } = state;
+ const existingMarks = selection.node.attrs.marks ?? [];
+ const newMark = {
+ type: "link",
+ attrs,
+ };
+ const updatedMarks = [...existingMarks, newMark];
+ dispatch?.(
+ state.tr.setNodeAttribute(selection.from, "marks", updatedMarks)
+ );
+ return true;
+ };
+
+const openLinkTextSelection =
+ (attrs: Attrs): Command =>
+ (state) => {
+ if (!(state.selection instanceof TextSelection)) {
+ return false;
+ }
+
+ const range = getMarkRange(state.selection.$from, state.schema.marks.link);
+ if (range && range.mark && attrs.onClickLink) {
+ try {
+ const event = new KeyboardEvent("keydown", { metaKey: false });
+ attrs.onClickLink(sanitizeUrl(range.mark.attrs.href), event);
+ } catch (_err) {
+ toast.error(attrs.dictionary.openLinkError);
+ }
+ return true;
+ }
+ return false;
+ };
+
+const openLinkNodeSelection =
+ (attrs: Attrs): Command =>
+ (state) => {
+ if (!(state.selection instanceof NodeSelection)) {
+ return false;
+ }
+
+ if (!attrs.onClickLink) {
+ return false;
+ }
+
+ const marks = state.selection.node.attrs.marks ?? [];
+ const linkMark = marks.find((mark: any) => mark.type === "link");
+ if (!linkMark) {
+ return false;
+ }
+
+ try {
+ const event = new KeyboardEvent("keydown", { metaKey: false });
+ attrs.onClickLink(sanitizeUrl(linkMark.attrs.href), event);
+ } catch (_err) {
+ toast.error(attrs.dictionary.openLinkError);
+ }
+ return true;
+ };
+
+const updateLinkTextSelection =
+ (attrs: Attrs): Command =>
+ (state, dispatch) => {
+ if (!(state.selection instanceof TextSelection)) {
+ return false;
+ }
+
+ const range = getMarkRange(state.selection.$from, state.schema.marks.link);
+
+ if (range && range.mark) {
+ const nextSelection =
+ Selection.findFrom(state.doc.resolve(range.to), 1, true) ??
+ TextSelection.create(state.tr.doc, 0);
+ dispatch?.(
+ state.tr
+ .setSelection(nextSelection)
+ .removeMark(range.from, range.to, state.schema.marks.link)
+ .addMark(range.from, range.to, state.schema.marks.link.create(attrs))
+ );
+ return true;
+ }
+ return false;
+ };
+
+const updateLinkNodeSelection =
+ (attrs: Attrs): Command =>
+ (state, dispatch) => {
+ if (!(state.selection instanceof NodeSelection)) {
+ return false;
+ }
+
+ const existingMarks = state.selection.node.attrs.marks ?? [];
+ const updatedMarks = existingMarks.map((mark: any) =>
+ mark.type === "link"
+ ? { ...mark, attrs: { ...mark.attrs, ...attrs } }
+ : mark
+ );
+ const updatedAttrs = {
+ ...state.selection.node.attrs,
+ marks: updatedMarks,
+ };
+ dispatch?.(
+ state.tr.setNodeMarkup(state.selection.from, undefined, updatedAttrs)
+ );
+ return true;
+ };
+
+const removeLinkTextSelection = (): Command => (state, dispatch) => {
+ if (!(state.selection instanceof TextSelection)) {
+ return false;
+ }
+ const range = getMarkRange(state.selection.$from, state.schema.marks.link);
+ if (range && range.mark) {
+ const nextSelection =
+ Selection.findFrom(state.doc.resolve(range.to), 1, true) ??
+ TextSelection.create(state.tr.doc, 0);
+ dispatch?.(
+ state.tr
+ .setSelection(nextSelection)
+ .removeMark(range.from, range.to, range.mark)
+ );
+ return true;
+ }
+ return false;
+};
+
+const removeLinkNodeSelection = (): Command => (state, dispatch) => {
+ if (!(state.selection instanceof NodeSelection)) {
+ return false;
+ }
+
+ const existingMarks = state.selection.node.attrs.marks ?? [];
+ const updatedMarks = existingMarks.filter(
+ (mark: any) => mark.type !== "link"
+ );
+ const attrs = {
+ ...state.selection.node.attrs,
+ marks: updatedMarks,
+ };
+ dispatch?.(state.tr.setNodeMarkup(state.selection.from, undefined, attrs));
+ return true;
+};
+
+const toggleLinkTextSelection =
+ (attrs: Attrs): Command =>
+ (state, dispatch) => {
+ if (!(state.selection instanceof TextSelection)) {
+ return false;
+ }
+
+ return toggleMark(state.schema.marks.link, attrs)(state, dispatch);
+ };
+
+const toggleLinkNodeSelection =
+ (attrs: Attrs): Command =>
+ (state, dispatch) => {
+ if (!(state.selection instanceof NodeSelection)) {
+ return false;
+ }
+
+ const existingMarks = state.selection.node.attrs.marks ?? [];
+ const linkMark = existingMarks.find((mark: any) => mark.type === "link");
+ if (linkMark) {
+ return removeLinkNodeSelection()(state, dispatch);
+ } else {
+ return addLinkNodeSelection(attrs)(state, dispatch);
+ }
+ };
+
+export const toggleLink = (attrs: Attrs): Command =>
+ chainCommands(toggleLinkTextSelection(attrs), toggleLinkNodeSelection(attrs));
+
+export const addLink = (attrs: Attrs): Command =>
+ chainCommands(addLinkTextSelection(attrs), addLinkNodeSelection(attrs));
+
+export const openLink = (attrs: Attrs): Command =>
+ chainCommands(openLinkTextSelection(attrs), openLinkNodeSelection(attrs));
+
+export const updateLink = (attrs: Attrs): Command =>
+ chainCommands(updateLinkTextSelection(attrs), updateLinkNodeSelection(attrs));
+
+export const removeLink = (): Command =>
+ chainCommands(removeLinkTextSelection(), removeLinkNodeSelection());
diff --git a/shared/editor/marks/Link.tsx b/shared/editor/marks/Link.tsx
index d630bbc9e6..40b8b0d911 100644
--- a/shared/editor/marks/Link.tsx
+++ b/shared/editor/marks/Link.tsx
@@ -1,29 +1,26 @@
import { Token } from "markdown-it";
-import { toggleMark } from "prosemirror-commands";
import { InputRule } from "prosemirror-inputrules";
import { MarkdownSerializerState } from "prosemirror-markdown";
import {
- Attrs,
MarkSpec,
MarkType,
Node,
Mark as ProsemirrorMark,
} from "prosemirror-model";
-import {
- Command,
- EditorState,
- Plugin,
- Selection,
- TextSelection,
-} from "prosemirror-state";
+import { Command, EditorState, Plugin, TextSelection } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
import { toast } from "sonner";
import { isUrl, sanitizeUrl } from "../../utils/urls";
import { getMarkRange } from "../queries/getMarkRange";
-import { isMarkActive } from "../queries/isMarkActive";
import Mark from "./Mark";
import { isInCode } from "../queries/isInCode";
-import { addMark } from "../commands/addMark";
+import {
+ addLink,
+ openLink,
+ removeLink,
+ toggleLink,
+ updateLink,
+} from "../commands/link";
const LINK_INPUT_REGEX = /\[([^[]+)]\((\S+)\)$/;
@@ -113,102 +110,32 @@ export default class Link extends Mark {
];
}
- keys({ type }: { type: MarkType }): Record {
+ keys(): Record {
return {
"Mod-k": (state, dispatch) => {
if (state.selection.empty) {
return false;
}
- return toggleMark(type, { href: "" })(state, dispatch);
- },
- "Mod-Enter": (state) => {
- if (isMarkActive(type)(state)) {
- const range = getMarkRange(
- state.selection.$from,
- state.schema.marks.link
- );
- if (range && range.mark && this.options.onClickLink) {
- try {
- const event = new KeyboardEvent("keydown", { metaKey: false });
- this.options.onClickLink(
- sanitizeUrl(range.mark.attrs.href),
- event
- );
- } catch (_err) {
- toast.error(this.options.dictionary.openLinkError);
- }
- return true;
- }
- }
- return false;
+ return toggleLink({ href: "" })(state, dispatch);
},
+ "Mod-Enter": openLink({
+ onClickLink: this.options.onClickLink,
+ dictionary: this.options.dictionary,
+ }),
};
}
- commands({ type }: { type: MarkType }) {
+ commands() {
return {
- addLink: (attrs: Attrs): Command => addMark(type, attrs),
- updateLink:
- (attrs: Attrs): Command =>
- (state, dispatch) => {
- const range = getMarkRange(
- state.selection.$from,
- state.schema.marks.link
- );
-
- if (range && range.mark) {
- const nextSelection =
- Selection.findFrom(state.doc.resolve(range.to), 1, true) ??
- TextSelection.create(state.tr.doc, 0);
- dispatch?.(
- state.tr
- .setSelection(nextSelection)
- .removeMark(range.from, range.to, state.schema.marks.link)
- .addMark(
- range.from,
- range.to,
- state.schema.marks.link.create(attrs)
- )
- );
- return true;
- }
- return false;
- },
- openLink: (): Command => (state) => {
- const range = getMarkRange(
- state.selection.$from,
- state.schema.marks.link
- );
- if (range && range.mark && this.options.onClickLink) {
- try {
- const event = new KeyboardEvent("keydown", { metaKey: false });
- this.options.onClickLink(sanitizeUrl(range.mark.attrs.href), event);
- } catch (_err) {
- toast.error(this.options.dictionary.openLinkError);
- }
- return true;
- }
- return false;
- },
- removeLink: (): Command => (state, dispatch) => {
- const range = getMarkRange(
- state.selection.$from,
- state.schema.marks.link
- );
- if (range && range.mark) {
- const nextSelection =
- Selection.findFrom(state.doc.resolve(range.to), 1, true) ??
- TextSelection.create(state.tr.doc, 0);
- dispatch?.(
- state.tr
- .setSelection(nextSelection)
- .removeMark(range.from, range.to, range.mark)
- );
- return true;
- }
- return false;
- },
+ addLink,
+ updateLink,
+ openLink: (): Command =>
+ openLink({
+ onClickLink: this.options.onClickLink,
+ dictionary: this.options.dictionary,
+ }),
+ removeLink,
};
}
diff --git a/shared/editor/nodes/Image.tsx b/shared/editor/nodes/Image.tsx
index 994e1c465c..eb37a453f0 100644
--- a/shared/editor/nodes/Image.tsx
+++ b/shared/editor/nodes/Image.tsx
@@ -17,6 +17,7 @@ import { ComponentProps } from "../types";
import SimpleImage from "./SimpleImage";
import { LightboxImageFactory } from "../lib/Lightbox";
import { addComment } from "../commands/comment";
+import { addLink } from "../commands/link";
const imageSizeRegex = /\s=(\d+)?x(\d+)?$/;
@@ -515,6 +516,7 @@ export default class Image extends SimpleImage {
},
commentOnImage: (): Command =>
addComment({ userId: this.options.userId }),
+ linkOnImage: (): Command => addLink({ href: "" }),
};
}
diff --git a/shared/editor/queries/getMarkRange.ts b/shared/editor/queries/getMarkRange.ts
index 1d02db389f..0bff1f81cc 100644
--- a/shared/editor/queries/getMarkRange.ts
+++ b/shared/editor/queries/getMarkRange.ts
@@ -1,4 +1,5 @@
import { ResolvedPos, MarkType } from "prosemirror-model";
+import { NodeSelection } from "prosemirror-state";
export function getMarkRange($pos?: ResolvedPos, type?: MarkType) {
if (!$pos || !type) {
@@ -38,3 +39,18 @@ export function getMarkRange($pos?: ResolvedPos, type?: MarkType) {
return { from: startPos, to: endPos, mark };
}
+
+export function getMarkRangeNodeSelection(
+ selection: NodeSelection,
+ type: MarkType
+) {
+ const mark = (selection.node.attrs.marks ?? []).find(
+ (mark: any) => mark.type === type.name
+ );
+
+ if (!mark) {
+ return false;
+ }
+
+ return { from: selection.from, to: selection.to, mark };
+}