diff --git a/src/constants/SnippetVariables.ts b/src/constants/SnippetVariables.ts new file mode 100644 index 00000000..5cdc2f2c --- /dev/null +++ b/src/constants/SnippetVariables.ts @@ -0,0 +1,7 @@ + + +export const SnippetVariables = { + FM_SELECTED_TEXT: 'FM_SELECTED_TEXT', + FM_TEXT: 'FM_TEXT_', + FM_MULTILINE: 'FM_MULTILINE_', +}; \ No newline at end of file diff --git a/src/constants/index.ts b/src/constants/index.ts index 9ae33331..4c6ae167 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -7,7 +7,9 @@ export * from './FrameworkDetectors'; export * from './Links'; export * from './LocalStore'; export * from './Navigation'; +export * from './SnippetVariables'; export * from './TelemetryEvent'; +export * from './charCode'; export * from './charMap'; export * from './context'; export * from './settings'; diff --git a/src/dashboardWebView/components/SnippetsView/NewForm.tsx b/src/dashboardWebView/components/SnippetsView/NewForm.tsx index fc319203..6d866231 100644 --- a/src/dashboardWebView/components/SnippetsView/NewForm.tsx +++ b/src/dashboardWebView/components/SnippetsView/NewForm.tsx @@ -1,5 +1,6 @@ import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/outline'; import * as React from 'react'; +import { SnippetVariables } from '../../../constants'; export interface INewFormProps { title: string; @@ -84,7 +85,7 @@ export const NewForm: React.FunctionComponent = ({ title, descrip
Insert selected text (can still be updated)
-
{`\${selection}`}
+
{`\${${SnippetVariables.FM_SELECTED_TEXT}}`}
diff --git a/src/dashboardWebView/components/SnippetsView/SnippetForm.tsx b/src/dashboardWebView/components/SnippetsView/SnippetForm.tsx index 9e673565..f476f471 100644 --- a/src/dashboardWebView/components/SnippetsView/SnippetForm.tsx +++ b/src/dashboardWebView/components/SnippetsView/SnippetForm.tsx @@ -1,11 +1,13 @@ import { Messenger } from '@estruyf/vscode/dist/client'; -import { ChevronDownIcon } from '@heroicons/react/outline'; import * as React from 'react'; import { useCallback, useEffect, useImperativeHandle, useMemo, useState } from 'react'; import { useRecoilValue } from 'recoil'; +import { SnippetVariables } from '../../../constants'; import { Choice, SnippetParser, Variable, VariableResolver } from '../../../helpers/SnippetParser'; +import { SnippetField } from '../../../models'; import { DashboardMessage } from '../../DashboardMessage'; import { ViewDataSelector } from '../../state'; +import { SnippetInputField } from './SnippetInputField'; export interface ISnippetFormProps { @@ -17,14 +19,6 @@ export interface SnippetFormHandle { onSave: () => void; } -interface SnippetField { - name: string; - value: string; - type: 'text' | 'select'; - tmString: string; - options?: string[]; -} - const SnippetForm: React.ForwardRefRenderFunction = ({ snippet, selection }, ref) => { const viewData = useRecoilValue(ViewDataSelector); const [ fields, setFields ] = useState([]); @@ -33,8 +27,8 @@ const SnippetForm: React.ForwardRefRenderFunction prevFields.map(f => f.name === field.name ? { ...f, value } : f)); }, [setFields]); - const insertSelectionValue = useCallback((fieldName: string) => { - if (selection && fieldName === 'selection') { + const insertSelectionValue = useCallback((value: string) => { + if (selection && value === SnippetVariables.FM_SELECTED_TEXT) { return selection; } @@ -59,6 +53,22 @@ const SnippetForm: React.ForwardRefRenderFunction { + if (fieldName === SnippetVariables.FM_SELECTED_TEXT) { + return 'Selected Text'; + } + + if (fieldName.startsWith(SnippetVariables.FM_TEXT)) { + return fieldName.replace(SnippetVariables.FM_TEXT, ''); + } + + if (fieldName.startsWith(SnippetVariables.FM_MULTILINE)) { + return fieldName.replace(SnippetVariables.FM_MULTILINE, ''); + } + + return fieldName; + } + useImperativeHandle(ref, () => ({ onSave() { if (!snippetBody) { @@ -73,6 +83,16 @@ const SnippetForm: React.ForwardRefRenderFunction { + // Defines the type of field that needs to be rendered + const getFieldType = (fieldName: string) => { + if (fieldName.startsWith(SnippetVariables.FM_MULTILINE)) { + return 'textarea'; + } + + return 'text'; + } + + // Get all placeholder variables from the snippet const snippetParser = new SnippetParser(); const body = typeof snippet.body === "string" ? snippet.body : snippet.body.join(`\n`); @@ -84,16 +104,16 @@ const SnippetForm: React.ForwardRefRenderFunction o.value); @@ -107,9 +127,9 @@ const SnippetForm: React.ForwardRefRenderFunction
- { - field.type === 'select' ? ( -
- - - -
- ) : ( -