(()=>{
addStyle(`
.layout__wnd--active .layout-tab-bar .item--pin--focus:after {
background-color: var(--b3-theme-primary);
}
.layout-tab-bar .item--pin--focus:after {
content: "";
width: 100%;
height: 3px;
border-radius: var(--b3-border-radius);
bottom: 0;
position: absolute;
background-color: var(--b3-theme-background-light);
}
`);
const isMovePinTabToFocusNextInRecentlyDialog = true;
const isMovePinTabToFocusNextInTabSwitchDialog = true;
whenElementExist('.layout__center').then(async element => {
let originalValues = {};
document.addEventListener('keydown', function(event) {
if ((event.ctrlKey || event.metaKey) && event.key === 'w') {
const focusPinTab = element.querySelector('.layout-tab-bar .item--pin.item--focus');
if(focusPinTab){
focusPinTab.classList.remove('item--focus');
focusPinTab.classList.add('item--pin--focus');
}
if(Object.keys(originalValues).length === 0) {
const items = element.querySelectorAll('.layout-tab-bar .item--pin');
let maxActivetimeItem = null;
let maxActivetime = 0;
items.forEach(item => {
originalValues[item.getAttribute("data-id")] = item.dataset.activetime;
if(item.dataset.activetime > maxActivetime){
maxActivetime = item.dataset.activetime;
maxActivetimeItem = item;
}
item.dataset.activetime = 0;
});
if(maxActivetimeItem) maxActivetimeItem.dataset.activetime = 1;
}
}
}, { capture: true });
document.addEventListener('keyup', function(event) {
const focusPinTab = element.querySelector('.layout-tab-bar .item--pin.item--pin--focus');
if(focusPinTab){
focusPinTab.classList.remove('item--pin--focus');
focusPinTab.classList.add('item--focus');
}
if(Object.keys(originalValues).length > 0) {
const items = element.querySelectorAll('.layout-tab-bar .item--pin');
items.forEach(item => {
if(item.dataset.activetime <= 1) item.dataset.activetime = originalValues[item.getAttribute("data-id")];
});
Object.keys(originalValues).forEach(key => {
delete originalValues[key];
});
}
}, true);
if(isMovePinTabToFocusNextInRecentlyDialog || isMovePinTabToFocusNextInTabSwitchDialog) {
observeDialogShow((node, dialog)=>{
if(dialog === 'dialog-recentdocs'){
const pinTabNodeIds = getAllPinTabNodeIds();
movePinTabItemToFocusTabNext(node, pinTabNodeIds);
}
if(dialog === 'dialog-switchtab'){
const pinTabNodeIds = getAllPinTabNodeIds();
movePinTabItemToFocusTabNext(node, pinTabNodeIds);
}
});
}
});
function getAllPinTabNodeIds() {
const pinTabs = document.querySelectorAll(".layout__center .layout-tab-bar li.item--pin");
let pinTabNodeIds = [];
pinTabs.forEach(pin => {
let initData = pin.getAttribute("data-initdata");
if(initData){
initData = JSON.parse(initData);
if(initData && initData.blockId) {
pinTabNodeIds.push(initData.blockId);
}
} else {
const dataId = pin.getAttribute("data-id");
const parents = pin.closest("div.fn__flex");
const nodeId = parents?.nextElementSibling?.querySelector('div[data-id="'+dataId+'"] .protyle-breadcrumb__item svg.popover__block')?.getAttribute("data-id");
if(nodeId) {
pinTabNodeIds.push(nodeId);
}
}
});
return pinTabNodeIds;
}
async function movePinTabItemToFocusTabNext(node, pinTabNodeIds) {
let ulElement = null;
let focusElement = null;
await whenElementExist(()=>{
ulElement = node.querySelector("ul.b3-list--background.fn__flex-1");
focusElement = ulElement?.querySelector("li.b3-list-item--focus");
return ulElement && focusElement;
});
pinTabNodeIds.reverse();
pinTabNodeIds.forEach(nodeId => {
const item = ulElement.querySelector(`li[data-node-id="${nodeId}"]`);
if (item) {
focusElement.parentElement.insertBefore(item, focusElement.nextSibling);
}
});
}
function observeDialogShow(callback) {
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
for (let node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE && node.tagName.toLowerCase() === 'div') {
if (node.hasAttribute('data-key') && node.getAttribute('data-key') === 'dialog-recentdocs') {
if(typeof callback === 'function') callback(node, 'dialog-recentdocs');
}
if (node.hasAttribute('data-key') && node.getAttribute('data-key') === 'dialog-switchtab') {
if(typeof callback === 'function') callback(node, 'dialog-switchtab');
}
}
}
}
}
});
const config = {
childList: true,
};
observer.observe(document.body, config);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function whenElementExist(selector) {
return new Promise(resolve => {
const checkForElement = () => {
let element = null;
if (typeof selector === 'function') {
element = selector();
} else {
element = document.querySelector(selector);
}
if (element) {
resolve(element);
} else {
requestAnimationFrame(checkForElement);
}
};
checkForElement();
});
}
function addStyle(styleContent) {
let styleTag = document.head.querySelector('ctrl-w-not-close-tab-style');
if (styleTag) {
styleTag.parentNode.removeChild(styleTag);
}
styleTag = document.createElement('style');
styleTag.textContent = styleContent;
document.head.appendChild(styleTag);
}
})();