Skip to content

Commit

Permalink
refactor drag'n'drop on search input
Browse files Browse the repository at this point in the history
current default behavior is to insert additional drag & drop content into pre-existing content: e.g. first `alpha` is dropped, then `beta`; and the search-string becomes `alpbetaha`.  This change causes additional dropped content to overwrite pre-existing content entirely, such that `alpha` becomes `beta`.  Currently, drag-and-drop content in Chrome consists of the URL of the item being dragged; but in Firefox, using the `x-moz-url-desc`, if a tag or an account is dragged, then the display-text (e.g. `#somehashtag` or `@user`) will be dropped instead.  This enables the content-specific search drop-down to work on the more specific data-types (so, search for hashtag, or profile).  This functionality currently only works with Firefox, though: hence the `TODO` annotation
  • Loading branch information
panarom committed Nov 12, 2023
1 parent 69d00e2 commit 97ee043
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/javascript/mastodon/features/compose/components/search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ class Search extends PureComponent {
this.setState({ expanded: false, selectedOption: -1 });
};

handleDragOver = (e) => {
e.preventDefault();
};
handleDrop = (e) => {
const { onChange } = this.props;

e.preventDefault();
e.target.value=query;

this.setState({ expanded: true, selectedOption: -1 });

// TODO: update the type parameter if/when `url-desc` is standardized
let query = e.dataTransfer.getData('x-moz-url-desc') ||
e.dataTransfer.getData('text/uri-list') ||
e.dataTransfer.getData('text/plain');
onChange(query);
this._calculateOptions(query);
e.target.focus();
};

handleHashtagClick = () => {
const { value, onClickSearchResult, history } = this.props;

Expand Down Expand Up @@ -333,6 +353,8 @@ class Search extends PureComponent {
onKeyDown={this.handleKeyDown}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onDragOver={this.handleDragOver}
onDrop={this.handleDrop}
/>

<div role='button' tabIndex={0} className='search__icon' onClick={this.handleClear}>
Expand Down

0 comments on commit 97ee043

Please sign in to comment.