Skip to content

Commit 09a606f

Browse files
committed
chore: add data source UX
1 parent 6b52d60 commit 09a606f

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
COMMAND_PREVIEW_TOGGLE,
88
PREVIEW_ACTIVATED,
99
PREVIEW_DEACTIVATED,
10+
COMMAND_ADD_DATA_SOURCE,
1011
} from './types'
1112
import { refreshDataSources } from './model/dataSourceManager'
1213
import { Editor } from 'grapesjs'

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export const COMMAND_PREVIEW_ACTIVATE = 'data-source:preview:activate'
114114
export const COMMAND_PREVIEW_DEACTIVATE = 'data-source:preview:deactivate'
115115
export const COMMAND_PREVIEW_REFRESH = 'data-source:preview:refresh'
116116
export const COMMAND_PREVIEW_TOGGLE = 'data-source:preview:toggle'
117+
export const COMMAND_ADD_DATA_SOURCE = 'data-source:add'
117118

118119
export type DataSourceType = 'graphql'
119120

src/view/settings.ts

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createRef, Ref, ref } from 'lit/directives/ref.js'
22
import {repeat} from 'lit/directives/repeat.js'
33
import GraphQL, { GraphQLOptions } from '../datasources/GraphQL'
4-
import { DATA_SOURCE_CHANGED, DATA_SOURCE_DATA_LOAD_END, DATA_SOURCE_ERROR, DATA_SOURCE_READY, DataSourceEditorViewOptions, IDataSource } from '../types'
4+
import { COMMAND_ADD_DATA_SOURCE, DATA_SOURCE_CHANGED, DATA_SOURCE_DATA_LOAD_END, DATA_SOURCE_ERROR, DATA_SOURCE_READY, DataSourceEditorViewOptions, IDataSource } from '../types'
55
import { getDefaultOptions, getElementFromOption } from '../utils'
66
import { getAllDataSources, addDataSource, removeDataSource } from '../model/dataSourceRegistry'
77
import { css, html, LitElement, render } from 'lit'
@@ -83,6 +83,13 @@ export default (editor: Editor, options: Partial<DataSourceEditorViewOptions> =
8383
}
8484
})
8585
renderSettings(editor, dsSettings, settingsEl)
86+
87+
// Add a data source
88+
editor.Commands.add(COMMAND_ADD_DATA_SOURCE, {
89+
run() {
90+
dsSettings.value?.openDataSourceModal()
91+
},
92+
})
8693
}
8794
}
8895

@@ -143,6 +150,7 @@ class SettingsDataSources extends LitElement {
143150
// Create the form element
144151
const formElement = document.createElement('ds-settings__data-source') as SettingsDataSource
145152
formElement.dataSource = ds
153+
formElement.isEdit = isEdit
146154

147155
// Handle form events
148156
const handleSave = () => {
@@ -159,11 +167,40 @@ class SettingsDataSources extends LitElement {
159167
this.editor!.Modal.close()
160168
}
161169

170+
const handleCancel = () => {
171+
this.editor!.Modal.close()
172+
}
173+
174+
const handleKeyDown = (e: KeyboardEvent) => {
175+
if (e.key === 'Escape') {
176+
this.editor!.Modal.close()
177+
}
178+
}
179+
162180
formElement.addEventListener('change', handleSave)
181+
formElement.addEventListener('cancel', handleCancel)
163182
if (isEdit) {
164183
formElement.addEventListener('delete', handleDelete)
165184
}
166185

186+
// Add ESC key handler
187+
document.addEventListener('keydown', handleKeyDown)
188+
189+
// Clean up on modal close
190+
const modalObserver = new MutationObserver(() => {
191+
if (!this.editor!.Modal.isOpen()) {
192+
document.removeEventListener('keydown', handleKeyDown)
193+
modalObserver.disconnect()
194+
}
195+
})
196+
const modalParent = this.editor.Modal.getContentEl()?.parentElement
197+
if (modalParent) {
198+
modalObserver.observe(modalParent, {
199+
attributes: true,
200+
attributeFilter: ['style', 'class'],
201+
})
202+
}
203+
167204
// Open the modal
168205
this.editor.Modal.open({
169206
title,
@@ -306,6 +343,8 @@ if(!customElements.get('ds-settings')) {
306343
class SettingsDataSource extends LitElement {
307344
@property({ type: Object })
308345
dataSource: IDataSource | null
346+
@property({ type: Boolean })
347+
isEdit: boolean = false
309348
errorMessage: string = ''
310349
connected: boolean = false
311350
isLoading: boolean = false
@@ -395,9 +434,19 @@ class SettingsDataSource extends LitElement {
395434
.ds-actions {
396435
display: flex;
397436
gap: 10px;
398-
justify-content: flex-start;
437+
justify-content: space-between;
438+
align-items: center;
399439
margin-top: 15px;
400440
}
441+
.ds-actions-left {
442+
display: flex;
443+
gap: 10px;
444+
}
445+
.ds-actions-right {
446+
display: flex;
447+
gap: 10px;
448+
margin-left: auto;
449+
}
401450
.ds-no-resize {
402451
flex: 0 0 auto;
403452
}
@@ -511,20 +560,20 @@ class SettingsDataSource extends LitElement {
511560
</div>
512561
513562
<div class="ds-field ds-actions">
514-
<button
515-
type="submit"
516-
class="ds-btn-prim"
517-
?disabled=${this.isLoading}
518-
>${this.isLoading ? 'Testing...' : 'Test Connection'}</button>
519-
${this.dataSource.readonly !== false ? '' : html`
563+
<div class="ds-actions-right">
520564
<button
521565
type="button"
522-
class="ds-btn-prim ds-btn-danger"
566+
class="ds-btn-prim"
523567
@click=${() => {
524-
this.dispatchEvent(new CustomEvent('delete'))
568+
this.dispatchEvent(new CustomEvent('cancel'))
525569
}}
526-
>Delete Data Source</button>
527-
`}
570+
>Cancel</button>
571+
<button
572+
type="submit"
573+
class="ds-btn-prim"
574+
?disabled=${this.isLoading}
575+
>${this.isLoading ? 'Testing...' : 'Apply'}</button>
576+
</div>
528577
</div>
529578
</div>
530579
</form>

0 commit comments

Comments
 (0)