refactor(client): refactor scratchpad to use Composition API (#8565)
This commit is contained in:
		
							parent
							
								
									8f32064fea
								
							
						
					
					
						commit
						5ad42d1d85
					
				
					 1 changed files with 75 additions and 88 deletions
				
			
		|  | @ -6,20 +6,20 @@ | ||||||
| 	</div> | 	</div> | ||||||
| 
 | 
 | ||||||
| 	<MkContainer :foldable="true" class="_gap"> | 	<MkContainer :foldable="true" class="_gap"> | ||||||
| 		<template #header>{{ $ts.output }}</template> | 		<template #header>{{ i18n.ts.output }}</template> | ||||||
| 		<div class="bepmlvbi"> | 		<div class="bepmlvbi"> | ||||||
| 			<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div> | 			<div v-for="log in logs" :key="log.id" class="log" :class="{ print: log.print }">{{ log.text }}</div> | ||||||
| 		</div> | 		</div> | ||||||
| 	</MkContainer> | 	</MkContainer> | ||||||
| 
 | 
 | ||||||
| 	<div class="_gap"> | 	<div class="_gap"> | ||||||
| 		{{ $ts.scratchpadDescription }} | 		{{ i18n.ts.scratchpadDescription }} | ||||||
| 	</div> | 	</div> | ||||||
| </div> | </div> | ||||||
| </template> | </template> | ||||||
| 
 | 
 | ||||||
| <script lang="ts"> | <script lang="ts" setup> | ||||||
| import { defineComponent } from 'vue'; | import { defineExpose, ref, watch } from 'vue'; | ||||||
| import 'prismjs'; | import 'prismjs'; | ||||||
| import { highlight, languages } from 'prismjs/components/prism-core'; | import { highlight, languages } from 'prismjs/components/prism-core'; | ||||||
| import 'prismjs/components/prism-clike'; | import 'prismjs/components/prism-clike'; | ||||||
|  | @ -27,50 +27,32 @@ import 'prismjs/components/prism-javascript'; | ||||||
| import 'prismjs/themes/prism-okaidia.css'; | import 'prismjs/themes/prism-okaidia.css'; | ||||||
| import { PrismEditor } from 'vue-prism-editor'; | import { PrismEditor } from 'vue-prism-editor'; | ||||||
| import 'vue-prism-editor/dist/prismeditor.min.css'; | import 'vue-prism-editor/dist/prismeditor.min.css'; | ||||||
| import { AiScript, parse, utils, values } from '@syuilo/aiscript'; | import { AiScript, parse, utils } from '@syuilo/aiscript'; | ||||||
| import MkContainer from '@/components/ui/container.vue'; | import MkContainer from '@/components/ui/container.vue'; | ||||||
| import MkButton from '@/components/ui/button.vue'; | import MkButton from '@/components/ui/button.vue'; | ||||||
| import { createAiScriptEnv } from '@/scripts/aiscript/api'; | import { createAiScriptEnv } from '@/scripts/aiscript/api'; | ||||||
| import * as os from '@/os'; | import * as os from '@/os'; | ||||||
| import * as symbols from '@/symbols'; | import * as symbols from '@/symbols'; | ||||||
|  | import { $i } from '@/account'; | ||||||
|  | import { i18n } from '@/i18n'; | ||||||
| 
 | 
 | ||||||
| export default defineComponent({ | const code = ref(''); | ||||||
| 	components: { | const logs = ref<any[]>([]); | ||||||
| 		MkContainer, |  | ||||||
| 		MkButton, |  | ||||||
| 		PrismEditor, |  | ||||||
| 	}, |  | ||||||
| 
 | 
 | ||||||
| 	data() { | const saved = localStorage.getItem('scratchpad'); | ||||||
| 		return { | if (saved) { | ||||||
| 			[symbols.PAGE_INFO]: { | 	code.value = saved; | ||||||
| 				title: this.$ts.scratchpad, | } | ||||||
| 				icon: 'fas fa-terminal', |  | ||||||
| 			}, |  | ||||||
| 			code: '', |  | ||||||
| 			logs: [], |  | ||||||
| 		} |  | ||||||
| 	}, |  | ||||||
| 
 | 
 | ||||||
| 	watch: { | watch(code, () => { | ||||||
| 		code() { | 	localStorage.setItem('scratchpad', code.value); | ||||||
| 			localStorage.setItem('scratchpad', this.code); | }); | ||||||
| 		} |  | ||||||
| 	}, |  | ||||||
| 
 | 
 | ||||||
| 	created() { | async function run() { | ||||||
| 		const saved = localStorage.getItem('scratchpad'); | 	logs.value = []; | ||||||
| 		if (saved) { |  | ||||||
| 			this.code = saved; |  | ||||||
| 		} |  | ||||||
| 	}, |  | ||||||
| 
 |  | ||||||
| 	methods: { |  | ||||||
| 		async run() { |  | ||||||
| 			this.logs = []; |  | ||||||
| 	const aiscript = new AiScript(createAiScriptEnv({ | 	const aiscript = new AiScript(createAiScriptEnv({ | ||||||
| 		storageKey: 'scratchpad', | 		storageKey: 'scratchpad', | ||||||
| 				token: this.$i?.token, | 		token: $i?.token, | ||||||
| 	}), { | 	}), { | ||||||
| 		in: (q) => { | 		in: (q) => { | ||||||
| 			return new Promise(ok => { | 			return new Promise(ok => { | ||||||
|  | @ -82,7 +64,7 @@ export default defineComponent({ | ||||||
| 			}); | 			}); | ||||||
| 		}, | 		}, | ||||||
| 		out: (value) => { | 		out: (value) => { | ||||||
| 					this.logs.push({ | 			logs.value.push({ | ||||||
| 				id: Math.random(), | 				id: Math.random(), | ||||||
| 				text: value.type === 'str' ? value.value : utils.valToString(value), | 				text: value.type === 'str' ? value.value : utils.valToString(value), | ||||||
| 				print: true | 				print: true | ||||||
|  | @ -90,7 +72,7 @@ export default defineComponent({ | ||||||
| 		}, | 		}, | ||||||
| 		log: (type, params) => { | 		log: (type, params) => { | ||||||
| 			switch (type) { | 			switch (type) { | ||||||
| 						case 'end': this.logs.push({ | 				case 'end': logs.value.push({ | ||||||
| 					id: Math.random(), | 					id: Math.random(), | ||||||
| 					text: utils.valToString(params.val, true), | 					text: utils.valToString(params.val, true), | ||||||
| 					print: false | 					print: false | ||||||
|  | @ -102,8 +84,8 @@ export default defineComponent({ | ||||||
| 
 | 
 | ||||||
| 	let ast; | 	let ast; | ||||||
| 	try { | 	try { | ||||||
| 				ast = parse(this.code); | 		ast = parse(code.value); | ||||||
| 			} catch (e) { | 	} catch (error) { | ||||||
| 		os.alert({ | 		os.alert({ | ||||||
| 			type: 'error', | 			type: 'error', | ||||||
| 			text: 'Syntax error :(' | 			text: 'Syntax error :(' | ||||||
|  | @ -112,18 +94,23 @@ export default defineComponent({ | ||||||
| 	} | 	} | ||||||
| 	try { | 	try { | ||||||
| 		await aiscript.exec(ast); | 		await aiscript.exec(ast); | ||||||
| 			} catch (e) { | 	} catch (error: any) { | ||||||
| 		os.alert({ | 		os.alert({ | ||||||
| 			type: 'error', | 			type: 'error', | ||||||
| 					text: e | 			text: error.message | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| 		}, | }; | ||||||
| 
 | 
 | ||||||
| 		highlighter(code) { | function highlighter(code) { | ||||||
| 	return highlight(code, languages.js, 'javascript'); | 	return highlight(code, languages.js, 'javascript'); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | defineExpose({ | ||||||
|  | 	[symbols.PAGE_INFO]: { | ||||||
|  | 		title: i18n.ts.scratchpad, | ||||||
|  | 		icon: 'fas fa-terminal', | ||||||
| 	}, | 	}, | ||||||
| 	} |  | ||||||
| }); | }); | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue