Skip to content

Commit

Permalink
extension/link: adds 'whenNotEditable' as option for openOnClick (#3312)
Browse files Browse the repository at this point in the history
* checks whenNotEditable condition in clickHandler

* passes whenNotEditable option from to helper

* adds docs for whenNotEditable

* adds  to ClickHandlerOptions type
  • Loading branch information
benkroeger authored Apr 8, 2024
1 parent b710783 commit 0f41e38
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/api/marks/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ Link.configure({
})
```

If set to `'whenNotEditable'`, links will be opened on click only when editor is not editable.

```js
Link.configure({
openOnClick: 'whenNotEditable',
})
```

### linkOnPaste
Adds a link to the current selection if the pasted content only contains an url.

Expand Down
6 changes: 5 additions & 1 deletion packages/extension-link/src/helpers/clickHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { MarkType } from '@tiptap/pm/model'
import { Plugin, PluginKey } from '@tiptap/pm/state'

type ClickHandlerOptions = {
type: MarkType
type: MarkType,
whenNotEditable: boolean,
}

export function clickHandler(options: ClickHandlerOptions): Plugin {
return new Plugin({
key: new PluginKey('handleClickLink'),
props: {
handleClick: (view, pos, event) => {
if (options.whenNotEditable && view.editable) {
return false
}
if (event.button !== 0) {
return false
}
Expand Down
3 changes: 2 additions & 1 deletion packages/extension-link/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface LinkOptions {
/**
* If enabled, links will be opened on click.
*/
openOnClick: boolean
openOnClick: boolean | 'whenNotEditable'
/**
* Adds a link to the current selection if the pasted content only contains an url.
*/
Expand Down Expand Up @@ -207,6 +207,7 @@ export const Link = Mark.create<LinkOptions>({
plugins.push(
clickHandler({
type: this.type,
whenNotEditable: this.options.openOnClick === 'whenNotEditable',
}),
)
}
Expand Down

0 comments on commit 0f41e38

Please sign in to comment.