Skip to content

Commit

Permalink
🎨 支持 ctrl+s #5376
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Jul 9, 2022
1 parent 45aa7ae commit 5bdaa3d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/config/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export const keymap = {
}

if (["⌘", "⇧", "⌥", "⌃"].includes(keymapStr.substr(keymapStr.length - 1, 1)) ||
["⌘S", "⌘A", "⌘X", "⌘C", "⌘V", "⌘/", "⌘↑", "⌘↓", "⇧↑", "⇧↓", "⇧→", "⇧←", "⇧⇥", "⇧⌘⇥", "⌃⇥", "⌃⌘⇥", "⇧⌘→", "⇧⌘←", "⌘Home", "⌘End", "⇧↩", "↩", "PageUp", "PageDown", "⌫", "⌦"].includes(keymapStr)) {
["⌘A", "⌘X", "⌘C", "⌘V", "⌘/", "⌘↑", "⌘↓", "⇧↑", "⇧↓", "⇧→", "⇧←", "⇧⇥", "⇧⌘⇥", "⌃⇥", "⌃⌘⇥", "⇧⌘→", "⇧⌘←", "⌘Home", "⌘End", "⇧↩", "↩", "PageUp", "PageDown", "⌫", "⌦"].includes(keymapStr)) {
showMessage(tip + "] " + window.siyuan.languages.invalid);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export abstract class Constants {
};

// "⌘", "⇧", "⌥", "⌃"
// "⌘S", "⌘A", "⌘X", "⌘C", "⌘V", "⌘/", "⌘↑", "⌘↓", "⇧↑", "⇧↓", "⇧→", "⇧←", "⇧⇥", "⇧⌘⇥", "⌃⇥", "⌃⌘⇥", "⇧⌘→", "⇧⌘←", "⌘Home", "⌘End", "⇧↩", "↩", "PageUp", "PageDown", "⌫", "⌦" 不可自定义
// "⌘A", "⌘X", "⌘C", "⌘V", "⌘/", "⌘↑", "⌘↓", "⇧↑", "⇧↓", "⇧→", "⇧←", "⇧⇥", "⇧⌘⇥", "⌃⇥", "⌃⌘⇥", "⇧⌘→", "⇧⌘←", "⌘Home", "⌘End", "⇧↩", "↩", "PageUp", "PageDown", "⌫", "⌦" 不可自定义
public static readonly SIYUAN_KEYMAP: IKeymap = {
general: {
syncNow: {default: "F9", custom: "F9"},
Expand Down
28 changes: 28 additions & 0 deletions app/src/protyle/wysiwyg/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,14 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
topNodeElement = topNodeElement.parentElement;
}
zoomOut(protyle, topNodeElement.getAttribute("data-node-id"));
event.preventDefault();
event.stopPropagation();
return;
}

if (matchHotKey(window.siyuan.config.keymap.general.enterBack.custom, event)) {
zoomOut(protyle, protyle.block.parent2ID, nodeElement.getAttribute("data-node-id"));
event.preventDefault();
event.stopPropagation();
return;
}
Expand Down Expand Up @@ -813,6 +815,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
writeText(`siyuan://blocks/${actionElement.getAttribute("data-node-id")}`);
event.preventDefault();
event.stopPropagation();
return true;
}
/// #if !MOBILE
Expand All @@ -837,6 +840,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
});
}
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.copyBlockEmbed.custom, event)) {
Expand All @@ -849,6 +853,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
writeText(`{{select * from blocks where id='${actionElement.getAttribute("data-node-id")}'}}`);
event.preventDefault();
event.stopPropagation();
return true;
}

Expand Down Expand Up @@ -876,6 +881,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
updateTransaction(protyle, topElement.getAttribute("data-node-id"), topElement.outerHTML, oldHTML);
}
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.rename.custom, event)) {
Expand All @@ -899,18 +905,21 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
});
}
event.preventDefault();
event.stopPropagation();
return;
}

if (selectText.trim() && matchHotKey(window.siyuan.config.keymap.editor.general.newNameFile.custom, event)) {
newFileBySelect(selectText, protyle);
event.preventDefault();
event.stopPropagation();
return;
}

