This commit is contained in:
Vanessa 2023-05-31 15:54:44 +08:00
parent c39877c6de
commit f8ab2c6d50
4 changed files with 51 additions and 42 deletions

View file

@ -85,38 +85,6 @@ const hasKeymap = (keymap: Record<string, IKeymapItem>, key1: "general" | "edito
return match;
};
const mergePluginHotkey = (app: App) => {
if (!window.siyuan.config.keymap.plugin) {
window.siyuan.config.keymap.plugin = {};
}
app.plugins.forEach(plugin => {
plugin.commands.forEach(command => {
if (!window.siyuan.config.keymap.plugin[plugin.name]) {
command.customHotkey = command.hotkey;
window.siyuan.config.keymap.plugin[plugin.name] = {
[command.langKey]: {
default: command.hotkey,
custom: command.hotkey,
}
};
return;
}
if (!window.siyuan.config.keymap.plugin[plugin.name][command.langKey]) {
command.customHotkey = command.hotkey;
window.siyuan.config.keymap.plugin[plugin.name][command.langKey] = {
default: command.hotkey,
custom: command.hotkey,
};
return;
}
if (window.siyuan.config.keymap.plugin[plugin.name][command.langKey]) {
command.customHotkey = window.siyuan.config.keymap.plugin[plugin.name][command.langKey].custom || command.hotkey;
window.siyuan.config.keymap.plugin[plugin.name][command.langKey]["default"] = command.hotkey;
}
});
});
};
export const onGetConfig = (isStart: boolean, app: App) => {
const matchKeymap1 = matchKeymap(Constants.SIYUAN_KEYMAP.general, "general");
const matchKeymap2 = matchKeymap(Constants.SIYUAN_KEYMAP.editor.general, "editor", "general");
@ -131,7 +99,6 @@ export const onGetConfig = (isStart: boolean, app: App) => {
const hasKeymap4 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.heading, "editor", "heading");
const hasKeymap5 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.list, "editor", "list");
const hasKeymap6 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.table, "editor", "table");
mergePluginHotkey(app);
if (!window.siyuan.config.readonly &&
(!matchKeymap1 || !matchKeymap2 || !matchKeymap3 || !matchKeymap4 || !matchKeymap5 || !matchKeymap6 ||
!hasKeymap1 || !hasKeymap2 || !hasKeymap3 || !hasKeymap4 || !hasKeymap5 || !hasKeymap6)) {

View file

@ -50,7 +50,7 @@ export const genItemPanel = (type: string, containerElement: Element, app: App)
case "keymap":
containerElement.innerHTML = keymap.genHTML(app);
keymap.element = containerElement;
keymap.bindEvent();
keymap.bindEvent(app);
break;
case "bazaar":
bazaar.element = containerElement;

View file

@ -165,17 +165,27 @@ export const keymap = {
${pluginHtml}
</div>`;
},
_setkeymap() {
_setkeymap(app: App) {
const data: IKeymap = JSON.parse(JSON.stringify(Constants.SIYUAN_KEYMAP));
keymap.element.querySelectorAll("label.b3-list-item input").forEach((item) => {
const keys = item.getAttribute("data-key").split(Constants.ZWSP);
const newHotkey = item.getAttribute("data-value")
if (keys[0] === "plugin") {
window.siyuan.config.keymap.plugin[keys[1]][keys[2]].custom = item.getAttribute("data-value");
window.siyuan.config.keymap.plugin[keys[1]][keys[2]].custom = newHotkey;
data.plugin = window.siyuan.config.keymap.plugin;
app.plugins.forEach((plugin) => {
if (plugin.name === keys[1]) {
plugin.commands.forEach(command => {
if (command.langKey === keys[2]) {
command.customHotkey = newHotkey;
}
})
}
})
} else if (keys[0] === "general") {
data[keys[0]][keys[1]].custom = item.getAttribute("data-value");
data[keys[0]][keys[1]].custom = newHotkey;
} else if (keys[0] === "editor" && (keys[1] === "general" || keys[1] === "insert" || keys[1] === "heading" || keys[1] === "list" || keys[1] === "table")) {
data[keys[0]][keys[1]][keys[2]].custom = item.getAttribute("data-value");
data[keys[0]][keys[1]][keys[2]].custom = newHotkey;
}
});
window.siyuan.config.keymap = data;
@ -252,7 +262,7 @@ export const keymap = {
}
return tip;
},
bindEvent() {
bindEvent(app: App) {
keymap.element.querySelector("#keymapRefreshBtn").addEventListener("click", () => {
exportLayout({
reload: true,
@ -308,7 +318,7 @@ export const keymap = {
inputElement.value = updateHotkeyTip(inputElement.getAttribute("data-default"));
inputElement.setAttribute("data-value", inputElement.getAttribute("data-default"));
inputElement.previousElementSibling.textContent = inputElement.value;
keymap._setkeymap();
keymap._setkeymap(app);
event.preventDefault();
event.stopPropagation();
break;
@ -317,7 +327,7 @@ export const keymap = {
inputElement.value = "";
inputElement.previousElementSibling.textContent = "";
inputElement.setAttribute("data-value", "");
keymap._setkeymap();
keymap._setkeymap(app);
event.preventDefault();
event.stopPropagation();
break;
@ -388,7 +398,7 @@ export const keymap = {
if (hasConflict) {
return;
}
keymap._setkeymap();
keymap._setkeymap(app);
}, 1000);
});
item.addEventListener("blur", function () {

View file

@ -64,6 +64,7 @@ const loadPluginJS = async (app: App, item: IPluginData) => {
return plugin;
};
// 启用插件
export const loadPlugin = async (app: App, item: IPluginData) => {
const plugin = await loadPluginJS(app, item);
const styleElement = document.createElement("style");
@ -98,6 +99,36 @@ const updateDock = (dockItem: IDockTab[], index: number, plugin: Plugin, type: s
});
};
const mergePluginHotkey = (plugin: Plugin) => {
if (!window.siyuan.config.keymap.plugin) {
window.siyuan.config.keymap.plugin = {};
}
plugin.commands.forEach(command => {
if (!window.siyuan.config.keymap.plugin[plugin.name]) {
command.customHotkey = command.hotkey;
window.siyuan.config.keymap.plugin[plugin.name] = {
[command.langKey]: {
default: command.hotkey,
custom: command.hotkey,
}
};
return;
}
if (!window.siyuan.config.keymap.plugin[plugin.name][command.langKey]) {
command.customHotkey = command.hotkey;
window.siyuan.config.keymap.plugin[plugin.name][command.langKey] = {
default: command.hotkey,
custom: command.hotkey,
};
return;
}
if (window.siyuan.config.keymap.plugin[plugin.name][command.langKey]) {
command.customHotkey = window.siyuan.config.keymap.plugin[plugin.name][command.langKey].custom || command.hotkey;
window.siyuan.config.keymap.plugin[plugin.name][command.langKey]["default"] = command.hotkey;
}
});
};
export const afterLoadPlugin = (plugin: Plugin) => {
try {
plugin.onLayoutReady();
@ -115,6 +146,7 @@ export const afterLoadPlugin = (plugin: Plugin) => {
});
}
/// #if !MOBILE
mergePluginHotkey(plugin);
plugin.statusBarIcons.forEach(element => {
const statusElement = document.getElementById("status")
if (element.getAttribute("data-position") === "right") {