Skip to content

Commit 0a2ea1f

Browse files
author
Guillaume Chau
committed
feat(ui): devtools suggestion
1 parent 9d1bfc5 commit 0a2ea1f

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

docs/dev-guide/ui-api.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,8 @@ api.addSuggestion({
11541154
// This will be displayed in a details modal
11551155
message: 'A longer message for the modal',
11561156
link: 'http://link-to-docs-in-the-modal',
1157+
// Optional image
1158+
image: '/_plugin/my-package/screenshot.png',
11571159
// Function called when suggestion is activated by user
11581160
async handler () {
11591161
// ...
@@ -1173,6 +1175,18 @@ Then you can remove the suggestion:
11731175
api.removeSuggestion('my-suggestion')
11741176
```
11751177

1178+
You can also open a page instead when the user activates the suggestion with `actionLink`:
1179+
1180+
```js
1181+
api.addSuggestion({
1182+
id: 'my-suggestion',
1183+
type: 'action', // Required
1184+
label: 'Add vue-router',
1185+
// Open a new tab
1186+
actionLink: 'https://vuejs.org/'
1187+
})
1188+
```
1189+
11761190
Typically, you will use hooks to display the suggestion in the right context:
11771191

11781192
```js

packages/@vue/cli-ui/locales/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@
359359
"label": "Add vuex",
360360
"message": "Official centralized State Management solution for large-scale apps. Very useful if multiple components need to access the same data."
361361
},
362+
"vue-devtools": {
363+
"label": "Install devtools",
364+
"message": "Browser devtools official extension for debugging Vue.js applications where you can inspect your components, vuex store and events."
365+
},
362366
"progress": "Installing {arg0}..."
363367
}
364368
},

packages/@vue/cli-ui/src/components/SuggestionBar.vue

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<template slot-scope="{ result: { data } }" v-if="data">
3636
<VueDropdown
37-
v-for="suggestion of data.suggestions"
37+
v-for="suggestion of withBuiltins(data.suggestions)"
3838
:key="suggestion.id"
3939
:disabled="!suggestion.message && !suggestion.link"
4040
class="suggestion"
@@ -60,6 +60,13 @@
6060
v-html="$t(suggestion.message)"
6161
/>
6262

63+
<div
64+
v-if="suggestion.image"
65+
class="info image"
66+
>
67+
<img :src="suggestion.image" alt="image">
68+
</div>
69+
6370
<div class="actions-bar">
6471
<VueButton
6572
:href="suggestion.link"
@@ -94,15 +101,54 @@ import SUGGESTION_ACTIVATE from '../graphql/suggestionActivate.gql'
94101
export default {
95102
methods: {
96103
async activate (suggestion) {
97-
this.showDetails = false
98-
await this.$apollo.mutate({
99-
mutation: SUGGESTION_ACTIVATE,
100-
variables: {
101-
input: {
102-
id: suggestion.id
104+
if (suggestion.actionLink) {
105+
const win = window.open(
106+
suggestion.actionLink,
107+
'_blank'
108+
)
109+
win.focus()
110+
} else {
111+
await this.$apollo.mutate({
112+
mutation: SUGGESTION_ACTIVATE,
113+
variables: {
114+
input: {
115+
id: suggestion.id
116+
}
103117
}
118+
})
119+
}
120+
},
121+
122+
// Builtin suggestions
123+
withBuiltins (suggestions) {
124+
let list = suggestions
125+
126+
// Install devtools
127+
if (!Object.prototype.hasOwnProperty.call(window, '__VUE_DEVTOOLS_GLOBAL_HOOK__')) {
128+
let devtoolsLink = null
129+
if (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)) {
130+
devtoolsLink = 'https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd'
131+
} else if (/Firefox/.test(navigator.userAgent)) {
132+
devtoolsLink = 'https://addons.mozilla.org/en-US/firefox/addon/vue-js-devtools/'
104133
}
105-
})
134+
135+
if (devtoolsLink) {
136+
list = [
137+
...list,
138+
{
139+
id: 'vue-devtools',
140+
type: 'action',
141+
label: 'cli-service.suggestions.vue-devtools.label',
142+
message: 'cli-service.suggestions.vue-devtools.message',
143+
link: 'https://github.com/vuejs/vue-devtools',
144+
image: 'https://raw.githubusercontent.com/vuejs/vue-devtools/master/media/screenshot.png',
145+
actionLink: devtoolsLink
146+
}
147+
]
148+
}
149+
}
150+
151+
return list
106152
}
107153
}
108154
}
@@ -130,4 +176,8 @@ export default {
130176
.info
131177
&:not(:last-child)
132178
margin-bottom $padding-item
179+
180+
&.image
181+
>>> img
182+
max-width 100%
133183
</style>

packages/@vue/cli-ui/src/graphql-api/api/suggestion.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const schema = createSchema(joi => ({
44
id: joi.string().required(),
55
label: joi.string().required(),
66
type: joi.string().required(),
7-
handler: joi.func().required(),
7+
handler: joi.func(),
8+
actionLink: joi.string(),
89
importance: joi.string(),
910
message: joi.string(),
10-
link: joi.string()
11+
link: joi.string(),
12+
image: joi.string()
1113
}))
1214

1315
exports.validateSuggestion = (options) => {

0 commit comments

Comments
 (0)