Skip to content

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

:recycle: Reuse input dialogs for snapshot memos https://github.com/siyuan-note/siyuan/issues/19392

Daniel05f3205abc9b
diff --git a/app/src/dialog/inputDialog.ts b/app/src/dialog/inputDialog.ts
index c789383bea..09530d1c41 100644
--- a/app/src/dialog/inputDialog.ts
+++ b/app/src/dialog/inputDialog.ts
@@ -8,6 +8,7 @@ export const openInputDialog = (options: {
label?: string,
width?: string,
maxLength?: number,
+ multiline?: boolean,
type?: "text" | "number",
min?: string,
max?: string,
@@ -18,7 +19,8 @@ export const openInputDialog = (options: {
onConfirm: (value: string, dialog: Dialog) => void,
destroyCallback?: (options?: IObject) => void,
}): Dialog => {
- const inputHTML = '<input spellcheck="false" class="b3-text-field fn__block" value="">';
+ const inputHTML = options.multiline ? '<textarea spellcheck="false" class="b3-text-field fn__block"></textarea>' :
+ '<input spellcheck="false" class="b3-text-field fn__block" value="">';
const dialog = new Dialog({
title: options.title,
content: `<div class="b3-dialog__content">${options.label ? `<label>${escapeHtml(options.label)}<div class="fn__hr"></div>${inputHTML}</label>` : inputHTML}${options.description ? `<div class="b3-label__text">${options.description}</div>` : ""}</div>
@@ -29,19 +31,21 @@ export const openInputDialog = (options: {
width: options.width || (isMobile() ? "92vw" : "520px"),
destroyCallback: options.destroyCallback,
});
- const inputElement = dialog.element.querySelector("input") as HTMLInputElement;
+ const inputElement = dialog.element.querySelector<HTMLInputElement | HTMLTextAreaElement>("input, textarea");
const btnsElement = dialog.element.querySelectorAll<HTMLButtonElement>(".b3-button");
- if (options.type !== undefined) {
- inputElement.type = options.type;
- }
- if (options.min !== undefined) {
- inputElement.min = options.min;
- }
- if (options.max !== undefined) {
- inputElement.max = options.max;
- }
- if (options.step !== undefined) {
- inputElement.step = options.step;
+ if (inputElement instanceof HTMLInputElement) {
+ if (options.type !== undefined) {
+ inputElement.type = options.type;
+ }
+ if (options.min !== undefined) {
+ inputElement.min = options.min;
+ }
+ if (options.max !== undefined) {
+ inputElement.max = options.max;
+ }
+ if (options.step !== undefined) {
+ inputElement.step = options.step;
+ }
}
if (options.placeholder !== undefined) {
inputElement.placeholder = options.placeholder;
@@ -60,6 +64,10 @@ export const openInputDialog = (options: {
dialog.bindInput(inputElement, () => {
btnsElement[1].click();
});
- inputElement.select();
+ if (options.multiline) {
+ inputElement.focus();
+ } else {
+ inputElement.select();
+ }
return dialog;
};
diff --git a/app/src/history/history.ts b/app/src/history/history.ts
index d1e1ffaf31..927e43c671 100644
--- a/app/src/history/history.ts
+++ b/app/src/history/history.ts
@@ -1,4 +1,5 @@
import {Dialog} from "../dialog";
+import {openInputDialog} from "../dialog/inputDialog";
import {confirmDialog} from "../dialog/confirmDialog";
import {Constants} from "../constants";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
@@ -24,50 +25,41 @@ const repoHistoryEditors = new WeakMap<Element, Protyle>();
const snapshotMemos = new WeakMap<Element, Map<string, string>>();
const openSnapshotMemo = (repoElement: Element, id?: string, memo = "") => {
- const dialog = new Dialog({
+ const dialog = openInputDialog({
title: id ? window.siyuan.languages.editSnapshotMemo : window.siyuan.languages.snapshotMemo,
- content: `<div class="b3-dialog__content">
- ${id ? `<div class="ft__secondary">${window.siyuan.languages.snapshotMemoLocalTip}</div><div class="fn__hr"></div>` : ""}
- <textarea class="b3-text-field fn__block" placeholder="${escapeAttr(window.siyuan.languages.snapshotMemoTip)}"></textarea>
-</div>
-<div class="b3-dialog__action">
- <button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
- <button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
-</div>`,
- width: isMobile() ? "92vw" : "520px",
- });
- dialog.element.setAttribute("data-key", Constants.DIALOG_SNAPSHOTMEMO);
- const textarea = dialog.element.querySelector("textarea");
- textarea.value = memo;
- textarea.focus();
- const buttons = dialog.element.querySelectorAll("button");
- dialog.bindInput(textarea, () => buttons[1].click());
- buttons[0].addEventListener("click", () => dialog.destroy());
- buttons[1].addEventListener("click", async () => {
- if (buttons[1].disabled) {
- return;
- }
- buttons[1].disabled = true;
- textarea.readOnly = true;
- try {
- if (id) {
- await fetchPost("/api/repo/setSnapshotMemo", {id, memo: textarea.value}, () => {
- dialog.destroy();
- renderRepo(repoElement, parseInt(repoElement.getAttribute("data-page")) || 1);
- });
- } else {
- await fetchPost("/api/repo/createSnapshot", {memo: textarea.value}, (response) => {
- if (response.data.created) {
+ value: memo,
+ multiline: true,
+ placeholder: window.siyuan.languages.snapshotMemoTip,
+ description: id ? window.siyuan.languages.snapshotMemoLocalTip : undefined,
+ onConfirm: async (value, dialog) => {
+ const textarea = dialog.element.querySelector("textarea");
+ const button = dialog.element.querySelector<HTMLButtonElement>(".b3-button--text");
+ if (button.disabled) {
+ return;
+ }
+ button.disabled = true;
+ textarea.readOnly = true;
+ try {
+ if (id) {
+ await fetchPost("/api/repo/setSnapshotMemo", {id, memo: value}, () => {
dialog.destroy();
- renderRepo(repoElement, 1);
- }
- });
+ renderRepo(repoElement, parseInt(repoElement.getAttribute("data-page")) || 1);
+ });
+ } else {
+ await fetchPost("/api/repo/createSnapshot", {memo: value}, (response) => {
+ if (response.data.created) {
+ dialog.destroy();
+ renderRepo(repoElement, 1);
+ }
+ });
+ }
+ } finally {
+ button.disabled = false;
+ textarea.readOnly = false;
}
- } finally {
- buttons[1].disabled = false;
- textarea.readOnly = false;
- }
+ },
});
+ dialog.element.setAttribute("data-key", Constants.DIALOG_SNAPSHOTMEMO);
};
const renderDoc = (element: HTMLElement, currentPage: number) => {