mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-04 06:11:53 +08:00
🎨 Support focus block export template https://github.com/siyuan-note/siyuan/issues/8117
This commit is contained in:
parent
9cb6e9ec25
commit
1c47a63cca
@ -9,7 +9,7 @@ import {MenuItem} from "./Menu";
|
|||||||
import {hasClosestByClassName} from "../protyle/util/hasClosest";
|
import {hasClosestByClassName} from "../protyle/util/hasClosest";
|
||||||
import {saveExport} from "../protyle/export";
|
import {saveExport} from "../protyle/export";
|
||||||
import {openByMobile, writeText} from "../protyle/util/compatibility";
|
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 {hideMessage, showMessage} from "../dialog/message";
|
||||||
import {Dialog} from "../dialog";
|
import {Dialog} from "../dialog";
|
||||||
import {focusBlock, focusByRange, getEditorRange} from "../protyle/util/selection";
|
import {focusBlock, focusByRange, getEditorRange} from "../protyle/util/selection";
|
||||||
@ -19,7 +19,7 @@ import {getAllModels} from "../layout/getAll";
|
|||||||
import {Bookmark} from "../layout/dock/Bookmark";
|
import {Bookmark} from "../layout/dock/Bookmark";
|
||||||
import {openAsset, openBy} from "../editor/util";
|
import {openAsset, openBy} from "../editor/util";
|
||||||
/// #endif
|
/// #endif
|
||||||
import {rename} from "../editor/rename";
|
import {rename, replaceFileName} from "../editor/rename";
|
||||||
import {matchHotKey} from "../protyle/util/hotKey";
|
import {matchHotKey} from "../protyle/util/hotKey";
|
||||||
import * as dayjs from "dayjs";
|
import * as dayjs from "dayjs";
|
||||||
import {Constants} from "../constants";
|
import {Constants} from "../constants";
|
||||||
@ -509,9 +509,39 @@ export const exportMd = (id: string) => {
|
|||||||
submenu: [{
|
submenu: [{
|
||||||
label: window.siyuan.languages.template,
|
label: window.siyuan.languages.template,
|
||||||
icon: "iconMarkdown",
|
icon: "iconMarkdown",
|
||||||
click: () => {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
fetchPost("/api/template/docSaveAsTemplate", {
|
fetchPost("/api/template/docSaveAsTemplate", {
|
||||||
id,
|
id,
|
||||||
|
name: inputElement.value,
|
||||||
overwrite: false
|
overwrite: false
|
||||||
}, response => {
|
}, response => {
|
||||||
if (response.code === 1) {
|
if (response.code === 1) {
|
||||||
@ -519,13 +549,21 @@ export const exportMd = (id: string) => {
|
|||||||
confirmDialog(window.siyuan.languages.export, window.siyuan.languages.exportTplTip, () => {
|
confirmDialog(window.siyuan.languages.export, window.siyuan.languages.exportTplTip, () => {
|
||||||
fetchPost("/api/template/docSaveAsTemplate", {
|
fetchPost("/api/template/docSaveAsTemplate", {
|
||||||
id,
|
id,
|
||||||
|
name: inputElement.value,
|
||||||
overwrite: true
|
overwrite: true
|
||||||
|
}, resp => {
|
||||||
|
if (resp.code === 0) {
|
||||||
|
showMessage(window.siyuan.languages.exportTplSucc);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
showMessage(window.siyuan.languages.exportTplSucc);
|
showMessage(window.siyuan.languages.exportTplSucc);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
dialog.destroy();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
label: "Markdown",
|
label: "Markdown",
|
||||||
|
@ -54,8 +54,9 @@ func docSaveAsTemplate(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
id := arg["id"].(string)
|
id := arg["id"].(string)
|
||||||
|
name := arg["name"].(string)
|
||||||
overwrite := arg["overwrite"].(bool)
|
overwrite := arg["overwrite"].(bool)
|
||||||
code, err := model.DocSaveAsTemplate(id, overwrite)
|
code, err := model.DocSaveAsTemplate(id, name, overwrite)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = util.EscapeHTML(err.Error())
|
ret.Msg = util.EscapeHTML(err.Error())
|
||||||
|
@ -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/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 h1:7ZMvlXDbH2sqaxowoAFcLjcdRN7ncVE0U1S2ukpUgac=
|
||||||
github.com/88250/gulu v1.2.3-0.20230321113152-38c4a3d73c37/go.mod h1:pTWnjt+6qUqNnP9xltswsJxgCBVu3C7eW09u48LWX0k=
|
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 h1:F6C9dVvQVwxCOAd88Ea4WchCzR6Ekr/Q4IiqLYWfDyw=
|
||||||
github.com/88250/lute v1.7.6-0.20230427065909-d447ad8fc493/go.mod h1:+wUqx/1kdFDbWtxn9LYJlaCOAeol2pjSO6w+WJTVQsg=
|
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=
|
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c h1:Dl/8S9iLyPMTElnWIBxmjaLiWrkI5P4a21ivwAn5pU0=
|
||||||
|
@ -132,21 +132,20 @@ func SearchTemplate(keyword string) (ret []*Block) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func DocSaveAsTemplate(id string, overwrite bool) (code int, err error) {
|
func DocSaveAsTemplate(id, name string, overwrite bool) (code int, err error) {
|
||||||
tree, err := loadTreeByBlockID(id)
|
bt := treenode.GetBlockTree(id)
|
||||||
if nil != err {
|
if nil == bt {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tree := prepareExportTree(bt)
|
||||||
addBlockIALNodes(tree, true)
|
addBlockIALNodes(tree, true)
|
||||||
|
|
||||||
luteEngine := NewLute()
|
luteEngine := NewLute()
|
||||||
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
|
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
|
||||||
md := formatRenderer.Render()
|
md := formatRenderer.Render()
|
||||||
title := tree.Root.IALAttr("title")
|
name = util.FilterFileName(name) + ".md"
|
||||||
title = util.FilterFileName(title)
|
savePath := filepath.Join(util.DataDir, "templates", name)
|
||||||
title += ".md"
|
|
||||||
savePath := filepath.Join(util.DataDir, "templates", title)
|
|
||||||
if gulu.File.IsExist(savePath) {
|
if gulu.File.IsExist(savePath) {
|
||||||
if !overwrite {
|
if !overwrite {
|
||||||
code = 1
|
code = 1
|
||||||
|
Loading…
Reference in New Issue
Block a user