Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ const c = new CommandPal({
c.start()
// Destroy the instance
c.destroy()
// Display the instance
c.displayPalette(true)
// Hide the instance
c.displayPalette(false)
// Toggle from display -> hide or hide -> display
c.displayPalette()
```

### Subscribe to events
Expand Down
14 changes: 14 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
setAllShortCuts,
initShortCuts
} from "./shortcuts";
import {
storeDisplayPaletteMethod,
} from "./displayMethod";
const dispatch = createEventDispatcher();
export let hotkey;
Expand Down Expand Up @@ -48,6 +51,17 @@
await asyncTimeout(100);
onHandleCommand(command);
});
storeDisplayPaletteMethod(async active_state => {
if (! showModal) {focusedElement = document.activeElement}
if (active_state === undefined) {
showModal = !showModal;
} else if ([true, false].includes(active_state)) {
showModal = active_state;
} else {
throw("Non-boolean: " + active_state +
" - passed to displayPalette")
}
})
});
function setItems(newItems) {
Expand Down
11 changes: 11 additions & 0 deletions src/displayMethod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let displayPaletteMethod; // temp variable for holding function
// generated by App by calling setDisplayPalette

export function storeDisplayPaletteMethod(method) {
displayPaletteMethod = method;
}

export function retrieveDisplayPaletteMethod() {
return displayPaletteMethod;
}

Comment on lines +1 to +11
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clever, it's basically using the module file scope, as a container to hold the variable which is a function. This is global to the project, but private everywhere else.

To be honest, I think it's a bit convoluted, but I've sat down for a bit and can't think of a better solution 😅 so nice work 👌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with your solution, as it needs to be shared with main.js which is outside of the svelte context.
We could use some kind of event to pass the function handler or something, but that sounds even worse haha

2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import App from "./App.svelte";
import pubsub from "micro-pubsub";
import { retrieveDisplayPaletteMethod } from "./displayMethod";

class CommandPal {
constructor(options) {
Expand All @@ -18,6 +19,7 @@ class CommandPal {
hotkeysGlobal: this.options.hotkeysGlobal || false
},
});
this.displayPalette = retrieveDisplayPaletteMethod();
const ctx = this;
function subTo(eventName) {
ctx.app.$on(eventName, (e) => ctx.ps.publish(eventName, e.detail));
Expand Down