Vanessa 2025-04-10 11:34:20 +08:00
parent 20f49699ad
commit 9729b72a9e

View file

@ -419,11 +419,15 @@ export class Background {
});
}
private removeTag(protyle: IProtyle) {
private removeTag(protyle: IProtyle, cb?: () => void) {
const tags = this.getTags();
fetchPost("/api/attr/setBlockAttrs", {
id: protyle.block.rootID,
attrs: {"tags": tags.toString()}
}, () => {
if (cb) {
cb();
}
});
if (tags.length === 0) {
delete this.ial.tags;
@ -540,13 +544,10 @@ export class Background {
upDownHint(listElement, event);
if (event.key === "Enter") {
const currentElement = listElement.querySelector(".b3-list-item--focus");
if (currentElement) {
this.addTags(currentElement.textContent.trim(), protyle);
} else {
this.addTags(inputElement.value.trim(), protyle);
}
inputElement.value = "";
inputElement.dispatchEvent(new CustomEvent("input"));
this.addTags(currentElement ? currentElement.textContent.trim() : inputElement.value.trim(), protyle, () => {
inputElement.value = "";
inputElement.dispatchEvent(new CustomEvent("input"));
});
} else if (event.key === "Escape") {
window.siyuan.menus.menu.remove();
}
@ -554,7 +555,7 @@ export class Background {
inputElement.addEventListener("input", (event) => {
event.stopPropagation();
fetchPost("/api/search/searchTag", {
k: inputElement.value,
k: inputElement.value.trim(),
}, (response) => {
let searchHTML = "";
let hasKey = false;
@ -569,7 +570,7 @@ export class Background {
}
});
if (!hasKey && response.data.k) {
searchHTML = `<div class="b3-list-item b3-list-item--narrow${searchHTML ? "" : " b3-list-item--focus"}"><div class="fn__flex-1">${window.siyuan.languages.new} <mark>${escapeHtml(response.data.k)}</mark></div></div>` + searchHTML;
searchHTML = `<div data-type="new" class="b3-list-item b3-list-item--narrow${searchHTML ? "" : " b3-list-item--focus"}"><div class="fn__flex-1">${window.siyuan.languages.new} <mark>${escapeHtml(response.data.k)}</mark></div></div>` + searchHTML;
}
listElement.innerHTML = searchHTML;
});
@ -580,8 +581,11 @@ export class Background {
if (!listItemElement) {
return;
}
this.addTags(listItemElement.textContent.trim(), protyle);
inputElement.dispatchEvent(new CustomEvent("input"));
this.addTags(listItemElement.dataset.type === "new" ? listItemElement.querySelector("mark").textContent.trim() : listItemElement.textContent.trim(),
protyle, () => {
inputElement.value = "";
inputElement.dispatchEvent(new CustomEvent("input"));
});
});
}
});
@ -609,16 +613,18 @@ export class Background {
return tags;
}
private addTags(tag: string, protyle: IProtyle) {
private addTags(tag: string, protyle: IProtyle, cb: () => void) {
const tags = this.getTags(tag);
if (tags.includes(tag)) {
this.removeTag(protyle);
this.removeTag(protyle, cb);
return;
}
tags.push(tag);
fetchPost("/api/attr/setBlockAttrs", {
id: protyle.block.rootID,
attrs: {"tags": tags.toString()}
}, () => {
cb();
});
this.ial.tags = tags.toString();
this.render(this.ial, protyle.block.rootID);