Skip to content

Commit

Permalink
✨ feat: support options
Browse files Browse the repository at this point in the history
  • Loading branch information
ccjr1120 committed Nov 2, 2024
1 parent 2e7ffda commit f8817d9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
29 changes: 21 additions & 8 deletions docs/components/playground/composables/use-editor/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { onMounted, Ref, ref, ShallowRef } from 'vue'
import useMonaco from './use-monaco'
import { DataMap } from '../../index'
import { DataMap } from '../..'

export default function useEditor({
el,
Expand All @@ -10,18 +10,31 @@ export default function useEditor({
dataMap: Ref<DataMap>
}) {
const activeDataKey = ref<keyof DataMap>('vertex')
const setMonacoValue = () => {
const value = dataMap.value[activeDataKey.value]
monaco?.editor.setModelLanguage(
editor!.getModel()!,
activeDataKey.value === 'options' ? 'typescript' : 'wgsl'
)
editor?.setValue(value)
}
const setDataMapValue = () => {
dataMap.value[activeDataKey.value] = editor?.getValue() || ''
}
const updateActiveDataKey = (key: keyof DataMap) => {
activeDataKey.value = key
monaco?.setValue(dataMap.value[key])
setMonacoValue()
}
let monaco: ReturnType<typeof useMonaco> | null = null
let monaco: ReturnType<typeof useMonaco>['monaco'] | null = null
let editor: ReturnType<typeof useMonaco>['editor'] | null = null
onMounted(() => {
if (!el.value) return
monaco = useMonaco(el.value)
monaco.setValue(dataMap.value[activeDataKey.value])
monaco.onDidChangeModelContent(() => {
const value = monaco?.getValue() || ''
dataMap.value[activeDataKey.value] = value
const monacoReturn = useMonaco(el.value)
monaco = monacoReturn.monaco
editor = monacoReturn.editor
setMonacoValue()
editor.onDidChangeModelContent(() => {
setDataMapValue()
})
})
return { dataMap, activeDataKey, updateActiveDataKey }
Expand Down
21 changes: 12 additions & 9 deletions docs/components/playground/composables/use-editor/use-monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker'
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'

export default function useMonaco(el: HTMLElement) {
return monaco.editor.create(el, {
theme: 'vs-dark',
value: '',
language: 'wgsl',
automaticLayout: true, // 自动调整大小
lineHeight: 32,
tabSize: 2,
fontSize: 22
})
return {
monaco: monaco,
editor: monaco.editor.create(el, {
theme: 'vs-dark',
value: '',
language: 'wgsl',
automaticLayout: true, // 自动调整大小
lineHeight: 32,
tabSize: 2,
fontSize: 22
})
}
}
self.MonacoEnvironment = {
getWorker(_, label) {
Expand Down
8 changes: 3 additions & 5 deletions docs/components/playground/composables/use-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ export default function useView({
const renderGPUMesh = _throttle(() => {
if (!el.value) return
el.value.innerHTML = ''
const options = eval(dataMap.value.options)
console.log(options)
new GPUMesh.Mesh(el.value, {
vertices: new Float32Array([
-0.8, -0.8, 0.8, -0.8, 0.8, 0.8,
// Triangle 2
-0.8, -0.8, 0.8, 0.8, -0.8, 0.8
]),
vertices: options.vertices,
shader: {
vertex: `${dataMap.value.vertex}`,
fragment: `${dataMap.value.fragment}`
Expand Down
16 changes: 16 additions & 0 deletions docs/components/playground/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import vert from './assets/vert.wgsl?raw'
import frag from './assets/frag.wgsl?raw'

export const DEFAULT_DATA = {
vertex: vert,
fragment: frag,
options: `(() => {
return {
vertices: new Float32Array([
-0.8, -0.8, 0.8, -0.8, 0.8, 0.8,
// Triangle 2
-0.8, -0.8, 0.8, 0.8, -0.8, 0.8
])
}
})()`
}
2 changes: 2 additions & 0 deletions docs/components/playground/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import { DEFAULT_DATA } from './constants'

export type DataMap = typeof DEFAULT_DATA
7 changes: 1 addition & 6 deletions docs/components/playground/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
import { ref, useTemplateRef } from 'vue'
import useView from './composables/use-view'
import useEditor from './composables/use-editor/index'
import vert from './assets/vert.wgsl?raw'
import frag from './assets/frag.wgsl?raw'
import { DataMap } from './index'
import { DEFAULT_DATA } from './constants'
const DEFAULT_DATA = {
vertex: vert,
fragment: frag
}
const dataMap = ref<DataMap>(DEFAULT_DATA)
const viewRef = useTemplateRef<HTMLElement>('viewRef')
Expand Down

0 comments on commit f8817d9

Please sign in to comment.