mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-07-04 00:41:00 +02:00
Added schema + array and string type
This commit is contained in:
@@ -43,7 +43,7 @@ export const Item: React.FunctionComponent<IItemProps> = ({ title, snippet }: Re
|
||||
const onOpenEdit = useCallback(() => {
|
||||
setSnippetTitle(title);
|
||||
setSnippetDescription(snippet.description);
|
||||
setSnippetOriginalBody(snippet.body.join(`\n`));
|
||||
setSnippetOriginalBody(typeof snippet.body === "string" ? snippet.body : snippet.body.join(`\n`));
|
||||
setShowEditDialog(true);
|
||||
}, [snippet]);
|
||||
|
||||
@@ -54,9 +54,10 @@ export const Item: React.FunctionComponent<IItemProps> = ({ title, snippet }: Re
|
||||
}
|
||||
|
||||
const snippets = Object.assign({}, settings?.snippets || {});
|
||||
const snippetLines = snippetOriginalBody.split("\n");
|
||||
const snippetContents = {
|
||||
description: snippetDescription || '',
|
||||
body: snippetOriginalBody.split("\n")
|
||||
body: snippetLines.length === 1 ? snippetLines[0] : snippetLines
|
||||
};
|
||||
|
||||
if (title === snippetTitle) {
|
||||
|
||||
@@ -42,7 +42,7 @@ const SnippetForm: React.ForwardRefRenderFunction<SnippetFormHandle, ISnippetFor
|
||||
}, [selection]);
|
||||
|
||||
const snippetBody = useMemo(() => {
|
||||
let body = snippet.body.join(`\n`);
|
||||
let body = typeof snippet.body === "string" ? snippet.body : snippet.body.join(`\n`);
|
||||
|
||||
for (const field of fields) {
|
||||
body = body.replace(field.tmString, field.value);
|
||||
@@ -51,6 +51,14 @@ const SnippetForm: React.ForwardRefRenderFunction<SnippetFormHandle, ISnippetFor
|
||||
return body;
|
||||
}, [fields, selection]);
|
||||
|
||||
const shouldShowField = (fieldName: string, idx: number, allFields: SnippetField[]) => {
|
||||
const crntField = allFields.findIndex(f => f.name === fieldName);
|
||||
if (crntField < idx) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
onSave() {
|
||||
if (!snippetBody) {
|
||||
@@ -66,7 +74,7 @@ const SnippetForm: React.ForwardRefRenderFunction<SnippetFormHandle, ISnippetFor
|
||||
|
||||
useEffect(() => {
|
||||
const snippetParser = new SnippetParser();
|
||||
const body = snippet.body.join(`\n`);
|
||||
const body = typeof snippet.body === "string" ? snippet.body : snippet.body.join(`\n`);
|
||||
|
||||
const parsed = snippetParser.parse(body);
|
||||
const placeholders = parsed.placeholderInfo.all;
|
||||
@@ -76,6 +84,8 @@ const SnippetForm: React.ForwardRefRenderFunction<SnippetFormHandle, ISnippetFor
|
||||
for (const placeholder of placeholders) {
|
||||
const tmString = placeholder.toTextmateString();
|
||||
|
||||
console.log(tmString)
|
||||
|
||||
if (placeholder.children.length === 0) {
|
||||
allFields.push({
|
||||
type: 'text',
|
||||
@@ -118,40 +128,42 @@ const SnippetForm: React.ForwardRefRenderFunction<SnippetFormHandle, ISnippetFor
|
||||
|
||||
<div className='space-y-4 mt-4'>
|
||||
{
|
||||
fields.map((field: SnippetField, index: number) => (
|
||||
<div key={index}>
|
||||
<label htmlFor={field.name} className="block text-sm font-medium capitalize">
|
||||
{field.name}
|
||||
</label>
|
||||
<div className="mt-1">
|
||||
{
|
||||
field.type === 'select' ? (
|
||||
<div className='relative'>
|
||||
<select
|
||||
name={field.name}
|
||||
value={field.value || ""}
|
||||
className="focus:outline-none block w-full sm:text-sm border-gray-300 text-vulcan-500"
|
||||
onChange={e => onTextChange(field, e.target.value)}>
|
||||
{
|
||||
field.options?.map((option: string, index: number) => (
|
||||
<option key={index} value={option}>{option}</option>
|
||||
))
|
||||
}
|
||||
</select>
|
||||
fields.map((field: SnippetField, index: number, allFields: SnippetField[]) => (
|
||||
shouldShowField(field.name, index, allFields) && (
|
||||
<div key={index}>
|
||||
<label htmlFor={field.name} className="block text-sm font-medium capitalize">
|
||||
{field.name}
|
||||
</label>
|
||||
<div className="mt-1">
|
||||
{
|
||||
field.type === 'select' ? (
|
||||
<div className='relative'>
|
||||
<select
|
||||
name={field.name}
|
||||
value={field.value || ""}
|
||||
className="focus:outline-none block w-full sm:text-sm border-gray-300 text-vulcan-500"
|
||||
onChange={e => onTextChange(field, e.target.value)}>
|
||||
{
|
||||
field.options?.map((option: string, index: number) => (
|
||||
<option key={index} value={option}>{option}</option>
|
||||
))
|
||||
}
|
||||
</select>
|
||||
|
||||
<ChevronDownIcon className="absolute top-3 right-2 w-4 h-4 text-gray-500" />
|
||||
</div>
|
||||
) : (
|
||||
<textarea
|
||||
name={field.name}
|
||||
value={field.value || ""}
|
||||
className="focus:outline-none block w-full sm:text-sm border-gray-300 text-vulcan-500"
|
||||
onChange={(e) => onTextChange(field, e.currentTarget.value)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
<ChevronDownIcon className="absolute top-3 right-2 w-4 h-4 text-gray-500" />
|
||||
</div>
|
||||
) : (
|
||||
<textarea
|
||||
name={field.name}
|
||||
value={field.value || ""}
|
||||
className="focus:outline-none block w-full sm:text-sm border-gray-300 text-vulcan-500"
|
||||
onChange={(e) => onTextChange(field, e.currentTarget.value)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
))
|
||||
}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user