Skip to content

Commit

Permalink
update link
Browse files Browse the repository at this point in the history
  • Loading branch information
tiavina-mika committed Jul 27, 2024
1 parent a2e96fc commit 5cb9216
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/components/LinkDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor } from '@tiptap/react';
import { useState, ChangeEvent } from 'react';
import { useState, ChangeEvent, useEffect } from 'react';

import { TextField } from '@mui/material';
import Dialog from './Dialog';
Expand All @@ -16,6 +16,13 @@ const LinkDialog = ({ editor, open, onClose, labels }: Props) => {
const [link, setLink] = useState<string>('');
const [error, setError] = useState<string>('');

useEffect(() => {
const previousUrl = editor.getAttributes('link').href;
console.log("🚀 ~ useEffect ~ previousUrl:", previousUrl)
if (!previousUrl) return;
setLink(previousUrl);
}, [editor])

if (!editor) {
return null;
}
Expand Down Expand Up @@ -43,17 +50,20 @@ const LinkDialog = ({ editor, open, onClose, labels }: Props) => {

// empty
if (link === '') {
(editor.commands as any).unsetLink();
editor.chain().focus().extendMarkRange('link').unsetLink().run()
return;
}

// editor.chain().focus().extendMarkRange('link').setLink({ href: link }).run();
editor.commands.setLink({ href: link })
// update link
(editor.commands as any).setLink({ href: link });
onClose();
};

const handleClose = () => {
(editor.commands as any)?.unsetLink();
editor.chain().focus().unsetLink().run();
setLink('');
setError('');
onClose();
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const Toolbar = ({
name: "link",
icon: Link,
onClick: toggleLinkDialog,
disabled: false,
disabled: !editor.isActive('link'),
tooltip: toolbarLabels?.link || 'Link'
},
{
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useTextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const extensions = [
TipTapTypography,
Underline,
Link.configure({
openOnClick: false,
protocols: [
"https",
"mailto",
Expand Down

0 comments on commit 5cb9216

Please sign in to comment.