From 2d10e5fdc6f6de7d8f9d0ae0c35570f09f5a01a4 Mon Sep 17 00:00:00 2001 From: sibiraj-s Date: Fri, 8 Jan 2021 15:49:53 +0530 Subject: [PATCH] fix: focus editor after inserting link or image --- src/lib/commands/Image.ts | 4 +++- src/lib/commands/Link.ts | 12 ++++++++++-- src/lib/modules/menu/image/image.component.ts | 1 + src/lib/modules/menu/link/link.component.ts | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/commands/Image.ts b/src/lib/commands/Image.ts index ca4f9c10..1ab57698 100644 --- a/src/lib/commands/Image.ts +++ b/src/lib/commands/Image.ts @@ -17,7 +17,9 @@ class Image { nodeAttrs.width = selection.node.attrs.width; } - tr.replaceSelectionWith(type.createAndFill(nodeAttrs)); + tr + .replaceSelectionWith(type.createAndFill(nodeAttrs)) + .scrollIntoView(); if (tr.docChanged) { dispatch?.(tr); diff --git a/src/lib/commands/Link.ts b/src/lib/commands/Link.ts index c20fb5c9..7a2d9c05 100644 --- a/src/lib/commands/Link.ts +++ b/src/lib/commands/Link.ts @@ -32,8 +32,16 @@ class Link { const node = schema.text(text, [schema.marks.link.create(attrs)]); - dispatch(tr.replaceSelectionWith(node, false)); - return true; + + tr.replaceSelectionWith(node, false) + .scrollIntoView(); + + if (tr.docChanged) { + dispatch?.(tr); + return true; + } + + return false; } isActive(state: EditorState, options = defaultOptions): boolean { diff --git a/src/lib/modules/menu/image/image.component.ts b/src/lib/modules/menu/image/image.component.ts index 2ed23b20..caa513ce 100644 --- a/src/lib/modules/menu/image/image.component.ts +++ b/src/lib/modules/menu/image/image.component.ts @@ -119,6 +119,7 @@ export class ImageComponent implements OnDestroy { }; ImageCommand.execute(attrs, state, dispatch); + this.editorView.focus(); this.hideForm(); } diff --git a/src/lib/modules/menu/link/link.component.ts b/src/lib/modules/menu/link/link.component.ts index 73fca703..65d4e41b 100644 --- a/src/lib/modules/menu/link/link.component.ts +++ b/src/lib/modules/menu/link/link.component.ts @@ -135,6 +135,7 @@ export class LinkComponent implements OnDestroy { if (selection.empty) { LinkCommand.insert(text, attrs, state, dispatch); + this.editorView.focus(); } else { LinkCommand.update(attrs, state, dispatch); }