-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Plugin, EditorState } from "prosemirror-state"; | ||
import { EditorView } from "prosemirror-view"; | ||
import { Mark } from "prosemirror-model"; | ||
import { richTextSchema as schema } from "../schema"; | ||
|
||
/** | ||
* Returns the mark at cursor if it is of type `link` | ||
* @param state The current editor state | ||
*/ | ||
function findLinkAtCursor(state: EditorState): Mark { | ||
const { $from, empty } = state.selection; | ||
return ( | ||
empty && $from.marks().find((mark) => mark.type === schema.marks.link) | ||
); | ||
} | ||
|
||
/** Plugin that opens link in new window/tab on Mod-click */ | ||
export const linkModClickHandler = () => | ||
new Plugin({ | ||
props: { | ||
handleClick( | ||
this, | ||
view: EditorView, | ||
pos: number, | ||
event: MouseEvent | ||
) { | ||
const selectedLink = findLinkAtCursor(view.state); | ||
const modPressed = event.getModifierState( | ||
/Mac/.test(navigator.userAgent) ? "Meta" : "Control" | ||
); | ||
|
||
if (selectedLink && modPressed) { | ||
window.open(selectedLink.attrs.href, "_blank"); | ||
} | ||
|
||
return true; | ||
}, | ||
}, | ||
}); |