Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): add customizable actions to Monaco editor components #1865

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/ui/src/components/MonacoEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ const props = withDefaults(
value?: monaco.editor.IStandaloneEditorConstructionOptions['value']
language?: monaco.editor.IStandaloneEditorConstructionOptions['language']
options?: Partial<monaco.editor.IStandaloneEditorConstructionOptions>
actions?: monaco.editor.IActionDescriptor[]
}>(),
{
value: '',
language: 'json',
options: () => ({}),
actions: () => [],
},
)

const emit = defineEmits<{
(e: 'update', value: string, event: monaco.editor.IModelContentChangedEvent): void
}>()

const { value, language, options } = toRefs(props)
const { value, language, options, actions } = toRefs(props)

const editorRef = ref<HTMLElement | null>(null)
// The editor cannot use ref, using ref will cause the editor to completely freeze when calling getValue or setValue
Expand Down Expand Up @@ -107,6 +109,10 @@ function initMonacoEditor() {
emit('update', editorValue, event)
}
})

actions.value.forEach((action) => {
editor!.addAction(action)
})
}

watch(value, (newValue) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/ui/src/components/script/function/Editor.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import type { ScriptFunction } from 'mqttx'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import { useScriptFunctionStore } from '../../../stores'

const modelLang = defineModel<ScriptFunction['lang']>('lang', { default: 'javascript' })
Expand Down Expand Up @@ -198,6 +199,16 @@ async function uploadFunction(file: { name: string, content: string }) {
// Must set functionContent after currentFunction is updated to avoid conflict with the assignment in watch
functionContent.value = file.content
}

const editorActions: monaco.editor.IActionDescriptor[] = [
{
id: 'save',
label: t('common.save'),
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS],
run: () => handleClickSave(),
contextMenuGroupId: 'navigation',
},
]
</script>

<template>
Expand Down Expand Up @@ -263,6 +274,7 @@ async function uploadFunction(file: { name: string, content: string }) {
:value="functionContent"
:language="currentLang"
:options="{ lineNumbers: 'on' }"
:actions="editorActions"
@update="functionContent = $event"
/>
</section>
Expand Down
12 changes: 12 additions & 0 deletions packages/ui/src/components/script/schema/Editor.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import type { ScriptSchema } from 'mqttx'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import { useScriptSchemaStore } from '../../../stores'

const modelCodec = defineModel<ScriptSchema['codec']>('codec', { default: 'protobuf' })
Expand Down Expand Up @@ -204,6 +205,16 @@ async function uploadSchema(file: { name: string, content: string }) {
// Must set schemaContent after currentSchema is updated to avoid conflict with the assignment in watch
schemaContent.value = file.content
}

const editorActions: monaco.editor.IActionDescriptor[] = [
{
id: 'save',
label: t('common.save'),
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS],
run: () => handleClickSave(),
contextMenuGroupId: 'navigation',
},
]
</script>

<template>
Expand Down Expand Up @@ -269,6 +280,7 @@ async function uploadSchema(file: { name: string, content: string }) {
:value="schemaContent"
:language="defaultSchema[currentCodec].editorLangugage"
:options="{ lineNumbers: 'on' }"
:actions="editorActions"
@update="schemaContent = $event"
/>
</section>
Expand Down
Loading