if (matchHotKey(window.siyuan.config.keymap.editor.general.newContentFile.custom, event)) {
newFileContentBySelect(protyle);
event.preventDefault();
event.stopPropagation();
return;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.alignLeft.custom, event)) {
Expand Down Expand Up @@ -987,31 +996,37 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (matchHotKey(window.siyuan.config.keymap.editor.heading.heading1.custom, event)) {
phTransaction(protyle, range, nodeElement, 1);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.heading.heading2.custom, event)) {
phTransaction(protyle, range, nodeElement, 2);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.heading.heading3.custom, event)) {
phTransaction(protyle, range, nodeElement, 3);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.heading.heading4.custom, event)) {
phTransaction(protyle, range, nodeElement, 4);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.heading.heading5.custom, event)) {
phTransaction(protyle, range, nodeElement, 5);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.heading.heading6.custom, event)) {
phTransaction(protyle, range, nodeElement, 6);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.insert.code.custom, event) && nodeElement.getAttribute("data-type") !== "NodeCodeBlock") {
Expand All @@ -1025,6 +1040,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
updateTransaction(protyle, id, newHTML, html);
highlightRender(newNodeElement);
event.preventDefault();
event.stopPropagation();
return true;
}
// toolbar action
Expand Down Expand Up @@ -1064,10 +1080,12 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (selectElements.length > 0 && selectElements[0].getAttribute("data-type") === "NodeListItem") {
listOutdent(protyle, Array.from(selectElements), range);
event.preventDefault();
event.stopPropagation();
return true;
} else if (nodeElement.parentElement.classList.contains("li") && nodeElement.getAttribute("data-type") !== "NodeCodeBlock") {
listOutdent(protyle, [nodeElement.parentElement], range);
event.preventDefault();
event.stopPropagation();
return true;
}
}
Expand All @@ -1076,10 +1094,12 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (selectElements.length > 0 && selectElements[0].getAttribute("data-type") === "NodeListItem") {
listIndent(protyle, Array.from(selectElements), range);
event.preventDefault();
event.stopPropagation();
return true;
} else if (nodeElement.parentElement.classList.contains("li") && nodeElement.getAttribute("data-type") !== "NodeCodeBlock") {
listIndent(protyle, [nodeElement.parentElement], range);
event.preventDefault();
event.stopPropagation();
return true;
}
}
Expand All @@ -1088,6 +1108,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
protyle.hint.lastIndex = -1;
protyle.hint.fill("* [ ] " + Lute.Caret, protyle);
event.preventDefault();
event.stopPropagation();
return;
}

Expand All @@ -1096,6 +1117,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
protyle.hint.lastIndex = -1;
protyle.hint.fill(`| col1${Lute.Caret} | col2 | col3 |\n| --- | --- | --- |\n| | | |\n| | | |`, protyle);
event.preventDefault();
event.stopPropagation();
return;
}

Expand All @@ -1115,6 +1137,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
taskItemElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, taskItemElement.getAttribute("data-node-id"), taskItemElement.outerHTML, html);
event.preventDefault();
event.stopPropagation();
return;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.insertBefore.custom, event)) {
Expand All @@ -1125,6 +1148,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (matchHotKey(window.siyuan.config.keymap.editor.general.insertAfter.custom, event)) {
insertEmptyBlock(protyle, "afterend");
event.preventDefault();
event.stopPropagation();
return true;
}

Expand Down Expand Up @@ -1329,6 +1353,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {

if (matchHotKey(window.siyuan.config.keymap.editor.general.vLayout.custom, event)) {
event.preventDefault();
event.stopPropagation();
const selectsElement: HTMLElement[] = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
if (selectsElement.length < 2) {
return;
Expand All @@ -1343,6 +1368,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {

if (matchHotKey(window.siyuan.config.keymap.editor.general.hLayout.custom, event)) {
event.preventDefault();
event.stopPropagation();
const selectsElement: HTMLElement[] = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
if (selectsElement.length < 2) {
return;
Expand Down Expand Up @@ -1460,6 +1486,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
getCurrentWindow().webContents.pasteAndMatchStyle();
}, 100);
event.preventDefault();
event.stopPropagation();
return;
}

Expand All @@ -1470,6 +1497,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (isLocalPath(linkAddress)) {
openBy(linkAddress, "folder");
event.preventDefault();
event.stopPropagation();
}
}
return;
Expand Down
38 changes: 29 additions & 9 deletions app/src/util/globalShortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ export const globalShortcut = () => {
});
return;
}

if (event.key === window.siyuan.config.keymap.general.syncNow.custom) {
if (matchHotKey(window.siyuan.config.keymap.general.syncNow.custom, event)) {
event.preventDefault();
event.stopPropagation();
if (needSubscribe() || document.querySelector("#barSync svg").classList.contains("fn__rotate")) {
return;
}
Expand All @@ -362,7 +363,6 @@ export const globalShortcut = () => {
return;
}
fetchPost("/api/sync/performSync", {});
event.preventDefault();
return;
}
if (matchHotKey(window.siyuan.config.keymap.general.lockScreen.custom, event)) {
Expand All @@ -372,23 +372,20 @@ export const globalShortcut = () => {
});
});
event.preventDefault();
event.stopPropagation();
return;
}
if (matchHotKey(window.siyuan.config.keymap.general.history.custom, event)) {
openHistory();
event.preventDefault();
event.stopPropagation();
return;
}
if (!window.siyuan.config.readonly && matchHotKey(window.siyuan.config.keymap.general.config.custom, event)) {
openSetting();
event.preventDefault();
return;
}
// https://github.com/siyuan-note/insider/issues/445
if (matchHotKey("⌘S", event)) {
event.preventDefault();
event.stopPropagation();
return true;
return;
}
const target = event.target as HTMLElement;
if (matchHotKey("⌘A", event) && target.tagName !== "INPUT" && target.tagName !== "TEXTAREA") {
Expand All @@ -404,6 +401,7 @@ export const globalShortcut = () => {
target.blur();
}
event.preventDefault();
event.stopPropagation();
return true;
}
});
Expand All @@ -418,11 +416,13 @@ export const globalShortcut = () => {
target.blur();
}
event.preventDefault();
event.stopPropagation();
return;
}
if (matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) {
newFile(undefined, undefined, true);
event.preventDefault();
event.stopPropagation();
return;
}

