Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/views/components/modals/template-modal/hooks/use-template.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import useCodeFormatting from "@hooks/use-code-formatting"
import useEditorWrapperState from "@hooks/use-editor-wrapper-state"
import { useEditorConfigStore } from "@store/editor-config"
import { useEditorWrapperStore } from "@store/editor-wrapper"
Expand All @@ -13,6 +14,7 @@ const useTemplate = () => {
const editorWrapperStore = useEditorWrapperStore()
const editorConfigStore = useEditorConfigStore()
const { initDefaultWrapper, initComponentWrapper } = useEditorWrapperState()
const { formatEditorCode } = useCodeFormatting()

const getTemplateBaseInfo = () => {
const { codeMap, tabMap, isComponentMode } = editorWrapperStore
Expand Down Expand Up @@ -55,7 +57,7 @@ const useTemplate = () => {

/** 应用模板 */
const applyTemplate = async (template: ITemplateBase) => {
const { codeMap, editorConfig: { libraries = {}, prepMap }, isComponent } = template
const { codeMap, editorConfig: { libraries = {}, prepMap = {} }, isComponent } = template
const { batchUpdateEditorConfig } = editorConfigStore
const { batchUpdateEditorWrapper } = editorWrapperStore
const { origin2TabIdMap } = storeToRefs(editorWrapperStore)
Expand All @@ -66,11 +68,12 @@ const useTemplate = () => {
} else {
await initDefaultWrapper()
}
// 设置代码内容
const finalCodeMap = Object.entries(origin2TabIdMap.value).reduce((acc, [origin, tabId]) => {
acc[tabId] = codeMap[origin as OriginLang] || ""
return acc
}, {} as Record<number, string>)
const finalCodeMap: Record<number, string> = {}
for (const [origin, tabId] of Object.entries(origin2TabIdMap.value)) {
const code = codeMap[origin as OriginLang] || ""
const prep = prepMap[origin as OriginLang]
finalCodeMap[tabId] = await formatEditorCode(code, prep!)
}
batchUpdateEditorWrapper({ codeMap: finalCodeMap })
// 设置编辑器预处理、库等配置
batchUpdateEditorConfig({
Expand Down
3 changes: 3 additions & 0 deletions src/views/components/modals/template-modal/template-modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:confirm-btn-opts="{
customClass: 'p-l',
disabled: !currTemplate,
loading: isConfirmLoading,
}"
@close="updateDisplayModal(null)"
@confirm="handleConfirmModal">
Expand Down Expand Up @@ -220,7 +221,9 @@ const handleConfirmModal = () => {
const handleUseTemplate = () => {
processUseTemplate()
}
const isConfirmLoading = ref<boolean>(false)
const processUseTemplate = async () => {
isConfirmLoading.value = true
await applyTemplate(currTemplate.value!)
isShowEditTipModal.value = false
updateDisplayModal(null)
Expand Down