Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Support a context menu for document tags on desktop https://github.com/siyuan-note/siyuan/issues/14745

Daniel47a97520478e
diff --git a/app/src/constants.ts b/app/src/constants.ts
index cccb1138f5..be90f031bc 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -261,6 +261,7 @@ export abstract class Constants {
public static readonly MENU_TITLE = "titleMenu"; // 文档块菜单
public static readonly MENU_FROM_TITLE_PROTYLE = "title-protyle"; // 在 Protyle 触发的文档块菜单
public static readonly MENU_FROM_TITLE_BREADCRUMB = "title-breadcrumb"; // 在面包屑触发的文档块菜单
+ public static readonly MENU_DOC_TAG = "doc-tag"; // 文档标签菜单
public static readonly MENU_BREADCRUMB_MORE = "breadcrumbMore"; // 面包屑更多菜单
public static readonly MENU_BREADCRUMB_MOBILE_PATH = "breadcrumb-mobile-path"; // 移动端面包屑菜单
public static readonly MENU_BREADCRUMB_CHILDREN = "breadcrumb-children"; // 面包屑下级块菜单
diff --git a/app/src/protyle/header/Background.ts b/app/src/protyle/header/Background.ts
index 2202771d70..2ebf5fbe1f 100644
--- a/app/src/protyle/header/Background.ts
+++ b/app/src/protyle/header/Background.ts
@@ -17,6 +17,9 @@ import {previewImages} from "../preview/image";
import {Menu} from "../../plugin/Menu";
import {escapeHtml} from "../../util/escape";
import {fetchCoverData, getCategoryLabel} from "./coverData";
+/// #if !MOBILE
+import {openDocTagMenu} from "./openDocTagMenu";
+/// #endif
const bgs = [
"background:radial-gradient(black 3px, transparent 4px),radial-gradient(black 3px, transparent 4px),linear-gradient(#fff 4px, transparent 0),linear-gradient(45deg, transparent 74px, transparent 75px, #a4a4a4 75px, #a4a4a4 76px, transparent 77px, transparent 109px),linear-gradient(-45deg, transparent 75px, transparent 76px, #a4a4a4 76px, #a4a4a4 77px, transparent 78px, transparent 109px),#fff;background-size: 109px 109px, 109px 109px,100% 6px, 109px 109px, 109px 109px;background-position: 54px 55px, 0px 0px, 0px 0px, 0px 0px, 0px 0px;",
@@ -517,9 +520,35 @@ export class Background {
}
});
+ /// #if !MOBILE
+ this.tagsElement.addEventListener("contextmenu", (event: MouseEvent) => {
+ if (event.shiftKey || protyle.disabled) {
+ return;
+ }
+ const tagElement = (event.target as HTMLElement).closest(".b3-chip") as HTMLElement;
+ if (!tagElement || !this.tagsElement.contains(tagElement)) {
+ return;
+ }
+ event.preventDefault();
+ event.stopPropagation();
+ const tagName = tagElement.textContent.trim();
+ openDocTagMenu({
+ protyle,
+ tagElement,
+ position: {x: event.clientX, y: event.clientY},
+ update: (tag) => {
+ this.updateTag(protyle, tagName, tag);
+ },
+ remove: () => {
+ this.removeTagByName(protyle, tagName);
+ }
+ });
+ });
+ /// #endif
+
this.element.addEventListener("mousedown", (event: MouseEvent) => {
this.dragOccurred = false;
- if (protyle.disabled) return;
+ if (protyle.disabled || event.button !== 0) return;
const target = event.target as HTMLElement;
let chipElement = target.closest(".b3-chip") as HTMLElement;
@@ -645,19 +674,46 @@ export class Background {
}
private removeTag(protyle: IProtyle, cb?: () => void) {
+ this.updateTags(protyle, this.getTags(), cb);
+ }
+
+ private removeTagByName(protyle: IProtyle, tagName: string) {
+ this.updateTags(protyle, this.getTags().filter((tag) => tag !== tagName));
+ }
+
+ private updateTag(protyle: IProtyle, oldTag: string, newTag: string) {
+ if (oldTag === newTag) {
+ return;
+ }
const tags = this.getTags();
+ const index = tags.indexOf(oldTag);
+ if (index === -1) {
+ return;
+ }
+ if (newTag) {
+ tags[index] = newTag;
+ } else {
+ tags.splice(index, 1);
+ }
+ this.updateTags(protyle, Array.from(new Set(tags)));
+ }
+
+ private updateTags(protyle: IProtyle, tags: string[], cb?: () => void) {
+ const tagsString = tags.toString();
+ if (tagsString === (this.ial.tags || "")) {
+ cb?.();
+ return;
+ }
fetchPost("/api/attr/setBlockAttrs", {
id: protyle.block.rootID,
- attrs: {"tags": tags.toString()}
+ attrs: {"tags": tagsString}
}, () => {
- if (cb) {
- cb();
- }
+ cb?.();
});
if (tags.length === 0) {
delete this.ial.tags;
} else {
- this.ial.tags = tags.toString();
+ this.ial.tags = tagsString;
}
this.render(this.ial, protyle.block.rootID);
}
diff --git a/app/src/protyle/header/openDocTagMenu.ts b/app/src/protyle/header/openDocTagMenu.ts
new file mode 100644
index 0000000000..e815ea78b9
--- /dev/null
+++ b/app/src/protyle/header/openDocTagMenu.ts
@@ -0,0 +1,147 @@
+import {MenuItem} from "../../menus/Menu";
+import {Constants} from "../../constants";
+import {genTagList, renameTag} from "../../util/noRelyPCFunction";
+import {upDownHint} from "../../util/upDownHint";
+import {setPosition} from "../../util/setPosition";
+import {encodeBase64, writeText} from "../util/compatibility";
+import {removeZWJ} from "../util/normalizeText";
+import {hasClosestByClassName} from "../util/hasClosest";
+import {openGlobalSearch} from "../../search/util";
+
+interface IOpenDocTagMenuOptions {
+ protyle: IProtyle;
+ tagElement: HTMLElement;
+ position: IPosition;
+ update: (tag: string) => void;
+ remove: () => void;
+}
+
+const copyTag = async (protyle: IProtyle, tag: string) => {
+ const textSiyuan = `<span data-type="tag">${Constants.ZWSP}${Lute.EscapeHTMLStr(tag)}</span>`;
+ const textHTML = `<!--data-siyuan='${encodeBase64(textSiyuan)}'-->${removeZWJ(protyle.lute.BlockDOM2HTML(textSiyuan))}`;
+ try {
+ await navigator.clipboard.write([
+ new ClipboardItem({
+ "text/plain": new Blob([tag], {type: "text/plain"}),
+ "text/html": new Blob([textHTML], {type: "text/html"}),
+ })
+ ]);
+ } catch (e) {
+ writeText(tag);
+ }
+};
+
+export const openDocTagMenu = (options: IOpenDocTagMenuOptions) => {
+ window.siyuan.menus.menu.remove();
+ const tagName = options.tagElement.textContent.trim();
+ let inputElement: HTMLInputElement;
+ let skipUpdate = false;
+ window.siyuan.menus.menu.removeCB = () => {
+ if (!skipUpdate) {
+ options.update(inputElement.value.trim());
+ }
+ };
+ window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_DOC_TAG);
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "tag",
+ iconHTML: "",
+ type: "readonly",
+ label: `<input ${Constants.ATTRIBUTE_MENU_KEYMAP}="true" class="b3-text-field fn__block" style="margin: 4px 0" placeholder="${window.siyuan.languages.tag}">
+<div class="fn__none b3-list fn__flex-1 b3-list--background protyle-hint" style="position: fixed"></div>`,
+ bind(element) {
+ const listElement = element.querySelector(".b3-list") as HTMLElement;
+ inputElement = element.querySelector("input");
+ inputElement.value = tagName;
+ const renderTagList = () => {
+ genTagList(listElement, inputElement.value.trim());
+ const inputRect = inputElement.getBoundingClientRect();
+ setPosition(listElement, inputRect.right + 8, inputRect.top, inputRect.height);
+ };
+ inputElement.addEventListener("compositionend", renderTagList);
+ inputElement.addEventListener("input", (event: InputEvent) => {
+ event.stopPropagation();
+ if (!event.isComposing) {
+ renderTagList();
+ }
+ });
+ inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (!listElement.classList.contains("fn__none")) {
+ upDownHint(listElement, event);
+ if (event.key === "Enter" || event.key === "Escape") {
+ listElement.classList.add("fn__none");
+ }
+ if (event.key === "Enter") {
+ const currentElement = listElement.querySelector(".b3-list-item--focus") as HTMLElement;
+ if (currentElement) {
+ inputElement.value = currentElement.dataset.type === "new" ?
+ currentElement.querySelector("mark").textContent.trim() : currentElement.textContent.trim();
+ }
+ }
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
+ if (event.key === "Enter") {
+ window.siyuan.menus.menu.remove();
+ event.preventDefault();
+ event.stopPropagation();
+ } else if (event.key === "Escape") {
+ window.siyuan.menus.menu.removeCB = null;
+ }
+ });
+ listElement.addEventListener("click", (event) => {
+ const listItemElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item");
+ if (!listItemElement) {
+ return;
+ }
+ inputElement.value = listItemElement.dataset.type === "new" ?
+ listItemElement.querySelector("mark").textContent.trim() : listItemElement.textContent.trim();
+ listElement.classList.add("fn__none");
+ inputElement.focus();
+ });
+ }
+ }).element);
+ window.siyuan.menus.menu.append(new MenuItem({id: "separator_1", type: "separator"}).element);
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "search",
+ label: window.siyuan.languages.search,
+ accelerator: window.siyuan.languages.click,
+ icon: "iconSearch",
+ click() {
+ openGlobalSearch(options.protyle.app, `#${tagName}#`, false, {method: 0});
+ }
+ }).element);
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "rename",
+ label: window.siyuan.languages.rename,
+ icon: "iconEdit",
+ click() {
+ window.siyuan.menus.menu.remove();
+ renameTag(tagName);
+ }
+ }).element);
+ window.siyuan.menus.menu.append(new MenuItem({id: "separator_2", type: "separator"}).element);
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "copy",
+ label: window.siyuan.languages.copy,
+ icon: "iconCopy",
+ click() {
+ void copyTag(options.protyle, tagName);
+ }
+ }).element);
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "remove",
+ label: window.siyuan.languages.remove,
+ icon: "iconTrashcan",
+ click() {
+ skipUpdate = true;
+ options.remove();
+ }
+ }).element);
+ window.siyuan.menus.menu.popup(options.position);
+ inputElement.focus();
+ inputElement.select();
+};