forked from siyuan-note/siyuan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTab.ts
More file actions
247 lines (236 loc) · 11.1 KB
/
Copy pathTab.ts
File metadata and controls
247 lines (236 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import {Wnd} from "./Wnd";
import {genUUID} from "../util/genID";
import type {Model} from "./Model";
import {Editor} from "../editor";
import {hasClosestByTag} from "../protyle/util/hasClosest";
import {Constants} from "../constants";
import {escapeHtml, escapeLessThans} from "../util/escape";
import {unicode2Emoji} from "../emoji";
import {fetchPost} from "../util/fetch";
import {hideTooltip, showTooltipIfPointerOver} from "../dialog/tooltip";
/// #if !BROWSER
import {openNewWindow} from "../window/openNewWindow";
import {ipcRenderer} from "electron";
/// #endif
import {layoutToJSON, saveLayout} from "./util";
import {setTitle} from "../util/processTitle";
export class Tab {
public parent: Wnd;
public id: string;
public headElement: HTMLElement;
public panelElement: HTMLElement;
public callback: (tab: Tab) => void;
public model: Model;
public title: string;
public icon: string;
public docIcon: string;
constructor(options: ITab) {
this.id = genUUID();
this.callback = options.callback;
if (options.title || options.icon) {
this.title = options.title;
this.icon = options.icon;
this.docIcon = options.docIcon;
this.headElement = document.createElement("li");
this.headElement.setAttribute("data-type", "tab-header");
this.headElement.setAttribute("draggable", "true");
this.headElement.setAttribute("data-id", this.id);
this.headElement.classList.add("item", "item--focus");
let iconHTML = "";
if (options.icon) {
iconHTML = `<svg class="item__graphic"><use xlink:href="#${options.icon}"></use></svg>`;
} else if (options.docIcon) {
iconHTML = `<span class="item__icon">${unicode2Emoji(options.docIcon)}</span>`;
}
this.headElement.innerHTML = `${iconHTML}<span class="item__text">${escapeHtml(options.title)}</span>
<span class="item__close"><svg><use xlink:href="#iconClose"></use></svg></span>`;
this.headElement.addEventListener("mouseenter", (event) => {
event.stopPropagation();
event.preventDefault();
const dragElement = Array.from(this.headElement.parentElement.childNodes).find((item: HTMLElement) => {
if (item.style?.opacity === "0.38") {
return true;
}
});
if (dragElement) {
hideTooltip();
return;
}
let id = "";
if (this.model instanceof Editor && this.model.editor?.protyle?.block?.rootID) {
id = (this.model as Editor).editor.protyle.block.rootID;
} else if (!this.model) {
const initData = JSON.parse(this.headElement.getAttribute("data-initdata") || "{}");
if (initData && initData.instance === "Editor") {
id = initData.blockId;
}
}
if (id) {
fetchPost("/api/filetree/getFullHPathByID", {
id
}, (response) => {
if (!this.headElement.getAttribute("aria-label")) {
showTooltipIfPointerOver(escapeLessThans(response.data), this.headElement);
}
this.headElement.setAttribute("aria-label", escapeLessThans(response.data));
});
} else {
this.headElement.setAttribute("aria-label", escapeLessThans(this.title));
}
});
this.headElement.addEventListener("dragstart", (event: DragEvent & { target: HTMLElement }) => {
window.getSelection().removeAllRanges();
hideTooltip();
const tabElement = hasClosestByTag(event.target, "LI");
if (tabElement) {
event.dataTransfer.setData("text/html", tabElement.outerHTML);
const modeJSON = {id: this.id};
layoutToJSON(this, modeJSON);
event.dataTransfer.setData(Constants.SIYUAN_DROP_TAB, JSON.stringify(modeJSON));
event.dataTransfer.dropEffect = "move";
tabElement.style.opacity = "0.38";
window.siyuan.dragElement = this.headElement;
}
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "removeRegionStyle"});
/// #endif
});
this.headElement.addEventListener("dragend", (event: DragEvent & { target: HTMLElement }) => {
const tabElement = hasClosestByTag(event.target, "LI");
if (tabElement) {
tabElement.style.opacity = "1";
}
/// #if !BROWSER
// 拖拽到屏幕外
setTimeout(() => {
if (document.body.contains(this.panelElement) &&
(event.clientX < 0 || event.clientY < 0 || event.clientX > window.innerWidth || event.clientY > window.innerHeight)) {
openNewWindow(this);
}
}, Constants.TIMEOUT_LOAD); // 等待主进程发送关闭消息
ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "rmDragStyle"});
/// #else
document.querySelectorAll(".layout-tab-bars--drag").forEach(item => {
item.classList.remove("layout-tab-bars--drag");
});
document.querySelectorAll(".layout-tab-bar li[data-clone='true']").forEach(tabItem => {
tabItem.remove();
});
/// #endif
window.siyuan.dragElement = undefined;
if (event.dataTransfer.dropEffect === "none") {
// 按 esc 取消的时候应该还原在 dragover 时交换的 tab
this.parent.children.forEach((item, index) => {
const currentElement = this.headElement.parentElement.children[index];
if (item.headElement !== currentElement) {
if (index === 0) {
this.headElement.parentElement.firstElementChild.before(item.headElement);
} else {
this.headElement.parentElement.children[index - 1].after(item.headElement);
}
}
});
}
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "addRegionStyle"});
/// #endif
});
}
this.panelElement = document.createElement("div");
this.panelElement.classList.add("fn__flex-1");
this.panelElement.innerHTML = options.panel || "";
this.panelElement.setAttribute("data-id", this.id);
}
public updateTitle(title: string) {
this.title = title;
this.headElement.querySelector(".item__text").innerHTML = escapeHtml(title);
if (document.querySelector(".layout__wnd--active .layout-tab-bar .item--focus")) {
if (this.headElement.closest(".layout__wnd--active")) {
setTitle(title);
}
} else if (this.headElement.classList.contains("item--focus")) {
setTitle(title);
}
}
public addModel(model: Model) {
this.model = model;
model.parent = this;
}
public pin() {
if (!this.headElement.previousElementSibling || (this.headElement.previousElementSibling && this.headElement.previousElementSibling.classList.contains("item--pin"))) {
// 如果是第一个,或者前一个是 pinned,则不处理
} else {
let tempTab: Tab;
let pinIndex = 0;
let lastHeadElement: Element;
this.parent.children.find((item, index) => {
if (item.headElement.classList.contains("item--pin")) {
pinIndex = index + 1;
lastHeadElement = item.headElement;
}
if (item.id === this.id) {
tempTab = this.parent.children.splice(index, 1)[0];
return true;
}
});
if (lastHeadElement) {
lastHeadElement.after(tempTab.headElement);
} else {
this.parent.children[0].headElement.before(tempTab.headElement);
}
this.parent.children.splice(pinIndex, 0, tempTab);
}
this.headElement.classList.add("item--pin");
if (this.docIcon || this.icon) {
this.headElement.querySelector(".item__text").classList.add("fn__none");
}
saveLayout();
}
public setDocIcon(icon: string) {
this.docIcon = icon;
if (this.docIcon) {
const iconElement = this.headElement.querySelector(".item__icon");
if (iconElement) {
iconElement.innerHTML = unicode2Emoji(icon);
} else {
this.headElement.querySelector(".item__text").insertAdjacentHTML("beforebegin", `<span class="item__icon">${unicode2Emoji(icon)}</span>`);
}
if (this.headElement.classList.contains("item--pin")) {
this.headElement.querySelector(".item__text").classList.add("fn__none");
}
} else {
// 添加图标后刷新界面,没有 icon
this.headElement.querySelector(".item__icon")?.remove();
this.headElement.querySelector(".item__text").classList.remove("fn__none");
}
}
public unpin() {
if (!this.headElement.nextElementSibling || (this.headElement.nextElementSibling && !this.headElement.nextElementSibling.classList.contains("item--pin"))) {
// 如果是最后一个,或者后一个是 unpinned,则不处理
} else {
let tempTab: Tab;
let pinIndex = 0;
let lastHeadElement: Element;
for (let index = 0; index < this.parent.children.length; index++) {
if (this.parent.children[index].id === this.id) {
tempTab = this.parent.children.splice(index, 1)[0];
index--;
}
if (index > -1 && this.parent.children[index].headElement.classList.contains("item--pin")) {
pinIndex = index + 1;
lastHeadElement = this.parent.children[index].headElement;
}
}
lastHeadElement.after(tempTab.headElement);
this.parent.children.splice(pinIndex, 0, tempTab);
}
this.headElement.classList.remove("item--pin");
if (this.docIcon || this.icon) {
this.headElement.querySelector(".item__text").classList.remove("fn__none");
}
saveLayout();
}
public close() {
this.parent.removeTab(this.id);
}
}