updated: 再看了一遍设置项,可以使用跳转到父块再折叠,一键的方法暂时没找到,或许会考虑用代码片段临时实现吧
在社区、思源快捷键中里搜索 折叠 父
暂未搜索到相关内容。
社区开发者文档里看到了 /api/block | 思源社区文档 折叠节点的接口,虽然还没了解怎么使用。但获取当前编辑节点、向上获取父节点、调用折叠实现的脚本思路已经跃然纸上了。
(function() {
const debug = true; // 调试模式开关
let processing = false;
// 增强版日志系统
function debugLog(...args) {
if (debug) console.debug('[KeyMapper]', ...args);
}
// 核心逻辑
document.addEventListener('keydown', async function(e) {
if (!isTargetCombo(e)) return;
e.preventDefault();
e.stopImmediatePropagation();
if (processing) {
debugLog('阻止重复触发');
return;
}
processing = true;
debugLog('开始处理组合键', `方向:${e.key}`);
try {
// 第一阶段:模拟 Ctrl+Shift+J
await simulatePhysicalKeystrokes([
{key: 'Control', type: 'down'},
{key: 'Shift', type: 'down'},
{key: 'j', type: 'press'}, // 包含down和up
{key: 'Shift', type: 'up'},
{key: 'Control', type: 'up'}
]);
// 第二阶段:模拟 Ctrl+方向键
await simulatePhysicalKeystrokes([
{key: 'Control', type: 'down'},
{key: e.key.replace('Arrow', ''), type: 'press'},
{key: 'Control', type: 'up'}
]);
debugLog('全部操作完成');
} catch (err) {
console.error('模拟失败:', err);
} finally {
processing = false;
}
});
// 组合键检测
function isTargetCombo(e) {
return e.ctrlKey &&
e.shiftKey &&
e.altKey &&
['ArrowUp','ArrowDown'].includes(e.key);
}
// 物理按键模拟器(改进版)
async function simulatePhysicalKeystrokes(steps) {
return new Promise(resolve => {
const target = document.activeElement;
let stepIndex = 0;
function processNext() {
if (stepIndex >= steps.length) {
debugLog('步骤完成');
return resolve();
}
const {key, type} = steps[stepIndex++];
const isDown = type === 'down' || type === 'press';
const isUp = type === 'up' || type === 'press';
const eventType = isDown ? 'keydown' : 'keyup';
// 构建事件参数
const eventOptions = {
key: key.toLowerCase(),
code: key.startsWith('Arrow') ? key : `Key${key.toUpperCase()}`,
keyCode: getKeyCode(key),
bubbles: true,
composed: true,
ctrlKey: steps.some(s => s.key === 'Control' && s.type === 'down'),
shiftKey: steps.some(s => s.key === 'Shift' && s.type === 'down')
};
// 派发事件
if (isDown) {
debugLog(`派发 ${eventType}:`, eventOptions);
target.dispatchEvent(new KeyboardEvent(eventType, eventOptions));
}
// 处理按下并立即释放的情况
if (type === 'press') {
setTimeout(() => {
debugLog(`派发 keyup:`, {...eventOptions, ctrlKey: false, shiftKey: false});
target.dispatchEvent(new KeyboardEvent('keyup', {
...eventOptions,
ctrlKey: false,
shiftKey: false
}));
setTimeout(processNext, 15);
}, 30);
} else {
setTimeout(processNext, 15);
}
}
processNext();
});
}
// 键码映射表
function getKeyCode(key) {
const map = {
'Control': 17,
'Shift': 16,
'j': 74,
'Up': 38,
'Down': 40
};
return map[key] || key.toUpperCase().charCodeAt(0);
}
})();
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于