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

feat: add setting for highlight duration #5

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { EditorView } from "@codemirror/view";
import { MarkViewPlugin, markViewPlugin } from "./markViewPlugin";
import { Vim, MarkdownViewExtended, vimEvents } from "./types";

import { Settings, SettingsTab, DEFAULT_SETTINGS } from "./settings"

// TODO: option to change highlight duration
// TODO: option to change the colour of highlight
// TODO: option to supress in visual mode
Expand All @@ -20,6 +22,8 @@ export default class VimYankHighlightPlugin extends Plugin {
codeMirrorVimObject: Vim;
timeoutHandle: number;

settings: Settings;

private get activeView() {
return this.app.workspace.getActiveViewOfType(
MarkdownView
Expand All @@ -42,6 +46,9 @@ export default class VimYankHighlightPlugin extends Plugin {
}

async onload() {
await this.loadSettings()
this.addSettingTab(new SettingsTab(this.app, this))

this.registerEditorExtension([markViewPlugin]);

this.registerEvent(
Expand Down Expand Up @@ -116,7 +123,15 @@ export default class VimYankHighlightPlugin extends Plugin {
clearTimeout(this.timeoutHandle);
this.timeoutHandle = window.setTimeout(() => {
plugin.cleanYankText(timeoutEditorView);
}, 500);
}, this.settings.highlightDuration);
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

onunload() {
Expand Down
44 changes: 44 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import VimYankHighlightPlugin from "./main";
import { App, PluginSettingTab, Setting } from "obsidian";

export interface Settings {
highlightDuration: number
}

export const DEFAULT_SETTINGS: Partial<Settings> = {
highlightDuration: 500
}

export class SettingsTab extends PluginSettingTab {
plugin: VimYankHighlightPlugin;

constructor(app: App, plugin: VimYankHighlightPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
let { containerEl } = this;

containerEl.empty();

new Setting(containerEl)
.setName("Date duration")
.setDesc("Duration in milliseconds for the highlights")
.addText((text) =>
text
.setPlaceholder("500")
.setValue(this.plugin.settings.highlightDuration.toString())
.onChange(async (durationString) => {
const duration = parseInt(durationString);
if (isNaN(duration)) {
this.plugin.settings.highlightDuration = DEFAULT_SETTINGS.highlightDuration!;
await this.plugin.saveSettings();
return;
}
this.plugin.settings.highlightDuration = duration;
await this.plugin.saveSettings();
})
);
}
}