Skip to content

Commit 29e1920

Browse files
committed
fix(layouter): resize
1 parent eb65290 commit 29e1920

File tree

4 files changed

+99
-52
lines changed

4 files changed

+99
-52
lines changed

src/main/parser/DocumentParser.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface Header {
66
value: string
77
}
88

9-
export interface RequestFormData {
9+
export interface RequestMultipartFormData {
1010
key: string
1111
value: string
1212
}
@@ -17,7 +17,7 @@ interface Request {
1717
httpVersion: string
1818
headers: Header[]
1919
body: string | null
20-
formData: RequestFormData[] | null
20+
multipartFormData: RequestMultipartFormData[] | null
2121
}
2222

2323
interface Metadata {
@@ -139,7 +139,7 @@ const parse = (content: string): Document | null => {
139139
let url: string = ''
140140
let httpVersion: string = ''
141141
let body: string | null = null
142-
let formData: RequestFormData[] | null = null
142+
let multipartFormData: RequestMultipartFormData[] | null = null
143143

144144
bn.children.forEach((node) => {
145145
if (node.type === 'pre_request_script') {
@@ -270,7 +270,7 @@ const parse = (content: string): Document | null => {
270270
if (child.children.length > 1 && child.children[1].type === 'external_body') {
271271
return
272272
}
273-
if (!formData) formData = []
273+
if (!multipartFormData) multipartFormData = []
274274
let key: string | null = null
275275
let value: string | null = null
276276
// The value looks something like this:
@@ -294,8 +294,8 @@ const parse = (content: string): Document | null => {
294294
value += part
295295
value = value.trim()
296296
}
297-
if (formData && key && value) {
298-
formData.push({ key, value })
297+
if (multipartFormData && key && value) {
298+
multipartFormData.push({ key, value })
299299
key = null
300300
value = null
301301
}
@@ -318,7 +318,7 @@ const parse = (content: string): Document | null => {
318318
httpVersion,
319319
headers,
320320
body,
321-
formData
321+
multipartFormData: multipartFormData
322322
}
323323
})
324324
if (block.request?.url.length || block.metadata.length > 0 || block.comments.length > 0) {

src/renderer/src/app.css

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
@import 'tailwindcss';
22
@plugin 'daisyui';
33

4-
.ui-resizable.ui-resizable-width {
5-
position: relative;
6-
}
7-
8-
.ui-resizable.ui-resizable-width::after {
4+
.ui-resizable.ui-resizable-width .ui-resizable-handle::after {
95
content: "";
106
position: absolute;
117
top: 0;
@@ -17,15 +13,15 @@
1713
transition: background-color 0.2s ease;
1814
}
1915

20-
.ui-resizable.ui-resizable-width::after:hover,
21-
.ui-resizable.ui-resizable-width::after:active {
16+
.ui-resizable.ui-resizable-width .ui-resizable-handle::after:hover,
17+
.ui-resizable.ui-resizable-width .ui-resizable-handle::after:active {
2218
background-color: rgba(0, 0, 0, 0.7);
2319
}
2420

25-
.ui-resizable.ui-resizable-width::after {
21+
.ui-resizable.ui-resizable-width .ui-resizable-handle::after {
2622
box-shadow: 1px 0 2px rgba(0,0,0,0.05);
2723
}
2824

29-
.ui-resizable.ui-resizable-width::after {
25+
.ui-resizable.ui-resizable-width .ui-resizable-handle::after {
3026
border-radius: 2px;
3127
}

src/renderer/src/views/explorer.svelte

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
let responseEditorContainer: HTMLElement
2121
let responseIsVisible = false
2222
let editorSyntaxIsVisible = true
23+
let isMultipartFormRequest = false
2324
2425
let loadingModal: HTMLDialogElement
2526
let addFilesModal: HTMLDialogElement
@@ -110,18 +111,36 @@
110111
$collections = await window.KulalaApi.getCollectionNames()
111112
}
112113
113-
const setFormDataValues = (formData: RequestFormData) => {
114+
const setMultipartFormDataValues = (formData: RequestFormData): void => {
114115
requestBodyTypeSelect.value = 'form-data'
115116
console.log({ formData })
116117
}
117118
119+
const setRequestType = (block: Block): void => {
120+
if (block.request.multipartFormData) {
121+
requestBodyTypeSelect.value = 'form-data'
122+
} else if (block.request.formData) {
123+
requestBodyTypeSelect.value = 'x-www-form-urlencoded'
124+
} else if (block.request.body) {
125+
requestBodyTypeSelect.value = 'raw'
126+
} else {
127+
requestBodyTypeSelect.value = 'none'
128+
}
129+
}
130+
118131
const setValuesBasedOnBlock = (block: Block): void => {
119132
requestMethodSelect.value = block.request.method
120133
requestUrlInput.value = block.request.url
121134
const body = block.request.body?.trim() || ''
122-
if (block.request.formData) {
123-
setFormDataValues(block.request.formData)
135+
if (block.request.multipartFormData) {
136+
isMultipartFormRequest = true
137+
editorSyntaxIsVisible = false
138+
setMultipartFormDataValues(block.request.multipartFormData)
139+
} else {
140+
isMultipartFormRequest = false
141+
editorSyntaxIsVisible = true
124142
}
143+
setRequestType(block)
125144
editor.setValue(body)
126145
}
127146
@@ -196,8 +215,8 @@
196215
processResponse(data)
197216
})
198217
.catch((err) => {
199-
showInfoModal = true
200-
infoModalContent = err
218+
console.error(err)
219+
// TODO: show error via modal
201220
})
202221
.finally(() => {
203222
isLoading = false
@@ -229,17 +248,28 @@
229248
})
230249
responseEditor.setModel(monaco.editor.createModel('', 'text'))
231250
232-
window.addEventListener('resize', () => {
251+
const layoutEditors = (): void => {
233252
editor.layout()
234253
responseEditor.layout()
235-
})
254+
}
255+
window.addEventListener('resize', layoutEditors)
236256
const layout = await window.KulalaApi.getLayout()
257+
NewResizer(
258+
containerLeftSection,
259+
true,
260+
false,
261+
async (w) => {
262+
await window.KulalaApi.saveLayout({
263+
leftSectionWidth: w
264+
})
265+
},
266+
() => {
267+
layoutEditors()
268+
}
269+
)
270+
console.log(layout.leftSectionWidth)
237271
containerLeftSection.style.width = layout.leftSectionWidth + 'px'
238-
NewResizer(containerLeftSection, true, false, (w) => {
239-
window.KulalaApi.saveLayout({
240-
leftSectionWidth: w
241-
})
242-
})
272+
layoutEditors()
243273
})
244274
onDestroy(() => {
245275
monaco?.editor.getModels().forEach((model) => model.dispose())
@@ -278,12 +308,6 @@
278308
$: if (editor) {
279309
onRawSyntaxSelectChange = createOnSyntaxChangeHandler(editor)
280310
}
281-
$: editorSyntax,
282-
(): void => {
283-
alert('Syntax changed to ' + editorSyntax)
284-
editorSyntaxIsVisible =
285-
editorSyntax !== 'form-data' && editorSyntax !== 'x-www-form-urlencoded'
286-
}
287311
$: $collections, onCollectionsChange()
288312
</script>
289313

@@ -356,7 +380,7 @@
356380

357381
<div class="join w-full">
358382
<div
359-
class="join-item ui-resizable ui-resizable-width w-full p-5"
383+
class="relative join-item ui-resizable ui-resizable-width w-full p-5"
360384
bind:this={containerLeftSection}
361385
>
362386
<div>
@@ -470,6 +494,7 @@
470494
{/each}
471495
</div>
472496
</div>
497+
<div class="ui-resizable-handle"></div>
473498
</div>
474499
<div class="join-item w-full p-5">
475500
{#if selectedRequest.block}
@@ -581,21 +606,37 @@
581606
</select>
582607
</div>
583608

584-
<div class="field mt-5">
585-
<div class="control">
586-
<div>
587-
<div class="editor-wrap">
588-
<div class="editor-container" bind:this={editorContainer} />
589-
</div>
590-
</div>
609+
<div class="mt-5 {isMultipartFormRequest ? 'hidden' : ''}">
610+
<div class="editor-wrap">
611+
<div class="editor-container" bind:this={editorContainer} />
591612
</div>
592613
</div>
593614

594-
<div class="field mt-5">
595-
<div class="control">
596-
<div class="editor-wrap {responseIsVisible ? '' : 'hidden'}">
597-
<div class="editor-container" bind:this={responseEditorContainer} />
598-
</div>
615+
<div class="mt-5 {isMultipartFormRequest ? '' : 'hidden'}">
616+
{#if isMultipartFormRequest}
617+
{#each selectedRequest.block.request.multipartFormData as item}
618+
<div class="join w-full mb-5">
619+
<input type="text" value={item.key} class="join-item input w-full" placeholder="Name" />
620+
<input type="text" value={item.value} class="join-item input w-full" placeholder="Value" />
621+
<button class="join-item btn btn-error">
622+
<span class="icon">
623+
<i class="fa fa-trash"></i>
624+
</span>
625+
</button>
626+
</div>
627+
{/each}
628+
<button class="btn btn-success">
629+
<span class="icon">
630+
<i class="fa fa-plus"></i>
631+
Add a new field
632+
</span>
633+
</button>
634+
{/if}
635+
</div>
636+
637+
<div class="mt-5">
638+
<div class="editor-wrap {responseIsVisible ? '' : 'hidden'}">
639+
<div class="editor-container" bind:this={responseEditorContainer} />
599640
</div>
600641
</div>
601642
</div>

src/renderer/src/views/explorer/resizer.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ export const NewResizer = (
22
container: HTMLElement,
33
width: boolean = true,
44
height: boolean = true,
5-
cb: (w: number, h: number) => void | undefined
5+
cbWhenDone: (w: number, h: number) => void | undefined,
6+
cbWhileDragging: (w: number, h: number) => void | undefined
67
): void => {
7-
const resizer = container
8+
const resizer = container.querySelector('.ui-resizable-handle') as HTMLDivElement
9+
810
let isResizing = false
911
let startX: number, startY: number, startWidth: number, startHeight: number
12+
let target: HTMLElement
1013

1114
resizer.addEventListener('mousedown', (e: MouseEvent) => {
15+
target = e.target as HTMLElement
16+
if (target !== resizer) return
1217
isResizing = true
1318
startX = e.clientX
1419
startY = e.clientY
@@ -20,13 +25,18 @@ export const NewResizer = (
2025
if (isResizing) {
2126
const w = startWidth + e.clientX - startX
2227
const h = startHeight + e.clientY - startY
23-
if (width) container.style.width = w + 'px'
24-
if (height) container.style.height = h + 'px'
28+
if (width && w) container.style.width = w + 'px'
29+
if (height && h) container.style.height = h + 'px'
30+
if (cbWhileDragging && w && h) cbWhileDragging(w, h)
2531
}
2632
})
2733

2834
document.addEventListener('mouseup', () => {
35+
if (!isResizing) return
2936
isResizing = false
30-
if (cb) cb(container.offsetWidth, container.offsetHeight)
37+
if (target !== resizer) return
38+
if (!container.offsetWidth || !container.offsetHeight) return
39+
if (startWidth !== container.offsetWidth && startHeight !== container.offsetHeight) return
40+
if (cbWhenDone) cbWhenDone(container.offsetWidth, container.offsetHeight)
3141
})
3242
}

0 commit comments

Comments
 (0)