Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Toggle the table mode using the status item #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export function exitContext(editor: vscode.TextEditor, type: ContextType) {
}
}

export function toggleContext(editor: vscode.TextEditor, type: ContextType) {
const editorState = state.get(editor.document.fileName) || [];
if (editorState.indexOf(ContextType.TableMode) >= 0) {
exitContext(editor, type);
} else {
enterContext(editor, type);
}
}

export function restoreContext(editor: vscode.TextEditor) {
let toEnter: ContextType[] = [];
// @ts-ignore
Expand All @@ -55,8 +64,8 @@ class Context {
setState(isEnabled: boolean) {
vscode.commands.executeCommand('setContext', this.type, isEnabled);
if (this.statusItem) {
const stateText = isEnabled ? 'On' : 'Off';
this.statusItem.text = `${this.title}: ${stateText}`;
const stateText = isEnabled ? '$(check)' : '$(x)';
this.statusItem.text = `${this.title} ${stateText}`;
}
}
}
9 changes: 6 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OrgLocator, OrgParser, OrgStringifier } from './ttOrg';
import { Locator, Parser, Stringifier, Table } from './ttTable';
import { MarkdownLocator, MarkdownParser, MarkdownStringifier } from './ttMarkdown';
import { isUndefined } from 'util';
import { registerContext, ContextType, enterContext, exitContext, restoreContext } from './context';
import { registerContext, ContextType, enterContext, exitContext, restoreContext, toggleContext } from './context';
import * as cfg from './configuration';

let locator: Locator;
Expand All @@ -33,7 +33,8 @@ export function activate(ctx: vscode.ExtensionContext) {
loadConfiguration();

const statusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
registerContext(ContextType.TableMode, '$(book) Table Mode', statusItem);
registerContext(ContextType.TableMode, 'Table', statusItem);
statusItem.command = 'text-tables.tableModeToggle';

if (configuration.showStatus) {
statusItem.show();
Expand Down Expand Up @@ -63,7 +64,9 @@ export function activate(ctx: vscode.ExtensionContext) {
(e) => enterContext(e, ContextType.TableMode)));
ctx.subscriptions.push(vscode.commands.registerTextEditorCommand('text-tables.tableModeOff',
(e) => exitContext(e, ContextType.TableMode)));

ctx.subscriptions.push(vscode.commands.registerTextEditorCommand('text-tables.tableModeToggle',
(e) => toggleContext(e, ContextType.TableMode)));

ctx.subscriptions.push(registerTableCommand('text-tables.moveRowDown', cmd.moveRowDown, {format: true}));
ctx.subscriptions.push(registerTableCommand('text-tables.moveRowUp', cmd.moveRowUp, {format: true}));
ctx.subscriptions.push(registerTableCommand('text-tables.moveColRight', async (editor, range, table) => {
Expand Down