Skip to content

Instantly share code, notes, and snippets.

@Quorafind
Last active January 7, 2023 13:24
Show Gist options
  • Save Quorafind/03cbeba9c55955622beb4d8958eb8179 to your computer and use it in GitHub Desktop.
Save Quorafind/03cbeba9c55955622beb4d8958eb8179 to your computer and use it in GitHub Desktop.
Obsidian Plugin Register Keymap for LeafView
export default class mapForNotePlugin extends Plugin {
//...
public activeMapForNoteView: MapForNote = null;
async onload(): Promise<void> {
//...
this.registerEventListeners();
}
//...
private popScope = null;
private registerEventListeners() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this.app.workspace.onLayoutReady(async () => {
const activeLeafChangeEventHandler = async (leaf: WorkspaceLeaf) => {
// const previouslyActiveEV = self.activeMapForNoteView;
const newActiveviewEV: MapForNote = leaf.view instanceof MapForNote ? leaf.view : null;
self.activeMapForNoteView = newActiveviewEV;
if (self.popScope) {
self.popScope();
self.popScope = null;
}
// if (!leaf.view instanceof ExcalidrawView) return;
if (newActiveviewEV) {
const scope = this.app.keymap.getRootScope();
console.log(scope);
const handler = scope.register(['Mod'], 'Enter', () => {
console.log('Hi');
return true;
});
scope.keys.unshift(scope.keys.pop()); // Force our handler to the front of the list
self.popScope = () => scope.unregister(handler);
} else {
const scope = new Scope(self.app.scope);
console.log(scope);
app.keymap.pushScope(scope);
app.keymap.popScope(scope);
}
};
self.registerEvent(self.app.workspace.on('active-leaf-change', activeLeafChangeEventHandler));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment