A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1import {fetchPost} from "../../util/fetch";
2import {escapeHtml} from "../../util/escape";
3import {openCard} from "../../card/openCard";
4import {getDockByType} from "../../layout/tabUtil";
5import {getAllTabs} from "../../layout/getAll";
6import {App} from "../../index";
7import {Constants} from "../../constants";
8import {matchHotKey} from "../../protyle/util/hotKey";
9import {isWindow} from "../../util/functions";
10
11export const windowKeyUp = (app: App, event: KeyboardEvent) => {
12 window.siyuan.ctrlIsPressed = false;
13 window.siyuan.shiftIsPressed = false;
14 window.siyuan.altIsPressed = false;
15 const switchDialog = window.siyuan.dialogs.find(item => {
16 if (item.element.getAttribute("data-key") === Constants.DIALOG_SWITCHTAB) {
17 return true;
18 }
19 });
20 if (switchDialog && switchDialog.element.parentElement) {
21 if (window.siyuan.config.keymap.general.goToEditTabNext.custom.endsWith(Constants.KEYCODELIST[event.keyCode]) ||
22 window.siyuan.config.keymap.general.goToEditTabPrev.custom.endsWith(Constants.KEYCODELIST[event.keyCode])) {
23 let currentLiElement = switchDialog.element.querySelector(".b3-list-item--focus");
24 currentLiElement.classList.remove("b3-list-item--focus");
25 if (matchHotKey(window.siyuan.config.keymap.general.goToEditTabPrev.custom, event)) {
26 while (true) {
27 if (currentLiElement.previousElementSibling) {
28 currentLiElement = currentLiElement.previousElementSibling;
29 } else if (currentLiElement.getAttribute("data-original")) {
30 currentLiElement.removeAttribute("data-original");
31 currentLiElement = currentLiElement.parentElement.lastElementChild;
32 } else if (currentLiElement.parentElement.nextElementSibling) {
33 if (currentLiElement.parentElement.nextElementSibling.lastElementChild) {
34 currentLiElement = currentLiElement.parentElement.nextElementSibling.lastElementChild;
35 } else {
36 currentLiElement = currentLiElement.parentElement.lastElementChild;
37 }
38 } else if (currentLiElement.parentElement.previousElementSibling) {
39 currentLiElement = currentLiElement.parentElement.previousElementSibling.lastElementChild;
40 } else if (isWindow()) {
41 currentLiElement = currentLiElement.parentElement.lastElementChild;
42 }
43 if (currentLiElement.getBoundingClientRect().height !== 0) {
44 break;
45 }
46 }
47 currentLiElement.classList.add("b3-list-item--focus");
48 } else {
49 while (true) {
50 if (currentLiElement.nextElementSibling) {
51 currentLiElement = currentLiElement.nextElementSibling;
52 } else if (currentLiElement.getAttribute("data-original")) {
53 currentLiElement.removeAttribute("data-original");
54 currentLiElement = currentLiElement.parentElement.firstElementChild;
55 } else if (currentLiElement.parentElement.nextElementSibling) {
56 if (currentLiElement.parentElement.nextElementSibling.firstElementChild) {
57 currentLiElement = currentLiElement.parentElement.nextElementSibling.firstElementChild;
58 } else {
59 currentLiElement = currentLiElement.parentElement.firstElementChild;
60 }
61 } else if (currentLiElement.parentElement.previousElementSibling) {
62 currentLiElement = currentLiElement.parentElement.previousElementSibling.firstElementChild;
63 } else if (isWindow()) {
64 currentLiElement = currentLiElement.parentElement.firstElementChild;
65 }
66 if (currentLiElement.getBoundingClientRect().height !== 0) {
67 break;
68 }
69 }
70 currentLiElement.classList.add("b3-list-item--focus");
71 }
72 if (currentLiElement) {
73 const rootId = currentLiElement.getAttribute("data-node-id");
74 if (rootId) {
75 fetchPost("/api/filetree/getFullHPathByID", {
76 id: rootId
77 }, (response) => {
78 currentLiElement.parentElement.parentElement.nextElementSibling.innerHTML = escapeHtml(response.data);
79 });
80 } else {
81 currentLiElement.parentElement.parentElement.nextElementSibling.innerHTML = currentLiElement.querySelector(".b3-list-item__text").innerHTML;
82 }
83 const currentRect = currentLiElement.getBoundingClientRect();
84 const currentParentRect = currentLiElement.parentElement.getBoundingClientRect();
85 if (currentRect.top < currentParentRect.top) {
86 currentLiElement.scrollIntoView(true);
87 } else if (currentRect.bottom > currentParentRect.bottom) {
88 currentLiElement.scrollIntoView(false);
89 }
90 }
91 const originalElement = switchDialog.element.querySelector('[data-original="true"]');
92 if (originalElement) {
93 originalElement.removeAttribute("data-original");
94 }
95 } else if (window.siyuan.config.keymap.general.goToEditTabNext.custom.startsWith(Constants.KEYCODELIST[event.keyCode]) ||
96 window.siyuan.config.keymap.general.goToEditTabPrev.custom.startsWith(Constants.KEYCODELIST[event.keyCode])) {
97 let currentLiElement = switchDialog.element.querySelector(".b3-list-item--focus");
98 // 快速切换时,不触发 Tab
99 if (currentLiElement.getAttribute("data-original")) {
100 currentLiElement.classList.remove("b3-list-item--focus");
101 if (matchHotKey(window.siyuan.config.keymap.general.goToEditTabPrev.custom, event)) {
102 // 上一个
103 if (currentLiElement.previousElementSibling) {
104 currentLiElement.previousElementSibling.classList.add("b3-list-item--focus");
105 } else {
106 currentLiElement.parentElement.lastElementChild.classList.add("b3-list-item--focus");
107 currentLiElement.removeAttribute("data-original");
108 }
109 } else {
110 if (currentLiElement.nextElementSibling) {
111 currentLiElement.nextElementSibling.classList.add("b3-list-item--focus");
112 } else {
113 currentLiElement.parentElement.firstElementChild.classList.add("b3-list-item--focus");
114 }
115 }
116 currentLiElement.removeAttribute("data-original");
117 currentLiElement = switchDialog.element.querySelector(".b3-list-item--focus");
118 }
119 const currentType = currentLiElement.getAttribute("data-type");
120 if (currentType) {
121 if (currentType === "riffCard") {
122 openCard(app);
123 } else {
124 getDockByType(currentType).toggleModel(currentType, true);
125 }
126 if (document.activeElement) {
127 (document.activeElement as HTMLElement).blur();
128 }
129 } else {
130 const currentId = currentLiElement.getAttribute("data-id");
131 getAllTabs().find(item => {
132 if (item.id === currentId) {
133 item.parent.switchTab(item.headElement);
134 item.parent.showHeading();
135 return true;
136 }
137 });
138 }
139 switchDialog.destroy();
140 }
141 }
142};