Skip to content

Commit 83f9d8a

Browse files
committed
feat: add json editor to the demo
1 parent 0d6ec6a commit 83f9d8a

File tree

5 files changed

+116
-12
lines changed

5 files changed

+116
-12
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"@semantic-release/changelog": "^6.0.3",
9393
"@semantic-release/git": "^10.0.1",
9494
"@semantic-release/npm": "^12.0.1",
95+
"ace-builds": "^1.36.5",
9596
"jsdom": "^25.0.1",
9697
"lefthook": "^1.7.18",
9798
"npm-run-all": "^2.1.0",

src/demo/index.html

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@
3939
</section>
4040
<div class="container render-btn-wrap" id="editor-action-buttons"></div>
4141
<div id="formData-popover" popover>
42-
<h3>Test formData</h3>
43-
<textarea placeholder="Paste formData here..."></textarea>
44-
<div>
45-
<button popovertarget="formData-popover" popovertargetaction="hide">Cancel</button>
46-
<button id="submit-popover">Submit</button>
42+
<div class="popover-header">
43+
<h3>Test formData</h3>
44+
<div>
45+
<button type="button" id="format-json">Format</button>
46+
<button type="button" id="collapse-json">Collapse</button>
47+
<button type="button" id="copy-json">Copy to Clipboard</button>
48+
</div>
49+
</div>
50+
<pre id="formData-editor"></pre>
51+
<div class=formData-actions>
52+
<button popovertarget="formData-popover" popovertargetaction="hide" type="button">Cancel</button>
53+
<button id="submit-formData" type="button">Submit</button>
4754
</div>
4855
</div>
4956
<footer id="demo-footer">

src/demo/js/actionButtons.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import startCase from 'lodash/startCase'
22

3+
import aceEditor, { config } from 'ace-builds/src-noconflict/ace'
4+
import Json from 'ace-builds/src-noconflict/mode-json?url'
5+
import githubTheme from 'ace-builds/src-noconflict/theme-github_light_default?url'
6+
7+
config.setModuleUrl('ace/mode/json', Json)
8+
config.setModuleUrl('ace/theme/github_light_default', githubTheme)
9+
const jsonEditor = aceEditor.edit('formData-editor')
10+
jsonEditor.session.setOption('useWorker', false)
11+
12+
jsonEditor.setOptions({
13+
theme: 'ace/theme/github_light_default',
14+
mode: 'ace/mode/json',
15+
})
16+
17+
const submitFormData = document.getElementById('submit-formData')
18+
const popover = document.getElementById('formData-popover')
19+
320
const editorActionButtonContainer = document.getElementById('editor-action-buttons')
421
const renderFormWrap = document.querySelector('.render-form')
522
const editorActions = (editor, renderer) => ({
@@ -18,12 +35,7 @@ const editorActions = (editor, renderer) => ({
1835
window.location.reload()
1936
},
2037
testData: () => {
21-
document.getElementById('submit-popover').addEventListener('click', () => {
22-
const textarea = document.querySelector('#formData-popover textarea')
23-
editor.formData = textarea.value
24-
textarea.value = ''
25-
textarea.closest('[popover]').hidePopover()
26-
})
38+
jsonEditor.setValue(JSON.stringify(editor.formData, null, 2), 1)
2739
},
2840
})
2941

@@ -36,6 +48,11 @@ const getButtonAttrs = id => {
3648
}
3749

3850
export const editorButtons = (editor, renderer) => {
51+
submitFormData.addEventListener('click', () => {
52+
editor.formData = jsonEditor.session.getValue()
53+
popover.hidePopover()
54+
})
55+
3956
const buttonActions = editorActions(editor, renderer)
4057
const buttons = Object.entries(buttonActions).map(([id, cb]) => {
4158
const attrs = getButtonAttrs(id)
@@ -52,3 +69,35 @@ export const editorButtons = (editor, renderer) => {
5269

5370
return buttons
5471
}
72+
73+
document.getElementById('format-json').addEventListener('click', formatJSON)
74+
document.getElementById('collapse-json').addEventListener('click', collapseJSON)
75+
document.getElementById('copy-json').addEventListener('click', copyJSON)
76+
77+
function formatJSON() {
78+
const val = jsonEditor.session.getValue()
79+
const o = JSON.parse(val)
80+
jsonEditor.setValue(JSON.stringify(o, null, 2), 1)
81+
}
82+
83+
function collapseJSON() {
84+
const val = jsonEditor.session.getValue()
85+
const o = JSON.parse(val)
86+
jsonEditor.setValue(JSON.stringify(o, null, 0), 1)
87+
}
88+
89+
async function copyJSON({ target }) {
90+
const textBackup = target.textContent
91+
target.textContent = 'Copied!'
92+
const timeout = setTimeout(() => {
93+
target.textContent = textBackup
94+
clearTimeout(timeout)
95+
}, 3000)
96+
97+
try {
98+
await navigator.clipboard.writeText(jsonEditor.session.getValue())
99+
console.log('Text copied to clipboard')
100+
} catch (err) {
101+
console.error('Failed to copy: ', err)
102+
}
103+
}

src/demo/sass/demo.scss

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,50 @@ body {
207207
display: flex;
208208
}
209209

210-
textarea {
210+
&::backdrop {
211+
background-color: rgba(0, 0, 0, 0.5);
212+
backdrop-filter: blur(4px);
213+
}
214+
215+
pre {
211216
width: 100%;
212217
min-height: 100px;
213218
margin-bottom: 8px;
214219
flex: 1;
215220
padding: 4px;
216221
}
222+
223+
.popover-header {
224+
display: flex;
225+
justify-content: space-between;
226+
227+
button {
228+
background-color: rgba(75, 75, 75, 0.75);
229+
color: white;
230+
}
231+
}
232+
233+
button {
234+
padding: 4px 8px;
235+
border: 0 none;
236+
border-radius: 4px;
237+
}
238+
239+
.formData-actions {
240+
display: flex;
241+
justify-content: flex-end;
242+
width: 100%;
243+
gap: 8px;
244+
245+
button {
246+
&:first-child {
247+
background-color: rgb(131, 0, 0);
248+
color: #fff;
249+
}
250+
&:last-child {
251+
background-color: rgb(0, 124, 0);
252+
color: #fff;
253+
}
254+
}
255+
}
217256
}

0 commit comments

Comments
 (0)