This commit is contained in:
Vanessa 2022-06-16 20:23:19 +08:00
parent 4bf4c1228c
commit c4360840c2
6 changed files with 16 additions and 25 deletions

View file

@ -394,10 +394,6 @@
&:focus {
outline: none;
}
&--untitled {
color: var(--b3-theme-on-surface);
}
}
}

View file

@ -19,7 +19,7 @@ export const validateName = (name: string) => {
};
export const replaceFileName = (name: string) => {
return name.replace(/\r\n|\r|\n|\u2028|\u2029|\t|\//g, "").trim();
return name.replace(/\r\n|\r|\n|\u2028|\u2029|\t|\//g, "");
};
export const rename = (options: {
@ -130,7 +130,7 @@ export const newFileBySelect = (fileName: string, protyle: IProtyle) => {
const id = Lute.NewNodeID();
fetchPost("/api/filetree/createDoc", {
notebook: protyle.notebookId,
path: pathPosix().join(getDisplayName(protyle.path, false, true), id+ ".sy"),
path: pathPosix().join(getDisplayName(protyle.path, false, true), id + ".sy"),
title: fileName,
md: ""
}, () => {

View file

@ -25,6 +25,7 @@ import {setTitle} from "../../dialog/processSystem";
import {getNoContainerElement} from "../wysiwyg/getBlock";
import {commonHotkey} from "../wysiwyg/commonHotkey";
import {setPosition} from "../../util/setPosition";
import {code160to32} from "../util/code160to32";
export class Title {
public element: HTMLElement;
@ -228,22 +229,15 @@ export class Title {
private rename(protyle: IProtyle) {
if (!validateName(this.editElement.textContent)) {
this.editElement.textContent = replaceFileName(this.editElement.textContent);
return false;
}
if (this.editElement.textContent.trim() === "" || this.editElement.textContent.trim() === window.siyuan.languages.untitled) {
this.editElement.textContent = window.siyuan.languages.untitled;
this.editElement.classList.add("protyle-title__input--untitled");
} else {
this.editElement.classList.remove("protyle-title__input--untitled");
}
const fileName = replaceFileName(this.editElement.textContent);
fetchPost("/api/filetree/renameDoc", {
notebook: protyle.notebookId,
path: protyle.path,
title: fileName,
});
this.editElement.textContent = fileName;
this.setTitle(fileName)
}
private renderMenu(protyle: IProtyle, iconElement: Element, position: { x: number, y: number }) {
@ -333,6 +327,12 @@ ${window.siyuan.languages.createdAt} ${dayjs(response.data.ial.id.substr(0, 14))
});
}
public setTitle(title: string) {
if (code160to32(title) !== code160to32(this.editElement.textContent)) {
this.editElement.textContent = title === "Untitled" ? "" : title;
}
}
public render(protyle: IProtyle, refresh = false) {
if (this.editElement.getAttribute("data-render") === "true" && !refresh) {
return;
@ -344,12 +344,7 @@ ${window.siyuan.languages.createdAt} ${dayjs(response.data.ial.id.substr(0, 14))
protyle.background.render(response.data.ial, protyle.block.rootID);
protyle.wysiwyg.renderCustom(response.data.ial);
this.editElement.setAttribute("data-render", "true");
this.editElement.textContent = response.data.ial.title;
if (response.data.ial.title === window.siyuan.languages.untitled) {
this.editElement.classList.add("protyle-title__input--untitled");
} else {
this.editElement.classList.remove("protyle-title__input--untitled");
}
this.setTitle(response.data.ial.title);
let nodeAttrHTML = "";
if (response.data.ial.bookmark) {
nodeAttrHTML += `<div class="protyle-attr--bookmark">${Lute.EscapeHTMLStr(response.data.ial.bookmark)}</div>`;
@ -367,7 +362,7 @@ ${window.siyuan.languages.createdAt} ${dayjs(response.data.ial.id.substr(0, 14))
if (response.data.refCount !== 0) {
this.element.querySelector(".protyle-attr").insertAdjacentHTML("beforeend", `<div class="protyle-attr--refcount popover__block" data-defids='${JSON.stringify([protyle.block.rootID])}' data-id='${JSON.stringify(response.data.refIDs)}'>${response.data.refCount}</div>`);
}
// 存在设置新建文档名模板,不能使用 window.siyuan.languages.untitled 进行判断https://ld246.com/article/1649301009888
// 存在设置新建文档名模板,不能使用 Untitled 进行判断https://ld246.com/article/1649301009888
if (new Date().getTime() - dayjs(response.data.id.split("-")[0]).toDate().getTime() < 2000) {
const range = this.editElement.ownerDocument.createRange();
range.selectNodeContents(this.editElement);

View file

@ -493,10 +493,10 @@ ${unicode2Emoji(emoji.unicode, true)}</button>`;
fetchPost("/api/filetree/createDoc", {
notebook: protyle.notebookId,
path: pathPosix().join(getDisplayName(protyle.path, false, true), newSubDocId + ".sy"),
title: window.siyuan.languages.untitled,
title: "Untitled",
md: ""
}, () => {
insertHTML(genEmptyBlock(false, false, `<span data-type="block-ref" data-id="${newSubDocId}" data-subtype="d">${escapeHtml(window.siyuan.languages.untitled)}</span>`), protyle);
insertHTML(genEmptyBlock(false, false, `<span data-type="block-ref" data-id="${newSubDocId}" data-subtype="d">Untitled</span>`), protyle);
if (isMobile()) {
openMobileFileById(newSubDocId, true);
} else {

View file

@ -108,7 +108,7 @@ class Protyle {
this.protyle.model.parent.updateTitle(data.data.title);
}
if (this.protyle.options.render.title && this.protyle.block.parentID === data.data.id) {
this.protyle.title.editElement.textContent = data.data.title;
this.protyle.title.setTitle(data.data.title);
}
// update ref
this.protyle.wysiwyg.element.querySelectorAll(`[data-type="block-ref"][data-id="${data.data.id}"]`).forEach(item => {

View file

@ -54,7 +54,7 @@ export const newFile = (notebookId?: string, currentPath?: string, open?: boolea
fetchPost("/api/filetree/createDoc", {
notebook: notebookId,
path: pathPosix().join(getDisplayName(currentPath, false, true), id + ".sy"),
title: data.data.name || window.siyuan.languages.untitled,
title: data.data.name || "Untitled",
md: "",
}, () => {
if (open && !isMobile()) {