Skip to content

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

:sparkles: Support entering document content from mobile titles https://github.com/siyuan-note/siyuan/issues/19099

Daniel8c4d02a36ebe
diff --git a/app/src/assets/template/mobile/index.tpl b/app/src/assets/template/mobile/index.tpl
index ce0ba588b0..40b626f71e 100644
--- a/app/src/assets/template/mobile/index.tpl
+++ b/app/src/assets/template/mobile/index.tpl
@@ -13,7 +13,7 @@
</div>
<div class="mobile-safe-area"></div>
<div id="mobileTopBar" class="toolbar toolbar--border mobile-topbar">
- <input class="toolbar__title fn__hidden" id="toolbarName" autocomplete="off">
+ <input class="toolbar__title fn__hidden" id="toolbarName" autocomplete="off" enterkeyhint="next">
<span class="toolbar__title toolbar__title--readonly fn__hidden fn__none" id="toolbarNameReadonly"></span>
<button id="toolbarSync" class="toolbar__button toolbar__icon-deactivate fn__none" type="button">
<svg><use xlink:href="#iconCloudSucc"></use></svg>
diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts
index f587572889..d4b832b026 100644
--- a/app/src/mobile/util/initFramework.ts
+++ b/app/src/mobile/util/initFramework.ts
@@ -45,6 +45,7 @@ import {
type IMobilePluginDockEntry,
} from "../dock/pluginDockState";
import {exitSiYuan} from "../../dialog/processSystem";
+import {enterDocumentFromTitle} from "../../protyle/header/titleEnter";
const getDockTabElement = (type: string) => {
return document.querySelector(`[data-type="${CSS.escape(`sidebar-${type}-tab`)}"]`) as HTMLElement;
@@ -411,21 +412,43 @@ export const initFramework = async (app: App, isStart: boolean) => {
const initEditorName = () => {
const inputElement = document.getElementById("toolbarName") as HTMLInputElement;
- inputElement.setAttribute("placeholder", window.siyuan.languages._kernel[16]);
- inputElement.addEventListener("blur", () => {
+ let titleSavePromise: Promise<unknown> = Promise.resolve();
+ const saveTitle = () => {
if (inputElement.getAttribute("readonly") === "readonly") {
- return;
+ return titleSavePromise;
}
if (!validateName(inputElement.value)) {
inputElement.value = inputElement.value.substring(0, Constants.SIZE_TITLE);
- return false;
+ return titleSavePromise;
}
- fetchPost("/api/filetree/renameDoc", {
+ titleSavePromise = fetchPost("/api/filetree/renameDoc", {
notebook: window.siyuan.mobile.editor.protyle.notebookId,
path: window.siyuan.mobile.editor.protyle.path,
title: inputElement.value,
});
setTitle(inputElement.value);
+ return titleSavePromise;
+ };
+ inputElement.setAttribute("placeholder", window.siyuan.languages._kernel[16]);
+ inputElement.addEventListener("blur", () => {
+ void saveTitle();
+ });
+ inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
+ if (event.isComposing || event.key !== "Enter") {
+ return;
+ }
+ const protyle = window.siyuan.mobile.editor?.protyle;
+ if (!protyle || protyle.disabled || inputElement.readOnly) {
+ return;
+ }
+ const rootID = protyle.block.rootID;
+ event.preventDefault();
+ event.stopPropagation();
+ inputElement.blur();
+ enterDocumentFromTitle(protyle, {
+ beforeLoad: titleSavePromise,
+ isValid: () => window.siyuan.mobile.editor?.protyle === protyle && protyle.block.rootID === rootID,
+ });
});
};
diff --git a/app/src/protyle/header/Title.ts b/app/src/protyle/header/Title.ts
index 0140bd057d..38d4a91090 100644
--- a/app/src/protyle/header/Title.ts
+++ b/app/src/protyle/header/Title.ts
@@ -2,7 +2,6 @@ import {
focusBlock,
focusByOffset,
focusByRange,
- focusByWbr,
getEditorRange,
getSelectionOffset,
} from "../util/selection";
@@ -18,11 +17,9 @@ import * as dayjs from "dayjs";
import {openFileById} from "../../editor/util";
/// #endif
import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";
-import {getContenteditableElement, getNoContainerElement} from "../wysiwyg/getBlock";
+import {getNoContainerElement} from "../wysiwyg/getBlock";
import {commonHotkey} from "../wysiwyg/commonHotkey";
import {nbsp2space} from "../util/normalizeText";
-import {genEmptyElement} from "../../block/util";
-import {transaction} from "../wysiwyg/transaction";
import {hideTooltip} from "../../dialog/tooltip";
import {commonClick} from "../wysiwyg/commonClick";
import {openTitleMenu} from "./openTitleMenu";
@@ -32,6 +29,7 @@ import {addSpellcheckMenuItems, requestSpellcheckContext} from "../../menus/spel
import {focusAVByArrow} from "../render/av/focus";
import {getParentDocumentID} from "../util/parentDocument";
import {getTextSiyuanFromClipboardData} from "../util/clipboardData";
+import {enterDocumentFromTitle} from "./titleEnter";
export class Title {
public element: HTMLElement;
@@ -164,27 +162,7 @@ export class Title {
event.stopPropagation();
}
} else if (event.key === "Enter") {
- const firstElement = protyle.wysiwyg.element.firstElementChild;
- const editElement = getContenteditableElement(firstElement);
- if (editElement && editElement.textContent === "" && editElement.getAttribute("placeholder") ||
- firstElement.classList.contains("li")) {
- // 配合提示文本使用,避免提示文本挤压到第二个块中
- focusBlock(firstElement, protyle.wysiwyg.element);
- } else {
- const newId = Lute.NewNodeID();
- const newElement = genEmptyElement(false, true, newId);
- protyle.wysiwyg.element.insertAdjacentElement("afterbegin", newElement);
- focusByWbr(newElement, protyle.toolbar.range || getEditorRange(newElement));
- transaction(protyle, [{
- action: "insert",
- data: newElement.outerHTML,
- id: newId,
- parentID: protyle.block.parentID
- }], [{
- action: "delete",
- id: newId,
- }]);
- }
+ enterDocumentFromTitle(protyle);
event.preventDefault();
event.stopPropagation();
} else if (matchHotKey(window.siyuan.config.keymap.editor.general.attr.custom, event)) {
diff --git a/app/src/protyle/header/titleEnter.ts b/app/src/protyle/header/titleEnter.ts
new file mode 100644
index 0000000000..0e4758a94f
--- /dev/null
+++ b/app/src/protyle/header/titleEnter.ts
@@ -0,0 +1,110 @@
+import {getTitleEnterAction} from "./titleEnterCore";
+import {isDocumentBoundaryLoaded} from "../util/documentRange";
+import {getContenteditableElement} from "../wysiwyg/getBlock";
+import {focusBlock, focusByWbr, getEditorRange} from "../util/selection";
+import {genEmptyElement} from "../../block/util";
+import {transaction} from "../wysiwyg/transaction";
+import {fetchPost} from "../../util/fetch";
+import {isEncryptedBox} from "../../util/pathName";
+import {onGet} from "../util/onGet";
+/// #if MOBILE
+import {pauseMobileBarsScroll} from "../../mobile/util/mobileBars";
+/// #endif
+
+const enterLoadedDocumentStart = (protyle: IProtyle) => {
+ const firstElement = protyle.wysiwyg.element.firstElementChild;
+ if (!firstElement) {
+ return;
+ }
+ const editElement = getContenteditableElement(firstElement);
+ const action = getTitleEnterAction({
+ documentStartLoaded: true,
+ hasFirstBlock: true,
+ firstBlockIsList: firstElement.classList.contains("li"),
+ firstEditableIsEmpty: editElement?.textContent === "",
+ firstEditableHasPlaceholder: Boolean(editElement?.getAttribute("placeholder")),
+ });
+ /// #if MOBILE
+ pauseMobileBarsScroll();
+ /// #endif
+ protyle.contentElement.scrollTop = 0;
+ protyle.scroll.lastScrollTop = 1;
+ if (action === "focus") {
+ // 配合提示文本使用,避免提示文本挤压到第二个块中
+ focusBlock(firstElement, protyle.wysiwyg.element);
+ return;
+ }
+
+ const newId = Lute.NewNodeID();
+ const newElement = genEmptyElement(false, true, newId);
+ protyle.wysiwyg.element.insertAdjacentElement("afterbegin", newElement);
+ focusByWbr(newElement, protyle.toolbar.range || getEditorRange(newElement));
+ transaction(protyle, [{
+ action: "insert",
+ data: newElement.outerHTML,
+ id: newId,
+ parentID: protyle.block.parentID,
+ }], [{
+ action: "delete",
+ id: newId,
+ }]);
+};
+
+export const enterDocumentFromTitle = (protyle: IProtyle, options?: {
+ beforeLoad?: Promise<unknown>,
+ isValid?: () => boolean,
+}) => {
+ const isValid = options?.isValid || (() => true);
+ if (!isValid()) {
+ return;
+ }
+ const firstElement = protyle.wysiwyg.element.firstElementChild;
+ const editElement = firstElement ? getContenteditableElement(firstElement) : undefined;
+ const action = getTitleEnterAction({
+ documentStartLoaded: isDocumentBoundaryLoaded(protyle.wysiwyg.element, "before"),
+ hasFirstBlock: Boolean(firstElement),
+ firstBlockIsList: firstElement?.classList.contains("li") || false,
+ firstEditableIsEmpty: editElement?.textContent === "",
+ firstEditableHasPlaceholder: Boolean(editElement?.getAttribute("placeholder")),
+ });
+ if (action !== "load") {
+ enterLoadedDocumentStart(protyle);
+ return;
+ }
+
+ const rootID = protyle.block.rootID;
+ const loadDocumentStart = () => {
+ if (!isValid() || protyle.block.rootID !== rootID) {
+ return;
+ }
+ const getDocParam: IObject = {
+ id: rootID,
+ mode: 0,
+ size: window.siyuan.config.editor.dynamicLoadBlocks,
+ };
+ if (isEncryptedBox(protyle.notebookId)) {
+ getDocParam.notebook = protyle.notebookId;
+ }
+ fetchPost("/api/filetree/getDoc", getDocParam, (response) => {
+ if (!isValid() || protyle.block.rootID !== rootID) {
+ return;
+ }
+ onGet({
+ data: response,
+ protyle,
+ action: [],
+ isValid: () => isValid() && protyle.block.rootID === rootID,
+ afterCB() {
+ if (isValid() && protyle.block.rootID === rootID) {
+ enterLoadedDocumentStart(protyle);
+ }
+ },
+ });
+ });
+ };
+ if (options?.beforeLoad) {
+ void options.beforeLoad.then(loadDocumentStart, loadDocumentStart);
+ } else {
+ loadDocumentStart();
+ }
+};
diff --git a/app/src/protyle/header/titleEnterCore.test.ts b/app/src/protyle/header/titleEnterCore.test.ts
new file mode 100644
index 0000000000..ebdbb40ef5
--- /dev/null
+++ b/app/src/protyle/header/titleEnterCore.test.ts
@@ -0,0 +1,34 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getTitleEnterAction} from "./titleEnterCore";
+
+const getAction = (options: Partial<Parameters<typeof getTitleEnterAction>[0]> = {}) => getTitleEnterAction({
+ documentStartLoaded: true,
+ hasFirstBlock: true,
+ firstBlockIsList: false,
+ firstEditableIsEmpty: false,
+ firstEditableHasPlaceholder: false,
+ ...options,
+});
+
+describe("getTitleEnterAction", () => {
+ it("loads the document start when the current range begins later", () => {
+ assert.equal(getAction({documentStartLoaded: false}), "load");
+ });
+
+ it("loads the document start when no first block is available", () => {
+ assert.equal(getAction({hasFirstBlock: false}), "load");
+ });
+
+ it("focuses an empty first block with a placeholder", () => {
+ assert.equal(getAction({firstEditableIsEmpty: true, firstEditableHasPlaceholder: true}), "focus");
+ });
+
+ it("focuses a list first block", () => {
+ assert.equal(getAction({firstBlockIsList: true}), "focus");
+ });
+
+ it("inserts a paragraph before existing content", () => {
+ assert.equal(getAction(), "insert");
+ });
+});
diff --git a/app/src/protyle/header/titleEnterCore.ts b/app/src/protyle/header/titleEnterCore.ts
new file mode 100644
index 0000000000..9f88bf4e75
--- /dev/null
+++ b/app/src/protyle/header/titleEnterCore.ts
@@ -0,0 +1,18 @@
+export type TTitleEnterAction = "load" | "focus" | "insert";
+
+export const getTitleEnterAction = (options: {
+ documentStartLoaded: boolean,
+ hasFirstBlock: boolean,
+ firstBlockIsList: boolean,
+ firstEditableIsEmpty: boolean,
+ firstEditableHasPlaceholder: boolean,
+}): TTitleEnterAction => {
+ if (!options.documentStartLoaded || !options.hasFirstBlock) {
+ return "load";
+ }
+ if (options.firstBlockIsList ||
+ (options.firstEditableIsEmpty && options.firstEditableHasPlaceholder)) {
+ return "focus";
+ }
+ return "insert";
+};