(()=>{
let documentListened = false;
observeElementCreation(
document.body,
'.av__row:not(.av__row--header) [data-dtype="block"]:has([data-type="block-ref"])',
async cell => {
const av = cell.closest('[data-av-type="table"][data-av-id]');
const avId = av?.dataset.avId;
const text = cell.querySelector('.av__celltext--ref');
if(text) text.dataset.text = text.textContent;
let blockId;
if(avId) {
const block = cell.querySelector('[data-type="block-ref"]');
blockId = block?.dataset.id;
if(blockId){
let blockAttrs = {};
try {
blockAttrs = await fetchSyncPost('/api/attr/getBlockAttrs', {"id": blockId});
} catch(e) {
console.log(e);
}
if(blockAttrs.code === 0 && blockAttrs.data) {
blockAttrs = blockAttrs.data;
const customText = blockAttrs['custom-dbid-' + avId];
if(customText){
if(text) text.textContent = customText;
}
}
}
}
cell.addEventListener('click', (event) => {
if(event.target.tagName === 'SPAN' && event.target.classList.contains('av__celltext--ref')) {
return;
}
event.stopPropagation();
const selectCells = document.querySelectorAll('.av__cell.av__cell--active.av__cell--select');
selectCells.forEach(item => {
if(item !== cell) {
item.classList.remove('av__cell--active');
item.classList.remove('av__cell--select');
}
});
addAvMask(cell);
const inputElement = document.querySelector('.av__mask--rename .b3-text-field');
inputElement.select();
inputElement.focus();
inputElement.addEventListener('click', (event) => {
event.stopPropagation();
});
inputElement.addEventListener('keydown', (event) => {
if (event.isComposing) {
return;
}
if(event.key === "Escape"|| event.key === "Tab" || (event.key === "Enter" && !event.shiftKey && isNotCtrl(event))){
event.preventDefault();
event.stopPropagation();
updateCellValue(blockId, avId, inputElement, text, cell, av, event);
}
});
inputElement.parentElement.addEventListener('click', (event) => {
updateCellValue(blockId, avId, inputElement, text, cell, av);
});
});
cell.querySelector('[data-type="block-more"]')?.addEventListener('click', (event) => {
event.stopPropagation();
});
if(!documentListened) {
documentListened = true;
document.addEventListener('keydown', (event) => {
if (event.isComposing) {
return;
}
if (event.key === "Enter" && !event.shiftKey && isNotCtrl(event)) {
const inputElement = document.querySelector('.av__mask--rename .b3-text-field');
if(!inputElement) {
const selectCell = document.querySelector('.av__cell.av__cell--active.av__cell--select[data-dtype="block"]:has([data-type="block-ref"])');
if(selectCell) selectCell.click();
}
event.preventDefault();
event.stopPropagation();
}
});
}
}
);
async function updateCellValue(blockId, avId, inputElement, text, cell, av, event) {
if(blockId && avId && inputElement) {
let value = inputElement.value.trim();
await fetchSyncPost('/api/attr/setBlockAttrs', {
"id": blockId,
"attrs": {
["custom-dbid-" + avId]: value
}
});
text = text || cell.querySelector('.av__celltext--ref');
if(!value && text) {
value = text.dataset.text || '';
}
if(text) text.textContent = value;
inputElement.parentElement.remove();
cell.classList.add('av__cell--active');
av?.querySelector('.av__header .av__title')?.focus();
pressKeyboard({key: 'Escape', keyCode: 27}, document);
if(event?.key === "Tab"){
pressKeyboard({key: 'Tab', keyCode: 9});
}
}
}
function addAvMask(cell) {
let html = "";
let cellRect = cell.getBoundingClientRect();
let height = cellRect.height;
const contentElement = cell.closest('.protyle-content');
if (contentElement) {
const contentRect = contentElement.getBoundingClientRect();
if (cellRect.bottom > contentRect.bottom) {
height = contentRect.bottom - cellRect.top;
}
}
const style = `style="padding-top: 6.5px;position:absolute;left: ${cellRect.left}px;top: ${cellRect.top}px;width:${Math.max(cellRect.width, 25)}px;height: ${height}px"`;
html = `<textarea ${style} spellcheck="false" class="b3-text-field">${cell.firstElementChild.textContent}</textarea>`;
document.body.insertAdjacentHTML("beforeend", `<div class="av__mask av__mask--rename" style="z-index: ${++window.siyuan.zIndex}">${html}</div>`);
}
function isNotCtrl(event) {
if (!event.metaKey && !event.ctrlKey) {
return true;
}
return false;
}
function pressKeyboard(keyInit, element) {
element = element || getProtyleEl();
keyInit["bubbles"] = true;
let keydownEvent = new KeyboardEvent('keydown', keyInit);
element?.dispatchEvent(keydownEvent);
let keyUpEvent = new KeyboardEvent('keyup', keyInit);
element?.dispatchEvent(keyUpEvent);
}
function getProtyleEl() {
return document.querySelector('[data-type="wnd"].layout__wnd--active .protyle:not(.fn__none) .protyle-wysiwyg.protyle-wysiwyg--attr');
}
async function fetchSyncPost(url, data, returnType = 'json') {
const init = {
method: "POST",
};
if (data) {
if (data instanceof FormData) {
init.body = data;
} else {
init.body = JSON.stringify(data);
}
}
try {
const res = await fetch(url, init);
const res2 = returnType === 'json' ? await res.json() : await res.text();
return res2;
} catch(e) {
console.log(e);
return returnType === 'json' ? {code:e.code||1, msg: e.message||"", data: null} : "";
}
}
function observeElementCreation(parentNode, selector, onElementCreated) {
const config = {
childList: true,
subtree: true
};
const callback = function(mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE) {
const elements = node.querySelectorAll(selector);
elements.forEach(element => {
onElementCreated(element);
});
}
});
}
}
};
const observer = new MutationObserver(callback);
observer.observe(parentNode, config);
return () => observer.disconnect();
}
(function tongji(enable) {
if(!enable) return;
!function(p){"use strict";!function(t){var s=window,e=document,i=p,c="".concat("https:"===e.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=e.createElement("script"),r=e.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",i.d=n;var o=function(){s.LA.ids.push(i)};s.LA?s.LA.ids&&o():(s.LA=p,s.LA.ids=[],o()),r.parentNode.insertBefore(n,r)}()}({id:"KctUAV1SBR5tEp7H",ck:"KctUAV1SBR5tEp7H"});
})(1);
})()