Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension/link: adds 'whenNotEditable' as option for openOnClick #3312

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading