Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace native dialog with vue modal #1781

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/studio/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
multiple
/>
<data-manager />
<confirmation-modal />
</div>
</template>

Expand All @@ -40,13 +41,13 @@ import DataManager from './components/data/DataManager.vue'
import querystring from 'query-string'
import NotificationManager from './components/NotificationManager.vue'
import UpgradeRequiredModal from './components/common/UpgradeRequiredModal.vue'

import ConfirmationModal from '@/components/common/modals/ConfirmationModal.vue'

export default Vue.extend({
name: 'App',
components: {
CoreInterface, ConnectionInterface, Titlebar, AutoUpdater, NotificationManager,
StateManager, DataManager, UpgradeRequiredModal
StateManager, DataManager, UpgradeRequiredModal, ConfirmationModal
},
data() {
return {
Expand Down
17 changes: 17 additions & 0 deletions apps/studio/src/assets/styles/app/modals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@
}
}

&.confirmation-modal {
.v--modal {
width: auto !important;
min-height: 0px;
}
.dialog-content {
padding: 1.5rem 1.5rem 2rem;
}
.dialog-c-title {
padding-bottom: 1.25rem;
}
.vue-dialog-buttons {
padding-left: 1.5rem;
padding-right: 1.5rem;
padding-bottom: 1.5rem;
}
}
}
71 changes: 71 additions & 0 deletions apps/studio/src/components/common/modals/ConfirmationModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<template>
<portal to="modals">
<modal
class="vue-dialog beekeeper-modal confirmation-modal"
:name="name"
@before-open="beforeOpen"
@before-close="beforeClose"
>
<div class="dialog-content">
<div class="dialog-c-title">
{{ title }}
</div>
{{ message }}
</div>
<div class="vue-dialog-buttons">
<button
class="btn btn-flat"
type="button"
@click.prevent="cancel"
>
Cancel
</button>
<button
class="btn btn-primary"
type="button"
@click.prevent="confirm"
autofocus
>
Confirm
</button>
</div>
</modal>
</portal>
</template>

<script lang="ts">
import Vue from "vue";

const DEFAULT_TITLE = "Are you sure?";
const DEFAULT_MESSAGE = "This action cannot be undone.";

export default Vue.extend({
data() {
return {
name: Vue.prototype.$confirmModalId,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that we need to get this from the prototype because we need it to call the modal function?

onConfirm: null,
onCancel: null,
title: "",
message: "",
};
},
methods: {
beforeOpen(event: any) {
this.onConfirm = event.params.onConfirm;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't super like this, but I guess it's fine for the confirm dialog.

this.onCancel = event.params.onCancel;
this.title = event.params.title || DEFAULT_TITLE;
this.message = event.params.message || DEFAULT_MESSAGE;
},
confirm() {
this.$modal.hide(this.name, { confirmed: true });
},
cancel() {
this.$modal.hide(this.name, { confirmed: false });
},
beforeClose(event: any) {
if (event.params.confirmed) this.onConfirm?.();
else this.onCancel?.();
},
},
});
</script>
2 changes: 1 addition & 1 deletion apps/studio/src/components/sidebar/core/FavoriteList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ import QueryRenameForm from '@/components/common/form/QueryRenameForm.vue'
this.$root.$emit('favoriteClick', item)
},
async remove(favorite) {
if (window.confirm("Really delete?")) {
if (await this.$confirm("Really delete?")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work adapting the API for this!

await this.$store.dispatch('data/queries/remove', favorite)
}
},
Expand Down
3 changes: 2 additions & 1 deletion apps/studio/src/components/tableinfo/TableSchema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ export default Vue.extend({
},
async refreshColumns() {
if(this.hasEdits) {
if (!window.confirm("Are you sure? You will lose unsaved changes")) {
const confirmed = await this.$confirm("Are you sure? You will lose unsaved changes")
if (!confirmed) {
return
}
}
Expand Down
14 changes: 14 additions & 0 deletions apps/studio/src/plugins/BeekeeperPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Vue from 'vue'
import ContextMenu from '@/components/common/ContextMenu.vue'
import { IConnection } from "@/common/interfaces/IConnection"
import path from 'path'
import { uuidv4 } from "@/lib/uuid"

export interface ContextOption {
name: string,
Expand Down Expand Up @@ -106,5 +107,18 @@ export default {
install(Vue) {
Vue.prototype.$app = BeekeeperPlugin
Vue.prototype.$bks = BeekeeperPlugin

Vue.prototype.$confirmModalId = uuidv4()
Vue.prototype.$confirm = function(title: string, message?: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
this.$modal.show(Vue.prototype.$confirmModalId, {
title,
message,
onCancel: () => resolve(false),
onConfirm: () => resolve(true),
})
})
}

}
}
4 changes: 3 additions & 1 deletion apps/studio/src/shims-plugins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ declare module 'vue/types/vue' {
warning(text: string, opts?: any): Noty
info(text: string, opts?: any): Noty
}
$confirm: Promise<boolean>
$confirmModalId: string

// TODO: figure out how to add these automatically from AppEvent.ts
registerHandlers(bindings: RootBinding[]): void
unregisterHandlers(bindings: RootBinding[]): void
trigger<T>(event: AppEvent, options: T): void
}
}
}
14 changes: 14 additions & 0 deletions bin/check-for-native-dialog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /bin/bash

git grep -a "window\.confirm" apps/studio/src
STATUS=$?

if [[ $STATUS -eq 0 ]]
then
echo "Found native dialogs. Please avoid using the native dialogs."
exit 1;

else
echo "OK. No native dialogs found."
exit 0
fi