[js] 在面包屑旁添加全屏按钮,快捷切换全屏

本贴最后更新于 401 天前,其中的信息可能已经时移世改

效果是在面包屑旁添加一个全屏按钮,快捷切换页签的全屏状态:

JS 片段:

// 在面包屑旁添加全屏按钮,快捷切换全屏 JS片段
// author by JeffreyChen https://ld246.com/article/1731698559408
// 参考 @wilsons 的方法进行了优化 https://ld246.com/article/1731683038390/comment/1731693866270#comments ,不再需要全屏切换快捷键
(function() {
  function addFullScreenButton(protyleElement) {
    // 检查该 protyle 是否已经有了 fullScreen_simulate 按钮
    if (protyleElement.querySelector('.fullScreen_simulate')) {
      return; // 如果已存在,直接返回
    }

    let mode = protyleElement.querySelector('.protyle-breadcrumb .block__icon[data-type="readonly"]');

    if (mode) {
      mode.insertAdjacentHTML(
        "beforebegin",
        '<button class="fullScreen_simulate block__icon fn__flex-center ariaLabel" aria-label="全屏切换"></button>'
      );

      let fullScreenBtn = protyleElement.querySelector(".fullScreen_simulate");
      fullScreenBtn.innerHTML = `<svg><use xlink:href="#iconFullscreen"></use></svg>`;

      fullScreenBtn.addEventListener("click", function (e) {
        // 获取 .layout-tab-container > .protyle .protyle-breadcrumb__space 元素
        const breadcrumbSpace = protyleElement.querySelector('.protyle-breadcrumb__space');
        // 如果元素存在,则模拟点击,聚焦当前页签
        if (breadcrumbSpace) {
          breadcrumbSpace.click();
        }

        toggleFullScreen(protyleElement, fullScreenBtn); // 切换全屏状态
      });
    }
  }

  // 切换全屏状态的函数
  function toggleFullScreen(protyle, fullScreenBtn) {
    if (!window.siyuan.editorIsFullscreen) {
      enterFullScreen(protyle, fullScreenBtn);
    } else {
      exitFullScreen(protyle, fullScreenBtn);
    }
  }

  function enterFullScreen(protyle, fullScreenBtn) {
    protyle.classList.add("fullscreen");
    window.siyuan.editorIsFullscreen = true;
    updateFullScreenButton(fullScreenBtn, true); // 更新按钮
  }

  function exitFullScreen(protyle, fullScreenBtn) {
    protyle.classList.remove("fullscreen");
    window.siyuan.editorIsFullscreen = false;
    updateFullScreenButton(fullScreenBtn, false); // 更新按钮
  }

  function updateFullScreenButton(fullScreenBtn, isFullScreen) {
    const iconUse = fullScreenBtn.querySelector('use');
    // 切换图标
    iconUse.setAttribute('xlink:href', isFullScreen ? '#iconFullscreenExit' : '#iconFullscreen');
    fullScreenBtn.setAttribute('aria-label', isFullScreen ? '退出全屏' : '全屏');
  }

  // 定期检查 .layout__center 是否存在于 DOM 中
  function checkForLayoutCenter() {
    const targetNode = document.querySelector('.layout__center');
    if (targetNode) {
      startObserving(targetNode);
      // 立即检查一次 protyle-breadcrumb
      checkProtyleElements(targetNode);
    } else {
      setTimeout(checkForLayoutCenter, 200);
    }
  }

  function checkProtyleElements(targetNode) {
    const protyles = targetNode.querySelectorAll('.layout-tab-container > .protyle');
    protyles.forEach(protyle => {
      addFullScreenButton(protyle);
    });
  }

  function startObserving(targetNode) {
    const observer = new MutationObserver((mutations) => {
      mutations.forEach(mutation => {
        if (mutation.type === 'childList') {
          mutation.addedNodes.forEach(node => {
            // 检查添加的节点是否是 .protyle 元素
            if (node.nodeType === 1 && node.matches('.layout-tab-container > .protyle')) {
              addFullScreenButton(node);
            }
          });
        } else if (mutation.type === 'attributes' && mutation.target.matches('.layout-tab-container > .protyle')) {
          // 如果已有的 protyle 的类名发生变化,尝试添加全屏按钮
          addFullScreenButton(mutation.target);
        }
      });
    });

    // 配置并开始观察
    const config = { childList: true, subtree: true, attributes: true };
    observer.observe(targetNode, config);
  }

  checkForLayoutCenter();
})();
旧方案(全屏切换需要设置快捷键)
// 在面包屑旁添加全屏按钮,快捷切换全屏 JS片段
// author by JeffreyChen https://ld246.com/article/1731698559408

