Skip to content

Commit

Permalink
resolve: #1276
Browse files Browse the repository at this point in the history
ctrl/cmd + / to open the magic key in note editor
  • Loading branch information
windingwind committed Feb 22, 2025
1 parent 8427bec commit d1664f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ To open a note in a tab, double-click/press `Enter` on the item in the library,

#### Magic Key

BN enhances the note editor with a _Magic Key_ command palette, which can be opened by typing `/` in the editor. You can type or use the arrow keys to navigate the commands, and press `Enter` to execute.
BN enhances the note editor with a _Magic Key_ command palette, which can be opened by typing `/` in the editor or pressing `Ctrl/Cmd` + `/`. You can type or use the arrow keys to navigate the commands, and press `Enter` to execute.

<div align=center><img src="https://github.com/user-attachments/assets/bdbe244e-c120-4d9b-aa28-5285a25a723a" width="800px"></img></div>

Expand Down
24 changes: 21 additions & 3 deletions src/extras/editor/magicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ class PluginState {
this._closePopup();
}

if (!this.options.enable) {
return;
}

// If the document hasn't changed, we don't need to do anything
if (prevState.doc.eq(state.doc)) {
return;
Expand Down Expand Up @@ -288,12 +292,26 @@ class PluginState {
}

handleKeydown = async (event: KeyboardEvent) => {
if (!this._hasPopup()) {
if (event.key === "Escape") {
if (this._hasPopup()) {
this._closePopup();
}
return;
}

if (event.key === "Escape") {
this._closePopup();
const isMac =
typeof navigator != "undefined" ? /Mac/.test(navigator.platform) : false;
if (
((isMac && event.metaKey) || (!isMac && event.ctrlKey)) &&
event.key === "/"
) {
if (!this._hasPopup()) {
this._openPopup(this.state);
} else {
this._closePopup();
}
event.preventDefault();
return;
}
};

Expand Down
3 changes: 1 addition & 2 deletions src/extras/editor/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ function initPlugins(options: {
if (options.linkPreview.previewType !== "disable")
plugins = initLinkPreviewPlugin(plugins, options.linkPreview);
if (options.markdownPaste.enable) plugins = initMarkdownPastePlugin(plugins);
if (options.magicKey.enable)
plugins = initMagicKeyPlugin(plugins, options.magicKey);
plugins = initMagicKeyPlugin(plugins, options.magicKey);
// Collect all plugins and reconfigure the state only once
const newState = core.view.state.reconfigure({
plugins,
Expand Down

0 comments on commit d1664f0

Please sign in to comment.