Skip to content

Commit

Permalink
Fix for issue jupyterlab#15966. Cell outputs searched even when repla…
Browse files Browse the repository at this point in the history
…ce is active, replace button is disabled when highlighting an output, and replace all only replaces editable strings.
  • Loading branch information
Tanmay-Deshmukh committed Apr 13, 2024
1 parent 7b95542 commit 2b07cbb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
18 changes: 16 additions & 2 deletions packages/documentsearch/src/searchmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ export class SearchDocumentModel
}
}

/**
* Whether the replace button is enabled or not.
*/
get replaceEnabled(): boolean {
return this._replaceEnabled;
}

/**
* Search expression
*/
Expand Down Expand Up @@ -270,7 +277,10 @@ export class SearchDocumentModel
* Highlight the next match.
*/
async highlightNext(): Promise<void> {
await this.searchProvider.highlightNext();
const match = await this.searchProvider.highlightNext();
if (match) {
this._replaceEnabled = match.node == undefined;
}
// Emit state change as the index needs to be updated
this.stateChanged.emit();
}
Expand All @@ -279,7 +289,10 @@ export class SearchDocumentModel
* Highlight the previous match
*/
async highlightPrevious(): Promise<void> {
await this.searchProvider.highlightPrevious();
const match = await this.searchProvider.highlightPrevious();
if (match) {
this._replaceEnabled = match.node == undefined;
}
// Emit state change as the index needs to be updated
this.stateChanged.emit();
}
Expand Down Expand Up @@ -386,6 +399,7 @@ export class SearchDocumentModel
private _initialQuery = '';
private _filters: IFilters = {};
private _replaceText: string = '';
private _replaceEnabled = true;
private _searchActive = false;
private _searchDebouncer: Debouncer;
private _searchExpression = '';
Expand Down
10 changes: 9 additions & 1 deletion packages/documentsearch/src/searchview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ interface IReplaceEntryProps {
preserveCase: boolean;
replaceOptionsSupport: IReplaceOptionsSupport | undefined;
replaceText: string;
replaceEnabled: boolean;
translator?: ITranslator;
}

Expand Down Expand Up @@ -259,6 +260,7 @@ function ReplaceEntry(props: IReplaceEntryProps): JSX.Element {
className={REPLACE_BUTTON_WRAPPER_CLASS}
onClick={() => props.onReplaceCurrent()}
tabIndex={0}
disabled={!props.replaceEnabled}
>
<span
className={`${REPLACE_BUTTON_CLASS} ${BUTTON_CONTENT_CLASS}`}
Expand Down Expand Up @@ -455,7 +457,7 @@ interface ISearchOverlayProps {
*/
replaceEntryVisible: boolean;
/**
* Whther the filters grid is visible.
* Whether the filters grid is visible.
*/
filtersVisible: boolean;
/**
Expand All @@ -466,6 +468,10 @@ interface ISearchOverlayProps {
* Replacement expression
*/
replaceText: string;
/**
* Whether the replace button is enabled.
*/
replaceEnabled: boolean;
/**
* The text in the search entry
*/
Expand Down Expand Up @@ -777,6 +783,7 @@ class SearchOverlay extends React.Component<ISearchOverlayProps> {
onReplaceAll={() => this.props.onReplaceAll()}
replaceOptionsSupport={this.props.replaceOptionsSupport}
replaceText={this.props.replaceText}
replaceEnabled={this.props.replaceEnabled}
preserveCase={this.props.preserveCase}
translator={this.translator}
/>
Expand Down Expand Up @@ -912,6 +919,7 @@ export class SearchDocumentView extends VDomRenderer<SearchDocumentModel> {
filtersVisible={this._showFilters}
replaceOptionsSupport={this.model.replaceOptionsSupport}
replaceText={this.model.replaceText}
replaceEnabled={this.model.replaceEnabled}
initialSearchText={this.model.initialQuery}
searchInputRef={
this._searchInput as React.RefObject<HTMLTextAreaElement>
Expand Down
5 changes: 5 additions & 0 deletions packages/documentsearch/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ export interface ISearchMatch {
* Start location of the match (in a text, this is the column)
*/
position: number;

/**
* Node containing the match
*/
node?: Text;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/notebook/src/searchprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class NotebookSearchProvider extends SearchProvider<NotebookPanel> {
'Search in the cell outputs (not available when replace options are shown).'
),
default: false,
supportReplace: false
supportReplace: true
},
selection: {
title:
Expand Down

0 comments on commit 2b07cbb

Please sign in to comment.