// 注意:需要为全屏切换设置一个快捷键

(function() {
  function addFullScreenButton(protyleElement) {
    // 检查该 protyle 是否已经有了 fullScreen_simulate 按钮
    if (protyleElement.querySelector('.fullScreen_simulate')) {
      return; // 如果已存在,直接返回
    }

    let mode = protyleElement.querySelector('.protyle-breadcrumb .block__icon[data-type="readonly"]');

    if (mode) {
      mode.insertAdjacentHTML(
        "beforebegin",
        '<button class="fullScreen_simulate block__icon fn__flex-center ariaLabel" aria-label="全屏切换"></button>'
      );

      let fullScreenBtn = protyleElement.querySelector(".fullScreen_simulate");
      fullScreenBtn.innerHTML = `<svg><use xlink:href="#iconFullscreen"></use></svg>`;

      fullScreenBtn.addEventListener("click", function (e) {
        // 获取 .layout-tab-container > .protyle .protyle-breadcrumb__space 元素
        const breadcrumbSpace = protyleElement.querySelector('.protyle-breadcrumb__space');
        // 如果元素存在,则模拟点击,聚焦当前页签
        if (breadcrumbSpace) {
          breadcrumbSpace.click();
        }

        dispatchKeyEvent();
  
        // 切换图标
        const iconUse = fullScreenBtn.querySelector('use');
        if (iconUse.getAttribute('xlink:href') === '#iconFullscreen') {
          iconUse.setAttribute('xlink:href', '#iconFullscreenExit');
        } else {
          iconUse.setAttribute('xlink:href', '#iconFullscreen');
        }
      });
    }
  }

  function dispatchKeyEvent() {
    let keyInit = parseHotKeyStr(window.top.siyuan.config.keymap.editor.general.fullscreen.custom);
    keyInit["bubbles"] = true;
    let keydownEvent = new KeyboardEvent('keydown', keyInit);
    document.getElementsByTagName("body")[0].dispatchEvent(keydownEvent);
    let keyUpEvent = new KeyboardEvent('keyup', keyInit);
    document.getElementsByTagName("body")[0].dispatchEvent(keyUpEvent);
  }

  /**
   * @param {*} hotkeyStr 思源hotkey格式 Refer: https://github.com/siyuan-note/siyuan/blob/d0f011b1a5b12e5546421f8bd442606bf0b5ad86/app/src/protyle/util/hotKey.ts#L4
   * @returns KeyboardEventInit Refer: https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/KeyboardEvent
   */
  function parseHotKeyStr(hotkeyStr) {
    let result = {
      ctrlKey: false,
      altKey: false,
      metaKey: false,
      shiftKey: false,
      key: 'A',
      keyCode: 0
    }
    if (hotkeyStr == "" || hotkeyStr == undefined || hotkeyStr == null) {
      console.error("解析快捷键设置失败", hotkeyStr);
      throw new Error("解析快捷键设置失败");
    }
    let onlyKey = hotkeyStr;
    if (hotkeyStr.indexOf("⌘") != -1) {
      result.ctrlKey = true;
      onlyKey = onlyKey.replace("⌘", "");
    }
    if (hotkeyStr.indexOf("⌥") != -1) {
      result.altKey = true;
      onlyKey = onlyKey.replace("⌥", "");
    }
    if (hotkeyStr.indexOf("⇧") != -1) {
      result.shiftKey = true;
      onlyKey = onlyKey.replace("⇧", "");
    }
    // 未处理 windows btn (MetaKey) 
    result.key = onlyKey;
    // 在https://github.com/siyuan-note/siyuan/commit/70acd57c4b4701b973a8ca93fadf6c003b24c789#diff-558f9f531a326d2fd53151e3fc250ac4bd545452ba782b0c7c18765a37a4e2cc
    // 更改中,思源改为使用keyCode判断快捷键按下事件,这里进行了对应的转换
    // 另请参考该提交中涉及的文件
    result.keyCode = keyCodeList[result.key];
    console.assert(result.keyCode != undefined, `keyCode转换错误,key为${result.key}`);
    switch (result.key) {
      case "→": {
        result.key = "ArrowRight";
        break;
      }
      case "←": {
        result.key = "ArrowLeft";
        break;
      }
      case "↑": {
        result.key = "ArrowUp";
        break;
      }
      case "↓": {
        result.key = "ArrowDown";
        break;
      }
      case "⌦": {
        result.key = "Delete";
        break;
      }
      case "⌫": {
        result.key = "Backspace";
        break;
      }
      case "↩": {
        result.key = "Enter";
        break;
      }
    }
    return result;
  }

  const keyCodeList = {
    "⌫": 8,
    "⇥": 9,
    "↩": 13,
    "⇧": 16,
    "⌘": 91,
    "⌥": 18,
    "Pause": 19,
    "CapsLock": 20,
    "Escape": 27,
    " ": 32,
    "PageUp": 33,
    "PageDown": 34,
    "End": 35,
    "Home": 36,
    "←": 37,
    "↑": 38,
    "→": 39,
    "↓": 40,
    "PrintScreen": 44,
    "Insert": 45,
    "⌦": 46,
    "0": 48,
    "1": 49,
    "2": 50,
    "3": 51,
    "4": 52,
    "5": 53,
    "6": 54,
    "7": 55,
    "8": 56,
    "9": 57,
    "A": 65,
    "B": 66,
    "C": 67,
    "D": 68,
    "E": 69,
    "F": 70,
    "G": 71,
    "H": 72,
    "I": 73,
    "J": 74,
    "K": 75,
    "L": 76,
    "M": 77,
    "N": 78,
    "O": 79,
    "P": 80,
    "Q": 81,
    "R": 82,
    "S": 83,
    "T": 84,
    "U": 85,
    "V": 86,
    "W": 87,
    "X": 88,
    "Y": 89,
    "Z": 90,
    "ContextMenu": 93,
    "MyComputer": 182,
    "MyCalculator": 183,
    ";": 186,
    "=": 187,
    ",": 188,
    "-": 189,
    ".": 190,
    "/": 191,
    "`": 192,
    "[": 219,
    "\\": 220,
    "]": 221,
    "'": 222,
    "*": 106,
    "+": 107,
    "-": 109,
    ".": 110,
    "/": 111,
    "F1": 112,
    "F2": 113,
    "F3": 114,
    "F4": 115,
    "F5": 116,
    "F6": 117,
    "F7": 118,
    "F8": 119,
    "F9": 120,
    "F10": 121,
    "F11": 122,
    "F12": 123,
    "NumLock": 144,
    "ScrollLock": 145
  };

  // 定期检查 .layout__center 是否存在于 DOM 中
  function checkForLayoutCenter() {
    const targetNode = document.querySelector('.layout__center');
    if (targetNode) {
      startObserving(targetNode);
      // 立即检查一次 protyle-breadcrumb
      checkProtyleElements(targetNode);
    } else {
      setTimeout(checkForLayoutCenter, 200);
    }
  }

  function checkProtyleElements(targetNode) {
    const protyles = targetNode.querySelectorAll('.layout-tab-container > .protyle');
    protyles.forEach(protyle => {
      addFullScreenButton(protyle);
    });
  }

  function startObserving(targetNode) {
    const observer = new MutationObserver((mutations) => {
      mutations.forEach(mutation => {
        if (mutation.type === 'childList') {
          mutation.addedNodes.forEach(node => {
            // 检查添加的节点是否是 .protyle 元素
            if (node.nodeType === 1 && node.matches('.layout-tab-container > .protyle')) {
              addFullScreenButton(node);
            }
          });
        } else if (mutation.type === 'attributes' && mutation.target.matches('.layout-tab-container > .protyle')) {
          // 如果已有的 protyle 的类名发生变化,尝试添加全屏按钮
          addFullScreenButton(mutation.target);
        }
      });
    });

    // 配置并开始观察
    const config = { childList: true, subtree: true, attributes: true };
    observer.observe(targetNode, config);
  }

  checkForLayoutCenter();
})();
打赏 50 积分后可见
50 积分 • 6 打赏
  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    28448 引用 • 119792 回帖
  • 代码片段

    代码片段分为 CSS 与 JS 两种代码,添加在 [设置 - 外观 - 代码片段] 中,这些代码会在思源笔记加载时自动执行,用于改善笔记的样式或功能。

    用户在该标签下分享代码片段时需在帖子标题前添加 [css] [js] 用于区分代码片段类型。

    285 引用 • 1988 回帖
