Last active
January 7, 2023 13:24
-
-
Save Quorafind/03cbeba9c55955622beb4d8958eb8179 to your computer and use it in GitHub Desktop.
Obsidian Plugin Register Keymap for LeafView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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