siyuan/app/src/protyle/wysiwyg/commonHotkey.ts

75 lines
3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {matchHotKey} from "../util/hotKey";
import {fetchPost} from "../../util/fetch";
import {writeText} from "../util/compatibility";
import {focusByOffset, getSelectionOffset} from "../util/selection";
import {fullscreen, netImg2LocalAssets} from "../breadcrumb/action";
import {addLoading, setPadding} from "../ui/initUI";
import {Constants} from "../../constants";
import {onGet} from "../util/onGet";
import {openBacklink, openGraph, openOutline} from "../../layout/dock/util";
import {saveScroll} from "../scroll/saveScroll";
export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent) => {
const target = event.target as HTMLElement;
if (matchHotKey(window.siyuan.config.keymap.editor.general.copyHPath.custom, event)) {
fetchPost("/api/filetree/getHPathByID", {
id: protyle.block.rootID
}, (response) => {
writeText(response.data);
});
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh.custom, event)) {
addLoading(protyle);
fetchPost("/api/filetree/getDoc", {
id: protyle.block.showAll ? protyle.block.id : protyle.block.rootID,
mode: 0,
size: protyle.block.showAll ? Constants.SIZE_GET_MAX : Constants.SIZE_GET,
}, getResponse => {
onGet(getResponse, protyle, protyle.block.showAll ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS], saveScroll(protyle, true), true);
});
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.fullscreen.custom, event)) {
fullscreen(protyle.element);
setPadding(protyle);
event.preventDefault();
event.stopPropagation();
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.netImg2LocalAsset.custom, event)) {
netImg2LocalAssets(protyle);
event.preventDefault();
event.stopPropagation();
return;
}
if (protyle.model) {
if (matchHotKey(window.siyuan.config.keymap.editor.general.backlinks.custom, event)) {
event.preventDefault();
event.stopPropagation();
openBacklink(protyle);
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.graphView.custom, event)) {
event.preventDefault();
event.stopPropagation();
openGraph(protyle);
return true;
}
if (matchHotKey(window.siyuan.config.keymap.editor.general.outline.custom, event)) {
event.preventDefault();
event.stopPropagation();
const offset = getSelectionOffset(target);
openOutline(protyle);
// switchWnd 后range会被清空需要重新设置
focusByOffset(target, offset.start, offset.end);
return true;
}
}
};