2 操作
JeffreyChen 在 2024-11-16 03:51:10 更新了该帖
JeffreyChen 在 2024-11-16 03:49:59 更新了该帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • wujianzhong

    试了,,在空白的新空间可以使用,,但安装了插件之后就出问题了

    1 回复
  • 其他回帖
  • 建议加上其他事件切换时(比如按快捷键,文档菜单全屏等)也自动切换按钮,类似我下面这样的代码

    // 监听其他元素全屏事件
            observeClassAddition(protyle, 'fullscreen', (eventType) => {
                if(eventType === 'fullscreen'){
                    fullScreenBtn.innerHTML = exitFullscreenSvg;
                    fullScreenBtn.setAttribute('aria-label', '退出全屏');
                } else {
                    fullScreenBtn.innerHTML = fullscreenSvg;
                    fullScreenBtn.setAttribute('aria-label', '全屏');
                }
            });
    
  • wujianzhong

    跟 移除按钮 和 移除按钮-经典 两个插件冲突

    1 回复
  • 你排查一下跟哪个插件冲突

    1 回复
  • 查看全部回帖
JeffreyChen
目前作为思源笔记的半个客服、测试、开发、评审,在爱发电接受捐赠:https://afdian.com/a/JeffreyChen

推荐标签 标签

  • SVN

    SVN 是 Subversion 的简称,是一个开放源代码的版本控制系统,相较于 RCS、CVS,它采用了分支管理系统,它的设计目标就是取代 CVS。

    29 引用 • 98 回帖 • 694 关注
  • 设计模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

    201 引用 • 120 回帖 • 1 关注
  • 反馈

    Communication channel for makers and users.

    120 引用 • 906 回帖 • 307 关注
  • 快应用

    快应用 是基于手机硬件平台的新型应用形态;标准是由主流手机厂商组成的快应用联盟联合制定;快应用标准的诞生将在研发接口、能力接入、开发者服务等层面建设标准平台;以平台化的生态模式对个人开发者和企业开发者全品类开放。

    15 引用 • 127 回帖
  • WordPress

    WordPress 是一个使用 PHP 语言开发的博客平台,用户可以在支持 PHP 和 MySQL 数据库的服务器上架设自己的博客。也可以把 WordPress 当作一个内容管理系统(CMS)来使用。WordPress 是一个免费的开源项目,在 GNU 通用公共许可证(GPLv2)下授权发布。

    46 引用 • 114 回帖 • 140 关注
  • SMTP

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。

    4 引用 • 18 回帖 • 663 关注
  • jsDelivr

    jsDelivr 是一个开源的 CDN 服务,可为 npm 包、GitHub 仓库提供免费、快速并且可靠的全球 CDN 加速服务。

    5 引用 • 31 回帖 • 120 关注
  • 支付宝

    支付宝是全球领先的独立第三方支付平台,致力于为广大用户提供安全快速的电子支付/网上支付/安全支付/手机支付体验,及转账收款/水电煤缴费/信用卡还款/AA 收款等生活服务应用。

    29 引用 • 347 回帖 • 2 关注
  • Oracle

    Oracle(甲骨文)公司,全称甲骨文股份有限公司(甲骨文软件系统有限公司),是全球最大的企业级软件公司,总部位于美国加利福尼亚州的红木滩。1989 年正式进入中国市场。2013 年,甲骨文已超越 IBM,成为继 Microsoft 后全球第二大软件公司。

    107 引用 • 127 回帖 • 329 关注
  • Laravel

    Laravel 是一套简洁、优雅的 PHP Web 开发框架。它采用 MVC 设计,是一款崇尚开发效率的全栈框架。

    19 引用 • 23 回帖 • 770 关注
  • Vditor

    Vditor 是一款浏览器端的 Markdown 编辑器,支持所见即所得、即时渲染(类似 Typora)和分屏预览模式。它使用 TypeScript 实现,支持原生 JavaScript、Vue、React 和 Angular。

    386 引用 • 1892 回帖
  • 旅游

    希望你我能在旅途中找到人生的下一站。

    105 引用 • 908 回帖
  • Dubbo

    Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是 [阿里巴巴] SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

    60 引用 • 82 回帖 • 636 关注
  • 阿里巴巴

    阿里巴巴网络技术有限公司(简称:阿里巴巴集团)是以曾担任英语教师的马云为首的 18 人,于 1999 年在中国杭州创立,他们相信互联网能够创造公平的竞争环境,让小企业通过创新与科技扩展业务,并在参与国内或全球市场竞争时处于更有利的位置。

    43 引用 • 221 回帖 • 11 关注
  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    23 引用 • 22 回帖
  • Folo

    Folo 是一个 RSS 阅读和信息聚合应用,整合多种内容源到统一时间线。

    项目地址:https://github.com/RSSNext/Folo

    1 引用 • 3 回帖 • 2 关注
  • jsoup

    jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址、HTML 文本内容。它提供了一套非常省力的 API,可通过 DOM,CSS 以及类似于 jQuery 的操作方法来取出和操作数据。

    6 引用 • 1 回帖 • 516 关注
  • 印象笔记
    3 引用 • 21 回帖 • 2 关注
  • CSS

    CSS(Cascading Style Sheet)“层叠样式表”是用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言。

    200 引用 • 545 回帖 • 2 关注
  • RemNote
    2 引用 • 16 回帖 • 39 关注
  • SEO

    发布对别人有帮助的原创内容是最好的 SEO 方式。

    36 引用 • 200 回帖 • 54 关注
  • Notion

    Notion - The all-in-one workspace for your notes, tasks, wikis, and databases.

    10 引用 • 80 回帖
  • Ruby

    Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发。在 Ruby 社区,松本也被称为马茨(Matz)。

    7 引用 • 31 回帖 • 298 关注
  • Netty

    Netty 是一个基于 NIO 的客户端-服务器编程框架,使用 Netty 可以让你快速、简单地开发出一个可维护、高性能的网络应用,例如实现了某种协议的客户、服务端应用。

    49 引用 • 33 回帖 • 63 关注
  • Mac

    Mac 是苹果公司自 1984 年起以“Macintosh”开始开发的个人消费型计算机,如:iMac、Mac mini、Macbook Air、Macbook Pro、Macbook、Mac Pro 等计算机。

    168 引用 • 598 回帖
  • frp

    frp 是一个可用于内网穿透的高性能的反向代理应用,支持 TCP、UDP、 HTTP 和 HTTPS 协议。

    17 引用 • 7 回帖 • 1 关注
  • C++

    C++ 是在 C 语言的基础上开发的一种通用编程语言,应用广泛。C++ 支持多种编程范式,面向对象编程、泛型编程和过程化编程。

    110 引用 • 153 回帖