Expand Down Expand Up @@ -587,6 +587,7 @@ export const globalShortcut = () => {
openGlobalSearch("", false);
}
event.preventDefault();
event.stopPropagation();
return;
}

Expand Down Expand Up @@ -615,8 +616,16 @@ export const globalShortcut = () => {
openSearch(searchKey);
}
event.preventDefault();
event.stopPropagation();
return;
}

// https://github.com/siyuan-note/insider/issues/445
if (matchHotKey("⌘S", event)) {
event.preventDefault();
event.stopPropagation();
return true;
}
});

window.addEventListener("blur", () => {
Expand Down Expand Up @@ -717,6 +726,7 @@ const editKeydown = (event: KeyboardEvent) => {
openSearch(searchKey);
}
event.preventDefault();
event.stopPropagation();
return true;
}
if (!isFileFocus && matchHotKey(window.siyuan.config.keymap.general.move.custom, event)) {
Expand All @@ -732,6 +742,7 @@ const editKeydown = (event: KeyboardEvent) => {
movePathTo(protyle.notebookId, protyle.path);
}
event.preventDefault();
event.stopPropagation();
return true;
}
const target = event.target as HTMLElement;
Expand All @@ -741,6 +752,7 @@ const editKeydown = (event: KeyboardEvent) => {
if (matchHotKey(window.siyuan.config.keymap.editor.general.preview.custom, event)) {
setEditMode(protyle, "preview");
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.wysiwyg.custom, event)) {
Expand All @@ -753,17 +765,20 @@ const editKeydown = (event: KeyboardEvent) => {
onGet(getResponse, protyle);
});
event.preventDefault();
event.stopPropagation();
return true;
}
// 没有光标时,无法撤销 https://ld246.com/article/1624021111567
if (matchHotKey(window.siyuan.config.keymap.editor.general.undo.custom, event)) {
protyle.undo.undo(protyle);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.redo.custom, event)) {
protyle.undo.redo(protyle);
event.preventDefault();
event.stopPropagation();
return true;
}
return false;
Expand All @@ -777,6 +792,7 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
const files = dockFile.data.file as Files;
if (matchHotKey(window.siyuan.config.keymap.general.selectOpen1.custom, event)) {
event.preventDefault();
event.stopPropagation();
const element = document.querySelector(".layout__wnd--active > .layout-tab-bar > .item--focus") ||
document.querySelector(".layout-tab-bar > .item--focus");
if (element) {
Expand Down Expand Up @@ -815,6 +831,8 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
name: isFile ? getDisplayName(liElement.getAttribute("data-name"), false, true) : getNotebookName(notebookId),
type: isFile ? "file" : "notebook",
});
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey("⌘/", event)) {
Expand All @@ -832,6 +850,7 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
if (isFile && matchHotKey(window.siyuan.config.keymap.general.move.custom, event)) {
movePathTo(notebookId, pathString, false);
event.preventDefault();
event.stopPropagation();
return true;
}
let searchKey = "";
Expand All @@ -847,6 +866,7 @@ const fileTreeKeydown = (event: KeyboardEvent) => {
openSearch(searchKey, undefined, notebookId);
}
event.preventDefault();
event.stopPropagation();
return true;
}
const target = event.target as HTMLElement;
Expand Down

0 comments on commit 5bdaa3d

Please sign in to comment.