This commit is contained in:
Liang Ding 2023-04-28 11:19:29 +08:00
parent 9cb6e9ec25
commit 1c47a63cca
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
4 changed files with 63 additions and 27 deletions

View file

@ -9,7 +9,7 @@ import {MenuItem} from "./Menu";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {saveExport} from "../protyle/export";
import {openByMobile, writeText} from "../protyle/util/compatibility";
import {fetchPost} from "../util/fetch";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {hideMessage, showMessage} from "../dialog/message";
import {Dialog} from "../dialog";
import {focusBlock, focusByRange, getEditorRange} from "../protyle/util/selection";
@ -19,7 +19,7 @@ import {getAllModels} from "../layout/getAll";
import {Bookmark} from "../layout/dock/Bookmark";
import {openAsset, openBy} from "../editor/util";
/// #endif
import {rename} from "../editor/rename";
import {rename, replaceFileName} from "../editor/rename";
import {matchHotKey} from "../protyle/util/hotKey";
import * as dayjs from "dayjs";
import {Constants} from "../constants";
@ -509,22 +509,60 @@ export const exportMd = (id: string) => {
submenu: [{
label: window.siyuan.languages.template,
icon: "iconMarkdown",
click: () => {
fetchPost("/api/template/docSaveAsTemplate", {
id,
overwrite: false
}, response => {
if (response.code === 1) {
// 重名
confirmDialog(window.siyuan.languages.export, window.siyuan.languages.exportTplTip, () => {
fetchPost("/api/template/docSaveAsTemplate", {
id,
overwrite: true
});
});
return;
click: async () => {
const result = await fetchSyncPost("/api/block/getRefText", {id: id});
const dialog = new Dialog({
title: window.siyuan.languages.fileName,
content: `<div class="b3-dialog__content"><input class="b3-text-field fn__block" value=""></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",
});
const inputElement = dialog.element.querySelector("input") as HTMLInputElement;
const btnsElement = dialog.element.querySelectorAll(".b3-button");
dialog.bindInput(inputElement, () => {
(btnsElement[1] as HTMLButtonElement).click();
});
inputElement.value = replaceFileName(result.data);
inputElement.focus();
inputElement.select();
btnsElement[0].addEventListener("click", () => {
dialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
if (inputElement.value.trim() === "") {
inputElement.value = "Untitled";
} else {
inputElement.value = replaceFileName(inputElement.value);
}
showMessage(window.siyuan.languages.exportTplSucc);
fetchPost("/api/template/docSaveAsTemplate", {
id,
name: inputElement.value,
overwrite: false
}, response => {
if (response.code === 1) {
// 重名
confirmDialog(window.siyuan.languages.export, window.siyuan.languages.exportTplTip, () => {
fetchPost("/api/template/docSaveAsTemplate", {
id,
name: inputElement.value,
overwrite: true
}, resp => {
if (resp.code === 0) {
showMessage(window.siyuan.languages.exportTplSucc);
}
});
});
return;
}
showMessage(window.siyuan.languages.exportTplSucc);
});
dialog.destroy();
});
}
}, {

View file

@ -54,8 +54,9 @@ func docSaveAsTemplate(c *gin.Context) {
}
id := arg["id"].(string)
name := arg["name"].(string)
overwrite := arg["overwrite"].(bool)
code, err := model.DocSaveAsTemplate(id, overwrite)
code, err := model.DocSaveAsTemplate(id, name, overwrite)
if nil != err {
ret.Code = -1
ret.Msg = util.EscapeHTML(err.Error())

View file

@ -8,8 +8,6 @@ github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5 h1:8HdZozCsXS
github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/88250/gulu v1.2.3-0.20230321113152-38c4a3d73c37 h1:7ZMvlXDbH2sqaxowoAFcLjcdRN7ncVE0U1S2ukpUgac=
github.com/88250/gulu v1.2.3-0.20230321113152-38c4a3d73c37/go.mod h1:pTWnjt+6qUqNnP9xltswsJxgCBVu3C7eW09u48LWX0k=
github.com/88250/lute v1.7.6-0.20230426013259-afae190c74ff h1:gWI6ch+YgmecbRpkLzq8mf9HiYWybEYCHcy+Szwelyg=
github.com/88250/lute v1.7.6-0.20230426013259-afae190c74ff/go.mod h1:+wUqx/1kdFDbWtxn9LYJlaCOAeol2pjSO6w+WJTVQsg=
github.com/88250/lute v1.7.6-0.20230427065909-d447ad8fc493 h1:F6C9dVvQVwxCOAd88Ea4WchCzR6Ekr/Q4IiqLYWfDyw=
github.com/88250/lute v1.7.6-0.20230427065909-d447ad8fc493/go.mod h1:+wUqx/1kdFDbWtxn9LYJlaCOAeol2pjSO6w+WJTVQsg=
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c h1:Dl/8S9iLyPMTElnWIBxmjaLiWrkI5P4a21ivwAn5pU0=

View file

@ -132,21 +132,20 @@ func SearchTemplate(keyword string) (ret []*Block) {
return
}
func DocSaveAsTemplate(id string, overwrite bool) (code int, err error) {
tree, err := loadTreeByBlockID(id)
if nil != err {
func DocSaveAsTemplate(id, name string, overwrite bool) (code int, err error) {
bt := treenode.GetBlockTree(id)
if nil == bt {
return
}
tree := prepareExportTree(bt)
addBlockIALNodes(tree, true)
luteEngine := NewLute()
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
md := formatRenderer.Render()
title := tree.Root.IALAttr("title")
title = util.FilterFileName(title)
title += ".md"
savePath := filepath.Join(util.DataDir, "templates", title)
name = util.FilterFileName(name) + ".md"
savePath := filepath.Join(util.DataDir, "templates", name)
if gulu.File.IsExist(savePath) {
if !overwrite {
code = 1