From 402406a3f71121f1cfbb5a0993be43ec32cfb278 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 28 Feb 2023 08:58:45 -0500 Subject: [PATCH] Search for docs matching selected text when opening link editor --- app/editor/components/LinkEditor.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/editor/components/LinkEditor.tsx b/app/editor/components/LinkEditor.tsx index e848cf1f0..4f23aae96 100644 --- a/app/editor/components/LinkEditor.tsx +++ b/app/editor/components/LinkEditor.tsx @@ -73,15 +73,18 @@ class LinkEditor extends React.Component { return sanitizeUrl(this.props.mark?.attrs.href) ?? ""; } - get suggestedLinkTitle(): string { + get selectedText(): string { const { state } = this.props.view; - const { value } = this.state; const selectionText = state.doc.cut( state.selection.from, state.selection.to ).textContent; - return value.trim() || selectionText.trim(); + return selectionText.trim(); + } + + get suggestedLinkTitle(): string { + return this.state.value.trim() || this.selectedText; } componentWillUnmount = () => { @@ -195,7 +198,7 @@ class LinkEditor extends React.Component { this.setState({ selectedIndex }); }; - handleChange = async ( + handleSearch = async ( event: React.ChangeEvent ): Promise => { const value = event.target.value; @@ -205,7 +208,7 @@ class LinkEditor extends React.Component { selectedIndex: -1, }); - const trimmedValue = value.trim(); + const trimmedValue = value.trim() || this.selectedText; if (trimmedValue && this.props.onSearchLink) { try { @@ -318,7 +321,8 @@ class LinkEditor extends React.Component { } onKeyDown={this.handleKeyDown} onPaste={this.handlePaste} - onChange={this.handleChange} + onChange={this.handleSearch} + onFocus={this.handleSearch} autoFocus={this.href === ""} />