selectList 80%

This commit is contained in:
fofolee 2022-04-11 12:15:31 +08:00
parent 7a04d2108a
commit 1b55377c3c
4 changed files with 364 additions and 285 deletions

View File

@ -94,15 +94,6 @@ window.quickcommand = {
})
},
// 显示选项列表
showSelectList: function(selects, opt = {}) {
return new Promise((reslove, reject) => {})
},
// 更新选项列表
updateSelectList: function(opt, id) {
},
// 关闭进程
kill: function(pid, signal = 'SIGTERM') {
process.kill(pid, signal)
@ -683,4 +674,4 @@ runCodeFile = (cmd, option, terminal, callback) => {
// let stderr = err_chunks.join("");
// callback(stdout, stderr)
// })
}
}

View File

@ -4,43 +4,54 @@
maximized
ref="dialog"
transition-show="fade"
transition-hide="fade"
@hide="onDialogHide"
>
<q-scroll-area
ref="scrollbar"
:thumb-style="{
width: '7px',
}"
>
<q-card>
<q-virtual-scroll
virtual-scroll-slice-size="50"
virtual-scroll-item-size="50"
@virtual-scroll="scrollEvent"
:items="items"
>
<template v-slot="{ item, index }">
<q-item
:key="index"
ref="qitems"
clickable
v-ripple
@mousemove="currentIndex = index"
manual-focus
:focused="index === currentIndex"
:active="index === currentIndex"
:style="{
height: itemHeight + 'px',
}"
>
<q-item-section>
<q-item-section>{{ item }}</q-item-section>
</q-item-section>
</q-item>
</template>
</q-virtual-scroll>
</q-card>
</q-scroll-area>
<q-card>
<q-virtual-scroll
ref="scrollBar"
:style="{ maxHeight: listMaxHeight + 'px' }"
:virtual-scroll-slice-size="lazyItemSize"
:virtual-scroll-item-size="itemHeight"
@virtual-scroll="scrollEvent"
:items="matchedItems"
>
<template v-slot="{ item, index }">
<q-item
:key="index"
clickable
v-ripple
@mousemove="currentIndex = index"
@click="onOKClick"
manual-focus
:focused="index === currentIndex"
:active="index === currentIndex"
:style="{
height: itemHeight + 'px',
}"
>
<q-item-section v-if="isText">{{ item }}</q-item-section>
<q-item-section v-else-if="isJson">
<q-avatar v-if="item.icon">
<q-icon :name="item.icon" />
</q-avatar>
<q-item-label>{{ item.title }}</q-item-label>
<q-item-label caption>{{ item.description }}</q-item-label>
</q-item-section>
<q-item-section v-else-if="isHtml">
<div v-html="item"></div>
</q-item-section>
</q-item>
</template>
</q-virtual-scroll>
<q-btn
class="absolute-bottom-right q-ma-md"
round
color="primary"
icon="close"
@click="onCancelClick"
/>
</q-card>
</q-dialog>
</template>
@ -48,17 +59,44 @@
export default {
data() {
return {
result: this.items[0],
listMaxHeight: 500,
currentIndex: 0,
itemHeight: 50,
lazyItemSize: 50,
searchWords: "",
};
},
mounted() {
window.SelectList = this;
this.setSubInput();
this.setUtoolsHeight(this.itemHeight * this.itemSize);
},
computed: {
maxIndex() {
return this.items.length - 1;
itemSize() {
return this.items.length;
},
matchedItems() {
if (!this.searchWords) return this.items;
let matchedItems = this.items.filter((x) => {
if (typeof x === "string") {
return x.toLowerCase().includes(this.searchWords.toLowerCase());
}
return (
x.title.toLowerCase().includes(this.searchWords.toLowerCase()) ||
x.description.toLowerCase().includes(this.searchWords.toLowerCase())
);
});
this.setUtoolsHeight(this.itemHeight * matchedItems.length);
return matchedItems;
},
isJson() {
return this.options.optionType === "json";
},
isHtml() {
return this.options.optionType === "html";
},
isText() {
return this.options.optionType === "plaintext";
},
},
props: {
@ -70,6 +108,7 @@ export default {
show() {
this.$refs.dialog.show();
},
hide() {
this.$refs.dialog.hide();
},
@ -79,11 +118,20 @@ export default {
},
onOKClick() {
this.$emit("ok", this.result);
utools.removeSubInput();
let selected =
this.options.optionType === "json"
? this.matchedItems[this.currentIndex]
: {
id: this.currentIndex,
text: this.matchedItems[this.currentIndex],
};
this.$emit("ok", selected);
this.hide();
},
onCancelClick() {
utools.removeSubInput();
this.hide();
},
@ -94,13 +142,33 @@ export default {
this.currentIndex = Math.max(0, this.currentIndex - 1);
break;
case 40:
this.currentIndex = Math.min(this.maxIndex, this.currentIndex + 1);
this.currentIndex = Math.min(
this.itemSize - 1,
this.currentIndex + 1
);
break;
case 13:
this.onOKClick();
return;
}
// this.$refs.qitems[this.currentIndex].$el.scrollIntoViewIfNeeded(false);
this.$refs.scrollBar.scrollTo(this.currentIndex);
},
scrollEvent(e) {
console.log(e);
this.currentIndex =
e.direction === "increase"
? Math.max(e.index, this.currentIndex)
: Math.min(e.index + 9, this.currentIndex);
},
setSubInput() {
utools.setSubInput(({ text }) => {
this.searchWords = text;
}, this.options.placeholder);
},
setUtoolsHeight(height) {
utools.setExpendHeight(Math.min(height, this.listMaxHeight));
},
},
};

View File

@ -89,11 +89,9 @@ const quickcommand = {
})
}),
showSelectList: (selects, options = {
placeholder: "请选择",
optionType: "plaintext"
}) => new Promise((reslove, reject) => {
showSelectList: (selects, options = {}) => new Promise((reslove, reject) => {
if (!options.placeholder) options.placeholder = "输入进行筛选"
if (!options.optionType) options.optionType = "plaintext"
let props = {
items: selects,
options: options
@ -106,7 +104,12 @@ const quickcommand = {
}).onCancel(() => {
console.log('取消')
})
})
}),
// 更新选项列表
updateSelectList: (opt, id) => {
},
}
export default quickcommand
export default quickcommand

