From 7e845242de34cfc486e347d3fe20caa6f80dce0a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 10 Dec 2017 15:54:54 -0800 Subject: [PATCH] Fix: Esc to close link toolbar Error when selection changes to not include link --- .../components/Toolbar/components/LinkToolbar.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/components/Editor/components/Toolbar/components/LinkToolbar.js b/app/components/Editor/components/Toolbar/components/LinkToolbar.js index afe84be60..9a3966688 100644 --- a/app/components/Editor/components/Toolbar/components/LinkToolbar.js +++ b/app/components/Editor/components/Toolbar/components/LinkToolbar.js @@ -58,6 +58,10 @@ class LinkToolbar extends Component { return; } + this.close(); + }; + + close = () => { if (this.input.value) { this.props.onBlur(); } else { @@ -93,7 +97,7 @@ class LinkToolbar extends Component { ev.preventDefault(); return this.save(ev.target.value); case 27: // escape - return this.input.blur(); + return this.close(); case 40: // down ev.preventDefault(); if (this.firstDocument) { @@ -129,11 +133,13 @@ class LinkToolbar extends Component { save = (href: string) => { const { editor, link } = this.props; href = href.trim(); + editor.change(change => { if (href) { change.setInline({ type: 'link', data: { href } }); } else if (link) { - change.unwrapInlineByKey(link.key); + const selContainsLink = !!change.value.startBlock.getChild(link.key); + if (selContainsLink) change.unwrapInlineByKey(link.key); } change.deselect(); this.props.onBlur(); @@ -145,8 +151,7 @@ class LinkToolbar extends Component { }; render() { - const { link } = this.props; - const href = link && link.data.get('href'); + const href = this.props.link.data.get('href'); const hasResults = this.resultIds.length > 0; return (