-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddSnippetDialog.js
147 lines (136 loc) · 5.25 KB
/
AddSnippetDialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, { useState } from 'react';
export function AddSnippetDialog({ isOpen, onClose, onSave }) {
const [snippet, setSnippet] = useState({
name: '',
category: 'Custom',
codeTemplate: '',
defaultParams: {},
paramControls: []
});
const [currentParam, setCurrentParam] = useState({
name: '',
type: 'slider',
min: 0,
max: 1,
step: 0.1
});
if (!isOpen) return null;
const handleSave = () => {
if (!snippet.name || !snippet.codeTemplate) {
alert('Please fill in all required fields');
return;
}
onSave(snippet);
onClose();
};
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-[600px] max-h-[80vh] overflow-y-auto">
<div className="p-4 border-b dark:border-gray-700">
<h2 className="text-lg font-semibold text-gray-800 dark:text-white">Add Custom Snippet</h2>
</div>
<div className="p-4 space-y-4">
{/* Name Input */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Name
</label>
<input
type="text"
value={snippet.name}
onChange={(e) => setSnippet(prev => ({ ...prev, name: e.target.value }))}
className="w-full p-2 border rounded-md dark:bg-gray-700 dark:border-gray-600 dark:text-gray-100"
placeholder="Enter snippet name"
/>
</div>
{/* Code Template */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Code Template
</label>
<textarea
value={snippet.codeTemplate}
onChange={(e) => setSnippet(prev => ({ ...prev, codeTemplate: e.target.value }))}
className="w-full p-2 border rounded-md h-32 font-mono dark:bg-gray-700 dark:border-gray-600 dark:text-gray-100"
placeholder="Enter shader code here. Use {{paramName}} for parameters"
/>
</div>
{/* Parameters Section */}
<div className="border-t pt-4 dark:border-gray-700">
<h3 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Parameters</h3>
{/* Parameter List */}
<div className="space-y-2 mb-4">
{snippet.paramControls.map((param, index) => (
<div key={index} className="flex items-center justify-between p-2 border rounded-md dark:border-gray-600">
<span className="text-sm text-gray-700 dark:text-gray-300">{param.name}</span>
<button
onClick={() => {
setSnippet(prev => ({
...prev,
paramControls: prev.paramControls.filter((_, i) => i !== index)
}));
}}
className="text-red-500 hover:text-red-600"
>
Remove
</button>
</div>
))}
</div>
{/* Add Parameter Form */}
<div className="p-2 border rounded-md dark:border-gray-600">
<div className="grid grid-cols-2 gap-2">
<input
placeholder="Parameter name"
value={currentParam.name}
onChange={(e) => setCurrentParam(prev => ({ ...prev, name: e.target.value }))}
className="p-1 border rounded dark:bg-gray-700 dark:border-gray-600 dark:text-gray-100"
/>
<button
onClick={() => {
if (currentParam.name) {
setSnippet(prev => ({
...prev,
paramControls: [...prev.paramControls, { ...currentParam }],
defaultParams: {
...prev.defaultParams,
[currentParam.name]: currentParam.min
}
}));
setCurrentParam({
name: '',
type: 'slider',
min: 0,
max: 1,
step: 0.1
});
}
}}
disabled={!currentParam.name}
className="bg-blue-500 text-white rounded px-2 py-1 disabled:opacity-50 hover:bg-blue-600"
>
Add Parameter
</button>
</div>
</div>
</div>
</div>
{/* Footer */}
<div className="p-4 border-t dark:border-gray-700 flex justify-end space-x-2">
<button
onClick={onClose}
className="px-4 py-2 text-gray-600 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200"
>
Cancel
</button>
<button
onClick={handleSave}
className="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600"
>
Save Snippet
</button>
</div>
</div>
</div>
);
}