View File

@ -12,270 +12,287 @@ interface quickcommandApi {
* @param buttons
* @param title
*/
showButtonBox(buttons: array, title?: string): Promise<{ id: number, text: string }>;
showButtonBox(
buttons: array,
title?: string
): Promise<{ id: number; text: string }>;
/**
*
*
* ```js
* quickcommand.showInputBox(["输入框1", "输入框2", "输入框3"]).then(values => {
* console.log(`输入的内容分别为${values}`)
* })
*
* quickcommand.showInputBox({labels:["输入框标签"],values:["默认值"],hints:["输入框提示"]}).then(values => {
* console.log(`输入的内容分别为${values}`)
* })
* ```
*
*
*
* @param options
* @param title
*/
showInputBox(options: array | { labels: array, values: array, hints: array }, title?: string): Promise<array>;
*
*
* ```js
* quickcommand.showInputBox(["输入框1", "输入框2", "输入框3"]).then(values => {
* console.log(`输入的内容分别为${values}`)
* })
*
* quickcommand.showInputBox({labels:["输入框标签"],values:["默认值"],hints:["输入框提示"]}).then(values => {
* console.log(`输入的内容分别为${values}`)
* })
* ```
*
*
*
* @param options
* @param title
*/
showInputBox(
options: array | { labels: array; values: array; hints: array },
title?: string
): Promise<array>;
/**
*
*
* ```js
* // plaintext
* var opt = []
* for (var i = 0; i < 15; i++) {
* // 每一个选项为文本格式
* opt.push(`选项` + i)
* }
* quickcommand.showSelectList(opt).then(choise => {
* console.log(`选择的选项为${choise.text}`)
* })
*
* // json
* var opt = []
* for (var i = 0; i < 15; i++) {
* // 每一个选项为 json 格式
* opt.push({title: `选项${i}`, description: `选项${i}的描述`, abcd: `选项${i}的自定义属性`})
* }
* quickcommand.showSelectList(opt, {optionType: 'json'}).then(choise => {
* console.log(`选择的选项为${choise.title}`)
* })
*
* // html
* var opt = []
* for (var i = 0; i < 15; i++) {
* // 每一个选项为 html 格式
* opt.push(`<div style="color: red">选项${i}</div>`)
* }
* quickcommand.showSelectList(opt, {optionType: 'html'}).then(choise => {
* console.log(`选择的选项为${quickcommand.htmlParse(choise.text).body.innerText}`)
* })
* ```
*
* @param selects
* @param options placeholder: 搜索框占位符,optionType: 选项的格式plaintext
*/
showSelectList(selects: array, options?: { placeholder: string, optionType: 'plaintext' | 'html' | 'json' }): Promise<{ id: number, text: string }>;
* html时
*
* ```js
* // plaintext
* var opt = []
* for (var i = 0; i < 15; i++) {
* // 每一个选项为文本格式
* opt.push(`选项` + i)
* }
* quickcommand.showSelectList(opt).then(choise => {
* console.log(`选择的选项为${choise.text}`)
* })
*
* // json
* var opt = []
* for (var i = 0; i < 15; i++) {
* // 每一个选项为 json 格式
* opt.push({title: `选项${i}`, description: `选项${i}的描述`, icon: `选项${i}的图标`,abcd: `选项${i}的自定义属性`})
* }
* quickcommand.showSelectList(opt, {optionType: 'json'}).then(choise => {
* console.log(`选择的选项为${choise.title}`)
* })
*
* // html
* var opt = []
* for (var i = 0; i < 15; i++) {
* // 每一个选项为 html 格式
* opt.push(`<div style="color: red">选项${i}</div>`)
* }
* quickcommand.showSelectList(opt, {optionType: 'html'}).then(choise => {
* console.log(`选择的选项为${quickcommand.htmlParse(choise.text).body.innerText}`)
* })
* ```
*
* @param selects
* @param options placeholder: 搜索框占位符,optionType: 选项的格式plaintext
*/
showSelectList(
selects: array,
options?: { placeholder: string; optionType: "plaintext" | "html" | "json" }
): Promise<{ id: number; text: string | object }>;
/**
*
*
* ```js
* // 初始状态只有 1、2、3 三个选项
* quickcommand.showSelectList(['1','2','3']).then(x=>{
* console.log(x)
* })
*
* // 1s 后追加一个选项
* quickcommand.setTimeout(()=>{
* quickcommand.updateSelectList('4')
* }, 1000)
*
* // 2s 后更新第二个选项的值
* quickcommand.setTimeout(()=>{
* quickcommand.updateSelectList('updated', 1)
* }, 2000)
* ```
*
* @param opt
* @param id
*/
*
*
* ```js
* // 初始状态只有 1、2、3 三个选项
* quickcommand.showSelectList(['1','2','3']).then(x=>{
* console.log(x)
* })
*
* // 1s 后追加一个选项
* quickcommand.setTimeout(()=>{
* quickcommand.updateSelectList('4')
* }, 1000)
*
* // 2s 后更新第二个选项的值
* quickcommand.setTimeout(()=>{
* quickcommand.updateSelectList('updated', 1)
* }, 2000)
* ```
*
* @param opt
* @param id
*/
updateSelectList(opt: string, id?: number): void;
/**
*
*
* ```js
* quickcommand.showTextAera("请输入文本").then(text=>{
* console.log(`输入的文本为${text}`)
* })
* ```
*
* @param placeholder
* @param value
*/
*
*
* ```js
* quickcommand.showTextAera("请输入文本").then(text=>{
* console.log(`输入的文本为${text}`)
* })
* ```
*
* @param placeholder
* @param value
*/
showTextArea(placeholder?: string, value?: string): Promise<string>;
/**
*
*
* ```js
* quickcommand.showMessageBox("这是一段3s后自动消失的成功提示")
* ```
* @param message
* @param icon success
* @param time 3000
*/
showMessageBox(message: string, icon?: 'success' | 'error' | 'warning' | 'info' , time?: number): void;
*
*
* ```js
* quickcommand.showMessageBox("这是一段3s后自动消失的成功提示")
* ```
* @param message
* @param icon success
* @param time 3000
*/
showMessageBox(
message: string,
icon?: "success" | "error" | "warning" | "info",
time?: number
): void;
/**
*
*
* ```js
* quickcommand.showConfirmBox().then(confirmed => {
* confirmed && console.log('点击了确定')
* })
* ```
* @param message
* @param title
*/
*
*
* ```js
* quickcommand.showConfirmBox().then(confirmed => {
* confirmed && console.log('点击了确定')
* })
* ```
* @param message
* @param title
*/
showConfirmBox(message?: string, title?: string): Promise<boolean>;
/**
*
* @param ms
*/
*
* @param ms
*/
sleep(ms: number): void;
/**
*
*
* ```js
* quickcommand.setTimeout(()=>{
* console.log('2000毫秒后执行')
* }, 2000)
* ```
*
* @param ms
*/
setTimeout(callback: () => void, ms)
*
*
* ```js
* quickcommand.setTimeout(()=>{
* console.log('2000毫秒后执行')
* }, 2000)
* ```
*
* @param ms
*/
setTimeout(callback: () => void, ms);
/**
* html字符串解析为 DOM
*
* ```js
* var html = `<a href="https://u.tools/">uTools</a>`
* var href = quickcommand.htmlParse(html).querySelector('a').href
* console.log(`解析出来的a标签地址为${href}`)
* ```
*
* @param html html
*/
htmlParse(html: string): object
* html字符串解析为 DOM
*
* ```js
* var html = `<a href="https://u.tools/">uTools</a>`
* var href = quickcommand.htmlParse(html).querySelector('a').href
* console.log(`解析出来的a标签地址为${href}`)
* ```
*
* @param html html
*/
htmlParse(html: string): object;
/**
* Buffer
*
* ```js
* // 下载文件到D:/
* quickcommand.downloadFile('https://res.u-tools.cn/currentversion/uTools-1.1.3.exe', 'D:/')
*
* // 下载文件,并弹出对话框询问保存路径
* quickcommand.downloadFile('https://res.u-tools.cn/currentversion/uTools-1.1.3.exe')
* ```
*
* @param url
* @param file
*
* Object时 options utools.showSaveDialog options一致
*/
downloadFile(url, file?: string | object): Promise<Buffer>
* Buffer
*
* ```js
* // 下载文件到D:/
* quickcommand.downloadFile('https://res.u-tools.cn/currentversion/uTools-1.1.3.exe', 'D:/')
*
* // 下载文件,并弹出对话框询问保存路径
* quickcommand.downloadFile('https://res.u-tools.cn/currentversion/uTools-1.1.3.exe')
* ```
*
* @param url
* @param file
*
* Object时 options utools.showSaveDialog options一致
*/
downloadFile(url, file?: string | object): Promise<Buffer>;
/**
*
*
* ```js
* // 上传图片到图床
* quickcommand.uploadFile("https://imgkr.com/api/v2/files/upload", "C:\\test.jpg").then(res=>{
* console.log('上传成功,图片地址为:' + res.data.data)
* })
*
* // 包含额外表单数据
* quickcommand.uploadFile("https://catbox.moe/user/api.php", "C:\\test.jpg", 'fileToUpload', {
* "reqtype": "fileupload"
* }).then(res=>{
* console.log('上传成功,图片地址为:' + res.data)
* })
* ```
*
* @param url
* @param file
*
* Object时 options utools.showOpenDialog options一致
*
* @param name file
* @param formData
*/
uploadFile(url: string, file?: string | object, name?: string, formData?: object): Promise<object>
*
*
* ```js
* // 上传图片到图床
* quickcommand.uploadFile("https://imgkr.com/api/v2/files/upload", "C:\\test.jpg").then(res=>{
* console.log('上传成功,图片地址为:' + res.data.data)
* })
*
* // 包含额外表单数据
* quickcommand.uploadFile("https://catbox.moe/user/api.php", "C:\\test.jpg", 'fileToUpload', {
* "reqtype": "fileupload"
* }).then(res=>{
* console.log('上传成功,图片地址为:' + res.data)
* })
* ```
*
* @param url
* @param file
*
* Object时 options utools.showOpenDialog options一致
*
* @param name file
* @param formData
*/
uploadFile(
url: string,
file?: string | object,
name?: string,
formData?: object
): Promise<object>;
/**
* export
*
* ```js
* let remote = 'https://cdn.jsdelivr.net/npm/sweetalert2@9'
* quickcommand.loadRemoteScript(remote).then(swal => {
* swal.fire('已加载 sweetalert2 并成功弹窗')
* })
* ```
*
* @param url
*/
loadRemoteScript(url: string): Promise<object>
* export
*
* ```js
* let remote = 'https://cdn.jsdelivr.net/npm/sweetalert2@9'
* quickcommand.loadRemoteScript(remote).then(swal => {
* swal.fire('已加载 sweetalert2 并成功弹窗')
* })
* ```
*
* @param url
*/
loadRemoteScript(url: string): Promise<object>;
/**
* signal pid , process.kill
* @param pid ID
* @param signal SIGTERM
*/
kill(pid: number, signal?: number | string): void
* signal pid , process.kill
* @param pid ID
* @param signal SIGTERM
*/
kill(pid: number, signal?: number | string): void;
/**
* windows VBS
*
* ```js
* quickcommand.runVbs(`CreateObject("SAPI.SpVoice").Speak"Hello"`)
* ```
*
* @param script VBS
*/
runVbs(script: string): Promise<string>
* windows VBS
*
* ```js
* quickcommand.runVbs(`CreateObject("SAPI.SpVoice").Speak"Hello"`)
* ```
*
* @param script VBS
*/
runVbs(script: string): Promise<string>;
/**
* utools.onPluginEnter code type payload
*
* code: 唯一标识
*
* type: text | img | files | regex | over | window
*
* payload: 当匹配模式为关键字时
*
* ```js
* if (quickcommand.enterData.type == 'regex'){
* var text = quickcommand.enterData.payload
* console.log(`主输入框匹配的文本为${text}`)
* }
* ```
*
*/
enterData: { code: string, type: string, payload: any }
* utools.onPluginEnter code type payload
*
* code: 唯一标识
*
* type: text | img | files | regex | over | window
*
* payload: 当匹配模式为关键字时
*
* ```js
* if (quickcommand.enterData.type == 'regex'){
* var text = quickcommand.enterData.payload
* console.log(`主输入框匹配的文本为${text}`)
* }
* ```
*
*/
enterData: { code: string; type: string; payload: any };
/**
*
*/
simulateCopy()
*
*/
simulateCopy();
/**
*
*/
simulatePaste()
*
*/
simulatePaste();
}
declare var quickcommand: quickcommandApi;