This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
component-builder.html
168 lines (138 loc) · 5.23 KB
/
component-builder.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web5 component builder</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/themes/prism.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Inconsolata:400,700&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/components/prism-javascript.min.js"></script>
<style>
body {
font-family: 'Inconsolata', monospace;
background-color: #c9d0f9;
color: #ffffff;
}
.container {
max-width: 900px;
margin: auto;
padding: 20px;
}
textarea, input {
width: 100%;
resize: vertical;
border: none;
padding: 0px;
font-family: 'Inconsolata', monospace;
font-size: 16px;
}
h1, h2 {
font-weight: 700;
margin-bottom: 10px;
}
button {
background-color: #333333;
color: #ffffff;
border: none;
padding: 10px 20px;
font-family: 'Inconsolata', monospace;
font-size: 16px;
cursor: pointer;
margin-bottom: 20px;
}
button:hover {
background-color: #4caf50;
}
pre code {
width: 100%;
font-family: 'Inconsolata', monospace !important;
}
body,nav {
color: #0f23da
}
</style>
</head>
<body>
<div class="container">
<pre>
______ .___ ___. _______ ____ __ ____ _______ .______ _____
/ __ \ | \/ | / _____| \ \ / \ / / | ____|| _ \ | ____|
| | | | | \ / | | | __ \ \/ \/ / | |__ | |_) | | |__
| | | | | |\/| | | | |_ | \ / | __| | _ < |___ \
| `--' | | | | | | |__| | \ /\ / | |____ | |_) | ___) |
\______/ |__| |__| \______| \__/ \__/ |_______||______/ |____/
</pre>
<h2>Enter a schema or describe your data:</h2>
<textarea id="schema-description" rows="6" placeholder="example:
1. use Person schema https://schema.org/Person
2. I want to store information about movies
"></textarea>
<div id="chosen-schema"></div>
<h2>Saving and Querying schema data with web5:</h2>
<pre class="language-javascript"><code class="language-javascript" id="web5-code">...</code></pre>
<h2>A web component to render this schema:</h2>
<pre class="language-javascript"><code class="language-javascript" id="web-component-code">...</code></pre>
</div>
<script>
const schema = document.getElementById('schema-description');
const webComponentCode = document.getElementById('web-component-code');
const web5Code = document.getElementById('web5-code');
const initialWeb5 = `
// writing data
response = await web5.dwn.records.write(myDid.id, {
author: myDid.id,
data: myData,
message: {
schema: 'schema_here',
dataFormat: 'application/json'
}
});
// Query for the record that was just created.
response = await web5.dwn.records.query(myDid.id, {
author: myDid.id,
message: {
filter: {
schema: 'schema_here'
}
}
});`
schema.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
// prevent the default behavior of the enter key
e.preventDefault();
// show that the browser is busy
schema.style.cursor = 'wait';
fetch('/identify-best-fit-schema', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
schema_description: schema.value,
})
}).then(response => response.text()).then(text => {
document.getElementById('chosen-schema').innerText = "Using " + text;
web5Code.textContent = initialWeb5.replaceAll('schema_here', text);
Prism.highlightAll();
// set the inner text of webComponentCode to something
webComponentCode.textContent = 'Generating web component... this may take some time...';
fetch("/generate-webcomponent", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
schema: text
})
}).then(response => response.text()).then(text => {
webComponentCode.textContent = text;
Prism.highlightAll();
// make cursor not busy
schema.style.cursor = 'auto';
});
});
}});
</script>
</body>
</html>