forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageScript.vue
More file actions
132 lines (123 loc) · 4.16 KB
/
PageScript.vue
File metadata and controls
132 lines (123 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<template>
<div class="flex flex-col gap-4">
<div class="flex gap-2">
<BuilderButton @click="showClientScriptEditor()" class="flex-1">Client Script</BuilderButton>
<BuilderButton @click="showServerScriptEditor()" class="flex-1">Data Script</BuilderButton>
</div>
<CodeEditor v-model="pageStore.pageData" type="JSON" label="Data Preview" :readonly="true"></CodeEditor>
<Dialog
style="z-index: 40"
class="overscroll-none"
:options="{
title: currentScriptEditor == 'data' ? 'Data Script' : 'Client Script',
size: '7xl',
}"
:isDirty="isDirty"
v-model="showDialog">
<template #body-content>
<div v-if="currentScriptEditor == 'client'">
<PageClientScriptManager
:page="pageStore.activePage as BuilderPage"
ref="clientScriptManager"></PageClientScriptManager>
</div>
<div v-else>
<div class="flex gap-4">
<CodeEditor
class="w-2/3 overscroll-none"
ref="dataScriptEditor"
v-model="page.page_data_script"
type="Python"
height="60vh"
:autofocus="true"
@save="savePageDataScript"
:showSaveButton="true"
:show-line-numbers="true"></CodeEditor>
<CodeEditor
v-model="pageStore.pageData"
type="JSON"
label="Data Preview"
:showLineNumbers="true"
class="-mt-5 w-1/3 [&>div>div]:bg-surface-white"
height="calc(100% - 110px)"
description='Use Data Script to provide dynamic data to your web page.<br>
<b>Example:</b> data.events = frappe.get_list("Event")<br><br>
For more details on how to write data script, refer to <b><a class="underline" href="https://docs.frappe.io/builder/data-script" target="_blank">this documentation</a></b>.
'
:readonly="true"></CodeEditor>
</div>
</div>
</template>
</Dialog>
</div>
</template>
<script lang="ts" setup>
import Dialog from "@/components/Controls/Dialog.vue";
import { webPages } from "@/data/webPage";
import useBuilderStore from "@/stores/builderStore";
import usePageStore from "@/stores/pageStore";
import { posthog } from "@/telemetry";
import { BuilderPage } from "@/types/Builder/BuilderPage";
import { computed, defineComponent, ref, watch } from "vue";
import { toast } from "vue-sonner";
import CodeEditor from "./Controls/CodeEditor.vue";
import PageClientScriptManager from "./PageClientScriptManager.vue";
const pageStore = usePageStore();
const builderStore = useBuilderStore();
const showDialog = ref(false);
const props = defineProps<{
page: BuilderPage;
}>();
const clientScriptManager = ref<null | InstanceType<typeof PageClientScriptManager>>(null);
const dataScriptEditor = ref<null | InstanceType<typeof CodeEditor>>(null);
const currentScriptEditor = ref<"client" | "data">("client");
const savePageDataScript = (value: string) => {
webPages.setValue
.submit({
name: props.page.name,
page_data_script: value,
})
.then(() => {
posthog.capture("builder_page_data_script_saved");
props.page.page_data_script = value;
pageStore.setPageData(props.page);
toast.success("Data script saved");
})
.catch((e: { message: string; exc: string; messages: [string] }) => {
let errorMessage = e.exc?.split("\n").slice(-2)[0];
if (!errorMessage) {
errorMessage = e.messages[0];
}
toast.error("Failed to save script", {
description: defineComponent({
template: `<div>${errorMessage}</div>`,
}),
});
});
};
const showClientScriptEditor = () => {
currentScriptEditor.value = "client";
showDialog.value = true;
};
const showServerScriptEditor = () => {
currentScriptEditor.value = "data";
showDialog.value = true;
};
const isDirty = computed(() => {
if (currentScriptEditor.value === "data" && dataScriptEditor.value) {
return dataScriptEditor.value.isDirty;
} else if (currentScriptEditor.value === "client" && clientScriptManager.value?.scriptEditor) {
return clientScriptManager.value?.scriptEditor.isDirty;
}
return false;
});
watch(
() => builderStore.showDataScriptDialog,
() => {
// if showDataScriptDialog is true, open the dialog
if (builderStore.showDataScriptDialog) {
showServerScriptEditor();
builderStore.showDataScriptDialog = false; // reset the flag
}
},
);
</script>