mirror of
https://github.com/modrinth/code.git
synced 2026-09-02 04:56:52 +00:00
use Ace editor
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<NewModal ref="ruleModal" :header="modalTitle" @hide="handleRuleModalHide">
|
<NewModal ref="ruleModal" :header="modalTitle" :on-hide="handleRuleModalHide">
|
||||||
<form class="flex w-[48rem] max-w-full flex-col gap-3" @submit.prevent="saveRule">
|
<form class="flex w-[48rem] max-w-full flex-col gap-3" @submit.prevent="saveRule">
|
||||||
<label class="font-semibold text-contrast" for="rule-name">Name</label>
|
<label class="font-semibold text-contrast" for="rule-name">Name</label>
|
||||||
<StyledInput
|
<StyledInput
|
||||||
@@ -11,16 +11,27 @@
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<label class="font-semibold text-contrast" for="rule-expression">CEL expression</label>
|
<label class="font-semibold text-contrast" for="rule-expression">CEL expression</label>
|
||||||
<StyledInput
|
<div
|
||||||
id="rule-expression"
|
class="relative overflow-hidden rounded-[20px] border border-solid border-surface-4 shadow-sm"
|
||||||
v-model="form.rule"
|
>
|
||||||
type="text"
|
<component
|
||||||
multiline
|
:is="editorComponent"
|
||||||
resize="vertical"
|
v-if="editorComponent"
|
||||||
class="font-mono"
|
id="rule-expression"
|
||||||
input-class="min-h-64 font-mono"
|
:value="form.rule"
|
||||||
@input="queueRuleTest"
|
lang="javascript"
|
||||||
/>
|
theme="modrinth"
|
||||||
|
:print-margin="false"
|
||||||
|
:options="RULE_EDITOR_OPTIONS"
|
||||||
|
:style="{ height: '16rem', fontSize: '0.875rem' }"
|
||||||
|
class="ace-modrinth rounded-[20px]"
|
||||||
|
@init="onRuleEditorInit"
|
||||||
|
@update:value="handleRuleInput"
|
||||||
|
/>
|
||||||
|
<div v-else class="flex h-64 items-center justify-center bg-bg-raised">
|
||||||
|
<LoaderCircleIcon class="size-8 animate-spin text-secondary" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<p class="m-0 text-sm text-secondary">
|
<p class="m-0 text-sm text-secondary">
|
||||||
Return <code>null</code> when the rule does not match, or a map containing
|
Return <code>null</code> when the rule does not match, or a map containing
|
||||||
<code>severity</code> and/or <code>hidden</code> when it does.
|
<code>severity</code> and/or <code>hidden</code> when it does.
|
||||||
@@ -229,10 +240,17 @@ import {
|
|||||||
StyledInput,
|
StyledInput,
|
||||||
} from '@modrinth/ui'
|
} from '@modrinth/ui'
|
||||||
import { useDebounceFn } from '@vueuse/core'
|
import { useDebounceFn } from '@vueuse/core'
|
||||||
|
import type { Ace } from 'ace-builds'
|
||||||
|
import type { Component } from 'vue'
|
||||||
|
|
||||||
const DEFAULT_RULE = `input.trace.issue_type == "OBFUSCATED_NAMES"
|
const DEFAULT_RULE = `input.trace.issue_type == "OBFUSCATED_NAMES"
|
||||||
? {"severity": "low", "hidden": false}
|
? {"severity": "low", "hidden": false}
|
||||||
: null`
|
: null`
|
||||||
|
const RULE_EDITOR_OPTIONS: Partial<Ace.EditorOptions> = {
|
||||||
|
useWorker: false,
|
||||||
|
tabSize: 2,
|
||||||
|
useSoftTabs: true,
|
||||||
|
}
|
||||||
|
|
||||||
const TEST_TRACES: Labrinth.TechReview.Internal.TestDelphiRuleTrace[] = [
|
const TEST_TRACES: Labrinth.TechReview.Internal.TestDelphiRuleTrace[] = [
|
||||||
{
|
{
|
||||||
@@ -264,6 +282,8 @@ const client = injectModrinthClient()
|
|||||||
const { addNotification } = injectNotificationManager()
|
const { addNotification } = injectNotificationManager()
|
||||||
const ruleModal = useTemplateRef<InstanceType<typeof NewModal>>('ruleModal')
|
const ruleModal = useTemplateRef<InstanceType<typeof NewModal>>('ruleModal')
|
||||||
const deleteModal = useTemplateRef<InstanceType<typeof ConfirmModal>>('deleteModal')
|
const deleteModal = useTemplateRef<InstanceType<typeof ConfirmModal>>('deleteModal')
|
||||||
|
const editorComponent = shallowRef<Component | null>(null)
|
||||||
|
const ruleEditorInstance = shallowRef<Ace.Editor | null>(null)
|
||||||
|
|
||||||
const rules = ref<Labrinth.TechReview.Internal.DelphiRule[]>([])
|
const rules = ref<Labrinth.TechReview.Internal.DelphiRule[]>([])
|
||||||
const isLoading = ref(true)
|
const isLoading = ref(true)
|
||||||
@@ -281,6 +301,14 @@ const form = reactive({
|
|||||||
})
|
})
|
||||||
let ruleTestRequestId = 0
|
let ruleTestRequestId = 0
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
const [{ VAceEditor }] = await Promise.all([
|
||||||
|
import('vue3-ace-editor'),
|
||||||
|
import('@modrinth/ui/src/utils/ace-theme'),
|
||||||
|
])
|
||||||
|
editorComponent.value = VAceEditor
|
||||||
|
})
|
||||||
|
|
||||||
const modalTitle = computed(() => (editingRuleId.value === null ? 'Create rule' : 'Edit rule'))
|
const modalTitle = computed(() => (editingRuleId.value === null ? 'Create rule' : 'Edit rule'))
|
||||||
const previewExamples = computed(() =>
|
const previewExamples = computed(() =>
|
||||||
TEST_TRACES.map((original, index) => {
|
TEST_TRACES.map((original, index) => {
|
||||||
@@ -323,6 +351,16 @@ function getSeverityBadgeColor(severity: Labrinth.TechReview.Internal.DelphiSeve
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onRuleEditorInit(editor: Ace.Editor) {
|
||||||
|
ruleEditorInstance.value = editor
|
||||||
|
editor.session.setUseWrapMode(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRuleInput(rule: string) {
|
||||||
|
form.rule = rule
|
||||||
|
queueRuleTest()
|
||||||
|
}
|
||||||
|
|
||||||
async function testRule() {
|
async function testRule() {
|
||||||
if (!isRuleModalOpen.value) return
|
if (!isRuleModalOpen.value) return
|
||||||
|
|
||||||
@@ -388,6 +426,7 @@ function openCreateModal() {
|
|||||||
form.rule = DEFAULT_RULE
|
form.rule = DEFAULT_RULE
|
||||||
isRuleModalOpen.value = true
|
isRuleModalOpen.value = true
|
||||||
ruleModal.value?.show()
|
ruleModal.value?.show()
|
||||||
|
nextTick(() => ruleEditorInstance.value?.resize(true))
|
||||||
void testRule()
|
void testRule()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -397,6 +436,7 @@ function openEditModal(rule: Labrinth.TechReview.Internal.DelphiRule) {
|
|||||||
form.rule = rule.rule
|
form.rule = rule.rule
|
||||||
isRuleModalOpen.value = true
|
isRuleModalOpen.value = true
|
||||||
ruleModal.value?.show()
|
ruleModal.value?.show()
|
||||||
|
nextTick(() => ruleEditorInstance.value?.resize(true))
|
||||||
void testRule()
|
void testRule()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,6 +446,7 @@ function closeRuleModal() {
|
|||||||
|
|
||||||
function handleRuleModalHide() {
|
function handleRuleModalHide() {
|
||||||
isRuleModalOpen.value = false
|
isRuleModalOpen.value = false
|
||||||
|
ruleEditorInstance.value = null
|
||||||
ruleTestRequestId += 1
|
ruleTestRequestId += 1
|
||||||
isTestingRule.value = false
|
isTestingRule.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user