Skip to content

Commit

Permalink
[M] Keep the visibility of search field #47
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrnperl committed Oct 16, 2023
1 parent 2e3159b commit 9ef22d1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@
"shortTitle": "Search/Nav",
"category": "outline-map"
},
{
"command": "outline-map.focusSearch",
"title": "Focus Search Field",
"icon": "$(search)",
"shortTitle": "Search/Nav",
"category": "outline-map"
},
{
"command": "outline-map.workspace.closeFile",
"title": "Close",
Expand Down Expand Up @@ -446,7 +453,7 @@
],
"keybindings": [
{
"command": "outline-map.toggleSearch",
"command": "outline-map.focusSearch",
"key": "Alt+l"
}
],
Expand Down
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export interface ConfigMsg extends Msg{

export interface FocusMsg extends Msg{
type: 'focus';
toggle: boolean;
}

export interface ScrollMsg extends Msg{
Expand Down
21 changes: 18 additions & 3 deletions src/extension/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commands } from 'vscode';
import { Memento, commands } from 'vscode';
import { OutlineView } from './outline';
import { config } from './config';
import { ChangeDepthMsg, FocusMsg, PinSMsg, PinStatus } from '../common';
Expand Down Expand Up @@ -45,9 +45,20 @@ function freeze(outlineProvider: OutlineView) {
}

// export
function switchSearchField(outlineProvider: OutlineView) {
function switchSearchField(outlineProvider: OutlineView, state: Memento) {
state.update('searchField', !state.get('searchField', false));
outlineProvider.postMessage({
type: 'focus',
toggle: true,
} as FocusMsg);
}

// export
function focusSearchField(outlineProvider: OutlineView, state: Memento) {
state.update('searchField', true);
outlineProvider.postMessage({
type: 'focus',
toggle: false,
} as FocusMsg);
}

Expand Down Expand Up @@ -91,7 +102,11 @@ export const OutlineViewCommandList: Command[] = [
{
name: 'outline-map.toggleSearch',
fn: switchSearchField,
}
},
{
name: 'outline-map.focusSearch',
fn: focusSearchField,
},
];


Expand Down
6 changes: 3 additions & 3 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { OutlineView } from './outline';

import { OutlineViewCommandList, WorkspaceCommandList } from './commands';
import { ScrollMsg } from '../common';
import { FocusMsg, ScrollMsg } from '../common';
import { config } from './config';
import { debounce, throttle } from '../utils';
import { RegionProvider, tokensLegend } from './region';
Expand Down Expand Up @@ -53,7 +53,7 @@ export function activate(context: ExtensionContext) {
);
}

const outlineView = new OutlineView(context, workspaceSymbols);
const outlineView = new OutlineView(context, workspaceSymbols, context.globalState.get('searchField', false));


context.subscriptions.push(
Expand Down Expand Up @@ -107,7 +107,7 @@ export function activate(context: ExtensionContext) {

//#endregion event
//#region command
...OutlineViewCommandList.map(command => commands.registerCommand(command.name, command.fn.bind(null, outlineView))),
...OutlineViewCommandList.map(command => commands.registerCommand(command.name, command.fn.bind(null, outlineView, context.globalState))),
...WorkspaceCommandList.map(command => commands.registerCommand(command.name, command.fn.bind(null, workspaceSymbols))),
//#endregion command
);
Expand Down
13 changes: 11 additions & 2 deletions src/extension/outline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from './config';
import { commands, DocumentSymbol, ExtensionContext, TextDocument, Uri, WebviewView, WebviewViewProvider, window, Range, Selection, Position, Diagnostic, DiagnosticSeverity } from 'vscode';
import { Msg, UpdateMsg, Op, UpdateOp, DeleteOp, InsertOp, SymbolNode, MoveOp, PinStatus } from '../common';
import { Msg, UpdateMsg, Op, UpdateOp, DeleteOp, InsertOp, SymbolNode, MoveOp, PinStatus, FocusMsg } from '../common';
import { WorkspaceSymbols } from './workspace';


Expand Down Expand Up @@ -30,9 +30,12 @@ export class OutlineView implements WebviewViewProvider {

private workspaceSymbols: WorkspaceSymbols | undefined;

constructor(context: ExtensionContext, workspaceSymbols?: WorkspaceSymbols) {
private initialSearch: boolean;

constructor(context: ExtensionContext, workspaceSymbols?: WorkspaceSymbols, initialSearch = false) {
this.extensionUri = context.extensionUri;
this.workspaceSymbols = workspaceSymbols;
this.initialSearch = initialSearch;
}

pin(pinStatus: PinStatus) {
Expand Down Expand Up @@ -91,6 +94,12 @@ export class OutlineView implements WebviewViewProvider {
debug: config.debug(),
},
});
if (this.initialSearch) {
this.postMessage({
type: 'focus',
toggle: false,
} as FocusMsg);
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/webview/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { DeleteOp, InsertOp, MoveOp, ScrollMsg, Msg, ChangeDepthMsg , SymbolNode, UpdateOp, UpdateMsg, ConfigMsg, GotoMsg } from '../common';
import { DeleteOp, InsertOp, MoveOp, ScrollMsg, Msg, ChangeDepthMsg , SymbolNode, UpdateOp, UpdateMsg, ConfigMsg, GotoMsg, FocusMsg } from '../common';

const vscode = acquireVsCodeApi();

Expand Down Expand Up @@ -89,9 +89,9 @@ const SMsgHandler = {
maxDepth = Math.max(1, maxDepth + msg.data.delta);
new Toast(`Depth: ${maxDepth}`, 3000);
},
switchSearchField: () => {
switchSearchField: (toggle: boolean) => {
const inputContainer = document.querySelector('#input-container') as HTMLDivElement;
if (inputContainer.classList.contains('active')) {
if (inputContainer.classList.contains('active') && toggle) {
inputContainer.classList.toggle('active', false);
}
else {
Expand Down Expand Up @@ -195,7 +195,7 @@ function init() {
SMsgHandler.changeDepth(message as ChangeDepthMsg);
break;
case 'focus':
SMsgHandler.switchSearchField();
SMsgHandler.switchSearchField((message as FocusMsg).toggle);
}
});

Expand Down

0 comments on commit 9ef22d1

Please sign in to comment.