Skip to content

Commit

Permalink
fix: edit link works
Browse files Browse the repository at this point in the history
Fixes #464
  • Loading branch information
petyosi committed May 21, 2024
1 parent 7d0d945 commit eb103b6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/plugins/link-dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,23 @@ export const linkDialogState$ = Cell<InactiveLinkDialog | PreviewLinkDialog | Ed
)

r.sub(r.pipe(updateLink$, withLatestFrom(activeEditor$, linkDialogState$, currentSelection$)), ([payload, editor, state, selection]) => {
const url = payload.url.trim()
const title = payload.title.trim()
const url = payload.url?.trim() ?? ''
const title = payload.title?.trim() ?? ''

if (url !== '') {
if (selection?.isCollapsed()) {
const linkContent = title || url
editor?.update(
() => {
if (!getLinkNodeInSelection(selection)) {
const linkNode = getLinkNodeInSelection(selection)
if (!linkNode) {
const node = $createLinkNode(url, { title })
node.append($createTextNode(linkContent))
$insertNodes([node])
node.select()
} else {
linkNode.setURL(url)
linkNode.setTitle(title)
}
},
{ discrete: true }
Expand Down Expand Up @@ -245,7 +249,7 @@ export const linkDialogState$ = Cell<InactiveLinkDialog | PreviewLinkDialog | Ed
* A signal that updates the current link with the published payload.
* @group Link Dialog
*/
export const updateLink$ = Signal<{ url: string; title: string }>()
export const updateLink$ = Signal<{ url: string | undefined; title: string | undefined }>()
/**
* An action that cancel the edit of the current link.
* @group Link Dialog
Expand Down

0 comments on commit eb103b6

Please sign in to comment.