Capture options

This commit is contained in:
Adria Navarro 2025-02-13 13:54:15 +01:00
parent 47a0f6e9cf
commit 60b605e6f0
1 changed files with 38 additions and 47 deletions

View File

@ -58,7 +58,7 @@
export let placeholder = null export let placeholder = null
export let showTabBar = true export let showTabBar = true
let mode: BindingMode | null let mode: BindingMode
let sidePanel: SidePanel | null let sidePanel: SidePanel | null
let initialValueJS = value?.startsWith?.("{{ js ") let initialValueJS = value?.startsWith?.("{{ js ")
let jsValue: string | null = initialValueJS ? value : null let jsValue: string | null = initialValueJS ? value : null
@ -71,6 +71,7 @@
let expressionError: string | undefined let expressionError: string | undefined
let evaluating = false let evaluating = false
let completions: BindingCompletion[] = [] let completions: BindingCompletion[] = []
let autocompleteOptions: BindingCompletionOption[] = []
$: useSnippets = allowSnippets && !$licensing.isFreePlan $: useSnippets = allowSnippets && !$licensing.isFreePlan
$: editorModeOptions = getModeOptions(allowHBS, allowJS) $: editorModeOptions = getModeOptions(allowHBS, allowJS)
@ -89,22 +90,44 @@
| null | null
$: runtimeExpression = readableToRuntimeBinding(enrichedBindings, value) $: runtimeExpression = readableToRuntimeBinding(enrichedBindings, value)
$: requestEval(runtimeExpression, context, snippets) $: requestEval(runtimeExpression, context, snippets)
$: bindingCompletions = bindingsToCompletions(enrichedBindings, editorMode)
$: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos) $: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos)
$: { function getOptions(
if (mode === BindingMode.Text) { mode: typeof editorMode,
completions = getHBSCompletions(bindingCompletions) bindings: EnrichedBinding[]
} else if (mode === BindingMode.JavaScript) { ): [BindingCompletionOption[], BindingCompletion[]] {
completions = getJSCompletions(bindingCompletions, snippets, { const autocompleteOptions = []
useHelpers: allowHelpers, const completions = []
useSnippets,
}) const bindingOptions = bindingsToCompletions(bindings, editorMode)
} else { const helperOptions = getHelperCompletions(mode)
completions = []
if (mode.name === "handlebars") {
autocompleteOptions.push(...bindingOptions)
autocompleteOptions.push(...helperOptions)
completions.push(hbAutocomplete(autocompleteOptions))
} else if (mode.name === "javascript") {
if (bindingOptions.length) {
completions.push(jsAutocomplete(bindingOptions))
}
if (allowHelpers) {
completions.push(jsHelperAutocomplete(helperOptions))
}
if (useSnippets && snippets?.length) {
completions.push(snippetAutoComplete(snippets))
} }
} }
return [autocompleteOptions, completions]
}
$: [autocompleteOptions, completions] = getOptions(
editorMode,
enrichedBindings
)
$: { $: {
// Ensure a valid side panel option is always selected // Ensure a valid side panel option is always selected
if (sidePanel && !sidePanelOptions.includes(sidePanel)) { if (sidePanel && !sidePanelOptions.includes(sidePanel)) {
@ -112,38 +135,6 @@
} }
} }
const getHBSCompletions = (bindingCompletions: BindingCompletionOption[]) => {
return [
hbAutocomplete([
...bindingCompletions,
...getHelperCompletions(EditorModes.Handlebars),
]),
]
}
const getJSCompletions = (
bindingCompletions: BindingCompletionOption[],
snippets: Snippet[] | null,
config: {
useHelpers: boolean
useSnippets: boolean
}
) => {
const completions: BindingCompletion[] = []
if (bindingCompletions.length) {
completions.push(jsAutocomplete([...bindingCompletions]))
}
if (config.useHelpers) {
completions.push(
jsHelperAutocomplete([...getHelperCompletions(EditorModes.JS)])
)
}
if (config.useSnippets && snippets) {
completions.push(snippetAutoComplete(snippets))
}
return completions
}
const getModeOptions = (allowHBS: boolean, allowJS: boolean) => { const getModeOptions = (allowHBS: boolean, allowJS: boolean) => {
let options = [] let options = []
if (allowHBS) { if (allowHBS) {
@ -223,7 +214,7 @@
bindings: EnrichedBinding[], bindings: EnrichedBinding[],
context: any, context: any,
snippets: Snippet[] | null snippets: Snippet[] | null
) => { ): EnrichedBinding[] => {
// Create a single big array to enrich in one go // Create a single big array to enrich in one go
const bindingStrings = bindings.map(binding => { const bindingStrings = bindings.map(binding => {
if (binding.runtimeBinding.startsWith('trim "')) { if (binding.runtimeBinding.startsWith('trim "')) {
@ -300,7 +291,7 @@
jsValue = null jsValue = null
hbsValue = null hbsValue = null
updateValue(null) updateValue(null)
mode = targetMode mode = targetMode!
targetMode = null targetMode = null
} }
@ -400,7 +391,7 @@
autofocus={autofocusEditor} autofocus={autofocusEditor}
placeholder={placeholder || placeholder={placeholder ||
"Add bindings by typing $ or use the menu on the right"} "Add bindings by typing $ or use the menu on the right"}
jsBindingWrapping={bindingCompletions.length > 0} jsBindingWrapping={completions.length > 0}
/> />
{/key} {/key}
{/if} {/if}