-
Notifications
You must be signed in to change notification settings - Fork 0
/
+page.svelte
151 lines (139 loc) · 4.13 KB
/
+page.svelte
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<script lang="ts">
import type { EditorProblem } from "$lib/editor/EditorProblem.js";
import EnvelopeEditor from "$lib/EnvelopeEditor.svelte";
import type { State } from "$lib/types/editor";
import BuilderNavbar from "$lib/BuilderNavbar.svelte";
import type { EnvelopeHeader, GOBLDocument } from "$lib/types/envelope";
import EditorFormModalSignatures from "$lib/editor/form/modals/EditorFormModalSignatures.svelte";
import EditorFormModalHeaders from "$lib/editor/form/modals/EditorFormModalHeaders.svelte";
import EditorFormModalCorrect from "$lib/editor/form/modals/EditorFormModalCorrect.svelte";
import type { UIModelField } from "$lib/editor/form/utils/model";
let data = "";
let problems: EditorProblem[] = [];
let jsonSchemaURL = "";
let defaultSchema = "";
let builder: EnvelopeEditor;
let state: State = "init";
let forceReadOnly = false;
let envelope = "";
let editorView = "code";
let openSignaturesModal = false;
let sigs: string[] | null = null;
let openHeadersModal = false;
let header: EnvelopeHeader | null = null;
let openCorrectModal = false;
let correctionModel: UIModelField | undefined;
function handleDocLoad(event: CustomEvent<GOBLDocument>) {
const newData = JSON.stringify(event.detail, null, 4);
data = newData;
// Hack for allowing load the same document again to restore data
if (newData === data) {
builder.reloadData();
}
jsonSchemaURL = event.detail.$schema;
defaultSchema = jsonSchemaURL.replace("https://gobl.org/draft-0/", "");
}
function handleSchemaChange(event: CustomEvent<string>) {
jsonSchemaURL = event.detail;
}
async function handleAction(event: CustomEvent) {
if (event.detail === "showSignatures") {
sigs = await builder.getSignatures();
openSignaturesModal = true;
return;
}
if (event.detail === "showHeaders") {
header = await builder.getHeaders();
openHeadersModal = true;
return;
}
if (event.detail === "correct") {
correctionModel = await builder.getCorrectionOptionsModel();
openCorrectModal = true;
return;
}
builder[event.detail]();
}
</script>
<div class="flex flex-col h-screen bg-gobl-900">
<BuilderNavbar
{state}
{defaultSchema}
{envelope}
bind:forceReadOnly
bind:editorView
on:load={handleDocLoad}
on:action={handleAction}
on:schemaChanged={handleSchemaChange}
/>
<div class="flex-1 h-full p-1 pt-14">
<div class="h-full bg-white rounded-t-lg">
<EnvelopeEditor
bind:this={builder}
bind:state
bind:data
bind:problems
{jsonSchemaURL}
{editorView}
signEnabled
{forceReadOnly}
on:change={(event) => {
envelope = event.detail.envelope;
console.log("Received change event.", event.detail);
}}
on:undo={() => {
console.log("User clicked `Undo`.");
}}
on:redo={() => {
console.log("User clicked `Redo`.");
}}
on:build={(event) => {
console.log("Received build result.", event.detail);
}}
on:sign={(event) => {
console.log("Received sign result.", event.detail);
}}
on:validate={(event) => {
console.log("Received validate result.", event.detail);
}}
on:correct={(event) => {
console.log("Received correct result.", event.detail);
}}
/>
</div>
</div>
</div>
{#if openSignaturesModal}
<EditorFormModalSignatures
{sigs}
on:close={() => {
openSignaturesModal = false;
sigs = null;
}}
/>
{/if}
{#if openHeadersModal}
<EditorFormModalHeaders
{header}
on:close={() => {
openHeadersModal = false;
header = null;
}}
on:confirm={(event) => {
builder.setHeaders(event.detail);
openHeadersModal = false;
}}
/>
{/if}
{#if openCorrectModal}
<EditorFormModalCorrect
model={correctionModel}
on:close={() => {
openCorrectModal = false;
}}
on:confirm={async (event) => {
const result = await builder.correctWithOptions(event.detail);
openCorrectModal = !result;
}}
/>
{/if}