Alt+Z 加粗与标记同时给文字加样式

see https://ld246.com/article/1744445101965

// alt+z 给文字加粗和标记
(()=>{
    // 监听键盘按下alt+z事件
    document.addEventListener('keydown', function(event) {
        // 检查是否按下了 Alt 键以及物理按键是 'KeyZ'
        if (
            event.altKey &&                     // 按下了 Alt 键
            event.code === 'KeyZ' &&            // 物理按键是 Z
            !event.shiftKey &&                  // 未按下 Shift 键
            !event.ctrlKey &&                   // 未按下 Ctrl 键
            !event.metaKey                     // 未按下 Meta 键
        ) {
            // 阻止默认行为(如果有必要)
            event.preventDefault();

            // 获取protyle
            const protyle = getProtyle();
            // 给文字加粗
            protyle.toolbar.setInlineMark(protyle, 'strong', "toolbar");
            // 给文字标记
            protyle.toolbar.setInlineMark(protyle, 'mark', "toolbar");
        }
    });

    function getProtyle() {
        try {
            if(document.getElementById("sidebar")) return siyuan.mobile.editor.protyle;
            const currDoc = siyuan?.layout?.centerLayout?.children.map(item=>item.children.find(item=>item.headElement?.classList.contains('item--focus') && (item.panelElement.closest('.layout__wnd--active')||item.panelElement.closest('[data-type="wnd"]')))).find(item=>item);
            return currDoc?.model.editor.protyle;
        } catch(e) {
            console.error(e);
            return null;
        }
    }
})();

这个 protyle.toolbar.setInlineMark(protyle, 'mark', "toolbar"); mark 这个参数可以任意改,工具栏上的"strong", "em", "s", "code", "mark", "tag", "u", "sup", "clear", "sub", "kbd"样式都支持。

参考 https://github.com/siyuan-note/siyuan/blob/a01523dc98799590396ffcabdc90b2dc6efe8474/app/src/mobile/util/keyboardToolbar.ts#L615C77-L615C154

如果想要改文字颜色或背景色也可以

比如:

protyle.toolbar.setInlineMark(protyle, "text", "range", {type:'color', color:'var(--b3-font-color8)'});
protyle.toolbar.setInlineMark(protyle, "text", "range", {type:'backgroundColor', color:'var(--b3-font-background2)'});

参考 https://github.com/siyuan-note/siyuan/blob/a01523dc98799590396ffcabdc90b2dc6efe8474/app/src/protyle/toolbar/Font.ts#L284

image.png

留下你的脚步
推荐阅读