Skip to content

Commit 9cef8fa

Browse files
committed
feat(app): common resolvers for script blocks
1 parent 609f287 commit 9cef8fa

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

packages/app/modules/block-editor/components/Block.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const icon = defineProp<string>('icon', {
8686

8787
<div
8888
class="flex-1 overflow-auto"
89-
:class="isControlPressed ? 'pointer-events-none opacity-75' : ''"
89+
:class="isControlPressed ? 'pointer-events-none' : ''"
9090
:inert="isControlPressed"
9191
>
9292
<slot />

packages/app/modules/block-editor/components/BlockChart.vue

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ToolbarBtn from './ToolbarBtn.vue'
99
import Block from './Block.vue'
1010
import ToolbarAlignment from './ToolbarAlignment.vue'
1111
import { useCss } from '@composables/css'
12+
import { resolvers } from '@modules/block-editor/composables/resolvers'
1213
1314
const MonacoEditor = defineAsyncComponent(() => import('@modules/monaco/components/MEditor.vue'))
1415
@@ -25,6 +26,8 @@ const chartLoading = ref(true)
2526
const evaluation = useEvaluation()
2627
const evaluationOutput = ref<string[]>([])
2728
29+
evaluation.addResolver(...resolvers)
30+
2831
evaluation.addResolver(
2932
defineResolver({
3033
test: (id) => id === 'app:chart',
@@ -58,8 +61,10 @@ async function setChart() {
5861
5962
await runtime.onDone()
6063
64+
console.log(chartOptions)
65+
6166
if (runtime.evaluation.stderr) {
62-
mode.value = 'debug'
67+
config.value.mode = 'debug'
6368
}
6469
6570
setTimeout(() => (chartLoading.value = false), 300)
@@ -80,6 +85,7 @@ async function update() {
8085
node.name = 'chart'
8186
node.body = code.value
8287
node.attrs = config.value
88+
node.meta = model.value.meta
8389
8490
let attrsAsString = Object.entries(node.attrs)
8591
.map(([key, value]) => `${key}="${value}"`)
@@ -95,8 +101,6 @@ async function update() {
95101
includeEndOfFileToken: false,
96102
})
97103
98-
node.meta = model.value.meta
99-
100104
model.value = node
101105
}
102106
@@ -117,18 +121,16 @@ watch(
117121
}
118122
)
119123
120-
// modes
121-
const mode = ref<'chart' | 'debug' | 'dataset'>('chart')
122-
123124
// attrs
124125
125126
const css = useCss()
126127
const loadingAttrs = ref(false)
127128
128129
const config = ref({
129130
height: '500',
130-
width: '500',
131+
width: '100%',
131132
align: 'left',
133+
mode: 'chart' as 'chart' | 'debug' | 'dataset',
132134
})
133135
134136
const style = computed(() => {
@@ -147,9 +149,10 @@ watch(
147149
148150
const { attrs } = model.value
149151
150-
config.value.height = attrs.height || '500'
151-
config.value.width = attrs.width || '500'
152-
config.value.align = attrs.align || 'left'
152+
config.value.height = attrs.height || config.value.height
153+
config.value.width = attrs.width || config.value.width
154+
config.value.align = attrs.align || config.value.align
155+
config.value.mode = (attrs.mode as any) || config.value.mode
153156
154157
loadingAttrs.value = false
155158
},
@@ -185,9 +188,9 @@ watch(
185188
<template #activator="{ attrs }">
186189
<ToolbarBtn
187190
v-bind="attrs"
188-
:active="mode === 'chart'"
191+
:active="config.mode === 'chart'"
189192
data-test-id="toolbar-chart-btn"
190-
@click="mode = 'chart'"
193+
@click="config.mode = 'chart'"
191194
>
192195
<v-icon name="chart-pie" />
193196
</ToolbarBtn>
@@ -208,9 +211,9 @@ watch(
208211
<template #activator="{ attrs }">
209212
<ToolbarBtn
210213
v-bind="attrs"
211-
:active="mode === 'debug'"
214+
:active="config.mode === 'debug'"
212215
data-test-id="toolbar-debug-btn"
213-
@click="mode = 'debug'"
216+
@click="config.mode = 'debug'"
214217
>
215218
<v-icon name="bug" />
216219
</ToolbarBtn>
@@ -222,9 +225,9 @@ watch(
222225
<template #activator="{ attrs }">
223226
<ToolbarBtn
224227
v-bind="attrs"
225-
:active="mode === 'dataset'"
228+
:active="config.mode === 'dataset'"
226229
data-test-id="toolbar-dataset-btn"
227-
@click="mode = 'dataset'"
230+
@click="config.mode = 'dataset'"
228231
>
229232
<v-icon name="mdi:code-json" />
230233
</ToolbarBtn>
@@ -266,10 +269,10 @@ watch(
266269
<v-icon name="chart-pie" class="text-[10rem] text-t-secondary" />
267270
</div>
268271

269-
<v-chart v-else-if="mode === 'chart'" :style="style" :options="chartOptions" />
272+
<v-chart v-else-if="config.mode === 'chart'" :style="style" :options="chartOptions" />
270273

271-
<template v-else-if="mode === 'debug'">
272-
<div :style="style" class="bg-b-03 p-4">
274+
<template v-else-if="config.mode === 'debug'">
275+
<div :style="{ height: style.height }" class="bg-b-03 p-4 w-full">
273276
<ANSICard :model-value="evaluationOutput" data-test-id="debug-view" />
274277

275278
<div
@@ -288,12 +291,13 @@ watch(
288291
</template>
289292

290293
<MonacoEditor
291-
v-else-if="mode === 'dataset'"
294+
v-else-if="config.mode === 'dataset'"
292295
data-test-id="dataset-view"
293296
:model-value="inspect(chartOptions)"
294297
language="json"
295298
folding
296299
readonly
300+
:style="{ height: style.height }"
297301
:padding="{
298302
top: 8,
299303
bottom: 8,

packages/app/modules/block-editor/components/BlockScript.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { useEvaluation } from '@modules/evaluation/composables/use-evaluation'
55
import Block from './Block.vue'
66
import ANSICard from '@modules/evaluation/components/ANSICard.vue'
77
import ToolbarBtn from './ToolbarBtn.vue'
8+
import { useStore } from '@modules/entry/store'
9+
import { resolvers } from '@modules/block-editor/composables/resolvers'
810
911
const MonacoEditor = defineAsyncComponent(() => import('@modules/monaco/components/MEditor.vue'))
1012
@@ -19,6 +21,10 @@ const code = ref(model.value.body)
1921
const evaluation = useEvaluation()
2022
const output = ref<string[]>([])
2123
24+
const drive = useStore()
25+
26+
evaluation.setResolvers(resolvers)
27+
2228
async function run() {
2329
running.value = true
2430
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import DirectoryEntry from '@core/entities/directory-entry'
2+
import { useStore } from '@modules/entry/store'
3+
import { defineResolver } from '@modules/evaluation/helpers/define-resolver'
4+
import { useItemStore } from '@modules/item/store'
5+
6+
export const resolvers = [
7+
defineResolver({
8+
test: (id) => id === 'app:drive',
9+
resolve: async () => {
10+
return {
11+
useDrive: () => useStore(),
12+
decode: DirectoryEntry.decode,
13+
encode: DirectoryEntry.encode,
14+
}
15+
},
16+
}),
17+
defineResolver({
18+
test: (id) => id === 'app:collection',
19+
resolve: async () => {
20+
return {
21+
useCollection: (id: string) => useItemStore(id),
22+
}
23+
},
24+
}),
25+
defineResolver({
26+
test: (id) => id.startsWith('npm:'),
27+
resolve: async (id) => {
28+
const name = id.replace('npm:', '')
29+
30+
const url = `https://unpkg.com/${name}?module`
31+
32+
return import(/* @vite-ignore */ url)
33+
},
34+
}),
35+
]

packages/app/modules/evaluation/resolvers/npm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default defineResolver({
44
test: (id) => id.startsWith('npm:'),
55
resolve: async (id) => {
66
const name = id.replace('npm:', '')
7-
const version = 'latest'
7+
88
const url = `https://unpkg.com/${name}?module`
99

1010
return import(/* @vite-ignore */ url)

0 commit comments

Comments
 (0)