Skip to content

Commit

Permalink
feat: remember history across files
Browse files Browse the repository at this point in the history
  • Loading branch information
heycalmdown committed Mar 11, 2022
1 parent 5e441a3 commit 81a575b
Show file tree
Hide file tree
Showing 3 changed files with 3,340 additions and 77 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
## Cursor Position

This plugin remembers the recent 20 cursor positions history and allows you to jump to them back and forth like VSCode.
This plugin remembers the recent 50 cursor positions history and allows you to jump to them back and forth like VSCode.

### Limitations

- It remembers the cursor position once a second
- It resets the cursor history when the another file is opened
- The history is not saved permanently
- The forward history will be resetted when you make new move while navigating the history

Expand Down
120 changes: 45 additions & 75 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,73 @@
import { App, Editor, MarkdownView, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { TFile, Editor, MarkdownView, Plugin } from 'obsidian';

// Remember to rename these classes and interfaces!

interface MyPluginSettings {
mySetting: string;
interface History {
file: TFile;
line: number;
}

const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
function historyToString(history: History) {
return `${history.file.path}@${history.line}`
}
function logHistory(backward: History[], cur: History, forward: History[]) {
console.log(backward.map(historyToString), historyToString(cur), forward.map(historyToString))
}

export default class MyPlugin extends Plugin {
settings: MyPluginSettings;

backward: number[] = [];
forward: number[] = [];
cur = 0;

restoreState() {
this.backward = []
this.forward = []
this.cur = 0
}
export default class CursorPositionPlugin extends Plugin {
backward: History[] = [];
forward: History[] = [];
cur: History;

async onload() {
await this.loadSettings();

this.registerEvent(
this.app.workspace.on('file-open', (file) => this.restoreState()),
this.app.workspace.on('file-open', (file) => {
if (!this.cur) {
this.cur = { file, line: 0 };
}
if (this.cur.file !== file) {
this.saveHistory(file, 0);
}
}),
);

// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'cursor-position-backward',
name: 'Go back',
editorCallback: (editor: Editor, view: MarkdownView) => {
editorCallback: async (editor: Editor, view: MarkdownView) => {
if (this.backward.length < 1) return;
const cur = this.cur
const prev = this.backward.pop()
this.forward.push(this.cur)
this.forward.push(cur)
if (cur.file !== prev.file) {
await this.app.workspace.getMostRecentLeaf().openFile(prev.file)
}
this.cur = prev
editor.setSelection({ line: prev, ch: 0 });
console.log(this.backward, this.cur, this.forward)
editor.setSelection({ line: prev.line, ch: 0 });
logHistory(this.backward, this.cur, this.forward)
}
});
this.addCommand({
id: 'cursor-position-forward',
name: 'Go forward',
editorCallback: (editor: Editor, view: MarkdownView) => {
editorCallback: async (editor: Editor, view: MarkdownView) => {
if (this.forward.length < 1) return;
const cur = this.cur
const prev = this.forward.pop()
this.backward.push(this.cur)
this.cur = prev
editor.setSelection({ line: prev, ch: 0 });
console.log(this.backward, this.cur, this.forward)
if (cur.file !== prev.file) {
await this.app.workspace.getMostRecentLeaf().openFile(prev.file)
}
editor.setSelection({ line: prev.line, ch: 0 });
logHistory(this.backward, this.cur, this.forward)
}
});

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));

this.registerInterval(window.setInterval(() => {
// const view = this.app.workspace.getActiveViewOfType(MarkdownView);
const editor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor
if (editor) {
// const from = editor.getCursor("anchor");
const to = editor.getCursor("head");
if (to.line === this.cur) return;
this.backward.push(this.cur)
this.cur = to.line
this.backward = this.backward.slice(-20)
this.forward = []
console.log(this.backward, this.cur, this.forward)
const cursor = editor.getCursor("head");
this.saveHistory(this.cur.file, cursor.line);
}
}, 1 * 1000));
}
Expand All @@ -79,39 +76,12 @@ export default class MyPlugin extends Plugin {

}

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

async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;

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

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

containerEl.empty();

containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});

new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => {
console.log('Secret: ' + value);
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
saveHistory(file: TFile, line: number) {
if (file === this.cur.file && line === this.cur.line) return;
this.backward.push(this.cur)
this.cur = { file, line }
this.backward = this.backward.slice(-50)
this.forward = []
logHistory(this.backward, this.cur, this.forward)
}
}
Loading

0 comments on commit 81a575b

Please sign in to comment.