This commit is contained in:
Vanessa 2022-10-22 17:37:41 +08:00
parent 6d2ff690b2
commit 0d5d63f22a
5 changed files with 14 additions and 2 deletions

View file

@ -9,17 +9,22 @@ import {isMobile} from "../util/functions";
import {getAssetName, getDisplayName, pathPosix, setNotebookName} from "../util/pathName"; import {getAssetName, getDisplayName, pathPosix, setNotebookName} from "../util/pathName";
import {fetchPost} from "../util/fetch"; import {fetchPost} from "../util/fetch";
import {escapeHtml} from "../util/escape"; import {escapeHtml} from "../util/escape";
import {Constants} from "../constants";
export const validateName = (name: string) => { export const validateName = (name: string) => {
if (/\r\n|\r|\n|\u2028|\u2029|\t|\//.test(name)) { if (/\r\n|\r|\n|\u2028|\u2029|\t|\//.test(name)) {
showMessage(window.siyuan.languages.fileNameRule); showMessage(window.siyuan.languages.fileNameRule);
return false; return false;
} }
if (name.length > Constants.SIZE_TITLE) {
showMessage(window.siyuan.languages["_kernel"]["106"]);
return false;
}
return true; return true;
}; };
export const replaceFileName = (name: string) => { export const replaceFileName = (name: string) => {
return name.replace(/\r\n|\r|\n|\u2028|\u2029|\t|\//g, ""); return name.replace(/\r\n|\r|\n|\u2028|\u2029|\t|\//g, "").substring(0, Constants.SIZE_TITLE);
}; };
export const replaceLocalPath = (name: string) => { export const replaceLocalPath = (name: string) => {

View file

@ -7,6 +7,7 @@ import {needSubscribe} from "../../util/needSubscribe";
import {MenuItem} from "../../menus/Menu"; import {MenuItem} from "../../menus/Menu";
import {hasClosestByAttribute, hasClosestByClassName} from "../../protyle/util/hasClosest"; import {hasClosestByAttribute, hasClosestByClassName} from "../../protyle/util/hasClosest";
import {confirmDialog} from "../../dialog/confirmDialog"; import {confirmDialog} from "../../dialog/confirmDialog";
import {replaceFileName} from "../../editor/rename";
export class Inbox extends Model { export class Inbox extends Model {
private element: Element; private element: Element;
@ -254,7 +255,7 @@ ${(Lute.New()).MarkdownStr("", data.shorthandContent)}
fetchPost("/api/filetree/createDoc", { fetchPost("/api/filetree/createDoc", {
notebook: notebookId, notebook: notebookId,
path: `/${Lute.NewNodeID()}.sy`, path: `/${Lute.NewNodeID()}.sy`,
title: this.data[item].shorthandTitle, title: replaceFileName(this.data[item].shorthandTitle),
md: this.data[item].shorthandContent, md: this.data[item].shorthandContent,
}, () => { }, () => {
this.remove(item); this.remove(item);

View file

@ -159,6 +159,7 @@ const initEditorName = () => {
return; return;
} }
if (!validateName(inputElement.value)) { if (!validateName(inputElement.value)) {
inputElement.value = inputElement.value.substring(0, Constants.SIZE_TITLE);
return false; return false;
} }

View file

@ -252,6 +252,7 @@ export class Title {
private rename(protyle: IProtyle) { private rename(protyle: IProtyle) {
clearTimeout(this.timeout); clearTimeout(this.timeout);
if (!validateName(this.editElement.textContent)) { if (!validateName(this.editElement.textContent)) {
this.setTitle(this.editElement.textContent.substring(0, Constants.SIZE_TITLE));
return false; return false;
} }
this.timeout = window.setTimeout(() => { this.timeout = window.setTimeout(() => {

View file

@ -9,6 +9,7 @@ import {openFileById} from "../editor/util";
import {fetchPost} from "./fetch"; import {fetchPost} from "./fetch";
import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName"; import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName";
import {Constants} from "../constants"; import {Constants} from "../constants";
import {validateName} from "../editor/rename";
export const newFile = (notebookId?: string, currentPath?: string, open?: boolean, paths?: string[]) => { export const newFile = (notebookId?: string, currentPath?: string, open?: boolean, paths?: string[]) => {
if (getOpenNotebookCount() === 0) { if (getOpenNotebookCount() === 0) {
@ -58,6 +59,9 @@ export const newFile = (notebookId?: string, currentPath?: string, open?: boolea
if (paths) { if (paths) {
paths[paths.indexOf(undefined)] = newPath; paths[paths.indexOf(undefined)] = newPath;
} }
if (!validateName(data.data.name)) {
return;
}
fetchPost("/api/filetree/createDoc", { fetchPost("/api/filetree/createDoc", {
notebook: notebookId, notebook: notebookId,
path: newPath, path: newPath,